Debian Codegraph @

src/Imath/ImathInterval.h

raw
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5
6 //
7 // An interval class
8 //
9
10 #ifndef INCLUDED_IMATHINTERVAL_H
11 #define INCLUDED_IMATHINTERVAL_H
12
13 #include "ImathExport.h"
14 #include "ImathNamespace.h"
15
16 #include "ImathVec.h"
17
18 IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
19
20 ///
21 /// An Interval has a min and a max and some miscellaneous
22 /// functions. It is basically a Box<T> that allows T to be a scalar.
23 ///
24
25 template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Interval
26 {
27 public:
28
29 /// @{
30 /// @name Direct access to bounds
31
32 /// The minimum value of the interval
33 T min;
34
35 /// The minimum value of the interval
36 T max;
37
38 /// @}
39
40 /// @{
41 /// @name Constructors
42
43 /// Initialize to the empty interval
44 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Interval() IMATH_NOEXCEPT;
45
46 /// Intitialize to a single point
47 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Interval (const T& point) IMATH_NOEXCEPT;
48
49 /// Intitialize to a given (min,max)
50 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Interval (const T& minT, const T& maxT) IMATH_NOEXCEPT;
51
52 /// @}
53
54 /// @{
55 /// @name Comparison
56
57 /// Equality
58 IMATH_HOSTDEVICE constexpr bool operator== (const Interval<T>& src) const IMATH_NOEXCEPT;
59 /// Inequality
60 IMATH_HOSTDEVICE constexpr bool operator!= (const Interval<T>& src) const IMATH_NOEXCEPT;
61
62 /// @}
63
64 /// @{
65 /// @name Manipulation
66
67 /// Set the interval to be empty. An interval is empty if the
68 /// minimum is greater than the maximum.
69 IMATH_HOSTDEVICE void makeEmpty() IMATH_NOEXCEPT;
70
71 /// Extend the interval to include the given point.
72 IMATH_HOSTDEVICE void extendBy (const T& point) IMATH_NOEXCEPT;
73
74 /// Extend the interval to include the given interval
75 IMATH_HOSTDEVICE void extendBy (const Interval<T>& interval) IMATH_NOEXCEPT;
76
77 /// Make the interval include the entire range of the base type.
78 IMATH_HOSTDEVICE void makeInfinite() IMATH_NOEXCEPT;
79
80 /// @}
81
82 /// @{
83 /// @name Query
84
85 /// Return the size of the interval. The size is (max-min). An empty box has a size of 0.
86 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T size() const IMATH_NOEXCEPT;
87
88 /// Return the center of the interval. The center is defined as
89 /// (max+min)/2. The center of an empty interval is undefined.
90 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T center() const IMATH_NOEXCEPT;
91
92 /// Return true if the given point is inside the interval, false otherwise.
93 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersects (const T& point) const IMATH_NOEXCEPT;
94
95 /// Return true if the given interval is inside the interval, false otherwise.
96 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersects (const Interval<T>& interval) const IMATH_NOEXCEPT;
97
98 /// Return true if the interval is empty, false otherwise. An
99 /// empty interval's minimum is greater than its maximum.
100 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool isEmpty() const IMATH_NOEXCEPT;
101
102 /// Return true if the interval is larger than a single point,
103 /// false otherwise.
104 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool hasVolume() const IMATH_NOEXCEPT;
105
106 /// Return true if the interval contains all points, false
107 /// otherwise. An infinite box has a mimimum of std::numeric_limits<T>::lowest()
108 /// and a maximum of std::numeric_limits<T>::max()
109 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool isInfinite() const IMATH_NOEXCEPT;
110
111 /// @}
112 };
113
114 /// Stream output, as "(min max)"
115 template <class T> std::ostream& operator<< (std::ostream& s, const Interval<T>& v);
116
117 /// Interval of type float
118 typedef Interval<float> Intervalf;
119
120 /// Interval of type double
121 typedef Interval<double> Intervald;
122
123 /// Interval of type short
124 typedef Interval<short> Intervals;
125
126 /// Interval of type integer
127 typedef Interval<int> Intervali;
128
129 template <class T>
130 IMATH_HOSTDEVICE inline IMATH_CONSTEXPR14 Interval<T>::Interval() IMATH_NOEXCEPT
131 {
132 makeEmpty();
133 }
134
135 template <class T>
136 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Interval<T>::Interval (const T& point) IMATH_NOEXCEPT
137 {
138 min = point;
139 max = point;
140 }
141
142 template <class T>
143 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Interval<T>::Interval (const T& minV, const T& maxV) IMATH_NOEXCEPT
144 {
145 min = minV;
146 max = maxV;
147 }
148
149 template <class T>
150 IMATH_HOSTDEVICE constexpr inline bool
151 Interval<T>::operator== (const Interval<T>& src) const IMATH_NOEXCEPT
152 {
153 return (min == src.min && max == src.max);
154 }
155
156 template <class T>
157 IMATH_HOSTDEVICE constexpr inline bool
158 Interval<T>::operator!= (const Interval<T>& src) const IMATH_NOEXCEPT
159 {
160 return (min != src.min || max != src.max);
161 }
162
163 template <class T>
164 IMATH_HOSTDEVICE inline void
165 Interval<T>::makeEmpty() IMATH_NOEXCEPT
166 {
167 min = std::numeric_limits<T>::max();
168 max = std::numeric_limits<T>::lowest();
169 }
170
171 template <class T>
172 IMATH_HOSTDEVICE inline void
173 Interval<T>::makeInfinite() IMATH_NOEXCEPT
174 {
175 min = std::numeric_limits<T>::lowest();
176 max = std::numeric_limits<T>::max();
177 }
178
179
180 template <class T>
181 IMATH_HOSTDEVICE inline void
182 Interval<T>::extendBy (const T& point) IMATH_NOEXCEPT
183 {
184 if (point < min)
185 min = point;
186
187 if (point > max)
188 max = point;
189 }
190
191 template <class T>
192 IMATH_HOSTDEVICE inline void
193 Interval<T>::extendBy (const Interval<T>& interval) IMATH_NOEXCEPT
194 {
195 if (interval.min < min)
196 min = interval.min;
197
198 if (interval.max > max)
199 max = interval.max;
200 }
201
202 template <class T>
203 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
204 Interval<T>::intersects (const T& point) const IMATH_NOEXCEPT
205 {
206 return point >= min && point <= max;
207 }
208
209 template <class T>
210 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
211 Interval<T>::intersects (const Interval<T>& interval) const IMATH_NOEXCEPT
212 {
213 return interval.max >= min && interval.min <= max;
214 }
215
216 template <class T>
217 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline T
218 Interval<T>::size() const IMATH_NOEXCEPT
219 {
220 if (isEmpty())
221 return T(0);
222
223 return max - min;
224 }
225
226 template <class T>
227 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline T
228 Interval<T>::center() const IMATH_NOEXCEPT
229 {
230 return (max + min) / 2;
231 }
232
233 template <class T>
234 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
235 Interval<T>::isEmpty() const IMATH_NOEXCEPT
236 {
237 return max < min;
238 }
239
240 template <class T>
241 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
242 Interval<T>::hasVolume() const IMATH_NOEXCEPT
243 {
244 return max > min;
245 }
246
247 template <class T>
248 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
249 Interval<T>::isInfinite() const IMATH_NOEXCEPT
250 {
251 if (min != std::numeric_limits<T>::lowest() || max != std::numeric_limits<T>::max())
252 return false;
253
254 return true;
255 }
256
257 /// Stream output
258 template <class T>
259 std::ostream&
260 operator<< (std::ostream& s, const Interval<T>& v)
261 {
262 return s << '(' << v.min << ' ' << v.max << ')';
263 }
264
265 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
266
267 #endif // INCLUDED_IMATHINTERVAL_H