1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
#include "UnityPrefix.h"
#include "AABB.h"
#include "Runtime/Math/Quaternion.h"
const AABB AABB::zero = AABB(Vector3f::zero, Vector3f::zero);
void CalculateClosestPoint (const Vector3f& rkPoint, const AABB& rkBox, Vector3f& outPoint, float& outSqrDistance)
{
// compute coordinates of point in box coordinate system
Vector3f kClosest = rkPoint - rkBox.GetCenter();
// project test point onto box
float fSqrDistance = 0.0f;
float fDelta;
for (int i=0;i<3;i++)
{
if ( kClosest[i] < -rkBox.GetExtent (i) )
{
fDelta = kClosest[i] + rkBox.GetExtent (i);
fSqrDistance += fDelta * fDelta;
kClosest[i] = -rkBox.GetExtent (i);
}
else if ( kClosest[i] > rkBox.GetExtent(i) )
{
fDelta = kClosest[i] - rkBox.GetExtent (i);
fSqrDistance += fDelta * fDelta;
kClosest[i] = rkBox.GetExtent (i);
}
}
// Inside
if (fSqrDistance == 0.0F)
{
outPoint = rkPoint;
outSqrDistance = 0.0F;
}
// Outside
else
{
outPoint = kClosest + rkBox.GetCenter();
outSqrDistance = fSqrDistance;
}
}
// Sphere-AABB distance, Arvo's algorithm
float CalculateSqrDistance (const Vector3f& rkPoint, const AABB& rkBox)
{
Vector3f closest = rkPoint - rkBox.GetCenter();
float sqrDistance = 0.0f;
for (int i = 0; i < 3; ++i)
{
float clos = closest[i];
float ext = rkBox.GetExtent(i);
if (clos < -ext)
{
float delta = clos + ext;
sqrDistance += delta * delta;
closest[i] = -ext;
}
else if (clos > ext)
{
float delta = clos - ext;
sqrDistance += delta * delta;
closest[i] = ext;
}
}
return sqrDistance;
}
void AABB::GetVertices (Vector3f* outVertices) const
{
outVertices[0] = m_Center + Vector3f (-m_Extent.x, -m_Extent.y, -m_Extent.z);
outVertices[1] = m_Center + Vector3f (+m_Extent.x, -m_Extent.y, -m_Extent.z);
outVertices[2] = m_Center + Vector3f (-m_Extent.x, +m_Extent.y, -m_Extent.z);
outVertices[3] = m_Center + Vector3f (+m_Extent.x, +m_Extent.y, -m_Extent.z);
outVertices[4] = m_Center + Vector3f (-m_Extent.x, -m_Extent.y, +m_Extent.z);
outVertices[5] = m_Center + Vector3f (+m_Extent.x, -m_Extent.y, +m_Extent.z);
outVertices[6] = m_Center + Vector3f (-m_Extent.x, +m_Extent.y, +m_Extent.z);
outVertices[7] = m_Center + Vector3f (+m_Extent.x, +m_Extent.y, +m_Extent.z);
}
void MinMaxAABB::GetVertices( Vector3f outVertices[8] ) const
{
// 7-----6
// / /|
// 3-----2 |
// | 4 | 5
// | |/
// 0-----1
outVertices[0].Set( m_Min.x, m_Min.y, m_Min.z );
outVertices[1].Set( m_Max.x, m_Min.y, m_Min.z );
outVertices[2].Set( m_Max.x, m_Max.y, m_Min.z );
outVertices[3].Set( m_Min.x, m_Max.y, m_Min.z );
outVertices[4].Set( m_Min.x, m_Min.y, m_Max.z );
outVertices[5].Set( m_Max.x, m_Min.y, m_Max.z );
outVertices[6].Set( m_Max.x, m_Max.y, m_Max.z );
outVertices[7].Set( m_Min.x, m_Max.y, m_Max.z );
}
bool AABB::IsInside (const Vector3f& inPoint) const
{
if (inPoint[0] < m_Center[0] - m_Extent[0])
return false;
if (inPoint[0] > m_Center[0] + m_Extent[0])
return false;
if (inPoint[1] < m_Center[1] - m_Extent[1])
return false;
if (inPoint[1] > m_Center[1] + m_Extent[1])
return false;
if (inPoint[2] < m_Center[2] - m_Extent[2])
return false;
if (inPoint[2] > m_Center[2] + m_Extent[2])
return false;
return true;
}
void AABB::Encapsulate (const Vector3f& inPoint) {
MinMaxAABB temp = *this;
temp.Encapsulate (inPoint);
FromMinMaxAABB (temp);
}
bool MinMaxAABB::IsInside (const Vector3f& inPoint) const
{
if (inPoint[0] < m_Min[0])
return false;
if (inPoint[0] > m_Max[0])
return false;
if (inPoint[1] < m_Min[1])
return false;
if (inPoint[1] > m_Max[1])
return false;
if (inPoint[2] < m_Min[2])
return false;
if (inPoint[2] > m_Max[2])
return false;
return true;
}
MinMaxAABB AddAABB (const MinMaxAABB& lhs, const MinMaxAABB& rhs)
{
MinMaxAABB minMax;
if (lhs.IsValid())
minMax = lhs;
if (rhs.IsValid())
{
minMax.Encapsulate (rhs.GetMax ());
minMax.Encapsulate (rhs.GetMin ());
}
return minMax;
}
inline Vector3f RotateExtents (const Vector3f& extents, const Matrix3x3f& rotation)
{
Vector3f newExtents;
for (int i=0;i<3;i++)
newExtents[i] = Abs (rotation.Get (i, 0) * extents.x) + Abs (rotation.Get (i, 1) * extents.y) + Abs (rotation.Get (i, 2) * extents.z);
return newExtents;
}
inline Vector3f RotateExtents (const Vector3f& extents, const Matrix4x4f& rotation)
{
Vector3f newExtents;
for (int i=0;i<3;i++)
newExtents[i] = Abs (rotation.Get (i, 0) * extents.x) + Abs (rotation.Get (i, 1) * extents.y) + Abs (rotation.Get (i, 2) * extents.z);
return newExtents;
}
void TransformAABB (const AABB& aabb, const Vector3f& position, const Quaternionf& rotation, AABB& result)
{
Matrix3x3f m;
QuaternionToMatrix (rotation, m);
Vector3f extents = RotateExtents (aabb.GetExtent (), m);
Vector3f center = m.MultiplyPoint3 (aabb.GetCenter ());
center += position;
result.SetCenterAndExtent( center, extents );
}
void TransformAABB (const AABB& aabb, const Matrix4x4f& transform, AABB& result)
{
Vector3f extents = RotateExtents (aabb.GetExtent (), transform);
Vector3f center = transform.MultiplyPoint3 (aabb.GetCenter ());
result.SetCenterAndExtent( center, extents );
}
void TransformAABBSlow (const AABB& aabb, const Matrix4x4f& transform, AABB& result)
{
MinMaxAABB transformed;
transformed.Init ();
Vector3f v[8];
aabb.GetVertices (v);
for (int i=0;i<8;i++)
transformed.Encapsulate (transform.MultiplyPoint3 (v[i]));
result = transformed;
}
void InverseTransformAABB (const AABB& aabb, const Vector3f& position, const Quaternionf& rotation, AABB& result)
{
Matrix3x3f m;
QuaternionToMatrix (Inverse (rotation), m);
Vector3f extents = RotateExtents (aabb.GetExtent (), m);
Vector3f center = aabb.GetCenter () - position;
center = m.MultiplyPoint3 (center);
result.SetCenterAndExtent( center, extents );
}
bool IsContainedInAABB (const AABB& inside, const AABB& bigBounds)
{
bool outside = false;
outside |= inside.m_Center[0] - inside.m_Extent[0] < bigBounds.m_Center[0] - bigBounds.m_Extent[0];
outside |= inside.m_Center[0] + inside.m_Extent[0] > bigBounds.m_Center[0] + bigBounds.m_Extent[0];
outside |= inside.m_Center[1] - inside.m_Extent[1] < bigBounds.m_Center[1] - bigBounds.m_Extent[1];
outside |= inside.m_Center[1] + inside.m_Extent[1] > bigBounds.m_Center[1] + bigBounds.m_Extent[1];
outside |= inside.m_Center[2] - inside.m_Extent[2] < bigBounds.m_Center[2] - bigBounds.m_Extent[2];
outside |= inside.m_Center[2] + inside.m_Extent[2] > bigBounds.m_Center[2] + bigBounds.m_Extent[2];
return !outside;
}
|