37 class UT_InclusiveRectImpl;
38 class UT_ExclusiveRectImpl;
40 template <
typename T>
class UT_Rect;
136 { _r.set(v1, v2, v3, v4); }
138 { _r.set(v[0], v[1], v[2], v[3]); }
166 { px -=
x1(); py -=
y1(); }
170 { px +=
x1(); py +=
y1(); }
174 void flipX(
int awidth) { _r.flipX( awidth ); }
175 void flipY(
int aheight) { _r.flipY( aheight ); }
201 void inset(
int dx,
int dy);
204 void inset(
int dx1,
int dy1,
int dx2,
int dy2);
225 int x()
const {
return _r.x(); }
226 int y()
const {
return _r.y(); }
227 int x1()
const {
return _r.x1(); }
228 int y1()
const {
return _r.y1(); }
233 void setX(
int x_) { _r.setX(x_); }
234 void setY(
int y_) { _r.setY(y_); }
239 void setX1(
int x_) { _r.setX1(x_); }
240 void setY1(
int y_) { _r.setY1(y_); }
245 int w()
const {
return _r.width(); }
246 int h()
const {
return _r.height(); }
247 int width()
const {
return _r.width(); }
248 int height()
const {
return _r.height(); }
259 int x2()
const {
return _r.x2(); }
260 int y2()
const {
return _r.y2(); }
264 void setX2(
int x_) { _r.setX2(x_); }
265 void setY2(
int y_) { _r.setY2(y_); }
269 int x2e()
const {
return _r.x2e(); }
270 int y2e()
const {
return _r.y2e(); }
280 int centerX()
const {
return (_r.x1() + _r.x2()) / 2; }
281 int centerY()
const {
return (_r.y1() + _r.y2()) / 2; }
291 void getDim(
int &x_,
int &y_,
int &w_,
int &h_)
const
293 void getIncl(
int &x1_,
int &y1_,
int &x2_,
int &y2_)
const
294 { x1_ =
x(); y1_ =
y(); x2_ =
x2(); y2_ =
y2(); }
295 void getExcl(
int &x1_,
int &y1_,
int &x2_,
int &y2_)
const
296 { x1_ =
x(); y1_ =
y(); x2_ =
x2e(); y2_ =
y2e(); }
304 void setDim(
int x_,
int y_,
int w_,
int h_)
307 {
setDim(v[0], v[1], v[2], v[3]); }
308 void setIncl(
int x1_,
int y1_,
int x2_,
int y2_)
311 {
setIncl(v[0], v[1], v[2], v[3]); }
312 void setExcl(
int x1_,
int y1_,
int x2_,
int y2_)
315 {
setExcl(v[0], v[1], v[2], v[3]); }
322 const int *
data()
const {
return _r.data(); }
323 int *
data() {
return _r.data(); }
328 int64 mem = inclusive ?
sizeof(*this) : 0;
329 mem += _r.getMemoryUsage(
false);
334 void dump(
const char *msg=
"")
const;
346 class UT_BaseRectImpl
349 UT_BaseRectImpl() { }
350 UT_BaseRectImpl(
int v0,
int v1,
int v2,
int v3 )
351 { set( v0, v1, v2, v3 ); }
352 UT_BaseRectImpl(
const UT_BaseRectImpl &
r )
353 { set( r.myVals[0], r.myVals[1], r.myVals[2], r.myVals[3] ); }
355 UT_BaseRectImpl &operator=(
const UT_BaseRectImpl &
r )
358 set( r.myVals[0], r.myVals[1], r.myVals[2], r.myVals[3] );
366 void set(
int v0,
int v1,
int v2,
int v3 )
367 { myVals[0] =
v0; myVals[1] =
v1; myVals[2] =
v2; myVals[3] =
v3; }
369 int &operator()(
int i) {
return myVals[i]; }
370 int operator()(
int i)
const {
return myVals[i]; }
372 const int *
data()
const {
return myVals; }
373 int *
data() {
return myVals; }
375 int64 getMemoryUsage(
bool inclusive)
const
376 {
return inclusive ?
sizeof(*this) : 0; }
380 int I2D(
int v1,
int v2 )
const {
return v2 - v1 + 1; }
381 int D2I(
int v,
int d )
const {
return v + d - 1; }
382 int E2D(
int v1,
int v2 )
const {
return v2 -
v1; }
383 int D2E(
int v,
int d )
const {
return v + d; }
384 int E2I(
int v2 )
const {
return v2 - 1; }
385 int I2E(
int v2 )
const {
return v2 + 1; }
394 class UT_DimRectImpl :
public UT_BaseRectImpl
407 void clear() { set( 0, 0, 0, 0 ); }
410 bool isValid()
const {
return width() >= 0 &&
height() >= 0; }
418 setWidth(-
width() + 2);
428 void flipX(
int w) { myVals[0] = flipI(x2(), w); }
429 void flipY(
int h) { myVals[1] = flipI(y2(), h); }
432 int x()
const {
return myVals[0]; }
433 int y()
const {
return myVals[1]; }
434 int width()
const {
return myVals[2]; }
435 int height()
const {
return myVals[3]; }
437 int x1()
const {
return x(); }
438 int y1()
const {
return y(); }
439 int x2()
const {
return D2I(
x(),
width()); }
440 int y2()
const {
return D2I(
y(),
height()); }
441 int x2e()
const {
return I2E(x2()); }
442 int y2e()
const {
return I2E(y2()); }
444 void setWidth(
int w) { myVals[2] =
w; }
445 void setHeight(
int h) { myVals[3] =
h; }
447 void setX(
int x_) { myVals[0] = x_; }
448 void setY(
int y_) { myVals[1] = y_; }
449 void setX1(
int x_) { myVals[2] += myVals[0]-x_; myVals[0] = x_; }
450 void setY1(
int y_) { myVals[3] += myVals[1]-y_; myVals[1] = y_; }
451 void setX2(
int x_) { myVals[2] = I2D(myVals[0], x_); }
452 void setY2(
int y_) { myVals[3] = I2D(myVals[1], y_); }
453 void setX2e(
int x_) { myVals[2] = E2D(myVals[0], x_); }
454 void setY2e(
int y_) { myVals[3] = E2D(myVals[1], y_); }
458 class UT_InclusiveRectImpl :
public UT_BaseRectImpl
461 UT_InclusiveRectImpl() { }
466 UT_InclusiveRectImpl &operator=(
const UT_DimRect &
r );
471 void clear() { set( 0, 0, -1, -1 ); }
474 bool isValid()
const {
return (
x1() <= x2e()) && (
y1() <= y2e()); }
479 if (myVals[0] > myVals[2])
480 UTswap(myVals[2], myVals[0]);
481 if (myVals[1] > myVals[3])
482 UTswap(myVals[1], myVals[3]);
488 myVals[0] = flipI(
x1(), w);
489 myVals[2] = flipI(x2(), w);
490 UTswap(myVals[0], myVals[2]);
494 myVals[1] = flipI(
y1(), h);
495 myVals[3] = flipI(y2(), h);
496 UTswap(myVals[1], myVals[3]);
500 int x1()
const {
return myVals[0]; }
501 int y1()
const {
return myVals[1]; }
502 int x2()
const {
return myVals[2]; }
503 int y2()
const {
return myVals[3]; }
504 int x2e()
const {
return I2E(x2()); }
505 int y2e()
const {
return I2E(y2()); }
507 int x()
const {
return x1(); }
508 int y()
const {
return y1(); }
509 int width()
const {
return I2D(
x1(), x2()); }
510 int height()
const {
return I2D(
y1(), y2()); }
512 void setWidth(
int w) { myVals[2] = D2I(myVals[0], w); }
513 void setHeight(
int h) { myVals[3] = D2I(myVals[1], h); }
515 void setX(
int x_) { myVals[2] = x_ +
width()-1; myVals[0] = x_; }
516 void setY(
int y_) { myVals[3] = y_ +
height()-1; myVals[1] = y_; }
517 void setX1(
int x_) { myVals[0] = x_; }
518 void setY1(
int y_) { myVals[1] = y_; }
519 void setX2(
int x_) { myVals[2] = x_; }
520 void setY2(
int y_) { myVals[3] = y_; }
521 void setX2e(
int x_) { myVals[2] = E2I(x_); }
522 void setY2e(
int y_) { myVals[3] = E2I(y_); }
525 class UT_ExclusiveRectImpl :
public UT_BaseRectImpl
528 UT_ExclusiveRectImpl() { }
533 UT_ExclusiveRectImpl &operator=(
const UT_DimRect &
r );
538 void clear() { set( 0, 0, 0, 0 ); }
541 bool isValid()
const {
return x1() <= x2e() &&
y1() <= y2e(); }
546 if (myVals[0] > myVals[2])
548 UTswap(myVals[2], myVals[0]);
552 if (myVals[1] > myVals[3])
554 UTswap(myVals[1], myVals[3]);
563 myVals[0] = I2E(flipI(
x1(), w));
564 myVals[2] = flipI(x2(), w);
565 UTswap(myVals[0], myVals[2]);
569 myVals[1] = I2E(flipI(
y1(), h));
570 myVals[3] = flipI(y2(), h);
571 UTswap(myVals[1], myVals[3]);
575 int x1()
const {
return myVals[0]; }
576 int y1()
const {
return myVals[1]; }
577 int x2e()
const {
return myVals[2]; }
578 int y2e()
const {
return myVals[3]; }
579 int x2()
const {
return E2I(x2e()); }
580 int y2()
const {
return E2I(y2e()); }
582 int x()
const {
return x1(); }
583 int y()
const {
return y1(); }
584 int width()
const {
return E2D(
x1(), x2e()); }
585 int height()
const {
return E2D(
y1(), y2e()); }
587 void setWidth(
int w) { myVals[2] = D2E(myVals[0], w); }
588 void setHeight(
int h) { myVals[3] = D2E(myVals[1], h); }
590 void setX(
int x_) { myVals[2] = x_ +
width(); myVals[0] = x_; }
591 void setY(
int y_) { myVals[3] = y_ +
height(); myVals[1] = y_; }
592 void setX1(
int x_) { myVals[0] = x_; }
593 void setY1(
int y_) { myVals[1] = y_; }
594 void setX2(
int x_) { myVals[2] = I2E(x_); }
595 void setY2(
int y_) { myVals[3] = I2E(y_); }
596 void setX2e(
int x_) { myVals[2] = x_; }
597 void setY2e(
int y_) { myVals[3] = y_; }
610 template <
typename T>
621 #define UT_RECT_OP_EQUALS(T1,T2) \
622 inline bool operator==( const T1 &r1, const T2 &r2 ) \
623 { return (r1.x() == r2.x() && r1.y() == r2.y() \
624 && r1.width() == r2.width() \
625 && r1.height() == r2.height()); }
631 UT_RECT_OP_EQUALS(UT_InclusiveRect,UT_InclusiveRect)
634 UT_RECT_OP_EQUALS(UT_ExclusiveRect,UT_InclusiveRect)
635 UT_RECT_OP_EQUALS(UT_ExclusiveRect,UT_ExclusiveRect)
636 #undef UT_RECT_OP_EQUALS
638 #define UT_RECT_OP_NOT_EQUALS(T1,T2) \
639 inline bool operator!=( const T1 &r1, const T2 &r2 ) \
640 { return !(r1 == r2); }
642 UT_RECT_OP_NOT_EQUALS(UT_DimRect,UT_DimRect)
643 UT_RECT_OP_NOT_EQUALS(UT_DimRect,UT_InclusiveRect)
644 UT_RECT_OP_NOT_EQUALS(UT_DimRect,UT_ExclusiveRect)
645 UT_RECT_OP_NOT_EQUALS(UT_InclusiveRect,UT_DimRect)
646 UT_RECT_OP_NOT_EQUALS(UT_InclusiveRect,UT_InclusiveRect)
647 UT_RECT_OP_NOT_EQUALS(UT_InclusiveRect,UT_ExclusiveRect)
648 UT_RECT_OP_NOT_EQUALS(UT_ExclusiveRect,UT_DimRect)
649 UT_RECT_OP_NOT_EQUALS(UT_ExclusiveRect,UT_InclusiveRect)
650 UT_RECT_OP_NOT_EQUALS(UT_ExclusiveRect,UT_ExclusiveRect)
651 #undef UT_RECT_OP_NOT_EQUALS
653 #define UT_RECT_COPY_TO_DIM(T1,T2) \
654 inline T1::T1( const T2 &r ) \
655 : UT_BaseRectImpl( r.x(), r.y(), r.width(), r.height() ) { } \
656 inline T1 & T1::operator=(const T2 &r) \
657 { set( r.x(), r.y(), r.width(), r.height() ); return *this; }
658 #define UT_RECT_COPY_TO_INCLUSIVE(T1,T2) \
659 inline T1::T1( const T2 &r ) \
660 : UT_BaseRectImpl( r.x1(), r.y1(), r.x2(), r.y2() ) { } \
661 inline T1 & T1::operator=(const T2 &r) \
662 { set( r.x1(), r.y1(), r.x2(), r.y2() ); return *this; }
663 #define UT_RECT_COPY_TO_EXCLUSIVE(T1,T2) \
664 inline T1::T1( const T2 &r ) \
665 : UT_BaseRectImpl( r.x1(), r.y1(), r.x2e(), r.y2e() ) { } \
666 inline T1 & T1::operator=(const T2 &r) \
667 { set( r.x1(), r.y1(), r.x2e(), r.y2e() ); return *this; }
669 UT_RECT_COPY_TO_DIM(UT_DimRectImpl,UT_DimRect)
670 UT_RECT_COPY_TO_DIM(UT_DimRectImpl,UT_InclusiveRect)
671 UT_RECT_COPY_TO_DIM(UT_DimRectImpl,UT_ExclusiveRect)
672 UT_RECT_COPY_TO_INCLUSIVE(UT_InclusiveRectImpl,UT_DimRect)
673 UT_RECT_COPY_TO_INCLUSIVE(UT_InclusiveRectImpl,UT_InclusiveRect)
674 UT_RECT_COPY_TO_INCLUSIVE(UT_InclusiveRectImpl,UT_ExclusiveRect)
675 UT_RECT_COPY_TO_EXCLUSIVE(UT_ExclusiveRectImpl,UT_DimRect)
676 UT_RECT_COPY_TO_EXCLUSIVE(UT_ExclusiveRectImpl,UT_InclusiveRect)
677 UT_RECT_COPY_TO_EXCLUSIVE(UT_ExclusiveRectImpl,UT_ExclusiveRect)
678 #undef UT_RECT_COPY_TO_DIM
679 #undef UT_RECT_COPY_TO_INCLUSIVE
680 #undef UT_RECT_COPY_TO_EXCLUSIVE
684 #endif // __UT_Rect_h__
void getDim(int &x_, int &y_, int &w_, int &h_) const
Get rectangle values.
int * data()
Native data access.
GLenum GLuint GLsizei bufsize
void setY2e(int y_)
Set upper-right corner (exclusive), without changing lower-left.
int x2() const
Get upper-right corner (inclusive)
void setIncl(int x1_, int y1_, int x2_, int y2_)
Set rectangle values.
int centerY() const
Get center.
int64 getMemoryUsage(bool inclusive) const
bool contains(const T &r) const
Returns true if T is entirely contained by this rectangle.
void setY1(int y_)
Set lower-left corner, without changing the upper-right corner.
void setDim(int v[4])
Set rectangle values.
void setX2(int x_)
Set upper-right corner (inclusive), without changing lower-left.
const int * data() const
Native data access.
UT_Rect & operator=(const UT_DimRect &r)
Assignment operator. Can convert from any rect type.
int h() const
Get width/height dimensions.
void set(int v[4])
Set the native rect type values.
bool isValid() const
Return true if rectangle is valid (true if empty)
UT_Rect(int v1, int v2, int v3, int v4)
Construct with native rect type values.
UT_Rect(int v[4])
Construct with native rect type values.
int x2e() const
Get upper-right corner (exclusive)
static UT_Rect zero()
Convenience method for creating an empty rect.
int operator()(int i) const
Native data access.
GLubyte GLubyte GLubyte GLubyte w
UT_Rect & operator=(const UT_ExclusiveRect &r)
Assignment operator. Can convert from any rect type.
std::ostream & operator<<(std::ostream &ostr, const DataType &a)
void setX2e(int x_)
Set upper-right corner (exclusive), without changing lower-left.
void enlarge(const T &r)
Enlarge this rectangle to include the given rectangle (ie. a union)
void setX(int x_)
Set the lower-left corner position, always maintaining the size.
void inset(int dx, int dy)
Inset the rect by a dx and dy margins.
bool overlapY(const T &r) const
Returns true if the given side would overlap the other rectangle if projected onto it...
void setExcl(int v[4])
Set rectangle values.
bool isInsideY(int py) const
Returns true if the rectangle contains the given coord.
void setHeight(int aheight)
Set the width/height, always maintaining the lower-left position.
bool overlapX(const T &r) const
Returns true if the given side would overlap the other rectangle if projected onto it...
GLfloat GLfloat GLfloat v2
GLuint GLfloat GLfloat GLfloat x1
bool intersect(const T &r)
Intersect this rectangle with the given one. Returns if result is valid.
bool isInside(int px, int py) const
Returns true if the given point is within the rectangle.
void set(int v1, int v2, int v3, int v4)
Set the native rect type values.
void getRelative(int &px, int &py) const
Get relative coordinates from absolute.
void flipY(int aheight)
Flip rectangle within the given width/height.
int UTinclusiveFlip(int v, int d)
Flip a particular coordinate value according to the given dimension.
UT_Rect< UT_ExclusiveRectImpl > UT_ExclusiveRect
Integer rectangle class which stores its data as exclusive lower-left to upper-right (x1...
int & operator()(int i)
Native data access.
int x() const
Get lower-left corner.
int w() const
Get width/height dimensions.
void dump(UT_WorkBuffer &wbuf, const char *msg="") const
GLfloat GLfloat GLfloat GLfloat v3
int x1() const
Get lower-left corner.
void getAbsolute(int &px, int &py) const
Get absolute coordinates from relative.
GLuint GLfloat GLfloat GLfloat GLfloat y1
GLint GLsizei GLsizei height
UT_Rect< UT_InclusiveRectImpl > UT_InclusiveRect
Integer rectangle class which stores its data as inclusive lower-left to upper-right (x1...
UT_Rect< UT_DimRectImpl > UT_DimRect
int width() const
Get width/height dimensions.
void clamp(int &px, int &py) const
Clamp the coordinates to be inclusive of this rect (ie. make it inside)
void setExcl(int x1_, int y1_, int x2_, int y2_)
Set rectangle values.
bool isEmpty() const
Return true if rectangle is empty.
void setY2(int y_)
Set upper-right corner (inclusive), without changing lower-left.
GLint GLint GLsizei GLint GLenum format
GLfloat GLfloat GLfloat GLfloat h
UT_Rect(const UT_InclusiveRect &r)
Conversion constructor.
void getExcl(int &x1_, int &y1_, int &x2_, int &y2_) const
Get rectangle values.
Base Integer Rectangle class.
UT_Rect(const UT_ExclusiveRect &r)
Conversion constructor.
UT_Rect(const UT_DimRect &r)
Conversion constructor.
int y() const
Get lower-left corner.
void setX1(int x_)
Set lower-left corner, without changing the upper-right corner.
void clear()
Clear rect to empty.
void setWidth(int awidth)
Set the width/height, always maintaining the lower-left position.
int height() const
Get width/height dimensions.
bool isInsideX(int px) const
Returns true if the rectangle contains the given coord.
int y2() const
Get upper-right corner (inclusive)
void setY(int y_)
Set the lower-left corner position, always maintaining the size.
void scale(fpreal factor)
Scale by given factor.
void setIncl(int v[4])
Set rectangle values.
UT_Rect & operator=(const UT_InclusiveRect &r)
Assignment operator. Can convert from any rect type.
int y1() const
Get lower-left corner.
int y2e() const
Get upper-right corner (exclusive)
void flipX(int awidth)
Flip rectangle within the given width/height.
void setDim(int x_, int y_, int w_, int h_)
Set rectangle values.
void translate(int x, int y)
Translate by given offset.
void getIncl(int &x1_, int &y1_, int &x2_, int &y2_) const
Get rectangle values.
int centerX() const
Get center.