Visual Servoing Platform  version 3.2.1 under development (2019-07-16)
vpMath.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Simple mathematical function not available in the C math library (math.h).
33  *
34  * Authors:
35  * Eric Marchand
36  *
37  *****************************************************************************/
38 
45 #ifndef vpMATH_HH
46 #define vpMATH_HH
47 
48 #include <visp3/core/vpConfig.h>
49 
50 #include <algorithm>
51 #include <climits>
52 #include <limits>
53 #if defined(_WIN32)
54 // Define _USE_MATH_DEFINES before including <math.h> to expose these macro
55 // definitions for common math constants. These are placed under an #ifdef
56 // since these commonly-defined names are not part of the C or C++ standards
57 # define _USE_MATH_DEFINES
58 #endif
59 #include <math.h>
60 #include <vector>
61 
62 #if defined(VISP_HAVE_FUNC_ISNAN) || defined(VISP_HAVE_FUNC_STD_ISNAN) || defined(VISP_HAVE_FUNC_ISINF) || \
63  defined(VISP_HAVE_FUNC_STD_ISINF) || defined(VISP_HAVE_FUNC_STD_ROUND)
64 #include <cmath>
65 #endif
66 
67 #if defined(_WIN32) // Not defined in Microsoft math.h
68 
69 #ifndef M_PI
70 #define M_PI 3.14159265358979323846
71 #endif
72 
73 #ifndef M_PI_2
74 #define M_PI_2 (M_PI / 2.0)
75 #endif
76 
77 #ifndef M_PI_4
78 #define M_PI_4 (M_PI / 4.0)
79 #endif
80 
81 #endif
82 
92 class VISP_EXPORT vpMath
93 {
94 public:
101  static inline double deg(double rad) { return (rad * 180.0) / M_PI; }
102 
108  static inline double rad(double deg) { return (deg * M_PI) / 180.0; }
109 
114  static inline double sqr(double x) { return x * x; }
115 
116  // factorial of x
117  static inline double fact(unsigned int x);
118 
119  // combinaison
120  static inline long double comb(unsigned int n, unsigned int p);
121 
122  // round x to the nearest integer
123  static inline int round(const double x);
124 
125  // return the sign of x (+-1)
126  static inline int(sign)(double x);
127 
128  // test if a number equals 0 (with threshold value)
129  static inline bool nul(double x, double s = 0.001);
130 
131  // test if two numbers are equals (with a user defined threshold)
132  static inline bool equal(double x, double y, double s = 0.001);
133 
134  // test if a number is greater than another (with a user defined threshold)
135  static inline bool greater(double x, double y, double s = 0.001);
136 
143  template <class Type> static Type maximum(const Type &a, const Type &b) { return (a > b) ? a : b; }
144 
151  template <class Type> static Type minimum(const Type &a, const Type &b) { return (a < b) ? a : b; }
152 
158  template <class Type> static Type abs(const Type &x) { return (x < 0) ? -x : x; }
159 
160  // sinus cardinal
161  static double sinc(double x);
162  static double sinc(double sinx, double x);
163  static double mcosc(double cosx, double x);
164  static double msinc(double sinx, double x);
165 
166  // sigmoid
167  static inline double sigmoid(double x, double x0 = 0., double x1 = 1., double n = 12.);
168 
175  template <class Type> static void swap(Type &a, Type &b)
176  {
177  Type tmp = b;
178  b = a;
179  a = tmp;
180  }
181 
182  static bool isNaN(const double value);
183  static bool isInf(const double value);
184 
185  template <typename _Tp> static inline _Tp saturate(unsigned char v) { return _Tp(v); }
186  template <typename _Tp> static inline _Tp saturate(char v) { return _Tp(v); }
187  template <typename _Tp> static inline _Tp saturate(unsigned short v) { return _Tp(v); }
188  template <typename _Tp> static inline _Tp saturate(short v) { return _Tp(v); }
189  template <typename _Tp> static inline _Tp saturate(unsigned v) { return _Tp(v); }
190  template <typename _Tp> static inline _Tp saturate(int v) { return _Tp(v); }
191  template <typename _Tp> static inline _Tp saturate(float v) { return _Tp(v); }
192  template <typename _Tp> static inline _Tp saturate(double v) { return _Tp(v); }
193 
194  static double getMean(const std::vector<double> &v);
195  static double getMedian(const std::vector<double> &v);
196  static double getStdev(const std::vector<double> &v, const bool useBesselCorrection = false);
197 
198  static int modulo(const int a, const int n);
199 
200 private:
201  static const double ang_min_sinc;
202  static const double ang_min_mc;
203 };
204 
205 // Begining of the inline functions definition
206 
211 double vpMath::fact(unsigned int x)
212 {
213  if ((x == 1) || (x == 0))
214  return 1;
215  return x * fact(x - 1);
216 }
217 
226 long double vpMath::comb(unsigned int n, unsigned int p)
227 {
228  if (n == p)
229  return 1;
230  return fact(n) / (fact(n - p) * fact(p));
231 }
232 
241 int vpMath::round(const double x)
242 {
243 #if defined(VISP_HAVE_FUNC_ROUND)
244  //:: to design the global namespace and avoid to call recursively
245  // vpMath::round
246  return (int)::round(x);
247 #elif defined(VISP_HAVE_FUNC_STD_ROUND)
248  return (int)std::round(x);
249 #else
250  return (x > 0.0) ? ((int)floor(x + 0.5)) : ((int)ceil(x - 0.5));
251 #endif
252 }
253 
260 int
261 #ifndef DOXYGEN_SHOULD_SKIP_THIS
262  (
263 #endif
265 #ifndef DOXYGEN_SHOULD_SKIP_THIS
266  )
267 #endif
268  (double x)
269 {
270  if (fabs(x) < std::numeric_limits<double>::epsilon())
271  return 0;
272  else {
273  if (x < 0)
274  return -1;
275  else
276  return 1;
277  }
278 }
279 
287 bool vpMath::nul(double x, double s) { return (fabs(x) < s); }
288 
296 bool vpMath::equal(double x, double y, double s) { return (nul(x - y, s)); }
297 
305 bool vpMath::greater(double x, double y, double s) { return (x > (y - s)); }
306 
316 double vpMath::sigmoid(double x, double x0, double x1, double n)
317 {
318  if (x < x0)
319  return 0.;
320  else if (x > x1)
321  return 1.;
322  double l0 = 1. / (1. + exp(0.5 * n));
323  double l1 = 1. / (1. + exp(-0.5 * n));
324  return (1. / (1. + exp(-n * ((x - x0) / (x1 - x0) - 0.5))) - l0) / (l1 - l0);
325 }
326 
327 // unsigned char
328 template <> inline unsigned char vpMath::saturate<unsigned char>(char v)
329 {
330  // On big endian arch like powerpc, char implementation is unsigned
331  // with CHAR_MIN=0, CHAR_MAX=255 and SCHAR_MIN=-128, SCHAR_MAX=127
332  // leading to (int)(char -127) = 129.
333  // On little endian arch, CHAR_MIN=-127 and CHAR_MAX=128 leading to
334  // (int)(char -127) = -127.
335  if (std::numeric_limits<char>::is_signed)
336  return (unsigned char)(((std::max))((int)v, 0));
337  else
338  return (unsigned char)((unsigned int)v > SCHAR_MAX ? 0 : v);
339 }
340 
341 template <> inline unsigned char vpMath::saturate<unsigned char>(unsigned short v)
342 {
343  return (unsigned char)((std::min))((unsigned int)v, (unsigned int)UCHAR_MAX);
344 }
345 
346 template <> inline unsigned char vpMath::saturate<unsigned char>(int v)
347 {
348  return (unsigned char)((unsigned int)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0);
349 }
350 
351 template <> inline unsigned char vpMath::saturate<unsigned char>(short v) { return saturate<unsigned char>((int)v); }
352 
353 template <> inline unsigned char vpMath::saturate<unsigned char>(unsigned int v)
354 {
355  return (unsigned char)((std::min))(v, (unsigned int)UCHAR_MAX);
356 }
357 
358 template <> inline unsigned char vpMath::saturate<unsigned char>(float v)
359 {
360  int iv = vpMath::round(v);
361  return saturate<unsigned char>(iv);
362 }
363 
364 template <> inline unsigned char vpMath::saturate<unsigned char>(double v)
365 {
366  int iv = vpMath::round(v);
367  return saturate<unsigned char>(iv);
368 }
369 
370 // char
371 template <> inline char vpMath::saturate<char>(unsigned char v) { return (char)((std::min))((int)v, SCHAR_MAX); }
372 
373 template <> inline char vpMath::saturate<char>(unsigned short v)
374 {
375  return (char)((std::min))((unsigned int)v, (unsigned int)SCHAR_MAX);
376 }
377 
378 template <> inline char vpMath::saturate<char>(int v)
379 {
380  return (char)((unsigned int)(v - SCHAR_MIN) <= (unsigned int)UCHAR_MAX ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN);
381 }
382 
383 template <> inline char vpMath::saturate<char>(short v) { return saturate<char>((int)v); }
384 
385 template <> inline char vpMath::saturate<char>(unsigned int v)
386 {
387  return (char)((std::min))(v, (unsigned int)SCHAR_MAX);
388 }
389 
390 template <> inline char vpMath::saturate<char>(float v)
391 {
392  int iv = vpMath::round(v);
393  return saturate<char>(iv);
394 }
395 
396 template <> inline char vpMath::saturate<char>(double v)
397 {
398  int iv = vpMath::round(v);
399  return saturate<char>(iv);
400 }
401 
402 // unsigned short
403 template <> inline unsigned short vpMath::saturate<unsigned short>(char v)
404 {
405  // On big endian arch like powerpc, char implementation is unsigned
406  // with CHAR_MIN=0, CHAR_MAX=255 and SCHAR_MIN=-128, SCHAR_MAX=127
407  // leading to (int)(char -127) = 129.
408  // On little endian arch, CHAR_MIN=-127 and CHAR_MAX=128 leading to
409  // (int)(char -127) = -127.
410  if (std::numeric_limits<char>::is_signed)
411  return (unsigned char)(((std::max))((int)v, 0));
412  else
413  return (unsigned char)((unsigned int)v > SCHAR_MAX ? 0 : v);
414 }
415 
416 template <> inline unsigned short vpMath::saturate<unsigned short>(short v)
417 {
418  return (unsigned short)((std::max))((int)v, 0);
419 }
420 
421 template <> inline unsigned short vpMath::saturate<unsigned short>(int v)
422 {
423  return (unsigned short)((unsigned int)v <= (unsigned int)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0);
424 }
425 
426 template <> inline unsigned short vpMath::saturate<unsigned short>(unsigned int v)
427 {
428  return (unsigned short)((std::min))(v, (unsigned int)USHRT_MAX);
429 }
430 
431 template <> inline unsigned short vpMath::saturate<unsigned short>(float v)
432 {
433  int iv = vpMath::round(v);
434  return vpMath::saturate<unsigned short>(iv);
435 }
436 
437 template <> inline unsigned short vpMath::saturate<unsigned short>(double v)
438 {
439  int iv = vpMath::round(v);
440  return vpMath::saturate<unsigned short>(iv);
441 }
442 
443 // short
444 template <> inline short vpMath::saturate<short>(unsigned short v) { return (short)((std::min))((int)v, SHRT_MAX); }
445 template <> inline short vpMath::saturate<short>(int v)
446 {
447  return (short)((unsigned int)(v - SHRT_MIN) <= (unsigned int)USHRT_MAX ? v : v > 0 ? SHRT_MAX : SHRT_MIN);
448 }
449 template <> inline short vpMath::saturate<short>(unsigned int v)
450 {
451  return (short)((std::min))(v, (unsigned int)SHRT_MAX);
452 }
453 template <> inline short vpMath::saturate<short>(float v)
454 {
455  int iv = vpMath::round(v);
456  return vpMath::saturate<short>(iv);
457 }
458 template <> inline short vpMath::saturate<short>(double v)
459 {
460  int iv = vpMath::round(v);
461  return vpMath::saturate<short>(iv);
462 }
463 
464 // int
465 template <> inline int vpMath::saturate<int>(float v) { return vpMath::round(v); }
466 
467 template <> inline int vpMath::saturate<int>(double v) { return vpMath::round(v); }
468 
469 // unsigned int
470 // (Comment from OpenCV) we intentionally do not clip negative numbers, to
471 // make -1 become 0xffffffff etc.
472 template <> inline unsigned int vpMath::saturate<unsigned int>(float v) { return (unsigned int)vpMath::round(v); }
473 
474 template <> inline unsigned int vpMath::saturate<unsigned int>(double v) { return (unsigned int)vpMath::round(v); }
475 
476 #endif
static _Tp saturate(double v)
Definition: vpMath.h:192
static void swap(Type &a, Type &b)
Definition: vpMath.h:175
static _Tp saturate(char v)
Definition: vpMath.h:186
static double sigmoid(double x, double x0=0., double x1=1., double n=12.)
Definition: vpMath.h:316
static _Tp saturate(unsigned char v)
Definition: vpMath.h:185
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:296
Provides simple mathematics computation tools that are not available in the C mathematics library (ma...
Definition: vpMath.h:92
static _Tp saturate(unsigned short v)
Definition: vpMath.h:187
static double fact(unsigned int x)
Definition: vpMath.h:211
static int round(const double x)
Definition: vpMath.h:241
static Type abs(const Type &x)
Definition: vpMath.h:158
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:143
static _Tp saturate(short v)
Definition: vpMath.h:188
static bool nul(double x, double s=0.001)
Definition: vpMath.h:287
static double sqr(double x)
Definition: vpMath.h:114
static Type minimum(const Type &a, const Type &b)
Definition: vpMath.h:151
static _Tp saturate(int v)
Definition: vpMath.h:190
static double rad(double deg)
Definition: vpMath.h:108
static _Tp saturate(unsigned v)
Definition: vpMath.h:189
static _Tp saturate(float v)
Definition: vpMath.h:191
static long double comb(unsigned int n, unsigned int p)
Definition: vpMath.h:226
static double deg(double rad)
Definition: vpMath.h:101
static bool greater(double x, double y, double s=0.001)
Definition: vpMath.h:305
static int() sign(double x)
Definition: vpMath.h:268