Main Page Class List Class Members

NvBlastExtAuthoringInternalCommon.h
Go to the documentation of this file.
1 // This code contains NVIDIA Confidential Information and is disclosed to you
2 // under a form of NVIDIA software license agreement provided separately to you.
3 //
4 // Notice
5 // NVIDIA Corporation and its licensors retain all intellectual property and
6 // proprietary rights in and to this software and related documentation and
7 // any modifications thereto. Any use, reproduction, disclosure, or
8 // distribution of this software and related documentation without an express
9 // license agreement from NVIDIA Corporation is strictly prohibited.
10 //
11 // ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
12 // NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
13 // THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
14 // MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
15 //
16 // Information and code furnished is believed to be accurate and reliable.
17 // However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
18 // information or for any infringement of patents or other rights of third parties that may
19 // result from its use. No license is granted by implication or otherwise under any patent
20 // or patent rights of NVIDIA Corporation. Details are subject to change without notice.
21 // This code supersedes and replaces all information previously supplied.
22 // NVIDIA Corporation products are not authorized for use as critical
23 // components in life support devices or systems without express written approval of
24 // NVIDIA Corporation.
25 //
26 // Copyright (c) 2020 NVIDIA Corporation. All rights reserved.
27
28
29 #ifndef NVBLASTINTERNALCOMMON_H
30 #define NVBLASTINTERNALCOMMON_H
31 #include "NvBlastExtAuthoringTypes.h"
32 #include "NvBlastPxSharedHelpers.h"
33 #include <PxVec2.h>
34 #include <PxVec3.h>
35 #include <PxPlane.h>
36 #include <PxBounds3.h>
37 #include <PxMath.h>
38 #include <algorithm>
39
40 namespace Nv
41 {
42 namespace Blast
43 {
44
48 struct EdgeWithParent
49 {
50 uint32_t s, e; // Starting and ending vertices
51 uint32_t parent; // Parent facet index
52 EdgeWithParent() : s(0), e(0), parent(0) {}
53 EdgeWithParent(uint32_t s, uint32_t e, uint32_t p) : s(s), e(e), parent(p) {}
54 };
55
56
60 struct EdgeComparator
61 {
62 bool operator()(const EdgeWithParent& a, const EdgeWithParent& b) const
63 {
64 if (a.parent == b.parent)
65 {
66 if (a.s == b.s)
67 {
68 return a.e < b.e;
69 }
70 else
71 {
72 return a.s < b.s;
73 }
74 }
75 else
76 {
77 return a.parent < b.parent;
78 }
79 }
80 };
81
82 inline bool operator<(const Edge& a, const Edge& b)
83 {
84 if (a.s == b.s)
85 return a.e < b.e;
86 else
87 return a.s < b.s;
88 }
89
93 enum ProjectionDirections
94 {
95 YZ_PLANE = 1 << 1,
96 XY_PLANE = 1 << 2,
97 ZX_PLANE = 1 << 3,
98
99 OPPOSITE_WINDING = 1 << 4
100 };
101
105 NV_FORCE_INLINE ProjectionDirections getProjectionDirection(const physx::PxVec3& normal)
106 {
107 float maxv = std::max(std::abs(normal.x), std::max(std::abs(normal.y), std::abs(normal.z)));
108 ProjectionDirections retVal;
109 if (maxv == std::abs(normal.x))
110 {
111 retVal = YZ_PLANE;
112 if (normal.x < 0) retVal = (ProjectionDirections)((int)retVal | (int)OPPOSITE_WINDING);
113 return retVal;
114 }
115 if (maxv == std::abs(normal.y))
116 {
117 retVal = ZX_PLANE;
118 if (normal.y > 0) retVal = (ProjectionDirections)((int)retVal | (int)OPPOSITE_WINDING);
119 return retVal;
120 }
121 retVal = XY_PLANE;
122 if (normal.z < 0) retVal = (ProjectionDirections)((int)retVal | (int)OPPOSITE_WINDING);
123 return retVal;
124 }
125
126
130 NV_FORCE_INLINE physx::PxVec2 getProjectedPoint(const physx::PxVec3& point, ProjectionDirections dir)
131 {
132 if (dir & YZ_PLANE)
133 {
134 return physx::PxVec2(point.y, point.z);
135 }
136 if (dir & ZX_PLANE)
137 {
138 return physx::PxVec2(point.x, point.z);
139 }
140 return physx::PxVec2(point.x, point.y);
141 }
142
143 NV_FORCE_INLINE physx::PxVec2 getProjectedPoint(const NvcVec3& point, ProjectionDirections dir)
144 {
145 return getProjectedPoint((const physx::PxVec3&)point, dir);
146 }
147
151 NV_FORCE_INLINE physx::PxVec2 getProjectedPointWithWinding(const physx::PxVec3& point, ProjectionDirections dir)
152 {
153 if (dir & YZ_PLANE)
154 {
155 if (dir & OPPOSITE_WINDING)
156 {
157 return physx::PxVec2(point.z, point.y);
158 }
159 else
160 return physx::PxVec2(point.y, point.z);
161 }
162 if (dir & ZX_PLANE)
163 {
164 if (dir & OPPOSITE_WINDING)
165 {
166 return physx::PxVec2(point.z, point.x);
167 }
168 return physx::PxVec2(point.x, point.z);
169 }
170 if (dir & OPPOSITE_WINDING)
171 {
172 return physx::PxVec2(point.y, point.x);
173 }
174 return physx::PxVec2(point.x, point.y);
175 }
176
177
178
179 #define MAXIMUM_EXTENT 1000 * 1000 * 1000
180 #define BBOX_TEST_EPS 1e-5f
181
185 NV_INLINE bool weakBoundingBoxIntersection(const physx::PxBounds3& aBox, const physx::PxBounds3& bBox)
186 {
187 if (std::max(aBox.minimum.x, bBox.minimum.x) > std::min(aBox.maximum.x, bBox.maximum.x) + BBOX_TEST_EPS)
188 return false;
189 if (std::max(aBox.minimum.y, bBox.minimum.y) > std::min(aBox.maximum.y, bBox.maximum.y) + BBOX_TEST_EPS)
190 return false;
191 if (std::max(aBox.minimum.z, bBox.minimum.z) > std::min(aBox.maximum.z, bBox.maximum.z) + BBOX_TEST_EPS)
192 return false;
193 return true;
194 }
195
196
197
201 NV_INLINE bool getPlaneSegmentIntersection(const physx::PxPlane& pl, const physx::PxVec3& a, const physx::PxVec3& b,
202 physx::PxVec3& result)
203 {
204 float div = (b - a).dot(pl.n);
205 if (physx::PxAbs(div) < 0.0001f)
206 {
207 if (pl.contains(a))
208 {
209 result = a;
210 return true;
211 }
212 else
213 {
214 return false;
215 }
216 }
217 float t = (-a.dot(pl.n) - pl.d) / div;
218 if (t < 0.0f || t > 1.0f)
219 {
220 return false;
221 }
222 result = (b - a) * t + a;
223 return true;
224 }
225
226
227 #define POS_COMPARISON_OFFSET 1e-5f
228 #define NORM_COMPARISON_OFFSET 1e-3f
229
232 struct VrtComp
233 {
234 bool operator()(const Vertex& a, const Vertex& b) const
235 {
236 if (a.p.x + POS_COMPARISON_OFFSET < b.p.x) return true;
237 if (a.p.x - POS_COMPARISON_OFFSET > b.p.x) return false;
238 if (a.p.y + POS_COMPARISON_OFFSET < b.p.y) return true;
239 if (a.p.y - POS_COMPARISON_OFFSET > b.p.y) return false;
240 if (a.p.z + POS_COMPARISON_OFFSET < b.p.z) return true;
241 if (a.p.z - POS_COMPARISON_OFFSET > b.p.z) return false;
242
243 if (a.n.x + NORM_COMPARISON_OFFSET < b.n.x) return true;
244 if (a.n.x - NORM_COMPARISON_OFFSET > b.n.x) return false;
245 if (a.n.y + NORM_COMPARISON_OFFSET < b.n.y) return true;
246 if (a.n.y - NORM_COMPARISON_OFFSET > b.n.y) return false;
247 if (a.n.z + NORM_COMPARISON_OFFSET < b.n.z) return true;
248 if (a.n.z - NORM_COMPARISON_OFFSET > b.n.z) return false;
249
250
251 if (a.uv[0].x + NORM_COMPARISON_OFFSET < b.uv[0].x) return true;
252 if (a.uv[0].x - NORM_COMPARISON_OFFSET > b.uv[0].x) return false;
253 if (a.uv[0].y + NORM_COMPARISON_OFFSET < b.uv[0].y) return true;
254 return false;
255 };
256 };
257
261 struct VrtPositionComparator
262 {
263 bool operator()(const NvcVec3& a, const NvcVec3& b) const
264 {
265 if (a.x + POS_COMPARISON_OFFSET < b.x) return true;
266 if (a.x - POS_COMPARISON_OFFSET > b.x) return false;
267 if (a.y + POS_COMPARISON_OFFSET < b.y) return true;
268 if (a.y - POS_COMPARISON_OFFSET > b.y) return false;
269 if (a.z + POS_COMPARISON_OFFSET < b.z) return true;
270 if (a.z - POS_COMPARISON_OFFSET > b.z) return false;
271 return false;
272 };
273 bool operator()(const Vertex& a, const Vertex& b) const
274 {
275 return operator()(a.p, b.p);
276 };
277 };
278
279
280 NV_INLINE float calculateCollisionHullVolume(const CollisionHull& hull)
281 {
282 if (hull.pointsCount == 0)
283 {
284 return 0.0f;
285 }
286
287 // Find an approximate centroid for a more accurate calculation
288 NvcVec3 centroid = { 0.0f, 0.0f, 0.0f };
289 for (uint32_t i = 0; i < hull.pointsCount; ++i)
290 {
291 centroid = centroid + hull.points[i];
292 }
293 centroid = centroid / hull.pointsCount;
294
295 float volume = 0.0f;
296
297 for (uint32_t i = 0; i < hull.polygonDataCount; ++i)
298 {
299 const HullPolygon& poly = hull.polygonData[i];
300 if (poly.vertexCount < 3)
301 {
302 continue;
303 }
304 const uint32_t i0 = hull.indices[poly.indexBase];
305 uint32_t i1 = hull.indices[poly.indexBase + 1];
306 for (uint32_t j = 2; j < poly.vertexCount; ++j)
307 {
308 const uint32_t i2 = hull.indices[poly.indexBase + j];
309 const NvcVec3 a = hull.points[i0] - centroid;
310 const NvcVec3 b = hull.points[i1] - centroid;
311 const NvcVec3 c = hull.points[i2] - centroid;
312 volume +=
313 (a.x * b.y * c.z - a.x * b.z * c.y - a.y * b.x * c.z + a.y * b.z * c.x + a.z * b.x * c.y - a.z * b.y * c.x);
314 i1 = i2;
315 }
316 }
317 return (1.0f / 6.0f) * std::abs(volume);
318 }
319
320 } // namespace Blast
321 } // namespace Nv
322
323 #endif
Nv::Blast::VrtPositionComparator::operator()
bool operator()(const Vertex &a, const Vertex &b) const
Definition: NvBlastExtAuthoringInternalCommon.h:273
Nv::Blast::ProjectionDirections
ProjectionDirections
Definition: NvBlastExtAuthoringInternalCommon.h:93
Nv::Blast::Edge::e
uint32_t e
Definition: NvBlastExtAuthoringTypes.h:61
Nv::Blast::EdgeWithParent::s
uint32_t s
Definition: NvBlastExtAuthoringInternalCommon.h:50
Nv::Blast::HullPolygon::indexBase
uint16_t indexBase
Definition: NvBlastExtAuthoringTypes.h:127
Nv::Blast::YZ_PLANE
Definition: NvBlastExtAuthoringInternalCommon.h:95
Nv::Blast::XY_PLANE
Definition: NvBlastExtAuthoringInternalCommon.h:96
NORM_COMPARISON_OFFSET
#define NORM_COMPARISON_OFFSET
Definition: NvBlastExtAuthoringInternalCommon.h:228
Nv::Blast::VecMath::normal
NV_INLINE float normal(const float a[3], float r[3])
Definition: NvBlastMath.h:93
Nv::Blast::EdgeWithParent
Definition: NvBlastExtAuthoringInternalCommon.h:48
NvBlastExtAuthoringTypes.h
NvcVec3::z
float z
Definition: NvCTypes.h:51
Nv::Blast::EdgeComparator
Definition: NvBlastExtAuthoringInternalCommon.h:60
Nv::Blast::HullPolygon::vertexCount
uint16_t vertexCount
Definition: NvBlastExtAuthoringTypes.h:125
Nv::Blast::Vertex::n
NvcVec3 n
Definition: NvBlastExtAuthoringTypes.h:72
Nv::Blast::CollisionHull::indices
uint32_t * indices
Definition: NvBlastExtAuthoringTypes.h:139
BBOX_TEST_EPS
#define BBOX_TEST_EPS
Definition: NvBlastExtAuthoringInternalCommon.h:180
Nv::Blast::operator<
bool operator<(const Edge &a, const Edge &b)
Definition: NvBlastExtAuthoringInternalCommon.h:82
Nv::Blast::calculateCollisionHullVolume
NV_INLINE float calculateCollisionHullVolume(const CollisionHull &hull)
Definition: NvBlastExtAuthoringInternalCommon.h:280
Nv::Blast::EdgeWithParent::e
uint32_t e
Definition: NvBlastExtAuthoringInternalCommon.h:50
Nv::Blast::CollisionHull::polygonData
HullPolygon * polygonData
Definition: NvBlastExtAuthoringTypes.h:140
Nv::Blast::HullPolygon
Definition: NvBlastExtAuthoringTypes.h:120
NvcVec3::x
float x
Definition: NvCTypes.h:51
Nv::Blast::OPPOSITE_WINDING
Definition: NvBlastExtAuthoringInternalCommon.h:99
Nv::Blast::CollisionHull
Definition: NvBlastExtAuthoringTypes.h:133
Nv::Blast::VecMath::div
NV_INLINE void div(float a[3], float divisor)
Definition: NvBlastMath.h:43
NvcVec2::x
float x
Definition: NvCTypes.h:45
Nv::Blast::VrtComp::operator()
bool operator()(const Vertex &a, const Vertex &b) const
Definition: NvBlastExtAuthoringInternalCommon.h:234
Nv::Blast::ZX_PLANE
Definition: NvBlastExtAuthoringInternalCommon.h:97
NV_INLINE
#define NV_INLINE
Definition: NvPreprocessor.h:350
Nv::Blast::VrtComp
Definition: NvBlastExtAuthoringInternalCommon.h:232
Nv::Blast::CollisionHull::points
NvcVec3 * points
Definition: NvBlastExtAuthoringTypes.h:138
Nv::Blast::CollisionHull::pointsCount
uint32_t pointsCount
Definition: NvBlastExtAuthoringTypes.h:135
Nv::Blast::Vertex::uv
NvcVec2 uv[1]
Definition: NvBlastExtAuthoringTypes.h:73
Nv::Blast::VrtPositionComparator
Definition: NvBlastExtAuthoringInternalCommon.h:261
Nv::Blast::getProjectedPointWithWinding
NV_FORCE_INLINE physx::PxVec2 getProjectedPointWithWinding(const physx::PxVec3 &point, ProjectionDirections dir)
Definition: NvBlastExtAuthoringInternalCommon.h:151
Nv::Blast::EdgeWithParent::parent
uint32_t parent
Definition: NvBlastExtAuthoringInternalCommon.h:51
Nv::Blast::VrtPositionComparator::operator()
bool operator()(const NvcVec3 &a, const NvcVec3 &b) const
Definition: NvBlastExtAuthoringInternalCommon.h:263
Nv::Blast::CollisionHull::polygonDataCount
uint32_t polygonDataCount
Definition: NvBlastExtAuthoringTypes.h:137
Nv::Blast::VecMath::dot
NV_INLINE float dot(const float a[3], const float b[3])
Definition: NvBlastMath.h:73
Nv::Blast::EdgeWithParent::EdgeWithParent
EdgeWithParent(uint32_t s, uint32_t e, uint32_t p)
Definition: NvBlastExtAuthoringInternalCommon.h:53
Nv::Blast::getProjectionDirection
NV_FORCE_INLINE ProjectionDirections getProjectionDirection(const physx::PxVec3 &normal)
Definition: NvBlastExtAuthoringInternalCommon.h:105
Nv::Blast::weakBoundingBoxIntersection
NV_INLINE bool weakBoundingBoxIntersection(const physx::PxBounds3 &aBox, const physx::PxBounds3 &bBox)
Definition: NvBlastExtAuthoringInternalCommon.h:185
POS_COMPARISON_OFFSET
#define POS_COMPARISON_OFFSET
Definition: NvBlastExtAuthoringInternalCommon.h:227
Nv::Blast::Edge
Definition: NvBlastExtAuthoringTypes.h:57
Nv::Blast::getPlaneSegmentIntersection
NV_INLINE bool getPlaneSegmentIntersection(const physx::PxPlane &pl, const physx::PxVec3 &a, const physx::PxVec3 &b, physx::PxVec3 &result)
Definition: NvBlastExtAuthoringInternalCommon.h:201
Nv::Blast::Vertex
Definition: NvBlastExtAuthoringTypes.h:67
Nv::Blast::Vertex::p
NvcVec3 p
Definition: NvBlastExtAuthoringTypes.h:71
Nv::Blast::getProjectedPoint
NV_FORCE_INLINE physx::PxVec2 getProjectedPoint(const physx::PxVec3 &point, ProjectionDirections dir)
Definition: NvBlastExtAuthoringInternalCommon.h:130
Nv::Blast::EdgeComparator::operator()
bool operator()(const EdgeWithParent &a, const EdgeWithParent &b) const
Definition: NvBlastExtAuthoringInternalCommon.h:62
NvBlastPxSharedHelpers.h
NvcVec2::y
float y
Definition: NvCTypes.h:45
Nv
Definition: NvBlastArray.h:37
NvcVec3
Definition: NvCTypes.h:49
NV_FORCE_INLINE
#define NV_FORCE_INLINE
Definition: NvPreprocessor.h:365
Nv::Blast::Edge::s
uint32_t s
Definition: NvBlastExtAuthoringTypes.h:60
NvcVec3::y
float y
Definition: NvCTypes.h:51
Nv::Blast::EdgeWithParent::EdgeWithParent
EdgeWithParent()
Definition: NvBlastExtAuthoringInternalCommon.h:52