RavEngine
Loading...
Searching...
No Matches
GuSweepConvexTri.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_SWEEP_CONVEX_TRI
30#define GU_SWEEP_CONVEX_TRI
31
32#include "geometry/PxConvexMeshGeometry.h"
33#include "GuVecTriangle.h"
34#include "GuVecConvexHull.h"
35#include "GuConvexMesh.h"
36#include "GuGJKRaycast.h"
37
38// return true if hit, false if no hit
39static PX_FORCE_INLINE bool sweepConvexVsTriangle(
40 const PxVec3& v0, const PxVec3& v1, const PxVec3& v2,
41 ConvexHullV& convexHull, const aos::PxMatTransformV& meshToConvex, const aos::PxTransformV& convexTransfV,
42 const aos::Vec3VArg convexSpaceDir, const PxVec3& unitDir, const PxVec3& meshSpaceUnitDir,
43 const aos::FloatVArg fullDistance, PxReal shrunkDistance,
44 PxGeomSweepHit& hit, bool isDoubleSided, const PxReal inflation, bool& initialOverlap, PxU32 faceIndex)
45{
46 using namespace aos;
47 if(!isDoubleSided)
48 {
49 // Create triangle normal
50 const PxVec3 denormalizedNormal = (v1 - v0).cross(v2 - v1);
51
52 // Backface culling
53 // PT: WARNING, the test is reversed compared to usual because we pass -unitDir to this function
54 const bool culled = denormalizedNormal.dot(meshSpaceUnitDir) <= 0.0f;
55 if(culled)
56 return false;
57 }
58
59 const Vec3V zeroV = V3Zero();
60 const FloatV zero = FZero();
61
62 const Vec3V p0 = V3LoadU(v0); // in mesh local space
63 const Vec3V p1 = V3LoadU(v1);
64 const Vec3V p2 = V3LoadU(v2);
65
66 // transform triangle verts from mesh local to convex local space
67 TriangleV triangleV(meshToConvex.transform(p0), meshToConvex.transform(p1), meshToConvex.transform(p2));
68
69 FloatV toi;
70 Vec3V closestA,normal;
71
72 const LocalConvex<TriangleV> convexA(triangleV);
73 const LocalConvex<ConvexHullV> convexB(convexHull);
74 const Vec3V initialSearchDir = V3Sub(triangleV.getCenter(), convexHull.getCenter());
75 // run GJK raycast
76 // sweep triangle in convex local space vs convex, closestA will be the impact point in convex local space
77 const bool gjkHit = gjkRaycastPenetration<LocalConvex<TriangleV>, LocalConvex<ConvexHullV> >(
78 convexA, convexB, initialSearchDir, zero, zeroV, convexSpaceDir, toi, normal, closestA, inflation, false);
79 if(!gjkHit)
80 return false;
81
82 if(FAllGrtrOrEq(zero, toi))
83 {
84 initialOverlap = true; // PT: TODO: redundant with hit distance, consider removing
85 return setInitialOverlapResults(hit, unitDir, faceIndex);
86 }
87
88 const FloatV minDist = FLoad(shrunkDistance);
89 const FloatV dist = FMul(toi, fullDistance); // scale the toi to original full sweep distance
90 if(FAllGrtr(minDist, dist)) // is current dist < minDist?
91 {
92 hit.faceIndex = faceIndex;
93 hit.flags = PxHitFlag::ePOSITION | PxHitFlag::eNORMAL | PxHitFlag::eFACE_INDEX;
94 const Vec3V destWorldPointA = convexTransfV.transform(closestA);
95 const Vec3V destNormal = V3Normalize(convexTransfV.rotate(normal));
96 V3StoreU(destWorldPointA, hit.position);
97 V3StoreU(destNormal, hit.normal);
98 FStore(dist, &hit.distance);
99 return true; // report a hit
100 }
101 return false; // report no hit
102}
103
104#endif
GLM_FUNC_QUALIFIER vec< 3, T, Q > cross(vec< 3, T, Q > const &x, vec< 3, T, Q > const &y)
Definition func_geometric.inl:175
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
GLM_FUNC_DECL GLM_CONSTEXPR genType zero()
Definition constants.inl:6