PolyVox  0.2.1
Open source voxel management library
MeshDecimator.h
Go to the documentation of this file.
1 /*******************************************************************************
2 Copyright (c) 2005-2009 David Williams
3 
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7 
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11 
12  1. The origin of this software must not be misrepresented; you must not
13  claim that you wrote the original software. If you use this software
14  in a product, an acknowledgment in the product documentation would be
15  appreciated but is not required.
16 
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19 
20  3. This notice may not be removed or altered from any source
21  distribution.
22 *******************************************************************************/
23 
24 #ifndef __PolyVox_MeshDecimator_H__
25 #define __PolyVox_MeshDecimator_H__
26 
28 #include "PolyVoxCore/Vector.h"
30 
31 #include <bitset>
32 #include <vector>
33 
34 namespace PolyVox
35 {
69  template <typename VertexType>
71  {
72  //Used to keep track of when a vertex is
73  //on one or more faces of the region
74  enum RegionFaceFlags
75  {
76  RFF_ON_REGION_FACE_NEG_X,
77  RFF_ON_REGION_FACE_POS_X ,
78  RFF_ON_REGION_FACE_NEG_Y ,
79  RFF_ON_REGION_FACE_POS_Y ,
80  RFF_ON_REGION_FACE_NEG_Z ,
81  RFF_ON_REGION_FACE_POS_Z,
82  RFF_NO_OF_REGION_FACE_FLAGS
83  };
84 
85  //Data about the initial mesh - this
86  //will be fill in once at the start
87  struct InitialVertexMetadata
88  {
89  Vector3DFloat normal;
90  bool isOnMaterialEdge;
91  std::bitset<RFF_NO_OF_REGION_FACE_FLAGS> isOnRegionFace;
92  };
93 
94  //Representing a triangle for decimation purposes.
95  struct Triangle
96  {
97  uint32_t v0;
98  uint32_t v1;
99  uint32_t v2;
100  Vector3DFloat normal;
101  };
102 
103  struct IntVertex
104  {
105  int32_t x;
106  int32_t y;
107  int32_t z;
108  uint32_t index;
109 
110  IntVertex(int32_t xVal, int32_t yVal, int32_t zVal, uint32_t indexVal)
111  :x(xVal)
112  ,y(yVal)
113  ,z(zVal)
114  ,index(indexVal)
115  {
116  }
117 
118  bool operator==(const IntVertex& rhs) const
119  {
120  return (x == rhs.x) && (y == rhs.y) && (z == rhs.z);
121  }
122 
123  bool operator<(const IntVertex& rhs) const
124  {
125  if (z < rhs.z)
126  return true;
127  if (rhs.z < z)
128  return false;
129 
130  if (y < rhs.y)
131  return true;
132  if (rhs.y < y)
133  return false;
134 
135  if (x < rhs.x)
136  return true;
137  if (rhs.x < x)
138  return false;
139 
140  return false;
141  }
142  };
143 
144  public:
146  POLYVOX_DEPRECATED MeshDecimator(const SurfaceMesh<VertexType>* pInputMesh, SurfaceMesh<VertexType>* pOutputMesh, float fEdgeCollapseThreshold = 0.95f);
147 
150 
151  private:
152 
153  void fillInitialVertexMetadata(std::vector<InitialVertexMetadata>& vecInitialVertexMetadata);
154 
155  void buildConnectivityData(void);
156 
157  bool attemptEdgeCollapse(uint32_t uSrc, uint32_t uDst);
158 
159  const SurfaceMesh<VertexType>* m_pInputMesh;
160  SurfaceMesh<VertexType>* m_pOutputMesh;
161 
162  uint32_t performDecimationPass(float m_fMinDotProductForCollapse);
163  bool isSubset(std::bitset<RFF_NO_OF_REGION_FACE_FLAGS> a, std::bitset<RFF_NO_OF_REGION_FACE_FLAGS> b);
164 
165  bool canCollapseEdge(uint32_t uSrc, uint32_t uDst);
166  bool canCollapseNormalEdge(uint32_t uSrc, uint32_t uDst);
167  bool canCollapseRegionEdge(uint32_t uSrc, uint32_t uDst);
168  bool canCollapseMaterialEdge(uint32_t uSrc, uint32_t uDst);
169  bool collapseChangesFaceNormals(uint32_t uSrc, uint32_t uDst, float fThreshold);
170 
171  //Data structures used during decimation
172 
173  std::vector<bool> vertexLocked;
174  std::vector<uint32_t> vertexMapper;
175 
176  std::vector<Triangle> m_vecTriangles;
177  std::vector< std::vector<uint32_t> > trianglesUsingVertex; //Should probably use vector of vectors, and resise in advance.
178 
179  std::vector<InitialVertexMetadata> m_vecInitialVertexMetadata;
180 
181  float m_fMinDotProductForCollapse;
182  };
183 }
184 
186 
187 #endif //__PolyVox_MeshDecimator_H__