Loading...
Searching...
No Matches
registration.hpp
/*
* Software License Agreement (BSD License)
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2010-2011, Willow Garage, Inc
* Copyright (c) 2012-, Open Perception, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the copyright holder(s) nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*
*/
#pragma once
namespace pcl {
template <typename PointSource, typename PointTarget, typename Scalar>
inline void
Registration<PointSource, PointTarget, Scalar>::setInputSource(
const PointCloudSourceConstPtr& cloud)
{
if (cloud->points.empty()) {
PCL_ERROR("[pcl::%s::setInputSource] Invalid or empty point cloud dataset given!\n",
getClassName().c_str());
return;
}
source_cloud_updated_ = true;
PCLBase<PointSource>::setInputCloud(cloud);
}
template <typename PointSource, typename PointTarget, typename Scalar>
inline void
Registration<PointSource, PointTarget, Scalar>::setInputTarget(
const PointCloudTargetConstPtr& cloud)
{
if (cloud->points.empty()) {
PCL_ERROR("[pcl::%s::setInputTarget] Invalid or empty point cloud dataset given!\n",
getClassName().c_str());
return;
}
target_ = cloud;
target_cloud_updated_ = true;
}
template <typename PointSource, typename PointTarget, typename Scalar>
bool
Registration<PointSource, PointTarget, Scalar>::initCompute()
{
if (!target_) {
PCL_ERROR("[pcl::registration::%s::compute] No input target dataset was given!\n",
getClassName().c_str());
return (false);
}
// Only update target kd-tree if a new target cloud was set
if (target_cloud_updated_ && !force_no_recompute_) {
tree_->setInputCloud(target_);
target_cloud_updated_ = false;
}
// Update the correspondence estimation
if (correspondence_estimation_) {
correspondence_estimation_->setSearchMethodTarget(tree_, force_no_recompute_);
correspondence_estimation_->setSearchMethodSource(tree_reciprocal_,
force_no_recompute_reciprocal_);
}
// Note: we /cannot/ update the search method on all correspondence rejectors, because
// we know nothing about them. If they should be cached, they must be cached
// individually.
return (PCLBase<PointSource>::initCompute());
}
template <typename PointSource, typename PointTarget, typename Scalar>
bool
Registration<PointSource, PointTarget, Scalar>::initComputeReciprocal()
{
if (!input_) {
PCL_ERROR("[pcl::registration::%s::compute] No input source dataset was given!\n",
getClassName().c_str());
return (false);
}
if (source_cloud_updated_ && !force_no_recompute_reciprocal_) {
tree_reciprocal_->setInputCloud(input_);
source_cloud_updated_ = false;
}
return (true);
}
template <typename PointSource, typename PointTarget, typename Scalar>
inline double
Registration<PointSource, PointTarget, Scalar>::getFitnessScore(
const std::vector<float>& distances_a, const std::vector<float>& distances_b)
{
unsigned int nr_elem =
static_cast<unsigned int>(std::min(distances_a.size(), distances_b.size()));
Eigen::VectorXf map_a = Eigen::VectorXf::Map(distances_a.data(), nr_elem);
Eigen::VectorXf map_b = Eigen::VectorXf::Map(distances_b.data(), nr_elem);
return (static_cast<double>((map_a - map_b).sum()) / static_cast<double>(nr_elem));
}
template <typename PointSource, typename PointTarget, typename Scalar>
inline double
Registration<PointSource, PointTarget, Scalar>::getFitnessScore(double max_range)
{
double fitness_score = 0.0;
// Transform the input dataset using the final transformation
PointCloudSource input_transformed;
transformPointCloud(*input_, input_transformed, final_transformation_);
pcl::Indices nn_indices(1);
std::vector<float> nn_dists(1);
// For each point in the source dataset
int nr = 0;
for (const auto& point : input_transformed) {
if (!input_->is_dense && !pcl::isXYZFinite(point))
continue;
// Find its nearest neighbor in the target
tree_->nearestKSearchT(point, 1, nn_indices, nn_dists);
// Deal with occlusions (incomplete targets)
if (nn_dists[0] <= max_range) {
// Add to the fitness score
fitness_score += nn_dists[0];
nr++;
}
}
if (nr > 0)
return (fitness_score / nr);
return (std::numeric_limits<double>::max());
}
template <typename PointSource, typename PointTarget, typename Scalar>
inline void
Registration<PointSource, PointTarget, Scalar>::align(PointCloudSource& output)
{
align(output, Matrix4::Identity());
}
template <typename PointSource, typename PointTarget, typename Scalar>
inline void
Registration<PointSource, PointTarget, Scalar>::align(PointCloudSource& output,
const Matrix4& guess)
{
if (!initCompute())
return;
// Resize the output dataset
output.resize(indices_->size());
// Copy the header
output.header = input_->header;
// Check if the output will be computed for all points or only a subset
if (indices_->size() != input_->size()) {
output.width = indices_->size();
output.height = 1;
}
else {
output.width = static_cast<std::uint32_t>(input_->width);
output.height = input_->height;
}
output.is_dense = input_->is_dense;
// Copy the point data to output
for (std::size_t i = 0; i < indices_->size(); ++i)
output[i] = (*input_)[(*indices_)[i]];
// Set the internal point representation of choice unless otherwise noted
if (point_representation_ && !force_no_recompute_)
tree_->setPointRepresentation(point_representation_);
// Perform the actual transformation computation
converged_ = false;
final_transformation_ = transformation_ = previous_transformation_ =
Matrix4::Identity();
// Right before we estimate the transformation, we set all the point.data[3] values to
// 1 to aid the rigid transformation
for (std::size_t i = 0; i < indices_->size(); ++i)
output[i].data[3] = 1.0;
computeTransformation(output, guess);
deinitCompute();
}
} // namespace pcl
pcl::PCLBase< PointSource >::input_
PointCloudConstPtr input_
Definition pcl_base.h:147
pcl::PCLBase::setInputCloud
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition pcl_base.hpp:65
pcl::PCLBase< PointSource >::indices_
IndicesPtr indices_
Definition pcl_base.h:150
pcl::PCLBase::initCompute
bool initCompute()
This method should get called before starting the actual computation.
Definition pcl_base.hpp:138
pcl::PCLBase< PointSource >::deinitCompute
bool deinitCompute()
Definition pcl_base.hpp:175
pcl::PointCloud::is_dense
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields).
Definition point_cloud.h:404
pcl::PointCloud::resize
void resize(std::size_t count)
Resizes the container to contain count elements.
Definition point_cloud.h:463
pcl::PointCloud::width
std::uint32_t width
The point cloud width (if organized as an image-structure).
Definition point_cloud.h:399
pcl::PointCloud::header
pcl::PCLHeader header
The point cloud header.
Definition point_cloud.h:393
pcl::PointCloud::height
std::uint32_t height
The point cloud height (if organized as an image-structure).
Definition point_cloud.h:401
pcl::Registration::target_cloud_updated_
bool target_cloud_updated_
Variable that stores whether we have a new target cloud, meaning we need to pre-process it again.
Definition registration.h:637
pcl::Registration::final_transformation_
Matrix4 final_transformation_
The final transformation matrix estimated by the registration method after N iterations.
Definition registration.h:573
pcl::Registration::initCompute
bool initCompute()
Internal computation initialization.
Definition registration.hpp:75
pcl::Registration::setInputSource
virtual void setInputSource(const PointCloudSourceConstPtr &cloud)
Provide a pointer to the input source (e.g., the point cloud that we want to align to the target).
Definition registration.hpp:47
pcl::Registration< PointSource, PointTarget, float >::transformation_
Matrix4 transformation_
Definition registration.h:576
pcl::Registration::PointCloudSource
pcl::PointCloud< PointSource > PointCloudSource
Definition registration.h:76
pcl::Registration::tree_reciprocal_
KdTreeReciprocalPtr tree_reciprocal_
A pointer to the spatial search object of the source.
Definition registration.h:554
pcl::Registration::PointCloudSourceConstPtr
typename PointCloudSource::ConstPtr PointCloudSourceConstPtr
Definition registration.h:78
pcl::Registration::setInputTarget
virtual void setInputTarget(const PointCloudTargetConstPtr &cloud)
Provide a pointer to the input target (e.g., the point cloud that we want to align the input source t...
Definition registration.hpp:61
pcl::Registration::correspondence_estimation_
CorrespondenceEstimationPtr correspondence_estimation_
A CorrespondenceEstimation object, used to estimate correspondences between the source and the target...
Definition registration.h:629
pcl::Registration::PointCloudTargetConstPtr
typename PointCloudTarget::ConstPtr PointCloudTargetConstPtr
Definition registration.h:82
pcl::Registration::force_no_recompute_
bool force_no_recompute_
A flag which, if set, means the tree operating on the target cloud will never be recomputed.
Definition registration.h:644
pcl::Registration::tree_
KdTreePtr tree_
A pointer to the spatial search object.
Definition registration.h:551
pcl::Registration< PointSource, PointTarget, float >::previous_transformation_
Matrix4 previous_transformation_
Definition registration.h:580
pcl::Registration< PointSource, PointTarget, float >::converged_
bool converged_
Definition registration.h:613
pcl::Registration::Matrix4
Eigen::Matrix< Scalar, 4, 4 > Matrix4
Definition registration.h:59
pcl::Registration::align
void align(PointCloudSource &output)
Call the registration algorithm which estimates the transformation and returns the transformed source...
Definition registration.hpp:168
pcl::Registration::getFitnessScore
double getFitnessScore(double max_range=std::numeric_limits< double >::max())
Obtain the Euclidean fitness score (e.g., mean of squared distances from the source to the target).
Definition registration.hpp:134
pcl::Registration::force_no_recompute_reciprocal_
bool force_no_recompute_reciprocal_
A flag which, if set, means the tree operating on the source cloud will never be recomputed.
Definition registration.h:648
pcl::Registration::initComputeReciprocal
bool initComputeReciprocal()
Internal computation when reciprocal lookup is needed.
Definition registration.hpp:105
pcl::Registration< PointSource, PointTarget, float >::computeTransformation
virtual void computeTransformation(PointCloudSource &output, const Matrix4 &guess)=0
pcl::Registration::source_cloud_updated_
bool source_cloud_updated_
Variable that stores whether we have a new source cloud, meaning we need to pre-process it again.
Definition registration.h:641
pcl::Registration::target_
PointCloudTargetConstPtr target_
The input point cloud dataset target.
Definition registration.h:569
pcl::Registration::getClassName
const std::string & getClassName() const
Abstract class get name method.
Definition registration.h:485
pcl::transformPointCloud
void transformPointCloud(const pcl::PointCloud< PointT > &cloud_in, pcl::PointCloud< PointT > &cloud_out, const Eigen::Matrix< Scalar, 4, 4 > &transform, bool copy_all_fields)
Apply a rigid transform defined by a 4x4 matrix.
Definition transforms.hpp:221
pcl
Definition convolution.h:46
pcl::Indices
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition types.h:133
pcl::isXYZFinite
constexpr bool isXYZFinite(const PointT &) noexcept
Definition point_tests.h:125