Steeriously  0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
Utilities.hpp
1 #ifndef UTILITIES_HPP
2 #define UTILITIES_HPP
3 
4 #include <math.h>
5 #include <sstream>
6 #include <string>
7 #include <vector>
8 #include <limits>
9 #include <cassert>
10 #include <iomanip>
11 
12 namespace steer //namespace steeriously
13 {
14 
16 typedef signed char Int8;
17 typedef unsigned char Uint8;
18 typedef signed short Int16;
19 typedef unsigned short Uint16;
20 typedef signed int Int32;
21 typedef unsigned int Uint32;
22 typedef signed long long Int64;
23 typedef unsigned long long Uint64;
24 
26 const int MaxInt = (std::numeric_limits<int>::max)();
27 const double MaxDouble = (std::numeric_limits<double>::max)();
28 const double MinDouble = (std::numeric_limits<double>::min)();
29 const float MaxFloat = (std::numeric_limits<float>::max)();
30 const float MinFloat = (std::numeric_limits<float>::min)();
31 
33 const float Pi = 3.14159f;
34 const float TwoPi = Pi * 2;
35 const float HalfPi = Pi / 2;
36 const float QuarterPi = Pi / 4;
37 
44 template <typename T>
45 inline bool isNaN(T val)
46 {
47  return val != val;
48 }
49 
55 inline float DegreesToRadians(float degrees)
56 {
57  return TwoPi * (degrees/360.f);
58 }
59 
65 inline bool IsZero(float val)
66 {
67  return ( (-MinFloat < val) && (val < MinFloat) );
68 }
69 
77 inline bool InRange(float start, float end, float val)
78 {
79  if (start < end)
80  {
81  if ( (val > start) && (val < end) ) return true;
82  else return false;
83  }
84 
85  else
86  {
87  if ( (val < start) && (val > end) ) return true;
88  else return false;
89  }
90 }
91 
99 template <class T>
100 T Maximum(const T& v1, const T& v2)
101 {
102  return v1 > v2 ? v1 : v2;
103 }
104 
105 //----------------------------------------------------------------------------
106 // some random number functions.
107 //----------------------------------------------------------------------------
108 
115 inline int RandInt(int x,int y) {return rand()%(y-x+1)+x;}
116 
121 inline float RandFloat(){return ((rand())/(RAND_MAX+1.0));}
122 
129 inline float RandInRange(float x, float y)
130 {
131  return x + RandFloat()*(y-x);
132 }
133 
138 inline bool RandBool()
139 {
140  if (RandInt(0,1)) return true;
141 
142  else return false;
143 }
144 
149 inline float RandomClamped(){return RandFloat() - RandFloat();}
150 
157 inline float RandGaussian(float mean = 0.0, float standard_deviation = 1.0)
158 {
159  float x1, x2, w, y1;
160  static float y2;
161  static int use_last = 0;
162 
163  if (use_last) /* use value from previous call */
164  {
165  y1 = y2;
166  use_last = 0;
167  }
168  else
169  {
170  do
171  {
172  x1 = 2.0 * RandFloat() - 1.0;
173  x2 = 2.0 * RandFloat() - 1.0;
174  w = x1 * x1 + x2 * x2;
175  }
176  while ( w >= 1.0 );
177 
178  w = sqrt( (-2.0 * log( w ) ) / w );
179  y1 = x1 * w;
180  y2 = x2 * w;
181  use_last = 1;
182  }
183 
184  return( mean + y1 * standard_deviation );
185 }
186 
187 //-----------------------------------------------------------------------
188 //
189 // some handy little functions
190 //-----------------------------------------------------------------------
191 
198 inline float Sigmoid(float input, float response = 1.0)
199 {
200  return ( 1.0 / ( 1.0 + exp(-input / response)));
201 }
202 
210 template <class T>
211 inline T MaxOf(const T& a, const T& b)
212 {
213  if (a>b) return a; return b;
214 }
215 
223 template <class T>
224 inline T MinOf(const T& a, const T& b)
225 {
226  if (a<b) return a; return b;
227 }
228 
237 template <class T, class U, class V>
238 inline void Clamp(T& arg, const U& minVal, const V& maxVal)
239 {
240  assert ( (minVal < maxVal) && "<Clamp>MaxVal < MinVal!");
241 
242  if (arg < (T)minVal)
243  {
244  arg = (T)minVal;
245  }
246 
247  if (arg > (T)maxVal)
248  {
249  arg = (T)maxVal;
250  }
251 }
252 
258 inline int Rounded(float val)
259 {
260  int integral = (int)val;
261  float mantissa = val - integral;
262 
263  if (mantissa < 0.5)
264  {
265  return integral;
266  }
267 
268  else
269  {
270  return integral + 1;
271  }
272 }
273 
280 inline int RoundUnderOffset(float val, float offset)
281 {
282  int integral = (int)val;
283  float mantissa = val - integral;
284 
285  if (mantissa < offset)
286  {
287  return integral;
288  }
289 
290  else
291  {
292  return integral + 1;
293  }
294 }
295 
302 inline bool isEqual(float a, float b)
303 {
304  if (fabs(a-b) < 1E-12)
305  {
306  return true;
307  }
308 
309  return false;
310 }
311 
318 inline bool isEqual(double a, double b)
319 {
320  if (fabs(a-b) < 1E-12)
321  {
322  return true;
323  }
324 
325  return false;
326 }
327 
334 template <class T>
335 inline float Average(const std::vector<T>& v)
336 {
337  float average = 0.0;
338 
339  for (unsigned int i=0; i < v.size(); ++i)
340  {
341  average += (float)v[i];
342  }
343 
344  return average / (float)v.size();
345 }
346 
352 inline float StandardDeviation(const std::vector<float>& v)
353 {
354  float sd = 0.0;
355  float average = Average(v);
356 
357  for (unsigned int i=0; i<v.size(); ++i)
358  {
359  sd += (v[i] - average) * (v[i] - average);
360  }
361 
362  sd = sd / v.size();
363 
364  return sqrt(sd);
365 }
366 
373 template <class container>
374 inline void DeleteSTLContainer(container& c)
375 {
376  for (typename container::iterator it = c.begin(); it!=c.end(); ++it)
377  {
378  delete *it;
379  *it = NULL;
380  }
381 }
382 
389 template <class map>
390 inline void DeleteSTLMap(map& m)
391 {
392  for (typename map::iterator it = m.begin(); it!=m.end(); ++it)
393  {
394  delete it->second;
395  it->second = NULL;
396  }
397 }
398 
399 } //namespace steeriously
400 
401 #endif //UTILITIES_HPP
402