44#include <visp3/core/vpMath.h>
45#include <visp3/core/vpColVector.h>
46#include <visp3/core/vpRxyzVector.h>
48#include <visp3/core/vpHomogeneousMatrix.h>
50#if defined _MSC_VER && _MSC_VER >= 1200
51#pragma warning(disable : 4723)
60static const unsigned long __nan[2] = { 0xffffffff, 0x7fffffff };
61#define NAN (*(const float *)__nan)
67#ifdef ENABLE_VISP_NAMESPACE
71#if !defined(VISP_HAVE_FAST_MATH)
74 std::cerr <<
"Fail: IsNaN(0.0)=" <<
vpMath::isNaN(0.0) <<
" / should be false" << std::endl;
78 double num = 1.0, den = 0.0;
80 std::cerr <<
"Fail: IsNaN(1.0/0.0)=" <<
vpMath::isNaN(num / den) <<
" / should be false" << std::endl;
85 std::cerr <<
"Fail: IsNaN(NAN)=" <<
vpMath::isNaN(NAN) <<
" / should be true" << std::endl;
91 std::cerr <<
"Fail: IsNaN(0.0/0.0)=" <<
vpMath::isNaN(num / den) <<
" / should be true" << std::endl;
95 if (!
vpMath::isNaN(std::numeric_limits<double>::quiet_NaN())) {
96 std::cerr <<
"Fail: IsNaN(quiet_NaN)=" <<
vpMath::isNaN(std::numeric_limits<double>::quiet_NaN())
97 <<
" / should be true" << std::endl;
101 if (!
vpMath::isNaN(std::numeric_limits<float>::quiet_NaN())) {
102 std::cerr <<
"Fail: IsNaN(quiet_NaN)=" <<
vpMath::isNaN(std::numeric_limits<float>::quiet_NaN())
103 <<
" / should be true" << std::endl;
107 if (!
vpMath::isNaN(std::numeric_limits<double>::signaling_NaN())) {
108 std::cerr <<
"Fail: IsNaN(signaling_NaN)=" <<
vpMath::isNaN(std::numeric_limits<double>::signaling_NaN())
109 <<
" / should be true" << std::endl;
113 if (!
vpMath::isNaN(std::numeric_limits<float>::signaling_NaN())) {
114 std::cerr <<
"Fail: IsNaN(signaling_NaN)=" <<
vpMath::isNaN(std::numeric_limits<float>::signaling_NaN())
115 <<
" / should be true" << std::endl;
119 if (
vpMath::isNaN(std::numeric_limits<double>::infinity())) {
120 std::cerr <<
"Fail: IsNaN(infinity)=" <<
vpMath::isNaN(std::numeric_limits<double>::infinity())
121 <<
" / should be false" << std::endl;
126 std::cerr <<
"Fail: IsNaN(infinity)=" <<
vpMath::isNaN(std::numeric_limits<float>::infinity())
127 <<
" / should be false" << std::endl;
131 if (
vpMath::isNaN(1.0 / std::numeric_limits<double>::epsilon())) {
132 std::cerr <<
"Fail: IsNaN(1.0/epsilon)=" <<
vpMath::isNaN(1.0 / std::numeric_limits<double>::epsilon())
133 <<
" / should be false" << std::endl;
137 if (!
vpMath::isNaN(std::numeric_limits<double>::infinity() - std::numeric_limits<double>::infinity())) {
138 std::cerr <<
"Fail: IsNaN(infinity - infinity)="
139 <<
vpMath::isNaN(std::numeric_limits<double>::infinity() - std::numeric_limits<double>::infinity())
140 <<
" / should be true" << std::endl;
144 float a = 0.0f, b = 0.0f;
146 std::cerr <<
"Fail: IsNaN(0.0f/0.0f)=" <<
vpMath::isNaN(a / b) <<
" / should be true" << std::endl;
149 std::cout <<
"vpMath::isNaN is Ok !" << std::endl;
153 std::cerr <<
"Fail: vpMath::isInf(NAN)=" <<
vpMath::isInf(NAN) <<
" / should be false" << std::endl;
158 std::cerr <<
"Fail: vpMath::isInf(1.0/0.0)=" <<
vpMath::isInf(1.0 / a) <<
" / should be true" << std::endl;
163 std::cerr <<
"Fail: vpMath::isInf(0.0)=" <<
vpMath::isInf(0.0) <<
" / should be false" << std::endl;
168 std::cerr <<
"Fail: vpMath::isInf(exp(800.))=" <<
vpMath::isInf(exp(800.)) <<
" / should be true" << std::endl;
173 std::cerr <<
"Fail: vpMath::isInf(DBL_MIN/2.0)=" <<
vpMath::isInf(DBL_MIN / 2.0) <<
" / should be false"
177 std::cout <<
"vpMath::isInf is Ok !" << std::endl;
181 std::cerr <<
"Fail: vpMath::isFinite(NAN)=" <<
vpMath::isFinite(NAN) <<
" / should be false" << std::endl;
186 std::cerr <<
"Fail: vpMath::isFinite(1.0/0.0)=" <<
vpMath::isFinite(1.0 / a) <<
" / should be false" << std::endl;
191 std::cerr <<
"Fail: vpMath::isFinite(exp(800.))=" <<
vpMath::isFinite(exp(800.)) <<
" / should be false" << std::endl;
197 std::cerr <<
"Fail: vpMath::isFinite(0.0)=" <<
vpMath::isFinite(0.0) <<
" / should be true" << std::endl;
202 std::cerr <<
"Fail: vpMath::isFinite(DBL_MIN/2.0)=" <<
vpMath::isFinite(DBL_MIN / 2.0) <<
" / should be true"
208 std::cerr <<
"Fail: vpMath::isFinite(DBL_MAX)=" <<
vpMath::isFinite(std::numeric_limits<float>::max()) <<
" / should be true"
212 std::cout <<
"vpMath::isFinite is Ok !" << std::endl;
216 std::cerr <<
"Fail: vpMath::isNumber(\"123\")=" <<
vpMath::isNumber(
"123") <<
" / should be false" << std::endl;
220 std::cerr <<
"Fail: vpMath::isNumber(\"string\")=" <<
vpMath::isNumber(
"string") <<
" / should be true" << std::endl;
224 std::cerr <<
"Fail: vpMath::isNumber(\"123string\")=" <<
vpMath::isNumber(
"123string") <<
" / should be true" << std::endl;
227 std::cout <<
"vpMath::isNumber is Ok !" << std::endl;
231 std::cerr <<
"Fail: vpMath::round(2.3)=" <<
vpMath::round(2.3) <<
" / should be 2" << std::endl;
236 std::cerr <<
"Fail: vpMath::round(3.8)=" <<
vpMath::round(3.8) <<
" / should be 4" << std::endl;
241 std::cerr <<
"Fail: vpMath::round(5.5)=" <<
vpMath::round(5.5) <<
" / should be 6" << std::endl;
246 std::cerr <<
"Fail: vpMath::round(-2.3)=" <<
vpMath::round(-2.3) <<
" / should be -2" << std::endl;
251 std::cerr <<
"Fail: vpMath::round(-3.8)=" <<
vpMath::round(-3.8) <<
" / should be -4" << std::endl;
256 std::cerr <<
"Fail: vpMath::round(-5.5)=" <<
vpMath::round(-5.5) <<
" / should be -6" << std::endl;
261 std::cerr <<
"Fail: vpMath::round(0.0)=" <<
vpMath::round(0.0) <<
" / should be 0" << std::endl;
264 std::cout <<
"vpMath::round is Ok !" << std::endl;
268 char char_value = -127;
270 if (uchar_value != 0) {
271 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-127)=" << uchar_value <<
" / should be 0" << std::endl;
275 unsigned short ushort_value = 60000;
277 if (uchar_value != UCHAR_MAX) {
278 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(60000)=" << uchar_value <<
" / should be " << UCHAR_MAX
283 int int_value = 70000;
285 if (uchar_value != UCHAR_MAX) {
286 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(70000)=" << uchar_value <<
" / should be " << UCHAR_MAX
293 if (uchar_value != 0) {
294 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-70000)=" << uchar_value <<
" / should be 0" << std::endl;
298 short short_value = 30000;
300 if (uchar_value != UCHAR_MAX) {
301 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(30000)=" << uchar_value <<
" / should be " << UCHAR_MAX
306 short_value = -30000;
308 if (uchar_value != 0) {
309 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-30000)=" << uchar_value <<
" / should be 0" << std::endl;
313 unsigned int uint_value = 10000;
315 if (uchar_value != UCHAR_MAX) {
316 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(10000)=" << uchar_value <<
" / should be " << UCHAR_MAX
321 float float_value = 10000.1f;
323 if (uchar_value != UCHAR_MAX) {
324 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(10000.1f)=" << uchar_value <<
" / should be " << UCHAR_MAX
329 float_value = -10000.1f;
331 if (uchar_value != 0) {
332 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-10000.1f)=" << uchar_value <<
" / should be 0" << std::endl;
336 double double_value = 10000.1;
338 if (uchar_value != UCHAR_MAX) {
339 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(10000.0)=" << uchar_value <<
" / should be " << UCHAR_MAX
344 double_value = -10000.1;
346 if (uchar_value != 0) {
347 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-10000.0)=" << uchar_value <<
" / should be 0" << std::endl;
350 std::cout <<
"vpMath::saturate<unsigned char>() is Ok !" << std::endl;
355 if (char_value != SCHAR_MAX) {
356 std::cerr <<
"Fail: vpMath::saturate<char>(255)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
360 ushort_value = 60000;
362 if (char_value != SCHAR_MAX) {
363 std::cerr <<
"Fail: vpMath::saturate<char>(60000)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
369 if (char_value != SCHAR_MAX) {
370 std::cerr <<
"Fail: vpMath::saturate<char>(70000)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
376 if (char_value !=
static_cast<char>(SCHAR_MIN)) {
377 std::cerr <<
"Fail: vpMath::saturate<char>(-70000)=" << char_value <<
" / should be " << SCHAR_MIN << std::endl;
383 if (char_value != SCHAR_MAX) {
384 std::cerr <<
"Fail: vpMath::saturate<char>(30000)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
388 short_value = -30000;
390 if (char_value !=
static_cast<char>(SCHAR_MIN)) {
391 std::cerr <<
"Fail: vpMath::saturate<char>(-30000)=" << char_value <<
" / should be " << SCHAR_MIN << std::endl;
397 if (char_value != SCHAR_MAX) {
398 std::cerr <<
"Fail: vpMath::saturate<char>(10000)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
402 float_value = 10000.1f;
404 if (char_value != SCHAR_MAX) {
405 std::cerr <<
"Fail: vpMath::saturate<char>(10000.1f)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
409 float_value = -10000.1f;
411 if (char_value !=
static_cast<char>(SCHAR_MIN)) {
412 std::cerr <<
"Fail: vpMath::saturate<char>(-10000.1f)=" << char_value <<
" / should be " << SCHAR_MIN << std::endl;
416 double_value = 10000.1;
418 if (char_value != SCHAR_MAX) {
419 std::cerr <<
"Fail: vpMath::saturate<char>(10000.1)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
423 double_value = -10000.1;
425 if (char_value !=
static_cast<char>(SCHAR_MIN)) {
426 std::cerr <<
"Fail: vpMath::saturate<char>(-10000.1)=" << char_value <<
" / should be " << SCHAR_MIN << std::endl;
429 std::cout <<
"vpMath::saturate<char>() is Ok !" << std::endl;
434 if (ushort_value != 0) {
435 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-127)=" << ushort_value <<
" / should be 0" << std::endl;
439 short_value = -30000;
441 if (ushort_value != 0) {
442 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-30000)=" << ushort_value <<
" / should be 0" << std::endl;
448 if (ushort_value != USHRT_MAX) {
449 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(70000)=" << ushort_value <<
" / should be " << USHRT_MAX
456 if (ushort_value != 0) {
457 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-70000)=" << ushort_value <<
" / should be 0" << std::endl;
463 if (ushort_value != USHRT_MAX) {
464 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(70000)=" << ushort_value <<
" / should be " << USHRT_MAX
469 float_value = 70000.1f;
471 if (ushort_value != USHRT_MAX) {
472 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(70000.1f)=" << ushort_value <<
" / should be " << USHRT_MAX
477 float_value = -10000.1f;
479 if (ushort_value != 0) {
480 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-10000.1f)=" << ushort_value <<
" / should be 0" << std::endl;
484 double_value = 70000.1;
486 if (ushort_value != USHRT_MAX) {
487 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(70000.1)=" << ushort_value <<
" / should be " << USHRT_MAX
492 double_value = -10000.1;
494 if (ushort_value != 0) {
495 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-10000.1)=" << ushort_value <<
" / should be 0" << std::endl;
498 std::cout <<
"vpMath::saturate<unsigned short>() is Ok !" << std::endl;
501 ushort_value = 60000;
503 if (short_value != SHRT_MAX) {
504 std::cerr <<
"Fail: vpMath::saturate<short>(60000)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
510 if (short_value != SHRT_MAX) {
511 std::cerr <<
"Fail: vpMath::saturate<short>(70000)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
517 if (short_value != SHRT_MIN) {
518 std::cerr <<
"Fail: vpMath::saturate<short>(-70000)=" << short_value <<
" / should be " << SHRT_MIN << std::endl;
524 if (short_value != SHRT_MAX) {
525 std::cerr <<
"Fail: vpMath::saturate<short>(70000)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
529 float_value = 70000.1f;
531 if (short_value != SHRT_MAX) {
532 std::cerr <<
"Fail: vpMath::saturate<short>(70000.1f)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
536 float_value = -70000.1f;
538 if (short_value != SHRT_MIN) {
539 std::cerr <<
"Fail: vpMath::saturate<short>(-70000.1f)=" << short_value <<
" / should be " << SHRT_MIN << std::endl;
543 double_value = 70000.1;
545 if (short_value != SHRT_MAX) {
546 std::cerr <<
"Fail: vpMath::saturate<short>(70000.1)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
550 double_value = -70000.1;
552 if (short_value != SHRT_MIN) {
553 std::cerr <<
"Fail: vpMath::saturate<short>(70000.1)=" << short_value <<
" / should be " << SHRT_MIN << std::endl;
556 std::cout <<
"vpMath::saturate<short>() is Ok !" << std::endl;
560 std::vector<double> vectorOfDoubles(10);
561 vectorOfDoubles[0] = 8.1472;
562 vectorOfDoubles[1] = 9.0579;
563 vectorOfDoubles[2] = 1.2699;
564 vectorOfDoubles[3] = 9.1338;
565 vectorOfDoubles[4] = 6.3236;
566 vectorOfDoubles[5] = 0.9754;
567 vectorOfDoubles[6] = 2.7850;
568 vectorOfDoubles[7] = 5.4688;
569 vectorOfDoubles[8] = 9.5751;
570 vectorOfDoubles[9] = 9.6489;
574 std::cerr <<
"Problem with vpMath::getMean()=" << res << std::endl;
577 std::cout <<
"vpMath::getMean() is Ok !" << std::endl;
581 std::cerr <<
"Problem with vpMath::getStdev()=" << res << std::endl;
587 std::cerr <<
"Problem with vpMath::getStdev() with Bessel correction=" << res << std::endl;
590 std::cout <<
"vpMath::getStdev() is Ok !" << std::endl;
594 std::cerr <<
"Problem with vpMath::getMedian()=" << res << std::endl;
599 vectorOfDoubles.push_back(1.5761);
602 std::cerr <<
"Problem with vpMath::getMedian()=" << res << std::endl;
605 std::cout <<
"vpMath::getMedian() is Ok !" << std::endl;
608#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
610 const double lower { -10. }, upper { +10. };
611 std::vector<double> testing_values { 5., -15., 15. };
612 std::vector<double> expected_values { 5., -10., 10. };
614 for (
size_t i = 0u;
i < testing_values.size();
i++) {
615 const double clamp_val =
vpMath::clamp(testing_values.at(i), lower, upper);
616 if (!
vpMath::equal(clamp_val, expected_values.at(i), 0.001)) {
617 std::cerr <<
"Problem with vpMath::clamp()=" << clamp_val << std::endl;
621 std::cout <<
"vpMath::clamp() is Ok !" << std::endl;
628 rxyz_deg_truth[0] = 10;
629 rxyz_deg_truth[1] = 20;
630 rxyz_deg_truth[2] = -30;
632 rxyz_rad_truth[0] =
vpMath::rad(rxyz_deg_truth[0]);
633 rxyz_rad_truth[1] =
vpMath::rad(rxyz_deg_truth[1]);
634 rxyz_rad_truth[2] =
vpMath::rad(rxyz_deg_truth[2]);
639 for (
unsigned int i = 0u;
i < rxyz_deg_truth.size();
i++) {
641 std::cerr <<
"Problem with vpMath::deg(vpRotationVector) " <<
i <<
": " << rxyz_deg[
i] << std::endl;
645 std::cout <<
"vpMath::deg(vpRxyzVector) is Ok !" << std::endl;
649 for (
unsigned int i = 0u;
i < rxyz_deg_truth.size();
i++) {
651 std::cerr <<
"Problem with vpMath::deg(vpColVector) " <<
i <<
": " << rxyz_deg[
i] << std::endl;
655 std::cout <<
"vpMath::deg(vpColVector) is Ok !" << std::endl;
659 for (
unsigned int i = 0u;
i < rxyz_deg_truth.size();
i++) {
661 std::cerr <<
"Problem with vpMath::rad(vpColVector) " <<
i <<
": " << rxyz_rad[
i] << std::endl;
665 std::cout <<
"vpMath::rad(vpColVector) is Ok !" << std::endl;
669 std::cout <<
"Test succeed" << std::endl;
Implementation of column vector and the associated operations.
static bool isNaN(double value)
static Tp saturate(unsigned char v)
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.