rect.h

Go to the documentation of this file.
00001 /* === S Y N F I G ========================================================= */
00021 /* ========================================================================= */
00022 
00023 /* === S T A R T =========================================================== */
00024 
00025 #ifndef __SYNFIG_RECT_H
00026 #define __SYNFIG_RECT_H
00027 
00028 /* === H E A D E R S ======================================================= */
00029 
00030 #include <ETL/rect>
00031 #include "real.h"
00032 #include "vector.h"
00033 #include <limits>
00034 #include <cmath>
00035 
00036 /* === M A C R O S ========================================================= */
00037 
00038 /* === T Y P E D E F S ===================================================== */
00039 
00040 /* === C L A S S E S & S T R U C T S ======================================= */
00041 
00042 namespace synfig {
00043 
00044 class Rect : public etl::rect<Real>
00045 {
00046 public:
00047 
00048     using etl::rect<Real>::set_point;
00049     using etl::rect<Real>::expand;
00050     using etl::rect<Real>::set;
00051 
00052     static Rect full_plane();
00053 
00054     static Rect zero()
00055     {
00056         return Rect(
00057             0,
00058             0,
00059             0,
00060             0
00061         );
00062     }
00063 
00064     Rect() { }
00065 
00066     Rect(const Point& x) { set_point(x); }
00067 
00068     Rect(const Point& min, const Point& max) { set_point(min); expand(max); }
00069 
00070     Rect(const value_type &x1,const value_type &y1) { set_point(x1,y1); }
00071     
00072     Rect(const value_type &x1,const value_type &y1,
00073             const value_type &x2,const value_type &y2)
00074     {
00075         set_point(x1,y1);
00076         expand(x2,y2);
00077     }
00078         
00079     void set_point(const Point& max) { set_point(max[0],max[1]);    }
00080     
00081     Rect& expand(const Point& max) { expand(max[0],max[1]); return *this; }
00082 
00083     Rect& expand(const Real& r) { minx-=r; miny-=r; maxx+=r; maxy+=r; return *this; }
00084 
00085     Rect& expand_x(const Real& r) { minx-=r; maxx+=r; return *this; }
00086 
00087     Rect& expand_y(const Real& r) { miny-=r; maxy+=r; return *this; }
00088     
00089     Rect& set(const Point& min,const Point& max) { set(min[0],min[1],max[0],max[1]); return *this; }
00090     
00091     Point get_min()const { return Point(minx,miny); }
00092     Point get_max()const { return Point(maxx,maxy); }
00093     
00094     bool is_inside(const Point& x) { return x[0]>minx && x[0]<maxx && x[1]>miny && x[1]<maxy; }
00095         
00096     Real area()const
00097     {
00098         return (maxx-minx)*(maxy-miny);
00099     }
00100     
00101     // Operators
00102     
00103     Rect& operator+=(const Vector& rhs)
00104     {
00105         minx+=rhs[0]; miny+=rhs[1];
00106         maxx+=rhs[0]; maxy+=rhs[1];
00107         return *this;
00108     }
00109 
00110     Rect& operator-=(const Vector& rhs)
00111     {
00112         minx-=rhs[0]; miny-=rhs[1];
00113         maxx-=rhs[0]; maxy-=rhs[1];
00114         return *this;
00115     }
00116 
00117     Rect& operator*=(const Real& rhs)
00118     {
00119         minx*=rhs; miny*=rhs;
00120         maxx*=rhs; maxy*=rhs;
00121         return *this;
00122     }
00123 
00124     Rect& operator/=(Real rhs)
00125     {
00126         rhs=1.0/rhs; // Avoid doing several divisions
00127         minx*=rhs; miny*=rhs;
00128         maxx*=rhs; maxy*=rhs;
00129         return *this;
00130     }
00131 
00132     Rect& operator&=(const Rect& rhs)
00133     {
00134         if(rhs.area()>0.00000001 && area()>0.00000001)
00135             etl::set_intersect(*this,*this,rhs);
00136         else
00137             *this=zero();
00138         return *this;
00139     }
00140 
00141     Rect& operator|=(const Rect& rhs)
00142     {
00143         if(rhs.area()>0.00000001 && area()>0.00000001)
00144             etl::set_union(*this,*this,rhs);
00145         else
00146         {
00147             if(area()<rhs.area())
00148                 *this=rhs;
00149         }
00150         return *this;
00151     }
00152 
00153     Rect operator+(const Vector& rhs)const { return Rect(*this)+=rhs; }
00154 
00155     Rect operator-(const Vector& rhs)const { return Rect(*this)-=rhs; }
00156 
00157     Rect operator*(const Real& rhs)const { return Rect(*this)*=rhs; }
00158 
00159     Rect operator/(const Real& rhs)const { return Rect(*this)/=rhs; }
00160 
00161     Rect operator&(const Rect& rhs)const { return Rect(*this)&=rhs; }
00162 
00163     Rect operator|(const Rect& rhs)const { return Rect(*this)|=rhs; }
00164 
00165     bool operator&&(const Rect& rhs)const { return etl::intersect(*this, rhs); }
00166 
00167     bool is_valid()const { return valid(); }
00168 }; // END of class Rect
00169 
00170 }; // END of namespace synfig
00171 
00172 /* === E N D =============================================================== */
00173 
00174 #endif

Generated on Fri Jun 23 15:20:50 2006 for synfig by  doxygen 1.4.6