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