ViewDesignEngine  3.1.0
高性能 CAD 计算几何引擎
tolerance.h
浏览该文件的文档.
1 #pragma once
2 #include <Eigen/Core>
3 #include <cmath>
4 #include <limits>
5 #include <vector>
6 
7 namespace vde::foundation {
8 
29 class Tolerance {
30 public:
39  Tolerance(double absolute = 1e-6, double relative = 1e-8,
40  double angular = 1e-8, double snapping = 1e-4)
41  : absolute_(absolute), relative_(relative),
42  angular_(angular), snapping_(snapping) {}
43 
44  // ── 判定 ──
45 
55  bool points_equal(const Eigen::MatrixXd& a, const Eigen::MatrixXd& b) const {
56  return (a - b).norm() < absolute_;
57  }
58 
65  template <typename T>
66  bool is_zero(T value) const {
67  return std::abs(value) < absolute_;
68  }
69 
87  template <typename T>
88  bool equals(T a, T b) const {
89  return std::abs(a - b) < absolute_ + relative_ * std::max(std::abs(a), std::abs(b));
90  }
91 
93  double absolute() const { return absolute_; }
95  double relative() const { return relative_; }
97  double angular() const { return angular_; }
99  double snapping() const { return snapping_; }
100 
101  // ── 分层容差:继承并收紧 ──
102 
116  Tolerance tighten(double factor = 0.1) const {
117  return Tolerance(absolute_ * factor, relative_ * factor, angular_ * factor, snapping_ * factor);
118  }
119 
128  Tolerance relax(double factor = 10.0) const {
129  return Tolerance(absolute_ * factor, relative_ * factor, angular_ * factor, snapping_ * factor);
130  }
131 
139  static Tolerance min(const Tolerance& a, const Tolerance& b) {
140  return Tolerance(std::min(a.absolute_, b.absolute_),
141  std::min(a.relative_, b.relative_),
142  std::min(a.angular_, b.angular_),
143  std::min(a.snapping_, b.snapping_));
144  }
145 
146  // ── 全局实例 ──
147 
155  static Tolerance& global() { return global_; }
156 
164  static void set_global(const Tolerance& tol) { global_ = tol; }
165 
166  // ── 默认建模容差等级 ──
167 
169  static constexpr double Modeling() { return 1e-6; }
171  static constexpr double Fitting() { return 1e-3; }
173  static constexpr double Intersection() { return 1e-8; }
175  static constexpr double Snapping() { return 1e-4; }
176 
177 private:
178  double absolute_;
179  double relative_;
180  double angular_;
181  double snapping_;
182  static Tolerance global_;
183 };
184 
202 public:
210  explicit ToleranceScope(const Tolerance& tol) : previous_(Tolerance::global()) {
212  }
213 
218 
219 private:
220  Tolerance previous_;
221 };
222 
223 } // namespace vde::foundation
局部容差作用域(RAII)
Definition: tolerance.h:201
~ToleranceScope()
退出容差作用域,恢复之前的全局容差
Definition: tolerance.h:217
ToleranceScope(const Tolerance &tol)
进入新的容差作用域
Definition: tolerance.h:210
分层容差管理类
Definition: tolerance.h:29
static Tolerance min(const Tolerance &a, const Tolerance &b)
合并两个容差,取更严格的(更小的)
Definition: tolerance.h:139
static constexpr double Intersection()
求交级精度 (1e-8):精确求交、布尔运算
Definition: tolerance.h:173
double snapping() const
获取吸附容差
Definition: tolerance.h:99
Tolerance relax(double factor=10.0) const
放宽容差
Definition: tolerance.h:128
Tolerance(double absolute=1e-6, double relative=1e-8, double angular=1e-8, double snapping=1e-4)
构造容差对象
Definition: tolerance.h:39
static Tolerance & global()
获取全局容差实例
Definition: tolerance.h:155
static constexpr double Snapping()
吸附级精度 (1e-4):点合并、顶点焊接
Definition: tolerance.h:175
double relative() const
获取相对容差
Definition: tolerance.h:95
double angular() const
获取角度容差
Definition: tolerance.h:97
static void set_global(const Tolerance &tol)
设置全局容差
Definition: tolerance.h:164
static constexpr double Fitting()
拟合级精度 (1e-3):曲面拟合、近似计算
Definition: tolerance.h:171
bool points_equal(const Eigen::MatrixXd &a, const Eigen::MatrixXd &b) const
判断两点是否相等(基于绝对容差)
Definition: tolerance.h:55
static constexpr double Modeling()
建模级精度 (1e-6):通用几何建模
Definition: tolerance.h:169
Tolerance tighten(double factor=0.1) const
收紧容差
Definition: tolerance.h:116
bool equals(T a, T b) const
混合容差相等判断
Definition: tolerance.h:88
bool is_zero(T value) const
判断值是否接近零
Definition: tolerance.h:66
double absolute() const
获取绝对容差
Definition: tolerance.h:93
自适应双精度谓词后端(默认)
Definition: error_codes.h:4