41#ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_REGISTRATION_H_
42#define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_REGISTRATION_H_
44#include <pcl/sample_consensus/sac_model_registration.h>
47template <
typename Po
intT>
bool
52 PCL_ERROR (
"[pcl::SampleConsensusModelRegistration::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (),
sample_size_);
58 PointT p10 = (*input_)[samples[1]] - (*input_)[samples[0]];
59 PointT p20 = (*input_)[samples[2]] - (*input_)[samples[0]];
60 PointT p21 = (*input_)[samples[2]] - (*input_)[samples[1]];
68template <
typename Po
intT>
bool
73 PCL_ERROR (
"[pcl::SampleConsensusModelRegistration::computeModelCoefficients] No target dataset given!\n");
83 for (
int i = 0; i < 3; ++i)
88 PCL_ERROR (
"[pcl::SampleConsensusModelRegistration::computeModelCoefficients] Element with key %i is not in map (map contains %lu elements).\n",
92 indices_tgt[i] = it->second;
100template <
typename Po
intT>
void
105 PCL_ERROR (
"[pcl::SampleConsensusModelRegistration::getDistancesToModel] Number of source indices (%lu) differs than number of target indices (%lu)!\n",
indices_->size (),
indices_tgt_->size ());
111 PCL_ERROR (
"[pcl::SampleConsensusModelRegistration::getDistanceToModel] No target dataset given!\n");
123 Eigen::Matrix4f transform;
124 transform.row (0).matrix () = model_coefficients.segment<4>(0);
125 transform.row (1).matrix () = model_coefficients.segment<4>(4);
126 transform.row (2).matrix () = model_coefficients.segment<4>(8);
127 transform.row (3).matrix () = model_coefficients.segment<4>(12);
129 for (std::size_t i = 0; i <
indices_->size (); ++i)
138 Eigen::Vector4f p_tr (transform * pt_src);
146template <
typename Po
intT>
void
151 PCL_ERROR (
"[pcl::SampleConsensusModelRegistration::selectWithinDistance] Number of source indices (%lu) differs than number of target indices (%lu)!\n",
indices_->size (),
indices_tgt_->size ());
157 PCL_ERROR (
"[pcl::SampleConsensusModelRegistration::selectWithinDistance] No target dataset given!\n");
161 double thresh = threshold * threshold;
172 inliers.reserve (
indices_->size ());
175 Eigen::Matrix4f transform;
176 transform.row (0).matrix () = model_coefficients.segment<4>(0);
177 transform.row (1).matrix () = model_coefficients.segment<4>(4);
178 transform.row (2).matrix () = model_coefficients.segment<4>(8);
179 transform.row (3).matrix () = model_coefficients.segment<4>(12);
181 for (std::size_t i = 0; i <
indices_->size (); ++i)
190 Eigen::Vector4f p_tr (transform * pt_src);
192 float distance = (p_tr - pt_tgt).squaredNorm ();
194 if (distance < thresh)
203template <
typename Po
intT> std::size_t
205 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
209 PCL_ERROR (
"[pcl::SampleConsensusModelRegistration::countWithinDistance] Number of source indices (%lu) differs than number of target indices (%lu)!\n",
indices_->size (),
indices_tgt_->size ());
214 PCL_ERROR (
"[pcl::SampleConsensusModelRegistration::countWithinDistance] No target dataset given!\n");
218 double thresh = threshold * threshold;
226 Eigen::Matrix4f transform;
227 transform.row (0).matrix () = model_coefficients.segment<4>(0);
228 transform.row (1).matrix () = model_coefficients.segment<4>(4);
229 transform.row (2).matrix () = model_coefficients.segment<4>(8);
230 transform.row (3).matrix () = model_coefficients.segment<4>(12);
232 std::size_t nr_p = 0;
233 for (std::size_t i = 0; i <
indices_->size (); ++i)
242 Eigen::Vector4f p_tr (transform * pt_src);
244 if ((p_tr - pt_tgt).squaredNorm () < thresh)
247 PCL_DEBUG (
"[pcl::SampleConsensusModelRegistration::countWithinDistance] %zu inliers of %zu total points, threshold=%g\n", nr_p,
indices_->size(), threshold);
252template <
typename Po
intT>
void
257 PCL_ERROR (
"[pcl::SampleConsensusModelRegistration::optimizeModelCoefficients] Number of source indices (%lu) differs than number of target indices (%lu)!\n",
indices_->size (),
indices_tgt_->size ());
258 optimized_coefficients = model_coefficients;
265 optimized_coefficients = model_coefficients;
269 Indices indices_src (inliers.size ());
270 Indices indices_tgt (inliers.size ());
271 for (std::size_t i = 0; i < inliers.size (); ++i)
273 indices_src[i] = inliers[i];
277 PCL_ERROR (
"[pcl::SampleConsensusModelRegistration::optimizeModelCoefficients] Element with key %i is not in map (map contains %lu elements).\n",
279 optimized_coefficients = model_coefficients;
282 indices_tgt[i] = it->second;
289template <
typename Po
intT>
void
295 Eigen::VectorXf &transform)
const
297 transform.resize (16);
299 Eigen::Matrix<double, 3, Eigen::Dynamic> src (3, indices_src.size ());
300 Eigen::Matrix<double, 3, Eigen::Dynamic> tgt (3, indices_tgt.size ());
302 for (std::size_t i = 0; i < indices_src.size (); ++i)
304 src (0, i) = cloud_src[indices_src[i]].x;
305 src (1, i) = cloud_src[indices_src[i]].y;
306 src (2, i) = cloud_src[indices_src[i]].z;
308 tgt (0, i) = cloud_tgt[indices_tgt[i]].x;
309 tgt (1, i) = cloud_tgt[indices_tgt[i]].y;
310 tgt (2, i) = cloud_tgt[indices_tgt[i]].z;
314 PCL_DEBUG_STREAM(
"[pcl::SampleConsensusModelRegistration::estimateRigidTransformationSVD] src and tgt:" << std::endl << src << std::endl << std::endl << tgt << std::endl);
315 Eigen::Matrix4d transformation_matrix =
pcl::umeyama (src, tgt,
false);
318 transform.segment<4> (0).matrix () = transformation_matrix.cast<
float> ().row (0);
319 transform.segment<4> (4).matrix () = transformation_matrix.cast<
float> ().row (1);
320 transform.segment<4> (8).matrix () = transformation_matrix.cast<
float> ().row (2);
321 transform.segment<4> (12).matrix () = transformation_matrix.cast<
float> ().row (3);
324#define PCL_INSTANTIATE_SampleConsensusModelRegistration(T) template class PCL_EXPORTS pcl::SampleConsensusModelRegistration<T>;
PointCloud represents the base class in PCL for storing collections of 3D points.
unsigned int sample_size_
The size of a sample from which the model is computed.
IndicesPtr indices_
A pointer to the vector of point indices to use.
PointCloudConstPtr input_
A boost shared pointer to the point cloud data array.
virtual bool isModelValid(const Eigen::VectorXf &model_coefficients) const
Check whether a model is valid given the user constraints.
std::vector< double > error_sqr_dists_
A vector holding the distances to the computed model.
std::map< index_t, index_t > correspondences_
Given the index in the original point cloud, give the matching original index in the target cloud.
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.
IndicesPtr indices_tgt_
A pointer to the vector of target point indices to use.
bool computeModelCoefficients(const Indices &samples, Eigen::VectorXf &model_coefficients) const override
Compute a 4x4 rigid transformation matrix from the samples given.
PointCloudConstPtr target_
A boost shared pointer to the target point cloud data array.
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all distances from the transformed points to their correspondences.
void estimateRigidTransformationSVD(const pcl::PointCloud< PointT > &cloud_src, const Indices &indices_src, const pcl::PointCloud< PointT > &cloud_tgt, const Indices &indices_tgt, Eigen::VectorXf &transform) const
Estimate a rigid transformation between a source and a target point cloud using an SVD closed-form so...
bool isSampleGood(const Indices &samples) const override
Check if a sample of indices results in a good sample of points indices.
void optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the 4x4 transformation using the given inlier set.
double sample_dist_thresh_
Internal distance threshold used for the sample selection step.
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers) override
Select all the points which respect the given model coefficients as inliers.
IndicesAllocator<> Indices
Type used for indices in PCL.
Eigen::internal::umeyama_transform_matrix_type< Derived, OtherDerived >::type umeyama(const Eigen::MatrixBase< Derived > &src, const Eigen::MatrixBase< OtherDerived > &dst, bool with_scaling=false)
Returns the transformation between two point sets.