HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
rect2i.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_GF_RECT2I_H
8 #define PXR_BASE_GF_RECT2I_H
9 
10 /// \file gf/rect2i.h
11 /// \ingroup group_gf_LinearAlgebra
12 
13 #include "pxr/pxr.h"
14 #include "pxr/base/gf/math.h"
15 #include "pxr/base/gf/vec2i.h"
16 #include "pxr/base/gf/api.h"
17 #include "pxr/base/tf/hash.h"
18 
19 #include <iosfwd>
20 
22 
23 /// \class GfRect2i
24 /// \ingroup group_gf_LinearAlgebra
25 ///
26 /// A 2D rectangle with integer coordinates.
27 ///
28 /// A rectangle is internally represented as two corners. We refer to these
29 /// as the min and max corner where the min's x-coordinate and y-coordinate
30 /// are assumed to be less than or equal to the max's corresponding coordinates.
31 /// Normally, it is expressed as a min corner and a size.
32 ///
33 /// Note that the max corner is included when computing the size (width and
34 /// height) of a rectangle as the number of integral points in the x- and
35 /// y-direction. In particular, if the min corner and max corner are the same,
36 /// then the width and the height of the rectangle will both be one since we
37 /// have exactly one integral point with coordinates greater or equal to the
38 /// min corner and less or equal to the max corner.
39 ///
40 /// Specifically, <em> width = maxX - minX + 1</em> and
41 /// <em>height = maxY - minY + 1.</em>
42 ///
43 class GfRect2i {
44 public:
45  /// Constructs an empty rectangle.
46  GfRect2i(): _min(0,0), _max(-1,-1)
47  {
48  }
49 
50  /// Constructs a rectangle with \p min and \p max corners.
51  GfRect2i(const GfVec2i& min, const GfVec2i& max)
52  : _min(min), _max(max)
53  {
54  }
55 
56  /// Constructs a rectangle with \p min corner and the indicated \p width
57  /// and \p height.
58  GfRect2i(const GfVec2i& min, int width, int height)
59  : _min(min), _max(min + GfVec2i(width-1, height-1))
60  {
61  }
62 
63  /// Returns true if the rectangle is a null rectangle.
64  ///
65  /// A null rectangle has both the width and the height set to 0, that is
66  /// \code
67  /// GetMaxX() == GetMinX() - 1
68  /// \endcode
69  /// and
70  /// \code
71  /// GetMaxY() == GetMinY() - 1
72  /// \endcode
73  /// Remember that if \c GetMinX() and \c GetMaxX() return the same value
74  /// then the rectangle has width 1, and similarly for the height.
75  ///
76  /// A null rectangle is both empty, and not valid.
77  bool IsNull() const {
78  return GetWidth() == 0 && GetHeight() == 0;
79  }
80 
81  /// Returns true if the rectangle is empty.
82  ///
83  /// An empty rectangle has one or both of its min coordinates strictly
84  /// greater than the corresponding max coordinate.
85  ///
86  /// An empty rectangle is not valid.
87  bool IsEmpty() const {
88  return GetWidth() <= 0 || GetHeight() <= 0;
89  }
90 
91  /// Return true if the rectangle is valid (equivalently, not empty).
92  bool IsValid() const {
93  return !IsEmpty();
94  }
95 
96  /// Returns a normalized rectangle, i.e. one that has a non-negative width
97  /// and height.
98  ///
99  /// \c GetNormalized() swaps the min and max x-coordinates to
100  /// ensure a non-negative width, and similarly for the
101  /// y-coordinates.
102  GF_API
103  GfRect2i GetNormalized() const;
104 
105  /// Returns the min corner of the rectangle.
106  const GfVec2i& GetMin() const {
107  return _min;
108  }
109 
110  /// Returns the max corner of the rectangle.
111  const GfVec2i& GetMax() const {
112  return _max;
113  }
114 
115  /// Return the X value of min corner.
116  ///
117  int GetMinX() const {
118  return _min[0];
119  }
120 
121  /// Set the X value of the min corner.
122  ///
123  void SetMinX(int x) {
124  _min[0] = x;
125  }
126 
127  /// Return the X value of the max corner.
128  ///
129  int GetMaxX() const {
130  return _max[0];
131  }
132 
133  /// Set the X value of the max corner
134  void SetMaxX(int x) {
135  _max[0] = x;
136  }
137 
138  /// Return the Y value of the min corner
139  ///
140  int GetMinY() const {
141  return _min[1];
142  }
143 
144  /// Set the Y value of the min corner.
145  ///
146  void SetMinY(int y) {
147  _min[1] = y;
148  }
149 
150  /// Return the Y value of the max corner
151  int GetMaxY() const {
152  return _max[1];
153  }
154 
155  /// Set the Y value of the max corner
156  void SetMaxY(int y) {
157  _max[1] = y;
158  }
159 
160  /// Sets the min corner of the rectangle.
161  void SetMin(const GfVec2i& min) {
162  _min = min;
163  }
164 
165  /// Sets the max corner of the rectangle.
166  void SetMax(const GfVec2i& max) {
167  _max = max;
168  }
169 
170  /// Returns the center point of the rectangle.
171  GfVec2i GetCenter() const {
172  return (_min + _max) / 2;
173  }
174 
175  /// Move the rectangle by \p displ.
176  void Translate(const GfVec2i& displacement) {
177  _min += displacement;
178  _max += displacement;
179  }
180 
181  /// Return the area of the rectangle.
182  unsigned long GetArea() const {
183  return (unsigned long)GetWidth() * (unsigned long)GetHeight();
184  }
185 
186  /// Returns the size of the rectangle as a vector (width,height).
187  GfVec2i GetSize() const {
188  return GfVec2i(GetWidth(), GetHeight());
189  }
190 
191  /// Returns the width of the rectangle.
192  ///
193  /// \note If the min and max x-coordinates are coincident, the width is
194  /// one.
195  int GetWidth() const {
196  return (_max[0] - _min[0]) + 1;
197  }
198 
199  /// Returns the height of the rectangle.
200  ///
201  /// \note If the min and max y-coordinates are coincident, the height is
202  /// one.
203  int GetHeight() const {
204  return (_max[1] - _min[1]) + 1;
205  }
206 
207  /// Computes the intersection of two rectangles.
208  GfRect2i GetIntersection(const GfRect2i& that) const {
209  if(IsEmpty())
210  return *this;
211  else if(that.IsEmpty())
212  return that;
213  else
214  return GfRect2i(GfVec2i(GfMax(_min[0], that._min[0]),
215  GfMax(_min[1], that._min[1])),
216  GfVec2i(GfMin(_max[0], that._max[0]),
217  GfMin(_max[1], that._max[1])));
218  }
219 
220  /// Computes the intersection of two rectangles.
221  /// \deprecated Use GetIntersection() instead
222  GfRect2i Intersect(const GfRect2i& that) const {
223  return GetIntersection(that);
224  }
225 
226  /// Computes the union of two rectangles.
227  GfRect2i GetUnion(const GfRect2i& that) const {
228  if(IsEmpty())
229  return that;
230  else if(that.IsEmpty())
231  return *this;
232  else
233  return GfRect2i(GfVec2i(GfMin(_min[0], that._min[0]),
234  GfMin(_min[1], that._min[1])),
235  GfVec2i(GfMax(_max[0], that._max[0]),
236  GfMax(_max[1], that._max[1])));
237  }
238 
239  /// Computes the union of two rectangles
240  /// \deprecated Use GetUnion() instead.
241  GfRect2i Union(const GfRect2i& that) const {
242  return GetUnion(that);
243  }
244 
245  /// Returns true if the specified point in the rectangle.
246  bool Contains(const GfVec2i& p) const {
247  return ((p[0] >= _min[0]) && (p[0] <= _max[0]) &&
248  (p[1] >= _min[1]) && (p[1] <= _max[1]));
249  }
250 
251  friend inline size_t hash_value(const GfRect2i &r) {
252  return TfHash::Combine(r._min, r._max);
253  }
254 
255  /// Returns true if \p r1 and \p r2 are equal.
256  friend bool operator==(const GfRect2i& r1, const GfRect2i& r2) {
257  return r1._min == r2._min && r1._max == r2._max;
258  }
259 
260  /// Returns true if \p r1 and \p r2 are different.
261  friend bool operator!=(const GfRect2i& r1, const GfRect2i& r2) {
262  return !(r1 == r2);
263  }
264 
265  /// Computes the union of two rectangles.
266  /// \see GetUnion()
268  *this = GetUnion(that);
269  return *this;
270  }
271 
272  friend GfRect2i operator + (const GfRect2i r1, const GfRect2i& r2) {
273  GfRect2i tmp(r1);
274  tmp += r2;
275  return tmp;
276  }
277 
278 private:
279  GfVec2i _min, _max;
280 };
281 
282 /// Output a GfRect2i using the format [(x y):(x y)].
283 /// \ingroup group_gf_DebuggingOutput
284 GF_API std::ostream& operator<<(std::ostream&, const GfRect2i&);
285 
287 
288 #endif
void SetMax(const GfVec2i &max)
Sets the max corner of the rectangle.
Definition: rect2i.h:166
Definition: vec2i.h:43
GfRect2i(const GfVec2i &min, const GfVec2i &max)
Constructs a rectangle with min and max corners.
Definition: rect2i.h:51
bool IsEmpty() const
Definition: rect2i.h:87
const GfVec2i & GetMax() const
Returns the max corner of the rectangle.
Definition: rect2i.h:111
GfRect2i operator+=(const GfRect2i &that)
Definition: rect2i.h:267
unsigned long GetArea() const
Return the area of the rectangle.
Definition: rect2i.h:182
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLint y
Definition: glcorearb.h:103
friend size_t hash_value(const GfRect2i &r)
Definition: rect2i.h:251
friend bool operator!=(const GfRect2i &r1, const GfRect2i &r2)
Returns true if r1 and r2 are different.
Definition: rect2i.h:261
bool IsNull() const
Definition: rect2i.h:77
void Translate(const GfVec2i &displacement)
Move the rectangle by displ.
Definition: rect2i.h:176
void SetMinY(int y)
Definition: rect2i.h:146
T GfMin(T a1, T a2)
Definition: math.h:307
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
T GfMax(T a1, T a2)
Definition: math.h:326
GfRect2i Intersect(const GfRect2i &that) const
Definition: rect2i.h:222
int GetMinY() const
Definition: rect2i.h:140
int GetHeight() const
Definition: rect2i.h:203
GfRect2i()
Constructs an empty rectangle.
Definition: rect2i.h:46
GfVec2i GetCenter() const
Returns the center point of the rectangle.
Definition: rect2i.h:171
GLint GLenum GLint x
Definition: glcorearb.h:409
GF_API std::ostream & operator<<(std::ostream &, const GfRect2i &)
GfRect2i Union(const GfRect2i &that) const
Definition: rect2i.h:241
int GetMaxX() const
Definition: rect2i.h:129
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
GfVec2i GetSize() const
Returns the size of the rectangle as a vector (width,height).
Definition: rect2i.h:187
friend GfRect2i operator+(const GfRect2i r1, const GfRect2i &r2)
Definition: rect2i.h:272
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
GfRect2i GetUnion(const GfRect2i &that) const
Computes the union of two rectangles.
Definition: rect2i.h:227
bool IsValid() const
Return true if the rectangle is valid (equivalently, not empty).
Definition: rect2i.h:92
bool Contains(const GfVec2i &p) const
Returns true if the specified point in the rectangle.
Definition: rect2i.h:246
int GetWidth() const
Definition: rect2i.h:195
void SetMaxX(int x)
Set the X value of the max corner.
Definition: rect2i.h:134
int GetMinX() const
Definition: rect2i.h:117
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
friend bool operator==(const GfRect2i &r1, const GfRect2i &r2)
Returns true if r1 and r2 are equal.
Definition: rect2i.h:256
GLint GLsizei width
Definition: glcorearb.h:103
const GfVec2i & GetMin() const
Returns the min corner of the rectangle.
Definition: rect2i.h:106
GLboolean r
Definition: glcorearb.h:1222
GfRect2i GetIntersection(const GfRect2i &that) const
Computes the intersection of two rectangles.
Definition: rect2i.h:208
void SetMinX(int x)
Definition: rect2i.h:123
void SetMaxY(int y)
Set the Y value of the max corner.
Definition: rect2i.h:156
GF_API GfRect2i GetNormalized() const
void SetMin(const GfVec2i &min)
Sets the min corner of the rectangle.
Definition: rect2i.h:161
GfRect2i(const GfVec2i &min, int width, int height)
Definition: rect2i.h:58
int GetMaxY() const
Return the Y value of the max corner.
Definition: rect2i.h:151
#define GF_API
Definition: api.h:23