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);
332 template <
typename T>
static std::vector<double>
linspace(T start_in, T end_in,
unsigned int num_in)
334 std::vector<double> linspaced;
336 double start =
static_cast<double>(start_in);
337 double end =
static_cast<double>(end_in);
338 double num =
static_cast<double>(num_in);
340 if (std::fabs(num) < std::numeric_limits<double>::epsilon()) {
343 if (std::fabs(num - 1) < std::numeric_limits<double>::epsilon()) {
344 linspaced.push_back(start);
348 double delta = (end - start) / (num - 1);
350 for (
int i = 0; i < (num - 1); ++i) {
351 linspaced.push_back(start + (delta * i));
353 linspaced.push_back(end);
358 static std::vector<std::pair<double, double> > computeRegularPointsOnSphere(
unsigned int maxPoints);
361 static std::vector<vpHomogeneousMatrix>
362 getLocalTangentPlaneTransformations(
const std::vector<std::pair<double, double> > &lonlatVec,
double radius,
363 LongLattToHomogeneous func);
368 static const double ang_min_sinc;
369 static const double ang_min_mc;
380 if ((x == 1) || (x == 0)) {
383 return x *
fact(x - 1);
411 #if defined(VISP_HAVE_FUNC_STD_ROUND)
412 return static_cast<int>(std::round(x));
413 #elif defined(VISP_HAVE_FUNC_ROUND)
416 return static_cast<int>(
::round(x));
418 return (x > 0.0) ? (
static_cast<int>(floor(x + 0.5))) : (
static_cast<int>(ceil(x - 0.5)));
430 if (fabs(x) < std::numeric_limits<double>::epsilon()) {
449 bool vpMath::nul(
double x,
double threshold) {
return (fabs(x) < threshold); }
458 bool vpMath::equal(
double x,
double y,
double threshold) {
return (
nul(x - y, threshold)); }
467 bool vpMath::greater(
double x,
double y,
double threshold) {
return (x > (y - threshold)); }
488 double l0 = 1. / (1. + exp(0.5 * n));
489 double l1 = 1. / (1. + exp(-0.5 * n));
490 return ((1. / (1. + exp(-n * (((x - x0) / (x1 - x0)) - 0.5)))) - l0) / (l1 - l0);
494 template <>
inline unsigned char vpMath::saturate<unsigned char>(
char v)
501 if (std::numeric_limits<char>::is_signed) {
502 return static_cast<unsigned char>(std::max<int>(
static_cast<int>(v), 0));
505 return static_cast<unsigned char>(
static_cast<unsigned int>(v) > SCHAR_MAX ? 0 : v);
509 template <>
inline unsigned char vpMath::saturate<unsigned char>(
unsigned short v)
511 return static_cast<unsigned char>(std::min<unsigned int>(
static_cast<unsigned int>(v),
static_cast<unsigned int>(UCHAR_MAX)));
514 template <>
inline unsigned char vpMath::saturate<unsigned char>(
int v)
516 return static_cast<unsigned char>(
static_cast<unsigned int>(v) <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0);
519 template <>
inline unsigned char vpMath::saturate<unsigned char>(
short v)
521 return saturate<unsigned char>(
static_cast<int>(v));
524 template <>
inline unsigned char vpMath::saturate<unsigned char>(
unsigned int v)
526 return static_cast<unsigned char>(std::min<unsigned int>(v,
static_cast<unsigned int>(UCHAR_MAX)));
529 template <>
inline unsigned char vpMath::saturate<unsigned char>(
float v)
532 return saturate<unsigned char>(iv);
535 template <>
inline unsigned char vpMath::saturate<unsigned char>(
double v)
538 return saturate<unsigned char>(iv);
542 template <>
inline char vpMath::saturate<char>(
unsigned char v)
544 return static_cast<char>(std::min<int>(
static_cast<int>(v), SCHAR_MAX));
547 template <>
inline char vpMath::saturate<char>(
unsigned short v)
549 return static_cast<char>(std::min<unsigned int>(
static_cast<unsigned int>(v),
static_cast<unsigned int>(SCHAR_MAX)));
552 template <>
inline char vpMath::saturate<char>(
int v)
554 return static_cast<char>(
static_cast<unsigned int>(v - SCHAR_MIN) <=
static_cast<unsigned int>(UCHAR_MAX) ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN);
557 template <>
inline char vpMath::saturate<char>(
short v)
559 return saturate<char>(
static_cast<int>(v));
562 template <>
inline char vpMath::saturate<char>(
unsigned int v)
564 return static_cast<char>(std::min<unsigned int>(v,
static_cast<unsigned int>(SCHAR_MAX)));
567 template <>
inline char vpMath::saturate<char>(
float v)
570 return saturate<char>(iv);
573 template <>
inline char vpMath::saturate<char>(
double v)
576 return saturate<char>(iv);
580 template <>
inline unsigned short vpMath::saturate<unsigned short>(
char v)
587 if (std::numeric_limits<char>::is_signed) {
588 return static_cast<unsigned short>(std::max<int>(
static_cast<int>(v), 0));
591 return static_cast<unsigned short>(
static_cast<unsigned int>(v) > SCHAR_MAX ? 0 : v);
595 template <>
inline unsigned short vpMath::saturate<unsigned short>(
short v)
597 return static_cast<unsigned short>(std::max<int>(
static_cast<int>(v), 0));
600 template <>
inline unsigned short vpMath::saturate<unsigned short>(
int v)
602 return static_cast<unsigned short>(
static_cast<unsigned int>(v) <=
static_cast<unsigned int>(USHRT_MAX) ? v : v > 0 ? USHRT_MAX : 0);
605 template <>
inline unsigned short vpMath::saturate<unsigned short>(
unsigned int v)
607 return static_cast<unsigned short>(std::min<unsigned int>(v,
static_cast<unsigned int>(USHRT_MAX)));
610 template <>
inline unsigned short vpMath::saturate<unsigned short>(
float v)
613 return vpMath::saturate<unsigned short>(iv);
616 template <>
inline unsigned short vpMath::saturate<unsigned short>(
double v)
619 return vpMath::saturate<unsigned short>(iv);
623 template <>
inline short vpMath::saturate<short>(
unsigned short v)
625 return static_cast<short>(std::min<int>(
static_cast<int>(v), SHRT_MAX));
627 template <>
inline short vpMath::saturate<short>(
int v)
629 return static_cast<short>(
static_cast<unsigned int>(v - SHRT_MIN) <=
static_cast<unsigned int>(USHRT_MAX) ? v : v > 0 ? SHRT_MAX : SHRT_MIN);
631 template <>
inline short vpMath::saturate<short>(
unsigned int v)
633 return static_cast<short>(std::min<unsigned int>(v,
static_cast<unsigned int>(SHRT_MAX)));
635 template <>
inline short vpMath::saturate<short>(
float v)
638 return vpMath::saturate<short>(iv);
640 template <>
inline short vpMath::saturate<short>(
double v)
643 return vpMath::saturate<short>(iv);
647 template <>
inline int vpMath::saturate<int>(
float v)
652 template <>
inline int vpMath::saturate<int>(
double v)
660 template <>
inline unsigned int vpMath::saturate<unsigned int>(
float v)
662 return static_cast<unsigned int>(
vpMath::round(
static_cast<double>(v)));
665 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.