-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPlane.h
371 lines (313 loc) · 11 KB
/
Plane.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// ======================================================================== //
// Copyright 2020 Ingo Wald //
// //
// Licensed under the Apache License, Version 2.0 (the "License"); //
// you may not use this file except in compliance with the License. //
// You may obtain a copy of the License at //
// //
// http://www.apache.org/licenses/LICENSE-2.0 //
// //
// Unless required by applicable law or agreed to in writing, software //
// distributed under the License is distributed on an "AS IS" BASIS, //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and //
// limitations under the License. //
// ======================================================================== //
#pragma once
typedef unsigned short ushort;
namespace exa {
struct Plane {
inline __device__ float eval(const vec3f v) const
{ return dot(v,N)-d; }
inline __device__ float eval(const vec4f v) const
{ return dot((const vec3f&)v,N)-d; }
vec3f N;
float d;
};
inline __device__ Plane makePlane(const vec3f a,
const vec3f b,
const vec3f c)
{
vec3f N = cross(b-a,c-a);
return { N,dot(a,N) };
}
inline __device__ Plane makePlane(const vec4f a,
const vec4f b,
const vec4f c)
{
return makePlane((const vec3f&)a,
(const vec3f&)b,
(const vec3f&)c);
}
inline __device__ Plane makePlane(const float4 a,
const float4 b,
const float4 c)
{
return makePlane((const vec3f&)a,
(const vec3f&)b,
(const vec3f&)c);
}
inline __both__
bool intersectTet(float &value,
const vec3f P,
const float4 a,
const float4 b,
const float4 c,
const float4 d)
{
// if (dbg()) {
// printf(" A> %f %f %f : %f\n",
// a.x,a.y,a.z,a.w);
// printf(" B> %f %f %f : %f\n",
// b.x,b.y,b.z,b.w);
// printf(" C> %f %f %f : %f\n",
// c.x,c.y,c.z,c.w);
// printf(" D> %f %f %f : %f\n",
// d.x,d.y,d.z,d.w);
// }
vec3f va = vec3f(a)-P;
vec3f vb = vec3f(b)-P;
vec3f vc = vec3f(c)-P;
vec3f vd = vec3f(d)-P;
Plane pa = makePlane(vb,vd,vc);
Plane pb = makePlane(va,vc,vd);
Plane pc = makePlane(va,vd,vb);
Plane pd = makePlane(va,vb,vc);
float fa = pa.eval(vec3f(0.f))/pa.eval(va);
if (fa < 0.f || fa > 1.f) return false;
float fb = pb.eval(vec3f(0.f))/pb.eval(vb);
if (fb < 0.f || fa > 1.f) return false;
float fc = pc.eval(vec3f(0.f))/pc.eval(vc);
if (fc < 0.f || fa > 1.f) return false;
float fd = pd.eval(vec3f(0.f))/pd.eval(vd);
if (fd < 0.f || fa > 1.f) return false;
value = fa*a.w + fb*b.w + fc*c.w + fd*d.w;
return true;
}
inline __both__
bool intersectPair(float &value,
const vec3f &P,
const float4 a,
const float4 b,
const float4 c,
const float4 d0,
const float4 d1)
{
if (intersectTet(value,P,a,b,c,d0)) return true;
if (intersectTet(value,P,a,c,b,d1)) return true;
return false;
}
inline __both__
bool intersectPyr(float &value,
const vec3f &P,
const float4 _v0,
const float4 _v1,
const float4 _v2,
const float4 _v3,
const float4 _v4
)
{
const float f0 = _v0.w;
const float f1 = _v1.w;
const float f2 = _v2.w;
const float f3 = _v3.w;
const float f4 = _v4.w;
const vec3f p0 = vec3f(_v0);
const vec3f p1 = vec3f(_v1);
const vec3f p2 = vec3f(_v2);
const vec3f p3 = vec3f(_v3);
const vec3f p4 = vec3f(_v4);
Plane base = makePlane(p0,p1,p2);
float w = base.eval(P)/base.eval(p4);
if (w > 1.f && w < 0.f) return false;
const float u0 = makePlane(p0,p4,p1).eval(P);
if (u0 < 0.f) return false;
const float u1 = makePlane(p2,p4,p3).eval(P);
if (u1 < 0.f) return false;
const float u = u0 / (u0+u1+1e-10f);
const float v0 = makePlane(p0,p3,p4).eval(P);
if (v0 < 0.f) return false;
const float v1 = makePlane(p1,p4,p2).eval(P);
if (v1 < 0.f) return false;
const float v = v0 / (v0+v1+1e-10f);
// prd.primID = primID;
value = w*f4 + (1.f-w)*((1.f-u)*(1.f-v)*f0+
(1.f-u)*( v)*f1+
( u)*(1.f-v)*f3+
( u)*( v)*f2);
return true;
}
inline __both__
bool intersectWedge(float &value,
const vec3f &P,
const float4 _v0,
const float4 _v1,
const float4 _v2,
const float4 _v3,
const float4 _v4,
const float4 _v5)
{
const float f0 = _v0.w;
const float f1 = _v1.w;
const float f2 = _v2.w;
const float f3 = _v3.w;
const float f4 = _v4.w;
const float f5 = _v5.w;
const vec3f p0 = vec3f(_v0);
const vec3f p1 = vec3f(_v1);
const vec3f p2 = vec3f(_v2);
const vec3f p3 = vec3f(_v3);
const vec3f p4 = vec3f(_v4);
const vec3f p5 = vec3f(_v5);
Plane base = makePlane(p0,p1,p3);
const float w0 = base.eval(P);
if (w0 < 0.f) return false;
Plane top;
top.N = cross(cross(base.N,p5-p2),p5-p2);
top.d = dot(top.N,p2);
const float w1 = top.eval(P);
if (w1 < 0.f) return false;
const float w = w0/(w0+w1+1e-10f);
Plane front = makePlane(p0,p2,p1);
Plane back = makePlane(p3,p4,p5);
const float u0 = front.eval(P);
if (u0 < 0.f) return false;
const float u1 = back.eval(P);
if (u1 < 0.f) return false;
const float u = u0/(u0+u1+1e-10f);
Plane left = makePlane(p0,p3,p2);
Plane right = makePlane(p1,p2,p4);
const float v0 = left.eval(P);
if (v0 < 0.f) return false;
const float v1 = right.eval(P);
if (v1 < 0.f) return false;
const float v = v0/(v0+v1+1e-10f);
const float fbase
= (1.f-u)*(1.f-v)*f0
+ (1.f-u)*( v)*f1
+ ( u)*(1.f-v)*f3
+ ( u)*( v)*f4;
const float ftop = (1.f-u)*f2 + u*f5;
// prd.value = (1.f-w)*fbase + w*ftop;
// prd.primID = primID;
value = (1.f-w)*fbase + w*ftop;
return true;
}
inline __device__
bool intersectHex(float &value,
const vec3f &P,
const float4 v0,
const float4 v1,
const float4 v2,
const float4 v3,
const float4 v4,
const float4 v5,
const float4 v6,
const float4 v7)
{
float f0 = v0.w;
float f1 = v1.w;
float f2 = v2.w;
float f3 = v3.w;
float f4 = v4.w;
float f5 = v5.w;
float f6 = v6.w;
float f7 = v7.w;
const Plane frt = makePlane(v0,v4,v1);
const Plane bck = makePlane(v3,v2,v7);
const Plane lft = makePlane(v0,v3,v4);
const Plane rgt = makePlane(v1,v5,v2);
const Plane top = makePlane(v4,v7,v5);
const Plane btm = makePlane(v0,v1,v3);
const float t_frt = frt.eval(P); if (t_frt < 0.f) return false;
const float t_bck = bck.eval(P); if (t_bck < 0.f) return false;
const float t_lft = lft.eval(P); if (t_lft < 0.f) return false;
const float t_rgt = rgt.eval(P); if (t_rgt < 0.f) return false;
const float t_top = top.eval(P); if (t_top < 0.f) return false;
const float t_btm = btm.eval(P); if (t_btm < 0.f) return false;
const float f_x = t_lft/(t_lft+t_rgt);
const float f_y = t_frt/(t_frt+t_bck);
const float f_z = t_btm/(t_btm+t_top);
value// = 1.f/8.f *(f0+f1+f2+f3+f4+f5+f6+f7);
=
+ (1.f-f_z)*(1.f-f_y)*(1.f-f_x)*f0
+ (1.f-f_z)*(1.f-f_y)*( f_x)*f1
+ (1.f-f_z)*( f_y)*(1.f-f_x)*f3
+ (1.f-f_z)*( f_y)*( f_x)*f2
+ ( f_z)*(1.f-f_y)*(1.f-f_x)*f4
+ ( f_z)*(1.f-f_y)*( f_x)*f5
+ ( f_z)*( f_y)*(1.f-f_x)*f7
+ ( f_z)*( f_y)*( f_x)*f6
;
return true;
}
inline __both__
bool intersectTet(float &value,
const vec3f &P,
const vec4f *vertices,
const ushort *indices)
{
float4 a = vertices[indices[0]];
float4 b = vertices[indices[1]];
float4 c = vertices[indices[2]];
float4 d = vertices[indices[3]];
return intersectTet(value,P,a,b,c,d);
}
inline __both__
bool intersectPair(float &value,
const vec3f &P,
const vec4f *vertices,
const ushort *indices)
{
float4 a = vertices[indices[0]];
float4 b = vertices[indices[1]];
float4 c = vertices[indices[2]];
float4 d0 = vertices[indices[3]];
float4 d1 = vertices[indices[4]];
return intersectPair(value,P,a,b,c,d0,d1);
}
inline __both__
bool intersectPyr(float &value,
const vec3f &P,
const vec4f *vertices,
const ushort *indices)
{
const float4 _v0 = vertices[indices[0]];
const float4 _v1 = vertices[indices[1]];
const float4 _v2 = vertices[indices[2]];
const float4 _v3 = vertices[indices[3]];
const float4 _v4 = vertices[indices[4]];
return intersectPyr(value,P,_v0,_v1,_v2,_v3,_v4);
}
inline __both__
bool intersectWedge(float &value,
const vec3f &P,
const vec4f *vertices,
const ushort *indices)
{
const float4 _v0 = vertices[indices[0]];
const float4 _v1 = vertices[indices[1]];
const float4 _v2 = vertices[indices[2]];
const float4 _v3 = vertices[indices[3]];
const float4 _v4 = vertices[indices[4]];
const float4 _v5 = vertices[indices[5]];
return intersectWedge(value,P,_v0,_v1,_v2,_v3,_v4,_v5);
}
inline __both__
bool intersectHex(float &value,
const vec3f &P,
const vec4f *vertices,
const ushort *indices)
{
float4 v0 = vertices[indices[0]];
float4 v1 = vertices[indices[1]];
float4 v2 = vertices[indices[2]];
float4 v3 = vertices[indices[3]];
float4 v4 = vertices[indices[4]];
float4 v5 = vertices[indices[5]];
float4 v6 = vertices[indices[6]];
float4 v7 = vertices[indices[7]];
return intersectHex(value,P,
v0,v1,v2,v3,v4,v5,v6,v7);
}
}