Files
ViewDesignEngine/tests/brep/test_gdt_stackup.cpp
T
hm 07f0493e54 feat: implement deferred roadmap items — complete geometry engine functionality
Phase 1: Fix 6 stubs (ConstraintGraph ctor, G2/G3 continuity, surface_torsion, fill_n_sided, unfold_sheet_metal)

Phase 2: New modules (as_array/from_array, closest_point BVH, tolerance_stackup WorstCase/RSS/MonteCarlo)

Phase 3: Curve factories (arc_3point/rectangle/polygon/slot/ellipse/closed_from_points) + explode_to_brep

Phase 4: Modeling wrappers (wrap/dome, boundary_surface_feature, create_fitting/auto_route_pipe)

Phase 5: Fix pipe_surface guard, constraint_solver warnings, arc_3point angle frame; sync 6 docs to Fixed; 31 tests pass
2026-08-04 23:04:18 +08:00

100 lines
3.5 KiB
C++
Raw 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.
#include <gtest/gtest.h>
#include "vde/brep/gdt_stackup.h"
#include "vde/brep/modeling.h"
#include <cmath>
#include <vector>
using namespace vde::brep;
using namespace vde::core;
// ═══════════════════════════════════════════════════════════
// VDE-496: 公差堆叠分析测试
// ═══════════════════════════════════════════════════════════
static std::vector<StackupDimension> sample_dims() {
return {
{"A", 10.0, 0.1, 0, false},
{"B", 20.0, 0.2, 1, false},
{"C", 5.0, 0.05, 2, false},
};
}
TEST(GdtStackupTest, WorstCase) {
auto dims = sample_dims();
auto r = tolerance_stackup_analysis(dims, StackupMethod::WorstCase);
ASSERT_TRUE(r.success);
// 名义值 = 10 + 20 + 5 = 35
EXPECT_NEAR(r.nominal, 35.0, 1e-9);
// 总公差带 = 0.1 + 0.2 + 0.05 = 0.35
EXPECT_NEAR(r.min_value, 35.0 - 0.35, 1e-9);
EXPECT_NEAR(r.max_value, 35.0 + 0.35, 1e-9);
}
TEST(GdtStackupTest, RSS) {
auto dims = sample_dims();
auto r = tolerance_stackup_analysis(dims, StackupMethod::RSS);
ASSERT_TRUE(r.success);
EXPECT_NEAR(r.nominal, 35.0, 1e-9);
// σ_i = tol_i / 3 → σ_total = √(Σσ_i²)
double expected_sigma = std::sqrt(
(0.1/3)*(0.1/3) + (0.2/3)*(0.2/3) + (0.05/3)*(0.05/3));
EXPECT_NEAR(r.sigma, expected_sigma, 1e-9);
// RSS ±3σ 范围比 WorstCase 更窄
EXPECT_GT(r.min_value, 35.0 - 0.35);
EXPECT_LT(r.max_value, 35.0 + 0.35);
}
TEST(GdtStackupTest, MonteCarlo) {
auto dims = sample_dims();
auto r = tolerance_stackup_analysis(dims, StackupMethod::MonteCarlo, 50000);
ASSERT_TRUE(r.success);
EXPECT_EQ(r.mc_samples, 50000);
// MC 均值应接近名义值 35
EXPECT_NEAR(r.mc_mean, 35.0, 0.05);
// 均匀分布 ±tol 的 σ_i = tol/√3 → σ_total = √(Σ(tol_i/√3)²)
double expected_sigma = std::sqrt(
(0.1*0.1 + 0.2*0.2 + 0.05*0.05) / 3.0);
EXPECT_NEAR(r.mc_stddev, expected_sigma, 0.02);
// 直方图已生成
EXPECT_EQ(r.mc_histogram.size(), 20u);
// 直方图总和 ≈ 1.0
double hist_sum = 0.0;
for (double h : r.mc_histogram) hist_sum += h;
EXPECT_NEAR(hist_sum, 1.0, 1e-6);
}
TEST(GdtStackupTest, Sensitivity) {
auto dims = sample_dims();
auto r = tolerance_stackup_analysis(dims, StackupMethod::WorstCase);
auto sens = tolerance_sensitivity(r);
ASSERT_EQ(sens.size(), 3u);
// 贡献比例 = |tol_i| / Σ|tol_j|, 总和 = 1.0
double total = 0.0;
for (double s : sens) total += s;
EXPECT_NEAR(total, 1.0, 1e-9);
// B 的公差最大 (0.2),贡献比最大
EXPECT_GT(sens[1], sens[0]);
EXPECT_GT(sens[0], sens[2]);
// 验证具体比例:0.2/0.35 ≈ 0.5714
EXPECT_NEAR(sens[1], 0.2 / 0.35, 1e-9);
}
TEST(GdtStackupTest, ExtractFromGDT) {
// 创建一个简单 box 模型作为 body
auto box = make_box(10, 10, 10);
ASSERT_GE(box.num_faces(), 1u);
std::vector<GDTFeature> callouts;
GDTFeature f;
f.type = GDTType::Position;
f.tolerance_value = 0.15;
f.target_face_id = 0;
callouts.push_back(f);
auto dims = extract_stackup_from_gdt(box, callouts);
ASSERT_EQ(dims.size(), 1u);
EXPECT_NEAR(dims[0].tolerance, 0.15, 1e-9);
EXPECT_EQ(dims[0].feature_id, 0);
EXPECT_FALSE(dims[0].is_bonus);
}