iterative closest points
CEV ICP algorithm library
Loading...
Searching...
No Matches
vanilla.cpp
Go to the documentation of this file.
1/*
2 * @author Ethan Uppal
3 * @copyright Copyright (C) 2024 Ethan Uppal. All rights reserved.
4 */
5
6#include <cassert>
7#include <cstdlib>
8#include "../icp.h"
9#include <Eigen/Core>
10#include <Eigen/SVD>
11#include <Eigen/Dense>
12
13/* #name Vanilla */
14
15/* #desc The vanilla algorithm for ICP will match the point-cloud centers
16exactly and then iterate until an optimal rotation has been found. */
17
18namespace icp {
19 struct Vanilla final : public ICP {
20 std::vector<icp::Vector> a_current;
21 icp::Vector b_cm;
22
23 Vanilla(): ICP() {}
24 ~Vanilla() override {}
25
26 void setup() override {
27 if (a_current.size() < a.size()) {
28 a_current.resize(a.size());
29 }
30
31 b_cm = get_centroid(b);
32 }
33
34 void iterate() override {
35 const size_t n = a.size();
36 const size_t m = b.size();
37
38 for (size_t i = 0; i < n; i++) {
39 a_current[i] = transform.apply_to(a[i]);
40 }
41
42 // can optimize by just transforming prev centroid if necessary
43 auto a_current_cm = get_centroid(a_current);
44
45 /*
46 #step
47 Matching Step: match closest points.
48
49 Sources:
50 https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=4767965
51 https://arxiv.org/pdf/2206.06435.pdf
52 https://web.archive.org/web/20220615080318/https://www.cs.technion.ac.il/~cs236329/tutorials/ICP.pdf
53 https://en.wikipedia.org/wiki/Iterative_closest_point
54 https://courses.cs.duke.edu/spring07/cps296.2/scribe_notes/lecture24.pdf
55 -> use k-d tree
56 */
57 for (size_t i = 0; i < n; i++) {
58 matches[i].sq_dist = std::numeric_limits<double>::infinity();
59 for (size_t j = 0; j < m; j++) {
60 // Point-to-point matching
61 double dist_ij = (b[j] - a_current[i]).squaredNorm();
62
63 if (dist_ij < matches[i].sq_dist) {
64 matches[i].sq_dist = dist_ij;
65 matches[i].pair = j;
66 }
67 }
68 }
69
70 Matrix N{};
71 for (size_t i = 0; i < n; i++) {
72 N += (a_current[i] - a_current_cm) * (b[matches[i].pair] - b_cm).transpose();
73 }
74 auto svd = N.jacobiSvd(Eigen::ComputeFullU | Eigen::ComputeFullV);
75 const Matrix U = svd.matrixU();
76 const Matrix V = svd.matrixV();
77 const Matrix R = V * U.transpose();
78
79 // TODO: tricks to handle this case
80 if (R.determinant() < 0) {
81 throw std::runtime_error(
82 "SVD determinant is negative. Got reflection instead of rotation.");
83 }
84
86
87 /*
88 #step
89 Transformation Step: determine optimal transformation.
90
91 The translation vector is determined by the displacement between
92 the centroids of both point clouds. The rotation matrix is
93 calculated via singular value decomposition.
94
95 Sources:
96 https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=4767965
97 https://courses.cs.duke.edu/spring07/cps296.2/scribe_notes/lecture24.pdf
98 */
99 transform.translation += b_cm - R * a_current_cm;
100 }
101 };
102
103 static bool static_initialization = []() {
104 assert(ICP::register_method("vanilla",
105 [](const ICP::Config& config) -> std::unique_ptr<ICP> {
106 return std::make_unique<Vanilla>();
107 }));
108 return true;
109 }();
110}
static bool register_method(std::string name, std::function< std::unique_ptr< ICP >(const Config &)> constructor)
Registers a new ICP method that can be created with constructor, returning false if name has already ...
Definition icp.cpp:85
RBTransform transform
The current point cloud transformation that is being optimized.
Definition icp.h:58
ICP()
Definition icp.cpp:12
std::vector< Vector > a
The source point cloud relative to its centroid.
Definition icp.h:61
std::vector< Vector > b
The destination point cloud relative to its centroid.
Definition icp.h:64
std::vector< Match > matches
The pairing of each point in a to its closest in b.
Definition icp.h:74
Definition geo.cpp:9
Eigen::Vector2d Vector
Definition geo.h:15
Vector get_centroid(const std::vector< Vector > &points)
Definition geo.cpp:10
Eigen::Matrix2d Matrix
Definition geo.h:16
Matrix rotation
Definition geo.h:21
Vector apply_to(Vector v) const
Definition geo.h:31
Vector translation
Definition geo.h:20