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 
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 
65 class GfRect2i {
66 public:
68  GfRect2i(): _lower(0,0), _higher(-1,-1)
69  {
70  }
71 
74  GfRect2i(const GfVec2i& topLeft, const GfVec2i& bottomRight)
75  : _lower(topLeft), _higher(bottomRight)
76  {
77  }
78 
81  GfRect2i(const GfVec2i& topLeft, int width, int height)
82  : _lower(topLeft), _higher(topLeft + GfVec2i(width-1, height-1))
83  {
84  }
85 
100  bool IsNull() const {
101  return GetWidth() == 0 && GetHeight() == 0;
102  }
103 
110  bool IsEmpty() const {
111  return GetWidth() <= 0 || GetHeight() <= 0;
112  }
113 
115  bool IsValid() const {
116  return !IsEmpty();
117  }
118 
124  GF_API
125  GfRect2i GetNormalized() const;
126 
128  const GfVec2i& GetLower() const {
129  return _lower;
130  }
131 
133  const GfVec2i& GetHigher() const {
134  return _higher;
135  }
136 
138  int GetLeft() const {
139  return _lower[0];
140  }
141 
143  void SetLeft(int x) {
144  _lower[0] = x;
145  }
146 
148  int GetRight() const {
149  return _higher[0];
150  }
151 
153  void SetRight(int x) {
154  _higher[0] = x;
155  }
156 
158  int GetTop() const {
159  return _lower[1];
160  }
161 
163  void SetTop(int y) {
164  _lower[1] = y;
165  }
166 
168  int GetBottom() const {
169  return _higher[1];
170  }
171 
173  void SetBottom(int y) {
174  _higher[1] = y;
175  }
176 
178  void SetLower(const GfVec2i& lower) {
179  _lower = lower;
180  }
181 
183  void SetHigher(const GfVec2i& higher) {
184  _higher = higher;
185  }
186 
188  GfVec2i GetCenter() const {
189  return (_lower + _higher) / 2;
190  }
191 
193  void Translate(const GfVec2i& displacement) {
194  _lower += displacement;
195  _higher += 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 
211  int GetWidth() const {
212  return (_higher[0] - _lower[0]) + 1;
213  }
214 
218  int GetHeight() const {
219  return (_higher[1] - _lower[1]) + 1;
220  }
221 
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 
237  GfRect2i Intersect(const GfRect2i& that) const {
238  return GetIntersection(that);
239  }
240 
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 
256  GfRect2i Union(const GfRect2i& that) const {
257  return GetUnion(that);
258  }
259 
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  boost::hash_combine(h, r._lower);
269  boost::hash_combine(h, r._higher);
270  return h;
271  }
272 
274  friend bool operator==(const GfRect2i& r1, const GfRect2i& r2) {
275  return r1._lower == r2._lower && r1._higher == r2._higher;
276  }
277 
279  friend bool operator!=(const GfRect2i& r1, const GfRect2i& r2) {
280  return !(r1 == r2);
281  }
282 
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 
302 GF_API std::ostream& operator<<(std::ostream&, const GfRect2i&);
303 
304 PXR_NAMESPACE_CLOSE_SCOPE
305 
306 #endif
Basic type for a vector of 2 int components.
Definition: vec2i.h:61
A 2D rectangle with integer coordinates for windowing operations.
Definition: rect2i.h:65
GfRect2i()
Constructs an empty rectangle.
Definition: rect2i.h:68
void SetLower(const GfVec2i &lower)
Sets the lower corner of the rectangle.
Definition: rect2i.h:178
int GetLeft() const
Return the X value of the left edge.
Definition: rect2i.h:138
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:115
unsigned long GetArea() const
Return the area of the rectangle.
Definition: rect2i.h:199
friend bool operator!=(const GfRect2i &r1, const GfRect2i &r2)
Returns true if r1 and r2 are different.
Definition: rect2i.h:279
T GfMin(T a1, T a2)
Returns the smallest of the given values.
Definition: math.h:197
int GetTop() const
Return the Y value of the top edge.
Definition: rect2i.h:158
GfRect2i GetUnion(const GfRect2i &that) const
Computes the union of two rectangles.
Definition: rect2i.h:242
bool IsEmpty() const
Returns true if the rectangle is empty.
Definition: rect2i.h:110
GfRect2i(const GfVec2i &topLeft, int width, int height)
Constructs a rectangle with topLeft as the top left corner and with the indicated width and height...
Definition: rect2i.h:81
GfRect2i Intersect(const GfRect2i &that) const
Computes the intersection of two rectangles.
Definition: rect2i.h:237
int GetBottom() const
Return the Y value of the bottom edge.
Definition: rect2i.h:168
void SetLeft(int x)
Set the X value of the left edge.
Definition: rect2i.h:143
int GetHeight() const
Returns the height of the rectangle.
Definition: rect2i.h:218
GfRect2i GetIntersection(const GfRect2i &that) const
Computes the intersection of two rectangles.
Definition: rect2i.h:223
GfRect2i operator+=(const GfRect2i &that)
Computes the union of two rectangles.
Definition: rect2i.h:285
bool Contains(const GfVec2i &p) const
Returns true if the specified point in the rectangle.
Definition: rect2i.h:261
T GfMax(T a1, T a2)
Returns the largest of the given values.
Definition: math.h:216
void SetRight(int x)
Set the X value of the right edge.
Definition: rect2i.h:153
const GfVec2i & GetHigher() const
Returns the upper corner of the rectangle.
Definition: rect2i.h:133
bool IsNull() const
Returns true if the rectangle is a null rectangle.
Definition: rect2i.h:100
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
void SetBottom(int y)
Set the Y value of the bottom edge.
Definition: rect2i.h:173
GfVec2i GetCenter() const
Returns the center point of the rectangle.
Definition: rect2i.h:188
GfRect2i Union(const GfRect2i &that) const
Computes the union of two rectangles.
Definition: rect2i.h:256
GfVec2i GetSize() const
Returns the size of the rectangle as a vector (width,height).
Definition: rect2i.h:204
friend bool operator==(const GfRect2i &r1, const GfRect2i &r2)
Returns true if r1 and r2 are equal.
Definition: rect2i.h:274
void SetTop(int y)
Set the Y value of the top edge.
Definition: rect2i.h:163
const GfVec2i & GetLower() const
Returns the lower corner of the rectangle.
Definition: rect2i.h:128
void SetHigher(const GfVec2i &higher)
Sets the upper corner of the rectangle.
Definition: rect2i.h:183
void Translate(const GfVec2i &displacement)
Move the rectangle by displ.
Definition: rect2i.h:193
int GetWidth() const
Returns the width of the rectangle.
Definition: rect2i.h:211
int GetRight() const
Return the X value of the right edge.
Definition: rect2i.h:148
GfRect2i(const GfVec2i &topLeft, const GfVec2i &bottomRight)
Constructs a rectangle with topLeft as the top left corner and bottomRight as the bottom right corner...
Definition: rect2i.h:74