Go to the documentation of this file.00001
00002 #ifndef vbl_bounding_box_h_
00003 #define vbl_bounding_box_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <vcl_iosfwd.h>
00022 #include <vcl_cassert.h>
00023
00024 #if defined(VCL_SUNPRO_CC_50)
00025
00026 #endif
00027
00028
00029 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00030
00031 template <int DIM>
00032 struct vbl_bounding_box_DIM { enum { value = DIM }; };
00033 #endif // DOXYGEN_SHOULD_SKIP_THIS
00034
00035 template <class T, class DIM_>
00036 class vbl_bounding_box_base
00037 {
00038 public:
00039
00040 inline vbl_bounding_box_base() : initialized_(false) { }
00041
00042
00043 inline void update(T const& x) {
00044 assert(DIM_::value == 1);
00045 update(&x);
00046 }
00047
00048
00049 inline void update(T const& x, T const& y) {
00050 assert(DIM_::value == 2);
00051 T tmp[2] = {x,y};
00052 update(tmp);
00053 }
00054
00055
00056 inline void update(T const& x, T const& y, T const& z) {
00057 assert(DIM_::value == 3);
00058 T tmp[3] = {x,y,z};
00059 update(tmp);
00060 }
00061
00062
00063 inline int dimension() const { return DIM_::value; }
00064
00065
00066 inline void update(T const* point) {
00067 if (!initialized_) {
00068 initialized_ = true;
00069 for (int i = 0; i < dimension(); ++i)
00070 min_[i] = max_[i] = point[i];
00071 } else {
00072 for (int i = 0; i < dimension(); ++i) {
00073 if (point[i] < min_[i]) min_[i] = point[i];
00074 if (point[i] > max_[i]) max_[i] = point[i];
00075 }
00076 }
00077 }
00078
00079
00080 inline void reset() { initialized_ = false; }
00081
00082
00083 inline bool empty() const { return !initialized_; }
00084
00085
00086 inline bool inside( const T& x, const T& y) const {
00087 assert (DIM_::value == 2);
00088 return
00089 initialized_ &&
00090 min_[0] <= x && x <= max_[0] &&
00091 min_[1] <= y && y <= max_[1];
00092 }
00093
00094
00095 inline bool inside( const T& x, const T& y, const T& z) const {
00096 assert (DIM_::value == 3);
00097 return
00098 initialized_ &&
00099 min_[0] <= x && x <= max_[0] &&
00100 min_[1] <= y && y <= max_[1] &&
00101 min_[2] <= z && z <= max_[2];
00102 }
00103
00104
00105 inline bool inside(T const* point) const {
00106 if (!initialized_) return false;
00107 for ( int i=0; i<dimension(); ++i )
00108 if ( point[i] < min_[i] || max_[i] < point[i] )
00109 return false;
00110 return true;
00111 }
00112
00113
00114 inline T volume() const {
00115 if (!initialized_) return T(0);
00116 T vol = 1;
00117 for (int i=0; i<dimension(); ++i)
00118 vol *= max_[i] - min_[i];
00119 return vol;
00120 }
00121
00122 vcl_ostream& print(vcl_ostream& s) const;
00123
00124 inline T const* min() const { return min_; }
00125 inline T const* max() const { return max_; }
00126
00127 inline T * min() { return min_; }
00128 inline T * max() { return max_; }
00129
00130 inline T const& xmin() const { return min_[0]; }
00131 inline T const& xmax() const { return max_[0]; }
00132 inline T const& ymin() const { assert(DIM_::value >= 2); return min_[1]; }
00133 inline T const& ymax() const { assert(DIM_::value >= 2); return max_[1]; }
00134 inline T const& zmin() const { assert(DIM_::value >= 3); return min_[2]; }
00135 inline T const& zmax() const { assert(DIM_::value >= 3); return max_[2]; }
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 private:
00161 bool initialized_;
00162 T min_[DIM_::value];
00163 T max_[DIM_::value];
00164 };
00165
00166
00167
00168
00169
00170
00171
00172 template <class T, int DIM>
00173 class vbl_bounding_box : public vbl_bounding_box_base<T, vbl_bounding_box_DIM<DIM> >
00174 {
00175 public:
00176 };
00177
00178
00179
00180
00181 template <class T, class DIM_>
00182 inline
00183 bool nested(vbl_bounding_box_base<T,DIM_> const &a, vbl_bounding_box_base<T,DIM_> const &b)
00184 {
00185 for (int i=0; i<DIM_::value; ++i)
00186 if (a.min()[i] < b.min()[i] || a.max()[i] > b.max()[i])
00187 return false;
00188 return true;
00189 }
00190
00191
00192 template <class T, class DIM_>
00193 inline
00194 bool disjoint(vbl_bounding_box_base<T,DIM_> const &a,
00195 vbl_bounding_box_base<T,DIM_> const &b)
00196 {
00197 for (int i=0; i<DIM_::value; ++i)
00198 if (a.min()[i] > b.max()[i] || a.max()[i] < b.min()[i])
00199 return true;
00200 return false;
00201 }
00202
00203
00204 template <class T, class DIM_>
00205 inline
00206 bool meet(vbl_bounding_box_base<T,DIM_> const &a,
00207 vbl_bounding_box_base<T,DIM_> const &b)
00208 { return ! disjoint(a, b); }
00209
00210
00211 template <class T, class DIM_>
00212 vcl_ostream& operator << (vcl_ostream& s, const vbl_bounding_box_base<T,DIM_>& bbox);
00213
00214 #endif // vbl_bounding_box_h_