gfx_types.hh

Go to the documentation of this file.
00001 /*
00008  * LEGAL:   COPYRIGHT (C) 2004 JIM E. BROOKS
00009  *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
00010  *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
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 // Basic types must be fast.  Virtual functions incur overhead from C++ vtable.
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         // NOP
00049     }
00050 
00051     Rect( T X1, T Y1, T X2, T Y2 )
00052 //  :   x1(X1), y1(Y1), x2(X2), y2(Y2)  // use Set() which can correct args
00053     {
00054         Set( X1, Y1, X2, Y2 );
00055     }
00056 
00057     void Set( T X1, T Y1, T X2, T Y2 )
00058     {
00059         // Try to maintain assumption that (x1,y1) < (x2,y2).
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       //return (x1 + y1 + x2 + y2) == 0;  // pos/neg values could cancel each other
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   // assumes (x1,y1) < (x2,y2)
00076                && x <= x2   // this could be recoded to not assume
00077                && y >= y1   // but it'd be slower
00078                && y <= y2;
00079     }
00080 
00081     bool IfInside( const Vector2& v ) const
00082     {
00083         return IfInside( T(v.x), T(v.y) );  // cast to suppress warning
00084     }
00085 
00087     bool IfOutside( const Rect<T>& rect ) const
00088     {
00089         return x2 < rect.x1     // if this right side is left of other
00090             || x1 > rect.x2     // if this left side is right of other
00091             || y1 > rect.y2     // if top of this is below other
00092             || y2 < rect.y1;    // if bottom of this is above other
00093     }
00094 
00096     bool IfOverlaps( const Rect& rect ) const
00097     {
00098         // Not outside means entirely inside or partially inside (overlap).
00099         return not IfOutside( rect );
00100     }
00101 
00103     void Grow( T x, T y )
00104     {
00105         // Try to maintain assumption that (x1,y1) < (x2,y2).
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;  // right - left
00115     }
00116 
00117     T GetHeight( void ) const
00118     {
00119         return y2 - y1;  // bottom - top
00120     }
00121 
00122 public:
00123     T   x1;  // smallest x (NW)
00124     T   y1;  // smallest y
00125     T   x2;  // greatest x (SE)
00126     T   y2;  // greatest y
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;  // equal
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 } // namespace gfx
00171 
00172 #endif // GFX_TYPES_HH
Palomino 3D Engine documents generated by doxygen 1.5.3 on Fri Nov 23 11:26:11 2007