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