ai_types.h
1 /*
2  * Arnold API header file
3  * Copyright (c) 1998-2009 Marcos Fajardo, (c) 2009-2013 Solid Angle SL
4  */
5 
11 #pragma once
12 #include "ai_api.h"
13 #include "ai_constants.h"
14 #include <cmath>
15 #include <cstdlib> // for int abs(int)
16 
29 #ifndef NULL
30 #define NULL 0
31 #endif
32 
33 
34 /****************************************************************************
35  Basic data types
36 ****************************************************************************/
37 
38 typedef char AtInt8;
39 typedef short AtInt16;
40 typedef int AtInt32;
41 typedef signed long long AtInt64;
42 typedef unsigned char AtByte;
43 typedef unsigned char AtUInt8;
44 typedef unsigned short AtUInt16;
45 typedef unsigned int AtUInt32;
46 typedef unsigned long long AtUInt64;
49 /****************************************************************************
50  Scalar operations
51 ****************************************************************************/
52 
56 template <typename T>
57 inline T ABS(T a)
58 {
59  return std::abs(a);
60 }
61 
62 #ifdef MIN
63 #undef MIN
64 #endif
65 #ifdef MAX
66 #undef MAX
67 #endif
68 
72 template <typename T>
73 inline T MIN(T a, T b)
74 {
75  return (a < b) ? a : b;
76 }
77 
81 template <typename T>
82 inline T MAX(T a, T b)
83 {
84  return (a > b) ? a : b;
85 }
86 
90 template <typename T>
91 inline T MIN3(T a, T b, T c)
92 {
93  return MIN(MIN(a, b), c);
94 }
95 
99 template <typename T>
100 inline T MAX3(T a, T b, T c)
101 {
102  return MAX(MAX(a, b), c);
103 }
104 
108 template <typename T>
109 inline T MIN4(T a, T b, T c, T d)
110 {
111  return MIN(MIN(a, b), MIN(c, d));
112 }
113 
117 template <typename T>
118 inline T MAX4(T a, T b, T c, T d)
119 {
120  return MAX(MAX(a, b), MAX(c, d));
121 }
122 
126 template <typename T>
127 inline T SQR(T a)
128 {
129  return (a * a);
130 }
131 
135 template <typename T>
136 inline AtInt64 FLOOR(T a)
137 {
138  return (AtInt64) a - (a < (T) 0);
139 }
140 
144 template <typename T>
145 inline AtInt64 CEIL(T a)
146 {
147  return (AtInt64) a + (a > (T) 0 && a != (AtInt64) a);
148 }
149 
153 template <typename T>
154 inline AtInt64 ROUND(T a)
155 {
156  return FLOOR(a + (T) 0.5);
157 }
158 
162 template <typename T>
163 inline int SGN(T a)
164 {
165  return (a < 0) ? -1 : 1;
166 }
167 
171 template <typename T>
172 inline void SWAP(T& a, T& b)
173 {
174  T temp = a;
175  a = b;
176  b = temp;
177 }
178 
182 template <typename T>
183 inline T CLAMP(T v, T lo, T hi)
184 {
185  return (v > lo) ? ((v > hi) ? hi : v) : lo;
186 }
187 
191 template <typename T>
192 inline T ACOS(T x)
193 {
194  return (x >= 1) ? 0 : std::acos(x);
195 }
196 
200 template <typename T>
201 inline T LERP(T t, T a, T b)
202 {
203  return ((1 - t) * a) + (b * t);
204 }
205 
209 template <typename T>
210 inline T HERP01(T t)
211 {
212  return t * t * ((T) 3 - (T) 2 * t);
213 }
214 
218 template <typename T>
219 inline T HERP(T t, T a, T b)
220 {
221  return LERP(HERP01(t), a, b);
222 }
223 
227 template <typename T>
228 inline T BILERP(T s, T t, T c00, T c10, T c01, T c11)
229 {
230  T c0x = LERP(t, c00, c01);
231  T c1x = LERP(t, c10, c11);
232  return LERP(s, c0x, c1x);
233 }
234 
238 template <typename T>
239 inline T BIHERP(T s, T t, T c00, T c10, T c01, T c11)
240 {
241  T c0x = HERP(t, c00, c01);
242  T c1x = HERP(t, c10, c11);
243  return HERP(s, c0x, c1x);
244 }
245 
249 template <typename T>
250 inline T BIAS(T a, T b)
251 {
252  return (a > 0) ? ((b == (T) 0.5) ? a : std::pow(a, (std::log(b) * (T)-1.442695041))) : 0;
253 }
254 
258 template <typename T>
259 inline T GAIN(T a, T g)
260 {
261  return (g == (T) 0.5) ? a : ((a < (T) 0.5) ? (BIAS(a + a, (T) 1 - g) * (T) 0.5) : ((T) 1 - BIAS((T) 2 - a - a, (T) 1 - g) * (T) 0.5));
262 }
263 
267 template <typename T>
268 inline T STEP(T x, T e)
269 {
270  return (x < e) ? (T) 0 : (T) 1;
271 }
272 
276 template <typename T>
277 inline T FILTERSTEP(T x, T e, T w)
278 {
279  return CLAMP((x + w * (T) 0.5 - e) / w, (T) 0, (T) 1);
280 }
281 
287 template <typename T>
288 inline T LINEARSTEP(T lo, T hi, T t)
289 {
290  return CLAMP((t - lo) / (hi - lo), (T) 0, (T) 1);
291 }
292 
298 template <typename T>
299 inline T SMOOTHSTEP(T e0, T e1, T t)
300 {
301  return HERP01(CLAMP((t - e0) / (e1 - e0), (T) 0, (T) 1));
302 }
303 
304 AI_API bool AiIsFinite(float x);
305 
306 AI_API float fast_exp(float x);
307 
308 /*\}*/

© 2009-2013 Solid Angle SL · all rights reserved · www.solidangle.com