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 
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 <boost/functional/hash.hpp>
36 
37 #include <iosfwd>
38 
39 PXR_NAMESPACE_OPEN_SCOPE
40 
61 class GfRect2i {
62 public:
64  GfRect2i(): _min(0,0), _max(-1,-1)
65  {
66  }
67 
69  GfRect2i(const GfVec2i& min, const GfVec2i& max)
70  : _min(min), _max(max)
71  {
72  }
73 
76  GfRect2i(const GfVec2i& min, int width, int height)
77  : _min(min), _max(min + GfVec2i(width-1, height-1))
78  {
79  }
80 
95  bool IsNull() const {
96  return GetWidth() == 0 && GetHeight() == 0;
97  }
98 
105  bool IsEmpty() const {
106  return GetWidth() <= 0 || GetHeight() <= 0;
107  }
108 
110  bool IsValid() const {
111  return !IsEmpty();
112  }
113 
120  GF_API
121  GfRect2i GetNormalized() const;
122 
124  const GfVec2i& GetMin() const {
125  return _min;
126  }
127 
129  const GfVec2i& GetMax() const {
130  return _max;
131  }
132 
135  int GetMinX() const {
136  return _min[0];
137  }
138 
141  void SetMinX(int x) {
142  _min[0] = x;
143  }
144 
147  int GetMaxX() const {
148  return _max[0];
149  }
150 
152  void SetMaxX(int x) {
153  _max[0] = x;
154  }
155 
158  int GetMinY() const {
159  return _min[1];
160  }
161 
164  void SetMinY(int y) {
165  _min[1] = y;
166  }
167 
169  int GetMaxY() const {
170  return _max[1];
171  }
172 
174  void SetMaxY(int y) {
175  _max[1] = y;
176  }
177 
179  void SetMin(const GfVec2i& min) {
180  _min = min;
181  }
182 
184  void SetMax(const GfVec2i& max) {
185  _max = max;
186  }
187 
189  GfVec2i GetCenter() const {
190  return (_min + _max) / 2;
191  }
192 
194  void Translate(const GfVec2i& displacement) {
195  _min += displacement;
196  _max += displacement;
197  }
198 
200  unsigned long GetArea() const {
201  return (unsigned long)GetWidth() * (unsigned long)GetHeight();
202  }
203 
205  GfVec2i GetSize() const {
206  return GfVec2i(GetWidth(), GetHeight());
207  }
208 
213  int GetWidth() const {
214  return (_max[0] - _min[0]) + 1;
215  }
216 
221  int GetHeight() const {
222  return (_max[1] - _min[1]) + 1;
223  }
224 
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 
240  GfRect2i Intersect(const GfRect2i& that) const {
241  return GetIntersection(that);
242  }
243 
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 
259  GfRect2i Union(const GfRect2i& that) const {
260  return GetUnion(that);
261  }
262 
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  boost::hash_combine(h, r._min);
272  boost::hash_combine(h, r._max);
273  return h;
274  }
275 
277  friend bool operator==(const GfRect2i& r1, const GfRect2i& r2) {
278  return r1._min == r2._min && r1._max == r2._max;
279  }
280 
282  friend bool operator!=(const GfRect2i& r1, const GfRect2i& r2) {
283  return !(r1 == r2);
284  }
285 
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 
305 GF_API std::ostream& operator<<(std::ostream&, const GfRect2i&);
306 
307 PXR_NAMESPACE_CLOSE_SCOPE
308 
309 #endif
GfRect2i(const GfVec2i &min, const GfVec2i &max)
Constructs a rectangle with min and max corners.
Definition: rect2i.h:69
int GetMinY() const
Return the Y value of the min corner.
Definition: rect2i.h:158
void SetMax(const GfVec2i &max)
Sets the max corner of the rectangle.
Definition: rect2i.h:184
Basic type for a vector of 2 int components.
Definition: vec2i.h:61
int GetMinX() const
Return the X value of min corner.
Definition: rect2i.h:135
Assorted mathematical utility functions.
A 2D rectangle with integer coordinates.
Definition: rect2i.h:61
void SetMaxX(int x)
Set the X value of the max corner.
Definition: rect2i.h:152
GfRect2i()
Constructs an empty rectangle.
Definition: rect2i.h:64
GF_API GfRect2i GetNormalized() const
Returns a normalized rectangle, i.e.
bool IsValid() const
Return true if the rectangle is valid (equivalently, not empty).
Definition: rect2i.h:110
unsigned long GetArea() const
Return the area of the rectangle.
Definition: rect2i.h:200
friend bool operator!=(const GfRect2i &r1, const GfRect2i &r2)
Returns true if r1 and r2 are different.
Definition: rect2i.h:282
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:174
GfRect2i GetUnion(const GfRect2i &that) const
Computes the union of two rectangles.
Definition: rect2i.h:245
bool IsEmpty() const
Returns true if the rectangle is empty.
Definition: rect2i.h:105
void SetMin(const GfVec2i &min)
Sets the min corner of the rectangle.
Definition: rect2i.h:179
const GfVec2i & GetMax() const
Returns the max corner of the rectangle.
Definition: rect2i.h:129
GfRect2i Intersect(const GfRect2i &that) const
Computes the intersection of two rectangles.
Definition: rect2i.h:240
GfRect2i(const GfVec2i &min, int width, int height)
Constructs a rectangle with min corner and the indicated width and height.
Definition: rect2i.h:76
int GetMaxX() const
Return the X value of the max corner.
Definition: rect2i.h:147
int GetHeight() const
Returns the height of the rectangle.
Definition: rect2i.h:221
int GetMaxY() const
Return the Y value of the max corner.
Definition: rect2i.h:169
GfRect2i GetIntersection(const GfRect2i &that) const
Computes the intersection of two rectangles.
Definition: rect2i.h:226
GfRect2i operator+=(const GfRect2i &that)
Computes the union of two rectangles.
Definition: rect2i.h:288
bool Contains(const GfVec2i &p) const
Returns true if the specified point in the rectangle.
Definition: rect2i.h:264
T GfMax(T a1, T a2)
Returns the largest of the given values.
Definition: math.h:219
void SetMinY(int y)
Set the Y value of the min corner.
Definition: rect2i.h:164
bool IsNull() const
Returns true if the rectangle is a null rectangle.
Definition: rect2i.h:95
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
GfVec2i GetCenter() const
Returns the center point of the rectangle.
Definition: rect2i.h:189
GfRect2i Union(const GfRect2i &that) const
Computes the union of two rectangles.
Definition: rect2i.h:259
GfVec2i GetSize() const
Returns the size of the rectangle as a vector (width,height).
Definition: rect2i.h:205
friend bool operator==(const GfRect2i &r1, const GfRect2i &r2)
Returns true if r1 and r2 are equal.
Definition: rect2i.h:277
void SetMinX(int x)
Set the X value of the min corner.
Definition: rect2i.h:141
void Translate(const GfVec2i &displacement)
Move the rectangle by displ.
Definition: rect2i.h:194
int GetWidth() const
Returns the width of the rectangle.
Definition: rect2i.h:213
const GfVec2i & GetMin() const
Returns the min corner of the rectangle.
Definition: rect2i.h:124