RavEngine
Loading...
Searching...
No Matches
GuIntersectionRayTriangle.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_RAY_TRIANGLE_H
30#define GU_INTERSECTION_RAY_TRIANGLE_H
31
32#include "foundation/PxVec3.h"
33#include "common/PxPhysXCommonConfig.h"
34
35namespace physx
36{
37
38namespace Gu
39{
40 // PT: this is used for backface culling. It existed in Moller's original code already. Basically this is only to avoid dividing by zero.
41 // This should not depend on what units are used, and neither should it depend on the size of triangles. A large triangle with the same
42 // orientation as a small triangle should be backface culled the same way. A triangle whose orientation does not change should not suddenly
43 // become culled or visible when we scale it.
44 //
45 // An absolute epsilon is fine here. The computation will work fine for small triangles, and large triangles will simply make 'det' larger,
46 // more and more inaccurate, but it won't suddenly make it negative.
47 //
48 // Using FLT_EPSILON^2 ensures that triangles whose edges are smaller than FLT_EPSILON long are rejected. This epsilon makes the code work
49 // for very small triangles, while still preventing divisions by too small values.
50 #define GU_CULLING_EPSILON_RAY_TRIANGLE FLT_EPSILON*FLT_EPSILON
51
53
72 PX_FORCE_INLINE bool intersectRayTriangle( const PxVec3& orig, const PxVec3& dir,
73 const PxVec3& vert0, const PxVec3& vert1, const PxVec3& vert2,
74 PxReal& at, PxReal& au, PxReal& av,
75 bool cull, float enlarge=0.0f)
76 {
77 // Find vectors for two edges sharing vert0
78 const PxVec3 edge1 = vert1 - vert0;
79 const PxVec3 edge2 = vert2 - vert0;
80
81 // Begin calculating determinant - also used to calculate U parameter
82 const PxVec3 pvec = dir.cross(edge2); // error ~ |v2-v0|
83
84 // If determinant is near zero, ray lies in plane of triangle
85 const PxReal det = edge1.dot(pvec); // error ~ |v2-v0|*|v1-v0|
86
87 if(cull)
88 {
89 if(det<GU_CULLING_EPSILON_RAY_TRIANGLE)
90 return false;
91
92 // Calculate distance from vert0 to ray origin
93 const PxVec3 tvec = orig - vert0;
94
95 // Calculate U parameter and test bounds
96 const PxReal u = tvec.dot(pvec);
97
98 const PxReal enlargeCoeff = enlarge*det;
99 const PxReal uvlimit = -enlargeCoeff;
100 const PxReal uvlimit2 = det + enlargeCoeff;
101
102 if(u<uvlimit || u>uvlimit2)
103 return false;
104
105 // Prepare to test V parameter
106 const PxVec3 qvec = tvec.cross(edge1);
107
108 // Calculate V parameter and test bounds
109 const PxReal v = dir.dot(qvec);
110 if(v<uvlimit || (u+v)>uvlimit2)
111 return false;
112
113 // Calculate t, scale parameters, ray intersects triangle
114 const PxReal t = edge2.dot(qvec);
115
116 const PxReal inv_det = 1.0f / det;
117 at = t*inv_det;
118 au = u*inv_det;
119 av = v*inv_det;
120 }
121 else
122 {
123 // the non-culling branch
124 if(PxAbs(det)<GU_CULLING_EPSILON_RAY_TRIANGLE)
125 return false;
126
127 const PxReal inv_det = 1.0f / det;
128
129 // Calculate distance from vert0 to ray origin
130 const PxVec3 tvec = orig - vert0; // error ~ |orig-v0|
131
132 // Calculate U parameter and test bounds
133 const PxReal u = tvec.dot(pvec) * inv_det;
134 if(u<-enlarge || u>1.0f+enlarge)
135 return false;
136
137 // prepare to test V parameter
138 const PxVec3 qvec = tvec.cross(edge1);
139
140 // Calculate V parameter and test bounds
141 const PxReal v = dir.dot(qvec) * inv_det;
142 if(v<-enlarge || (u+v)>1.0f+enlarge)
143 return false;
144
145 // Calculate t, ray intersects triangle
146 const PxReal t = edge2.dot(qvec) * inv_det;
147
148 at = t;
149 au = u;
150 av = v;
151 }
152 return true;
153 }
154
155 /* \note u, v and t will remain unchanged if false is returned. */
156 PX_FORCE_INLINE bool intersectRayTriangleCulling( const PxVec3& orig, const PxVec3& dir,
157 const PxVec3& vert0, const PxVec3& vert1, const PxVec3& vert2,
158 PxReal& t, PxReal& u, PxReal& v,
159 float enlarge=0.0f)
160 {
161 return intersectRayTriangle(orig, dir, vert0, vert1, vert2, t, u, v, true, enlarge);
162 }
163
164 /* \note u, v and t will remain unchanged if false is returned. */
165 PX_FORCE_INLINE bool intersectRayTriangleNoCulling( const PxVec3& orig, const PxVec3& dir,
166 const PxVec3& vert0, const PxVec3& vert1, const PxVec3& vert2,
167 PxReal& t, PxReal& u, PxReal& v,
168 float enlarge=0.0f)
169 {
170 return intersectRayTriangle(orig, dir, vert0, vert1, vert2, t, u, v, false, enlarge);
171 }
172
173} // namespace Gu
174
175}
176
177#endif
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 PxVec3 cross(const PxVec3 &v) const
cross product
Definition PxVec3.h:284
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
PX_FORCE_INLINE bool intersectRayTriangle(const PxVec3 &orig, const PxVec3 &dir, const PxVec3 &vert0, const PxVec3 &vert1, const PxVec3 &vert2, PxReal &at, PxReal &au, PxReal &av, bool cull, float enlarge=0.0f)
Definition GuIntersectionRayTriangle.h:72
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