ICPPointAlign.cpp
Go to the documentation of this file.
1 
36 #include "lvr2/io/Timestamp.hpp"
37 
38 #include <iomanip>
39 #include <chrono>
40 
41 using namespace std;
42 
43 namespace lvr2
44 {
45 
46 ICPPointAlign::ICPPointAlign(SLAMScanPtr model, SLAMScanPtr data) :
47  m_modelCloud(model), m_dataCloud(data)
48 {
49  // Init default values
50  m_maxDistanceMatch = 25;
51  m_maxIterations = 50;
52  m_epsilon = 0.00001;
53  m_verbose = false;
54 
56 }
57 
59 {
60  if (m_maxIterations == 0)
61  {
62  return Matrix4d::Identity();
63  }
64 
65  auto start_time = chrono::steady_clock::now();
66 
67  double ret = 0.0, prev_ret = 0.0, prev_prev_ret = 0.0;
69  int iteration = 0;
70 
71  Vector3d centroid_m = Vector3d::Zero();
72  Vector3d centroid_d = Vector3d::Zero();
73  Transformd transform = Matrix4d::Identity();
74  Transformd delta = Matrix4d::Identity();
75 
76  size_t numPoints = m_dataCloud->numPoints();
77 
78  KDTree::Neighbor* neighbors = new KDTree::Neighbor[numPoints];
79 
80  for (iteration = 0; iteration < m_maxIterations; iteration++)
81  {
82  // Update break variables
83  prev_prev_ret = prev_ret;
84  prev_ret = ret;
85 
86  // Get point pairs
87  size_t pairs = KDTree::nearestNeighbors(m_searchTree, m_dataCloud, neighbors, m_maxDistanceMatch, centroid_m, centroid_d);
88 
89  // Get transformation
90  transform = Transformd::Identity();
91  ret = align.alignPoints(m_dataCloud, neighbors, centroid_m, centroid_d, transform);
92 
93  // Apply transformation
94  m_dataCloud->transform(transform, false);
95  delta = delta * transform;
96 
97  if (m_verbose)
98  {
99  cout << timestamp << "ICP Error is " << ret << " in iteration " << iteration << " / " << m_maxIterations << " using " << pairs << " points." << endl;
100  }
101 
102  // Check minimum distance
103  if ((fabs(ret - prev_ret) < m_epsilon) && (fabs(ret - prev_prev_ret) < m_epsilon))
104  {
105  break;
106  }
107  }
108 
109  delete[] neighbors;
110 
111  auto duration = chrono::steady_clock::now() - start_time;
112  cout << setw(6) << (int)(duration.count() / 1e6) << " ms, ";
113  cout << "Error: " << fixed << setprecision(3) << setw(7) << ret;
114  if (iteration < m_maxIterations)
115  {
116  cout << " after " << iteration << " Iterations";
117  }
118  cout << endl;
119  if (m_verbose)
120  {
121  cout << "Result: " << endl << m_dataCloud->deltaPose() << endl;
122  }
123 
124  return delta;
125 }
126 
128 {
129  m_maxDistanceMatch = d;
130 }
131 
133 {
134  m_maxIterations = i;
135 }
136 
138 {
139  m_maxLeafSize = m;
140 }
141 
143 {
144  m_epsilon = e;
145 }
146 void ICPPointAlign::setVerbose(bool verbose)
147 {
148  m_verbose = verbose;
149 }
150 
152 {
153  return m_maxDistanceMatch;
154 }
155 
157 {
158  return m_maxIterations;
159 }
160 
162 {
163  return m_maxLeafSize;
164 }
165 
167 {
168  return m_epsilon;
169 }
170 
172 {
173  return m_verbose;
174 }
175 
176 } /* namespace lvr2 */
void setMaxMatchDistance(double distance)
SLAMScanPtr m_dataCloud
void setMaxLeafSize(int maxLeafSize)
static size_t nearestNeighbors(KDTreePtr tree, SLAMScanPtr scan, Neighbor *neighbors, double maxDistance, Vector3d &centroid_m, Vector3d &centroid_d)
Finds the nearest neighbors of all points in a Scan using a pre-generated KDTree. ...
Definition: KDTree.cpp:176
static Timestamp timestamp
A global time stamp object for program runtime measurement.
Definition: Timestamp.hpp:116
Eigen::Vector3d Vector3d
Eigen 3D vector, double precision.
Point * Neighbor
Definition: KDTree.hpp:56
Transform< double > Transformd
4x4 double precision transformation matrix
Definition: MatrixTypes.hpp:71
static std::shared_ptr< KDTree > create(SLAMScanPtr scan, int maxLeafSize=20)
Creates a new KDTree from the given Scan.
Definition: KDTree.cpp:153
double getEpsilon() const
int getMaxLeafSize() const
void setEpsilon(double epsilon)
double getMaxMatchDistance() const
std::shared_ptr< SLAMScanWrapper > SLAMScanPtr
bool getVerbose() const
Transformd match()
Executes the ICPAlign.
void setVerbose(bool verbose)
T alignPoints(SLAMScanPtr scan, Point3 **neighbors, const Vec3 &centroid_m, const Vec3 &centroid_d, Mat4 &align) const
Calculates the estimated Transformation to match a Data Pointcloud to a Model Pointcloud.
void setMaxIterations(int iterations)
PointBufferPtr transform(PointBufferPtr pc_in, const Transformd &T)
Definition: qttf.cpp:32
int getMaxIterations() const


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Mon Feb 28 2022 22:46:06