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