41 #ifndef PCL_REGISTRATION_IMPL_LUM_HPP_
42 #define PCL_REGISTRATION_IMPL_LUM_HPP_
48 namespace registration {
50 template <
typename Po
intT>
54 slam_graph_ = slam_graph;
57 template <
typename Po
intT>
64 template <
typename Po
intT>
68 return (num_vertices(*slam_graph_));
71 template <
typename Po
intT>
75 max_iterations_ = max_iterations;
78 template <
typename Po
intT>
82 return (max_iterations_);
85 template <
typename Po
intT>
89 convergence_threshold_ = convergence_threshold;
92 template <
typename Po
intT>
96 return (convergence_threshold_);
99 template <
typename Po
intT>
103 Vertex v = add_vertex(*slam_graph_);
104 (*slam_graph_)[v].cloud_ = cloud;
105 if (v == 0 && pose != Eigen::Vector6f::Zero()) {
107 "[pcl::registration::LUM::addPointCloud] The pose estimate is ignored for the "
108 "first cloud in the graph since that will become the reference pose.\n");
109 (*slam_graph_)[v].pose_ = Eigen::Vector6f::Zero();
112 (*slam_graph_)[v].pose_ = pose;
116 template <
typename Po
intT>
120 if (vertex >= getNumVertices()) {
121 PCL_ERROR(
"[pcl::registration::LUM::setPointCloud] You are attempting to set a "
122 "point cloud to a non-existing graph vertex.\n");
125 (*slam_graph_)[vertex].cloud_ = cloud;
128 template <
typename Po
intT>
132 if (vertex >= getNumVertices()) {
133 PCL_ERROR(
"[pcl::registration::LUM::getPointCloud] You are attempting to get a "
134 "point cloud from a non-existing graph vertex.\n");
137 return ((*slam_graph_)[vertex].cloud_);
140 template <
typename Po
intT>
144 if (vertex >= getNumVertices()) {
145 PCL_ERROR(
"[pcl::registration::LUM::setPose] You are attempting to set a pose "
146 "estimate to a non-existing graph vertex.\n");
150 PCL_ERROR(
"[pcl::registration::LUM::setPose] The pose estimate is ignored for the "
151 "first cloud in the graph since that will become the reference pose.\n");
154 (*slam_graph_)[vertex].pose_ = pose;
157 template <
typename Po
intT>
161 if (vertex >= getNumVertices()) {
162 PCL_ERROR(
"[pcl::registration::LUM::getPose] You are attempting to get a pose "
163 "estimate from a non-existing graph vertex.\n");
164 return (Eigen::Vector6f::Zero());
166 return ((*slam_graph_)[vertex].pose_);
169 template <
typename Po
intT>
170 inline Eigen::Affine3f
177 template <
typename Po
intT>
180 const Vertex& target_vertex,
183 if (source_vertex >= getNumVertices() || target_vertex >= getNumVertices() ||
184 source_vertex == target_vertex) {
186 "[pcl::registration::LUM::setCorrespondences] You are attempting to set a set "
187 "of correspondences between non-existing or identical graph vertices.\n");
192 std::tie(e, present) = edge(source_vertex, target_vertex, *slam_graph_);
194 std::tie(e, present) = add_edge(source_vertex, target_vertex, *slam_graph_);
195 (*slam_graph_)[e].corrs_ = corrs;
198 template <
typename Po
intT>
201 const Vertex& target_vertex)
const
203 if (source_vertex >= getNumVertices() || target_vertex >= getNumVertices()) {
204 PCL_ERROR(
"[pcl::registration::LUM::getCorrespondences] You are attempting to get "
205 "a set of correspondences between non-existing graph vertices.\n");
210 std::tie(e, present) = edge(source_vertex, target_vertex, *slam_graph_);
212 PCL_ERROR(
"[pcl::registration::LUM::getCorrespondences] You are attempting to get "
213 "a set of correspondences from a non-existing graph edge.\n");
216 return ((*slam_graph_)[e].corrs_);
219 template <
typename Po
intT>
223 int n =
static_cast<int>(getNumVertices());
225 PCL_ERROR(
"[pcl::registration::LUM::compute] The slam graph needs at least 2 "
229 for (
int i = 0; i < max_iterations_; ++i) {
232 typename SLAMGraph::edge_iterator e, e_end;
233 for (std::tie(e, e_end) = edges(*slam_graph_); e != e_end; ++e)
237 Eigen::MatrixXf G = Eigen::MatrixXf::Zero(6 * (n - 1), 6 * (n - 1));
238 Eigen::VectorXf
B = Eigen::VectorXf::Zero(6 * (n - 1));
241 for (
int vi = 1; vi != n; ++vi) {
242 for (
int vj = 0; vj != n; ++vj) {
247 std::tie(e, present1) = edge(vi, vj, *slam_graph_);
250 std::tie(e, present2) = edge(vj, vi, *slam_graph_);
257 G.block(6 * (vi - 1), 6 * (vj - 1), 6, 6) = -(*slam_graph_)[e].cinv_;
258 G.block(6 * (vi - 1), 6 * (vi - 1), 6, 6) += (*slam_graph_)[e].cinv_;
259 B.segment(6 * (vi - 1), 6) += (present1 ? 1 : -1) * (*slam_graph_)[e].cinvd_;
266 Eigen::VectorXf X = G.colPivHouseholderQr().solve(
B);
270 for (
int vi = 1; vi != n; ++vi) {
272 -incidenceCorrection(getPose(vi)).inverse() * X.segment(6 * (vi - 1), 6));
273 sum += difference_pose.norm();
274 setPose(vi, getPose(vi) + difference_pose);
278 if (sum <= convergence_threshold_ *
static_cast<float>(n - 1))
283 template <
typename Po
intT>
292 template <
typename Po
intT>
297 typename SLAMGraph::vertex_iterator v, v_end;
298 for (std::tie(v, v_end) = vertices(*slam_graph_); v != v_end; ++v) {
306 template <
typename Po
intT>
311 PointCloudPtr source_cloud = (*slam_graph_)[source(e, *slam_graph_)].cloud_;
312 PointCloudPtr target_cloud = (*slam_graph_)[target(e, *slam_graph_)].cloud_;
313 Eigen::Vector6f source_pose = (*slam_graph_)[source(e, *slam_graph_)].pose_;
314 Eigen::Vector6f target_pose = (*slam_graph_)[target(e, *slam_graph_)].pose_;
318 std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f>> corrs_aver(
320 std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f>> corrs_diff(
323 for (
int ici = 0; ici !=
static_cast<int>(corrs->size());
327 Eigen::Vector3f source_compounded =
334 (*source_cloud)[(*corrs)[ici].index_query].getVector3fMap();
335 Eigen::Vector3f target_compounded =
342 (*target_cloud)[(*corrs)[ici].index_match].getVector3fMap();
345 if (!std::isfinite(source_compounded(0)) || !std::isfinite(source_compounded(1)) ||
346 !std::isfinite(source_compounded(2)) || !std::isfinite(target_compounded(0)) ||
347 !std::isfinite(target_compounded(1)) || !std::isfinite(target_compounded(2)))
351 corrs_aver[oci] = 0.5 * (source_compounded + target_compounded);
352 corrs_diff[oci] = source_compounded - target_compounded;
355 corrs_aver.resize(oci);
356 corrs_diff.resize(oci);
360 PCL_WARN(
"[pcl::registration::LUM::compute] The correspondences between vertex %d "
361 "and %d do not contain enough valid correspondences to be considered for "
363 source(e, *slam_graph_),
364 target(e, *slam_graph_));
365 (*slam_graph_)[e].cinv_ = Eigen::Matrix6f::Zero();
366 (*slam_graph_)[e].cinvd_ = Eigen::Vector6f::Zero();
373 for (
int ci = 0; ci != oci; ++ci)
376 MM(0, 4) -= corrs_aver[ci](1);
377 MM(0, 5) += corrs_aver[ci](2);
378 MM(1, 3) -= corrs_aver[ci](2);
379 MM(1, 4) += corrs_aver[ci](0);
380 MM(2, 3) += corrs_aver[ci](1);
381 MM(2, 5) -= corrs_aver[ci](0);
382 MM(3, 4) -= corrs_aver[ci](0) * corrs_aver[ci](2);
383 MM(3, 5) -= corrs_aver[ci](0) * corrs_aver[ci](1);
384 MM(4, 5) -= corrs_aver[ci](1) * corrs_aver[ci](2);
386 corrs_aver[ci](1) * corrs_aver[ci](1) + corrs_aver[ci](2) * corrs_aver[ci](2);
388 corrs_aver[ci](0) * corrs_aver[ci](0) + corrs_aver[ci](1) * corrs_aver[ci](1);
390 corrs_aver[ci](0) * corrs_aver[ci](0) + corrs_aver[ci](2) * corrs_aver[ci](2);
393 MZ(0) += corrs_diff[ci](0);
394 MZ(1) += corrs_diff[ci](1);
395 MZ(2) += corrs_diff[ci](2);
397 corrs_aver[ci](1) * corrs_diff[ci](2) - corrs_aver[ci](2) * corrs_diff[ci](1);
399 corrs_aver[ci](0) * corrs_diff[ci](1) - corrs_aver[ci](1) * corrs_diff[ci](0);
401 corrs_aver[ci](2) * corrs_diff[ci](0) - corrs_aver[ci](0) * corrs_diff[ci](2);
404 MM(0, 0) = MM(1, 1) = MM(2, 2) =
static_cast<float>(oci);
420 for (
int ci = 0; ci != oci; ++ci)
421 ss +=
static_cast<float>(
422 std::pow(corrs_diff[ci](0) -
423 (D(0) + corrs_aver[ci](2) * D(5) - corrs_aver[ci](1) * D(4)),
425 std::pow(corrs_diff[ci](1) -
426 (D(1) + corrs_aver[ci](0) * D(4) - corrs_aver[ci](2) * D(3)),
428 std::pow(corrs_diff[ci](2) -
429 (D(2) + corrs_aver[ci](1) * D(3) - corrs_aver[ci](0) * D(5)),
433 if (ss < 0.0000000000001 || !std::isfinite(ss)) {
434 (*slam_graph_)[e].cinv_ = Eigen::Matrix6f::Zero();
435 (*slam_graph_)[e].cinvd_ = Eigen::Vector6f::Zero();
440 (*slam_graph_)[e].cinv_ = MM * (1.0f / ss);
441 (*slam_graph_)[e].cinvd_ = MZ * (1.0f / ss);
444 template <
typename Po
intT>
449 float cx = std::cos(pose(3)), sx = sinf(pose(3)), cy = std::cos(pose(4)),
451 out(0, 4) = pose(1) * sx - pose(2) * cx;
452 out(0, 5) = pose(1) * cx * cy + pose(2) * sx * cy;
454 out(1, 4) = -pose(0) * sx;
455 out(1, 5) = -pose(0) * cx * cy + pose(2) * sy;
456 out(2, 3) = -pose(1);
457 out(2, 4) = pose(0) * cx;
458 out(2, 5) = -pose(0) * sx * cy - pose(1) * sy;
463 out(5, 5) = -sx * cy;
470 #define PCL_INSTANTIATE_LUM(T) template class PCL_EXPORTS pcl::registration::LUM<T>;
472 #endif // PCL_REGISTRATION_IMPL_LUM_HPP_