| 1 |
// Copyright(C) 1999-2020, 2022, 2023 National Technology & Engineering Solutions |
| 2 |
// of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 3 |
// NTESS, the U.S. Government retains certain rights in this software. |
| 4 |
// |
| 5 |
// See packages/seacas/LICENSE for details |
| 6 |
#pragma once |
| 7 |
|
| 8 |
#include "map.h" |
| 9 |
#include <cmath> |
| 10 |
|
| 11 |
// See http://realtimecollisiondetection.net/blog/?p=89 for a |
| 12 |
// description of the COMBINED tolerance. Basically: |
| 13 |
// The absolute tolerance test fails when x and y become large, and |
| 14 |
// the relative tolerance test fails when they become small. It is |
| 15 |
// therefore desired to combine these two tests together in a single |
| 16 |
// test. Over the years at GDC, as well as in my book, I've |
| 17 |
// suggested the following combined tolerance test: |
| 18 |
// |
| 19 |
// if (Abs(x - y) <= EPSILON * Max(1.0f, Abs(x), Abs(y)) ... |
| 20 |
|
| 21 |
enum class ToleranceMode { |
| 22 |
RELATIVE_ = 0, |
| 23 |
ABSOLUTE_ = 1, |
| 24 |
COMBINED_ = 2, |
| 25 |
IGNORE_ = 3, |
| 26 |
EIGEN_REL_ = 4, |
| 27 |
EIGEN_ABS_ = 5, |
| 28 |
EIGEN_COM_ = 6, |
| 29 |
ULPS_FLOAT_ = 7, |
| 30 |
ULPS_DOUBLE_ = 8 |
| 31 |
}; |
| 32 |
|
| 33 |
class Tolerance |
| 34 |
{ |
| 35 |
public: |
| 36 |
Tolerance() = default; |
| 37 |
|
| 38 |
Tolerance(ToleranceMode tol_type, double tol_value, double tol_floor) |
| 39 |
: type(tol_type), value(tol_value), floor(tol_floor) |
| 40 |
{ |
| 41 |
} |
| 42 |
|
| 43 |
// Default copy constructor and operator= should work for this simple class... |
| 44 |
|
| 45 |
bool Diff(double v1, double v2) const; |
| 46 |
|
| 47 |
double Delta(double v1, double v2) const; |
| 48 |
|
| 49 |
const char *typestr() const; |
| 50 |
const char *abrstr() const; |
| 51 |
|
| 52 |
ToleranceMode type{ToleranceMode::RELATIVE_}; |
| 53 |
double value{0.0}; |
| 54 |
double floor{0.0}; |
| 55 |
|
| 56 |
// If true, use the older definition of the floor tolerance which was |
| 57 |
// |a-b| < floor. The new definition is |a| < floor && |b| < floor |
| 58 |
static bool use_old_floor; |
| 59 |
|
| 60 |
private: |
| 61 |
double UlpsDiffFloat(double A, double B) const; |
| 62 |
double UlpsDiffDouble(double A, double B) const; |
| 63 |
}; |
| 64 |
|
| 65 |
inline double Tolerance::Delta(double v1, double v2) const |
| 66 |
{ |
| 67 |
if (type == ToleranceMode::IGNORE_) { |
| 68 |
return 0.0; |
| 69 |
} |
| 70 |
|
| 71 |
double fabv1 = std::fabs(v1); |
| 72 |
double fabv2 = std::fabs(v2); |
| 73 |
bool diff = false; |
| 74 |
if (!use_old_floor) { |
| 75 |
if (fabv1 >= floor || fabv2 >= floor) { |
| 76 |
diff = true; |
| 77 |
} |
| 78 |
} |
| 79 |
else { |
| 80 |
if (std::fabs(v1 - v2) >= floor) { |
| 81 |
diff = true; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
if (diff) { |
| 86 |
if (type == ToleranceMode::RELATIVE_) { |
| 87 |
if (v1 == 0.0 && v2 == 0.0) { |
| 88 |
return 0.0; |
| 89 |
} |
| 90 |
double max = fabv1 < fabv2 ? fabv2 : fabv1; |
| 91 |
return std::fabs(v1 - v2) / max; |
| 92 |
} |
| 93 |
if (type == ToleranceMode::ABSOLUTE_) { |
| 94 |
return std::fabs(v1 - v2); |
| 95 |
} |
| 96 |
if (type == ToleranceMode::COMBINED_) { |
| 97 |
double max = fabv1 < fabv2 ? fabv2 : fabv1; |
| 98 |
if (max > 1.0) { |
| 99 |
return std::fabs(v1 - v2) / max; |
| 100 |
} |
| 101 |
return std::fabs(v1 - v2); |
| 102 |
} |
| 103 |
if (type == ToleranceMode::ULPS_FLOAT_) { |
| 104 |
return UlpsDiffFloat(v1, v2); |
| 105 |
} |
| 106 |
if (type == ToleranceMode::ULPS_DOUBLE_) { |
| 107 |
return UlpsDiffDouble(v1, v2); |
| 108 |
} |
| 109 |
if (type == ToleranceMode::EIGEN_REL_) { |
| 110 |
if (v1 == 0.0 && v2 == 0.0) { |
| 111 |
return 0.0; |
| 112 |
} |
| 113 |
double max = fabv1 < fabv2 ? fabv2 : fabv1; |
| 114 |
return std::fabs(fabv1 - fabv2) / max; |
| 115 |
} |
| 116 |
if (type == ToleranceMode::EIGEN_ABS_) { |
| 117 |
return std::fabs(fabv1 - fabv2); |
| 118 |
} |
| 119 |
if (type == ToleranceMode::EIGEN_COM_) { |
| 120 |
double max = fabv1 < fabv2 ? fabv2 : fabv1; |
| 121 |
if (max > 1.0) { |
| 122 |
return std::fabs(fabv1 - fabv2) / max; |
| 123 |
} |
| 124 |
return std::fabs(fabv1 - fabv2); |
| 125 |
} |
| 126 |
} |
| 127 |
return 0.0; |
| 128 |
} |