RavEngine
Loading...
Searching...
No Matches
ExtMeshSimplificator.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
27#ifndef EXT_MESH_SIMPLIFICATOR_H
28#define EXT_MESH_SIMPLIFICATOR_H
29
30
31#include "foundation/PxBounds3.h"
32#include "foundation/PxArray.h"
33#include "geometry/PxSimpleTriangleMesh.h"
34
35#include "ExtQuadric.h"
36#include "ExtRandomAccessHeap.h"
37
38// ------------------------------------------------------------------------------
39
40// MM: implementation of paper Garland and Heckbert: "Surface Simplification Using Quadric Error Metrics"
41
42namespace physx
43{
44 namespace Ext
45 {
46
48 {
49 public:
50
52
53 void init(const PxSimpleTriangleMesh& inputMesh, PxReal edgeLengthCostWeight_ = 1e-1f, PxReal flatnessDetectionThreshold_ = 1e-2f);
54 void init(const PxArray<PxVec3> &vertices, const PxArray<PxU32> &triIds, PxReal edgeLengthCostWeight_ = 1e-1f, PxReal flatnessDetectionThreshold_ = 1e-2f);
55 void decimateByRatio(PxF32 relativeOutputMeshSize = 0.5f, PxF32 maximalEdgeLength = 0.0f);
56 void decimateBySize(PxI32 targetTriangleCount, PxF32 maximalEdgeLength = 0.0f);
57 void readBack(PxArray<PxVec3>& vertices, PxArray<PxU32>& triIds, PxArray<PxU32> *vertexMap = NULL);
58
59 private:
60 PxArray<PxVec3> vertices;
61 PxArray<PxI32> triIds;
62
63 void init();
64 bool step(PxF32 maximalEdgeLength);
65 bool getAdjTris(PxI32 triNr, PxI32 vertNr, PxI32& valence, bool& open,
66 PxArray<PxI32>* tris) const;
67 bool getAdjTris(PxI32 triNr, PxI32 vertNr, PxArray<PxI32>& tris) const;
68
69 void replaceNeighbor(PxI32 triNr, PxI32 oldNeighbor, PxI32 newNeighbor);
70 PxI32 getEdgeId(PxI32 triNr, PxI32 edgeNr);
71 bool collapseEdge(PxI32 triNr, PxI32 edgeNr);
72 void evalEdgeCost(PxI32 triNr, PxI32 edgeNr, float& cost, float& ratio);
73 void findTriNeighbors();
74
75 void transformPointsToUnitBox(PxArray<PxVec3>& points);
76 void transformPointsToOriginalPosition(PxArray<PxVec3>& points);
77
78 PxI32 numMeshTris;
79
80 PxArray<Quadric> quadrics;
81 PxArray<PxI32> vertMarks;
82 PxArray<PxI32> adjTris;
83 PxI32 currentVertMark;
84 PxArray<PxI32> triNeighbors;
85
86 //Scale input points into 0...1 unit-box
87 PxReal scaling;
88 PxVec3 origin;
89
90 PxReal edgeLengthCostWeight;
91 PxReal flatnessDetectionThreshold;
92
93 PxArray<PxI32> simplificationMap;
94
95 struct HeapElem
96 {
97 HeapElem() : triNr(0), edgeNr(0), cost(0.0f) {}
98 HeapElem(PxI32 triNr_, PxI32 edgeNr_, float cost_) :
99 triNr(triNr_), edgeNr(edgeNr_), cost(cost_) {}
100
101 PxI32 triNr, edgeNr;
102 float cost;
103
104 PX_FORCE_INLINE bool operator < (const HeapElem& e) const
105 {
106 return cost < e.cost;
107 }
108 };
109
111 };
112 }
113}
114
115#endif
Definition ExtMeshSimplificator.h:48
Definition ExtRandomAccessHeap.h:45
Definition PxArray.h:53
A structure describing a triangle mesh.
Definition PxSimpleTriangleMesh.h:83
3 Element vector class.
Definition PxVec3.h:50
#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