PolyVox  0.3.0-dev
Open source voxel management library
Region.cpp
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 #include "PolyVoxCore/Region.h"
25 
26 #include <limits>
27 
28 namespace PolyVox
29 {
32  const Region Region::MaxRegion
33  (
34  Vector3DInt32((std::numeric_limits<int32_t>::min)(), (std::numeric_limits<int32_t>::min)(), (std::numeric_limits<int32_t>::min)()),
35  Vector3DInt32((std::numeric_limits<int32_t>::max)(), (std::numeric_limits<int32_t>::max)(), (std::numeric_limits<int32_t>::max)())
36  );
37 
43  const Region Region::InvertedRegion
44  (
45  Vector3DInt32((std::numeric_limits<int32_t>::max)(), (std::numeric_limits<int32_t>::max)(), (std::numeric_limits<int32_t>::max)()),
46  Vector3DInt32((std::numeric_limits<int32_t>::min)(), (std::numeric_limits<int32_t>::min)(), (std::numeric_limits<int32_t>::min)())
47  );
48 
55  {
56  m_iLowerX = ((std::min)(m_iLowerX, iX));
57  m_iLowerY = ((std::min)(m_iLowerY, iY));
58  m_iLowerZ = ((std::min)(m_iLowerZ, iZ));
59  m_iUpperX = ((std::max)(m_iUpperX, iX));
60  m_iUpperY = ((std::max)(m_iUpperY, iY));
61  m_iUpperZ = ((std::max)(m_iUpperZ, iZ));
62  }
63 
67  void Region::accumulate(const Vector3DInt32& v3dPos)
68  {
69  accumulate(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ());
70  }
71 
79  void Region::accumulate(const Region& reg)
80  {
81  POLYVOX_ASSERT(reg.isValid(), "You cannot accumulate an invalid region."); //The result of accumulating an invalid region is not defined.
82 
83  m_iLowerX = ((std::min)(m_iLowerX, reg.getLowerX()));
84  m_iLowerY = ((std::min)(m_iLowerY, reg.getLowerY()));
85  m_iLowerZ = ((std::min)(m_iLowerZ, reg.getLowerZ()));
86  m_iUpperX = ((std::max)(m_iUpperX, reg.getUpperX()));
87  m_iUpperY = ((std::max)(m_iUpperY, reg.getUpperY()));
88  m_iUpperZ = ((std::max)(m_iUpperZ, reg.getUpperZ()));
89  }
90 
95  :m_iLowerX(0)
96  ,m_iLowerY(0)
97  ,m_iLowerZ(0)
98  ,m_iUpperX(0)
99  ,m_iUpperY(0)
100  ,m_iUpperZ(0)
101  {
102  }
103 
109  Region::Region(const Vector3DInt32& v3dLowerCorner, const Vector3DInt32& v3dUpperCorner)
110  :m_iLowerX(v3dLowerCorner.getX())
111  ,m_iLowerY(v3dLowerCorner.getY())
112  ,m_iLowerZ(v3dLowerCorner.getZ())
113  ,m_iUpperX(v3dUpperCorner.getX())
114  ,m_iUpperY(v3dUpperCorner.getY())
115  ,m_iUpperZ(v3dUpperCorner.getZ())
116  {
117  }
118 
128  Region::Region(int32_t iLowerX, int32_t iLowerY, int32_t iLowerZ, int32_t iUpperX, int32_t iUpperY, int32_t iUpperZ)
129  :m_iLowerX(iLowerX)
130  ,m_iLowerY(iLowerY)
131  ,m_iLowerZ(iLowerZ)
132  ,m_iUpperX(iUpperX)
133  ,m_iUpperY(iUpperY)
134  ,m_iUpperZ(iUpperZ)
135  {
136  }
137 
144  bool Region::operator==(const Region& rhs) const
145  {
146  return ((m_iLowerX == rhs.m_iLowerX) && (m_iLowerY == rhs.m_iLowerY) && (m_iLowerZ == rhs.m_iLowerZ)
147  && (m_iUpperX == rhs.m_iUpperX) && (m_iUpperY == rhs.m_iUpperY) && (m_iUpperZ == rhs.m_iUpperZ));
148  }
149 
156  bool Region::operator!=(const Region& rhs) const
157  {
158  return !(*this == rhs);
159  }
160 
170  bool Region::containsPoint(float fX, float fY, float fZ, float boundary) const
171  {
172  return (fX <= m_iUpperX - boundary)
173  && (fY <= m_iUpperY - boundary)
174  && (fZ <= m_iUpperZ - boundary)
175  && (fX >= m_iLowerX + boundary)
176  && (fY >= m_iLowerY + boundary)
177  && (fZ >= m_iLowerZ + boundary);
178  }
179 
187  bool Region::containsPoint(const Vector3DFloat& pos, float boundary) const
188  {
189  return containsPoint(pos.getX(), pos.getY(), pos.getZ(), boundary);
190  }
191 
201  bool Region::containsPoint(int32_t iX, int32_t iY, int32_t iZ, uint8_t boundary) const
202  {
203  return (iX <= m_iUpperX - boundary)
204  && (iY <= m_iUpperY - boundary)
205  && (iZ <= m_iUpperZ - boundary)
206  && (iX >= m_iLowerX + boundary)
207  && (iY >= m_iLowerY + boundary)
208  && (iZ >= m_iLowerZ + boundary);
209  }
210 
218  bool Region::containsPoint(const Vector3DInt32& pos, uint8_t boundary) const
219  {
220  return containsPoint(pos.getX(), pos.getY(), pos.getZ(), boundary);
221  }
222 
230  bool Region::containsPointInX(float pos, float boundary) const
231  {
232  return (pos <= m_iUpperX - boundary)
233  && (pos >= m_iLowerX + boundary);
234  }
235 
243  bool Region::containsPointInX(int32_t pos, uint8_t boundary) const
244  {
245  return (pos <= m_iUpperX - boundary)
246  && (pos >= m_iLowerX + boundary);
247  }
248 
256  bool Region::containsPointInY(float pos, float boundary) const
257  {
258  return (pos <= m_iUpperY - boundary)
259  && (pos >= m_iLowerY + boundary);
260  }
261 
269  bool Region::containsPointInY(int32_t pos, uint8_t boundary) const
270  {
271  return (pos <= m_iUpperY - boundary)
272  && (pos >= m_iLowerY + boundary);
273  }
274 
282  bool Region::containsPointInZ(float pos, float boundary) const
283  {
284  return (pos <= m_iUpperZ - boundary)
285  && (pos >= m_iLowerZ + boundary);
286  }
287 
295  bool Region::containsPointInZ(int32_t pos, uint8_t boundary) const
296  {
297  return (pos <= m_iUpperZ - boundary)
298  && (pos >= m_iLowerZ + boundary);
299  }
300 
306  void Region::cropTo(const Region& other)
307  {
308  m_iLowerX = ((std::max)(m_iLowerX, other.m_iLowerX));
309  m_iLowerY = ((std::max)(m_iLowerY, other.m_iLowerY));
310  m_iLowerZ = ((std::max)(m_iLowerZ, other.m_iLowerZ));
311  m_iUpperX = ((std::min)(m_iUpperX, other.m_iUpperX));
312  m_iUpperY = ((std::min)(m_iUpperY, other.m_iUpperY));
313  m_iUpperZ = ((std::min)(m_iUpperZ, other.m_iUpperZ));
314  }
315 
321  void Region::grow(int32_t iAmount)
322  {
323  m_iLowerX -= iAmount;
324  m_iLowerY -= iAmount;
325  m_iLowerZ -= iAmount;
326 
327  m_iUpperX += iAmount;
328  m_iUpperY += iAmount;
329  m_iUpperZ += iAmount;
330  }
331 
339  void Region::grow(int32_t iAmountX, int32_t iAmountY, int32_t iAmountZ)
340  {
341  m_iLowerX -= iAmountX;
342  m_iLowerY -= iAmountY;
343  m_iLowerZ -= iAmountZ;
344 
345  m_iUpperX += iAmountX;
346  m_iUpperY += iAmountY;
347  m_iUpperZ += iAmountZ;
348  }
349 
355  void Region::grow(const Vector3DInt32& v3dAmount)
356  {
357  grow(v3dAmount.getX(), v3dAmount.getY(), v3dAmount.getZ());
358  }
359 
362  bool Region::isValid(void) const
363  {
364  return (m_iUpperX >= m_iLowerX) && (m_iUpperY >= m_iLowerY) && (m_iUpperZ >= m_iLowerZ);
365  }
366 
372  void Region::shift(int32_t iAmountX, int32_t iAmountY, int32_t iAmountZ)
373  {
374  shiftLowerCorner(iAmountX, iAmountY, iAmountZ);
375  shiftUpperCorner(iAmountX, iAmountY, iAmountZ);
376  }
377 
381  void Region::shift(const Vector3DInt32& v3dAmount)
382  {
383  shiftLowerCorner(v3dAmount);
384  shiftUpperCorner(v3dAmount);
385  }
386 
392  void Region::shiftLowerCorner(int32_t iAmountX, int32_t iAmountY, int32_t iAmountZ)
393  {
394  m_iLowerX += iAmountX;
395  m_iLowerY += iAmountY;
396  m_iLowerZ += iAmountZ;
397  }
398 
403  {
404  shiftLowerCorner(v3dAmount.getX(), v3dAmount.getY(), v3dAmount.getZ());
405  }
406 
412  void Region::shiftUpperCorner(int32_t iAmountX, int32_t iAmountY, int32_t iAmountZ)
413  {
414  m_iUpperX += iAmountX;
415  m_iUpperY += iAmountY;
416  m_iUpperZ += iAmountZ;
417  }
418 
423  {
424  shiftUpperCorner(v3dAmount.getX(), v3dAmount.getY(), v3dAmount.getZ());
425  }
426 
432  void Region::shrink(int32_t iAmount)
433  {
434  m_iLowerX += iAmount;
435  m_iLowerY += iAmount;
436  m_iLowerZ += iAmount;
437 
438  m_iUpperX -= iAmount;
439  m_iUpperY -= iAmount;
440  m_iUpperZ -= iAmount;
441  }
442 
450  void Region::shrink(int32_t iAmountX, int32_t iAmountY, int32_t iAmountZ)
451  {
452  m_iLowerX += iAmountX;
453  m_iLowerY += iAmountY;
454  m_iLowerZ += iAmountZ;
455 
456  m_iUpperX -= iAmountX;
457  m_iUpperY -= iAmountY;
458  m_iUpperZ -= iAmountZ;
459  }
460 
466  void Region::shrink(const Vector3DInt32& v3dAmount)
467  {
468  shrink(v3dAmount.getX(), v3dAmount.getY(), v3dAmount.getZ());
469  }
470 }