#include <cfloat>
#include <iostream>
#include <limits>
#include <visp3/core/vpMath.h>
#include <visp3/core/vpColVector.h>
#include <visp3/core/vpRxyzVector.h>
#include <visp3/core/vpHomogeneousMatrix.h>
#if defined _MSC_VER && _MSC_VER >= 1200
#pragma warning(disable : 4723)
#endif
#ifdef _WIN32
#ifndef NAN
static const unsigned long __nan[2] = { 0xffffffff, 0x7fffffff };
#define NAN (*(const float *)__nan)
#endif
#endif
int main()
{
#ifdef ENABLE_VISP_NAMESPACE
#endif
std::cerr <<
"Fail: IsNaN(0.0)=" <<
vpMath::isNaN(0.0) <<
" / should be false" << std::endl;
return EXIT_FAILURE;
}
double num = 1.0, den = 0.0;
std::cerr <<
"Fail: IsNaN(1.0/0.0)=" <<
vpMath::isNaN(num / den) <<
" / should be false" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: IsNaN(NAN)=" <<
vpMath::isNaN(NAN) <<
" / should be true" << std::endl;
return EXIT_FAILURE;
}
num = 0.0;
std::cerr <<
"Fail: IsNaN(0.0/0.0)=" <<
vpMath::isNaN(num / den) <<
" / should be true" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: IsNaN(quiet_NaN)=" <<
vpMath::isNaN(std::numeric_limits<double>::quiet_NaN())
<< " / should be true" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: IsNaN(quiet_NaN)=" <<
vpMath::isNaN(std::numeric_limits<float>::quiet_NaN())
<< " / should be true" << std::endl;
return EXIT_FAILURE;
}
if (!
vpMath::isNaN(std::numeric_limits<double>::signaling_NaN())) {
std::cerr <<
"Fail: IsNaN(signaling_NaN)=" <<
vpMath::isNaN(std::numeric_limits<double>::signaling_NaN())
<< " / should be true" << std::endl;
return EXIT_FAILURE;
}
if (!
vpMath::isNaN(std::numeric_limits<float>::signaling_NaN())) {
std::cerr <<
"Fail: IsNaN(signaling_NaN)=" <<
vpMath::isNaN(std::numeric_limits<float>::signaling_NaN())
<< " / should be true" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: IsNaN(infinity)=" <<
vpMath::isNaN(std::numeric_limits<double>::infinity())
<< " / should be false" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: IsNaN(infinity)=" <<
vpMath::isNaN(std::numeric_limits<float>::infinity())
<< " / should be false" << std::endl;
return EXIT_FAILURE;
}
if (
vpMath::isNaN(1.0 / std::numeric_limits<double>::epsilon())) {
std::cerr <<
"Fail: IsNaN(1.0/epsilon)=" <<
vpMath::isNaN(1.0 / std::numeric_limits<double>::epsilon())
<< " / should be false" << std::endl;
return EXIT_FAILURE;
}
if (!
vpMath::isNaN(std::numeric_limits<double>::infinity() - std::numeric_limits<double>::infinity())) {
std::cerr << "Fail: IsNaN(infinity - infinity)="
<<
vpMath::isNaN(std::numeric_limits<double>::infinity() - std::numeric_limits<double>::infinity())
<< " / should be true" << std::endl;
return EXIT_FAILURE;
}
float a = 0.0f, b = 0.0f;
std::cerr <<
"Fail: IsNaN(0.0f/0.0f)=" <<
vpMath::isNaN(a / b) <<
" / should be true" << std::endl;
return EXIT_FAILURE;
}
std::cout << "vpMath::isNaN is Ok !" << std::endl;
std::cerr <<
"Fail: vpMath::isInf(NAN)=" <<
vpMath::isInf(NAN) <<
" / should be false" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: vpMath::isInf(1.0/0.0)=" <<
vpMath::isInf(1.0 / a) <<
" / should be true" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: vpMath::isInf(0.0)=" <<
vpMath::isInf(0.0) <<
" / should be false" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: vpMath::isInf(exp(800.))=" <<
vpMath::isInf(exp(800.)) <<
" / should be true" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: vpMath::isInf(DBL_MIN/2.0)=" <<
vpMath::isInf(DBL_MIN / 2.0) <<
" / should be false"
<< std::endl;
return EXIT_FAILURE;
}
std::cout << "vpMath::isInf is Ok !" << std::endl;
std::cerr <<
"Fail: vpMath::isFinite(NAN)=" <<
vpMath::isFinite(NAN) <<
" / should be false" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: vpMath::isFinite(1.0/0.0)=" <<
vpMath::isFinite(1.0 / a) <<
" / should be false" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: vpMath::isFinite(0.0)=" <<
vpMath::isFinite(0.0) <<
" / should be true" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: vpMath::isFinite(exp(800.))=" <<
vpMath::isFinite(exp(800.)) <<
" / should be false" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: vpMath::isFinite(DBL_MIN/2.0)=" <<
vpMath::isFinite(DBL_MIN / 2.0) <<
" / should be true"
<< std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: vpMath::isFinite(DBL_MAX)=" <<
vpMath::isFinite(std::numeric_limits<float>::max()) <<
" / should be true"
<< std::endl;
return EXIT_FAILURE;
}
std::cout << "vpMath::isFinite is Ok !" << std::endl;
std::cerr <<
"Fail: vpMath::isNumber(\"123\")=" <<
vpMath::isNumber(
"123") <<
" / should be false" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: vpMath::isNumber(\"string\")=" <<
vpMath::isNumber(
"string") <<
" / should be true" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: vpMath::isNumber(\"123string\")=" <<
vpMath::isNumber(
"123string") <<
" / should be true" << std::endl;
return EXIT_FAILURE;
}
std::cout << "vpMath::isNumber is Ok !" << std::endl;
std::cerr <<
"Fail: vpMath::round(2.3)=" <<
vpMath::round(2.3) <<
" / should be 2" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: vpMath::round(3.8)=" <<
vpMath::round(3.8) <<
" / should be 4" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: vpMath::round(5.5)=" <<
vpMath::round(5.5) <<
" / should be 6" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: vpMath::round(-2.3)=" <<
vpMath::round(-2.3) <<
" / should be -2" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: vpMath::round(-3.8)=" <<
vpMath::round(-3.8) <<
" / should be -4" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: vpMath::round(-5.5)=" <<
vpMath::round(-5.5) <<
" / should be -6" << std::endl;
return EXIT_FAILURE;
}
std::cerr <<
"Fail: vpMath::round(0.0)=" <<
vpMath::round(0.0) <<
" / should be 0" << std::endl;
return EXIT_FAILURE;
}
std::cout << "vpMath::round is Ok !" << std::endl;
char char_value = -127;
unsigned char uchar_value = vpMath::saturate<unsigned char>(char_value);
if (uchar_value != 0) {
std::cerr << "Fail: vpMath::saturate<unsigned char>(-127)=" << uchar_value << " / should be 0" << std::endl;
return EXIT_FAILURE;
}
unsigned short ushort_value = 60000;
uchar_value = vpMath::saturate<unsigned char>(ushort_value);
if (uchar_value != UCHAR_MAX) {
std::cerr << "Fail: vpMath::saturate<unsigned char>(60000)=" << uchar_value << " / should be " << UCHAR_MAX
<< std::endl;
return EXIT_FAILURE;
}
int int_value = 70000;
uchar_value = vpMath::saturate<unsigned char>(int_value);
if (uchar_value != UCHAR_MAX) {
std::cerr << "Fail: vpMath::saturate<unsigned char>(70000)=" << uchar_value << " / should be " << UCHAR_MAX
<< std::endl;
return EXIT_FAILURE;
}
int_value = -70000;
uchar_value = vpMath::saturate<unsigned char>(int_value);
if (uchar_value != 0) {
std::cerr << "Fail: vpMath::saturate<unsigned char>(-70000)=" << uchar_value << " / should be 0" << std::endl;
return EXIT_FAILURE;
}
short short_value = 30000;
uchar_value = vpMath::saturate<unsigned char>(short_value);
if (uchar_value != UCHAR_MAX) {
std::cerr << "Fail: vpMath::saturate<unsigned char>(30000)=" << uchar_value << " / should be " << UCHAR_MAX
<< std::endl;
return EXIT_FAILURE;
}
short_value = -30000;
uchar_value = vpMath::saturate<unsigned char>(short_value);
if (uchar_value != 0) {
std::cerr << "Fail: vpMath::saturate<unsigned char>(-30000)=" << uchar_value << " / should be 0" << std::endl;
return EXIT_FAILURE;
}
unsigned int uint_value = 10000;
uchar_value = vpMath::saturate<unsigned char>(uint_value);
if (uchar_value != UCHAR_MAX) {
std::cerr << "Fail: vpMath::saturate<unsigned char>(10000)=" << uchar_value << " / should be " << UCHAR_MAX
<< std::endl;
return EXIT_FAILURE;
}
float float_value = 10000.1f;
uchar_value = vpMath::saturate<unsigned char>(float_value);
if (uchar_value != UCHAR_MAX) {
std::cerr << "Fail: vpMath::saturate<unsigned char>(10000.1f)=" << uchar_value << " / should be " << UCHAR_MAX
<< std::endl;
return EXIT_FAILURE;
}
float_value = -10000.1f;
uchar_value = vpMath::saturate<unsigned char>(float_value);
if (uchar_value != 0) {
std::cerr << "Fail: vpMath::saturate<unsigned char>(-10000.1f)=" << uchar_value << " / should be 0" << std::endl;
return EXIT_FAILURE;
}
double double_value = 10000.1;
uchar_value = vpMath::saturate<unsigned char>(double_value);
if (uchar_value != UCHAR_MAX) {
std::cerr << "Fail: vpMath::saturate<unsigned char>(10000.0)=" << uchar_value << " / should be " << UCHAR_MAX
<< std::endl;
return EXIT_FAILURE;
}
double_value = -10000.1;
uchar_value = vpMath::saturate<unsigned char>(double_value);
if (uchar_value != 0) {
std::cerr << "Fail: vpMath::saturate<unsigned char>(-10000.0)=" << uchar_value << " / should be 0" << std::endl;
return EXIT_FAILURE;
}
std::cout << "vpMath::saturate<unsigned char>() is Ok !" << std::endl;
uchar_value = 255;
char_value = vpMath::saturate<char>(uchar_value);
if (char_value != SCHAR_MAX) {
std::cerr << "Fail: vpMath::saturate<char>(255)=" << char_value << " / should be " << SCHAR_MAX << std::endl;
return EXIT_FAILURE;
}
ushort_value = 60000;
char_value = vpMath::saturate<char>(ushort_value);
if (char_value != SCHAR_MAX) {
std::cerr << "Fail: vpMath::saturate<char>(60000)=" << char_value << " / should be " << SCHAR_MAX << std::endl;
return EXIT_FAILURE;
}
int_value = 70000;
char_value = vpMath::saturate<char>(int_value);
if (char_value != SCHAR_MAX) {
std::cerr << "Fail: vpMath::saturate<char>(70000)=" << char_value << " / should be " << SCHAR_MAX << std::endl;
return EXIT_FAILURE;
}
int_value = -70000;
char_value = vpMath::saturate<char>(int_value);
if (char_value != (char)SCHAR_MIN) {
std::cerr << "Fail: vpMath::saturate<char>(-70000)=" << char_value << " / should be " << SCHAR_MIN << std::endl;
return EXIT_FAILURE;
}
short_value = 30000;
char_value = vpMath::saturate<char>(short_value);
if (char_value != SCHAR_MAX) {
std::cerr << "Fail: vpMath::saturate<char>(30000)=" << char_value << " / should be " << SCHAR_MAX << std::endl;
return EXIT_FAILURE;
}
short_value = -30000;
char_value = vpMath::saturate<char>(short_value);
if (char_value != (char)SCHAR_MIN) {
std::cerr << "Fail: vpMath::saturate<char>(-30000)=" << char_value << " / should be " << SCHAR_MIN << std::endl;
return EXIT_FAILURE;
}
uint_value = 10000;
char_value = vpMath::saturate<char>(uint_value);
if (char_value != SCHAR_MAX) {
std::cerr << "Fail: vpMath::saturate<char>(10000)=" << char_value << " / should be " << SCHAR_MAX << std::endl;
return EXIT_FAILURE;
}
float_value = 10000.1f;
char_value = vpMath::saturate<char>(float_value);
if (char_value != SCHAR_MAX) {
std::cerr << "Fail: vpMath::saturate<char>(10000.1f)=" << char_value << " / should be " << SCHAR_MAX << std::endl;
return EXIT_FAILURE;
}
float_value = -10000.1f;
char_value = vpMath::saturate<char>(float_value);
if (char_value != (char)SCHAR_MIN) {
std::cerr << "Fail: vpMath::saturate<char>(-10000.1f)=" << char_value << " / should be " << SCHAR_MIN << std::endl;
return EXIT_FAILURE;
}
double_value = 10000.1;
char_value = vpMath::saturate<char>(double_value);
if (char_value != SCHAR_MAX) {
std::cerr << "Fail: vpMath::saturate<char>(10000.1)=" << char_value << " / should be " << SCHAR_MAX << std::endl;
return EXIT_FAILURE;
}
double_value = -10000.1;
char_value = vpMath::saturate<char>(double_value);
if (char_value != (char)SCHAR_MIN) {
std::cerr << "Fail: vpMath::saturate<char>(-10000.1)=" << char_value << " / should be " << SCHAR_MIN << std::endl;
return EXIT_FAILURE;
}
std::cout << "vpMath::saturate<char>() is Ok !" << std::endl;
char_value = -127;
ushort_value = vpMath::saturate<unsigned short>(char_value);
if (ushort_value != 0) {
std::cerr << "Fail: vpMath::saturate<unsigned short>(-127)=" << ushort_value << " / should be 0" << std::endl;
return EXIT_FAILURE;
}
short_value = -30000;
ushort_value = vpMath::saturate<unsigned short>(short_value);
if (ushort_value != 0) {
std::cerr << "Fail: vpMath::saturate<unsigned short>(-30000)=" << ushort_value << " / should be 0" << std::endl;
return EXIT_FAILURE;
}
int_value = 70000;
ushort_value = vpMath::saturate<unsigned short>(int_value);
if (ushort_value != USHRT_MAX) {
std::cerr << "Fail: vpMath::saturate<unsigned short>(70000)=" << ushort_value << " / should be " << USHRT_MAX
<< std::endl;
return EXIT_FAILURE;
}
int_value = -70000;
ushort_value = vpMath::saturate<unsigned short>(int_value);
if (ushort_value != 0) {
std::cerr << "Fail: vpMath::saturate<unsigned short>(-70000)=" << ushort_value << " / should be 0" << std::endl;
return EXIT_FAILURE;
}
uint_value = 70000;
ushort_value = vpMath::saturate<unsigned short>(uint_value);
if (ushort_value != USHRT_MAX) {
std::cerr << "Fail: vpMath::saturate<unsigned short>(70000)=" << ushort_value << " / should be " << USHRT_MAX
<< std::endl;
return EXIT_FAILURE;
}
float_value = 70000.1f;
ushort_value = vpMath::saturate<unsigned short>(float_value);
if (ushort_value != USHRT_MAX) {
std::cerr << "Fail: vpMath::saturate<unsigned short>(70000.1f)=" << ushort_value << " / should be " << USHRT_MAX
<< std::endl;
return EXIT_FAILURE;
}
float_value = -10000.1f;
ushort_value = vpMath::saturate<unsigned short>(float_value);
if (ushort_value != 0) {
std::cerr << "Fail: vpMath::saturate<unsigned short>(-10000.1f)=" << ushort_value << " / should be 0" << std::endl;
return EXIT_FAILURE;
}
double_value = 70000.1;
ushort_value = vpMath::saturate<unsigned short>(double_value);
if (ushort_value != USHRT_MAX) {
std::cerr << "Fail: vpMath::saturate<unsigned short>(70000.1)=" << ushort_value << " / should be " << USHRT_MAX
<< std::endl;
return EXIT_FAILURE;
}
double_value = -10000.1;
ushort_value = vpMath::saturate<unsigned short>(double_value);
if (ushort_value != 0) {
std::cerr << "Fail: vpMath::saturate<unsigned short>(-10000.1)=" << ushort_value << " / should be 0" << std::endl;
return EXIT_FAILURE;
}
std::cout << "vpMath::saturate<unsigned short>() is Ok !" << std::endl;
ushort_value = 60000;
short_value = vpMath::saturate<short>(ushort_value);
if (short_value != SHRT_MAX) {
std::cerr << "Fail: vpMath::saturate<short>(60000)=" << short_value << " / should be " << SHRT_MAX << std::endl;
return EXIT_FAILURE;
}
int_value = 70000;
short_value = vpMath::saturate<short>(int_value);
if (short_value != SHRT_MAX) {
std::cerr << "Fail: vpMath::saturate<short>(70000)=" << short_value << " / should be " << SHRT_MAX << std::endl;
return EXIT_FAILURE;
}
int_value = -70000;
short_value = vpMath::saturate<short>(int_value);
if (short_value != SHRT_MIN) {
std::cerr << "Fail: vpMath::saturate<short>(-70000)=" << short_value << " / should be " << SHRT_MIN << std::endl;
return EXIT_FAILURE;
}
uint_value = 70000;
short_value = vpMath::saturate<short>(uint_value);
if (short_value != SHRT_MAX) {
std::cerr << "Fail: vpMath::saturate<short>(70000)=" << short_value << " / should be " << SHRT_MAX << std::endl;
return EXIT_FAILURE;
}
float_value = 70000.1f;
short_value = vpMath::saturate<short>(float_value);
if (short_value != SHRT_MAX) {
std::cerr << "Fail: vpMath::saturate<short>(70000.1f)=" << short_value << " / should be " << SHRT_MAX << std::endl;
return EXIT_FAILURE;
}
float_value = -70000.1f;
short_value = vpMath::saturate<short>(float_value);
if (short_value != SHRT_MIN) {
std::cerr << "Fail: vpMath::saturate<short>(-70000.1f)=" << short_value << " / should be " << SHRT_MIN << std::endl;
return EXIT_FAILURE;
}
double_value = 70000.1;
short_value = vpMath::saturate<short>(double_value);
if (short_value != SHRT_MAX) {
std::cerr << "Fail: vpMath::saturate<short>(70000.1)=" << short_value << " / should be " << SHRT_MAX << std::endl;
return EXIT_FAILURE;
}
double_value = -70000.1;
short_value = vpMath::saturate<short>(double_value);
if (short_value != SHRT_MIN) {
std::cerr << "Fail: vpMath::saturate<short>(70000.1)=" << short_value << " / should be " << SHRT_MIN << std::endl;
return EXIT_FAILURE;
}
std::cout << "vpMath::saturate<short>() is Ok !" << std::endl;
std::vector<double> vectorOfDoubles(10);
vectorOfDoubles[0] = 8.1472;
vectorOfDoubles[1] = 9.0579;
vectorOfDoubles[2] = 1.2699;
vectorOfDoubles[3] = 9.1338;
vectorOfDoubles[4] = 6.3236;
vectorOfDoubles[5] = 0.9754;
vectorOfDoubles[6] = 2.7850;
vectorOfDoubles[7] = 5.4688;
vectorOfDoubles[8] = 9.5751;
vectorOfDoubles[9] = 9.6489;
std::cerr << "Problem with vpMath::getMean()=" << res << std::endl;
return EXIT_FAILURE;
}
std::cout << "vpMath::getMean() is Ok !" << std::endl;
std::cerr << "Problem with vpMath::getStdev()=" << res << std::endl;
return EXIT_FAILURE;
}
std::cerr << "Problem with vpMath::getStdev() with Bessel correction=" << res << std::endl;
return EXIT_FAILURE;
}
std::cout << "vpMath::getStdev() is Ok !" << std::endl;
std::cerr << "Problem with vpMath::getMedian()=" << res << std::endl;
return EXIT_FAILURE;
}
vectorOfDoubles.push_back(1.5761);
std::cerr << "Problem with vpMath::getMedian()=" << res << std::endl;
return EXIT_FAILURE;
}
std::cout << "vpMath::getMedian() is Ok !" << std::endl;
#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
{
const double lower { -10. }, upper { +10. };
std::vector<double> testing_values { 5., -15., 15. };
std::vector<double> expected_values { 5., -10., 10. };
for (size_t i = 0u; i < testing_values.size(); i++) {
const double clamp_val =
vpMath::clamp(testing_values.at(i), lower, upper);
std::cerr << "Problem with vpMath::clamp()=" << clamp_val << std::endl;
return EXIT_FAILURE;
}
}
std::cout << "vpMath::clamp() is Ok !" << std::endl;
}
#endif
{
rxyz_deg_truth[0] = 10;
rxyz_deg_truth[1] = 20;
rxyz_deg_truth[2] = -30;
{
for (unsigned int i = 0u; i < rxyz_deg_truth.size(); i++) {
std::cerr << "Problem with vpMath::deg(vpRotationVector) " << i << ": " << rxyz_deg[i] << std::endl;
return EXIT_FAILURE;
}
}
std::cout << "vpMath::deg(vpRxyzVector) is Ok !" << std::endl;
}
{
for (unsigned int i = 0u; i < rxyz_deg_truth.size(); i++) {
std::cerr << "Problem with vpMath::deg(vpColVector) " << i << ": " << rxyz_deg[i] << std::endl;
return EXIT_FAILURE;
}
}
std::cout << "vpMath::deg(vpColVector) is Ok !" << std::endl;
}
{
for (unsigned int i = 0u; i < rxyz_deg_truth.size(); i++) {
std::cerr << "Problem with vpMath::rad(vpColVector) " << i << ": " << rxyz_rad[i] << std::endl;
return EXIT_FAILURE;
}
}
std::cout << "vpMath::rad(vpColVector) is Ok !" << std::endl;
}
}
std::cout << "Test succeed" << std::endl;
return EXIT_SUCCESS;
}
Implementation of column vector and the associated operations.
static bool isNaN(double value)
static double rad(double deg)
static double getMedian(const std::vector< double > &v)
static double getStdev(const std::vector< double > &v, bool useBesselCorrection=false)
static bool equal(double x, double y, double threshold=0.001)
static T clamp(const T &v, const T &lower, const T &upper)
static int round(double x)
static bool isFinite(double value)
static double getMean(const std::vector< double > &v)
static bool isInf(double value)
static double deg(double rad)
static bool isNumber(const std::string &str)
Implementation of a rotation vector as Euler angle minimal representation.