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;
179 float quotient = std::floor(value /
modulo);
180 float rest = value - (quotient *
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);
219 template <
typename T>
static inline T
clamp(
const T &v,
const T &lower,
const T &upper)
224#if ((__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
225 return std::clamp(v, lower, upper);
230 return (v < lower) ? lower : (upper < v) ? upper : v;
235 static inline int round(
double x);
238 static inline int sign(
double x);
241 static inline bool nul(
double x,
double threshold = 0.001);
242 static inline bool nul(
float x,
float threshold = 0.001f);
245 static inline bool equal(
double x,
double y,
double threshold = 0.001);
246 static inline bool equal(
float x,
float y,
float threshold = 0.001f);
249 static inline bool greater(
double x,
double y,
double threshold = 0.001);
257 template <
class Type>
static Type
maximum(
const Type &a,
const Type &b) {
return (a > b) ? a : b; }
265 template <
class Type>
static Type
minimum(
const Type &a,
const Type &b) {
return (a < b) ? a : b; }
272 template <
class Type>
static Type
abs(
const Type &x) {
return (x < 0) ? -x : x; }
275 static double sinc(
double x);
276 static double sinc(
double sinx,
double x);
277 static double mcosc(
double cosx,
double x);
278 static double msinc(
double sinx,
double x);
281 static inline double sigmoid(
double x,
double x0 = 0.,
double x1 = 1.,
double n = 12.);
289 template <
class Type>
static void swap(Type &a, Type &b)
296 static bool isNaN(
double value);
297 static bool isNaN(
float value);
298 static bool isInf(
double value);
299 static bool isInf(
float value);
300 static bool isFinite(
double value);
301 static bool isFinite(
float value);
302 static bool isNumber(
const std::string &str);
304 static double lineFitting(
const std::vector<vpImagePoint> &imPts,
double &a,
double &b,
double &c);
306 template <
typename Tp>
static inline Tp
saturate(
unsigned char v) {
return Tp(v); }
307 template <
typename Tp>
static inline Tp
saturate(
char v) {
return Tp(v); }
308 template <
typename Tp>
static inline Tp
saturate(
unsigned short v) {
return Tp(v); }
309 template <
typename Tp>
static inline Tp
saturate(
short v) {
return Tp(v); }
310 template <
typename Tp>
static inline Tp
saturate(
unsigned v) {
return Tp(v); }
311 template <
typename Tp>
static inline Tp
saturate(
int v) {
return Tp(v); }
312 template <
typename Tp>
static inline Tp
saturate(
float v) {
return Tp(v); }
313 template <
typename Tp>
static inline Tp
saturate(
double v) {
return Tp(v); }
315 static double getMean(
const std::vector<double> &v);
316 static double getMedian(
const std::vector<double> &v);
317 static double getStdev(
const std::vector<double> &v,
bool useBesselCorrection =
false);
319 static int modulo(
int a,
int n);
320 static unsigned int modulo(
unsigned int a,
unsigned int n);
336 template <
typename T>
static std::vector<double>
linspace(T start_in, T end_in,
unsigned int num_in)
338 std::vector<double> linspaced;
340 double start =
static_cast<double>(start_in);
341 double end =
static_cast<double>(end_in);
342 double num =
static_cast<double>(num_in);
344 if (std::fabs(num) < std::numeric_limits<double>::epsilon()) {
347 if (std::fabs(num - 1) < std::numeric_limits<double>::epsilon()) {
348 linspaced.push_back(start);
352 double delta = (end - start) / (num - 1);
354 for (
int i = 0; i < (num - 1); ++i) {
355 linspaced.push_back(start + (delta * i));
357 linspaced.push_back(end);
362 static std::vector<std::pair<double, double> > computeRegularPointsOnSphere(
unsigned int maxPoints);
365 static std::vector<vpHomogeneousMatrix>
372 static const double ang_min_sinc;
373 static const double ang_min_mc;
384 if ((x == 1) || (x == 0)) {
387 return x *
fact(x - 1);
415#if defined(VISP_HAVE_FUNC_STD_ROUND)
416 return static_cast<int>(std::round(x));
417#elif defined(VISP_HAVE_FUNC_ROUND)
420 return static_cast<int>(
::round(x));
422 return (x > 0.0) ? (
static_cast<int>(floor(x + 0.5))) : (
static_cast<int>(ceil(x - 0.5)));
434 if (fabs(x) < std::numeric_limits<double>::epsilon()) {
453bool vpMath::nul(
double x,
double threshold) {
return (fabs(x) < threshold); }
461bool vpMath::nul(
float x,
float threshold) {
return (fabs(x) < threshold); }
470bool vpMath::equal(
double x,
double y,
double threshold) {
return (
nul(x - y, threshold)); }
488bool vpMath::greater(
double x,
double y,
double threshold) {
return (x > (y - threshold)); }
509 double l0 = 1. / (1. + exp(0.5 * n));
510 double l1 = 1. / (1. + exp(-0.5 * n));
511 return ((1. / (1. + exp(-n * (((x - x0) / (x1 - x0)) - 0.5)))) - l0) / (l1 - l0);
522 if (std::numeric_limits<char>::is_signed) {
523 return static_cast<unsigned char>(std::max<int>(
static_cast<int>(v), 0));
526 return static_cast<unsigned char>(
static_cast<unsigned int>(v) > SCHAR_MAX ? 0 : v);
532 return static_cast<unsigned char>(std::min<unsigned int>(
static_cast<unsigned int>(v),
static_cast<unsigned int>(UCHAR_MAX)));
537 return static_cast<unsigned char>(
static_cast<unsigned int>(v) <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0);
547 return static_cast<unsigned char>(std::min<unsigned int>(v,
static_cast<unsigned int>(UCHAR_MAX)));
565 return static_cast<char>(std::min<int>(
static_cast<int>(v), SCHAR_MAX));
570 return static_cast<char>(std::min<unsigned int>(
static_cast<unsigned int>(v),
static_cast<unsigned int>(SCHAR_MAX)));
575 return static_cast<char>(
static_cast<unsigned int>(v - SCHAR_MIN) <=
static_cast<unsigned int>(UCHAR_MAX) ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN);
585 return static_cast<char>(std::min<unsigned int>(v,
static_cast<unsigned int>(SCHAR_MAX)));
608 if (std::numeric_limits<char>::is_signed) {
609 return static_cast<unsigned short>(std::max<int>(
static_cast<int>(v), 0));
612 return static_cast<unsigned short>(
static_cast<unsigned int>(v) > SCHAR_MAX ? 0 : v);
618 return static_cast<unsigned short>(std::max<int>(
static_cast<int>(v), 0));
623 return static_cast<unsigned short>(
static_cast<unsigned int>(v) <=
static_cast<unsigned int>(USHRT_MAX) ? v : v > 0 ? USHRT_MAX : 0);
628 return static_cast<unsigned short>(std::min<unsigned int>(v,
static_cast<unsigned int>(USHRT_MAX)));
646 return static_cast<short>(std::min<int>(
static_cast<int>(v), SHRT_MAX));
650 return static_cast<short>(
static_cast<unsigned int>(v - SHRT_MIN) <=
static_cast<unsigned int>(USHRT_MAX) ? v : v > 0 ? SHRT_MAX : SHRT_MIN);
654 return static_cast<short>(std::min<unsigned int>(v,
static_cast<unsigned int>(SHRT_MAX)));
683 return static_cast<unsigned int>(
vpMath::round(
static_cast<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 std::vector< double > linspace(T start_in, T end_in, unsigned int num_in)
static std::vector< vpHomogeneousMatrix > getLocalTangentPlaneTransformations(const std::vector< std::pair< double, double > > &lonlatVec, double radius, LongLattToHomogeneous func)
static double sqr(double x)
static vpHomogeneousMatrix lookAt(const vpColVector &from, const vpColVector &to, vpColVector tmp)
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)
vpHomogeneousMatrix(* LongLattToHomogeneous)(double lonDeg, double latDeg, double radius)
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)
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.