4#ifndef OPENVDB_MATH_COORD_HAS_BEEN_INCLUDED
5#define OPENVDB_MATH_COORD_HAS_BEEN_INCLUDED
16#include <tbb/blocked_range.h>
33 using Limits = std::numeric_limits<ValueType>;
38 explicit Coord(
const Vec3i& v): mVec{{v[0], v[1], v[2]}} {}
40 explicit Coord(
const Int32* v): mVec{{v[0], v[1], v[2]}} {}
93 return Coord(mVec[0] + dx, mVec[1] + dy, mVec[2] + dz);
113 return Coord(mVec[0] + rhs[0], mVec[1] + rhs[1], mVec[2] + rhs[2]);
117 return Coord(mVec[0] - rhs[0], mVec[1] - rhs[1], mVec[2] - rhs[2]);
121 Coord operator>> (
size_t n)
const {
return Coord(mVec[0]>>n, mVec[1]>>n, mVec[2]>>n); }
122 Coord operator<< (
size_t n)
const {
return Coord(mVec[0]<<n, mVec[1]<<n, mVec[2]<<n); }
125 Coord operator& (
Int32 n)
const {
return Coord(mVec[0] & n, mVec[1] & n, mVec[2] & n); }
126 Coord operator| (
Int32 n)
const {
return Coord(mVec[0] | n, mVec[1] | n, mVec[2] | n); }
127 Coord& operator&= (
Int32 n) { mVec[0]&=n; mVec[1]&=n; mVec[2]&=n;
return *
this; }
128 Coord& operator|= (
Int32 n) { mVec[0]|=n; mVec[1]|=n; mVec[2]|=n;
return *
this; }
151 return (mVec[0] == rhs.mVec[0] && mVec[1] == rhs.mVec[1] && mVec[2] == rhs.mVec[2]);
158 return this->x() < rhs.
x() ? true : this->x() > rhs.
x() ? false
159 : this->y() < rhs.
y() ? true : this->y() > rhs.
y() ? false
160 : this->z() < rhs.
z() ? true :
false;
165 return this->x() < rhs.
x() ? true : this->x() > rhs.
x() ? false
166 : this->y() < rhs.
y() ? true : this->y() > rhs.
y() ? false
167 : this->z() <=rhs.
z() ? true :
false;
177 mVec[0] = std::min(mVec[0], other.mVec[0]);
178 mVec[1] = std::min(mVec[1], other.mVec[1]);
179 mVec[2] = std::min(mVec[2], other.mVec[2]);
185 mVec[0] = std::max(mVec[0], other.mVec[0]);
186 mVec[1] = std::max(mVec[1], other.mVec[1]);
187 mVec[2] = std::max(mVec[2], other.mVec[2]);
193 return Coord(std::min(lhs.
x(), rhs.
x()),
194 std::min(lhs.
y(), rhs.
y()),
195 std::min(lhs.
z(), rhs.
z()));
201 return Coord(std::max(lhs.
x(), rhs.
x()),
202 std::max(lhs.
y(), rhs.
y()),
203 std::max(lhs.
z(), rhs.
z()));
210 return (a[0] < b[0] || a[1] < b[1] || a[2] < b[2]);
219 void read(std::istream& is) { is.read(
reinterpret_cast<char*
>(mVec.data()),
sizeof(mVec)); }
222 os.write(
reinterpret_cast<const char*
>(mVec.data()),
sizeof(mVec));
229 template<
int Log2N = 20>
232 const uint32_t* vec =
reinterpret_cast<const uint32_t*
>(mVec.data());
233 return ((1<<Log2N)-1) & (vec[0]*73856093 ^ vec[1]*19349663 ^ vec[2]*83492791);
237 std::array<Int32, 3> mVec;
257 template<
bool ZYXOrder>
268 operator bool()
const {
return ZYXOrder ? (mPos[0] <= mMax[0]) : (mPos[2] <= mMax[2]); }
274 return ((mPos == other.mPos) && (mMin == other.mMin) && (mMax == other.mMax));
279 template<
size_t a,
size_t b,
size_t c>
282 if (mPos[a] < mMax[a]) { ++mPos[a]; }
283 else if (mPos[b] < mMax[b]) { mPos[a] = mMin[a]; ++mPos[b]; }
284 else if (mPos[c] <= mMax[c]) { mPos[a] = mMin[a]; mPos[b] = mMin[b]; ++mPos[c]; }
286 Coord mPos, mMin, mMax;
300 : mMin(xMin, yMin, zMin), mMax(xMax, yMax, zMax)
307 assert(this->is_divisible());
308 const size_t n = this->maxExtent();
309 mMax[n] = (mMin[n] + mMax[n]) >> 1;
310 other.mMin[n] = mMax[n] + 1;
315 return CoordBBox(min, min.offsetBy(dim - 1));
327 void reset() { mMin = Coord::max(); mMax = Coord::min(); }
358#if defined(__GNUC__) && !defined(__INTEL_COMPILER)
359 #pragma GCC diagnostic push
360 #pragma GCC diagnostic ignored "-Wstrict-overflow"
362 return (mMin[0] > mMax[0] || mMin[1] > mMax[1] || mMin[2] > mMax[2]);
363#if defined(__GNUC__) && !defined(__INTEL_COMPILER)
364 #pragma GCC diagnostic pop
369 operator bool()
const {
return !this->empty(); }
380 Coord dim()
const {
return empty() ?
Coord(0) : (mMax.offsetBy(1) - mMin); }
387 const Coord d = this->dim();
391 bool is_divisible()
const {
return mMin[0]<mMax[0] && mMin[1]<mMax[1] && mMin[2]<mMax[2]; }
394 size_t minExtent()
const {
return this->dim().minIndex(); }
397 size_t maxExtent()
const {
return this->dim().maxIndex(); }
402 return !(Coord::lessThan(xyz,mMin) || Coord::lessThan(mMax,xyz));
408 return !(Coord::lessThan(b.mMin,mMin) || Coord::lessThan(mMax,b.mMax));
414 return !(Coord::lessThan(mMax,b.mMin) || Coord::lessThan(b.mMax,mMin));
420 mMin.offset(-padding);
421 mMax.offset( padding);
427 return CoordBBox(mMin.offsetBy(-padding),mMax.offsetBy(padding));
433 mMin.minComponent(xyz);
434 mMax.maxComponent(xyz);
440 mMin.minComponent(bbox.
min());
441 mMax.maxComponent(bbox.
max());
446 mMin.maxComponent(bbox.
min());
447 mMax.minComponent(bbox.
max());
453 mMin.minComponent(min);
454 mMax.maxComponent(min.offsetBy(dim-1));
472 assert(p !=
nullptr);
473 p->
reset(mMin.x(), mMin.y(), mMin.z()); ++p;
474 p->
reset(mMin.x(), mMin.y(), mMax.z()); ++p;
475 p->
reset(mMin.x(), mMax.y(), mMin.z()); ++p;
476 p->
reset(mMin.x(), mMax.y(), mMax.z()); ++p;
477 p->
reset(mMax.x(), mMin.y(), mMin.z()); ++p;
478 p->
reset(mMax.x(), mMin.y(), mMax.z()); ++p;
479 p->
reset(mMax.x(), mMax.y(), mMin.z()); ++p;
480 p->
reset(mMax.x(), mMax.y(), mMax.z());
496 void read(std::istream& is) { mMin.read(is); mMax.read(is); }
498 void write(std::ostream& os)
const { mMin.write(os); mMax.write(os); }
510 os << xyz.
asVec3i();
return os;
575 os << b.
min() <<
" -> " << b.
max();
595 std::size_t
operator()(
const Coord& ijk)
const noexcept {
return ijk.Coord::hash<>(); }
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Iterator over the Coord domain covered by a CoordBBox.
Definition: Coord.h:259
bool operator!=(const Iterator &other) const
Return true if this iterator and the given iterator point to different coordinates.
Definition: Coord.h:277
const Coord & operator*() const
Return a const reference to the coordinate currently pointed to.
Definition: Coord.h:270
Iterator & operator++()
Increment the iterator to point to the next coordinate.
Definition: Coord.h:266
bool operator==(const Iterator &other) const
Return true if this iterator and the given iterator point to the same coordinate.
Definition: Coord.h:272
Iterator(const CoordBBox &b)
C-tor from a bounding box.
Definition: Coord.h:262
Axis-aligned bounding box of signed integer coordinates.
Definition: Coord.h:249
bool operator!=(const CoordBBox &rhs) const
Definition: Coord.h:353
CoordBBox()
The default constructor produces an empty bounding box.
Definition: Coord.h:294
bool isInside(const CoordBBox &b) const
Return true if the given bounding box is inside this bounding box.
Definition: Coord.h:406
ZYXIterator beginZYX() const
Return a ZYX-order iterator that points to the minimum coordinate.
Definition: Coord.h:341
size_t minExtent() const
Return the index (0, 1 or 2) of the shortest axis.
Definition: Coord.h:394
void moveMax(const Coord &max)
Move this bounding box to the specified max.
Definition: Coord.h:464
bool is_divisible() const
Return true if this bounding box can be subdivided [mainly for use by TBB].
Definition: Coord.h:391
Coord::ValueType ValueType
Definition: Coord.h:252
Coord extents() const
Definition: Coord.h:382
void write(std::ostream &os) const
Serialize this bounding box to the given stream.
Definition: Coord.h:498
void translate(const Coord &t)
Translate this bounding box by (tx, ty, tz).
Definition: Coord.h:458
bool hasVolume() const
Return true if this bounding box is nonempty (i.e., encloses at least one coordinate).
Definition: Coord.h:372
Vec3d getCenter() const
Return the floating-point position of the center of this bounding box.
Definition: Coord.h:375
ZYXIterator end() const
Return a ZYX-order iterator that points past the maximum coordinate.
Definition: Coord.h:346
XYZIterator beginXYZ() const
Return an XYZ-order iterator that points to the minimum coordinate.
Definition: Coord.h:343
void expand(ValueType padding)
Pad this bounding box with the specified padding.
Definition: Coord.h:418
void expand(const Coord &xyz)
Expand this bounding box to enclose point (x, y, z).
Definition: Coord.h:431
CoordBBox(CoordBBox &other, const tbb::split &)
Splitting constructor for use in TBB ranges.
Definition: Coord.h:305
CoordBBox & operator>>=(size_t n)
Definition: Coord.h:488
ZYXIterator endZYX() const
Return a ZYX-order iterator that points past the maximum coordinate.
Definition: Coord.h:348
bool empty() const
Return true if this bounding box is empty (i.e., encloses no coordinates).
Definition: Coord.h:356
Coord & max()
Definition: Coord.h:325
void moveMin(const Coord &min)
Move this bounding box to the specified min.
Definition: Coord.h:461
const Coord & min() const
Definition: Coord.h:321
bool hasOverlap(const CoordBBox &b) const
Return true if the given bounding box overlaps with this bounding box.
Definition: Coord.h:412
const Coord & max() const
Definition: Coord.h:322
size_t maxExtent() const
Return the index (0, 1 or 2) of the longest axis.
Definition: Coord.h:397
XYZIterator endXYZ() const
Return an XYZ-order iterator that points past the maximum coordinate.
Definition: Coord.h:350
CoordBBox & operator<<=(size_t n)
Definition: Coord.h:487
void resetToCube(const Coord &min, ValueType dim)
Definition: Coord.h:329
bool operator==(const CoordBBox &rhs) const
Definition: Coord.h:352
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:451
CoordBBox expandBy(ValueType padding) const
Return a new instance that is expanded by the specified padding.
Definition: Coord.h:425
Index64 volume() const
Return the integer volume of coordinates spanned by this bounding box.
Definition: Coord.h:385
static CoordBBox inf()
Return an "infinite" bounding box, as defined by the Coord value range.
Definition: Coord.h:319
CoordBBox(const Coord &min, const Coord &max)
Construct a bounding box with the given min and max bounds.
Definition: Coord.h:296
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:298
Coord & min()
Definition: Coord.h:324
void expand(const CoordBBox &bbox)
Union this bounding box with the given bounding box.
Definition: Coord.h:438
bool isInside(const Coord &xyz) const
Return true if point (x, y, z) is inside this bounding box.
Definition: Coord.h:400
Coord dim() const
Return the dimensions of the coordinates spanned by this bounding box.
Definition: Coord.h:380
Coord getStart() const
Return the minimum coordinate.
Definition: Coord.h:333
Coord getEnd() const
Return the maximum coordinate plus one.
Definition: Coord.h:336
uint64_t Index64
Definition: Coord.h:251
void intersect(const CoordBBox &bbox)
Intersect this bounding box with the given bounding box.
Definition: Coord.h:444
void reset()
Definition: Coord.h:327
void getCornerPoints(Coord *p) const
Populates an array with the eight corner points of this bounding box.
Definition: Coord.h:470
static CoordBBox createCube(const Coord &min, ValueType dim)
Definition: Coord.h:313
ZYXIterator begin() const
Return a ZYX-order iterator that points to the minimum coordinate.
Definition: Coord.h:339
void reset(const Coord &min, const Coord &max)
Definition: Coord.h:328
void read(std::istream &is)
Unserialize this bounding box from the given stream.
Definition: Coord.h:496
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:25
Int32 ValueType
Definition: Coord.h:32
Coord operator-(const Coord &rhs) const
Definition: Coord.h:115
Coord & offset(Int32 dx, Int32 dy, Int32 dz)
Definition: Coord.h:83
Int32 & z()
Definition: Coord.h:136
Vec3d asVec3d() const
Definition: Coord.h:143
Coord operator-() const
Definition: Coord.h:119
Int32 y() const
Definition: Coord.h:131
size_t hash() const
Return a hash value for this coordinate.
Definition: Coord.h:230
Vec3s asVec3s() const
Definition: Coord.h:144
static Coord round(const Vec3< T > &xyz)
Return xyz rounded to the closest integer coordinates (cell centered conversion).
Definition: Coord.h:50
Int32 operator[](size_t i) const
Definition: Coord.h:133
Vec3I asVec3I() const
Definition: Coord.h:146
static Coord ceil(const Vec3< T > &xyz)
Return the largest integer coordinates that are not greater than xyz+1 (node centered conversion).
Definition: Coord.h:63
Coord & operator-=(const Coord &rhs)
Definition: Coord.h:104
std::numeric_limits< ValueType > Limits
Definition: Coord.h:33
Int32 * asPointer()
Definition: Coord.h:142
Coord(const Vec3I &v)
Definition: Coord.h:39
void asXYZ(Int32 &x, Int32 &y, Int32 &z) const
Definition: Coord.h:147
Coord operator+(const Coord &rhs) const
Definition: Coord.h:111
Coord offsetBy(Int32 dx, Int32 dy, Int32 dz) const
Definition: Coord.h:91
void write(std::ostream &os) const
Definition: Coord.h:220
size_t maxIndex() const
Return the index (0, 1 or 2) with the largest value.
Definition: Coord.h:217
Coord offsetBy(Int32 n) const
Definition: Coord.h:95
bool operator<(const Coord &rhs) const
Lexicographic less than.
Definition: Coord.h:156
void minComponent(const Coord &other)
Perform a component-wise minimum with the other Coord.
Definition: Coord.h:175
Int32 * data()
Definition: Coord.h:140
Coord & operator+=(const Coord &rhs)
Definition: Coord.h:97
bool operator>=(const Coord &rhs) const
Lexicographic greater than or equal to.
Definition: Coord.h:172
Int32 & x()
Definition: Coord.h:134
Coord & operator<<=(size_t n)
Definition: Coord.h:123
Coord()
Definition: Coord.h:35
static Coord maxComponent(const Coord &lhs, const Coord &rhs)
Return the component-wise maximum of the two Coords.
Definition: Coord.h:199
void maxComponent(const Coord &other)
Perform a component-wise maximum with the other Coord.
Definition: Coord.h:183
Coord & reset(Int32 x, Int32 y, Int32 z)
Reset all three coordinates with the specified arguments.
Definition: Coord.h:69
Coord(const Int32 *v)
Definition: Coord.h:40
uint32_t Index32
Definition: Coord.h:28
const Int32 * asPointer() const
Definition: Coord.h:141
Int32 x() const
Definition: Coord.h:130
Vec3i asVec3i() const
Definition: Coord.h:145
size_t minIndex() const
Return the index (0, 1 or 2) with the smallest value.
Definition: Coord.h:214
bool operator!=(const Coord &rhs) const
Definition: Coord.h:153
static Coord min()
Return the smallest possible coordinate.
Definition: Coord.h:43
Coord & reset(Int32 xyz)
Reset all three coordinates with the same specified argument.
Definition: Coord.h:77
Coord(Int32 xyz)
Definition: Coord.h:36
Coord & setZ(Int32 z)
Definition: Coord.h:81
int32_t Int32
Definition: Coord.h:27
Coord & setY(Int32 y)
Definition: Coord.h:80
bool operator==(const Coord &rhs) const
Definition: Coord.h:149
static Coord floor(const Vec3< T > &xyz)
Return the largest integer coordinates that are not greater than xyz (node centered conversion).
Definition: Coord.h:56
bool operator<=(const Coord &rhs) const
Lexicographic less than or equal to.
Definition: Coord.h:163
static Coord max()
Return the largest possible coordinate.
Definition: Coord.h:46
Int32 & operator[](size_t i)
Definition: Coord.h:137
Coord(Int32 x, Int32 y, Int32 z)
Definition: Coord.h:37
Coord(const Vec3i &v)
Definition: Coord.h:38
static bool lessThan(const Coord &a, const Coord &b)
Definition: Coord.h:208
Int32 z() const
Definition: Coord.h:132
Coord & setX(Int32 x)
Definition: Coord.h:79
Coord & offset(Int32 n)
Definition: Coord.h:90
const Int32 * data() const
Definition: Coord.h:139
bool operator>(const Coord &rhs) const
Lexicographic greater than.
Definition: Coord.h:170
static Coord minComponent(const Coord &lhs, const Coord &rhs)
Return the component-wise minimum of the two Coords.
Definition: Coord.h:191
Coord & operator>>=(size_t n)
Definition: Coord.h:124
Int32 & y()
Definition: Coord.h:135
void read(std::istream &is)
Definition: Coord.h:219
__hostdev__ uint32_t hash(uint32_t x)
Definition: common.h:13
size_t MaxIndex(const Vec3T &v)
Return the index [0,1,2] of the largest value in a 3D vector.
Definition: Math.h:947
int Ceil(float x)
Return the ceiling of x.
Definition: Math.h:856
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:551
size_t MinIndex(const Vec3T &v)
Return the index [0,1,2] of the smallest value in a 3D vector.
Definition: Math.h:931
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:525
Coord Abs(const Coord &xyz)
Definition: Coord.h:515
float Round(float x)
Return x rounded to the nearest integer.
Definition: Math.h:819
int Floor(float x)
Return the floor of x.
Definition: Math.h:848
std::ostream & operator<<(std::ostream &ostr, const Metadata &metadata)
Write a Metadata to an output stream.
Definition: Metadata.h:351
Definition: Exceptions.h:13
std::size_t result_type
Definition: Coord.h:594
std::size_t operator()(const Coord &ijk) const noexcept
Definition: Coord.h:595
openvdb::math::Coord Coord
Definition: Coord.h:592
Coord argument_type
Definition: Coord.h:593
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:212