-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntrAabb3Cyl3.h
285 lines (259 loc) · 12.9 KB
/
IntrAabb3Cyl3.h
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Sebastian Wouters
// Copyright (c) 2021
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
// Alternative AABB-CYL collision detection
#pragma once
#include <Mathematics/AlignedBox.h>
#include <Mathematics/Cylinder3.h>
namespace gte
{
template <typename Real>
class Solver
{
public:
struct Result
{
bool intersect;
};
private:
template <size_t size>
uint8_t convexHull2dSimple(std::array<uint8_t, size>& hull, const uint8_t numPoints, const std::array<Vector<2, Real>, size>& points) const
{
if (numPoints <= 2)
{
for (uint8_t idx = 0; idx < numPoints; ++idx) { hull[idx] = idx; }
return numPoints;
}
// Compute the convex hull counter clockwise
// Use Andrew's algorithm (see section 3.9.1 of Christer, Real-time collision detection (2005), ISBN 1-55860-732-3)
std::array<uint8_t, size> sorted = {};
for (uint8_t idx = 0; idx < numPoints; ++idx) { sorted[idx] = idx; }
std::sort(sorted.begin(), sorted.begin() + numPoints,
[&points](const uint8_t left, const uint8_t right)
{
return (points[left][0] < points[right][0]) ||
((points[left][0] == points[right][0]) && (points[left][1] < points[right][1]));
});
const uint8_t jMin = sorted[0];
const uint8_t jMax = sorted[numPoints - 1];
// Lower half: points below line(points[jMin], points[jMax])
hull[0] = jMin;
uint8_t last = 0;
for (uint8_t next = 1; next < numPoints - 1; ++next)
{
const uint8_t jNext = sorted[next];
const uint8_t jLast = hull[last];
Vector<2, Real> v1 = points[jMax] - points[jLast];
Vector<2, Real> v2 = points[jNext] - points[jLast];
// if points[jNext] below line(points[jLast], points[jMax]): insert jNext in hull
if (v1[0] * v2[1] < v1[1] * v2[0])
{
// points[jMin] must belong to hull: do not check erasing jMin
bool check = last != 0;
while (check)
{
const uint8_t j1 = hull[last];
const uint8_t j0 = hull[last - 1];
v1 = points[j1] - points[j0];
v2 = points[jNext] - points[j0];
// if points[jNext] below or on line(points[j0], points[j1]): erase j1 from hull
check = (v1[0] * v2[1] <= v1[1] * v2[0]) && (--last != 0);
}
hull[++last] = jNext;
// if points[jNext] below line(points[jMin], points[jMax]), skip check whether above:
sorted[next] = UINT8_MAX;
}
}
// Upper half: points above line(points[jMin], points[jMax])
hull[++last] = jMax;
const uint8_t limit = last;
for (uint8_t next = numPoints - 2; next > 0; --next)
{
const uint8_t jNext = sorted[next];
if (jNext != UINT8_MAX)
{
const uint8_t jLast = hull[last];
Vector<2, Real> v1 = points[jMin] - points[jLast];
Vector<2, Real> v2 = points[jNext] - points[jLast];
// if points[jNext] above line(points[jLast], points[jMin]): insert jNext in hull
if (v1[0] * v2[1] < v1[1] * v2[0])
{
// points[jMax] must belong to hull: do not check erasing jMax
bool check = last != limit;
while (check)
{
const uint8_t j1 = hull[last];
const uint8_t j0 = hull[last - 1];
v1 = points[j1] - points[j0];
v2 = points[jNext] - points[j0];
// if points[jNext] above or on line(points[j0], points[j1]): erase j1 from hull
check = (v1[0] * v2[1] <= v1[1] * v2[0]) && (--last != limit);
}
hull[++last] = jNext;
}
}
}
return ++last;
}
public:
Result operator()(AlignedBox3<Real> const& box, Cylinder3<Real> const& cyl) const
{
constexpr Result disjoint = { false };
constexpr Result intersects = { true };
struct Vertex
{
const Vector<3, Real> coord;
const Real proj;
Vertex(const std::array<Real, 3>& in, const Vector<3, Real>& axis)
: coord(in)
, proj(Dot(coord, axis)) {}
};
/*
z
^ 6-----7
| /| /|
|/ | / |
4--+--5 |
| 2--+--3
| / | /
|/ |/
0-----1----> x
*/
const std::array<Vertex, 8> vertices = {
Vertex({ box.min[0], box.min[1], box.min[2] }, cyl.axis.direction),
Vertex({ box.max[0], box.min[1], box.min[2] }, cyl.axis.direction),
Vertex({ box.min[0], box.max[1], box.min[2] }, cyl.axis.direction),
Vertex({ box.max[0], box.max[1], box.min[2] }, cyl.axis.direction),
Vertex({ box.min[0], box.min[1], box.max[2] }, cyl.axis.direction),
Vertex({ box.max[0], box.min[1], box.max[2] }, cyl.axis.direction),
Vertex({ box.min[0], box.max[1], box.max[2] }, cyl.axis.direction),
Vertex({ box.max[0], box.max[1], box.max[2] }, cyl.axis.direction) };
const Real midAxisCyl = Dot(cyl.axis.direction, cyl.axis.origin);
const Real halfHeight = static_cast<Real>(0.5) * cyl.height;
const Real maxAxisCyl = midAxisCyl + halfHeight;
const Real minAxisCyl = midAxisCyl - halfHeight;
bool allAbove = true;
bool allBelow = true;
for (const Vertex& vtx : vertices)
{
allAbove = allAbove && (vtx.proj > maxAxisCyl);
allBelow = allBelow && (vtx.proj < minAxisCyl);
}
if (allAbove || allBelow)
return disjoint; // cyl.axis.direction is a separating axis
// Compute the vertices of a polyhedron which is the box capped by the cylinder's
// top and bottom planes and project them into a plane perpendicular to the
// cylinder axis direction.
const Vector<3, Real> direction1 = fabs(cyl.axis.direction[0]) > static_cast<Real>(0.9)
? UnitCross({ static_cast<Real>(0.0), static_cast<Real>(1.0), static_cast<Real>(0.0) }, cyl.axis.direction)
: UnitCross({ static_cast<Real>(1.0), static_cast<Real>(0.0), static_cast<Real>(0.0) }, cyl.axis.direction);
const Vector<3, Real> direction2 = Cross(cyl.axis.direction, direction1);
// Suppose N vertices are cropped by the cyl's top plane:
// no. of vertices convex polyhedron
// <= 8 [original]
// - N [cropped]
// + 3 * N [cropped vertex splits in 3 along its edges due to crop plane]
// - 2 * (N-1) [*]
// <= 8 [original] + 2 [independent of N]
// [*] After a first vertex is cropped by a crop plane, each additional vertex cropped
// by that crop plane is attached via one or more of its edges to one or more of
// the already cropped vertices (cropped by that crop plane). The splits accounted
// for by the term 3 * N are hence corrected by this term, as edges in between two
// vertices cropped by that crop plane are removed as well by that crop plane.
// Same consideration for cyl's bottom plane: total no. of vertices <= 8 + 2 + 2 = 12.
std::array<Vector<2, Real>, 12> points;
uint8_t numPoints = 0;
for (uint32_t idx = 0; idx < vertices.size(); ++idx)
{
const Vertex& vtx = vertices[idx];
if (vtx.proj > maxAxisCyl)
{
for (uint32_t shift = 0; shift < 3; ++shift)
{
const Vertex& neighbor = vertices[idx ^ (1U << shift)];
if (neighbor.proj > maxAxisCyl)
continue; // both cropped by maxAxisCyl
const Real denom = vtx.proj - neighbor.proj; // denom > 0.0
if (denom == static_cast<Real>(0.0))
continue; // floating point accuracy check
const Real z = (maxAxisCyl - neighbor.proj) / denom; // 1.0 > z >= 0.0
const Vector<3, Real> point = z * vtx.coord + (static_cast<Real>(1.0) - z) * neighbor.coord;
points[numPoints++] = { Dot(direction1, point), Dot(direction2, point) };
}
}
else if (vtx.proj < minAxisCyl)
{
for (uint32_t shift = 0; shift < 3; ++shift)
{
const Vertex& neighbor = vertices[idx ^ (1U << shift)];
if (neighbor.proj < minAxisCyl)
continue; // both cropped by minAxisCyl
const Real denom = vtx.proj - neighbor.proj; // denom < 0.0
if (denom == static_cast<Real>(0.0))
continue; // floating point accuracy check
const Real z = (minAxisCyl - neighbor.proj) / denom; // 1.0 > z >= 0.0
const Vector<3, Real> point = z * vtx.coord + (static_cast<Real>(1.0) - z) * neighbor.coord;
points[numPoints++] = { Dot(direction1, point), Dot(direction2, point) };
}
}
else
{
points[numPoints++] = { Dot(direction1, vtx.coord), Dot(direction2, vtx.coord) };
}
}
// The polyhedron is convex. Its projection is a convex polygon.
// The convex polygon can be computed as the convex hull of the projected polyhedron's vertices.
// Determine whether the polygon contains a point sufficiently close to the cylinder's center.
const Vector<2, Real> center = { Dot(direction1, cyl.axis.origin), Dot(direction2, cyl.axis.origin) };
const Real radiusSquared = cyl.radius * cyl.radius;
if (numPoints == 0)
return disjoint;
else if (numPoints == 1)
{
Vector<2, Real> diff = points[0] - center;
return Dot(diff, diff) > radiusSquared ? disjoint : intersects;
}
std::array<uint8_t, points.size()> hull;
const uint8_t end = convexHull2dSimple(hull, numPoints, points);
// Compare center's distance w.r.t. hull and/or whether center is enclosed in the hull
bool enclosed = true;
for (uint8_t idx = 0; idx < end; ++idx)
{
const Vector<2, Real>& P = points[hull[idx]];
const Vector<2, Real>& Q = points[hull[(idx + 1) % end]];
const Vector<2, Real> PQ = Q - P;
const Vector<2, Real> PC = center - P;
if (PQ[0] * PC[1] < PQ[1] * PC[0])
enclosed = false;
// point(z) = z * Q + (1 - z) * P
// |point(z) - C|^2 = |z * PQ - PC|^2
const Real PQsq = Dot(PQ, PQ);
const Real PCsq = Dot(PC, PC);
Real value = static_cast<Real>(0.0);
if (PQsq == static_cast<Real>(0.0))
value = PCsq;
else
{
const Real PQdotPC = Dot(PQ, PC);
const Real z = PQdotPC / PQsq;
if (z <= static_cast<Real>(0.0))
value = PCsq;
else if (z >= static_cast<Real>(1.0))
{
const Vector<2, Real> QC = center - Q;
value = Dot(QC, QC);
}
else
value = z * (z * PQsq - static_cast<Real>(2.0) * PQdotPC) + PCsq;
}
if (value <= radiusSquared)
return intersects;
}
if (enclosed)
return intersects;
else
return disjoint;
}
};
}