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