[855] | 1 | /////////////////////////////////////////////////////////////////////////// |
---|
| 2 | // |
---|
| 3 | // Copyright (c) 2002, 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 | |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | #ifndef INCLUDED_IMATHINTERVAL_H |
---|
| 38 | #define INCLUDED_IMATHINTERVAL_H |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | //------------------------------------------------------------------- |
---|
| 42 | // |
---|
| 43 | // class Imath::Interval<class T> |
---|
| 44 | // -------------------------------- |
---|
| 45 | // |
---|
| 46 | // An Interval has a min and a max and some miscellaneous |
---|
| 47 | // functions. It is basically a Box<T> that allows T to be |
---|
| 48 | // a scalar. |
---|
| 49 | // |
---|
| 50 | //------------------------------------------------------------------- |
---|
| 51 | |
---|
| 52 | #include <ImathVec.h> |
---|
| 53 | |
---|
| 54 | namespace Imath { |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | template <class T> |
---|
| 58 | class Interval |
---|
| 59 | { |
---|
| 60 | public: |
---|
| 61 | |
---|
| 62 | //------------------------- |
---|
| 63 | // Data Members are public |
---|
| 64 | //------------------------- |
---|
| 65 | |
---|
| 66 | T min; |
---|
| 67 | T max; |
---|
| 68 | |
---|
| 69 | //----------------------------------------------------- |
---|
| 70 | // Constructors - an "empty" Interval is created by default |
---|
| 71 | //----------------------------------------------------- |
---|
| 72 | |
---|
| 73 | Interval(); |
---|
| 74 | Interval(const T& point); |
---|
| 75 | Interval(const T& minT, const T& maxT); |
---|
| 76 | |
---|
| 77 | //-------------------------------- |
---|
| 78 | // Operators: we get != from STL |
---|
| 79 | //-------------------------------- |
---|
| 80 | |
---|
| 81 | bool operator == (const Interval<T> &src) const; |
---|
| 82 | |
---|
| 83 | //------------------ |
---|
| 84 | // Interval manipulation |
---|
| 85 | //------------------ |
---|
| 86 | |
---|
| 87 | void makeEmpty(); |
---|
| 88 | void extendBy(const T& point); |
---|
| 89 | void extendBy(const Interval<T>& interval); |
---|
| 90 | |
---|
| 91 | //--------------------------------------------------- |
---|
| 92 | // Query functions - these compute results each time |
---|
| 93 | //--------------------------------------------------- |
---|
| 94 | |
---|
| 95 | T size() const; |
---|
| 96 | T center() const; |
---|
| 97 | bool intersects(const T &point) const; |
---|
| 98 | bool intersects(const Interval<T> &interval) const; |
---|
| 99 | |
---|
| 100 | //---------------- |
---|
| 101 | // Classification |
---|
| 102 | //---------------- |
---|
| 103 | |
---|
| 104 | bool hasVolume() const; |
---|
| 105 | bool isEmpty() const; |
---|
| 106 | }; |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | //-------------------- |
---|
| 110 | // Convenient typedefs |
---|
| 111 | //-------------------- |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | typedef Interval <float> Intervalf; |
---|
| 115 | typedef Interval <double> Intervald; |
---|
| 116 | typedef Interval <short> Intervals; |
---|
| 117 | typedef Interval <int> Intervali; |
---|
| 118 | |
---|
| 119 | //---------------- |
---|
| 120 | // Implementation |
---|
| 121 | //---------------- |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | template <class T> |
---|
| 125 | inline Interval<T>::Interval() |
---|
| 126 | { |
---|
| 127 | makeEmpty(); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | template <class T> |
---|
| 131 | inline Interval<T>::Interval(const T& point) |
---|
| 132 | { |
---|
| 133 | min = point; |
---|
| 134 | max = point; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | template <class T> |
---|
| 138 | inline Interval<T>::Interval(const T& minV, const T& maxV) |
---|
| 139 | { |
---|
| 140 | min = minV; |
---|
| 141 | max = maxV; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | template <class T> |
---|
| 145 | inline bool |
---|
| 146 | Interval<T>::operator == (const Interval<T> &src) const |
---|
| 147 | { |
---|
| 148 | return (min == src.min && max == src.max); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | template <class T> |
---|
| 152 | inline void |
---|
| 153 | Interval<T>::makeEmpty() |
---|
| 154 | { |
---|
| 155 | min = limits<T>::max(); |
---|
| 156 | max = limits<T>::min(); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | template <class T> |
---|
| 160 | inline void |
---|
| 161 | Interval<T>::extendBy(const T& point) |
---|
| 162 | { |
---|
| 163 | if ( point < min ) |
---|
| 164 | min = point; |
---|
| 165 | |
---|
| 166 | if ( point > max ) |
---|
| 167 | max = point; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | template <class T> |
---|
| 171 | inline void |
---|
| 172 | Interval<T>::extendBy(const Interval<T>& interval) |
---|
| 173 | { |
---|
| 174 | if ( interval.min < min ) |
---|
| 175 | min = interval.min; |
---|
| 176 | |
---|
| 177 | if ( interval.max > max ) |
---|
| 178 | max = interval.max; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | template <class T> |
---|
| 182 | inline bool |
---|
| 183 | Interval<T>::intersects(const T& point) const |
---|
| 184 | { |
---|
| 185 | return point >= min && point <= max; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | template <class T> |
---|
| 189 | inline bool |
---|
| 190 | Interval<T>::intersects(const Interval<T>& interval) const |
---|
| 191 | { |
---|
| 192 | return interval.max >= min && interval.min <= max; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | template <class T> |
---|
| 196 | inline T |
---|
| 197 | Interval<T>::size() const |
---|
| 198 | { |
---|
| 199 | return max-min; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | template <class T> |
---|
| 203 | inline T |
---|
| 204 | Interval<T>::center() const |
---|
| 205 | { |
---|
| 206 | return (max+min)/2; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | template <class T> |
---|
| 210 | inline bool |
---|
| 211 | Interval<T>::isEmpty() const |
---|
| 212 | { |
---|
| 213 | return max < min; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | template <class T> |
---|
| 217 | inline bool Interval<T>::hasVolume() const |
---|
| 218 | { |
---|
| 219 | return max > min; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | } // namespace Imath |
---|
| 223 | |
---|
| 224 | #endif |
---|