W27: exact trim-base AABB lower-bound prune in CBRA classification

face_contains_uv falls back to a per-point global closest projection of the
trim base surface (0.3-0.5 ms/pt) for every point of a numeric trace branch.
The trim base surface is contained in its control-point AABB, so
dist(p, bbox) is a strict lower bound of dist(p, base_surface); when it
exceeds base_tol the original residual gate must fail, so classification is
provably false. Prune before reprojecting: bit-identical classification,
snap_clip union cut-graph stage 121.3s -> 6.8s (17.9x).

Also record two reverted attempts (batch warm hint, cheap local projection)
in docs/planning/wave5/cbra-notes.md.
This commit is contained in:
W27 CBRA
2026-11-20 06:53:07 +08:00
parent 702eb78ee6
commit 0cbcdf27ec
2 changed files with 130 additions and 0 deletions
+116
View File
@@ -0,0 +1,116 @@
# Wave 5 W27 — CBRA 真实模型 cut graph 剩余性能
- 日期:2026-11-20
- worktree:`D:\3_Source\VDE_cp5_cbra`,分支 `feat/kcp5-cbra`,基线 `702eb78e`(1.0.471,含 W21)
- 目标:真实模型 CBRA 相对 `702eb78e` ≥2× 加速(≥3 例真实模型、同窗口交替 before/after、记录负载),不改判定语义/审计口径/预算开关默认值/18 行行为
## 1. 基线画像(复现 W21 结论并更新到 1.0.471)
snap_clip(30 面,raw union,生产预算下 60 s 内部 cut-graph 预算):
```
[CB1-PROF] pair a=4 b=0 tot=56923.06ms | trace=229.21 num_clip=56691.93 fill=0.29 | E/T/R=1/1/0 ptsN=131073
[CB1-PROF] SUMMARY total=57631.53ms | closest=56910.78ms/134470calls
[CB1-PROF] CLS hint=131217 baseonly=0 global=0 baseused=0 baseretry=0
```
热点:截断数值追踪分支 131,073 点;`face_contains_uv` 在“不同帧 trim 基面”回退分支
(`closest_uv(fd.ts.base_surface, p)`)逐点做全精度最近点投影,约 0.3–0.5 ms/点
(131,073×2 面 ≈ 55 s)。批量面级 UV hint 已经工作(hint=131,217、global=0),只有
trim 基面 UV 未批量化。
## 2. 三次尝试(含两次失败的完整记录)
### W27-A(失败,已回退):CROSS_WARM 默认开 + 整矩形 base-hint 接受
- 内容:`warm_base_*` 条件放宽到 `g_cross_warm || domain_is_full_rect`,并默认开启
W21 的跨帧批量 warm start(保留 kill switch)。
- 硬门槛全过(18 行 IDENTICAL、VERIFY_UV mismatch=0、ctest 105/105、B5 pass=4/4),
但真实模型**变慢**:snap_clip union raw 200000 → 96.5 s(基线 ~57 s cut graph)。
原因:trim 基面批量投影本身耗时 45.3 s(172 µs/点,链条逐点失败触发升级),且
返回的 UV 残差 > base_tol(baseretry=261,858 / 262,146),仍旧回退逐点全局投影
(50.8 s)→ 两条路径都付钱。
- 证据:`w27_hintpath_attempt.diff`。
### W27-B(失败,已回退):廉价局部投影 fast path
- 内容:整矩形域用 `surface_contains_point_fast` 确认距离;非整矩形用
`project_point_on_surface_quick` + 残差门限,失败再回退全精度投影。
- 结果:closest 调用数不变(265,267 vs 基线 265,587),无收益。临时插桩
`VDE_CB1_W27DBG` 显示数值分支点离基面 **res=653.9 单位**(截断追踪已跑出有限
base patch),fast 无法“确认远”,只能回退全精度投影来证明 false。
- 证据:`w27_fastpath_attempt.diff`、`w27dbg.err`。
### W27-C(保留):trim 基面控制点 AABB 精确下界剪枝
- 原理:NURBS 曲面上任意点必在其控制点凸包内,控制点 AABB 是凸包的保守外包 ⇒
`dist(p, base_bbox) ≤ dist(p, base_surface)`。若 `dist(p, base_bbox) > base_tol`,
则原逐点全局最近点投影的残差门限**必然**失败 ⇒ 该 `face_contains_uv` 分类必为
false。因此在回退到全局投影前先判 `dist(p, bbox) > base_tol` 立即返回 false,只跳过
注定失败的重投影,**分类结果与全精度全局投影逐位一致**(不是近似)。
- 实现(`src/brep/curved_boolean_topology.cpp`):
- `FaceTrimDomain` 增加 `core::AABB3D base_bbox; bool base_bbox_valid = false;`
- `build_face_trim_domain` 中 `out.base_bbox = out.ts.base_surface.bbox()`(
`NurbsSurface::bbox()` = 控制点 AABB,`src/curves/nurbs_surface.cpp:1200`)
- `face_contains_uv` 全局回退前:`if (fd.base_bbox_valid && distance_to_aabb(fd.base_bbox, p) > base_tol) return false;`
- 风险:语义零改变(下界严格、方向安全);唯一风险是 `bbox()` 若实现不保守会误剪,
已核对实现并保留 invalid 兜底;性能上只对已证明必失败的点生效,不影响任何 true 分支。
- 真实模型效果(cut graph 阶段,snap_clip union raw 200000):
- `total 121,322.45 ms → 6,790.24 ms`(**17.9×**;closest 120.4 s/265,587 calls →
1.8 s/22,204 calls;num_clip 120,238.74 → 2,111.68 ms)
- 注:基线在 60 s 内部预算处中止(只处理 4 对);优化后 cut graph 完整跑完 29 对
(ptsN 2,490,453),所以此对比仍偏保守。
## 3. 端到端 caveat(如实报告)
优化后 cut graph 首次**跑完**,于是暴露出下游既有的 domain split 失败:
`curved_boolean: domain split failed: ...`,该阶段本身耗时 ~298 s(预算 200000 时);
budget=8000 时 domain split 的剩余预算为 0(=不限),wall 141.58 s 后同样失败。
即:**端到端真实模型 CBRA 仍不能产出合法结果,且 wall 时间没有变快**(基线在更早的
cut graph 预算超时处失败)。W27 的目标是 cut graph 剩余性能,此下游失败是独立问题,
不在本任务写域/时间盒内,已作为下一步首要项记录。
## 4. A/B 测量(同窗口交替,记录负载)
测量用 `cbra_bench_before.exe`(链接 702eb78e 的 pristine 静态库快照
`before_libs/`)与 `cbra_bench_after.exe`(链接当前 build/src),同窗口交替,环境变量
`VDE_CB1_PROFILE=1`,负载由 `Win32_Processor.LoadPercentage` 记录(本机其它 agent
同时活动,LOAD 60–100%)。指标取 `[CB1-PROF] SUMMARY` 出现时刻的 cut-graph 阶段耗时;
端到端 wall 单独记录。
| 模型 | before cut graph | after cut graph | 加速 | 负载 | 端到端(含 domain split) |
|---|---|---|---|---|---|
| snap_clip 30F,union raw budget=200000 | 121,322.45 ms(中止于内部 60 s 预算,只跑 4 对) | 6,790.24 ms(完整跑完 29 对) | **17.9×** | 96–100% | before 121.4 s 失败(cut 预算);after 304.7 s 失败(domain split);budget=8000 时 after 141.6 s 失败 |
| card_holder 197F,union raw | 100,332 ms(budget=30000,中止) | 72,145 ms(budget=200000,内部 60 s 预算中止) | 预算不同,不可比 | 94–100% | 均失败 |
| grid_panel 397F,union raw | 60,054 ms(内部 60 s 预算中止) | 60,256 ms(同样中止) | 无可观测加速 | 99% | 均失败 |
结论(如实):**≥2× 端到端目标未达成,≥3 例真实模型同窗口 A/B 仅 snap_clip 一例拿到
干净数据**。snap_clip 的 cut graph 阶段 17.9× 是真实的、且分类逐位一致;但端到端仍失败
(下游 domain split 问题,见 §3),card_holder/grid_panel 的 cut graph 在内部 60 s 预算内
仍跑不完(397 面模型 pair 数更多),没有可测量的端到端收益。所有模型 CBRA 端到端都
> 8 s 且未成功产出结果。
## 5. 硬门槛(最终代码,2026-11-20T06:50:09 LOAD=94)
- GATE1:gated_lines=18 got_lines=18 **GATE_IDENTICAL=YES**
- GATE2:VDE_CB1_VERIFY_UV=1 连跑 3 次,每次 9 条 op 行 **mismatch_nonzero=0**,gtest 均 PASSED
- GATE3:ctest -R "^CurvedBoolean" → **100% tests passed, 0 failed out of 105**(82.81 s)
- GATE4:B5 **pass=4/4**,elapsed_ms=1376.2
(注:GATE1/GATE2/GATE3/GATE4 的 summary 中 exit= 字段为空是 run_gates_final.ps1 的
WinPS 5.1 输出捕获瑕疵,脚本以 gtest/ctest/bench 自身输出判定;evidence 中有原始文件。)
## 6. 证据清单(`build/kcp5_evidence/cbra/`)
- `w27_hintpath_attempt.diff`、`w27_fastpath_attempt.diff`:两次失败尝试的完整 diff
- `w27dbg.err`:fast path 失败诊断(res=653.9)
- `gate_final_summary.txt`、`gate_final_raw.out`:18 行 gate
- `verify_uv_final_r1..r3.out`:VERIFY_UV 3 次
- `ctest_final.out`、`b5_final.out`
- `ab_stage_results.txt` + `stage_*.err`:A/B 原始数据
- `cbra_bench.cpp` / `build_bench.ps1` / `before_libs/`:基于 702eb78e 库的 before 基准
与 after 构建脚本(cbra_bench_before.exe 链接 pristine 库,cbra_bench_after.exe 链接
当前 build/src)
## 7. 下一步
1. domain split 失败根因(新暴露的真实模型问题,优先级最高)。
2. cut graph 预算检查粒度(当前只在 face-pair 循环起点检查,基线会 overshoot 到
120 s 才中止;W27 后单对已快,但预算控制仍不精确)。
3. 若未来要恢复 batch warm hint 路线,需要先解决 trim 基面重建差异/局部极小问题。
+14
View File
@@ -322,6 +322,11 @@ struct FaceTrimDomain {
core::AABB3D bounds;
/// 解析环面片的角度框(P3b):AABB 之上的更紧剔除,仅 Torus 面片有效。
PatchAngleBox box;
/// W27:trim 基面(`ts.base_surface`)控制点 AABB。基面曲面 ⊆ 控制点凸包 ⊆
/// 该盒,故「点到盒距离」是「点到基面距离」的下界:盒外距离 > base_tol 时
/// 原逐点全局最近点投影的残差门限必失败(分类为 false),可直接剪枝。
core::AABB3D base_bbox;
bool base_bbox_valid = false;
/// W21:每条 p-curve 的 UV 包围盒 `{ulo,vlo,uhi,vhi}`(控制点凸包)。
/// 有理曲线权重 > 0 ⇒ 曲线点必在控制点凸包内,故「UV 点到盒的距离」是
/// 「UV 点到该 p-curve 距离」的**下界**。`face_violation_uv` 用它精确剪枝:
@@ -658,6 +663,9 @@ bool build_face_trim_domain(const BrepModel& body, int face_index, double deflec
out.ts = *ts;
out.dom = std::move(dom);
out.has_trim = true;
// W27:trim 基面控制点包围盒(分类剪枝下界,见 FaceTrimDomain::base_bbox)。
out.base_bbox = out.ts.base_surface.bbox();
out.base_bbox_valid = true;
// 帧一致性探测:同一 UV 下两基面求值一致 → 可直接复用面 UV。
out.same_frame = true;
std::size_t probes = 0;
@@ -849,6 +857,12 @@ bool face_contains_uv(const FaceTrimDomain& fd, const core::Point3D& p, double u
}
if (g_prof != nullptr) ++g_prof->cls_base_retry;
}
// W27 精确剪枝:trim 基面 ⊂ 控制点 AABB,故点到盒距离 ≤ 点到基面距离。
// 盒外距离已超 base_tol ⇒ 原逐点全局最近点投影的残差门限必失败 ⇒ false。
// 与全精度全局投影分类逐位一致(只跳过注定失败的重投影),真实模型上
// 数值分支大量点落在基面片外很远(实测残差 6.5e+2),省 0.3–0.5 ms/点。
if (fd.base_bbox_valid && distance_to_aabb(fd.base_bbox, p) > base_tol)
return false;
const auto uvt = closest_uv(fd.ts.base_surface, p);
const core::Point3D qt = fd.ts.base_surface.evaluate(uvt.first, uvt.second);
if ((qt - p).norm() > base_tol) return false;