00001
00025
00026
00027 #ifndef __ETL_RECT_H
00028 #define __ETL_RECT_H
00029
00030
00031
00032 #include <functional>
00033 #include <algorithm>
00034
00035
00036
00037
00038
00039
00040
00041 _ETL_BEGIN_NAMESPACE
00042
00043 template < typename T >
00044 class rect
00045 {
00046 public:
00047 typedef T value_type;
00048
00049 public:
00050
00051 value_type minx,maxx,miny,maxy;
00052
00053 public:
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
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
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
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
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
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
00160
00161
00162
00163 }
00164
00165 _ETL_END_NAMESPACE
00166
00167
00168
00169
00170
00171 #endif