Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

glrectangle.h

00001 /*
00002 Copyright (c) 2000-2003 Lee Thomason (www.grinninglizard.com)
00003 Grinning Lizard Utilities.
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to anyone to use this software for any 
00010 purpose, including commercial applications, and to alter it and 
00011 redistribute it freely, subject to the following restrictions:
00012 
00013 1. The origin of this software must not be misrepresented; you must 
00014 not claim that you wrote the original software. If you use this 
00015 software in a product, an acknowledgment in the product documentation 
00016 would be appreciated but is not required.
00017 
00018 2. Altered source versions must be plainly marked as such, and 
00019 must not be misrepresented as being the original software.
00020 
00021 3. This notice may not be removed or altered from any source 
00022 distribution.
00023 */
00024 
00025 
00026 #ifndef GRINLIZ_RECTANGLE_INCLUDED
00027 #define GRINLIZ_RECTANGLE_INCLUDED
00028 
00029 //#include "gldebug.h"
00030 #include "glvector.h"
00031 #include "limits.h"
00032 
00033 namespace grinliz {
00034 
00037 template< class T >
00038 struct Rectangle2
00039 {
00040         Vector2< T > min;
00041         Vector2< T > max;
00042 
00044         void Set( T _xmin, T _ymin, T _xmax, T _ymax )  { 
00045                 min.x = _xmin; min.y = _ymin; max.x = _xmax; max.y = _ymax;
00046         }
00048         void Zero() {
00049                 min.x = min.y = max.x = max.y = (T) 0;
00050         }
00051 
00055         void FromPair( T x0, T y0, T x1, T y1 )
00056         {
00057                 min.x = grinliz::Min( x0, x1 );
00058                 max.y = grinliz::Max( x0, x1 );
00059                 min.y = grinliz::Min( y0, y1 );
00060                 max.y = grinliz::Max( y0, y1 );
00061         }
00062 
00064         bool Intersect( const Rectangle2<T>& rect ) const
00065         {
00066                 if (    rect.max.x < min.x
00067                          || rect.min.x > max.x
00068                          || rect.max.y < min.y
00069                          || rect.min.y > max.y )
00070                 {
00071                         return false;
00072                 }
00073                 return true;
00074         }       
00075 
00076         bool Intersect( const Vector2<T>& point ) const
00077         {
00078                 if (    point.x < min.x
00079                          || point.x > max.x
00080                          || point.y < min.y
00081                          || point.y > max.y )
00082                 {
00083                         return false;
00084                 }
00085                 return true;
00086         }
00087 
00088         bool Intersect( T x, T y ) const
00089         {
00090                 if (    x < min.x
00091                          || x > max.x
00092                          || y < min.y
00093                          || y > max.y )
00094                 {
00095                         return false;
00096                 }
00097                 return true;
00098         }
00099 
00100 
00102         bool Contains( const Rectangle2<T>& rect ) const
00103         {
00104                 if (    rect.min.x >= min.x
00105                          && rect.max.x <= max.x
00106                          && rect.min.y >= min.y
00107                          && rect.max.y <= max.y )
00108                 {
00109                         return true;
00110                 }
00111                 return false;
00112         }
00113 
00114         bool Contains( const Vector2<T>& point ) const
00115         {
00116                 if (    point.x >= min.x
00117                          && point.x <= max.x
00118                          && point.y >= min.y
00119                          && point.y <= max.y )
00120                 {
00121                         return true;
00122                 }
00123                 return false;
00124         }
00125 
00127         void DoUnion( const Rectangle2<T>& rect )
00128         {
00129                 min.x = grinliz::Min( min.x, rect.min.x );
00130                 max.x = grinliz::Max( max.x, rect.max.x );
00131                 min.y = grinliz::Min( min.y, rect.min.y );
00132                 max.y = grinliz::Max( max.y, rect.max.y );
00133         }
00134 
00136         void DoUnion( T x, T y )
00137         {
00138                 min.x = grinliz::Min( min.x, x );
00139                 max.x = grinliz::Max( max.x, x );
00140                 min.y = grinliz::Min( min.y, y );
00141                 max.y = grinliz::Max( max.y, y );
00142         }
00143  
00145         void DoIntersection( const Rectangle2<T>& rect )
00146         {
00147                 min.x = grinliz::Max( min.x, rect.min.x );
00148                 max.x = grinliz::Min( max.x, rect.max.x );
00149                 min.y = grinliz::Max( min.y, rect.min.y );
00150                 max.y = grinliz::Min( max.y, rect.max.y );
00151         }
00152 
00154         void DoClip( const Rectangle2<T>& rect )
00155         {
00156                 min.x = rect.min.x > min.x ? rect.min.x : min.x;
00157                 max.x = rect.max.x < max.x ? rect.max.x : max.x;
00158                 min.y = rect.min.y > min.y ? rect.min.y : min.y;
00159                 max.y = rect.max.y < max.y ? rect.max.y : max.y;
00160         }
00161 
00162 
00164         void Scale( T x, T y )
00165         {
00166                 min.x = ( x * min.x );
00167                 min.y = ( y * min.y );
00168                 max.x = ( x * max.x );
00169                 max.y = ( y * max.y );
00170         }
00171 
00173         void EdgeAdd( T i )
00174         {
00175                 min.x -= i;
00176                 max.x += i;
00177                 min.y -= i;
00178                 max.y += i;
00179         }
00180 
00181         bool operator==( const Rectangle2<T>& that ) const { return     ( min.x == that.min.x )
00182                                                                                                         && ( max.x == that.max.x )
00183                                                                                                         && ( min.y == that.min.y )
00184                                                                                                         && ( max.y == that.max.y ); }
00185         bool operator!=( const Rectangle2<T>& that ) const { return     ( min.x != that.min.x )
00186                                                                                                         || ( max.x != that.max.x )
00187                                                                                                         || ( min.y != that.min.y )
00188                                                                                                         || ( max.y != that.max.y ); }
00189 
00190 };
00191 
00192 
00193 struct Rectangle2I : public Rectangle2< int >
00194 {
00195         enum { INVALID = INT_MIN };
00196 
00197         int Width()      const  { return max.x - min.x + 1; }           
00198         int Height() const      { return max.y - min.y + 1; }           
00199         int Area()   const      { return Width() * Height();    }   
00200 
00202         void SetInvalid()       { min.x = INVALID + 1; max.x = INVALID; min.y = INVALID + 1; max.y = INVALID; }
00203 
00205         bool IsValid() const {
00206                 return ( min.x <= max.x ) && ( min.y <= max.y );
00207         }
00208 };
00209 
00210 struct Rectangle2F : public Rectangle2< float >
00211 {
00212         float Width()    const  { return max.x - min.x; }               
00213         float Height() const    { return max.y - min.y; }               
00214         float Area()   const    { return Width() * Height();    }   
00215 };
00216 
00217 
00218 template< class T >
00219 struct Rectangle3
00220 {
00221         Vector3< T > min;
00222         Vector3< T > max;
00223 
00225         void Set( T _xmin, T _ymin, T _zmin, T _xmax, T _ymax, T _zmax )        { 
00226                 min.x = _xmin; min.y = _ymin; min.z = _zmin; 
00227                 max.x = _xmax; max.y = _ymax; max.z = _zmax;
00228         }
00230         void Zero() {
00231                 min.x = min.y = max.x = max.y = min.z = max.z = (T) 0;
00232         }
00233 
00235         bool Intersect( const Rectangle3<T>& rect ) const
00236         {
00237                 if (    rect.max.x < min.x
00238                          || rect.min.x > max.x
00239                          || rect.max.y < min.y
00240                          || rect.min.y > max.y
00241                          || rect.max.z < min.z
00242                          || rect.min.z > max.z )
00243                 {
00244                         return false;
00245                 }
00246                 return true;
00247         }       
00248 
00249         bool Intersect( const Vector3<T>& point ) const
00250         {
00251                 if (    point.x < min.x
00252                          || point.x > max.x
00253                          || point.y < min.y
00254                          || point.y > max.y
00255                          || point.z < min.z
00256                          || point.z > max.z )
00257                 {
00258                         return false;
00259                 }
00260                 return true;
00261         }
00262 
00263         bool Intersect( T x, T y, T z ) const
00264         {
00265                 if (    x < min.x
00266                          || x > max.x
00267                          || y < min.y
00268                          || y > max.y
00269                          || z < min.z
00270                          || z > max.z )
00271                 {
00272                         return false;
00273                 }
00274                 return true;
00275         }
00276 
00277 
00279         bool Contains( const Rectangle3<T>& rect ) const
00280         {
00281                 if (    rect.min.x >= min.x
00282                          && rect.max.x <= max.x
00283                          && rect.min.y >= min.y
00284                          && rect.max.y <= max.y
00285                          && rect.min.z >= min.z
00286                          && rect.max.z <= max.z )
00287                 {
00288                         return true;
00289                 }
00290                 return false;
00291         }
00292 
00293         bool Contains( const Vector3<T>& point ) const
00294         {
00295                 if (    point.x >= min.x
00296                          && point.x <= max.x
00297                          && point.y >= min.y
00298                          && point.y <= max.y
00299                          && point.z >= min.z
00300                          && point.z <= max.z )
00301                 {
00302                         return true;
00303                 }
00304                 return false;
00305         }
00306 
00308         void DoUnion( const Rectangle3<T>& rect )
00309         {
00310                 min.x = grinliz::Min( min.x, rect.min.x );
00311                 max.x = grinliz::Max( max.x, rect.max.x );
00312                 min.y = grinliz::Min( min.y, rect.min.y );
00313                 max.y = grinliz::Max( max.y, rect.max.y );
00314                 min.z = grinliz::Min( min.z, rect.min.z );
00315                 max.z = grinliz::Max( max.z, rect.max.z );
00316         }
00317 
00319         void DoUnion( const Vector3<T>& vec )
00320         {
00321                 min.x = grinliz::Min( min.x, vec.x );
00322                 max.x = grinliz::Max( max.x, vec.x );
00323                 min.y = grinliz::Min( min.y, vec.y );
00324                 max.y = grinliz::Max( max.y, vec.y );
00325                 min.z = grinliz::Min( min.z, vec.z );
00326                 max.z = grinliz::Max( max.z, vec.z );
00327         }
00328         
00330         void DoIntersection( const Rectangle2<T>& rect )
00331         {
00332                 min.x = grinliz::Max( min.x, rect.min.x );
00333                 max.x = grinliz::Min( max.x, rect.max.x );
00334                 min.y = grinliz::Max( min.y, rect.min.y );
00335                 max.y = grinliz::Min( max.y, rect.max.y );
00336                 min.z = grinliz::Max( min.z, rect.min.z );
00337                 max.z = grinliz::Min( max.z, rect.max.z );
00338         }
00339 
00341         void DoClip( const Rectangle3<T>& rect )
00342         {
00343                 min.x = rect.min.x > min.x ? rect.min.x : min.x;
00344                 max.x = rect.max.x < max.x ? rect.max.x : max.x;
00345                 min.y = rect.min.y > min.y ? rect.min.y : min.y;
00346                 max.y = rect.max.y < max.y ? rect.max.y : max.y;
00347                 min.z = rect.min.z > min.z ? rect.min.z : min.z;
00348                 max.z = rect.max.z < max.z ? rect.max.z : max.z;
00349         }
00350 
00351 
00353         void Scale( T x, T y, T z )
00354         {
00355                 min.x = ( x * min.x );
00356                 max.x = ( x * max.x );
00357                 min.y = ( y * min.y );
00358                 max.y = ( y * max.y );
00359                 min.z = ( z * min.z );
00360                 max.z = ( z * max.z );
00361         }
00362 
00364         void EdgeAdd( T i )
00365         {
00366                 min.x -= i;
00367                 max.x += i;
00368                 min.y -= i;
00369                 max.y += i;
00370                 min.z -= i;
00371                 max.z += i;
00372         }
00373 
00374         bool operator==( const Rectangle2<T>& that ) const { return     ( min.x == that.min.x )
00375                                                                                                                                 && ( max.x == that.max.x )
00376                                                                                                                                 && ( min.y == that.min.y )
00377                                                                                                                                 && ( max.y == that.max.y )
00378                                                                                                                                 && ( min.z == that.min.z )
00379                                                                                                                                 && ( max.z == that.max.z ); }
00380         bool operator!=( const Rectangle2<T>& that ) const { return     ( min.x != that.min.x )
00381                                                                                                                                 || ( max.x != that.max.x )
00382                                                                                                                                 || ( min.y != that.min.y )
00383                                                                                                                                 || ( max.y != that.max.y )
00384                                                                                                                                 || ( min.z != that.min.z )
00385                                                                                                                                 || ( max.z != that.max.z ); }
00386 
00387 };
00388 
00389 
00390 
00391 struct Rectangle3I : public Rectangle3< int >
00392 {
00393         enum { INVALID = INT_MIN };
00394 
00395         int SizeX()      const  { return max.x - min.x + 1; }           
00396         int SizeY() const       { return max.y - min.y + 1; }           
00397         int SizeZ()  const  { return max.z - min.z + 1; }
00398         int Volume() const      { return SizeX() * SizeY() * SizeZ();   }   
00399         int Size( int i ) const { return max.X(i) - min.X(i) + 1; }
00400         #ifdef DEBUG
00401         void Dump() { GLOUTPUT(( "(%d,%d,%d)-(%d,%d,%d)", min.x, min.y, min.z, max.x, max.y, max.z )); }
00402         #endif
00403 };
00404 
00405 
00406 struct Rectangle3F : public Rectangle3< float >
00407 {
00408         float SizeX() const     { return max.x - min.x; }               
00409         float SizeY() const             { return max.y - min.y; }               
00410         float SizeZ() const             { return max.z - min.z; }               
00411         float Volume()const             { return SizeX() * SizeY() * SizeZ();   }   
00412         float Size( int i ) const { return max.X(i) - min.X(i); }
00413 
00414         #ifdef DEBUG
00415         void Dump() { 
00416                 GLOUTPUT(( "(%.1f,%.1f,%.1f)-(%.1f,%.1f,%.1f)", min.x, min.y, min.z, max.x, max.y, max.z )); 
00417         }
00418         #endif
00419 };
00420 
00421 
00422 };      // namespace grinliz
00423 
00424 
00425 
00426 #endif
00427 

Generated on Sun Sep 25 16:25:49 2005 for Kyra by  doxygen 1.4.3