iterative closest points
CEV ICP algorithm library
Loading...
Searching...
No Matches
icp.h
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#pragma once
7
8#include <cmath>
9#include <vector>
10#include <memory>
11#include <string>
12#include <functional>
13#include <unordered_map>
14#include "geo.h"
15
16namespace icp {
44 class ICP {
45 protected:
46 // TODO: make this more general to allow for point-to-line
49 struct Match {
50 size_t point;
51 size_t pair;
52 double sq_dist;
53 };
54
58
61
64
66 std::vector<Vector> a;
67
69 std::vector<Vector> b;
70
74
77
79 std::vector<Match> matches;
80
81 ICP();
82
83 virtual void setup();
84
85 public:
89 double final_cost;
90
94 };
95
97 class Config {
98 using Param = std::variant<int, double, std::string>;
99 std::unordered_map<std::string, Param> params;
100
101 public:
104
106 template<typename T>
107 void set(std::string key, T value) {
108 params[key] = value;
109 }
110
113 template<typename T>
114 T get(std::string key, T otherwise) const {
115 if (params.find(key) == params.end()) {
116 return otherwise;
117 } else {
118 return std::get<T>(params.at(key));
119 }
120 }
121 };
122
123 virtual ~ICP() = default;
124
127 void begin(const std::vector<Vector>& a, const std::vector<Vector>& b,
128 RBTransform t);
129
134 virtual void iterate() = 0;
135
142 double calculate_cost() const;
143
154 ConvergenceReport converge(size_t burn_in,
155 double convergence_threshold);
156
158 const RBTransform& current_transform() const;
159
162 static bool register_method(std::string name,
163 std::function<std::unique_ptr<ICP>(const Config&)> constructor);
164
167 static const std::vector<std::string>& registered_methods();
168
176 static std::unique_ptr<ICP> from_method(std::string name,
177 const Config& params = Config());
178
180 static bool is_registered_method(std::string name);
181 };
182
183 struct Methods {
184 std::vector<std::string> registered_method_names;
185 std::vector<std::function<std::unique_ptr<ICP>(const ICP::Config&)>>
187 };
188}
Configuration for ICP instances.
Definition icp.h:97
Config()
Constructs an empty configuration.
Definition icp.h:103
T get(std::string key, T otherwise) const
Retrieves the integer, double, or string value associated with key.
Definition icp.h:114
void set(std::string key, T value)
Associates key with an integer, double, or string value.
Definition icp.h:107
Interface for iterative closest points.
Definition icp.h:44
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:99
double previous_cost
Keeps track of the previous cost to ensure that progress is being made.
Definition icp.h:73
virtual void setup()
Definition icp.cpp:14
static std::unique_ptr< ICP > from_method(std::string name, const Config &params=Config())
Factory constructor for the ICP method name with configuration config.
Definition icp.cpp:112
RBTransform transform
The current point cloud transformation that is being optimized.
Definition icp.h:57
Vector b_cm
The centroid of the destination point cloud.
Definition icp.h:63
ICP()
Definition icp.cpp:12
Vector a_cm
The centroid of the source point cloud.
Definition icp.h:60
ConvergenceReport converge(size_t burn_in, double convergence_threshold)
Perform ICP for the point clouds a and b provided with ICP::begin until the cost is below convergence...
Definition icp.cpp:58
double current_cost
The RMS (root mean square) cost of the current transformation.
Definition icp.h:76
std::vector< Vector > a
The source point cloud relative to its centroid.
Definition icp.h:66
virtual ~ICP()=default
static const std::vector< std::string > & registered_methods()
Returns a current list of the names of currently registered ICP methods.
Definition icp.cpp:107
std::vector< Vector > b
The destination point cloud relative to its centroid.
Definition icp.h:69
std::vector< Match > matches
The pairing of each point in a to its closest in b.
Definition icp.h:79
void begin(const std::vector< Vector > &a, const std::vector< Vector > &b, RBTransform t)
Begins the ICP process for point clouds a and b with an initial guess for the transform t.
Definition icp.cpp:16
const RBTransform & current_transform() const
The current transform.
Definition icp.cpp:89
virtual void iterate()=0
Perform one iteration of ICP for the point clouds a and b provided with ICP::begin.
double calculate_cost() const
Computes the cost of the current transformation.
Definition icp.cpp:50
static bool is_registered_method(std::string name)
Whether name is a registered ICP method.
Definition icp.cpp:121
Definition geo.cpp:9
Eigen::Vector2d Vector
Definition geo.h:15
The result of running ICP::converge.
Definition icp.h:87
size_t iteration_count
The number of iterations performed, including the burn-in period.
Definition icp.h:93
double final_cost
The least cost achieved.
Definition icp.h:89
A point-to-point matching between point and pair at a distance of sqrt(sq_dist).
Definition icp.h:49
size_t pair
Definition icp.h:51
double sq_dist
Definition icp.h:52
size_t point
Definition icp.h:50
std::vector< std::function< std::unique_ptr< ICP >(const ICP::Config &)> registered_method_constructors)
Definition icp.h:186
std::vector< std::string > registered_method_names
Definition icp.h:184
Rigid-body transformation.
Definition geo.h:19