4b90438315
P2 — TolerantEdge (ACIS): - TolerantEdge: tube_radius, path_tolerance, is_within_tube, intersects - detect_tolerance_conflicts: VertexInSphere/EdgeInTube/FaceGap - resolve_tolerance_conflicts: auto merge vertices/edges/gaps P2 — .vde Native Format (ACIS): - Binary format: VdeHeader(32B), 6 SerializationSections - save_vde/load_vde: full B-Rep topology + geometry + tolerance + attributes - save_vde_json: debuggable JSON export P2 — Boolean Fallback Ladder (Parasolid): - 4-level cascade: SSI → tolerant → mesh → SDF - Level 4 never fails (SDF + MC mathematical guarantee) - BooleanFallbackResult with per-level diagnostics Total: +~2650 lines across 8 files
136 lines
5.6 KiB
C++
136 lines
5.6 KiB
C++
#pragma once
|
||
/**
|
||
* @file boolean_fallback.h
|
||
* @brief 4级布尔策略回退阶梯
|
||
*
|
||
* 当精确SSI布尔失败时,自动降级尝试更鲁棒的布尔策略:
|
||
*
|
||
* Level 1: ssi_boolean — 精确SSI + 全局容差(B-Rep)
|
||
* Level 2: tolerant_boolean — heal → boolean → heal,容差放大×10(B-Rep)
|
||
* Level 3: mesh_boolean — 网格布尔回退(三角网格)
|
||
* Level 4: SDF boolean — SDF → min/max → Marching Cubes(永不失败)
|
||
*
|
||
* 每一级失败后自动尝试下一级,记录完整的诊断信息。
|
||
*
|
||
* @ingroup brep
|
||
*/
|
||
#include "vde/brep/brep.h"
|
||
#include "vde/brep/tolerant_modeling.h"
|
||
#include "vde/mesh/halfedge_mesh.h"
|
||
#include <optional>
|
||
#include <vector>
|
||
|
||
namespace vde::brep {
|
||
|
||
/// 布尔操作类型(与 vde::mesh::BooleanOp 对齐)
|
||
enum class FallbackBoolOp {
|
||
Union = 0, ///< A ∪ B
|
||
Intersection = 1, ///< A ∩ B
|
||
Difference = 2 ///< A \ B
|
||
};
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// BooleanFallbackResult — 回退结果
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief 布尔策略回退结果
|
||
*
|
||
* 包含最终使用的级别、结果几何体和完整的诊断链。
|
||
* final_level = 1~4,数值越小质量越高。
|
||
*/
|
||
struct BooleanFallbackResult {
|
||
int final_level = 4; ///< 最终使用的级别 (1-4)
|
||
mesh::HalfedgeMesh result_body; ///< 结果几何体(统一为网格表示)
|
||
std::optional<BrepModel> brep_body; ///< B-Rep结果(仅Level 1/2有效)
|
||
BooleanDiagnostic diagnostic; ///< 最终诊断(融合各级信息)
|
||
std::vector<int> levels_tried; ///< 尝试过的级别(含失败的)
|
||
std::vector<BooleanDiagnostic> level_diagnostics; ///< 每一级的诊断快照
|
||
bool brep_quality = false; ///< 结果是否为B-Rep精度
|
||
|
||
/// 结果是否成功(至少有一级成功)
|
||
[[nodiscard]] bool success() const {
|
||
return result_body.num_vertices() > 0 || !diagnostic.error_message.empty();
|
||
}
|
||
|
||
/// 摘要报告
|
||
[[nodiscard]] std::string summary() const;
|
||
};
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// brep_boolean_robust — 四级策略回退
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief 鲁棒布尔运算(四级策略回退)
|
||
*
|
||
* 按优先级依次尝试4种布尔策略,任一成功即返回。
|
||
*
|
||
* **回退阶梯:**
|
||
*
|
||
* | 级别 | 策略 | 失败条件 | 输出类型 |
|
||
* |------|------------------|--------------------------|---------|
|
||
* | 1 | ssi_boolean | 有 failed_faces | B-Rep |
|
||
* | 2 | tolerant_boolean | 仍有 failed_faces | B-Rep |
|
||
* | 3 | mesh_boolean | 崩溃/异常 | 网格 |
|
||
* | 4 | SDF boolean | 永不失败(数学保证) | 网格 |
|
||
*
|
||
* Level 2 在 Level 1 失败后使用放大10倍的容差重新尝试。
|
||
* Level 3 将两实体 tessellate 为三角网格后执行网格布尔。
|
||
* Level 4 使用 SDF + Marching Cubes 保证总能返回结果。
|
||
*
|
||
* @param a 第一个 B-Rep 实体
|
||
* @param b 第二个 B-Rep 实体
|
||
* @param op 布尔操作类型
|
||
* @return BooleanFallbackResult 含结果和诊断链
|
||
*
|
||
* @code{.cpp}
|
||
* auto result = brep_boolean_robust(box, sphere, FallbackBoolOp::Union);
|
||
* if (result.final_level == 1) {
|
||
* // 精确 B-Rep 结果
|
||
* export_step(result.brep_body.value(), "out.step");
|
||
* } else {
|
||
* // 使用网格结果
|
||
* export_obj(result.result_body, "out.obj");
|
||
* }
|
||
* @endcode
|
||
*/
|
||
[[nodiscard]] BooleanFallbackResult brep_boolean_robust(
|
||
const BrepModel& a,
|
||
const BrepModel& b,
|
||
FallbackBoolOp op);
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 每级内部函数(跨编译单元可见,便于测试)
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief Level 1: SSI精确布尔
|
||
* @return pair<BooleanDiagnostic, bool> — 第二个为是否成功
|
||
*/
|
||
[[nodiscard]] std::pair<BooleanDiagnostic, bool>
|
||
try_ssi_boolean(const BrepModel& a, const BrepModel& b, FallbackBoolOp op);
|
||
|
||
/**
|
||
* @brief Level 2: 容差感知布尔(容差放大×10)
|
||
* @return pair<BooleanDiagnostic, bool>
|
||
*/
|
||
[[nodiscard]] std::pair<BooleanDiagnostic, bool>
|
||
try_tolerant_boolean(const BrepModel& a, const BrepModel& b, FallbackBoolOp op);
|
||
|
||
/**
|
||
* @brief Level 3: 网格布尔回退
|
||
* @return pair<mesh::HalfedgeMesh, bool> — 第二个为是否成功
|
||
*/
|
||
[[nodiscard]] std::pair<mesh::HalfedgeMesh, bool>
|
||
try_mesh_boolean_fallback(const BrepModel& a, const BrepModel& b, FallbackBoolOp op);
|
||
|
||
/**
|
||
* @brief Level 4: SDF布尔(永不失败)
|
||
* @return mesh::HalfedgeMesh 结果网格
|
||
*/
|
||
[[nodiscard]] mesh::HalfedgeMesh
|
||
try_sdf_boolean_fallback(const BrepModel& a, const BrepModel& b, FallbackBoolOp op);
|
||
|
||
} // namespace vde::brep
|