Loading...
Searching...
No Matches
rect2i.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_GF_RECT2I_H
8#define PXR_BASE_GF_RECT2I_H
9
12
13#include "pxr/pxr.h"
14#include "pxr/base/gf/math.h"
15#include "pxr/base/gf/vec2i.h"
16#include "pxr/base/gf/api.h"
17#include "pxr/base/tf/hash.h"
18
19#include <iosfwd>
20
21PXR_NAMESPACE_OPEN_SCOPE
22
43class GfRect2i {
44public:
46 GfRect2i(): _min(0,0), _max(-1,-1)
47 {
48 }
49
51 GfRect2i(const GfVec2i& min, const GfVec2i& max)
52 : _min(min), _max(max)
53 {
54 }
55
58 GfRect2i(const GfVec2i& min, int width, int height)
59 : _min(min), _max(min + GfVec2i(width-1, height-1))
60 {
61 }
62
77 bool IsNull() const {
78 return GetWidth() == 0 && GetHeight() == 0;
79 }
80
87 bool IsEmpty() const {
88 return GetWidth() <= 0 || GetHeight() <= 0;
89 }
90
92 bool IsValid() const {
93 return !IsEmpty();
94 }
95
102 GF_API
104
106 const GfVec2i& GetMin() const {
107 return _min;
108 }
109
111 const GfVec2i& GetMax() const {
112 return _max;
113 }
114
117 int GetMinX() const {
118 return _min[0];
119 }
120
123 void SetMinX(int x) {
124 _min[0] = x;
125 }
126
129 int GetMaxX() const {
130 return _max[0];
131 }
132
134 void SetMaxX(int x) {
135 _max[0] = x;
136 }
137
140 int GetMinY() const {
141 return _min[1];
142 }
143
146 void SetMinY(int y) {
147 _min[1] = y;
148 }
149
151 int GetMaxY() const {
152 return _max[1];
153 }
154
156 void SetMaxY(int y) {
157 _max[1] = y;
158 }
159
161 void SetMin(const GfVec2i& min) {
162 _min = min;
163 }
164
166 void SetMax(const GfVec2i& max) {
167 _max = max;
168 }
169
172 return (_min + _max) / 2;
173 }
174
176 void Translate(const GfVec2i& displacement) {
177 _min += displacement;
178 _max += displacement;
179 }
180
182 unsigned long GetArea() const {
183 return (unsigned long)GetWidth() * (unsigned long)GetHeight();
184 }
185
187 GfVec2i GetSize() const {
188 return GfVec2i(GetWidth(), GetHeight());
189 }
190
195 int GetWidth() const {
196 return (_max[0] - _min[0]) + 1;
197 }
198
203 int GetHeight() const {
204 return (_max[1] - _min[1]) + 1;
205 }
206
208 GfRect2i GetIntersection(const GfRect2i& that) const {
209 if(IsEmpty())
210 return *this;
211 else if(that.IsEmpty())
212 return that;
213 else
214 return GfRect2i(GfVec2i(GfMax(_min[0], that._min[0]),
215 GfMax(_min[1], that._min[1])),
216 GfVec2i(GfMin(_max[0], that._max[0]),
217 GfMin(_max[1], that._max[1])));
218 }
219
222 GfRect2i Intersect(const GfRect2i& that) const {
223 return GetIntersection(that);
224 }
225
227 GfRect2i GetUnion(const GfRect2i& that) const {
228 if(IsEmpty())
229 return that;
230 else if(that.IsEmpty())
231 return *this;
232 else
233 return GfRect2i(GfVec2i(GfMin(_min[0], that._min[0]),
234 GfMin(_min[1], that._min[1])),
235 GfVec2i(GfMax(_max[0], that._max[0]),
236 GfMax(_max[1], that._max[1])));
237 }
238
241 GfRect2i Union(const GfRect2i& that) const {
242 return GetUnion(that);
243 }
244
246 bool Contains(const GfVec2i& p) const {
247 return ((p[0] >= _min[0]) && (p[0] <= _max[0]) &&
248 (p[1] >= _min[1]) && (p[1] <= _max[1]));
249 }
250
251 friend inline size_t hash_value(const GfRect2i &r) {
252 return TfHash::Combine(r._min, r._max);
253 }
254
256 friend bool operator==(const GfRect2i& r1, const GfRect2i& r2) {
257 return r1._min == r2._min && r1._max == r2._max;
258 }
259
261 friend bool operator!=(const GfRect2i& r1, const GfRect2i& r2) {
262 return !(r1 == r2);
263 }
264
268 *this = GetUnion(that);
269 return *this;
270 }
271
272 friend GfRect2i operator + (const GfRect2i r1, const GfRect2i& r2) {
273 GfRect2i tmp(r1);
274 tmp += r2;
275 return tmp;
276 }
277
278private:
279 GfVec2i _min, _max;
280};
281
284GF_API std::ostream& operator<<(std::ostream&, const GfRect2i&);
285
286PXR_NAMESPACE_CLOSE_SCOPE
287
288#endif
A 2D rectangle with integer coordinates.
Definition: rect2i.h:43
void SetMinX(int x)
Set the X value of the min corner.
Definition: rect2i.h:123
const GfVec2i & GetMax() const
Returns the max corner of the rectangle.
Definition: rect2i.h:111
GfRect2i()
Constructs an empty rectangle.
Definition: rect2i.h:46
void SetMax(const GfVec2i &max)
Sets the max corner of the rectangle.
Definition: rect2i.h:166
int GetMinX() const
Return the X value of min corner.
Definition: rect2i.h:117
GfRect2i GetIntersection(const GfRect2i &that) const
Computes the intersection of two rectangles.
Definition: rect2i.h:208
GfRect2i(const GfVec2i &min, const GfVec2i &max)
Constructs a rectangle with min and max corners.
Definition: rect2i.h:51
GfRect2i Intersect(const GfRect2i &that) const
Computes the intersection of two rectangles.
Definition: rect2i.h:222
int GetWidth() const
Returns the width of the rectangle.
Definition: rect2i.h:195
int GetHeight() const
Returns the height of the rectangle.
Definition: rect2i.h:203
int GetMaxX() const
Return the X value of the max corner.
Definition: rect2i.h:129
GfRect2i Union(const GfRect2i &that) const
Computes the union of two rectangles.
Definition: rect2i.h:241
void SetMinY(int y)
Set the Y value of the min corner.
Definition: rect2i.h:146
GfVec2i GetSize() const
Returns the size of the rectangle as a vector (width,height).
Definition: rect2i.h:187
int GetMinY() const
Return the Y value of the min corner.
Definition: rect2i.h:140
GfVec2i GetCenter() const
Returns the center point of the rectangle.
Definition: rect2i.h:171
void SetMaxY(int y)
Set the Y value of the max corner.
Definition: rect2i.h:156
GF_API GfRect2i GetNormalized() const
Returns a normalized rectangle, i.e.
bool IsEmpty() const
Returns true if the rectangle is empty.
Definition: rect2i.h:87
int GetMaxY() const
Return the Y value of the max corner.
Definition: rect2i.h:151
unsigned long GetArea() const
Return the area of the rectangle.
Definition: rect2i.h:182
void SetMaxX(int x)
Set the X value of the max corner.
Definition: rect2i.h:134
void SetMin(const GfVec2i &min)
Sets the min corner of the rectangle.
Definition: rect2i.h:161
bool Contains(const GfVec2i &p) const
Returns true if the specified point in the rectangle.
Definition: rect2i.h:246
GfRect2i operator+=(const GfRect2i &that)
Computes the union of two rectangles.
Definition: rect2i.h:267
friend bool operator!=(const GfRect2i &r1, const GfRect2i &r2)
Returns true if r1 and r2 are different.
Definition: rect2i.h:261
GfRect2i GetUnion(const GfRect2i &that) const
Computes the union of two rectangles.
Definition: rect2i.h:227
const GfVec2i & GetMin() const
Returns the min corner of the rectangle.
Definition: rect2i.h:106
bool IsNull() const
Returns true if the rectangle is a null rectangle.
Definition: rect2i.h:77
bool IsValid() const
Return true if the rectangle is valid (equivalently, not empty).
Definition: rect2i.h:92
friend bool operator==(const GfRect2i &r1, const GfRect2i &r2)
Returns true if r1 and r2 are equal.
Definition: rect2i.h:256
GfRect2i(const GfVec2i &min, int width, int height)
Constructs a rectangle with min corner and the indicated width and height.
Definition: rect2i.h:58
void Translate(const GfVec2i &displacement)
Move the rectangle by displ.
Definition: rect2i.h:176
Basic type for a vector of 2 int components.
Definition: vec2i.h:44
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
Assorted mathematical utility functions.
T GfMin(T a1, T a2)
Returns the smallest of the given values.
Definition: math.h:307
T GfMax(T a1, T a2)
Returns the largest of the given values.
Definition: math.h:326
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].