53 lines
1.5 KiB
C++
53 lines
1.5 KiB
C++
#pragma once
|
|
#include <cmath>
|
|
#include <limits>
|
|
|
|
namespace vde::foundation {
|
|
|
|
class Tolerance {
|
|
public:
|
|
Tolerance(double absolute = 1e-6, double relative = 1e-8,
|
|
double angular = 1e-8, double snapping = 1e-4)
|
|
: absolute_(absolute), relative_(relative),
|
|
angular_(angular), snapping_(snapping) {}
|
|
|
|
/// Two points are coincident
|
|
template <typename T, size_t D>
|
|
bool points_equal(const Eigen::Matrix<T, D, 1>& a,
|
|
const Eigen::Matrix<T, D, 1>& b) const {
|
|
return (a - b).norm() < absolute_;
|
|
}
|
|
|
|
/// Value is effectively zero
|
|
template <typename T>
|
|
bool is_zero(T value) const {
|
|
return std::abs(value) < absolute_;
|
|
}
|
|
|
|
/// Two values are equal within tolerance
|
|
template <typename T>
|
|
bool equals(T a, T b) const {
|
|
return std::abs(a - b) < absolute_ + relative_ * std::max(std::abs(a), std::abs(b));
|
|
}
|
|
|
|
double absolute() const { return absolute_; }
|
|
double relative() const { return relative_; }
|
|
double angular() const { return angular_; }
|
|
double snapping() const { return snapping_; }
|
|
|
|
void set_absolute(double v) { absolute_ = v; }
|
|
void set_relative(double v) { relative_ = v; }
|
|
|
|
static Tolerance& global() { return global_; }
|
|
static void set_global(const Tolerance& tol) { global_ = tol; }
|
|
|
|
private:
|
|
double absolute_;
|
|
double relative_;
|
|
double angular_;
|
|
double snapping_;
|
|
static Tolerance global_;
|
|
};
|
|
|
|
} // namespace vde::foundation
|