Visual Servoing Platform  version 3.4.0
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 
83 #include <visp3/core/vpImagePoint.h>
84 
94 class VISP_EXPORT vpMath
95 {
96 public:
103  static inline double deg(double rad) { return (rad * 180.0) / M_PI; }
104 
110  static inline double rad(double deg) { return (deg * M_PI) / 180.0; }
111 
116  static inline double sqr(double x) { return x * x; }
117 
118  // factorial of x
119  static inline double fact(unsigned int x);
120 
121  // combinaison
122  static inline long double comb(unsigned int n, unsigned int p);
123 
124  // round x to the nearest integer
125  static inline int round(double x);
126 
127  // return the sign of x (+-1)
128  static inline int(sign)(double x);
129 
130  // test if a number equals 0 (with threshold value)
131  static inline bool nul(double x, double s = 0.001);
132 
133  // test if two numbers are equals (with a user defined threshold)
134  static inline bool equal(double x, double y, double s = 0.001);
135 
136  // test if a number is greater than another (with a user defined threshold)
137  static inline bool greater(double x, double y, double s = 0.001);
138 
145  template <class Type> static Type maximum(const Type &a, const Type &b) { return (a > b) ? a : b; }
146 
153  template <class Type> static Type minimum(const Type &a, const Type &b) { return (a < b) ? a : b; }
154 
160  template <class Type> static Type abs(const Type &x) { return (x < 0) ? -x : x; }
161 
162  // sinus cardinal
163  static double sinc(double x);
164  static double sinc(double sinx, double x);
165  static double mcosc(double cosx, double x);
166  static double msinc(double sinx, double x);
167 
168  // sigmoid
169  static inline double sigmoid(double x, double x0 = 0., double x1 = 1., double n = 12.);
170 
177  template <class Type> static void swap(Type &a, Type &b)
178  {
179  Type tmp = b;
180  b = a;
181  a = tmp;
182  }
183 
184  static bool isNaN(double value);
185  static bool isInf(double value);
186 
187  static double lineFitting(const std::vector<vpImagePoint>& imPts, double& a, double& b, double& c);
188 
189  template <typename _Tp> static inline _Tp saturate(unsigned char v) { return _Tp(v); }
190  template <typename _Tp> static inline _Tp saturate(char v) { return _Tp(v); }
191  template <typename _Tp> static inline _Tp saturate(unsigned short v) { return _Tp(v); }
192  template <typename _Tp> static inline _Tp saturate(short v) { return _Tp(v); }
193  template <typename _Tp> static inline _Tp saturate(unsigned v) { return _Tp(v); }
194  template <typename _Tp> static inline _Tp saturate(int v) { return _Tp(v); }
195  template <typename _Tp> static inline _Tp saturate(float v) { return _Tp(v); }
196  template <typename _Tp> static inline _Tp saturate(double v) { return _Tp(v); }
197 
198  static double getMean(const std::vector<double> &v);
199  static double getMedian(const std::vector<double> &v);
200  static double getStdev(const std::vector<double> &v, bool useBesselCorrection = false);
201 
202  static int modulo(int a, int n);
203 
204 private:
205  static const double ang_min_sinc;
206  static const double ang_min_mc;
207 };
208 
209 // Begining of the inline functions definition
210 
215 double vpMath::fact(unsigned int x)
216 {
217  if ((x == 1) || (x == 0))
218  return 1;
219  return x * fact(x - 1);
220 }
221 
230 long double vpMath::comb(unsigned int n, unsigned int p)
231 {
232  if (n == p)
233  return 1;
234  return fact(n) / (fact(n - p) * fact(p));
235 }
236 
245 int vpMath::round(double x)
246 {
247 #if defined(VISP_HAVE_FUNC_ROUND)
248  //:: to design the global namespace and avoid to call recursively
249  // vpMath::round
250  return (int)::round(x);
251 #elif defined(VISP_HAVE_FUNC_STD_ROUND)
252  return (int)std::round(x);
253 #else
254  return (x > 0.0) ? ((int)floor(x + 0.5)) : ((int)ceil(x - 0.5));
255 #endif
256 }
257 
265 int ( vpMath::sign ) (double x)
266 {
267  if (fabs(x) < std::numeric_limits<double>::epsilon())
268  return 0;
269  else {
270  if (x < 0)
271  return -1;
272  else
273  return 1;
274  }
275 }
276 
284 bool vpMath::nul(double x, double s) { return (fabs(x) < s); }
285 
293 bool vpMath::equal(double x, double y, double s) { return (nul(x - y, s)); }
294 
302 bool vpMath::greater(double x, double y, double s) { return (x > (y - s)); }
303 
315 double vpMath::sigmoid(double x, double x0, double x1, double n)
316 {
317  if (x < x0)
318  return 0.;
319  else if (x > x1)
320  return 1.;
321  double l0 = 1. / (1. + exp(0.5 * n));
322  double l1 = 1. / (1. + exp(-0.5 * n));
323  return (1. / (1. + exp(-n * ((x - x0) / (x1 - x0) - 0.5))) - l0) / (l1 - l0);
324 }
325 
326 // unsigned char
327 template <> inline unsigned char vpMath::saturate<unsigned char>(char v)
328 {
329  // On big endian arch like powerpc, char implementation is unsigned
330  // with CHAR_MIN=0, CHAR_MAX=255 and SCHAR_MIN=-128, SCHAR_MAX=127
331  // leading to (int)(char -127) = 129.
332  // On little endian arch, CHAR_MIN=-127 and CHAR_MAX=128 leading to
333  // (int)(char -127) = -127.
334  if (std::numeric_limits<char>::is_signed)
335  return (unsigned char)(((std::max))((int)v, 0));
336  else
337  return (unsigned char)((unsigned int)v > SCHAR_MAX ? 0 : v);
338 }
339 
340 template <> inline unsigned char vpMath::saturate<unsigned char>(unsigned short v)
341 {
342  return (unsigned char)((std::min))((unsigned int)v, (unsigned int)UCHAR_MAX);
343 }
344 
345 template <> inline unsigned char vpMath::saturate<unsigned char>(int v)
346 {
347  return (unsigned char)((unsigned int)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0);
348 }
349 
350 template <> inline unsigned char vpMath::saturate<unsigned char>(short v) { return saturate<unsigned char>((int)v); }
351 
352 template <> inline unsigned char vpMath::saturate<unsigned char>(unsigned int v)
353 {
354  return (unsigned char)((std::min))(v, (unsigned int)UCHAR_MAX);
355 }
356 
357 template <> inline unsigned char vpMath::saturate<unsigned char>(float v)
358 {
359  int iv = vpMath::round(v);
360  return saturate<unsigned char>(iv);
361 }
362 
363 template <> inline unsigned char vpMath::saturate<unsigned char>(double v)
364 {
365  int iv = vpMath::round(v);
366  return saturate<unsigned char>(iv);
367 }
368 
369 // char
370 template <> inline char vpMath::saturate<char>(unsigned char v) { return (char)((std::min))((int)v, SCHAR_MAX); }
371 
372 template <> inline char vpMath::saturate<char>(unsigned short v)
373 {
374  return (char)((std::min))((unsigned int)v, (unsigned int)SCHAR_MAX);
375 }
376 
377 template <> inline char vpMath::saturate<char>(int v)
378 {
379  return (char)((unsigned int)(v - SCHAR_MIN) <= (unsigned int)UCHAR_MAX ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN);
380 }
381 
382 template <> inline char vpMath::saturate<char>(short v) { return saturate<char>((int)v); }
383 
384 template <> inline char vpMath::saturate<char>(unsigned int v)
385 {
386  return (char)((std::min))(v, (unsigned int)SCHAR_MAX);
387 }
388 
389 template <> inline char vpMath::saturate<char>(float v)
390 {
391  int iv = vpMath::round(v);
392  return saturate<char>(iv);
393 }
394 
395 template <> inline char vpMath::saturate<char>(double v)
396 {
397  int iv = vpMath::round(v);
398  return saturate<char>(iv);
399 }
400 
401 // unsigned short
402 template <> inline unsigned short vpMath::saturate<unsigned short>(char v)
403 {
404  // On big endian arch like powerpc, char implementation is unsigned
405  // with CHAR_MIN=0, CHAR_MAX=255 and SCHAR_MIN=-128, SCHAR_MAX=127
406  // leading to (int)(char -127) = 129.
407  // On little endian arch, CHAR_MIN=-127 and CHAR_MAX=128 leading to
408  // (int)(char -127) = -127.
409  if (std::numeric_limits<char>::is_signed)
410  return (unsigned char)(((std::max))((int)v, 0));
411  else
412  return (unsigned char)((unsigned int)v > SCHAR_MAX ? 0 : v);
413 }
414 
415 template <> inline unsigned short vpMath::saturate<unsigned short>(short v)
416 {
417  return (unsigned short)((std::max))((int)v, 0);
418 }
419 
420 template <> inline unsigned short vpMath::saturate<unsigned short>(int v)
421 {
422  return (unsigned short)((unsigned int)v <= (unsigned int)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0);
423 }
424 
425 template <> inline unsigned short vpMath::saturate<unsigned short>(unsigned int v)
426 {
427  return (unsigned short)((std::min))(v, (unsigned int)USHRT_MAX);
428 }
429 
430 template <> inline unsigned short vpMath::saturate<unsigned short>(float v)
431 {
432  int iv = vpMath::round(v);
433  return vpMath::saturate<unsigned short>(iv);
434 }
435 
436 template <> inline unsigned short vpMath::saturate<unsigned short>(double v)
437 {
438  int iv = vpMath::round(v);
439  return vpMath::saturate<unsigned short>(iv);
440 }
441 
442 // short
443 template <> inline short vpMath::saturate<short>(unsigned short v) { return (short)((std::min))((int)v, SHRT_MAX); }
444 template <> inline short vpMath::saturate<short>(int v)
445 {
446  return (short)((unsigned int)(v - SHRT_MIN) <= (unsigned int)USHRT_MAX ? v : v > 0 ? SHRT_MAX : SHRT_MIN);
447 }
448 template <> inline short vpMath::saturate<short>(unsigned int v)
449 {
450  return (short)((std::min))(v, (unsigned int)SHRT_MAX);
451 }
452 template <> inline short vpMath::saturate<short>(float v)
453 {
454  int iv = vpMath::round(v);
455  return vpMath::saturate<short>(iv);
456 }
457 template <> inline short vpMath::saturate<short>(double v)
458 {
459  int iv = vpMath::round(v);
460  return vpMath::saturate<short>(iv);
461 }
462 
463 // int
464 template <> inline int vpMath::saturate<int>(float v) { return vpMath::round(v); }
465 
466 template <> inline int vpMath::saturate<int>(double v) { return vpMath::round(v); }
467 
468 // unsigned int
469 // (Comment from OpenCV) we intentionally do not clip negative numbers, to
470 // make -1 become 0xffffffff etc.
471 template <> inline unsigned int vpMath::saturate<unsigned int>(float v) { return (unsigned int)vpMath::round(v); }
472 
473 template <> inline unsigned int vpMath::saturate<unsigned int>(double v) { return (unsigned int)vpMath::round(v); }
474 
475 #endif
static _Tp saturate(double v)
Definition: vpMath.h:196
static void swap(Type &a, Type &b)
Definition: vpMath.h:177
static _Tp saturate(char v)
Definition: vpMath.h:190
static double sigmoid(double x, double x0=0., double x1=1., double n=12.)
Definition: vpMath.h:315
static _Tp saturate(unsigned char v)
Definition: vpMath.h:189
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:293
Provides simple mathematics computation tools that are not available in the C mathematics library (ma...
Definition: vpMath.h:94
static _Tp saturate(unsigned short v)
Definition: vpMath.h:191
static double fact(unsigned int x)
Definition: vpMath.h:215
static Type abs(const Type &x)
Definition: vpMath.h:160
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:145
static _Tp saturate(short v)
Definition: vpMath.h:192
static bool nul(double x, double s=0.001)
Definition: vpMath.h:284
static double sqr(double x)
Definition: vpMath.h:116
static Type minimum(const Type &a, const Type &b)
Definition: vpMath.h:153
static _Tp saturate(int v)
Definition: vpMath.h:194
static double rad(double deg)
Definition: vpMath.h:110
static _Tp saturate(unsigned v)
Definition: vpMath.h:193
static int round(double x)
Definition: vpMath.h:245
static _Tp saturate(float v)
Definition: vpMath.h:195
static long double comb(unsigned int n, unsigned int p)
Definition: vpMath.h:230
static double deg(double rad)
Definition: vpMath.h:103
static bool greater(double x, double y, double s=0.001)
Definition: vpMath.h:302
static int() sign(double x)