HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_Rect.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_Rect.h (C++)
7  *
8  * COMMENTS:
9  *
10  * This file provides a bunch of integer rectangle classes.
11  *
12  */
13 
14 #ifndef __UT_Rect_h__
15 #define __UT_Rect_h__
16 
17 #include "UT_API.h"
18 #include "UT_Swap.h"
19 
20 #include <SYS/SYS_Types.h>
21 
22 #include <iostream>
23 
24 #include <stddef.h>
25 
26 
27 class UT_WorkBuffer;
28 
29 /// @file
30 
31 
32 /// @brief Flip a particular coordinate value according to the given dimension
33 inline int UTinclusiveFlip( int v, int d ) { return d - 1 - v; }
34 
35 
36 class UT_DimRectImpl;
37 class UT_InclusiveRectImpl;
38 class UT_ExclusiveRectImpl;
39 
40 template <typename T> class UT_Rect;
41 
42 
43 /// @var typedef UT_Rect<UT_DimRectImpl> UT_DimRect
44 /// @brief Integer rectangle class which stores its data as
45 /// (x, y, width, height).
46 /// @see UT_Rect
48 
49 /// @var typedef UT_Rect<UT_InclusiveRectImpl> UT_InclusiveRect
50 /// @brief Integer rectangle class which stores its data as inclusive
51 /// lower-left to upper-right (x1, y1, x2, y2).
52 /// @see UT_Rect
54 
55 /// @var typedef UT_Rect<UT_ExclusiveRectImpl> UT_ExclusiveRect
56 /// @brief Integer rectangle class which stores its data as exclusive
57 /// lower-left to upper-right (x1, y1, x2e, y2e). The upper-right corner
58 /// position (x2e, y2e) is equivalent to (x2+1, y2+1).
59 /// @see UT_Rect
61 
62 
63 /// @brief Base Integer Rectangle class
64 ///
65 /// UT_Rect is an integer rectangle class. You do *NOT* instantiate this but
66 /// instead choose from one of the 3 different types of underlying rectangle
67 /// implementations, each with the same interface:
68 ///
69 /// @code
70 /// UT_DimRect: Stores the data as (x, y, width, height)
71 /// UT_InclusiveRect: Stores the data as inclusive (x1, y1, x2, y2).
72 /// ie. lower-left and upper-right corner positions
73 /// UT_ExclusiveRect: Stores the data as exclusive (x1, y1, x2e, y2e).
74 /// The upper-right corner position (x2e, y2e) is
75 /// equivalent to (x2+1, y2+1).
76 /// @endcode
77 ///
78 /// The "lower-left"/"upper-right" terms here assume (0,0) is in the lower-left
79 /// corner of your coordinate system. If your (0,0) is in the top-left corner
80 /// instead, substitute these terms with "top-left"/"bottom-right".
81 ///
82 /// To convert internal data representations from one type to another, use
83 /// the copy constructor and assignment operators. Otherwise, use the methods
84 /// found in UT_Rect.
85 ///
86 template <typename T>
87 class UT_Rect
88 {
89 public:
90  UT_Rect() { clear(); }
91 
92  /// @brief Construct with native rect type values
93  ///
94  /// The actual construction signatures are:
95  /// @code
96  /// UT_DimRect(int x, int y, int width, int height)
97  /// UT_InclusiveRect(int x1, int y1, int x2, int y2)
98  /// UT_ExclusiveRect(int x1, int y1, int x2e, int y2e)
99  /// @endcode
100  /// @{
101  UT_Rect(int v1, int v2, int v3, int v4) { set(v1, v2, v3, v4); }
102  UT_Rect(int v[4]) { set(v); }
103  /// @}
104 
105  /// @brief Conversion constructor
106  /// @{
107  UT_Rect(const UT_DimRect &r) : _r(r) { }
108  UT_Rect(const UT_InclusiveRect &r) : _r(r) { }
109  UT_Rect(const UT_ExclusiveRect &r) : _r(r) { }
110  /// @}
111 
112  /// @brief Assignment operator. Can convert from any rect type.
113  /// @{
114  UT_Rect &operator=(const UT_DimRect &r) { _r = r; return *this; }
115  UT_Rect &operator=(const UT_InclusiveRect &r) { _r = r; return *this; }
116  UT_Rect &operator=(const UT_ExclusiveRect &r) { _r = r; return *this; }
117  /// @}
118 
119  /// Convenience method for creating an empty rect
120  static inline UT_Rect zero();
121 
122  /// Clear rect to empty
123  void clear() { _r.clear(); }
124 
125  /// @brief Set the native rect type values
126  ///
127  /// Interpretation will differ depending on type T.
128  /// The actual signatures are:
129  /// @code
130  /// UT_DimRect::set(int x, int y, int width, int height)
131  /// UT_InclusiveRect::set(int x1, int y1, int x2, int y2)
132  /// UT_ExclusiveRect::set(int x1, int y1e, int x2, int y2e)
133  /// @endcode
134  /// @{
135  void set(int v1, int v2, int v3, int v4)
136  { _r.set(v1, v2, v3, v4); }
137  void set(int v[4])
138  { _r.set(v[0], v[1], v[2], v[3]); }
139  /// @}
140 
141  /// Return true if rectangle is empty
142  bool isEmpty() const { return (width() == 0 || height() == 0); }
143 
144  /// Return true if rectangle is valid (true if empty)
145  bool isValid() const { return _r.isValid(); }
146 
147  /// Modify the rectangle so that it has non-negative width/height values
148  /// (ie. isValid() returns true)
149  void standardize() { _r.standardize(); }
150 
151  /// @brief Returns true if the rectangle contains the given coord
152  /// @{
153  bool isInsideX(int px) const { return (x1() <= px && px <= x2()); }
154  bool isInsideY(int py) const { return (y1() <= py && py <= y2()); }
155  /// @}
156 
157  /// Returns true if the given point is within the rectangle
158  bool isInside(int px, int py) const
159  { return isInsideX(px) && isInsideY(py); }
160 
161  /// Returns true if T is entirely contained by this rectangle
162  bool contains(const T &r) const;
163 
164  /// Get relative coordinates from absolute
165  void getRelative(int &px, int &py) const
166  { px -= x1(); py -= y1(); }
167 
168  /// Get absolute coordinates from relative
169  void getAbsolute(int &px, int &py) const
170  { px += x1(); py += y1(); }
171 
172  /// @brief Flip rectangle within the given width/height
173  /// @{
174  void flipX(int awidth) { _r.flipX( awidth ); }
175  void flipY(int aheight) { _r.flipY( aheight ); }
176  /// @}
177 
178  /// Intersect this rectangle with the given one. Returns if result is valid.
179  bool intersect(const T &r);
180 
181  /// @brief Returns true if the given side would overlap the other rectangle
182  /// if projected onto it.
183  /// @{
184  bool overlapX(const T &r) const;
185  bool overlapY(const T &r) const;
186  /// @}
187 
188  /// Enlarge this rectangle to include the given rectangle (ie. a union)
189  void enlarge(const T &r);
190 
191  /// @brief Enlarge this rectangle to include the given point
192  /// @{
193  void enlarge(int x, int y);
194  void enlarge(float x, float y);
195  /// @}
196 
197  /// Translate by given offset
198  void translate(int x, int y);
199 
200  /// Inset the rect by a dx and dy margins
201  void inset(int dx, int dy);
202 
203  /// Replace by the inclusive rect (x1()+dx1, y1()+dy1, x2()-dx2, y2()-dy2)
204  void inset(int dx1, int dy1, int dx2, int dy2);
205 
206  /// Scale by given factor
207  void scale(fpreal factor);
208 
209  /// Clamp the coordinates to be inclusive of this rect (ie. make it inside)
210  void clamp(int &px, int &py) const
211  {
212  if (px < x1())
213  px = x1();
214  else if (px > x2())
215  px = x2();
216 
217  if (py < y1())
218  py = y1();
219  else if (py > y2())
220  py = y2();
221  }
222 
223  /// @brief Get lower-left corner.
224  /// @{
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(); }
229  /// @}
230 
231  /// @brief Set the lower-left corner position, always maintaining the size.
232  /// @{
233  void setX(int x_) { _r.setX(x_); }
234  void setY(int y_) { _r.setY(y_); }
235  /// @}
236 
237  /// @brief Set lower-left corner, without changing the upper-right corner
238  /// @{
239  void setX1(int x_) { _r.setX1(x_); }
240  void setY1(int y_) { _r.setY1(y_); }
241  /// @}
242 
243  /// @brief Get width/height dimensions
244  /// @{
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(); }
249  /// @}
250 
251  /// @brief Set the width/height, always maintaining the lower-left position
252  /// @{
253  void setWidth(int awidth) { _r.setWidth(awidth); }
254  void setHeight(int aheight) { _r.setHeight(aheight); }
255  /// @}
256 
257  /// @brief Get upper-right corner (inclusive)
258  /// @{
259  int x2() const { return _r.x2(); }
260  int y2() const { return _r.y2(); }
261  /// @}
262  /// @brief Set upper-right corner (inclusive), without changing lower-left.
263  /// @{
264  void setX2(int x_) { _r.setX2(x_); }
265  void setY2(int y_) { _r.setY2(y_); }
266  /// @}
267  /// @brief Get upper-right corner (exclusive)
268  /// @{
269  int x2e() const { return _r.x2e(); }
270  int y2e() const { return _r.y2e(); }
271  /// @}
272  /// @brief Set upper-right corner (exclusive), without changing lower-left.
273  /// @{
274  void setX2e(int x_) { _r.setX2e(x_); }
275  void setY2e(int y_) { _r.setY2e(y_); }
276  /// @}
277 
278  /// @brief Get center
279  /// @{
280  int centerX() const { return (_r.x1() + _r.x2()) / 2; }
281  int centerY() const { return (_r.y1() + _r.y2()) / 2; }
282  /// @}
283 
284  // Scalar <-> Rectangle conversions
285 
286  /// @brief Get rectangle values.
287  ///
288  /// This will return you the desired semantic
289  /// regardless of the underlying rectangle type.
290  /// @{
291  void getDim(int &x_, int &y_, int &w_, int &h_) const
292  { x_ = x(); y_ = y(); w_ = width(); h_ = height(); }
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(); }
297  /// @}
298 
299  /// @brief Set rectangle values.
300  ///
301  /// Regardless of the underlying type of rectangle, it will set it up
302  /// properly using the given semantic.
303  /// @{
304  void setDim(int x_, int y_, int w_, int h_)
305  { setX(x_); setY(y_); setWidth(w_); setHeight(h_); }
306  void setDim(int v[4])
307  { setDim(v[0], v[1], v[2], v[3]); }
308  void setIncl(int x1_, int y1_, int x2_, int y2_)
309  { setX1(x1_); setX2(x2_); setY1(y1_); setY2(y2_); }
310  void setIncl(int v[4])
311  { setIncl(v[0], v[1], v[2], v[3]); }
312  void setExcl(int x1_, int y1_, int x2_, int y2_)
313  { setX1(x1_); setX2e(x2_); setY1(y1_); setY2e(y2_); }
314  void setExcl(int v[4])
315  { setExcl(v[0], v[1], v[2], v[3]); }
316  /// @}
317 
318  /// @brief Native data access. @see UT_Rect::UT_Rect()
319  /// @{
320  int &operator()(int i) { return _r(i); }
321  int operator()(int i) const { return _r(i); }
322  const int *data() const { return _r.data(); }
323  int *data() { return _r.data(); }
324  /// @}
325 
326  int64 getMemoryUsage(bool inclusive) const
327  {
328  int64 mem = inclusive ? sizeof(*this) : 0;
329  mem += _r.getMemoryUsage(false);
330  return mem;
331  }
332 
333  void dump(UT_WorkBuffer &wbuf, const char *msg="") const;
334  void dump(const char *msg="") const;
335 
336 private:
337  T _r;
338 };
339 
340 //////////////////////////////////////////////////////////////////////////////
341 //
342 // Implementation
343 //
344 /// @cond
345 
346 class UT_BaseRectImpl
347 {
348 public:
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] ); }
354 
355  UT_BaseRectImpl &operator=( const UT_BaseRectImpl &r )
356  {
357  if( &r != this )
358  set( r.myVals[0], r.myVals[1], r.myVals[2], r.myVals[3] );
359  return *this;
360  }
361 
362  // The actual signatures are:
363  // UT_DimRect::set(int x, int y, int width, int height)
364  // UT_InclusiveRect::set(int x1, int y1, int x2, int y2)
365  // UT_ExclusiveRect::set(int x1, int y1e, int x2, int y2e)
366  void set( int v0, int v1, int v2, int v3 )
367  { myVals[0] = v0; myVals[1] = v1; myVals[2] = v2; myVals[3] = v3; }
368 
369  int &operator()(int i) { return myVals[i]; }
370  int operator()(int i) const { return myVals[i]; }
371 
372  const int * data() const { return myVals; }
373  int * data() { return myVals; }
374 
375  int64 getMemoryUsage(bool inclusive) const
376  { return inclusive ? sizeof(*this) : 0; }
377 
378 protected:
379  // D == Dimension Rect, I == Inclusive Rect, E == Exclusive Rect
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; }
386 
387  // Flip a particular inclusive coord value according to the given dimension
388  static int flipI( int v, int d ) { return UTinclusiveFlip( v, d ); }
389 
390 protected:
391  int myVals[4];
392 };
393 
394 class UT_DimRectImpl : public UT_BaseRectImpl
395 {
396 public:
397  UT_DimRectImpl() { }
398  UT_DimRectImpl( const UT_DimRect &r );
399  UT_DimRectImpl( const UT_InclusiveRect &r );
400  UT_DimRectImpl( const UT_ExclusiveRect &r );
401 
402  UT_DimRectImpl &operator=( const UT_DimRect &r );
403  UT_DimRectImpl &operator=( const UT_InclusiveRect &r );
404  UT_DimRectImpl &operator=( const UT_ExclusiveRect &r );
405 
406  // clear it to empty
407  void clear() { set( 0, 0, 0, 0 ); }
408 
409  // Return true if rectangle is valid (true if empty)
410  bool isValid() const { return width() >= 0 && height() >= 0; }
411 
412  // Modify the rectangle so that it has non-negative width/height values
413  void standardize()
414  {
415  if (width() < 0)
416  {
417  setX(x2());
418  setWidth(-width() + 2); // nw = nx2-nx1+1 = x1-(x1+w-1)+1 = -w + 2
419  }
420  if (height() < 0)
421  {
422  setY(y2());
423  setHeight(-height() + 2);
424  }
425  }
426 
427  // Flip rectangle within the given width/height
428  void flipX(int w) { myVals[0] = flipI(x2(), w); }
429  void flipY(int h) { myVals[1] = flipI(y2(), h); }
430 
431  // Accessors
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]; }
436 
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()); }
443 
444  void setWidth(int w) { myVals[2] = w; }
445  void setHeight(int h) { myVals[3] = h; }
446 
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_); }
455 };
456 
457 
458 class UT_InclusiveRectImpl : public UT_BaseRectImpl
459 {
460 public:
461  UT_InclusiveRectImpl() { }
462  UT_InclusiveRectImpl( const UT_DimRect &r );
463  UT_InclusiveRectImpl( const UT_InclusiveRect &r );
464  UT_InclusiveRectImpl( const UT_ExclusiveRect &r );
465 
466  UT_InclusiveRectImpl &operator=( const UT_DimRect &r );
467  UT_InclusiveRectImpl &operator=( const UT_InclusiveRect &r );
468  UT_InclusiveRectImpl &operator=( const UT_ExclusiveRect &r );
469 
470  // clear it to empty
471  void clear() { set( 0, 0, -1, -1 ); }
472 
473  // Return true if rectangle is valid (true if empty)
474  bool isValid() const { return (x1() <= x2e()) && (y1() <= y2e()); }
475 
476  // Modify the rectangle so that it has non-negative width/height values
477  void standardize()
478  {
479  if (myVals[0] > myVals[2])
480  UTswap(myVals[2], myVals[0]);
481  if (myVals[1] > myVals[3])
482  UTswap(myVals[1], myVals[3]);
483  }
484 
485  // Flip rectangle within the given width/height
486  void flipX( int w )
487  {
488  myVals[0] = flipI(x1(), w);
489  myVals[2] = flipI(x2(), w);
490  UTswap(myVals[0], myVals[2]);
491  }
492  void flipY( int h )
493  {
494  myVals[1] = flipI(y1(), h);
495  myVals[3] = flipI(y2(), h);
496  UTswap(myVals[1], myVals[3]);
497  }
498 
499  // Accessors
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()); }
506 
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()); }
511 
512  void setWidth(int w) { myVals[2] = D2I(myVals[0], w); }
513  void setHeight(int h) { myVals[3] = D2I(myVals[1], h); }
514 
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_); }
523 };
524 
525 class UT_ExclusiveRectImpl : public UT_BaseRectImpl
526 {
527 public:
528  UT_ExclusiveRectImpl() { }
529  UT_ExclusiveRectImpl( const UT_DimRect &r );
530  UT_ExclusiveRectImpl( const UT_InclusiveRect &r );
531  UT_ExclusiveRectImpl( const UT_ExclusiveRect &r );
532 
533  UT_ExclusiveRectImpl &operator=( const UT_DimRect &r );
534  UT_ExclusiveRectImpl &operator=( const UT_InclusiveRect &r );
535  UT_ExclusiveRectImpl &operator=( const UT_ExclusiveRect &r );
536 
537  // clear it to empty
538  void clear() { set( 0, 0, 0, 0 ); }
539 
540  // Return true if rectangle is valid (true if empty)
541  bool isValid() const { return x1() <= x2e() && y1() <= y2e(); }
542 
543  // Modify the rectangle so that it has non-negative width/height values
544  void standardize()
545  {
546  if (myVals[0] > myVals[2])
547  {
548  UTswap(myVals[2], myVals[0]);
549  myVals[0] -= 1; // convert first coord from excl to incl
550  myVals[2] += 1; // convert last coord from incl to excl
551  }
552  if (myVals[1] > myVals[3])
553  {
554  UTswap(myVals[1], myVals[3]);
555  myVals[1] -= 1;
556  myVals[3] += 1;
557  }
558  }
559 
560  // Flip rectangle within the given width/height
561  void flipX( int w )
562  {
563  myVals[0] = I2E(flipI(x1(), w)); // convert to exclusive for swap
564  myVals[2] = flipI(x2(), w);
565  UTswap(myVals[0], myVals[2]);
566  }
567  void flipY( int h )
568  {
569  myVals[1] = I2E(flipI(y1(), h)); // convert to exclusive for swap
570  myVals[3] = flipI(y2(), h);
571  UTswap(myVals[1], myVals[3]);
572  }
573 
574  // Accessors
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()); }
581 
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()); }
586 
587  void setWidth(int w) { myVals[2] = D2E(myVals[0], w); }
588  void setHeight(int h) { myVals[3] = D2E(myVals[1], h); }
589 
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_; }
598 };
599 
600 template<typename T>
602 {
603  return UT_Rect(UT_DimRect(0,0,0,0));
604 }
605 
606 inline std::ostream &
607 operator<<(std::ostream &os, const UT_DimRect &r)
608 {
609  os << "[" << r.x() << ","<< r.y() << "]-[" << r.x2() << "," << r.y2()
610  << "] (" << r.w() << "x" << r.h() << ")";
611  return os;
612 }
613 inline std::ostream &
614 operator<<(std::ostream &os, const UT_InclusiveRect &r)
615 {
616  os << "[" << r.x() << ","<< r.y() << "]-[" << r.x2() << "," << r.y2()
617  << "] (" << r.w() << "x" << r.h() << ")";
618  return os;
619 }
620 inline std::ostream &
621 operator<<(std::ostream &os, const UT_ExclusiveRect &r)
622 {
623  os << "[" << r.x() << ","<< r.y() << "]-[" << r.x2() << "," << r.y2()
624  << "] (" << r.w() << "x" << r.h() << ")";
625  return os;
626 }
627 
628 template <typename T>
629 UT_API size_t format(char *buffer, size_t bufsize, const UT_Rect<T> &r);
630 
631 //UT_API size_t format(char *buffer, size_t bufsize, const UT_DimRect &r);
632 //UT_API size_t format(char *buffer, size_t bufsize, const UT_InclusiveRect &r);
633 //UT_API size_t format(char *buffer, size_t bufsize, const UT_ExclusiveRect &r);
634 
635 //////////////////////////////////////////////////////////////////////////
636 // Inline Implementations
637 //////////////////////////////////////////////////////////////////////////
638 
639 #define UT_RECT_OP_EQUALS(T1,T2) \
640  inline bool operator==( const T1 &r1, const T2 &r2 ) \
641  { return (r1.x() == r2.x() && r1.y() == r2.y() \
642  && r1.width() == r2.width() \
643  && r1.height() == r2.height()); }
644 
645  UT_RECT_OP_EQUALS(UT_DimRect,UT_DimRect)
646  UT_RECT_OP_EQUALS(UT_DimRect,UT_InclusiveRect)
647  UT_RECT_OP_EQUALS(UT_DimRect,UT_ExclusiveRect)
648  UT_RECT_OP_EQUALS(UT_InclusiveRect,UT_DimRect)
649  UT_RECT_OP_EQUALS(UT_InclusiveRect,UT_InclusiveRect)
650  UT_RECT_OP_EQUALS(UT_InclusiveRect,UT_ExclusiveRect)
651  UT_RECT_OP_EQUALS(UT_ExclusiveRect,UT_DimRect)
652  UT_RECT_OP_EQUALS(UT_ExclusiveRect,UT_InclusiveRect)
653  UT_RECT_OP_EQUALS(UT_ExclusiveRect,UT_ExclusiveRect)
654 #undef UT_RECT_OP_EQUALS
655 
656 #define UT_RECT_OP_NOT_EQUALS(T1,T2) \
657  inline bool operator!=( const T1 &r1, const T2 &r2 ) \
658  { return !(r1 == r2); }
659 
660  UT_RECT_OP_NOT_EQUALS(UT_DimRect,UT_DimRect)
661  UT_RECT_OP_NOT_EQUALS(UT_DimRect,UT_InclusiveRect)
662  UT_RECT_OP_NOT_EQUALS(UT_DimRect,UT_ExclusiveRect)
663  UT_RECT_OP_NOT_EQUALS(UT_InclusiveRect,UT_DimRect)
664  UT_RECT_OP_NOT_EQUALS(UT_InclusiveRect,UT_InclusiveRect)
665  UT_RECT_OP_NOT_EQUALS(UT_InclusiveRect,UT_ExclusiveRect)
666  UT_RECT_OP_NOT_EQUALS(UT_ExclusiveRect,UT_DimRect)
667  UT_RECT_OP_NOT_EQUALS(UT_ExclusiveRect,UT_InclusiveRect)
668  UT_RECT_OP_NOT_EQUALS(UT_ExclusiveRect,UT_ExclusiveRect)
669 #undef UT_RECT_OP_NOT_EQUALS
670 
671 #define UT_RECT_COPY_TO_DIM(T1,T2) \
672  inline T1::T1( const T2 &r ) \
673  : UT_BaseRectImpl( r.x(), r.y(), r.width(), r.height() ) { } \
674  inline T1 & T1::operator=(const T2 &r) \
675  { set( r.x(), r.y(), r.width(), r.height() ); return *this; }
676 #define UT_RECT_COPY_TO_INCLUSIVE(T1,T2) \
677  inline T1::T1( const T2 &r ) \
678  : UT_BaseRectImpl( r.x1(), r.y1(), r.x2(), r.y2() ) { } \
679  inline T1 & T1::operator=(const T2 &r) \
680  { set( r.x1(), r.y1(), r.x2(), r.y2() ); return *this; }
681 #define UT_RECT_COPY_TO_EXCLUSIVE(T1,T2) \
682  inline T1::T1( const T2 &r ) \
683  : UT_BaseRectImpl( r.x1(), r.y1(), r.x2e(), r.y2e() ) { } \
684  inline T1 & T1::operator=(const T2 &r) \
685  { set( r.x1(), r.y1(), r.x2e(), r.y2e() ); return *this; }
686 
687  UT_RECT_COPY_TO_DIM(UT_DimRectImpl,UT_DimRect)
688  UT_RECT_COPY_TO_DIM(UT_DimRectImpl,UT_InclusiveRect)
689  UT_RECT_COPY_TO_DIM(UT_DimRectImpl,UT_ExclusiveRect)
690  UT_RECT_COPY_TO_INCLUSIVE(UT_InclusiveRectImpl,UT_DimRect)
691  UT_RECT_COPY_TO_INCLUSIVE(UT_InclusiveRectImpl,UT_InclusiveRect)
692  UT_RECT_COPY_TO_INCLUSIVE(UT_InclusiveRectImpl,UT_ExclusiveRect)
693  UT_RECT_COPY_TO_EXCLUSIVE(UT_ExclusiveRectImpl,UT_DimRect)
694  UT_RECT_COPY_TO_EXCLUSIVE(UT_ExclusiveRectImpl,UT_InclusiveRect)
695  UT_RECT_COPY_TO_EXCLUSIVE(UT_ExclusiveRectImpl,UT_ExclusiveRect)
696 #undef UT_RECT_COPY_TO_DIM
697 #undef UT_RECT_COPY_TO_INCLUSIVE
698 #undef UT_RECT_COPY_TO_EXCLUSIVE
699 
700 /// @endcond
701 
702 #endif // __UT_Rect_h__
void getDim(int &x_, int &y_, int &w_, int &h_) const
Get rectangle values.
Definition: UT_Rect.h:291
int * data()
Native data access.
Definition: UT_Rect.h:323
GLenum GLuint GLsizei bufsize
Definition: glcorearb.h:1817
void setY2e(int y_)
Set upper-right corner (exclusive), without changing lower-left.
Definition: UT_Rect.h:275
int x2() const
Get upper-right corner (inclusive)
Definition: UT_Rect.h:259
void setIncl(int x1_, int y1_, int x2_, int y2_)
Set rectangle values.
Definition: UT_Rect.h:308
int centerY() const
Get center.
Definition: UT_Rect.h:281
int64 getMemoryUsage(bool inclusive) const
Definition: UT_Rect.h:326
bool contains(const T &r) const
Returns true if T is entirely contained by this rectangle.
void UTswap(T &a, T &b)
Definition: UT_Swap.h:35
void setY1(int y_)
Set lower-left corner, without changing the upper-right corner.
Definition: UT_Rect.h:240
const GLdouble * v
Definition: glcorearb.h:836
void setDim(int v[4])
Set rectangle values.
Definition: UT_Rect.h:306
void setX2(int x_)
Set upper-right corner (inclusive), without changing lower-left.
Definition: UT_Rect.h:264
const int * data() const
Native data access.
Definition: UT_Rect.h:322
UT_Rect & operator=(const UT_DimRect &r)
Assignment operator. Can convert from any rect type.
Definition: UT_Rect.h:114
int h() const
Get width/height dimensions.
Definition: UT_Rect.h:246
void set(int v[4])
Set the native rect type values.
Definition: UT_Rect.h:137
bool isValid() const
Return true if rectangle is valid (true if empty)
Definition: UT_Rect.h:145
UT_Rect(int v1, int v2, int v3, int v4)
Construct with native rect type values.
Definition: UT_Rect.h:101
#define UT_API
Definition: UT_API.h:12
GLint y
Definition: glcorearb.h:102
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:817
UT_Rect(int v[4])
Construct with native rect type values.
Definition: UT_Rect.h:102
GLfloat GLfloat GLfloat GLfloat v3
Definition: glcorearb.h:818
GLuint buffer
Definition: glcorearb.h:659
int x2e() const
Get upper-right corner (exclusive)
Definition: UT_Rect.h:269
png_uint_32 i
Definition: png.h:2877
static UT_Rect zero()
Convenience method for creating an empty rect.
int operator()(int i) const
Native data access.
Definition: UT_Rect.h:321
GLint GLsizei width
Definition: glcorearb.h:102
UT_Rect & operator=(const UT_ExclusiveRect &r)
Assignment operator. Can convert from any rect type.
Definition: UT_Rect.h:116
std::ostream & operator<<(std::ostream &ostr, const DataType &a)
Definition: DataType.h:133
void setX2e(int x_)
Set upper-right corner (exclusive), without changing lower-left.
Definition: UT_Rect.h:274
void enlarge(const T &r)
Enlarge this rectangle to include the given rectangle (ie. a union)
long long int64
Definition: SYS_Types.h:100
void setX(int x_)
Set the lower-left corner position, always maintaining the size.
Definition: UT_Rect.h:233
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.
Definition: UT_Rect.h:314
bool isInsideY(int py) const
Returns true if the rectangle contains the given coord.
Definition: UT_Rect.h:154
void setHeight(int aheight)
Set the width/height, always maintaining the lower-left position.
Definition: UT_Rect.h:254
bool overlapX(const T &r) const
Returns true if the given side would overlap the other rectangle if projected onto it...
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.
Definition: UT_Rect.h:158
void set(int v1, int v2, int v3, int v4)
Set the native rect type values.
Definition: UT_Rect.h:135
void getRelative(int &px, int &py) const
Get relative coordinates from absolute.
Definition: UT_Rect.h:165
void flipY(int aheight)
Flip rectangle within the given width/height.
Definition: UT_Rect.h:175
int UTinclusiveFlip(int v, int d)
Flip a particular coordinate value according to the given dimension.
Definition: UT_Rect.h:33
UT_Rect< UT_ExclusiveRectImpl > UT_ExclusiveRect
Integer rectangle class which stores its data as exclusive lower-left to upper-right (x1...
Definition: UT_Rect.h:60
int & operator()(int i)
Native data access.
Definition: UT_Rect.h:320
int x() const
Get lower-left corner.
Definition: UT_Rect.h:225
int w() const
Get width/height dimensions.
Definition: UT_Rect.h:245
GLboolean * data
Definition: glcorearb.h:130
void dump(UT_WorkBuffer &wbuf, const char *msg="") const
int x1() const
Get lower-left corner.
Definition: UT_Rect.h:227
void getAbsolute(int &px, int &py) const
Get absolute coordinates from relative.
Definition: UT_Rect.h:169
UT_Rect< UT_InclusiveRectImpl > UT_InclusiveRect
Integer rectangle class which stores its data as inclusive lower-left to upper-right (x1...
Definition: UT_Rect.h:53
UT_Rect< UT_DimRectImpl > UT_DimRect
Definition: UT_Rect.h:47
GLfloat v0
Definition: glcorearb.h:815
GLint GLsizei GLsizei height
Definition: glcorearb.h:102
int width() const
Get width/height dimensions.
Definition: UT_Rect.h:247
void clamp(int &px, int &py) const
Clamp the coordinates to be inclusive of this rect (ie. make it inside)
Definition: UT_Rect.h:210
IFDmantra py
Definition: HDK_Image.dox:266
void setExcl(int x1_, int y1_, int x2_, int y2_)
Set rectangle values.
Definition: UT_Rect.h:312
bool isEmpty() const
Return true if rectangle is empty.
Definition: UT_Rect.h:142
void setY2(int y_)
Set upper-right corner (inclusive), without changing lower-left.
Definition: UT_Rect.h:265
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:107
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2001
double fpreal
Definition: SYS_Types.h:263
UT_Rect(const UT_InclusiveRect &r)
Conversion constructor.
Definition: UT_Rect.h:108
void getExcl(int &x1_, int &y1_, int &x2_, int &y2_) const
Get rectangle values.
Definition: UT_Rect.h:295
Base Integer Rectangle class.
UT_Rect(const UT_ExclusiveRect &r)
Conversion constructor.
Definition: UT_Rect.h:109
UT_Rect(const UT_DimRect &r)
Conversion constructor.
Definition: UT_Rect.h:107
GLint GLenum GLint x
Definition: glcorearb.h:408
GLfloat GLfloat v1
Definition: glcorearb.h:816
int y() const
Get lower-left corner.
Definition: UT_Rect.h:226
void setX1(int x_)
Set lower-left corner, without changing the upper-right corner.
Definition: UT_Rect.h:239
void clear()
Clear rect to empty.
Definition: UT_Rect.h:123
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
void setWidth(int awidth)
Set the width/height, always maintaining the lower-left position.
Definition: UT_Rect.h:253
int height() const
Get width/height dimensions.
Definition: UT_Rect.h:248
bool isInsideX(int px) const
Returns true if the rectangle contains the given coord.
Definition: UT_Rect.h:153
int y2() const
Get upper-right corner (inclusive)
Definition: UT_Rect.h:260
GLboolean r
Definition: glcorearb.h:1221
UT_Rect()
Definition: UT_Rect.h:90
void setY(int y_)
Set the lower-left corner position, always maintaining the size.
Definition: UT_Rect.h:234
void scale(fpreal factor)
Scale by given factor.
void setIncl(int v[4])
Set rectangle values.
Definition: UT_Rect.h:310
UT_Rect & operator=(const UT_InclusiveRect &r)
Assignment operator. Can convert from any rect type.
Definition: UT_Rect.h:115
int y1() const
Get lower-left corner.
Definition: UT_Rect.h:228
int y2e() const
Get upper-right corner (exclusive)
Definition: UT_Rect.h:270
void flipX(int awidth)
Flip rectangle within the given width/height.
Definition: UT_Rect.h:174
void setDim(int x_, int y_, int w_, int h_)
Set rectangle values.
Definition: UT_Rect.h:304
void standardize()
Definition: UT_Rect.h:149
void translate(int x, int y)
Translate by given offset.
void getIncl(int &x1_, int &y1_, int &x2_, int &y2_) const
Get rectangle values.
Definition: UT_Rect.h:293
int centerX() const
Get center.
Definition: UT_Rect.h:280