RavEngine
Loading...
Searching...
No Matches
GuIntersectionCapsuleTriangle.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_INTERSECTION_CAPSULE_TRIANGLE_H
30#define GU_INTERSECTION_CAPSULE_TRIANGLE_H
31
32#include "GuCapsule.h"
33#include "foundation/PxUtilities.h"
34
35namespace physx
36{
37namespace Gu
38{
39 // PT: precomputed data for capsule-triangle test. Useful when testing the same capsule vs several triangles.
41 {
42 PxVec3 mCapsuleDir;
43 float mBDotB;
44 float mOneOverBDotB;
45
46 void init(const Capsule& capsule)
47 {
48 const PxVec3 dir = capsule.p1 - capsule.p0;
49 const float BDotB = dir.dot(dir);
50 mCapsuleDir = dir;
51 mBDotB = BDotB;
52 mOneOverBDotB = BDotB!=0.0f ? 1.0f/BDotB : 0.0f;
53 }
54 };
55
56 // PT: tests if projections of capsule & triangle overlap on given axis
57 PX_FORCE_INLINE PxU32 testAxis(const PxVec3& p0, const PxVec3& p1, const PxVec3& p2, const Capsule& capsule, const PxVec3& axis)
58 {
59 // Project capsule
60 float min0 = capsule.p0.dot(axis);
61 float max0 = capsule.p1.dot(axis);
62 if(min0>max0)
63 PxSwap(min0, max0);
64 const float MR = axis.magnitude()*capsule.radius;
65 min0 -= MR;
66 max0 += MR;
67
68 // Project triangle
69 float min1, max1;
70 {
71 min1 = max1 = p0.dot(axis);
72 float dp = p1.dot(axis);
73 if(dp<min1) min1 = dp;
74 if(dp>max1) max1 = dp;
75 dp = p2.dot(axis);
76 if(dp<min1) min1 = dp;
77 if(dp>max1) max1 = dp;
78 }
79
80 // Test projections
81 if(max0<min1 || max1<min0)
82 return 0;
83
84 return 1;
85 }
86
87 // PT: computes shortest vector going from capsule axis to triangle edge
88 PX_FORCE_INLINE PxVec3 computeEdgeAxis( const PxVec3& p, const PxVec3& a,
89 const PxVec3& q, const PxVec3& b,
90 float BDotB, float oneOverBDotB)
91 {
92 const PxVec3 T = q - p;
93 const float ADotA = a.dot(a);
94 const float ADotB = a.dot(b);
95 const float ADotT = a.dot(T);
96 const float BDotT = b.dot(T);
97
98 const float denom = ADotA*BDotB - ADotB*ADotB;
99
100 float t = denom!=0.0f ? (ADotT*BDotB - BDotT*ADotB) / denom : 0.0f;
101 t = PxClamp(t, 0.0f, 1.0f);
102
103 float u = (t*ADotB - BDotT) * oneOverBDotB;
104
105 if(u<0.0f)
106 {
107 u = 0.0f;
108 t = ADotT / ADotA;
109 t = PxClamp(t, 0.0f, 1.0f);
110 }
111 else if(u>1.0f)
112 {
113 u = 1.0f;
114 t = (ADotB + ADotT) / ADotA;
115 t = PxClamp(t, 0.0f, 1.0f);
116 }
117 return T + b*u - a*t;
118 }
119
131 bool intersectCapsuleTriangle(const PxVec3& normal, const PxVec3& p0, const PxVec3& p1, const PxVec3& p2, const Gu::Capsule& capsule, const CapsuleTriangleOverlapData& params);
132}
133}
134
135#endif
Represents a capsule.
Definition GuCapsule.h:47
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
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
bool intersectCapsuleTriangle(const PxVec3 &normal, const PxVec3 &p0, const PxVec3 &p1, const PxVec3 &p2, const Gu::Capsule &capsule, const CapsuleTriangleOverlapData &params)
Definition GuIntersectionCapsuleTriangle.cpp:35
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 T PxClamp(T v, T lo, T hi)
Clamps v to the range [hi,lo].
Definition PxMath.h:139
Definition GuIntersectionCapsuleTriangle.h:41