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 */
lvr2::ICPPointAlign::getEpsilon
double getEpsilon() const
Definition: ICPPointAlign.cpp:166
lvr2::ICPPointAlign::setMaxLeafSize
void setMaxLeafSize(int maxLeafSize)
Definition: ICPPointAlign.cpp:137
lvr2::ICPPointAlign::m_maxDistanceMatch
double m_maxDistanceMatch
Definition: ICPPointAlign.hpp:84
lvr2::ICPPointAlign::m_maxIterations
int m_maxIterations
Definition: ICPPointAlign.hpp:85
lvr2::ICPPointAlign::getMaxIterations
int getMaxIterations() const
Definition: ICPPointAlign.cpp:156
transform
Definition: src/tools/lvr2_transform/Options.cpp:44
lvr2::ICPPointAlign::m_searchTree
KDTreePtr m_searchTree
Definition: ICPPointAlign.hpp:93
lvr2::EigenSVDPointAlign::alignPoints
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.
lvr2::Transformd
Transform< double > Transformd
4x4 double precision transformation matrix
Definition: MatrixTypes.hpp:71
lvr2::ICPPointAlign::getVerbose
bool getVerbose() const
Definition: ICPPointAlign.cpp:171
lvr2::ICPPointAlign::m_epsilon
double m_epsilon
Definition: ICPPointAlign.hpp:83
lvr2::ICPPointAlign::setVerbose
void setVerbose(bool verbose)
Definition: ICPPointAlign.cpp:146
lvr2::ICPPointAlign::setEpsilon
void setEpsilon(double epsilon)
Definition: ICPPointAlign.cpp:142
lvr2::ICPPointAlign::getMaxMatchDistance
double getMaxMatchDistance() const
Definition: ICPPointAlign.cpp:151
lvr2::KDTree::create
static std::shared_ptr< KDTree > create(SLAMScanPtr scan, int maxLeafSize=20)
Creates a new KDTree from the given Scan.
Definition: KDTree.cpp:153
lvr2::ICPPointAlign::m_maxLeafSize
int m_maxLeafSize
Definition: ICPPointAlign.hpp:86
lvr2::ICPPointAlign::m_verbose
bool m_verbose
Definition: ICPPointAlign.hpp:88
lvr2::ICPPointAlign::getMaxLeafSize
int getMaxLeafSize() const
Definition: ICPPointAlign.cpp:161
lvr2::timestamp
static Timestamp timestamp
A global time stamp object for program runtime measurement.
Definition: Timestamp.hpp:116
lvr2::KDTree::nearestNeighbors
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
lvr2::SLAMScanPtr
std::shared_ptr< SLAMScanWrapper > SLAMScanPtr
Definition: SLAMScanWrapper.hpp:221
lvr2::ICPPointAlign::m_dataCloud
SLAMScanPtr m_dataCloud
Definition: ICPPointAlign.hpp:91
lvr2::qttf::transform
PointBufferPtr transform(PointBufferPtr pc_in, const Transformd &T)
Definition: qttf.cpp:32
lvr2::Vector3d
Eigen::Vector3d Vector3d
Eigen 3D vector, double precision.
Definition: MatrixTypes.hpp:121
EigenSVDPointAlign.hpp
lvr2::ICPPointAlign::setMaxMatchDistance
void setMaxMatchDistance(double distance)
Definition: ICPPointAlign.cpp:127
std
Definition: HalfEdge.hpp:124
lvr2
Definition: BaseBufferManipulators.hpp:39
Timestamp.hpp
lvr2::ICPPointAlign::setMaxIterations
void setMaxIterations(int iterations)
Definition: ICPPointAlign.cpp:132
lvr2::EigenSVDPointAlign
Definition: EigenSVDPointAlign.hpp:46
ICPPointAlign.hpp
lvr2::ICPPointAlign::match
Transformd match()
Executes the ICPAlign.
Definition: ICPPointAlign.cpp:58
lvr2::KDTree::Neighbor
Point * Neighbor
Definition: KDTree.hpp:56


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 Wed Mar 2 2022 00:37:23