130 lines
3.2 KiB
C++
130 lines
3.2 KiB
C++
#ifndef BBOX3_H
|
|
#define BBOX3_H
|
|
|
|
#include "Point3.h"
|
|
|
|
template <typename Scalar>
|
|
class _BBox3 {
|
|
|
|
private:
|
|
/** minimum */
|
|
_Point3<Scalar> p1;
|
|
|
|
/** maximum */
|
|
_Point3<Scalar> p2;
|
|
|
|
public:
|
|
|
|
/** empty ctor */
|
|
_BBox3() : p1(std::numeric_limits<Scalar>::max(),
|
|
std::numeric_limits<Scalar>::max(),
|
|
std::numeric_limits<Scalar>::max()),
|
|
p2(std::numeric_limits<Scalar>::lowest(),
|
|
std::numeric_limits<Scalar>::lowest(),
|
|
std::numeric_limits<Scalar>::lowest())
|
|
{;}
|
|
|
|
/** ctor with min and max */
|
|
_BBox3(const _Point3<Scalar> min, const _Point3<Scalar> max) : p1(min), p2(max) {;}
|
|
|
|
/** create a bbox around the given point */
|
|
static _BBox3 around(const _Point3<Scalar> center, const _Point3<Scalar> size) {
|
|
return _BBox3(center-size/2, center+size/2);
|
|
}
|
|
|
|
/** adjust the bounding-box by adding this point */
|
|
void add(const _Point3<Scalar>& p) {
|
|
|
|
if (p.x > p2.x) {p2.x = p.x;}
|
|
if (p.y > p2.y) {p2.y = p.y;}
|
|
if (p.z > p2.z) {p2.z = p.z;}
|
|
|
|
if (p.x < p1.x) {p1.x = p.x;}
|
|
if (p.y < p1.y) {p1.y = p.y;}
|
|
if (p.z < p1.z) {p1.z = p.z;}
|
|
|
|
}
|
|
|
|
/** add the given bounding-box to this one */
|
|
void add(const _BBox3& bb) {
|
|
add(bb.getMin());
|
|
add(bb.getMax());
|
|
}
|
|
|
|
/** get the bbox's minimum */
|
|
const _Point3<Scalar>& getMin() const {return p1;}
|
|
|
|
/** get the bbox's maximum */
|
|
const _Point3<Scalar>& getMax() const {return p2;}
|
|
|
|
/** get the bbox's size */
|
|
const _Point3<Scalar> getSize() const {return p2-p1;}
|
|
|
|
/** get the boox's center */
|
|
const _Point3<Scalar> getCenter() const {return (p2+p1)/2;}
|
|
|
|
/** equal? */
|
|
bool operator == (const _BBox3& o) const {
|
|
return (p1.x == o.p1.x) &&
|
|
(p1.y == o.p1.y) &&
|
|
(p1.z == o.p1.z) &&
|
|
(p2.x == o.p2.x) &&
|
|
(p2.y == o.p2.y) &&
|
|
(p2.z == o.p2.z);
|
|
}
|
|
|
|
/** shrink the bbox in each dimension by the given amount */
|
|
void shrink(const Scalar v) {
|
|
shrink(_Point3<Scalar>(v,v,v));
|
|
}
|
|
|
|
/** shrink the bbox by the amount given for each dimension */
|
|
void shrink(const _Point3<Scalar>& p) {
|
|
p1 += p; // increase minimum
|
|
p2 -= p; // decrease maximum
|
|
}
|
|
|
|
/** grow the bbox by the amount given for each dimension */
|
|
void grow(const Scalar v) {
|
|
grow(_Point3<Scalar>(v,v,v));
|
|
}
|
|
|
|
/** grow the bbox by the amount given for each dimension */
|
|
void grow(const _Point3<Scalar>& p) {
|
|
p1 -= p; // decrease minimum
|
|
p2 += p; // increase maximum
|
|
}
|
|
|
|
/** set both, min/max z to the same value */
|
|
void setZ(const Scalar z) {
|
|
p1.z = z;
|
|
p2.z = z;
|
|
}
|
|
|
|
void setMinZ(const Scalar z) {this->p1.z = z;}
|
|
void setMaxZ(const Scalar z) {this->p2.z = z;}
|
|
|
|
/** does the bbox contain the given point? */
|
|
bool contains(const _Point3<Scalar>& p) const {
|
|
if (p.x < p1.x) {return false;}
|
|
if (p.x > p2.x) {return false;}
|
|
if (p.y < p1.y) {return false;}
|
|
if (p.y > p2.y) {return false;}
|
|
if (p.z < p1.z) {return false;}
|
|
if (p.z > p2.z) {return false;}
|
|
return true;
|
|
}
|
|
|
|
/** combine two bboxes */
|
|
static _BBox3 join(const _BBox3& bb1, const _BBox3& bb2) {
|
|
const _Point3<Scalar> min( std::min(bb1.p1.x, bb2.p1.x), std::min(bb1.p1.y, bb2.p1.y), std::min(bb1.p1.z, bb2.p1.z) );
|
|
const _Point3<Scalar> max( std::max(bb1.p2.x, bb2.p2.x), std::max(bb1.p2.y, bb2.p2.y), std::max(bb1.p2.z, bb2.p2.z) );
|
|
return _BBox3(min,max);
|
|
}
|
|
|
|
};
|
|
|
|
using BBox3 = _BBox3<float>;
|
|
|
|
#endif // BBOX3_H
|