41 #ifndef PCL_REGISTRATION_IMPL_LUM_HPP_
42 #define PCL_REGISTRATION_IMPL_LUM_HPP_
47 template<
typename Po
intT>
inline void
50 slam_graph_ = slam_graph;
64 return (num_vertices (*slam_graph_));
68 template<
typename Po
intT>
void
71 max_iterations_ = max_iterations;
75 template<
typename Po
intT>
inline int
78 return (max_iterations_);
82 template<
typename Po
intT>
void
85 convergence_threshold_ = convergence_threshold;
89 template<
typename Po
intT>
inline float
92 return (convergence_threshold_);
99 Vertex v = add_vertex (*slam_graph_);
100 (*slam_graph_)[v].cloud_ = cloud;
101 if (v == 0 && pose != Eigen::Vector6f::Zero ())
103 PCL_WARN(
"[pcl::registration::LUM::addPointCloud] The pose estimate is ignored for the first cloud in the graph since that will become the reference pose.\n");
104 (*slam_graph_)[v].pose_ = Eigen::Vector6f::Zero ();
107 (*slam_graph_)[v].pose_ = pose;
112 template<
typename Po
intT>
inline void
115 if (vertex >= getNumVertices ())
117 PCL_ERROR(
"[pcl::registration::LUM::setPointCloud] You are attempting to set a point cloud to a non-existing graph vertex.\n");
120 (*slam_graph_)[vertex].cloud_ = cloud;
127 if (vertex >= getNumVertices ())
129 PCL_ERROR(
"[pcl::registration::LUM::getPointCloud] You are attempting to get a point cloud from a non-existing graph vertex.\n");
132 return ((*slam_graph_)[vertex].cloud_);
136 template<
typename Po
intT>
inline void
139 if (vertex >= getNumVertices ())
141 PCL_ERROR(
"[pcl::registration::LUM::setPose] You are attempting to set a pose estimate to a non-existing graph vertex.\n");
146 PCL_ERROR(
"[pcl::registration::LUM::setPose] The pose estimate is ignored for the first cloud in the graph since that will become the reference pose.\n");
149 (*slam_graph_)[vertex].pose_ = pose;
156 if (vertex >= getNumVertices ())
158 PCL_ERROR(
"[pcl::registration::LUM::getPose] You are attempting to get a pose estimate from a non-existing graph vertex.\n");
159 return (Eigen::Vector6f::Zero ());
161 return ((*slam_graph_)[vertex].pose_);
165 template<
typename Po
intT>
inline Eigen::Affine3f
173 template<
typename Po
intT>
void
176 if (source_vertex >= getNumVertices () || target_vertex >= getNumVertices () || source_vertex == target_vertex)
178 PCL_ERROR(
"[pcl::registration::LUM::setCorrespondences] You are attempting to set a set of correspondences between non-existing or identical graph vertices.\n");
183 std::tie (e, present) = edge (source_vertex, target_vertex, *slam_graph_);
185 std::tie (e, present) = add_edge (source_vertex, target_vertex, *slam_graph_);
186 (*slam_graph_)[e].corrs_ = corrs;
193 if (source_vertex >= getNumVertices () || target_vertex >= getNumVertices ())
195 PCL_ERROR(
"[pcl::registration::LUM::getCorrespondences] You are attempting to get a set of correspondences between non-existing graph vertices.\n");
200 std::tie (e, present) = edge (source_vertex, target_vertex, *slam_graph_);
203 PCL_ERROR(
"[pcl::registration::LUM::getCorrespondences] You are attempting to get a set of correspondences from a non-existing graph edge.\n");
206 return ((*slam_graph_)[e].corrs_);
210 template<
typename Po
intT>
void
213 int n = static_cast<int> (getNumVertices ());
216 PCL_ERROR(
"[pcl::registration::LUM::compute] The slam graph needs at least 2 vertices.\n");
219 for (
int i = 0; i < max_iterations_; ++i)
222 typename SLAMGraph::edge_iterator e, e_end;
223 for (std::tie (e, e_end) = edges (*slam_graph_); e != e_end; ++e)
227 Eigen::MatrixXf G = Eigen::MatrixXf::Zero (6 * (n - 1), 6 * (n - 1));
228 Eigen::VectorXf
B = Eigen::VectorXf::Zero (6 * (n - 1));
231 for (
int vi = 1; vi != n; ++vi)
233 for (
int vj = 0; vj != n; ++vj)
238 std::tie (e, present1) = edge (vi, vj, *slam_graph_);
242 std::tie (e, present2) = edge (vj, vi, *slam_graph_);
249 G.block (6 * (vi - 1), 6 * (vj - 1), 6, 6) = -(*slam_graph_)[e].cinv_;
250 G.block (6 * (vi - 1), 6 * (vi - 1), 6, 6) += (*slam_graph_)[e].cinv_;
251 B.segment (6 * (vi - 1), 6) += (present1 ? 1 : -1) * (*slam_graph_)[e].cinvd_;
257 Eigen::VectorXf X = G.colPivHouseholderQr ().solve (
B);
261 for (
int vi = 1; vi != n; ++vi)
263 Eigen::Vector6f difference_pose = static_cast<Eigen::Vector6f> (-incidenceCorrection (getPose (vi)).inverse () * X.segment (6 * (vi - 1), 6));
264 sum += difference_pose.norm ();
265 setPose (vi, getPose (vi) + difference_pose);
269 if (sum <= convergence_threshold_ * static_cast<float> (n - 1))
288 typename SLAMGraph::vertex_iterator v, v_end;
289 for (std::tie (v, v_end) = vertices (*slam_graph_); v != v_end; ++v)
299 template<
typename Po
intT>
void
303 PointCloudPtr source_cloud = (*slam_graph_)[source (e, *slam_graph_)].cloud_;
304 PointCloudPtr target_cloud = (*slam_graph_)[target (e, *slam_graph_)].cloud_;
305 Eigen::Vector6f source_pose = (*slam_graph_)[source (e, *slam_graph_)].pose_;
306 Eigen::Vector6f target_pose = (*slam_graph_)[target (e, *slam_graph_)].pose_;
310 std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f> > corrs_aver (corrs->size ());
311 std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f> > corrs_diff (corrs->size ());
313 for (
int ici = 0; ici != static_cast<int> (corrs->size ()); ++ici)
316 Eigen::Vector3f source_compounded =
pcl::getTransformation (source_pose (0), source_pose (1), source_pose (2), source_pose (3), source_pose (4), source_pose (5)) * source_cloud->points[(*corrs)[ici].index_query].getVector3fMap ();
317 Eigen::Vector3f target_compounded =
pcl::getTransformation (target_pose (0), target_pose (1), target_pose (2), target_pose (3), target_pose (4), target_pose (5)) * target_cloud->points[(*corrs)[ici].index_match].getVector3fMap ();
320 if (!std::isfinite (source_compounded (0)) || !std::isfinite (source_compounded (1)) || !std::isfinite (source_compounded (2)) || !std::isfinite (target_compounded (0)) || !std::isfinite (target_compounded (1)) || !std::isfinite (target_compounded (2)))
324 corrs_aver[oci] = 0.5 * (source_compounded + target_compounded);
325 corrs_diff[oci] = source_compounded - target_compounded;
328 corrs_aver.resize (oci);
329 corrs_diff.resize (oci);
334 PCL_WARN(
"[pcl::registration::LUM::compute] The correspondences between vertex %d and %d do not contain enough valid correspondences to be considered for computation.\n", source (e, *slam_graph_), target (e, *slam_graph_));
335 (*slam_graph_)[e].cinv_ = Eigen::Matrix6f::Zero ();
336 (*slam_graph_)[e].cinvd_ = Eigen::Vector6f::Zero ();
343 for (
int ci = 0; ci != oci; ++ci)
346 MM (0, 4) -= corrs_aver[ci] (1);
347 MM (0, 5) += corrs_aver[ci] (2);
348 MM (1, 3) -= corrs_aver[ci] (2);
349 MM (1, 4) += corrs_aver[ci] (0);
350 MM (2, 3) += corrs_aver[ci] (1);
351 MM (2, 5) -= corrs_aver[ci] (0);
352 MM (3, 4) -= corrs_aver[ci] (0) * corrs_aver[ci] (2);
353 MM (3, 5) -= corrs_aver[ci] (0) * corrs_aver[ci] (1);
354 MM (4, 5) -= corrs_aver[ci] (1) * corrs_aver[ci] (2);
355 MM (3, 3) += corrs_aver[ci] (1) * corrs_aver[ci] (1) + corrs_aver[ci] (2) * corrs_aver[ci] (2);
356 MM (4, 4) += corrs_aver[ci] (0) * corrs_aver[ci] (0) + corrs_aver[ci] (1) * corrs_aver[ci] (1);
357 MM (5, 5) += corrs_aver[ci] (0) * corrs_aver[ci] (0) + corrs_aver[ci] (2) * corrs_aver[ci] (2);
360 MZ (0) += corrs_diff[ci] (0);
361 MZ (1) += corrs_diff[ci] (1);
362 MZ (2) += corrs_diff[ci] (2);
363 MZ (3) += corrs_aver[ci] (1) * corrs_diff[ci] (2) - corrs_aver[ci] (2) * corrs_diff[ci] (1);
364 MZ (4) += corrs_aver[ci] (0) * corrs_diff[ci] (1) - corrs_aver[ci] (1) * corrs_diff[ci] (0);
365 MZ (5) += corrs_aver[ci] (2) * corrs_diff[ci] (0) - corrs_aver[ci] (0) * corrs_diff[ci] (2);
368 MM (0, 0) = MM (1, 1) = MM (2, 2) = static_cast<float> (oci);
369 MM (4, 0) = MM (0, 4);
370 MM (5, 0) = MM (0, 5);
371 MM (3, 1) = MM (1, 3);
372 MM (4, 1) = MM (1, 4);
373 MM (3, 2) = MM (2, 3);
374 MM (5, 2) = MM (2, 5);
375 MM (4, 3) = MM (3, 4);
376 MM (5, 3) = MM (3, 5);
377 MM (5, 4) = MM (4, 5);
384 for (
int ci = 0; ci != oci; ++ci)
385 ss += static_cast<float> (std::pow (corrs_diff[ci] (0) - (D (0) + corrs_aver[ci] (2) * D (5) - corrs_aver[ci] (1) * D (4)), 2.0f)
386 + std::pow (corrs_diff[ci] (1) - (D (1) + corrs_aver[ci] (0) * D (4) - corrs_aver[ci] (2) * D (3)), 2.0f)
387 + std::pow (corrs_diff[ci] (2) - (D (2) + corrs_aver[ci] (1) * D (3) - corrs_aver[ci] (0) * D (5)), 2.0f));
390 if (ss < 0.0000000000001 || !std::isfinite (ss))
392 (*slam_graph_)[e].cinv_ = Eigen::Matrix6f::Zero ();
393 (*slam_graph_)[e].cinvd_ = Eigen::Vector6f::Zero ();
398 (*slam_graph_)[e].cinv_ = MM * (1.0f / ss);
399 (*slam_graph_)[e].cinvd_ = MZ * (1.0f / ss);
407 float cx = std::cos (pose (3)), sx = sinf (pose (3)), cy = std::cos (pose (4)), sy = sinf (pose (4));
408 out (0, 4) = pose (1) * sx - pose (2) * cx;
409 out (0, 5) = pose (1) * cx * cy + pose (2) * sx * cy;
410 out (1, 3) = pose (2);
411 out (1, 4) = -pose (0) * sx;
412 out (1, 5) = -pose (0) * cx * cy + pose (2) * sy;
413 out (2, 3) = -pose (1);
414 out (2, 4) = pose (0) * cx;
415 out (2, 5) = -pose (0) * sx * cy - pose (1) * sy;
418 out (4, 5) = cx * cy;
420 out (5, 5) = -sx * cy;
424 #define PCL_INSTANTIATE_LUM(T) template class PCL_EXPORTS pcl::registration::LUM<T>;
426 #endif // PCL_REGISTRATION_IMPL_LUM_HPP_