All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
39 #include <boost/functional/hash.hpp>
40 
41 #include <cfloat>
42 #include <cstddef>
43 #include <iosfwd>
44 
45 PXR_NAMESPACE_OPEN_SCOPE
46 
47 class GfRange1d;
48 class GfRange1f;
49 
50 template <>
51 struct GfIsGfRange<class GfRange1d> { static const bool value = true; };
52 
62 class GfRange1d
63 {
64 public:
65 
67  typedef double MinMaxType;
68 
69  static const size_t dimension = 1;
70  typedef MinMaxType ScalarType;
71 
73  // TODO check whether this can be deprecated.
74  void inline SetEmpty() {
75  _min = FLT_MAX;
76  _max = -FLT_MAX;
77  }
78 
81  SetEmpty();
82  }
83 
85  GfRange1d(double min, double max)
86  : _min(min), _max(max)
87  {
88  }
89 
91  double GetMin() const { return _min; }
92 
94  double GetMax() const { return _max; }
95 
97  double GetSize() const { return _max - _min; }
98 
102  double GetMidpoint() const {
103  return static_cast<ScalarType>(0.5) * _min
104  + static_cast<ScalarType>(0.5) * _max;
105  }
106 
108  void SetMin(double min) { _min = min; }
109 
111  void SetMax(double max) { _max = max; }
112 
114  bool IsEmpty() const {
115  return _min > _max;
116  }
117 
120  void ExtendBy(double point) { UnionWith(point); }
121 
124  void ExtendBy(const GfRange1d &range) { UnionWith(range); }
125 
128  bool Contains(double point) const {
129  return (point >= _min && point <= _max);
130  }
131 
135  bool Contains(const GfRange1d &range) const {
136  return Contains(range._min) && Contains(range._max);
137  }
138 
142  bool IsInside(double point) const {
143  return Contains(point);
144  }
145 
150  bool IsInside(const GfRange1d &range) const {
151  return Contains(range);
152  }
153 
157  bool IsOutside(const GfRange1d &range) const {
158  return (range._max < _min || range._min > _max);
159  }
160 
162  static GfRange1d GetUnion(const GfRange1d &a, const GfRange1d &b) {
163  GfRange1d res = a;
164  _FindMin(res._min,b._min);
165  _FindMax(res._max,b._max);
166  return res;
167  }
168 
170  const GfRange1d &UnionWith(const GfRange1d &b) {
171  _FindMin(_min,b._min);
172  _FindMax(_max,b._max);
173  return *this;
174  }
175 
177  const GfRange1d &UnionWith(double b) {
178  _FindMin(_min,b);
179  _FindMax(_max,b);
180  return *this;
181  }
182 
185  static GfRange1d Union(const GfRange1d &a, const GfRange1d &b) {
186  return GetUnion(a, b);
187  }
188 
191  const GfRange1d &Union(const GfRange1d &b) {
192  return UnionWith(b);
193  }
194 
197  const GfRange1d &Union(double b) {
198  return UnionWith(b);
199  }
200 
202  static GfRange1d GetIntersection(const GfRange1d &a, const GfRange1d &b) {
203  GfRange1d res = a;
204  _FindMax(res._min,b._min);
205  _FindMin(res._max,b._max);
206  return res;
207  }
208 
211  static GfRange1d Intersection(const GfRange1d &a, const GfRange1d &b) {
212  return GetIntersection(a, b);
213  }
214 
217  const GfRange1d &IntersectWith(const GfRange1d &b) {
218  _FindMax(_min,b._min);
219  _FindMin(_max,b._max);
220  return *this;
221  }
222 
226  const GfRange1d &Intersection(const GfRange1d &b) {
227  return IntersectWith(b);
228  }
229 
232  _min += b._min;
233  _max += b._max;
234  return *this;
235  }
236 
239  _min -= b._max;
240  _max -= b._min;
241  return *this;
242  }
243 
245  GfRange1d operator *=(double m) {
246  if (m > 0) {
247  _min *= m;
248  _max *= m;
249  } else {
250  double tmp = _min;
251  _min = _max * m;
252  _max = tmp * m;
253  }
254  return *this;
255  }
256 
258  GfRange1d operator /=(double m) {
259  return *this *= (1.0 / m);
260  }
261 
263  GfRange1d operator +(const GfRange1d &b) const {
264  return GfRange1d(_min + b._min, _max + b._max);
265  }
266 
267 
269  GfRange1d operator -(const GfRange1d &b) const {
270  return GfRange1d(_min - b._max, _max - b._min);
271  }
272 
274  friend GfRange1d operator *(double m, const GfRange1d &r) {
275  return (m > 0 ?
276  GfRange1d(r._min*m, r._max*m) :
277  GfRange1d(r._max*m, r._min*m));
278  }
279 
281  friend GfRange1d operator *(const GfRange1d &r, double m) {
282  return (m > 0 ?
283  GfRange1d(r._min*m, r._max*m) :
284  GfRange1d(r._max*m, r._min*m));
285  }
286 
288  friend GfRange1d operator /(const GfRange1d &r, double m) {
289  return r * (1.0 / m);
290  }
291 
293  friend inline size_t hash_value(const GfRange1d &r) {
294  size_t h = 0;
295  boost::hash_combine(h, r._min);
296  boost::hash_combine(h, r._max);
297  return h;
298  }
299 
301  bool operator ==(const GfRange1d &b) const {
302  return (_min == b._min && _max == b._max);
303  }
304 
305  bool operator !=(const GfRange1d &b) const {
306  return !(*this == b);
307  }
308 
313  GF_API inline bool operator ==(const GfRange1f& other) const;
314  GF_API inline bool operator !=(const GfRange1f& other) const;
315 
317  GF_API
318  double GetDistanceSquared(double p) const;
319 
320 
321  private:
323  double _min, _max;
324 
326  static void _FindMin(double &dest, double point) {
327  if (point < dest) dest = point;
328  }
329 
331  static void _FindMax(double &dest, double point) {
332  if (point > dest) dest = point;
333  }
334 };
335 
338 GF_API std::ostream& operator<<(std::ostream &, GfRange1d const &);
339 
340 PXR_NAMESPACE_CLOSE_SCOPE
341 #include "pxr/base/gf/range1f.h"
342 PXR_NAMESPACE_OPEN_SCOPE
343 
344 inline bool
345 GfRange1d::operator ==(const GfRange1f& other) const {
346  return _min == double(other.GetMin()) &&
347  _max == double(other.GetMax());
348 }
349 
350 inline bool
351 GfRange1d::operator !=(const GfRange1f& other) const {
352  return !(*this == other);
353 }
354 
355 
356 PXR_NAMESPACE_CLOSE_SCOPE
357 
358 #endif // PXR_BASE_GF_RANGE1D_H
void SetEmpty()
Sets the range to an empty interval.
Definition: range1d.h:74
const GfRange1d & Union(const GfRange1d &b)
Extend this to include b.
Definition: range1d.h:191
double MinMaxType
Helper typedef.
Definition: range1d.h:67
const GfRange1d & UnionWith(const GfRange1d &b)
Extend this to include b.
Definition: range1d.h:170
friend size_t hash_value(const GfRange1d &r)
hash.
Definition: range1d.h:293
Basic type: 1-dimensional floating point range.
Definition: range1d.h:62
const GfRange1d & IntersectWith(const GfRange1d &b)
Modifies this range to hold its intersection with b and returns the result.
Definition: range1d.h:217
A metafunction with a static const bool member &#39;value&#39; that is true for GfRange types and false for a...
Definition: traits.h:52
void ExtendBy(const GfRange1d &range)
Modifies the range if necessary to surround the given range.
Definition: range1d.h:124
double GetMin() const
Returns the minimum value of the range.
Definition: range1d.h:91
GfRange1d operator-=(const GfRange1d &b)
unary difference.
Definition: range1d.h:238
GfRange1d operator+=(const GfRange1d &b)
unary sum.
Definition: range1d.h:231
double GetMidpoint() const
Returns the midpoint of the range, that is, 0.5*(min+max).
Definition: range1d.h:102
bool IsOutside(const GfRange1d &range) const
Returns true if the range is located entirely outside the range.
Definition: range1d.h:157
friend GfRange1d operator*(double m, const GfRange1d &r)
scalar multiply.
Definition: range1d.h:274
GF_API double GetDistanceSquared(double p) const
Compute the squared distance from a point to the range.
GfRange1d operator-(const GfRange1d &b) const
binary difference.
Definition: range1d.h:269
bool IsInside(const GfRange1d &range) const
Returns true if the range is located entirely inside the range.
Definition: range1d.h:150
bool IsEmpty() const
Returns whether the range is empty (max &lt; min).
Definition: range1d.h:114
const GfRange1d & Intersection(const GfRange1d &b)
Modifies this range to hold its intersection with b and returns the result.
Definition: range1d.h:226
float GetMax() const
Returns the maximum value of the range.
Definition: range1f.h:94
Basic type: 1-dimensional floating point range.
Definition: range1f.h:62
bool operator==(const GfRange1d &b) const
The min and max points must match exactly for equality.
Definition: range1d.h:301
double GetSize() const
Returns the size of the range.
Definition: range1d.h:97
static GfRange1d Union(const GfRange1d &a, const GfRange1d &b)
Returns the smallest GfRange1d which contains both a and b.
Definition: range1d.h:185
void ExtendBy(double point)
Modifies the range if necessary to surround the given value.
Definition: range1d.h:120
bool IsInside(double point) const
Returns true if the point is located inside the range.
Definition: range1d.h:142
float GetMin() const
Returns the minimum value of the range.
Definition: range1f.h:91
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
double GetMax() const
Returns the maximum value of the range.
Definition: range1d.h:94
GfRange1d operator*=(double m)
unary multiply.
Definition: range1d.h:245
static GfRange1d GetUnion(const GfRange1d &a, const GfRange1d &b)
Returns the smallest GfRange1d which contains both a and b.
Definition: range1d.h:162
static GfRange1d Intersection(const GfRange1d &a, const GfRange1d &b)
Returns a GfRange1d that describes the intersection of a and b.
Definition: range1d.h:211
GfRange1d operator/=(double m)
unary division.
Definition: range1d.h:258
GfRange1d operator+(const GfRange1d &b) const
binary sum.
Definition: range1d.h:263
friend GfRange1d operator/(const GfRange1d &r, double m)
scalar divide.
Definition: range1d.h:288
void SetMax(double max)
Sets the maximum value of the range.
Definition: range1d.h:111
static GfRange1d GetIntersection(const GfRange1d &a, const GfRange1d &b)
Returns a GfRange1d that describes the intersection of a and b.
Definition: range1d.h:202
const GfRange1d & UnionWith(double b)
Extend this to include b.
Definition: range1d.h:177
bool Contains(double point) const
Returns true if the point is located inside the range.
Definition: range1d.h:128
GfRange1d(double min, double max)
This constructor initializes the minimum and maximum points.
Definition: range1d.h:85
const GfRange1d & Union(double b)
Extend this to include b.
Definition: range1d.h:197
bool Contains(const GfRange1d &range) const
Returns true if the range is located entirely inside the range.
Definition: range1d.h:135
void SetMin(double min)
Sets the minimum value of the range.
Definition: range1d.h:108
GfRange1d()
The default constructor creates an empty range.
Definition: range1d.h:80