49 #include <visp3/core/vpConfig.h>
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)
61 #if defined(_WIN32) // Not defined in Microsoft math.h
64 # define M_PI 3.14159265358979323846
68 # define M_PI_2 (M_PI/2.0)
72 # define M_PI_4 (M_PI/4.0)
97 static inline double deg(
double rad) {
return (rad*180.0)/M_PI ; }
104 static inline double rad(
double deg) {
return (deg*M_PI)/180.0 ; }
110 static inline double sqr(
double x) {
return x*x ; }
113 static inline double fact(
unsigned int x) ;
116 static inline long double comb(
unsigned int n,
unsigned int p) ;
119 static inline int round(
const double x) ;
122 static inline int sign(
double x) ;
126 static inline bool nul(
double x,
double s=0.001);
129 static inline bool equal(
double x,
double y,
double s=0.001);
132 static inline bool greater(
double x,
double y,
double s=0.001);
141 template <
class Type>
static Type
maximum(
const Type& a,
const Type& b)
143 return (a > b) ? a : b;
152 template <
class Type>
static Type
minimum(
const Type& a,
const Type& b)
154 return (a < b) ? a : b;
162 template <
class Type>
static Type
abs(
const Type& x)
164 return (x < 0) ? -x : x;
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) ;
175 static inline double sigmoid(
double x,
double x0=0.,
double x1=1.,
double n=12.);
183 template <
class Type>
static void swap(Type& a, Type& b)
190 static bool isNaN(
const double value);
191 static bool isInf(
const double value);
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); }
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);
207 static const double ang_min_sinc;
208 static const double ang_min_mc;
221 if ( (x == 1) || (x == 0))
return 1;
222 return x *
fact(x-1);
235 if (n == p)
return 1;
250 #if defined(VISP_HAVE_FUNC_ROUND)
253 #elif defined(VISP_HAVE_FUNC_STD_ROUND)
254 return (
int)std::round(x)
256 return (x > 0.0) ? ((int) floor(x + 0.5)) : ((
int) ceil(x - 0.5));
268 if (fabs(x) < std::numeric_limits<double>::epsilon())
299 return(
nul(x-y, s) );
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);
336 template<>
inline unsigned char vpMath::saturate<unsigned char>(
char v) {
337 return (
unsigned char) (std::max)((
int) v, 0);
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);
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);
348 template<>
inline unsigned char vpMath::saturate<unsigned char>(
short v) {
349 return saturate<unsigned char> ((int) v);
352 template<>
inline unsigned char vpMath::saturate<unsigned char>(
unsigned int v) {
353 return (
unsigned char) (std::min)(v, (
unsigned int) UCHAR_MAX);
356 template<>
inline unsigned char vpMath::saturate<unsigned char>(
float v) {
358 return saturate<unsigned char> (iv);
361 template<>
inline unsigned char vpMath::saturate<unsigned char>(
double v) {
363 return saturate<unsigned char> (iv);
367 template<>
inline char vpMath::saturate<char>(
unsigned char v) {
368 return (
char) (std::min)((
int) v, SCHAR_MAX);
371 template<>
inline char vpMath::saturate<char>(
unsigned short v) {
372 return (
char) (std::min)((
unsigned int) v, (
unsigned int) SCHAR_MAX);
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);
380 template<>
inline char vpMath::saturate<char>(
short v) {
381 return saturate<char>((int) v);
384 template<>
inline char vpMath::saturate<char>(
unsigned int v) {
385 return (
char) (std::min)(v, (
unsigned int) SCHAR_MAX);
388 template<>
inline char vpMath::saturate<char>(
float v) {
390 return saturate<char>(iv);
393 template<>
inline char vpMath::saturate<char>(
double v) {
395 return saturate<char>(iv);
399 template<>
inline unsigned short vpMath::saturate<unsigned short>(
char v) {
400 return (
unsigned short) (std::max)((
int) v, 0);
403 template<>
inline unsigned short vpMath::saturate<unsigned short>(
short v) {
404 return (
unsigned short) (std::max)((
int) v, 0);
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);
412 template<>
inline unsigned short vpMath::saturate<unsigned short>(
unsigned int v) {
413 return (
unsigned short) (std::min)(v, (
unsigned int) USHRT_MAX);
416 template<>
inline unsigned short vpMath::saturate<unsigned short>(
float v) {
418 return vpMath::saturate<unsigned short>(iv);
421 template<>
inline unsigned short vpMath::saturate<unsigned short>(
double v) {
423 return vpMath::saturate<unsigned short>(iv);
427 template<>
inline short vpMath::saturate<short>(
unsigned short v) {
428 return (
short) (std::min)((
int) v, SHRT_MAX);
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);
434 template<>
inline short vpMath::saturate<short>(
unsigned int v) {
435 return (
short) (std::min)(v, (
unsigned int) SHRT_MAX);
437 template<>
inline short vpMath::saturate<short>(
float v) {
439 return vpMath::saturate<short>(iv);
441 template<>
inline short vpMath::saturate<short>(
double v) {
443 return vpMath::saturate<short>(iv);
447 template<>
inline int vpMath::saturate<int>(
float v) {
451 template<>
inline int vpMath::saturate<int>(
double v) {
457 template<>
inline unsigned int vpMath::saturate<unsigned int>(
float v) {
461 template<>
inline unsigned int vpMath::saturate<unsigned int>(
double v) {
static _Tp saturate(double v)
static void swap(Type &a, Type &b)
static _Tp saturate(char v)
static double sigmoid(double x, double x0=0., double x1=1., double n=12.)
static _Tp saturate(unsigned char v)
static bool equal(double x, double y, double s=0.001)
Provides simple mathematics computation tools that are not available in the C mathematics library (ma...
static _Tp saturate(unsigned short v)
static double fact(unsigned int x)
static int round(const double x)
static Type abs(const Type &x)
static Type maximum(const Type &a, const Type &b)
static _Tp saturate(short v)
static bool nul(double x, double s=0.001)
static double sqr(double x)
static Type minimum(const Type &a, const Type &b)
static _Tp saturate(int v)
static double rad(double deg)
static _Tp saturate(unsigned v)
static _Tp saturate(float v)
static long double comb(unsigned int n, unsigned int p)
static double deg(double rad)
static bool greater(double x, double y, double s=0.001)
static int sign(double x)