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