PolyVox  0.2.1
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 {
30  const Region Region::MaxRegion
31  (
32  Vector3DInt32((std::numeric_limits<int32_t>::min)(), (std::numeric_limits<int32_t>::min)(), (std::numeric_limits<int32_t>::min)()),
33  Vector3DInt32((std::numeric_limits<int32_t>::max)(), (std::numeric_limits<int32_t>::max)(), (std::numeric_limits<int32_t>::max)())
34  );
35 
36 
38  :m_v3dLowerCorner(0,0,0)
39  ,m_v3dUpperCorner(0,0,0)
40  {
41  }
42 
43  Region::Region(const Vector3DInt32& v3dLowerCorner, const Vector3DInt32& v3dUpperCorner)
44  :m_v3dLowerCorner(v3dLowerCorner)
45  ,m_v3dUpperCorner(v3dUpperCorner)
46  {
47  //Check the region is valid.
48  assert(m_v3dUpperCorner.getX() >= m_v3dLowerCorner.getX());
49  assert(m_v3dUpperCorner.getY() >= m_v3dLowerCorner.getY());
50  assert(m_v3dUpperCorner.getZ() >= m_v3dLowerCorner.getZ());
51  }
52 
53  Region::Region(int32_t iLowerX, int32_t iLowerY, int32_t iLowerZ, int32_t iUpperX, int32_t iUpperY, int32_t iUpperZ)
54  :m_v3dLowerCorner(iLowerX, iLowerY, iLowerZ)
55  ,m_v3dUpperCorner(iUpperX, iUpperY, iUpperZ)
56  {
57  //Check the region is valid.
58  assert(m_v3dUpperCorner.getX() >= m_v3dLowerCorner.getX());
59  assert(m_v3dUpperCorner.getY() >= m_v3dLowerCorner.getY());
60  assert(m_v3dUpperCorner.getZ() >= m_v3dLowerCorner.getZ());
61  }
62 
69  bool Region::operator==(const Region& rhs) const
70  {
71  return ((m_v3dLowerCorner == rhs.m_v3dLowerCorner) && (m_v3dUpperCorner == rhs.m_v3dUpperCorner));
72  }
73 
80  bool Region::operator!=(const Region& rhs) const
81  {
82  return !(*this == rhs);
83  }
84 
86  {
87  return m_v3dLowerCorner;
88  }
89 
91  {
92  return m_v3dUpperCorner;
93  }
94 
96  {
97  return getWidthInCells() + 1;
98  }
99 
101  {
102  return getHeightInCells() + 1;
103  }
104 
106  {
107  return getDepthInCells() + 1;
108  }
109 
111  {
112  return getDimensionsInCells() + Vector3DInt32(1, 1, 1);
113  }
114 
116  {
117  return m_v3dUpperCorner.getX() - m_v3dLowerCorner.getX();
118  }
119 
121  {
122  return m_v3dUpperCorner.getY() - m_v3dLowerCorner.getY();
123  }
124 
126  {
127  return m_v3dUpperCorner - m_v3dLowerCorner;
128  }
129 
131  {
132  return m_v3dUpperCorner.getZ() - m_v3dLowerCorner.getZ();
133  }
134 
135  void Region::setLowerCorner(const Vector3DInt32& v3dLowerCorner)
136  {
137  m_v3dLowerCorner = v3dLowerCorner;
138  }
139 
140  void Region::setUpperCorner(const Vector3DInt32& v3dUpperCorner)
141  {
142  m_v3dUpperCorner = v3dUpperCorner;
143  }
144 
145  bool Region::containsPoint(const Vector3DFloat& pos, float boundary) const
146  {
147  return (pos.getX() <= m_v3dUpperCorner.getX() - boundary)
148  && (pos.getY() <= m_v3dUpperCorner.getY() - boundary)
149  && (pos.getZ() <= m_v3dUpperCorner.getZ() - boundary)
150  && (pos.getX() >= m_v3dLowerCorner.getX() + boundary)
151  && (pos.getY() >= m_v3dLowerCorner.getY() + boundary)
152  && (pos.getZ() >= m_v3dLowerCorner.getZ() + boundary);
153  }
154 
155  bool Region::containsPoint(const Vector3DInt32& pos, uint8_t boundary) const
156  {
157  return (pos.getX() <= m_v3dUpperCorner.getX() - boundary)
158  && (pos.getY() <= m_v3dUpperCorner.getY() - boundary)
159  && (pos.getZ() <= m_v3dUpperCorner.getZ() - boundary)
160  && (pos.getX() >= m_v3dLowerCorner.getX() + boundary)
161  && (pos.getY() >= m_v3dLowerCorner.getY() + boundary)
162  && (pos.getZ() >= m_v3dLowerCorner.getZ() + boundary);
163  }
164 
165  bool Region::containsPointInX(float pos, float boundary) const
166  {
167  return (pos <= m_v3dUpperCorner.getX() - boundary)
168  && (pos >= m_v3dLowerCorner.getX() + boundary);
169  }
170 
171  bool Region::containsPointInX(int32_t pos, uint8_t boundary) const
172  {
173  return (pos <= m_v3dUpperCorner.getX() - boundary)
174  && (pos >= m_v3dLowerCorner.getX() + boundary);
175  }
176 
177  bool Region::containsPointInY(float pos, float boundary) const
178  {
179  return (pos <= m_v3dUpperCorner.getY() - boundary)
180  && (pos >= m_v3dLowerCorner.getY() + boundary);
181  }
182 
183  bool Region::containsPointInY(int32_t pos, uint8_t boundary) const
184  {
185  return (pos <= m_v3dUpperCorner.getY() - boundary)
186  && (pos >= m_v3dLowerCorner.getY() + boundary);
187  }
188 
189  bool Region::containsPointInZ(float pos, float boundary) const
190  {
191  return (pos <= m_v3dUpperCorner.getZ() - boundary)
192  && (pos >= m_v3dLowerCorner.getZ() + boundary);
193  }
194 
195  bool Region::containsPointInZ(int32_t pos, uint8_t boundary) const
196  {
197  return (pos <= m_v3dUpperCorner.getZ() - boundary)
198  && (pos >= m_v3dLowerCorner.getZ() + boundary);
199  }
200 
201  void Region::cropTo(const Region& other)
202  {
203  m_v3dLowerCorner.setX((std::max)(m_v3dLowerCorner.getX(), other.m_v3dLowerCorner.getX()));
204  m_v3dLowerCorner.setY((std::max)(m_v3dLowerCorner.getY(), other.m_v3dLowerCorner.getY()));
205  m_v3dLowerCorner.setZ((std::max)(m_v3dLowerCorner.getZ(), other.m_v3dLowerCorner.getZ()));
206  m_v3dUpperCorner.setX((std::min)(m_v3dUpperCorner.getX(), other.m_v3dUpperCorner.getX()));
207  m_v3dUpperCorner.setY((std::min)(m_v3dUpperCorner.getY(), other.m_v3dUpperCorner.getY()));
208  m_v3dUpperCorner.setZ((std::min)(m_v3dUpperCorner.getZ(), other.m_v3dUpperCorner.getZ()));
209  }
210 
212  int32_t Region::depth(void) const
213  {
214  //This function is deprecated and wrong.
215  assert(false);
216  return m_v3dUpperCorner.getZ() - m_v3dLowerCorner.getZ();
217  }
218 
220  int32_t Region::height(void) const
221  {
222  //This function is deprecated and wrong.
223  assert(false);
224  return m_v3dUpperCorner.getY() - m_v3dLowerCorner.getY();
225  }
226 
227  void Region::shift(const Vector3DInt32& amount)
228  {
229  m_v3dLowerCorner += amount;
230  m_v3dUpperCorner += amount;
231  }
232 
234  {
235  m_v3dLowerCorner += amount;
236  }
237 
239  {
240  m_v3dUpperCorner += amount;
241  }
242 
244  Vector3DInt32 Region::dimensions(void)
245  {
246  //This function is deprecated and wrong.
247  assert(false);
248  return m_v3dUpperCorner - m_v3dLowerCorner;
249  }
250 
252  int32_t Region::width(void) const
253  {
254  //This function is deprecated and wrong.
255  assert(false);
256  return m_v3dUpperCorner.getX() - m_v3dLowerCorner.getX();
257  }
258 }