Atoms Crowd  7.0.0
ImathInterval.h
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_ATOMSMATHINTERVAL_H
11 #define INCLUDED_ATOMSMATHINTERVAL_H
12 
13 #include <AtomsMath/ImathNamespace.h>
14 #include <AtomsMath/ImathVec.h>
15 
16 ATOMSMATH_INTERNAL_NAMESPACE_HEADER_ENTER
17 
22 
23 template <class T> class Interval
24 {
25  public:
26 
29 
31  T min;
32 
34  T max;
35 
37 
40 
42  ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 Interval() noexcept;
43 
45  ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 Interval (const T& point) noexcept;
46 
48  ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 Interval (const T& minT, const T& maxT) noexcept;
49 
51 
54 
56  ATOMSMATH_HOSTDEVICE constexpr bool operator== (const Interval<T>& src) const noexcept;
58  ATOMSMATH_HOSTDEVICE constexpr bool operator!= (const Interval<T>& src) const noexcept;
59 
61 
64 
67  ATOMSMATH_HOSTDEVICE void makeEmpty() noexcept;
68 
70  ATOMSMATH_HOSTDEVICE void extendBy (const T& point) noexcept;
71 
73  ATOMSMATH_HOSTDEVICE void extendBy (const Interval<T>& interval) noexcept;
74 
76  ATOMSMATH_HOSTDEVICE void makeInfinite() noexcept;
77 
79 
82 
84  ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 T size() const noexcept;
85 
88  ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 T center() const noexcept;
89 
91  ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 bool intersects (const T& point) const noexcept;
92 
94  ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 bool intersects (const Interval<T>& interval) const noexcept;
95 
98  ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 bool isEmpty() const noexcept;
99 
102  ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 bool hasVolume() const noexcept;
103 
107  ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 bool isInfinite() const noexcept;
108 
110 };
111 
113 template <class T> std::ostream& operator<< (std::ostream& s, const Interval<T>& v);
114 
116 typedef Interval<float> Intervalf;
117 
119 typedef Interval<double> Intervald;
120 
122 typedef Interval<short> Intervals;
123 
125 typedef Interval<int> Intervali;
126 
127 template <class T> inline ATOMSMATH_CONSTEXPR14 Interval<T>::Interval() noexcept
128 {
129  makeEmpty();
130 }
131 
132 template <class T> ATOMSMATH_CONSTEXPR14 inline Interval<T>::Interval (const T& point) noexcept
133 {
134  min = point;
135  max = point;
136 }
137 
138 template <class T> ATOMSMATH_CONSTEXPR14 inline Interval<T>::Interval (const T& minV, const T& maxV) noexcept
139 {
140  min = minV;
141  max = maxV;
142 }
143 
144 template <class T>
145 constexpr inline bool
146 Interval<T>::operator== (const Interval<T>& src) const noexcept
147 {
148  return (min == src.min && max == src.max);
149 }
150 
151 template <class T>
152 constexpr inline bool
153 Interval<T>::operator!= (const Interval<T>& src) const noexcept
154 {
155  return (min != src.min || max != src.max);
156 }
157 
158 template <class T>
159 inline void
161 {
162  min = std::numeric_limits<T>::max();
163  max = std::numeric_limits<T>::lowest();
164 }
165 
166 template <class T>
167 inline void
169 {
170  min = std::numeric_limits<T>::lowest();
171  max = std::numeric_limits<T>::max();
172 }
173 
174 
175 template <class T>
176 inline void
177 Interval<T>::extendBy (const T& point) noexcept
178 {
179  if (point < min)
180  min = point;
181 
182  if (point > max)
183  max = point;
184 }
185 
186 template <class T>
187 inline void
188 Interval<T>::extendBy (const Interval<T>& interval) noexcept
189 {
190  if (interval.min < min)
191  min = interval.min;
192 
193  if (interval.max > max)
194  max = interval.max;
195 }
196 
197 template <class T>
198 ATOMSMATH_CONSTEXPR14 inline bool
199 Interval<T>::intersects (const T& point) const noexcept
200 {
201  return point >= min && point <= max;
202 }
203 
204 template <class T>
205 ATOMSMATH_CONSTEXPR14 inline bool
206 Interval<T>::intersects (const Interval<T>& interval) const noexcept
207 {
208  return interval.max >= min && interval.min <= max;
209 }
210 
211 template <class T>
212 ATOMSMATH_CONSTEXPR14 inline T
213 Interval<T>::size() const noexcept
214 {
215  if (isEmpty())
216  return T(0);
217 
218  return max - min;
219 }
220 
221 template <class T>
222 ATOMSMATH_CONSTEXPR14 inline T
223 Interval<T>::center() const noexcept
224 {
225  return (max + min) / 2;
226 }
227 
228 template <class T>
229 ATOMSMATH_CONSTEXPR14 inline bool
230 Interval<T>::isEmpty() const noexcept
231 {
232  return max < min;
233 }
234 
235 template <class T>
236 ATOMSMATH_CONSTEXPR14 inline bool
237 Interval<T>::hasVolume() const noexcept
238 {
239  return max > min;
240 }
241 
242 template <class T>
243 ATOMSMATH_CONSTEXPR14 inline bool
244 Interval<T>::isInfinite() const noexcept
245 {
246  if (min != std::numeric_limits<T>::lowest() || max != std::numeric_limits<T>::max())
247  return false;
248 
249  return true;
250 }
251 
253 template <class T>
254 std::ostream&
255 operator<< (std::ostream& s, const Interval<T>& v)
256 {
257  return s << '(' << v.min << ' ' << v.max << ')';
258 }
259 
260 ATOMSMATH_INTERNAL_NAMESPACE_HEADER_EXIT
261 
262 #endif // INCLUDED_ATOMSMATHINTERVAL_H
Definition: ImathInterval.h:24
ATOMSMATH_HOSTDEVICE void extendBy(const T &point) noexcept
Extend the interval to include the given point.
Definition: ImathInterval.h:177
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 bool hasVolume() const noexcept
Definition: ImathInterval.h:237
T max
The minimum value of the interval.
Definition: ImathInterval.h:34
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 T size() const noexcept
Return the size of the interval. The size is (max-min). An empty box has a size of 0.
Definition: ImathInterval.h:213
T min
The minimum value of the interval.
Definition: ImathInterval.h:31
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 bool isInfinite() const noexcept
Definition: ImathInterval.h:244
ATOMSMATH_HOSTDEVICE void makeEmpty() noexcept
Definition: ImathInterval.h:160
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 bool intersects(const T &point) const noexcept
Return true if the given point is inside the interval, false otherwise.
Definition: ImathInterval.h:199
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 Interval() noexcept
Initialize to the empty interval.
Definition: ImathInterval.h:127
constexpr ATOMSMATH_HOSTDEVICE bool operator!=(const Interval< T > &src) const noexcept
Inequality.
Definition: ImathInterval.h:153
constexpr ATOMSMATH_HOSTDEVICE bool operator==(const Interval< T > &src) const noexcept
Equality.
Definition: ImathInterval.h:146
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 bool isEmpty() const noexcept
Definition: ImathInterval.h:230
ATOMSMATH_HOSTDEVICE void makeInfinite() noexcept
Make the interval include the entire range of the base type.
Definition: ImathInterval.h:168
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 T center() const noexcept
Definition: ImathInterval.h:223