Structural deformable models
mathutil.h
Go to the documentation of this file.
1 #ifndef _MATHUTIL_H_
2 #define _MATHUTIL_H_
3 
4 #include <math.h>
5 #include "simpletypes.h"
6 #include <stdlib.h>
7 
8 #ifndef M_PI
9 #define M_PI 3.14159265359
10 #endif
11 #ifndef M_1_PI
12 #define M_1_PI (1/M_PI)
13 #endif
14 #ifndef M_E
15 #define M_E 2.718281828
16 #endif
17 #ifndef M_SQRT2PI
18 #define M_SQRT2PI 2.5066283
19 #endif
20 
21 inline int absint(int v) { return v<0 ? -v : v;}
22 
23 template <class T>
24 bool clamp(T& x, T& y, const T x0, const T y0, const T x1, const T y1)
25 {
26  bool ret=false;
27  if(x<x0) { ret = true; x = x0;}
28  if(x>x1) { ret = true; x = x1;}
29  if(y<y0) { ret = true; y = y0;}
30  if(y>y1) { ret = true; y = y1;}
31  return ret;
32 }
33 
34 /*
35  template <class T>
36  T min(const T a, const T b)
37  {
38  return a<b ? a : b;
39  }
40 
41  template <class T>
42  T max(const T a, const T b)
43  {
44  return a>b ? a : b;
45  }
46 */
47 
48 #define frand(max) (((float)rand())*(max)/RAND_MAX)
49 #define FRAND1 (((float)rand())/RAND_MAX)
50 
51 /* Polar (Box-Mueller) method; See Knuth v2, 3rd ed, p122 */
52 /* from GSL/gauss.c */
53 inline float fgauss01()
54 {
55  float x, y, r2;
56  do
57  {
58  /* choose x,y in uniform square (-1,-1) to (+1,+1) */
59  x = -1 + frand(2);
60  y = -1 + frand(2);
61  /* see if it is in the unit circle */
62  r2 = x * x + y * y;
63  } while (r2 > 1.0 || r2 == 0);
64  /* Box-Mueller transform */
65  return y * sqrt (-2.0 * log (r2) / r2);
66 }
67 
68 inline float gauss(float x, float stdev) {
69  return exp(-x*x/(2*stdev*stdev)) / (stdev*M_SQRT2PI);
70 }
71 inline float gauss2(float x2, float stdev) {
72  return exp(-x2/(2*stdev*stdev)) / (stdev*M_SQRT2PI);
73 }
74 inline double gaussd(float x, double stdev) {
75  return exp(-x*x/(2*stdev*stdev)) / (stdev*M_SQRT2PI);
76 }
77 inline float gaussd2(float x2, double stdev) {
78  return exp(-x2/(2*stdev*stdev)) / (stdev*M_SQRT2PI);
79 }
80 
81 inline float mapAngle360(float a) {
82  int d=(int)a/360 - (int)(a<0);
83  return a-360*(float)d;
84 }
85 
86 inline float mapAngle360(float a, float mina) {
87  float ashift = a-mina;
88  int d=(int)ashift/360 - (int)(ashift<0);
89  return a-360*(float)d;
90 }
91 
92 inline float mapAngle180(float a) {
93  a = mapAngle360(a);
94  return a<=180 ? a : a-360;
95 }
96 
97 inline float mapAngle2PI(float a) {
98  float turns=floor(a*(0.5*M_1_PI));
99  return a-(2*M_PI)*turns;
100 }
101 
102 inline float mapAngle2PI(float a, float mina) {
103  float turns=floor((a-mina)*(0.5*M_1_PI));
104  return a-(2*M_PI)*turns;
105 }
106 
107 inline float mapAnglePI(float a) {
108  a = mapAngle2PI(a);
109  return (a>M_PI) ? a-(2*M_PI) : a;
110 }
111 
112 inline float mapAnglePI(float a, float cnta) {
113  a = mapAngle2PI(a, cnta);
114  return (a-cnta>M_PI) ? a-(2*M_PI) : a;
115 }
116 
117 inline dword power2(dword x)
118 {
119  return 1<< int(ceil(log(float(x))/log(2.f)));
120 }
121 
122 #endif
float gaussd2(float x2, double stdev)
Definition: mathutil.h:77
#define frand(max)
Definition: mathutil.h:48
bool clamp(T &x, T &y, const T x0, const T y0, const T x1, const T y1)
Definition: mathutil.h:24
float gauss2(float x2, float stdev)
Definition: mathutil.h:71
#define M_1_PI
Definition: mathutil.h:12
float mapAngle2PI(float a)
Definition: mathutil.h:97
float mapAngle180(float a)
Definition: mathutil.h:92
float mapAngle360(float a)
Definition: mathutil.h:81
double gaussd(float x, double stdev)
Definition: mathutil.h:74
#define M_PI
Definition: mathutil.h:9
int absint(int v)
Definition: mathutil.h:21
unsigned long dword
Definition: simpletypes.h:6
dword power2(dword x)
Definition: mathutil.h:117
float fgauss01()
Definition: mathutil.h:53
float gauss(float x, float stdev)
Definition: mathutil.h:68
#define M_SQRT2PI
Definition: mathutil.h:18
DMatrix< T > stdev(const DMatrix< T > &mat)
Definition: DMatrixUtil.h:106
float mapAnglePI(float a)
Definition: mathutil.h:107
DMatrix< T > & sqrt(DMatrix< T > &mat)
Definition: DMatrixUtil.h:81