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)
80 #include <visp3/core/vpException.h>
81 #include <visp3/core/vpImagePoint.h>
106 static inline double deg(
double rad) {
return (rad * 180.0) / M_PI; }
116 static inline double rad(
double deg) {
return (deg * M_PI) / 180.0; }
124 static inline double sqr(
double x) {
return x * x; }
127 static inline double fact(
unsigned int x);
130 static inline long double comb(
unsigned int n,
unsigned int p);
139 template <
typename T>
static inline T
clamp(
const T &v,
const T &lower,
const T &upper)
141 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_17)
142 return std::clamp(v, lower, upper);
147 return (v < lower) ? lower : (upper < v) ? upper : v;
152 static inline int round(
double x);
155 static inline int sign(
double x);
158 static inline bool nul(
double x,
double threshold = 0.001);
161 static inline bool equal(
double x,
double y,
double threshold = 0.001);
164 static inline bool greater(
double x,
double y,
double threshold = 0.001);
172 template <
class Type>
static Type
maximum(
const Type &a,
const Type &b) {
return (a > b) ? a : b; }
180 template <
class Type>
static Type
minimum(
const Type &a,
const Type &b) {
return (a < b) ? a : b; }
187 template <
class Type>
static Type
abs(
const Type &x) {
return (x < 0) ? -x : x; }
190 static double sinc(
double x);
191 static double sinc(
double sinx,
double x);
192 static double mcosc(
double cosx,
double x);
193 static double msinc(
double sinx,
double x);
196 static inline double sigmoid(
double x,
double x0 = 0.,
double x1 = 1.,
double n = 12.);
204 template <
class Type>
static void swap(Type &a, Type &b)
211 static bool isNaN(
double value);
212 static bool isNaN(
float value);
213 static bool isInf(
double value);
214 static bool isInf(
float value);
215 static bool isFinite(
double value);
216 static bool isFinite(
float value);
217 static bool isNumber(
const std::string &str);
219 static double lineFitting(
const std::vector<vpImagePoint> &imPts,
double &a,
double &b,
double &c);
221 template <
typename _Tp>
static inline _Tp
saturate(
unsigned char v) {
return _Tp(v); }
222 template <
typename _Tp>
static inline _Tp
saturate(
char v) {
return _Tp(v); }
223 template <
typename _Tp>
static inline _Tp
saturate(
unsigned short v) {
return _Tp(v); }
224 template <
typename _Tp>
static inline _Tp
saturate(
short v) {
return _Tp(v); }
225 template <
typename _Tp>
static inline _Tp
saturate(
unsigned v) {
return _Tp(v); }
226 template <
typename _Tp>
static inline _Tp
saturate(
int v) {
return _Tp(v); }
227 template <
typename _Tp>
static inline _Tp
saturate(
float v) {
return _Tp(v); }
228 template <
typename _Tp>
static inline _Tp
saturate(
double v) {
return _Tp(v); }
230 static double getMean(
const std::vector<double> &v);
231 static double getMedian(
const std::vector<double> &v);
232 static double getStdev(
const std::vector<double> &v,
bool useBesselCorrection =
false);
234 static int modulo(
int a,
int n);
250 template <
typename T>
static std::vector<double>
linspace(T start_in, T end_in,
unsigned int num_in)
252 std::vector<double> linspaced;
254 double start =
static_cast<double>(start_in);
255 double end =
static_cast<double>(end_in);
256 double num =
static_cast<double>(num_in);
258 if (std::fabs(num) < std::numeric_limits<double>::epsilon()) {
261 if (std::fabs(num - 1) < std::numeric_limits<double>::epsilon()) {
262 linspaced.push_back(start);
266 double delta = (end - start) / (num - 1);
268 for (
int i = 0; i < num - 1; i++) {
269 linspaced.push_back(start + delta * i);
271 linspaced.push_back(end);
276 static std::vector<std::pair<double, double> > computeRegularPointsOnSphere(
unsigned int maxPoints);
277 static std::vector<vpHomogeneousMatrix>
278 getLocalTangentPlaneTransformations(
const std::vector<std::pair<double, double> > &lonlatVec,
double radius,
284 static const double ang_min_sinc;
285 static const double ang_min_mc;
296 if ((x == 1) || (x == 0))
298 return x *
fact(x - 1);
325 #if defined(VISP_HAVE_FUNC_STD_ROUND)
326 return (
int)std::round(x);
327 #elif defined(VISP_HAVE_FUNC_ROUND)
332 return (x > 0.0) ? ((int)floor(x + 0.5)) : ((
int)ceil(x - 0.5));
344 if (fabs(x) < std::numeric_limits<double>::epsilon())
360 bool vpMath::nul(
double x,
double threshold) {
return (fabs(x) < threshold); }
369 bool vpMath::equal(
double x,
double y,
double threshold) {
return (
nul(x - y, threshold)); }
378 bool vpMath::greater(
double x,
double y,
double threshold) {
return (x > (y - threshold)); }
397 double l0 = 1. / (1. + exp(0.5 * n));
398 double l1 = 1. / (1. + exp(-0.5 * n));
399 return (1. / (1. + exp(-n * ((x - x0) / (x1 - x0) - 0.5))) - l0) / (l1 - l0);
403 template <>
inline unsigned char vpMath::saturate<unsigned char>(
char v)
410 if (std::numeric_limits<char>::is_signed)
411 return (
unsigned char)(((std::max))((int)v, 0));
413 return (
unsigned char)((
unsigned int)v > SCHAR_MAX ? 0 : v);
416 template <>
inline unsigned char vpMath::saturate<unsigned char>(
unsigned short v)
418 return (
unsigned char)((std::min))((
unsigned int)v, (
unsigned int)UCHAR_MAX);
421 template <>
inline unsigned char vpMath::saturate<unsigned char>(
int v)
423 return (
unsigned char)((
unsigned int)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0);
426 template <>
inline unsigned char vpMath::saturate<unsigned char>(
short v) {
return saturate<unsigned char>((
int)v); }
428 template <>
inline unsigned char vpMath::saturate<unsigned char>(
unsigned int v)
430 return (
unsigned char)((std::min))(v, (
unsigned int)UCHAR_MAX);
433 template <>
inline unsigned char vpMath::saturate<unsigned char>(
float v)
436 return saturate<unsigned char>(iv);
439 template <>
inline unsigned char vpMath::saturate<unsigned char>(
double v)
442 return saturate<unsigned char>(iv);
446 template <>
inline char vpMath::saturate<char>(
unsigned char v) {
return (
char)((std::min))((int)v, SCHAR_MAX); }
448 template <>
inline char vpMath::saturate<char>(
unsigned short v)
450 return (
char)((std::min))((
unsigned int)v, (
unsigned int)SCHAR_MAX);
453 template <>
inline char vpMath::saturate<char>(
int v)
455 return (
char)((
unsigned int)(v - SCHAR_MIN) <= (
unsigned int)UCHAR_MAX ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN);
458 template <>
inline char vpMath::saturate<char>(
short v) {
return saturate<char>((
int)v); }
460 template <>
inline char vpMath::saturate<char>(
unsigned int v)
462 return (
char)((std::min))(v, (
unsigned int)SCHAR_MAX);
465 template <>
inline char vpMath::saturate<char>(
float v)
468 return saturate<char>(iv);
471 template <>
inline char vpMath::saturate<char>(
double v)
474 return saturate<char>(iv);
478 template <>
inline unsigned short vpMath::saturate<unsigned short>(
char v)
485 if (std::numeric_limits<char>::is_signed)
486 return (
unsigned char)(((std::max))((int)v, 0));
488 return (
unsigned char)((
unsigned int)v > SCHAR_MAX ? 0 : v);
491 template <>
inline unsigned short vpMath::saturate<unsigned short>(
short v)
493 return (
unsigned short)((std::max))((int)v, 0);
496 template <>
inline unsigned short vpMath::saturate<unsigned short>(
int v)
498 return (
unsigned short)((
unsigned int)v <= (
unsigned int)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0);
501 template <>
inline unsigned short vpMath::saturate<unsigned short>(
unsigned int v)
503 return (
unsigned short)((std::min))(v, (
unsigned int)USHRT_MAX);
506 template <>
inline unsigned short vpMath::saturate<unsigned short>(
float v)
509 return vpMath::saturate<unsigned short>(iv);
512 template <>
inline unsigned short vpMath::saturate<unsigned short>(
double v)
515 return vpMath::saturate<unsigned short>(iv);
519 template <>
inline short vpMath::saturate<short>(
unsigned short v) {
return (
short)((std::min))((int)v, SHRT_MAX); }
520 template <>
inline short vpMath::saturate<short>(
int v)
522 return (
short)((
unsigned int)(v - SHRT_MIN) <= (
unsigned int)USHRT_MAX ? v : v > 0 ? SHRT_MAX : SHRT_MIN);
524 template <>
inline short vpMath::saturate<short>(
unsigned int v)
526 return (
short)((std::min))(v, (
unsigned int)SHRT_MAX);
528 template <>
inline short vpMath::saturate<short>(
float v)
531 return vpMath::saturate<short>(iv);
533 template <>
inline short vpMath::saturate<short>(
double v)
536 return vpMath::saturate<short>(iv);
540 template <>
inline int vpMath::saturate<int>(
float v) {
return vpMath::round(v); }
542 template <>
inline int vpMath::saturate<int>(
double v) {
return vpMath::round(v); }
547 template <>
inline unsigned int vpMath::saturate<unsigned int>(
float v) {
return (
unsigned int)
vpMath::round(v); }
549 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 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(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 _Tp saturate(unsigned short v)
static bool greater(double x, double y, double threshold=0.001)
static Type abs(const Type &x)
static _Tp saturate(float v)
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 _Tp saturate(unsigned v)
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 _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 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.