Visual Servoing Platform  version 3.0.0
vpMath.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Simple mathematical function not available in the C math library (math.h).
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
46 #ifndef vpMATH_HH
47 #define vpMATH_HH
48 
49 #include <visp3/core/vpConfig.h>
50 
51 #include <math.h>
52 #include <limits>
53 #include <climits>
54 #include <algorithm>
55 #include <vector>
56 
57 #if defined(VISP_HAVE_FUNC_ISNAN) || defined(VISP_HAVE_FUNC_STD_ISNAN) || defined(VISP_HAVE_FUNC_ISINF) || defined(VISP_HAVE_FUNC_STD_ISINF) || defined(VISP_HAVE_FUNC_STD_ROUND)
58 # include <cmath>
59 #endif
60 
61 #if defined(_WIN32) // Not defined in Microsoft math.h
62 
63 # ifndef M_PI
64 # define M_PI 3.14159265358979323846
65 # endif
66 
67 # ifndef M_PI_2
68 # define M_PI_2 (M_PI/2.0)
69 # endif
70 
71 # ifndef M_PI_4
72 # define M_PI_4 (M_PI/4.0)
73 # endif
74 
75 #endif
76 
77 
87 class VISP_EXPORT vpMath
88 {
89  public:
90 
97  static inline double deg(double rad) { return (rad*180.0)/M_PI ; }
98 
104  static inline double rad(double deg) { return (deg*M_PI)/180.0 ; }
105 
110  static inline double sqr(double x) { return x*x ; }
111 
112  // factorial of x
113  static inline double fact(unsigned int x) ;
114 
115  // combinaison
116  static inline long double comb(unsigned int n, unsigned int p) ;
117 
118  // round x to the nearest integer
119  static inline int round(const double x) ;
120 
121  // return the sign of x (+-1)
122  static inline int sign(double x) ;
123 
124 
125  // test if a number equals 0 (with threshold value)
126  static inline bool nul(double x, double s=0.001);
127 
128  // test if two numbers are equals (with a user defined threshold)
129  static inline bool equal(double x, double y, double s=0.001);
130 
131  // test if a number is greater than another (with a user defined threshold)
132  static inline bool greater(double x, double y, double s=0.001);
133 
134 
141  template <class Type> static Type maximum(const Type& a, const Type& b)
142  {
143  return (a > b) ? a : b;
144  }
145 
152  template <class Type> static Type minimum(const Type& a, const Type& b)
153  {
154  return (a < b) ? a : b;
155  }
156 
162  template <class Type> static Type abs(const Type& x)
163  {
164  return (x < 0) ? -x : x;
165  }
166 
167 
168  // sinus cardinal
169  static double sinc(double x) ;
170  static double sinc(double sinx, double x) ;
171  static double mcosc(double cosx, double x) ;
172  static double msinc(double sinx, double x) ;
173 
174  // sigmoid
175  static inline double sigmoid(double x, double x0=0.,double x1=1., double n=12.);
176 
183  template <class Type> static void swap(Type& a, Type& b)
184  {
185  Type tmp = b;
186  b = a;
187  a = tmp;
188  }
189 
190  static bool isNaN(const double value);
191  static bool isInf(const double value);
192 
193  template<typename _Tp> static inline _Tp saturate(unsigned char v) { return _Tp(v); }
194  template<typename _Tp> static inline _Tp saturate(char v) { return _Tp(v); }
195  template<typename _Tp> static inline _Tp saturate(unsigned short v) { return _Tp(v); }
196  template<typename _Tp> static inline _Tp saturate(short v) { return _Tp(v); }
197  template<typename _Tp> static inline _Tp saturate(unsigned v) { return _Tp(v); }
198  template<typename _Tp> static inline _Tp saturate(int v) { return _Tp(v); }
199  template<typename _Tp> static inline _Tp saturate(float v) { return _Tp(v); }
200  template<typename _Tp> static inline _Tp saturate(double v) { return _Tp(v); }
201 
202  static double getMean(const std::vector<double> &v);
203  static double getMedian(const std::vector<double> &v);
204  static double getStdev(const std::vector<double> &v, const bool useBesselCorrection=false);
205 
206  private:
207  static const double ang_min_sinc;
208  static const double ang_min_mc;
209 };
210 
211 
212 
213 //Begining of the inline functions definition
214 
219 double vpMath::fact(unsigned int x)
220 {
221  if ( (x == 1) || (x == 0)) return 1;
222  return x * fact(x-1);
223 }
224 
233 long double vpMath::comb(unsigned int n, unsigned int p)
234 {
235  if (n == p) return 1;
236  return fact(n)/ (fact(n-p) * fact(p));
237 }
238 
239 
248 int vpMath::round(const double x)
249 {
250 #if defined(VISP_HAVE_FUNC_ROUND)
251  //:: to design the global namespace and avoid to call recursively vpMath::round
252  return (int)::round(x);
253 #elif defined(VISP_HAVE_FUNC_STD_ROUND)
254  return (int)std::round(x)
255 #else
256  return (x > 0.0) ? ((int) floor(x + 0.5)) : ((int) ceil(x - 0.5));
257 #endif
258 }
259 
266 int vpMath::sign(double x)
267 {
268  if (fabs(x) < std::numeric_limits<double>::epsilon())
269  return 0;
270  else {
271  if (x < 0)
272  return -1;
273  else
274  return 1;
275  }
276 }
277 
285 bool vpMath::nul(double x, double s)
286 {
287  return(fabs(x)<s);
288 }
289 
297 bool vpMath::equal(double x, double y, double s)
298 {
299  return( nul(x-y, s) );
300 }
301 
309 bool vpMath::greater(double x, double y, double s)
310 {
311  return(x>(y-s));
312 }
313 
324 double vpMath::sigmoid(double x, double x0,double x1, double n)
325 {
326  if(x < x0)
327  return 0.;
328  else if(x > x1)
329  return 1.;
330  double l0 = 1./(1.+exp(0.5*n));
331  double l1 = 1./(1.+exp(-0.5*n));
332  return (1./(1.+exp(-n*((x-x0)/(x1-x0)-0.5)))-l0)/(l1-l0);
333 }
334 
335 //unsigned char
336 template<> inline unsigned char vpMath::saturate<unsigned char>(char v) {
337  return (unsigned char) (std::max)((int) v, 0);
338 }
339 
340 template<> inline unsigned char vpMath::saturate<unsigned char>(unsigned short v) {
341  return (unsigned char) (std::min)((unsigned int) v, (unsigned int) UCHAR_MAX);
342 }
343 
344 template<> inline unsigned char vpMath::saturate<unsigned char>(int v) {
345  return (unsigned char) ((unsigned int) v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0);
346 }
347 
348 template<> inline unsigned char vpMath::saturate<unsigned char>(short v) {
349  return saturate<unsigned char> ((int) v);
350 }
351 
352 template<> inline unsigned char vpMath::saturate<unsigned char>(unsigned int v) {
353  return (unsigned char) (std::min)(v, (unsigned int) UCHAR_MAX);
354 }
355 
356 template<> inline unsigned char vpMath::saturate<unsigned char>(float v) {
357  int iv = vpMath::round(v);
358  return saturate<unsigned char> (iv);
359 }
360 
361 template<> inline unsigned char vpMath::saturate<unsigned char>(double v) {
362  int iv = vpMath::round(v);
363  return saturate<unsigned char> (iv);
364 }
365 
366 //char
367 template<> inline char vpMath::saturate<char>(unsigned char v) {
368  return (char) (std::min)((int) v, SCHAR_MAX);
369 }
370 
371 template<> inline char vpMath::saturate<char>(unsigned short v) {
372  return (char) (std::min)((unsigned int) v, (unsigned int) SCHAR_MAX);
373 }
374 
375 template<> inline char vpMath::saturate<char>(int v) {
376  return (char) ((unsigned int) (v - SCHAR_MIN) <= (unsigned int) UCHAR_MAX ? v :
377  v > 0 ? SCHAR_MAX : SCHAR_MIN);
378 }
379 
380 template<> inline char vpMath::saturate<char>(short v) {
381  return saturate<char>((int) v);
382 }
383 
384 template<> inline char vpMath::saturate<char>(unsigned int v) {
385  return (char) (std::min)(v, (unsigned int) SCHAR_MAX);
386 }
387 
388 template<> inline char vpMath::saturate<char>(float v) {
389  int iv = vpMath::round(v);
390  return saturate<char>(iv);
391 }
392 
393 template<> inline char vpMath::saturate<char>(double v) {
394  int iv = vpMath::round(v);
395  return saturate<char>(iv);
396 }
397 
398 //unsigned short
399 template<> inline unsigned short vpMath::saturate<unsigned short>(char v) {
400  return (unsigned short) (std::max)((int) v, 0);
401 }
402 
403 template<> inline unsigned short vpMath::saturate<unsigned short>(short v) {
404  return (unsigned short) (std::max)((int) v, 0);
405 }
406 
407 template<> inline unsigned short vpMath::saturate<unsigned short>(int v) {
408  return (unsigned short) ((unsigned int) v <= (unsigned int) USHRT_MAX ? v :
409  v > 0 ? USHRT_MAX : 0);
410 }
411 
412 template<> inline unsigned short vpMath::saturate<unsigned short>(unsigned int v) {
413  return (unsigned short) (std::min)(v, (unsigned int) USHRT_MAX);
414 }
415 
416 template<> inline unsigned short vpMath::saturate<unsigned short>(float v) {
417  int iv = vpMath::round(v);
418  return vpMath::saturate<unsigned short>(iv);
419 }
420 
421 template<> inline unsigned short vpMath::saturate<unsigned short>(double v) {
422  int iv = vpMath::round(v);
423  return vpMath::saturate<unsigned short>(iv);
424 }
425 
426 //short
427 template<> inline short vpMath::saturate<short>(unsigned short v) {
428  return (short) (std::min)((int) v, SHRT_MAX);
429 }
430 template<> inline short vpMath::saturate<short>(int v) {
431  return (short) ((unsigned int) (v - SHRT_MIN) <= (unsigned int) USHRT_MAX ? v :
432  v > 0 ? SHRT_MAX : SHRT_MIN);
433 }
434 template<> inline short vpMath::saturate<short>(unsigned int v) {
435  return (short) (std::min)(v, (unsigned int) SHRT_MAX);
436 }
437 template<> inline short vpMath::saturate<short>(float v) {
438  int iv = vpMath::round(v);
439  return vpMath::saturate<short>(iv);
440 }
441 template<> inline short vpMath::saturate<short>(double v) {
442  int iv = vpMath::round(v);
443  return vpMath::saturate<short>(iv);
444 }
445 
446 //int
447 template<> inline int vpMath::saturate<int>(float v) {
448  return vpMath::round(v);
449 }
450 
451 template<> inline int vpMath::saturate<int>(double v) {
452  return vpMath::round(v);
453 }
454 
455 //unsigned int
456 // (Comment from OpenCV) we intentionally do not clip negative numbers, to make -1 become 0xffffffff etc.
457 template<> inline unsigned int vpMath::saturate<unsigned int>(float v) {
458  return (unsigned int) vpMath::round(v);
459 }
460 
461 template<> inline unsigned int vpMath::saturate<unsigned int>(double v) {
462  return (unsigned int) vpMath::round(v);
463 }
464 
465 
466 #endif
467 
468 /*
469  * Local variables:
470  * c-basic-offset: 2
471  * End:
472  */
static _Tp saturate(double v)
Definition: vpMath.h:200
static void swap(Type &a, Type &b)
Definition: vpMath.h:183
static _Tp saturate(char v)
Definition: vpMath.h:194
static double sigmoid(double x, double x0=0., double x1=1., double n=12.)
Definition: vpMath.h:324
static _Tp saturate(unsigned char v)
Definition: vpMath.h:193
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:297
Provides simple mathematics computation tools that are not available in the C mathematics library (ma...
Definition: vpMath.h:87
static _Tp saturate(unsigned short v)
Definition: vpMath.h:195
static double fact(unsigned int x)
Definition: vpMath.h:219
static int round(const double x)
Definition: vpMath.h:248
static Type abs(const Type &x)
Definition: vpMath.h:162
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:141
static _Tp saturate(short v)
Definition: vpMath.h:196
static bool nul(double x, double s=0.001)
Definition: vpMath.h:285
static double sqr(double x)
Definition: vpMath.h:110
static Type minimum(const Type &a, const Type &b)
Definition: vpMath.h:152
static _Tp saturate(int v)
Definition: vpMath.h:198
static double rad(double deg)
Definition: vpMath.h:104
static _Tp saturate(unsigned v)
Definition: vpMath.h:197
static _Tp saturate(float v)
Definition: vpMath.h:199
static long double comb(unsigned int n, unsigned int p)
Definition: vpMath.h:233
static double deg(double rad)
Definition: vpMath.h:97
static bool greater(double x, double y, double s=0.001)
Definition: vpMath.h:309
static int sign(double x)
Definition: vpMath.h:266