/// @file bench_batch_boolean.cpp /// @brief B-Rep 批量布尔(fuse_all, VDE-170)性能基准工具 /// /// 自包含 chrono 计时版本,不依赖 Google Benchmark(离线可用)。 /// 对比 fuse_all(两两递归 + AABB 空间分区)与朴素逐对合并(无优化)。 /// /// 场景: /// - Overlap :球体排布在晶格上、相邻重叠 20% → 真实布尔合并工作量 /// - Disjoint:球体沿 X 轴线性排列互不接触 → AABB 扫描 + 壳拼接开销 /// /// Usage: /// bench_batch_boolean [overlap|disjoint|all] [N1,N2,...] /// /// 编译(MinGW): /// g++ -O2 -std=c++20 tools/bench_batch_boolean.cpp -Iinclude \ /// -o bench_batch_boolean.exe \ /// src/libvde_foundation.a src/libvde_core.a src/libvde_curves.a \ /// src/libvde_mesh.a src/libvde_spatial.a src/libvde_collision.a \ /// src/libvde_boolean.a src/libvde_brep.a src/libvde_sketch.a \ /// src/libvde_capi.a src/libvde_render.a src/vde_render/libvde_render.a \ /// -fopenmp -lm #include "vde/brep/brep.h" #include "vde/brep/brep_boolean.h" #include "vde/brep/modeling.h" #include "vde/brep/parallel_boolean.h" #include "vde/core/transform.h" #include #include #include #include #include #include #include #include using namespace vde; namespace { using Clock = std::chrono::steady_clock; /// 防止结果被优化掉的空引用点 volatile std::size_t g_sink = 0; /// 生成位于 (x,y,z)、半径为 r 的低细分球体模型 brep::BrepModel sphere_at(double r, double x, double y, double z) { auto s = brep::make_sphere(r, 6, 6); return s.transformed(core::translate(core::Vector3D(x, y, z))); } /// N 个球体排布在晶格上,相邻球体重叠 overlap_frac std::vector make_overlapping_grid(int n, double r, double overlap_frac) { std::vector bodies; bodies.reserve(n); const double spacing = 2.0 * r * (1.0 - overlap_frac); const int per_row = static_cast(std::ceil(std::cbrt(static_cast(n)))); int idx = 0; for (int ix = 0; ix < per_row && idx < n; ++ix) for (int iy = 0; iy < per_row && idx < n; ++iy) for (int iz = 0; iz < per_row && idx < n; ++iz, ++idx) bodies.push_back(sphere_at(r, ix * spacing, iy * spacing, iz * spacing)); return bodies; } /// N 个球体沿 X 轴线性排列,互不接触 std::vector make_disjoint_chain(int n, double r) { std::vector bodies; bodies.reserve(n); for (int i = 0; i < n; ++i) bodies.push_back(sphere_at(r, i * 3.0 * r, 0.0, 0.0)); return bodies; } /// 朴素逐对合并(无 AABB 优化,对照基准) brep::BrepModel naive_fuse(const std::vector& bodies) { if (bodies.empty()) return brep::BrepModel(); brep::BrepModel acc = bodies[0]; for (size_t i = 1; i < bodies.size(); ++i) acc = brep::brep_union(acc, bodies[i]); return acc; } /// 计时一次操作,返回毫秒 template double time_ms(Fn&& fn) { auto t0 = Clock::now(); fn(); auto t1 = Clock::now(); return std::chrono::duration(t1 - t0).count(); } /// 运行 2 次取中位数(减少总耗时) double median_ms(std::vector samples) { std::sort(samples.begin(), samples.end()); return samples[samples.size() / 2]; } template double measure(Fn&& fn) { std::vector samples; samples.reserve(2); for (int i = 0; i < 2; ++i) samples.push_back(time_ms(fn)); return median_ms(samples); } void run_scenario(const char* name, const std::vector& bodies, bool with_naive) { const int n = static_cast(bodies.size()); const double t_fuse = measure([&] { auto r = brep::fuse_all(bodies); g_sink = r.num_faces(); }); double t_naive = 0.0; if (with_naive) { t_naive = measure([&] { auto r = naive_fuse(bodies); g_sink = r.num_faces(); }); } if (with_naive) { std::printf("[%s] N=%4d fuse_all: %9.1f ms naive: %10.1f ms speedup: %6.2fx\n", name, n, t_fuse, t_naive, t_naive / t_fuse); } else { std::printf("[%s] N=%4d fuse_all: %9.1f ms\n", name, n, t_fuse); } } } // namespace int main(int argc, char** argv) { std::string mode = "all"; if (argc > 1) mode = argv[1]; std::fprintf(stderr, "[dbg] argc=%d mode=%s\n", argc, mode.c_str()); std::fflush(stderr); const bool do_overlap = (mode == "all" || mode == "overlap"); const bool do_disjoint = (mode == "all" || mode == "disjoint"); std::printf("=== VDE batch boolean benchmark (fuse_all VDE-170) ===\n"); if (do_overlap) { std::printf("\n-- Overlapping grid (real boolean workload) --\n"); // 小规模:真实布尔合并复杂度随 N 快速增长 for (int n : {5, 10, 20}) { auto bodies = make_overlapping_grid(n, 0.5, 0.2); run_scenario("Overlap", bodies, false); } } if (do_disjoint) { std::printf("\n-- Disjoint chain (AABB scan + shell merge) --\n"); // 不相交合并以 AABB 扫描与壳拼接为主,fuse_all 的 // while 循环每轮 O(M²) 扫描,总复杂度 O(N³),规模需克制 for (int n : {50, 100, 200}) { auto bodies = make_disjoint_chain(n, 0.5); run_scenario("Disjoint", bodies, true); } } (void)g_sink; std::printf("\ndone.\n"); return 0; }