PolyVox  0.3.0-dev
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  {
34  this->setBorderValue(VoxelType());
35 
36  //Create a volume of the right size.
37  initialise(regValid);
38  }
39 
47  template <typename VoxelType>
49  {
50  POLYVOX_ASSERT(false, "Copy constructor not implemented."); // 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  POLYVOX_ASSERT(false, "Assignment operator not implemented."); // See function comment above.
74  }
75 
82  template <typename VoxelType>
84  {
85  POLYVOX_ASSERT(this->m_regValidRegion.containsPoint(Vector3DInt32(uXPos, uYPos, uZPos)), "Position is outside valid region");
86 
87  const Vector3DInt32& v3dLowerCorner = this->m_regValidRegion.getLowerCorner();
88  int32_t iLocalXPos = uXPos - v3dLowerCorner.getX();
89  int32_t iLocalYPos = uYPos - v3dLowerCorner.getY();
90  int32_t iLocalZPos = uZPos - v3dLowerCorner.getZ();
91 
92  return m_pData
93  [
94  iLocalXPos +
95  iLocalYPos * this->getWidth() +
96  iLocalZPos * this->getWidth() * this->getHeight()
97  ];
98  }
99 
104  template <typename VoxelType>
106  {
107  return getVoxel(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ());
108  }
109 
116  template <typename VoxelType>
118  {
119  if(this->m_regValidRegion.containsPoint(Vector3DInt32(uXPos, uYPos, uZPos)))
120  {
121  const Vector3DInt32& v3dLowerCorner = this->m_regValidRegion.getLowerCorner();
122  int32_t iLocalXPos = uXPos - v3dLowerCorner.getX();
123  int32_t iLocalYPos = uYPos - v3dLowerCorner.getY();
124  int32_t iLocalZPos = uZPos - v3dLowerCorner.getZ();
125 
126  return m_pData
127  [
128  iLocalXPos +
129  iLocalYPos * this->getWidth() +
130  iLocalZPos * this->getWidth() * this->getHeight()
131  ];
132  }
133  else
134  {
135  return this->getBorderValue();
136  }
137  }
138 
143  template <typename VoxelType>
145  {
146  return getVoxelAt(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ());
147  }
148 
155  template <typename VoxelType>
157  {
158  switch(eWrapMode)
159  {
160  case WrapModes::Clamp:
161  {
162  //Perform clamping
163  uXPos = (std::max)(uXPos, this->m_regValidRegion.getLowerX());
164  uYPos = (std::max)(uYPos, this->m_regValidRegion.getLowerY());
165  uZPos = (std::max)(uZPos, this->m_regValidRegion.getLowerZ());
166  uXPos = (std::min)(uXPos, this->m_regValidRegion.getUpperX());
167  uYPos = (std::min)(uYPos, this->m_regValidRegion.getUpperY());
168  uZPos = (std::min)(uZPos, this->m_regValidRegion.getUpperZ());
169 
170  //Get the voxel value
171  return getVoxel(uXPos, uYPos, uZPos);
172  //No need to break as we've returned
173  }
174  case WrapModes::Border:
175  {
176  if(this->m_regValidRegion.containsPoint(uXPos, uYPos, uZPos))
177  {
178  return getVoxel(uXPos, uYPos, uZPos);
179  }
180  else
181  {
182  return tBorder;
183  }
184  //No need to break as we've returned
185  }
186  default:
187  {
188  //Should never happen
189  POLYVOX_ASSERT(false, "Invalid case.");
190  return VoxelType();
191  }
192  }
193  }
194 
199  template <typename VoxelType>
201  {
202  return getVoxelWithWrapping(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ(), eWrapMode, tBorder);
203  }
204 
212  template <typename VoxelType>
214  {
215  if(this->m_regValidRegion.containsPoint(Vector3DInt32(uXPos, uYPos, uZPos)))
216  {
217  const Vector3DInt32& v3dLowerCorner = this->m_regValidRegion.getLowerCorner();
218  int32_t iLocalXPos = uXPos - v3dLowerCorner.getX();
219  int32_t iLocalYPos = uYPos - v3dLowerCorner.getY();
220  int32_t iLocalZPos = uZPos - v3dLowerCorner.getZ();
221 
222  m_pData
223  [
224  iLocalXPos +
225  iLocalYPos * this->getWidth() +
226  iLocalZPos * this->getWidth() * this->getHeight()
227  ] = tValue;
228 
229  //Return true to indicate that we modified a voxel.
230  return true;
231  }
232  else
233  {
234  return false;
235  }
236  }
237 
243  template <typename VoxelType>
245  {
246  return setVoxelAt(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ(), tValue);
247  }
248 
252  template <typename VoxelType>
253  void RawVolume<VoxelType>::initialise(const Region& regValidRegion)
254  {
255  this->m_regValidRegion = regValidRegion;
256 
257  //Ensure dimensions of the specified Region are valid
258  POLYVOX_ASSERT(this->getWidth() > 0, "Volume width must be greater than zero.");
259  POLYVOX_ASSERT(this->getHeight() > 0, "Volume width must be greater than zero.");
260  POLYVOX_ASSERT(this->getDepth() > 0, "Volume width must be greater than zero.");
261 
262  //Create the data
263  m_pData = new VoxelType[this->getWidth() * this->getHeight()* this->getDepth()];
264 
265  //Other properties we might find useful later
266  this->m_uLongestSideLength = (std::max)((std::max)(this->getWidth(),this->getHeight()),this->getDepth());
267  this->m_uShortestSideLength = (std::min)((std::min)(this->getWidth(),this->getHeight()),this->getDepth());
268  this->m_fDiagonalLength = sqrtf(static_cast<float>(this->getWidth() * this->getWidth() + this->getHeight() * this->getHeight() + this->getDepth() * this->getDepth()));
269  }
270 
274  template <typename VoxelType>
276  {
277  return this->getWidth() * this->getHeight() * this->getDepth() * sizeof(VoxelType);
278  }
279 
280 }
281