TDME2  1.9.200
BoundingBox.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
4 #include <vector>
5 
6 #include <tdme/tdme.h>
9 #include <tdme/math/fwd-tdme.h>
10 #include <tdme/math/Matrix4x4.h>
11 #include <tdme/math/Vector3.h>
12 
13 using std::array;
14 using std::vector;
15 
20 
21 /**
22  * Axis aligned bounding box used for frustum, this is not directly connectable with physics engine
23  * @author Andreas Drewke
24  */
26 {
27 
28 private:
29  STATIC_DLL_IMPEXT static const array<int32_t, 3> FACE0_INDICES;
30  STATIC_DLL_IMPEXT static const array<int32_t, 3> FACE1_INDICES;
31  STATIC_DLL_IMPEXT static const array<int32_t, 3> FACE2_INDICES;
32  STATIC_DLL_IMPEXT static const array<int32_t, 3> FACE3_INDICES;
33  STATIC_DLL_IMPEXT static const array<int32_t, 3> FACE4_INDICES;
34  STATIC_DLL_IMPEXT static const array<int32_t, 3> FACE5_INDICES;
35  STATIC_DLL_IMPEXT static const array<int32_t, 3> FACE6_INDICES;
36  STATIC_DLL_IMPEXT static const array<int32_t, 3> FACE7_INDICES;
37  STATIC_DLL_IMPEXT static const array<int32_t, 3> FACE8_INDICES;
38  STATIC_DLL_IMPEXT static const array<int32_t, 3> FACE9_INDICES;
39  STATIC_DLL_IMPEXT static const array<int32_t, 3> FACE10_INDICES;
40  STATIC_DLL_IMPEXT static const array<int32_t, 3> FACE11_INDICES;
41  STATIC_DLL_IMPEXT static const array<array<int32_t,3>,12> facesVerticesIndexes;
44  vector<Vector3> vertices;
47 
48 public:
49  /**
50  * Public constructor
51  */
52  BoundingBox();
53 
54  /**
55  * Public constructor
56  * @param boundingBox bounding box
57  */
58  BoundingBox(BoundingBox* boundingBox);
59 
60  /**
61  * Public constructor
62  * @param obb oriented bounding box
63  */
65 
66  /**
67  * Public constructor
68  * @param min min
69  * @param max max
70  */
71  BoundingBox(const Vector3& min, const Vector3& max);
72 
73  /**
74  * Compute closest point in bounding box of given point
75  * @param point point
76  * @return closest point of given point in bounding box
77  */
79  const auto& pointXYZ = point.getArray();
80  const auto& minXYZ = min.getArray();
81  const auto& maxXYZ = max.getArray();
82  return Vector3(
83  pointXYZ[0] < minXYZ[0]?minXYZ[0]:pointXYZ[0] > maxXYZ[0]?maxXYZ[0]:pointXYZ[0],
84  pointXYZ[1] < minXYZ[1]?minXYZ[1]:pointXYZ[1] > maxXYZ[1]?maxXYZ[1]:pointXYZ[1],
85  pointXYZ[2] < minXYZ[2]?minXYZ[2]:pointXYZ[2] > maxXYZ[2]?maxXYZ[2]:pointXYZ[2]
86  );
87  }
88 
89  /**
90  * @return min x,y,z vertex
91  */
92  inline Vector3& getMin() {
93  return min;
94  }
95 
96  /**
97  * @return max x,y,z vertex
98  */
99  inline Vector3& getMax() {
100  return max;
101  }
102 
103  /**
104  * Returns bounding box vertices
105  * @return vertices
106  */
107  const vector<Vector3>& getVertices() const {
108  return vertices;
109  }
110 
111  /**
112  * @return faces vertices indexes
113  */
114  inline static const array<array<int32_t,3>,12>* getFacesVerticesIndexes() {
115  return &facesVerticesIndexes;
116  }
117 
118  /**
119  * @return center
120  */
121  inline const Vector3& getCenter() const {
122  return center;
123  }
124 
125  /**
126  * @return half extension
127  */
128  inline const Vector3& getDimensions() const {
129  return dimensions;
130  }
131 
132  /**
133  * Set up this bounding volume from given bounding volume
134  * @param original original bounding box
135  */
136  void fromBoundingVolume(BoundingBox* original);
137 
138  /**
139  * Create bounding volume from given original(of same type) with applied transform matrix
140  * @param original original bounding box
141  * @param transformMatrix transform matrix
142  */
143  void fromBoundingVolumeWithTransformMatrix(BoundingBox* original, const Matrix4x4& transformMatrix);
144 
145  /**
146  * Create bounding volume from given original(of same type) with applied transform
147  * @param original original bounding box
148  * @param transform transform
149  */
150  inline void fromBoundingVolumeWithTransform(BoundingBox* original, const Transform& transform) {
152  }
153 
154  /**
155  * Extend bounding box with given bounding box
156  * @param boundingBox bounding box
157  */
158  inline void extend(BoundingBox* boundingBox) {
159  for (auto i = 0; i < 3; i++) {
160  if (boundingBox->getMin()[i] < min[i]) min[i] = boundingBox->getMin()[i];
161  if (boundingBox->getMax()[i] > max[i]) max[i] = boundingBox->getMax()[i];
162  }
163  update();
164  }
165 
166  /**
167  * Extend bounding box with given point
168  * @param point point
169  */
170  inline void extend(const Vector3& point) {
171  for (auto i = 0; i < 3; i++) {
172  if (point[i] < min[i]) min[i] = point[i];
173  if (point[i] > max[i]) max[i] = point[i];
174  }
175  update();
176  }
177 
178  /**
179  * Updates this bounding box
180  */
181  void update();
182 
183 };
Transform which contain scale, rotations and translation.
Definition: Transform.h:29
const Matrix4x4 & getTransformMatrix() const
Definition: Transform.h:169
Axis aligned bounding box used for frustum, this is not directly connectable with physics engine.
Definition: BoundingBox.h:26
static STATIC_DLL_IMPEXT const array< int32_t, 3 > FACE6_INDICES
Definition: BoundingBox.h:35
static STATIC_DLL_IMPEXT const array< int32_t, 3 > FACE1_INDICES
Definition: BoundingBox.h:30
static STATIC_DLL_IMPEXT const array< array< int32_t, 3 >, 12 > facesVerticesIndexes
Definition: BoundingBox.h:41
static STATIC_DLL_IMPEXT const array< int32_t, 3 > FACE10_INDICES
Definition: BoundingBox.h:39
void fromBoundingVolume(BoundingBox *original)
Set up this bounding volume from given bounding volume.
Definition: BoundingBox.cpp:70
static STATIC_DLL_IMPEXT const array< int32_t, 3 > FACE5_INDICES
Definition: BoundingBox.h:34
static STATIC_DLL_IMPEXT const array< int32_t, 3 > FACE2_INDICES
Definition: BoundingBox.h:31
const Vector3 & getCenter() const
Definition: BoundingBox.h:121
void fromBoundingVolumeWithTransform(BoundingBox *original, const Transform &transform)
Create bounding volume from given original(of same type) with applied transform.
Definition: BoundingBox.h:150
static const array< array< int32_t, 3 >, 12 > * getFacesVerticesIndexes()
Definition: BoundingBox.h:114
static STATIC_DLL_IMPEXT const array< int32_t, 3 > FACE11_INDICES
Definition: BoundingBox.h:40
static STATIC_DLL_IMPEXT const array< int32_t, 3 > FACE7_INDICES
Definition: BoundingBox.h:36
static STATIC_DLL_IMPEXT const array< int32_t, 3 > FACE9_INDICES
Definition: BoundingBox.h:38
void extend(const Vector3 &point)
Extend bounding box with given point.
Definition: BoundingBox.h:170
static STATIC_DLL_IMPEXT const array< int32_t, 3 > FACE4_INDICES
Definition: BoundingBox.h:33
static STATIC_DLL_IMPEXT const array< int32_t, 3 > FACE3_INDICES
Definition: BoundingBox.h:32
const Vector3 & getDimensions() const
Definition: BoundingBox.h:128
Vector3 computeClosestPointInBoundingBox(const Vector3 &point)
Compute closest point in bounding box of given point.
Definition: BoundingBox.h:78
void extend(BoundingBox *boundingBox)
Extend bounding box with given bounding box.
Definition: BoundingBox.h:158
const vector< Vector3 > & getVertices() const
Returns bounding box vertices.
Definition: BoundingBox.h:107
void fromBoundingVolumeWithTransformMatrix(BoundingBox *original, const Matrix4x4 &transformMatrix)
Create bounding volume from given original(of same type) with applied transform matrix.
Definition: BoundingBox.cpp:79
void update()
Updates this bounding box.
static STATIC_DLL_IMPEXT const array< int32_t, 3 > FACE0_INDICES
Definition: BoundingBox.h:29
static STATIC_DLL_IMPEXT const array< int32_t, 3 > FACE8_INDICES
Definition: BoundingBox.h:37
Oriented bounding box physics primitive.
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
const array< float, 3 > & getArray() const
Definition: Vector3.h:366
#define STATIC_DLL_IMPEXT
Definition: tdme.h:15