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 
24 class AABox;
25 class Extents;
26 
27 class AACube {
28 
29 public:
30  AACube(const AABox& other);
31  AACube(const Extents& other);
32  AACube(const glm::vec3& corner, float size);
33  AACube();
34  ~AACube() {};
35 
36  void setBox(const glm::vec3& corner, float scale);
37  glm::vec3 getFarthestVertex(const glm::vec3& normal) const; // return vertex most parallel to normal
38  glm::vec3 getNearestVertex(const glm::vec3& normal) const; // return vertex most anti-parallel to normal
39  void scale(float scale);
40  const glm::vec3& getCorner() const { return _corner; }
41  float getScale() const { return _scale; }
42  glm::vec3 getDimensions() const { return glm::vec3(_scale,_scale,_scale); }
43  float getLargestDimension() const { return _scale; }
44 
45  glm::vec3 calcCenter() const;
46  glm::vec3 calcTopFarLeft() const;
47  glm::vec3 getVertex(BoxVertex vertex) const;
48 
49  const glm::vec3& getMinimumPoint() const { return _corner; }
50  glm::vec3 getMaximumPoint() const { return calcTopFarLeft(); }
51 
52  bool contains(const glm::vec3& point) const;
53  bool contains(const AACube& otherCube) const;
54  bool touches(const AACube& otherCube) const;
55  bool contains(const AABox& otherBox) const;
56  bool touches(const AABox& otherBox) const;
57  bool expandedContains(const glm::vec3& point, float expansion) const;
58  bool expandedIntersectsSegment(const glm::vec3& start, const glm::vec3& end, float expansion) const;
59  bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, const glm::vec3& invDirection,
60  float& distance, BoxFace& face, glm::vec3& surfaceNormal) const;
61  bool findParabolaIntersection(const glm::vec3& origin, const glm::vec3& velocity, const glm::vec3& acceleration,
62  float& parabolicDistance, BoxFace& face, glm::vec3& surfaceNormal) const;
63  bool touchesSphere(const glm::vec3& center, float radius) const;
64  bool findSpherePenetration(const glm::vec3& center, float radius, glm::vec3& penetration) const;
65  bool findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius, glm::vec3& penetration) const;
66 
67  AABox clamp(const glm::vec3& min, const glm::vec3& max) const;
68  AABox clamp(float min, float max) const;
69 
70  AACube& operator += (const glm::vec3& point);
71 
72  bool containsNaN() const;
73 
74 private:
75  glm::vec3 getClosestPointOnFace(const glm::vec3& point, BoxFace face) const;
76  glm::vec3 getClosestPointOnFace(const glm::vec4& origin, const glm::vec4& direction, BoxFace face) const;
77  glm::vec4 getPlane(BoxFace face) const;
78 
79  static BoxFace getOppositeFace(BoxFace face);
80 
81  glm::vec3 _corner;
82  float _scale;
83 };
84 
85 inline bool operator==(const AACube& a, const AACube& b) {
86  return a.getCorner() == b.getCorner() && a.getScale() == b.getScale();
87 }
88 
89 inline bool operator!=(const AACube& a, const AACube& b) {
90  return a.getCorner() != b.getCorner() || a.getScale() != b.getScale();
91 }
92 
93 inline QDebug operator<<(QDebug debug, const AACube& cube) {
94  debug << "AACube[ ("
95  << cube.getCorner().x << "," << cube.getCorner().y << "," << cube.getCorner().z << " ) to ("
96  << cube.calcTopFarLeft().x << "," << cube.calcTopFarLeft().y << "," << cube.calcTopFarLeft().z << ") size: ("
97  << cube.getDimensions().x << "," << cube.getDimensions().y << "," << cube.getDimensions().z << ")"
98  << "]";
99  return debug;
100 }
101 
102 
103 #endif // hifi_AACube_h