Visual Servoing Platform  version 3.5.1 under development (2022-12-04)
vpMath.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2022 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  * Fabien Spindler
37  * Julien Dufour
38  *
39  *****************************************************************************/
40 
47 #ifndef vpMATH_HH
48 #define vpMATH_HH
49 
50 #include <visp3/core/vpConfig.h>
51 
52 #include <algorithm>
53 #include <climits>
54 #include <limits>
55 #if defined(_WIN32)
56 // Define _USE_MATH_DEFINES before including <math.h> to expose these macro
57 // definitions for common math constants. These are placed under an #ifdef
58 // since these commonly-defined names are not part of the C or C++ standards
59 #define _USE_MATH_DEFINES
60 #endif
61 #include <math.h>
62 #include <vector>
63 
64 #if defined(VISP_HAVE_FUNC_ISNAN) || defined(VISP_HAVE_FUNC_STD_ISNAN) || defined(VISP_HAVE_FUNC_ISINF) || \
65  defined(VISP_HAVE_FUNC_STD_ISINF) || defined(VISP_HAVE_FUNC_STD_ROUND)
66 #include <cmath>
67 #endif
68 
69 #if defined(_WIN32) // Not defined in Microsoft math.h
70 
71 #ifndef M_PI
72 #define M_PI 3.14159265358979323846
73 #endif
74 
75 #ifndef M_PI_2
76 #define M_PI_2 (M_PI / 2.0)
77 #endif
78 
79 #ifndef M_PI_4
80 #define M_PI_4 (M_PI / 4.0)
81 #endif
82 
83 #endif
84 
85 #include <visp3/core/vpException.h>
86 #include <visp3/core/vpImagePoint.h>
87 
88 class vpPoint;
90 class vpColVector;
91 
101 class VISP_EXPORT vpMath
102 {
103 public:
110  static inline double deg(double rad) { return (rad * 180.0) / M_PI; }
111 
117  static inline double rad(double deg) { return (deg * M_PI) / 180.0; }
118 
123  static inline double sqr(double x) { return x * x; }
124 
125  // factorial of x
126  static inline double fact(unsigned int x);
127 
128  // combinaison
129  static inline long double comb(unsigned int n, unsigned int p);
130 
138  template <typename T> static inline T clamp(const T &v, const T &lower, const T &upper)
139  {
140 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_17)
141  return std::clamp(v, lower, upper);
142 #else
143  if (upper < lower) {
144  throw vpException(vpException::badValue, "clamp: lower bound is greater than upper bound");
145  }
146  return (v < lower) ? lower : (upper < v) ? upper : v;
147 #endif
148  }
149 
150  // round x to the nearest integer
151  static inline int round(double x);
152 
153  // return the sign of x (+-1)
154  static inline int(sign)(double x);
155 
156  // test if a number equals 0 (with threshold value)
157  static inline bool nul(double x, double s = 0.001);
158 
159  // test if two numbers are equals (with a user defined threshold)
160  static inline bool equal(double x, double y, double s = 0.001);
161 
162  // test if a number is greater than another (with a user defined threshold)
163  static inline bool greater(double x, double y, double s = 0.001);
164 
171  template <class Type> static Type maximum(const Type &a, const Type &b) { return (a > b) ? a : b; }
172 
179  template <class Type> static Type minimum(const Type &a, const Type &b) { return (a < b) ? a : b; }
180 
186  template <class Type> static Type abs(const Type &x) { return (x < 0) ? -x : x; }
187 
188  // sinus cardinal
189  static double sinc(double x);
190  static double sinc(double sinx, double x);
191  static double mcosc(double cosx, double x);
192  static double msinc(double sinx, double x);
193 
194  // sigmoid
195  static inline double sigmoid(double x, double x0 = 0., double x1 = 1., double n = 12.);
196 
203  template <class Type> static void swap(Type &a, Type &b)
204  {
205  Type tmp = b;
206  b = a;
207  a = tmp;
208  }
209 
210  static bool isNaN(double value);
211  static bool isNaN(float value);
212  static bool isInf(double value);
213  static bool isInf(float value);
214 
215  static double lineFitting(const std::vector<vpImagePoint> &imPts, double &a, double &b, double &c);
216 
217  template <typename _Tp> static inline _Tp saturate(unsigned char v) { return _Tp(v); }
218  template <typename _Tp> static inline _Tp saturate(char v) { return _Tp(v); }
219  template <typename _Tp> static inline _Tp saturate(unsigned short v) { return _Tp(v); }
220  template <typename _Tp> static inline _Tp saturate(short v) { return _Tp(v); }
221  template <typename _Tp> static inline _Tp saturate(unsigned v) { return _Tp(v); }
222  template <typename _Tp> static inline _Tp saturate(int v) { return _Tp(v); }
223  template <typename _Tp> static inline _Tp saturate(float v) { return _Tp(v); }
224  template <typename _Tp> static inline _Tp saturate(double v) { return _Tp(v); }
225 
226  static double getMean(const std::vector<double> &v);
227  static double getMedian(const std::vector<double> &v);
228  static double getStdev(const std::vector<double> &v, bool useBesselCorrection = false);
229 
230  static int modulo(int a, int n);
231 
232  static vpHomogeneousMatrix ned2ecef(double lonDeg, double latDeg, double radius);
233  static vpHomogeneousMatrix enu2ecef(double lonDeg, double latDeg, double radius);
234 
245  template <typename T> static std::vector<double> linspace(T start_in, T end_in, unsigned int num_in)
246  {
247  std::vector<double> linspaced;
248 
249  double start = static_cast<double>(start_in);
250  double end = static_cast<double>(end_in);
251  double num = static_cast<double>(num_in);
252 
253  if (std::fabs(num) < std::numeric_limits<double>::epsilon()) {
254  return linspaced;
255  }
256  if (std::fabs(num - 1) < std::numeric_limits<double>::epsilon()) {
257  linspaced.push_back(start);
258  return linspaced;
259  }
260 
261  double delta = (end - start) / (num - 1);
262 
263  for (int i = 0; i < num - 1; i++) {
264  linspaced.push_back(start + delta * i);
265  }
266  linspaced.push_back(end); // I want to ensure that start and end
267  // are exactly the same as the input
268  return linspaced;
269  }
270 
271  static std::vector<std::pair<double, double> > computeRegularPointsOnSphere(unsigned int maxPoints);
272  static std::vector<vpHomogeneousMatrix>
273  getLocalTangentPlaneTransformations(const std::vector<std::pair<double, double> > &lonlatVec, double radius,
274  vpHomogeneousMatrix (*toECEF)(double lonDeg, double latDeg, double radius));
275 
276  static vpHomogeneousMatrix lookAt(const vpColVector &from, const vpColVector &to, vpColVector tmp);
277 
278 private:
279  static const double ang_min_sinc;
280  static const double ang_min_mc;
281 };
282 
283 // Begining of the inline functions definition
284 
289 double vpMath::fact(unsigned int x)
290 {
291  if ((x == 1) || (x == 0))
292  return 1;
293  return x * fact(x - 1);
294 }
295 
304 long double vpMath::comb(unsigned int n, unsigned int p)
305 {
306  if (n == p)
307  return 1;
308  return fact(n) / (fact(n - p) * fact(p));
309 }
310 
318 int vpMath::round(double x)
319 {
320 #if defined(VISP_HAVE_FUNC_ROUND)
321  //:: to design the global namespace and avoid to call recursively
322  // vpMath::round
323  return (int)::round(x);
324 #elif defined(VISP_HAVE_FUNC_STD_ROUND)
325  return (int)std::round(x);
326 #else
327  return (x > 0.0) ? ((int)floor(x + 0.5)) : ((int)ceil(x - 0.5));
328 #endif
329 }
330 
337 int(vpMath::sign)(double x)
338 {
339  if (fabs(x) < std::numeric_limits<double>::epsilon())
340  return 0;
341  else {
342  if (x < 0)
343  return -1;
344  else
345  return 1;
346  }
347 }
348 
355 bool vpMath::nul(double x, double s) { return (fabs(x) < s); }
356 
364 bool vpMath::equal(double x, double y, double s) { return (nul(x - y, s)); }
365 
373 bool vpMath::greater(double x, double y, double s) { return (x > (y - s)); }
374 
386 double vpMath::sigmoid(double x, double x0, double x1, double n)
387 {
388  if (x < x0)
389  return 0.;
390  else if (x > x1)
391  return 1.;
392  double l0 = 1. / (1. + exp(0.5 * n));
393  double l1 = 1. / (1. + exp(-0.5 * n));
394  return (1. / (1. + exp(-n * ((x - x0) / (x1 - x0) - 0.5))) - l0) / (l1 - l0);
395 }
396 
397 // unsigned char
398 template <> inline unsigned char vpMath::saturate<unsigned char>(char v)
399 {
400  // On big endian arch like powerpc, char implementation is unsigned
401  // with CHAR_MIN=0, CHAR_MAX=255 and SCHAR_MIN=-128, SCHAR_MAX=127
402  // leading to (int)(char -127) = 129.
403  // On little endian arch, CHAR_MIN=-127 and CHAR_MAX=128 leading to
404  // (int)(char -127) = -127.
405  if (std::numeric_limits<char>::is_signed)
406  return (unsigned char)(((std::max))((int)v, 0));
407  else
408  return (unsigned char)((unsigned int)v > SCHAR_MAX ? 0 : v);
409 }
410 
411 template <> inline unsigned char vpMath::saturate<unsigned char>(unsigned short v)
412 {
413  return (unsigned char)((std::min))((unsigned int)v, (unsigned int)UCHAR_MAX);
414 }
415 
416 template <> inline unsigned char vpMath::saturate<unsigned char>(int v)
417 {
418  return (unsigned char)((unsigned int)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0);
419 }
420 
421 template <> inline unsigned char vpMath::saturate<unsigned char>(short v) { return saturate<unsigned char>((int)v); }
422 
423 template <> inline unsigned char vpMath::saturate<unsigned char>(unsigned int v)
424 {
425  return (unsigned char)((std::min))(v, (unsigned int)UCHAR_MAX);
426 }
427 
428 template <> inline unsigned char vpMath::saturate<unsigned char>(float v)
429 {
430  int iv = vpMath::round(v);
431  return saturate<unsigned char>(iv);
432 }
433 
434 template <> inline unsigned char vpMath::saturate<unsigned char>(double v)
435 {
436  int iv = vpMath::round(v);
437  return saturate<unsigned char>(iv);
438 }
439 
440 // char
441 template <> inline char vpMath::saturate<char>(unsigned char v) { return (char)((std::min))((int)v, SCHAR_MAX); }
442 
443 template <> inline char vpMath::saturate<char>(unsigned short v)
444 {
445  return (char)((std::min))((unsigned int)v, (unsigned int)SCHAR_MAX);
446 }
447 
448 template <> inline char vpMath::saturate<char>(int v)
449 {
450  return (char)((unsigned int)(v - SCHAR_MIN) <= (unsigned int)UCHAR_MAX ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN);
451 }
452 
453 template <> inline char vpMath::saturate<char>(short v) { return saturate<char>((int)v); }
454 
455 template <> inline char vpMath::saturate<char>(unsigned int v)
456 {
457  return (char)((std::min))(v, (unsigned int)SCHAR_MAX);
458 }
459 
460 template <> inline char vpMath::saturate<char>(float v)
461 {
462  int iv = vpMath::round(v);
463  return saturate<char>(iv);
464 }
465 
466 template <> inline char vpMath::saturate<char>(double v)
467 {
468  int iv = vpMath::round(v);
469  return saturate<char>(iv);
470 }
471 
472 // unsigned short
473 template <> inline unsigned short vpMath::saturate<unsigned short>(char v)
474 {
475  // On big endian arch like powerpc, char implementation is unsigned
476  // with CHAR_MIN=0, CHAR_MAX=255 and SCHAR_MIN=-128, SCHAR_MAX=127
477  // leading to (int)(char -127) = 129.
478  // On little endian arch, CHAR_MIN=-127 and CHAR_MAX=128 leading to
479  // (int)(char -127) = -127.
480  if (std::numeric_limits<char>::is_signed)
481  return (unsigned char)(((std::max))((int)v, 0));
482  else
483  return (unsigned char)((unsigned int)v > SCHAR_MAX ? 0 : v);
484 }
485 
486 template <> inline unsigned short vpMath::saturate<unsigned short>(short v)
487 {
488  return (unsigned short)((std::max))((int)v, 0);
489 }
490 
491 template <> inline unsigned short vpMath::saturate<unsigned short>(int v)
492 {
493  return (unsigned short)((unsigned int)v <= (unsigned int)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0);
494 }
495 
496 template <> inline unsigned short vpMath::saturate<unsigned short>(unsigned int v)
497 {
498  return (unsigned short)((std::min))(v, (unsigned int)USHRT_MAX);
499 }
500 
501 template <> inline unsigned short vpMath::saturate<unsigned short>(float v)
502 {
503  int iv = vpMath::round(v);
504  return vpMath::saturate<unsigned short>(iv);
505 }
506 
507 template <> inline unsigned short vpMath::saturate<unsigned short>(double v)
508 {
509  int iv = vpMath::round(v);
510  return vpMath::saturate<unsigned short>(iv);
511 }
512 
513 // short
514 template <> inline short vpMath::saturate<short>(unsigned short v) { return (short)((std::min))((int)v, SHRT_MAX); }
515 template <> inline short vpMath::saturate<short>(int v)
516 {
517  return (short)((unsigned int)(v - SHRT_MIN) <= (unsigned int)USHRT_MAX ? v : v > 0 ? SHRT_MAX : SHRT_MIN);
518 }
519 template <> inline short vpMath::saturate<short>(unsigned int v)
520 {
521  return (short)((std::min))(v, (unsigned int)SHRT_MAX);
522 }
523 template <> inline short vpMath::saturate<short>(float v)
524 {
525  int iv = vpMath::round(v);
526  return vpMath::saturate<short>(iv);
527 }
528 template <> inline short vpMath::saturate<short>(double v)
529 {
530  int iv = vpMath::round(v);
531  return vpMath::saturate<short>(iv);
532 }
533 
534 // int
535 template <> inline int vpMath::saturate<int>(float v) { return vpMath::round(v); }
536 
537 template <> inline int vpMath::saturate<int>(double v) { return vpMath::round(v); }
538 
539 // unsigned int
540 // (Comment from OpenCV) we intentionally do not clip negative numbers, to
541 // make -1 become 0xffffffff etc.
542 template <> inline unsigned int vpMath::saturate<unsigned int>(float v) { return (unsigned int)vpMath::round(v); }
543 
544 template <> inline unsigned int vpMath::saturate<unsigned int>(double v) { return (unsigned int)vpMath::round(v); }
545 
546 #endif
Implementation of column vector and the associated operations.
Definition: vpColVector.h:131
error that can be emited by ViSP classes.
Definition: vpException.h:72
@ badValue
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:97
Implementation of an homogeneous matrix and operations on such kind of matrices.
Provides simple mathematics computation tools that are not available in the C mathematics library (ma...
Definition: vpMath.h:102
static _Tp saturate(char v)
Definition: vpMath.h:218
static void swap(Type &a, Type &b)
Definition: vpMath.h:203
static int() sign(double x)
static double fact(unsigned int x)
Definition: vpMath.h:289
static double rad(double deg)
Definition: vpMath.h:117
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:171
static _Tp saturate(short v)
Definition: vpMath.h:220
static _Tp saturate(double v)
Definition: vpMath.h:224
static double sqr(double x)
Definition: vpMath.h:123
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:364
static _Tp saturate(unsigned short v)
Definition: vpMath.h:219
static Type abs(const Type &x)
Definition: vpMath.h:186
static _Tp saturate(float v)
Definition: vpMath.h:223
static double sigmoid(double x, double x0=0., double x1=1., double n=12.)
Definition: vpMath.h:386
static bool nul(double x, double s=0.001)
Definition: vpMath.h:355
static _Tp saturate(unsigned v)
Definition: vpMath.h:221
static T clamp(const T &v, const T &lower, const T &upper)
Definition: vpMath.h:138
static int round(double x)
Definition: vpMath.h:318
static _Tp saturate(int v)
Definition: vpMath.h:222
static Type minimum(const Type &a, const Type &b)
Definition: vpMath.h:179
static long double comb(unsigned int n, unsigned int p)
Definition: vpMath.h:304
static double deg(double rad)
Definition: vpMath.h:110
static _Tp saturate(unsigned char v)
Definition: vpMath.h:217
static bool greater(double x, double y, double s=0.001)
Definition: vpMath.h:373
static std::vector< double > linspace(T start_in, T end_in, unsigned int num_in)
Definition: vpMath.h:245
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:82