PolyVox  0.3.0-dev
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 <cstdlib> //For abort()
35 #include <cstring> //For memcpy
36 #include <limits>
37 #include <memory>
38 #include <stdexcept> //For invalid_argument
39 
40 namespace PolyVox
41 {
42  template <typename VoxelType>
43  class SimpleVolume : public BaseVolume<VoxelType>
44  {
45  public:
46  #ifndef SWIG
47  //Could be made private?
48  class Block
49  {
50  public:
51  Block(uint16_t uSideLength = 0);
52  ~Block();
53 
54  uint16_t getSideLength(void) const;
55  VoxelType getVoxelAt(uint16_t uXPos, uint16_t uYPos, uint16_t uZPos) const;
56  VoxelType getVoxelAt(const Vector3DUint16& v3dPos) const;
57 
58  void setVoxelAt(uint16_t uXPos, uint16_t uYPos, uint16_t uZPos, VoxelType tValue);
59  void setVoxelAt(const Vector3DUint16& v3dPos, VoxelType tValue);
60 
61  void fill(VoxelType tValue);
62  void initialise(uint16_t uSideLength);
64 
65  public:
69  };
70 
71  //There seems to be some descrepency between Visual Studio and GCC about how the following class should be declared.
72  //There is a work around (see also See http://goo.gl/qu1wn) given below which appears to work on VS2010 and GCC, but
73  //which seems to cause internal compiler errors on VS2008 when building with the /Gm 'Enable Minimal Rebuild' compiler
74  //option. For now it seems best to 'fix' it with the preprocessor insstead, but maybe the workaround can be reinstated
75  //in the future
76  //typedef Volume<VoxelType> VolumeOfVoxelType; //Workaround for GCC/VS2010 differences.
77  //class Sampler : public VolumeOfVoxelType::template Sampler< SimpleVolume<VoxelType> >
78 #if defined(_MSC_VER)
79  class Sampler : public BaseVolume<VoxelType>::Sampler< SimpleVolume<VoxelType> > //This line works on VS2010
80 #else
81  class Sampler : public BaseVolume<VoxelType>::template Sampler< SimpleVolume<VoxelType> > //This line works on GCC
82 #endif
83  {
84  public:
87  ~Sampler();
88 
92  inline VoxelType getVoxel(void) const;
93 
95  void setPosition(const Vector3DInt32& v3dNewPos);
97  void setPosition(int32_t xPos, int32_t yPos, int32_t zPos);
99  inline bool setVoxel(VoxelType tValue);
100 
102  void movePositiveX(void);
104  void movePositiveY(void);
106  void movePositiveZ(void);
107 
109  void moveNegativeX(void);
111  void moveNegativeY(void);
113  void moveNegativeZ(void);
114 
115  inline VoxelType peekVoxel1nx1ny1nz(void) const;
116  inline VoxelType peekVoxel1nx1ny0pz(void) const;
117  inline VoxelType peekVoxel1nx1ny1pz(void) const;
118  inline VoxelType peekVoxel1nx0py1nz(void) const;
119  inline VoxelType peekVoxel1nx0py0pz(void) const;
120  inline VoxelType peekVoxel1nx0py1pz(void) const;
121  inline VoxelType peekVoxel1nx1py1nz(void) const;
122  inline VoxelType peekVoxel1nx1py0pz(void) const;
123  inline VoxelType peekVoxel1nx1py1pz(void) const;
124 
125  inline VoxelType peekVoxel0px1ny1nz(void) const;
126  inline VoxelType peekVoxel0px1ny0pz(void) const;
127  inline VoxelType peekVoxel0px1ny1pz(void) const;
128  inline VoxelType peekVoxel0px0py1nz(void) const;
129  inline VoxelType peekVoxel0px0py0pz(void) const;
130  inline VoxelType peekVoxel0px0py1pz(void) const;
131  inline VoxelType peekVoxel0px1py1nz(void) const;
132  inline VoxelType peekVoxel0px1py0pz(void) const;
133  inline VoxelType peekVoxel0px1py1pz(void) const;
134 
135  inline VoxelType peekVoxel1px1ny1nz(void) const;
136  inline VoxelType peekVoxel1px1ny0pz(void) const;
137  inline VoxelType peekVoxel1px1ny1pz(void) const;
138  inline VoxelType peekVoxel1px0py1nz(void) const;
139  inline VoxelType peekVoxel1px0py0pz(void) const;
140  inline VoxelType peekVoxel1px0py1pz(void) const;
141  inline VoxelType peekVoxel1px1py1nz(void) const;
142  inline VoxelType peekVoxel1px1py0pz(void) const;
143  inline VoxelType peekVoxel1px1py1pz(void) const;
144 
145  private:
146  //Other current position information
147  VoxelType* mCurrentVoxel;
148  };
149  #endif
150 
151  public:
153  SimpleVolume(const Region& regValid, uint16_t uBlockSideLength = 32);
154 
156  ~SimpleVolume();
157 
159  VoxelType getVoxel(int32_t uXPos, int32_t uYPos, int32_t uZPos) const;
161  VoxelType getVoxel(const Vector3DInt32& v3dPos) const;
163  VoxelType getVoxelAt(int32_t uXPos, int32_t uYPos, int32_t uZPos) const;
165  VoxelType getVoxelAt(const Vector3DInt32& v3dPos) const;
167  VoxelType getVoxelWithWrapping(int32_t uXPos, int32_t uYPos, int32_t uZPos, WrapMode eWrapMode = WrapModes::Border, VoxelType tBorder = VoxelType()) const;
169  VoxelType getVoxelWithWrapping(const Vector3DInt32& v3dPos, WrapMode eWrapMode = WrapModes::Border, VoxelType tBorder = VoxelType()) const;
170 
172  bool setVoxelAt(int32_t uXPos, int32_t uYPos, int32_t uZPos, VoxelType tValue);
174  bool setVoxelAt(const Vector3DInt32& v3dPos, VoxelType tValue);
175 
178 
179  protected:
181  SimpleVolume(const SimpleVolume& rhs);
182 
184  SimpleVolume& operator=(const SimpleVolume& rhs);
185 
186  private:
187  void initialise(const Region& regValidRegion, uint16_t uBlockSideLength);
188 
189  Block* getUncompressedBlock(int32_t uBlockX, int32_t uBlockY, int32_t uBlockZ) const;
190 
191  //The block data
192  Block* m_pBlocks;
193 
194  //The size of the volume in vlocks
195  Region m_regValidRegionInBlocks;
196 
197  //Volume size measured in blocks.
198  uint32_t m_uNoOfBlocksInVolume;
199  uint16_t m_uWidthInBlocks;
200  uint16_t m_uHeightInBlocks;
201  uint16_t m_uDepthInBlocks;
202 
203  //The size of the blocks
204  uint32_t m_uNoOfVoxelsPerBlock;
205  uint16_t m_uBlockSideLength;
206  uint8_t m_uBlockSideLengthPower;
207  };
208 }
209 
213 
214 #endif //__PolyVox_SimpleVolume_H__