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 #include "pxr/base/tf/hash.h"
41 
42 #include <cfloat>
43 #include <cstddef>
44 #include <iosfwd>
45 
46 PXR_NAMESPACE_OPEN_SCOPE
47 
48 class GfRange2d;
49 class GfRange2f;
50 
51 template <>
52 struct GfIsGfRange<class GfRange2d> { static const bool value = true; };
53 
63 class GfRange2d
64 {
65 public:
66 
69 
70  static const size_t dimension = GfVec2d::dimension;
71  typedef GfVec2d::ScalarType ScalarType;
72 
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 
82  SetEmpty();
83  }
84 
86  GfRange2d(const GfVec2d &min, const GfVec2d &max)
87  : _min(min), _max(max)
88  {
89  }
90 
91 
93  GF_API
94  GfRange2d(class GfRange2f const &other);
95 
97  const GfVec2d &GetMin() const { return _min; }
98 
100  const GfVec2d &GetMax() const { return _max; }
101 
103  GfVec2d GetSize() const { return _max - _min; }
104 
109  return static_cast<ScalarType>(0.5) * _min
110  + static_cast<ScalarType>(0.5) * _max;
111  }
112 
114  void SetMin(const GfVec2d &min) { _min = min; }
115 
117  void SetMax(const GfVec2d &max) { _max = max; }
118 
120  bool IsEmpty() const {
121  return _min[0] > _max[0] || _min[1] > _max[1];
122  }
123 
126  void ExtendBy(const GfVec2d &point) { UnionWith(point); }
127 
130  void ExtendBy(const GfRange2d &range) { UnionWith(range); }
131 
134  bool Contains(const GfVec2d &point) const {
135  return (point[0] >= _min[0] && point[0] <= _max[0]
136  && point[1] >= _min[1] && point[1] <= _max[1]);
137  }
138 
142  bool Contains(const GfRange2d &range) const {
143  return Contains(range._min) && Contains(range._max);
144  }
145 
149  bool IsInside(const GfVec2d &point) const {
150  return Contains(point);
151  }
152 
157  bool IsInside(const GfRange2d &range) const {
158  return Contains(range);
159  }
160 
164  bool IsOutside(const GfRange2d &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 
170  static GfRange2d GetUnion(const GfRange2d &a, const GfRange2d &b) {
171  GfRange2d res = a;
172  _FindMin(res._min,b._min);
173  _FindMax(res._max,b._max);
174  return res;
175  }
176 
178  const GfRange2d &UnionWith(const GfRange2d &b) {
179  _FindMin(_min,b._min);
180  _FindMax(_max,b._max);
181  return *this;
182  }
183 
185  const GfRange2d &UnionWith(const GfVec2d &b) {
186  _FindMin(_min,b);
187  _FindMax(_max,b);
188  return *this;
189  }
190 
193  static GfRange2d Union(const GfRange2d &a, const GfRange2d &b) {
194  return GetUnion(a, b);
195  }
196 
199  const GfRange2d &Union(const GfRange2d &b) {
200  return UnionWith(b);
201  }
202 
205  const GfRange2d &Union(const GfVec2d &b) {
206  return UnionWith(b);
207  }
208 
210  static GfRange2d GetIntersection(const GfRange2d &a, const GfRange2d &b) {
211  GfRange2d res = a;
212  _FindMax(res._min,b._min);
213  _FindMin(res._max,b._max);
214  return res;
215  }
216 
219  static GfRange2d Intersection(const GfRange2d &a, const GfRange2d &b) {
220  return GetIntersection(a, b);
221  }
222 
225  const GfRange2d &IntersectWith(const GfRange2d &b) {
226  _FindMax(_min,b._min);
227  _FindMin(_max,b._max);
228  return *this;
229  }
230 
234  const GfRange2d &Intersection(const GfRange2d &b) {
235  return IntersectWith(b);
236  }
237 
240  _min += b._min;
241  _max += b._max;
242  return *this;
243  }
244 
247  _min -= b._max;
248  _max -= b._min;
249  return *this;
250  }
251 
253  GfRange2d &operator *=(double m) {
254  if (m > 0) {
255  _min *= m;
256  _max *= m;
257  } else {
258  GfVec2d tmp = _min;
259  _min = _max * m;
260  _max = tmp * m;
261  }
262  return *this;
263  }
264 
266  GfRange2d &operator /=(double m) {
267  return *this *= (1.0 / m);
268  }
269 
271  GfRange2d operator +(const GfRange2d &b) const {
272  return GfRange2d(_min + b._min, _max + b._max);
273  }
274 
275 
277  GfRange2d operator -(const GfRange2d &b) const {
278  return GfRange2d(_min - b._max, _max - b._min);
279  }
280 
282  friend GfRange2d operator *(double m, const GfRange2d &r) {
283  return (m > 0 ?
284  GfRange2d(r._min*m, r._max*m) :
285  GfRange2d(r._max*m, r._min*m));
286  }
287 
289  friend GfRange2d operator *(const GfRange2d &r, double m) {
290  return (m > 0 ?
291  GfRange2d(r._min*m, r._max*m) :
292  GfRange2d(r._max*m, r._min*m));
293  }
294 
296  friend GfRange2d operator /(const GfRange2d &r, double m) {
297  return r * (1.0 / m);
298  }
299 
301  friend inline size_t hash_value(const GfRange2d &r) {
302  return TfHash::Combine(r._min, r._max);
303  }
304 
306  bool operator ==(const GfRange2d &b) const {
307  return (_min == b._min && _max == b._max);
308  }
309 
310  bool operator !=(const GfRange2d &b) const {
311  return !(*this == b);
312  }
313 
318  GF_API inline bool operator ==(const GfRange2f& other) const;
319  GF_API inline bool operator !=(const GfRange2f& other) const;
320 
322  GF_API
323  double GetDistanceSquared(const GfVec2d &p) const;
324 
327  GF_API
328  GfVec2d GetCorner(size_t i) const;
329 
332  GF_API
333  GfRange2d GetQuadrant(size_t i) const;
334 
336  GF_API
337  static const GfRange2d UnitSquare;
338 
339  private:
341  GfVec2d _min, _max;
342 
344  static void _FindMin(GfVec2d &dest, const GfVec2d &point) {
345  if (point[0] < dest[0]) dest[0] = point[0];
346  if (point[1] < dest[1]) dest[1] = point[1];
347  }
348 
350  static void _FindMax(GfVec2d &dest, const GfVec2d &point) {
351  if (point[0] > dest[0]) dest[0] = point[0];
352  if (point[1] > dest[1]) dest[1] = point[1];
353  }
354 };
355 
358 GF_API std::ostream& operator<<(std::ostream &, GfRange2d const &);
359 
360 PXR_NAMESPACE_CLOSE_SCOPE
361 #include "pxr/base/gf/range2f.h"
362 PXR_NAMESPACE_OPEN_SCOPE
363 
364 inline bool
365 GfRange2d::operator ==(const GfRange2f& other) const {
366  return _min == GfVec2d(other.GetMin()) &&
367  _max == GfVec2d(other.GetMax());
368 }
369 
370 inline bool
371 GfRange2d::operator !=(const GfRange2f& other) const {
372  return !(*this == other);
373 }
374 
375 
376 PXR_NAMESPACE_CLOSE_SCOPE
377 
378 #endif // PXR_BASE_GF_RANGE2D_H
bool Contains(const GfVec2d &point) const
Returns true if the point is located inside the range.
Definition: range2d.h:134
GfRange2d()
The default constructor creates an empty range.
Definition: range2d.h:81
static GF_API const GfRange2d UnitSquare
The unit square.
Definition: range2d.h:337
const GfRange2d & UnionWith(const GfRange2d &b)
Extend this to include b.
Definition: range2d.h:178
void ExtendBy(const GfVec2d &point)
Modifies the range if necessary to surround the given value.
Definition: range2d.h:126
static GfRange2d GetUnion(const GfRange2d &a, const GfRange2d &b)
Returns the smallest GfRange2d which contains both a and b.
Definition: range2d.h:170
const GfVec2d & GetMax() const
Returns the maximum value of the range.
Definition: range2d.h:100
GfVec2d GetSize() const
Returns the size of the range.
Definition: range2d.h:103
const GfRange2d & UnionWith(const GfVec2d &b)
Extend this to include b.
Definition: range2d.h:185
friend GfRange2d operator *(const GfRange2d &r, double m)
scalar multiply.
Definition: range2d.h:289
A metafunction with a static const bool member 'value' that is true for GfRange types and false for a...
Definition: traits.h:57
GF_API std::ostream & operator<<(std::ostream &, GfRange2d const &)
Output a GfRange2d.
bool Contains(const GfRange2d &range) const
Returns true if the range is located entirely inside the range.
Definition: range2d.h:142
GfVec2d GetMidpoint() const
Returns the midpoint of the range, that is, 0.5*(min+max).
Definition: range2d.h:108
GfRange2d & operator/=(double m)
unary division.
Definition: range2d.h:266
GF_API double GetDistanceSquared(const GfVec2d &p) const
Compute the squared distance from a point to the range.
GF_API GfVec2d GetCorner(size_t i) const
Returns the ith corner of the range, in the following order: SW, SE, NW, NE.
Basic type for a vector of 2 double components.
Definition: vec2d.h:62
GfVec2d MinMaxType
Helper typedef.
Definition: range2d.h:68
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:519
const GfVec2f & GetMin() const
Returns the minimum value of the range.
Definition: range2f.h:97
Basic type: 2-dimensional floating point range.
Definition: range2d.h:63
const GfVec2d & GetMin() const
Returns the minimum value of the range.
Definition: range2d.h:97
void SetEmpty()
Sets the range to an empty interval.
Definition: range2d.h:75
friend GfRange2d operator *(double m, const GfRange2d &r)
scalar multiply.
Definition: range2d.h:282
friend size_t hash_value(const GfRange2d &r)
hash.
Definition: range2d.h:301
GfRange2d & operator *=(double m)
unary multiply.
Definition: range2d.h:253
bool IsEmpty() const
Returns whether the range is empty (max < min).
Definition: range2d.h:120
static GfRange2d GetIntersection(const GfRange2d &a, const GfRange2d &b)
Returns a GfRange2d that describes the intersection of a and b.
Definition: range2d.h:210
GfRange2d operator -(const GfRange2d &b) const
binary difference.
Definition: range2d.h:277
GfRange2d & operator -=(const GfRange2d &b)
unary difference.
Definition: range2d.h:246
const GfRange2d & Union(const GfRange2d &b)
Extend this to include b.
Definition: range2d.h:199
bool IsOutside(const GfRange2d &range) const
Returns true if the range is located entirely outside the range.
Definition: range2d.h:164
GfRange2d(const GfVec2d &min, const GfVec2d &max)
This constructor initializes the minimum and maximum points.
Definition: range2d.h:86
GF_API GfRange2d GetQuadrant(size_t i) const
Returns the ith quadrant of the range, in the following order: SW, SE, NW, NE.
bool IsInside(const GfVec2d &point) const
Returns true if the point is located inside the range.
Definition: range2d.h:149
void ExtendBy(const GfRange2d &range)
Modifies the range if necessary to surround the given range.
Definition: range2d.h:130
const GfRange2d & IntersectWith(const GfRange2d &b)
Modifies this range to hold its intersection with b and returns the result.
Definition: range2d.h:225
GfRange2d operator+(const GfRange2d &b) const
binary sum.
Definition: range2d.h:271
void SetMin(const GfVec2d &min)
Sets the minimum value of the range.
Definition: range2d.h:114
bool IsInside(const GfRange2d &range) const
Returns true if the range is located entirely inside the range.
Definition: range2d.h:157
const GfVec2f & GetMax() const
Returns the maximum value of the range.
Definition: range2f.h:100
const GfRange2d & Union(const GfVec2d &b)
Extend this to include b.
Definition: range2d.h:205
double ScalarType
Scalar element type and dimension.
Definition: vec2d.h:66
Basic type: 2-dimensional floating point range.
Definition: range2f.h:63
static GfRange2d Union(const GfRange2d &a, const GfRange2d &b)
Returns the smallest GfRange2d which contains both a and b.
Definition: range2d.h:193
static GfRange2d Intersection(const GfRange2d &a, const GfRange2d &b)
Returns a GfRange2d that describes the intersection of a and b.
Definition: range2d.h:219
void SetMax(const GfVec2d &max)
Sets the maximum value of the range.
Definition: range2d.h:117
friend GfRange2d operator/(const GfRange2d &r, double m)
scalar divide.
Definition: range2d.h:296
bool operator==(const GfRange2d &b) const
The min and max points must match exactly for equality.
Definition: range2d.h:306
GfRange2d & operator+=(const GfRange2d &b)
unary sum.
Definition: range2d.h:239
const GfRange2d & Intersection(const GfRange2d &b)
Modifies this range to hold its intersection with b and returns the result.
Definition: range2d.h:234