00001 #ifndef NMWR_GB_GEOMETRY_BOX_H
00002 #define NMWR_GB_GEOMETRY_BOX_H
00003
00004
00005
00006
00007 #include <algorithm>
00008 #include "Geometry/point-traits.h"
00009
00025 template<class coord>
00026 class box {
00027 typedef box<coord> self;
00028 typedef point_traits<coord> pt;
00029 private:
00030 coord minc, maxc;
00031 public:
00032
00033
00034 box(const coord& cmin) : minc(cmin), maxc(cmin) {}
00035 box(const coord& cmin, const coord& cmax) : minc(cmin), maxc(cmin)
00036 { *this |= cmax; }
00037
00038 const coord& the_min() const { return minc;}
00039 const coord& the_max() const { return maxc;}
00040 coord center() const { return 0.5*(minc+maxc);}
00041
00042
00043 coord& the_min() { return minc;}
00044 coord& the_max() { return maxc;}
00045
00046
00047 coord global_coords(const coord& local) const {
00048 coord res = minc;
00049 for(int i = pt::LowerIndex(res); i <= pt::UpperIndex(res); ++i) {
00050 res[i] = (1-local[i]) * minc[i] + local[i] * maxc[i];
00051 }
00052 return res;
00053 }
00054
00055 typedef coord argument_type;
00056 typedef coord result_type;
00057 coord operator()(const coord& local) const
00058 { return global_coords(local); }
00059
00060 bool contains(coord const& p) const {
00061 bool res = true;
00062 for(int i = pt::LowerIndex(p); i <= pt::UpperIndex(p); ++i) {
00063 res = res && (p[i] >= minc[i]) && (p[i] <= maxc[i]);
00064 }
00065 return res;
00066 }
00067
00068
00069
00070
00071
00072
00073
00074
00075 self & operator |= (self const& rs)
00076 {
00077 for(int i = pt::LowerIndex(minc); i <= pt::UpperIndex(minc); ++i) {
00078 minc[i] = std::min(minc[i],rs.minc[i]);
00079 maxc[i] = std::max(maxc[i],rs.maxc[i]);
00080 }
00081 return *this;
00082 }
00083 friend self operator | (const self& ls, const self& rs)
00084 {
00085 self res(ls);
00086 return res |= rs;
00087 }
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 };
00098
00099 #endif
00100
00101