Overte C++ Documentation
AACube.h
1 //
2 // AACube.h
3 // libraries/shared/src
4 //
5 // Created by Brad Hefta-Gaub on 04/11/13.
6 // Copyright 2013 High Fidelity, Inc.
7 //
8 // Originally from lighthouse3d. Modified to utilize glm::vec3 and clean up to our coding standards
9 // Simple axis aligned box class.
10 //
11 // Distributed under the Apache License, Version 2.0.
12 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
13 //
14 
15 #ifndef hifi_AACube_h
16 #define hifi_AACube_h
17 
18 #include <glm/glm.hpp>
19 
20 #include <QDebug>
21 
22 #include "BoxBase.h"
23 #include "SerDes.h"
24 
25 class AABox;
26 class Extents;
27 
28 class AACube {
29 
30 public:
31  AACube(const AABox& other);
32  AACube(const Extents& other);
33  AACube(const glm::vec3& corner, float size);
34  AACube();
35  ~AACube() {};
36 
37  void setBox(const glm::vec3& corner, float scale);
38  glm::vec3 getFarthestVertex(const glm::vec3& normal) const; // return vertex most parallel to normal
39  glm::vec3 getNearestVertex(const glm::vec3& normal) const; // return vertex most anti-parallel to normal
40  void scale(float scale);
41  const glm::vec3& getCorner() const { return _corner; }
42  float getScale() const { return _scale; }
43  glm::vec3 getDimensions() const { return glm::vec3(_scale,_scale,_scale); }
44  float getLargestDimension() const { return _scale; }
45 
46  glm::vec3 calcCenter() const;
47  glm::vec3 calcTopFarLeft() const;
48  glm::vec3 getVertex(BoxVertex vertex) const;
49 
50  const glm::vec3& getMinimumPoint() const { return _corner; }
51  glm::vec3 getMaximumPoint() const { return calcTopFarLeft(); }
52 
53  bool contains(const glm::vec3& point) const;
54  bool contains(const AACube& otherCube) const;
55  bool touches(const AACube& otherCube) const;
56  bool contains(const AABox& otherBox) const;
57  bool touches(const AABox& otherBox) const;
58  bool expandedContains(const glm::vec3& point, float expansion) const;
59  bool expandedIntersectsSegment(const glm::vec3& start, const glm::vec3& end, float expansion) const;
60  bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, const glm::vec3& invDirection,
61  float& distance, BoxFace& face, glm::vec3& surfaceNormal) const;
62  bool findParabolaIntersection(const glm::vec3& origin, const glm::vec3& velocity, const glm::vec3& acceleration,
63  float& parabolicDistance, BoxFace& face, glm::vec3& surfaceNormal) const;
64  bool touchesSphere(const glm::vec3& center, float radius) const;
65  bool findSpherePenetration(const glm::vec3& center, float radius, glm::vec3& penetration) const;
66  bool findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius, glm::vec3& penetration) const;
67 
68  AABox clamp(const glm::vec3& min, const glm::vec3& max) const;
69  AABox clamp(float min, float max) const;
70 
71  AACube& operator += (const glm::vec3& point);
72 
73  bool containsNaN() const;
74 
75 private:
76  glm::vec3 getClosestPointOnFace(const glm::vec3& point, BoxFace face) const;
77  glm::vec3 getClosestPointOnFace(const glm::vec4& origin, const glm::vec4& direction, BoxFace face) const;
78  glm::vec4 getPlane(BoxFace face) const;
79 
80  static BoxFace getOppositeFace(BoxFace face);
81 
82  glm::vec3 _corner;
83  float _scale;
84 
85  friend DataSerializer& operator<<(DataSerializer &ser, const AACube &cube);
86  friend DataDeserializer& operator>>(DataDeserializer &des, AACube &cube);
87 
88 };
89 
90 inline bool operator==(const AACube& a, const AACube& b) {
91  return a.getCorner() == b.getCorner() && a.getScale() == b.getScale();
92 }
93 
94 inline bool operator!=(const AACube& a, const AACube& b) {
95  return a.getCorner() != b.getCorner() || a.getScale() != b.getScale();
96 }
97 
98 inline QDebug operator<<(QDebug debug, const AACube& cube) {
99  debug << "AACube[ ("
100  << cube.getCorner().x << "," << cube.getCorner().y << "," << cube.getCorner().z << " ) to ("
101  << cube.calcTopFarLeft().x << "," << cube.calcTopFarLeft().y << "," << cube.calcTopFarLeft().z << ") size: ("
102  << cube.getDimensions().x << "," << cube.getDimensions().y << "," << cube.getDimensions().z << ")"
103  << "]";
104  return debug;
105 }
106 
107 inline DataSerializer& operator<<(DataSerializer &ser, const AACube &cube) {
108  ser << cube._corner;
109  ser << cube._scale;
110  return ser;
111 }
112 
113 inline DataDeserializer& operator>>(DataDeserializer &des, AACube &cube) {
114  des >> cube._corner;
115  des >> cube._scale;
116  return des;
117 }
118 
119 #endif // hifi_AACube_h
Data deserializer.
Definition: SerDes.h:573
Data serializer.
Definition: SerDes.h:61