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 GF_RECT2I_H
25 #define 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 for windowing operations.
45 ///
46 /// A rectangle is internally represented as an upper left corner and a
47 /// bottom right corner, but it is normally expressed as an upper left
48 /// corner and a size.
49 ///
50 /// Note that the size (width and height) of a rectangle might be
51 /// different from what you are used to. If the top left corner and the
52 /// bottom right corner are the same, then the height and the width of
53 /// the rectangle will both be one.
54 ///
55 /// Specifically, <em> width = right - left + 1</em> and
56 /// <em>height = bottom - top + 1.</em> The design corresponds to
57 /// rectangular spaces used by drawing functions, where
58 /// the width and height denote a number of pixels. For example,
59 /// drawing a rectangle with width and height one draws a single pixel.
60 ///
61 /// The default coordinate system has origin (0,0) in the top left
62 /// corner, the positive direction of the y axis is downward and the
63 /// positive x axis is to the right.
64 ///
65 class GfRect2i {
66 public:
67  /// Constructs an empty rectangle.
68  GfRect2i(): _lower(0,0), _higher(-1,-1)
69  {
70  }
71 
72  /// Constructs a rectangle with \p topLeft as the top left corner and \p
73  /// bottomRight as the bottom right corner.
74  GfRect2i(const GfVec2i& topLeft, const GfVec2i& bottomRight)
75  : _lower(topLeft), _higher(bottomRight)
76  {
77  }
78 
79  /// Constructs a rectangle with \p topLeft as the top left corner and with
80  /// the indicated width and height.
81  GfRect2i(const GfVec2i& topLeft, int width, int height)
82  : _lower(topLeft), _higher(topLeft + GfVec2i(width-1, height-1))
83  {
84  }
85 
86  /// Returns true if the rectangle is a null rectangle.
87  ///
88  /// A null rectangle has both the width and the height set to 0, that is
89  /// \code
90  /// GetRight() == GetLeft() - 1
91  /// \endcode
92  /// and
93  /// \code
94  /// GetBottom() == GetTop() - 1
95  /// \endcode
96  /// Remember that if \c GetRight() and \c GetLeft() return the same value
97  /// then the rectangle has width 1, and similarly for the height.
98  ///
99  /// A null rectangle is both empty, and not valid.
100  bool IsNull() const {
101  return GetWidth() == 0 && GetHeight() == 0;
102  }
103 
104  /// Returns true if the rectangle is empty.
105  ///
106  /// An empty rectangle has its left side strictly greater than its right
107  /// side or its top strictly greater than its bottom.
108  ///
109  /// An empty rectangle is not valid.
110  bool IsEmpty() const {
111  return GetWidth() <= 0 || GetHeight() <= 0;
112  }
113 
114  /// Return true if the rectangle is valid (equivalently, not empty).
115  bool IsValid() const {
116  return !IsEmpty();
117  }
118 
119  /// Returns a normalized rectangle, i.e. one that has a non-negative width
120  /// and height.
121  ///
122  /// \c GetNormalized() swaps left and right to ensure a non-negative
123  /// width, and similarly for top and bottom.
124  GF_API
125  GfRect2i GetNormalized() const;
126 
127  /// Returns the lower corner of the rectangle.
128  const GfVec2i& GetLower() const {
129  return _lower;
130  }
131 
132  /// Returns the upper corner of the rectangle.
133  const GfVec2i& GetHigher() const {
134  return _higher;
135  }
136 
137  /// Return the X value of the left edge.
138  int GetLeft() const {
139  return _lower[0];
140  }
141 
142  /// Set the X value of the left edge.
143  void SetLeft(int x) {
144  _lower[0] = x;
145  }
146 
147  /// Return the X value of the right edge.
148  int GetRight() const {
149  return _higher[0];
150  }
151 
152  /// Set the X value of the right edge.
153  void SetRight(int x) {
154  _higher[0] = x;
155  }
156 
157  /// Return the Y value of the top edge.
158  int GetTop() const {
159  return _lower[1];
160  }
161 
162  /// Set the Y value of the top edge.
163  void SetTop(int y) {
164  _lower[1] = y;
165  }
166 
167  /// Return the Y value of the bottom edge.
168  int GetBottom() const {
169  return _higher[1];
170  }
171 
172  /// Set the Y value of the bottom edge.
173  void SetBottom(int y) {
174  _higher[1] = y;
175  }
176 
177  /// Sets the lower corner of the rectangle.
178  void SetLower(const GfVec2i& lower) {
179  _lower = lower;
180  }
181 
182  /// Sets the upper corner of the rectangle.
183  void SetHigher(const GfVec2i& higher) {
184  _higher = higher;
185  }
186 
187  /// Returns the center point of the rectangle.
188  GfVec2i GetCenter() const {
189  return (_lower + _higher) / 2;
190  }
191 
192  /// Move the rectangle by \p displ.
193  void Translate(const GfVec2i& displacement) {
194  _lower += displacement;
195  _higher += 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 left and right sides are coincident, the width is one.
211  int GetWidth() const {
212  return (_higher[0] - _lower[0]) + 1;
213  }
214 
215  /// Returns the height of the rectangle.
216  ///
217  /// \note If the top and bottom sides are coincident, the height is one.
218  int GetHeight() const {
219  return (_higher[1] - _lower[1]) + 1;
220  }
221 
222  /// Computes the intersection of two rectangles.
223  GfRect2i GetIntersection(const GfRect2i& that) const {
224  if(IsEmpty())
225  return *this;
226  else if(that.IsEmpty())
227  return that;
228  else
229  return GfRect2i(GfVec2i(GfMax(_lower[0], that._lower[0]),
230  GfMax(_lower[1], that._lower[1])),
231  GfVec2i(GfMin(_higher[0], that._higher[0]),
232  GfMin(_higher[1], that._higher[1])));
233  }
234 
235  /// Computes the intersection of two rectangles.
236  /// \deprecated Use GetIntersection() instead
237  GfRect2i Intersect(const GfRect2i& that) const {
238  return GetIntersection(that);
239  }
240 
241  /// Computes the union of two rectangles.
242  GfRect2i GetUnion(const GfRect2i& that) const {
243  if(IsEmpty())
244  return that;
245  else if(that.IsEmpty())
246  return *this;
247  else
248  return GfRect2i(GfVec2i(GfMin(_lower[0], that._lower[0]),
249  GfMin(_lower[1], that._lower[1])),
250  GfVec2i(GfMax(_higher[0], that._higher[0]),
251  GfMax(_higher[1], that._higher[1])));
252  }
253 
254  /// Computes the union of two rectangles
255  /// \deprecated Use GetUnion() instead.
256  GfRect2i Union(const GfRect2i& that) const {
257  return GetUnion(that);
258  }
259 
260  /// Returns true if the specified point in the rectangle.
261  bool Contains(const GfVec2i& p) const {
262  return ((p[0] >= _lower[0]) && (p[0] <= _higher[0]) &&
263  (p[1] >= _lower[1]) && (p[1] <= _higher[1]));
264  }
265 
266  friend inline size_t hash_value(const GfRect2i &r) {
267  size_t h = 0;
268  hboost::hash_combine(h, r._lower);
269  hboost::hash_combine(h, r._higher);
270  return h;
271  }
272 
273  /// Returns true if \p r1 and \p r2 are equal.
274  friend bool operator==(const GfRect2i& r1, const GfRect2i& r2) {
275  return r1._lower == r2._lower && r1._higher == r2._higher;
276  }
277 
278  /// Returns true if \p r1 and \p r2 are different.
279  friend bool operator!=(const GfRect2i& r1, const GfRect2i& r2) {
280  return !(r1 == r2);
281  }
282 
283  /// Computes the union of two rectangles.
284  /// \see GetUnion()
286  *this = GetUnion(that);
287  return *this;
288  }
289 
290  friend GfRect2i operator + (const GfRect2i r1, const GfRect2i& r2) {
291  GfRect2i tmp(r1);
292  tmp += r2;
293  return tmp;
294  }
295 
296 private:
297  GfVec2i _lower, _higher;
298 };
299 
300 /// Output a GfRect2i using the format [(x y):(x y)].
301 /// \ingroup group_gf_DebuggingOutput
302 GF_API std::ostream& operator<<(std::ostream&, const GfRect2i&);
303 
305 
306 #endif
Definition: vec2i.h:61
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1252
bool IsEmpty() const
Definition: rect2i.h:110
int GetLeft() const
Return the X value of the left edge.
Definition: rect2i.h:138
GfRect2i operator+=(const GfRect2i &that)
Definition: rect2i.h:285
GLint GLint GLint GLint GLint GLint GLsizei GLsizei height
Definition: glew.h:1252
unsigned long GetArea() const
Return the area of the rectangle.
Definition: rect2i.h:199
friend size_t hash_value(const GfRect2i &r)
Definition: rect2i.h:266
void SetLower(const GfVec2i &lower)
Sets the lower corner of the rectangle.
Definition: rect2i.h:178
friend bool operator!=(const GfRect2i &r1, const GfRect2i &r2)
Returns true if r1 and r2 are different.
Definition: rect2i.h:279
bool IsNull() const
Definition: rect2i.h:100
void Translate(const GfVec2i &displacement)
Move the rectangle by displ.
Definition: rect2i.h:193
const GfVec2i & GetHigher() const
Returns the upper corner of the rectangle.
Definition: rect2i.h:133
T GfMin(T a1, T a2)
Definition: math.h:197
void SetLeft(int x)
Set the X value of the left edge.
Definition: rect2i.h:143
void SetBottom(int y)
Set the Y value of the bottom edge.
Definition: rect2i.h:173
GLint GLint GLint GLint GLint x
Definition: glew.h:1252
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1252
void SetHigher(const GfVec2i &higher)
Sets the upper corner of the rectangle.
Definition: rect2i.h:183
T GfMax(T a1, T a2)
Definition: math.h:216
GfRect2i Intersect(const GfRect2i &that) const
Definition: rect2i.h:237
GfRect2i(const GfVec2i &topLeft, const GfVec2i &bottomRight)
Definition: rect2i.h:74
int GetHeight() const
Definition: rect2i.h:218
GfRect2i()
Constructs an empty rectangle.
Definition: rect2i.h:68
GfRect2i(const GfVec2i &topLeft, int width, int height)
Definition: rect2i.h:81
GfVec2i GetCenter() const
Returns the center point of the rectangle.
Definition: rect2i.h:188
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:8011
void SetRight(int x)
Set the X value of the right edge.
Definition: rect2i.h:153
int GetTop() const
Return the Y value of the top edge.
Definition: rect2i.h:158
int GetBottom() const
Return the Y value of the bottom edge.
Definition: rect2i.h:168
int GetRight() const
Return the X value of the right edge.
Definition: rect2i.h:148
GF_API std::ostream & operator<<(std::ostream &, const GfRect2i &)
GLfloat GLfloat p
Definition: glew.h:16321
GfRect2i Union(const GfRect2i &that) const
Definition: rect2i.h:256
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:290
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1245
GfRect2i GetUnion(const GfRect2i &that) const
Computes the union of two rectangles.
Definition: rect2i.h:242
bool IsValid() const
Return true if the rectangle is valid (equivalently, not empty).
Definition: rect2i.h:115
bool Contains(const GfVec2i &p) const
Returns true if the specified point in the rectangle.
Definition: rect2i.h:261
const GfVec2i & GetLower() const
Returns the lower corner of the rectangle.
Definition: rect2i.h:128
int GetWidth() const
Definition: rect2i.h:211
GLdouble GLdouble GLdouble r
Definition: glew.h:1406
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:83
friend bool operator==(const GfRect2i &r1, const GfRect2i &r2)
Returns true if r1 and r2 are equal.
Definition: rect2i.h:274
GfRect2i GetIntersection(const GfRect2i &that) const
Computes the intersection of two rectangles.
Definition: rect2i.h:223
void SetTop(int y)
Set the Y value of the top edge.
Definition: rect2i.h:163
GF_API GfRect2i GetNormalized() const
#define GF_API
Definition: api.h:40