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 //
24 ////////////////////////////////////////////////////////////////////////
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_RANGE2F_H
29 #define PXR_BASE_GF_RANGE2F_H
30 
31 /// \file gf/range2f.h
32 /// \ingroup group_gf_BasicGeometry
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 GfRange2f> { static const bool value = true; };
54 
55 /// \class GfRange2f
56 /// \ingroup group_gf_BasicGeometry
57 ///
58 /// Basic type: 2-dimensional floating point range.
59 ///
60 /// This class represents a 2-dimensional range (or interval) All
61 /// operations are component-wise and conform to interval mathematics. An
62 /// empty range is one where max < min.
63 /// The default empty is [FLT_MAX,-FLT_MAX]
64 class GfRange2f
65 {
66 public:
67 
68     /// Helper typedef.
69     typedef GfVec2f MinMaxType;
70 
71     static const size_t dimension = GfVec2f::dimension;
72     typedef GfVec2f::ScalarType ScalarType;
73 
74     /// Sets the range to an empty interval
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 
81     /// The default constructor creates an empty range.
82     GfRange2f() {
83         SetEmpty();
84     }
85 
86     /// This constructor initializes the minimum and maximum points.
87     GfRange2f(const GfVec2f &min, const GfVec2f &max)
88         : _min(min), _max(max)
89     {
90     }
91 
92     /// Returns the minimum value of the range.
93     const GfVec2f &GetMin() const { return _min; }
94 
95     /// Returns the maximum value of the range.
96     const GfVec2f &GetMax() const { return _max; }
97 
98     /// Returns the size of the range.
99     GfVec2f GetSize() const { return _max - _min; }
100 
101     /// Returns the midpoint of the range, that is, 0.5*(min+max).
102     /// Note: this returns zero in the case of default-constructed ranges,
103     /// or ranges set via SetEmpty().
104     GfVec2f GetMidpoint() const {
105         return static_cast<ScalarType>(0.5) * _min
106                + static_cast<ScalarType>(0.5) * _max;
107     }
108 
109     /// Sets the minimum value of the range.
110     void SetMin(const GfVec2f &min) { _min = min; }
111 
112     /// Sets the maximum value of the range.
113     void SetMax(const GfVec2f &max) { _max = max; }
114 
115     /// Returns whether the range is empty (max < min).
116     bool IsEmpty() const {
117         return _min[0] > _max[0] || _min[1] > _max[1];
118     }
119 
120     /// Modifies the range if necessary to surround the given value.
121     /// \deprecated Use UnionWith() instead.
122     void ExtendBy(const GfVec2f &point) { UnionWith(point); }
123 
124     /// Modifies the range if necessary to surround the given range.
125     /// \deprecated Use UnionWith() instead.
126     void ExtendBy(const GfRange2f &range) { UnionWith(range); }
127 
128     /// Returns true if the \p point is located inside the range. As with all
129     /// operations of this type, the range is assumed to include its extrema.
130     bool Contains(const GfVec2f &point) const {
131         return (point[0] >= _min[0] && point[0] <= _max[0]
132              && point[1] >= _min[1] && point[1] <= _max[1]);
133     }
134 
135     /// Returns true if the \p range is located entirely inside the range. As
136     /// with all operations of this type, the ranges are assumed to include
137     /// their extrema.
138     bool Contains(const GfRange2f &range) const {
139         return Contains(range._min) && Contains(range._max);
140     }
141 
142     /// Returns true if the \p point is located inside the range. As with all
143     /// operations of this type, the range is assumed to include its extrema.
144     /// \deprecated Use Contains() instead.
145     bool IsInside(const GfVec2f &point) const {
146         return Contains(point);
147     }
148 
149     /// Returns true if the \p range is located entirely inside the range. As
150     /// with all operations of this type, the ranges are assumed to include
151     /// their extrema.
152     /// \deprecated Use Contains() instead.
153     bool IsInside(const GfRange2f &range) const {
154         return Contains(range);
155     }
156 
157     /// Returns true if the \p range is located entirely outside the range. As
158     /// with all operations of this type, the ranges are assumed to include
159     /// their extrema.
160     bool IsOutside(const GfRange2f &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 
165     /// Returns the smallest \c GfRange2f which contains both \p a and \p b.
166     static GfRange2f GetUnion(const GfRange2f &a, const GfRange2f &b) {
167         GfRange2f res = a;
168         _FindMin(res._min,b._min);
169         _FindMax(res._max,b._max);
170         return res;
171     }
172 
173     /// Extend \p this to include \p b.
174     const GfRange2f &UnionWith(const GfRange2f &b) {
175         _FindMin(_min,b._min);
176         _FindMax(_max,b._max);
177         return *this;
178     }
179 
180     /// Extend \p this to include \p b.
181     const GfRange2f &UnionWith(const GfVec2f &b) {
182         _FindMin(_min,b);
183         _FindMax(_max,b);
184         return *this;
185     }
186 
187     /// Returns the smallest \c GfRange2f which contains both \p a and \p b
188     /// \deprecated Use GetUnion() instead.
189     static GfRange2f Union(const GfRange2f &a, const GfRange2f &b) {
190         return GetUnion(a, b);
191     }
192 
193     /// Extend \p this to include \p b.
194     /// \deprecated Use UnionWith() instead.
195     const GfRange2f &Union(const GfRange2f &b) {
196         return UnionWith(b);
197     }
198 
199     /// Extend \p this to include \p b.
200     /// \deprecated Use UnionWith() instead.
201     const GfRange2f &Union(const GfVec2f &b) {
202         return UnionWith(b);
203     }
204 
205     /// Returns a \c GfRange2f that describes the intersection of \p a and \p b.
206     static GfRange2f GetIntersection(const GfRange2f &a, const GfRange2f &b) {
207         GfRange2f res = a;
208         _FindMax(res._min,b._min);
209         _FindMin(res._max,b._max);
210         return res;
211     }
212 
213     /// Returns a \c GfRange2f that describes the intersection of \p a and \p b.
214     /// \deprecated Use GetIntersection() instead.
215     static GfRange2f Intersection(const GfRange2f &a, const GfRange2f &b) {
216         return GetIntersection(a, b);
217     }
218 
219     /// Modifies this range to hold its intersection with \p b and returns the
220     /// result
221     const GfRange2f &IntersectWith(const GfRange2f &b) {
222         _FindMax(_min,b._min);
223         _FindMin(_max,b._max);
224         return *this;
225     }
226 
227     /// Modifies this range to hold its intersection with \p b and returns the
228     /// result.
229     /// \deprecated Use IntersectWith() instead.
230     const GfRange2f &Intersection(const GfRange2f &b) {
231         return IntersectWith(b);
232     }
233 
234     /// unary sum.
235     GfRange2f operator +=(const GfRange2f &b) {
236         _min += b._min;
237         _max += b._max;
238         return *this;
239     }
240 
241     /// unary difference.
242     GfRange2f operator -=(const GfRange2f &b) {
243         _min -= b._max;
244         _max -= b._min;
245         return *this;
246     }
247 
248     /// unary multiply.
249     GfRange2f operator *=(double m) {
250         if (m > 0) {
251             _min *= m;
252             _max *= m;
253         } else {
254             GfVec2f tmp = _min;
255             _min = _max * m;
256             _max = tmp * m;
257         }
258         return *this;
259     }
260 
261     /// unary division.
262     GfRange2f operator /=(double m) {
263         return *this *= (1.0 / m);
264     }
265 
266     /// binary sum.
267     GfRange2f operator +(const GfRange2f &b) const {
268         return GfRange2f(_min + b._min, _max + b._max);
269     }
270 
271 
272     /// binary difference.
273     GfRange2f operator -(const GfRange2f &b) const {
274         return GfRange2f(_min - b._max, _max - b._min);
275     }
276 
277     /// scalar multiply.
278     friend GfRange2f operator *(double m, const GfRange2f &r) {
279         return (m > 0 ?
280             GfRange2f(r._min*m, r._max*m) :
281             GfRange2f(r._max*m, r._min*m));
282     }
283 
284     /// scalar multiply.
285     friend GfRange2f operator *(const GfRange2f &r, double m) {
286         return (m > 0 ?
287             GfRange2f(r._min*m, r._max*m) :
288             GfRange2f(r._max*m, r._min*m));
289     }
290 
291     /// scalar divide.
292     friend GfRange2f operator /(const GfRange2f &r, double m) {
293         return r * (1.0 / m);
294     }
295 
296     /// hash.
297     friend inline size_t hash_value(const GfRange2f &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 
304     /// The min and max points must match exactly for equality.
305     bool operator ==(const GfRange2f &b) const {
306         return (_min == b._min && _max == b._max);
307     }
308 
309     bool operator !=(const GfRange2f &b) const {
310         return !(*this == b);
311     }
312 
313     /// Compare this range to a GfRange2d.
314     ///
315     /// The values must match exactly and it does exactly what you might
316     /// expect when comparing float and double values.
317     GF_API inline bool operator ==(const GfRange2d& other) const;
318     GF_API inline bool operator !=(const GfRange2d& other) const;
319 
320     /// Compute the squared distance from a point to the range.
321     GF_API
322     double GetDistanceSquared(const GfVec2f &p) const;
323 
324     /// Returns the ith corner of the range, in the following order:
325     /// SW, SE, NW, NE.
326     GF_API
327     GfVec2f GetCorner(size_t i) const;
328 
329     /// Returns the ith quadrant of the range, in the following order:
330     /// SW, SE, NW, NE.
331     GF_API
332     GfRange2f GetQuadrant(size_t i) const;
333 
334     /// The unit square.
335     GF_API
336     static const GfRange2f UnitSquare;
337 
338   private:
339     /// Minimum and maximum points.
340     GfVec2f _min, _max;
341 
342     /// Extends minimum point if necessary to contain given point.
343     static void _FindMin(GfVec2f &dest, const GfVec2f &point) {
344         if (point[0] < dest[0]) dest[0] = point[0];
345         if (point[1] < dest[1]) dest[1] = point[1];
346     }
347 
348     /// Extends maximum point if necessary to contain given point.
349     static void _FindMax(GfVec2f &dest, const GfVec2f &point) {
350         if (point[0] > dest[0]) dest[0] = point[0];
351         if (point[1] > dest[1]) dest[1] = point[1];
352     }
353 };
354 
355 /// Output a GfRange2f.
356 /// \ingroup group_gf_DebuggingOutput
357 GF_API std::ostream& operator<<(std::ostream &, GfRange2f const &);
358 
359 PXR_NAMESPACE_CLOSE_SCOPE
360 #include "pxr/base/gf/range2d.h"
361 PXR_NAMESPACE_OPEN_SCOPE
362 
363 inline bool
364 GfRange2f::operator ==(const GfRange2d& other) const {
365     return _min == GfVec2f(other.GetMin()) &&
366            _max == GfVec2f(other.GetMax());
367 }
368 
369 inline bool
370 GfRange2f::operator !=(const GfRange2d& other) const {
371     return !(*this == other);
372 }
373 
374 
375 PXR_NAMESPACE_CLOSE_SCOPE
376 
377 #endif // PXR_BASE_GF_RANGE2F_H
378