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 
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 
38 PXR_NAMESPACE_OPEN_SCOPE
39 
60 class GfRect2i {
61 public:
63  GfRect2i(): _min(0,0), _max(-1,-1)
64  {
65  }
66 
68  GfRect2i(const GfVec2i& min, const GfVec2i& max)
69  : _min(min), _max(max)
70  {
71  }
72 
75  GfRect2i(const GfVec2i& min, int width, int height)
76  : _min(min), _max(min + GfVec2i(width-1, height-1))
77  {
78  }
79 
94  bool IsNull() const {
95  return GetWidth() == 0 && GetHeight() == 0;
96  }
97 
104  bool IsEmpty() const {
105  return GetWidth() <= 0 || GetHeight() <= 0;
106  }
107 
109  bool IsValid() const {
110  return !IsEmpty();
111  }
112 
119  GF_API
120  GfRect2i GetNormalized() const;
121 
123  const GfVec2i& GetMin() const {
124  return _min;
125  }
126 
128  const GfVec2i& GetMax() const {
129  return _max;
130  }
131 
134  int GetMinX() const {
135  return _min[0];
136  }
137 
140  void SetMinX(int x) {
141  _min[0] = x;
142  }
143 
146  int GetMaxX() const {
147  return _max[0];
148  }
149 
151  void SetMaxX(int x) {
152  _max[0] = x;
153  }
154 
157  int GetMinY() const {
158  return _min[1];
159  }
160 
163  void SetMinY(int y) {
164  _min[1] = y;
165  }
166 
168  int GetMaxY() const {
169  return _max[1];
170  }
171 
173  void SetMaxY(int y) {
174  _max[1] = y;
175  }
176 
178  void SetMin(const GfVec2i& min) {
179  _min = min;
180  }
181 
183  void SetMax(const GfVec2i& max) {
184  _max = max;
185  }
186 
188  GfVec2i GetCenter() const {
189  return (_min + _max) / 2;
190  }
191 
193  void Translate(const GfVec2i& displacement) {
194  _min += displacement;
195  _max += displacement;
196  }
197 
199  unsigned long GetArea() const {
200  return (unsigned long)GetWidth() * (unsigned long)GetHeight();
201  }
202 
204  GfVec2i GetSize() const {
205  return GfVec2i(GetWidth(), GetHeight());
206  }
207 
212  int GetWidth() const {
213  return (_max[0] - _min[0]) + 1;
214  }
215 
220  int GetHeight() const {
221  return (_max[1] - _min[1]) + 1;
222  }
223 
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 
239  GfRect2i Intersect(const GfRect2i& that) const {
240  return GetIntersection(that);
241  }
242 
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 
258  GfRect2i Union(const GfRect2i& that) const {
259  return GetUnion(that);
260  }
261 
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 
273  friend bool operator==(const GfRect2i& r1, const GfRect2i& r2) {
274  return r1._min == r2._min && r1._max == r2._max;
275  }
276 
278  friend bool operator!=(const GfRect2i& r1, const GfRect2i& r2) {
279  return !(r1 == r2);
280  }
281 
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 
301 GF_API std::ostream& operator<<(std::ostream&, const GfRect2i&);
302 
303 PXR_NAMESPACE_CLOSE_SCOPE
304 
305 #endif
GfRect2i(const GfVec2i &min, const GfVec2i &max)
Constructs a rectangle with min and max corners.
Definition: rect2i.h:68
void SetMax(const GfVec2i &max)
Sets the max corner of the rectangle.
Definition: rect2i.h:183
Basic type for a vector of 2 int components.
Definition: vec2i.h:60
int GetWidth() const
Returns the width of the rectangle.
Definition: rect2i.h:212
bool IsValid() const
Return true if the rectangle is valid (equivalently, not empty).
Definition: rect2i.h:109
int GetMinX() const
Return the X value of min corner.
Definition: rect2i.h:134
Assorted mathematical utility functions.
const GfVec2i & GetMax() const
Returns the max corner of the rectangle.
Definition: rect2i.h:128
A 2D rectangle with integer coordinates.
Definition: rect2i.h:60
int GetMaxY() const
Return the Y value of the max corner.
Definition: rect2i.h:168
void SetMaxX(int x)
Set the X value of the max corner.
Definition: rect2i.h:151
GfRect2i()
Constructs an empty rectangle.
Definition: rect2i.h:63
GF_API GfRect2i GetNormalized() const
Returns a normalized rectangle, i.e.
friend bool operator!=(const GfRect2i &r1, const GfRect2i &r2)
Returns true if r1 and r2 are different.
Definition: rect2i.h:278
T GfMin(T a1, T a2)
Returns the smallest of the given values.
Definition: math.h:200
void SetMaxY(int y)
Set the Y value of the max corner.
Definition: rect2i.h:173
void SetMin(const GfVec2i &min)
Sets the min corner of the rectangle.
Definition: rect2i.h:178
int GetMaxX() const
Return the X value of the max corner.
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
GfRect2i(const GfVec2i &min, int width, int height)
Constructs a rectangle with min corner and the indicated width and height.
Definition: rect2i.h:75
const GfVec2i & GetMin() const
Returns the min corner of the rectangle.
Definition: rect2i.h:123
bool IsNull() const
Returns true if the rectangle is a null rectangle.
Definition: rect2i.h:94
int GetMinY() const
Return the Y value of the min corner.
Definition: rect2i.h:157
GfRect2i operator+=(const GfRect2i &that)
Computes the union of two rectangles.
Definition: rect2i.h:284
T GfMax(T a1, T a2)
Returns the largest of the given values.
Definition: math.h:219
GfVec2i GetSize() const
Returns the size of the rectangle as a vector (width,height).
Definition: rect2i.h:204
void SetMinY(int y)
Set the Y value of the min corner.
Definition: rect2i.h:163
GF_API std::ostream & operator<<(std::ostream &, const GfRect2i &)
Output a GfRect2i using the format [(x y):(x y)].
unsigned long GetArea() const
Return the area of the rectangle.
Definition: rect2i.h:199
GfRect2i Union(const GfRect2i &that) const
Computes the union of two rectangles.
Definition: rect2i.h:258
friend bool operator==(const GfRect2i &r1, const GfRect2i &r2)
Returns true if r1 and r2 are equal.
Definition: rect2i.h:273
void SetMinX(int x)
Set the X value of the min corner.
Definition: rect2i.h:140
bool IsEmpty() const
Returns true if the rectangle is empty.
Definition: rect2i.h:104
void Translate(const GfVec2i &displacement)
Move the rectangle by displ.
Definition: rect2i.h:193
GfRect2i GetIntersection(const GfRect2i &that) const
Computes the intersection of two rectangles.
Definition: rect2i.h:225
GfRect2i GetUnion(const GfRect2i &that) const
Computes the union of two rectangles.
Definition: rect2i.h:244
GfRect2i Intersect(const GfRect2i &that) const
Computes the intersection of two rectangles.
Definition: rect2i.h:239
GfVec2i GetCenter() const
Returns the center point of the rectangle.
Definition: rect2i.h:188
int GetHeight() const
Returns the height of the rectangle.
Definition: rect2i.h:220
bool Contains(const GfVec2i &p) const
Returns true if the specified point in the rectangle.
Definition: rect2i.h:263