All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
range2d.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_RANGE2D_H
29 #define PXR_BASE_GF_RANGE2D_H
30 
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 
41 #include <boost/functional/hash.hpp>
42 
43 #include <cfloat>
44 #include <cstddef>
45 #include <iosfwd>
46 
47 PXR_NAMESPACE_OPEN_SCOPE
48 
49 class GfRange2d;
50 class GfRange2f;
51 
52 template <>
53 struct GfIsGfRange<class GfRange2d> { static const bool value = true; };
54 
64 class GfRange2d
65 {
66 public:
67 
70 
71  static const size_t dimension = GfVec2d::dimension;
72  typedef GfVec2d::ScalarType ScalarType;
73 
75  // TODO check whether this can be deprecated.
76  void inline SetEmpty() {
77  _min[0] = _min[1] = FLT_MAX;
78  _max[0] = _max[1] = -FLT_MAX;
79  }
80 
83  SetEmpty();
84  }
85 
87  GfRange2d(const GfVec2d &min, const GfVec2d &max)
88  : _min(min), _max(max)
89  {
90  }
91 
93  const GfVec2d &GetMin() const { return _min; }
94 
96  const GfVec2d &GetMax() const { return _max; }
97 
99  GfVec2d GetSize() const { return _max - _min; }
100 
105  return static_cast<ScalarType>(0.5) * _min
106  + static_cast<ScalarType>(0.5) * _max;
107  }
108 
110  void SetMin(const GfVec2d &min) { _min = min; }
111 
113  void SetMax(const GfVec2d &max) { _max = max; }
114 
116  bool IsEmpty() const {
117  return _min[0] > _max[0] || _min[1] > _max[1];
118  }
119 
122  void ExtendBy(const GfVec2d &point) { UnionWith(point); }
123 
126  void ExtendBy(const GfRange2d &range) { UnionWith(range); }
127 
130  bool Contains(const GfVec2d &point) const {
131  return (point[0] >= _min[0] && point[0] <= _max[0]
132  && point[1] >= _min[1] && point[1] <= _max[1]);
133  }
134 
138  bool Contains(const GfRange2d &range) const {
139  return Contains(range._min) && Contains(range._max);
140  }
141 
145  bool IsInside(const GfVec2d &point) const {
146  return Contains(point);
147  }
148 
153  bool IsInside(const GfRange2d &range) const {
154  return Contains(range);
155  }
156 
160  bool IsOutside(const GfRange2d &range) const {
161  return ((range._max[0] < _min[0] || range._min[0] > _max[0])
162  || (range._max[1] < _min[1] || range._min[1] > _max[1]));
163  }
164 
166  static GfRange2d GetUnion(const GfRange2d &a, const GfRange2d &b) {
167  GfRange2d res = a;
168  _FindMin(res._min,b._min);
169  _FindMax(res._max,b._max);
170  return res;
171  }
172 
174  const GfRange2d &UnionWith(const GfRange2d &b) {
175  _FindMin(_min,b._min);
176  _FindMax(_max,b._max);
177  return *this;
178  }
179 
181  const GfRange2d &UnionWith(const GfVec2d &b) {
182  _FindMin(_min,b);
183  _FindMax(_max,b);
184  return *this;
185  }
186 
189  static GfRange2d Union(const GfRange2d &a, const GfRange2d &b) {
190  return GetUnion(a, b);
191  }
192 
195  const GfRange2d &Union(const GfRange2d &b) {
196  return UnionWith(b);
197  }
198 
201  const GfRange2d &Union(const GfVec2d &b) {
202  return UnionWith(b);
203  }
204 
206  static GfRange2d GetIntersection(const GfRange2d &a, const GfRange2d &b) {
207  GfRange2d res = a;
208  _FindMax(res._min,b._min);
209  _FindMin(res._max,b._max);
210  return res;
211  }
212 
215  static GfRange2d Intersection(const GfRange2d &a, const GfRange2d &b) {
216  return GetIntersection(a, b);
217  }
218 
221  const GfRange2d &IntersectWith(const GfRange2d &b) {
222  _FindMax(_min,b._min);
223  _FindMin(_max,b._max);
224  return *this;
225  }
226 
230  const GfRange2d &Intersection(const GfRange2d &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  GfRange2d operator *=(double m) {
250  if (m > 0) {
251  _min *= m;
252  _max *= m;
253  } else {
254  GfVec2d tmp = _min;
255  _min = _max * m;
256  _max = tmp * m;
257  }
258  return *this;
259  }
260 
262  GfRange2d operator /=(double m) {
263  return *this *= (1.0 / m);
264  }
265 
267  GfRange2d operator +(const GfRange2d &b) const {
268  return GfRange2d(_min + b._min, _max + b._max);
269  }
270 
271 
273  GfRange2d operator -(const GfRange2d &b) const {
274  return GfRange2d(_min - b._max, _max - b._min);
275  }
276 
278  friend GfRange2d operator *(double m, const GfRange2d &r) {
279  return (m > 0 ?
280  GfRange2d(r._min*m, r._max*m) :
281  GfRange2d(r._max*m, r._min*m));
282  }
283 
285  friend GfRange2d operator *(const GfRange2d &r, double m) {
286  return (m > 0 ?
287  GfRange2d(r._min*m, r._max*m) :
288  GfRange2d(r._max*m, r._min*m));
289  }
290 
292  friend GfRange2d operator /(const GfRange2d &r, double m) {
293  return r * (1.0 / m);
294  }
295 
297  friend inline size_t hash_value(const GfRange2d &r) {
298  size_t h = 0;
299  boost::hash_combine(h, r._min);
300  boost::hash_combine(h, r._max);
301  return h;
302  }
303 
305  bool operator ==(const GfRange2d &b) const {
306  return (_min == b._min && _max == b._max);
307  }
308 
309  bool operator !=(const GfRange2d &b) const {
310  return !(*this == b);
311  }
312 
317  GF_API inline bool operator ==(const GfRange2f& other) const;
318  GF_API inline bool operator !=(const GfRange2f& other) const;
319 
321  GF_API
322  double GetDistanceSquared(const GfVec2d &p) const;
323 
326  GF_API
327  GfVec2d GetCorner(size_t i) const;
328 
331  GF_API
332  GfRange2d GetQuadrant(size_t i) const;
333 
335  GF_API
336  static const GfRange2d UnitSquare;
337 
338  private:
340  GfVec2d _min, _max;
341 
343  static void _FindMin(GfVec2d &dest, const GfVec2d &point) {
344  if (point[0] < dest[0]) dest[0] = point[0];
345  if (point[1] < dest[1]) dest[1] = point[1];
346  }
347 
349  static void _FindMax(GfVec2d &dest, const GfVec2d &point) {
350  if (point[0] > dest[0]) dest[0] = point[0];
351  if (point[1] > dest[1]) dest[1] = point[1];
352  }
353 };
354 
357 GF_API std::ostream& operator<<(std::ostream &, GfRange2d const &);
358 
359 PXR_NAMESPACE_CLOSE_SCOPE
360 #include "pxr/base/gf/range2f.h"
361 PXR_NAMESPACE_OPEN_SCOPE
362 
363 inline bool
364 GfRange2d::operator ==(const GfRange2f& other) const {
365  return _min == GfVec2d(other.GetMin()) &&
366  _max == GfVec2d(other.GetMax());
367 }
368 
369 inline bool
370 GfRange2d::operator !=(const GfRange2f& other) const {
371  return !(*this == other);
372 }
373 
374 
375 PXR_NAMESPACE_CLOSE_SCOPE
376 
377 #endif // PXR_BASE_GF_RANGE2D_H
const GfVec2f & GetMin() const
Returns the minimum value of the range.
Definition: range2f.h:93
GfRange2d()
The default constructor creates an empty range.
Definition: range2d.h:82
GfRange2d operator+(const GfRange2d &b) const
binary sum.
Definition: range2d.h:267
static GF_API const GfRange2d UnitSquare
The unit square.
Definition: range2d.h:336
bool IsEmpty() const
Returns whether the range is empty (max &lt; min).
Definition: range2d.h:116
const GfRange2d & UnionWith(const GfRange2d &b)
Extend this to include b.
Definition: range2d.h:174
void ExtendBy(const GfVec2d &point)
Modifies the range if necessary to surround the given value.
Definition: range2d.h:122
GfVec2d GetSize() const
Returns the size of the range.
Definition: range2d.h:99
const GfVec2d & GetMin() const
Returns the minimum value of the range.
Definition: range2d.h:93
static GfRange2d GetUnion(const GfRange2d &a, const GfRange2d &b)
Returns the smallest GfRange2d which contains both a and b.
Definition: range2d.h:166
GfVec2d GetMidpoint() const
Returns the midpoint of the range, that is, 0.5*(min+max).
Definition: range2d.h:104
const GfVec2f & GetMax() const
Returns the maximum value of the range.
Definition: range2f.h:96
const GfRange2d & UnionWith(const GfVec2d &b)
Extend this to include b.
Definition: range2d.h:181
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
GF_API double GetDistanceSquared(const GfVec2d &p) const
Compute the squared distance from a point to the range.
bool operator==(const GfRange2d &b) const
The min and max points must match exactly for equality.
Definition: range2d.h:305
GfRange2d operator/=(double m)
unary division.
Definition: range2d.h:262
Basic type for a vector of 2 double components.
Definition: vec2d.h:63
friend GfRange2d operator*(double m, const GfRange2d &r)
scalar multiply.
Definition: range2d.h:278
GfVec2d MinMaxType
Helper typedef.
Definition: range2d.h:69
bool IsOutside(const GfRange2d &range) const
Returns true if the range is located entirely outside the range.
Definition: range2d.h:160
GfRange2d operator+=(const GfRange2d &b)
unary sum.
Definition: range2d.h:235
Basic type: 2-dimensional floating point range.
Definition: range2d.h:64
GF_API GfVec2d GetCorner(size_t i) const
Returns the ith corner of the range, in the following order: SW, SE, NW, NE.
void SetEmpty()
Sets the range to an empty interval.
Definition: range2d.h:76
friend size_t hash_value(const GfRange2d &r)
hash.
Definition: range2d.h:297
static GfRange2d GetIntersection(const GfRange2d &a, const GfRange2d &b)
Returns a GfRange2d that describes the intersection of a and b.
Definition: range2d.h:206
const GfRange2d & Union(const GfRange2d &b)
Extend this to include b.
Definition: range2d.h:195
bool Contains(const GfRange2d &range) const
Returns true if the range is located entirely inside the range.
Definition: range2d.h:138
bool IsInside(const GfRange2d &range) const
Returns true if the range is located entirely inside the range.
Definition: range2d.h:153
GfRange2d(const GfVec2d &min, const GfVec2d &max)
This constructor initializes the minimum and maximum points.
Definition: range2d.h:87
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
void ExtendBy(const GfRange2d &range)
Modifies the range if necessary to surround the given range.
Definition: range2d.h:126
const GfRange2d & IntersectWith(const GfRange2d &b)
Modifies this range to hold its intersection with b and returns the result.
Definition: range2d.h:221
GfRange2d operator-(const GfRange2d &b) const
binary difference.
Definition: range2d.h:273
void SetMin(const GfVec2d &min)
Sets the minimum value of the range.
Definition: range2d.h:110
bool Contains(const GfVec2d &point) const
Returns true if the point is located inside the range.
Definition: range2d.h:130
bool IsInside(const GfVec2d &point) const
Returns true if the point is located inside the range.
Definition: range2d.h:145
const GfRange2d & Union(const GfVec2d &b)
Extend this to include b.
Definition: range2d.h:201
GfRange2d operator-=(const GfRange2d &b)
unary difference.
Definition: range2d.h:242
double ScalarType
Scalar element type and dimension.
Definition: vec2d.h:67
Basic type: 2-dimensional floating point range.
Definition: range2f.h:64
static GfRange2d Union(const GfRange2d &a, const GfRange2d &b)
Returns the smallest GfRange2d which contains both a and b.
Definition: range2d.h:189
static GfRange2d Intersection(const GfRange2d &a, const GfRange2d &b)
Returns a GfRange2d that describes the intersection of a and b.
Definition: range2d.h:215
void SetMax(const GfVec2d &max)
Sets the maximum value of the range.
Definition: range2d.h:113
GF_API GfRange2d GetQuadrant(size_t i) const
Returns the ith quadrant of the range, in the following order: SW, SE, NW, NE.
friend GfRange2d operator/(const GfRange2d &r, double m)
scalar divide.
Definition: range2d.h:292
GfRange2d operator*=(double m)
unary multiply.
Definition: range2d.h:249
const GfVec2d & GetMax() const
Returns the maximum value of the range.
Definition: range2d.h:96
const GfRange2d & Intersection(const GfRange2d &b)
Modifies this range to hold its intersection with b and returns the result.
Definition: range2d.h:230