PolyVox  0.2.1
Open source voxel management library
RawVolume.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 {
30  template <typename VoxelType>
32  :BaseVolume<VoxelType>(regValid)
33  {
35 
36  //Create a volume of the right size.
37  initialise(regValid);
38  }
39 
47  template <typename VoxelType>
49  {
50  assert(false); // See function comment above.
51  }
52 
56  template <typename VoxelType>
58  {
59  delete[] m_pData;
60  m_pData = 0;
61  }
62 
70  template <typename VoxelType>
72  {
73  assert(false); // See function comment above.
74  }
75 
81  template <typename VoxelType>
83  {
84  return m_tBorderValue;
85  }
86 
93  template <typename VoxelType>
95  {
96  if(this->m_regValidRegion.containsPoint(Vector3DInt32(uXPos, uYPos, uZPos)))
97  {
98  const Vector3DInt32& v3dLowerCorner = this->m_regValidRegion.getLowerCorner();
99  int32_t iLocalXPos = uXPos - v3dLowerCorner.getX();
100  int32_t iLocalYPos = uYPos - v3dLowerCorner.getY();
101  int32_t iLocalZPos = uZPos - v3dLowerCorner.getZ();
102 
103  return m_pData
104  [
105  iLocalXPos +
106  iLocalYPos * this->getWidth() +
107  iLocalZPos * this->getWidth() * this->getHeight()
108  ];
109  }
110  else
111  {
112  return this->getBorderValue();
113  }
114  }
115 
120  template <typename VoxelType>
122  {
123  return getVoxelAt(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ());
124  }
125 
129  template <typename VoxelType>
131  {
132  m_tBorderValue = tBorder;
133  }
134 
142  template <typename VoxelType>
144  {
145  if(this->m_regValidRegion.containsPoint(Vector3DInt32(uXPos, uYPos, uZPos)))
146  {
147  const Vector3DInt32& v3dLowerCorner = this->m_regValidRegion.getLowerCorner();
148  int32_t iLocalXPos = uXPos - v3dLowerCorner.getX();
149  int32_t iLocalYPos = uYPos - v3dLowerCorner.getY();
150  int32_t iLocalZPos = uZPos - v3dLowerCorner.getZ();
151 
152  m_pData
153  [
154  iLocalXPos +
155  iLocalYPos * this->getWidth() +
156  iLocalZPos * this->getWidth() * this->getHeight()
157  ] = tValue;
158 
159  //Return true to indicate that we modified a voxel.
160  return true;
161  }
162  else
163  {
164  return false;
165  }
166  }
167 
173  template <typename VoxelType>
175  {
176  return setVoxelAt(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ(), tValue);
177  }
178 
182  template <typename VoxelType>
183  void RawVolume<VoxelType>::initialise(const Region& regValidRegion)
184  {
185  this->m_regValidRegion = regValidRegion;
186 
187  //Ensure dimensions of the specified Region are valid
188  assert(this->getWidth() > 0);
189  assert(this->getHeight() > 0);
190  assert(this->getDepth() > 0);
191 
192  //Create the data
193  m_pData = new VoxelType[this->getWidth() * this->getHeight()* this->getDepth()];
194 
195  //Other properties we might find useful later
196  this->m_uLongestSideLength = (std::max)((std::max)(this->getWidth(),this->getHeight()),this->getDepth());
197  this->m_uShortestSideLength = (std::min)((std::min)(this->getWidth(),this->getHeight()),this->getDepth());
198  this->m_fDiagonalLength = sqrtf(static_cast<float>(this->getWidth() * this->getWidth() + this->getHeight() * this->getHeight() + this->getDepth() * this->getDepth()));
199  }
200 
204  template <typename VoxelType>
206  {
207  return this->getWidth() * this->getHeight() * this->getDepth() * sizeof(VoxelType);
208  }
209 
210 }
211