-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathrsi_geometry.h
More file actions
357 lines (320 loc) · 12.6 KB
/
Copy pathrsi_geometry.h
File metadata and controls
357 lines (320 loc) · 12.6 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
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
//Copyright (c) 2022, Raymond Leung
//All rights reserved.
//
//This source code is licensed under the BSD-3-clause license found
//in the LICENSE.md file in the root directory of this source tree.
//
#pragma once
//Implement ray-surface (line-segment and triangle) intersection tests
#include <vector>
#include <stdint.h>
namespace lib_rsi {
#define EPSILON 0.00001
#define MAX_INTERSECTIONS 32
using namespace std;
//axes-aligned bounding box
struct AABB
{
float xMin, xMax, yMin, yMax, zMin, zMax;
};
struct InterceptDistances
{
float t[MAX_INTERSECTIONS];
int count;
};
//declaration
__device__ void subtract(const float *aVec3, const float *bVec3, float *outVec3);
__device__ void dot(const float *aVec3, const float *bVec3, float &out);
__device__ void cross(const float *aVec3, const float *bVec3, float *outVec3);
__device__ float tolerance(const float *dir, const float *edge1, const float *edge2);
__device__ void lineSegmentBbox(const float *p0, const float *p1, AABB &box);
__device__ bool notOverlap(const float *tMin, const float *tMax,
const float *rayMin, const float *rayMax);
__device__ int intersectMoller(
const float *v0, const float *v1, const float *v2,
const float *edge1, const float *edge2,
const float *q0, const float *q1);
__device__ int intersectMoller(
const float *v0, const float *v1, const float *v2,
const float *edge1, const float *edge2,
const float *q0, const float *q1,
float &t, float &u, float &v);
__device__ void checkRayTriangleIntersection(const float* __restrict__ vertices,
const int* __restrict__ triangles,
const float* __restrict__ rayFrom,
const float* __restrict__ rayTo,
int* __restrict__ results,
int rayIdx, int triangleID);
__device__ void checkRayTriangleIntersection(const float* __restrict__ vertices,
const int* __restrict__ triangles,
const float* __restrict__ rayFrom,
const float* __restrict__ rayTo,
int* __restrict__ intersectTriangle,
float* baryT, float* baryU, float* baryV,
int rayIdx, int triangleID);
__device__ void checkRayTriangleIntersection(const float* __restrict__ vertices,
const int* __restrict__ triangles,
const float* __restrict__ rayFrom,
const float* __restrict__ rayTo,
InterceptDistances* __restrict__ interceptDists,
int* __restrict__ results,
int rayIdx, int triangleID);
__global__ void rbxKernel(const float* __restrict__ rayFrom,
const float* __restrict__ rayTo,
AABB* __restrict__ rayBox, int numRays);
template <typename T>
__global__ void initArrayKernel(T* array, T value, int numElements);
//implementation
__device__ void subtract(const float *a, const float *b, float *out)
{
out[0] = a[0] - b[0];
out[1] = a[1] - b[1];
out[2] = a[2] - b[2];
}
__device__ void dot(const float *a, const float *b, float &out)
{
out = a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
}
__device__ void cross(const float *a, const float *b, float *out)
{
out[0] = a[1]*b[2] - a[2]*b[1];
out[1] = a[2]*b[0] - a[0]*b[2];
out[2] = a[0]*b[1] - a[1]*b[0];
}
__device__ float tolerance(const float *d, const float *eAB, const float *eAC)
{
float scaling = sqrtf((d[0]*d[0] + d[1]*d[1] + d[2]*d[2]) *
(eAB[0]*eAB[0] + eAB[1]*eAB[1] + eAB[2]*eAB[2]) *
(eAC[0]*eAC[0] + eAC[1]*eAC[1] + eAC[2]*eAC[2]));
return (scaling > 1)? scaling * EPSILON : EPSILON;
}
__device__ void lineSegmentBbox(const float *p0, const float *p1, AABB &box)
{
if (p0[0] > p1[0]) { box.xMin = p1[0]; box.xMax = p0[0]; }
else { box.xMin = p0[0]; box.xMax = p1[0]; }
if (p0[1] > p1[1]) { box.yMin = p1[1]; box.yMax = p0[1]; }
else { box.yMin = p0[1]; box.yMax = p1[1]; }
if (p0[2] > p1[2]) { box.zMin = p1[2]; box.zMax = p0[2]; }
else { box.zMin = p0[2]; box.zMax = p1[2]; }
}
__device__ bool notOverlap(const float *tMin, const float *tMax,
const float *rayMin, const float *rayMax)
{ //this version uses precomputed rayMin and rayMax
if (rayMin[0] > tMax[0] || rayMax[0] < tMin[0])
return true;
if (rayMin[1] > tMax[1] || rayMax[1] < tMin[1])
return true;
if (rayMin[2] > tMax[2] || rayMax[2] < tMin[2])
return true;
return false;
}
// Implement the Moller-Trumbore ray-triangle intersection algorithm
// Only care about distance t, NOT the remaining barycentric coordinates (u,v)
// - Ray model: R(t) = Q0 + t *(Q1 - Q0), where Q0, Q1 denote segment end points
// - Point on triangle: T(u,v) = (1-u-v)*V0 + u*V1 + v*V2
//
__device__ int intersectMoller(
const float *v0, const float *v1, const float *v2,
const float *edge1, const float *edge2,
const float *q0, const float *q1)
{
float direction[3], avec[3], bvec[3], tvec[3], t, u, v, det, inv_det;
subtract(q1, q0, direction);
cross(direction, edge2, avec);
dot(avec, edge1, det);
float epsilon = tolerance(direction, edge1, edge2);
if (det > epsilon) {
subtract(q0, v0, tvec);
dot(avec, tvec, u);
if (u < 0 || u > det)
return 0;
cross(tvec, edge1, bvec);
dot(bvec, direction, v);
if (v < 0 || u + v > det)
return 0;
}
else if (det < -epsilon) {
subtract(q0, v0, tvec);
dot(avec, tvec, u);
if (u > 0 || u < det)
return 0;
cross(tvec, edge1, bvec);
dot(bvec, direction, v);
if (v > 0 || u + v < det)
return 0;
}
else
return 0;
inv_det = 1.0 / det;
dot(bvec, edge2, t);
t *= inv_det;
if (t < 0 || t > 1) {
return 0;
}
else {
return 1;
}
}
__device__ int intersectMoller(
const float *v0, const float *v1, const float *v2,
const float *edge1, const float *edge2,
const float *q0, const float *q1,
float &t, float &u, float &v)
{
float direction[3], avec[3], bvec[3], tvec[3], det, inv_det;
subtract(q1, q0, direction);
cross(direction, edge2, avec);
dot(avec, edge1, det);
float epsilon = tolerance(direction, edge1, edge2);
if (det > epsilon) {
subtract(q0, v0, tvec);
dot(avec, tvec, u);
if (u < 0 || u > det)
return 0;
cross(tvec, edge1, bvec);
dot(bvec, direction, v);
if (v < 0 || u + v > det)
return 0;
}
else if (det < -epsilon) {
subtract(q0, v0, tvec);
dot(avec, tvec, u);
if (u > 0 || u < det)
return 0;
cross(tvec, edge1, bvec);
dot(bvec, direction, v);
if (v > 0 || u + v < det)
return 0;
}
else
return 0;
inv_det = 1.0 / det;
dot(bvec, edge2, t);
t *= inv_det;
if (t < 0 || t > 1) {
return 0;
}
else {
u *= inv_det;
v *= inv_det;
return 1;
}
}
/* Report intersection as boolean in `results` */
__device__ void checkRayTriangleIntersection(const float* __restrict__ vertices,
const int* __restrict__ triangles,
const float* __restrict__ rayFrom,
const float* __restrict__ rayTo,
int* __restrict__ results,
int rayIdx, int triangleID)
{
float triangleVerts[9], edge1[3], edge2[3];
const float *v0 = &triangleVerts[0],
*v1 = &triangleVerts[3],
*v2 = &triangleVerts[6];
for(int j = 0; j < 3; j++) {
int v = triangles[3*triangleID+j];
for (int k = 0; k < 3; k++) {
triangleVerts[3*j+k] = vertices[3*v+k];
}
}
subtract(v1, v0, edge1);
subtract(v2, v0, edge2);
//apply Moller-Trumbore ray-triangle intersection test
const float *start = &rayFrom[3*rayIdx], *finish = &rayTo[3*rayIdx];
if (intersectMoller(v0, v1, v2, edge1, edge2, start, finish)) {
results[rayIdx] = 1;
}
}
/* @overload Report barycentric coordinates (t,u,v) where t=distance(rayFrom,surface) */
__device__ void checkRayTriangleIntersection(const float* __restrict__ vertices,
const int* __restrict__ triangles,
const float* __restrict__ rayFrom,
const float* __restrict__ rayTo,
int* __restrict__ intersectTriangle,
float* baryT, float* baryU, float* baryV,
int rayIdx, int triangleID)
{
float triangleVerts[9], edge1[3], edge2[3];
const float *v0 = &triangleVerts[0],
*v1 = &triangleVerts[3],
*v2 = &triangleVerts[6];
for(int j = 0; j < 3; j++) {
int v = triangles[3*triangleID+j];
for (int k = 0; k < 3; k++) {
triangleVerts[3*j+k] = vertices[3*v+k];
}
}
subtract(v1, v0, edge1);
subtract(v2, v0, edge2);
const float *start = &rayFrom[3*rayIdx], *finish = &rayTo[3*rayIdx];
float t, u, v;
if (intersectMoller(v0, v1, v2, edge1, edge2, start, finish, t, u, v)) {
if (t < baryT[rayIdx]) {
intersectTriangle[rayIdx] = triangleID;
baryT[rayIdx] = t;
baryU[rayIdx] = u;
baryV[rayIdx] = v;
}
}
}
/* @overload Report number of unique ray-surface intersections (limited to < 32) */
__device__ void checkRayTriangleIntersection(const float* __restrict__ vertices,
const int* __restrict__ triangles,
const float* __restrict__ rayFrom,
const float* __restrict__ rayTo,
InterceptDistances &interceptDists,
int* __restrict__ results,
int rayIdx, int triangleID)
{
float triangleVerts[9], edge1[3], edge2[3];
const float tol(EPSILON);
const float *v0 = &triangleVerts[0],
*v1 = &triangleVerts[3],
*v2 = &triangleVerts[6];
for(int j = 0; j < 3; j++) {
int v = triangles[3*triangleID+j];
for (int k = 0; k < 3; k++) {
triangleVerts[3*j+k] = vertices[3*v+k];
}
}
subtract(v1, v0, edge1);
subtract(v2, v0, edge2);
const float *start = &rayFrom[3*rayIdx], *finish = &rayTo[3*rayIdx];
float *tp = interceptDists.t; //circular buffer
float t, u, v;
if (intersectMoller(v0, v1, v2, edge1, edge2, start, finish, t, u, v)) {
bool newIntercept(true);
for (int i = 0; i < MAX_INTERSECTIONS; i++) {
if ((t > tp[i] - tol) && (t < tp[i] + tol)) {
newIntercept = false;
break;
}
}
if (newIntercept) {
tp[interceptDists.count & (MAX_INTERSECTIONS - 1)] = t;
interceptDists.count++;
results[rayIdx] += 1;
}
}
}
__global__ void rbxKernel(const float* __restrict__ rayFrom,
const float* __restrict__ rayTo,
AABB* __restrict__ rayBox, int numRays)
{ //Pre-compute min/max coordinates for all line segments,
//instead of repeating the same in each thread-block.
const int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < numRays) {
const float *start = &rayFrom[3*i], *finish = &rayTo[3*i];
lineSegmentBbox(start, finish, rayBox[i]);
}
}
template <typename T>
__global__ void initArrayKernel(T* array, T value, int numElements)
{
const int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < numElements) {
array[i] = value;
}
}
}