4 #ifndef OPENVDB_MATH_BBOX_HAS_BEEN_INCLUDED 5 #define OPENVDB_MATH_BBOX_HAS_BEEN_INCLUDED 13 #include <type_traits> 22 template<
typename Vec3T>
35 BBox(
const Vec3T& xyzMin,
const Vec3T& xyzMax);
40 BBox(
const Vec3T& xyzMin,
const Vec3T& xyzMax,
bool sorted);
55 BBox& operator=(
const BBox&) =
default;
62 const Vec3T&
min()
const {
return mMin; }
64 const Vec3T&
max()
const {
return mMax; }
66 Vec3T&
min() {
return mMin; }
68 Vec3T&
max() {
return mMax; }
86 bool isSorted()
const;
89 Vec3d getCenter()
const;
93 Vec3T extents()
const;
103 bool isInside(
const Vec3T& xyz)
const;
105 bool isInside(
const BBox&)
const;
107 bool hasOverlap(
const BBox&)
const;
114 void expand(
const Vec3T& xyz);
116 void expand(
const BBox&);
120 void expand(
const Vec3T& xyzMin,
const ElementType& length);
124 void translate(
const Vec3T& t);
127 template<
typename MapType>
128 BBox applyMap(
const MapType& map)
const;
130 template<
typename MapType>
131 BBox applyInverseMap(
const MapType& map)
const;
134 void read(std::istream& is) { mMin.read(is); mMax.read(is); }
136 void write(std::ostream& os)
const { mMin.write(os); mMax.write(os); }
146 template<
typename Vec3T>
154 template<
typename Vec3T>
157 mMin(xyzMin), mMax(xyzMax)
161 template<
typename Vec3T>
164 mMin(xyzMin), mMax(xyzMax)
166 if (!sorted) this->
sort();
169 template<
typename Vec3T>
172 mMin(xyzMin), mMax(xyzMin)
175 const ElementType size = std::is_integral<ElementType>::value ? length-1 : length;
181 template<
typename Vec3T>
184 mMin(xyz[0], xyz[1], xyz[2]),
185 mMax(xyz[3], xyz[4], xyz[5])
187 if (!sorted) this->
sort();
194 template<
typename Vec3T>
198 if (std::is_integral<ElementType>::value) {
200 return (mMin[0] > mMax[0] || mMin[1] > mMax[1] || mMin[2] > mMax[2]);
202 return mMin[0] >= mMax[0] || mMin[1] >= mMax[1] || mMin[2] >= mMax[2];
206 template<
typename Vec3T>
210 if (std::is_integral<ElementType>::value) {
211 return mMin == rhs.
min() && mMax == rhs.
max();
218 template<
typename Vec3T>
222 Vec3T tMin(mMin), tMax(mMax);
223 for (
int i = 0; i < 3; ++i) {
224 mMin[i] =
std::min(tMin[i], tMax[i]);
225 mMax[i] =
std::max(tMin[i], tMax[i]);
230 template<
typename Vec3T>
234 if (std::is_integral<ElementType>::value) {
235 return (mMin[0] <= mMax[0] && mMin[1] <= mMax[1] && mMin[2] <= mMax[2]);
238 return (mMin[0] < (mMax[0] + t) && mMin[1] < (mMax[1] + t) && mMin[2] < (mMax[2] + t));
243 template<
typename Vec3T>
247 return (
Vec3d(mMin.asPointer()) +
Vec3d(mMax.asPointer())) * 0.5;
251 template<
typename Vec3T>
255 if (std::is_integral<ElementType>::value) {
256 return (mMax - mMin) + Vec3T(1, 1, 1);
258 return (mMax - mMin);
265 template<
typename Vec3T>
269 if (std::is_integral<ElementType>::value) {
270 return xyz[0] >= mMin[0] && xyz[0] <= mMax[0] &&
271 xyz[1] >= mMin[1] && xyz[1] <= mMax[1] &&
272 xyz[2] >= mMin[2] && xyz[2] <= mMax[2];
275 return xyz[0] > (mMin[0]-t) && xyz[0] < (mMax[0]+t) &&
276 xyz[1] > (mMin[1]-t) && xyz[1] < (mMax[1]+t) &&
277 xyz[2] > (mMin[2]-t) && xyz[2] < (mMax[2]+t);
282 template<
typename Vec3T>
286 if (std::is_integral<ElementType>::value) {
287 return b.
min()[0] >= mMin[0] && b.
max()[0] <= mMax[0] &&
288 b.
min()[1] >= mMin[1] && b.
max()[1] <= mMax[1] &&
289 b.
min()[2] >= mMin[2] && b.
max()[2] <= mMax[2];
292 return (b.
min()[0]-t) > mMin[0] && (b.
max()[0]+t) < mMax[0] &&
293 (b.
min()[1]-t) > mMin[1] && (b.
max()[1]+t) < mMax[1] &&
294 (b.
min()[2]-t) > mMin[2] && (b.
max()[2]+t) < mMax[2];
299 template<
typename Vec3T>
303 if (std::is_integral<ElementType>::value) {
304 return mMax[0] >= b.
min()[0] && mMin[0] <= b.
max()[0] &&
305 mMax[1] >= b.
min()[1] && mMin[1] <= b.
max()[1] &&
306 mMax[2] >= b.
min()[2] && mMin[2] <= b.
max()[2];
309 return mMax[0] > (b.
min()[0]-t) && mMin[0] < (b.
max()[0]+t) &&
310 mMax[1] > (b.
min()[1]-t) && mMin[1] < (b.
max()[1]+t) &&
311 mMax[2] > (b.
min()[2]-t) && mMin[2] < (b.
max()[2]+t);
319 template<
typename Vec3T>
324 for (
int i = 0; i < 3; ++i) {
331 template<
typename Vec3T>
335 for (
int i = 0; i < 3; ++i) {
336 mMin[i] =
std::min(mMin[i], xyz[i]);
337 mMax[i] =
std::max(mMax[i], xyz[i]);
342 template<
typename Vec3T>
346 for (
int i = 0; i < 3; ++i) {
352 template<
typename Vec3T>
356 const ElementType size = std::is_integral<ElementType>::value ? length-1 : length;
357 for (
int i = 0; i < 3; ++i) {
358 mMin[i] =
std::min(mMin[i], xyzMin[i]);
359 mMax[i] =
std::max(mMax[i], xyzMin[i] + size);
364 template<
typename Vec3T>
372 template<
typename Vec3T>
373 template<
typename MapType>
379 bbox.
expand(map.applyMap(
Vec3R(mMin[0], mMin[1], mMin[2])));
380 bbox.
expand(map.applyMap(
Vec3R(mMin[0], mMin[1], mMax[2])));
381 bbox.
expand(map.applyMap(
Vec3R(mMin[0], mMax[1], mMin[2])));
382 bbox.
expand(map.applyMap(
Vec3R(mMax[0], mMin[1], mMin[2])));
383 bbox.
expand(map.applyMap(
Vec3R(mMax[0], mMax[1], mMin[2])));
384 bbox.
expand(map.applyMap(
Vec3R(mMax[0], mMin[1], mMax[2])));
385 bbox.
expand(map.applyMap(
Vec3R(mMin[0], mMax[1], mMax[2])));
386 bbox.
expand(map.applyMap(
Vec3R(mMax[0], mMax[1], mMax[2])));
390 template<
typename Vec3T>
391 template<
typename MapType>
397 bbox.
expand(map.applyInverseMap(
Vec3R(mMin[0], mMin[1], mMin[2])));
398 bbox.
expand(map.applyInverseMap(
Vec3R(mMin[0], mMin[1], mMax[2])));
399 bbox.
expand(map.applyInverseMap(
Vec3R(mMin[0], mMax[1], mMin[2])));
400 bbox.
expand(map.applyInverseMap(
Vec3R(mMax[0], mMin[1], mMin[2])));
401 bbox.
expand(map.applyInverseMap(
Vec3R(mMax[0], mMax[1], mMin[2])));
402 bbox.
expand(map.applyInverseMap(
Vec3R(mMax[0], mMin[1], mMax[2])));
403 bbox.
expand(map.applyInverseMap(
Vec3R(mMin[0], mMax[1], mMax[2])));
404 bbox.
expand(map.applyInverseMap(
Vec3R(mMax[0], mMax[1], mMax[2])));
411 template<
typename Vec3T>
413 operator<<(std::ostream& os, const BBox<Vec3T>& b)
415 os << b.
min() <<
" -> " << b.max();
423 #endif // OPENVDB_MATH_BBOX_HAS_BEEN_INCLUDED BBox applyMap(const MapType &map) const
Apply a map to this bounding box.
const Vec3T & max() const
Return a const reference to the maximum point of this bounding box.
Definition: BBox.h:64
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
ElementType volume() const
Return the volume enclosed by this bounding box.
Definition: BBox.h:100
void expand(ElementType padding)
Pad this bounding box.
Definition: BBox.h:321
typename Vec3Type::ValueType ElementType
Definition: BBox.h:29
void sort()
Sort the mininum and maximum points of this bounding box by their x, y and z components.
Definition: BBox.h:220
const Vec3T & min() const
Return a const reference to the minimum point of this bounding box.
Definition: BBox.h:62
Vec3d getCenter() const
Return the center point of this bounding box.
Definition: BBox.h:245
bool isApproxEqual(const Type &a, const Type &b, const Type &tolerance)
Return true if a is equal to b to within the given tolerance.
Definition: Math.h:406
Axis-aligned bounding box.
Definition: BBox.h:23
bool isInside(const Vec3T &xyz) const
Return true if the given point is inside this bounding box.
Definition: BBox.h:267
bool hasOverlap(const BBox &) const
Return true if the given bounding box overlaps with this bounding box.
Definition: BBox.h:301
Vec3< double > Vec3d
Definition: Vec3.h:665
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:474
BBox()
The default constructor creates an invalid bounding box.
Definition: BBox.h:148
void translate(const Vec3T &t)
Translate this bounding box by (tx, ty, tz).
Definition: BBox.h:366
Vec3T & max()
Return a non-const reference to the maximum point of this bounding box.
Definition: BBox.h:68
BBox applyInverseMap(const MapType &map) const
Apply the inverse of a map to this bounding box.
bool empty(const char *str)
tests if a c-string str is empty, that is its first value is '\0'
Definition: Util.h:144
size_t minExtent() const
Return the index (0, 1 or 2) of the shortest axis.
Definition: BBox.h:97
Definition: Exceptions.h:13
double ValueType
Definition: Vec3.h:28
size_t MaxIndex(const Vec3T &v)
Return the index [0,1,2] of the largest value in a 3D vector.
Definition: Math.h:946
bool operator!=(const BBox &rhs) const
Return true if this bounding box differs from the given bounding box.
Definition: BBox.h:73
size_t maxExtent() const
Return the index (0, 1 or 2) of the longest axis.
Definition: BBox.h:95
static T value()
Definition: Math.h:148
bool intersects(const BBox &other) const
Return true if the given bounding box overlaps with this bounding box.
Definition: BBox.h:109
bool operator==(const BBox &rhs) const
Return true if this bounding box is identical to the given bounding box.
Definition: BBox.h:208
void read(std::istream &is)
Unserialize this bounding box from the given stream.
Definition: BBox.h:134
void write(std::ostream &os) const
Serialize this bounding box to the given stream.
Definition: BBox.h:136
Vec3T extents() const
Return the extents of this bounding box, i.e., the length along each axis.
Definition: BBox.h:253
bool isSorted() const
Return true if all components of the minimum point are less than or equal to the corresponding compon...
Definition: BBox.h:232
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
Vec3T & min()
Return a non-const reference to the minimum point of this bounding box.
Definition: BBox.h:66
bool hasVolume() const
Return true if this bounding box has (positive) volume.
Definition: BBox.h:78
bool empty() const
Return true if this bounding box is empty, i.e., it has no (positive) volume.
Definition: BBox.h:196
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:218
size_t MinIndex(const Vec3T &v)
Return the index [0,1,2] of the smallest value in a 3D vector.
Definition: Math.h:930