range1d.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 //
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_RANGE1D_H
12 #define PXR_BASE_GF_RANGE1D_H
13 
16 
17 #include "pxr/pxr.h"
18 
19 #include "pxr/base/gf/api.h"
20 #include "pxr/base/gf/traits.h"
21 #include "pxr/base/tf/hash.h"
22 
23 #include <cfloat>
24 #include <cstddef>
25 #include <iosfwd>
26 
27 PXR_NAMESPACE_OPEN_SCOPE
28 
29 class GfRange1d;
30 class GfRange1f;
31 
32 template <>
33 struct GfIsGfRange<class GfRange1d> { static const bool value = true; };
34 
44 class GfRange1d
45 {
46 public:
47 
49  typedef double MinMaxType;
50 
51  static const size_t dimension = 1;
52  typedef MinMaxType ScalarType;
53 
55  // TODO check whether this can be deprecated.
56  void inline SetEmpty() {
57  _min = FLT_MAX;
58  _max = -FLT_MAX;
59  }
60 
63  SetEmpty();
64  }
65 
67  GfRange1d(double min, double max)
68  : _min(min), _max(max)
69  {
70  }
71 
72 
74  GF_API
75  GfRange1d(class GfRange1f const &other);
76 
78  double GetMin() const { return _min; }
79 
81  double GetMax() const { return _max; }
82 
84  double GetSize() const { return _max - _min; }
85 
89  double GetMidpoint() const {
90  return static_cast<ScalarType>(0.5) * _min
91  + static_cast<ScalarType>(0.5) * _max;
92  }
93 
95  void SetMin(double min) { _min = min; }
96 
98  void SetMax(double max) { _max = max; }
99 
101  bool IsEmpty() const {
102  return _min > _max;
103  }
104 
107  void ExtendBy(double point) { UnionWith(point); }
108 
111  void ExtendBy(const GfRange1d &range) { UnionWith(range); }
112 
115  bool Contains(double point) const {
116  return (point >= _min && point <= _max);
117  }
118 
122  bool Contains(const GfRange1d &range) const {
123  return Contains(range._min) && Contains(range._max);
124  }
125 
129  bool IsInside(double point) const {
130  return Contains(point);
131  }
132 
137  bool IsInside(const GfRange1d &range) const {
138  return Contains(range);
139  }
140 
144  bool IsOutside(const GfRange1d &range) const {
145  return (range._max < _min || range._min > _max);
146  }
147 
149  static GfRange1d GetUnion(const GfRange1d &a, const GfRange1d &b) {
150  GfRange1d res = a;
151  _FindMin(res._min,b._min);
152  _FindMax(res._max,b._max);
153  return res;
154  }
155 
157  const GfRange1d &UnionWith(const GfRange1d &b) {
158  _FindMin(_min,b._min);
159  _FindMax(_max,b._max);
160  return *this;
161  }
162 
164  const GfRange1d &UnionWith(double b) {
165  _FindMin(_min,b);
166  _FindMax(_max,b);
167  return *this;
168  }
169 
172  static GfRange1d Union(const GfRange1d &a, const GfRange1d &b) {
173  return GetUnion(a, b);
174  }
175 
178  const GfRange1d &Union(const GfRange1d &b) {
179  return UnionWith(b);
180  }
181 
184  const GfRange1d &Union(double b) {
185  return UnionWith(b);
186  }
187 
189  static GfRange1d GetIntersection(const GfRange1d &a, const GfRange1d &b) {
190  GfRange1d res = a;
191  _FindMax(res._min,b._min);
192  _FindMin(res._max,b._max);
193  return res;
194  }
195 
198  static GfRange1d Intersection(const GfRange1d &a, const GfRange1d &b) {
199  return GetIntersection(a, b);
200  }
201 
204  const GfRange1d &IntersectWith(const GfRange1d &b) {
205  _FindMax(_min,b._min);
206  _FindMin(_max,b._max);
207  return *this;
208  }
209 
213  const GfRange1d &Intersection(const GfRange1d &b) {
214  return IntersectWith(b);
215  }
216 
219  _min += b._min;
220  _max += b._max;
221  return *this;
222  }
223 
226  _min -= b._max;
227  _max -= b._min;
228  return *this;
229  }
230 
232  GfRange1d &operator *=(double m) {
233  if (m > 0) {
234  _min *= m;
235  _max *= m;
236  } else {
237  double tmp = _min;
238  _min = _max * m;
239  _max = tmp * m;
240  }
241  return *this;
242  }
243 
245  GfRange1d &operator /=(double m) {
246  return *this *= (1.0 / m);
247  }
248 
250  GfRange1d operator +(const GfRange1d &b) const {
251  return GfRange1d(_min + b._min, _max + b._max);
252  }
253 
254 
256  GfRange1d operator -(const GfRange1d &b) const {
257  return GfRange1d(_min - b._max, _max - b._min);
258  }
259 
261  friend GfRange1d operator *(double m, const GfRange1d &r) {
262  return (m > 0 ?
263  GfRange1d(r._min*m, r._max*m) :
264  GfRange1d(r._max*m, r._min*m));
265  }
266 
268  friend GfRange1d operator *(const GfRange1d &r, double m) {
269  return (m > 0 ?
270  GfRange1d(r._min*m, r._max*m) :
271  GfRange1d(r._max*m, r._min*m));
272  }
273 
275  friend GfRange1d operator /(const GfRange1d &r, double m) {
276  return r * (1.0 / m);
277  }
278 
280  friend inline size_t hash_value(const GfRange1d &r) {
281  return TfHash::Combine(r._min, r._max);
282  }
283 
285  bool operator ==(const GfRange1d &b) const {
286  return (_min == b._min && _max == b._max);
287  }
288 
289  bool operator !=(const GfRange1d &b) const {
290  return !(*this == b);
291  }
292 
297  GF_API inline bool operator ==(const GfRange1f& other) const;
298  GF_API inline bool operator !=(const GfRange1f& other) const;
299 
301  GF_API
302  double GetDistanceSquared(double p) const;
303 
304 
305  private:
307  double _min, _max;
308 
310  static void _FindMin(double &dest, double point) {
311  if (point < dest) dest = point;
312  }
313 
315  static void _FindMax(double &dest, double point) {
316  if (point > dest) dest = point;
317  }
318 };
319 
322 GF_API std::ostream& operator<<(std::ostream &, GfRange1d const &);
323 
324 PXR_NAMESPACE_CLOSE_SCOPE
325 #include "pxr/base/gf/range1f.h"
326 PXR_NAMESPACE_OPEN_SCOPE
327 
328 inline bool
329 GfRange1d::operator ==(const GfRange1f& other) const {
330  return _min == double(other.GetMin()) &&
331  _max == double(other.GetMax());
332 }
333 
334 inline bool
335 GfRange1d::operator !=(const GfRange1f& other) const {
336  return !(*this == other);
337 }
338 
339 
340 PXR_NAMESPACE_CLOSE_SCOPE
341 
342 #endif // PXR_BASE_GF_RANGE1D_H
void SetEmpty()
Sets the range to an empty interval.
Definition: range1d.h:56
GfRange1d & operator/=(double m)
unary division.
Definition: range1d.h:245
const GfRange1d & Union(const GfRange1d &b)
Extend this to include b.
Definition: range1d.h:178
double MinMaxType
Helper typedef.
Definition: range1d.h:49
const GfRange1d & UnionWith(const GfRange1d &b)
Extend this to include b.
Definition: range1d.h:157
bool Contains(double point) const
Returns true if the point is located inside the range.
Definition: range1d.h:115
double GetSize() const
Returns the size of the range.
Definition: range1d.h:84
friend size_t hash_value(const GfRange1d &r)
hash.
Definition: range1d.h:280
friend GfRange1d operator *(double m, const GfRange1d &r)
scalar multiply.
Definition: range1d.h:261
Basic type: 1-dimensional floating point range.
Definition: range1d.h:44
bool IsEmpty() const
Returns whether the range is empty (max < min).
Definition: range1d.h:101
GF_API std::ostream & operator<<(std::ostream &, GfRange1d const &)
Output a GfRange1d.
const GfRange1d & IntersectWith(const GfRange1d &b)
Modifies this range to hold its intersection with b and returns the result.
Definition: range1d.h:204
A metafunction with a static const bool member 'value' that is true for GfRange types and false for a...
Definition: traits.h:44
bool Contains(const GfRange1d &range) const
Returns true if the range is located entirely inside the range.
Definition: range1d.h:122
GfRange1d & operator *=(double m)
unary multiply.
Definition: range1d.h:232
void ExtendBy(const GfRange1d &range)
Modifies the range if necessary to surround the given range.
Definition: range1d.h:111
float GetMax() const
Returns the maximum value of the range.
Definition: range1f.h:81
float GetMin() const
Returns the minimum value of the range.
Definition: range1f.h:78
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
GfRange1d operator+(const GfRange1d &b) const
binary sum.
Definition: range1d.h:250
const GfRange1d & Intersection(const GfRange1d &b)
Modifies this range to hold its intersection with b and returns the result.
Definition: range1d.h:213
bool IsOutside(const GfRange1d &range) const
Returns true if the range is located entirely outside the range.
Definition: range1d.h:144
GF_API double GetDistanceSquared(double p) const
Compute the squared distance from a point to the range.
Basic type: 1-dimensional floating point range.
Definition: range1f.h:44
bool operator==(const GfRange1d &b) const
The min and max points must match exactly for equality.
Definition: range1d.h:285
static GfRange1d Union(const GfRange1d &a, const GfRange1d &b)
Returns the smallest GfRange1d which contains both a and b.
Definition: range1d.h:172
friend GfRange1d operator *(const GfRange1d &r, double m)
scalar multiply.
Definition: range1d.h:268
void ExtendBy(double point)
Modifies the range if necessary to surround the given value.
Definition: range1d.h:107
double GetMax() const
Returns the maximum value of the range.
Definition: range1d.h:81
double GetMidpoint() const
Returns the midpoint of the range, that is, 0.5*(min+max).
Definition: range1d.h:89
double GetMin() const
Returns the minimum value of the range.
Definition: range1d.h:78
GfRange1d operator -(const GfRange1d &b) const
binary difference.
Definition: range1d.h:256
static GfRange1d GetUnion(const GfRange1d &a, const GfRange1d &b)
Returns the smallest GfRange1d which contains both a and b.
Definition: range1d.h:149
static GfRange1d Intersection(const GfRange1d &a, const GfRange1d &b)
Returns a GfRange1d that describes the intersection of a and b.
Definition: range1d.h:198
friend GfRange1d operator/(const GfRange1d &r, double m)
scalar divide.
Definition: range1d.h:275
void SetMax(double max)
Sets the maximum value of the range.
Definition: range1d.h:98
static GfRange1d GetIntersection(const GfRange1d &a, const GfRange1d &b)
Returns a GfRange1d that describes the intersection of a and b.
Definition: range1d.h:189
GfRange1d & operator -=(const GfRange1d &b)
unary difference.
Definition: range1d.h:225
GfRange1d & operator+=(const GfRange1d &b)
unary sum.
Definition: range1d.h:218
bool IsInside(const GfRange1d &range) const
Returns true if the range is located entirely inside the range.
Definition: range1d.h:137
const GfRange1d & UnionWith(double b)
Extend this to include b.
Definition: range1d.h:164
bool IsInside(double point) const
Returns true if the point is located inside the range.
Definition: range1d.h:129
GfRange1d(double min, double max)
This constructor initializes the minimum and maximum points.
Definition: range1d.h:67
const GfRange1d & Union(double b)
Extend this to include b.
Definition: range1d.h:184
void SetMin(double min)
Sets the minimum value of the range.
Definition: range1d.h:95
GfRange1d()
The default constructor creates an empty range.
Definition: range1d.h:62