_rect.h

Go to the documentation of this file.
00001 
00025 /* === S T A R T =========================================================== */
00026 
00027 #ifndef __ETL_RECT_H
00028 #define __ETL_RECT_H
00029 
00030 /* === H E A D E R S ======================================================= */
00031 
00032 #include <functional>
00033 #include <algorithm>
00034 
00035 /* === M A C R O S ========================================================= */
00036 
00037 /* === T Y P E D E F S ===================================================== */
00038 
00039 /* === C L A S S E S & S T R U C T S ======================================= */
00040 
00041 _ETL_BEGIN_NAMESPACE
00042 
00043 template < typename T >
00044 class rect
00045 {
00046 public: //type niceties
00047     typedef T   value_type;
00048 
00049 public: //representation
00050     
00051     value_type  minx,maxx,miny,maxy;
00052 
00053 public: //interface
00054     
00055     rect() {}
00056     
00057     rect(const value_type &x1,const value_type &y1)
00058     {
00059         set_point(x1,y1);
00060     }
00061     
00062     rect(const value_type &x1,const value_type &y1,
00063             const value_type &x2,const value_type &y2)
00064     {
00065         set_point(x1,y1);
00066         expand(x2,y2);
00067     }
00068     
00069     rect(const rect<T> &o)
00070     :minx(o.minx),maxx(o.maxx),miny(o.miny),maxy(o.maxy)
00071     {}
00072         
00073     template < typename U >
00074     rect(const rect<U> &o)
00075     :minx(o.minx),maxx(o.maxx),miny(o.miny),maxy(o.maxy)
00076     {}
00077     
00078     void set_point(const value_type &x1,const value_type &y1)
00079     {
00080         minx = maxx = x1;
00081         miny = maxy = y1;       
00082     }
00083     
00084     void expand(const value_type &x1,const value_type &y1)
00085     {
00086         minx = std::min(minx,x1);
00087         maxx = std::max(maxx,x1);
00088         miny = std::min(miny,y1);
00089         maxy = std::max(maxy,y1);       
00090     }
00091     
00092     void set(const value_type &x1,const value_type &y1,
00093             const value_type &x2,const value_type &y2)
00094     {
00095         minx = x1; maxx = x2;
00096         miny = y1; maxy = y2;
00097     }
00098 
00099     //HACK HACK HACK (stupid compiler doesn't like default arguments of any type)
00100     bool valid() const
00101     {
00102         return valid(std::less<T>());
00103     }
00104     
00105     template < typename F >
00106     bool valid(const F & func) const
00107     {
00108         return func(minx,maxx) && func(miny,maxy);
00109     }
00110 };
00111 
00112 template < typename T, typename F >
00113 inline bool intersect(const rect<T> &r1, const rect<T> &r2, const F & func)
00114 {
00115     /* We wan to do the edge compare test
00116               |-----|   
00117         |------|     intersecting
00118     
00119         |-----|
00120                 |-----| not intersecting
00121     
00122         So we want to compare the mins of the one against the maxs of the other, and
00123         visa versa
00124     
00125         by default (exclude edge sharing) less will not be true if they are equal...
00126     */
00127     
00128     return func(r1.minx,r2.maxx) &&
00129            func(r2.minx,r1.maxx) &&
00130            func(r1.miny,r2.maxy) &&
00131            func(r2.miny,r1.maxy);
00132 }
00133 
00134 template < typename T >
00135 inline bool intersect(const rect<T> &r1, const rect<T> &r2)
00136 {
00137     return intersect(r1,r2,std::less<T>());
00138 }
00139 
00140 template < typename T >
00141 void set_intersect(rect<T> &rout, const rect<T> &r1, const rect<T> &r2)
00142 {
00143     //takes the intersection of the two rectangles
00144     rout.minx = std::max(r1.minx,r2.minx);
00145     rout.miny = std::max(r1.miny,r2.miny);
00146     rout.maxx = std::min(r1.maxx,r2.maxx);
00147     rout.maxy = std::min(r1.maxy,r2.maxy);
00148 }
00149 
00150 template < typename T >
00151 void set_union(rect<T> &rout, const rect<T> &r1, const rect<T> &r2)
00152 {
00153     //takes the union of the two rectangles (bounds both... will contain extra info, but that's ok)
00154     rout.set(
00155         std::min(r1.minx,r2.minx),
00156         std::min(r1.miny,r2.miny),
00157         std::max(r1.maxx,r2.maxx),
00158         std::max(r1.maxy,r2.maxy));
00159     /*rect<T> local = r1;
00160     rout.expand(r2.minx,r2.miny);
00161     rout.expand(r2.maxx,r2.maxy);
00162     rout = local;*/
00163 }
00164 
00165 _ETL_END_NAMESPACE
00166 
00167 /* === E X T E R N S ======================================================= */
00168 
00169 /* === E N D =============================================================== */
00170 
00171 #endif

Generated on Mon Jan 16 14:51:53 2006 for ETL by  doxygen 1.4.6