aboutsummaryrefslogtreecommitdiff
path: root/PhysX_3.4/Source/GeomUtils/src/sweep/GuSweepTriangleUtils.h
blob: 979504fd4601bcb9adc1dea42c87bb11c0b5fd3a (plain) (blame)
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
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//  * Neither the name of NVIDIA CORPORATION nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.  

#ifndef GU_SWEEP_TRIANGLE_UTILS_H
#define GU_SWEEP_TRIANGLE_UTILS_H

#include "CmPhysXCommon.h"
#include "GuSweepSharedTests.h"
#include "GuInternal.h"
#include "PxTriangle.h"
#include "PxQueryReport.h"

namespace physx
{

namespace Gu
{
	// PT: computes proper impact data for sphere-sweep-vs-tri, after the closest tri has been found.
	void computeSphereTriImpactData(PxVec3& hit, PxVec3& normal, const PxVec3& center, const PxVec3& dir, float t, const PxTriangle& tri);

	// PT: computes proper impact data for box-sweep-vs-tri, after the closest tri has been found.
	void computeBoxTriImpactData(PxVec3& hit, PxVec3& normal, const PxVec3& boxExtents, const PxVec3& localDir, const PxTriangle& triInBoxSpace, PxReal impactDist);

	// PT: computes impact normal between two edges. Produces better normals than just the EE cross product.
	// This version properly computes the closest points between two colliding edges and makes a normal from these.
	void computeEdgeEdgeNormal(PxVec3& normal, const PxVec3& p1, const PxVec3& p2_p1, const PxVec3& p3, const PxVec3& p4_p3, const PxVec3& dir, float d);

	// PT: small function just to avoid duplicating the code.
	// Returns index of first triangle we should process (when processing arrays of input triangles)
	PX_FORCE_INLINE PxU32 getInitIndex(const PxU32* PX_RESTRICT cachedIndex, PxU32 nbTris)
	{
		PxU32 initIndex = 0;	// PT: by default the first triangle to process is just the first one in the array
		if(cachedIndex)			// PT: but if we cached the last closest triangle from a previous call...
		{
			PX_ASSERT(*cachedIndex < nbTris);
			PX_UNUSED(nbTris);
			initIndex = *cachedIndex;	// PT: ...then we should start with that one, to potentially shrink the ray as early as possible
		}
		return initIndex;
	}

	// PT: quick triangle rejection for sphere-based sweeps.
	// Please refer to \\sw\physx\PhysXSDK\3.4\trunk\InternalDocumentation\GU\cullTriangle.png for details & diagram.
	PX_FORCE_INLINE bool cullTriangle(const PxVec3* PX_RESTRICT triVerts, const PxVec3& dir, PxReal radius, PxReal t, const PxReal dpc0)
	{
		// PT: project triangle on axis
		const PxReal dp0 = triVerts[0].dot(dir);
		const PxReal dp1 = triVerts[1].dot(dir);
		const PxReal dp2 = triVerts[2].dot(dir);

		// PT: keep min value = earliest possible impact distance
		PxReal dp = dp0;
		dp = physx::intrinsics::selectMin(dp, dp1);
		dp = physx::intrinsics::selectMin(dp, dp2);

		// PT: make sure we keep triangles that are about as close as best current distance
		radius += 0.001f + GU_EPSILON_SAME_DISTANCE;

		// PT: if earliest possible impact distance for this triangle is already larger than
		// sphere's current best known impact distance, we can skip the triangle
		if(dp>dpc0 + t + radius)
		{
			//PX_ASSERT(resx == 0.0f);
			return false;
		}

		// PT: if triangle is fully located before the sphere's initial position, skip it too
		const PxReal dpc1 = dpc0 - radius;
		if(dp0<dpc1 && dp1<dpc1 && dp2<dpc1)
		{
			//PX_ASSERT(resx == 0.0f);
			return false;
		}

		//PX_ASSERT(resx != 0.0f);
		return true;
	}

	// PT: quick quad rejection for sphere-based sweeps. Same as for triangle, adapted for one more vertex.
	PX_FORCE_INLINE bool cullQuad(const PxVec3* PX_RESTRICT quadVerts, const PxVec3& dir, PxReal radius, PxReal t, const PxReal dpc0)
	{
		// PT: project quad on axis
		const PxReal dp0 = quadVerts[0].dot(dir);
		const PxReal dp1 = quadVerts[1].dot(dir);
		const PxReal dp2 = quadVerts[2].dot(dir);
		const PxReal dp3 = quadVerts[3].dot(dir);

		// PT: keep min value = earliest possible impact distance
		PxReal dp = dp0;
		dp = physx::intrinsics::selectMin(dp, dp1);
		dp = physx::intrinsics::selectMin(dp, dp2);
		dp = physx::intrinsics::selectMin(dp, dp3);

		// PT: make sure we keep quads that are about as close as best current distance
		radius += 0.001f;

		// PT: if earliest possible impact distance for this quad is already larger than
		// sphere's current best known impact distance, we can skip the quad
		if(dp>dpc0 + t + radius)
			return false;

		// PT: if quad is fully located before the sphere's initial position, skip it too
		const float dpc1 = dpc0 - radius;
		if(dp0<dpc1 && dp1<dpc1 && dp2<dpc1 && dp3<dpc1)
			return false;

		return true;
	}

	// PT: computes distance between a point 'point' and a segment. The segment is defined as a starting point 'p0'
	// and a direction vector 'dir' plus a length 't'. Segment's endpoint is p0 + dir * t.
	//
	//                     point
	//                      o
	//                   __/|
	//                __/ / |
	//             __/   /  |(B)
	//          __/  (A)/   |
	//       __/       /    |                dir
	//  p0 o/---------o---------------o--    -->
	//                t (t<=fT)       t (t>fT)
	//                return (A)^2    return (B)^2
	//
	//     |<-------------->|
	//             fT
	//
	PX_FORCE_INLINE PxReal squareDistance(const PxVec3& p0, const PxVec3& dir, PxReal t, const PxVec3& point)
	{
		PxVec3 diff = point - p0;
		PxReal fT = diff.dot(dir);
		fT = physx::intrinsics::selectMax(fT, 0.0f);
		fT = physx::intrinsics::selectMin(fT, t);
		diff -= fT*dir;
		return diff.magnitudeSquared();
	}

	// PT: quick triangle culling for sphere-based sweeps
	// Please refer to \\sw\physx\PhysXSDK\3.4\trunk\InternalDocumentation\GU\coarseCulling.png for details & diagram.
	PX_FORCE_INLINE bool coarseCullingTri(const PxVec3& center, const PxVec3& dir, PxReal t, PxReal radius, const PxVec3* PX_RESTRICT triVerts)
	{
		// PT: compute center of triangle ### could be precomputed?
		const PxVec3 triCenter = (triVerts[0] + triVerts[1] + triVerts[2]) * (1.0f/3.0f);

		// PT: distance between the triangle center and the swept path (an LSS)
		// Same as: distancePointSegmentSquared(center, center+dir*t, TriCenter);
		PxReal d = PxSqrt(squareDistance(center, dir, t, triCenter)) - radius - 0.0001f;

		if (d < 0.0f)	// The triangle center lies inside the swept sphere
			return true;

		d*=d;

		// PT: coarse capsule-vs-triangle overlap test ### distances could be precomputed?
		if(1)
		{
			if(d <= (triCenter-triVerts[0]).magnitudeSquared())
				return true;
			if(d <= (triCenter-triVerts[1]).magnitudeSquared())
				return true;
			if(d <= (triCenter-triVerts[2]).magnitudeSquared())
				return true;
		}
		else
		{
			const float d0 = (triCenter-triVerts[0]).magnitudeSquared();
			const float d1 = (triCenter-triVerts[1]).magnitudeSquared();
			const float d2 = (triCenter-triVerts[2]).magnitudeSquared();
			float triRadius = physx::intrinsics::selectMax(d0, d1);
			triRadius = physx::intrinsics::selectMax(triRadius, d2);
			if(d <= triRadius)
				return true;
		}
		return false;
	}

	// PT: quick quad culling for sphere-based sweeps. Same as for triangle, adapted for one more vertex.
	PX_FORCE_INLINE bool coarseCullingQuad(const PxVec3& center, const PxVec3& dir, PxReal t, PxReal radius, const PxVec3* PX_RESTRICT quadVerts)
	{
		// PT: compute center of quad ### could be precomputed?
		const PxVec3 quadCenter = (quadVerts[0] + quadVerts[1] + quadVerts[2] + quadVerts[3]) * (1.0f/4.0f);

		// PT: distance between the quad center and the swept path (an LSS)
		PxReal d = PxSqrt(squareDistance(center, dir, t, quadCenter)) - radius - 0.0001f;

		if (d < 0.0f)	// The quad center lies inside the swept sphere
			return true;

		d*=d;

		// PT: coarse capsule-vs-quad overlap test ### distances could be precomputed?
		if(1)
		{
			if(d <= (quadCenter-quadVerts[0]).magnitudeSquared())
				return true;
			if(d <= (quadCenter-quadVerts[1]).magnitudeSquared())
				return true;
			if(d <= (quadCenter-quadVerts[2]).magnitudeSquared())
				return true;
			if(d <= (quadCenter-quadVerts[3]).magnitudeSquared())
				return true;
		}
		return false;
	}

	// PT: combined triangle culling for sphere-based sweeps
	PX_FORCE_INLINE bool rejectTriangle(const PxVec3& center, const PxVec3& unitDir, PxReal curT, PxReal radius, const PxVec3* PX_RESTRICT triVerts, const PxReal dpc0)
	{
		if(!coarseCullingTri(center, unitDir, curT, radius, triVerts))
			return true;
		if(!cullTriangle(triVerts, unitDir, radius, curT, dpc0))
			return true;
		return false;
	}

	// PT: combined quad culling for sphere-based sweeps
	PX_FORCE_INLINE bool rejectQuad(const PxVec3& center, const PxVec3& unitDir, PxReal curT, PxReal radius, const PxVec3* PX_RESTRICT quadVerts, const PxReal dpc0)
	{
		if(!coarseCullingQuad(center, unitDir, curT, radius, quadVerts))
			return true;
		if(!cullQuad(quadVerts, unitDir, radius, curT, dpc0))
			return true;
		return false;
	}

	PX_FORCE_INLINE bool shouldFlipNormal(const PxVec3& normal, bool meshBothSides, bool isDoubleSided, const PxVec3& triangleNormal, const PxVec3& dir)
	{
		// PT: this function assumes that input normal is opposed to the ray/sweep direction. This is always
		// what we want except when we hit a single-sided back face with 'meshBothSides' enabled.

		if(!meshBothSides || isDoubleSided)
			return false;

		PX_ASSERT(normal.dot(dir) <= 0.0f);	// PT: if this fails, the logic below cannot be applied
		PX_UNUSED(normal);
		return triangleNormal.dot(dir) > 0.0f;	// PT: true for back-facing hits
	}

	PX_FORCE_INLINE bool shouldFlipNormal(const PxVec3& normal, bool meshBothSides, bool isDoubleSided, const PxTriangle& triangle, const PxVec3& dir, const PxTransform* pose)
	{
		// PT: this function assumes that input normal is opposed to the ray/sweep direction. This is always
		// what we want except when we hit a single-sided back face with 'meshBothSides' enabled.

		if(!meshBothSides || isDoubleSided)
			return false;

		PX_ASSERT(normal.dot(dir) <= 0.0f);	// PT: if this fails, the logic below cannot be applied
		PX_UNUSED(normal);

		PxVec3 triangleNormal;
		triangle.denormalizedNormal(triangleNormal);

		if(pose)
			triangleNormal = pose->rotate(triangleNormal);

		return triangleNormal.dot(dir) > 0.0f;	// PT: true for back-facing hits
	}

	// PT: implements the spec for IO sweeps in a single place (to ensure consistency)
	PX_FORCE_INLINE bool setInitialOverlapResults(PxSweepHit& hit, const PxVec3& unitDir, PxU32 faceIndex)
	{
		// PT: please write these fields in the order they are listed in the struct.
		hit.faceIndex	= faceIndex;
		hit.flags		= PxHitFlag::eDISTANCE|PxHitFlag::eNORMAL|PxHitFlag::eFACE_INDEX;
		hit.normal		= -unitDir;
		hit.distance	= 0.0f;
		return true;	// PT: true indicates a hit, saves some lines in calling code
	}

	PX_FORCE_INLINE void computeBoxLocalImpact(	PxVec3& pos, PxVec3& normal, PxHitFlags& outFlags,
												const Box& box, const PxVec3& localDir, const PxTriangle& triInBoxSpace,
												const PxHitFlags inFlags, bool isDoubleSided, bool meshBothSides, PxReal impactDist)
	{
		if(inFlags & (PxHitFlag::eNORMAL|PxHitFlag::ePOSITION))
		{			
			PxVec3 localPos, localNormal;
			computeBoxTriImpactData(localPos, localNormal, box.extents, localDir, triInBoxSpace, impactDist);

			if(inFlags & PxHitFlag::eNORMAL)
			{
				localNormal.normalize();

				// PT: doing this after the 'rotate' minimizes errors when normal and dir are close to perpendicular
				// ....but we must do it before the rotate now, because triangleNormal is in box space (and thus we
				// need the normal with the proper orientation, in box space. We can't fix it after it's been rotated
				// to box space.
				// Technically this one is only here because of the EE cross product in the feature-based sweep.
				// PT: TODO: revisit corresponding code in computeImpactData, get rid of ambiguity
				// PT: TODO: this may not be needed anymore
				if((localNormal.dot(localDir))>0.0f)
					localNormal = -localNormal;

				// PT: this one is to ensure the normal respects the mesh-both-sides/double-sided convention
				if(shouldFlipNormal(localNormal, meshBothSides, isDoubleSided, triInBoxSpace, localDir, NULL))
					localNormal = -localNormal;

				normal = box.rotate(localNormal);
				outFlags |= PxHitFlag::eNORMAL;
			}

			if(inFlags & PxHitFlag::ePOSITION)
			{
				pos = box.transform(localPos);
				outFlags |= PxHitFlag::ePOSITION;
			}
		}
	}

} // namespace Gu

}

#endif