31 #ifndef OPENVDB_MATH_BBOX_HAS_BEEN_INCLUDED 32 #define OPENVDB_MATH_BBOX_HAS_BEEN_INCLUDED 40 #include <type_traits> 49 template<
typename Vec3T>
62 BBox(
const Vec3T& xyzMin,
const Vec3T& xyzMax);
67 BBox(
const Vec3T& xyzMin,
const Vec3T& xyzMax,
bool sorted);
82 BBox& operator=(
const BBox&) =
default;
89 const Vec3T&
min()
const {
return mMin; }
91 const Vec3T&
max()
const {
return mMax; }
93 Vec3T&
min() {
return mMin; }
95 Vec3T&
max() {
return mMax; }
107 operator bool()
const {
return !this->empty(); }
113 bool isSorted()
const;
116 Vec3d getCenter()
const;
120 Vec3T extents()
const;
130 bool isInside(
const Vec3T& xyz)
const;
132 bool isInside(
const BBox&)
const;
134 bool hasOverlap(
const BBox&)
const;
141 void expand(
const Vec3T& xyz);
143 void expand(
const BBox&);
147 void expand(
const Vec3T& xyzMin,
const ElementType& length);
151 void translate(
const Vec3T& t);
154 template<
typename MapType>
155 BBox applyMap(
const MapType& map)
const;
157 template<
typename MapType>
158 BBox applyInverseMap(
const MapType& map)
const;
161 void read(std::istream& is) { mMin.read(is); mMax.read(is); }
163 void write(std::ostream& os)
const { mMin.write(os); mMax.write(os); }
173 template<
typename Vec3T>
181 template<
typename Vec3T>
184 mMin(xyzMin), mMax(xyzMax)
188 template<
typename Vec3T>
191 mMin(xyzMin), mMax(xyzMax)
193 if (!sorted) this->sort();
196 template<
typename Vec3T>
199 mMin(xyzMin), mMax(xyzMin)
202 const ElementType size = std::is_integral<ElementType>::value ? length-1 : length;
208 template<
typename Vec3T>
211 mMin(xyz[0], xyz[1], xyz[2]),
212 mMax(xyz[3], xyz[4], xyz[5])
214 if (!sorted) this->sort();
221 template<
typename Vec3T>
225 if (std::is_integral<ElementType>::value) {
227 return (mMin[0] > mMax[0] || mMin[1] > mMax[1] || mMin[2] > mMax[2]);
229 return mMin[0] >= mMax[0] || mMin[1] >= mMax[1] || mMin[2] >= mMax[2];
233 template<
typename Vec3T>
237 if (std::is_integral<ElementType>::value) {
238 return mMin == rhs.
min() && mMax == rhs.
max();
245 template<
typename Vec3T>
249 Vec3T tMin(mMin), tMax(mMax);
250 for (
int i = 0; i < 3; ++i) {
251 mMin[i] =
std::min(tMin[i], tMax[i]);
252 mMax[i] =
std::max(tMin[i], tMax[i]);
257 template<
typename Vec3T>
261 if (std::is_integral<ElementType>::value) {
262 return (mMin[0] <= mMax[0] && mMin[1] <= mMax[1] && mMin[2] <= mMax[2]);
265 return (mMin[0] < (mMax[0] + t) && mMin[1] < (mMax[1] + t) && mMin[2] < (mMax[2] + t));
270 template<
typename Vec3T>
274 return (
Vec3d(mMin.asPointer()) +
Vec3d(mMax.asPointer())) * 0.5;
278 template<
typename Vec3T>
282 if (std::is_integral<ElementType>::value) {
283 return (mMax - mMin) + Vec3T(1, 1, 1);
285 return (mMax - mMin);
292 template<
typename Vec3T>
296 if (std::is_integral<ElementType>::value) {
297 return xyz[0] >= mMin[0] && xyz[0] <= mMax[0] &&
298 xyz[1] >= mMin[1] && xyz[1] <= mMax[1] &&
299 xyz[2] >= mMin[2] && xyz[2] <= mMax[2];
302 return xyz[0] > (mMin[0]-t) && xyz[0] < (mMax[0]+t) &&
303 xyz[1] > (mMin[1]-t) && xyz[1] < (mMax[1]+t) &&
304 xyz[2] > (mMin[2]-t) && xyz[2] < (mMax[2]+t);
309 template<
typename Vec3T>
313 if (std::is_integral<ElementType>::value) {
314 return b.
min()[0] >= mMin[0] && b.
max()[0] <= mMax[0] &&
315 b.
min()[1] >= mMin[1] && b.
max()[1] <= mMax[1] &&
316 b.
min()[2] >= mMin[2] && b.
max()[2] <= mMax[2];
319 return (b.
min()[0]-t) > mMin[0] && (b.
max()[0]+t) < mMax[0] &&
320 (b.
min()[1]-t) > mMin[1] && (b.
max()[1]+t) < mMax[1] &&
321 (b.
min()[2]-t) > mMin[2] && (b.
max()[2]+t) < mMax[2];
326 template<
typename Vec3T>
330 if (std::is_integral<ElementType>::value) {
331 return mMax[0] >= b.
min()[0] && mMin[0] <= b.
max()[0] &&
332 mMax[1] >= b.
min()[1] && mMin[1] <= b.
max()[1] &&
333 mMax[2] >= b.
min()[2] && mMin[2] <= b.
max()[2];
336 return mMax[0] > (b.
min()[0]-t) && mMin[0] < (b.
max()[0]+t) &&
337 mMax[1] > (b.
min()[1]-t) && mMin[1] < (b.
max()[1]+t) &&
338 mMax[2] > (b.
min()[2]-t) && mMin[2] < (b.
max()[2]+t);
346 template<
typename Vec3T>
351 for (
int i = 0; i < 3; ++i) {
358 template<
typename Vec3T>
362 for (
int i = 0; i < 3; ++i) {
363 mMin[i] =
std::min(mMin[i], xyz[i]);
364 mMax[i] =
std::max(mMax[i], xyz[i]);
369 template<
typename Vec3T>
373 for (
int i = 0; i < 3; ++i) {
379 template<
typename Vec3T>
383 const ElementType size = std::is_integral<ElementType>::value ? length-1 : length;
384 for (
int i = 0; i < 3; ++i) {
385 mMin[i] =
std::min(mMin[i], xyzMin[i]);
386 mMax[i] =
std::max(mMax[i], xyzMin[i] + size);
391 template<
typename Vec3T>
399 template<
typename Vec3T>
400 template<
typename MapType>
406 bbox.
expand(map.applyMap(
Vec3R(mMin[0], mMin[1], mMin[2])));
407 bbox.
expand(map.applyMap(
Vec3R(mMin[0], mMin[1], mMax[2])));
408 bbox.
expand(map.applyMap(
Vec3R(mMin[0], mMax[1], mMin[2])));
409 bbox.
expand(map.applyMap(
Vec3R(mMax[0], mMin[1], mMin[2])));
410 bbox.
expand(map.applyMap(
Vec3R(mMax[0], mMax[1], mMin[2])));
411 bbox.
expand(map.applyMap(
Vec3R(mMax[0], mMin[1], mMax[2])));
412 bbox.
expand(map.applyMap(
Vec3R(mMin[0], mMax[1], mMax[2])));
413 bbox.
expand(map.applyMap(
Vec3R(mMax[0], mMax[1], mMax[2])));
417 template<
typename Vec3T>
418 template<
typename MapType>
424 bbox.
expand(map.applyInverseMap(
Vec3R(mMin[0], mMin[1], mMin[2])));
425 bbox.
expand(map.applyInverseMap(
Vec3R(mMin[0], mMin[1], mMax[2])));
426 bbox.
expand(map.applyInverseMap(
Vec3R(mMin[0], mMax[1], mMin[2])));
427 bbox.
expand(map.applyInverseMap(
Vec3R(mMax[0], mMin[1], mMin[2])));
428 bbox.
expand(map.applyInverseMap(
Vec3R(mMax[0], mMax[1], mMin[2])));
429 bbox.
expand(map.applyInverseMap(
Vec3R(mMax[0], mMin[1], mMax[2])));
430 bbox.
expand(map.applyInverseMap(
Vec3R(mMin[0], mMax[1], mMax[2])));
431 bbox.
expand(map.applyInverseMap(
Vec3R(mMax[0], mMax[1], mMax[2])));
438 template<
typename Vec3T>
440 operator<<(std::ostream& os, const BBox<Vec3T>& b)
442 os << b.
min() <<
" -> " << b.max();
450 #endif // OPENVDB_MATH_BBOX_HAS_BEEN_INCLUDED void write(std::ostream &os) const
Serialize this bounding box to the given stream.
Definition: BBox.h:163
void read(std::istream &is)
Unserialize this bounding box from the given stream.
Definition: BBox.h:161
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
size_t MinIndex(const Vec3T &v)
Return the index [0,1,2] of the smallest value in a 3D vector.
Definition: Math.h:890
Axis-aligned bounding box.
Definition: BBox.h:50
const Vec3T & min() const
Return a const reference to the minimum point of this bounding box.
Definition: BBox.h:89
Tolerance for floating-point comparison.
Definition: Math.h:117
bool operator!=(const BBox &rhs) const
Return true if this bounding box differs from the given bounding box.
Definition: BBox.h:100
Vec3< double > Vec3d
Definition: Vec3.h:679
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
ElementType volume() const
Return the volume enclosed by this bounding box.
Definition: BBox.h:127
double ValueType
Definition: Vec3.h:54
Definition: Exceptions.h:40
bool intersects(const BBox &other) const
Return true if the given bounding box overlaps with this bounding box.
Definition: BBox.h:136
size_t MaxIndex(const Vec3T &v)
Return the index [0,1,2] of the largest value in a 3D vector.
Definition: Math.h:911
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:488
size_t maxExtent() const
Return the index (0, 1 or 2) of the longest axis.
Definition: BBox.h:122
const Vec3T & max() const
Return a const reference to the maximum point of this bounding box.
Definition: BBox.h:91
Vec3T & min()
Return a non-const reference to the minimum point of this bounding box.
Definition: BBox.h:93
typename Vec3Type::ValueType ElementType
Definition: BBox.h:56
bool isApproxEqual(const Type &a, const Type &b)
Return true if a is equal to b to within the default floating-point comparison tolerance.
Definition: Math.h:358
Vec3T & max()
Return a non-const reference to the maximum point of this bounding box.
Definition: BBox.h:95
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
size_t minExtent() const
Return the index (0, 1 or 2) of the shortest axis.
Definition: BBox.h:124
void expand(ElementType padding)
Pad this bounding box.
Definition: BBox.h:348
bool hasVolume() const
Return true if this bounding box has (positive) volume.
Definition: BBox.h:105