range1d.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 //
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_RANGE1D_H
29 #define PXR_BASE_GF_RANGE1D_H
30 
33 
34 #include "pxr/pxr.h"
35 
36 #include "pxr/base/gf/api.h"
37 #include "pxr/base/gf/traits.h"
38 #include "pxr/base/tf/hash.h"
39 
40 #include <cfloat>
41 #include <cstddef>
42 #include <iosfwd>
43 
44 PXR_NAMESPACE_OPEN_SCOPE
45 
46 class GfRange1d;
47 class GfRange1f;
48 
49 template <>
50 struct GfIsGfRange<class GfRange1d> { static const bool value = true; };
51 
61 class GfRange1d
62 {
63 public:
64 
66  typedef double MinMaxType;
67 
68  static const size_t dimension = 1;
69  typedef MinMaxType ScalarType;
70 
72  // TODO check whether this can be deprecated.
73  void inline SetEmpty() {
74  _min = FLT_MAX;
75  _max = -FLT_MAX;
76  }
77 
80  SetEmpty();
81  }
82 
84  GfRange1d(double min, double max)
85  : _min(min), _max(max)
86  {
87  }
88 
89 
91  GF_API
92  GfRange1d(class GfRange1f const &other);
93 
95  double GetMin() const { return _min; }
96 
98  double GetMax() const { return _max; }
99 
101  double GetSize() const { return _max - _min; }
102 
106  double GetMidpoint() const {
107  return static_cast<ScalarType>(0.5) * _min
108  + static_cast<ScalarType>(0.5) * _max;
109  }
110 
112  void SetMin(double min) { _min = min; }
113 
115  void SetMax(double max) { _max = max; }
116 
118  bool IsEmpty() const {
119  return _min > _max;
120  }
121 
124  void ExtendBy(double point) { UnionWith(point); }
125 
128  void ExtendBy(const GfRange1d &range) { UnionWith(range); }
129 
132  bool Contains(double point) const {
133  return (point >= _min && point <= _max);
134  }
135 
139  bool Contains(const GfRange1d &range) const {
140  return Contains(range._min) && Contains(range._max);
141  }
142 
146  bool IsInside(double point) const {
147  return Contains(point);
148  }
149 
154  bool IsInside(const GfRange1d &range) const {
155  return Contains(range);
156  }
157 
161  bool IsOutside(const GfRange1d &range) const {
162  return (range._max < _min || range._min > _max);
163  }
164 
166  static GfRange1d GetUnion(const GfRange1d &a, const GfRange1d &b) {
167  GfRange1d res = a;
168  _FindMin(res._min,b._min);
169  _FindMax(res._max,b._max);
170  return res;
171  }
172 
174  const GfRange1d &UnionWith(const GfRange1d &b) {
175  _FindMin(_min,b._min);
176  _FindMax(_max,b._max);
177  return *this;
178  }
179 
181  const GfRange1d &UnionWith(double b) {
182  _FindMin(_min,b);
183  _FindMax(_max,b);
184  return *this;
185  }
186 
189  static GfRange1d Union(const GfRange1d &a, const GfRange1d &b) {
190  return GetUnion(a, b);
191  }
192 
195  const GfRange1d &Union(const GfRange1d &b) {
196  return UnionWith(b);
197  }
198 
201  const GfRange1d &Union(double b) {
202  return UnionWith(b);
203  }
204 
206  static GfRange1d GetIntersection(const GfRange1d &a, const GfRange1d &b) {
207  GfRange1d res = a;
208  _FindMax(res._min,b._min);
209  _FindMin(res._max,b._max);
210  return res;
211  }
212 
215  static GfRange1d Intersection(const GfRange1d &a, const GfRange1d &b) {
216  return GetIntersection(a, b);
217  }
218 
221  const GfRange1d &IntersectWith(const GfRange1d &b) {
222  _FindMax(_min,b._min);
223  _FindMin(_max,b._max);
224  return *this;
225  }
226 
230  const GfRange1d &Intersection(const GfRange1d &b) {
231  return IntersectWith(b);
232  }
233 
236  _min += b._min;
237  _max += b._max;
238  return *this;
239  }
240 
243  _min -= b._max;
244  _max -= b._min;
245  return *this;
246  }
247 
249  GfRange1d &operator *=(double m) {
250  if (m > 0) {
251  _min *= m;
252  _max *= m;
253  } else {
254  double tmp = _min;
255  _min = _max * m;
256  _max = tmp * m;
257  }
258  return *this;
259  }
260 
262  GfRange1d &operator /=(double m) {
263  return *this *= (1.0 / m);
264  }
265 
267  GfRange1d operator +(const GfRange1d &b) const {
268  return GfRange1d(_min + b._min, _max + b._max);
269  }
270 
271 
273  GfRange1d operator -(const GfRange1d &b) const {
274  return GfRange1d(_min - b._max, _max - b._min);
275  }
276 
278  friend GfRange1d operator *(double m, const GfRange1d &r) {
279  return (m > 0 ?
280  GfRange1d(r._min*m, r._max*m) :
281  GfRange1d(r._max*m, r._min*m));
282  }
283 
285  friend GfRange1d operator *(const GfRange1d &r, double m) {
286  return (m > 0 ?
287  GfRange1d(r._min*m, r._max*m) :
288  GfRange1d(r._max*m, r._min*m));
289  }
290 
292  friend GfRange1d operator /(const GfRange1d &r, double m) {
293  return r * (1.0 / m);
294  }
295 
297  friend inline size_t hash_value(const GfRange1d &r) {
298  return TfHash::Combine(r._min, r._max);
299  }
300 
302  bool operator ==(const GfRange1d &b) const {
303  return (_min == b._min && _max == b._max);
304  }
305 
306  bool operator !=(const GfRange1d &b) const {
307  return !(*this == b);
308  }
309 
314  GF_API inline bool operator ==(const GfRange1f& other) const;
315  GF_API inline bool operator !=(const GfRange1f& other) const;
316 
318  GF_API
319  double GetDistanceSquared(double p) const;
320 
321 
322  private:
324  double _min, _max;
325 
327  static void _FindMin(double &dest, double point) {
328  if (point < dest) dest = point;
329  }
330 
332  static void _FindMax(double &dest, double point) {
333  if (point > dest) dest = point;
334  }
335 };
336 
339 GF_API std::ostream& operator<<(std::ostream &, GfRange1d const &);
340 
341 PXR_NAMESPACE_CLOSE_SCOPE
342 #include "pxr/base/gf/range1f.h"
343 PXR_NAMESPACE_OPEN_SCOPE
344 
345 inline bool
346 GfRange1d::operator ==(const GfRange1f& other) const {
347  return _min == double(other.GetMin()) &&
348  _max == double(other.GetMax());
349 }
350 
351 inline bool
352 GfRange1d::operator !=(const GfRange1f& other) const {
353  return !(*this == other);
354 }
355 
356 
357 PXR_NAMESPACE_CLOSE_SCOPE
358 
359 #endif // PXR_BASE_GF_RANGE1D_H
void SetEmpty()
Sets the range to an empty interval.
Definition: range1d.h:73
GfRange1d & operator/=(double m)
unary division.
Definition: range1d.h:262
const GfRange1d & Union(const GfRange1d &b)
Extend this to include b.
Definition: range1d.h:195
double MinMaxType
Helper typedef.
Definition: range1d.h:66
const GfRange1d & UnionWith(const GfRange1d &b)
Extend this to include b.
Definition: range1d.h:174
bool Contains(double point) const
Returns true if the point is located inside the range.
Definition: range1d.h:132
double GetSize() const
Returns the size of the range.
Definition: range1d.h:101
friend size_t hash_value(const GfRange1d &r)
hash.
Definition: range1d.h:297
friend GfRange1d operator *(double m, const GfRange1d &r)
scalar multiply.
Definition: range1d.h:278
Basic type: 1-dimensional floating point range.
Definition: range1d.h:61
bool IsEmpty() const
Returns whether the range is empty (max < min).
Definition: range1d.h:118
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:221
A metafunction with a static const bool member 'value' that is true for GfRange types and false for a...
Definition: traits.h:57
bool Contains(const GfRange1d &range) const
Returns true if the range is located entirely inside the range.
Definition: range1d.h:139
GfRange1d & operator *=(double m)
unary multiply.
Definition: range1d.h:249
void ExtendBy(const GfRange1d &range)
Modifies the range if necessary to surround the given range.
Definition: range1d.h:128
float GetMax() const
Returns the maximum value of the range.
Definition: range1f.h:98
float GetMin() const
Returns the minimum value of the range.
Definition: range1f.h:95
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:519
GfRange1d operator+(const GfRange1d &b) const
binary sum.
Definition: range1d.h:267
const GfRange1d & Intersection(const GfRange1d &b)
Modifies this range to hold its intersection with b and returns the result.
Definition: range1d.h:230
bool IsOutside(const GfRange1d &range) const
Returns true if the range is located entirely outside the range.
Definition: range1d.h:161
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:61
bool operator==(const GfRange1d &b) const
The min and max points must match exactly for equality.
Definition: range1d.h:302
static GfRange1d Union(const GfRange1d &a, const GfRange1d &b)
Returns the smallest GfRange1d which contains both a and b.
Definition: range1d.h:189
friend GfRange1d operator *(const GfRange1d &r, double m)
scalar multiply.
Definition: range1d.h:285
void ExtendBy(double point)
Modifies the range if necessary to surround the given value.
Definition: range1d.h:124
double GetMax() const
Returns the maximum value of the range.
Definition: range1d.h:98
double GetMidpoint() const
Returns the midpoint of the range, that is, 0.5*(min+max).
Definition: range1d.h:106
double GetMin() const
Returns the minimum value of the range.
Definition: range1d.h:95
GfRange1d operator -(const GfRange1d &b) const
binary difference.
Definition: range1d.h:273
static GfRange1d GetUnion(const GfRange1d &a, const GfRange1d &b)
Returns the smallest GfRange1d which contains both a and b.
Definition: range1d.h:166
static GfRange1d Intersection(const GfRange1d &a, const GfRange1d &b)
Returns a GfRange1d that describes the intersection of a and b.
Definition: range1d.h:215
friend GfRange1d operator/(const GfRange1d &r, double m)
scalar divide.
Definition: range1d.h:292
void SetMax(double max)
Sets the maximum value of the range.
Definition: range1d.h:115
static GfRange1d GetIntersection(const GfRange1d &a, const GfRange1d &b)
Returns a GfRange1d that describes the intersection of a and b.
Definition: range1d.h:206
GfRange1d & operator -=(const GfRange1d &b)
unary difference.
Definition: range1d.h:242
GfRange1d & operator+=(const GfRange1d &b)
unary sum.
Definition: range1d.h:235
bool IsInside(const GfRange1d &range) const
Returns true if the range is located entirely inside the range.
Definition: range1d.h:154
const GfRange1d & UnionWith(double b)
Extend this to include b.
Definition: range1d.h:181
bool IsInside(double point) const
Returns true if the point is located inside the range.
Definition: range1d.h:146
GfRange1d(double min, double max)
This constructor initializes the minimum and maximum points.
Definition: range1d.h:84
const GfRange1d & Union(double b)
Extend this to include b.
Definition: range1d.h:201
void SetMin(double min)
Sets the minimum value of the range.
Definition: range1d.h:112
GfRange1d()
The default constructor creates an empty range.
Definition: range1d.h:79