RavEngine
Loading...
Searching...
No Matches
GuInternal.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef GU_INTERNAL_H
30#define GU_INTERNAL_H
31
32#include "geometry/PxCapsuleGeometry.h"
33#include "geometry/PxBoxGeometry.h"
34#include "GuCapsule.h"
35#include "foundation/PxTransform.h"
36#include "foundation/PxMathUtils.h"
37#include "foundation/PxUtilities.h"
38#include "foundation/PxMat33.h"
39
40#define GU_EPSILON_SAME_DISTANCE 1e-3f
41
42namespace physx
43{
44namespace Gu
45{
46 class Box;
47
48 // PT: TODO: now that the Gu files are not exposed to users anymore, we should move back capsule-related functions
49 // to GuCapsule.h, etc
50
51 PX_PHYSX_COMMON_API const PxU8* getBoxEdges();
52
53 PX_PHYSX_COMMON_API void computeBoxPoints(const PxBounds3& bounds, PxVec3* PX_RESTRICT pts);
54
55 void computeBoxAroundCapsule(const Capsule& capsule, Box& box);
56
57 PxPlane getPlane(const PxTransform& pose);
58
59 PX_FORCE_INLINE PxVec3 getCapsuleHalfHeightVector(const PxTransform& transform, const PxCapsuleGeometry& capsuleGeom)
60 {
61 return transform.q.getBasisVector0() * capsuleGeom.halfHeight;
62 }
63
64 PX_FORCE_INLINE void getCapsuleSegment(const PxTransform& transform, const PxCapsuleGeometry& capsuleGeom, Gu::Segment& segment)
65 {
66 const PxVec3 tmp = getCapsuleHalfHeightVector(transform, capsuleGeom);
67 segment.p0 = transform.p + tmp;
68 segment.p1 = transform.p - tmp;
69 }
70
71 PX_FORCE_INLINE void getCapsule(Gu::Capsule& capsule, const PxCapsuleGeometry& capsuleGeom, const PxTransform& pose)
72 {
73 getCapsuleSegment(pose, capsuleGeom, capsule);
74 capsule.radius = capsuleGeom.radius;
75 }
76
77 void computeSweptBox(Gu::Box& box, const PxVec3& extents, const PxVec3& center, const PxMat33& rot, const PxVec3& unitDir, const PxReal distance);
78
88 PX_FORCE_INLINE PxReal computeAlignmentValue(const PxVec3& triNormal, const PxVec3& unitDir)
89 {
90 PX_ASSERT(triNormal.isNormalized());
91 // PT: initial dot product gives the angle between the two, with "best" triangles getting a +1 or -1 score
92 // depending on their winding. We take the absolute value to ignore the impact of winding. We negate the result
93 // to make the function compatible with the initial code, which assumed single-sided triangles and expected -1
94 // for best triangles.
95 return -PxAbs(triNormal.dot(unitDir));
96 }
97
110 PX_FORCE_INLINE bool keepTriangle( float triImpactDistance, float triAlignmentValue,
111 float bestImpactDistance, float bestAlignmentValue, float maxDistance)
112 {
113 // Reject triangle if further than the maxDistance
114 if(triImpactDistance > maxDistance)
115 return false;
116
117 // If initial overlap happens, keep the triangle
118 if(triImpactDistance == 0.0f)
119 return true;
120
121 // tris have "similar" impact distances if the difference is smaller than 2*distEpsilon
122 float distEpsilon = GU_EPSILON_SAME_DISTANCE; // pick a farther hit within distEpsilon that is more opposing than the previous closest hit
123
124 // PT: make it a relative epsilon to make sure it still works with large distances
125 distEpsilon *= PxMax(1.0f, PxMax(triImpactDistance, bestImpactDistance));
126
127 // If new distance is more than epsilon closer than old distance
128 if(triImpactDistance < bestImpactDistance - distEpsilon)
129 return true;
130
131 // If new distance is no more than epsilon farther than oldDistance and "face is more opposing than previous"
132 if(triImpactDistance < bestImpactDistance+distEpsilon && triAlignmentValue < bestAlignmentValue)
133 return true;
134
135 // If alignment value is the same, but the new triangle is closer than the best distance
136 if(triAlignmentValue == bestAlignmentValue && triImpactDistance < bestImpactDistance)
137 return true;
138
139 return false;
140 }
141
142 PX_FORCE_INLINE bool keepTriangleBasic(float triImpactDistance, float bestImpactDistance, float maxDistance)
143 {
144 // Reject triangle if further than the maxDistance
145 if(triImpactDistance > maxDistance)
146 return false;
147
148 // If initial overlap happens, keep the triangle
149 if(triImpactDistance == 0.0f)
150 return true;
151
152 // If new distance is more than epsilon closer than old distance
153 if(triImpactDistance < bestImpactDistance)
154 return true;
155
156 return false;
157 }
158
159 PX_FORCE_INLINE PxVec3 cross100(const PxVec3& b)
160 {
161 return PxVec3(0.0f, -b.z, b.y);
162 }
163 PX_FORCE_INLINE PxVec3 cross010(const PxVec3& b)
164 {
165 return PxVec3(b.z, 0.0f, -b.x);
166 }
167 PX_FORCE_INLINE PxVec3 cross001(const PxVec3& b)
168 {
169 return PxVec3(-b.y, b.x, 0.0f);
170 }
171
173 PX_FORCE_INLINE PxVec3 computeBarycentricPoint(const PxVec3& p0, const PxVec3& p1, const PxVec3& p2, PxReal u, PxReal v)
174 {
175 // This seems to confuse the compiler...
176 // return (1.0f - u - v)*p0 + u*p1 + v*p2;
177 const PxF32 w = 1.0f - u - v;
178 return PxVec3(w * p0.x + u * p1.x + v * p2.x, w * p0.y + u * p1.y + v * p2.y, w * p0.z + u * p1.z + v * p2.z);
179 }
180
181 PX_FORCE_INLINE PxReal computeTetrahedronVolume(const PxVec3& x0, const PxVec3& x1, const PxVec3& x2, const PxVec3& x3, PxMat33& edgeMatrix)
182 {
183 const PxVec3 u1 = x1 - x0;
184 const PxVec3 u2 = x2 - x0;
185 const PxVec3 u3 = x3 - x0;
186
187 edgeMatrix = PxMat33(u1, u2, u3);
188
189 const PxReal det = edgeMatrix.getDeterminant();
190
191 const PxReal volume = det / 6.0f;
192 return volume;
193 }
194
195 PX_FORCE_INLINE PxReal computeTetrahedronVolume(const PxVec3& x0, const PxVec3& x1, const PxVec3& x2, const PxVec3& x3)
196 {
197 PxMat33 edgeMatrix;
198 return computeTetrahedronVolume(x0, x1, x2, x3, edgeMatrix);
199 }
200
201 // IndexType should be PxU16 or PxU32.
202 template<typename IndexType>
203 PX_FORCE_INLINE PxReal computeTriangleMeshVolume(const PxVec3* vertices, const IndexType* indices,
204 const PxU32 numTriangles)
205 {
206 // See https://twitter.com/keenanisalive/status/1437178786286653445?lang=en
207 float volume = 0.0f;
208
209 for(PxU32 i = 0; i < numTriangles; ++i)
210 {
211 PxVec3 v0 = vertices[indices[3*i]];
212 PxVec3 v1 = vertices[indices[3 * i + 1]];
213 PxVec3 v2 = vertices[indices[3 * i + 2]];
214
215 PxVec3 v0v1 = v0.cross(v1);
216
217 volume += v0v1.dot(v2);
218 }
219 return volume / 6.0f;
220 }
221
222 // IndexType should be PxU16 or PxU32.
223 // W in PxVec4 of vertices are ignored.
224 template <typename IndexType>
225 PX_FORCE_INLINE PxReal computeTriangleMeshVolume(const PxVec4* vertices, const IndexType* indices,
226 const PxU32 numTriangles)
227 {
228 // See https://twitter.com/keenanisalive/status/1437178786286653445?lang=en
229 float volume = 0.0f;
230
231 for(PxU32 i = 0; i < numTriangles; ++i)
232 {
233 PxVec3 v0 = vertices[indices[3 * i]].getXYZ();
234 PxVec3 v1 = vertices[indices[3 * i + 1]].getXYZ();
235 PxVec3 v2 = vertices[indices[3 * i + 2]].getXYZ();
236
237 PxVec3 v0v1 = v0.cross(v1);
238
239 volume += v0v1.dot(v2);
240 }
241 return volume / 6.0f;
242 }
243
247 PX_FORCE_INLINE void makeFatEdge(PxVec3& p0, PxVec3& p1, PxReal fatCoeff)
248 {
249 PxVec3 delta = p1 - p0;
250
251 const PxReal m = delta.magnitude();
252 if (m > 0.0f)
253 {
254 delta *= fatCoeff / m;
255 p0 -= delta;
256 p1 += delta;
257 }
258 }
259
260#if 0
264 PX_FORCE_INLINE void makeFatEdge(aos::Vec3V& p0, aos::Vec3V& p1, const aos::FloatVArg fatCoeff)
265 {
266 const aos::Vec3V delta = aos::V3Sub(p1, p0);
267 const aos::FloatV m = aos::V3Length(delta);
268 const aos::BoolV con = aos::FIsGrtr(m, aos::FZero());
269 const aos::Vec3V fatDelta = aos::V3Scale(aos::V3ScaleInv(delta, m), fatCoeff);
270 p0 = aos::V3Sel(con, aos::V3Sub(p0, fatDelta), p0);
271 p1 = aos::V3Sel(con, aos::V3Add(p1, fatDelta), p1);
272 }
273#endif
274
275 PX_FORCE_INLINE PxU32 closestAxis(const PxVec3& v, PxU32& j, PxU32& k)
276 {
277 // find largest 2D plane projection
278 const PxF32 absPx = PxAbs(v.x);
279 const PxF32 absNy = PxAbs(v.y);
280 const PxF32 absNz = PxAbs(v.z);
281
282 PxU32 m = 0; // x biggest axis
283 j = 1;
284 k = 2;
285 if (absNy > absPx && absNy > absNz)
286 {
287 // y biggest
288 j = 2;
289 k = 0;
290 m = 1;
291 }
292 else if (absNz > absPx)
293 {
294 // z biggest
295 j = 0;
296 k = 1;
297 m = 2;
298 }
299 return m;
300 }
301
302 PX_FORCE_INLINE bool isAlmostZero(const PxVec3& v)
303 {
304 if (PxAbs(v.x) > 1e-6f || PxAbs(v.y) > 1e-6f || PxAbs(v.z) > 1e-6f)
305 return false;
306 return true;
307 }
308
309} // namespace Gu
310
311}
312
313#endif
3x3 matrix class
Definition PxMat33.h:91
PX_CUDA_CALLABLE PX_INLINE float getDeterminant() const
Get determinant.
Definition PxMat33.h:230
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T cross(const PxVec3T &v) const
cross product
Definition PxVec3.h:282
3 Element vector class.
Definition PxVec3.h:50
PX_CUDA_CALLABLE PX_FORCE_INLINE float dot(const PxVec3 &v) const
returns the scalar product of this and other.
Definition PxVec3.h:276
PX_CUDA_CALLABLE PX_FORCE_INLINE float magnitude() const
returns the magnitude
Definition PxVec3.h:183
PX_CUDA_CALLABLE PX_FORCE_INLINE bool isNormalized() const
is normalized - used by API parameter validation
Definition PxVec3.h:164
#define PX_RESTRICT
Definition PxPreprocessor.h:355
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
transform(pattern)
Definition docopt.py:72
PX_FORCE_INLINE bool keepTriangle(float triImpactDistance, float triAlignmentValue, float bestImpactDistance, float bestAlignmentValue, float maxDistance)
Definition GuInternal.h:110
PX_PHYSX_COMMON_API void computeBoxPoints(const PxBounds3 &bounds, PxVec3 *PX_RESTRICT pts)
Definition GuInternal.cpp:44
PX_FORCE_INLINE void makeFatEdge(PxVec3 &p0, PxVec3 &p1, PxReal fatCoeff)
Definition GuInternal.h:247
PX_FORCE_INLINE PxVec3 computeBarycentricPoint(const PxVec3 &p0, const PxVec3 &p1, const PxVec3 &p2, PxReal u, PxReal v)
Compute point as combination of barycentric coordinates.
Definition GuInternal.h:173
PX_FORCE_INLINE PxReal computeAlignmentValue(const PxVec3 &triNormal, const PxVec3 &unitDir)
Definition GuInternal.h:88
PX_PHYSX_COMMON_API const PxU8 * getBoxEdges()
Definition GuBox.cpp:62
void computeBoxAroundCapsule(const Capsule &capsule, Box &box)
Definition GuCapsule.cpp:40
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAbs(float a)
abs returns the absolute value of its argument.
Definition PxMath.h:109
PX_CUDA_CALLABLE PX_FORCE_INLINE T PxMax(T a, T b)
The return value is the greater of the two specified values.
Definition PxMath.h:72
Definition PxVecMathAoSScalar.h:90
Definition PxVecMathAoSScalar.h:52
Definition PxVecMathAoSScalar.h:77