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
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 
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 
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 
94 {
95  YZ_PLANE = 1 << 1,
96  XY_PLANE = 1 << 2,
97  ZX_PLANE = 1 << 3,
98 
100 };
101 
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 
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 
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 
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
bool operator()(const Vertex &a, const Vertex &b) const
Definition: NvBlastExtAuthoringInternalCommon.h:273
ProjectionDirections
Definition: NvBlastExtAuthoringInternalCommon.h:93
uint32_t e
Definition: NvBlastExtAuthoringTypes.h:61
uint32_t s
Definition: NvBlastExtAuthoringInternalCommon.h:50
uint16_t indexBase
Definition: NvBlastExtAuthoringTypes.h:127
Definition: NvBlastExtAuthoringInternalCommon.h:95
Definition: NvBlastExtAuthoringInternalCommon.h:96
#define NORM_COMPARISON_OFFSET
Definition: NvBlastExtAuthoringInternalCommon.h:228
NV_INLINE float normal(const float a[3], float r[3])
Definition: NvBlastMath.h:93
Definition: NvBlastExtAuthoringInternalCommon.h:48
float z
Definition: NvCTypes.h:51
Definition: NvBlastExtAuthoringInternalCommon.h:60
uint16_t vertexCount
Definition: NvBlastExtAuthoringTypes.h:125
NvcVec3 n
Definition: NvBlastExtAuthoringTypes.h:72
uint32_t * indices
Definition: NvBlastExtAuthoringTypes.h:139
#define BBOX_TEST_EPS
Definition: NvBlastExtAuthoringInternalCommon.h:180
bool operator<(const Edge &a, const Edge &b)
Definition: NvBlastExtAuthoringInternalCommon.h:82
NV_INLINE float calculateCollisionHullVolume(const CollisionHull &hull)
Definition: NvBlastExtAuthoringInternalCommon.h:280
uint32_t e
Definition: NvBlastExtAuthoringInternalCommon.h:50
HullPolygon * polygonData
Definition: NvBlastExtAuthoringTypes.h:140
Definition: NvBlastExtAuthoringTypes.h:120
float x
Definition: NvCTypes.h:51
Definition: NvBlastExtAuthoringInternalCommon.h:99
Definition: NvBlastExtAuthoringTypes.h:133
NV_INLINE void div(float a[3], float divisor)
Definition: NvBlastMath.h:43
float x
Definition: NvCTypes.h:45
bool operator()(const Vertex &a, const Vertex &b) const
Definition: NvBlastExtAuthoringInternalCommon.h:234
Definition: NvBlastExtAuthoringInternalCommon.h:97
#define NV_INLINE
Definition: NvPreprocessor.h:350
Definition: NvBlastExtAuthoringInternalCommon.h:232
NvcVec3 * points
Definition: NvBlastExtAuthoringTypes.h:138
uint32_t pointsCount
Definition: NvBlastExtAuthoringTypes.h:135
NvcVec2 uv[1]
Definition: NvBlastExtAuthoringTypes.h:73
Definition: NvBlastExtAuthoringInternalCommon.h:261
NV_FORCE_INLINE physx::PxVec2 getProjectedPointWithWinding(const physx::PxVec3 &point, ProjectionDirections dir)
Definition: NvBlastExtAuthoringInternalCommon.h:151
uint32_t parent
Definition: NvBlastExtAuthoringInternalCommon.h:51
bool operator()(const NvcVec3 &a, const NvcVec3 &b) const
Definition: NvBlastExtAuthoringInternalCommon.h:263
uint32_t polygonDataCount
Definition: NvBlastExtAuthoringTypes.h:137
NV_INLINE float dot(const float a[3], const float b[3])
Definition: NvBlastMath.h:73
EdgeWithParent(uint32_t s, uint32_t e, uint32_t p)
Definition: NvBlastExtAuthoringInternalCommon.h:53
NV_FORCE_INLINE ProjectionDirections getProjectionDirection(const physx::PxVec3 &normal)
Definition: NvBlastExtAuthoringInternalCommon.h:105
NV_INLINE bool weakBoundingBoxIntersection(const physx::PxBounds3 &aBox, const physx::PxBounds3 &bBox)
Definition: NvBlastExtAuthoringInternalCommon.h:185
#define POS_COMPARISON_OFFSET
Definition: NvBlastExtAuthoringInternalCommon.h:227
Definition: NvBlastExtAuthoringTypes.h:57
NV_INLINE bool getPlaneSegmentIntersection(const physx::PxPlane &pl, const physx::PxVec3 &a, const physx::PxVec3 &b, physx::PxVec3 &result)
Definition: NvBlastExtAuthoringInternalCommon.h:201
Definition: NvBlastExtAuthoringTypes.h:67
NvcVec3 p
Definition: NvBlastExtAuthoringTypes.h:71
NV_FORCE_INLINE physx::PxVec2 getProjectedPoint(const physx::PxVec3 &point, ProjectionDirections dir)
Definition: NvBlastExtAuthoringInternalCommon.h:130
bool operator()(const EdgeWithParent &a, const EdgeWithParent &b) const
Definition: NvBlastExtAuthoringInternalCommon.h:62
float y
Definition: NvCTypes.h:45
Definition: NvBlastArray.h:37
Definition: NvCTypes.h:49
#define NV_FORCE_INLINE
Definition: NvPreprocessor.h:365
uint32_t s
Definition: NvBlastExtAuthoringTypes.h:60
float y
Definition: NvCTypes.h:51
EdgeWithParent()
Definition: NvBlastExtAuthoringInternalCommon.h:52