#include #include #include "vde/core/icp.h" #include "vde/core/transform.h" using namespace vde::core; // GCC 8.1 (MinGW-W64) 无 (C++20,GCC 9+),此处用局部常量替代 namespace { constexpr double PI = 3.14159265358979323846; } static std::vector apply_xform(const std::vector& c, const Transform3D& T) { std::vector r(c.size()); for (size_t i = 0; i < c.size(); ++i) r[i] = T * c[i]; return r; } static std::vector square_cloud() { return {Point3D(-1, -1, 0), Point3D(1, -1, 0), Point3D(1, 1, 0), Point3D(-1, 1, 0), Point3D(0, 0, 0)}; } static void expect_trans(const Transform3D& T, double tx, double ty, double tz, double tol = 1e-4) { EXPECT_NEAR(T.translation().x(), tx, tol); EXPECT_NEAR(T.translation().y(), ty, tol); EXPECT_NEAR(T.translation().z(), tz, tol); } static void expect_rot_identity(const Transform3D& T, double tol = 1e-4) { auto R = T.linear(); EXPECT_NEAR(R(0, 0), 1.0, tol); EXPECT_NEAR(R(1, 1), 1.0, tol); EXPECT_NEAR(R(2, 2), 1.0, tol); EXPECT_NEAR(R(0, 1), 0.0, tol); EXPECT_NEAR(R(1, 0), 0.0, tol); EXPECT_NEAR(R(0, 2), 0.0, tol); EXPECT_NEAR(R(2, 0), 0.0, tol); EXPECT_NEAR(R(1, 2), 0.0, tol); EXPECT_NEAR(R(2, 1), 0.0, tol); } TEST(IcpTest, ExactMatch_RMSNearZero) { auto c = square_cloud(); auto r = icp_register(c, c); EXPECT_NEAR(r.rms_error, 0.0, 1e-10); EXPECT_TRUE(r.converged); } TEST(IcpTest, ExactMatch_IdentityTransform) { auto c = square_cloud(); auto r = icp_register(c, c); expect_trans(r.transform, 0, 0, 0); expect_rot_identity(r.transform); } TEST(IcpTest, KnownTranslation_RecoverX) { auto src = square_cloud(); auto tgt = apply_xform(src, translate(1, 0, 0)); auto r = icp_register(src, tgt); EXPECT_TRUE(r.converged); EXPECT_NEAR(r.rms_error, 0.0, 1e-6); expect_trans(r.transform, 1, 0, 0); } TEST(IcpTest, KnownTranslation_RecoverY) { auto src = square_cloud(); auto tgt = apply_xform(src, translate(0, -2, 0)); auto r = icp_register(src, tgt); EXPECT_TRUE(r.converged); expect_trans(r.transform, 0, -2, 0); } TEST(IcpTest, KnownTranslation_RecoverXYZ) { auto src = square_cloud(); auto tgt = apply_xform(src, translate(3, -1, 5)); auto r = icp_register(src, tgt); EXPECT_TRUE(r.converged); EXPECT_NEAR(r.rms_error, 0.0, 1e-6); expect_trans(r.transform, 3, -1, 5); } // 正方形四角点云对 45° 旋转存在最近邻 tie,H 矩阵退化会使 SVD 返回恒等。 // 加一个明显偏离对称中心的点,保持旋转可精确恢复,同时消除夹具歧义。 static std::vector asymmetric_cloud() { auto c = square_cloud(); c.push_back(Point3D(0.37, 0.11, 0.0)); return c; } TEST(IcpTest, KnownRotation_45DegAroundZ) { auto src = asymmetric_cloud(); constexpr double a = PI / 4.0; auto tgt = apply_xform(src, rotate_z(a)); auto r = icp_register(src, tgt, 100); EXPECT_TRUE(r.converged); auto R = r.transform.linear(); EXPECT_NEAR(R(0, 0), std::cos(a), 1e-3); EXPECT_NEAR(R(0, 1), -std::sin(a), 1e-3); EXPECT_NEAR(R(1, 0), std::sin(a), 1e-3); EXPECT_NEAR(R(1, 1), std::cos(a), 1e-3); } TEST(IcpTest, KnownRotation_60DegAroundZ) { auto src = asymmetric_cloud(); constexpr double a = PI / 3.0; auto tgt = apply_xform(src, rotate_z(a)); auto r = icp_register(src, tgt, 100); EXPECT_TRUE(r.converged); EXPECT_NEAR(r.rms_error, 0.0, 1e-4); auto R = r.transform.linear(); EXPECT_NEAR(R(0, 0), std::cos(a), 1e-3); EXPECT_NEAR(R(0, 1), -std::sin(a), 1e-3); EXPECT_NEAR(R(1, 0), std::sin(a), 1e-3); EXPECT_NEAR(R(1, 1), std::cos(a), 1e-3); } TEST(IcpTest, KnownRotation_90DegAroundZ) { // NOTE: ICP has limited capture range for large rotations. // 90 degrees from identity typically cannot converge without good initial alignment. // This test verifies ICP handles the case gracefully (may not converge to exact solution). auto src = square_cloud(); constexpr double a = PI / 2.0; auto tgt = apply_xform(src, rotate_z(a)); auto r = icp_register(src, tgt, 100); // ICP may or may not converge for 90-degree rotation; just verify it runs without error EXPECT_GE(r.iterations, 0); EXPECT_GE(r.rms_error, 0.0); } TEST(IcpTest, CombinedTranslationAndRotation) { auto src = square_cloud(); Transform3D T = translate(2, -3, 1) * rotate_z(PI / 6.0); auto tgt = apply_xform(src, T); auto r = icp_register(src, tgt, 100); EXPECT_TRUE(r.converged); EXPECT_NEAR(r.rms_error, 0.0, 1e-4); expect_trans(r.transform, 2, -3, 1); } TEST(IcpTest, DifferentPointCounts) { auto src = square_cloud(); auto tgt = square_cloud(); tgt.push_back(Point3D(5, 5, 0)); tgt.push_back(Point3D(-5, -5, 0)); tgt.push_back(Point3D(0, 5, 0)); auto r = icp_register(src, tgt, 100); EXPECT_TRUE(r.converged); EXPECT_NEAR(r.rms_error, 0.0, 1e-6); } TEST(IcpTest, MoreTargetThanSource) { // NOTE: When target has more points than source, some target points may not have // source correspondences, which can affect convergence. The algorithm still works // but may not converge to the exact solution. std::vector src = {Point3D(0, 0, 0), Point3D(1, 0, 0)}; std::vector tgt = {Point3D(5, 0, 0), Point3D(6, 0, 0), Point3D(5.5, 0.5, 0), Point3D(5.5, -0.5, 0)}; auto r = icp_register(src, tgt, 100); // Verify ICP runs without error and produces a reasonable result EXPECT_GE(r.iterations, 0); EXPECT_GE(r.rms_error, 0.0); // Translation should be roughly in the right direction (positive x) EXPECT_GT(r.transform(0, 3), 0.0); } TEST(IcpTest, SinglePointCloud) { std::vector src = {Point3D(0, 0, 0)}; std::vector tgt = {Point3D(3, 4, 0)}; auto r = icp_register(src, tgt); EXPECT_TRUE(r.converged); EXPECT_NEAR(r.rms_error, 0.0, 1e-6); expect_trans(r.transform, 3, 4, 0, 1e-4); } TEST(IcpTest, SinglePointCloud_SameLocation) { std::vector pt = {Point3D(1, 2, 3)}; auto r = icp_register(pt, pt); EXPECT_NEAR(r.rms_error, 0.0, 1e-10); expect_rot_identity(r.transform); } TEST(IcpTest, ConvergedFlag_SimpleCase) { auto src = square_cloud(); auto tgt = apply_xform(src, translate(0.5, 0.5, 0)); auto r = icp_register(src, tgt); EXPECT_TRUE(r.converged); } TEST(IcpTest, Iterations_GreaterThanZero) { auto src = square_cloud(); auto tgt = apply_xform(src, translate(1, 1, 1) * rotate_z(0.3)); auto r = icp_register(src, tgt, 100); EXPECT_GT(r.iterations, 0); } TEST(IcpTest, EmptyPointClouds) { std::vector empty; auto r = icp_register(empty, empty); EXPECT_NEAR(r.rms_error, 0.0, 1e-10); } TEST(IcpTest, EmptySourceNonEmptyTarget) { std::vector empty; auto tgt = square_cloud(); auto r = icp_register(empty, tgt); EXPECT_GE(r.iterations, 0); } TEST(IcpTest, DegenerateCollinearCloud_IdentityFallback) { std::vector src = {Point3D(0, 0, 0), Point3D(1, 0, 0), Point3D(2, 0, 0), Point3D(3, 0, 0), Point3D(4, 0, 0)}; auto r = icp_register(src, src); EXPECT_TRUE(r.converged); EXPECT_NEAR(r.rms_error, 0.0, 1e-10); EXPECT_TRUE(r.transform.linear().isApprox(Eigen::Matrix3d::Identity(), 1e-10)); } // ═══ point_to_plane_icp ═══ TEST(IcpTest, PointToPlane_KnownTranslation) { auto src = square_cloud(); auto tgt = apply_xform(src, translate(1, 0, 0)); std::vector n(tgt.size(), Vector3D(0, 0, 1)); auto r = point_to_plane_icp(src, tgt, n, 50, 1e-6); EXPECT_TRUE(r.converged); EXPECT_NEAR(r.rms_error, 0.0, 1e-4); expect_trans(r.transform, 1, 0, 0); } TEST(IcpTest, PointToPlane_KnownRotation) { auto src = asymmetric_cloud(); auto tgt = apply_xform(src, rotate_z(PI / 4.0)); std::vector n(tgt.size(), Vector3D(0, 0, 1)); auto r = point_to_plane_icp(src, tgt, n, 100); EXPECT_TRUE(r.converged); EXPECT_NEAR(r.rms_error, 0.0, 1e-3); } TEST(IcpTest, PointToPlane_KnownRotation60) { auto src = asymmetric_cloud(); auto tgt = apply_xform(src, rotate_z(PI / 3.0)); std::vector n(tgt.size(), Vector3D(0, 0, 1)); auto r = point_to_plane_icp(src, tgt, n, 100, 1e-6); EXPECT_TRUE(r.converged); EXPECT_NEAR(r.rms_error, 0.0, 1e-3); } TEST(IcpTest, PointToPlane_ExactMatch) { auto c = square_cloud(); std::vector n(c.size(), Vector3D(0, 0, 1)); auto r = point_to_plane_icp(c, c, n); EXPECT_NEAR(r.rms_error, 0.0, 1e-10); EXPECT_TRUE(r.converged); } // ═══ colored_icp ═══ static std::vector colored_cloud() { std::vector c; c.push_back({Point3D(-1, -1, 0), 1.0, 0.0, 0.0}); c.push_back({Point3D(1, -1, 0), 0.0, 1.0, 0.0}); c.push_back({Point3D(1, 1, 0), 0.0, 0.0, 1.0}); c.push_back({Point3D(-1, 1, 0), 1.0, 1.0, 0.0}); c.push_back({Point3D(0, 0, 0), 0.5, 0.5, 0.5}); return c; } static std::vector apply_colored_xform(const std::vector& c, const Transform3D& T) { std::vector r(c.size()); for (size_t i = 0; i < c.size(); ++i) { r[i].position = T * c[i].position; r[i].r = c[i].r; r[i].g = c[i].g; r[i].b = c[i].b; } return r; } TEST(IcpTest, ColoredIcp_ExactMatch) { auto c = colored_cloud(); auto r = colored_icp(c, c); EXPECT_NEAR(r.rms_error, 0.0, 1e-10); EXPECT_TRUE(r.converged); } TEST(IcpTest, ColoredIcp_KnownTranslation) { auto src = colored_cloud(); auto tgt = apply_colored_xform(src, translate(1, 0, 0)); auto r = colored_icp(src, tgt, 0.3, 50, 1e-6); EXPECT_TRUE(r.converged); EXPECT_NEAR(r.rms_error, 0.0, 1e-4); expect_trans(r.transform, 1, 0, 0); } // ═══ scale_aware_registration ═══ TEST(IcpTest, ScaleAware_ExactMatch) { auto c = square_cloud(); auto r = scale_aware_registration(c, c); EXPECT_NEAR(r.rms_error, 0.0, 1e-10); EXPECT_NEAR(r.scale, 1.0, 1e-6); } TEST(IcpTest, ScaleAware_ScaledPointCloud) { auto src = square_cloud(); double known_scale = 2.0; auto tgt = apply_xform(src, scale(known_scale)); auto r = scale_aware_registration(src, tgt, 100); EXPECT_NEAR(r.scale, known_scale, 1e-3); EXPECT_NEAR(r.rms_error, 0.0, 1e-3); } // ═══ VDE-385 提案命名 API(与等价实现一致性)═══ TEST(IcpTest, RegisterPlane_MatchesPointToPlane) { auto src = square_cloud(); auto tgt = apply_xform(src, translate(1, 0, 0)); std::vector n(tgt.size(), Vector3D(0, 0, 1)); auto a = icp_register_plane(src, tgt, n, 50, 1e-6); auto b = point_to_plane_icp(src, tgt, n, 50, 1e-6); EXPECT_NEAR(a.rms_error, b.rms_error, 1e-12); EXPECT_EQ(a.iterations, b.iterations); EXPECT_EQ(a.converged, b.converged); expect_trans(a.transform, 1, 0, 0); } TEST(IcpTest, RegisterColored_MatchesColoredIcp) { auto src = colored_cloud(); auto tgt = apply_colored_xform(src, translate(1, 0, 0)); auto a = icp_register_colored(src, tgt, 0.3, 50, 1e-6); auto b = colored_icp(src, tgt, 0.3, 50, 1e-6); EXPECT_NEAR(a.rms_error, b.rms_error, 1e-12); EXPECT_EQ(a.iterations, b.iterations); EXPECT_EQ(a.converged, b.converged); expect_trans(a.transform, 1, 0, 0); } TEST(IcpTest, RegisterScaled_RecoversScaleAndMatches) { auto src = square_cloud(); double known_scale = 2.0; auto tgt = apply_xform(src, scale(known_scale)); ScaledICPResult r = icp_register_scaled(src, tgt, 100); EXPECT_NEAR(r.scale, known_scale, 1e-3); EXPECT_NEAR(r.rms_error, 0.0, 1e-3); EXPECT_GT(r.iterations, 0); EXPECT_TRUE(r.converged); // 与 scale_aware_registration 结果一致 auto b = scale_aware_registration(src, tgt, 100); EXPECT_NEAR(r.scale, b.scale, 1e-12); EXPECT_NEAR(r.rms_error, b.rms_error, 1e-12); EXPECT_EQ(r.iterations, b.iterations); EXPECT_EQ(r.converged, b.converged); // from() 逐字段搬运 auto f = ScaledICPResult::from(b); EXPECT_NEAR(f.scale, b.scale, 1e-15); EXPECT_NEAR(f.rms_error, b.rms_error, 1e-15); EXPECT_EQ(f.iterations, b.iterations); EXPECT_EQ(f.converged, b.converged); }