HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImathInterval.h
Go to the documentation of this file.
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  /// @name Direct access to bounds
30 
31  /// The minimum value of the interval
32  T min;
33 
34  /// The minimum value of the interval
35  T max;
36 
37  /// @}
38 
39  /// @{
40  /// @name Constructors
41 
42  /// Initialize to the empty interval
43  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Interval () IMATH_NOEXCEPT;
44 
45  /// Intitialize to a single point
46  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Interval (const T& point) IMATH_NOEXCEPT;
47 
48  /// Intitialize to a given (min,max)
49  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Interval (const T& minT, const T& maxT)
50  IMATH_NOEXCEPT;
51 
52  /// @}
53 
54  /// @{
55  /// @name Comparison
56 
57  /// Equality
58  IMATH_HOSTDEVICE constexpr bool
59  operator== (const Interval<T>& src) const IMATH_NOEXCEPT;
60  /// Inequality
61  IMATH_HOSTDEVICE constexpr bool
62  operator!= (const Interval<T>& src) const IMATH_NOEXCEPT;
63 
64  /// @}
65 
66  /// @{
67  /// @name Manipulation
68 
69  /// Set the interval to be empty. An interval is empty if the
70  /// minimum is greater than the maximum.
71  IMATH_HOSTDEVICE void makeEmpty () IMATH_NOEXCEPT;
72 
73  /// Extend the interval to include the given point.
74  IMATH_HOSTDEVICE void extendBy (const T& point) IMATH_NOEXCEPT;
75 
76  /// Extend the interval to include the given interval
77  IMATH_HOSTDEVICE void extendBy (const Interval<T>& interval) IMATH_NOEXCEPT;
78 
79  /// Make the interval include the entire range of the base type.
80  IMATH_HOSTDEVICE void makeInfinite () IMATH_NOEXCEPT;
81 
82  /// @}
83 
84  /// @{
85  /// @name Query
86 
87  /// Return the size of the interval. The size is (max-min). An empty box has a size of 0.
88  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T size () const IMATH_NOEXCEPT;
89 
90  /// Return the center of the interval. The center is defined as
91  /// (max+min)/2. The center of an empty interval is undefined.
92  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T center () const IMATH_NOEXCEPT;
93 
94  /// Return true if the given point is inside the interval, false otherwise.
95  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool
96  intersects (const T& point) const IMATH_NOEXCEPT;
97 
98  /// Return true if the given interval is inside the interval, false otherwise.
99  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool
100  intersects (const Interval<T>& interval) const IMATH_NOEXCEPT;
101 
102  /// Return true if the interval is empty, false otherwise. An
103  /// empty interval's minimum is greater than its maximum.
104  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool isEmpty () const IMATH_NOEXCEPT;
105 
106  /// Return true if the interval is larger than a single point,
107  /// false otherwise.
108  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool hasVolume () const IMATH_NOEXCEPT;
109 
110  /// Return true if the interval contains all points, false
111  /// otherwise. An infinite box has a mimimum of std::numeric_limits<T>::lowest()
112  /// and a maximum of std::numeric_limits<T>::max()
113  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool isInfinite () const IMATH_NOEXCEPT;
114 
115  /// @}
116 };
117 
118 /// Stream output, as "(min max)"
119 template <class T>
120 std::ostream& operator<< (std::ostream& s, const Interval<T>& v);
121 
122 /// Interval of type float
123 typedef Interval<float> Intervalf;
124 
125 /// Interval of type double
126 typedef Interval<double> Intervald;
127 
128 /// Interval of type short
129 typedef Interval<short> Intervals;
130 
131 /// Interval of type integer
132 typedef Interval<int> Intervali;
133 
134 template <class T>
135 IMATH_HOSTDEVICE inline IMATH_CONSTEXPR14
136 Interval<T>::Interval () IMATH_NOEXCEPT
137 {
138  makeEmpty ();
139 }
140 
141 template <class T>
142 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Interval<T>::Interval (const T& point)
144 {
145  min = point;
146  max = point;
147 }
148 
149 template <class T>
150 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Interval<T>::Interval (
151  const T& minV, const T& maxV) IMATH_NOEXCEPT
152 {
153  min = minV;
154  max = maxV;
155 }
156 
157 template <class T>
158 IMATH_HOSTDEVICE constexpr inline bool
160 {
161  return (min == src.min && max == src.max);
162 }
163 
164 template <class T>
165 IMATH_HOSTDEVICE constexpr inline bool
167 {
168  return (min != src.min || max != src.max);
169 }
170 
171 template <class T>
172 IMATH_HOSTDEVICE inline void
174 {
176  max = std::numeric_limits<T>::lowest ();
177 }
178 
179 template <class T>
180 IMATH_HOSTDEVICE inline void
182 {
183  min = std::numeric_limits<T>::lowest ();
185 }
186 
187 template <class T>
188 IMATH_HOSTDEVICE inline void
190 {
191  if (point < min) min = point;
192 
193  if (point > max) max = point;
194 }
195 
196 template <class T>
197 IMATH_HOSTDEVICE inline void
199 {
200  if (interval.min < min) min = interval.min;
201 
202  if (interval.max > max) max = interval.max;
203 }
204 
205 template <class T>
206 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
208 {
209  return point >= min && point <= max;
210 }
211 
212 template <class T>
213 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
215 {
216  return interval.max >= min && interval.min <= max;
217 }
218 
219 template <class T>
220 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline T
222 {
223  if (isEmpty ()) return T (0);
224 
225  return max - min;
226 }
227 
228 template <class T>
229 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline T
231 {
232  return (max + min) / 2;
233 }
234 
235 template <class T>
236 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
238 {
239  return max < min;
240 }
241 
242 template <class T>
243 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
245 {
246  return max > min;
247 }
248 
249 template <class T>
250 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
252 {
253  if (min != std::numeric_limits<T>::lowest () ||
255  return false;
256 
257  return true;
258 }
259 
260 /// Stream output
261 template <class T>
262 std::ostream&
263 operator<< (std::ostream& s, const Interval<T>& v)
264 {
265  return s << '(' << v.min << ' ' << v.max << ')';
266 }
267 
268 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
269 
270 #endif // INCLUDED_IMATHINTERVAL_H
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T size() const IMATH_NOEXCEPT
Return the size of the interval. The size is (max-min). An empty box has a size of 0...
#define IMATH_NOEXCEPT
Definition: ImathConfig.h:78
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:632
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Interval() IMATH_NOEXCEPT
Initialize to the empty interval.
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersects(const T &point) const IMATH_NOEXCEPT
Return true if the given point is inside the interval, false otherwise.
const GLdouble * v
Definition: glcorearb.h:837
T min
The minimum value of the interval.
Definition: ImathInterval.h:32
GLdouble s
Definition: glad.h:3009
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool hasVolume() const IMATH_NOEXCEPT
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
#define IMATH_HOSTDEVICE
Definition: ImathConfig.h:108
IMATH_HOSTDEVICE void makeInfinite() IMATH_NOEXCEPT
Make the interval include the entire range of the base type.
T max
The minimum value of the interval.
Definition: ImathInterval.h:35
bool isInfinite(const float x)
Return true if x is an infinity value (either positive infinity or negative infinity).
Definition: Math.h:402
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T center() const IMATH_NOEXCEPT
IMATH_HOSTDEVICE constexpr bool operator!=(const Interval< T > &src) const IMATH_NOEXCEPT
Inequality.
GLsizeiptr size
Definition: glcorearb.h:664
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool isEmpty() const IMATH_NOEXCEPT
IMATH_HOSTDEVICE constexpr bool operator==(const Interval< T > &src) const IMATH_NOEXCEPT
Equality.
#define IMATH_EXPORT_TEMPLATE_TYPE
Definition: ImathExport.h:61
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersects(const Box< Vec3< T >> &b, const Line3< T > &r, Vec3< T > &ip) IMATH_NOEXCEPT
Definition: ImathBoxAlgo.h:649
IMATH_HOSTDEVICE void extendBy(const T &point) IMATH_NOEXCEPT
Extend the interval to include the given point.
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool isInfinite() const IMATH_NOEXCEPT
IMATH_HOSTDEVICE void makeEmpty() IMATH_NOEXCEPT
GLenum src
Definition: glcorearb.h:1793