RavEngine
Loading...
Searching...
No Matches
GuDistancePointTriangle.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_DISTANCE_POINT_TRIANGLE_H
30#define GU_DISTANCE_POINT_TRIANGLE_H
31
32#include "foundation/PxVec3.h"
33#include "common/PxPhysXCommonConfig.h"
34
35namespace physx
36{
37namespace Gu
38{
39 // PT: special version:
40 // - inlined
41 // - doesn't compute (s,t) output params
42 // - expects precomputed edges in input
43 PX_FORCE_INLINE PX_CUDA_CALLABLE PxVec3 closestPtPointTriangle2(const PxVec3& p, const PxVec3& a, const PxVec3& b, const PxVec3& c, const PxVec3& ab, const PxVec3& ac)
44 {
45 // Check if P in vertex region outside A
46 //const PxVec3 ab = b - a;
47 //const PxVec3 ac = c - a;
48 const PxVec3 ap = p - a;
49 const float d1 = ab.dot(ap);
50 const float d2 = ac.dot(ap);
51 if(d1<=0.0f && d2<=0.0f)
52 return a; // Barycentric coords 1,0,0
53
54 // Check if P in vertex region outside B
55 const PxVec3 bp = p - b;
56 const float d3 = ab.dot(bp);
57 const float d4 = ac.dot(bp);
58 if(d3>=0.0f && d4<=d3)
59 return b; // Barycentric coords 0,1,0
60
61 // Check if P in edge region of AB, if so return projection of P onto AB
62 const float vc = d1*d4 - d3*d2;
63 if(vc<=0.0f && d1>=0.0f && d3<=0.0f)
64 {
65 const float v = d1 / (d1 - d3);
66 return a + v * ab; // barycentric coords (1-v, v, 0)
67 }
68
69 // Check if P in vertex region outside C
70 const PxVec3 cp = p - c;
71 const float d5 = ab.dot(cp);
72 const float d6 = ac.dot(cp);
73 if(d6>=0.0f && d5<=d6)
74 return c; // Barycentric coords 0,0,1
75
76 // Check if P in edge region of AC, if so return projection of P onto AC
77 const float vb = d5*d2 - d1*d6;
78 if(vb<=0.0f && d2>=0.0f && d6<=0.0f)
79 {
80 const float w = d2 / (d2 - d6);
81 return a + w * ac; // barycentric coords (1-w, 0, w)
82 }
83
84 // Check if P in edge region of BC, if so return projection of P onto BC
85 const float va = d3*d6 - d5*d4;
86 if(va<=0.0f && (d4-d3)>=0.0f && (d5-d6)>=0.0f)
87 {
88 const float w = (d4-d3) / ((d4 - d3) + (d5-d6));
89 return b + w * (c-b); // barycentric coords (0, 1-w, w)
90 }
91
92 // P inside face region. Compute Q through its barycentric coords (u,v,w)
93 const float denom = 1.0f / (va + vb + vc);
94 const float v = vb * denom;
95 const float w = vc * denom;
96 return a + ab*v + ac*w;
97 }
98
99 PX_PHYSX_COMMON_API PxVec3 closestPtPointTriangle(const PxVec3& p, const PxVec3& a, const PxVec3& b, const PxVec3& c, float& s, float& t);
100
101 PX_FORCE_INLINE PxReal distancePointTriangleSquared(const PxVec3& point,
102 const PxVec3& triangleOrigin,
103 const PxVec3& triangleEdge0,
104 const PxVec3& triangleEdge1,
105 PxReal* param0=NULL,
106 PxReal* param1=NULL)
107 {
108 const PxVec3 pt0 = triangleEdge0 + triangleOrigin;
109 const PxVec3 pt1 = triangleEdge1 + triangleOrigin;
110 float s,t;
111 const PxVec3 cp = closestPtPointTriangle(point, triangleOrigin, pt0, pt1, s, t);
112 if(param0)
113 *param0 = s;
114 if(param1)
115 *param1 = t;
116 return (cp - point).magnitudeSquared();
117 }
118
119} // namespace Gu
120
121}
122
123#endif
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39