PBRT
|
00001 00002 // 00003 // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas 00004 // Digital Ltd. LLC 00005 // 00006 // All rights reserved. 00007 // 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are 00010 // met: 00011 // * Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // * Redistributions in binary form must reproduce the above 00014 // copyright notice, this list of conditions and the following disclaimer 00015 // in the documentation and/or other materials provided with the 00016 // distribution. 00017 // * Neither the name of Industrial Light & Magic nor the names of 00018 // its contributors may be used to endorse or promote products derived 00019 // from this software without specific prior written permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00024 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00025 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00027 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00028 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00029 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00030 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00031 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00034 00035 00036 00037 #ifndef INCLUDED_IMATHINTERVAL_H 00038 #define INCLUDED_IMATHINTERVAL_H 00039 00040 00041 //------------------------------------------------------------------- 00042 // 00043 // class Imath::Interval<class T> 00044 // -------------------------------- 00045 // 00046 // An Interval has a min and a max and some miscellaneous 00047 // functions. It is basically a Box<T> that allows T to be 00048 // a scalar. 00049 // 00050 //------------------------------------------------------------------- 00051 00052 #include "ImathVec.h" 00053 00054 namespace Imath { 00055 00056 00057 template <class T> 00058 class Interval 00059 { 00060 public: 00061 00062 //------------------------- 00063 // Data Members are public 00064 //------------------------- 00065 00066 T min; 00067 T max; 00068 00069 //----------------------------------------------------- 00070 // Constructors - an "empty" Interval is created by default 00071 //----------------------------------------------------- 00072 00073 Interval(); 00074 Interval(const T& point); 00075 Interval(const T& minT, const T& maxT); 00076 00077 //-------------------------------- 00078 // Operators: we get != from STL 00079 //-------------------------------- 00080 00081 bool operator == (const Interval<T> &src) const; 00082 00083 //------------------ 00084 // Interval manipulation 00085 //------------------ 00086 00087 void makeEmpty(); 00088 void extendBy(const T& point); 00089 void extendBy(const Interval<T>& interval); 00090 00091 //--------------------------------------------------- 00092 // Query functions - these compute results each time 00093 //--------------------------------------------------- 00094 00095 T size() const; 00096 T center() const; 00097 bool intersects(const T &point) const; 00098 bool intersects(const Interval<T> &interval) const; 00099 00100 //---------------- 00101 // Classification 00102 //---------------- 00103 00104 bool hasVolume() const; 00105 bool isEmpty() const; 00106 }; 00107 00108 00109 //-------------------- 00110 // Convenient typedefs 00111 //-------------------- 00112 00113 00114 typedef Interval <float> Intervalf; 00115 typedef Interval <double> Intervald; 00116 typedef Interval <short> Intervals; 00117 typedef Interval <int> Intervali; 00118 00119 //---------------- 00120 // Implementation 00121 //---------------- 00122 00123 00124 template <class T> 00125 inline Interval<T>::Interval() 00126 { 00127 makeEmpty(); 00128 } 00129 00130 template <class T> 00131 inline Interval<T>::Interval(const T& point) 00132 { 00133 min = point; 00134 max = point; 00135 } 00136 00137 template <class T> 00138 inline Interval<T>::Interval(const T& minV, const T& maxV) 00139 { 00140 min = minV; 00141 max = maxV; 00142 } 00143 00144 template <class T> 00145 inline bool 00146 Interval<T>::operator == (const Interval<T> &src) const 00147 { 00148 return (min == src.min && max == src.max); 00149 } 00150 00151 template <class T> 00152 inline void 00153 Interval<T>::makeEmpty() 00154 { 00155 min = limits<T>::max(); 00156 max = limits<T>::min(); 00157 } 00158 00159 template <class T> 00160 inline void 00161 Interval<T>::extendBy(const T& point) 00162 { 00163 if ( point < min ) 00164 min = point; 00165 00166 if ( point > max ) 00167 max = point; 00168 } 00169 00170 template <class T> 00171 inline void 00172 Interval<T>::extendBy(const Interval<T>& interval) 00173 { 00174 if ( interval.min < min ) 00175 min = interval.min; 00176 00177 if ( interval.max > max ) 00178 max = interval.max; 00179 } 00180 00181 template <class T> 00182 inline bool 00183 Interval<T>::intersects(const T& point) const 00184 { 00185 return point >= min && point <= max; 00186 } 00187 00188 template <class T> 00189 inline bool 00190 Interval<T>::intersects(const Interval<T>& interval) const 00191 { 00192 return interval.max >= min && interval.min <= max; 00193 } 00194 00195 template <class T> 00196 inline T 00197 Interval<T>::size() const 00198 { 00199 return max-min; 00200 } 00201 00202 template <class T> 00203 inline T 00204 Interval<T>::center() const 00205 { 00206 return (max+min)/2; 00207 } 00208 00209 template <class T> 00210 inline bool 00211 Interval<T>::isEmpty() const 00212 { 00213 return max < min; 00214 } 00215 00216 template <class T> 00217 inline bool Interval<T>::hasVolume() const 00218 { 00219 return max > min; 00220 } 00221 00222 } // namespace Imath 00223 00224 #endif