sac_model_registration.h
/*
* 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
#include <pcl/pcl_macros.h>
#include <pcl/pcl_base.h>
#include <pcl/sample_consensus/eigen.h>
#include <pcl/sample_consensus/sac_model.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/common/eigen.h>
#include <pcl/common/centroid.h>
#include <map>
namespace pcl
{
/** \brief SampleConsensusModelRegistration defines a model for Point-To-Point registration outlier rejection.
* \author Radu Bogdan Rusu
* \ingroup sample_consensus
*/
template <typename PointT>
class SampleConsensusModelRegistration : public SampleConsensusModel<PointT>
{
public:
using SampleConsensusModel<PointT>::model_name_;
using SampleConsensusModel<PointT>::input_;
using SampleConsensusModel<PointT>::indices_;
using SampleConsensusModel<PointT>::error_sqr_dists_;
using SampleConsensusModel<PointT>::isModelValid;
using PointCloud = typename SampleConsensusModel<PointT>::PointCloud;
using PointCloudPtr = typename SampleConsensusModel<PointT>::PointCloudPtr;
using PointCloudConstPtr = typename SampleConsensusModel<PointT>::PointCloudConstPtr;
using Ptr = shared_ptr<SampleConsensusModelRegistration<PointT> >;
using ConstPtr = shared_ptr<const SampleConsensusModelRegistration<PointT>>;
/** \brief Constructor for base SampleConsensusModelRegistration.
* \param[in] cloud the input point cloud dataset
* \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
*/
SampleConsensusModelRegistration (const PointCloudConstPtr &cloud,
bool random = false)
: SampleConsensusModel<PointT> (cloud, random)
, target_ ()
, sample_dist_thresh_ (0)
{
// Call our own setInputCloud
setInputCloud (cloud);
model_name_ = "SampleConsensusModelRegistration";
sample_size_ = 3;
model_size_ = 16;
}
/** \brief Constructor for base SampleConsensusModelRegistration.
* \param[in] cloud the input point cloud dataset
* \param[in] indices a vector of point indices to be used from \a cloud
* \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
*/
SampleConsensusModelRegistration (const PointCloudConstPtr &cloud,
const std::vector<int> &indices,
bool random = false)
: SampleConsensusModel<PointT> (cloud, indices, random)
, target_ ()
, sample_dist_thresh_ (0)
{
computeOriginalIndexMapping ();
computeSampleDistanceThreshold (cloud, indices);
model_name_ = "SampleConsensusModelRegistration";
sample_size_ = 3;
model_size_ = 16;
}
/** \brief Empty destructor */
~SampleConsensusModelRegistration () {}
/** \brief Provide a pointer to the input dataset
* \param[in] cloud the const boost shared pointer to a PointCloud message
*/
inline void
setInputCloud (const PointCloudConstPtr &cloud) override
{
SampleConsensusModel<PointT>::setInputCloud (cloud);
computeOriginalIndexMapping ();
computeSampleDistanceThreshold (cloud);
}
/** \brief Set the input point cloud target.
* \param[in] target the input point cloud target
*/
inline void
setInputTarget (const PointCloudConstPtr &target)
{
target_ = target;
indices_tgt_.reset (new std::vector<int>);
// Cache the size and fill the target indices
int target_size = static_cast<int> (target->size ());
indices_tgt_->resize (target_size);
for (int i = 0; i < target_size; ++i)
(*indices_tgt_)[i] = i;
computeOriginalIndexMapping ();
}
/** \brief Set the input point cloud target.
* \param[in] target the input point cloud target
* \param[in] indices_tgt a vector of point indices to be used from \a target
*/
inline void
setInputTarget (const PointCloudConstPtr &target, const std::vector<int> &indices_tgt)
{
target_ = target;
indices_tgt_.reset (new std::vector<int> (indices_tgt));
computeOriginalIndexMapping ();
}
/** \brief Compute a 4x4 rigid transformation matrix from the samples given
* \param[in] samples the indices found as good candidates for creating a valid model
* \param[out] model_coefficients the resultant model coefficients
*/
bool
computeModelCoefficients (const std::vector<int> &samples,
Eigen::VectorXf &model_coefficients) const override;
/** \brief Compute all distances from the transformed points to their correspondences
* \param[in] model_coefficients the 4x4 transformation matrix
* \param[out] distances the resultant estimated distances
*/
void
getDistancesToModel (const Eigen::VectorXf &model_coefficients,
std::vector<double> &distances) const override;
/** \brief Select all the points which respect the given model coefficients as inliers.
* \param[in] model_coefficients the 4x4 transformation matrix
* \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
* \param[out] inliers the resultant model inliers
*/
void
selectWithinDistance (const Eigen::VectorXf &model_coefficients,
const double threshold,
std::vector<int> &inliers) override;
/** \brief Count all the points which respect the given model coefficients as inliers.
*
* \param[in] model_coefficients the coefficients of a model that we need to compute distances to
* \param[in] threshold maximum admissible distance threshold for determining the inliers from the outliers
* \return the resultant number of inliers
*/
std::size_t
countWithinDistance (const Eigen::VectorXf &model_coefficients,
const double threshold) const override;
/** \brief Recompute the 4x4 transformation using the given inlier set
* \param[in] inliers the data inliers found as supporting the model
* \param[in] model_coefficients the initial guess for the optimization
* \param[out] optimized_coefficients the resultant recomputed transformation
*/
void
optimizeModelCoefficients (const std::vector<int> &inliers,
const Eigen::VectorXf &model_coefficients,
Eigen::VectorXf &optimized_coefficients) const override;
void
projectPoints (const std::vector<int> &,
const Eigen::VectorXf &,
PointCloud &, bool = true) const override
{
};
bool
doSamplesVerifyModel (const std::set<int> &,
const Eigen::VectorXf &,
const double) const override
{
return (false);
}
/** \brief Return a unique id for this model (SACMODEL_REGISTRATION). */
inline pcl::SacModel
getModelType () const override { return (SACMODEL_REGISTRATION); }
protected:
using SampleConsensusModel<PointT>::sample_size_;
using SampleConsensusModel<PointT>::model_size_;
/** \brief Check if a sample of indices results in a good sample of points
* indices.
* \param[in] samples the resultant index samples
*/
bool
isSampleGood (const std::vector<int> &samples) const override;
/** \brief Computes an "optimal" sample distance threshold based on the
* principal directions of the input cloud.
* \param[in] cloud the const boost shared pointer to a PointCloud message
*/
inline void
computeSampleDistanceThreshold (const PointCloudConstPtr &cloud)
{
// Compute the principal directions via PCA
Eigen::Vector4f xyz_centroid;
Eigen::Matrix3f covariance_matrix = Eigen::Matrix3f::Zero ();
computeMeanAndCovarianceMatrix (*cloud, covariance_matrix, xyz_centroid);
// Check if the covariance matrix is finite or not.
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
if (!std::isfinite (covariance_matrix.coeffRef (i, j)))
PCL_ERROR ("[pcl::SampleConsensusModelRegistration::computeSampleDistanceThreshold] Covariance matrix has NaN values! Is the input cloud finite?\n");
Eigen::Vector3f eigen_values;
pcl::eigen33 (covariance_matrix, eigen_values);
// Compute the distance threshold for sample selection
sample_dist_thresh_ = eigen_values.array ().sqrt ().sum () / 3.0;
sample_dist_thresh_ *= sample_dist_thresh_;
PCL_DEBUG ("[pcl::SampleConsensusModelRegistration::setInputCloud] Estimated a sample selection distance threshold of: %f\n", sample_dist_thresh_);
}
/** \brief Computes an "optimal" sample distance threshold based on the
* principal directions of the input cloud.
* \param[in] cloud the const boost shared pointer to a PointCloud message
* \param indices
*/
inline void
computeSampleDistanceThreshold (const PointCloudConstPtr &cloud,
const std::vector<int> &indices)
{
// Compute the principal directions via PCA
Eigen::Vector4f xyz_centroid;
Eigen::Matrix3f covariance_matrix;
computeMeanAndCovarianceMatrix (*cloud, indices, covariance_matrix, xyz_centroid);
// Check if the covariance matrix is finite or not.
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
if (!std::isfinite (covariance_matrix.coeffRef (i, j)))
PCL_ERROR ("[pcl::SampleConsensusModelRegistration::computeSampleDistanceThreshold] Covariance matrix has NaN values! Is the input cloud finite?\n");
Eigen::Vector3f eigen_values;
pcl::eigen33 (covariance_matrix, eigen_values);
// Compute the distance threshold for sample selection
sample_dist_thresh_ = eigen_values.array ().sqrt ().sum () / 3.0;
sample_dist_thresh_ *= sample_dist_thresh_;
PCL_DEBUG ("[pcl::SampleConsensusModelRegistration::setInputCloud] Estimated a sample selection distance threshold of: %f\n", sample_dist_thresh_);
}
/** \brief Estimate a rigid transformation between a source and a target point cloud using an SVD closed-form
* solution of absolute orientation using unit quaternions
* \param[in] cloud_src the source point cloud dataset
* \param[in] indices_src the vector of indices describing the points of interest in cloud_src
* \param[in] cloud_tgt the target point cloud dataset
* \param[in] indices_tgt the vector of indices describing the correspondences of the interest points from
* indices_src
* \param[out] transform the resultant transformation matrix (as model coefficients)
*
* This method is an implementation of: Horn, B. “Closed-Form Solution of Absolute Orientation Using Unit Quaternions,” JOSA A, Vol. 4, No. 4, 1987
*/
void
estimateRigidTransformationSVD (const pcl::PointCloud<PointT> &cloud_src,
const std::vector<int> &indices_src,
const pcl::PointCloud<PointT> &cloud_tgt,
const std::vector<int> &indices_tgt,
Eigen::VectorXf &transform) const;
/** \brief Compute mappings between original indices of the input_/target_ clouds. */
void
computeOriginalIndexMapping ()
{
if (!indices_tgt_ || !indices_ || indices_->empty () || indices_->size () != indices_tgt_->size ())
return;
for (std::size_t i = 0; i < indices_->size (); ++i)
correspondences_[(*indices_)[i]] = (*indices_tgt_)[i];
}
/** \brief A boost shared pointer to the target point cloud data array. */
PointCloudConstPtr target_;
/** \brief A pointer to the vector of target point indices to use. */
IndicesPtr indices_tgt_;
/** \brief Given the index in the original point cloud, give the matching original index in the target cloud */
std::map<int, int> correspondences_;
/** \brief Internal distance threshold used for the sample selection step. */
double sample_dist_thresh_;
public:
PCL_MAKE_ALIGNED_OPERATOR_NEW
};
}
#include <pcl/sample_consensus/impl/sac_model_registration.hpp>
pcl::computeMeanAndCovarianceMatrix
unsigned int computeMeanAndCovarianceMatrix(const pcl::PointCloud< PointT > &cloud, Eigen::Matrix< Scalar, 3, 3 > &covariance_matrix, Eigen::Matrix< Scalar, 4, 1 > &centroid)
Compute the normalized 3x3 covariance matrix and the centroid of a given set of points in a single lo...
Definition: centroid.hpp:483
pcl::SampleConsensusModelRegistration::sample_dist_thresh_
double sample_dist_thresh_
Internal distance threshold used for the sample selection step.
Definition: sac_model_registration.h:325
pcl_macros.h
Defines all the PCL and non-PCL macros used.
pcl
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
pcl::IndicesPtr
shared_ptr< Indices > IndicesPtr
Definition: pcl_base.h:61
pcl::SampleConsensusModelRegistration::setInputCloud
void setInputCloud(const PointCloudConstPtr &cloud) override
Provide a pointer to the input dataset.
Definition: sac_model_registration.h:118
pcl::SampleConsensusModelRegistration::getModelType
pcl::SacModel getModelType() const override
Return a unique id for this model (SACMODEL_REGISTRATION).
Definition: sac_model_registration.h:217
pcl::SampleConsensusModelRegistration::computeSampleDistanceThreshold
void computeSampleDistanceThreshold(const PointCloudConstPtr &cloud, const std::vector< int > &indices)
Computes an "optimal" sample distance threshold based on the principal directions of the input cloud.
Definition: sac_model_registration.h:264
pcl::SampleConsensusModelRegistration::computeSampleDistanceThreshold
void computeSampleDistanceThreshold(const PointCloudConstPtr &cloud)
Computes an "optimal" sample distance threshold based on the principal directions of the input cloud.
Definition: sac_model_registration.h:235
pcl::SampleConsensusModelRegistration::isSampleGood
bool isSampleGood(const std::vector< int > &samples) const override
Check if a sample of indices results in a good sample of points indices.
Definition: sac_model_registration.hpp:51
pcl::SampleConsensusModel::sample_size_
unsigned int sample_size_
The size of a sample from which the model is computed.
Definition: sac_model.h:561
pcl::SampleConsensusModelRegistration::optimizeModelCoefficients
void optimizeModelCoefficients(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the 4x4 transformation using the given inlier set.
Definition: sac_model_registration.hpp:239
pcl::SampleConsensusModel::model_size_
unsigned int model_size_
The number of coefficients in the model.
Definition: sac_model.h:564
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: projection_matrix.h:52
pcl::eigen33
void eigen33(const Matrix &mat, typename Matrix::Scalar &eigenvalue, Vector &eigenvector)
determines the eigenvector and eigenvalue of the smallest eigenvalue of the symmetric positive semi d...
Definition: eigen.hpp:291
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:623
pcl::SampleConsensusModelRegistration::estimateRigidTransformationSVD
void estimateRigidTransformationSVD(const pcl::PointCloud< PointT > &cloud_src, const std::vector< int > &indices_src, const pcl::PointCloud< PointT > &cloud_tgt, const std::vector< int > &indices_tgt, Eigen::VectorXf &transform) const
Estimate a rigid transformation between a source and a target point cloud using an SVD closed-form so...
Definition: sac_model_registration.hpp:268
pcl::SampleConsensusModelRegistration::PointCloudConstPtr
typename SampleConsensusModel< PointT >::PointCloudConstPtr PointCloudConstPtr
Definition: sac_model_registration.h:70
pcl::SampleConsensusModelRegistration::getDistancesToModel
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all distances from the transformed points to their correspondences.
Definition: sac_model_registration.hpp:88
pcl::SampleConsensusModelRegistration::indices_tgt_
IndicesPtr indices_tgt_
A pointer to the vector of target point indices to use.
Definition: sac_model_registration.h:319
pcl::SacModel
SacModel
Definition: model_types.h:45
pcl::SampleConsensusModel::indices_
IndicesPtr indices_
A pointer to the vector of point indices to use.
Definition: sac_model.h:529
pcl::SampleConsensusModel::ConstPtr
shared_ptr< const SampleConsensusModel< PointT > > ConstPtr
Definition: sac_model.h:77
pcl::SampleConsensusModel::setInputCloud
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition: sac_model.h:293
pcl::SampleConsensusModelRegistration::countWithinDistance
std::size_t countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold) const override
Count all the points which respect the given model coefficients as inliers.
Definition: sac_model_registration.hpp:193
PCL_MAKE_ALIGNED_OPERATOR_NEW
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: pcl_macros.h:371
pcl::SampleConsensusModelRegistration::computeOriginalIndexMapping
void computeOriginalIndexMapping()
Compute mappings between original indices of the input_/target_ clouds.
Definition: sac_model_registration.h:307
pcl::SampleConsensusModel::Ptr
shared_ptr< SampleConsensusModel< PointT > > Ptr
Definition: sac_model.h:76
pcl::SampleConsensusModel::model_name_
std::string model_name_
The model name.
Definition: sac_model.h:523
pcl::SampleConsensusModelRegistration::correspondences_
std::map< int, int > correspondences_
Given the index in the original point cloud, give the matching original index in the target cloud.
Definition: sac_model_registration.h:322
pcl::SampleConsensusModel::PointCloudConstPtr
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: sac_model.h:72
pcl::SampleConsensusModelRegistration::SampleConsensusModelRegistration
SampleConsensusModelRegistration(const PointCloudConstPtr &cloud, const std::vector< int > &indices, bool random=false)
Constructor for base SampleConsensusModelRegistration.
Definition: sac_model_registration.h:97
pcl::SampleConsensusModelRegistration::doSamplesVerifyModel
bool doSamplesVerifyModel(const std::set< int > &, const Eigen::VectorXf &, const double) const override
Verify whether a subset of indices verifies a given set of model coefficients.
Definition: sac_model_registration.h:208
pcl::SampleConsensusModelRegistration::setInputTarget
void setInputTarget(const PointCloudConstPtr &target)
Set the input point cloud target.
Definition: sac_model_registration.h:129
pcl::SampleConsensusModelRegistration::computeModelCoefficients
bool computeModelCoefficients(const std::vector< int > &samples, Eigen::VectorXf &model_coefficients) const override
Compute a 4x4 rigid transformation matrix from the samples given.
Definition: sac_model_registration.hpp:67
pcl::SampleConsensusModelRegistration::projectPoints
void projectPoints(const std::vector< int > &, const Eigen::VectorXf &, PointCloud &, bool=true) const override
Create a new point cloud with inliers projected onto the model.
Definition: sac_model_registration.h:201
pcl::SampleConsensusModelRegistration::target_
PointCloudConstPtr target_
A boost shared pointer to the target point cloud data array.
Definition: sac_model_registration.h:316
pcl::SampleConsensusModel::PointCloudPtr
typename PointCloud::Ptr PointCloudPtr
Definition: sac_model.h:73
pcl::SampleConsensusModel
SampleConsensusModel represents the base model class.
Definition: sac_model.h:68
pcl::SampleConsensusModelRegistration::setInputTarget
void setInputTarget(const PointCloudConstPtr &target, const std::vector< int > &indices_tgt)
Set the input point cloud target.
Definition: sac_model_registration.h:147
pcl::SampleConsensusModelRegistration
SampleConsensusModelRegistration defines a model for Point-To-Point registration outlier rejection.
Definition: sac_model_registration.h:59
pcl::SACMODEL_REGISTRATION
Definition: model_types.h:60
centroid.h
pcl::SampleConsensusModelRegistration::SampleConsensusModelRegistration
SampleConsensusModelRegistration(const PointCloudConstPtr &cloud, bool random=false)
Constructor for base SampleConsensusModelRegistration.
Definition: sac_model_registration.h:79
pcl::shared_ptr
boost::shared_ptr< T > shared_ptr
Alias for boost::shared_ptr.
Definition: pcl_macros.h:90
pcl::SampleConsensusModelRegistration::selectWithinDistance
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, std::vector< int > &inliers) override
Select all the points which respect the given model coefficients as inliers.
Definition: sac_model_registration.hpp:134
pcl::SampleConsensusModelRegistration::~SampleConsensusModelRegistration
~SampleConsensusModelRegistration()
Empty destructor.
Definition: sac_model_registration.h:112