LightWrap++
AC++wrapperfortheLightWave3DSDK
math.h
Go to the documentation of this file.
1 
4 #ifndef LWPP_MATH_H
5 #define LWPP_MATH_H
6 
7 #include <lwpp/debug.h>
8 
9 #ifndef PI
10  #define PI (3.14159265358979324)
11  #define PI_f (3.14159265358979324f)
12 #endif
13 #ifndef TWOPI
14  #define TWOPI (2.0 * PI)
15 #endif
16 #ifndef FOURPI
17  #define FOURPI (4.0 * PI)
18 #endif
19 
20 #ifndef HALFPI
21  #define HALFPI (0.5 * PI)
22 #endif
23 
24 #define _doublemagicroundeps (.5-1.4e-11) //almost .5f = .5f - 1e^(number of exp bit)
25 #if (defined(_WIN32) & !defined(_WIN64))
26 #define FAST_INT
27 #endif
28 
29 namespace lwpp {
30 
32  inline int Round2Int(double val)
33  {
34  #ifdef _MACOS
35  return (int) val;
36  #endif
37  #ifdef FAST_INT
38  #define _doublemagic double (6755399441055744.0) //2^52 * 1.5, uses limited precision to floor
39  val = val + _doublemagic;
40  return ((long*)&val)[0];
41  #else
42  return int (val+_doublemagicroundeps);
43  #endif
44  }
46  inline int Double2Int(double val)
47  {
48  #ifdef _MACOS
49  return (int) val;
50  #endif
51  #ifdef FAST_INT
52  return (val<0) ? Round2Int(val+_doublemagicroundeps) : Round2Int(val-_doublemagicroundeps);
53  #else
54  return (int)val;
55  #endif
56  }
57 /*
58  template<class Type>
59  Type BiLerp(double wx, double wy, Type d00, Type d10, Type d01, Type d11)
60  {
61  double iwx = 1.0 - wy;
62  double iwy = 1.0 - wy;
63 
64  return (Type) (iwx * iwy * d00 +
65  wx * iwy * d10 +
66  iwx * wy * d01 +
67  wx * wy * d11);
68  }
69 
70  template<>
71  unsigned char BiLerp<unsigned char>(double wx, double wy, unsigned char d00, unsigned char d10, unsigned char d01, unsigned char d11)
72  {
73  double iwx = 1.0 - wy;
74  double iwy = 1.0 - wy;
75 
76  return (unsigned char) (iwx * iwy * d00 +
77  wx * iwy * d10 +
78  iwx * wy * d01 +
79  wx * wy * d11);
80  }
81 */
83 
86  template<class Type>
87  Type Clamp(const Type value, const Type min, const Type max)
88  {
89  return (value < min) ? min : (value > max) ? max : value;
90  }
91 
92  template<class Type>
93  Type SmoothStep(const Type value, const Type min, const Type max)
94  {
95  if (value >= max) return 1;
96  value = (value - min)/(max-min);
97  return (value * value * (3 - 2 * value));
98  }
99 
100  template<class Type>
101  Type SmoothStep(const Type value)
102  {
103  if (value >= 1) return 1;
104  return (value * value * (3 - 2 * value));
105  }
106 
107 
108  template<class Type>
109  int Mod(const Type a, const Type b)
110  {
111  Type n = (int)(a/b);
112  int r = a - n*b;
113  if (r < 0) r += (int)b;
114  return r;
115  }
116 
118  template <class Type>
119  Type Radians(const Type deg)
120  {
121  return deg * static_cast<Type>(0.017453292519943295769236907684886);
122  }
124  template <class Type>
125  Type Degrees(const Type rad)
126  {
127  return rad * static_cast<Type>(57.2957795130823208767981548141052);
128  }
129 
131  template <class Type>
132  Type Max(Type a, Type b)
133  {
134  return (a > b ? a : b);
135  }
136 
137  template <class Type>
138  Type Max(const Type v[3])
139  {
140  return Max(v[0], Max(v[1], v[2]));
141  }
142 
144  template <class Type>
145  Type Min(const Type a, const Type b)
146  {
147  return (a < b ? a : b);
148  }
149 
150  template <class Type>
151  Type Min(const Type v[3])
152  {
153  return Min(v[0], Min(v[1], v[2]));
154  }
155 
156 
158  template <class Type>
159  Type Sqr(const Type a)
160  {
161  return a*a;
162  }
163 
164  template <class Type>
165  Type log2(const Type a)
166  {
167  return log(a) / log((Type)2);
168  }
169 
170  template <class Type>
171  Type Range(const Type x, const Type min, const Type max)
172  {
173  return (x * (max - min) + min);
174  }
175 
177  template <class Type>
178  bool inRange(const Type x, const Type min, const Type max)
179  {
180  return (x >= min) && (x <= max);
181  }
182 
183  template <class Type>
184  Type Normalize(const Type x, const Type min, const Type max)
185  {
186  return (x - min) / (max - min);
187  }
188 
189  template <class Type>
190  int Floor(const Type x) {
191  return (static_cast<int> (x)) - (x < 0 && (x != static_cast<int>(x)));
192  }
193 
194  template <class Type>
195  int Ceil(const Type x)
196  {
197  return ((int)x) + (x > 0 && (x != (int) x));
198  }
199 
200  template <class Type>
201  int Round(const double r)
202  {
203  return (r > 0.0) ? Floor(r + 0.5) : Ceil(r - 0.5);
204  }
205 
206  inline int Pow2(const int n) {
207  return 1 << n;
208  }
209 
210  template <class T> T Lerp (const double f, const T a, const T b)
211  {
212  const double g = 1.0 - f;
213  return T( a * g + b * f );
214  }
215 
216  template <class T> void Swap (T &a, T &b)
217  {
218  T temp(a);
219  a = b;
220  b = temp;
221  }
222 
227  inline double xztoh (const double x, const double z)
228  {
229  return atan2(x , z);
230  }
231 
235  inline void xyztohp (const double x, const double y, const double z, double &h, double &p)
236  {
237  h = xztoh(x, z);
238  p = atan2(y, sqrt (Sqr(x) + Sqr(z)));
239  }
240 
241  template <class T> T SignOf (const T a)
242  {
243  return (a > static_cast<T>(0)) ? static_cast<T>(1) : static_cast<T>(-1);
244  }
245 
246 } // end namespace lwpp
247 
248 #endif // LWPP_MATH_H