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