PolyVox  0.3.0-dev
Open source voxel management library
RawVolumeSampler.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 #define CAN_GO_NEG_X(val) (val > this->mVolume->getEnclosingRegion().getLowerX())
25 #define CAN_GO_POS_X(val) (val < this->mVolume->getEnclosingRegion().getUpperX())
26 #define CAN_GO_NEG_Y(val) (val > this->mVolume->getEnclosingRegion().getLowerY())
27 #define CAN_GO_POS_Y(val) (val < this->mVolume->getEnclosingRegion().getUpperY())
28 #define CAN_GO_NEG_Z(val) (val > this->mVolume->getEnclosingRegion().getLowerZ())
29 #define CAN_GO_POS_Z(val) (val < this->mVolume->getEnclosingRegion().getUpperZ())
30 
31 namespace PolyVox
32 {
33  template <typename VoxelType>
35  :BaseVolume<VoxelType>::template Sampler< RawVolume<VoxelType> >(volume)
36  ,mCurrentVoxel(0)
37  {
38  }
39 
40  template <typename VoxelType>
42  {
43  }
44 
45  template <typename VoxelType>
47  {
48  if(this->isCurrentPositionValid())
49  {
50  return *mCurrentVoxel;
51  }
52  else
53  {
54  return getVoxelAt(this->mXPosInVolume, this->mYPosInVolume, this->mZPosInVolume);
55  }
56  }
57 
58  template <typename VoxelType>
60  {
61  setPosition(v3dNewPos.getX(), v3dNewPos.getY(), v3dNewPos.getZ());
62  }
63 
64  template <typename VoxelType>
66  {
67  // Base version updates position and validity flags.
68  BaseVolume<VoxelType>::template Sampler< RawVolume<VoxelType> >::setPosition(xPos, yPos, zPos);
69 
70  // Then we update the voxel pointer
71  if(this->isCurrentPositionValid())
72  {
73  const Vector3DInt32& v3dLowerCorner = this->mVolume->m_regValidRegion.getLowerCorner();
74  int32_t iLocalXPos = xPos - v3dLowerCorner.getX();
75  int32_t iLocalYPos = yPos - v3dLowerCorner.getY();
76  int32_t iLocalZPos = zPos - v3dLowerCorner.getZ();
77 
78  const int32_t uVoxelIndex = iLocalXPos +
79  iLocalYPos * this->mVolume->getWidth() +
80  iLocalZPos * this->mVolume->getWidth() * this->mVolume->getHeight();
81 
82  mCurrentVoxel = this->mVolume->m_pData + uVoxelIndex;
83  }
84  else
85  {
86  mCurrentVoxel = 0;
87  }
88  }
89 
90  template <typename VoxelType>
92  {
93  //return m_bIsCurrentPositionValid ? *mCurrentVoxel : this->mVolume->getBorderValue();
94  if(this->m_bIsCurrentPositionValidInX && this->m_bIsCurrentPositionValidInY && this->m_bIsCurrentPositionValidInZ)
95  {
96  *mCurrentVoxel = tValue;
97  return true;
98  }
99  else
100  {
101  return false;
102  }
103  }
104 
105  template <typename VoxelType>
107  {
108  // We'll need this in a moment...
109  bool bIsOldPositionValid = this->isCurrentPositionValid();
110 
111  // Base version updates position and validity flags.
113 
114  // Then we update the voxel pointer
115  if(this->isCurrentPositionValid() && bIsOldPositionValid )
116  {
117  ++mCurrentVoxel;
118  }
119  else
120  {
121  setPosition(this->mXPosInVolume, this->mYPosInVolume, this->mZPosInVolume);
122  }
123  }
124 
125  template <typename VoxelType>
127  {
128  // We'll need this in a moment...
129  bool bIsOldPositionValid = this->isCurrentPositionValid();
130 
131  // Base version updates position and validity flags.
133 
134  // Then we update the voxel pointer
135  if(this->isCurrentPositionValid() && bIsOldPositionValid )
136  {
137  mCurrentVoxel += this->mVolume->getWidth();
138  }
139  else
140  {
141  setPosition(this->mXPosInVolume, this->mYPosInVolume, this->mZPosInVolume);
142  }
143  }
144 
145  template <typename VoxelType>
147  {
148  // We'll need this in a moment...
149  bool bIsOldPositionValid = this->isCurrentPositionValid();
150 
151  // Base version updates position and validity flags.
153 
154  // Then we update the voxel pointer
155  if(this->isCurrentPositionValid() && bIsOldPositionValid )
156  {
157  mCurrentVoxel += this->mVolume->getWidth() * this->mVolume->getHeight();
158  }
159  else
160  {
161  setPosition(this->mXPosInVolume, this->mYPosInVolume, this->mZPosInVolume);
162  }
163  }
164 
165  template <typename VoxelType>
167  {
168  // We'll need this in a moment...
169  bool bIsOldPositionValid = this->isCurrentPositionValid();
170 
171  // Base version updates position and validity flags.
173 
174  // Then we update the voxel pointer
175  if(this->isCurrentPositionValid() && bIsOldPositionValid )
176  {
177  --mCurrentVoxel;
178  }
179  else
180  {
181  setPosition(this->mXPosInVolume, this->mYPosInVolume, this->mZPosInVolume);
182  }
183  }
184 
185  template <typename VoxelType>
187  {
188  // We'll need this in a moment...
189  bool bIsOldPositionValid = this->isCurrentPositionValid();
190 
191  // Base version updates position and validity flags.
193 
194  // Then we update the voxel pointer
195  if(this->isCurrentPositionValid() && bIsOldPositionValid )
196  {
197  mCurrentVoxel -= this->mVolume->getWidth();
198  }
199  else
200  {
201  setPosition(this->mXPosInVolume, this->mYPosInVolume, this->mZPosInVolume);
202  }
203  }
204 
205  template <typename VoxelType>
207  {
208  // We'll need this in a moment...
209  bool bIsOldPositionValid = this->isCurrentPositionValid();
210 
211  // Base version updates position and validity flags.
213 
214  // Then we update the voxel pointer
215  if(this->isCurrentPositionValid() && bIsOldPositionValid )
216  {
217  mCurrentVoxel -= this->mVolume->getWidth() * this->mVolume->getHeight();
218  }
219  else
220  {
221  setPosition(this->mXPosInVolume, this->mYPosInVolume, this->mZPosInVolume);
222  }
223  }
224 
225  template <typename VoxelType>
227  {
228  if((this->isCurrentPositionValid()) && CAN_GO_NEG_X(this->mXPosInVolume) && CAN_GO_NEG_Y(this->mYPosInVolume) && CAN_GO_NEG_Z(this->mZPosInVolume) )
229  {
230  return *(mCurrentVoxel - 1 - this->mVolume->getWidth() - this->mVolume->getWidth() * this->mVolume->getHeight());
231  }
232  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume-1,this->mYPosInVolume-1,this->mZPosInVolume-1,this->m_eWrapMode, this->m_tBorder);
233  }
234 
235  template <typename VoxelType>
237  {
238  if((this->isCurrentPositionValid()) && CAN_GO_NEG_X(this->mXPosInVolume) && CAN_GO_NEG_Y(this->mYPosInVolume) )
239  {
240  return *(mCurrentVoxel - 1 - this->mVolume->getWidth());
241  }
242  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume-1,this->mYPosInVolume-1,this->mZPosInVolume,this->m_eWrapMode, this->m_tBorder);
243  }
244 
245  template <typename VoxelType>
247  {
248  if((this->isCurrentPositionValid()) && CAN_GO_NEG_X(this->mXPosInVolume) && CAN_GO_NEG_Y(this->mYPosInVolume) && CAN_GO_POS_Z(this->mZPosInVolume) )
249  {
250  return *(mCurrentVoxel - 1 - this->mVolume->getWidth() + this->mVolume->getWidth() * this->mVolume->getHeight());
251  }
252  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume-1,this->mYPosInVolume-1,this->mZPosInVolume+1,this->m_eWrapMode, this->m_tBorder);
253  }
254 
255  template <typename VoxelType>
257  {
258  if((this->isCurrentPositionValid()) && CAN_GO_NEG_X(this->mXPosInVolume) && CAN_GO_NEG_Z(this->mZPosInVolume) )
259  {
260  return *(mCurrentVoxel - 1 - this->mVolume->getWidth() * this->mVolume->getHeight());
261  }
262  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume-1,this->mYPosInVolume,this->mZPosInVolume-1,this->m_eWrapMode, this->m_tBorder);
263  }
264 
265  template <typename VoxelType>
267  {
268  if((this->isCurrentPositionValid()) && CAN_GO_NEG_X(this->mXPosInVolume) )
269  {
270  return *(mCurrentVoxel - 1);
271  }
272  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume-1,this->mYPosInVolume,this->mZPosInVolume,this->m_eWrapMode, this->m_tBorder);
273  }
274 
275  template <typename VoxelType>
277  {
278  if((this->isCurrentPositionValid()) && CAN_GO_NEG_X(this->mXPosInVolume) && CAN_GO_POS_Z(this->mZPosInVolume) )
279  {
280  return *(mCurrentVoxel - 1 + this->mVolume->getWidth() * this->mVolume->getHeight());
281  }
282  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume-1,this->mYPosInVolume,this->mZPosInVolume+1,this->m_eWrapMode, this->m_tBorder);
283  }
284 
285  template <typename VoxelType>
287  {
288  if((this->isCurrentPositionValid()) && CAN_GO_NEG_X(this->mXPosInVolume) && CAN_GO_POS_Y(this->mYPosInVolume) && CAN_GO_NEG_Z(this->mZPosInVolume) )
289  {
290  return *(mCurrentVoxel - 1 + this->mVolume->getWidth() - this->mVolume->getWidth() * this->mVolume->getHeight());
291  }
292  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume-1,this->mYPosInVolume+1,this->mZPosInVolume-1,this->m_eWrapMode, this->m_tBorder);
293  }
294 
295  template <typename VoxelType>
297  {
298  if((this->isCurrentPositionValid()) && CAN_GO_NEG_X(this->mXPosInVolume) && CAN_GO_POS_Y(this->mYPosInVolume) )
299  {
300  return *(mCurrentVoxel - 1 + this->mVolume->getWidth());
301  }
302  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume-1,this->mYPosInVolume+1,this->mZPosInVolume,this->m_eWrapMode, this->m_tBorder);
303  }
304 
305  template <typename VoxelType>
307  {
308  if((this->isCurrentPositionValid()) && CAN_GO_NEG_X(this->mXPosInVolume) && CAN_GO_POS_Y(this->mYPosInVolume) && CAN_GO_POS_Z(this->mZPosInVolume) )
309  {
310  return *(mCurrentVoxel - 1 + this->mVolume->getWidth() + this->mVolume->getWidth() * this->mVolume->getHeight());
311  }
312  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume-1,this->mYPosInVolume+1,this->mZPosInVolume+1,this->m_eWrapMode, this->m_tBorder);
313  }
314 
316 
317  template <typename VoxelType>
319  {
320  if((this->isCurrentPositionValid()) && CAN_GO_NEG_Y(this->mYPosInVolume) && CAN_GO_NEG_Z(this->mZPosInVolume) )
321  {
322  return *(mCurrentVoxel - this->mVolume->getWidth() - this->mVolume->getWidth() * this->mVolume->getHeight());
323  }
324  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume,this->mYPosInVolume-1,this->mZPosInVolume-1,this->m_eWrapMode, this->m_tBorder);
325  }
326 
327  template <typename VoxelType>
329  {
330  if((this->isCurrentPositionValid()) && CAN_GO_NEG_Y(this->mYPosInVolume) )
331  {
332  return *(mCurrentVoxel - this->mVolume->getWidth());
333  }
334  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume,this->mYPosInVolume-1,this->mZPosInVolume,this->m_eWrapMode, this->m_tBorder);
335  }
336 
337  template <typename VoxelType>
339  {
340  if((this->isCurrentPositionValid()) && CAN_GO_NEG_Y(this->mYPosInVolume) && CAN_GO_POS_Z(this->mZPosInVolume) )
341  {
342  return *(mCurrentVoxel - this->mVolume->getWidth() + this->mVolume->getWidth() * this->mVolume->getHeight());
343  }
344  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume,this->mYPosInVolume-1,this->mZPosInVolume+1,this->m_eWrapMode, this->m_tBorder);
345  }
346 
347  template <typename VoxelType>
349  {
350  if((this->isCurrentPositionValid()) && CAN_GO_NEG_Z(this->mZPosInVolume) )
351  {
352  return *(mCurrentVoxel - this->mVolume->getWidth() * this->mVolume->getHeight());
353  }
354  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume,this->mYPosInVolume,this->mZPosInVolume-1,this->m_eWrapMode, this->m_tBorder);
355  }
356 
357  template <typename VoxelType>
359  {
360  if((this->isCurrentPositionValid()))
361  {
362  return *mCurrentVoxel;
363  }
364  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume, this->mYPosInVolume, this->mZPosInVolume,this->m_eWrapMode, this->m_tBorder);
365  }
366 
367  template <typename VoxelType>
369  {
370  if((this->isCurrentPositionValid()) && CAN_GO_POS_Z(this->mZPosInVolume) )
371  {
372  return *(mCurrentVoxel + this->mVolume->getWidth() * this->mVolume->getHeight());
373  }
374  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume,this->mYPosInVolume,this->mZPosInVolume+1,this->m_eWrapMode, this->m_tBorder);
375  }
376 
377  template <typename VoxelType>
379  {
380  if((this->isCurrentPositionValid()) && CAN_GO_POS_Y(this->mYPosInVolume) && CAN_GO_NEG_Z(this->mZPosInVolume) )
381  {
382  return *(mCurrentVoxel + this->mVolume->getWidth() - this->mVolume->getWidth() * this->mVolume->getHeight());
383  }
384  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume,this->mYPosInVolume+1,this->mZPosInVolume-1,this->m_eWrapMode, this->m_tBorder);
385  }
386 
387  template <typename VoxelType>
389  {
390  if((this->isCurrentPositionValid()) && CAN_GO_POS_Y(this->mYPosInVolume) )
391  {
392  return *(mCurrentVoxel + this->mVolume->getWidth());
393  }
394  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume,this->mYPosInVolume+1,this->mZPosInVolume,this->m_eWrapMode, this->m_tBorder);
395  }
396 
397  template <typename VoxelType>
399  {
400  if((this->isCurrentPositionValid()) && CAN_GO_POS_Y(this->mYPosInVolume) && CAN_GO_POS_Z(this->mZPosInVolume) )
401  {
402  return *(mCurrentVoxel + this->mVolume->getWidth() + this->mVolume->getWidth() * this->mVolume->getHeight());
403  }
404  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume,this->mYPosInVolume+1,this->mZPosInVolume+1,this->m_eWrapMode, this->m_tBorder);
405  }
406 
408 
409  template <typename VoxelType>
411  {
412  if((this->isCurrentPositionValid()) && CAN_GO_POS_X(this->mXPosInVolume) && CAN_GO_NEG_Y(this->mYPosInVolume) && CAN_GO_NEG_Z(this->mZPosInVolume) )
413  {
414  return *(mCurrentVoxel + 1 - this->mVolume->getWidth() - this->mVolume->getWidth() * this->mVolume->getHeight());
415  }
416  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume+1,this->mYPosInVolume-1,this->mZPosInVolume-1,this->m_eWrapMode, this->m_tBorder);
417  }
418 
419  template <typename VoxelType>
421  {
422  if((this->isCurrentPositionValid()) && CAN_GO_POS_X(this->mXPosInVolume) && CAN_GO_NEG_Y(this->mYPosInVolume) )
423  {
424  return *(mCurrentVoxel + 1 - this->mVolume->getWidth());
425  }
426  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume+1,this->mYPosInVolume-1,this->mZPosInVolume,this->m_eWrapMode, this->m_tBorder);
427  }
428 
429  template <typename VoxelType>
431  {
432  if((this->isCurrentPositionValid()) && CAN_GO_POS_X(this->mXPosInVolume) && CAN_GO_NEG_Y(this->mYPosInVolume) && CAN_GO_POS_Z(this->mZPosInVolume) )
433  {
434  return *(mCurrentVoxel + 1 - this->mVolume->getWidth() + this->mVolume->getWidth() * this->mVolume->getHeight());
435  }
436  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume+1,this->mYPosInVolume-1,this->mZPosInVolume+1,this->m_eWrapMode, this->m_tBorder);
437  }
438 
439  template <typename VoxelType>
441  {
442  if((this->isCurrentPositionValid()) && CAN_GO_POS_X(this->mXPosInVolume) && CAN_GO_NEG_Z(this->mZPosInVolume) )
443  {
444  return *(mCurrentVoxel + 1 - this->mVolume->getWidth() * this->mVolume->getHeight());
445  }
446  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume+1,this->mYPosInVolume,this->mZPosInVolume-1,this->m_eWrapMode, this->m_tBorder);
447  }
448 
449  template <typename VoxelType>
451  {
452  if((this->isCurrentPositionValid()) && CAN_GO_POS_X(this->mXPosInVolume) )
453  {
454  return *(mCurrentVoxel + 1);
455  }
456  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume+1,this->mYPosInVolume,this->mZPosInVolume,this->m_eWrapMode, this->m_tBorder);
457  }
458 
459  template <typename VoxelType>
461  {
462  if((this->isCurrentPositionValid()) && CAN_GO_POS_X(this->mXPosInVolume) && CAN_GO_POS_Z(this->mZPosInVolume) )
463  {
464  return *(mCurrentVoxel + 1 + this->mVolume->getWidth() * this->mVolume->getHeight());
465  }
466  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume+1,this->mYPosInVolume,this->mZPosInVolume+1,this->m_eWrapMode, this->m_tBorder);
467  }
468 
469  template <typename VoxelType>
471  {
472  if((this->isCurrentPositionValid()) && CAN_GO_POS_X(this->mXPosInVolume) && CAN_GO_POS_Y(this->mYPosInVolume) && CAN_GO_NEG_Z(this->mZPosInVolume) )
473  {
474  return *(mCurrentVoxel + 1 + this->mVolume->getWidth() - this->mVolume->getWidth() * this->mVolume->getHeight());
475  }
476  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume+1,this->mYPosInVolume+1,this->mZPosInVolume-1,this->m_eWrapMode, this->m_tBorder);
477  }
478 
479  template <typename VoxelType>
481  {
482  if((this->isCurrentPositionValid()) && CAN_GO_POS_X(this->mXPosInVolume) && CAN_GO_POS_Y(this->mYPosInVolume) )
483  {
484  return *(mCurrentVoxel + 1 + this->mVolume->getWidth());
485  }
486  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume+1,this->mYPosInVolume+1,this->mZPosInVolume,this->m_eWrapMode, this->m_tBorder);
487  }
488 
489  template <typename VoxelType>
491  {
492  if((this->isCurrentPositionValid()) && CAN_GO_POS_X(this->mXPosInVolume) && CAN_GO_POS_Y(this->mYPosInVolume) && CAN_GO_POS_Z(this->mZPosInVolume) )
493  {
494  return *(mCurrentVoxel + 1 + this->mVolume->getWidth() + this->mVolume->getWidth() * this->mVolume->getHeight());
495  }
496  return this->mVolume->getVoxelWithWrapping(this->mXPosInVolume+1,this->mYPosInVolume+1,this->mZPosInVolume+1,this->m_eWrapMode, this->m_tBorder);
497  }
498 }
499 
500 #undef CAN_GO_NEG_X
501 #undef CAN_GO_POS_X
502 #undef CAN_GO_NEG_Y
503 #undef CAN_GO_POS_Y
504 #undef CAN_GO_NEG_Z
505 #undef CAN_GO_POS_Z