Files
ViewDesignEngine/docs/archive/01-需求分析/06-精度提升方案调研.md
T
茂之钳 486a0fe011
CI / Build & Test (push) Failing after 35s
CI / Release Build (push) Failing after 33s
Build & Test / build-and-test (push) Has been cancelled
Build & Test / python-bindings (push) Has been cancelled
docs: reorganize documentation — developer-friendly structure
New structure:
- docs/index.md — main navigation
- docs/getting-started.md — 5-minute quick start
- docs/guides/module-manual.md — all 11 modules with file listing
- docs/guides/api-usage.md — API reference with code examples
- docs/guides/ — architecture, building, testing, contributing
- docs/reference/ — competitive analysis, gap analysis, versioning
- docs/api/ — Doxygen generated HTML
- docs/archive/ — historical development plans

Old development plans archived. Clean 4-directory layout.
2026-07-27 13:32:20 +08:00

253 lines
7.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 几何引擎精度提升 — 业内方案调研
> 调研时间:2026-07-23
## 目录
1. [精度问题的根源](#1-精度问题的根源)
2. [主流方案总览](#2-主流方案总览)
3. [方案一:自适应浮点谓词 Shewchuk](#3-方案一自适应浮点谓词shewchuk)
4. [方案二:惰性精确求值 CGAL](#4-方案二惰性精确求值cgal)
5. [方案三:容差建模 Tolerant Modeling](#5-方案三容差建模tolerant-modeling)
6. [方案四:拓扑固定与吸附 Snapping](#6-方案四拓扑固定与吸附snapping)
7. [方案五:区间算术](#7-方案五区间算术)
8. [方案六:多精度算术 GMPMPFR](#8-方案六多精度算术gmpmpfr)
9. [商业内核方案解密](#9-商业内核方案解密)
10. [对 VDE 的建议路线](#10-对-vde-的建议路线)
---
## 1. 精度问题的根源
IEEE 754 双精度浮点有 53 位尾数(约 15-16 位有效数字)。
| 场景 | 问题 | 示例 |
|------|------|------|
| 大坐标减法抵消 | 两个大数相减丢失精度 | `(1e15+1) - 1e15 = 0` |
| 行列式计算 | 乘积累加累积舍入误差 | orient_2d/3d 符号误判 |
| 除法截断 | 交点坐标不精确 | 两线交点偏离真实位置 |
| 迭代累积 | 每次操作误差叠加 | 布尔运算链式操作 |
**关键文献**
- Kahan (1965): "Pracniques: further remarks on reducing truncation errors"
- Dekker (1971): "A floating-point technique for extending the available precision"
- Shewchuk (1997): "Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates" (引用 >2000 次)
---
## 2. 主流方案总览
| 方案 | 精度提升 | 性能损失 | 实现难度 | 代表 |
|------|---------|---------|---------|------|
| 自适应谓词 | ~10⁻³⁰ | 2-5x | ⭐⭐ | CGAL |
| 惰性精确求值 | 完全精确 | 5-50x | ⭐⭐⭐⭐ | CGAL Lazy_kernel |
| 容差建模 | 应用层保证 | ~0% | ⭐⭐⭐⭐⭐ | Parasolid, ACIS |
| 拓扑吸附 | 拓扑一致 | 10-20% | ⭐⭐⭐ | OCCT ShapeHealing |
| 区间算术 | 结果带边界 | 2-10x | ⭐⭐⭐ | Boost.Interval |
| 多精度(GMP) | 完全精确(任意) | 50-500x | ⭐⭐ | CGAL Gmpq kernel |
---
## 3. 方案一:自适应浮点谓词 Shewchuk
### 原理
不直接使用 GMP,而是用浮点运算模拟更高精度:
```
阶段 1: 用普通 double 计算行列式
阶段 2: 计算误差边界 (error bound)
阶段 3: 如果 |det| > error_bound → 结果可靠,返回
阶段 4: 否则,用 Dekker 分裂 + 扩展精度补偿项重算
阶段 5: 还不够 → 继续增加精度级别
```
### Shewchuk ABCD 四级
| 级别 | 方法 | 速度 |
|------|------|------|
| Level A | 普通 double | 最快 |
| Level B | double + 补偿项 (Dekker) | 2x |
| Level C | 扩展精度 (2-4 doubles) | 5x |
| Level D | 全自适应 (自动升级) | 10x |
### 关键子算法
- **Dekker Split**: 将 double 拆成 `hi + lo`,各 26 位有效数字
- **Two-Sum / Fast-Two-Sum**: 精确计算 `a+b` 的结果和误差
- **Two-Product**: 精确计算 `a*b` 的结果和误差
### VDE 当前状态
已实现 Level B。可升级到 Level C。
---
## 4. 方案二:惰性精确求值 CGAL
### CGAL 双层架构
```
每个几何对象存储两份数据:
┌─────────────────────────────────────┐
│ Fast approximation (double) │ ← 99% 操作在这里完成
│ Exact representation (expression) │ ← 仅当 double 不够时才求值
└─────────────────────────────────────┘
```
**表达式 DAG**: 不立即计算精确值,而是构建表达式树。点到精确坐标时才递归展开。
### 对 VDE
架构改动大,建议 v2.0 考虑。
---
## 5. 方案三:容差建模 Tolerant Modeling
### Parasolid/ACIS 的核心策略
| 容差类型 | Parasolid 默认值 | 说明 |
|---------|-----------------|------|
| 全局建模容差 | 1e-6 | 所有几何操作的默认精度 |
| 位置容差 | 1e-5 | 两点距离 < 此值视为重合 |
| 角度容差 | 1e-11 rad | 线面角的容差 |
| 边拟合容差 | 1e-3 | 曲线拟合时允许的偏差 |
**分层容差**
```
全局容差 → 部件容差 → 实体容差 → 面容差 → 边容差(自适应)
```
当判定重合时:
```
if distance(a, b) < max(tol_a, tol_b):
a 和 b 重合 → 拓扑合并
```
---
## 6. 方案四:拓扑固定与吸附 Snapping
### 原理
当计算产生的新点(如交点)与已有拓扑元素距离 < 容差时,不创建新拓扑,而是吸附到已有元素。
### OCCT ShapeHealing 迭代策略
```
第1轮: 粗吸附(coarse snap, tol=1e-3
第2轮: 细吸附(fine snap, tol=1e-5
第3轮: 拓扑修复(remove small edges, close gaps
```
---
## 7. 方案五:区间算术
每个值存储为 `[下界, 上界]` 区间:
```
运算规则:
[a,b] + [c,d] = [a+c, b+d]
[a,b] × [c,d] = [min(ac,ad,bc,bd), max(ac,ad,bc,bd)]
几何判定:
如果 [a_lo, a_hi] > [b_lo, b_hi] → 确定 a > b
如果区间重叠 → 无法确定,需要更高精度
```
**对 VDE**: 高适用性。Boost.Interval 可直接使用。
---
## 8. 方案六:多精度算术 GMP/MPFR
| 精度 | 相对 double 的性能损失 |
|------|----------------------|
| 53 位(double) | 1x |
| 113 位(quad) | 10-30x |
| 256 位 | 30-100x |
| 512 位 | 100-500x |
**对 VDE**: 作为可选后端 (`VDE_USE_GMP=ON`)。
---
## 9. 商业内核方案解密
### Parasolid (Siemens)
| 技术 | 说明 |
|------|------|
| Tolerant Modeling | 全局 + 局部容差体系 |
| 容差传递 | 布尔运算后沿拓扑链传递收紧的容差 |
| 回退策略 | 浮点计算失败 → 回退到有理数计算 |
### ACIS (Dassault/Spatial)
| 技术 | 说明 |
|------|------|
| SPAresabs | 绝对容差,默认 1e-5 |
| SPAresnor | 相对容差,默认 1e-6 |
| 拓扑容差链 | face→loop→coedge→edge→vertex 逐级收紧 |
### OCCT (Open Cascade)
| 技术 | 说明 |
|------|------|
| Precision::Confusion() | 默认 1e-7 |
| Precision::Angular() | 默认 1e-7 rad |
| ShapeHealing | 自动修复工具包 |
| 拓扑共享 | 边/顶点在面之间共享,容差自动适配 |
### CGAL
| Kernel 配置 | 精度 | 速度 |
|------------|------|------|
| Exact_predicates_inexact_constructions | 谓词精确,构造近似 | 中等 |
| Exact_predicates_exact_constructions | 完全精确 | 慢 |
| Filtered_kernel | 双层过滤 | 快速+安全 |
**CGAL 的关键洞察**: 谓词必须精确(影响判定正确性),构造可以近似(影响显示精度)。这是性价比最高的策略。
---
## 10. 对 VDE 的建议路线
### 当前评估
| 层级 | VDE 当前 | 对标 |
|------|---------|------|
| 谓词精度 | Level B (Dekker) | → Level C |
| 容差系统 | 全局单级 | → 分层 |
| 拓扑修复 | 基础实现 | → 吸附+迭代 |
| 精确计算 | 无 | → 可选 GMP |
| 惰性求值 | 无 | → v2.0 |
### 阶段提升路线
```
v0.6.0 (近期 1-2 周):
├── 分层容差系统(LocalTolerance + 容差继承)
├── Shewchuk Level C (expansion arithmetic)
├── 顶点吸附(vertex snapping
└── 容差感知的交点计算
v1.0.0 (中期):
├── GMP/MPFR 可选后端
├── 容差传递机制
└── 自动拓扑修复流水线
v2.0.0 (远期):
└── 惰性精确求值框架(与 CGAL 同级)
```
### 最小可行提升 v0.6.0 关键指标
| 指标 | 当前 | v0.6 目标 | 商业内核 |
|------|------|----------|---------|
| orient_2d 大坐标正确性 | 90% | 99.99% | 99.999% |
| 布尔运算退化通过率 | 60% | 90% | 99.9% |
| 网格修复成功率 | 50% | 85% | 99% |