PolyVox  0.2.1
Open source voxel management library
SimpleVolume.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_SimpleVolume_H__
25 #define __PolyVox_SimpleVolume_H__
26 
27 #include "Impl/Utility.h"
28 
29 #include "PolyVoxCore/BaseVolume.h"
30 #include "PolyVoxCore/Log.h"
31 #include "PolyVoxCore/Region.h"
32 #include "PolyVoxCore/Vector.h"
33 
34 #include <cassert>
35 #include <cstdlib> //For abort()
36 #include <cstring> //For memcpy
37 #include <limits>
38 #include <memory>
39 #include <stdexcept> //For invalid_argument
40 
41 namespace PolyVox
42 {
43  template <typename VoxelType>
44  class SimpleVolume : public BaseVolume<VoxelType>
45  {
46  public:
47  #ifndef SWIG
48  //Could be made private?
49  class Block
50  {
51  public:
52  Block(uint16_t uSideLength = 0);
53  ~Block();
54 
55  uint16_t getSideLength(void) const;
56  VoxelType getVoxelAt(uint16_t uXPos, uint16_t uYPos, uint16_t uZPos) const;
57  VoxelType getVoxelAt(const Vector3DUint16& v3dPos) const;
58 
59  void setVoxelAt(uint16_t uXPos, uint16_t uYPos, uint16_t uZPos, VoxelType tValue);
60  void setVoxelAt(const Vector3DUint16& v3dPos, VoxelType tValue);
61 
62  void fill(VoxelType tValue);
63  void initialise(uint16_t uSideLength);
64  uint32_t calculateSizeInBytes(void);
65 
66  public:
68  uint16_t m_uSideLength;
70  };
71 
72  //There seems to be some descrepency between Visual Studio and GCC about how the following class should be declared.
73  //There is a work around (see also See http://goo.gl/qu1wn) given below which appears to work on VS2010 and GCC, but
74  //which seems to cause internal compiler errors on VS2008 when building with the /Gm 'Enable Minimal Rebuild' compiler
75  //option. For now it seems best to 'fix' it with the preprocessor insstead, but maybe the workaround can be reinstated
76  //in the future
77  //typedef Volume<VoxelType> VolumeOfVoxelType; //Workaround for GCC/VS2010 differences.
78  //class Sampler : public VolumeOfVoxelType::template Sampler< SimpleVolume<VoxelType> >
79 #if defined(_MSC_VER)
80  class Sampler : public BaseVolume<VoxelType>::Sampler< SimpleVolume<VoxelType> > //This line works on VS2010
81 #else
82  class Sampler : public BaseVolume<VoxelType>::template Sampler< SimpleVolume<VoxelType> > //This line works on GCC
83 #endif
84  {
85  public:
88  ~Sampler();
89 
90  Sampler& operator=(const Sampler& rhs);
91 
92  VoxelType getSubSampledVoxel(uint8_t uLevel) const;
94  inline VoxelType getVoxel(void) const;
95 
97  void setPosition(const Vector3DInt32& v3dNewPos);
99  void setPosition(int32_t xPos, int32_t yPos, int32_t zPos);
101  inline bool setVoxel(VoxelType tValue);
102 
104  void movePositiveX(void);
106  void movePositiveY(void);
108  void movePositiveZ(void);
109 
111  void moveNegativeX(void);
113  void moveNegativeY(void);
115  void moveNegativeZ(void);
116 
117  inline VoxelType peekVoxel1nx1ny1nz(void) const;
118  inline VoxelType peekVoxel1nx1ny0pz(void) const;
119  inline VoxelType peekVoxel1nx1ny1pz(void) const;
120  inline VoxelType peekVoxel1nx0py1nz(void) const;
121  inline VoxelType peekVoxel1nx0py0pz(void) const;
122  inline VoxelType peekVoxel1nx0py1pz(void) const;
123  inline VoxelType peekVoxel1nx1py1nz(void) const;
124  inline VoxelType peekVoxel1nx1py0pz(void) const;
125  inline VoxelType peekVoxel1nx1py1pz(void) const;
126 
127  inline VoxelType peekVoxel0px1ny1nz(void) const;
128  inline VoxelType peekVoxel0px1ny0pz(void) const;
129  inline VoxelType peekVoxel0px1ny1pz(void) const;
130  inline VoxelType peekVoxel0px0py1nz(void) const;
131  inline VoxelType peekVoxel0px0py0pz(void) const;
132  inline VoxelType peekVoxel0px0py1pz(void) const;
133  inline VoxelType peekVoxel0px1py1nz(void) const;
134  inline VoxelType peekVoxel0px1py0pz(void) const;
135  inline VoxelType peekVoxel0px1py1pz(void) const;
136 
137  inline VoxelType peekVoxel1px1ny1nz(void) const;
138  inline VoxelType peekVoxel1px1ny0pz(void) const;
139  inline VoxelType peekVoxel1px1ny1pz(void) const;
140  inline VoxelType peekVoxel1px0py1nz(void) const;
141  inline VoxelType peekVoxel1px0py0pz(void) const;
142  inline VoxelType peekVoxel1px0py1pz(void) const;
143  inline VoxelType peekVoxel1px1py1nz(void) const;
144  inline VoxelType peekVoxel1px1py0pz(void) const;
145  inline VoxelType peekVoxel1px1py1pz(void) const;
146 
147  private:
148  //Other current position information
149  VoxelType* mCurrentVoxel;
150  };
151  #endif
152 
153  public:
155  SimpleVolume(const Region& regValid, uint16_t uBlockSideLength = 32);
156 
158  ~SimpleVolume();
159 
161  VoxelType getBorderValue(void) const;
163  VoxelType getVoxelAt(int32_t uXPos, int32_t uYPos, int32_t uZPos) const;
165  VoxelType getVoxelAt(const Vector3DInt32& v3dPos) const;
166 
168  void setBorderValue(const VoxelType& tBorder);
170  bool setVoxelAt(int32_t uXPos, int32_t uYPos, int32_t uZPos, VoxelType tValue);
172  bool setVoxelAt(const Vector3DInt32& v3dPos, VoxelType tValue);
173 
175  uint32_t calculateSizeInBytes(void);
176 
177  protected:
179  SimpleVolume(const SimpleVolume& rhs);
180 
182  SimpleVolume& operator=(const SimpleVolume& rhs);
183 
184  private:
185  void initialise(const Region& regValidRegion, uint16_t uBlockSideLength);
186 
187  Block* getUncompressedBlock(int32_t uBlockX, int32_t uBlockY, int32_t uBlockZ) const;
188 
189  //The block data
190  Block* m_pBlocks;
191 
192  //We don't store an actual Block for the border, just the uncompressed data. This is partly because the border
193  //block does not have a position (so can't be passed to getUncompressedBlock()) and partly because there's a
194  //good chance we'll often hit it anyway. It's a chunk of homogenous data (rather than a single value) so that
195  //the VolumeIterator can do it's usual pointer arithmetic without needing to know it's gone outside the volume.
196  VoxelType* m_pUncompressedBorderData;
197 
198  //The size of the volume in vlocks
199  Region m_regValidRegionInBlocks;
200 
201  //Volume size measured in blocks.
202  uint32_t m_uNoOfBlocksInVolume;
203  uint16_t m_uWidthInBlocks;
204  uint16_t m_uHeightInBlocks;
205  uint16_t m_uDepthInBlocks;
206 
207  //The size of the blocks
208  uint32_t m_uNoOfVoxelsPerBlock;
209  uint16_t m_uBlockSideLength;
210  uint8_t m_uBlockSideLengthPower;
211  };
212 }
213 
217 
218 #endif //__PolyVox_SimpleVolume_H__