HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Coord.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
29 ///////////////////////////////////////////////////////////////////////////
30 
31 #ifndef OPENVDB_MATH_COORD_HAS_BEEN_INCLUDED
32 #define OPENVDB_MATH_COORD_HAS_BEEN_INCLUDED
33 
34 #include <functional>// for std::hash
35 #include <algorithm> // for std::min(), std::max()
36 #include <array> // for std::array
37 #include <iostream>
38 #include <limits>
39 #include <openvdb/Platform.h>
40 #include "Math.h"
41 #include "Vec3.h"
42 
43 namespace tbb { class split; } // forward declaration
44 
45 
46 namespace openvdb {
48 namespace OPENVDB_VERSION_NAME {
49 namespace math {
50 
51 /// @brief Signed (x, y, z) 32-bit integer coordinates
52 class Coord
53 {
54 public:
55  using Int32 = int32_t;
56  using Index32 = uint32_t;
57  using Vec3i = Vec3<Int32>;
59 
60  using ValueType = Int32;
61  using Limits = std::numeric_limits<ValueType>;
62 
63  Coord(): mVec{{0, 0, 0}} {}
64  explicit Coord(Int32 xyz): mVec{{xyz, xyz, xyz}} {}
65  Coord(Int32 x, Int32 y, Int32 z): mVec{{x, y, z}} {}
66  explicit Coord(const Vec3i& v): mVec{{v[0], v[1], v[2]}} {}
67  explicit Coord(const Vec3I& v): mVec{{Int32(v[0]), Int32(v[1]), Int32(v[2])}} {}
68  explicit Coord(const Int32* v): mVec{{v[0], v[1], v[2]}} {}
69 
70  /// @brief Return the smallest possible coordinate
71  static Coord min() { return Coord(Limits::min()); }
72 
73  /// @brief Return the largest possible coordinate
74  static Coord max() { return Coord(Limits::max()); }
75 
76  /// @brief Return @a xyz rounded to the closest integer coordinates
77  /// (cell centered conversion).
78  template<typename T> static Coord round(const Vec3<T>& xyz)
79  {
80  return Coord(Int32(Round(xyz[0])), Int32(Round(xyz[1])), Int32(Round(xyz[2])));
81  }
82  /// @brief Return the largest integer coordinates that are not greater
83  /// than @a xyz (node centered conversion).
84  template<typename T> static Coord floor(const Vec3<T>& xyz)
85  {
86  return Coord(Int32(Floor(xyz[0])), Int32(Floor(xyz[1])), Int32(Floor(xyz[2])));
87  }
88 
89  /// @brief Return the largest integer coordinates that are not greater
90  /// than @a xyz+1 (node centered conversion).
91  template<typename T> static Coord ceil(const Vec3<T>& xyz)
92  {
93  return Coord(Int32(Ceil(xyz[0])), Int32(Ceil(xyz[1])), Int32(Ceil(xyz[2])));
94  }
95 
96  /// @brief Reset all three coordinates with the specified arguments
98  {
99  mVec[0] = x;
100  mVec[1] = y;
101  mVec[2] = z;
102  return *this;
103  }
104  /// @brief Reset all three coordinates with the same specified argument
105  Coord& reset(Int32 xyz) { return this->reset(xyz, xyz, xyz); }
106 
107  Coord& setX(Int32 x) { mVec[0] = x; return *this; }
108  Coord& setY(Int32 y) { mVec[1] = y; return *this; }
109  Coord& setZ(Int32 z) { mVec[2] = z; return *this; }
110 
111  Coord& offset(Int32 dx, Int32 dy, Int32 dz)
112  {
113  mVec[0] += dx;
114  mVec[1] += dy;
115  mVec[2] += dz;
116  return *this;
117  }
118  Coord& offset(Int32 n) { return this->offset(n, n, n); }
119  Coord offsetBy(Int32 dx, Int32 dy, Int32 dz) const
120  {
121  return Coord(mVec[0] + dx, mVec[1] + dy, mVec[2] + dz);
122  }
123  Coord offsetBy(Int32 n) const { return offsetBy(n, n, n); }
124 
125  Coord& operator+=(const Coord& rhs)
126  {
127  mVec[0] += rhs[0];
128  mVec[1] += rhs[1];
129  mVec[2] += rhs[2];
130  return *this;
131  }
132  Coord& operator-=(const Coord& rhs)
133  {
134  mVec[0] -= rhs[0];
135  mVec[1] -= rhs[1];
136  mVec[2] -= rhs[2];
137  return *this;
138  }
139  Coord operator+(const Coord& rhs) const
140  {
141  return Coord(mVec[0] + rhs[0], mVec[1] + rhs[1], mVec[2] + rhs[2]);
142  }
143  Coord operator-(const Coord& rhs) const
144  {
145  return Coord(mVec[0] - rhs[0], mVec[1] - rhs[1], mVec[2] - rhs[2]);
146  }
147  Coord operator-() const { return Coord(-mVec[0], -mVec[1], -mVec[2]); }
148 
149  Coord operator>> (size_t n) const { return Coord(mVec[0]>>n, mVec[1]>>n, mVec[2]>>n); }
150  Coord operator<< (size_t n) const { return Coord(mVec[0]<<n, mVec[1]<<n, mVec[2]<<n); }
151  Coord& operator<<=(size_t n) { mVec[0]<<=n; mVec[1]<<=n; mVec[2]<<=n; return *this; }
152  Coord& operator>>=(size_t n) { mVec[0]>>=n; mVec[1]>>=n; mVec[2]>>=n; return *this; }
153  Coord operator& (Int32 n) const { return Coord(mVec[0] & n, mVec[1] & n, mVec[2] & n); }
154  Coord operator| (Int32 n) const { return Coord(mVec[0] | n, mVec[1] | n, mVec[2] | n); }
155  Coord& operator&= (Int32 n) { mVec[0]&=n; mVec[1]&=n; mVec[2]&=n; return *this; }
156  Coord& operator|= (Int32 n) { mVec[0]|=n; mVec[1]|=n; mVec[2]|=n; return *this; }
157 
158  Int32 x() const { return mVec[0]; }
159  Int32 y() const { return mVec[1]; }
160  Int32 z() const { return mVec[2]; }
161  Int32 operator[](size_t i) const { assert(i < 3); return mVec[i]; }
162  Int32& x() { return mVec[0]; }
163  Int32& y() { return mVec[1]; }
164  Int32& z() { return mVec[2]; }
165  Int32& operator[](size_t i) { assert(i < 3); return mVec[i]; }
166 
167  const Int32* data() const { return mVec.data(); }
168  Int32* data() { return mVec.data(); }
169  const Int32* asPointer() const { return mVec.data(); }
170  Int32* asPointer() { return mVec.data(); }
171  Vec3d asVec3d() const { return Vec3d(double(mVec[0]), double(mVec[1]), double(mVec[2])); }
172  Vec3s asVec3s() const { return Vec3s(float(mVec[0]), float(mVec[1]), float(mVec[2])); }
173  Vec3i asVec3i() const { return Vec3i(mVec.data()); }
174  Vec3I asVec3I() const { return Vec3I(Index32(mVec[0]), Index32(mVec[1]), Index32(mVec[2])); }
175  void asXYZ(Int32& x, Int32& y, Int32& z) const { x = mVec[0]; y = mVec[1]; z = mVec[2]; }
176 
177  bool operator==(const Coord& rhs) const
178  {
179  return (mVec[0] == rhs.mVec[0] && mVec[1] == rhs.mVec[1] && mVec[2] == rhs.mVec[2]);
180  }
181  bool operator!=(const Coord& rhs) const { return !(*this == rhs); }
182 
183  /// Lexicographic less than
184  bool operator<(const Coord& rhs) const
185  {
186  return this->x() < rhs.x() ? true : this->x() > rhs.x() ? false
187  : this->y() < rhs.y() ? true : this->y() > rhs.y() ? false
188  : this->z() < rhs.z() ? true : false;
189  }
190  /// Lexicographic less than or equal to
191  bool operator<=(const Coord& rhs) const
192  {
193  return this->x() < rhs.x() ? true : this->x() > rhs.x() ? false
194  : this->y() < rhs.y() ? true : this->y() > rhs.y() ? false
195  : this->z() <=rhs.z() ? true : false;
196  }
197  /// Lexicographic greater than
198  bool operator>(const Coord& rhs) const { return !(*this <= rhs); }
199  /// Lexicographic greater than or equal to
200  bool operator>=(const Coord& rhs) const { return !(*this < rhs); }
201 
202  /// Perform a component-wise minimum with the other Coord.
203  void minComponent(const Coord& other)
204  {
205  mVec[0] = std::min(mVec[0], other.mVec[0]);
206  mVec[1] = std::min(mVec[1], other.mVec[1]);
207  mVec[2] = std::min(mVec[2], other.mVec[2]);
208  }
209 
210  /// Perform a component-wise maximum with the other Coord.
211  void maxComponent(const Coord& other)
212  {
213  mVec[0] = std::max(mVec[0], other.mVec[0]);
214  mVec[1] = std::max(mVec[1], other.mVec[1]);
215  mVec[2] = std::max(mVec[2], other.mVec[2]);
216  }
217 
218  /// Return the component-wise minimum of the two Coords.
219  static inline Coord minComponent(const Coord& lhs, const Coord& rhs)
220  {
221  return Coord(std::min(lhs.x(), rhs.x()),
222  std::min(lhs.y(), rhs.y()),
223  std::min(lhs.z(), rhs.z()));
224  }
225 
226  /// Return the component-wise maximum of the two Coords.
227  static inline Coord maxComponent(const Coord& lhs, const Coord& rhs)
228  {
229  return Coord(std::max(lhs.x(), rhs.x()),
230  std::max(lhs.y(), rhs.y()),
231  std::max(lhs.z(), rhs.z()));
232  }
233 
234  /// Return true if any of the components of @a a are smaller than the
235  /// corresponding components of @a b.
236  static inline bool lessThan(const Coord& a, const Coord& b)
237  {
238  return (a[0] < b[0] || a[1] < b[1] || a[2] < b[2]);
239  }
240 
241  /// @brief Return the index (0, 1 or 2) with the smallest value.
242  size_t minIndex() const { return MinIndex(mVec); }
243 
244  /// @brief Return the index (0, 1 or 2) with the largest value.
245  size_t maxIndex() const { return MaxIndex(mVec); }
246 
247  void read(std::istream& is) { is.read(reinterpret_cast<char*>(mVec.data()), sizeof(mVec)); }
248  void write(std::ostream& os) const
249  {
250  os.write(reinterpret_cast<const char*>(mVec.data()), sizeof(mVec));
251  }
252 
253  /// @brief Return a hash value for this coordinate
254  /// @note Log2N is the binary logarithm of the hash table size.
255  /// @details The hash function is taken from the SIGGRAPH paper:
256  /// "VDB: High-resolution sparse volumes with dynamic topology"
257  template<int Log2N = 20>
258  size_t hash() const
259  {
260  return ((1<<Log2N)-1) & (mVec[0]*73856093 ^ mVec[1]*19349663 ^ mVec[2]*83492791);
261  }
262 
263 private:
264  std::array<Int32, 3> mVec;
265 }; // class Coord
266 
267 
268 ////////////////////////////////////////
269 
270 
271 /// @brief Axis-aligned bounding box of signed integer coordinates
272 /// @note The range of the integer coordinates, [min, max], is inclusive.
273 /// Thus, a bounding box with min = max is not empty but rather encloses
274 /// a single coordinate.
276 {
277 public:
278  using Index64 = uint64_t;
280 
281  /// @brief Iterator over the Coord domain covered by a CoordBBox
282  /// @note If ZYXOrder is @c true, @e z is the fastest-moving coordinate,
283  /// otherwise the traversal is in XYZ order (i.e., @e x is fastest-moving).
284  template<bool ZYXOrder>
285  class Iterator
286  {
287  public:
288  /// @brief C-tor from a bounding box
289  Iterator(const CoordBBox& b): mPos(b.min()), mMin(b.min()), mMax(b.max()) {}
290  /// @brief Increment the iterator to point to the next coordinate.
291  /// @details Iteration stops one past the maximum coordinate
292  /// along the axis determined by the template parameter.
293  Iterator& operator++() { ZYXOrder ? next<2,1,0>() : next<0,1,2>(); return *this; }
294  /// @brief Return @c true if the iterator still points to a valid coordinate.
295  operator bool() const { return ZYXOrder ? (mPos[0] <= mMax[0]) : (mPos[2] <= mMax[2]); }
296  /// @brief Return a const reference to the coordinate currently pointed to.
297  const Coord& operator*() const { return mPos; }
298  /// Return @c true if this iterator and the given iterator point to the same coordinate.
299  bool operator==(const Iterator& other) const
300  {
301  return ((mPos == other.mPos) && (mMin == other.mMin) && (mMax == other.mMax));
302  }
303  /// Return @c true if this iterator and the given iterator point to different coordinates.
304  bool operator!=(const Iterator& other) const { return !(*this == other); }
305  private:
306  template<size_t a, size_t b, size_t c>
307  void next()
308  {
309  if (mPos[a] < mMax[a]) { ++mPos[a]; } // this is the most common case
310  else if (mPos[b] < mMax[b]) { mPos[a] = mMin[a]; ++mPos[b]; }
311  else if (mPos[c] <= mMax[c]) { mPos[a] = mMin[a]; mPos[b] = mMin[b]; ++mPos[c]; }
312  }
313  Coord mPos, mMin, mMax;
314  friend class CoordBBox; // for CoordBBox::end()
315  };// CoordBBox::Iterator
316 
317  using ZYXIterator = Iterator</*ZYX=*/true>;
318  using XYZIterator = Iterator</*ZYX=*/false>;
319 
320  /// @brief The default constructor produces an empty bounding box.
321  CoordBBox(): mMin(Coord::max()), mMax(Coord::min()) {}
322  /// @brief Construct a bounding box with the given @a min and @a max bounds.
323  CoordBBox(const Coord& min, const Coord& max): mMin(min), mMax(max) {}
324  /// @brief Construct from individual components of the min and max bounds.
326  ValueType xMax, ValueType yMax, ValueType zMax)
327  : mMin(xMin, yMin, zMin), mMax(xMax, yMax, zMax)
328  {
329  }
330  /// @brief Splitting constructor for use in TBB ranges
331  /// @note The other bounding box is assumed to be divisible.
332  CoordBBox(CoordBBox& other, const tbb::split&): mMin(other.mMin), mMax(other.mMax)
333  {
334  assert(this->is_divisible());
335  const size_t n = this->maxExtent();
336  mMax[n] = (mMin[n] + mMax[n]) >> 1;
337  other.mMin[n] = mMax[n] + 1;
338  }
339 
341  {
342  return CoordBBox(min, min.offsetBy(dim - 1));
343  }
344 
345  /// Return an "infinite" bounding box, as defined by the Coord value range.
346  static CoordBBox inf() { return CoordBBox(Coord::min(), Coord::max()); }
347 
348  const Coord& min() const { return mMin; }
349  const Coord& max() const { return mMax; }
350 
351  Coord& min() { return mMin; }
352  Coord& max() { return mMax; }
353 
354  void reset() { mMin = Coord::max(); mMax = Coord::min(); }
355  void reset(const Coord& min, const Coord& max) { mMin = min; mMax = max; }
356  void resetToCube(const Coord& min, ValueType dim) { mMin = min; mMax = min.offsetBy(dim - 1); }
357 
358  /// @brief Return the minimum coordinate.
359  /// @note The start coordinate is inclusive.
360  Coord getStart() const { return mMin; }
361  /// @brief Return the maximum coordinate plus one.
362  /// @note This end coordinate is exclusive.
363  Coord getEnd() const { return mMax.offsetBy(1); }
364 
365  /// @brief Return a ZYX-order iterator that points to the minimum coordinate.
366  ZYXIterator begin() const { return ZYXIterator{*this}; }
367  /// @brief Return a ZYX-order iterator that points to the minimum coordinate.
368  ZYXIterator beginZYX() const { return ZYXIterator{*this}; }
369  /// @brief Return an XYZ-order iterator that points to the minimum coordinate.
370  XYZIterator beginXYZ() const { return XYZIterator{*this}; }
371 
372  /// @brief Return a ZYX-order iterator that points past the maximum coordinate.
373  ZYXIterator end() const { ZYXIterator it{*this}; it.mPos[0] = mMax[0] + 1; return it; }
374  /// @brief Return a ZYX-order iterator that points past the maximum coordinate.
375  ZYXIterator endZYX() const { return end(); }
376  /// @brief Return an XYZ-order iterator that points past the maximum coordinate.
377  XYZIterator endXYZ() const { XYZIterator it{*this}; it.mPos[2] = mMax[2] + 1; return it; }
378 
379  bool operator==(const CoordBBox& rhs) const { return mMin == rhs.mMin && mMax == rhs.mMax; }
380  bool operator!=(const CoordBBox& rhs) const { return !(*this == rhs); }
381 
382  /// @brief Return @c true if this bounding box is empty (i.e., encloses no coordinates).
383  bool empty() const
384  {
385 #if defined(__GNUC__) && !defined(__INTEL_COMPILER)
386  #pragma GCC diagnostic push
387  #pragma GCC diagnostic ignored "-Wstrict-overflow"
388 #endif
389  return (mMin[0] > mMax[0] || mMin[1] > mMax[1] || mMin[2] > mMax[2]);
390 #if defined(__GNUC__) && !defined(__INTEL_COMPILER)
391  #pragma GCC diagnostic pop
392 #endif
393  }
394  /// @brief Return @c true if this bounding box is nonempty
395  /// (i.e., encloses at least one coordinate).
396  operator bool() const { return !this->empty(); }
397  /// @brief Return @c true if this bounding box is nonempty
398  /// (i.e., encloses at least one coordinate).
399  bool hasVolume() const { return !this->empty(); }
400 
401  /// @brief Return the floating-point position of the center of this bounding box.
402  Vec3d getCenter() const { return 0.5 * Vec3d((mMin + mMax).asPointer()); }
403 
404  /// @brief Return the dimensions of the coordinates spanned by this bounding box.
405  /// @note Since coordinates are inclusive, a bounding box with min = max
406  /// has dimensions of (1, 1, 1).
407  Coord dim() const { return empty() ? Coord(0) : (mMax.offsetBy(1) - mMin); }
408  /// @todo deprecate - use dim instead
409  Coord extents() const { return this->dim(); }
410  /// @brief Return the integer volume of coordinates spanned by this bounding box.
411  /// @note Since coordinates are inclusive, a bounding box with min = max has volume one.
412  Index64 volume() const
413  {
414  const Coord d = this->dim();
415  return Index64(d[0]) * Index64(d[1]) * Index64(d[2]);
416  }
417  /// @brief Return @c true if this bounding box can be subdivided [mainly for use by TBB].
418  bool is_divisible() const { return mMin[0]<mMax[0] && mMin[1]<mMax[1] && mMin[2]<mMax[2]; }
419 
420  /// @brief Return the index (0, 1 or 2) of the shortest axis.
421  size_t minExtent() const { return this->dim().minIndex(); }
422 
423  /// @brief Return the index (0, 1 or 2) of the longest axis.
424  size_t maxExtent() const { return this->dim().maxIndex(); }
425 
426  /// @brief Return @c true if point (x, y, z) is inside this bounding box.
427  bool isInside(const Coord& xyz) const
428  {
429  return !(Coord::lessThan(xyz,mMin) || Coord::lessThan(mMax,xyz));
430  }
431 
432  /// @brief Return @c true if the given bounding box is inside this bounding box.
433  bool isInside(const CoordBBox& b) const
434  {
435  return !(Coord::lessThan(b.mMin,mMin) || Coord::lessThan(mMax,b.mMax));
436  }
437 
438  /// @brief Return @c true if the given bounding box overlaps with this bounding box.
439  bool hasOverlap(const CoordBBox& b) const
440  {
441  return !(Coord::lessThan(mMax,b.mMin) || Coord::lessThan(b.mMax,mMin));
442  }
443 
444  /// @brief Pad this bounding box with the specified padding.
445  void expand(ValueType padding)
446  {
447  mMin.offset(-padding);
448  mMax.offset( padding);
449  }
450 
451  /// @brief Return a new instance that is expanded by the specified padding.
452  CoordBBox expandBy(ValueType padding) const
453  {
454  return CoordBBox(mMin.offsetBy(-padding),mMax.offsetBy(padding));
455  }
456 
457  /// @brief Expand this bounding box to enclose point (x, y, z).
458  void expand(const Coord& xyz)
459  {
460  mMin.minComponent(xyz);
461  mMax.maxComponent(xyz);
462  }
463 
464  /// @brief Union this bounding box with the given bounding box.
465  void expand(const CoordBBox& bbox)
466  {
467  mMin.minComponent(bbox.min());
468  mMax.maxComponent(bbox.max());
469  }
470  /// @brief Intersect this bounding box with the given bounding box.
471  void intersect(const CoordBBox& bbox)
472  {
473  mMin.maxComponent(bbox.min());
474  mMax.minComponent(bbox.max());
475  }
476  /// @brief Union this bounding box with the cubical bounding box
477  /// of the given size and with the given minimum coordinates.
479  {
480  mMin.minComponent(min);
481  mMax.maxComponent(min.offsetBy(dim-1));
482  }
483  /// @brief Translate this bounding box by
484  /// (<i>t<sub>x</sub></i>, <i>t<sub>y</sub></i>, <i>t<sub>z</sub></i>).
485  void translate(const Coord& t) { mMin += t; mMax += t; }
486 
487  /// @brief Move this bounding box to the specified min
488  void moveMin(const Coord& min) { mMax += min - mMin; mMin = min; }
489 
490  /// @brief Move this bounding box to the specified max
491  void moveMax(const Coord& max) { mMin += max - mMax; mMax = max; }
492 
493  /// @brief Populates an array with the eight corner points of this bounding box.
494  /// @details The ordering of the corner points is lexicographic.
495  /// @warning It is assumed that the pointer can be incremented at
496  /// least seven times, i.e. has storage for eight Coord elements!
497  void getCornerPoints(Coord *p) const
498  {
499  assert(p != nullptr);
500  p->reset(mMin.x(), mMin.y(), mMin.z()); ++p;
501  p->reset(mMin.x(), mMin.y(), mMax.z()); ++p;
502  p->reset(mMin.x(), mMax.y(), mMin.z()); ++p;
503  p->reset(mMin.x(), mMax.y(), mMax.z()); ++p;
504  p->reset(mMax.x(), mMin.y(), mMin.z()); ++p;
505  p->reset(mMax.x(), mMin.y(), mMax.z()); ++p;
506  p->reset(mMax.x(), mMax.y(), mMin.z()); ++p;
507  p->reset(mMax.x(), mMax.y(), mMax.z());
508  }
509 
510  //@{
511  /// @brief Bit-wise operations performed on both the min and max members
512  CoordBBox operator>> (size_t n) const { return CoordBBox(mMin>>n, mMax>>n); }
513  CoordBBox operator<< (size_t n) const { return CoordBBox(mMin<<n, mMax<<n); }
514  CoordBBox& operator<<=(size_t n) { mMin <<= n; mMax <<= n; return *this; }
515  CoordBBox& operator>>=(size_t n) { mMin >>= n; mMax >>= n; return *this; }
516  CoordBBox operator& (Coord::Int32 n) const { return CoordBBox(mMin & n, mMax & n); }
517  CoordBBox operator| (Coord::Int32 n) const { return CoordBBox(mMin | n, mMax | n); }
518  CoordBBox& operator&= (Coord::Int32 n) { mMin &= n; mMax &= n; return *this; }
519  CoordBBox& operator|= (Coord::Int32 n) { mMin |= n; mMax |= n; return *this; }
520  //@}
521 
522  /// @brief Unserialize this bounding box from the given stream.
523  void read(std::istream& is) { mMin.read(is); mMax.read(is); }
524  /// @brief Serialize this bounding box to the given stream.
525  void write(std::ostream& os) const { mMin.write(os); mMax.write(os); }
526 
527 private:
528  Coord mMin, mMax;
529 }; // class CoordBBox
530 
531 
532 ////////////////////////////////////////
533 
534 
535 inline std::ostream& operator<<(std::ostream& os, const Coord& xyz)
536 {
537  os << xyz.asVec3i(); return os;
538 }
539 
540 
541 inline Coord
542 Abs(const Coord& xyz)
543 {
544  return Coord(Abs(xyz[0]), Abs(xyz[1]), Abs(xyz[2]));
545 }
546 
547 
548 //@{
549 /// Allow a Coord to be added to or subtracted from a Vec3.
550 template<typename T>
552 operator+(const Vec3<T>& v0, const Coord& v1)
553 {
555  result[0] += v1[0];
556  result[1] += v1[1];
557  result[2] += v1[2];
558  return result;
559 }
560 
561 template<typename T>
563 operator+(const Coord& v1, const Vec3<T>& v0)
564 {
566  result[0] += v1[0];
567  result[1] += v1[1];
568  result[2] += v1[2];
569  return result;
570 }
571 //@}
572 
573 
574 //@{
575 /// Allow a Coord to be subtracted from a Vec3.
576 template <typename T>
578 operator-(const Vec3<T>& v0, const Coord& v1)
579 {
581  result[0] -= v1[0];
582  result[1] -= v1[1];
583  result[2] -= v1[2];
584  return result;
585 }
586 
587 template <typename T>
589 operator-(const Coord& v1, const Vec3<T>& v0)
590 {
592  result[0] -= v1[0];
593  result[1] -= v1[1];
594  result[2] -= v1[2];
595  return -result;
596 }
597 //@}
598 
599 inline std::ostream&
600 operator<<(std::ostream& os, const CoordBBox& b)
601 {
602  os << b.min() << " -> " << b.max();
603  return os;
604 }
605 
606 } // namespace math
607 } // namespace OPENVDB_VERSION_NAME
608 } // namespace openvdb
609 
610 ////////////////////////////////////////
611 
612 // template specialization of std::hash with Coord, which
613 // allows for Coord to be used as the key in std::unordered_map
614 namespace std {// injected in namespace std
615 
616 template<>
617 struct hash<openvdb::math::Coord>
618 {
619  using Coord = openvdb::math::Coord;
621  using result_type = std::size_t;
622  std::size_t operator()(const Coord& ijk) const noexcept { return ijk.Coord::hash<>(); }
623 };// std::hash<openvdb::math::Coord>
624 
625 }// namespace std
626 
627 #endif // OPENVDB_MATH_COORD_HAS_BEEN_INCLUDED
628 
629 // Copyright (c) DreamWorks Animation LLC
630 // All rights reserved. This software is distributed under the
631 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
Vec2< T > minComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec2.h:530
void expand(const CoordBBox &bbox)
Union this bounding box with the given bounding box.
Definition: Coord.h:465
vint4 max(const vint4 &a, const vint4 &b)
Definition: simd.h:4703
CoordBBox & operator<<=(size_t n)
Bit-wise operations performed on both the min and max members.
Definition: Coord.h:514
int Ceil(float x)
Return the ceiling of x.
Definition: Math.h:830
bool operator==(const Iterator &other) const
Return true if this iterator and the given iterator point to the same coordinate. ...
Definition: Coord.h:299
Coord getEnd() const
Return the maximum coordinate plus one.
Definition: Coord.h:363
OIIO_FORCEINLINE const vint4 & operator>>=(vint4 &a, const unsigned int bits)
Definition: simd.h:4401
Vec3d getCenter() const
Return the floating-point position of the center of this bounding box.
Definition: Coord.h:402
void expand(const Coord &xyz)
Expand this bounding box to enclose point (x, y, z).
Definition: Coord.h:458
void translate(const Coord &t)
Translate this bounding box by (tx, ty, tz).
Definition: Coord.h:485
static CoordBBox createCube(const Coord &min, ValueType dim)
Definition: Coord.h:340
size_t MaxIndex(const Vec3T &v)
Return the index [0,1,2] of the largest value in a 3D vector.
Definition: Math.h:931
openvdb::math::Coord Coord
Definition: Coord.h:619
HALF_EXPORT std::istream & operator>>(std::istream &is, half &h)
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:9477
Coord dim() const
Return the dimensions of the coordinates spanned by this bounding box.
Definition: Coord.h:407
bool hasVolume() const
Return true if this bounding box is nonempty (i.e., encloses at least one coordinate).
Definition: Coord.h:399
Vec3< typename promote< T, Coord::ValueType >::type > operator-(const Vec3< T > &v0, const Coord &v1)
Allow a Coord to be subtracted from a Vec3.
Definition: Coord.h:578
void intersect(const CoordBBox &bbox)
Intersect this bounding box with the given bounding box.
Definition: Coord.h:471
void getCornerPoints(Coord *p) const
Populates an array with the eight corner points of this bounding box.
Definition: Coord.h:497
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:200
OIIO_FORCEINLINE const vbool4 & operator|=(vbool4 &a, const vbool4 &b)
Definition: simd.h:3232
CoordBBox operator|(Coord::Int32 n) const
Bit-wise operations performed on both the min and max members.
Definition: Coord.h:517
size_t minExtent() const
Return the index (0, 1 or 2) of the shortest axis.
Definition: Coord.h:421
const GLdouble * v
Definition: glew.h:1391
Iterator over the Coord domain covered by a CoordBBox.
Definition: Coord.h:285
bool empty() const
Return true if this bounding box is empty (i.e., encloses no coordinates).
Definition: Coord.h:383
GLboolean reset
Definition: glew.h:4959
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:52
void read(T &in, bool &v)
Definition: ImfXdr.h:611
std::numeric_limits< ValueType > Limits
Definition: Coord.h:61
OIIO_FORCEINLINE vbool4 operator>=(const vint4 &a, const vint4 &b)
Definition: simd.h:4444
void moveMax(const Coord &max)
Move this bounding box to the specified max.
Definition: Coord.h:491
void reset(const Coord &min, const Coord &max)
Definition: Coord.h:355
void resetToCube(const Coord &min, ValueType dim)
Definition: Coord.h:356
CoordBBox expandBy(ValueType padding) const
Return a new instance that is expanded by the specified padding.
Definition: Coord.h:452
GLdouble GLdouble z
Definition: glew.h:1559
CoordBBox()
The default constructor produces an empty bounding box.
Definition: Coord.h:321
bool operator!=(const Iterator &other) const
Return true if this iterator and the given iterator point to different coordinates.
Definition: Coord.h:304
void expand(const Coord &min, Coord::ValueType dim)
Union this bounding box with the cubical bounding box of the given size and with the given minimum co...
Definition: Coord.h:478
static CoordBBox inf()
Return an "infinite" bounding box, as defined by the Coord value range.
Definition: Coord.h:346
ZYXIterator begin() const
Return a ZYX-order iterator that points to the minimum coordinate.
Definition: Coord.h:366
OIIO_FORCEINLINE vbool4 operator<=(const vint4 &a, const vint4 &b)
Definition: simd.h:4448
void OIIO_API split(string_view str, std::vector< string_view > &result, string_view sep=string_view(), int maxsplit=-1)
size_t MinIndex(const Vec3T &v)
Return the index [0,1,2] of the smallest value in a 3D vector.
Definition: Math.h:910
bool operator==(const CoordBBox &rhs) const
Definition: Coord.h:379
bool hasOverlap(const CoordBBox &b) const
Return true if the given bounding box overlaps with this bounding box.
Definition: Coord.h:439
GLint GLint GLint GLint GLint x
Definition: glew.h:1252
CoordBBox operator&(Coord::Int32 n) const
Bit-wise operations performed on both the min and max members.
Definition: Coord.h:516
Coord Abs(const Coord &xyz)
Definition: Coord.h:542
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1252
OIIO_FORCEINLINE const vint4 & operator+=(vint4 &a, const vint4 &b)
Definition: simd.h:4246
CoordBBox & operator&=(Coord::Int32 n)
Bit-wise operations performed on both the min and max members.
Definition: Coord.h:518
Index64 volume() const
Return the integer volume of coordinates spanned by this bounding box.
Definition: Coord.h:412
float Round(float x)
Return x rounded to the nearest integer.
Definition: Math.h:793
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
CoordBBox(const Coord &min, const Coord &max)
Construct a bounding box with the given min and max bounds.
Definition: Coord.h:323
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
void read(std::istream &is)
Unserialize this bounding box from the given stream.
Definition: Coord.h:523
Iterator(const CoordBBox &b)
C-tor from a bounding box.
Definition: Coord.h:289
void write(std::ostream &os) const
Serialize this bounding box to the given stream.
Definition: Coord.h:525
GLsizei n
Definition: glew.h:4040
const GLfloat * c
Definition: glew.h:16296
Axis-aligned bounding box of signed integer coordinates.
Definition: Coord.h:275
CoordBBox operator<<(size_t n) const
Bit-wise operations performed on both the min and max members.
Definition: Coord.h:513
bool is_divisible() const
Return true if this bounding box can be subdivided [mainly for use by TBB].
Definition: Coord.h:418
bool isInside(const Coord &xyz) const
Return true if point (x, y, z) is inside this bounding box.
Definition: Coord.h:427
size_t maxExtent() const
Return the index (0, 1 or 2) of the longest axis.
Definition: Coord.h:424
vfloat4 round(const vfloat4 &a)
Definition: simd.h:7186
bool operator>(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:219
Coord getStart() const
Return the minimum coordinate.
Definition: Coord.h:360
void expand(ValueType padding)
Pad this bounding box with the specified padding.
Definition: Coord.h:445
int floor(T x)
Definition: ImathFun.h:150
Iterator & operator++()
Increment the iterator to point to the next coordinate.
Definition: Coord.h:293
std::size_t operator()(const Coord &ijk) const noexcept
Definition: Coord.h:622
CoordBBox & operator>>=(size_t n)
Bit-wise operations performed on both the min and max members.
Definition: Coord.h:515
ZYXIterator end() const
Return a ZYX-order iterator that points past the maximum coordinate.
Definition: Coord.h:373
GLdouble GLdouble GLdouble b
Definition: glew.h:9122
bool operator!=(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Inequality operator, does exact floating point comparisons.
Definition: Vec3.h:506
GLfloat GLfloat p
Definition: glew.h:16321
CoordBBox operator>>(size_t n) const
Bit-wise operations performed on both the min and max members.
Definition: Coord.h:512
XYZIterator beginXYZ() const
Return an XYZ-order iterator that points to the minimum coordinate.
Definition: Coord.h:370
OIIO_FORCEINLINE const vint4 & operator<<=(vint4 &a, const unsigned int bits)
Definition: simd.h:4388
XYZIterator endXYZ() const
Return an XYZ-order iterator that points past the maximum coordinate.
Definition: Coord.h:377
void moveMin(const Coord &min)
Move this bounding box to the specified min.
Definition: Coord.h:488
ZYXIterator beginZYX() const
Return a ZYX-order iterator that points to the minimum coordinate.
Definition: Coord.h:368
bool operator!=(const CoordBBox &rhs) const
Definition: Coord.h:380
Vec2< T > maxComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise maximum of the two vectors.
Definition: Vec2.h:539
GLfloat v0
Definition: glew.h:1848
OIIO_FORCEINLINE const vint4 & operator-=(vint4 &a, const vint4 &b)
Definition: simd.h:4269
GLuint64EXT * result
Definition: glew.h:14007
Vec3< typename promote< T, typename Coord::ValueType >::type > operator+(const Vec3< T > &v0, const Coord &v1)
Allow a Coord to be added to or subtracted from a Vec3.
Definition: Coord.h:552
OIIO_FORCEINLINE const vbool4 & operator&=(vbool4 &a, const vbool4 &b)
Definition: simd.h:3228
int Floor(float x)
Return the floor of x.
Definition: Math.h:822
CoordBBox(ValueType xMin, ValueType yMin, ValueType zMin, ValueType xMax, ValueType yMax, ValueType zMax)
Construct from individual components of the min and max bounds.
Definition: Coord.h:325
int ceil(T x)
Definition: ImathFun.h:158
vint4 min(const vint4 &a, const vint4 &b)
Definition: simd.h:4694
bool isInside(const CoordBBox &b) const
Return true if the given bounding box is inside this bounding box.
Definition: Coord.h:433
void write(T &out, bool v)
Definition: ImfXdr.h:332
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:146
ZYXIterator endZYX() const
Return a ZYX-order iterator that points past the maximum coordinate.
Definition: Coord.h:375
GLdouble GLdouble t
Definition: glew.h:1398
GLfloat GLfloat v1
Definition: glew.h:1852
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:498
CoordBBox & operator|=(Coord::Int32 n)
Bit-wise operations performed on both the min and max members.
Definition: Coord.h:519
CoordBBox(CoordBBox &other, const tbb::split &)
Splitting constructor for use in TBB ranges.
Definition: Coord.h:332
const Coord & operator*() const
Return a const reference to the coordinate currently pointed to.
Definition: Coord.h:297
std::ostream & operator<<(std::ostream &os, const BBox< Vec3T > &b)
Definition: BBox.h:440
GLintptr offset
Definition: glew.h:1682
bool operator<(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:207