31 #ifndef OPENVDB_MATH_BBOX_HAS_BEEN_INCLUDED 32 #define OPENVDB_MATH_BBOX_HAS_BEEN_INCLUDED 38 #include <boost/type_traits/is_integral.hpp> 46 template<
typename Vec3T>
59 BBox(
const Vec3T& xyzMin,
const Vec3T& xyzMax);
63 BBox(
const Vec3T& xyzMin,
const Vec3T& xyzMax,
bool sorted);
68 BBox(
const Vec3T& xyzMin,
const ElementType& length);
72 explicit BBox(
const ElementType* xyz,
bool sorted =
true);
81 const Vec3T&
min()
const {
return mMin; }
84 const Vec3T&
max()
const {
return mMax; }
87 Vec3T&
min() {
return mMin; }
90 Vec3T&
max() {
return mMax; }
106 operator bool()
const {
return !this->empty(); }
111 bool isSorted()
const;
114 Vec3d getCenter()
const;
120 Vec3T extents()
const;
123 ElementType
volume()
const { Vec3T e = this->extents();
return e[0] * e[1] * e[2]; }
132 bool isInside(
const Vec3T& xyz)
const;
135 bool isInside(
const BBox&)
const;
138 bool hasOverlap(
const BBox&)
const;
141 void expand(ElementType padding);
144 void expand(
const Vec3T& xyz);
147 void expand(
const BBox&);
151 void expand(
const Vec3T& xyzMin,
const ElementType& length);
154 void translate(
const Vec3T& t);
157 template<
typename MapType>
158 BBox applyMap(
const MapType& map)
const;
161 template<
typename MapType>
162 BBox applyInverseMap(
const MapType& map)
const;
165 void read(std::istream& is) { mMin.read(is); mMax.read(is); }
168 void write(std::ostream& os)
const { mMin.write(os); mMax.write(os); }
178 template<
typename Vec3T>
186 template<
typename Vec3T>
189 mMin(xyzMin), mMax(xyzMax)
193 template<
typename Vec3T>
196 mMin(xyzMin), mMax(xyzMax)
198 if (!sorted) this->
sort();
201 template<
typename Vec3T>
204 mMin(xyzMin), mMax(xyzMin)
207 const ElementType size = boost::is_integral<ElementType>::value ? length-1 : length;
213 template<
typename Vec3T>
216 mMin(xyz[0], xyz[1], xyz[2]),
217 mMax(xyz[3], xyz[4], xyz[5])
219 if (!sorted) this->
sort();
223 template<
typename Vec3T>
226 mMin(other.mMin), mMax(other.mMax)
234 template<
typename Vec3T>
238 if (boost::is_integral<ElementType>::value) {
240 return (mMin[0] > mMax[0] || mMin[1] > mMax[1] || mMin[2] > mMax[2]);
242 return mMin[0] >= mMax[0] || mMin[1] >= mMax[1] || mMin[2] >= mMax[2];
246 template<
typename Vec3T>
250 if (boost::is_integral<ElementType>::value) {
251 return mMin == rhs.
min() && mMax == rhs.
max();
258 template<
typename Vec3T>
262 Vec3T tMin(mMin), tMax(mMax);
263 for (
int i = 0; i < 3; ++i) {
264 mMin[i] =
std::min(tMin[i], tMax[i]);
265 mMax[i] =
std::max(tMin[i], tMax[i]);
270 template<
typename Vec3T>
274 if (boost::is_integral<ElementType>::value) {
275 return (mMin[0] <= mMax[0] && mMin[1] <= mMax[1] && mMin[2] <= mMax[2]);
278 return (mMin[0] < (mMax[0] + t) && mMin[1] < (mMax[1] + t) && mMin[2] < (mMax[2] + t));
283 template<
typename Vec3T>
287 return (
Vec3d(mMin.asPointer()) +
Vec3d(mMax.asPointer())) * 0.5;
291 template<
typename Vec3T>
295 if (boost::is_integral<ElementType>::value) {
296 return (mMax - mMin) + Vec3T(1, 1, 1);
298 return (mMax - mMin);
305 template<
typename Vec3T>
309 if (boost::is_integral<ElementType>::value) {
310 return xyz[0] >= mMin[0] && xyz[0] <= mMax[0] &&
311 xyz[1] >= mMin[1] && xyz[1] <= mMax[1] &&
312 xyz[2] >= mMin[2] && xyz[2] <= mMax[2];
315 return xyz[0] > (mMin[0]-t) && xyz[0] < (mMax[0]+t) &&
316 xyz[1] > (mMin[1]-t) && xyz[1] < (mMax[1]+t) &&
317 xyz[2] > (mMin[2]-t) && xyz[2] < (mMax[2]+t);
322 template<
typename Vec3T>
326 if (boost::is_integral<ElementType>::value) {
327 return b.
min()[0] >= mMin[0] && b.
max()[0] <= mMax[0] &&
328 b.
min()[1] >= mMin[1] && b.
max()[1] <= mMax[1] &&
329 b.
min()[2] >= mMin[2] && b.
max()[2] <= mMax[2];
332 return (b.
min()[0]-t) > mMin[0] && (b.
max()[0]+t) < mMax[0] &&
333 (b.
min()[1]-t) > mMin[1] && (b.
max()[1]+t) < mMax[1] &&
334 (b.
min()[2]-t) > mMin[2] && (b.
max()[2]+t) < mMax[2];
339 template<
typename Vec3T>
343 if (boost::is_integral<ElementType>::value) {
344 return mMax[0] >= b.
min()[0] && mMin[0] <= b.
max()[0] &&
345 mMax[1] >= b.
min()[1] && mMin[1] <= b.
max()[1] &&
346 mMax[2] >= b.
min()[2] && mMin[2] <= b.
max()[2];
349 return mMax[0] > (b.
min()[0]-t) && mMin[0] < (b.
max()[0]+t) &&
350 mMax[1] > (b.
min()[1]-t) && mMin[1] < (b.
max()[1]+t) &&
351 mMax[2] > (b.
min()[2]-t) && mMin[2] < (b.
max()[2]+t);
359 template<
typename Vec3T>
364 for (
int i = 0; i < 3; ++i) {
371 template<
typename Vec3T>
375 for (
int i = 0; i < 3; ++i) {
376 mMin[i] =
std::min(mMin[i], xyz[i]);
377 mMax[i] =
std::max(mMax[i], xyz[i]);
382 template<
typename Vec3T>
386 for (
int i = 0; i < 3; ++i) {
392 template<
typename Vec3T>
396 const ElementType size = boost::is_integral<ElementType>::value ? length-1 : length;
397 for (
int i = 0; i < 3; ++i) {
398 mMin[i] =
std::min(mMin[i], xyzMin[i]);
399 mMax[i] =
std::max(mMax[i], xyzMin[i] + size);
404 template<
typename Vec3T>
412 template<
typename Vec3T>
413 template<
typename MapType>
419 bbox.
expand(map.applyMap(Vec3R(mMin[0], mMin[1], mMin[2])));
420 bbox.
expand(map.applyMap(Vec3R(mMin[0], mMin[1], mMax[2])));
421 bbox.
expand(map.applyMap(Vec3R(mMin[0], mMax[1], mMin[2])));
422 bbox.
expand(map.applyMap(Vec3R(mMax[0], mMin[1], mMin[2])));
423 bbox.
expand(map.applyMap(Vec3R(mMax[0], mMax[1], mMin[2])));
424 bbox.
expand(map.applyMap(Vec3R(mMax[0], mMin[1], mMax[2])));
425 bbox.
expand(map.applyMap(Vec3R(mMin[0], mMax[1], mMax[2])));
426 bbox.
expand(map.applyMap(Vec3R(mMax[0], mMax[1], mMax[2])));
430 template<
typename Vec3T>
431 template<
typename MapType>
437 bbox.
expand(map.applyInverseMap(Vec3R(mMin[0], mMin[1], mMin[2])));
438 bbox.
expand(map.applyInverseMap(Vec3R(mMin[0], mMin[1], mMax[2])));
439 bbox.
expand(map.applyInverseMap(Vec3R(mMin[0], mMax[1], mMin[2])));
440 bbox.
expand(map.applyInverseMap(Vec3R(mMax[0], mMin[1], mMin[2])));
441 bbox.
expand(map.applyInverseMap(Vec3R(mMax[0], mMax[1], mMin[2])));
442 bbox.
expand(map.applyInverseMap(Vec3R(mMax[0], mMin[1], mMax[2])));
443 bbox.
expand(map.applyInverseMap(Vec3R(mMin[0], mMax[1], mMax[2])));
444 bbox.
expand(map.applyInverseMap(Vec3R(mMax[0], mMax[1], mMax[2])));
451 template<
typename Vec3T>
453 operator<<(std::ostream& os, const BBox<Vec3T>& b)
455 os << b.
min() <<
" -> " << b.max();
463 #endif // OPENVDB_MATH_BBOX_HAS_BEEN_INCLUDED Vec3Type::ValueType ElementType
Definition: BBox.h:53
size_t maxExtent() const
Return the index (0, 1 or 2) of the longest axis.
Definition: BBox.h:126
double ValueType
Definition: Vec3.h:51
static T value()
Definition: Math.h:125
size_t minExtent() const
Return the index (0, 1 or 2) of the shortest axis.
Definition: BBox.h:129
bool hasVolume() const
Return true if the BBox has a (positive) volume.
Definition: BBox.h:103
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:450
size_t MaxIndex(const Vec3T &v)
Return the index [0,1,2] of the largest value in a 3D vector.
Definition: Math.h:893
bool operator!=(const BBox &rhs) const
Return true if the two BBox'es are not identical.
Definition: BBox.h:96
math::Vec3< Real > Vec3R
Definition: Types.h:76
BBox applyMap(const MapType &map) const
Apply a map to this bounding box.
void read(std::istream &is)
Unserialize this bounding box from the given stream.
Definition: BBox.h:165
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:370
Vec3T & min()
Return a non-const reference to the minimum point of the BBox.
Definition: BBox.h:87
Vec3T Vec3Type
Definition: BBox.h:50
const Vec3T & min() const
Return a const reference to the minimum point of the BBox.
Definition: BBox.h:81
const Vec3T & max() const
Return a const reference to the maximum point of the BBox.
Definition: BBox.h:84
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Vec3T VectorType
Definition: BBox.h:52
bool empty() const
Return true if the BBox is empty, i.e. has no (positive) volume.
Definition: BBox.h:236
Vec3T & max()
Return a non-const reference to the maximum point of the BBox.
Definition: BBox.h:90
Definition: Exceptions.h:39
bool operator==(const BBox &rhs) const
Return true if the two BBox'es are identical.
Definition: BBox.h:248
bool isSorted() const
Return true if the all components of mMin <= mMax, i.e. the volume is not negative.
Definition: BBox.h:272
BBox()
Default constructor creates an invalid BBox.
Definition: BBox.h:180
Vec3< double > Vec3d
Definition: Vec3.h:643
BBox applyInverseMap(const MapType &map) const
Apply the inverse of a map to this bounding box.
Axis-aligned bounding box.
Definition: BBox.h:47
ElementType volume() const
Return the volume spanned by this BBox.
Definition: BBox.h:123
bool isInside(const Vec3T &xyz) const
Return true if point (x, y, z) is inside this bounding box.
Definition: BBox.h:307
Vec3T extents() const
Returns the extents of the BBox, i.e. the length per axis for floating points values or number of gri...
Definition: BBox.h:293
size_t MinIndex(const Vec3T &v)
Return the index [0,1,2] of the smallest value in a 3D vector.
Definition: Math.h:872
void write(std::ostream &os) const
Serialize this bounding box to the given stream.
Definition: BBox.h:168
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
bool hasOverlap(const BBox &) const
Return true if the given bounding box overlaps with this bounding box.
Definition: BBox.h:341
void translate(const Vec3T &t)
Translate this bounding box by .
Definition: BBox.h:406
Vec3d getCenter() const
Return the center point of the BBox.
Definition: BBox.h:285
void sort()
Sort the min/max by x,y,z component.
Definition: BBox.h:260
Vec3T ValueType
Definition: BBox.h:51
void expand(ElementType padding)
Pad this bounding box.
Definition: BBox.h:361