-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmesh_query.cpp
More file actions
322 lines (294 loc) · 10.2 KB
/
Copy pathmesh_query.cpp
File metadata and controls
322 lines (294 loc) · 10.2 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <cfloat>
#include "mesh_query.h"
#include "bounding_box_tree.h"
#include "predicates.h"
//==============================================================================
// helper functions for ray casts along positive z axis
// returns true if the ray cast from p along positive z axis hits box
static bool
box_zcast(const Vec3d& p,
const BoundingBox& box)
{
return p[0]>=box.xmin[0] && p[0]<=box.xmax[0]
&& p[1]>=box.xmin[1] && p[1]<=box.xmax[1]
&& p[2]<=box.xmax[2];
}
// helper function for tri_zcast below...
// Here we are given a robust 2d orientation of qrs in xy projection, which
// must be positive.
static bool
tri_zcast_inner(const Vec3d& p,
double qrs,
const Vec3d& q,
const Vec3d& r,
const Vec3d& s)
{
assert(qrs>0);
// first check if point is above or below triangle in z
double pqrs=orient3d(p.v, q.v, r.v, s.v);
if(pqrs>=0) return false; // point is on or above triangle - no intersection
// then check if point lies outside triangle in 2D xy projection
double pqr=orient2d(p.v, q.v, r.v);
if(pqr<0) return false;
double prs=orient2d(p.v, r.v, s.v);
if(prs<0) return false;
double psq=orient2d(p.v, s.v, q.v);
if(psq<0) return false;
// note: the following tests are somewhat redundant, but it's a pretty
// tiny optimization to eliminate the redundancy compared to the loss in
// clarity.
// check if point is strictly inside the triangle in xy
if(pqr>0 && prs>0 && psq>0) return true;
// check if point is strictly on edge qr
if(pqr==0 && prs>0 && psq>0){
if(q[1]<r[1]) return false;
if(q[1]>r[1]) return true;
if(q[0]<r[0]) return true;
assert(q[0]>r[0]); // q!=r because triangle is not degenerate
return false;
}
// check if point is strictly on edge rs
if(prs==0 && pqr>0 && psq>0){
if(r[1]<s[1]) return false;
if(r[1]>s[1]) return true;
if(r[0]<s[0]) return true;
assert(r[0]>s[0]); // r!=s because triangle is not degenerate
return false;
}
// check if point is strictly on edge sq
if(psq==0 && pqr>0 && prs>0){
if(s[1]<q[1]) return false;
if(s[1]>q[1]) return true;
if(s[0]<q[0]) return true;
assert(s[0]>q[0]); // r!=s because triangle is not degenerate
return false;
}
// check if point is on vertex q
if(p[0]==q[0] && p[1]==q[1]){
return q[1]>=r[1] && q[1]<s[1];
}
// check if point is on vertex r
if(p[0]==r[0] && p[1]==r[1]){
return r[1]>=s[1] && r[1]<q[1];
}
// check if point is on vertex s
if(p[0]==s[0] && p[1]==s[1]){
return s[1]>=q[1] && s[1]<r[1];
}
assert(false); // we should have covered all cases at this point
return false; // just to quiet compiler warnings
}
// returns true if the ray cast from p along positive z axis hits triangle in
// exactly one spot, with edge cases handled appropriately
static bool
tri_zcast(const Vec3d& p,
const Vec3d& q,
const Vec3d& r,
const Vec3d& s)
{
// robustly find orientation of qrs in 2D xy projection
double qrs=orient2d(q.v, r.v, s.v);
if(qrs>0)
return tri_zcast_inner(p, qrs, q, r, s);
else if(qrs<0)
return tri_zcast_inner(p, -qrs, q, s, r); // flip triangle to reorient
else
return false; // triangle is degenerate in 2D projection - ignore
}
//==============================================================================
// helper functions for segment intersecting triangles
// returns false only if the segment for sure can't intersect the box
static bool
segment_box_intersect(const Vec3d& p,
const Vec3d& q,
const BoundingBox& box)
{
// these are conservative bounds on error factor from rounding
const double lo=1-5*DBL_EPSILON, hi=1+5*DBL_EPSILON;
double s=0, t=1; // bounds on parameter for intersection interval
for(unsigned int i=0; i<3; ++i){
if(p[i]<q[i]){
double d=q[i]-p[i];
double s0=lo*(box.xmin[i]-p[i])/d, t0=hi*(box.xmax[i]-p[i])/d;
if(s0>s) s=s0;
if(t0<t) t=t0;
}else if(p[i]>q[i]){
double d=q[i]-p[i];
double s0=lo*(box.xmax[i]-p[i])/d, t0=hi*(box.xmin[i]-p[i])/d;
if(s0>s) s=s0;
if(t0<t) t=t0;
}else{
if(p[i]<box.xmin[i] || p[i]>box.xmax[i]) return false;
}
if(s>t) return false;
}
return true;
}
// determine if segment pq intersects triangle uvw, setting approximate
// barycentric coordinates if so.
static bool
segment_tri_intersect(const Vec3d& p,
const Vec3d& q,
const Vec3d& u,
const Vec3d& v,
const Vec3d& w,
double* s,
double* t,
double* a,
double* b,
double* c)
{
// find where segment hits plane of triangle
double puvw=orient3d(p.v, u.v, v.v, w.v),
uvwq=orient3d(u.v, v.v, w.v, q.v);
if((puvw<=0 && uvwq>=0) || (puvw>=0 && uvwq<=0))
return false; // either no intersection, or a degenerate one
if(puvw<0 || uvwq<0){
double pqvw=orient3d(p.v, q.v, v.v, w.v);
if(pqvw>0) return false;
double puqw=orient3d(p.v, u.v, q.v, w.v);
if(puqw>0) return false;
double puvq=orient3d(p.v, u.v, v.v, q.v);
if(puvq>0) return false;
*s=uvwq/(puvw+uvwq);
*t=puvw/(puvw+uvwq);
*a=pqvw/(pqvw+puqw+puvq);
*b=puqw/(pqvw+puqw+puvq);
*c=puvq/(pqvw+puqw+puvq);
return true;
}else{ //(puvw>0 || uvwq>0)
double pqvw=orient3d(p.v, q.v, v.v, w.v);
if(pqvw<0) return false;
double puqw=orient3d(p.v, u.v, q.v, w.v);
if(puqw<0) return false;
double puvq=orient3d(p.v, u.v, v.v, q.v);
if(puvq<0) return false;
*s=uvwq/(puvw+uvwq);
*t=puvw/(puvw+uvwq);
*a=pqvw/(pqvw+puqw+puvq);
*b=puqw/(pqvw+puqw+puvq);
*c=puvq/(pqvw+puqw+puvq);
return true;
}
}
//==============================================================================
// the actual accelerated mesh class
struct MeshObject
{
int n;
const Vec3d *x;
int nt;
const Vec3i *tri;
BoundingBoxTree tree;
MeshObject(int n_,
const double *x_,
int nt_,
const int *tri_)
: n(n_), x((const Vec3d*)x_), nt(nt_), tri((const Vec3i*)tri_)
{
assert(x && tri && n>=0 && nt>=0);
if(nt==0) return;
std::vector<BoundingBox> box(nt);
for(int t=0; t<nt; ++t){
int i, j, k; assign(tri[t], i, j, k);
box[t].build_from_points(x[i], x[j], x[k]);
}
tree.construct_from_leaf_boxes(nt, &box[0]);
}
bool
inside(const double point[3]) const
{
Vec3d p(point);
// quick check on root bounding box
if(!tree.box.contains(p)) return false;
// count intersections along a ray-cast, check parity for inside/outside
int intersection_count=0;
// we cast ray along positive z axis
std::vector<const BoundingBoxTree*> stack;
stack.push_back(&tree);
while(!stack.empty()){
const BoundingBoxTree *node=stack.back();
stack.pop_back();
// check any triangles in this node
for(unsigned int i=0; i<node->index.size(); ++i){
int t=node->index[i];
if(tri_zcast(p, x[tri[t][0]], x[tri[t][1]], x[tri[t][2]]))
++intersection_count;
}
// check any subtrees for this node
for(unsigned int i=0; i<node->children.size(); ++i){
if(box_zcast(p, node->children[i]->box))
stack.push_back(node->children[i]);
}
}
return intersection_count%2;
}
bool
intersects(const Vec3d &p,
const Vec3d &q,
int *triangle_index,
double *s,
double *t,
double *a,
double *b,
double *c) const
{
std::vector<const BoundingBoxTree*> stack;
stack.push_back(&tree);
while(!stack.empty()){
const BoundingBoxTree *node=stack.back();
stack.pop_back();
if(!segment_box_intersect(p, q, node->box))
continue; // no need to go further with this node
// check any triangles in this node
for(unsigned int i=0; i<node->index.size(); ++i){
int u, v, w; assign(tri[node->index[i]], u, v, w);
if(segment_tri_intersect(p, q, x[u], x[v], x[w],
s, t, a, b, c)){
*triangle_index=node->index[i];
return true;
}
}
// check any subtrees for this node
for(unsigned int i=0; i<node->children.size(); ++i)
stack.push_back(node->children[i]);
}
return false;
}
};
//==============================================================================
// the plain C wrapper API
MeshObject*
construct_mesh_object(int num_vertices,
const double *positions,
int num_triangles,
const int *triangles)
{
exactinit(); // really only need to call this once, but safe to call always
return new MeshObject(num_vertices, positions, num_triangles, triangles);
}
void
destroy_mesh_object(MeshObject *mesh)
{
delete mesh;
}
bool
point_inside_mesh(const double point[3],
const MeshObject *mesh)
{
return mesh->inside(point);
}
bool
segment_intersects_mesh(const double point0[3],
const double point1[3],
const MeshObject *mesh,
int *triangle_index,
double *s,
double *t,
double *a,
double *b,
double *c)
{
return mesh->intersects(Vec3d(point0), Vec3d(point1),
triangle_index, s, t, a, b, c);
}