ImathInterval.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. #ifndef INCLUDED_IMATHINTERVAL_H
  35. #define INCLUDED_IMATHINTERVAL_H
  36. //-------------------------------------------------------------------
  37. //
  38. // class Imath::Interval<class T>
  39. // --------------------------------
  40. //
  41. // An Interval has a min and a max and some miscellaneous
  42. // functions. It is basically a Box<T> that allows T to be
  43. // a scalar.
  44. //
  45. //-------------------------------------------------------------------
  46. #include "ImathVec.h"
  47. #include "ImathNamespace.h"
  48. IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
  49. template <class T>
  50. class Interval
  51. {
  52. public:
  53. //-------------------------
  54. // Data Members are public
  55. //-------------------------
  56. T min;
  57. T max;
  58. //-----------------------------------------------------
  59. // Constructors - an "empty" Interval is created by default
  60. //-----------------------------------------------------
  61. Interval();
  62. Interval(const T& point);
  63. Interval(const T& minT, const T& maxT);
  64. //--------------------------------
  65. // Operators: we get != from STL
  66. //--------------------------------
  67. bool operator == (const Interval<T> &src) const;
  68. //------------------
  69. // Interval manipulation
  70. //------------------
  71. void makeEmpty();
  72. void extendBy(const T& point);
  73. void extendBy(const Interval<T>& interval);
  74. //---------------------------------------------------
  75. // Query functions - these compute results each time
  76. //---------------------------------------------------
  77. T size() const;
  78. T center() const;
  79. bool intersects(const T &point) const;
  80. bool intersects(const Interval<T> &interval) const;
  81. //----------------
  82. // Classification
  83. //----------------
  84. bool hasVolume() const;
  85. bool isEmpty() const;
  86. };
  87. //--------------------
  88. // Convenient typedefs
  89. //--------------------
  90. typedef Interval <float> Intervalf;
  91. typedef Interval <double> Intervald;
  92. typedef Interval <short> Intervals;
  93. typedef Interval <int> Intervali;
  94. //----------------
  95. // Implementation
  96. //----------------
  97. template <class T>
  98. inline Interval<T>::Interval()
  99. {
  100. makeEmpty();
  101. }
  102. template <class T>
  103. inline Interval<T>::Interval(const T& point)
  104. {
  105. min = point;
  106. max = point;
  107. }
  108. template <class T>
  109. inline Interval<T>::Interval(const T& minV, const T& maxV)
  110. {
  111. min = minV;
  112. max = maxV;
  113. }
  114. template <class T>
  115. inline bool
  116. Interval<T>::operator == (const Interval<T> &src) const
  117. {
  118. return (min == src.min && max == src.max);
  119. }
  120. template <class T>
  121. inline void
  122. Interval<T>::makeEmpty()
  123. {
  124. min = limits<T>::max();
  125. max = limits<T>::min();
  126. }
  127. template <class T>
  128. inline void
  129. Interval<T>::extendBy(const T& point)
  130. {
  131. if ( point < min )
  132. min = point;
  133. if ( point > max )
  134. max = point;
  135. }
  136. template <class T>
  137. inline void
  138. Interval<T>::extendBy(const Interval<T>& interval)
  139. {
  140. if ( interval.min < min )
  141. min = interval.min;
  142. if ( interval.max > max )
  143. max = interval.max;
  144. }
  145. template <class T>
  146. inline bool
  147. Interval<T>::intersects(const T& point) const
  148. {
  149. return point >= min && point <= max;
  150. }
  151. template <class T>
  152. inline bool
  153. Interval<T>::intersects(const Interval<T>& interval) const
  154. {
  155. return interval.max >= min && interval.min <= max;
  156. }
  157. template <class T>
  158. inline T
  159. Interval<T>::size() const
  160. {
  161. return max-min;
  162. }
  163. template <class T>
  164. inline T
  165. Interval<T>::center() const
  166. {
  167. return (max+min)/2;
  168. }
  169. template <class T>
  170. inline bool
  171. Interval<T>::isEmpty() const
  172. {
  173. return max < min;
  174. }
  175. template <class T>
  176. inline bool Interval<T>::hasVolume() const
  177. {
  178. return max > min;
  179. }
  180. IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
  181. #endif // INCLUDED_IMATHINTERVAL_H