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

PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractor.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_CubicSurfaceExtractor_H__
00025 #define __PolyVox_CubicSurfaceExtractor_H__
00026 
00027 #include "PolyVoxImpl/TypeDef.h"
00028 
00029 #include "PolyVoxCore/Array.h"
00030 #include "PolyVoxCore/SurfaceMesh.h"
00031 
00032 namespace PolyVox
00033 {
00034     template< template<typename> class VolumeType, typename VoxelType>
00035     class CubicSurfaceExtractor
00036     {
00037         struct IndexAndMaterial
00038         {
00039             int32_t iIndex;
00040             int32_t uMaterial; //Should actually use the material type here, but this is ok for now.
00041         };
00042 
00043         enum FaceNames
00044         {
00045             PositiveX,
00046             PositiveY,
00047             PositiveZ,
00048             NegativeX,
00049             NegativeY,
00050             NegativeZ,
00051             NoOfFaces
00052         };
00053 
00054         struct Quad
00055         {
00056             uint32_t vertices[4];
00057         };
00058 
00059     public:
00060         CubicSurfaceExtractor(VolumeType<VoxelType>* volData, Region region, SurfaceMesh<PositionMaterial>* result, bool bMergeQuads = true);
00061 
00062         void execute();     
00063 
00064     private:
00065         int32_t addVertex(float fX, float fY, float fZ, uint32_t uMaterial, Array<3, IndexAndMaterial>& existingVertices);
00066         bool performQuadMerging(std::list<Quad>& quads);
00067         bool mergeQuads(Quad& q1, Quad& q2);
00068 
00069         //The volume data and a sampler to access it.
00070         VolumeType<VoxelType>* m_volData;
00071 
00072         //Information about the region we are currently processing
00073         Region m_regSizeInVoxels;
00074 
00075         //The surface patch we are currently filling.
00076         SurfaceMesh<PositionMaterial>* m_meshCurrent;
00077 
00078         //Used to avoid creating duplicate vertices.
00079         Array<3, IndexAndMaterial> m_previousSliceVertices;
00080         Array<3, IndexAndMaterial> m_currentSliceVertices;
00081 
00082         //During extraction we create a number of different lists of quads. All the 
00083         //quads in a given list are in the same plane and facing in the same direction.
00084         std::vector< std::list<Quad> > m_vecQuads[NoOfFaces];
00085 
00086         //Controls whether quad merging should be performed. This might be undesirable
00087         //is the user needs per-vertex attributes, or to perform per vertex lighting.
00088         bool m_bMergeQuads;
00089 
00090         //Although we try to avoid creating multiple vertices at the same location, sometimes this is unavoidable
00091         //if they have different materials. For example, four different materials next to each other would mean
00092         //four quads (though more triangles) sharing the vertex. As far as I can tell, four is the worst case scenario.
00093         static const uint32_t MaxQuadsSharingVertex;        
00094     };
00095 }
00096 
00097 #include "PolyVoxCore/CubicSurfaceExtractor.inl"
00098 
00099 #endif

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