00001
00008
00009
00010
00011
00012
00013 #ifndef GFX_TYPES_HH
00014 #define GFX_TYPES_HH 1
00015
00016 #include "gfx_types_vertex.hh"
00017
00018 namespace gfx {
00019
00020
00021 #define virtual VIRTUAL_METHODS_IN_BASIC_TYPES_IS_TOO_SLOW
00022
00026 template<typename T>
00027 struct WidthHeight
00028 {
00029 WidthHeight( void ) : w(0), h(0) { }
00030 WidthHeight( T W, T H ) : w(W), h(H) { }
00031 T w, h;
00032 };
00033
00041 template<typename T>
00042 class Rect
00043 {
00044 public:
00045 Rect( void )
00046 : x1(0), y1(0), x2(0), y2(0)
00047 {
00048
00049 }
00050
00051 Rect( T X1, T Y1, T X2, T Y2 )
00052
00053 {
00054 Set( X1, Y1, X2, Y2 );
00055 }
00056
00057 void Set( T X1, T Y1, T X2, T Y2 )
00058 {
00059
00060 x1 = MIN( X1, X2 );
00061 y1 = MIN( Y1, Y2 );
00062 x2 = MAX( X1, X2 );
00063 y2 = MAX( Y1, Y2 );
00064 }
00065
00066 bool IfZero( void ) const
00067 {
00068
00069 return (ABS(x1) + ABS(y1) + ABS(x2) + ABS(y2)) == 0;
00070 }
00071
00073 bool IfInside( T x, T y ) const
00074 {
00075 return x >= x1
00076 && x <= x2
00077 && y >= y1
00078 && y <= y2;
00079 }
00080
00081 bool IfInside( const Vector2& v ) const
00082 {
00083 return IfInside( T(v.x), T(v.y) );
00084 }
00085
00087 bool IfOutside( const Rect<T>& rect ) const
00088 {
00089 return x2 < rect.x1
00090 || x1 > rect.x2
00091 || y1 > rect.y2
00092 || y2 < rect.y1;
00093 }
00094
00096 bool IfOverlaps( const Rect& rect ) const
00097 {
00098
00099 return not IfOutside( rect );
00100 }
00101
00103 void Grow( T x, T y )
00104 {
00105
00106 x1 = MIN(x1,x);
00107 y1 = MIN(y1,y);
00108 x2 = MAX(x2,x);
00109 y2 = MAX(y2,y);
00110 }
00111
00112 T GetWidth( void ) const
00113 {
00114 return x2 - x1;
00115 }
00116
00117 T GetHeight( void ) const
00118 {
00119 return y2 - y1;
00120 }
00121
00122 public:
00123 T x1;
00124 T y1;
00125 T x2;
00126 T y2;
00127 };
00128
00129 template<typename T>
00130 bool operator==( const Rect<T>& a, const Rect<T>& b )
00131 {
00132 return a.x1 == b.x1
00133 && a.y1 == b.y1
00134 && a.x2 == b.x2
00135 && a.y2 == b.y2;
00136 }
00137
00138 template<typename T>
00139 bool operator!=( const Rect<T>& a, const Rect<T>& b )
00140 {
00141 return ! (a == b);
00142 }
00143
00144 template<typename T>
00145 bool operator<( const Rect<T>& a, const Rect<T>& b )
00146 {
00147 RETURN_LT_TRUE_GT_FALSE( a.x1, b.x1 )
00148 RETURN_LT_TRUE_GT_FALSE( a.y1, b.y1 )
00149 RETURN_LT_TRUE_GT_FALSE( a.x2, b.x2 )
00150 RETURN_LT_TRUE_GT_FALSE( a.y2, b.y2 )
00151 return false;
00152 }
00153
00154 template<typename T>
00155 ostream&
00156 operator<<( ostream& strm, const Rect<T>& o )
00157 {
00158 ios::fmtflags savedFlags = strm.flags();
00159 strm << '{'
00160 << "x1=" << OMANIP_FIELD(0) << o.x1 << ','
00161 << "y1=" << OMANIP_FIELD(0) << o.y1 << ','
00162 << "x2=" << OMANIP_FIELD(0) << o.x2 << ','
00163 << "y2=" << OMANIP_FIELD(0) << o.y2 << '}';
00164 strm.flags(savedFlags);
00165 return strm;
00166 }
00167
00168 #undef virtual
00169
00170 }
00171
00172 #endif // GFX_TYPES_HH