PolyVox  0.2.1
Open source voxel management library
SimpleVolumeBlock.inl
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 namespace PolyVox
25 {
26  template <typename VoxelType>
28  :m_tUncompressedData(0)
29  ,m_uSideLength(0)
30  ,m_uSideLengthPower(0)
31  {
32  if(uSideLength != 0)
33  {
34  initialise(uSideLength);
35  }
36  }
37 
38  template <typename VoxelType>
40  {
41  delete[] m_tUncompressedData;
42  }
43 
44  template <typename VoxelType>
46  {
47  return m_uSideLength;
48  }
49 
50  template <typename VoxelType>
51  VoxelType SimpleVolume<VoxelType>::Block::getVoxelAt(uint16_t uXPos, uint16_t uYPos, uint16_t uZPos) const
52  {
53  assert(uXPos < m_uSideLength);
54  assert(uYPos < m_uSideLength);
55  assert(uZPos < m_uSideLength);
56 
57  assert(m_tUncompressedData);
58 
59  return m_tUncompressedData
60  [
61  uXPos +
62  uYPos * m_uSideLength +
63  uZPos * m_uSideLength * m_uSideLength
64  ];
65  }
66 
67  template <typename VoxelType>
69  {
70  return getVoxelAt(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ());
71  }
72 
73  template <typename VoxelType>
74  void SimpleVolume<VoxelType>::Block::setVoxelAt(uint16_t uXPos, uint16_t uYPos, uint16_t uZPos, VoxelType tValue)
75  {
76  assert(uXPos < m_uSideLength);
77  assert(uYPos < m_uSideLength);
78  assert(uZPos < m_uSideLength);
79 
80  assert(m_tUncompressedData);
81 
82  m_tUncompressedData
83  [
84  uXPos +
85  uYPos * m_uSideLength +
86  uZPos * m_uSideLength * m_uSideLength
87  ] = tValue;
88  }
89 
90  template <typename VoxelType>
92  {
93  setVoxelAt(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ(), tValue);
94  }
95 
96  template <typename VoxelType>
98  {
99  const uint32_t uNoOfVoxels = m_uSideLength * m_uSideLength * m_uSideLength;
100  std::fill(m_tUncompressedData, m_tUncompressedData + uNoOfVoxels, tValue);
101  }
102 
103  template <typename VoxelType>
105  {
106  //Debug mode validation
107  assert(isPowerOf2(uSideLength));
108 
109  //Release mode validation
110  if(!isPowerOf2(uSideLength))
111  {
112  throw std::invalid_argument("Block side length must be a power of two.");
113  }
114 
115  //Compute the side length
116  m_uSideLength = uSideLength;
117  m_uSideLengthPower = logBase2(uSideLength);
118 
119  m_tUncompressedData = new VoxelType[m_uSideLength * m_uSideLength * m_uSideLength];
120 
122  }
123 
124  template <typename VoxelType>
126  {
127  uint32_t uSizeInBytes = sizeof(Block);
128  uSizeInBytes += sizeof(VoxelType) * m_uSideLength * m_uSideLength * m_uSideLength;
129  return uSizeInBytes;
130  }
131 }