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
372
373
|
// This code contains NVIDIA Confidential Information and is disclosed to you
// under a form of NVIDIA software license agreement provided separately to you.
//
// Notice
// NVIDIA Corporation and its licensors retain all intellectual property and
// proprietary rights in and to this software and related documentation and
// any modifications thereto. Any use, reproduction, disclosure, or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA Corporation is strictly prohibited.
//
// ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
// NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
// THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
// MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
//
// Information and code furnished is believed to be accurate and reliable.
// However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
// information or for any infringement of patents or other rights of third parties that may
// result from its use. No license is granted by implication or otherwise under any patent
// or patent rights of NVIDIA Corporation. Details are subject to change without notice.
// This code supersedes and replaces all information previously supplied.
// NVIDIA Corporation products are not authorized for use as critical
// components in life support devices or systems without express written approval of
// NVIDIA Corporation.
//
// Copyright (c) 2016-2018 NVIDIA Corporation. All rights reserved.
#include "NvBlastExtTriangleProcessor.h"
#include "NvBlastExtAuthoringInternalCommon.h"
#define COLLIN_EPS 1e-4f
#define V_COMP_EPS 1e-5f
using namespace physx;
namespace Nv
{
namespace Blast
{
/**
Segments bounding box interseciton test
*/
bool boundingRectangleIntersection(const PxVec2& s1, const PxVec2& e1, const PxVec2& s2, const PxVec2& e2)
{
// sl1/sl2 is always left bottom end of rectangle
// se1/el2 is always right top end of rectangle
PxF32 sl1, sl2, el1, el2;
if (s1.x < e1.x)
{
sl1 = s1.x;
el1 = e1.x;
}
else
{
el1 = s1.x;
sl1 = e1.x;
}
if (s2.x < e2.x)
{
sl2 = s2.x;
el2 = e2.x;
}
else
{
el2 = s2.x;
sl2 = e2.x;
}
if (PxMax(sl1, sl2) > PxMin(el1, el2))
return false;
if (s1.y < e1.y)
{
sl1 = s1.y;
el1 = e1.y;
}
else
{
el1 = s1.y;
sl1 = e1.y;
}
if (s2.y < e2.y)
{
sl2 = s2.y;
el2 = e2.y;
}
else
{
el2 = s2.y;
sl2 = e2.y;
}
if (PxMax(sl1, sl2) > PxMin(el1, el2))
return false;
return true;
}
inline PxF32 getRotation(PxVec2 a, PxVec2 b)
{
return a.x * b.y - a.y * b.x;
}
inline PxF32 getParameter(const PxVec2& a, const PxVec2& b, const PxVec2& point)
{
return (point - a).magnitude() / (b - a).magnitude();
}
inline PxVec3 lerp3D(const PxVec3& a, const PxVec3& b, const PxF32 t)
{
return (b - a) * t + a;
}
struct Line2D
{
PxVec2 normal;
PxF32 c;
Line2D(PxVec2 vec, PxVec2 point)
{
normal.x = vec.y;
normal.y = -vec.x;
c = -normal.dot(point);
}
};
uint32_t TriangleProcessor::getSegmentIntersection(const PxVec2& s1, const PxVec2& e1, const PxVec2& s2, const PxVec2& e2, PxF32& t1)
{
if (!boundingRectangleIntersection(s1, e1, s2, e2))
return 0;
PxVec2 vec1 = e1 - s1;
PxVec2 vec2 = e2 - s2;
PxF32 det1 = getRotation(vec1, vec2);
if (PxAbs(det1) < COLLIN_EPS)
{
return 0;
}
Line2D lineA(vec1, s1);
Line2D lineB(vec2, s2);
PxVec2 fInt;
PxF32 detX = lineA.normal.y * lineB.c - lineA.c * lineB.normal.y;
PxF32 detY = lineA.c * lineB.normal.x - lineB.c * lineA.normal.x;
PxF32 x = detX / det1;
PxF32 y = detY / det1;
if (x + V_COMP_EPS >= PxMax(PxMin(s1.x, e1.x), PxMin(s2.x, e2.x)) &&
x - V_COMP_EPS <= PxMin(PxMax(s1.x, e1.x), PxMax(s2.x, e2.x)) &&
y + V_COMP_EPS >= PxMax(PxMin(s1.y, e1.y), PxMin(s2.y, e2.y)) &&
y - V_COMP_EPS <= PxMin(PxMax(s1.y, e1.y), PxMax(s2.y, e2.y)))
{
fInt.x = x;
fInt.y = y;
t1 = getParameter(s1, e1, fInt);
return 1;
}
return 0;
}
struct cwComparer
{
PxVec3 basePoint;
PxVec3 normal;
cwComparer(PxVec3 basePointIn, PxVec3 norm)
{
basePoint = basePointIn;
normal = norm;
};
bool operator()(const PxVec3& a, const PxVec3& b)
{
PxVec3 norm = (a - basePoint).cross(b - basePoint);
return normal.dot(norm) > 0;
}
};
bool vec3Comparer(const PxVec3& a, const PxVec3& b)
{
if (a.x + V_COMP_EPS < b.x) return true;
if (a.x - V_COMP_EPS > b.x) return false;
if (a.y + V_COMP_EPS < b.y) return true;
if (a.y - V_COMP_EPS > b.y) return false;
if (a.z + V_COMP_EPS < b.z) return true;
return false;
}
void TriangleProcessor::sortToCCW(std::vector<PxVec3>& points, PxVec3& normal)
{
std::sort(points.begin(), points.end(), vec3Comparer);
int lastUnique = 0;
for (uint32_t i = 1; i < points.size(); ++i)
{
PxVec3 df = points[i] - points[lastUnique];
if (df.x > V_COMP_EPS || df.y > V_COMP_EPS || df.z > V_COMP_EPS)
{
points[++lastUnique] = points[i];
}
}
points.resize(lastUnique + 1);
if (points.size() > 2)
{
cwComparer compr(points[0], normal);
std::sort(points.begin() + 1, points.end(), compr);
}
}
void TriangleProcessor::buildConvexHull(std::vector<PxVec3>& points, std::vector<PxVec3>& convexHull,const PxVec3& normal)
{
std::sort(points.begin(), points.end(), vec3Comparer);
int lastUnique = 0;
for (uint32_t i = 1; i < points.size(); ++i)
{
PxVec3 df = points[i] - points[lastUnique];
if (df.x > V_COMP_EPS || df.y > V_COMP_EPS || df.z > V_COMP_EPS)
{
points[++lastUnique] = points[i];
}
}
points.resize(lastUnique + 1);
if (points.size() > 2)
{
cwComparer compr(points[0], normal);
std::sort(points.begin() + 1, points.end(), compr);
}
if (points.size() < 3)
return;
convexHull.push_back(points[0]);
convexHull.push_back(points[1]);
ProjectionDirections projectionDirection = getProjectionDirection(normal);
for (uint32_t i = 2; i < points.size(); ++i)
{
PxVec2 pnt = getProjectedPointWithWinding(points[i], projectionDirection);
PxVec2 vec = pnt - getProjectedPointWithWinding(convexHull.back(), projectionDirection);
if (vec.x < V_COMP_EPS && vec.y < V_COMP_EPS)
{
continue;
}
if (getRotation(vec, getProjectedPointWithWinding(convexHull.back(), projectionDirection) - getProjectedPointWithWinding(convexHull[convexHull.size() - 2], projectionDirection)) < 0)
{
convexHull.push_back(points[i]);
}
else
{
while (convexHull.size() > 1 && getRotation(vec, getProjectedPointWithWinding(convexHull.back(), projectionDirection) - getProjectedPointWithWinding(convexHull[convexHull.size() - 2], projectionDirection)) > 0)
{
convexHull.pop_back();
vec = pnt - getProjectedPointWithWinding(convexHull.back(), projectionDirection);
}
convexHull.push_back(points[i]);
}
}
}
uint32_t TriangleProcessor::getTriangleIntersection(TrPrcTriangle& a, TrPrcTriangle2d& aProjected, TrPrcTriangle &b, PxVec3& centroid, std::vector<PxVec3>& intersectionBuffer, PxVec3 normal)
{
b.points[0] -= centroid;
b.points[1] -= centroid;
b.points[2] -= centroid;
ProjectionDirections prjDir = getProjectionDirection(normal);
TrPrcTriangle2d bProjected;
bProjected.points[0] = getProjectedPointWithWinding(b.points[0], prjDir);
bProjected.points[1] = getProjectedPointWithWinding(b.points[1], prjDir);
bProjected.points[2] = getProjectedPointWithWinding(b.points[2], prjDir);
if (!triangleBoundingBoxIntersection(aProjected, bProjected)) return 0;
//* Check triangle A against points of B *//
for (int i = 0; i < 3; ++i)
{
if (isPointInside(bProjected.points[i], aProjected))
{
intersectionBuffer.push_back(b.points[i]);
}
}
//* Check triangle B against points of A *//
for (int i = 0; i < 3; ++i)
{
if (isPointInside(aProjected.points[i], bProjected))
{
intersectionBuffer.push_back(a.points[i]);
}
}
//* Check edges intersection *//
float param = 0;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
if (getSegmentIntersection(aProjected.points[i], aProjected.points[(i + 1) % 3], bProjected.points[j], bProjected.points[(j + 1) % 3], param))
{
intersectionBuffer.push_back(lerp3D(a.points[i], a.points[(i + 1) % 3], param));
}
}
}
if (intersectionBuffer.size() == 0)
return 0;
// Intersection between two triangles is convex, but points should be reordered to construct right polygon //
std::vector<PxVec3> intrs;
buildConvexHull(intersectionBuffer, intrs, normal);
intersectionBuffer = intrs;
// Return all points back from origin //
for (uint32_t i = 0; i < intersectionBuffer.size(); ++i)
{
intersectionBuffer[i] += centroid;
}
return 1;
}
bool TriangleProcessor::triangleBoundingBoxIntersection(TrPrcTriangle2d& a, TrPrcTriangle2d& b)
{
float fb = std::min(a.points[0].x, std::min(a.points[1].x, a.points[2].x));
float fe = std::max(a.points[0].x, std::max(a.points[1].x, a.points[2].x));
float sb = std::min(b.points[0].x, std::min(b.points[1].x, b.points[2].x));
float se = std::max(b.points[0].x, std::max(b.points[1].x, b.points[2].x));
if (std::min(fe, se) + V_COMP_EPS < std::max(fb, sb)) return 0;
fb = std::min(a.points[0].y, std::min(a.points[1].y, a.points[2].y));
fe = std::max(a.points[0].y, std::max(a.points[1].y, a.points[2].y));
sb = std::min(b.points[0].y, std::min(b.points[1].y, b.points[2].y));
se = std::max(b.points[0].y, std::max(b.points[1].y, b.points[2].y));
if (std::min(fe, se) + V_COMP_EPS < std::max(fb, sb)) return 0;
return 1;
}
uint32_t TriangleProcessor::isPointInside(const PxVec2& point, const TrPrcTriangle2d& triangle)
{
PxF32 av = getRotation(point - triangle.points[0], triangle.points[1] - triangle.points[0]);
PxF32 bv = getRotation(point - triangle.points[1], triangle.points[2] - triangle.points[1]);
PxF32 cv = getRotation(point - triangle.points[2], triangle.points[0] - triangle.points[2]);
if (PxAbs(av) < COLLIN_EPS) av = 0;
if (PxAbs(bv) < COLLIN_EPS) bv = 0;
if (PxAbs(cv) < COLLIN_EPS) cv = 0;
if (av >= 0 && bv >= 0 && cv >= 0)
{
if (av == 0 || bv == 0 || cv == 0)
return 2;
return 1;
}
if (av <= 0 && bv <= 0 && cv <= 0)
{
if (av == 0 || bv == 0 || cv == 0)
return 2;
return 1;
}
return 0;
}
} // namespace Blast
} // namespace Nv
|