PolyVox  0.2.1
Open source voxel management library
CubicSurfaceExtractor.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_CubicSurfaceExtractor_H__
25 #define __PolyVox_CubicSurfaceExtractor_H__
26 
27 #include "Impl/TypeDef.h"
28 
29 #include "PolyVoxCore/Array.h"
32 
33 namespace PolyVox
34 {
76  template<typename VolumeType, typename IsQuadNeeded = DefaultIsQuadNeeded<typename VolumeType::VoxelType> >
78  {
79  struct IndexAndMaterial
80  {
81  int32_t iIndex;
82  int32_t uMaterial; //Should actually use the material type here, but this is ok for now.
83  };
84 
85  enum FaceNames
86  {
87  PositiveX,
88  PositiveY,
89  PositiveZ,
90  NegativeX,
91  NegativeY,
92  NegativeZ,
93  NoOfFaces
94  };
95 
96  struct Quad
97  {
98  Quad(uint32_t v0, uint32_t v1, uint32_t v2, uint32_t v3)
99  {
100  vertices[0] = v0;
101  vertices[1] = v1;
102  vertices[2] = v2;
103  vertices[3] = v3;
104  }
105 
106  uint32_t vertices[4];
107  };
108 
109  public:
110  CubicSurfaceExtractor(VolumeType* volData, Region region, SurfaceMesh<PositionMaterial>* result, bool bMergeQuads = true, IsQuadNeeded isQuadNeeded = IsQuadNeeded());
111 
112 
113  void execute();
114 
115  private:
116  int32_t addVertex(float fX, float fY, float fZ, uint32_t uMaterial, Array<3, IndexAndMaterial>& existingVertices);
117  bool performQuadMerging(std::list<Quad>& quads);
118  bool mergeQuads(Quad& q1, Quad& q2);
119 
120  IsQuadNeeded m_funcIsQuadNeededCallback;
121 
122  //The volume data and a sampler to access it.
123  VolumeType* m_volData;
124 
125  //Information about the region we are currently processing
126  Region m_regSizeInVoxels;
127 
128  //The surface patch we are currently filling.
129  SurfaceMesh<PositionMaterial>* m_meshCurrent;
130 
131  //Used to avoid creating duplicate vertices.
132  Array<3, IndexAndMaterial> m_previousSliceVertices;
133  Array<3, IndexAndMaterial> m_currentSliceVertices;
134 
135  //During extraction we create a number of different lists of quads. All the
136  //quads in a given list are in the same plane and facing in the same direction.
137  std::vector< std::list<Quad> > m_vecQuads[NoOfFaces];
138 
139  //Controls whether quad merging should be performed. This might be undesirable
140  //is the user needs per-vertex attributes, or to perform per vertex lighting.
141  bool m_bMergeQuads;
142 
143  //This constant defines the maximum number of quads which can share a
144  //vertex in a cubic style mesh. See the initialisation for more details.
145  static const uint32_t MaxVerticesPerPosition;
146  };
147 }
148 
150 
151 #endif