#include #include "vde_ui/viewport_api.h" #include "vde/brep/brep.h" #include "vde/core/matrix4d.h" #include #include using namespace vde::brep; using namespace vde::ui; using namespace vde::core; /// 创建一个简单的立方体 BrepModel static BrepModel make_test_model() { BrepModel model; int v0 = model.add_vertex(Point3D(0, 0, 0)); int v1 = model.add_vertex(Point3D(1, 0, 0)); int v2 = model.add_vertex(Point3D(1, 1, 0)); int v3 = model.add_vertex(Point3D(0, 1, 0)); model.add_edge(v0, v1); model.add_edge(v1, v2); model.add_edge(v2, v3); model.add_edge(v3, v0); int loop = model.add_loop({0, 1, 2, 3}, true); auto surf = vde::curves::NurbsSurface::plane( Point3D(0,0,0), Vector3D(1,0,0), Vector3D(0,1,0), 1.0, 1.0); int sid = model.add_surface(surf); int fid = model.add_face(sid, {loop}); int shell = model.add_shell({fid}, true); model.add_body({shell}, "test"); return model; } // 标准透视投影(行主序,右手,相机看向 -Z) static Matrix4D make_perspective(double fovy_deg, double aspect, double znear, double zfar) { const double f = 1.0 / std::tan(fovy_deg * M_PI / 360.0); Matrix4D p; p.m00 = f / aspect; p.m11 = f; p.m22 = (zfar + znear) / (znear - zfar); p.m23 = 2.0 * zfar * znear / (znear - zfar); p.m32 = -1.0; p.m33 = 0.0; return p; } // 用 VP 矩阵将点投影为 NDC static Point3D project_ndc(const Matrix4D& vp, const Point3D& p) { double x = vp.m00*p.x() + vp.m01*p.y() + vp.m02*p.z() + vp.m03; double y = vp.m10*p.x() + vp.m11*p.y() + vp.m12*p.z() + vp.m13; double z = vp.m20*p.x() + vp.m21*p.y() + vp.m22*p.z() + vp.m23; double w = vp.m30*p.x() + vp.m31*p.y() + vp.m32*p.z() + vp.m33; return Point3D(x/w, y/w, z/w); } TEST(ViewportApiTest, BoundingSphereNonEmpty) { auto model = make_test_model(); auto bs = bounding_sphere(model); EXPECT_GT(bs.radius, 0.0); } TEST(ViewportApiTest, OrientationCubeFaceRight) { // 朝向 +X → "right" auto face = orientation_cube_face(Vector3D(1, 0, 0)); EXPECT_EQ(face, "right"); } TEST(ViewportApiTest, OrientationCubeFaceLeft) { // 朝向 -X → "left" auto face = orientation_cube_face(Vector3D(-1, 0, 0)); EXPECT_EQ(face, "left"); } TEST(ViewportApiTest, OrientationCubeFaceTop) { // 朝向 +Z → "top" auto face = orientation_cube_face(Vector3D(0, 0, 1)); EXPECT_EQ(face, "top"); } TEST(ViewportApiTest, OrientationCubeFaceBottom) { EXPECT_EQ(orientation_cube_face(Vector3D(0, 0, -1)), "bottom"); } TEST(ViewportApiTest, OrientationCubeFaceFrontBack) { EXPECT_EQ(orientation_cube_face(Vector3D(0, -1, 0)), "front"); EXPECT_EQ(orientation_cube_face(Vector3D(0, 1, 0)), "back"); } TEST(ViewportApiTest, SceneGridBasic) { auto grid = scene_grid(10.0, 1.0); // 应生成网格线 EXPECT_GT(grid.lines.size(), 0u); // 范围应对称 EXPECT_GT(grid.extent_x, 0.0); EXPECT_GT(grid.extent_y, 0.0); } TEST(ViewportApiTest, GroundPlane) { auto model = make_test_model(); std::vector bodies = {model}; auto plane = ground_plane(bodies); // 地平面法向量应朝上 EXPECT_NEAR(plane.normal.z(), 1.0, 1e-9); } // ═══════════════════════════════════════════════════════════ // VDE-399: screen_to_ray(原始矩阵反投影) // ═══════════════════════════════════════════════════════════ TEST(ViewportApi399Test, ScreenToRay_CenterMatchesForward) { auto model = make_test_model(); const Point3D cam(0.5, 5.0, 0.5); const Point3D target(0.5, 0.5, 0.5); const Vector3D up(0, 0, 1); Matrix4D view = camera_matrix(model, cam, target, up); Matrix4D proj = make_perspective(45.0, 1.0, 0.1, 100.0); auto ray = screen_to_ray(400, 300, 800, 600, view, proj); Vector3D expected(target.x() - cam.x(), target.y() - cam.y(), target.z() - cam.z()); expected = expected / expected.norm(); const double dot = ray.direction.x()*expected.x() + ray.direction.y()*expected.y() + ray.direction.z()*expected.z(); EXPECT_NEAR(dot, 1.0, 1e-6); // 近平面点距相机 ≈ znear Vector3D to_origin(ray.origin.x() - cam.x(), ray.origin.y() - cam.y(), ray.origin.z() - cam.z()); EXPECT_NEAR(to_origin.norm(), 0.1, 1e-6); // t_far ≈ zfar - znear EXPECT_NEAR(ray.t_far, 99.9, 1e-3); } TEST(ViewportApi399Test, ScreenToRay_LeftRightOrdering) { auto model = make_test_model(); Matrix4D view = camera_matrix(model, Point3D(0.5, 5.0, 0.5), Point3D(0.5, 0.5, 0.5), Vector3D(0, 0, 1)); Matrix4D proj = make_perspective(45.0, 1.0, 0.1, 100.0); auto rayL = screen_to_ray(0, 300, 800, 600, view, proj); auto rayC = screen_to_ray(400, 300, 800, 600, view, proj); auto rayR = screen_to_ray(800, 300, 800, 600, view, proj); // 相机右向量(view 矩阵第 0 行) Vector3D s(view.m00, view.m01, view.m02); auto along = [&](const Vector3D& d) { return d.x()*s.x() + d.y()*s.y() + d.z()*s.z(); }; EXPECT_NEAR(along(rayC.direction), 0.0, 1e-9); EXPECT_LT(along(rayL.direction), along(rayR.direction)); } TEST(ViewportApi399Test, ScreenToRay_DegenerateViewport) { Matrix4D identity; auto ray = screen_to_ray(0, 0, 0, 0, identity, identity); EXPECT_NEAR(ray.direction.z(), -1.0, 1e-12); } // ═══════════════════════════════════════════════════════════ // VDE-399: orbit_to_pose / update_orbit // ═══════════════════════════════════════════════════════════ TEST(ViewportApi399Test, OrbitToPose_BasicAxes) { OrbitState o; o.azimuth = 0.0; o.elevation = 0.0; o.distance = 10.0; o.pivot = Point3D(0, 0, 0); auto pose = orbit_to_pose(o); EXPECT_NEAR(pose.position.x(), 10.0, 1e-9); EXPECT_NEAR(pose.position.y(), 0.0, 1e-9); EXPECT_NEAR(pose.position.z(), 0.0, 1e-9); EXPECT_NEAR(pose.target.x(), 0.0, 1e-9); EXPECT_NEAR(pose.up.z(), 1.0, 1e-9); o.azimuth = M_PI / 2.0; auto pose2 = orbit_to_pose(o); EXPECT_NEAR(pose2.position.x(), 0.0, 1e-9); EXPECT_NEAR(pose2.position.y(), 10.0, 1e-9); } TEST(ViewportApi399Test, OrbitToPose_ElevationAndPivot) { OrbitState o; o.azimuth = 0.0; o.elevation = M_PI / 2.0; // 正上方 o.distance = 5.0; o.pivot = Point3D(1, 2, 3); auto pose = orbit_to_pose(o); EXPECT_NEAR(pose.position.x(), 1.0, 1e-9); EXPECT_NEAR(pose.position.y(), 2.0, 1e-9); EXPECT_NEAR(pose.position.z(), 8.0, 1e-9); EXPECT_NEAR(pose.target.z(), 3.0, 1e-9); } TEST(ViewportApi399Test, UpdateOrbit_ClampsElevation) { OrbitState o; auto up_state = update_orbit(o, 0.0, 10.0, 0.0); const double lim = 89.0 * M_PI / 180.0; EXPECT_NEAR(up_state.elevation, lim, 1e-9); auto down_state = update_orbit(o, 0.0, -10.0, 0.0); EXPECT_NEAR(down_state.elevation, -lim, 1e-9); } TEST(ViewportApi399Test, UpdateOrbit_AzimuthWrapsAndDistanceClamped) { OrbitState o; o.azimuth = 0.0; auto s = update_orbit(o, 3.0 * M_PI, 0.0, 0.0); // 环绕 EXPECT_GT(s.azimuth, -M_PI - 1e-9); EXPECT_LE(s.azimuth, M_PI + 1e-9); auto s2 = update_orbit(o, 0.0, 0.0, -1000.0); EXPECT_NEAR(s2.distance, 1e-6, 1e-12); } TEST(ViewportApi399Test, UpdateOrbit_Accumulates) { OrbitState o; o.azimuth = 0.1; o.elevation = 0.2; o.distance = 4.0; auto s = update_orbit(o, 0.3, 0.1, 1.5); EXPECT_NEAR(s.azimuth, 0.4, 1e-12); EXPECT_NEAR(s.elevation, 0.3, 1e-12); EXPECT_NEAR(s.distance, 5.5, 1e-12); } // ═══════════════════════════════════════════════════════════ // VDE-399: zoom_to_fit // ═══════════════════════════════════════════════════════════ TEST(ViewportApi399Test, ZoomToFit_TargetsCenterAndAngle) { auto model = make_test_model(); auto bb = model.bounds(); const Point3D center = bb.center(); auto pose = zoom_to_fit(model); EXPECT_NEAR(pose.target.x(), center.x(), 1e-9); EXPECT_NEAR(pose.target.y(), center.y(), 1e-9); EXPECT_NEAR(pose.target.z(), center.z(), 1e-9); // 相机方向与上轴夹角 = 30° Vector3D dir(pose.position.x() - center.x(), pose.position.y() - center.y(), pose.position.z() - center.z()); dir = dir / dir.norm(); const double angle = std::acos(std::max(-1.0, std::min(1.0, dir.z()))) * 180.0 / M_PI; EXPECT_NEAR(angle, 30.0, 1e-6); } TEST(ViewportApi399Test, ZoomToFit_AllCornersInsideFrustum) { auto model = make_test_model(); auto bb = model.bounds(); auto pose = zoom_to_fit(model, 1.2); Matrix4D view = camera_matrix(model, pose.position, pose.target, pose.up); Matrix4D proj = make_perspective(45.0, 1.0, 0.1, 1000.0); Matrix4D vp = proj * view; const Point3D lo = bb.min(), hi = bb.max(); for (int i = 0; i < 8; ++i) { Point3D c((i & 1) ? hi.x() : lo.x(), (i & 2) ? hi.y() : lo.y(), (i & 4) ? hi.z() : lo.z()); Point3D ndc = project_ndc(vp, c); EXPECT_GE(ndc.x(), -1.0 - 1e-9) << "corner " << i; EXPECT_LE(ndc.x(), 1.0 + 1e-9) << "corner " << i; EXPECT_GE(ndc.y(), -1.0 - 1e-9) << "corner " << i; EXPECT_LE(ndc.y(), 1.0 + 1e-9) << "corner " << i; } } TEST(ViewportApi399Test, ZoomToFit_PaddingIncreasesDistance) { auto model = make_test_model(); auto bb = model.bounds(); const Point3D center = bb.center(); auto near_pose = zoom_to_fit(model, 1.1); auto far_pose = zoom_to_fit(model, 2.0); auto dist = [&](const CameraPose& p) { Vector3D v(p.position.x() - center.x(), p.position.y() - center.y(), p.position.z() - center.z()); return v.norm(); }; EXPECT_LT(dist(near_pose), dist(far_pose)); } // ═══════════════════════════════════════════════════════════ // VDE-399: compute_view_layout // ═══════════════════════════════════════════════════════════ TEST(ViewportApi399Test, ComputeViewLayout_CountsAndNames) { auto model = make_test_model(); auto single = compute_view_layout(ViewLayout::Single, model); ASSERT_EQ(single.size(), 1u); EXPECT_EQ(single[0].name, "Iso"); auto h2 = compute_view_layout(ViewLayout::TwoHorizontal, model); ASSERT_EQ(h2.size(), 2u); EXPECT_EQ(h2[0].name, "Front"); EXPECT_EQ(h2[1].name, "Right"); auto v2 = compute_view_layout(ViewLayout::TwoVertical, model); ASSERT_EQ(v2.size(), 2u); auto quad = compute_view_layout(ViewLayout::FourQuad, model); ASSERT_EQ(quad.size(), 4u); EXPECT_EQ(quad[0].name, "Top"); EXPECT_EQ(quad[3].name, "Iso"); auto three = compute_view_layout(ViewLayout::ThreeStandard, model); ASSERT_EQ(three.size(), 3u); EXPECT_EQ(three[0].name, "Front"); EXPECT_EQ(three[1].name, "Top"); EXPECT_EQ(three[2].name, "Right"); } TEST(ViewportApi399Test, ComputeViewLayout_ActiveFlag) { auto model = make_test_model(); auto quad = compute_view_layout(ViewLayout::FourQuad, model); ASSERT_EQ(quad.size(), 4u); EXPECT_TRUE(quad[0].is_active); EXPECT_FALSE(quad[1].is_active); EXPECT_FALSE(quad[2].is_active); EXPECT_FALSE(quad[3].is_active); } TEST(ViewportApi399Test, ComputeViewLayout_CenterMapsInFrontOfCamera) { auto model = make_test_model(); auto bb = model.bounds(); const Point3D center = bb.center(); auto views = compute_view_layout(ViewLayout::ThreeStandard, model); ASSERT_EQ(views.size(), 3u); for (const auto& lv : views) { // 视图空间:包围盒中心应位于相机前方(z<0),且落在光轴上(x=y=0) Point3D v = lv.view_matrix.transform_point(center); EXPECT_NEAR(v.x(), 0.0, 1e-9) << lv.name; EXPECT_NEAR(v.y(), 0.0, 1e-9) << lv.name; EXPECT_LT(v.z(), 0.0) << lv.name; } }