TDME2  1.9.200
BoundingBox.cpp
Go to the documentation of this file.
2 
3 #include <vector>
4 
5 #include <tdme/tdme.h>
8 #include <tdme/math/Matrix4x4.h>
9 #include <tdme/math/Vector3.h>
10 
11 using std::vector;
12 
18 
19 const array<int32_t, 3> BoundingBox::FACE0_INDICES = {{ 0, 4, 7 }};
20 const array<int32_t, 3> BoundingBox::FACE1_INDICES = {{ 7, 3, 0 }};
21 const array<int32_t, 3> BoundingBox::FACE2_INDICES = {{ 6, 5, 1 }};
22 const array<int32_t, 3> BoundingBox::FACE3_INDICES = {{ 1, 2, 6 }};
23 const array<int32_t, 3> BoundingBox::FACE4_INDICES = {{ 5, 4, 0 }};
24 const array<int32_t, 3> BoundingBox::FACE5_INDICES = {{ 0, 1, 5 }};
25 const array<int32_t, 3> BoundingBox::FACE6_INDICES = {{ 3, 7, 6 }};
26 const array<int32_t, 3> BoundingBox::FACE7_INDICES = {{ 6, 2, 3 }};
27 const array<int32_t, 3> BoundingBox::FACE8_INDICES = {{ 2, 1, 0 }};
28 const array<int32_t, 3> BoundingBox::FACE9_INDICES = {{ 0, 3, 2 }};
29 const array<int32_t, 3> BoundingBox::FACE10_INDICES = {{ 4, 5, 6 }};
30 const array<int32_t, 3> BoundingBox::FACE11_INDICES = {{ 6, 7, 4 }};
31 const array<array<int32_t,3>,12> BoundingBox::facesVerticesIndexes =
32 {{
33  FACE0_INDICES, FACE1_INDICES, FACE2_INDICES, FACE3_INDICES,
34  FACE4_INDICES, FACE5_INDICES, FACE6_INDICES, FACE7_INDICES,
35  FACE8_INDICES, FACE9_INDICES, FACE10_INDICES, FACE11_INDICES
36 }};
37 
38 BoundingBox::BoundingBox()
39 {
40  vertices.resize(8);
41  update();
42 }
43 
45 {
46  vertices.resize(8);
47  this->min.set(boundingBox->min);
48  this->max.set(boundingBox->max);
49  update();
50 }
51 
53 {
54  vertices.resize(8);
55  min = obb->getCenter();
56  max = obb->getCenter();
57  for (auto& vertex: obb->getVertices()) {
58  extend(vertex);
59  }
60 }
61 
62 BoundingBox::BoundingBox(const Vector3& min, const Vector3& max)
63 {
64  vertices.resize(8);
65  this->min.set(min);
66  this->max.set(max);
67  update();
68 }
69 
71 {
72  min = boundingBox->min;
73  max = boundingBox->max;
74  vertices = boundingBox->vertices;
75  center = boundingBox->center;
76  dimensions = boundingBox->dimensions;
77 }
78 
80 {
81  // apply transform from original vertices to local vertices
82  auto _vertices = boundingBox->getVertices();
83  for (auto i = 0; i < vertices.size(); i++) {
84  vertices[i] = transformMatrix.multiply(_vertices[i]);
85  }
86  // determine axis aligned bounding box constraints based on local vertices
87  float minX = vertices[0][0], minY = vertices[0][1], minZ = vertices[0][2];
88  float maxX = vertices[0][0], maxY = vertices[0][1], maxZ = vertices[0][2];
89  for (auto vertexIndex = 1; vertexIndex < vertices.size(); vertexIndex++) {
90  const auto& vertex = vertices[vertexIndex];
91  if (vertex[0] < minX) minX = vertex[0];
92  if (vertex[1] < minY) minY = vertex[1];
93  if (vertex[2] < minZ) minZ = vertex[2];
94  if (vertex[0] > maxX) maxX = vertex[0];
95  if (vertex[1] > maxY) maxY = vertex[1];
96  if (vertex[2] > maxZ) maxZ = vertex[2];
97  }
98  // set up new aabb
99  min.set(minX, minY, minZ);
100  max.set(maxX, maxY, maxZ);
101  // compute new vertices based on aabb constraints
102  update();
103 }
104 
106  // near, left, top
107  vertices[0].set(min[0], min[1], min[2]);
108  // near, right, top
109  vertices[1].set(max[0], min[1], min[2]);
110  // near, right, bottom
111  vertices[2].set(max[0], max[1], min[2]);
112  // near, left, bottom
113  vertices[3].set(min[0], max[1], min[2]);
114  // far, left, top
115  vertices[4].set(min[0], min[1], max[2]);
116  // far, right, top
117  vertices[5].set(max[0], min[1], max[2]);
118  // far, right, bottom
119  vertices[6].set(max[0], max[1], max[2]);
120  // far, left, bottom
121  vertices[7].set(min[0], max[1], max[2]);
122  center.set(min).add(max).scale(0.5f);
124 }
125 
126 
Transform which contain scale, rotations and translation.
Definition: Transform.h:29
Axis aligned bounding box used for frustum, this is not directly connectable with physics engine.
Definition: BoundingBox.h:26
void fromBoundingVolume(BoundingBox *original)
Set up this bounding volume from given bounding volume.
Definition: BoundingBox.cpp:70
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.
Oriented bounding box physics primitive.
const array< Vector3, 8 > getVertices() const
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
Vector3 multiply(const Vector3 &vector3) const
Multiplies this matrix with vector3.
Definition: Matrix4x4.h:225
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
Vector3 & add(float scalar)
Adds a scalar.
Definition: Vector3.h:153
Vector3 & sub(float scalar)
Subtracts a scalar.
Definition: Vector3.h:177
Vector3 & scale(float scalar)
Scales by scalar.
Definition: Vector3.h:201
Vector3 & set(float x, float y, float z)
Sets this vector3 by its components.
Definition: Vector3.h:70