vtf-logo

Interval.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00008 #if !defined(__geom_Interval_h__)
00009 #define __geom_Interval_h__
00010 
00011 // If we are debugging the whole geom package.
00012 #if defined(DEBUG_geom) && !defined(DEBUG_Interval)
00013 #define DEBUG_Interval
00014 #endif
00015 
00016 #ifdef DEBUG_Interval
00017 // Debug the FixedArray class as well.
00018 #ifndef DEBUG_FixedArray
00019 #define DEBUG_FixedArray
00020 #endif
00021 #endif
00022 
00023 #include "../defs.h"
00024 
00025 #include "../../ads/array/FixedArray.h"
00026   
00027 #include <iosfwd>
00028 
00029 BEGIN_NAMESPACE_GEOM
00030 
00032 
00039 template<int N, typename T = double>
00040 class Interval {
00041 public:
00042 
00043   //
00044   // Public types
00045   //
00046 
00048   typedef T Number;
00050   typedef ads::FixedArray<N,T> Point;
00051 
00052 private:
00053 
00054   //
00055   // Data
00056   //
00057 
00058   // The lower and upper corners of the interval.
00059   Point _min, _max;
00060   
00061 public:
00062 
00063   //--------------------------------------------------------------------------
00065   // @{
00066   
00068   Interval() :
00069     _min(),
00070     _max()
00071   {}
00072 
00074   Interval(const Point& min, const Point& max) :
00075     _min(min),
00076     _max(max)
00077   {}
00078     
00080   Interval(const Number xmin, const Number xmax) :
00081     _min(xmin),
00082     _max(xmax) {
00083     LOKI_STATIC_CHECK(N == 1, DimensionMustBe1);
00084   }
00085     
00087   Interval(const Number xmin, const Number ymin, 
00088            const Number xmax, const Number ymax) :
00089     _min(xmin, ymin),
00090     _max(xmax, ymax) {
00091     LOKI_STATIC_CHECK(N == 2, DimensionMustBe2);
00092   }
00093     
00095   Interval(const Number xmin, const Number ymin, const Number zmin,
00096            const Number xmax, const Number ymax, const Number zmax) :
00097     _min(xmin, ymin, zmin),
00098     _max(xmax, ymax, zmax) {
00099     LOKI_STATIC_CHECK(N == 3, DimensionMustBe3);
00100   }
00101     
00103 
00106   Interval(const Number* coordinates) :
00107     _min(coordinates),
00108     _max(coordinates + N)
00109   {}
00110     
00112   Interval(const Interval& other) :
00113     _min(other._min),
00114     _max(other._max)
00115   {}
00116 
00118   Interval& 
00119   operator=(const Interval& other);
00120 
00122   void 
00123   add(const Point& p);
00124 
00126   ~Interval()
00127   {}
00128 
00129   // @}
00130   //--------------------------------------------------------------------------
00132   // @{
00133 
00135   void 
00136   add(const Interval& x);
00137 
00139   template<class InputIterator>
00140   void 
00141   bound(InputIterator first, InputIterator last);
00142 
00144   void 
00145   bound(const Point& p);
00146 
00148   void 
00149   bound(const Point& p, const Point& q);
00150 
00152   void 
00153   bound(const Point& p, const Point& q, const Point& r);
00154 
00155   // @}
00156   //--------------------------------------------------------------------------
00158   // @{
00159     
00161   const Point& 
00162   getLowerCorner() const { 
00163     return _min; 
00164   }
00165 
00167   const Point& 
00168   getUpperCorner() const { 
00169     return _max; 
00170   }
00171 
00173   Number
00174   computeContent() const {
00175     Number x = 1;
00176     for (int n = 0; n != N; ++n) {
00177       x *= _max[n] - _min[n];
00178     }
00179     return x;
00180   }
00181 
00182   // @}
00183   //--------------------------------------------------------------------------
00185   // @{
00186 
00188   void
00189   setLowerCorner(const Point& lowerCorner) { 
00190     _min = lowerCorner; 
00191   }
00192 
00194   void
00195   setUpperCorner(const Point& upperCorner) { 
00196     _max = upperCorner; 
00197   }
00198 
00200   void
00201   setCorners(const Point& lowerCorner, const Point& upperCorner) { 
00202     _min = lowerCorner; 
00203     _max = upperCorner; 
00204   }
00205 
00207   void
00208   setLowerCoordinate(const int index, const Number value) { 
00209     _min[index] = value; 
00210   }
00211 
00213   void
00214   setUpperCoordinate(const int index, const Number value) { 
00215     _max[index] = value; 
00216   }
00217 
00218   // @}
00219   //--------------------------------------------------------------------------
00221   // @{
00222 
00224   static
00225   int 
00226   getDimension() { 
00227     return N; 
00228   }
00229 
00230   // @}
00231 };
00232 
00233 
00234 //
00235 // File I/O.
00236 //
00237 
00238   
00240 
00241 template<int N, typename T>
00242 void
00243 printFormatted(std::ostream& out, const Interval<N,T>& x);
00244 
00245 
00247 
00248 template<int N, typename T>
00249 inline
00250 std::istream& 
00251 operator>>(std::istream& in, Interval<N,T>& x) {
00252   typename Interval<N,T>::Point point;
00253   in >> point;
00254   x.setLowerCorner(point);
00255   in >> point;
00256   x.setUpperCorner(point);
00257   return in;
00258 }
00259 
00260 
00262 
00263 template<int N, typename T>
00264 inline
00265 std::ostream& 
00266 operator<<(std::ostream& out, const Interval<N,T>& x) {
00267   return out << x.getLowerCorner() << " " << x.getUpperCorner();
00268 }
00269 
00270 
00271 //
00272 // Equality Operators.
00273 //
00274 
00275   
00277 
00278 template<int N, typename T>
00279 inline
00280 bool 
00281 operator==(const Interval<N,T>& a, const Interval<N,T>& b) {
00282   return (a.getLowerCorner() == b.getLowerCorner() && 
00283           a.getUpperCorner() == b.getUpperCorner());
00284 }
00285 
00286 
00288 
00289 template<int N, typename T>
00290 inline
00291 bool 
00292 operator!=(const Interval<N,T>& a, const Interval<N,T>& b) {
00293   return !(a == b);
00294 }
00295 
00296 
00297 //
00298 // Mathematical Functions.
00299 //
00300 
00301 
00303 
00304 template<int N, typename T>
00305 bool 
00306 doOverlap(const Interval<N,T>& a, const Interval<N,T>& b);
00307 
00308 
00310 
00311 template<int N, typename T>
00312 Interval<N,T>
00313 computeIntersection(const Interval<N,T>& a, const Interval<N,T>& b);
00314 
00315 
00317 
00318 template<int N, typename T>
00319 void
00320 computeIntersection(const Interval<N,T>& a, const Interval<N,T>& b,
00321                     Interval<N,T>* x);
00322 
00323 
00324 END_NAMESPACE_GEOM
00325 
00326 #define __geom_Interval_ipp__
00327 #include "Interval.ipp"
00328 #undef __geom_Interval_ipp__
00329 
00330 #endif

Generated on Fri Aug 24 12:55:54 2007 for Computational Geometry Package by  doxygen 1.4.7