-
Notifications
You must be signed in to change notification settings - Fork 6
/
BoxIntersection.cpp
211 lines (188 loc) · 5.4 KB
/
BoxIntersection.cpp
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
/*
This file is part of the Geometry library.
Copyright (C) 2007-2012 Benjamin Eikel <[email protected]>
Copyright (C) 2007-2012 Claudius Jähn <[email protected]>
Copyright (C) 2007-2012 Ralf Petring <[email protected]>
This library is subject to the terms of the Mozilla Public License, v. 2.0.
You should have received a copy of the MPL along with this library; see the
file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "BoxIntersection.h"
#include "Box.h"
#include "Triangle.h"
#include "Vec3.h"
namespace Geometry {
namespace Intersection {
bool isBoxIntersectingTriangle(const Box_f & box, const Triangle_f & triangle) {
/* use separating axis theorem to test overlap between triangle and box */
/* need to test for overlap in these directions: */
/* 1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */
/* we do not even need to test these) */
/* 2) normal of the triangle */
/* 3) crossproduct(edge from tri, {x,y,z}-directin) */
/* this gives 3x3=9 more tests */
Vec3 boxcenter = box.getCenter();
Vec3 boxhalfsize(0.5f * box.getExtentX(), 0.5f * box.getExtentY(), 0.5f * box.getExtentZ());
/* This is the fastest branch on Sun */
/* move everything so that the boxcenter is in (0,0,0) */
const auto v0 = triangle.getVertexA() - boxcenter;
const auto v1 = triangle.getVertexB() - boxcenter;
const auto v2 = triangle.getVertexC() - boxcenter;
/* compute triangle edges */
const auto e0 = triangle.getEdgeAB();
const auto e1 = triangle.getEdgeBC();
const auto e2 = triangle.getEdgeCA();
/* Bullet 3: */
/* test the 9 tests first (this was faster) */
float p0, p1, p2, rad, pMin, pMax;
Vec3 fe = e0.getAbs();
p0 = e0.z() * v0.y() - e0.y() * v0.z();
p2 = e0.z() * v2.y() - e0.y() * v2.z();
if (p0 < p2) {
pMin = p0;
pMax = p2;
} else {
pMin = p2;
pMax = p0;
}
rad = fe.z() * boxhalfsize.y() + fe.y() * boxhalfsize.z();
if (pMin > rad || pMax < -rad)
return 0;
p0 = -e0.z() * v0.x() + e0.x() * v0.z();
p2 = -e0.z() * v2.x() + e0.x() * v2.z();
if (p0 < p2) {
pMin = p0;
pMax = p2;
} else {
pMin = p2;
pMax = p0;
}
rad = fe.z() * boxhalfsize.x() + fe.x() * boxhalfsize.z();
if (pMin > rad || pMax < -rad)
return 0;
p1 = e0.y() * v1.x() - e0.x() * v1.y();
p2 = e0.y() * v2.x() - e0.x() * v2.y();
if (p2 < p1) {
pMin = p2;
pMax = p1;
} else {
pMin = p1;
pMax = p2;
}
rad = fe.y() * boxhalfsize.x() + fe.x() * boxhalfsize.y();
if (pMin > rad || pMax < -rad)
return 0;
fe = e1.getAbs();
p0 = e1.z() * v0.y() - e1.y() * v0.z();
p2 = e1.z() * v2.y() - e1.y() * v2.z();
if (p0 < p2) {
pMin = p0;
pMax = p2;
} else {
pMin = p2;
pMax = p0;
}
rad = fe.z() * boxhalfsize.y() + fe.y() * boxhalfsize.z();
if (pMin > rad || pMax < -rad)
return 0;
p0 = -e1.z() * v0.x() + e1.x() * v0.z();
p2 = -e1.z() * v2.x() + e1.x() * v2.z();
if (p0 < p2) {
pMin = p0;
pMax = p2;
} else {
pMin = p2;
pMax = p0;
}
rad = fe.z() * boxhalfsize.x() + fe.x() * boxhalfsize.z();
if (pMin > rad || pMax < -rad)
return 0;
p0 = e1.y() * v0.x() - e1.x() * v0.y();
p1 = e1.y() * v1.x() - e1.x() * v1.y();
if (p0 < p1) {
pMin = p0;
pMax = p1;
} else {
pMin = p1;
pMax = p0;
}
rad = fe.y() * boxhalfsize.x() + fe.x() * boxhalfsize.y();
if (pMin > rad || pMax < -rad)
return 0;
fe = e2.getAbs();
p0 = e2.z() * v0.y() - e2.y() * v0.z();
p1 = e2.z() * v1.y() - e2.y() * v1.z();
if (p0 < p1) {
pMin = p0;
pMax = p1;
} else {
pMin = p1;
pMax = p0;
}
rad = fe.z() * boxhalfsize.y() + fe.y() * boxhalfsize.z();
if (pMin > rad || pMax < -rad)
return 0;
p0 = -e2.z() * v0.x() + e2.x() * v0.z();
p1 = -e2.z() * v1.x() + e2.x() * v1.z();
if (p0 < p1) {
pMin = p0;
pMax = p1;
} else {
pMin = p1;
pMax = p0;
}
rad = fe.z() * boxhalfsize.x() + fe.x() * boxhalfsize.z();
if (pMin > rad || pMax < -rad)
return 0;
p1 = e2.y() * v1.x() - e2.x() * v1.y();
p2 = e2.y() * v2.x() - e2.x() * v2.y();
if (p2 < p1) {
pMin = p2;
pMax = p1;
} else {
pMin = p1;
pMax = p2;
}
rad = fe.y() * boxhalfsize.x() + fe.x() * boxhalfsize.y();
if (pMin > rad || pMax < -rad)
return 0;
/* Bullet 1: */
/* first test overlap in the {x,y,z}-directions */
/* find min, max of the triangle each direction, and test for overlap in */
/* that direction -- this is equivalent to testing a minimal AABB around */
/* the triangle against the AABB */
/* test in X-direction */
if (std::min(std::min(v0.x(), v1.x()), v2.x()) > boxhalfsize[0]
|| std::max(std::max(v0.x(), v1.x()), v2.x()) < -boxhalfsize[0])
return 0;
/* test in Y-direction */
if (std::min(std::min(v0.y(), v1.y()), v2.y()) > boxhalfsize[1]
|| std::max(std::max(v0.y(), v1.y()), v2.y()) < -boxhalfsize[1])
return 0;
/* test in Z-direction */
if (std::min(std::min(v0.z(), v1.z()), v2.z()) > boxhalfsize[2]
|| std::max(std::max(v0.z(), v1.z()), v2.z()) < -boxhalfsize[2])
return 0;
/* Bullet 2: */
/* test if the box intersects the plane of the triangle */
/* compute plane equation of triangle: normal*x+d=0 */
Vec3 normal = e0.cross(e1);
float d = -normal.dot(v0); /* plane eq: normal.x+d=0 */
Vec3 vmin, vmax;
for (int q = 0; q < 3; q++) {
if (normal[q] > 0.0f) {
vmin[q] = -boxhalfsize[q];
vmax[q] = boxhalfsize[q];
} else {
vmin[q] = boxhalfsize[q];
vmax[q] = -boxhalfsize[q];
}
}
if (normal.dot(vmin) + d > 0.0f)
return 0;
if (normal.dot(vmax) + d >= 0.0f)
return 1;
return 0;
}
}
}