00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __UT_Rect_h__
00023 #define __UT_Rect_h__
00024
00025 #include <math.h>
00026 #include <SYS/SYS_Types.h>
00027 #include <iostream.h>
00028 #include "UT_Algorithm.h"
00029
00030
00031 inline int UTinclusiveFlip( int v, int d ) { return d - 1 - v; }
00032
00033 class UT_DimRectImpl;
00034 class UT_InclusiveRectImpl;
00035 class UT_ExclusiveRectImpl;
00036
00037 template <typename T> class UT_Rect;
00038
00039 typedef UT_Rect<UT_DimRectImpl> UT_DimRect;
00040 typedef UT_Rect<UT_InclusiveRectImpl> UT_InclusiveRect;
00041 typedef UT_Rect<UT_ExclusiveRectImpl> UT_ExclusiveRect;
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 template <typename T> class UT_Rect
00059 {
00060 public:
00061 UT_Rect() { clear(); }
00062 UT_Rect( int v1, int v2, int v3, int v4 ) { set( v1, v2, v3, v4 ); }
00063
00064 UT_Rect( const UT_DimRect &r ) : _r( r ) { }
00065 UT_Rect( const UT_InclusiveRect &r ) : _r( r ) { }
00066 UT_Rect( const UT_ExclusiveRect &r ) : _r( r ) { }
00067
00068 UT_Rect &operator=( const UT_DimRect &r ) { _r = r; return *this; }
00069 UT_Rect &operator=( const UT_InclusiveRect &r ) { _r = r; return *this; }
00070 UT_Rect &operator=( const UT_ExclusiveRect &r ) { _r = r; return *this; }
00071
00072
00073 void clear() { _r.clear(); }
00074
00075
00076 void set( int v1, int v2, int v3, int v4 ) { _r.set(v1, v2, v3, v4); }
00077
00078
00079 bool isEmpty() const { return (width() == 0 || height() == 0); }
00080
00081
00082 bool isValid() const { return _r.isValid(); }
00083
00084
00085 bool isInsideX( int px ) const { return (x1() <= px && px <= x2()); }
00086 bool isInsideY( int py ) const { return (y1() <= py && py <= y2()); }
00087
00088
00089 bool isInside( int px, int py ) const { return isInsideX(px) &&
00090 isInsideY(py); }
00091
00092
00093 bool contains( const T &r) const;
00094
00095
00096 void getRelative( int &px, int &py ) const { px -= x1(); py -= y1(); }
00097
00098
00099 void getAbsolute( int &px, int &py ) const { px += x1(); py += y1(); }
00100
00101
00102 void flipX(int awidth) { _r.flipX( awidth ); }
00103 void flipY(int aheight) { _r.flipY( aheight ); }
00104
00105
00106 bool intersect( const T &r );
00107
00108
00109
00110 bool overlapX( const T &r ) const;
00111 bool overlapY( const T &r ) const;
00112
00113
00114 void enlarge( const T &r );
00115
00116
00117 void enlarge( int x, int y );
00118 void enlarge( float x, float y);
00119
00120
00121 void translate( int x, int y );
00122
00123
00124 void inset( int dx, int dy );
00125
00126
00127 void inset( int dx1, int dy1, int dx2, int dy2 );
00128
00129
00130 void scale( fpreal factor );
00131
00132
00133 void clamp( int &px, int &py ) const
00134 {
00135 if (px < x1())
00136 px = x1();
00137 else if (px > x2())
00138 px = x2();
00139
00140 if (py < y1())
00141 py = y1();
00142 else if (py > y2())
00143 py = y2();
00144 }
00145
00146
00147 int x() const { return _r.x(); }
00148 int y() const { return _r.y(); }
00149 int w() const { return _r.width(); }
00150 int h() const { return _r.height(); }
00151 int width() const { return _r.width(); }
00152 int height() const { return _r.height(); }
00153
00154 int x1() const { return _r.x1(); }
00155 int y1() const { return _r.y1(); }
00156 int x2() const { return _r.x2(); }
00157 int y2() const { return _r.y2(); }
00158 int x2e() const { return _r.x2e(); }
00159 int y2e() const { return _r.y2e(); }
00160
00161
00162 void setWidth(int awidth) { _r.setWidth(awidth); }
00163 void setHeight(int aheight) { _r.setHeight(aheight); }
00164
00165
00166 void setX(int x_) { _r.setX(x_); }
00167 void setY(int y_) { _r.setY(y_); }
00168
00169
00170 void setX1(int x_) { _r.setX1(x_); }
00171 void setY1(int y_) { _r.setY1(y_); }
00172 void setX2(int x_) { _r.setX2(x_); }
00173 void setY2(int y_) { _r.setY2(y_); }
00174 void setX2e(int x_) { _r.setX2e(x_); }
00175 void setY2e(int y_) { _r.setY2e(y_); }
00176
00177
00178
00179
00180
00181
00182
00183 void getDim(int &x_, int &y_, int &w_, int &h_) const
00184 { x_ = x(); y_ = y(); w_ = width(); h_ = height(); }
00185 void getIncl(int &x1_, int &y1_, int &x2_, int &y2_) const
00186 { x1_ = x(); y1_ = y(); x2_ = x2(); y2_ = y2(); }
00187 void getExcl(int &x1_, int &y1_, int &x2_, int &y2_) const
00188 { x1_ = x(); y1_ = y(); x2_ = x2e(); y2_ = y2e(); }
00189
00190
00191
00192 void setDim(int x_, int y_, int w_, int h_)
00193 { setX(x_); setY(y_); setWidth(w_); setHeight(h_); }
00194 void setIncl(int x1_, int y1_, int x2_, int y2_)
00195 { setX1(x1_); setX2(x2_); setY1(y1_); setY2(y2_); }
00196 void setExcl(int x1_, int y1_, int x2_, int y2_)
00197 { setX1(x1_); setX2e(x2_); setY1(y1_); setY2e(y2_); }
00198
00199
00200 int &operator()(int i) { return _r(i); }
00201 int operator()(int i) const { return _r(i); }
00202
00203 const int *data() const { return _r.data(); }
00204 int *data() { return _r.data(); }
00205
00206 private:
00207 T _r;
00208 };
00209
00210
00211
00212
00213
00214
00215 class UT_BaseRectImpl
00216 {
00217 public:
00218 UT_BaseRectImpl() { }
00219 UT_BaseRectImpl( int v0, int v1, int v2, int v3 )
00220 { set( v0, v1, v2, v3 ); }
00221 UT_BaseRectImpl( const UT_BaseRectImpl &r )
00222 { set( r.myVals[0], r.myVals[1], r.myVals[2], r.myVals[3] ); }
00223
00224 UT_BaseRectImpl &operator=( const UT_BaseRectImpl &r )
00225 {
00226 if( &r != this )
00227 set( r.myVals[0], r.myVals[1], r.myVals[2], r.myVals[3] );
00228 return *this;
00229 }
00230
00231 void set( int v0, int v1, int v2, int v3 )
00232 { myVals[0] = v0; myVals[1] = v1; myVals[2] = v2; myVals[3] = v3; }
00233
00234 int &operator()(int i) { return myVals[i]; }
00235 int operator()(int i) const { return myVals[i]; }
00236
00237 const int * data() const { return myVals; }
00238 int * data() { return myVals; }
00239
00240 protected:
00241
00242 int I2D( int v1, int v2 ) const { return v2 - v1 + 1; }
00243 int D2I( int v, int d ) const { return v + d - 1; }
00244 int E2D( int v1, int v2 ) const { return v2 - v1; }
00245 int D2E( int v, int d ) const { return v + d; }
00246 int E2I( int v2 ) const { return v2 - 1; }
00247 int I2E( int v2 ) const { return v2 + 1; }
00248
00249
00250 static int flipI( int v, int d ) { return UTinclusiveFlip( v, d ); }
00251
00252 protected:
00253 int myVals[4];
00254 };
00255
00256 class UT_DimRectImpl : public UT_BaseRectImpl
00257 {
00258 public:
00259 UT_DimRectImpl() { }
00260 UT_DimRectImpl( const UT_DimRect &r );
00261 UT_DimRectImpl( const UT_InclusiveRect &r );
00262 UT_DimRectImpl( const UT_ExclusiveRect &r );
00263
00264 UT_DimRectImpl &operator=( const UT_DimRect &r );
00265 UT_DimRectImpl &operator=( const UT_InclusiveRect &r );
00266 UT_DimRectImpl &operator=( const UT_ExclusiveRect &r );
00267
00268
00269 void clear() { set( 0, 0, 0, 0 ); }
00270
00271
00272 bool isValid() const { return width() >= 0 && height() >= 0; }
00273
00274
00275 void flipX(int w) { myVals[0] = flipI(x2(), w); }
00276 void flipY(int h) { myVals[1] = flipI(y2(), h); }
00277
00278
00279 int x() const { return myVals[0]; }
00280 int y() const { return myVals[1]; }
00281 int width() const { return myVals[2]; }
00282 int height() const { return myVals[3]; }
00283
00284 int x1() const { return x(); }
00285 int y1() const { return y(); }
00286 int x2() const { return D2I(x(), width()); }
00287 int y2() const { return D2I(y(), height()); }
00288 int x2e() const { return I2E(x2()); }
00289 int y2e() const { return I2E(y2()); }
00290
00291 void setWidth(int w) { myVals[2] = w; }
00292 void setHeight(int h) { myVals[3] = h; }
00293
00294 void setX(int x_) { myVals[0] = x_; }
00295 void setY(int y_) { myVals[1] = y_; }
00296 void setX1(int x_) { myVals[2] += myVals[0]-x_; myVals[0] = x_; }
00297 void setY1(int y_) { myVals[3] += myVals[1]-y_; myVals[1] = y_; }
00298 void setX2(int x_) { myVals[2] = I2D(myVals[0], x_); }
00299 void setY2(int y_) { myVals[3] = I2D(myVals[1], y_); }
00300 void setX2e(int x_) { myVals[2] = E2D(myVals[0], x_); }
00301 void setY2e(int y_) { myVals[3] = E2D(myVals[1], y_); }
00302 };
00303
00304
00305 class UT_InclusiveRectImpl : public UT_BaseRectImpl
00306 {
00307 public:
00308 UT_InclusiveRectImpl() { }
00309 UT_InclusiveRectImpl( const UT_DimRect &r );
00310 UT_InclusiveRectImpl( const UT_InclusiveRect &r );
00311 UT_InclusiveRectImpl( const UT_ExclusiveRect &r );
00312
00313 UT_InclusiveRectImpl &operator=( const UT_DimRect &r );
00314 UT_InclusiveRectImpl &operator=( const UT_InclusiveRect &r );
00315 UT_InclusiveRectImpl &operator=( const UT_ExclusiveRect &r );
00316
00317
00318 void clear() { set( 0, 0, -1, -1 ); }
00319
00320
00321 bool isValid() const { return (x1() <= x2e()) && (y1() <= y2e()); }
00322
00323
00324 void flipX( int w )
00325 {
00326 myVals[0] = flipI(x1(), w);
00327 myVals[2] = flipI(x2(), w);
00328 UTswap(myVals[0], myVals[2]);
00329 }
00330 void flipY( int h )
00331 {
00332 myVals[1] = flipI(y1(), h);
00333 myVals[3] = flipI(y2(), h);
00334 UTswap(myVals[1], myVals[3]);
00335 }
00336
00337
00338 int x1() const { return myVals[0]; }
00339 int y1() const { return myVals[1]; }
00340 int x2() const { return myVals[2]; }
00341 int y2() const { return myVals[3]; }
00342 int x2e() const { return I2E(x2()); }
00343 int y2e() const { return I2E(y2()); }
00344
00345 int x() const { return x1(); }
00346 int y() const { return y1(); }
00347 int width() const { return I2D(x1(), x2()); }
00348 int height() const { return I2D(y1(), y2()); }
00349
00350 void setWidth(int w) { myVals[2] = D2I(myVals[0], w); }
00351 void setHeight(int h) { myVals[3] = D2I(myVals[1], h); }
00352
00353 void setX(int x_) { myVals[2] = x_ + width()-1; myVals[0] = x_; }
00354 void setY(int y_) { myVals[3] = y_ +height()-1; myVals[1] = y_; }
00355 void setX1(int x_) { myVals[0] = x_; }
00356 void setY1(int y_) { myVals[1] = y_; }
00357 void setX2(int x_) { myVals[2] = x_; }
00358 void setY2(int y_) { myVals[3] = y_; }
00359 void setX2e(int x_) { myVals[2] = E2I(x_); }
00360 void setY2e(int y_) { myVals[3] = E2I(y_); }
00361 };
00362
00363 class UT_ExclusiveRectImpl : public UT_BaseRectImpl
00364 {
00365 public:
00366 UT_ExclusiveRectImpl() { }
00367 UT_ExclusiveRectImpl( const UT_DimRect &r );
00368 UT_ExclusiveRectImpl( const UT_InclusiveRect &r );
00369 UT_ExclusiveRectImpl( const UT_ExclusiveRect &r );
00370
00371 UT_ExclusiveRectImpl &operator=( const UT_DimRect &r );
00372 UT_ExclusiveRectImpl &operator=( const UT_InclusiveRect &r );
00373 UT_ExclusiveRectImpl &operator=( const UT_ExclusiveRect &r );
00374
00375
00376 void clear() { set( 0, 0, 0, 0 ); }
00377
00378
00379 bool isValid() const { return x1() <= x2e() && y1() <= y2e(); }
00380
00381
00382 void flipX( int w )
00383 {
00384 myVals[0] = I2E(flipI(x1(), w));
00385 myVals[2] = flipI(x2(), w);
00386 UTswap(myVals[0], myVals[2]);
00387 }
00388 void flipY( int h )
00389 {
00390 myVals[1] = I2E(flipI(y1(), h));
00391 myVals[3] = flipI(y2(), h);
00392 UTswap(myVals[1], myVals[3]);
00393 }
00394
00395
00396 int x1() const { return myVals[0]; }
00397 int y1() const { return myVals[1]; }
00398 int x2e() const { return myVals[2]; }
00399 int y2e() const { return myVals[3]; }
00400 int x2() const { return E2I(x2e()); }
00401 int y2() const { return E2I(y2e()); }
00402
00403 int x() const { return x1(); }
00404 int y() const { return y1(); }
00405 int width() const { return E2D(x1(), x2e()); }
00406 int height() const { return E2D(y1(), y2e()); }
00407
00408 void setWidth(int w) { myVals[2] = D2E(myVals[0], w); }
00409 void setHeight(int h) { myVals[3] = D2E(myVals[1], h); }
00410
00411 void setX(int x_) { myVals[2] = x_ + width(); myVals[0] = x_; }
00412 void setY(int y_) { myVals[3] = y_ + height(); myVals[1] = y_; }
00413 void setX1(int x_) { myVals[0] = x_; }
00414 void setY1(int y_) { myVals[1] = y_; }
00415 void setX2(int x_) { myVals[2] = I2E(x_); }
00416 void setY2(int y_) { myVals[3] = I2E(y_); }
00417 void setX2e(int x_) { myVals[2] = x_; }
00418 void setY2e(int y_) { myVals[3] = y_; }
00419 };
00420
00421 inline ostream &operator<<(ostream &os, const UT_DimRect &r)
00422 {
00423 os << "[" << r.x() << ","<< r.y() << "]-[" << r.x2() << "," << r.y2()
00424 << "] (" << r.w() << "x" << r.h() << ")";
00425 return os;
00426 }
00427 inline ostream &operator<<(ostream &os, const UT_InclusiveRect &r)
00428 {
00429 os << "[" << r.x() << ","<< r.y() << "]-[" << r.x2() << "," << r.y2()
00430 << "] (" << r.w() << "x" << r.h() << ")";
00431 return os;
00432 }
00433 inline ostream &operator<<(ostream &os, const UT_ExclusiveRect &r)
00434 {
00435 os << "[" << r.x() << ","<< r.y() << "]-[" << r.x2() << "," << r.y2()
00436 << "] (" << r.w() << "x" << r.h() << ")";
00437 return os;
00438 }
00439
00440
00441
00442
00443
00444 #define UT_RECT_OP_EQUALS(T1,T2) \
00445 inline bool operator==( const T1 &r1, const T2 &r2 ) \
00446 { return (r1.x() == r2.x() && r1.y() == r2.y() \
00447 && r1.width() == r2.width() \
00448 && r1.height() == r2.height()); }
00449
00450 UT_RECT_OP_EQUALS(UT_DimRect,UT_DimRect)
00451 UT_RECT_OP_EQUALS(UT_DimRect,UT_InclusiveRect)
00452 UT_RECT_OP_EQUALS(UT_DimRect,UT_ExclusiveRect)
00453 UT_RECT_OP_EQUALS(UT_InclusiveRect,UT_DimRect)
00454 UT_RECT_OP_EQUALS(UT_InclusiveRect,UT_InclusiveRect)
00455 UT_RECT_OP_EQUALS(UT_InclusiveRect,UT_ExclusiveRect)
00456 UT_RECT_OP_EQUALS(UT_ExclusiveRect,UT_DimRect)
00457 UT_RECT_OP_EQUALS(UT_ExclusiveRect,UT_InclusiveRect)
00458 UT_RECT_OP_EQUALS(UT_ExclusiveRect,UT_ExclusiveRect)
00459 #undef UT_RECT_OP_EQUALS
00460
00461 #define UT_RECT_OP_NOT_EQUALS(T1,T2) \
00462 inline bool operator!=( const T1 &r1, const T2 &r2 ) \
00463 { return !(r1 == r2); }
00464
00465 UT_RECT_OP_NOT_EQUALS(UT_DimRect,UT_DimRect)
00466 UT_RECT_OP_NOT_EQUALS(UT_DimRect,UT_InclusiveRect)
00467 UT_RECT_OP_NOT_EQUALS(UT_DimRect,UT_ExclusiveRect)
00468 UT_RECT_OP_NOT_EQUALS(UT_InclusiveRect,UT_DimRect)
00469 UT_RECT_OP_NOT_EQUALS(UT_InclusiveRect,UT_InclusiveRect)
00470 UT_RECT_OP_NOT_EQUALS(UT_InclusiveRect,UT_ExclusiveRect)
00471 UT_RECT_OP_NOT_EQUALS(UT_ExclusiveRect,UT_DimRect)
00472 UT_RECT_OP_NOT_EQUALS(UT_ExclusiveRect,UT_InclusiveRect)
00473 UT_RECT_OP_NOT_EQUALS(UT_ExclusiveRect,UT_ExclusiveRect)
00474 #undef UT_RECT_OP_NOT_EQUALS
00475
00476 #define UT_RECT_COPY_TO_DIM(T1,T2) \
00477 inline T1::T1( const T2 &r ) \
00478 : UT_BaseRectImpl( r.x(), r.y(), r.width(), r.height() ) { } \
00479 inline T1 & T1::operator=(const T2 &r) \
00480 { set( r.x(), r.y(), r.width(), r.height() ); return *this; }
00481 #define UT_RECT_COPY_TO_INCLUSIVE(T1,T2) \
00482 inline T1::T1( const T2 &r ) \
00483 : UT_BaseRectImpl( r.x1(), r.y1(), r.x2(), r.y2() ) { } \
00484 inline T1 & T1::operator=(const T2 &r) \
00485 { set( r.x1(), r.y1(), r.x2(), r.y2() ); return *this; }
00486 #define UT_RECT_COPY_TO_EXCLUSIVE(T1,T2) \
00487 inline T1::T1( const T2 &r ) \
00488 : UT_BaseRectImpl( r.x1(), r.y1(), r.x2e(), r.y2e() ) { } \
00489 inline T1 & T1::operator=(const T2 &r) \
00490 { set( r.x1(), r.y1(), r.x2e(), r.y2e() ); return *this; }
00491
00492 UT_RECT_COPY_TO_DIM(UT_DimRectImpl,UT_DimRect)
00493 UT_RECT_COPY_TO_DIM(UT_DimRectImpl,UT_InclusiveRect)
00494 UT_RECT_COPY_TO_DIM(UT_DimRectImpl,UT_ExclusiveRect)
00495 UT_RECT_COPY_TO_INCLUSIVE(UT_InclusiveRectImpl,UT_DimRect)
00496 UT_RECT_COPY_TO_INCLUSIVE(UT_InclusiveRectImpl,UT_InclusiveRect)
00497 UT_RECT_COPY_TO_INCLUSIVE(UT_InclusiveRectImpl,UT_ExclusiveRect)
00498 UT_RECT_COPY_TO_EXCLUSIVE(UT_ExclusiveRectImpl,UT_DimRect)
00499 UT_RECT_COPY_TO_EXCLUSIVE(UT_ExclusiveRectImpl,UT_InclusiveRect)
00500 UT_RECT_COPY_TO_EXCLUSIVE(UT_ExclusiveRectImpl,UT_ExclusiveRect)
00501 #undef UT_RECT_COPY_TO_DIM
00502 #undef UT_RECT_COPY_TO_INCLUSIVE
00503 #undef UT_RECT_COPY_TO_EXCLUSIVE
00504
00505
00506 #if defined( WIN32 ) || defined( LINUX ) || defined( MBSD ) || defined(GAMEOS)
00507 #include "UT_Rect.C"
00508 #endif
00509
00510 #endif // __UT_Rect_h__