[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_IMATHFUN_H |
---|
| 38 | #define INCLUDED_IMATHFUN_H |
---|
| 39 | |
---|
| 40 | //----------------------------------------------------------------------------- |
---|
| 41 | // |
---|
| 42 | // Miscellaneous utility functions |
---|
| 43 | // |
---|
| 44 | //----------------------------------------------------------------------------- |
---|
| 45 | |
---|
| 46 | #include <ImathLimits.h> |
---|
| 47 | |
---|
| 48 | namespace Imath { |
---|
| 49 | |
---|
| 50 | template <class T> |
---|
| 51 | inline T |
---|
| 52 | abs (T a) |
---|
| 53 | { |
---|
| 54 | return (a > 0) ? a : -a; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | template <class T> |
---|
| 59 | inline int |
---|
| 60 | sign (T a) |
---|
| 61 | { |
---|
| 62 | return (a > 0)? 1 : ((a < 0) ? -1 : 0); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | template <class T, class Q> |
---|
| 67 | inline T |
---|
| 68 | lerp (T a, T b, Q t) |
---|
| 69 | { |
---|
| 70 | return (T) (a + (b - a) * t); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | template <class T, class Q> |
---|
| 75 | inline T |
---|
| 76 | ulerp (T a, T b, Q t) |
---|
| 77 | { |
---|
| 78 | return (T) ((a > b)? (a - (a - b) * t): (a + (b - a) * t)); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | template <class T> |
---|
| 83 | inline T |
---|
| 84 | lerpfactor(T m, T a, T b) |
---|
| 85 | { |
---|
| 86 | // |
---|
| 87 | // Return how far m is between a and b, that is return t such that |
---|
| 88 | // if: |
---|
| 89 | // t = lerpfactor(m, a, b); |
---|
| 90 | // then: |
---|
| 91 | // m = lerp(a, b, t); |
---|
| 92 | // |
---|
| 93 | // If a==b, return 0. |
---|
| 94 | // |
---|
| 95 | |
---|
| 96 | T d = b - a; |
---|
| 97 | T n = m - a; |
---|
| 98 | |
---|
| 99 | if (abs(d) > T(1) || abs(n) < limits<T>::max() * abs(d)) |
---|
| 100 | return n / d; |
---|
| 101 | |
---|
| 102 | return T(0); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | template <class T> |
---|
| 107 | inline T |
---|
| 108 | clamp (T a, T l, T h) |
---|
| 109 | { |
---|
| 110 | return (a < l)? l : ((a > h)? h : a); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | template <class T> |
---|
| 115 | inline int |
---|
| 116 | cmp (T a, T b) |
---|
| 117 | { |
---|
| 118 | return Imath::sign (a - b); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | template <class T> |
---|
| 123 | inline int |
---|
| 124 | cmpt (T a, T b, T t) |
---|
| 125 | { |
---|
| 126 | return (Imath::abs (a - b) <= t)? 0 : cmp (a, b); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | template <class T> |
---|
| 131 | inline bool |
---|
| 132 | iszero (T a, T t) |
---|
| 133 | { |
---|
| 134 | return (Imath::abs (a) <= t) ? 1 : 0; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | template <class T1, class T2, class T3> |
---|
| 139 | inline bool |
---|
| 140 | equal (T1 a, T2 b, T3 t) |
---|
| 141 | { |
---|
| 142 | return Imath::abs (a - b) <= t; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | template <class T> |
---|
| 146 | inline int |
---|
| 147 | floor (T x) |
---|
| 148 | { |
---|
| 149 | return (x >= 0)? int (x): -(int (-x) + (-x > int (-x))); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | template <class T> |
---|
| 154 | inline int |
---|
| 155 | ceil (T x) |
---|
| 156 | { |
---|
| 157 | return -floor (-x); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | template <class T> |
---|
| 161 | inline int |
---|
| 162 | trunc (T x) |
---|
| 163 | { |
---|
| 164 | return (x >= 0) ? int(x) : -int(-x); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | // |
---|
| 169 | // Integer division and remainder where the |
---|
| 170 | // remainder of x/y has the same sign as x: |
---|
| 171 | // |
---|
| 172 | // divs(x,y) == (abs(x) / abs(y)) * (sign(x) * sign(y)) |
---|
| 173 | // mods(x,y) == x - y * divs(x,y) |
---|
| 174 | // |
---|
| 175 | |
---|
| 176 | inline int |
---|
| 177 | divs (int x, int y) |
---|
| 178 | { |
---|
| 179 | return (x >= 0)? ((y >= 0)? ( x / y): -( x / -y)): |
---|
| 180 | ((y >= 0)? -(-x / y): (-x / -y)); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | |
---|
| 184 | inline int |
---|
| 185 | mods (int x, int y) |
---|
| 186 | { |
---|
| 187 | return (x >= 0)? ((y >= 0)? ( x % y): ( x % -y)): |
---|
| 188 | ((y >= 0)? -(-x % y): -(-x % -y)); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | // |
---|
| 193 | // Integer division and remainder where the |
---|
| 194 | // remainder of x/y is always positive: |
---|
| 195 | // |
---|
| 196 | // divp(x,y) == floor (double(x) / double (y)) |
---|
| 197 | // modp(x,y) == x - y * divp(x,y) |
---|
| 198 | // |
---|
| 199 | |
---|
| 200 | inline int |
---|
| 201 | divp (int x, int y) |
---|
| 202 | { |
---|
| 203 | return (x >= 0)? ((y >= 0)? ( x / y): -( x / -y)): |
---|
| 204 | ((y >= 0)? -((y-1-x) / y): ((-y-1-x) / -y)); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | |
---|
| 208 | inline int |
---|
| 209 | modp (int x, int y) |
---|
| 210 | { |
---|
| 211 | return x - y * divp (x, y); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | //---------------------------------------------------------- |
---|
| 215 | // Successor and predecessor for floating-point numbers: |
---|
| 216 | // |
---|
| 217 | // succf(f) returns float(f+e), where e is the smallest |
---|
| 218 | // positive number such that float(f+e) != f. |
---|
| 219 | // |
---|
| 220 | // predf(f) returns float(f-e), where e is the smallest |
---|
| 221 | // positive number such that float(f-e) != f. |
---|
| 222 | // |
---|
| 223 | // succd(d) returns double(d+e), where e is the smallest |
---|
| 224 | // positive number such that double(d+e) != d. |
---|
| 225 | // |
---|
| 226 | // predd(d) returns double(d-e), where e is the smallest |
---|
| 227 | // positive number such that double(d-e) != d. |
---|
| 228 | // |
---|
| 229 | // Exceptions: If the input value is an infinity or a nan, |
---|
| 230 | // succf(), predf(), succd(), and predd() all |
---|
| 231 | // return the input value without changing it. |
---|
| 232 | // |
---|
| 233 | //---------------------------------------------------------- |
---|
| 234 | |
---|
| 235 | float succf (float f); |
---|
| 236 | float predf (float f); |
---|
| 237 | |
---|
| 238 | double succd (double d); |
---|
| 239 | double predd (double d); |
---|
| 240 | |
---|
| 241 | // |
---|
| 242 | // Return true if the number is not a NaN or Infinity. |
---|
| 243 | // |
---|
| 244 | |
---|
| 245 | inline bool |
---|
| 246 | finitef (float f) |
---|
| 247 | { |
---|
| 248 | union {float f; int i;} u; |
---|
| 249 | u.f = f; |
---|
| 250 | |
---|
| 251 | return (u.i & 0x7f800000) != 0x7f800000; |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | inline bool |
---|
| 255 | finited (double d) |
---|
| 256 | { |
---|
| 257 | #if ULONG_MAX == 18446744073709551615LU |
---|
| 258 | typedef long unsigned int Int64; |
---|
| 259 | #else |
---|
| 260 | typedef long long unsigned int Int64; |
---|
| 261 | #endif |
---|
| 262 | |
---|
| 263 | union {double d; Int64 i;} u; |
---|
| 264 | u.d = d; |
---|
| 265 | |
---|
| 266 | return (u.i & 0x7ff0000000000000LL) != 0x7ff0000000000000LL; |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | |
---|
| 270 | } // namespace Imath |
---|
| 271 | |
---|
| 272 | #endif |
---|