43 #include <visp3/core/vpConfig.h>
52 #ifndef _USE_MATH_DEFINES
53 #define _USE_MATH_DEFINES
59 #if defined(VISP_HAVE_FUNC_ISNAN) || defined(VISP_HAVE_FUNC_STD_ISNAN) || defined(VISP_HAVE_FUNC_ISINF) || \
60 defined(VISP_HAVE_FUNC_STD_ISINF) || defined(VISP_HAVE_FUNC_STD_ROUND)
67 #define M_PI 3.14159265358979323846
71 #define M_PI_2 (M_PI / 2.0)
75 #define M_PI_4 (M_PI / 4.0)
81 #define M_PI_FLOAT 3.14159265358979323846f
85 #define M_PI_2_FLOAT (M_PI_FLOAT / 2.0f)
89 #define M_PI_4_FLOAT (M_PI_FLOAT / 4.0f)
92 #include <visp3/core/vpException.h>
93 #include <visp3/core/vpImagePoint.h>
119 static inline double deg(
double rad) {
return (rad * 180.0) / M_PI; }
129 static inline double rad(
double deg) {
return (deg * M_PI) / 180.0; }
141 float theta1 = theta;
142 if (theta1 > M_PI_FLOAT) {
143 theta1 -= 2.0f * M_PI_FLOAT;
145 else if (theta1 <= -M_PI_FLOAT) {
146 theta1 += 2.0f * M_PI_FLOAT;
159 double theta1 = theta;
161 theta1 -= 2.0 * M_PI;
163 else if (theta1 < -M_PI) {
164 theta1 += 2.0 * M_PI;
177 static float modulo(
const float &value,
const float &modulo)
179 float quotient = std::floor(value / modulo);
180 float rest = value - (quotient * modulo);
192 static double modulo(
const double &value,
const double &modulo)
194 double quotient = std::floor(value / modulo);
195 double rest = value - (quotient * modulo);
203 static inline double sqr(
double x) {
return x * x; }
206 static inline double fact(
unsigned int x);
209 static inline long double comb(
unsigned int n,
unsigned int p);
218 template <
typename T>
static inline T
clamp(
const T &v,
const T &lower,
const T &upper)
223 #if ((__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
224 return std::clamp(v, lower, upper);
229 return (v < lower) ? lower : (upper < v) ? upper : v;
234 static inline int round(
double x);
237 static inline int sign(
double x);
240 static inline bool nul(
double x,
double threshold = 0.001);
243 static inline bool equal(
double x,
double y,
double threshold = 0.001);
246 static inline bool greater(
double x,
double y,
double threshold = 0.001);
254 template <
class Type>
static Type
maximum(
const Type &a,
const Type &b) {
return (a > b) ? a : b; }
262 template <
class Type>
static Type
minimum(
const Type &a,
const Type &b) {
return (a < b) ? a : b; }
269 template <
class Type>
static Type
abs(
const Type &x) {
return (x < 0) ? -x : x; }
272 static double sinc(
double x);
273 static double sinc(
double sinx,
double x);
274 static double mcosc(
double cosx,
double x);
275 static double msinc(
double sinx,
double x);
278 static inline double sigmoid(
double x,
double x0 = 0.,
double x1 = 1.,
double n = 12.);
286 template <
class Type>
static void swap(Type &a, Type &b)
293 static bool isNaN(
double value);
294 static bool isNaN(
float value);
295 static bool isInf(
double value);
296 static bool isInf(
float value);
297 static bool isFinite(
double value);
298 static bool isFinite(
float value);
299 static bool isNumber(
const std::string &str);
301 static double lineFitting(
const std::vector<vpImagePoint> &imPts,
double &a,
double &b,
double &c);
303 template <
typename Tp>
static inline Tp
saturate(
unsigned char v) {
return Tp(v); }
304 template <
typename Tp>
static inline Tp
saturate(
char v) {
return Tp(v); }
305 template <
typename Tp>
static inline Tp
saturate(
unsigned short v) {
return Tp(v); }
306 template <
typename Tp>
static inline Tp
saturate(
short v) {
return Tp(v); }
307 template <
typename Tp>
static inline Tp
saturate(
unsigned v) {
return Tp(v); }
308 template <
typename Tp>
static inline Tp
saturate(
int v) {
return Tp(v); }
309 template <
typename Tp>
static inline Tp
saturate(
float v) {
return Tp(v); }
310 template <
typename Tp>
static inline Tp
saturate(
double v) {
return Tp(v); }
312 static double getMean(
const std::vector<double> &v);
313 static double getMedian(
const std::vector<double> &v);
314 static double getStdev(
const std::vector<double> &v,
bool useBesselCorrection =
false);
316 static int modulo(
int a,
int n);
317 static unsigned int modulo(
unsigned int a,
unsigned int n);
333 template <
typename T>
static std::vector<double>
linspace(T start_in, T end_in,
unsigned int num_in)
335 std::vector<double> linspaced;
337 double start =
static_cast<double>(start_in);
338 double end =
static_cast<double>(end_in);
339 double num =
static_cast<double>(num_in);
341 if (std::fabs(num) < std::numeric_limits<double>::epsilon()) {
344 if (std::fabs(num - 1) < std::numeric_limits<double>::epsilon()) {
345 linspaced.push_back(start);
349 double delta = (end - start) / (num - 1);
351 for (
int i = 0; i < (num - 1); ++i) {
352 linspaced.push_back(start + (delta * i));
354 linspaced.push_back(end);
359 static std::vector<std::pair<double, double> > computeRegularPointsOnSphere(
unsigned int maxPoints);
362 static std::vector<vpHomogeneousMatrix>
363 getLocalTangentPlaneTransformations(
const std::vector<std::pair<double, double> > &lonlatVec,
double radius,
364 LongLattToHomogeneous func);
369 static const double ang_min_sinc;
370 static const double ang_min_mc;
381 if ((x == 1) || (x == 0)) {
384 return x *
fact(x - 1);
412 #if defined(VISP_HAVE_FUNC_STD_ROUND)
413 return static_cast<int>(std::round(x));
414 #elif defined(VISP_HAVE_FUNC_ROUND)
417 return static_cast<int>(
::round(x));
419 return (x > 0.0) ? (
static_cast<int>(floor(x + 0.5))) : (
static_cast<int>(ceil(x - 0.5)));
431 if (fabs(x) < std::numeric_limits<double>::epsilon()) {
450 bool vpMath::nul(
double x,
double threshold) {
return (fabs(x) < threshold); }
459 bool vpMath::equal(
double x,
double y,
double threshold) {
return (
nul(x - y, threshold)); }
468 bool vpMath::greater(
double x,
double y,
double threshold) {
return (x > (y - threshold)); }
489 double l0 = 1. / (1. + exp(0.5 * n));
490 double l1 = 1. / (1. + exp(-0.5 * n));
491 return ((1. / (1. + exp(-n * (((x - x0) / (x1 - x0)) - 0.5)))) - l0) / (l1 - l0);
495 template <>
inline unsigned char vpMath::saturate<unsigned char>(
char v)
502 if (std::numeric_limits<char>::is_signed) {
503 return static_cast<unsigned char>(std::max<int>(
static_cast<int>(v), 0));
506 return static_cast<unsigned char>(
static_cast<unsigned int>(v) > SCHAR_MAX ? 0 : v);
510 template <>
inline unsigned char vpMath::saturate<unsigned char>(
unsigned short v)
512 return static_cast<unsigned char>(std::min<unsigned int>(
static_cast<unsigned int>(v),
static_cast<unsigned int>(UCHAR_MAX)));
515 template <>
inline unsigned char vpMath::saturate<unsigned char>(
int v)
517 return static_cast<unsigned char>(
static_cast<unsigned int>(v) <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0);
520 template <>
inline unsigned char vpMath::saturate<unsigned char>(
short v)
522 return saturate<unsigned char>(
static_cast<int>(v));
525 template <>
inline unsigned char vpMath::saturate<unsigned char>(
unsigned int v)
527 return static_cast<unsigned char>(std::min<unsigned int>(v,
static_cast<unsigned int>(UCHAR_MAX)));
530 template <>
inline unsigned char vpMath::saturate<unsigned char>(
float v)
533 return saturate<unsigned char>(iv);
536 template <>
inline unsigned char vpMath::saturate<unsigned char>(
double v)
539 return saturate<unsigned char>(iv);
543 template <>
inline char vpMath::saturate<char>(
unsigned char v)
545 return static_cast<char>(std::min<int>(
static_cast<int>(v), SCHAR_MAX));
548 template <>
inline char vpMath::saturate<char>(
unsigned short v)
550 return static_cast<char>(std::min<unsigned int>(
static_cast<unsigned int>(v),
static_cast<unsigned int>(SCHAR_MAX)));
553 template <>
inline char vpMath::saturate<char>(
int v)
555 return static_cast<char>(
static_cast<unsigned int>(v - SCHAR_MIN) <=
static_cast<unsigned int>(UCHAR_MAX) ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN);
558 template <>
inline char vpMath::saturate<char>(
short v)
560 return saturate<char>(
static_cast<int>(v));
563 template <>
inline char vpMath::saturate<char>(
unsigned int v)
565 return static_cast<char>(std::min<unsigned int>(v,
static_cast<unsigned int>(SCHAR_MAX)));
568 template <>
inline char vpMath::saturate<char>(
float v)
571 return saturate<char>(iv);
574 template <>
inline char vpMath::saturate<char>(
double v)
577 return saturate<char>(iv);
581 template <>
inline unsigned short vpMath::saturate<unsigned short>(
char v)
588 if (std::numeric_limits<char>::is_signed) {
589 return static_cast<unsigned short>(std::max<int>(
static_cast<int>(v), 0));
592 return static_cast<unsigned short>(
static_cast<unsigned int>(v) > SCHAR_MAX ? 0 : v);
596 template <>
inline unsigned short vpMath::saturate<unsigned short>(
short v)
598 return static_cast<unsigned short>(std::max<int>(
static_cast<int>(v), 0));
601 template <>
inline unsigned short vpMath::saturate<unsigned short>(
int v)
603 return static_cast<unsigned short>(
static_cast<unsigned int>(v) <=
static_cast<unsigned int>(USHRT_MAX) ? v : v > 0 ? USHRT_MAX : 0);
606 template <>
inline unsigned short vpMath::saturate<unsigned short>(
unsigned int v)
608 return static_cast<unsigned short>(std::min<unsigned int>(v,
static_cast<unsigned int>(USHRT_MAX)));
611 template <>
inline unsigned short vpMath::saturate<unsigned short>(
float v)
614 return vpMath::saturate<unsigned short>(iv);
617 template <>
inline unsigned short vpMath::saturate<unsigned short>(
double v)
620 return vpMath::saturate<unsigned short>(iv);
624 template <>
inline short vpMath::saturate<short>(
unsigned short v)
626 return static_cast<short>(std::min<int>(
static_cast<int>(v), SHRT_MAX));
628 template <>
inline short vpMath::saturate<short>(
int v)
630 return static_cast<short>(
static_cast<unsigned int>(v - SHRT_MIN) <=
static_cast<unsigned int>(USHRT_MAX) ? v : v > 0 ? SHRT_MAX : SHRT_MIN);
632 template <>
inline short vpMath::saturate<short>(
unsigned int v)
634 return static_cast<short>(std::min<unsigned int>(v,
static_cast<unsigned int>(SHRT_MAX)));
636 template <>
inline short vpMath::saturate<short>(
float v)
639 return vpMath::saturate<short>(iv);
641 template <>
inline short vpMath::saturate<short>(
double v)
644 return vpMath::saturate<short>(iv);
648 template <>
inline int vpMath::saturate<int>(
float v)
653 template <>
inline int vpMath::saturate<int>(
double v)
661 template <>
inline unsigned int vpMath::saturate<unsigned int>(
float v)
663 return static_cast<unsigned int>(
vpMath::round(
static_cast<double>(v)));
666 template <>
inline unsigned int vpMath::saturate<unsigned int>(
double v)
Implementation of column vector and the associated operations.
error that can be emitted by ViSP classes.
@ badValue
Used to indicate that a value is not in the allowed range.
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...
static Tp saturate(double v)
static Tp saturate(char v)
static void swap(Type &a, Type &b)
static Tp saturate(unsigned char v)
static float getAngleBetweenMinPiAndPi(const float &theta)
static Tp saturate(unsigned v)
static double fact(unsigned int x)
static double rad(double deg)
static Type maximum(const Type &a, const Type &b)
static double sqr(double x)
static Tp saturate(int v)
static bool greater(double x, double y, double threshold=0.001)
static Type abs(const Type &x)
static bool equal(double x, double y, double threshold=0.001)
static double sigmoid(double x, double x0=0., double x1=1., double n=12.)
static T clamp(const T &v, const T &lower, const T &upper)
static bool nul(double x, double threshold=0.001)
static int round(double x)
static Type minimum(const Type &a, const Type &b)
static Tp saturate(float v)
static int sign(double x)
static double modulo(const double &value, const double &modulo)
Gives the rest of value divided by modulo when the quotient can only be an integer.
static long double comb(unsigned int n, unsigned int p)
static double deg(double rad)
static float modulo(const float &value, const float &modulo)
Gives the rest of value divided by modulo when the quotient can only be an integer.
static Tp saturate(unsigned short v)
static double getAngleBetweenMinPiAndPi(const double &theta)
static Tp saturate(short v)
static std::vector< double > linspace(T start_in, T end_in, unsigned int num_in)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Implementation of a generic rotation vector.
Implementation of a rotation vector as Euler angle minimal representation.
Class that consider the case of a translation vector.