ViSP  2.10.0
vpMath.h
1 /****************************************************************************
2  *
3  * $Id: vpMath.h 4604 2014-01-21 14:15:23Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Simple mathematical function not available in the C math library (math.h).
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
41 
50 #ifndef vpMATH_HH
51 #define vpMATH_HH
52 
53 #include <visp/vpConfig.h>
54 
55 #include <math.h>
56 
57 #if defined(_WIN32) // Not defined in Microsoft math.h
58 
59 # ifndef M_PI
60 # define M_PI 3.14159265358979323846f
61 # endif
62 
63 # ifndef M_PI_2
64 # define M_PI_2 (M_PI/2.f)
65 # endif
66 
67 # ifndef M_PI_4
68 # define M_PI_4 (M_PI/4.f)
69 # endif
70 
71 #endif
72 
73 
83 class VISP_EXPORT vpMath
84 {
85  public:
86 
93  static inline double deg(double rad) { return (rad*180.0)/M_PI ; }
94 
100  static inline double rad(double deg) { return (deg*M_PI)/180.0 ; }
101 
106  static inline double sqr(double x) { return x*x ; }
107 
108  // factorial of x
109  static inline double fact(unsigned int x) ;
110 
111  // combinaison
112  static inline long double comb(unsigned int n, unsigned int p) ;
113 
114  // round x to the nearest integer
115  static inline int round(const double x) ;
116 
117  // return the sign of x (+-1)
118  static inline int sign(double x) ;
119 
120 
121  // test if a number equals 0 (with threshold value)
122  static inline bool nul(double x, double s=0.001);
123 
124  // test if two numbers are equals (with a user defined threshold)
125  static inline bool equal(double x, double y, double s=0.001);
126 
127  // test if a number is greater than another (with a user defined threshold)
128  static inline bool greater(double x, double y, double s=0.001);
129 
130 
137  template <class Type> static Type maximum(const Type& a, const Type& b)
138  {
139  return (a > b) ? a : b;
140  }
141 
148  template <class Type> static Type minimum(const Type& a, const Type& b)
149  {
150  return (a < b) ? a : b;
151  }
152 
158  template <class Type> static Type abs(const Type& x)
159  {
160  return (x < 0) ? -x : x;
161  }
162 
163 
164  // sinus cardinal
165  static inline double sinc(double x) ;
166  static inline double sinc(double sinx, double x) ;
167  static inline double mcosc(double cosx, double x) ;
168  static inline double msinc(double sinx, double x) ;
169 
170  // sigmoid
171  static inline double sigmoid(double x, double x0=0.,double x1=1., double n=12.);
172 
179  template <class Type> static void swap(Type& a, Type& b)
180  {
181  Type tmp = b;
182  b = a;
183  a = tmp;
184  }
185 
186  private:
187  static const double ang_min_sinc;
188  static const double ang_min_mc;
189 };
190 
191 
192 
193 //Begining of the inline functions definition
194 
199 double vpMath::fact(unsigned int x)
200 {
201  if ( (x == 1) || (x == 0)) return 1;
202  return x * fact(x-1);
203 }
204 
213 long double vpMath::comb(unsigned int n, unsigned int p)
214 {
215  if (n == p) return 1;
216  return fact(n)/ (fact(n-p) * fact(p));
217 }
218 
219 
228 int vpMath::round(const double x)
229 {
230  if (sign(x) > 0)
231  {
232  if ((x-(int)x) <= 0.5) return (int)x ;
233  else return (int)x+1 ;
234  }
235  else
236  {
237  if (fabs(x-(int)x) <= 0.5) return (int)x ;
238  else return (int)x-1 ;
239  }
240 }
241 
248 int vpMath::sign(double x)
249 {
250  if (fabs(x) < 1e-15) return 0 ;else
251  {
252  if (x<0) return -1 ; else return 1 ;
253  }
254 }
255 
263 bool vpMath::nul(double x, double s)
264 {
265  return(fabs(x)<s);
266 }
267 
275 bool vpMath::equal(double x, double y, double s)
276 {
277  return( nul(x-y, s) );
278 }
279 
287 bool vpMath::greater(double x, double y, double s)
288 {
289  return(x>(y-s));
290 }
291 
301 double vpMath::sinc(double x)
302 {
303  if (fabs(x) < ang_min_sinc) return 1.0 ;
304  else return sin(x)/x ;
305 }
316 double vpMath::sinc(double sinx, double x)
317 {
318  if (fabs(x) < ang_min_sinc) return 1.0 ;
319  else return (sinx/x) ;
320 }
321 
331 double vpMath::mcosc(double cosx, double x)
332 {
333  if (fabs(x) < ang_min_mc) return 0.5 ;
334  else return ((1.0-cosx)/x/x) ;
335 }
336 
347 double vpMath::msinc(double sinx, double x)
348 {
349  if (fabs(x) < ang_min_mc) return (1./6.0) ;
350  else return ((1.0-sinx/x)/x/x) ;
351 }
352 
363 double vpMath::sigmoid(double x, double x0,double x1, double n)
364 {
365  if(x < x0)
366  return 0.;
367  else if(x > x1)
368  return 1.;
369  double l0 = 1./(1.+exp(0.5*n));
370  double l1 = 1./(1.+exp(-0.5*n));
371  return (1./(1.+exp(-n*((x-x0)/(x1-x0)-0.5)))-l0)/(l1-l0);
372 }
373 
374 #endif
375 
376 /*
377  * Local variables:
378  * c-basic-offset: 2
379  * End:
380  */
static void swap(Type &a, Type &b)
Definition: vpMath.h:179
static double sigmoid(double x, double x0=0., double x1=1., double n=12.)
Definition: vpMath.h:363
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:275
Provides simple mathematics computation tools that are not available in the C mathematics library (ma...
Definition: vpMath.h:83
static double fact(unsigned int x)
Definition: vpMath.h:199
static int round(const double x)
Definition: vpMath.h:228
static double sinc(double x)
Definition: vpMath.h:301
static Type abs(const Type &x)
Definition: vpMath.h:158
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:137
static bool nul(double x, double s=0.001)
Definition: vpMath.h:263
static double mcosc(double cosx, double x)
Definition: vpMath.h:331
static double sqr(double x)
Definition: vpMath.h:106
static Type minimum(const Type &a, const Type &b)
Definition: vpMath.h:148
static double rad(double deg)
Definition: vpMath.h:100
static long double comb(unsigned int n, unsigned int p)
Definition: vpMath.h:213
static double deg(double rad)
Definition: vpMath.h:93
static bool greater(double x, double y, double s=0.001)
Definition: vpMath.h:287
static int sign(double x)
Definition: vpMath.h:248
static double msinc(double sinx, double x)
Definition: vpMath.h:347