KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
utils.h
Go to the documentation of this file.
1 // utils.h - miscellaneous utilities (mostly math)
3 // Author: Doron Tal
4 // Date Created: January, 1993
5 // Date Last Modified: Aug 15, 2000.
6 
7 #ifndef _UTILS_H
8 #define _UTILS_H
9 
10 #include <math.h>
11 
13 // CONSTANTS
15 
16 #undef CONST_MAX_FLOAT
17 #define CONST_MAX_FLOAT (float)1.0e38
18 
19 #undef CONST_MIN_FLOAT
20 #define CONST_MIN_FLOAT (float)-1.0e38
21 
22 #undef CONST_EPSILON
23 #define CONST_EPSILON (float)0.00000000001
24 
25 #undef CONST_E
26 #define CONST_E (float)2.7182818284590452354
27 
28 #undef CONST_LOG2E
29 #define CONST_LOG2E (float)1.4426950408889634074
30 
31 #undef CONST_LOG10E
32 #define CONST_LOG10E (float)0.43429448190325182765
33 
34 #undef CONST_LN2
35 #define CONST_LN2 (float)0.69314718055994530942
36 
37 #undef CONST_LN10
38 #define CONST_LN10 (float)2.30258509299404568402
39 
40 #undef CONST_PI
41 #define CONST_PI (float)3.14159265358979323846
42 
43 #undef CONST_2PI
44 #define CONST_2PI (float)6.28318530717958647692
45 
46 #undef CONST_PI_2
47 #define CONST_PI_2 (float)1.57079632679489661923
48 
49 #undef CONST_PI_4
50 #define CONST_PI_4 (float)0.78539816339744830962
51 
52 #undef CONST_3PI_4
53 #define CONST_3PI_4 (float)2.35619449019234492885
54 
55 #undef CONST_ONE_PI
56 #define CONST_ONE_PI (float)0.31830988618379067154
57 
58 #undef CONST_TWO_PI
59 #define CONST_TWO_PI (float)0.63661977236758134308
60 
61 #undef CONST_TWO_SQRTPI
62 #define CONST_TWO_SQRTPI (float)1.12837916709551257390
63 
64 #undef CONST_180_PI
65 #define CONST_180_PI (float)57.2957795130823208768
66 
67 #undef CONST_SQRT2
68 #define CONST_SQRT2 (float)1.41421356237309504880
69 
70 #undef CONST_SQRT1_2
71 #define CONST_SQRT1_2 (float)0.70710678118654752440
72 
73 #undef CONST_SQRT3OVER2
74 #define CONST_SQRT3OVER2 (float)0.86602540378443864676
75 
76 #undef CONST_3PI_2
77 #define CONST_3PI_2 (float)4.71238898038468985769
78 
79 #undef CONST_PI_8
80 #define CONST_PI_8 (float)0.39269908169872415480
81 
82 #undef CONST_3PI_8
83 #define CONST_3PI_8 (float)1.17809724509617246442
84 
85 #undef CONST_5PI_8
86 #define CONST_5PI_8 (float)1.96349540849362077403
87 
88 #undef CONST_7PI_8
89 #define CONST_7PI_8 (float)2.74889357189106908365
90 
92 // MACROS
94 
95 #undef BYTE
96 #define BYTE unsigned char
97 
98 #undef SQR
99 #define SQR(x) ((x)*(x))
100 
101 #undef IS_ODD
102 #define IS_ODD(x) ((x) % 2) /* only use these on ints */
103 
104 #undef IS_EVEN
105 #define IS_EVEN(x) (!IS_ODD(x))
106 
107 #undef ODDIFY
108 #define ODDIFY(x) if (IS_EVEN(x)) x++
109 
110 #undef F2I
111 #define F2I(x) ((x > 0.0f) ? (int)(x+0.5f) : (int)(x-0.5f))
112 
113 #undef APPXEQL
114 #define APPXEQL(x,y,e) (fabs((x)-(y)) <= e)
115 
116 #undef SGN
117 #define SGN(x) (((x) > 0) ? 1 : (((x) == 0) ? 0 : -1))
118 
119 #undef SAME_SGN
120 #define SAME_SGN(x, y) (((x) > 0) ? ((y) > 0) : (y <= 0))
121 
122 #undef MAX
123 #define MAX(a,b) ((a) > (b) ? (a) : (b))
124 
125 #undef MIN
126 #define MIN(a,b) ((a) < (b) ? (a) : (b))
127 
128 #undef ABS
129 #define ABS(x) ((x) < 0 ? -(x) : (x))
130 
131 
133 //Modified by Prasad
134 
135 //This is to check the correct usage of delete operator for arrays.
136 #undef zap
137 #define zap(x) do { if (x) { delete(x); x = 0; } } while (0)
138 //#define zap(x) if (x) { delete[] x; x = NULL; }
139 
140 #undef IS_A_FLOAT
141 #define IS_A_FLOAT(x) ((x >= CONST_MIN_FLOAT) && (x <= CONST_MAX_FLOAT))
142 
144 // MISCELLANEOUS FUNCTIONS
146 
147 namespace DTLib {
148 
150  // Returns difference between angles a1 and a2. There are always two
151  // Values for the difference of two angles. The sum of these two
152  // difference-Values is always 2pi. This returns the smaller of the
153  // two Values. Returns in range [0, PI].
154 
155  double AngleDiff(double a1, double a2);
156 
157  float AngleDiff(float a1, float a2);
158 
160  // Returns in range [-PI, PI].
161 
162  double AngleSum(double a1, double a2);
163 
164  float AngleSum(float a1, float a2);
165 
167  // Bivariate Gaussian distribution. The six parameters of the
168  // Gaussian are: mu1, mu2, mu12, sigma1, sigma2, sigma12 Given the
169  // above parameters and Values for x1, and x2, returns the probability
170  // P(x1, x2).
171 
172  float BiGauss(float mu1, float mu2, float mu12,
173  float sigma1, float sigma2, float sigma12,
174  float x1, float x2);
175 
177 
178  float FuzzySigmoid(float g, float a, float b, float c);
179 
181  // ***TODO:*** add this sigmoid, and add this one reversed..
182  // WijTX = 1.0f-
183  // 1.0f/(1.0f+exp(-20.0f*(ChiSqr-SigmaTX)));
184 
186  // Maximum element of pBuf, a float array
187 
188  float Max(float* pBuf, int Size);
189 
190  int Max(int* pBuf, int Size);
191 
193  // Minimum element of pBuf
194 
195  float Min(float* pBuf, int Size);
196 
197  // same as above, but returns the index of minimum element
198  int iMin(float* pBuf, int Size);
199 
200 
202  // log to any base
203 
204  double logbase(const double base, const double argument);
205 
207  // Next higher power of 2. Returns the first P such that 2^P >= abs(N).
208 
209  int NextPowerOfTwo(const int x);
210 
212  // routines for converting between floats and ints, for interger arithmetic
213 
214  void FloatToInt(const float& Xfloat, int& Xint,
215  const float& MinRange, const float& MaxRange,
216  const int& Resolution);
217 
218  void IntToFloat(const int& Xint, float& Xfloat,
219  const float& MinRange, const float& MaxRange,
220  const int& Resolution);
221 
223  // is an angle 'accute' or not?
224 
225  bool IsAccute(const float& Theta);
226 
228  // returns abscissa location of parabola's max, given three points
229  // to describe the parabola
230 
231  float ParabolicInverseInterpolation(const float& x0, const float& y0,
232  const float& x1, const float& y1,
233  const float& x2, const float& y2);
234 
236  // same as above, but for (orientation, amplitude) pairs, corrects
237  // orientations at the end to be in [0, pi] PRECONDITION:
238  // |Theta1-Theta2| == |Theta2-Theta3| returns the interpolated
239  // orientation in 'InterpolatedThetaResult'
240 
241  void ParabolicOrientationInterpolation(const float& Theta1,
242  const float& Rho1,
243  const float& Theta2,
244  const float& Rho2,
245  const float& Theta3,
246  const float& Rho3,
247  float& InterpolatedThetaResult);
248 
250  // given three arbitrary points (x0,y0), (x1,y1), (x2,y2) that are
251  // assumed to be points on a parabola y=ax^2+bx+c, returns the parabola
252  // coefficients in 'a', 'b' and 'c' PRECOND: x1-x0 == x2-x1
253 
254  void ParabolicInterpolation(const float& x0, const float& y0,
255  const float& x1, const float& y1,
256  const float& x2, const float& y2,
257  float& a, float& b, float& c);
258 
260  // Fixes orientation range to be [0.0, MaxTheta], where 'MaxTheta'
261  // = Pi if 'bHalfPhase' is true and 'MaxTheta' = 2Pi if
262  // 'bHalfPhase' is false.
263 
264  void FixThetaRange(float& Theta, const bool& bHalfPhase = true);
265 
266  // swapping of pointers
267  template <class T>
268  inline void Swap(T& p1, T& p2) {
269  T pTmp = p1;
270  p1 = p2;
271  p2 = pTmp;
272  }
273 
274  template <class T>
275  inline T SSD(const T& x1, const T& y1, const T& x2, const T& y2) {
276  const T dx = x1-x2;
277  const T dy = y1-y2;
278  return (SQR(dx)+SQR(dy));
279  }
280 
281 } // namespace DTLib {
282 
283 #endif /* _UTILS_H */
T SSD(const T &x1, const T &y1, const T &x2, const T &y2)
Definition: utils.h:275
float Max(float *pBuf, int Size)
Definition: utils.cpp:90
void FloatToInt(const float &Xfloat, int &Xint, const float &MinRange, const float &MaxRange, const int &Resolution)
Definition: utils.cpp:150
y2
Definition: APPgetLargeConnectedEdges.m:125
int NextPowerOfTwo(const int x)
Definition: utils.cpp:138
void FixThetaRange(float &Theta, const bool &bHalfPhase=true)
Definition: utils.cpp:261
float FuzzySigmoid(float g, float a, float b, float c)
Definition: utils.cpp:78
int iMin(float *pBuf, int Size)
Definition: utils.cpp:116
void IntToFloat(const int &Xint, float &Xfloat, const float &MinRange, const float &MaxRange, const int &Resolution)
Definition: utils.cpp:160
void ParabolicOrientationInterpolation(const float &Theta1, const float &Rho1, const float &Theta2, const float &Rho2, const float &Theta3, const float &Rho3, float &InterpolatedThetaResult)
Definition: utils.cpp:180
float BiGauss(float mu1, float mu2, float mu12, float sigma1, float sigma2, float sigma12, float x1, float x2)
Definition: utils.cpp:59
double AngleDiff(double a1, double a2)
Definition: utils.cpp:19
x1
Definition: APPgetLargeConnectedEdges.m:122
float ParabolicInverseInterpolation(const float &x0, const float &y0, const float &x1, const float &y1, const float &x2, const float &y2)
Definition: utils.cpp:222
bool IsAccute(const float &Theta)
Definition: utils.cpp:172
double AngleSum(double a1, double a2)
Definition: utils.cpp:39
x
Definition: APPgetLargeConnectedEdges.m:100
y1
Definition: APPgetLargeConnectedEdges.m:124
x2
Definition: APPgetLargeConnectedEdges.m:123
void ParabolicInterpolation(const float &x0, const float &y0, const float &x1, const float &y1, const float &x2, const float &y2, float &a, float &b, float &c)
Definition: utils.cpp:245
void Swap(T &p1, T &p2)
Definition: utils.h:268
double logbase(const double base, const double argument)
Definition: utils.cpp:130
#define SQR(x)
Definition: utils.h:99
float Min(float *pBuf, int Size)
Definition: utils.cpp:107