00001 #ifndef GLUE_MLIBBBOX_H
00002 #define GLUE_MLIBBBOX_H
00003 #include "std/config.H"
00004 #ifdef min
00005 #undef min
00006 #endif
00007 #ifdef max
00008 #undef max
00009 #endif
00010 template <class P, class V>
00011 class BBOX {
00012 public:
00013 BBOX() : _valid(false) {}
00014 BBOX(const P &l, const P &h) : _valid(true), _min(l), _max(h) {}
00015 BBOX(const BBOX<P,V> &b) : _valid(b._valid), _min(b._min),_max(b._max) {}
00016 void reset() { _valid = false; }
00017 bool valid() const { return _valid; }
00018 const P &min() const { return _min; }
00019 const P &max() const { return _max; }
00020 P center() const { return (_min % _max)/2; }
00021 V dim() const { return _max - _min; }
00022 bool overlaps (const BBOX<P,V> &bbox) const {
00023 if (!_valid) return false;
00024 else return (bbox.min()[0] <= max()[0] && bbox.max()[0] >= min()[0] &&
00025 bbox.min()[1] <= max()[1] && bbox.max()[1] >= min()[1] &&
00026 bbox.min()[2] <= max()[2] && bbox.max()[2] >= min()[2]);
00027 }
00028 BBOX<P,V> &update(const P &p) {
00029 if (!_valid) {
00030 _min = p;
00031 _max = p;
00032 _valid = true;
00033 } else {
00034 for (int i=0;i<3;i++) {
00035 if (p[i] < _min[i]) _min[i] = p[i];
00036 if (p[i] > _max[i]) _max[i] = p[i];
00037 }
00038 }
00039 return *this;
00040 }
00041
00042 bool contains (const P &p) const {
00043 if (!_valid) return false;
00044 else return p[0] >= _min[0] && p[1] >= _min[1] && p[2] >= _min[2] &&
00045 p[0] <= _max[0] && p[1] <= _max[1] && p[2] <= _max[2];
00046 }
00047
00048 BBOX<P,V> &operator+=(const BBOX<P,V> &b)
00049 { return (!b.valid() ? *this : update(b.min()).update(b.max()));}
00050 bool operator==(const BBOX<P,V> &bb) const
00051 { return (!bb.valid() && !valid()) ||
00052 bb.valid() == valid() &&
00053 bb.min() == min() &&
00054 bb.max()==max(); }
00055 friend ostream &operator <<(ostream &os,const BBOX<P,V> &b)
00056 { if (b.valid()) os <<b.min()<<b.max();
00057 else os << "(invalid)";
00058 return os; }
00059
00060 protected:
00061 bool _valid;
00062 P _min, _max;
00063 };
00064 #endif