HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
range2f.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 ////////////////////////////////////////////////////////////////////////
8 // This file is generated by a script. Do not edit directly. Edit the
9 // range.template.h file to make changes.
10 
11 #ifndef PXR_BASE_GF_RANGE2F_H
12 #define PXR_BASE_GF_RANGE2F_H
13 
14 /// \file gf/range2f.h
15 /// \ingroup group_gf_BasicGeometry
16 
17 #include "pxr/pxr.h"
18 
19 #include "pxr/base/gf/api.h"
20 #include "pxr/base/gf/vec2d.h"
21 #include "pxr/base/gf/vec2f.h"
22 #include "pxr/base/gf/traits.h"
23 #include "pxr/base/tf/hash.h"
24 
25 #include <cfloat>
26 #include <cstddef>
27 #include <iosfwd>
28 
30 
31 class GfRange2d;
32 class GfRange2f;
33 
34 template <>
35 struct GfIsGfRange<class GfRange2f> { static const bool value = true; };
36 
37 /// \class GfRange2f
38 /// \ingroup group_gf_BasicGeometry
39 ///
40 /// Basic type: 2-dimensional floating point range.
41 ///
42 /// This class represents a 2-dimensional range (or interval) All
43 /// operations are component-wise and conform to interval mathematics. An
44 /// empty range is one where max < min.
45 /// The default empty is [FLT_MAX,-FLT_MAX]
46 class GfRange2f
47 {
48 public:
49 
50  /// Helper typedef.
52 
53  static const size_t dimension = GfVec2f::dimension;
55 
56  /// Sets the range to an empty interval
57  // TODO check whether this can be deprecated.
58  void inline SetEmpty() {
59  _min[0] = _min[1] = FLT_MAX;
60  _max[0] = _max[1] = -FLT_MAX;
61  }
62 
63  /// The default constructor creates an empty range.
65  SetEmpty();
66  }
67 
68  /// This constructor initializes the minimum and maximum points.
69  GfRange2f(const GfVec2f &min, const GfVec2f &max)
70  : _min(min), _max(max)
71  {
72  }
73 
74 
75  /// Construct from GfRange2d.
76  GF_API
77  explicit GfRange2f(class GfRange2d const &other);
78 
79  /// Returns the minimum value of the range.
80  const GfVec2f &GetMin() const { return _min; }
81 
82  /// Returns the maximum value of the range.
83  const GfVec2f &GetMax() const { return _max; }
84 
85  /// Returns the size of the range.
86  GfVec2f GetSize() const { return _max - _min; }
87 
88  /// Returns the midpoint of the range, that is, 0.5*(min+max).
89  /// Note: this returns zero in the case of default-constructed ranges,
90  /// or ranges set via SetEmpty().
91  GfVec2f GetMidpoint() const {
92  return static_cast<ScalarType>(0.5) * _min
93  + static_cast<ScalarType>(0.5) * _max;
94  }
95 
96  /// Sets the minimum value of the range.
97  void SetMin(const GfVec2f &min) { _min = min; }
98 
99  /// Sets the maximum value of the range.
100  void SetMax(const GfVec2f &max) { _max = max; }
101 
102  /// Returns whether the range is empty (max < min).
103  bool IsEmpty() const {
104  return _min[0] > _max[0] || _min[1] > _max[1];
105  }
106 
107  /// Modifies the range if necessary to surround the given value.
108  /// \deprecated Use UnionWith() instead.
109  void ExtendBy(const GfVec2f &point) { UnionWith(point); }
110 
111  /// Modifies the range if necessary to surround the given range.
112  /// \deprecated Use UnionWith() instead.
113  void ExtendBy(const GfRange2f &range) { UnionWith(range); }
114 
115  /// Returns true if the \p point is located inside the range. As with all
116  /// operations of this type, the range is assumed to include its extrema.
117  bool Contains(const GfVec2f &point) const {
118  return (point[0] >= _min[0] && point[0] <= _max[0]
119  && point[1] >= _min[1] && point[1] <= _max[1]);
120  }
121 
122  /// Returns true if the \p range is located entirely inside the range. As
123  /// with all operations of this type, the ranges are assumed to include
124  /// their extrema.
125  bool Contains(const GfRange2f &range) const {
126  return Contains(range._min) && Contains(range._max);
127  }
128 
129  /// Returns true if the \p point is located inside the range. As with all
130  /// operations of this type, the range is assumed to include its extrema.
131  /// \deprecated Use Contains() instead.
132  bool IsInside(const GfVec2f &point) const {
133  return Contains(point);
134  }
135 
136  /// Returns true if the \p range is located entirely inside the range. As
137  /// with all operations of this type, the ranges are assumed to include
138  /// their extrema.
139  /// \deprecated Use Contains() instead.
140  bool IsInside(const GfRange2f &range) const {
141  return Contains(range);
142  }
143 
144  /// Returns true if the \p range is located entirely outside the range. As
145  /// with all operations of this type, the ranges are assumed to include
146  /// their extrema.
147  bool IsOutside(const GfRange2f &range) const {
148  return ((range._max[0] < _min[0] || range._min[0] > _max[0])
149  || (range._max[1] < _min[1] || range._min[1] > _max[1]));
150  }
151 
152  /// Returns the smallest \c GfRange2f which contains both \p a and \p b.
153  static GfRange2f GetUnion(const GfRange2f &a, const GfRange2f &b) {
154  GfRange2f res = a;
155  _FindMin(res._min,b._min);
156  _FindMax(res._max,b._max);
157  return res;
158  }
159 
160  /// Extend \p this to include \p b.
161  const GfRange2f &UnionWith(const GfRange2f &b) {
162  _FindMin(_min,b._min);
163  _FindMax(_max,b._max);
164  return *this;
165  }
166 
167  /// Extend \p this to include \p b.
168  const GfRange2f &UnionWith(const GfVec2f &b) {
169  _FindMin(_min,b);
170  _FindMax(_max,b);
171  return *this;
172  }
173 
174  /// Returns the smallest \c GfRange2f which contains both \p a and \p b
175  /// \deprecated Use GetUnion() instead.
176  static GfRange2f Union(const GfRange2f &a, const GfRange2f &b) {
177  return GetUnion(a, b);
178  }
179 
180  /// Extend \p this to include \p b.
181  /// \deprecated Use UnionWith() instead.
182  const GfRange2f &Union(const GfRange2f &b) {
183  return UnionWith(b);
184  }
185 
186  /// Extend \p this to include \p b.
187  /// \deprecated Use UnionWith() instead.
188  const GfRange2f &Union(const GfVec2f &b) {
189  return UnionWith(b);
190  }
191 
192  /// Returns a \c GfRange2f that describes the intersection of \p a and \p b.
193  static GfRange2f GetIntersection(const GfRange2f &a, const GfRange2f &b) {
194  GfRange2f res = a;
195  _FindMax(res._min,b._min);
196  _FindMin(res._max,b._max);
197  return res;
198  }
199 
200  /// Returns a \c GfRange2f that describes the intersection of \p a and \p b.
201  /// \deprecated Use GetIntersection() instead.
202  static GfRange2f Intersection(const GfRange2f &a, const GfRange2f &b) {
203  return GetIntersection(a, b);
204  }
205 
206  /// Modifies this range to hold its intersection with \p b and returns the
207  /// result
209  _FindMax(_min,b._min);
210  _FindMin(_max,b._max);
211  return *this;
212  }
213 
214  /// Modifies this range to hold its intersection with \p b and returns the
215  /// result.
216  /// \deprecated Use IntersectWith() instead.
218  return IntersectWith(b);
219  }
220 
221  /// unary sum.
223  _min += b._min;
224  _max += b._max;
225  return *this;
226  }
227 
228  /// unary difference.
230  _min -= b._max;
231  _max -= b._min;
232  return *this;
233  }
234 
235  /// unary multiply.
236  GfRange2f &operator *=(double m) {
237  if (m > 0) {
238  _min *= m;
239  _max *= m;
240  } else {
241  GfVec2f tmp = _min;
242  _min = _max * m;
243  _max = tmp * m;
244  }
245  return *this;
246  }
247 
248  /// unary division.
249  GfRange2f &operator /=(double m) {
250  return *this *= (1.0 / m);
251  }
252 
253  /// binary sum.
254  GfRange2f operator +(const GfRange2f &b) const {
255  return GfRange2f(_min + b._min, _max + b._max);
256  }
257 
258 
259  /// binary difference.
260  GfRange2f operator -(const GfRange2f &b) const {
261  return GfRange2f(_min - b._max, _max - b._min);
262  }
263 
264  /// scalar multiply.
265  friend GfRange2f operator *(double m, const GfRange2f &r) {
266  return (m > 0 ?
267  GfRange2f(r._min*m, r._max*m) :
268  GfRange2f(r._max*m, r._min*m));
269  }
270 
271  /// scalar multiply.
272  friend GfRange2f operator *(const GfRange2f &r, double m) {
273  return (m > 0 ?
274  GfRange2f(r._min*m, r._max*m) :
275  GfRange2f(r._max*m, r._min*m));
276  }
277 
278  /// scalar divide.
279  friend GfRange2f operator /(const GfRange2f &r, double m) {
280  return r * (1.0 / m);
281  }
282 
283  /// hash.
284  friend inline size_t hash_value(const GfRange2f &r) {
285  return TfHash::Combine(r._min, r._max);
286  }
287 
288  /// The min and max points must match exactly for equality.
289  bool operator ==(const GfRange2f &b) const {
290  return (_min == b._min && _max == b._max);
291  }
292 
293  bool operator !=(const GfRange2f &b) const {
294  return !(*this == b);
295  }
296 
297  /// Compare this range to a GfRange2d.
298  ///
299  /// The values must match exactly and it does exactly what you might
300  /// expect when comparing float and double values.
301  GF_API inline bool operator ==(const GfRange2d& other) const;
302  GF_API inline bool operator !=(const GfRange2d& other) const;
303 
304  /// Compute the squared distance from a point to the range.
305  GF_API
306  double GetDistanceSquared(const GfVec2f &p) const;
307 
308  /// Returns the ith corner of the range, in the following order:
309  /// SW, SE, NW, NE.
310  GF_API
311  GfVec2f GetCorner(size_t i) const;
312 
313  /// Returns the ith quadrant of the range, in the following order:
314  /// SW, SE, NW, NE.
315  GF_API
316  GfRange2f GetQuadrant(size_t i) const;
317 
318  /// The unit square.
319  GF_API
320  static const GfRange2f UnitSquare;
321 
322  private:
323  /// Minimum and maximum points.
324  GfVec2f _min, _max;
325 
326  /// Extends minimum point if necessary to contain given point.
327  static void _FindMin(GfVec2f &dest, const GfVec2f &point) {
328  if (point[0] < dest[0]) dest[0] = point[0];
329  if (point[1] < dest[1]) dest[1] = point[1];
330  }
331 
332  /// Extends maximum point if necessary to contain given point.
333  static void _FindMax(GfVec2f &dest, const GfVec2f &point) {
334  if (point[0] > dest[0]) dest[0] = point[0];
335  if (point[1] > dest[1]) dest[1] = point[1];
336  }
337 };
338 
339 /// Output a GfRange2f.
340 /// \ingroup group_gf_DebuggingOutput
341 GF_API std::ostream& operator<<(std::ostream &, GfRange2f const &);
342 
344 #include "pxr/base/gf/range2d.h"
346 
347 inline bool
348 GfRange2f::operator ==(const GfRange2d& other) const {
349  return _min == GfVec2f(other.GetMin()) &&
350  _max == GfVec2f(other.GetMax());
351 }
352 
353 inline bool
354 GfRange2f::operator !=(const GfRange2d& other) const {
355  return !(*this == other);
356 }
357 
358 
360 
361 #endif // PXR_BASE_GF_RANGE2F_H
static const size_t dimension
Definition: range2f.h:53
const GfVec2d & GetMax() const
Returns the maximum value of the range.
Definition: range2d.h:83
const GfRange2f & UnionWith(const GfVec2f &b)
Extend this to include b.
Definition: range2f.h:168
GLenum GLint * range
Definition: glcorearb.h:1925
GfRange2f operator-(const GfRange2f &b) const
binary difference.
Definition: range2f.h:260
static GfRange2f GetIntersection(const GfRange2f &a, const GfRange2f &b)
Returns a GfRange2f that describes the intersection of a and b.
Definition: range2f.h:193
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:632
GF_API GfVec2f GetCorner(size_t i) const
void SetEmpty()
Sets the range to an empty interval.
Definition: range2f.h:58
GLsizei const GLfloat * value
Definition: glcorearb.h:824
void ExtendBy(const GfVec2f &point)
Definition: range2f.h:109
const GfRange2f & Intersection(const GfRange2f &b)
Definition: range2f.h:217
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
void SetMax(const GfVec2f &max)
Sets the maximum value of the range.
Definition: range2f.h:100
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GfRange2f & operator-=(const GfRange2f &b)
unary difference.
Definition: range2f.h:229
const GfRange2f & Union(const GfVec2f &b)
Definition: range2f.h:188
const GfRange2f & Union(const GfRange2f &b)
Definition: range2f.h:182
bool IsInside(const GfVec2f &point) const
Definition: range2f.h:132
GfVec2f::ScalarType ScalarType
Definition: range2f.h:54
static const size_t dimension
Definition: vec2f.h:50
GfRange2f & operator*=(double m)
unary multiply.
Definition: range2f.h:236
static GfRange2f Union(const GfRange2f &a, const GfRange2f &b)
Definition: range2f.h:176
GfRange2f & operator+=(const GfRange2f &b)
unary sum.
Definition: range2f.h:222
GF_API std::ostream & operator<<(std::ostream &, GfRange2f const &)
friend size_t hash_value(const GfRange2f &r)
hash.
Definition: range2f.h:284
static GfRange2f GetUnion(const GfRange2f &a, const GfRange2f &b)
Returns the smallest GfRange2f which contains both a and b.
Definition: range2f.h:153
GfRange2f(const GfVec2f &min, const GfVec2f &max)
This constructor initializes the minimum and maximum points.
Definition: range2f.h:69
static GF_API const GfRange2f UnitSquare
The unit square.
Definition: range2f.h:320
bool Contains(const GfRange2f &range) const
Definition: range2f.h:125
GfVec2f MinMaxType
Helper typedef.
Definition: range2f.h:51
friend GfRange2f operator*(double m, const GfRange2f &r)
scalar multiply.
Definition: range2f.h:265
bool operator!=(const GfRange2f &b) const
Definition: range2f.h:293
const GfVec2f & GetMax() const
Returns the maximum value of the range.
Definition: range2f.h:83
bool IsInside(const GfRange2f &range) const
Definition: range2f.h:140
GfRange2f & operator/=(double m)
unary division.
Definition: range2f.h:249
bool operator==(const GfRange2f &b) const
The min and max points must match exactly for equality.
Definition: range2f.h:289
float ScalarType
Scalar element type and dimension.
Definition: vec2f.h:49
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
void SetMin(const GfVec2f &min)
Sets the minimum value of the range.
Definition: range2f.h:97
GfVec2f GetSize() const
Returns the size of the range.
Definition: range2f.h:86
static GfRange2f Intersection(const GfRange2f &a, const GfRange2f &b)
Definition: range2f.h:202
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
GfRange2f operator+(const GfRange2f &b) const
binary sum.
Definition: range2f.h:254
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
GfVec2f GetMidpoint() const
Definition: range2f.h:91
void ExtendBy(const GfRange2f &range)
Definition: range2f.h:113
Definition: vec2f.h:45
const GfVec2d & GetMin() const
Returns the minimum value of the range.
Definition: range2d.h:80
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GfRange2f()
The default constructor creates an empty range.
Definition: range2f.h:64
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
const GfRange2f & IntersectWith(const GfRange2f &b)
Definition: range2f.h:208
GF_API double GetDistanceSquared(const GfVec2f &p) const
Compute the squared distance from a point to the range.
const GfRange2f & UnionWith(const GfRange2f &b)
Extend this to include b.
Definition: range2f.h:161
GLboolean r
Definition: glcorearb.h:1222
GF_API GfRange2f GetQuadrant(size_t i) const
friend GfRange2f operator/(const GfRange2f &r, double m)
scalar divide.
Definition: range2f.h:279
bool IsEmpty() const
Returns whether the range is empty (max < min).
Definition: range2f.h:103
const GfVec2f & GetMin() const
Returns the minimum value of the range.
Definition: range2f.h:80
bool IsOutside(const GfRange2f &range) const
Definition: range2f.h:147
#define GF_API
Definition: api.h:23
bool Contains(const GfVec2f &point) const
Definition: range2f.h:117