Skip to content

Commit 17b05fb

Browse files
committed
Address review comments: use #pragma once, pcl::index_t, squared residuals, Eigen::Index
1 parent 1e17bf6 commit 17b05fb

1 file changed

Lines changed: 20 additions & 17 deletions

File tree

  • registration/include/pcl/registration/impl

registration/include/pcl/registration/impl/fricp.hpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
* All rights reserved.
88
*/
99

10-
#ifndef PCL_REGISTRATION_IMPL_FRICP_HPP_
11-
#define PCL_REGISTRATION_IMPL_FRICP_HPP_
10+
#pragma once
1211

1312
#include <pcl/common/common.h>
1413
#include <pcl/common/transforms.h>
@@ -133,7 +132,7 @@ FastRobustIterativeClosestPoint<PointSource, PointTarget, Scalar>::
133132
return;
134133
}
135134

136-
std::vector<int> source_indices;
135+
std::vector<pcl::index_t> source_indices;
137136
if (this->indices_ && !this->indices_->empty())
138137
source_indices.assign(this->indices_->begin(), this->indices_->end());
139138
else {
@@ -212,8 +211,8 @@ FastRobustIterativeClosestPoint<PointSource, PointTarget, Scalar>::
212211
if (use_welsch) {
213212
const double neighbor_med = findKNearestMedian(*target_centered, tree_data, 7);
214213
std::vector<double> residual_values(static_cast<std::size_t>(residuals.size()));
215-
for (int i = 0; i < residuals.size(); ++i)
216-
residual_values[static_cast<std::size_t>(i)] = residuals[i];
214+
for (Eigen::Index i = 0; i < residuals.size(); ++i)
215+
residual_values[static_cast<std::size_t>(i)] = std::sqrt(residuals(i));
217216
const double residual_med =
218217
pcl::computeMedian(residual_values.begin(), residual_values.end());
219218
nu_limit = std::max(nu_end_ratio_ * neighbor_med, same_threshold_);
@@ -239,7 +238,7 @@ FastRobustIterativeClosestPoint<PointSource, PointTarget, Scalar>::
239238
while (!outer_done) {
240239
for (int iter = 0; iter < this->max_iterations_; ++iter) {
241240
double energy =
242-
use_welsch ? computeEnergy(residuals, nu_current) : residuals.squaredNorm();
241+
use_welsch ? computeEnergy(residuals, nu_current) : residuals.sum();
243242
if (use_anderson_) {
244243
if (energy <= last_energy) {
245244
last_energy = energy;
@@ -259,8 +258,7 @@ FastRobustIterativeClosestPoint<PointSource, PointTarget, Scalar>::
259258
this->getClassName().c_str());
260259
return;
261260
}
262-
energy = use_welsch ? computeEnergy(residuals, nu_current)
263-
: residuals.squaredNorm();
261+
energy = use_welsch ? computeEnergy(residuals, nu_current) : residuals.sum();
264262
last_energy = energy;
265263
}
266264
}
@@ -371,7 +369,7 @@ FastRobustIterativeClosestPoint<PointSource, PointTarget, Scalar>::
371369
pcl::Indices nn_indices(1);
372370
std::vector<float> nn_sqr_dists(1);
373371

374-
for (int i = 0; i < source.cols(); ++i) {
372+
for (Eigen::Index i = 0; i < source.cols(); ++i) {
375373
const Eigen::Vector3d current = R * source.col(i) + t;
376374
query.x = static_cast<float>(current.x());
377375
query.y = static_cast<float>(current.y());
@@ -380,7 +378,9 @@ FastRobustIterativeClosestPoint<PointSource, PointTarget, Scalar>::
380378
return false;
381379
const auto idx = nn_indices[0];
382380
matched_targets.col(i) = target.col(static_cast<int>(idx));
383-
residuals[i] = (current - matched_targets.col(i)).norm();
381+
// Store squared distance reported by the search (avoid recomputing
382+
// the difference). Promote to double for internal computations.
383+
residuals(i) = static_cast<double>(nn_sqr_dists[0]);
384384
}
385385
return true;
386386
}
@@ -393,7 +393,8 @@ FastRobustIterativeClosestPoint<PointSource, PointTarget, Scalar>::computeEnergy
393393
if (nu < same_threshold_)
394394
nu = same_threshold_;
395395
const double denom = 2.0 * nu * nu;
396-
const Eigen::ArrayXd dist2 = residuals.array().square();
396+
// "residuals" contains squared distances already
397+
const Eigen::ArrayXd dist2 = residuals.array();
397398
return (1.0 - (-dist2 / denom).exp()).sum();
398399
}
399400

@@ -405,7 +406,8 @@ FastRobustIterativeClosestPoint<PointSource, PointTarget, Scalar>::computeWeight
405406
if (nu < same_threshold_)
406407
nu = same_threshold_;
407408
const double denom = 2.0 * nu * nu;
408-
return (-residuals.array().square() / denom).exp().matrix();
409+
// residuals already squared
410+
return (-residuals.array() / denom).exp().matrix();
409411
}
410412

411413
template <typename PointSource, typename PointTarget, typename Scalar>
@@ -459,7 +461,7 @@ FastRobustIterativeClosestPoint<PointSource, PointTarget, Scalar>::findKNearestM
459461
if (k < 2)
460462
return 0.0;
461463

462-
std::vector<double> local_medians(cloud.size(), 0.0);
464+
std::vector<double> local_medians;
463465
pcl::Indices nn_indices(k);
464466
std::vector<float> nn_sqr_dists(k);
465467
std::vector<double> dists;
@@ -471,10 +473,13 @@ FastRobustIterativeClosestPoint<PointSource, PointTarget, Scalar>::findKNearestM
471473
dists.clear();
472474
for (int j = 1; j < k; ++j)
473475
dists.push_back(std::sqrt(nn_sqr_dists[j]));
474-
if (!dists.empty())
475-
local_medians[i] = pcl::computeMedian(dists.begin(), dists.end());
476+
if (!dists.empty()) {
477+
local_medians.push_back(pcl::computeMedian(dists.begin(), dists.end()));
478+
}
476479
}
477480

481+
if (local_medians.empty())
482+
return 0.0;
478483
return pcl::computeMedian(local_medians.begin(), local_medians.end());
479484
}
480485

@@ -529,5 +534,3 @@ FastRobustIterativeClosestPoint<PointSource, PointTarget, Scalar>::matrixLog(
529534
}
530535

531536
} // namespace pcl
532-
533-
#endif // PCL_REGISTRATION_IMPL_FRICP_HPP_

0 commit comments

Comments
 (0)