Files
茂之钳 921c29cb22
Build & Test / build-and-test (push) Waiting to run
Build & Test / python-bindings (push) Blocked by required conditions
CI / Build & Test (push) Failing after 33s
CI / Release Build (push) Failing after 29s
feat(v7): B-Rep deep attack + Class-A surfacing + CAM deep + performance tuning
v7.1 — B-Rep 深度攻坚 (对标 Parasolid 95%):
- advanced_healing: auto_heal_pipeline, face_splitting/merging, topology_optimization
- watertight_verification, tolerance_analysis, tolerance diagnostic report
- sheet_metal: unfold_sheet_metal (K-Factor/BFS), bend_deduction_table, create_flange
- direct_modeling enhanced: draft_face_advanced (hinge), scale_body (non-uniform), mirror_body
- 32 tests (17 healing + 15 sheet metal), syntax-check passed

v7.2 — Class-A 曲面攻坚 (对标 CGM 95%):
- class_a_surfacing: g3_blend (4-row CP), curvature_matching (Levenberg-Marquardt)
- highlight_lines, reflection_lines, iso_photes, surface_diagnosis, shape_modification
- advanced_intersection: robust_ssi (3-stage: AABB+subdivision→Newton 1e-12→singularity)
- curve_surface_intersection, self_intersection_detection (BVH)
- 30 tests (18 class-A + 12 intersection), zero compile errors

v7.3 — CAM 深化 + 性能优化:
- cam_advanced: adaptive_clearing, trochoidal_milling, rest_machining, pencil_tracing
- tool_holder_collision_check, toolpath_optimization, feed_rate_optimization
- performance_tuning: parallel_task_graph (DAG+Kahn), work_stealing_scheduler
- memory_pool_integration, cache_optimization_hints, profile_guided_layout
- Fixed BrepModel API compatibility (body.bounds()/to_mesh() instead of .faces())
- 20 tests

12 files, ~5200 lines, 82 tests
2026-07-26 22:54:46 +08:00

312 lines
11 KiB
C++
Raw Permalink 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.
#pragma once
/**
* @file advanced_healing.h
* @brief 高级 B-Rep 修复 — 对标 Parasolid 95%
*
* 提供 Parasolid 级别的全自动修复流水线和诊断工具:
*
* | 操作 | 对标 Parasolid 功能 |
* |----------------------------|-----------------------------------------|
* | auto_heal_pipeline | auto-heal / optimize |
* | face_splitting | face splitting with p-curve |
* | face_merging | coplanar merge / face stitching |
* | topology_optimization | redundant edge/vertex cleanup |
* | tolerance_analysis | tolerance diagnostic report |
* | watertight_verification | strict watertightness certification |
*
* @ingroup brep
*/
#include "vde/brep/brep.h"
#include "vde/brep/brep_validate.h"
#include "vde/brep/tolerance.h"
#include "vde/brep/brep_heal.h"
#include "vde/curves/nurbs_curve.h"
#include "vde/curves/nurbs_surface.h"
#include <string>
#include <vector>
#include <map>
#include <functional>
namespace vde::brep {
// ═══════════════════════════════════════════════════════════
// 修复诊断报告
// ═══════════════════════════════════════════════════════════
/**
* @brief 高级修复诊断报告
*
* 包含自动修复流水线每一步的详细结果,
* 用于日志记录、UI 展示和下游分析。
*/
struct AdvancedHealingReport {
/** @brief 修复是否完全成功 */
bool success = false;
/** @brief 执行步骤链 */
std::vector<std::string> steps_executed;
/** @brief 每步修复的详情 */
struct StepDetail {
std::string step_name; ///< 步骤名称
bool ok = false; ///< 该步骤是否通过
int items_fixed = 0; ///< 修复项数
std::string message; ///< 该步骤的消息
};
std::vector<StepDetail> details;
/** @brief 修复前验证结果 */
ValidationResult before_validation;
/** @brief 修复后验证结果 */
ValidationResult after_validation;
/** @brief 警告汇总 */
std::vector<std::string> warnings;
/** @brief 总修复项数 */
int total_fixes = 0;
};
// ═══════════════════════════════════════════════════════════
// 容差诊断报告
// ═══════════════════════════════════════════════════════════
/**
* @brief 容差分析项
*
* 记录单个拓扑元素的容差诊断信息。
*/
struct ToleranceItem {
int element_id = -1; ///< 拓扑元素 ID
std::string element_type; ///< "vertex", "edge", "face"
double observed_value = 0.0; ///< 观测值(距离/角度/面积)
double tolerance = 0.0; ///< 适用的容差阈值
bool pass = true; ///< 是否通过检查
std::string description; ///< 可读诊断描述
};
/**
* @brief 容差诊断报告
*
* 全面的几何质量诊断报告,对标 Parasolid 的 tolerance analysis。
* 包含顶点间距、边长度、面面积、角度偏差、水密性间隙等诊断项。
*/
struct ToleranceAnalysisReport {
/** @brief 整体通过 */
bool overall_pass = false;
/** @brief 模型信息 */
size_t total_vertices = 0;
size_t total_edges = 0;
size_t total_faces = 0;
double model_size = 0.0; ///< 包围盒对角线长度
/** @brief 容差配置 */
ToleranceConfig config_used;
/** @brief 诊断项列表 */
std::vector<ToleranceItem> items;
/** @brief 通过/失败统计 */
size_t items_passed = 0;
size_t items_failed = 0;
/** @brief 最小间隙(水密性相关) */
double min_gap = std::numeric_limits<double>::max();
/** @brief 最大间隙 */
double max_gap = 0.0;
/** @brief 平均间隙 */
double avg_gap = 0.0;
/** @brief 间隙超过容差的边对数量 */
size_t gap_violations = 0;
/** @brief 退化元素计数 */
size_t degenerate_edges = 0; ///< 长度 < sliver_area 的边
size_t degenerate_faces = 0; ///< 面积 < sliver_area 的面
size_t near_degenerate_elements = 0; ///< 接近退化的元素
/** @brief 建议的操作列表 */
std::vector<std::string> recommendations;
};
// ═══════════════════════════════════════════════════════════
// 水密性验证
// ═══════════════════════════════════════════════════════════
/**
* @brief 水密性严格验证结果
*
* 对标 Parasolid 的 watertight 验证,提供更详细的间隙/穿透分析。
*/
struct WatertightVerificationResult {
/** @brief 是否水密 */
bool is_watertight = false;
/** @brief 水密性分数 (0.0 ~ 1.0) */
double watertight_score = 0.0;
/** @brief 总边数 */
size_t total_edges = 0;
/** @brief 边界边数(恰好被 1 个面引用的边) */
size_t boundary_edges = 0;
/** @brief 非流形边数(被 > 2 个面引用的边) */
size_t non_manifold_edges = 0;
/** @brief 悬挂边(0 个面引用) */
size_t dangling_edges = 0;
/** @brief 间隙位置列表 */
struct GapLocation {
int edge_id = -1; ///< 间隙对应的边 ID
int face_a = -1; ///< 相邻面 A
int face_b = -1; ///< 相邻面 B (可能为 -1)
double gap_size = 0.0; ///< 间隙尺寸
core::Point3D location; ///< 间隙位置
};
std::vector<GapLocation> gaps;
/** @brief 穿透位置 */
struct PenetrationLocation {
int face_a = -1;
int face_b = -1;
double depth = 0.0;
core::Point3D location;
};
std::vector<PenetrationLocation> penetrations;
/** @brief 壳闭合检查 */
struct ShellStatus {
int shell_id = -1;
bool closed = false;
size_t boundary_edge_count = 0;
};
std::vector<ShellStatus> shell_statuses;
/** @brief 错误消息 */
std::vector<std::string> errors;
};
// ═══════════════════════════════════════════════════════════
// Advanced Healing API
// ═══════════════════════════════════════════════════════════
/**
* @brief 全自动修复流水线
*
* 对标 Parasolid auto-heal。按最佳顺序执行:
*
* 1. 容差分析 → 确定自适应容差
* 2. 间隙闭合 (heal_gaps)
* 3. 退化元素移除 (heal_slivers)
* 4. 冗余边顶点清理 (拓扑优化)
* 5. 共面面合并
* 6. 面方向统一 (heal_orientation)
* 7. 水密性验证
*
* @param body 输入 B-Rep 模型(原地修改)
* @return AdvancedHealingReport 含详细诊断报表
*/
[[nodiscard]] AdvancedHealingReport auto_heal_pipeline(BrepModel& body);
/**
* @brief 面分割 — 使用 p-curve 在参数域分割面
*
* 对标 Parasolid face splitting。根据给定的参数曲线(p-curve)
* 在面的参数域 (u,v) 中切割面,生成两个或多个子面。
*
* 算法:
* 1. 将 p-curve 投影到面的 3D 曲面上
* 2. 查找 p-curve 与面边界的交点
* 3. 在交点和端点处分割原始边
* 4. 为每个子区域构建新环和新面
*
* @param body 输入 B-Rep 模型(原地修改面的拓扑)
* @param face_id 要分割的面索引
* @param pcurve_2d 二维参数曲线(在面的 (u,v) 参数域中定义)
* @return 新生成的面 ID 列表(包含原面,因原地修改后原面 ID 变为第一个新面)
*/
[[nodiscard]] std::vector<int> face_splitting(
BrepModel& body, int face_id,
const curves::NurbsCurve& pcurve_2d);
/**
* @brief 共面面合并
*
* 对标 Parasolid coplanar face merge。
* 检测并合并给定面列表中彼此共面且共享边的面。
*
* 算法:
* 1. 对每个面计算平面方程(法向量 + 距原点距离)
* 2. 分组:具有相同平面方程的面归为一组
* 3. 在每组中查找共享边的面对
* 4. 使用 KEF 欧拉操作合并共享边
* 5. 重建合并面的环和曲面
*
* @param body 输入 B-Rep 模型(原地修改)
* @param face_ids 候选面 ID 列表
* @return 实际合并的面对数量
*/
int face_merging(BrepModel& body, const std::vector<int>& face_ids);
/**
* @brief 拓扑优化 — 冗余边/顶点清理
*
* 对标 Parasolid topology optimization。
* 移除对几何形状无贡献的冗余拓扑元素:
*
* 1. **冗余顶点**: 度为 2 且两侧边共线的顶点 → KEV
* 2. **冗余边**: 共面面之间的共享边 → KEF
* 3. **零长边**: 两端点重叠的边 → 合并顶点 + 移除边
* 4. **冗余内环**: 空内环或无面积内环
*
* @param body 输入 B-Rep 模型(原地修改)
* @return 优化项数(移除的顶点数 + 边数 + 面合并数)
*/
int topology_optimization(BrepModel& body);
/**
* @brief 容差诊断报告
*
* 对标 Parasolid tolerance analysis。
* 生成全面的几何质量诊断报告,包括:
* - 顶点间距检查
* - 边长度检查
* - 面面积与平面性检查
* - 面间角度偏差
* - 水密性间隙扫描
*
* @param body 输入 B-Rep 模型
* @param cfg 容差配置(默认使用全局配置)
* @return ToleranceAnalysisReport 含完整诊断报表
*/
[[nodiscard]] ToleranceAnalysisReport tolerance_analysis(
const BrepModel& body,
const ToleranceConfig& cfg = ToleranceConfig::global());
/**
* @brief 水密性严格验证
*
* 对标 Parasolid watertight verification。
* 提供比 validate() 更详细的水密性分析,包括:
* - 每条边的相邻面数统计
* - 间隙位置精确识别
* - 穿透检测(面间非法相交)
* - 逐壳闭合性检查
* - 0.0~1.0 的连续水密性评分
*
* @param body 输入 B-Rep 模型
* @param tolerance 容差(默认 1e-6
* @return WatertightVerificationResult 含详细报表
*/
[[nodiscard]] WatertightVerificationResult watertight_verification(
const BrepModel& body,
double tolerance = 1e-6);
} // namespace vde::brep