48 #include <visp3/core/vpConfig.h> 57 # define _USE_MATH_DEFINES 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) 67 #if defined(_WIN32) // Not defined in Microsoft math.h 70 #define M_PI 3.14159265358979323846 74 #define M_PI_2 (M_PI / 2.0) 78 #define M_PI_4 (M_PI / 4.0) 83 #include <visp3/core/vpImagePoint.h> 103 static inline double deg(
double rad) {
return (rad * 180.0) / M_PI; }
110 static inline double rad(
double deg) {
return (deg * M_PI) / 180.0; }
116 static inline double sqr(
double x) {
return x * x; }
119 static inline double fact(
unsigned int x);
122 static inline long double comb(
unsigned int n,
unsigned int p);
125 static inline int round(
double x);
128 static inline int(sign)(
double x);
131 static inline bool nul(
double x,
double s = 0.001);
134 static inline bool equal(
double x,
double y,
double s = 0.001);
137 static inline bool greater(
double x,
double y,
double s = 0.001);
145 template <
class Type>
static Type
maximum(
const Type &a,
const Type &b) {
return (a > b) ? a : b; }
153 template <
class Type>
static Type
minimum(
const Type &a,
const Type &b) {
return (a < b) ? a : b; }
160 template <
class Type>
static Type
abs(
const Type &x) {
return (x < 0) ? -x : x; }
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);
169 static inline double sigmoid(
double x,
double x0 = 0.,
double x1 = 1.,
double n = 12.);
177 template <
class Type>
static void swap(Type &a, Type &b)
184 static bool isNaN(
double value);
185 static bool isInf(
double value);
187 static double lineFitting(
const std::vector<vpImagePoint>& imPts,
double& a,
double& b,
double& c);
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); }
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);
202 static int modulo(
int a,
int n);
205 static const double ang_min_sinc;
206 static const double ang_min_mc;
217 if ((x == 1) || (x == 0))
219 return x * fact(x - 1);
234 return fact(n) / (fact(n - p) * fact(p));
247 #if defined(VISP_HAVE_FUNC_ROUND) 250 return (
int)::round(x);
251 #elif defined(VISP_HAVE_FUNC_STD_ROUND) 252 return (
int)std::round(x);
254 return (x > 0.0) ? ((int)floor(x + 0.5)) : ((
int)ceil(x - 0.5));
267 if (fabs(x) < std::numeric_limits<double>::epsilon())
293 bool vpMath::equal(
double x,
double y,
double s) {
return (nul(x - y, s)); }
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);
327 template <>
inline unsigned char vpMath::saturate<unsigned char>(
char v)
334 if (std::numeric_limits<char>::is_signed)
335 return (
unsigned char)(((std::max))((int)v, 0));
337 return (
unsigned char)((
unsigned int)v > SCHAR_MAX ? 0 : v);
340 template <>
inline unsigned char vpMath::saturate<unsigned char>(
unsigned short v)
342 return (
unsigned char)((std::min))((
unsigned int)v, (
unsigned int)UCHAR_MAX);
345 template <>
inline unsigned char vpMath::saturate<unsigned char>(
int v)
347 return (
unsigned char)((
unsigned int)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0);
350 template <>
inline unsigned char vpMath::saturate<unsigned char>(
short v) {
return saturate<unsigned char>((int)v); }
352 template <>
inline unsigned char vpMath::saturate<unsigned char>(
unsigned int v)
354 return (
unsigned char)((std::min))(v, (
unsigned int)UCHAR_MAX);
357 template <>
inline unsigned char vpMath::saturate<unsigned char>(
float v)
360 return saturate<unsigned char>(iv);
363 template <>
inline unsigned char vpMath::saturate<unsigned char>(
double v)
366 return saturate<unsigned char>(iv);
370 template <>
inline char vpMath::saturate<char>(
unsigned char v) {
return (
char)((std::min))((int)v, SCHAR_MAX); }
372 template <>
inline char vpMath::saturate<char>(
unsigned short v)
374 return (
char)((std::min))((
unsigned int)v, (
unsigned int)SCHAR_MAX);
377 template <>
inline char vpMath::saturate<char>(
int v)
379 return (
char)((
unsigned int)(v - SCHAR_MIN) <= (
unsigned int)UCHAR_MAX ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN);
382 template <>
inline char vpMath::saturate<char>(
short v) {
return saturate<char>((int)v); }
384 template <>
inline char vpMath::saturate<char>(
unsigned int v)
386 return (
char)((std::min))(v, (
unsigned int)SCHAR_MAX);
389 template <>
inline char vpMath::saturate<char>(
float v)
392 return saturate<char>(iv);
395 template <>
inline char vpMath::saturate<char>(
double v)
398 return saturate<char>(iv);
402 template <>
inline unsigned short vpMath::saturate<unsigned short>(
char v)
409 if (std::numeric_limits<char>::is_signed)
410 return (
unsigned char)(((std::max))((int)v, 0));
412 return (
unsigned char)((
unsigned int)v > SCHAR_MAX ? 0 : v);
415 template <>
inline unsigned short vpMath::saturate<unsigned short>(
short v)
417 return (
unsigned short)((std::max))((int)v, 0);
420 template <>
inline unsigned short vpMath::saturate<unsigned short>(
int v)
422 return (
unsigned short)((
unsigned int)v <= (
unsigned int)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0);
425 template <>
inline unsigned short vpMath::saturate<unsigned short>(
unsigned int v)
427 return (
unsigned short)((std::min))(v, (
unsigned int)USHRT_MAX);
430 template <>
inline unsigned short vpMath::saturate<unsigned short>(
float v)
433 return vpMath::saturate<unsigned short>(iv);
436 template <>
inline unsigned short vpMath::saturate<unsigned short>(
double v)
439 return vpMath::saturate<unsigned short>(iv);
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)
446 return (
short)((
unsigned int)(v - SHRT_MIN) <= (
unsigned int)USHRT_MAX ? v : v > 0 ? SHRT_MAX : SHRT_MIN);
448 template <>
inline short vpMath::saturate<short>(
unsigned int v)
450 return (
short)((std::min))(v, (
unsigned int)SHRT_MAX);
452 template <>
inline short vpMath::saturate<short>(
float v)
455 return vpMath::saturate<short>(iv);
457 template <>
inline short vpMath::saturate<short>(
double v)
460 return vpMath::saturate<short>(iv);
464 template <>
inline int vpMath::saturate<int>(
float v) {
return vpMath::round(v); }
466 template <>
inline int vpMath::saturate<int>(
double v) {
return vpMath::round(v); }
471 template <>
inline unsigned int vpMath::saturate<unsigned int>(
float v) {
return (
unsigned int)
vpMath::round(v); }
473 template <>
inline unsigned int vpMath::saturate<unsigned int>(
double v) {
return (
unsigned int)
vpMath::round(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 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 int round(double x)
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)