• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

PolyVoxCore/include/PolyVoxCore/MeshDecimator.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002 Copyright (c) 2005-2009 David Williams
00003 
00004 This software is provided 'as-is', without any express or implied
00005 warranty. In no event will the authors be held liable for any damages
00006 arising from the use of this software.
00007 
00008 Permission is granted to anyone to use this software for any purpose,
00009 including commercial applications, and to alter it and redistribute it
00010 freely, subject to the following restrictions:
00011 
00012     1. The origin of this software must not be misrepresented; you must not
00013     claim that you wrote the original software. If you use this software
00014     in a product, an acknowledgment in the product documentation would be
00015     appreciated but is not required.
00016 
00017     2. Altered source versions must be plainly marked as such, and must not be
00018     misrepresented as being the original software.
00019 
00020     3. This notice may not be removed or altered from any source
00021     distribution.
00022 *******************************************************************************/
00023 
00024 #ifndef __PolyVox_MeshDecimator_H__
00025 #define __PolyVox_MeshDecimator_H__
00026 
00027 #include "PolyVoxCore/SurfaceMesh.h"
00028 #include "PolyVoxCore/Vector.h"
00029 #include "PolyVoxCore/VertexTypes.h"
00030 
00031 #include <bitset>
00032 #include <vector>
00033 
00034 namespace PolyVox
00035 {
00067     template <typename VertexType>
00068     class MeshDecimator
00069     {
00070         //Used to keep track of when a vertex is
00071         //on one or more faces of  the region
00072         enum RegionFaceFlags
00073         {
00074             RFF_ON_REGION_FACE_NEG_X,
00075             RFF_ON_REGION_FACE_POS_X ,
00076             RFF_ON_REGION_FACE_NEG_Y ,
00077             RFF_ON_REGION_FACE_POS_Y ,
00078             RFF_ON_REGION_FACE_NEG_Z ,
00079             RFF_ON_REGION_FACE_POS_Z,
00080             RFF_NO_OF_REGION_FACE_FLAGS
00081         };
00082 
00083         //Data about the initial mesh - this
00084         //will be fill in once at the start
00085         struct InitialVertexMetadata
00086         {
00087             Vector3DFloat normal;
00088             bool isOnMaterialEdge;
00089             std::bitset<RFF_NO_OF_REGION_FACE_FLAGS> isOnRegionFace;
00090         };
00091 
00092         //Representing a triangle for decimation purposes.
00093         struct Triangle
00094         {
00095             uint32_t v0;
00096             uint32_t v1;
00097             uint32_t v2;
00098             Vector3DFloat normal;
00099         };
00100 
00101         struct IntVertex
00102         {           
00103             int32_t x;
00104             int32_t y;
00105             int32_t z;
00106             uint32_t index;
00107 
00108             IntVertex(int32_t xVal, int32_t yVal, int32_t zVal, uint32_t indexVal)
00109                 :x(xVal)
00110                 ,y(yVal)
00111                 ,z(zVal)
00112                 ,index(indexVal)
00113             {
00114             }
00115 
00116             bool operator==(const IntVertex& rhs) const
00117             {
00118                 return (x == rhs.x) && (y == rhs.y) && (z == rhs.z);
00119             }
00120 
00121             bool operator<(const IntVertex& rhs) const
00122             {
00123                 if (z < rhs.z)
00124                     return true;
00125                 if (rhs.z < z)
00126                     return false;
00127 
00128                 if (y < rhs.y)
00129                     return true;
00130                 if (rhs.y < y)
00131                     return false;
00132 
00133                 if (x < rhs.x)
00134                     return true;
00135                 if (rhs.x < x)
00136                     return false;
00137 
00138                 return false;
00139             }
00140         };
00141 
00142     public:
00144         MeshDecimator(const SurfaceMesh<VertexType>* pInputMesh, SurfaceMesh<VertexType>* pOutputMesh, float fEdgeCollapseThreshold = 0.95f);
00145 
00147         void execute();
00148 
00149     private:
00150 
00151         void fillInitialVertexMetadata(std::vector<InitialVertexMetadata>& vecInitialVertexMetadata);
00152 
00153         void buildConnectivityData(void);
00154 
00155         bool attemptEdgeCollapse(uint32_t uSrc, uint32_t uDest);
00156 
00157         const SurfaceMesh<VertexType>* m_pInputMesh;
00158         SurfaceMesh<VertexType>* m_pOutputMesh;
00159 
00160         uint32_t performDecimationPass(float m_fMinDotProductForCollapse);
00161         bool isSubset(std::bitset<RFF_NO_OF_REGION_FACE_FLAGS> a, std::bitset<RFF_NO_OF_REGION_FACE_FLAGS> b);
00162 
00163         bool canCollapseEdge(uint32_t uSrc, uint32_t uDest);
00164         bool canCollapseNormalEdge(uint32_t uSrc, uint32_t uDst);
00165         bool canCollapseRegionEdge(uint32_t uSrc, uint32_t uDst);
00166         bool canCollapseMaterialEdge(uint32_t uSrc, uint32_t uDst);
00167         bool collapseChangesFaceNormals(uint32_t uSrc, uint32_t uDst, float fThreshold);
00168 
00169         //Data structures used during decimation
00170 
00171         std::vector<bool> vertexLocked;
00172         std::vector<uint32_t> vertexMapper;
00173 
00174         std::vector<Triangle> m_vecTriangles;
00175         std::vector< std::vector<uint32_t> > trianglesUsingVertex; //Should probably use vector of vectors, and resise in advance.
00176 
00177         std::vector<InitialVertexMetadata> m_vecInitialVertexMetadata;
00178 
00179         float m_fMinDotProductForCollapse;
00180     };
00181 }
00182 
00183 #include "PolyVoxCore/MeshDecimator.inl"
00184 
00185 #endif //__PolyVox_MeshDecimator_H__

Generated on Sat Nov 19 2011 00:27:30 for PolyVox by  doxygen 1.7.1