From e387dbdd7ddcd5a72a2d43ab21bbd86406addec5 Mon Sep 17 00:00:00 2001 From: hm-thinkbook16p Date: Mon, 2 Nov 2026 23:28:27 +0800 Subject: [PATCH] fix(measure): VDE-1269 multiply-connected volume/mass (1.0.125) --- CHANGELOG.md | 14 + CMakeLists.txt | 2 +- README.md | 2 +- docs/feedback/VDE-1269.md | 2 +- docs/guides/module-manual.md | 2 +- docs/index.md | 3 +- ...0.125-VDE1269多连通体测量修复自验证报告.md | 37 ++ docs/reference/README.md | 3 +- include/vde/brep/brep.h | 2 +- include/vde/brep/measure.h | 2 +- include/vde/core/version.h | 6 +- src/brep/measure.cpp | 354 +++++++++++++++++- tests/brep/test_measure.cpp | 62 +++ tests/core/test_version.cpp | 14 +- 14 files changed, 482 insertions(+), 23 deletions(-) create mode 100644 docs/reference/129-VDE1.0.125-VDE1269多连通体测量修复自验证报告.md diff --git a/CHANGELOG.md b/CHANGELOG.md index cb5029e1..8f05ff6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,20 @@ > 与 `origin/main` 同期新增的 1.0.45..1.0.61(格式/语料工作)合并后按时间顺序重编号为 **1.0.62..1.0.65** > (本文件中旧编号 1.0.45..1.0.48 的引用即指这四批;历史验证报告 55-58 中的版本号为验证当时编号)。 +## [1.0.125] — 2026-11-02 + +### VDE-1269 多连通体测量 + +- analytic_measure/mass_properties 按 closed component + 嵌套树(solid-angle winding)分组;不相交 abs 相加、嵌套按深度符号; +- VDE-1269 场景 volume=4000、area=2000、centroid=(0,0,5);嵌套腔体 7000;单连通不变; +- test_measure 40/40、test_brep_boolean 39/39; + +### 验证与工程(1.0.125) + +- 版本 1.0.125 全链路同步;构建 0 error; +- 全量 ctest **7962 注册、7962 PASS、0 failed**(681.93s); +- 自验证报告:[129-VDE1.0.125-VDE1269多连通体测量修复自验证报告.md](docs/reference/129-VDE1.0.125-VDE1269多连通体测量修复自验证报告.md); + ## [1.0.124] — 2026-11-02 ### FMT-11 DWFx 图像/二进制 W2D 诊断 diff --git a/CMakeLists.txt b/CMakeLists.txt index c95d9893..f1d9f001 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.16) -project(ViewDesignEngine VERSION 1.0.124 LANGUAGES C CXX) +project(ViewDesignEngine VERSION 1.0.125 LANGUAGES C CXX) # MSVC: expose M_PI from (GCC/MinGW define it by default). if(MSVC) diff --git a/README.md b/README.md index eb6f4a20..824bc0c8 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ C++20 全栈 B-Rep 几何内核。对标 Parasolid/ACIS,覆盖曲面建模、网格处理、格式交换、CAM 制造、工程图、装配体。 -[![Version](https://img.shields.io/badge/version-1.0.124-blue)](CHANGELOG.md) +[![Version](https://img.shields.io/badge/version-1.0.125-blue)](CHANGELOG.md) [![C++](https://img.shields.io/badge/C%2B%2B-20-blue)](CMakeLists.txt) [![License](https://img.shields.io/badge/license-Apache%202.0-green)](LICENSE) [![Build](https://img.shields.io/badge/build-0%20errors-brightgreen)]() diff --git a/docs/feedback/VDE-1269.md b/docs/feedback/VDE-1269.md index 230a30f1..8869d40e 100644 --- a/docs/feedback/VDE-1269.md +++ b/docs/feedback/VDE-1269.md @@ -6,7 +6,7 @@ | **日期** | 2026-11-02 | | **模块** | `vde::brep` — `src/brep/measure.cpp`(`analytic_measure` / `mass_properties`)、`src/brep/brep.cpp`(`BrepModel::body_volume`) | | **严重度** | P2(不崩溃;多体 volume/mass 错误) | -| **状态** | **Open**(host 侧已加 workaround;VDE 侧建议修) | +| **状态** | **Fixed(1.0.125)**:component 分组 + 嵌套树;VDE-1269 场景 volume=4000/area=2000/centroid(0,0,5);嵌套腔体 7000;单连通不变 | | | **宿主环境** | ViewDesign `main`(VDE pin `e98f50c5` v1.0.0-2156);Windows x64 Release | ## 现象 diff --git a/docs/guides/module-manual.md b/docs/guides/module-manual.md index 7a204569..44fb7b12 100644 --- a/docs/guides/module-manual.md +++ b/docs/guides/module-manual.md @@ -1,6 +1,6 @@ # 模块功能说明书 -> ViewDesignEngine v1.0.124 · **30 个 CMake 模块**(11 几何内核 + 2 集成层 + 17 官方/应用模块)+ 分层聚合 `vde::kernel` / `vde::io` / `vde::app`;插件 ABI **1.9**,管理器见 `include/vde/plugin/`。当前状态总览见 [106-VDE工程现状总览](../reference/106-VDE工程现状总览(1.0.107).md)。 +> ViewDesignEngine v1.0.125 · **30 个 CMake 模块**(11 几何内核 + 2 集成层 + 17 官方/应用模块)+ 分层聚合 `vde::kernel` / `vde::io` / `vde::app`;插件 ABI **1.9**,管理器见 `include/vde/plugin/`。当前状态总览见 [106-VDE工程现状总览](../reference/106-VDE工程现状总览(1.0.107).md)。 ## foundation — 基础设施 + 格式 IO diff --git a/docs/index.md b/docs/index.md index bad308b7..d9ed91d6 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1,6 +1,6 @@ # ViewDesignEngine 文档 -> v1.0.124 · C++20 B-Rep 几何引擎 + 微内核插件架构(ABI 1.9 / 30 模块 / 7822 测试) · Apache 2.0 +> v1.0.125 · C++20 B-Rep 几何引擎 + 微内核插件架构(ABI 1.9 / 30 模块 / 7822 测试) · Apache 2.0 ## 快速导航 @@ -21,6 +21,7 @@ | CAE 工程立项 | [VDCAE 独立 CAE 工程设计与需求规格](design/cae-project-design.md) | | 插件生态团队交接 | [插件生态交接与规划](design/plugin-ecosystem-handoff.md) | | 查看功能缺口/缺陷 | [107-VDE功能与缺陷审计(1.0.107)](reference/107-VDE功能与缺陷审计(1.0.107).md) | +| 查看 1.0.125 VDE-1269 | [129 VDE-1269 自验证报告](reference/129-VDE1.0.125-VDE1269多连通体测量修复自验证报告.md) | | 查看 1.0.124 FMT-11 DWFx | [128 DWFx 自验证报告](reference/128-VDE1.0.124-FMT11-DWFx图像与二进制W2D自验证报告.md) | | 查看 1.0.123 FMT-11 COLLADA | [127 FMT-11 自验证报告](reference/127-VDE1.0.123-FMT11-COLLADA材质自验证报告.md) | | 查看 1.0.122 FMT-10 glTF | [126 FMT-10 自验证报告](reference/126-VDE1.0.122-FMT10-glTF-PBR材质自验证报告.md) | diff --git a/docs/reference/129-VDE1.0.125-VDE1269多连通体测量修复自验证报告.md b/docs/reference/129-VDE1.0.125-VDE1269多连通体测量修复自验证报告.md new file mode 100644 index 00000000..ae32fb7e --- /dev/null +++ b/docs/reference/129-VDE1.0.125-VDE1269多连通体测量修复自验证报告.md @@ -0,0 +1,37 @@ +# VDE 1.0.125 VDE-1269 多连通体测量修复自验证报告 + +> 版本 **1.0.125** · 日期 2026-11-02 · 性质:Lead 自验证(独立 verifier 待) +> 反馈:[VDE-1269](../feedback/VDE-1269.md) 多连通 BrepModel analytic_measure/mass_properties 符号相消 +> 基线:1.0.124 全量 7959/7959 PASS + +## 1. 结论 + +一个 BrepModel 含多个不连通闭合壳时,volume/mass 不再全局符号相消为 0;VDE-1269 场景 volume=4000、area=2000、centroid=(0,0,5)。 +**Release 全量 CTest 7962/7962 PASS,0 failed。** + +## 2. 实现 + +- 按几何共享边把面分成 closed component(DSU,兼容 boolean 未完全缝合的重复顶点); +- AABB + solid-angle winding number 建立嵌套树(避免 ray parity 在重复共面三角形上的偶偶误判); +- analytic_measure:每个 component signed triple 先 abs,再按嵌套深度奇偶组合(root +、腔体 −、岛中岛 +);不嵌套 root 各自 abs 相加; +- mass_properties:多分量按同一嵌套树计算体积/一阶矩/二阶矩,嵌套按深度符号,root 质心/惯量正体积加权; +- 单连通仍走原实现;area/centroid 口径不变; +- `analytic_volume`/`BrepModel::body_volume`/`volume` 同步。 + +## 3. 测试与结果 + +- VDE1269Measure.DisjointComponentsVolumeMass:volume=4000、area=2000、centroid=(0,0,5)、mass volume 4000; +- VDE1269Measure.NestedCavityKeepsSign:20³−10³ volume=7000、area=3000; +- VDE1269Measure.SingleConnectedUnchanged:480/376/centroid(1,2,3); +- test_measure 40/40、test_brep_boolean 39/39;定向 Measure/BrepBoolean 51/51。 + +## 4. 验证 + +- 构建 0 error;全量 ctest **7962 注册 / 7962 PASS / 0 failed**(681.93s,20 skip); +- release 门禁提交前复跑(目标 14/14)。 + +## 5. 限制 + +- mesh-only 三角汤 fast path(VDE-1262)在多分量时仍走全局 signed/abs,未纳入本次分组;host 精确 Boolean 路径已覆盖; +- 嵌套关系依赖 component 可三角化;极薄/不水密 shell 会退回各自 abs; +- 1.0.125 尚未独立 verifier。 diff --git a/docs/reference/README.md b/docs/reference/README.md index 72d3f7b5..f5294af7 100644 --- a/docs/reference/README.md +++ b/docs/reference/README.md @@ -1,6 +1,6 @@ # ViewDesignEngine API 参考文档 -> **版本**: 1.0.124 · **分支**: main · **语言**: C++20 · **插件 ABI**: 1.9 +> **版本**: 1.0.125 · **分支**: main · **语言**: C++20 · **插件 ABI**: 1.9 ## 📚 文档索引 @@ -22,6 +22,7 @@ | **1.0.108 计划** | [108-VDE1.0.108改进计划与执行看板](108-VDE1.0.108改进计划与执行看板.md) | 本轮 WP-108-01..10 范围/验收/执行记录 | | **格式专项立项** | [109-格式互操作专项分批计划](../reference/109-格式互操作专项分批计划.md) | F1 STEP/IGES、F2 压缩/网格、F3 DWG/Rhino/PRC、F4 插件化 | | **1.0.108 验证** | [108 自验证报告](108-VDE1.0.108改进批次自验证报告.md) | 7850/7850、定向、Debug ICP/fuzz、覆盖率基线 | +| **1.0.125 VDE-1269** | [129 VDE-1269 自验证报告](129-VDE1.0.125-VDE1269多连通体测量修复自验证报告.md) | 多连通体积/质量;7962/7962 | | **1.0.124 FMT-11 DWFx** | [128 DWFx 自验证报告](128-VDE1.0.124-FMT11-DWFx图像与二进制W2D自验证报告.md) | 图像元数据/W2D 诊断;7959/7959 | | **1.0.123 FMT-11** | [127 FMT-11 自验证报告](127-VDE1.0.123-FMT11-COLLADA材质自验证报告.md) | COLLADA 材质/多 primitive;7955/7955 | | **1.0.122 FMT-10** | [126 FMT-10 自验证报告](126-VDE1.0.122-FMT10-glTF-PBR材质自验证报告.md) | glTF PBR/动画元数据;7952/7952 | diff --git a/include/vde/brep/brep.h b/include/vde/brep/brep.h index 62876713..1b2a561d 100644 --- a/include/vde/brep/brep.h +++ b/include/vde/brep/brep.h @@ -1160,7 +1160,7 @@ public: // VDE-181: 包围盒 + 体属性 // ═══════════════════════════════════════════════ - /** @brief 模型体积近似 (VDE-181) — AABB 体积 */ + /** @brief 体体积(解析质量度量;多连通按 closed component 分组,嵌套腔体相减;VDE-1269) */ [[nodiscard]] double body_volume() const; /** @brief 模型表面积近似 (VDE-181) — AABB 表面积 */ diff --git a/include/vde/brep/measure.h b/include/vde/brep/measure.h index 52df8590..30cf914e 100644 --- a/include/vde/brep/measure.h +++ b/include/vde/brep/measure.h @@ -93,7 +93,7 @@ struct BomEntry; * @return 解析体积(正数)与面积,与 SolidWorks MassProperty 同口径 */ struct AnalyticMeasure { - double volume = 0.0; ///< ∫S·(Su×Sv)/3,封闭整壳 + double volume = 0.0; ///< 多 closed component 分组后的正体积(嵌套腔体相减) double surface_area = 0.0; ///< ∫|Su×Sv| long long evals = 0; ///< [diag] 全件曲面求值次数(= 解析档实际开销) int capped_faces = 0; ///< [diag] 命中求值上限而降级为粗积的面数(>0 表示 diff --git a/include/vde/core/version.h b/include/vde/core/version.h index 005b973e..8c6cc3b0 100644 --- a/include/vde/core/version.h +++ b/include/vde/core/version.h @@ -28,11 +28,11 @@ #define VDE_VERSION_MINOR 0 /// 修订版本号(向后兼容的问题修复) -#define VDE_VERSION_PATCH 124 +#define VDE_VERSION_PATCH 125 /// 版本字符串(与 CHANGELOG / CMake project VERSION 保持同步; /// 1.0.1 之前的漂移已在本版修正) -#define VDE_VERSION_STRING "1.0.124" +#define VDE_VERSION_STRING "1.0.125" // ═══════════════════════════════════════════════════════════════ // D11: API 版本化 + 稳定性声明 @@ -90,7 +90,7 @@ /// 当前版本号整型编码(1.0.83 → 10083;与 VDE_VERSION_MAJOR/MINOR/PATCH 及 /// CHANGELOG / CMake project VERSION 同步) -#define VDE_VERSION_NUM VDE_VERSION_CODE(1, 0, 124) +#define VDE_VERSION_NUM VDE_VERSION_CODE(1, 0, 125) // ═══════════════════════════════════════════════════════════════ // 构建信息(由 CMake configure_file 注入) diff --git a/src/brep/measure.cpp b/src/brep/measure.cpp index 34c79228..ffc2a155 100644 --- a/src/brep/measure.cpp +++ b/src/brep/measure.cpp @@ -16,6 +16,8 @@ #include #include #include +#include +#include using vde::core::AABB3D; using vde::core::Point2D; @@ -745,6 +747,287 @@ FaceMeasure face_measure(const BrepModel& body, int face_index, double deflectio return out; } +namespace { + +// ── VDE-1269:多连通/多体 B-Rep 的 closed component 分组 ────────── +// analytic_measure/mass_properties 的 volume 不能对全部面做一次 signed 求和 +// 后统一 abs:同一 BrepModel 内两个绕向相反的独立闭合壳会抵消为 0。 +// 这里按共享边把面分成连通分量(closed component),再按 AABB+射线包含 +// 关系还原嵌套树:同层不嵌套分量取 abs 后相加,嵌套分量保留符号相减。 + +struct MeasureComponentInfo { + std::vector faces; + AABB3D bounds; +}; + +struct MeasurePartition { + std::vector face_component; // face index -> component index + std::vector components; +}; + +MeasurePartition partition_measure_components(const BrepModel& body) { + MeasurePartition out; + const int n = static_cast(body.num_faces()); + if (n <= 0) return out; + out.face_component.assign(static_cast(n), -1); + std::vector uf(static_cast(n)); + std::iota(uf.begin(), uf.end(), 0); + std::function find = [&](int x) -> int { + return uf[static_cast(x)] == x + ? x + : (uf[static_cast(x)] = find(uf[static_cast(x)])); + }; + auto unite = [&](int a, int b) { + a = find(a); b = find(b); + if (a != b) uf[static_cast(b)] = a; + }; + auto vkey = [&](int vid) -> std::array { + const auto& p = body.vertex(vid).point; + return {std::llround(p.x() * 1e9), std::llround(p.y() * 1e9), + std::llround(p.z() * 1e9)}; + }; + using EdgeKey = std::pair, std::array>; + auto ekey = [&](int eid) -> EdgeKey { + const auto& e = body.edge(eid); + auto a = vkey(e.v_start), b = vkey(e.v_end); + if (a > b) std::swap(a, b); + return {a, b}; + }; + std::map edge_owner; + for (int fi = 0; fi < n; ++fi) { + for (int eid : body.face_edges(fi)) { + const EdgeKey k = ekey(eid); + auto it = edge_owner.find(k); + if (it == edge_owner.end()) edge_owner.emplace(k, fi); + else unite(fi, it->second); + } + } + std::map root_comp; + for (int fi = 0; fi < n; ++fi) { + const int r = find(fi); + auto it = root_comp.find(r); + int ci; + if (it == root_comp.end()) { + ci = static_cast(out.components.size()); + root_comp.emplace(r, ci); + out.components.push_back({}); + } else { + ci = it->second; + } + out.face_component[static_cast(fi)] = ci; + auto& comp = out.components[static_cast(ci)]; + comp.faces.push_back(fi); + comp.bounds.expand(body.face_bounds(fi)); + } + return out; +} + +struct ComponentMeshData { + std::vector verts; + std::vector> tris; + std::vector tri_face; + std::vector>> comp_tris; +}; + +void build_component_mesh(const BrepModel& body, const MeasurePartition& part, + double deflection, ComponentMeshData& out) { + out = ComponentMeshData{}; + body.to_mesh_tagged(deflection, out.verts, out.tris, out.tri_face); + out.comp_tris.assign(part.components.size(), {}); + if (out.tri_face.size() != out.tris.size()) return; + for (size_t ti = 0; ti < out.tris.size(); ++ti) { + const int f = out.tri_face[ti]; + if (f < 0 || f >= static_cast(part.face_component.size())) continue; + const int c = part.face_component[static_cast(f)]; + if (c < 0 || c >= static_cast(out.comp_tris.size())) continue; + out.comp_tris[static_cast(c)].push_back(out.tris[ti]); + } +} + +bool bounds_contain(const AABB3D& outer, const AABB3D& inner) { + const double scale = std::max(1.0, outer.extent().norm()); + const double tol = 1e-9 * scale; + const Point3D& omin = outer.min(); + const Point3D& omax = outer.max(); + const Point3D& imin = inner.min(); + const Point3D& imax = inner.max(); + return imin.x() >= omin.x() - tol && imax.x() <= omax.x() + tol && + imin.y() >= omin.y() - tol && imax.y() <= omax.y() + tol && + imin.z() >= omin.z() - tol && imax.z() <= omax.z() + tol; +} + +bool point_inside_component(const std::vector& verts, + const std::vector>& tris, + const Point3D& p) { + if (tris.empty()) return false; + // Solid-angle winding number:比 ray parity 更能容忍 boolean 结果里 + // 共面重复/重叠三角形(parity 会被重复面翻成偶数而误判)。 + double angle = 0.0; + for (const auto& t : tris) { + const Vector3D a = verts[static_cast(t[0])] - p; + const Vector3D b = verts[static_cast(t[1])] - p; + const Vector3D c = verts[static_cast(t[2])] - p; + const double la = a.norm(), lb = b.norm(), lc = c.norm(); + if (la < 1e-15 || lb < 1e-15 || lc < 1e-15) continue; + const double num = a.dot(b.cross(c)); + const double den = la * lb * lc + a.dot(b) * lc + b.dot(c) * la + + c.dot(a) * lb; + angle += 2.0 * std::atan2(num, den); + } + return std::abs(angle) > 2.0 * 3.14159265358979323846; +} + +std::vector component_parents(const MeasurePartition& part, + const ComponentMeshData& mesh) { + const int nc = static_cast(part.components.size()); + std::vector parents(static_cast(nc), -1); + for (int i = 0; i < nc; ++i) { + const auto& ci = part.components[static_cast(i)]; + if (ci.faces.empty()) continue; + Point3D rep = ci.bounds.center(); + if (!mesh.comp_tris[static_cast(i)].empty()) { + const auto& tri = mesh.comp_tris[static_cast(i)][0]; + rep = mesh.verts[static_cast(tri[0])]; + } + const double ivol = ci.bounds.volume(); + double best_vol = std::numeric_limits::max(); + int best = -1; + for (int j = 0; j < nc; ++j) { + if (j == i) continue; + const auto& cj = part.components[static_cast(j)]; + if (cj.faces.empty()) continue; + const double jvol = cj.bounds.volume(); + if (!(jvol > ivol * (1.0 + 1e-12))) continue; // 防 parent 环 + if (!bounds_contain(cj.bounds, ci.bounds)) continue; + if (!point_inside_component( + mesh.verts, mesh.comp_tris[static_cast(j)], rep)) + continue; + if (jvol < best_vol) { best_vol = jvol; best = j; } + } + parents[static_cast(i)] = best; + } + return parents; +} + +std::vector component_roots(const std::vector& parents) { + std::vector roots(parents.size(), -1); + for (size_t i = 0; i < parents.size(); ++i) { + int x = static_cast(i); + while (x >= 0 && parents[static_cast(x)] >= 0) + x = parents[static_cast(x)]; + roots[i] = x; + } + return roots; +} + +MassProperties mass_properties_multi_component(const BrepModel& body, + const MeasurePartition& part) { + ComponentMeshData mesh; + build_component_mesh(body, part, 0.05, mesh); + const auto parents = component_parents(part, mesh); + const auto roots = component_roots(parents); + const size_t nc = part.components.size(); + std::vector comp_vol(nc, 0.0); + std::vector comp_first(nc, Point3D(0, 0, 0)); + std::vector comp_second(nc, Eigen::Matrix3d::Zero()); + + auto comp = [](const Point3D& p, int i) -> double { + return i == 0 ? p.x() : (i == 1 ? p.y() : p.z()); + }; + for (size_t ti = 0; ti < mesh.tris.size(); ++ti) { + const int f = mesh.tri_face[ti]; + if (f < 0 || f >= static_cast(part.face_component.size())) continue; + const int ci = part.face_component[static_cast(f)]; + if (ci < 0 || ci >= static_cast(nc)) continue; + const auto& tri = mesh.tris[ti]; + const Point3D& v0 = mesh.verts[static_cast(tri[0])]; + const Point3D& v1 = mesh.verts[static_cast(tri[1])]; + const Point3D& v2 = mesh.verts[static_cast(tri[2])]; + const double tet_vol = (v0.cross(v1)).dot(v2) / 6.0; + if (std::abs(tet_vol) < 1e-15) continue; + comp_vol[static_cast(ci)] += tet_vol; + comp_first[static_cast(ci)] += + (v0 + v1 + v2) * (0.25 * tet_vol); + const Point3D* vs[3] = {&v0, &v1, &v2}; + for (int p = 0; p < 3; ++p) { + for (int q = 0; q < 3; ++q) { + const double w = tet_vol * (p == q ? 0.1 : 0.05); + for (int i = 0; i < 3; ++i) + for (int j = 0; j < 3; ++j) + comp_second[static_cast(ci)](i, j) += + w * comp(*vs[p], i) * comp(*vs[q], j); + } + } + } + + std::vector depth(nc, 0); + for (size_t i = 0; i < nc; ++i) { + int x = static_cast(i); + while (x >= 0 && parents[static_cast(x)] >= 0) { + ++depth[i]; + x = parents[static_cast(x)]; + } + } + std::vector root_vol(nc, 0.0); + std::vector root_first(nc, Point3D(0, 0, 0)); + std::vector root_second(nc, Eigen::Matrix3d::Zero()); + for (size_t i = 0; i < nc; ++i) { + const int r = roots[i]; + if (r < 0) continue; + // 分量自身绕向归一为正值,再按嵌套深度奇偶加/减。 + double V = comp_vol[i]; + Point3D M = comp_first[i]; + Eigen::Matrix3d S = comp_second[i]; + if (V < 0.0) { V = -V; M = M * -1.0; S = -S; } + const double sign = (depth[i] % 2 == 1) ? -1.0 : 1.0; + root_vol[static_cast(r)] += sign * V; + root_first[static_cast(r)] += M * sign; + root_second[static_cast(r)] += S * sign; + } + + double total_vol = 0.0; + Point3D weighted_centroid(0, 0, 0); + Eigen::Matrix3d second_moment = Eigen::Matrix3d::Zero(); + for (size_t i = 0; i < nc; ++i) { + if (parents[i] >= 0) continue; // 只看树根 + double V = root_vol[i]; + if (std::abs(V) < 1e-15) continue; + Point3D M = root_first[i]; + Eigen::Matrix3d S = root_second[i]; + if (V < 0.0) { V = -V; M = M * -1.0; S = -S; } + total_vol += V; + weighted_centroid += M; + second_moment += S; + } + MassProperties mp; + if (total_vol < 1e-15) return mp; + mp.volume = total_vol; + mp.centroid = weighted_centroid / total_vol; + const double trace_sm = second_moment(0,0) + second_moment(1,1) + second_moment(2,2); + Eigen::Matrix3d I_ref = -second_moment; + I_ref(0,0) += trace_sm; + I_ref(1,1) += trace_sm; + I_ref(2,2) += trace_sm; + const double m = total_vol; + const double cx = mp.centroid.x(), cy = mp.centroid.y(), cz = mp.centroid.z(); + Eigen::Matrix3d shift; + shift(0,0) = m * (cy*cy + cz*cz); + shift(1,1) = m * (cx*cx + cz*cz); + shift(2,2) = m * (cx*cx + cy*cy); + shift(0,1) = shift(1,0) = -m * cx * cy; + shift(0,2) = shift(2,0) = -m * cx * cz; + shift(1,2) = shift(2,1) = -m * cy * cz; + mp.inertia = I_ref - shift; + auto gyrate = [&](int i) -> double { + const double val = mp.inertia(i,i) / m; + return val > 0 ? std::sqrt(val) : 0.0; + }; + mp.gyration_radii = core::Vector3D(gyrate(0), gyrate(1), gyrate(2)); + return mp; +} + +} // namespace + double analytic_surface_area(const BrepModel& body, double deflection) { return analytic_measure(body, deflection).surface_area; } @@ -772,6 +1055,11 @@ AnalyticMeasure analytic_measure(const BrepModel& body, double deflection) { return fast; } + // VDE-1269:先按共享边把面分成 closed component;volume 分量化后再按 + // 嵌套树组合,避免多个独立壳的 signed triple 全局抵消。 + const MeasurePartition measure_part = partition_measure_components(body); + std::vector comp_triple(measure_part.components.size(), 0.0); + // 单趟:对每个面只调一次 mass_integral(同一遍 Gauss 同时给面积与 // 带符号体积系数),area 直接累加;triple 按面法向反转取带符号, // 封闭整壳 /3 得到体积(同 analytic_volume 语义)。 @@ -812,12 +1100,63 @@ AnalyticMeasure analytic_measure(const BrepModel& body, double deflection) { faces[i].reversed ? 1 : 0, ms); } m.surface_area += fm.area; - if (fm.triple_is_outward) - tri_sum += fm.triple; // 已是外法向口径(平面精确积分/三角化回退) - else - tri_sum += faces[i].reversed ? -fm.triple : fm.triple; + const double signed_triple = + fm.triple_is_outward ? fm.triple + : (faces[i].reversed ? -fm.triple : fm.triple); + tri_sum += signed_triple; + if (i < measure_part.face_component.size()) { + const int ci = measure_part.face_component[i]; + if (ci >= 0 && ci < static_cast(comp_triple.size())) + comp_triple[static_cast(ci)] += signed_triple; + } + } + if (measure_part.components.size() <= 1) { + m.volume = std::abs(tri_sum) / 3.0; // 单连通:保持原口径 + } else { + // 多分量:每个 closed component 内部 signed 求和;仅在有 AABB 包含 + // 候选时才建网格做精确包含判定(不嵌套的并排/多体件无额外开销)。 + bool need_nesting = false; + for (size_t i = 0; i < measure_part.components.size() && !need_nesting; ++i) { + for (size_t j = 0; j < measure_part.components.size(); ++j) { + if (i == j) continue; + if (bounds_contain(measure_part.components[j].bounds, + measure_part.components[i].bounds)) { + need_nesting = true; + break; + } + } + } + std::vector parents(measure_part.components.size(), -1); + if (need_nesting) { + ComponentMeshData mesh; + build_component_mesh(body, measure_part, deflection, mesh); + parents = component_parents(measure_part, mesh); + } + const auto roots = component_roots(parents); + // 嵌套深度奇偶决定加减:root=+,腔体深度1=-,岛中岛深度2=+。 + // 分量自身 triple 先取 abs(忽略单一壳整体绕向),再按深度带符号, + // 与“不相交分量 abs 相加”一致,且不依赖 boolean 是否保留腔体绕向。 + std::vector depth(measure_part.components.size(), 0); + for (size_t ci = 0; ci < measure_part.components.size(); ++ci) { + int x = static_cast(ci); + while (x >= 0 && parents[static_cast(x)] >= 0) { + ++depth[ci]; + x = parents[static_cast(x)]; + } + } + std::vector root_triple(measure_part.components.size(), 0.0); + for (size_t ci = 0; ci < comp_triple.size(); ++ci) { + const int r = roots[ci]; + if (r < 0) continue; + const double sign = (depth[ci] % 2 == 1) ? -1.0 : 1.0; + root_triple[static_cast(r)] += sign * std::abs(comp_triple[ci]); + } + double total = 0.0; + for (size_t ci = 0; ci < root_triple.size(); ++ci) { + if (parents[ci] < 0) total += std::abs(root_triple[ci]) / 3.0; + } + m.volume = total; } - m.volume = std::abs(tri_sum) / 3.0; m.num_faces = static_cast(faces.size()); if (m.capped_faces > 0 || m.nonfinite_faces > 0 || m.tessellated_faces > 0 || m.num_faces > AnalyticMeasure::kFaceCountLimit) { @@ -881,6 +1220,11 @@ Point3D centroid(const BrepModel& body) { // ── mass_properties ───────────────────────────────────────── MassProperties mass_properties(const BrepModel& body) { + // VDE-1269:多连通/多体按 closed component 分组求体积/质心/惯性; + // 单连通保持原实现(单分量走下方原路径,数值口径不变)。 + const MeasurePartition measure_part = partition_measure_components(body); + if (measure_part.components.size() > 1) + return mass_properties_multi_component(body, measure_part); auto mesh = body.to_mesh(0.05); MassProperties mp; diff --git a/tests/brep/test_measure.cpp b/tests/brep/test_measure.cpp index a35371bb..4a1f40ae 100644 --- a/tests/brep/test_measure.cpp +++ b/tests/brep/test_measure.cpp @@ -3,6 +3,7 @@ #include "vde/brep/modeling.h" #include "vde/brep/measure.h" #include "vde/brep/assembly.h" +#include "vde/brep/brep_boolean.h" #include "vde/curves/nurbs_surface.h" #include #include @@ -511,3 +512,64 @@ TEST(AnalyticMeasureTest, MeshOnlyTriangleSoupFastPath) { EXPECT_LE(m.evals, 1000) << "快速路径不应做自适应 Gauss 曲面求值"; EXPECT_LE(ms, 100.0) << "mesh-only 三角汤度量应 <100ms(Release)"; } + +// ═══════════════════════════════════════════════════════════ +// VDE-1269:多连通/多体 BrepModel 的 volume / mass 符号分组 +// ═══════════════════════════════════════════════════════════ + +TEST(VDE1269Measure, DisjointComponentsVolumeMass) { + // 30×20×10 外盒减去贯穿中部的 10×20×10 内盒 → 两个不相交 10×20×10 盒。 + // 两壳 winding 相反时旧实现全局 signed 求和后 abs → 0。 + const Point3D center(0, 0, 5); + BrepModel outer = make_box(30, 20, 10, center); + BrepModel inner = make_box(10, 20, 10, center); + BrepModel cut = brep_difference(outer, inner); + ASSERT_GT(cut.num_faces(), 0u); + EXPECT_GE(cut.all_shells().size(), 1u); + const AnalyticMeasure m = analytic_measure(cut); + EXPECT_NEAR(m.volume, 4000.0, 1e-4) << "两个不连通分量应 abs 后相加"; + EXPECT_NEAR(m.surface_area, 2000.0, 1e-4) << "面积口径不应改变"; + const double v = analytic_volume(cut); + EXPECT_NEAR(v, 4000.0, 1e-4); + EXPECT_NEAR(cut.body_volume(), 4000.0, 1e-4); + const MassProperties mp = mass_properties(cut); + EXPECT_NEAR(mp.volume, 4000.0, 1e-3); + EXPECT_NEAR(mp.centroid.x(), 0.0, 1e-5); + EXPECT_NEAR(mp.centroid.y(), 0.0, 1e-5); + EXPECT_NEAR(mp.centroid.z(), 5.0, 1e-5); + EXPECT_GT(mp.inertia(0, 0), 0.0); + EXPECT_GT(mp.inertia(1, 1), 0.0); + EXPECT_GT(mp.inertia(2, 2), 0.0); +} + +TEST(VDE1269Measure, NestedCavityKeepsSign) { + // 20³ 外盒内挖 10³ 腔体:一个 closed component 的两个嵌套壳,腔体应减。 + const Point3D center(0, 0, 5); + BrepModel outer = make_box(20, 20, 20, center); + BrepModel inner = make_box(10, 10, 10, center); + BrepModel cut = brep_difference(outer, inner); + ASSERT_GT(cut.num_faces(), 0u); + const AnalyticMeasure m = analytic_measure(cut); + EXPECT_NEAR(m.volume, 7000.0, 1e-4) << "嵌套腔体应保留负号相减"; + EXPECT_NEAR(m.surface_area, 3000.0, 1e-4); + const MassProperties mp = mass_properties(cut); + EXPECT_NEAR(mp.volume, 7000.0, 1e-3); + EXPECT_NEAR(mp.centroid.x(), 0.0, 1e-5); + EXPECT_NEAR(mp.centroid.y(), 0.0, 1e-5); + EXPECT_NEAR(mp.centroid.z(), 5.0, 1e-5); + EXPECT_GT(mp.inertia(0, 0), 0.0); + EXPECT_GT(mp.inertia(1, 1), 0.0); + EXPECT_GT(mp.inertia(2, 2), 0.0); +} + +TEST(VDE1269Measure, SingleConnectedUnchanged) { + BrepModel box = make_box(10, 8, 6, Point3D(1, 2, 3)); + const AnalyticMeasure m = analytic_measure(box); + EXPECT_NEAR(m.volume, 480.0, 1e-6); + EXPECT_NEAR(m.surface_area, 2.0 * (10*8 + 10*6 + 8*6), 1e-6); + const MassProperties mp = mass_properties(box); + EXPECT_NEAR(mp.volume, 480.0, 1e-3); + EXPECT_NEAR(mp.centroid.x(), 1.0, 1e-5); + EXPECT_NEAR(mp.centroid.y(), 2.0, 1e-5); + EXPECT_NEAR(mp.centroid.z(), 3.0, 1e-5); +} diff --git a/tests/core/test_version.cpp b/tests/core/test_version.cpp index 2cb5ecd0..fa5fbcc1 100644 --- a/tests/core/test_version.cpp +++ b/tests/core/test_version.cpp @@ -22,15 +22,15 @@ TEST(VersionMacro, MinorVersion) { } TEST(VersionMacro, PatchVersion) { - EXPECT_EQ(VDE_VERSION_PATCH, 124); + EXPECT_EQ(VDE_VERSION_PATCH, 125); } TEST(VersionMacro, VersionString) { - EXPECT_STREQ(VDE_VERSION_STRING, "1.0.124"); + EXPECT_STREQ(VDE_VERSION_STRING, "1.0.125"); } TEST(VersionMacro, VersionNum) { - EXPECT_EQ(VDE_VERSION_NUM, 10124); // 1*10000 + 0*100 + 83 + EXPECT_EQ(VDE_VERSION_NUM, 10125); // 1*10000 + 0*100 + 83 } // 版本口径一致性:字符串 / 整型校验值 / 分量必须互相对齐(防止再次出现 @@ -79,8 +79,8 @@ TEST(VersionInfo, DefaultConstruction) { VersionInfo info = vde_version(); EXPECT_EQ(info.major, 1); EXPECT_EQ(info.minor, 0); - EXPECT_EQ(info.patch, 124); - EXPECT_EQ(info.version_string, "1.0.124"); + EXPECT_EQ(info.patch, 125); + EXPECT_EQ(info.version_string, "1.0.125"); EXPECT_FALSE(info.build_type.empty()); EXPECT_FALSE(info.compiler_info.empty()); EXPECT_EQ(info.cpp_standard, "C++20"); @@ -98,8 +98,8 @@ TEST(VersionInfo, BuildTimestamp) { TEST(VersionString, Format) { std::string vs = vde_version_string(); - // 格式:ViewDesignEngine v1.0.124 (C++20, GCC x.x) - EXPECT_TRUE(vs.find("ViewDesignEngine v1.0.124") != std::string::npos); + // 格式:ViewDesignEngine v1.0.125 (C++20, GCC x.x) + EXPECT_TRUE(vs.find("ViewDesignEngine v1.0.125") != std::string::npos); EXPECT_TRUE(vs.find("C++20") != std::string::npos); EXPECT_TRUE(vs.find("(") != std::string::npos); EXPECT_TRUE(vs.find(")") != std::string::npos);