#include #include "vde_render/section_view.h" #include "vde_render/render_types.h" #include "vde/brep/modeling.h" #include "vde/core/point.h" #include "vde/core/matrix4d.h" using namespace vde::render; using namespace vde::core; /// 创建一个简单的盒子用于截面测试 static vde::brep::BrepModel make_test_body() { return vde::brep::make_box(10, 10, 10); } // ═══════════════════════════════════════════════════════════ // 活动截面平面默认值 // ═══════════════════════════════════════════════════════════ TEST(SectionViewTest, ActiveSectionPlane_Default) { EXPECT_EQ(active_section_plane(), -1); } // ═══════════════════════════════════════════════════════════ // 截面平面数量 // ═══════════════════════════════════════════════════════════ TEST(SectionViewTest, SectionPlaneCount_StartsAtZero) { EXPECT_EQ(section_plane_count(), 0); } TEST(SectionViewTest, SectionPlaneCount_AfterAdd) { auto body = make_test_body(); add_section_plane(body, Point3D(0, 0, 0), Vector3D(0, 0, 1)); EXPECT_GE(section_plane_count(), 1); } // ═══════════════════════════════════════════════════════════ // add_section_plane 返回有效索引 // ═══════════════════════════════════════════════════════════ TEST(SectionViewTest, AddSectionPlane_ReturnsValidIndex) { auto body = make_test_body(); int idx = add_section_plane(body, Point3D(0, 0, 0), Vector3D(0, 0, 1)); EXPECT_GE(idx, 0); } // ═══════════════════════════════════════════════════════════ // 多次添加返回递增索引 // ═══════════════════════════════════════════════════════════ TEST(SectionViewTest, AddSectionPlane_IncreasingIndices) { auto body = make_test_body(); int i0 = add_section_plane(body, Point3D(0, 0, 0), Vector3D(0, 0, 1)); int i1 = add_section_plane(body, Point3D(0, 0, 5), Vector3D(0, 0, 1)); int i2 = add_section_plane(body, Point3D(0, 0, -5), Vector3D(0, 0, 1)); EXPECT_LT(i0, i1); EXPECT_LT(i1, i2); } // ═══════════════════════════════════════════════════════════ // remove_section_plane 减少数量 // ═══════════════════════════════════════════════════════════ TEST(SectionViewTest, RemoveSectionPlane_ReducesCount) { auto body = make_test_body(); int i0 = add_section_plane(body, Point3D(0, 0, 0), Vector3D(0, 0, 1)); int i1 = add_section_plane(body, Point3D(0, 0, 5), Vector3D(0, 0, 1)); int count_before = section_plane_count(); remove_section_plane(i0); EXPECT_EQ(section_plane_count(), count_before - 1); } // ═══════════════════════════════════════════════════════════ // get_section_plane 返回正确的点/法向量 // ═══════════════════════════════════════════════════════════ TEST(SectionViewTest, GetSectionPlane_CorrectPointAndNormal) { auto body = make_test_body(); int idx = add_section_plane(body, Point3D(1.0, 2.0, 3.0), Vector3D(0.0, 0.0, 1.0)); auto plane = get_section_plane(idx); EXPECT_NEAR(plane.point.x(), 1.0, 1e-9); EXPECT_NEAR(plane.point.y(), 2.0, 1e-9); EXPECT_NEAR(plane.point.z(), 3.0, 1e-9); EXPECT_NEAR(plane.normal.x(), 0.0, 1e-9); EXPECT_NEAR(plane.normal.y(), 0.0, 1e-9); EXPECT_NEAR(plane.normal.z(), 1.0, 1e-9); } TEST(SectionViewTest, GetSectionPlane_DifferentNormals) { auto body = make_test_body(); int idx = add_section_plane(body, Point3D(0, 0, 0), Vector3D(1.0, 0.0, 0.0)); auto plane = get_section_plane(idx); EXPECT_NEAR(plane.normal.x(), 1.0, 1e-9); EXPECT_NEAR(plane.normal.y(), 0.0, 1e-9); EXPECT_NEAR(plane.normal.z(), 0.0, 1e-9); } // ═══════════════════════════════════════════════════════════ // set_section_plane_transform 更新截面平面 // ═══════════════════════════════════════════════════════════ TEST(SectionViewTest, SetSectionPlaneTransform_UpdatePosition) { auto body = make_test_body(); int idx = add_section_plane(body, Point3D(0, 0, 0), Vector3D(0, 0, 1)); // 通过变换矩阵平移到新位置 Matrix4D T = Matrix4D::Translation(Vector3D(5.0, 0.0, 0.0)); set_section_plane_transform(idx, T); auto plane = get_section_plane(idx); EXPECT_NEAR(plane.point.x(), 5.0, 1e-6); } TEST(SectionViewTest, SetSectionPlaneTransform_RotateNormal) { auto body = make_test_body(); int idx = add_section_plane(body, Point3D(0, 0, 0), Vector3D(0, 0, 1)); // 绕 X 轴旋转 90°(右手定则),使 Z 法向量变为 -Y 方向 Matrix4D Rx = Matrix4D::RotationX(M_PI / 2.0); set_section_plane_transform(idx, Rx); auto plane = get_section_plane(idx); EXPECT_NEAR(plane.normal.y(), -1.0, 1e-6); EXPECT_NEAR(plane.normal.z(), 0.0, 1e-6); } // ═══════════════════════════════════════════════════════════ // 活动截面平面设置/获取 // ═══════════════════════════════════════════════════════════ TEST(SectionViewTest, SetActiveSectionPlane) { auto body = make_test_body(); int i0 = add_section_plane(body, Point3D(0, 0, 0), Vector3D(0, 0, 1)); int i1 = add_section_plane(body, Point3D(0, 0, 5), Vector3D(0, 0, 1)); set_active_section_plane(i1); EXPECT_EQ(active_section_plane(), i1); } TEST(SectionViewTest, SetActiveSectionPlane_CancelWithMinusOne) { auto body = make_test_body(); int idx = add_section_plane(body, Point3D(0, 0, 0), Vector3D(0, 0, 1)); set_active_section_plane(idx); EXPECT_EQ(active_section_plane(), idx); set_active_section_plane(-1); EXPECT_EQ(active_section_plane(), -1); } // ═══════════════════════════════════════════════════════════ // section_manipulator_state 返回有效状态 // ═══════════════════════════════════════════════════════════ TEST(SectionViewTest, ManipulatorState_ValidWithModelMatrix) { auto body = make_test_body(); int idx = add_section_plane(body, Point3D(1.0, 2.0, 3.0), Vector3D(0, 0, 1)); auto state = section_manipulator_state(idx); // 操纵器位置应与截面平面位置一致 EXPECT_NEAR(state.position.x(), 1.0, 1e-6); EXPECT_NEAR(state.position.y(), 2.0, 1e-6); EXPECT_NEAR(state.position.z(), 3.0, 1e-6); // model_matrix 应返回有效矩阵(非全零) auto mm = state.model_matrix(); // 对角线元素至少有一个非零 bool has_nonzero = false; for (int r = 0; r < 4; ++r) { if (std::abs(mm.at(r, r)) > 1e-12) { has_nonzero = true; break; } } EXPECT_TRUE(has_nonzero); } // ═══════════════════════════════════════════════════════════ // SectionPlane 结构体默认值 // ═══════════════════════════════════════════════════════════ TEST(SectionViewTest, SectionPlaneStruct_DefaultValues) { SectionPlane sp; EXPECT_NEAR(sp.point.x(), 0.0, 1e-9); EXPECT_NEAR(sp.point.y(), 0.0, 1e-9); EXPECT_NEAR(sp.point.z(), 0.0, 1e-9); EXPECT_NEAR(sp.normal.x(), 0.0, 1e-9); EXPECT_NEAR(sp.normal.y(), 0.0, 1e-9); EXPECT_NEAR(sp.normal.z(), 1.0, 1e-9); EXPECT_NEAR(sp.color.r, 1.0, 1e-9); EXPECT_NEAR(sp.color.g, 0.5, 1e-9); EXPECT_NEAR(sp.color.b, 0.0, 1e-9); EXPECT_TRUE(sp.active); } TEST(SectionViewTest, SectionPlaneStruct_CustomValues) { SectionPlane sp; sp.point = Point3D(10, 20, 30); sp.normal = Vector3D(1, 0, 0); sp.color = Color(0.0, 1.0, 0.0); sp.active = false; EXPECT_NEAR(sp.point.x(), 10.0, 1e-9); EXPECT_NEAR(sp.normal.x(), 1.0, 1e-9); EXPECT_NEAR(sp.color.g, 1.0, 1e-9); EXPECT_FALSE(sp.active); }