PolyVox  0.2.1
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 BORDER_LOWX(val) (val > this->mVolume->getEnclosingRegion().getLowerCorner().getX())
25 #define BORDER_HIGHX(val) (val < this->mVolume->getEnclosingRegion().getUpperCorner().getX())
26 #define BORDER_LOWY(val) (val > this->mVolume->getEnclosingRegion().getLowerCorner().getY())
27 #define BORDER_HIGHY(val) (val < this->mVolume->getEnclosingRegion().getUpperCorner().getY())
28 #define BORDER_LOWZ(val) (val > this->mVolume->getEnclosingRegion().getLowerCorner().getZ())
29 #define BORDER_HIGHZ(val) (val < this->mVolume->getEnclosingRegion().getUpperCorner().getZ())
30 
31 namespace PolyVox
32 {
33  template <typename VoxelType>
35  :BaseVolume<VoxelType>::template Sampler< RawVolume<VoxelType> >(volume)
36  ,mCurrentVoxel(0)
37  ,m_bIsCurrentPositionValidInX(false)
38  ,m_bIsCurrentPositionValidInY(false)
39  ,m_bIsCurrentPositionValidInZ(false)
40  {
41  }
42 
43  template <typename VoxelType>
45  {
46  }
47 
48  template <typename VoxelType>
50  {
51  return (m_bIsCurrentPositionValidInX && m_bIsCurrentPositionValidInY && m_bIsCurrentPositionValidInZ) ? *mCurrentVoxel : this->mVolume->getBorderValue();
52  }
53 
54  template <typename VoxelType>
56  {
57  setPosition(v3dNewPos.getX(), v3dNewPos.getY(), v3dNewPos.getZ());
58  }
59 
60  template <typename VoxelType>
62  {
63  this->mXPosInVolume = xPos;
64  this->mYPosInVolume = yPos;
65  this->mZPosInVolume = zPos;
66 
67  const Vector3DInt32& v3dLowerCorner = this->mVolume->m_regValidRegion.getLowerCorner();
68  int32_t iLocalXPos = xPos - v3dLowerCorner.getX();
69  int32_t iLocalYPos = yPos - v3dLowerCorner.getY();
70  int32_t iLocalZPos = zPos - v3dLowerCorner.getZ();
71 
72  const int32_t uVoxelIndex = iLocalXPos +
73  iLocalYPos * this->mVolume->getWidth() +
74  iLocalZPos * this->mVolume->getWidth() * this->mVolume->getHeight();
75 
76  mCurrentVoxel = this->mVolume->m_pData + uVoxelIndex;
77 
78  m_bIsCurrentPositionValidInX = this->mVolume->getEnclosingRegion().containsPointInX(xPos);
79  m_bIsCurrentPositionValidInY = this->mVolume->getEnclosingRegion().containsPointInY(yPos);
80  m_bIsCurrentPositionValidInZ = this->mVolume->getEnclosingRegion().containsPointInZ(zPos);
81  }
82 
83  template <typename VoxelType>
85  {
86  //return m_bIsCurrentPositionValid ? *mCurrentVoxel : this->mVolume->getBorderValue();
87  if(m_bIsCurrentPositionValidInX && m_bIsCurrentPositionValidInY && m_bIsCurrentPositionValidInZ)
88  {
89  *mCurrentVoxel = tValue;
90  return true;
91  }
92  else
93  {
94  return false;
95  }
96  }
97 
98  template <typename VoxelType>
100  {
101  this->mXPosInVolume++;
102  ++mCurrentVoxel;
103  m_bIsCurrentPositionValidInX = this->mVolume->getEnclosingRegion().containsPointInX(this->mXPosInVolume);
104  }
105 
106  template <typename VoxelType>
108  {
109  this->mYPosInVolume++;
110  mCurrentVoxel += this->mVolume->getWidth();
111  m_bIsCurrentPositionValidInY = this->mVolume->getEnclosingRegion().containsPointInY(this->mYPosInVolume);
112  }
113 
114  template <typename VoxelType>
116  {
117  this->mZPosInVolume++;
118  mCurrentVoxel += this->mVolume->getWidth() * this->mVolume->getHeight();
119  m_bIsCurrentPositionValidInZ = this->mVolume->getEnclosingRegion().containsPointInZ(this->mZPosInVolume);
120  }
121 
122  template <typename VoxelType>
124  {
125  this->mXPosInVolume--;
126  --mCurrentVoxel;
127  m_bIsCurrentPositionValidInX = this->mVolume->getEnclosingRegion().containsPointInX(this->mXPosInVolume);
128  }
129 
130  template <typename VoxelType>
132  {
133  this->mYPosInVolume--;
134  mCurrentVoxel -= this->mVolume->getWidth();
135  m_bIsCurrentPositionValidInY = this->mVolume->getEnclosingRegion().containsPointInY(this->mYPosInVolume);
136  }
137 
138  template <typename VoxelType>
140  {
141  this->mZPosInVolume--;
142  mCurrentVoxel -= this->mVolume->getWidth() * this->mVolume->getHeight();
143  m_bIsCurrentPositionValidInZ = this->mVolume->getEnclosingRegion().containsPointInZ(this->mZPosInVolume);
144  }
145 
146  template <typename VoxelType>
148  {
149  if( BORDER_LOWX(this->mXPosInVolume) && BORDER_LOWY(this->mYPosInVolume) && BORDER_LOWZ(this->mZPosInVolume) )
150  {
151  return *(mCurrentVoxel - 1 - this->mVolume->getWidth() - this->mVolume->getWidth() * this->mVolume->getHeight());
152  }
153  return this->mVolume->getVoxelAt(this->mXPosInVolume-1,this->mYPosInVolume-1,this->mZPosInVolume-1);
154  }
155 
156  template <typename VoxelType>
158  {
159  if( BORDER_LOWX(this->mXPosInVolume) && BORDER_LOWY(this->mYPosInVolume) )
160  {
161  return *(mCurrentVoxel - 1 - this->mVolume->getWidth());
162  }
163  return this->mVolume->getVoxelAt(this->mXPosInVolume-1,this->mYPosInVolume-1,this->mZPosInVolume);
164  }
165 
166  template <typename VoxelType>
168  {
169  if( BORDER_LOWX(this->mXPosInVolume) && BORDER_LOWY(this->mYPosInVolume) && BORDER_HIGHZ(this->mZPosInVolume) )
170  {
171  return *(mCurrentVoxel - 1 - this->mVolume->getWidth() + this->mVolume->getWidth() * this->mVolume->getHeight());
172  }
173  return this->mVolume->getVoxelAt(this->mXPosInVolume-1,this->mYPosInVolume-1,this->mZPosInVolume+1);
174  }
175 
176  template <typename VoxelType>
178  {
179  if( BORDER_LOWX(this->mXPosInVolume) && BORDER_LOWZ(this->mZPosInVolume) )
180  {
181  return *(mCurrentVoxel - 1 - this->mVolume->getWidth() * this->mVolume->getHeight());
182  }
183  return this->mVolume->getVoxelAt(this->mXPosInVolume-1,this->mYPosInVolume,this->mZPosInVolume-1);
184  }
185 
186  template <typename VoxelType>
188  {
189  if( BORDER_LOWX(this->mXPosInVolume) )
190  {
191  return *(mCurrentVoxel - 1);
192  }
193  return this->mVolume->getVoxelAt(this->mXPosInVolume-1,this->mYPosInVolume,this->mZPosInVolume);
194  }
195 
196  template <typename VoxelType>
198  {
199  if( BORDER_LOWX(this->mXPosInVolume) && BORDER_HIGHZ(this->mZPosInVolume) )
200  {
201  return *(mCurrentVoxel - 1 + this->mVolume->getWidth() * this->mVolume->getHeight());
202  }
203  return this->mVolume->getVoxelAt(this->mXPosInVolume-1,this->mYPosInVolume,this->mZPosInVolume+1);
204  }
205 
206  template <typename VoxelType>
208  {
209  if( BORDER_LOWX(this->mXPosInVolume) && BORDER_HIGHY(this->mYPosInVolume) && BORDER_LOWZ(this->mZPosInVolume) )
210  {
211  return *(mCurrentVoxel - 1 + this->mVolume->getWidth() - this->mVolume->getWidth() * this->mVolume->getHeight());
212  }
213  return this->mVolume->getVoxelAt(this->mXPosInVolume-1,this->mYPosInVolume+1,this->mZPosInVolume-1);
214  }
215 
216  template <typename VoxelType>
218  {
219  if( BORDER_LOWX(this->mXPosInVolume) && BORDER_HIGHY(this->mYPosInVolume) )
220  {
221  return *(mCurrentVoxel - 1 + this->mVolume->getWidth());
222  }
223  return this->mVolume->getVoxelAt(this->mXPosInVolume-1,this->mYPosInVolume+1,this->mZPosInVolume);
224  }
225 
226  template <typename VoxelType>
228  {
229  if( BORDER_LOWX(this->mXPosInVolume) && BORDER_HIGHY(this->mYPosInVolume) && BORDER_HIGHZ(this->mZPosInVolume) )
230  {
231  return *(mCurrentVoxel - 1 + this->mVolume->getWidth() + this->mVolume->getWidth() * this->mVolume->getHeight());
232  }
233  return this->mVolume->getVoxelAt(this->mXPosInVolume-1,this->mYPosInVolume+1,this->mZPosInVolume+1);
234  }
235 
237 
238  template <typename VoxelType>
240  {
241  if( BORDER_LOWY(this->mYPosInVolume) && BORDER_LOWZ(this->mZPosInVolume) )
242  {
243  return *(mCurrentVoxel - this->mVolume->getWidth() - this->mVolume->getWidth() * this->mVolume->getHeight());
244  }
245  return this->mVolume->getVoxelAt(this->mXPosInVolume,this->mYPosInVolume-1,this->mZPosInVolume-1);
246  }
247 
248  template <typename VoxelType>
250  {
251  if( BORDER_LOWY(this->mYPosInVolume) )
252  {
253  return *(mCurrentVoxel - this->mVolume->getWidth());
254  }
255  return this->mVolume->getVoxelAt(this->mXPosInVolume,this->mYPosInVolume-1,this->mZPosInVolume);
256  }
257 
258  template <typename VoxelType>
260  {
261  if( BORDER_LOWY(this->mYPosInVolume) && BORDER_HIGHZ(this->mZPosInVolume) )
262  {
263  return *(mCurrentVoxel - this->mVolume->getWidth() + this->mVolume->getWidth() * this->mVolume->getHeight());
264  }
265  return this->mVolume->getVoxelAt(this->mXPosInVolume,this->mYPosInVolume-1,this->mZPosInVolume+1);
266  }
267 
268  template <typename VoxelType>
270  {
271  if( BORDER_LOWZ(this->mZPosInVolume) )
272  {
273  return *(mCurrentVoxel - this->mVolume->getWidth() * this->mVolume->getHeight());
274  }
275  return this->mVolume->getVoxelAt(this->mXPosInVolume,this->mYPosInVolume,this->mZPosInVolume-1);
276  }
277 
278  template <typename VoxelType>
280  {
281  return *mCurrentVoxel;
282  }
283 
284  template <typename VoxelType>
286  {
287  if( BORDER_HIGHZ(this->mZPosInVolume) )
288  {
289  return *(mCurrentVoxel + this->mVolume->getWidth() * this->mVolume->getHeight());
290  }
291  return this->mVolume->getVoxelAt(this->mXPosInVolume,this->mYPosInVolume,this->mZPosInVolume+1);
292  }
293 
294  template <typename VoxelType>
296  {
297  if( BORDER_HIGHY(this->mYPosInVolume) && BORDER_LOWZ(this->mZPosInVolume) )
298  {
299  return *(mCurrentVoxel + this->mVolume->getWidth() - this->mVolume->getWidth() * this->mVolume->getHeight());
300  }
301  return this->mVolume->getVoxelAt(this->mXPosInVolume,this->mYPosInVolume+1,this->mZPosInVolume-1);
302  }
303 
304  template <typename VoxelType>
306  {
307  if( BORDER_HIGHY(this->mYPosInVolume) )
308  {
309  return *(mCurrentVoxel + this->mVolume->getWidth());
310  }
311  return this->mVolume->getVoxelAt(this->mXPosInVolume,this->mYPosInVolume+1,this->mZPosInVolume);
312  }
313 
314  template <typename VoxelType>
316  {
317  if( BORDER_HIGHY(this->mYPosInVolume) && BORDER_HIGHZ(this->mZPosInVolume) )
318  {
319  return *(mCurrentVoxel + this->mVolume->getWidth() + this->mVolume->getWidth() * this->mVolume->getHeight());
320  }
321  return this->mVolume->getVoxelAt(this->mXPosInVolume,this->mYPosInVolume+1,this->mZPosInVolume+1);
322  }
323 
325 
326  template <typename VoxelType>
328  {
329  if( BORDER_HIGHX(this->mXPosInVolume) && BORDER_LOWY(this->mYPosInVolume) && BORDER_LOWZ(this->mZPosInVolume) )
330  {
331  return *(mCurrentVoxel + 1 - this->mVolume->getWidth() - this->mVolume->getWidth() * this->mVolume->getHeight());
332  }
333  return this->mVolume->getVoxelAt(this->mXPosInVolume+1,this->mYPosInVolume-1,this->mZPosInVolume-1);
334  }
335 
336  template <typename VoxelType>
338  {
339  if( BORDER_HIGHX(this->mXPosInVolume) && BORDER_LOWY(this->mYPosInVolume) )
340  {
341  return *(mCurrentVoxel + 1 - this->mVolume->getWidth());
342  }
343  return this->mVolume->getVoxelAt(this->mXPosInVolume+1,this->mYPosInVolume-1,this->mZPosInVolume);
344  }
345 
346  template <typename VoxelType>
348  {
349  if( BORDER_HIGHX(this->mXPosInVolume) && BORDER_LOWY(this->mYPosInVolume) && BORDER_HIGHZ(this->mZPosInVolume) )
350  {
351  return *(mCurrentVoxel + 1 - this->mVolume->getWidth() + this->mVolume->getWidth() * this->mVolume->getHeight());
352  }
353  return this->mVolume->getVoxelAt(this->mXPosInVolume+1,this->mYPosInVolume-1,this->mZPosInVolume+1);
354  }
355 
356  template <typename VoxelType>
358  {
359  if( BORDER_HIGHX(this->mXPosInVolume) && BORDER_LOWZ(this->mZPosInVolume) )
360  {
361  return *(mCurrentVoxel + 1 - this->mVolume->getWidth() * this->mVolume->getHeight());
362  }
363  return this->mVolume->getVoxelAt(this->mXPosInVolume+1,this->mYPosInVolume,this->mZPosInVolume-1);
364  }
365 
366  template <typename VoxelType>
368  {
369  if( BORDER_HIGHX(this->mXPosInVolume) )
370  {
371  return *(mCurrentVoxel + 1);
372  }
373  return this->mVolume->getVoxelAt(this->mXPosInVolume+1,this->mYPosInVolume,this->mZPosInVolume);
374  }
375 
376  template <typename VoxelType>
378  {
379  if( BORDER_HIGHX(this->mXPosInVolume) && BORDER_HIGHZ(this->mZPosInVolume) )
380  {
381  return *(mCurrentVoxel + 1 + this->mVolume->getWidth() * this->mVolume->getHeight());
382  }
383  return this->mVolume->getVoxelAt(this->mXPosInVolume+1,this->mYPosInVolume,this->mZPosInVolume+1);
384  }
385 
386  template <typename VoxelType>
388  {
389  if( BORDER_HIGHX(this->mXPosInVolume) && BORDER_HIGHY(this->mYPosInVolume) && BORDER_LOWZ(this->mZPosInVolume) )
390  {
391  return *(mCurrentVoxel + 1 + this->mVolume->getWidth() - this->mVolume->getWidth() * this->mVolume->getHeight());
392  }
393  return this->mVolume->getVoxelAt(this->mXPosInVolume+1,this->mYPosInVolume+1,this->mZPosInVolume-1);
394  }
395 
396  template <typename VoxelType>
398  {
399  if( BORDER_HIGHX(this->mXPosInVolume) && BORDER_HIGHY(this->mYPosInVolume) )
400  {
401  return *(mCurrentVoxel + 1 + this->mVolume->getWidth());
402  }
403  return this->mVolume->getVoxelAt(this->mXPosInVolume+1,this->mYPosInVolume+1,this->mZPosInVolume);
404  }
405 
406  template <typename VoxelType>
408  {
409  if( BORDER_HIGHX(this->mXPosInVolume) && BORDER_HIGHY(this->mYPosInVolume) && BORDER_HIGHZ(this->mZPosInVolume) )
410  {
411  return *(mCurrentVoxel + 1 + this->mVolume->getWidth() + this->mVolume->getWidth() * this->mVolume->getHeight());
412  }
413  return this->mVolume->getVoxelAt(this->mXPosInVolume+1,this->mYPosInVolume+1,this->mZPosInVolume+1);
414  }
415 }
416 
417 #undef BORDER_LOWX
418 #undef BORDER_HIGHX
419 #undef BORDER_LOWY
420 #undef BORDER_HIGHY
421 #undef BORDER_LOWZ
422 #undef BORDER_HIGHZ