Files
hm d9d39e86e1
Build & Test / build-and-test (push) Canceled after 0s
VDE Build & Test / build-and-test (push) Canceled after 0s
Build & Test / python-bindings (push) Canceled after 0s
feat(core): 落地 VDE-385 提案命名 ICP API(点到面/彩色/尺度感知)
- 核实:§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
2026-10-05 14:18:21 +08:00

186 lines
6.5 KiB
C++
Raw Permalink 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.
#pragma once
#include "vde/core/point.h"
#include "vde/core/transform.h"
#include <vector>
namespace vde::core {
using foundation::Point2D;
using foundation::Point3D;
using foundation::Vector2D;
using foundation::Vector3D;
/**
* @brief ICP(迭代最近点)配准结果
*
* 包含刚体变换、误差度量、迭代信息和收敛状态。
*
* @ingroup core
*/
struct ICPResult {
/// 将 source 对齐到 target 的刚体变换
Transform3D transform;
/**
* @brief 配准后的均方根误差
*
* RMS = sqrt(Σ ||target_i - T(source_i)||² / N)
*/
double rms_error;
/// 实际使用的迭代次数
int iterations;
/// 是否在达到最大迭代次数前收敛
bool converged;
/// Scale factor (1.0 for rigid ICP, used in scale-aware)
double scale = 1.0;
};
/**
* @brief 迭代最近点(ICP)刚体点云配准
*
* 使用标准 ICP 算法将 source 点云配准到 target 点云。
* 每步迭代:
* 1. 对每个 source 点找 target 中的最近邻
* 2. 用 SVD 求解最优刚体变换
* 3. 应用变换,检查收敛
*
* @param source 源点云(将被移动)
* @param target 目标点云(固定参考)
* @param max_iter 最大迭代次数(默认 50)
* @param tolerance 收敛容差:相邻两次 RMS 变化的阈值(默认 1e-6)
* @return 配准结果,包含最终变换和误差
*
* @note source 和 target 点数可以不同
* @note 初始对齐越好,收敛越快;若初始偏差太大,可能收敛到局部最优
*
* @code{.cpp}
* std::vector<Point3D> src = // ...;
* std::vector<Point3D> tgt = // ...;
* auto result = icp_register(src, tgt, 100, 1e-8);
* if (result.converged) {
* std::cout << "RMS error: " << result.rms_error << "\n";
* // 应用变换
* for (auto& p : src) p = result.transform * p;
* }
* @endcode
*
* @ingroup core
*/
[[nodiscard]] ICPResult icp_register(const std::vector<Point3D>& source,
const std::vector<Point3D>& target,
int max_iter = 50,
double tolerance = 1e-6);
// ═══════════════════════════════════════════════════════════
// VDE-385: point_to_plane_icp + colored_icp + scale_aware
// ═══════════════════════════════════════════════════════════
/// Colored point with RGB value
struct ColoredPoint {
Point3D position;
double r = 0.0, g = 0.0, b = 0.0; ///< Color channels [0,1]
};
/// Point-to-Plane ICP: minimizes point-to-plane distance
/// @param source Source point cloud
/// @param target Target point cloud
/// @param target_normals Target point normals (must match target size)
/// @param max_iter Maximum iterations
/// @param tolerance Convergence tolerance
/// @return ICP result
[[nodiscard]] ICPResult point_to_plane_icp(
const std::vector<Point3D>& source,
const std::vector<Point3D>& target,
const std::vector<Vector3D>& target_normals,
int max_iter = 50,
double tolerance = 1e-6);
/// Colored ICP: uses color information for better correspondence
/// @param source Source colored points
/// @param target Target colored points
/// @param color_weight Weight of color term (0=pure geometry, 1=pure color)
/// @param max_iter Maximum iterations
/// @param tolerance Convergence tolerance
/// @return ICP result
[[nodiscard]] ICPResult colored_icp(
const std::vector<ColoredPoint>& source,
const std::vector<ColoredPoint>& target,
double color_weight = 0.3,
int max_iter = 50,
double tolerance = 1e-6);
/// Scale-aware registration: estimates uniform scale + rigid transform
/// @param source Source point cloud
/// @param target Target point cloud
/// @param max_iter Maximum iterations
/// @param tolerance Convergence tolerance
/// @return ICP result with scale factor in result.scale
[[nodiscard]] ICPResult scale_aware_registration(
const std::vector<Point3D>& source,
const std::vector<Point3D>& target,
int max_iter = 50,
double tolerance = 1e-6);
// ═══════════════════════════════════════════════════════════
// VDE-385 提案命名 API(对上述已验证实现的等价封装)
// ═══════════════════════════════════════════════════════════
/**
* @brief 尺度感知配准结果(VDE-385 §3)
*
* 在刚体变换之外额外给出统一缩放因子。
*/
struct ScaledICPResult {
Transform3D transform; ///< 将 source 对齐到 target 的相似变换(含缩放)
double scale = 1.0; ///< 估计的统一缩放因子
double rms_error = 0.0;///< 配准后 RMS 误差
int iterations = 0; ///< 实际迭代次数
bool converged = false;///< 是否收敛
/// 由通用 ICPResult 构造(取其 transform / scale / rms / 迭代 / 收敛)
[[nodiscard]] static ScaledICPResult from(const ICPResult& r);
};
/**
* @brief 点到面 ICP(VDE-385 §1 提案命名)
*
* 等价于 `point_to_plane_icp`:最小化点到目标切平面的距离,
* 对带法向的扫描数据收敛更快、更准。
*/
[[nodiscard]] ICPResult icp_register_plane(
const std::vector<Point3D>& source,
const std::vector<Point3D>& target,
const std::vector<Vector3D>& target_normals,
int max_iter = 50,
double tolerance = 1e-6);
/**
* @brief 彩色 ICP(VDE-385 §2 提案命名)
*
* 等价于 `colored_icp`:在几何项之外加入颜色(光度)一致性项。
*
* @note 提案原文用 `std::array<uint8_t,3>` 表示颜色;本实现沿用既有
* `ColoredPoint`(double r/g/b ∈ [0,1]),语义一致。
*/
[[nodiscard]] ICPResult icp_register_colored(
const std::vector<ColoredPoint>& source,
const std::vector<ColoredPoint>& target,
double color_weight = 0.3,
int max_iter = 50,
double tolerance = 1e-6);
/**
* @brief 尺度感知 ICP(VDE-385 §3 提案命名)
*
* 等价于 `scale_aware_registration`,返回 `ScaledICPResult`。
*/
[[nodiscard]] ScaledICPResult icp_register_scaled(
const std::vector<Point3D>& source,
const std::vector<Point3D>& target,
int max_iter = 50,
double tolerance = 1e-6);
} // namespace vde::core