50 #include <visp3/core/vpConfig.h>
59 #define _USE_MATH_DEFINES
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)
72 #define M_PI 3.14159265358979323846
76 #define M_PI_2 (M_PI / 2.0)
80 #define M_PI_4 (M_PI / 4.0)
85 #include <visp3/core/vpException.h>
86 #include <visp3/core/vpImagePoint.h>
111 static inline double deg(
double rad) {
return (rad * 180.0) / M_PI; }
121 static inline double rad(
double deg) {
return (deg * M_PI) / 180.0; }
127 static inline double sqr(
double x) {
return x * x; }
130 static inline double fact(
unsigned int x);
133 static inline long double comb(
unsigned int n,
unsigned int p);
142 template <
typename T>
static inline T
clamp(
const T &v,
const T &lower,
const T &upper)
144 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_17)
145 return std::clamp(v, lower, upper);
150 return (v < lower) ? lower : (upper < v) ? upper : v;
155 static inline int round(
double x);
158 static inline int sign(
double x);
161 static inline bool nul(
double x,
double s = 0.001);
164 static inline bool equal(
double x,
double y,
double s = 0.001);
167 static inline bool greater(
double x,
double y,
double s = 0.001);
175 template <
class Type>
static Type
maximum(
const Type &a,
const Type &b) {
return (a > b) ? a : b; }
183 template <
class Type>
static Type
minimum(
const Type &a,
const Type &b) {
return (a < b) ? a : b; }
190 template <
class Type>
static Type
abs(
const Type &x) {
return (x < 0) ? -x : x; }
193 static double sinc(
double x);
194 static double sinc(
double sinx,
double x);
195 static double mcosc(
double cosx,
double x);
196 static double msinc(
double sinx,
double x);
199 static inline double sigmoid(
double x,
double x0 = 0.,
double x1 = 1.,
double n = 12.);
207 template <
class Type>
static void swap(Type &a, Type &b)
214 static bool isNaN(
double value);
215 static bool isNaN(
float value);
216 static bool isInf(
double value);
217 static bool isInf(
float value);
218 static bool isFinite(
double value);
219 static bool isFinite(
float value);
221 static double lineFitting(
const std::vector<vpImagePoint> &imPts,
double &a,
double &b,
double &c);
223 template <
typename _Tp>
static inline _Tp
saturate(
unsigned char v) {
return _Tp(v); }
224 template <
typename _Tp>
static inline _Tp
saturate(
char v) {
return _Tp(v); }
225 template <
typename _Tp>
static inline _Tp
saturate(
unsigned short v) {
return _Tp(v); }
226 template <
typename _Tp>
static inline _Tp
saturate(
short v) {
return _Tp(v); }
227 template <
typename _Tp>
static inline _Tp
saturate(
unsigned v) {
return _Tp(v); }
228 template <
typename _Tp>
static inline _Tp
saturate(
int v) {
return _Tp(v); }
229 template <
typename _Tp>
static inline _Tp
saturate(
float v) {
return _Tp(v); }
230 template <
typename _Tp>
static inline _Tp
saturate(
double v) {
return _Tp(v); }
232 static double getMean(
const std::vector<double> &v);
233 static double getMedian(
const std::vector<double> &v);
234 static double getStdev(
const std::vector<double> &v,
bool useBesselCorrection =
false);
236 static int modulo(
int a,
int n);
253 template <
typename T>
static std::vector<double>
linspace(T start_in, T end_in,
unsigned int num_in)
255 std::vector<double> linspaced;
257 double start =
static_cast<double>(start_in);
258 double end =
static_cast<double>(end_in);
259 double num =
static_cast<double>(num_in);
261 if (std::fabs(num) < std::numeric_limits<double>::epsilon()) {
264 if (std::fabs(num - 1) < std::numeric_limits<double>::epsilon()) {
265 linspaced.push_back(start);
269 double delta = (end - start) / (num - 1);
271 for (
int i = 0; i < num - 1; i++) {
272 linspaced.push_back(start + delta * i);
274 linspaced.push_back(end);
279 static std::vector<std::pair<double, double> > computeRegularPointsOnSphere(
unsigned int maxPoints);
280 static std::vector<vpHomogeneousMatrix>
281 getLocalTangentPlaneTransformations(
const std::vector<std::pair<double, double> > &lonlatVec,
double radius,
287 static const double ang_min_sinc;
288 static const double ang_min_mc;
299 if ((x == 1) || (x == 0))
301 return x *
fact(x - 1);
328 #if defined(VISP_HAVE_FUNC_ROUND)
332 #elif defined(VISP_HAVE_FUNC_STD_ROUND)
333 return (
int)std::round(x);
335 return (x > 0.0) ? ((int)floor(x + 0.5)) : ((
int)ceil(x - 0.5));
347 if (fabs(x) < std::numeric_limits<double>::epsilon())
400 double l0 = 1. / (1. + exp(0.5 * n));
401 double l1 = 1. / (1. + exp(-0.5 * n));
402 return (1. / (1. + exp(-n * ((x - x0) / (x1 - x0) - 0.5))) - l0) / (l1 - l0);
406 template <>
inline unsigned char vpMath::saturate<unsigned char>(
char v)
413 if (std::numeric_limits<char>::is_signed)
414 return (
unsigned char)(((std::max))((int)v, 0));
416 return (
unsigned char)((
unsigned int)v > SCHAR_MAX ? 0 : v);
419 template <>
inline unsigned char vpMath::saturate<unsigned char>(
unsigned short v)
421 return (
unsigned char)((std::min))((
unsigned int)v, (
unsigned int)UCHAR_MAX);
424 template <>
inline unsigned char vpMath::saturate<unsigned char>(
int v)
426 return (
unsigned char)((
unsigned int)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0);
429 template <>
inline unsigned char vpMath::saturate<unsigned char>(
short v) {
return saturate<unsigned char>((
int)v); }
431 template <>
inline unsigned char vpMath::saturate<unsigned char>(
unsigned int v)
433 return (
unsigned char)((std::min))(v, (
unsigned int)UCHAR_MAX);
436 template <>
inline unsigned char vpMath::saturate<unsigned char>(
float v)
439 return saturate<unsigned char>(iv);
442 template <>
inline unsigned char vpMath::saturate<unsigned char>(
double v)
445 return saturate<unsigned char>(iv);
449 template <>
inline char vpMath::saturate<char>(
unsigned char v) {
return (
char)((std::min))((int)v, SCHAR_MAX); }
451 template <>
inline char vpMath::saturate<char>(
unsigned short v)
453 return (
char)((std::min))((
unsigned int)v, (
unsigned int)SCHAR_MAX);
456 template <>
inline char vpMath::saturate<char>(
int v)
458 return (
char)((
unsigned int)(v - SCHAR_MIN) <= (
unsigned int)UCHAR_MAX ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN);
461 template <>
inline char vpMath::saturate<char>(
short v) {
return saturate<char>((
int)v); }
463 template <>
inline char vpMath::saturate<char>(
unsigned int v)
465 return (
char)((std::min))(v, (
unsigned int)SCHAR_MAX);
468 template <>
inline char vpMath::saturate<char>(
float v)
471 return saturate<char>(iv);
474 template <>
inline char vpMath::saturate<char>(
double v)
477 return saturate<char>(iv);
481 template <>
inline unsigned short vpMath::saturate<unsigned short>(
char v)
488 if (std::numeric_limits<char>::is_signed)
489 return (
unsigned char)(((std::max))((int)v, 0));
491 return (
unsigned char)((
unsigned int)v > SCHAR_MAX ? 0 : v);
494 template <>
inline unsigned short vpMath::saturate<unsigned short>(
short v)
496 return (
unsigned short)((std::max))((int)v, 0);
499 template <>
inline unsigned short vpMath::saturate<unsigned short>(
int v)
501 return (
unsigned short)((
unsigned int)v <= (
unsigned int)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0);
504 template <>
inline unsigned short vpMath::saturate<unsigned short>(
unsigned int v)
506 return (
unsigned short)((std::min))(v, (
unsigned int)USHRT_MAX);
509 template <>
inline unsigned short vpMath::saturate<unsigned short>(
float v)
512 return vpMath::saturate<unsigned short>(iv);
515 template <>
inline unsigned short vpMath::saturate<unsigned short>(
double v)
518 return vpMath::saturate<unsigned short>(iv);
522 template <>
inline short vpMath::saturate<short>(
unsigned short v) {
return (
short)((std::min))((int)v, SHRT_MAX); }
523 template <>
inline short vpMath::saturate<short>(
int v)
525 return (
short)((
unsigned int)(v - SHRT_MIN) <= (
unsigned int)USHRT_MAX ? v : v > 0 ? SHRT_MAX : SHRT_MIN);
527 template <>
inline short vpMath::saturate<short>(
unsigned int v)
529 return (
short)((std::min))(v, (
unsigned int)SHRT_MAX);
531 template <>
inline short vpMath::saturate<short>(
float v)
534 return vpMath::saturate<short>(iv);
536 template <>
inline short vpMath::saturate<short>(
double v)
539 return vpMath::saturate<short>(iv);
543 template <>
inline int vpMath::saturate<int>(
float v) {
return vpMath::round(v); }
545 template <>
inline int vpMath::saturate<int>(
double v) {
return vpMath::round(v); }
550 template <>
inline unsigned int vpMath::saturate<unsigned int>(
float v) {
return (
unsigned int)
vpMath::round(v); }
552 template <>
inline unsigned int vpMath::saturate<unsigned int>(
double v) {
return (
unsigned int)
vpMath::round(v); }
Implementation of column vector and the associated operations.
error that can be emited 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(char v)
static void swap(Type &a, Type &b)
static double fact(unsigned int x)
static double rad(double deg)
static Type maximum(const Type &a, const Type &b)
static _Tp saturate(short v)
static _Tp saturate(double v)
static double sqr(double x)
static bool equal(double x, double y, double s=0.001)
static _Tp saturate(unsigned short v)
static Type abs(const Type &x)
static _Tp saturate(float v)
static double sigmoid(double x, double x0=0., double x1=1., double n=12.)
static bool nul(double x, double s=0.001)
static _Tp saturate(unsigned v)
static T clamp(const T &v, const T &lower, const T &upper)
static int round(double x)
static _Tp saturate(int v)
static Type minimum(const Type &a, const Type &b)
static int sign(double x)
static long double comb(unsigned int n, unsigned int p)
static double deg(double rad)
static _Tp saturate(unsigned char v)
static bool greater(double x, double y, double s=0.001)
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.