1633 lines
72 KiB
C++
1633 lines
72 KiB
C++
// KCP-1.0 Wave 1 / W4「语料与基准」:B1 / B2 / B3 / B5 基准 harness 命令行入口。
|
||
//
|
||
// 本文件是一个**独立可执行**(非 gtest),不注册 ctest 用例,因此不会改变
|
||
// 既有 ctest 结果;由 tools/bench_kcp/run_kcp_bench.ps1(或其它脚本)驱动。
|
||
//
|
||
// 子命令(--suite):
|
||
// b1 STEP 布尔通过率:fixtures 目录内全部 .step/.stp -> import_step ->
|
||
// bbox 派生 box cutter 上的 3 个布尔 op(union/intersection/difference),
|
||
// 干净集 + 合成扰动集(P1 顶点确定性抖动 / P2 面方向翻转)。
|
||
// b2 healing 恢复曲线:box/cylinder/sphere 控制集 + 合成脏模型
|
||
// (逐面克隆顶点/边的 detach 缺陷,gap 严重度 0/1e-5/1e-4/1e-3 相对最大尺寸;
|
||
// 以及 detach+隔面翻转) -> validate -> automatic_heal -> validate。
|
||
// 另加仓库内真实脏夹具 tests/data/w0_body87_open_5dangling.vde(存在才跑)。
|
||
// b3 SSI 切线/近重合 KPI(Wave 12 / W71):tests/data 合成近重合盒对、薄重叠
|
||
// slab、相切圆柱-平面、近切球-球小环、部分切向重叠诚实失败、fuzzy off 与
|
||
// W3 容差重载确定性;另用 corpus 最小的 3 个真实 STEP × 3 个布尔 op。
|
||
// 诚实口径:显式 failure_reason+failure_detail 或可验证正确;否则计
|
||
// silent_fail(计划 §5 目标 0 静默失败)。
|
||
// b5 确定性/加速比:固定工作负载多轮跑布尔流水线,输出拓扑+几何 IEEE-754
|
||
// bit digest;线程数由 driver 以 OMP_NUM_THREADS + --threads 双设定。
|
||
//
|
||
// 输出:JSON(字段 bench/suite/pass/total/elapsed_ms/env/git_sha/params/cases)。
|
||
// 用法:bench_kcp --suite b1 --fixtures-root <repo>/tests/data/fixtures
|
||
// --json-out build/kcp_b1.json [--rounds 3] [--threads 4]
|
||
// [--git-sha <sha>] [--workload box_cylinder] [--verbose]
|
||
//
|
||
// 约束(诚实性):
|
||
// - 不伪造“真实语料”:B1 的语料只有仓库内 fixtures(合成 STEP + 1 个 OCCT
|
||
// 官方回归模型),manifest/README 中如实标注;本 harness 不生成任何“真实模型”。
|
||
// - 只读测试数据,不写回仓库。
|
||
|
||
#include "vde/brep/brep.h"
|
||
#include "vde/brep/brep_boolean.h"
|
||
#include "vde/brep/brep_heal.h"
|
||
#include "vde/brep/brep_validate.h"
|
||
#include "vde/brep/modeling.h"
|
||
#include "vde/brep/parallel_boolean.h"
|
||
#include "vde/brep/ssi_boolean.h"
|
||
#include "vde/brep/step_import.h"
|
||
#include "vde/foundation/vde_format.h"
|
||
|
||
#include <algorithm>
|
||
#include <array>
|
||
#include <chrono>
|
||
#include <cmath>
|
||
#include <cstdint>
|
||
#include <cstdlib>
|
||
#include <cstring>
|
||
#include <filesystem>
|
||
#include <fstream>
|
||
#include <iomanip>
|
||
#include <iostream>
|
||
#include <locale>
|
||
#include <sstream>
|
||
#include <string>
|
||
#include <thread>
|
||
#include <unordered_map>
|
||
#include <utility>
|
||
#include <vector>
|
||
|
||
namespace fs = std::filesystem;
|
||
|
||
using vde::core::Point3D;
|
||
using vde::brep::BooleanCheckResult;
|
||
using vde::brep::BrepModel;
|
||
using vde::brep::ValidationResult;
|
||
|
||
namespace {
|
||
|
||
constexpr const char* kBenchId = "KCP-1.0-Wave1-W4";
|
||
constexpr const char* kHarnessVersion = "0.1.0";
|
||
|
||
// ───────────────────────────── 基础工具 ─────────────────────────────
|
||
|
||
double now_ms() {
|
||
using clock = std::chrono::steady_clock;
|
||
static const auto t0 = clock::now();
|
||
return std::chrono::duration<double, std::milli>(clock::now() - t0).count();
|
||
}
|
||
|
||
std::string getenv_str(const char* key) {
|
||
const char* v = std::getenv(key);
|
||
return v ? std::string(v) : std::string();
|
||
}
|
||
|
||
std::string lower_ascii(std::string s) {
|
||
std::transform(s.begin(), s.end(), s.begin(),
|
||
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
|
||
return s;
|
||
}
|
||
|
||
std::string escape_json(const std::string& in) {
|
||
std::string out;
|
||
out.reserve(in.size() + 16);
|
||
for (unsigned char c : in) {
|
||
switch (c) {
|
||
case '"': out += "\\\""; break;
|
||
case '\\': out += "\\\\"; break;
|
||
case '\n': out += "\\n"; break;
|
||
case '\r': out += "\\r"; break;
|
||
case '\t': out += "\\t"; break;
|
||
default:
|
||
if (c < 0x20) {
|
||
char buf[8];
|
||
std::snprintf(buf, sizeof(buf), "\\u%04x", static_cast<unsigned>(c));
|
||
out += buf;
|
||
} else {
|
||
out.push_back(static_cast<char>(c));
|
||
}
|
||
}
|
||
}
|
||
return out;
|
||
}
|
||
|
||
std::string jstr(const std::string& v) { return "\"" + escape_json(v) + "\""; }
|
||
|
||
std::string jnum(double v, int precision = 6) {
|
||
if (!std::isfinite(v)) return "null";
|
||
std::ostringstream os;
|
||
os.imbue(std::locale::classic());
|
||
os << std::fixed << std::setprecision(precision) << v;
|
||
return os.str();
|
||
}
|
||
|
||
std::string jint(long long v) {
|
||
std::ostringstream os;
|
||
os.imbue(std::locale::classic());
|
||
os << v;
|
||
return os.str();
|
||
}
|
||
|
||
// ───────────────────────────── 用例结果 ─────────────────────────────
|
||
|
||
struct CaseResult {
|
||
std::string id;
|
||
std::string group;
|
||
bool pass = false;
|
||
double elapsed_ms = 0.0;
|
||
std::string detail;
|
||
std::vector<std::pair<std::string, double>> metrics;
|
||
std::vector<std::pair<std::string, std::string>> notes;
|
||
|
||
void metric(const std::string& name, double value) { metrics.emplace_back(name, value); }
|
||
void note(const std::string& name, const std::string& value) { notes.emplace_back(name, value); }
|
||
};
|
||
|
||
std::string case_json(const CaseResult& c) {
|
||
std::ostringstream os;
|
||
os.imbue(std::locale::classic());
|
||
os << "{\"id\":" << jstr(c.id) << ",\"group\":" << jstr(c.group)
|
||
<< ",\"status\":" << jstr(c.pass ? "pass" : "fail")
|
||
<< ",\"elapsed_ms\":" << jnum(c.elapsed_ms, 3)
|
||
<< ",\"detail\":" << jstr(c.detail);
|
||
if (!c.metrics.empty()) {
|
||
os << ",\"metrics\":{";
|
||
bool first = true;
|
||
for (const auto& kv : c.metrics) {
|
||
if (!first) os << ",";
|
||
first = false;
|
||
os << jstr(kv.first) << ":" << jnum(kv.second, 6);
|
||
}
|
||
os << "}";
|
||
}
|
||
if (!c.notes.empty()) {
|
||
os << ",\"notes\":{";
|
||
bool first = true;
|
||
for (const auto& kv : c.notes) {
|
||
if (!first) os << ",";
|
||
first = false;
|
||
os << jstr(kv.first) << ":" << jstr(kv.second);
|
||
}
|
||
os << "}";
|
||
}
|
||
os << "}";
|
||
return os.str();
|
||
}
|
||
|
||
// ───────────────────────────── 命令行 ─────────────────────────────
|
||
|
||
struct Cli {
|
||
std::string suite;
|
||
std::string json_out;
|
||
std::string fixtures_root = "tests/data/fixtures";
|
||
std::string test_data_root;
|
||
std::string git_sha;
|
||
std::string workload = "box_cylinder";
|
||
int rounds = 3;
|
||
int threads = 0;
|
||
bool verbose = false;
|
||
bool help = false;
|
||
std::string exe_path;
|
||
};
|
||
|
||
void print_usage() {
|
||
std::cout
|
||
<< "bench_kcp " << kHarnessVersion << " — KCP-1.0 W1/W4 corpus benchmark harness\n"
|
||
<< "usage: bench_kcp --suite <b1|b2|b3|b5> [options]\n"
|
||
<< " --suite <b1|b2|b3|b5> required; benchmark suite\n"
|
||
<< " --fixtures-root <dir> corpus root to scan (default tests/data/fixtures)\n"
|
||
<< " --test-data-root <dir> tests/data root (for w0_body87 dirty fixture)\n"
|
||
<< " --json-out <file> write JSON report to file\n"
|
||
<< " --git-sha <sha> git commit of the checked-out tree\n"
|
||
<< " --rounds <n> b5 repeat count (default 3)\n"
|
||
<< " --threads <n> b5 thread hint (0 = library default)\n"
|
||
<< " --workload <name> b5 workload: box_cylinder | box_cylinder_sphere | torus_box\n"
|
||
<< " --verbose print per-case progress to stderr\n"
|
||
<< " --help this message\n";
|
||
}
|
||
|
||
Cli parse_cli(int argc, char** argv) {
|
||
Cli c;
|
||
c.exe_path = (argc > 0) ? argv[0] : "";
|
||
for (int i = 1; i < argc; ++i) {
|
||
const std::string arg = argv[i];
|
||
auto value = [&](const char* name) -> std::string {
|
||
if (i + 1 >= argc) {
|
||
std::cerr << "missing value for " << name << "\n";
|
||
std::exit(2);
|
||
}
|
||
return argv[++i];
|
||
};
|
||
if (arg == "--suite") c.suite = value("--suite");
|
||
else if (arg == "--json-out") c.json_out = value("--json-out");
|
||
else if (arg == "--fixtures-root") c.fixtures_root = value("--fixtures-root");
|
||
else if (arg == "--test-data-root") c.test_data_root = value("--test-data-root");
|
||
else if (arg == "--git-sha") c.git_sha = value("--git-sha");
|
||
else if (arg == "--workload") c.workload = value("--workload");
|
||
else if (arg == "--rounds") c.rounds = std::stoi(value("--rounds"));
|
||
else if (arg == "--threads") c.threads = std::stoi(value("--threads"));
|
||
else if (arg == "--verbose") c.verbose = true;
|
||
else if (arg == "--help" || arg == "-h") c.help = true;
|
||
else {
|
||
std::cerr << "unknown argument: " << arg << "\n";
|
||
print_usage();
|
||
std::exit(2);
|
||
}
|
||
}
|
||
if (c.suite.empty()) {
|
||
std::cerr << "--suite is required\n";
|
||
print_usage();
|
||
std::exit(2);
|
||
}
|
||
if (c.test_data_root.empty()) {
|
||
// fixtures-root 默认 <repo>/tests/data/fixtures,则 tests/data 为其父目录。
|
||
fs::path p(c.fixtures_root);
|
||
c.test_data_root = p.parent_path().string();
|
||
}
|
||
return c;
|
||
}
|
||
|
||
// ───────────────────────────── 报告 ─────────────────────────────
|
||
|
||
std::string env_json(const Cli& c) {
|
||
std::ostringstream os;
|
||
os.imbue(std::locale::classic());
|
||
os << "{\"os\":\"windows\""
|
||
<< ",\"cpu_threads\":" << std::thread::hardware_concurrency()
|
||
<< ",\"omp_num_threads\":" << jstr(getenv_str("OMP_NUM_THREADS"))
|
||
<< ",\"omp_dynamic\":" << jstr(getenv_str("OMP_DYNAMIC"))
|
||
<< ",\"omp_thread_limit\":" << jstr(getenv_str("OMP_THREAD_LIMIT"))
|
||
<< ",\"arg_threads\":" << c.threads
|
||
<< ",\"parallel_thread_count\":" << vde::brep::parallel_thread_count()
|
||
<< ",\"cwd\":" << jstr(fs::current_path().string())
|
||
<< ",\"exe\":" << jstr(c.exe_path)
|
||
<< ",\"harness_version\":" << jstr(kHarnessVersion)
|
||
<< "}";
|
||
return os.str();
|
||
}
|
||
|
||
std::string params_json(const Cli& c) {
|
||
std::ostringstream os;
|
||
os.imbue(std::locale::classic());
|
||
os << "{\"fixtures_root\":" << jstr(c.fixtures_root)
|
||
<< ",\"test_data_root\":" << jstr(c.test_data_root)
|
||
<< ",\"rounds\":" << c.rounds
|
||
<< ",\"threads\":" << c.threads
|
||
<< ",\"workload\":" << jstr(c.workload);
|
||
os << "}";
|
||
return os.str();
|
||
}
|
||
|
||
struct SuiteResult {
|
||
std::string suite;
|
||
std::vector<CaseResult> cases;
|
||
double elapsed_ms = 0.0;
|
||
std::string summary_json = "{}";
|
||
std::vector<std::string> notes;
|
||
};
|
||
|
||
std::string report_json(const Cli& c, const SuiteResult& r) {
|
||
int pass = 0;
|
||
for (const auto& cs : r.cases) pass += cs.pass ? 1 : 0;
|
||
std::ostringstream os;
|
||
os.imbue(std::locale::classic());
|
||
os << "{\"bench\":" << jstr(kBenchId)
|
||
<< ",\"suite\":" << jstr(r.suite)
|
||
<< ",\"pass\":" << pass
|
||
<< ",\"total\":" << static_cast<int>(r.cases.size())
|
||
<< ",\"elapsed_ms\":" << jnum(r.elapsed_ms, 3)
|
||
<< ",\"git_sha\":" << jstr(c.git_sha)
|
||
<< ",\"env\":" << env_json(c)
|
||
<< ",\"params\":" << params_json(c)
|
||
<< ",\"summary\":" << r.summary_json
|
||
<< ",\"notes\":[";
|
||
for (size_t i = 0; i < r.notes.size(); ++i) {
|
||
if (i) os << ",";
|
||
os << jstr(r.notes[i]);
|
||
}
|
||
os << "],\"cases\":[";
|
||
for (size_t i = 0; i < r.cases.size(); ++i) {
|
||
if (i) os << ",";
|
||
os << case_json(r.cases[i]);
|
||
}
|
||
os << "]}";
|
||
return os.str();
|
||
}
|
||
|
||
bool write_text_file(const std::string& path, const std::string& content) {
|
||
if (path.empty()) return false;
|
||
fs::path p(path);
|
||
std::error_code ec;
|
||
if (p.has_parent_path()) fs::create_directories(p.parent_path(), ec);
|
||
std::ofstream f(p, std::ios::binary | std::ios::trunc);
|
||
if (!f) return false;
|
||
f << content;
|
||
return static_cast<bool>(f);
|
||
}
|
||
|
||
// ───────────────────────────── STEP 发现 ─────────────────────────────
|
||
|
||
struct StepFile {
|
||
fs::path path;
|
||
std::string rel;
|
||
};
|
||
|
||
std::vector<StepFile> discover_step_files(const std::string& root) {
|
||
std::vector<StepFile> files;
|
||
std::error_code ec;
|
||
if (!fs::exists(root, ec)) return files;
|
||
for (const auto& entry : fs::recursive_directory_iterator(
|
||
root, fs::directory_options::skip_permission_denied, ec)) {
|
||
if (ec) { ec.clear(); continue; }
|
||
if (!entry.is_regular_file(ec) || ec) { ec.clear(); continue; }
|
||
const std::string ext = lower_ascii(entry.path().extension().string());
|
||
if (ext != ".step" && ext != ".stp") continue;
|
||
StepFile f;
|
||
f.path = entry.path();
|
||
std::error_code rel_ec;
|
||
fs::path rel = fs::relative(entry.path(), root, rel_ec);
|
||
f.rel = rel_ec ? entry.path().filename().string() : rel.generic_string();
|
||
files.push_back(std::move(f));
|
||
}
|
||
std::sort(files.begin(), files.end(),
|
||
[](const StepFile& a, const StepFile& b) { return a.rel < b.rel; });
|
||
return files;
|
||
}
|
||
|
||
// ───────────────────────────── b1:STEP 布尔通过率 ─────────────────────────────
|
||
|
||
BooleanCheckResult run_boolean_op(const std::string& op, const BrepModel& a, const BrepModel& b) {
|
||
if (op == "union") return vde::brep::brep_union_checked(a, b);
|
||
if (op == "intersection") return vde::brep::brep_intersection_checked(a, b);
|
||
return vde::brep::brep_difference_checked(a, b);
|
||
}
|
||
|
||
// 用 A 的包围盒派生盒体 cutter 集;返回 {op, cutter}。
|
||
std::vector<std::pair<std::string, BrepModel>> make_cutters(const BrepModel& a) {
|
||
std::vector<std::pair<std::string, BrepModel>> cutters;
|
||
const auto bb = a.bounds();
|
||
const Point3D mn = bb.min();
|
||
const Point3D mx = bb.max();
|
||
const double ex = mx.x() - mn.x();
|
||
const double ey = mx.y() - mn.y();
|
||
const double ez = mx.z() - mn.z();
|
||
if (!(ex > 1e-9) || !(ey > 1e-9) || !(ez > 1e-9)) return cutters;
|
||
const Point3D c = bb.center();
|
||
// union:沿 +X/+Y 偏移的大盒,与 A 部分重叠并外伸,保证结果非退化。
|
||
cutters.emplace_back(
|
||
"union", vde::brep::make_box(ex * 0.8, ey * 0.8, ez * 0.8,
|
||
Point3D(c.x() + ex * 0.15, c.y() + ey * 0.15, c.z())));
|
||
// intersection:切一小角,结果既非空也非全 A。
|
||
cutters.emplace_back(
|
||
"intersection", vde::brep::make_box(ex * 0.5, ey * 0.5, ez * 0.5,
|
||
Point3D(c.x() + ex * 0.35, c.y() + ey * 0.35, c.z())));
|
||
// difference:切掉一角。
|
||
cutters.emplace_back(
|
||
"difference", vde::brep::make_box(ex * 0.5, ey * 0.5, ez * 0.6,
|
||
Point3D(c.x() + ex * 0.45, c.y() + ey * 0.45, c.z())));
|
||
return cutters;
|
||
}
|
||
|
||
void run_b1_ops(const BrepModel& a,
|
||
const std::vector<std::pair<std::string, BrepModel>>& cutters,
|
||
const std::string& file_rel,
|
||
const std::string& variant,
|
||
const std::string& group,
|
||
std::vector<CaseResult>& out,
|
||
bool verbose) {
|
||
for (const auto& op : cutters) {
|
||
CaseResult cs;
|
||
cs.id = file_rel + "::" + variant + "::" + op.first;
|
||
cs.group = group;
|
||
const double t0 = now_ms();
|
||
BooleanCheckResult r;
|
||
try {
|
||
r = run_boolean_op(op.first, a, op.second);
|
||
} catch (const std::exception& e) {
|
||
cs.elapsed_ms = now_ms() - t0;
|
||
cs.pass = false;
|
||
cs.detail = std::string("exception: ") + e.what();
|
||
out.push_back(std::move(cs));
|
||
if (verbose) std::cerr << "[b1] " << cs.id << " EXCEPTION\n";
|
||
continue;
|
||
}
|
||
cs.elapsed_ms = now_ms() - t0;
|
||
cs.pass = r.ok() && !r.quality.empty;
|
||
cs.metric("result_faces", static_cast<double>(r.quality.faces));
|
||
cs.metric("result_empty", r.quality.empty ? 1.0 : 0.0);
|
||
cs.metric("watertightness", r.quality.watertightness);
|
||
cs.metric("dangling_edges", static_cast<double>(r.quality.dangling_edges));
|
||
cs.metric("non_manifold_edges", static_cast<double>(r.quality.non_manifold_edges));
|
||
cs.metric("quality_ok", r.ok() ? 1.0 : 0.0);
|
||
if (!cs.pass) {
|
||
std::string why = r.quality.reason.empty() ? "quality_gate_failed" : r.quality.reason;
|
||
if (r.quality.empty) why = "empty_result";
|
||
cs.detail = why + " (wt=" + jnum(r.quality.watertightness, 4) +
|
||
", dangling=" + jint(static_cast<long long>(r.quality.dangling_edges)) + ")";
|
||
} else {
|
||
cs.detail = "ok";
|
||
}
|
||
if (verbose) {
|
||
std::cerr << "[b1] " << cs.id << " " << (cs.pass ? "pass" : "FAIL")
|
||
<< " " << jnum(cs.elapsed_ms, 1) << "ms\n";
|
||
}
|
||
out.push_back(std::move(cs));
|
||
}
|
||
}
|
||
|
||
void add_failure_case(std::vector<CaseResult>& out, const std::string& id,
|
||
const std::string& group, const std::string& detail) {
|
||
CaseResult cs;
|
||
cs.id = id;
|
||
cs.group = group;
|
||
cs.pass = false;
|
||
cs.detail = detail;
|
||
out.push_back(std::move(cs));
|
||
}
|
||
|
||
std::string build_b1_summary(const std::vector<CaseResult>& cases) {
|
||
int clean_pass = 0, clean_total = 0, pert_pass = 0, pert_total = 0;
|
||
int import_pass = 0, import_total = 0;
|
||
for (const auto& c : cases) {
|
||
if (c.group == "import") { import_total++; import_pass += c.pass ? 1 : 0; }
|
||
else if (c.group == "clean") { clean_total++; clean_pass += c.pass ? 1 : 0; }
|
||
else if (c.group.rfind("perturbed_", 0) == 0) {
|
||
pert_total++;
|
||
pert_pass += c.pass ? 1 : 0;
|
||
}
|
||
}
|
||
auto pct = [](int p, int t) { return t > 0 ? (100.0 * p / t) : 100.0; };
|
||
std::ostringstream os;
|
||
os.imbue(std::locale::classic());
|
||
os << "{\"import\":{\"pass\":" << import_pass << ",\"total\":" << import_total
|
||
<< ",\"rate_pct\":" << jnum(pct(import_pass, import_total), 2)
|
||
<< "},\"clean\":{\"pass\":" << clean_pass << ",\"total\":" << clean_total
|
||
<< ",\"rate_pct\":" << jnum(pct(clean_pass, clean_total), 2)
|
||
<< "},\"perturbed\":{\"pass\":" << pert_pass << ",\"total\":" << pert_total
|
||
<< ",\"rate_pct\":" << jnum(pct(pert_pass, pert_total), 2) << "}}";
|
||
return os.str();
|
||
}
|
||
|
||
void run_b1(const Cli& c, SuiteResult& result) {
|
||
const double t_suite = now_ms();
|
||
const auto files = discover_step_files(c.fixtures_root);
|
||
if (files.empty()) {
|
||
add_failure_case(result.cases, "discover::step", "infra",
|
||
"no .step/.stp files under fixtures root: " + c.fixtures_root);
|
||
result.notes.push_back("corpus_missing: fixtures root contains no STEP files");
|
||
result.elapsed_ms = now_ms() - t_suite;
|
||
result.summary_json = build_b1_summary(result.cases);
|
||
return;
|
||
}
|
||
for (const auto& f : files) {
|
||
// import
|
||
CaseResult ic;
|
||
ic.id = f.rel + "::import";
|
||
ic.group = "import";
|
||
const double ti = now_ms();
|
||
std::vector<BrepModel> bodies;
|
||
std::string import_err;
|
||
try {
|
||
bodies = vde::brep::import_step(f.path.string());
|
||
} catch (const std::exception& e) {
|
||
import_err = std::string("exception: ") + e.what();
|
||
}
|
||
ic.elapsed_ms = now_ms() - ti;
|
||
if (bodies.empty()) {
|
||
if (import_err.empty()) {
|
||
import_err = "empty_result";
|
||
try {
|
||
import_err += " step_error=" + std::to_string(
|
||
static_cast<int>(vde::brep::step_last_error()));
|
||
import_err += " msg=" + vde::brep::step_last_error_message();
|
||
} catch (...) {
|
||
import_err += " (step error detail unavailable)";
|
||
}
|
||
}
|
||
ic.pass = false;
|
||
ic.detail = import_err;
|
||
result.cases.push_back(std::move(ic));
|
||
if (c.verbose) std::cerr << "[b1] " << f.rel << " IMPORT FAILED: " << import_err << "\n";
|
||
continue;
|
||
}
|
||
ic.pass = true;
|
||
ic.metric("bodies", static_cast<double>(bodies.size()));
|
||
ic.detail = "bodies=" + jint(static_cast<long long>(bodies.size()));
|
||
result.cases.push_back(std::move(ic));
|
||
|
||
const BrepModel& a = bodies.front();
|
||
ic.metric("first_body_faces", static_cast<double>(a.num_faces()));
|
||
auto cutters = make_cutters(a);
|
||
if (cutters.empty()) {
|
||
add_failure_case(result.cases, f.rel + "::bounds", "infra",
|
||
"degenerate bbox on first imported body");
|
||
continue;
|
||
}
|
||
if (a.num_faces() == 0 || a.num_edges() == 0) {
|
||
add_failure_case(result.cases, f.rel + "::topology", "infra",
|
||
"imported body has no faces/edges");
|
||
continue;
|
||
}
|
||
|
||
// 干净集
|
||
run_b1_ops(a, cutters, f.rel, "clean", "clean", result.cases, c.verbose);
|
||
|
||
// P1 合成扰动:每 5 个顶点沿确定性方向移动 1e-5 * bbox 对角长。
|
||
{
|
||
BrepModel& m = bodies.front();
|
||
std::vector<Point3D> original;
|
||
original.reserve(m.num_vertices());
|
||
for (const auto& v : m.all_vertices()) original.push_back(v.point);
|
||
const auto bb = m.bounds();
|
||
const double diag = (bb.max() - bb.min()).norm();
|
||
const double amp = 1e-5 * (diag > 0.0 ? diag : 1.0);
|
||
for (size_t i = 0; i < original.size(); ++i) {
|
||
if (i % 5 != 0) continue;
|
||
const double a1 = static_cast<double>(i) * 12.9898;
|
||
const double b1 = static_cast<double>(i) * 78.233;
|
||
const double c1 = static_cast<double>(i) * 37.719;
|
||
Point3D dir(std::sin(a1), std::cos(b1), std::sin(c1));
|
||
const double len = dir.norm();
|
||
if (len < 1e-12) continue;
|
||
dir /= len;
|
||
m.set_vertex_point(static_cast<int>(i), original[i] + dir * amp);
|
||
}
|
||
run_b1_ops(m, cutters, f.rel, "jitter", "perturbed_jitter", result.cases, c.verbose);
|
||
for (size_t i = 0; i < original.size(); ++i) {
|
||
m.set_vertex_point(static_cast<int>(i), original[i]);
|
||
}
|
||
}
|
||
|
||
// P2 合成扰动:翻转若干面方向(拓扑不变,方向语义被破坏)。
|
||
{
|
||
BrepModel& m = bodies.front();
|
||
const size_t nf = m.num_faces();
|
||
if (nf == 0) continue;
|
||
std::vector<size_t> flipped;
|
||
for (size_t idx : {size_t{0}, nf / 3, (2 * nf) / 3}) {
|
||
if (idx < nf) flipped.push_back(idx);
|
||
}
|
||
std::sort(flipped.begin(), flipped.end());
|
||
flipped.erase(std::unique(flipped.begin(), flipped.end()), flipped.end());
|
||
for (size_t idx : flipped) {
|
||
m.set_face_reversed(static_cast<int>(idx), !m.all_faces()[idx].reversed);
|
||
}
|
||
run_b1_ops(m, cutters, f.rel, "orientation", "perturbed_orientation", result.cases,
|
||
c.verbose);
|
||
for (size_t idx : flipped) {
|
||
m.set_face_reversed(static_cast<int>(idx), !m.all_faces()[idx].reversed);
|
||
}
|
||
}
|
||
}
|
||
result.elapsed_ms = now_ms() - t_suite;
|
||
result.summary_json = build_b1_summary(result.cases);
|
||
result.notes.push_back("b1 corpus = repository fixtures only (synthetic STEP + 1 OCCT "
|
||
"regression model); no real-customer corpus is present or fabricated");
|
||
result.notes.push_back("perturbations are synthetic deformations (P1 vertex jitter 1e-5*diag, "
|
||
"P2 deterministic face-orientation flips)");
|
||
}
|
||
|
||
// ───────────────────────────── b2:healing 恢复曲线 ─────────────────────────────
|
||
|
||
BrepModel make_primitive(const std::string& name) {
|
||
if (name == "box") return vde::brep::make_box(4.0, 3.0, 2.0);
|
||
if (name == "cylinder") return vde::brep::make_cylinder(1.5, 4.0, 24);
|
||
if (name == "sphere") return vde::brep::make_sphere(1.5, 12, 6);
|
||
return vde::brep::make_box(2.0, 2.0, 2.0);
|
||
}
|
||
|
||
// 把 clean 的每个面“撕开”:每个面拥有独立的顶点/边副本(不共享),并沿
|
||
// “体中心 -> 面质心”方向整体平移 gap_abs(0 表示只撕开不分离)。这是可控、
|
||
// 可复现的合成脏模型来源:gap 严重度决定缝合难度。
|
||
BrepModel detach_faces(const BrepModel& clean, double gap_abs) {
|
||
BrepModel dirty;
|
||
for (size_t s = 0; s < clean.num_surfaces(); ++s) {
|
||
dirty.add_surface(clean.surface(static_cast<int>(s)));
|
||
}
|
||
const auto bb = clean.bounds();
|
||
const Point3D center = bb.center();
|
||
const auto& faces = clean.all_faces();
|
||
const auto& loops = clean.all_loops();
|
||
const auto& edges = clean.all_edges();
|
||
const auto& verts = clean.all_vertices();
|
||
for (size_t fi = 0; fi < faces.size(); ++fi) {
|
||
const auto& face = faces[fi];
|
||
Point3D fc(0.0, 0.0, 0.0);
|
||
size_t cnt = 0;
|
||
for (int li : face.loops) {
|
||
if (li < 0 || static_cast<size_t>(li) >= loops.size()) continue;
|
||
for (int ei : loops[static_cast<size_t>(li)].edges) {
|
||
if (ei < 0 || static_cast<size_t>(ei) >= edges.size()) continue;
|
||
const auto& e = edges[static_cast<size_t>(ei)];
|
||
fc += verts[static_cast<size_t>(e.v_start)].point;
|
||
fc += verts[static_cast<size_t>(e.v_end)].point;
|
||
cnt += 2;
|
||
}
|
||
}
|
||
Point3D offset(0.0, 0.0, 0.0);
|
||
if (gap_abs != 0.0 && cnt > 0) {
|
||
fc /= static_cast<double>(cnt);
|
||
Point3D dir = fc - center;
|
||
const double len = dir.norm();
|
||
if (len > 1e-12) offset = (dir / len) * gap_abs;
|
||
}
|
||
std::unordered_map<int, int> vmap;
|
||
std::vector<int> new_loops;
|
||
for (int li : face.loops) {
|
||
if (li < 0 || static_cast<size_t>(li) >= loops.size()) continue;
|
||
const auto& loop = loops[static_cast<size_t>(li)];
|
||
std::vector<int> new_edges;
|
||
for (int ei : loop.edges) {
|
||
if (ei < 0 || static_cast<size_t>(ei) >= edges.size()) continue;
|
||
const auto& e = edges[static_cast<size_t>(ei)];
|
||
auto get_vertex = [&](int ov) -> int {
|
||
auto it = vmap.find(ov);
|
||
if (it != vmap.end()) return it->second;
|
||
const int nv = dirty.add_vertex(
|
||
verts[static_cast<size_t>(ov)].point + offset);
|
||
vmap[ov] = nv;
|
||
return nv;
|
||
};
|
||
const int nv0 = get_vertex(e.v_start);
|
||
const int nv1 = get_vertex(e.v_end);
|
||
const int ne = e.curve ? dirty.add_edge(nv0, nv1, *e.curve)
|
||
: dirty.add_edge(nv0, nv1);
|
||
new_edges.push_back(ne);
|
||
}
|
||
new_loops.push_back(dirty.add_loop(new_edges, loop.is_outer));
|
||
}
|
||
const int nf = dirty.add_face(face.surface_id, new_loops);
|
||
dirty.set_face_reversed(nf, face.reversed);
|
||
}
|
||
dirty.rebuild_single_shell(true);
|
||
return dirty;
|
||
}
|
||
|
||
struct HealOutcome {
|
||
bool recovered = false;
|
||
ValidationResult before;
|
||
ValidationResult after;
|
||
int healed_items = 0;
|
||
int heal_score = 0;
|
||
int heal_steps = 0;
|
||
bool heal_degraded = false;
|
||
double elapsed_ms = 0.0;
|
||
};
|
||
|
||
HealOutcome heal_and_evaluate(BrepModel& body) {
|
||
HealOutcome o;
|
||
const double t0 = now_ms();
|
||
o.before = vde::brep::validate(body);
|
||
const auto rep = vde::brep::automatic_heal(body);
|
||
o.after = vde::brep::validate(body);
|
||
o.elapsed_ms = now_ms() - t0;
|
||
o.healed_items = rep.total_fixed();
|
||
o.heal_score = rep.overall_score;
|
||
o.heal_steps = rep.successful_steps();
|
||
o.heal_degraded = rep.degraded;
|
||
o.recovered = o.after.valid && o.after.watertightness >= 0.999 &&
|
||
o.after.dangling_edges == 0;
|
||
return o;
|
||
}
|
||
|
||
CaseResult make_heal_case(const std::string& id, const std::string& group,
|
||
const std::string& severity_note, BrepModel& body) {
|
||
CaseResult cs;
|
||
cs.id = id;
|
||
cs.group = group;
|
||
const HealOutcome o = heal_and_evaluate(body);
|
||
cs.elapsed_ms = o.elapsed_ms;
|
||
cs.pass = o.recovered;
|
||
cs.metric("before_valid", o.before.valid ? 1.0 : 0.0);
|
||
cs.metric("before_watertightness", o.before.watertightness);
|
||
cs.metric("before_dangling_edges", static_cast<double>(o.before.dangling_edges));
|
||
cs.metric("before_non_manifold_edges", static_cast<double>(o.before.non_manifold_edges));
|
||
cs.metric("before_errors", static_cast<double>(o.before.errors.size()));
|
||
cs.metric("after_valid", o.after.valid ? 1.0 : 0.0);
|
||
cs.metric("after_watertightness", o.after.watertightness);
|
||
cs.metric("after_dangling_edges", static_cast<double>(o.after.dangling_edges));
|
||
cs.metric("after_non_manifold_edges", static_cast<double>(o.after.non_manifold_edges));
|
||
cs.metric("after_errors", static_cast<double>(o.after.errors.size()));
|
||
cs.metric("healed_items", static_cast<double>(o.healed_items));
|
||
cs.metric("heal_score", static_cast<double>(o.heal_score));
|
||
cs.metric("heal_steps", static_cast<double>(o.heal_steps));
|
||
cs.metric("heal_degraded", o.heal_degraded ? 1.0 : 0.0);
|
||
cs.metric("recovered", o.recovered ? 1.0 : 0.0);
|
||
cs.note("severity", severity_note);
|
||
cs.detail = std::string(o.recovered ? "recovered" : "not_recovered") +
|
||
" (before wt=" + jnum(o.before.watertightness, 4) +
|
||
" dangling=" + jint(static_cast<long long>(o.before.dangling_edges)) +
|
||
"; after wt=" + jnum(o.after.watertightness, 4) +
|
||
" dangling=" + jint(static_cast<long long>(o.after.dangling_edges)) + ")";
|
||
return cs;
|
||
}
|
||
|
||
std::string build_b2_summary(const std::vector<CaseResult>& cases) {
|
||
// 按 group/severity 聚合恢复率,形成 “gap 严重度 -> 恢复率” 曲线。
|
||
struct Agg { int pass = 0; int total = 0; };
|
||
std::vector<std::pair<std::string, Agg>> ordered;
|
||
auto key_of = [](const CaseResult& c) {
|
||
std::string sev;
|
||
for (const auto& n : c.notes) {
|
||
if (n.first == "severity") sev = n.second;
|
||
}
|
||
return c.group + "|" + sev;
|
||
};
|
||
for (const auto& c : cases) {
|
||
const std::string key = key_of(c);
|
||
auto it = std::find_if(ordered.begin(), ordered.end(),
|
||
[&](const auto& kv) { return kv.first == key; });
|
||
if (it == ordered.end()) {
|
||
ordered.emplace_back(key, Agg{});
|
||
it = ordered.end() - 1;
|
||
}
|
||
it->second.total++;
|
||
it->second.pass += c.pass ? 1 : 0;
|
||
}
|
||
std::ostringstream os;
|
||
os.imbue(std::locale::classic());
|
||
os << "{\"curve\":[";
|
||
bool first = true;
|
||
for (const auto& kv : ordered) {
|
||
if (!first) os << ",";
|
||
first = false;
|
||
const auto sep = kv.first.find('|');
|
||
const std::string group = kv.first.substr(0, sep);
|
||
const std::string sev = sep == std::string::npos ? "" : kv.first.substr(sep + 1);
|
||
os << "{\"group\":" << jstr(group) << ",\"severity\":" << jstr(sev)
|
||
<< ",\"pass\":" << kv.second.pass << ",\"total\":" << kv.second.total
|
||
<< ",\"rate_pct\":"
|
||
<< jnum(kv.second.total > 0 ? 100.0 * kv.second.pass / kv.second.total : 0.0, 2)
|
||
<< "}";
|
||
}
|
||
os << "]}";
|
||
return os.str();
|
||
}
|
||
|
||
void run_b2(const Cli& c, SuiteResult& result) {
|
||
const double t_suite = now_ms();
|
||
const std::vector<std::string> prims = {"box", "cylinder", "sphere"};
|
||
for (const auto& name : prims) {
|
||
// 控制集:干净模型 heal 后应保持有效(恢复率基线)。
|
||
{
|
||
BrepModel clean = make_primitive(name);
|
||
CaseResult cs = make_heal_case(name + "::clean_control", "control", "clean", clean);
|
||
result.cases.push_back(std::move(cs));
|
||
}
|
||
// gap 严重度曲线:detach 缺陷 + 径向分离。
|
||
for (double rel : {0.0, 1e-5, 1e-4, 1e-3}) {
|
||
BrepModel clean = make_primitive(name);
|
||
const auto bb = clean.bounds();
|
||
const Point3D ext = bb.extent();
|
||
const double maxdim = std::max({std::abs(ext.x()), std::abs(ext.y()), std::abs(ext.z())});
|
||
BrepModel dirty = detach_faces(clean, rel * (maxdim > 0.0 ? maxdim : 1.0));
|
||
std::ostringstream sev;
|
||
sev.imbue(std::locale::classic());
|
||
sev << "gap_rel=" << rel;
|
||
CaseResult cs = make_heal_case(name + "::detach_gap_" + std::to_string(rel),
|
||
"detach_gap", sev.str(), dirty);
|
||
result.cases.push_back(std::move(cs));
|
||
}
|
||
// detach + 隔面翻转:同时考验缝合与方向统一。
|
||
{
|
||
BrepModel clean = make_primitive(name);
|
||
BrepModel dirty = detach_faces(clean, 0.0);
|
||
for (size_t i = 1; i < dirty.num_faces(); i += 2) {
|
||
dirty.set_face_reversed(static_cast<int>(i), !dirty.all_faces()[i].reversed);
|
||
}
|
||
CaseResult cs = make_heal_case(name + "::detach_flip", "detach_flip",
|
||
"gap_rel=0+flip_every_other", dirty);
|
||
result.cases.push_back(std::move(cs));
|
||
}
|
||
}
|
||
// 仓库内真实脏夹具(开放体 + 5 悬挂边):存在才跑,缺失如实标注。
|
||
{
|
||
const fs::path fixture = fs::path(c.test_data_root) / "w0_body87_open_5dangling.vde";
|
||
if (fs::exists(fixture)) {
|
||
try {
|
||
BrepModel body = vde::foundation::load_vde(fixture.string());
|
||
CaseResult cs = make_heal_case("w0_body87_open_5dangling::heal",
|
||
"fixture_real_dirty", "repository_fixture", body);
|
||
result.cases.push_back(std::move(cs));
|
||
} catch (const std::exception& e) {
|
||
add_failure_case(result.cases, "w0_body87_open_5dangling::load",
|
||
"fixture_real_dirty", std::string("load_vde exception: ") + e.what());
|
||
}
|
||
} else {
|
||
result.notes.push_back("fixture missing: " + fixture.string() + " (skipped, not fabricated)");
|
||
}
|
||
}
|
||
result.elapsed_ms = now_ms() - t_suite;
|
||
result.summary_json = build_b2_summary(result.cases);
|
||
result.notes.push_back("dirty models are synthetic, deterministic deformations of primitives "
|
||
"(per-face detached vertices/edges; radial gap = rel*max_dim)");
|
||
result.notes.push_back("recovered := validate().valid && watertightness>=0.999 && "
|
||
"dangling_edges==0 after automatic_heal()");
|
||
}
|
||
|
||
// ───────────────────────────── b3:SSI 切线/近重合 KPI ─────────────────────────────
|
||
//
|
||
// 诚实口径(KCP-1.0 Wave 12 / W71):
|
||
// - SSI 布尔结果必须落在两类之一:
|
||
// (a) 可验证正确:failure_reason==None 且结果非空、validate().valid 且
|
||
// watertightness>=0.999(合成用例另有更严的合并/守恒断言);
|
||
// (b) 显式诚实失败:failure_reason!=None 且 failure_detail 非空(引擎明确
|
||
// 报告失败原因,绝不静默交付错误体)。
|
||
// - silent_fail = 结果未通过 (a) 却也没有 (b) 的显式失败(含空结果、异常)。
|
||
// 计划 §5 的 3 个月目标即「0 静默失败」,本 suite 把它变成可计数的 KPI。
|
||
// - 合成用例来自 tests/brep/test_ssi_fuzzy_patch.cpp 的同一几何(近重合盒对
|
||
// gap 1e-7/1e-6/1e-5、-1e-6 薄重叠 slab、超出 fuzzy_tol 的 1e-2 大间隙、
|
||
// 相切圆柱-平面、近切球-球小环、部分切向重叠诚实失败、fuzzy off 与 W3
|
||
// 容差重载确定性);real_corpus 用例用 corpus/real 最小的 3 个 STEP 代表
|
||
// 模型 × 3 个布尔 op,记录真实件的 honest/silent 结果(不伪造通过)。
|
||
|
||
using vde::brep::BooleanFailureReason;
|
||
using vde::brep::SSIBooleanFuzzyOptions;
|
||
using vde::brep::SSIBooleanResult;
|
||
using vde::brep::ssi_boolean_difference;
|
||
using vde::brep::ssi_boolean_intersection;
|
||
using vde::brep::ssi_boolean_union;
|
||
|
||
SSIBooleanFuzzyOptions fuzzy_on(double tol) {
|
||
SSIBooleanFuzzyOptions o;
|
||
o.enable = true;
|
||
o.fuzzy_tol = tol;
|
||
return o;
|
||
}
|
||
|
||
BrepModel unit_box(double x, double y = 0.0, double z = 0.0) {
|
||
return vde::brep::make_box(1.0, 1.0, 1.0, Point3D(x, y, z));
|
||
}
|
||
|
||
SSIBooleanResult run_ssi_op(const std::string& op, const BrepModel& a, const BrepModel& b,
|
||
const SSIBooleanFuzzyOptions& opts) {
|
||
if (op == "union") return ssi_boolean_union(a, b, opts);
|
||
if (op == "intersection") return ssi_boolean_intersection(a, b, opts);
|
||
return ssi_boolean_difference(a, b, opts);
|
||
}
|
||
|
||
// 把 SSI 诊断字段写入 case metrics(KPI 审计用;honest_fail/silent_fail 由调用方填)。
|
||
void fill_ssi_metrics(CaseResult& cs, const SSIBooleanResult& r) {
|
||
cs.metric("fuzzy_applied", r.fuzzy_applied ? 1.0 : 0.0);
|
||
cs.metric("near_coincident_pairs", static_cast<double>(r.fuzzy_stats.near_coincident_pairs));
|
||
cs.metric("merged_pairs", static_cast<double>(r.fuzzy_stats.merged_pairs));
|
||
cs.metric("snap_vertices", static_cast<double>(r.fuzzy_stats.snap_vertices));
|
||
cs.metric("dropped_fragments", static_cast<double>(r.fuzzy_stats.dropped_fragments));
|
||
cs.metric("dropped_small_loops", static_cast<double>(r.fuzzy_stats.dropped_small_loops));
|
||
cs.metric("tangent_pairs", static_cast<double>(r.fuzzy_stats.tangent_pairs));
|
||
cs.metric("merged_tangent_points",
|
||
static_cast<double>(r.fuzzy_stats.merged_tangent_points));
|
||
cs.metric("rejected_pairs", static_cast<double>(r.fuzzy_stats.rejected_pairs));
|
||
cs.metric("overlap_volume", r.fuzzy_stats.overlap_volume);
|
||
cs.metric("result_volume", r.result.body_volume());
|
||
cs.metric("expected_volume", r.fuzzy_stats.expected_volume);
|
||
cs.metric("conservation_ok", r.fuzzy_stats.conservation_ok ? 1.0 : 0.0);
|
||
cs.metric("direction_ok", r.fuzzy_stats.direction_ok ? 1.0 : 0.0);
|
||
cs.metric("num_ssi_curves", static_cast<double>(r.num_ssi_curves));
|
||
cs.metric("num_kept", static_cast<double>(r.num_kept));
|
||
cs.metric("result_faces", static_cast<double>(r.result.num_faces()));
|
||
const ValidationResult vr = vde::brep::validate(r.result);
|
||
cs.metric("valid", vr.valid ? 1.0 : 0.0);
|
||
cs.metric("watertightness", vr.watertightness);
|
||
cs.note("failure_reason", static_cast<int>(r.failure_reason) == 0
|
||
? std::string("None")
|
||
: vde::brep::to_string(r.failure_reason));
|
||
if (!r.failure_detail.empty()) cs.note("failure_detail", r.failure_detail);
|
||
}
|
||
|
||
void push_b3_case(std::vector<CaseResult>& out, CaseResult cs, bool honest, bool silent) {
|
||
cs.metric("honest_fail", honest ? 1.0 : 0.0);
|
||
cs.metric("silent_fail", silent ? 1.0 : 0.0);
|
||
out.push_back(std::move(cs));
|
||
}
|
||
|
||
// 通用判定:显式失败=honest(通行);无失败原因则必须可验证正确,否则 silent。
|
||
// 返回 {pass, honest, silent};detail 给出人可读原因。
|
||
struct B3Verdict {
|
||
bool pass = false;
|
||
bool honest = false;
|
||
bool silent = false;
|
||
std::string detail;
|
||
};
|
||
|
||
B3Verdict classify_ssi(const SSIBooleanResult& r, bool require_watertight) {
|
||
B3Verdict v;
|
||
if (r.failure_reason != BooleanFailureReason::None) {
|
||
v.honest = !r.failure_detail.empty();
|
||
v.pass = v.honest;
|
||
v.detail = std::string(v.honest ? "honest_fail" : "failure_without_detail") + ": " +
|
||
vde::brep::to_string(r.failure_reason);
|
||
if (!r.failure_detail.empty()) v.detail += " (" + r.failure_detail + ")";
|
||
return v;
|
||
}
|
||
const ValidationResult vr = vde::brep::validate(r.result);
|
||
const bool nonempty = r.result.num_faces() > 0;
|
||
const bool watertight_ok = !require_watertight || vr.watertightness >= 0.999;
|
||
v.pass = nonempty && vr.valid && watertight_ok;
|
||
if (!v.pass) {
|
||
v.silent = true;
|
||
v.detail = "silent bad result without failure_reason: faces=" +
|
||
jint(static_cast<long long>(r.result.num_faces())) +
|
||
" valid=" + std::string(vr.valid ? "1" : "0") +
|
||
" wt=" + jnum(vr.watertightness, 4);
|
||
} else {
|
||
v.detail = "ok";
|
||
}
|
||
return v;
|
||
}
|
||
|
||
void run_b3_near_coincident(std::vector<CaseResult>& out) {
|
||
struct GapSpec {
|
||
const char* id;
|
||
double gap;
|
||
};
|
||
const GapSpec specs[] = {
|
||
{"synthetic::near_coincident::gap_1e-7", 1e-7},
|
||
{"synthetic::near_coincident::gap_1e-6", 1e-6},
|
||
{"synthetic::near_coincident::gap_1e-5", 1e-5},
|
||
};
|
||
for (const auto& spec : specs) {
|
||
CaseResult cs;
|
||
cs.id = spec.id;
|
||
cs.group = "synthetic_near_coincident";
|
||
const double t0 = now_ms();
|
||
bool honest = false, silent = false;
|
||
try {
|
||
const auto r = ssi_boolean_union(unit_box(0.0), unit_box(1.0 + spec.gap),
|
||
fuzzy_on(1e-4));
|
||
cs.elapsed_ms = now_ms() - t0;
|
||
fill_ssi_metrics(cs, r);
|
||
const ValidationResult vr = vde::brep::validate(r.result);
|
||
const bool merged = r.fuzzy_applied && r.fuzzy_stats.merged_pairs == 1 &&
|
||
r.fuzzy_stats.near_coincident_pairs >= 1 &&
|
||
r.fuzzy_stats.snap_vertices >= 4 &&
|
||
r.fuzzy_stats.dropped_fragments == 2 &&
|
||
r.fuzzy_stats.rejected_pairs == 0 &&
|
||
r.failure_reason == BooleanFailureReason::None &&
|
||
r.result.num_faces() > 0 && r.result.num_faces() <= 10 &&
|
||
vr.valid && vr.watertightness >= 0.999 &&
|
||
r.fuzzy_stats.conservation_ok &&
|
||
r.fuzzy_stats.direction_ok &&
|
||
std::fabs(r.result.body_volume() - 2.0) <= 1e-4;
|
||
cs.pass = merged;
|
||
if (merged) {
|
||
cs.detail = "merged near-coincident union (volume conserved)";
|
||
} else if (r.failure_reason != BooleanFailureReason::None) {
|
||
honest = !r.failure_detail.empty();
|
||
cs.pass = honest;
|
||
cs.detail = "honest_fail: " + vde::brep::to_string(r.failure_reason);
|
||
} else {
|
||
silent = true;
|
||
cs.detail = "silent non-merged result (fuzzy_applied=" +
|
||
std::string(r.fuzzy_applied ? "1" : "0") + ", merged_pairs=" +
|
||
jint(static_cast<long long>(r.fuzzy_stats.merged_pairs)) +
|
||
", faces=" +
|
||
jint(static_cast<long long>(r.result.num_faces())) + ")";
|
||
}
|
||
} catch (const std::exception& e) {
|
||
cs.elapsed_ms = now_ms() - t0;
|
||
cs.detail = std::string("exception: ") + e.what();
|
||
silent = true;
|
||
}
|
||
push_b3_case(out, std::move(cs), honest, silent);
|
||
}
|
||
// 薄重叠 slab(overlap = -1e-6,即间隙为负):同样按重合接口合并。
|
||
{
|
||
CaseResult cs;
|
||
cs.id = "synthetic::near_coincident::thin_slab_overlap_1e-6";
|
||
cs.group = "synthetic_near_coincident";
|
||
const double t0 = now_ms();
|
||
bool honest = false, silent = false;
|
||
try {
|
||
const double overlap = 1e-6;
|
||
const auto r = ssi_boolean_union(unit_box(0.0), unit_box(1.0 - overlap),
|
||
fuzzy_on(1e-4));
|
||
cs.elapsed_ms = now_ms() - t0;
|
||
fill_ssi_metrics(cs, r);
|
||
const ValidationResult vr = vde::brep::validate(r.result);
|
||
const bool merged = r.fuzzy_applied && r.fuzzy_stats.merged_pairs == 1 &&
|
||
r.fuzzy_stats.rejected_pairs == 0 &&
|
||
r.failure_reason == BooleanFailureReason::None &&
|
||
r.result.num_faces() > 0 && r.result.num_faces() <= 10 &&
|
||
vr.valid && vr.watertightness >= 0.999 &&
|
||
r.fuzzy_stats.conservation_ok &&
|
||
std::fabs(r.result.body_volume() - 2.0) <= 1e-4;
|
||
cs.pass = merged;
|
||
if (merged) {
|
||
cs.detail = "merged thin-overlap union (conservation ok)";
|
||
} else if (r.failure_reason != BooleanFailureReason::None) {
|
||
honest = !r.failure_detail.empty();
|
||
cs.pass = honest;
|
||
cs.detail = "honest_fail: " + vde::brep::to_string(r.failure_reason);
|
||
} else {
|
||
silent = true;
|
||
cs.detail = "silent non-merged thin-overlap result";
|
||
}
|
||
} catch (const std::exception& e) {
|
||
cs.elapsed_ms = now_ms() - t0;
|
||
cs.detail = std::string("exception: ") + e.what();
|
||
silent = true;
|
||
}
|
||
push_b3_case(out, std::move(cs), honest, silent);
|
||
}
|
||
// 超出 fuzzy_tol 的大间隙(1e-2):不得误合并(假阳性 = 失败,不计 silent)。
|
||
{
|
||
CaseResult cs;
|
||
cs.id = "synthetic::near_coincident::gap_beyond_tolerance";
|
||
cs.group = "synthetic_near_coincident";
|
||
const double t0 = now_ms();
|
||
bool honest = false, silent = false;
|
||
try {
|
||
const auto r = ssi_boolean_union(unit_box(0.0), unit_box(1.0 + 1e-2),
|
||
fuzzy_on(1e-4));
|
||
cs.elapsed_ms = now_ms() - t0;
|
||
fill_ssi_metrics(cs, r);
|
||
const ValidationResult vr = vde::brep::validate(r.result);
|
||
const bool no_false_merge = !r.fuzzy_applied && r.fuzzy_stats.merged_pairs == 0 &&
|
||
r.result.num_faces() == 12 && vr.valid;
|
||
cs.pass = no_false_merge;
|
||
if (no_false_merge) {
|
||
cs.detail = "gap beyond fuzzy_tol correctly not merged (baseline 12 faces)";
|
||
} else if (r.failure_reason != BooleanFailureReason::None) {
|
||
honest = !r.failure_detail.empty();
|
||
cs.detail = "honest_fail: " + vde::brep::to_string(r.failure_reason);
|
||
} else {
|
||
cs.detail = "false-positive merge or unexpected result (faces=" +
|
||
jint(static_cast<long long>(r.result.num_faces())) + ")";
|
||
}
|
||
} catch (const std::exception& e) {
|
||
cs.elapsed_ms = now_ms() - t0;
|
||
cs.detail = std::string("exception: ") + e.what();
|
||
}
|
||
push_b3_case(out, std::move(cs), honest, silent);
|
||
}
|
||
}
|
||
|
||
void run_b3_tangent(std::vector<CaseResult>& out) {
|
||
// 相切圆柱-平面:平面盒左端面距轴线 1-2e-3,与半径 1 近切。
|
||
{
|
||
CaseResult cs;
|
||
cs.id = "synthetic::tangent::cylinder_plane";
|
||
cs.group = "synthetic_tangent";
|
||
const double t0 = now_ms();
|
||
bool honest = false, silent = false;
|
||
try {
|
||
const auto cyl = vde::brep::make_cylinder(1.0, 2.0, 0);
|
||
const auto box = vde::brep::make_box(1.0, 1.0, 2.0, Point3D(1.5 - 2e-3, 0.0, 0.0));
|
||
const auto r = ssi_boolean_union(cyl, box, fuzzy_on(1e-2));
|
||
cs.elapsed_ms = now_ms() - t0;
|
||
fill_ssi_metrics(cs, r);
|
||
const ValidationResult vr = vde::brep::validate(r.result);
|
||
const bool detected = r.fuzzy_stats.tangent_pairs >= 1 && r.fuzzy_applied &&
|
||
r.result.num_faces() > 0;
|
||
cs.pass = detected && (r.failure_reason == BooleanFailureReason::None || !r.failure_detail.empty());
|
||
if (detected && r.failure_reason != BooleanFailureReason::None) {
|
||
honest = true;
|
||
cs.detail = "tangent detected; explicit failure: " +
|
||
vde::brep::to_string(r.failure_reason);
|
||
} else if (detected) {
|
||
cs.pass = cs.pass && vr.valid;
|
||
cs.detail = "tangent cylinder-plane detected (tangent_pairs=" +
|
||
jint(static_cast<long long>(r.fuzzy_stats.tangent_pairs)) + ")";
|
||
if (!vr.valid) {
|
||
cs.pass = false;
|
||
silent = true;
|
||
cs.detail = "silent invalid tangent result";
|
||
}
|
||
} else {
|
||
cs.pass = false;
|
||
silent = (r.failure_reason == BooleanFailureReason::None);
|
||
cs.detail = "tangent not detected (tangent_pairs=" +
|
||
jint(static_cast<long long>(r.fuzzy_stats.tangent_pairs)) + ")";
|
||
}
|
||
} catch (const std::exception& e) {
|
||
cs.elapsed_ms = now_ms() - t0;
|
||
cs.detail = std::string("exception: ") + e.what();
|
||
silent = true;
|
||
}
|
||
push_b3_case(out, std::move(cs), honest, silent);
|
||
}
|
||
// 近切球-球小环:圆心距 2-1e-3,fuzzy_tol=0.05(小环阈值 0.5)。
|
||
{
|
||
CaseResult cs;
|
||
cs.id = "synthetic::tangent::sphere_sphere_small_loop";
|
||
cs.group = "synthetic_tangent";
|
||
const double t0 = now_ms();
|
||
bool honest = false, silent = false;
|
||
try {
|
||
const auto a = vde::brep::make_sphere(1.0, 8, 4);
|
||
const auto b = vde::brep::make_sphere(1.0, 8, 4, Point3D(2.0 - 1e-3, 0.0, 0.0));
|
||
const auto r = ssi_boolean_union(a, b, fuzzy_on(0.05));
|
||
cs.elapsed_ms = now_ms() - t0;
|
||
fill_ssi_metrics(cs, r);
|
||
const bool dropped = r.fuzzy_stats.dropped_small_loops >= 1 &&
|
||
r.fuzzy_stats.tangent_pairs >= 1 && r.fuzzy_applied;
|
||
cs.pass = dropped;
|
||
if (dropped) {
|
||
cs.detail = "near-tangent sphere small loop dropped (dropped_small_loops=" +
|
||
jint(static_cast<long long>(r.fuzzy_stats.dropped_small_loops)) + ")";
|
||
} else if (r.failure_reason != BooleanFailureReason::None) {
|
||
honest = !r.failure_detail.empty();
|
||
cs.pass = honest;
|
||
cs.detail = "honest_fail: " + vde::brep::to_string(r.failure_reason);
|
||
} else {
|
||
silent = true;
|
||
cs.detail = "silent small-loop not handled";
|
||
}
|
||
} catch (const std::exception& e) {
|
||
cs.elapsed_ms = now_ms() - t0;
|
||
cs.detail = std::string("exception: ") + e.what();
|
||
silent = true;
|
||
}
|
||
push_b3_case(out, std::move(cs), honest, silent);
|
||
}
|
||
// drop_small_loops=false:小环不再丢弃(开关被尊重)。
|
||
{
|
||
CaseResult cs;
|
||
cs.id = "synthetic::tangent::small_loop_discard_off";
|
||
cs.group = "synthetic_tangent";
|
||
const double t0 = now_ms();
|
||
bool honest = false, silent = false;
|
||
try {
|
||
const auto a = vde::brep::make_sphere(1.0, 8, 4);
|
||
const auto b = vde::brep::make_sphere(1.0, 8, 4, Point3D(2.0 - 1e-3, 0.0, 0.0));
|
||
SSIBooleanFuzzyOptions o = fuzzy_on(0.05);
|
||
o.drop_small_loops = false;
|
||
o.split_tangent_points = false;
|
||
const auto r = ssi_boolean_union(a, b, o);
|
||
cs.elapsed_ms = now_ms() - t0;
|
||
fill_ssi_metrics(cs, r);
|
||
cs.pass = r.fuzzy_stats.dropped_small_loops == 0 &&
|
||
r.fuzzy_stats.merged_tangent_points == 0;
|
||
if (cs.pass) {
|
||
cs.detail = "drop_small_loops=false honored (no loops dropped)";
|
||
} else if (r.failure_reason != BooleanFailureReason::None) {
|
||
honest = !r.failure_detail.empty();
|
||
cs.pass = honest;
|
||
cs.detail = "honest_fail: " + vde::brep::to_string(r.failure_reason);
|
||
} else {
|
||
silent = true;
|
||
cs.detail = "silent: switch not honored without failure_reason";
|
||
}
|
||
} catch (const std::exception& e) {
|
||
cs.elapsed_ms = now_ms() - t0;
|
||
cs.detail = std::string("exception: ") + e.what();
|
||
silent = true;
|
||
}
|
||
push_b3_case(out, std::move(cs), honest, silent);
|
||
}
|
||
// 部分切向重叠:切向缝不吻合 -> 必须显式拒绝(诚实失败),回退结果可交付。
|
||
{
|
||
CaseResult cs;
|
||
cs.id = "synthetic::tangent::partial_overlap_honest_fail";
|
||
cs.group = "synthetic_tangent";
|
||
const double t0 = now_ms();
|
||
bool honest = false, silent = false;
|
||
try {
|
||
const auto r = ssi_boolean_union(unit_box(0.0), unit_box(1.0 + 1e-5, 0.5),
|
||
fuzzy_on(1e-4));
|
||
cs.elapsed_ms = now_ms() - t0;
|
||
fill_ssi_metrics(cs, r);
|
||
const bool rejected = !r.fuzzy_applied && r.fuzzy_stats.near_coincident_pairs >= 1 &&
|
||
r.fuzzy_stats.rejected_pairs >= 1 &&
|
||
r.failure_reason == BooleanFailureReason::FallbackLevelRejected &&
|
||
r.failure_detail.find("mismatch") != std::string::npos &&
|
||
r.result.num_faces() > 0;
|
||
cs.pass = rejected;
|
||
honest = rejected;
|
||
if (rejected) {
|
||
cs.detail = "honest failure reported for partial tangential overlap "
|
||
"(rejected_pairs=" +
|
||
jint(static_cast<long long>(r.fuzzy_stats.rejected_pairs)) + ")";
|
||
} else if (r.failure_reason != BooleanFailureReason::None &&
|
||
!r.failure_detail.empty()) {
|
||
honest = true;
|
||
cs.pass = true;
|
||
cs.detail = "honest_fail: " + vde::brep::to_string(r.failure_reason);
|
||
} else {
|
||
silent = r.failure_reason == BooleanFailureReason::None && !r.fuzzy_applied;
|
||
cs.detail = "partial tangential overlap not honestly rejected";
|
||
}
|
||
} catch (const std::exception& e) {
|
||
cs.elapsed_ms = now_ms() - t0;
|
||
cs.detail = std::string("exception: ") + e.what();
|
||
silent = true;
|
||
}
|
||
push_b3_case(out, std::move(cs), honest, silent);
|
||
}
|
||
}
|
||
|
||
void run_b3_determinism(std::vector<CaseResult>& out) {
|
||
const auto a = vde::brep::make_box(2.0, 2.0, 2.0);
|
||
const auto b = vde::brep::make_box(2.0, 2.0, 2.0, Point3D(0.5, 0.25, 0.1));
|
||
const vde::brep::SSIBooleanToleranceOptions tol;
|
||
SSIBooleanFuzzyOptions off; // enable=false:应逐位转发 W3 容差重载
|
||
const char* ops[] = {"union", "intersection", "difference"};
|
||
for (const char* op : ops) {
|
||
CaseResult cs;
|
||
cs.id = std::string("synthetic::determinism::fuzzy_off_") + op;
|
||
cs.group = "synthetic_determinism";
|
||
const double t0 = now_ms();
|
||
bool honest = false, silent = false;
|
||
try {
|
||
SSIBooleanResult rt, rf;
|
||
if (std::string(op) == "union") {
|
||
rt = ssi_boolean_union(a, b, tol);
|
||
rf = ssi_boolean_union(a, b, off);
|
||
} else if (std::string(op) == "intersection") {
|
||
rt = ssi_boolean_intersection(a, b, tol);
|
||
rf = ssi_boolean_intersection(a, b, off);
|
||
} else {
|
||
rt = ssi_boolean_difference(a, b, tol);
|
||
rf = ssi_boolean_difference(a, b, off);
|
||
}
|
||
cs.elapsed_ms = now_ms() - t0;
|
||
fill_ssi_metrics(cs, rf);
|
||
const bool same = rt.result.num_faces() == rf.result.num_faces() &&
|
||
rt.result.num_vertices() == rf.result.num_vertices() &&
|
||
rt.result.num_edges() == rf.result.num_edges() &&
|
||
std::fabs(rt.result.body_volume() - rf.result.body_volume()) <= 1e-9 &&
|
||
rt.num_ssi_curves == rf.num_ssi_curves &&
|
||
rt.num_kept == rf.num_kept && !rf.fuzzy_applied &&
|
||
rf.failure_reason == BooleanFailureReason::None;
|
||
cs.pass = same;
|
||
cs.metric("tolerance_faces", static_cast<double>(rt.result.num_faces()));
|
||
cs.metric("fuzzy_off_faces", static_cast<double>(rf.result.num_faces()));
|
||
cs.metric("volume_delta", std::fabs(rt.result.body_volume() - rf.result.body_volume()));
|
||
if (same) {
|
||
cs.detail = "fuzzy off path identical to tolerance overload";
|
||
} else if (rf.failure_reason != BooleanFailureReason::None) {
|
||
honest = !rf.failure_detail.empty();
|
||
cs.detail = "honest_fail: " + vde::brep::to_string(rf.failure_reason);
|
||
} else {
|
||
cs.detail = "fuzzy off path diverged from tolerance overload";
|
||
}
|
||
} catch (const std::exception& e) {
|
||
cs.elapsed_ms = now_ms() - t0;
|
||
cs.detail = std::string("exception: ") + e.what();
|
||
}
|
||
push_b3_case(out, std::move(cs), honest, silent);
|
||
}
|
||
}
|
||
|
||
void run_b3_real_corpus(std::vector<CaseResult>& out, const Cli& c) {
|
||
const auto files = discover_step_files(c.fixtures_root);
|
||
if (files.empty()) {
|
||
add_failure_case(out, "real_corpus::discover", "real_corpus",
|
||
"no .step/.stp files under fixtures root: " + c.fixtures_root);
|
||
return;
|
||
}
|
||
// 代表模型:取最小的 3 个 STEP(按文件字节数升序),既覆盖真实件,
|
||
// 又把 KPI 运行时间控制在 nightly 预算内(避免 6.5MB 装配体拖垮 300s job)。
|
||
std::vector<StepFile> picked;
|
||
{
|
||
std::vector<std::pair<std::uintmax_t, StepFile>> sized;
|
||
sized.reserve(files.size());
|
||
for (const auto& f : files) {
|
||
std::error_code fec;
|
||
std::uintmax_t sz = fs::file_size(f.path, fec);
|
||
if (fec) sz = static_cast<std::uintmax_t>(-1);
|
||
sized.emplace_back(sz, f);
|
||
}
|
||
std::sort(sized.begin(), sized.end(),
|
||
[](const auto& l, const auto& r) {
|
||
if (l.first != r.first) return l.first < r.first;
|
||
return l.second.rel < r.second.rel;
|
||
});
|
||
for (const auto& kv : sized) {
|
||
if (picked.size() >= 3) break;
|
||
picked.push_back(kv.second);
|
||
}
|
||
}
|
||
for (const auto& f : picked) {
|
||
CaseResult ic;
|
||
ic.id = f.rel + "::ssi::import";
|
||
ic.group = "real_corpus";
|
||
const double ti = now_ms();
|
||
std::vector<BrepModel> bodies;
|
||
std::string import_err;
|
||
try {
|
||
bodies = vde::brep::import_step(f.path.string());
|
||
} catch (const std::exception& e) {
|
||
import_err = std::string("exception: ") + e.what();
|
||
}
|
||
ic.elapsed_ms = now_ms() - ti;
|
||
if (bodies.empty()) {
|
||
ic.pass = false;
|
||
ic.detail = import_err.empty() ? "empty_result" : import_err;
|
||
push_b3_case(out, std::move(ic), false, true);
|
||
if (c.verbose) std::cerr << "[b3] " << f.rel << " IMPORT FAILED\n";
|
||
continue;
|
||
}
|
||
ic.pass = true;
|
||
ic.metric("bodies", static_cast<double>(bodies.size()));
|
||
ic.metric("first_body_faces", static_cast<double>(bodies.front().num_faces()));
|
||
ic.detail = "imported bodies=" + jint(static_cast<long long>(bodies.size()));
|
||
push_b3_case(out, std::move(ic), false, false);
|
||
|
||
const BrepModel& a = bodies.front();
|
||
auto cutters = make_cutters(a);
|
||
if (cutters.empty()) {
|
||
add_failure_case(out, f.rel + "::ssi::bounds", "real_corpus",
|
||
"degenerate bbox on first imported body");
|
||
continue;
|
||
}
|
||
for (const auto& op : cutters) {
|
||
CaseResult cs;
|
||
cs.id = f.rel + "::ssi::" + op.first;
|
||
cs.group = "real_corpus";
|
||
const double t0 = now_ms();
|
||
bool honest = false, silent = false;
|
||
try {
|
||
const auto r = run_ssi_op(op.first, a, op.second, fuzzy_on(1e-4));
|
||
cs.elapsed_ms = now_ms() - t0;
|
||
fill_ssi_metrics(cs, r);
|
||
const B3Verdict v = classify_ssi(r, true);
|
||
cs.pass = v.pass;
|
||
honest = v.honest;
|
||
silent = v.silent;
|
||
cs.detail = v.detail;
|
||
} catch (const std::exception& e) {
|
||
cs.elapsed_ms = now_ms() - t0;
|
||
cs.detail = std::string("exception: ") + e.what();
|
||
silent = true;
|
||
}
|
||
if (c.verbose) {
|
||
std::cerr << "[b3] " << cs.id << " " << (cs.pass ? "pass" : "FAIL")
|
||
<< " " << jnum(cs.elapsed_ms, 1) << "ms\n";
|
||
}
|
||
push_b3_case(out, std::move(cs), honest, silent);
|
||
}
|
||
}
|
||
}
|
||
|
||
std::string build_b3_summary(const std::vector<CaseResult>& cases) {
|
||
struct Agg {
|
||
int pass = 0, total = 0, honest = 0, silent = 0;
|
||
};
|
||
std::vector<std::pair<std::string, Agg>> ordered;
|
||
auto metric_of = [](const CaseResult& c, const char* name) {
|
||
for (const auto& m : c.metrics) {
|
||
if (m.first == name) return m.second;
|
||
}
|
||
return 0.0;
|
||
};
|
||
auto get = [&](const std::string& group) -> Agg& {
|
||
for (auto& kv : ordered) {
|
||
if (kv.first == group) return kv.second;
|
||
}
|
||
ordered.emplace_back(group, Agg{});
|
||
return ordered.back().second;
|
||
};
|
||
Agg total;
|
||
for (const auto& c : cases) {
|
||
Agg& g = get(c.group);
|
||
g.total++;
|
||
g.pass += c.pass ? 1 : 0;
|
||
if (metric_of(c, "honest_fail") == 1.0) g.honest++;
|
||
if (metric_of(c, "silent_fail") == 1.0) g.silent++;
|
||
total.total++;
|
||
total.pass += c.pass ? 1 : 0;
|
||
if (metric_of(c, "honest_fail") == 1.0) total.honest++;
|
||
if (metric_of(c, "silent_fail") == 1.0) total.silent++;
|
||
}
|
||
std::ostringstream os;
|
||
os.imbue(std::locale::classic());
|
||
os << "{\"pass\":" << total.pass << ",\"total\":" << total.total
|
||
<< ",\"honest_fail\":" << total.honest << ",\"silent_fail\":" << total.silent
|
||
<< ",\"groups\":[";
|
||
bool first = true;
|
||
for (const auto& kv : ordered) {
|
||
if (!first) os << ",";
|
||
first = false;
|
||
os << "{\"group\":" << jstr(kv.first) << ",\"pass\":" << kv.second.pass
|
||
<< ",\"total\":" << kv.second.total << ",\"honest_fail\":"
|
||
<< kv.second.honest << ",\"silent_fail\":" << kv.second.silent << "}";
|
||
}
|
||
os << "]}";
|
||
return os.str();
|
||
}
|
||
|
||
void run_b3(const Cli& c, SuiteResult& result) {
|
||
const double t_suite = now_ms();
|
||
run_b3_near_coincident(result.cases);
|
||
run_b3_tangent(result.cases);
|
||
run_b3_determinism(result.cases);
|
||
run_b3_real_corpus(result.cases, c);
|
||
result.elapsed_ms = now_ms() - t_suite;
|
||
result.summary_json = build_b3_summary(result.cases);
|
||
result.notes.push_back("honest KPI: pass = verified-correct result OR explicit "
|
||
"failure_reason+failure_detail; silent_fail = neither");
|
||
result.notes.push_back("synthetic geometry mirrors tests/brep/test_ssi_fuzzy_patch.cpp; "
|
||
"real_corpus uses the 3 smallest corpus/real STEP parts x 3 ops");
|
||
result.notes.push_back("plan section 5 target is silent_fail==0; honest failures are "
|
||
"counted (never rounded up to passes)");
|
||
}
|
||
|
||
// ───────────────────────────── b5:确定性 digest / 加速比 ─────────────────────────────
|
||
|
||
struct BitDigest {
|
||
uint64_t hash = 1469598103934665603ULL; // FNV-1a offset basis
|
||
size_t vertices = 0, edges = 0, loops = 0, faces = 0, shells = 0;
|
||
|
||
void mix(uint64_t v) {
|
||
hash ^= v;
|
||
hash *= 1099511628211ULL;
|
||
}
|
||
void mix_int(int v) { mix(static_cast<uint64_t>(static_cast<int64_t>(v))); }
|
||
void mix_double(double d) {
|
||
uint64_t u = 0;
|
||
std::memcpy(&u, &d, sizeof(u));
|
||
mix(u);
|
||
}
|
||
std::string hex() const {
|
||
std::ostringstream os;
|
||
os.imbue(std::locale::classic());
|
||
os << std::hex << std::setw(16) << std::setfill('0') << hash;
|
||
return os.str();
|
||
}
|
||
};
|
||
|
||
BitDigest compute_digest(const BrepModel& m) {
|
||
BitDigest d;
|
||
d.vertices = m.num_vertices();
|
||
d.edges = m.num_edges();
|
||
d.loops = m.all_loops().size();
|
||
d.faces = m.num_faces();
|
||
d.shells = m.all_shells().size();
|
||
d.mix_int(static_cast<int>(d.vertices));
|
||
d.mix_int(static_cast<int>(d.edges));
|
||
d.mix_int(static_cast<int>(d.loops));
|
||
d.mix_int(static_cast<int>(d.faces));
|
||
d.mix_int(static_cast<int>(d.shells));
|
||
for (const auto& v : m.all_vertices()) {
|
||
d.mix_double(v.point.x());
|
||
d.mix_double(v.point.y());
|
||
d.mix_double(v.point.z());
|
||
d.mix_double(v.tolerance);
|
||
}
|
||
for (const auto& e : m.all_edges()) {
|
||
d.mix_int(e.v_start);
|
||
d.mix_int(e.v_end);
|
||
d.mix_int(e.reversed ? 1 : 0);
|
||
// 只掺入“是否有曲线几何”这一位;不掺指针地址(ASLR 会破坏跨轮确定性)。
|
||
d.mix_int(e.curve ? 1 : 0);
|
||
}
|
||
for (const auto& l : m.all_loops()) {
|
||
d.mix_int(l.is_outer ? 1 : 0);
|
||
d.mix_int(static_cast<int>(l.edges.size()));
|
||
for (int ei : l.edges) d.mix_int(ei);
|
||
}
|
||
for (const auto& f : m.all_faces()) {
|
||
d.mix_int(f.surface_id);
|
||
d.mix_int(f.trimmed_surface_id);
|
||
d.mix_int(f.reversed ? 1 : 0);
|
||
d.mix_int(f.visible ? 1 : 0);
|
||
d.mix_int(static_cast<int>(f.loops.size()));
|
||
for (int li : f.loops) d.mix_int(li);
|
||
}
|
||
for (const auto& s : m.all_shells()) {
|
||
d.mix_int(s.closed ? 1 : 0);
|
||
d.mix_int(static_cast<int>(s.faces.size()));
|
||
for (int fi : s.faces) d.mix_int(fi);
|
||
}
|
||
for (const auto& b : m.all_bodies()) {
|
||
d.mix_int(static_cast<int>(b.shells.size()));
|
||
for (int si : b.shells) d.mix_int(si);
|
||
}
|
||
return d;
|
||
}
|
||
|
||
BrepModel make_workload(const std::string& name) {
|
||
if (name == "torus_box") {
|
||
BrepModel t = vde::brep::make_torus(4.0, 1.2, 48, 24);
|
||
BrepModel cut = vde::brep::make_box(6.0, 6.0, 4.0, Point3D(3.0, 0.0, 0.0));
|
||
return vde::brep::brep_difference(t, cut);
|
||
}
|
||
if (name == "box_cylinder_sphere") {
|
||
BrepModel a = vde::brep::make_box(10.0, 8.0, 6.0);
|
||
BrepModel b = vde::brep::make_cylinder(3.0, 14.0, 64, Point3D(2.0, 1.0, 0.0));
|
||
BrepModel c = vde::brep::make_sphere(3.0, 24, 12, Point3D(-2.0, -1.0, 1.0));
|
||
BrepModel ab = vde::brep::brep_union(a, b);
|
||
BrepModel abc = vde::brep::brep_union(ab, c);
|
||
BrepModel cutter = vde::brep::make_box(4.0, 4.0, 8.0, Point3D(3.0, 2.0, 0.0));
|
||
return vde::brep::brep_difference(abc, cutter);
|
||
}
|
||
// 默认 box_cylinder
|
||
BrepModel a = vde::brep::make_box(10.0, 8.0, 6.0);
|
||
BrepModel b = vde::brep::make_cylinder(3.0, 14.0, 64, Point3D(2.0, 1.0, 0.0));
|
||
BrepModel ab = vde::brep::brep_union(a, b);
|
||
BrepModel cutter = vde::brep::make_box(4.0, 4.0, 8.0, Point3D(3.0, 2.0, 0.0));
|
||
return vde::brep::brep_difference(ab, cutter);
|
||
}
|
||
|
||
std::string build_b5_summary(const std::vector<BitDigest>& digests,
|
||
const std::vector<double>& round_ms) {
|
||
bool identical = true;
|
||
for (size_t i = 1; i < digests.size(); ++i) {
|
||
if (digests[i].hash != digests[0].hash) identical = false;
|
||
}
|
||
double tmin = 1e300;
|
||
std::vector<double> sorted = round_ms;
|
||
std::sort(sorted.begin(), sorted.end());
|
||
for (double t : round_ms) tmin = std::min(tmin, t);
|
||
const double median = sorted.empty() ? 0.0 : sorted[sorted.size() / 2];
|
||
std::ostringstream os;
|
||
os.imbue(std::locale::classic());
|
||
os << "{\"bit_identical\":" << (identical ? "true" : "false")
|
||
<< ",\"digest\":" << jstr(digests.empty() ? "" : digests[0].hex())
|
||
<< ",\"rounds\":" << static_cast<int>(digests.size())
|
||
<< ",\"t_min_ms\":" << jnum(tmin, 3)
|
||
<< ",\"t_median_ms\":" << jnum(median, 3)
|
||
<< ",\"topology\":{\"vertices\":" << static_cast<long long>(digests.empty() ? 0 : digests[0].vertices)
|
||
<< ",\"edges\":" << static_cast<long long>(digests.empty() ? 0 : digests[0].edges)
|
||
<< ",\"loops\":" << static_cast<long long>(digests.empty() ? 0 : digests[0].loops)
|
||
<< ",\"faces\":" << static_cast<long long>(digests.empty() ? 0 : digests[0].faces)
|
||
<< ",\"shells\":" << static_cast<long long>(digests.empty() ? 0 : digests[0].shells)
|
||
<< "}}";
|
||
return os.str();
|
||
}
|
||
|
||
void run_b5(const Cli& c, SuiteResult& result) {
|
||
const double t_suite = now_ms();
|
||
if (c.threads > 0) {
|
||
vde::brep::set_parallel_threads(c.threads);
|
||
}
|
||
const int rounds = std::max(1, c.rounds);
|
||
std::vector<BitDigest> digests;
|
||
std::vector<double> round_ms;
|
||
for (int r = 0; r < rounds; ++r) {
|
||
const double t0 = now_ms();
|
||
BrepModel body = make_workload(c.workload);
|
||
const double ms = now_ms() - t0;
|
||
const BitDigest d = compute_digest(body);
|
||
CaseResult cs;
|
||
cs.id = "round_" + std::to_string(r + 1);
|
||
cs.group = "round";
|
||
cs.pass = d.faces > 0;
|
||
cs.elapsed_ms = ms;
|
||
cs.metric("faces", static_cast<double>(d.faces));
|
||
cs.metric("vertices", static_cast<double>(d.vertices));
|
||
cs.note("digest", d.hex());
|
||
cs.detail = d.hex();
|
||
result.cases.push_back(std::move(cs));
|
||
digests.push_back(d);
|
||
round_ms.push_back(ms);
|
||
}
|
||
// 确定性用例:多轮 digest 必须全部相同。
|
||
CaseResult det;
|
||
det.id = "bit_identical";
|
||
det.group = "determinism";
|
||
bool identical = true;
|
||
for (size_t i = 1; i < digests.size(); ++i) {
|
||
if (digests[i].hash != digests[0].hash) identical = false;
|
||
}
|
||
det.pass = identical;
|
||
det.detail = identical ? "all rounds produced identical topology+geometry digest"
|
||
: "digest mismatch across rounds";
|
||
det.metric("rounds", static_cast<double>(rounds));
|
||
det.metric("bit_identical", identical ? 1.0 : 0.0);
|
||
result.cases.push_back(std::move(det));
|
||
result.elapsed_ms = now_ms() - t_suite;
|
||
result.summary_json = build_b5_summary(digests, round_ms);
|
||
result.notes.push_back("digest covers topology indices + IEEE-754 bits of vertex coordinates "
|
||
"and tolerances; threads set via --threads and OMP_NUM_THREADS");
|
||
result.notes.push_back("speedup is computed by the driver comparing --threads 1 vs N");
|
||
}
|
||
|
||
} // namespace
|
||
|
||
int main(int argc, char** argv) {
|
||
Cli cli = parse_cli(argc, argv);
|
||
if (cli.help) { print_usage(); return 0; }
|
||
|
||
SuiteResult result;
|
||
if (cli.suite == "b1") { result.suite = "B1"; run_b1(cli, result); }
|
||
else if (cli.suite == "b2") { result.suite = "B2"; run_b2(cli, result); }
|
||
else if (cli.suite == "b3") { result.suite = "B3"; run_b3(cli, result); }
|
||
else if (cli.suite == "b5") { result.suite = "B5"; run_b5(cli, result); }
|
||
else {
|
||
std::cerr << "unknown suite: " << cli.suite << "\n";
|
||
print_usage();
|
||
return 2;
|
||
}
|
||
|
||
const std::string json = report_json(cli, result);
|
||
if (!cli.json_out.empty()) {
|
||
if (!write_text_file(cli.json_out, json)) {
|
||
std::cerr << "failed to write JSON report: " << cli.json_out << "\n";
|
||
return 3;
|
||
}
|
||
}
|
||
int pass = 0;
|
||
for (const auto& cs : result.cases) pass += cs.pass ? 1 : 0;
|
||
std::cout << "BENCH " << result.suite << " pass=" << pass << "/" << result.cases.size()
|
||
<< " elapsed_ms=" << jnum(result.elapsed_ms, 1)
|
||
<< (cli.json_out.empty() ? "" : (" json=" + cli.json_out)) << "\n";
|
||
if (!cli.json_out.empty()) {
|
||
std::cout << json << "\n";
|
||
}
|
||
return 0;
|
||
}
|