ImathInterval.h 6.7 KB

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