_min(SGLimits<T>::max(), SGLimits<T>::max(), SGLimits<T>::max()),
_max(-SGLimits<T>::max(), -SGLimits<T>::max(), -SGLimits<T>::max())
{ }
+ SGBox(const SGVec3<T>& pt) :
+ _min(pt),
+ _max(pt)
+ { }
+ SGBox(const SGVec3<T>& min, const SGVec3<T>& max) :
+ _min(min),
+ _max(max)
+ { }
+ template<typename S>
+ explicit SGBox(const SGBox<S>& box) :
+ _min(box.getMin()),
+ _max(box.getMax())
+ { }
+
void setMin(const SGVec3<T>& min)
{ _min = min; }
const SGVec3<T>& getMin() const
const SGVec3<T>& getMax() const
{ return _max; }
+ SGVec3<T> getCorner(unsigned i) const
+ { return SGVec3<T>((i&1) ? _min[0] : _max[0],
+ (i&2) ? _min[1] : _max[1],
+ (i&4) ? _min[2] : _max[2]); }
+
+ template<typename S>
+ SGVec3<T> getNearestCorner(const SGVec3<S>& pt) const
+ {
+ SGVec3<T> center = getCenter();
+ return SGVec3<T>((pt[0] <= center[0]) ? _min[0] : _max[0],
+ (pt[1] <= center[1]) ? _min[1] : _max[1],
+ (pt[2] <= center[2]) ? _min[2] : _max[2]);
+ }
+ template<typename S>
+ SGVec3<T> getFarestCorner(const SGVec3<S>& pt) const
+ {
+ SGVec3<T> center = getCenter();
+ return SGVec3<T>((pt[0] > center[0]) ? _min[0] : _max[0],
+ (pt[1] > center[1]) ? _min[1] : _max[1],
+ (pt[2] > center[2]) ? _min[2] : _max[2]);
+ }
+
+ // return the closest point to pt still in the box
+ template<typename S>
+ SGVec3<T> getClosestPoint(const SGVec3<S>& pt) const
+ {
+ return SGVec3<T>((pt[0] < _min[0]) ? _min[0] : ((_max[0] < pt[0]) ? _max[0] : T(pt[0])),
+ (pt[1] < _min[1]) ? _min[1] : ((_max[1] < pt[1]) ? _max[1] : T(pt[1])),
+ (pt[2] < _min[2]) ? _min[2] : ((_max[2] < pt[2]) ? _max[2] : T(pt[2])));
+ }
+
// Only works for floating point types
SGVec3<T> getCenter() const
{ return T(0.5)*(_min + _max); }
// Only valid for nonempty boxes
SGVec3<T> getSize() const
{ return _max - _min; }
+ SGVec3<T> getHalfSize() const
+ { return T(0.5)*getSize(); }
T getVolume() const
{
return (_max[0] - _min[0])*(_max[1] - _min[1])*(_max[2] - _min[2]);
}
- const bool empty() const
+ bool empty() const
{ return !valid(); }
bool valid() const
// Note that this only works if the box is nonmepty
unsigned getBroadestAxis() const
{
- SGVec3d size = getSize();
+ SGVec3<T> size = getSize();
if (size[1] <= size[0] && size[2] <= size[0])
return 0;
else if (size[2] <= size[1])
return 2;
}
+ // Note that this only works if the box is nonmepty
+ unsigned getSmallestAxis() const
+ {
+ SGVec3<T> size = getSize();
+ if (size[1] >= size[0] && size[2] >= size[0])
+ return 0;
+ else if (size[2] >= size[1])
+ return 1;
+ else
+ return 2;
+ }
+
private:
SGVec3<T> _min;
SGVec3<T> _max;