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)
60 static const unsigned long __nan[2] = { 0xffffffff, 0x7fffffff };
61 #define NAN (*(const float *)__nan)
67 #ifdef ENABLE_VISP_NAMESPACE
72 std::cerr <<
"Fail: IsNaN(0.0)=" <<
vpMath::isNaN(0.0) <<
" / should be false" << std::endl;
76 double num = 1.0, den = 0.0;
78 std::cerr <<
"Fail: IsNaN(1.0/0.0)=" <<
vpMath::isNaN(num / den) <<
" / should be false" << std::endl;
83 std::cerr <<
"Fail: IsNaN(NAN)=" <<
vpMath::isNaN(NAN) <<
" / should be true" << std::endl;
89 std::cerr <<
"Fail: IsNaN(0.0/0.0)=" <<
vpMath::isNaN(num / den) <<
" / should be true" << std::endl;
93 if (!
vpMath::isNaN(std::numeric_limits<double>::quiet_NaN())) {
94 std::cerr <<
"Fail: IsNaN(quiet_NaN)=" <<
vpMath::isNaN(std::numeric_limits<double>::quiet_NaN())
95 <<
" / should be true" << std::endl;
99 if (!
vpMath::isNaN(std::numeric_limits<float>::quiet_NaN())) {
100 std::cerr <<
"Fail: IsNaN(quiet_NaN)=" <<
vpMath::isNaN(std::numeric_limits<float>::quiet_NaN())
101 <<
" / should be true" << std::endl;
105 if (!
vpMath::isNaN(std::numeric_limits<double>::signaling_NaN())) {
106 std::cerr <<
"Fail: IsNaN(signaling_NaN)=" <<
vpMath::isNaN(std::numeric_limits<double>::signaling_NaN())
107 <<
" / should be true" << std::endl;
111 if (!
vpMath::isNaN(std::numeric_limits<float>::signaling_NaN())) {
112 std::cerr <<
"Fail: IsNaN(signaling_NaN)=" <<
vpMath::isNaN(std::numeric_limits<float>::signaling_NaN())
113 <<
" / should be true" << std::endl;
117 if (
vpMath::isNaN(std::numeric_limits<double>::infinity())) {
118 std::cerr <<
"Fail: IsNaN(infinity)=" <<
vpMath::isNaN(std::numeric_limits<double>::infinity())
119 <<
" / should be false" << std::endl;
124 std::cerr <<
"Fail: IsNaN(infinity)=" <<
vpMath::isNaN(std::numeric_limits<float>::infinity())
125 <<
" / should be false" << std::endl;
129 if (
vpMath::isNaN(1.0 / std::numeric_limits<double>::epsilon())) {
130 std::cerr <<
"Fail: IsNaN(1.0/epsilon)=" <<
vpMath::isNaN(1.0 / std::numeric_limits<double>::epsilon())
131 <<
" / should be false" << std::endl;
135 if (!
vpMath::isNaN(std::numeric_limits<double>::infinity() - std::numeric_limits<double>::infinity())) {
136 std::cerr <<
"Fail: IsNaN(infinity - infinity)="
137 <<
vpMath::isNaN(std::numeric_limits<double>::infinity() - std::numeric_limits<double>::infinity())
138 <<
" / should be true" << std::endl;
142 float a = 0.0f, b = 0.0f;
144 std::cerr <<
"Fail: IsNaN(0.0f/0.0f)=" <<
vpMath::isNaN(a / b) <<
" / should be true" << std::endl;
147 std::cout <<
"vpMath::isNaN is Ok !" << std::endl;
151 std::cerr <<
"Fail: vpMath::isInf(NAN)=" <<
vpMath::isInf(NAN) <<
" / should be false" << std::endl;
156 std::cerr <<
"Fail: vpMath::isInf(1.0/0.0)=" <<
vpMath::isInf(1.0 / a) <<
" / should be true" << std::endl;
161 std::cerr <<
"Fail: vpMath::isInf(0.0)=" <<
vpMath::isInf(0.0) <<
" / should be false" << std::endl;
166 std::cerr <<
"Fail: vpMath::isInf(exp(800.))=" <<
vpMath::isInf(exp(800.)) <<
" / should be true" << std::endl;
171 std::cerr <<
"Fail: vpMath::isInf(DBL_MIN/2.0)=" <<
vpMath::isInf(DBL_MIN / 2.0) <<
" / should be false"
175 std::cout <<
"vpMath::isInf is Ok !" << std::endl;
179 std::cerr <<
"Fail: vpMath::isFinite(NAN)=" <<
vpMath::isFinite(NAN) <<
" / should be false" << std::endl;
184 std::cerr <<
"Fail: vpMath::isFinite(1.0/0.0)=" <<
vpMath::isFinite(1.0 / a) <<
" / should be false" << std::endl;
189 std::cerr <<
"Fail: vpMath::isFinite(0.0)=" <<
vpMath::isFinite(0.0) <<
" / should be true" << std::endl;
194 std::cerr <<
"Fail: vpMath::isFinite(exp(800.))=" <<
vpMath::isFinite(exp(800.)) <<
" / should be false" << std::endl;
199 std::cerr <<
"Fail: vpMath::isFinite(DBL_MIN/2.0)=" <<
vpMath::isFinite(DBL_MIN / 2.0) <<
" / should be true"
205 std::cerr <<
"Fail: vpMath::isFinite(DBL_MAX)=" <<
vpMath::isFinite(std::numeric_limits<float>::max()) <<
" / should be true"
209 std::cout <<
"vpMath::isFinite is Ok !" << std::endl;
213 std::cerr <<
"Fail: vpMath::isNumber(\"123\")=" <<
vpMath::isNumber(
"123") <<
" / should be false" << std::endl;
217 std::cerr <<
"Fail: vpMath::isNumber(\"string\")=" <<
vpMath::isNumber(
"string") <<
" / should be true" << std::endl;
221 std::cerr <<
"Fail: vpMath::isNumber(\"123string\")=" <<
vpMath::isNumber(
"123string") <<
" / should be true" << std::endl;
224 std::cout <<
"vpMath::isNumber is Ok !" << std::endl;
228 std::cerr <<
"Fail: vpMath::round(2.3)=" <<
vpMath::round(2.3) <<
" / should be 2" << std::endl;
233 std::cerr <<
"Fail: vpMath::round(3.8)=" <<
vpMath::round(3.8) <<
" / should be 4" << std::endl;
238 std::cerr <<
"Fail: vpMath::round(5.5)=" <<
vpMath::round(5.5) <<
" / should be 6" << std::endl;
243 std::cerr <<
"Fail: vpMath::round(-2.3)=" <<
vpMath::round(-2.3) <<
" / should be -2" << std::endl;
248 std::cerr <<
"Fail: vpMath::round(-3.8)=" <<
vpMath::round(-3.8) <<
" / should be -4" << std::endl;
253 std::cerr <<
"Fail: vpMath::round(-5.5)=" <<
vpMath::round(-5.5) <<
" / should be -6" << std::endl;
258 std::cerr <<
"Fail: vpMath::round(0.0)=" <<
vpMath::round(0.0) <<
" / should be 0" << std::endl;
261 std::cout <<
"vpMath::round is Ok !" << std::endl;
265 char char_value = -127;
266 unsigned char uchar_value = vpMath::saturate<unsigned char>(char_value);
267 if (uchar_value != 0) {
268 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-127)=" << uchar_value <<
" / should be 0" << std::endl;
272 unsigned short ushort_value = 60000;
273 uchar_value = vpMath::saturate<unsigned char>(ushort_value);
274 if (uchar_value != UCHAR_MAX) {
275 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(60000)=" << uchar_value <<
" / should be " << UCHAR_MAX
280 int int_value = 70000;
281 uchar_value = vpMath::saturate<unsigned char>(int_value);
282 if (uchar_value != UCHAR_MAX) {
283 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(70000)=" << uchar_value <<
" / should be " << UCHAR_MAX
289 uchar_value = vpMath::saturate<unsigned char>(int_value);
290 if (uchar_value != 0) {
291 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-70000)=" << uchar_value <<
" / should be 0" << std::endl;
295 short short_value = 30000;
296 uchar_value = vpMath::saturate<unsigned char>(short_value);
297 if (uchar_value != UCHAR_MAX) {
298 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(30000)=" << uchar_value <<
" / should be " << UCHAR_MAX
303 short_value = -30000;
304 uchar_value = vpMath::saturate<unsigned char>(short_value);
305 if (uchar_value != 0) {
306 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-30000)=" << uchar_value <<
" / should be 0" << std::endl;
310 unsigned int uint_value = 10000;
311 uchar_value = vpMath::saturate<unsigned char>(uint_value);
312 if (uchar_value != UCHAR_MAX) {
313 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(10000)=" << uchar_value <<
" / should be " << UCHAR_MAX
318 float float_value = 10000.1f;
319 uchar_value = vpMath::saturate<unsigned char>(float_value);
320 if (uchar_value != UCHAR_MAX) {
321 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(10000.1f)=" << uchar_value <<
" / should be " << UCHAR_MAX
326 float_value = -10000.1f;
327 uchar_value = vpMath::saturate<unsigned char>(float_value);
328 if (uchar_value != 0) {
329 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-10000.1f)=" << uchar_value <<
" / should be 0" << std::endl;
333 double double_value = 10000.1;
334 uchar_value = vpMath::saturate<unsigned char>(double_value);
335 if (uchar_value != UCHAR_MAX) {
336 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(10000.0)=" << uchar_value <<
" / should be " << UCHAR_MAX
341 double_value = -10000.1;
342 uchar_value = vpMath::saturate<unsigned char>(double_value);
343 if (uchar_value != 0) {
344 std::cerr <<
"Fail: vpMath::saturate<unsigned char>(-10000.0)=" << uchar_value <<
" / should be 0" << std::endl;
347 std::cout <<
"vpMath::saturate<unsigned char>() is Ok !" << std::endl;
351 char_value = vpMath::saturate<char>(uchar_value);
352 if (char_value != SCHAR_MAX) {
353 std::cerr <<
"Fail: vpMath::saturate<char>(255)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
357 ushort_value = 60000;
358 char_value = vpMath::saturate<char>(ushort_value);
359 if (char_value != SCHAR_MAX) {
360 std::cerr <<
"Fail: vpMath::saturate<char>(60000)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
365 char_value = vpMath::saturate<char>(int_value);
366 if (char_value != SCHAR_MAX) {
367 std::cerr <<
"Fail: vpMath::saturate<char>(70000)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
372 char_value = vpMath::saturate<char>(int_value);
373 if (char_value != (
char)SCHAR_MIN) {
374 std::cerr <<
"Fail: vpMath::saturate<char>(-70000)=" << char_value <<
" / should be " << SCHAR_MIN << std::endl;
379 char_value = vpMath::saturate<char>(short_value);
380 if (char_value != SCHAR_MAX) {
381 std::cerr <<
"Fail: vpMath::saturate<char>(30000)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
385 short_value = -30000;
386 char_value = vpMath::saturate<char>(short_value);
387 if (char_value != (
char)SCHAR_MIN) {
388 std::cerr <<
"Fail: vpMath::saturate<char>(-30000)=" << char_value <<
" / should be " << SCHAR_MIN << std::endl;
393 char_value = vpMath::saturate<char>(uint_value);
394 if (char_value != SCHAR_MAX) {
395 std::cerr <<
"Fail: vpMath::saturate<char>(10000)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
399 float_value = 10000.1f;
400 char_value = vpMath::saturate<char>(float_value);
401 if (char_value != SCHAR_MAX) {
402 std::cerr <<
"Fail: vpMath::saturate<char>(10000.1f)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
406 float_value = -10000.1f;
407 char_value = vpMath::saturate<char>(float_value);
408 if (char_value != (
char)SCHAR_MIN) {
409 std::cerr <<
"Fail: vpMath::saturate<char>(-10000.1f)=" << char_value <<
" / should be " << SCHAR_MIN << std::endl;
413 double_value = 10000.1;
414 char_value = vpMath::saturate<char>(double_value);
415 if (char_value != SCHAR_MAX) {
416 std::cerr <<
"Fail: vpMath::saturate<char>(10000.1)=" << char_value <<
" / should be " << SCHAR_MAX << std::endl;
420 double_value = -10000.1;
421 char_value = vpMath::saturate<char>(double_value);
422 if (char_value != (
char)SCHAR_MIN) {
423 std::cerr <<
"Fail: vpMath::saturate<char>(-10000.1)=" << char_value <<
" / should be " << SCHAR_MIN << std::endl;
426 std::cout <<
"vpMath::saturate<char>() is Ok !" << std::endl;
430 ushort_value = vpMath::saturate<unsigned short>(char_value);
431 if (ushort_value != 0) {
432 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-127)=" << ushort_value <<
" / should be 0" << std::endl;
436 short_value = -30000;
437 ushort_value = vpMath::saturate<unsigned short>(short_value);
438 if (ushort_value != 0) {
439 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-30000)=" << ushort_value <<
" / should be 0" << std::endl;
444 ushort_value = vpMath::saturate<unsigned short>(int_value);
445 if (ushort_value != USHRT_MAX) {
446 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(70000)=" << ushort_value <<
" / should be " << USHRT_MAX
452 ushort_value = vpMath::saturate<unsigned short>(int_value);
453 if (ushort_value != 0) {
454 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-70000)=" << ushort_value <<
" / should be 0" << std::endl;
459 ushort_value = vpMath::saturate<unsigned short>(uint_value);
460 if (ushort_value != USHRT_MAX) {
461 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(70000)=" << ushort_value <<
" / should be " << USHRT_MAX
466 float_value = 70000.1f;
467 ushort_value = vpMath::saturate<unsigned short>(float_value);
468 if (ushort_value != USHRT_MAX) {
469 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(70000.1f)=" << ushort_value <<
" / should be " << USHRT_MAX
474 float_value = -10000.1f;
475 ushort_value = vpMath::saturate<unsigned short>(float_value);
476 if (ushort_value != 0) {
477 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-10000.1f)=" << ushort_value <<
" / should be 0" << std::endl;
481 double_value = 70000.1;
482 ushort_value = vpMath::saturate<unsigned short>(double_value);
483 if (ushort_value != USHRT_MAX) {
484 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(70000.1)=" << ushort_value <<
" / should be " << USHRT_MAX
489 double_value = -10000.1;
490 ushort_value = vpMath::saturate<unsigned short>(double_value);
491 if (ushort_value != 0) {
492 std::cerr <<
"Fail: vpMath::saturate<unsigned short>(-10000.1)=" << ushort_value <<
" / should be 0" << std::endl;
495 std::cout <<
"vpMath::saturate<unsigned short>() is Ok !" << std::endl;
498 ushort_value = 60000;
499 short_value = vpMath::saturate<short>(ushort_value);
500 if (short_value != SHRT_MAX) {
501 std::cerr <<
"Fail: vpMath::saturate<short>(60000)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
506 short_value = vpMath::saturate<short>(int_value);
507 if (short_value != SHRT_MAX) {
508 std::cerr <<
"Fail: vpMath::saturate<short>(70000)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
513 short_value = vpMath::saturate<short>(int_value);
514 if (short_value != SHRT_MIN) {
515 std::cerr <<
"Fail: vpMath::saturate<short>(-70000)=" << short_value <<
" / should be " << SHRT_MIN << std::endl;
520 short_value = vpMath::saturate<short>(uint_value);
521 if (short_value != SHRT_MAX) {
522 std::cerr <<
"Fail: vpMath::saturate<short>(70000)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
526 float_value = 70000.1f;
527 short_value = vpMath::saturate<short>(float_value);
528 if (short_value != SHRT_MAX) {
529 std::cerr <<
"Fail: vpMath::saturate<short>(70000.1f)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
533 float_value = -70000.1f;
534 short_value = vpMath::saturate<short>(float_value);
535 if (short_value != SHRT_MIN) {
536 std::cerr <<
"Fail: vpMath::saturate<short>(-70000.1f)=" << short_value <<
" / should be " << SHRT_MIN << std::endl;
540 double_value = 70000.1;
541 short_value = vpMath::saturate<short>(double_value);
542 if (short_value != SHRT_MAX) {
543 std::cerr <<
"Fail: vpMath::saturate<short>(70000.1)=" << short_value <<
" / should be " << SHRT_MAX << std::endl;
547 double_value = -70000.1;
548 short_value = vpMath::saturate<short>(double_value);
549 if (short_value != SHRT_MIN) {
550 std::cerr <<
"Fail: vpMath::saturate<short>(70000.1)=" << short_value <<
" / should be " << SHRT_MIN << std::endl;
553 std::cout <<
"vpMath::saturate<short>() is Ok !" << std::endl;
557 std::vector<double> vectorOfDoubles(10);
558 vectorOfDoubles[0] = 8.1472;
559 vectorOfDoubles[1] = 9.0579;
560 vectorOfDoubles[2] = 1.2699;
561 vectorOfDoubles[3] = 9.1338;
562 vectorOfDoubles[4] = 6.3236;
563 vectorOfDoubles[5] = 0.9754;
564 vectorOfDoubles[6] = 2.7850;
565 vectorOfDoubles[7] = 5.4688;
566 vectorOfDoubles[8] = 9.5751;
567 vectorOfDoubles[9] = 9.6489;
571 std::cerr <<
"Problem with vpMath::getMean()=" << res << std::endl;
574 std::cout <<
"vpMath::getMean() is Ok !" << std::endl;
578 std::cerr <<
"Problem with vpMath::getStdev()=" << res << std::endl;
584 std::cerr <<
"Problem with vpMath::getStdev() with Bessel correction=" << res << std::endl;
587 std::cout <<
"vpMath::getStdev() is Ok !" << std::endl;
591 std::cerr <<
"Problem with vpMath::getMedian()=" << res << std::endl;
596 vectorOfDoubles.push_back(1.5761);
599 std::cerr <<
"Problem with vpMath::getMedian()=" << res << std::endl;
602 std::cout <<
"vpMath::getMedian() is Ok !" << std::endl;
605 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
607 const double lower { -10. }, upper { +10. };
608 std::vector<double> testing_values { 5., -15., 15. };
609 std::vector<double> expected_values { 5., -10., 10. };
611 for (
size_t i = 0u; i < testing_values.size(); i++) {
612 const double clamp_val =
vpMath::clamp(testing_values.at(i), lower, upper);
613 if (!
vpMath::equal(clamp_val, expected_values.at(i), 0.001)) {
614 std::cerr <<
"Problem with vpMath::clamp()=" << clamp_val << std::endl;
618 std::cout <<
"vpMath::clamp() is Ok !" << std::endl;
625 rxyz_deg_truth[0] = 10;
626 rxyz_deg_truth[1] = 20;
627 rxyz_deg_truth[2] = -30;
629 rxyz_rad_truth[0] =
vpMath::rad(rxyz_deg_truth[0]);
630 rxyz_rad_truth[1] =
vpMath::rad(rxyz_deg_truth[1]);
631 rxyz_rad_truth[2] =
vpMath::rad(rxyz_deg_truth[2]);
636 for (
unsigned int i = 0u; i < rxyz_deg_truth.size(); i++) {
638 std::cerr <<
"Problem with vpMath::deg(vpRotationVector) " << i <<
": " << rxyz_deg[i] << std::endl;
642 std::cout <<
"vpMath::deg(vpRxyzVector) is Ok !" << std::endl;
646 for (
unsigned int i = 0u; i < rxyz_deg_truth.size(); i++) {
648 std::cerr <<
"Problem with vpMath::deg(vpColVector) " << i <<
": " << rxyz_deg[i] << std::endl;
652 std::cout <<
"vpMath::deg(vpColVector) is Ok !" << std::endl;
656 for (
unsigned int i = 0u; i < rxyz_deg_truth.size(); i++) {
658 std::cerr <<
"Problem with vpMath::rad(vpColVector) " << i <<
": " << rxyz_rad[i] << std::endl;
662 std::cout <<
"vpMath::rad(vpColVector) is Ok !" << std::endl;
666 std::cout <<
"Test succeed" << std::endl;
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.