RavEngine
Loading...
Searching...
No Matches
GuMeshAnalysis.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_MESH_ANALYSIS_H
30#define GU_MESH_ANALYSIS_H
31
32#include "foundation/Px.h"
33#include "common/PxPhysXCommonConfig.h"
34#include "GuTriangle.h"
35#include "foundation/PxHashMap.h"
36#include "foundation/PxSort.h"
37
38namespace physx
39{
40namespace Gu
41{
42 using Triangle = Gu::IndexedTriangleT<PxI32>;
43
44
46 {
47 struct Range
48 {
49 PxI32 start;
50 PxI32 end; //Exclusive
51
52 Range(PxI32 start_, PxI32 end_)
53 {
54 start = start_;
55 end = end_;
56 }
57
58 PxI32 Length() const { return end - start; }
59 };
60
61 template<typename T, typename S>
62 static void splitRanges(PxArray<Range>& mergeRanges, const PxArray<PxI32>& indexer, const PxArray<T>& points, PxI32 dimIndex, S tol)
63 {
64 PxArray<Range> newMergeRanges;
65
66 for (PxU32 i = 0; i < mergeRanges.size(); ++i)
67 {
68 const Range& r = mergeRanges[i];
69 PxI32 start = r.start;
70 for (PxI32 j = r.start + 1; j < r.end; ++j)
71 {
72 //PxF64 delta = PxAbs(points[start][dimIndex] - points[j - 1][dimIndex]);
73 S delta = PxAbs(points[indexer[j]][dimIndex] - points[indexer[j - 1]][dimIndex]);
74 if (delta > tol)
75 {
76 if (j - start > 1)
77 newMergeRanges.pushBack(Range(start, j));
78 start = j;
79 }
80 }
81 if (r.end - start > 1)
82 newMergeRanges.pushBack(Range(start, r.end));
83 }
84
85 mergeRanges.clear();
86 for (PxU32 i = 0; i < newMergeRanges.size(); ++i)
87 mergeRanges.pushBack(newMergeRanges[i]);
88 }
89
90 template<typename T>
91 struct Comparer
92 {
93 const PxArray<T>& points;
94 const PxU32 dimension;
95
96 Comparer(const PxArray<T>& points_, const PxU32 dimension_) : points(points_), dimension(dimension_) {}
97
98 bool operator()(const PxI32& a, const PxI32& b) const
99 {
100 return points[a][dimension] > points[b][dimension];
101 }
102
103 private:
104 PX_NOCOPY(Comparer)
105 };
106
107 public:
108 template<typename T, typename S>
109 static void mapDuplicatePoints(const PxArray<T>& points, PxArray<PxI32>& result, S duplicateDistanceManhattanMetric = static_cast<S>(1e-6))
110 {
111 PxArray<PxI32> indexer;
112 indexer.reserve(points.size());
113 indexer.forceSize_Unsafe(points.size());
114 for (PxU32 i = 0; i < points.size(); ++i)
115 {
116 indexer[i] = i;
117 result[i] = i;
118 }
119
120 PxSort(indexer.begin(), indexer.size(), Comparer<T>(points, 0));
121
122 PxArray<Range> mergeRanges;
123 mergeRanges.pushBack(Range(0, points.size()));
124 splitRanges<T>(mergeRanges, indexer, points, 0, duplicateDistanceManhattanMetric);
125
126 for (PxU32 i = 0; i < mergeRanges.size(); ++i)
127 {
128 const Range& r = mergeRanges[i];
129 PxSort(indexer.begin() + r.start, r.Length(), Comparer<T>(points, 1));
130 }
131 splitRanges<T>(mergeRanges, indexer, points, 1, duplicateDistanceManhattanMetric);
132
133 for (PxU32 i = 0; i < mergeRanges.size(); ++i)
134 {
135 const Range& r = mergeRanges[i];
136 PxSort(indexer.begin() + r.start, r.Length(), Comparer<T>(points, 2));
137 }
138 splitRanges<T>(mergeRanges, indexer, points, 2, duplicateDistanceManhattanMetric);
139
140 //Merge the ranges
141 for (PxU32 i = 0; i < mergeRanges.size(); ++i)
142 {
143 const Range& r = mergeRanges[i];
144 PxSort(indexer.begin() + r.start, r.Length());
145 for (PxI32 j = r.start + 1; j < r.end; ++j)
146 result[indexer[j]] = result[indexer[r.start]];
147 }
148 }
149
150 PX_PHYSX_COMMON_API static bool buildTriangleAdjacency(const Triangle* tris, PxU32 numTriangles, PxArray<PxI32>& result, PxHashMap<PxU64, PxI32>& edges);
151 PX_PHYSX_COMMON_API static bool checkConsistentTriangleOrientation(const Triangle* tris, PxU32 numTriangles, PxArray<bool>& flip, PxHashMap<PxU64, PxI32>& edges);
152 PX_PHYSX_COMMON_API static bool makeTriOrientationConsistent(Triangle* tris, PxU32 numTriangles, bool invertOrientation = false);
153 PX_PHYSX_COMMON_API static bool checkMeshWatertightness(const Triangle* tris, PxU32 numTriangles);
154 };
155}
156}
157
158#endif
159
Definition GuMeshAnalysis.h:46
Definition PxArray.h:53
PX_FORCE_INLINE uint32_t size() const
Definition PxArray.h:242
PX_FORCE_INLINE void forceSize_Unsafe(uint32_t size)
Definition PxArray.h:507
PX_INLINE void clear()
Definition PxArray.h:250
PX_FORCE_INLINE ConstIterator begin() const
Definition PxArray.h:176
PX_FORCE_INLINE T & pushBack(const T &a)
Definition PxArray.h:296
PX_INLINE void reserve(const uint32_t capacity)
Definition PxArray.h:486
Definition PxHashMap.h:78
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
Definition SnippetImmediateMode.cpp:303
Definition GuTriangle.h:48