- 核实:§1~§3 功能早已由 point_to_plane_icp / colored_icp / scale_aware_registration(+ColoredPoint、ICPResult::scale)真实实现并有测试 - 补齐提案命名等价封装:icp_register_plane / icp_register_colored / icp_register_scaled + ScaledICPResult(含 from(ICPResult) 逐字段搬运) - tests/core/test_icp.cpp 新增 3 测试比对封装与既有实现逐字段一致,26/26 全绿 - 核验表 VDE-385 → Fixed(缺失 3→0),计数 47/30/210 → 48/29/207
364 lines
11 KiB
C++
364 lines
11 KiB
C++
#include <gtest/gtest.h>
|
||
|
||
#include <cmath>
|
||
|
||
#include "vde/core/icp.h"
|
||
#include "vde/core/transform.h"
|
||
|
||
using namespace vde::core;
|
||
|
||
// GCC 8.1 (MinGW-W64) 无 <numbers>(C++20,GCC 9+),此处用局部常量替代
|
||
namespace {
|
||
constexpr double PI = 3.14159265358979323846;
|
||
}
|
||
|
||
static std::vector<Point3D> apply_xform(const std::vector<Point3D>& c, const Transform3D& T)
|
||
{
|
||
std::vector<Point3D> r(c.size());
|
||
for (size_t i = 0; i < c.size(); ++i)
|
||
r[i] = T * c[i];
|
||
return r;
|
||
}
|
||
|
||
static std::vector<Point3D> 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);
|
||
}
|
||
|
||
TEST(IcpTest, KnownRotation_45DegAroundZ)
|
||
{
|
||
auto src = square_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_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<Point3D> src = {Point3D(0, 0, 0), Point3D(1, 0, 0)};
|
||
std::vector<Point3D> 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<Point3D> src = {Point3D(0, 0, 0)};
|
||
std::vector<Point3D> 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<Point3D> 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<Point3D> empty;
|
||
auto r = icp_register(empty, empty);
|
||
EXPECT_NEAR(r.rms_error, 0.0, 1e-10);
|
||
}
|
||
|
||
TEST(IcpTest, EmptySourceNonEmptyTarget)
|
||
{
|
||
std::vector<Point3D> empty;
|
||
auto tgt = square_cloud();
|
||
auto r = icp_register(empty, tgt);
|
||
EXPECT_GE(r.iterations, 0);
|
||
}
|
||
|
||
// ═══ point_to_plane_icp ═══
|
||
|
||
TEST(IcpTest, PointToPlane_KnownTranslation)
|
||
{
|
||
auto src = square_cloud();
|
||
auto tgt = apply_xform(src, translate(1, 0, 0));
|
||
std::vector<Vector3D> 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 = square_cloud();
|
||
auto tgt = apply_xform(src, rotate_z(PI / 4.0));
|
||
std::vector<Vector3D> 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_ExactMatch)
|
||
{
|
||
auto c = square_cloud();
|
||
std::vector<Vector3D> 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<ColoredPoint> colored_cloud()
|
||
{
|
||
std::vector<ColoredPoint> 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<ColoredPoint> apply_colored_xform(const std::vector<ColoredPoint>& c,
|
||
const Transform3D& T)
|
||
{
|
||
std::vector<ColoredPoint> 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<Vector3D> 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);
|
||
} |