41 #ifndef PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_SVD_HPP_
42 #define PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_SVD_HPP_
44 #include <pcl/common/eigen.h>
48 namespace registration {
50 template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
55 Matrix4& transformation_matrix)
const
57 const auto nr_points = cloud_src.
size();
58 if (cloud_tgt.
size() != nr_points) {
59 PCL_ERROR(
"[pcl::TransformationEstimationSVD::estimateRigidTransformation] Number "
60 "or points in source (%zu) differs than target (%zu)!\n",
61 static_cast<std::size_t
>(nr_points),
62 static_cast<std::size_t
>(cloud_tgt.
size()));
68 estimateRigidTransformation(source_it, target_it, transformation_matrix);
71 template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
77 Matrix4& transformation_matrix)
const
79 if (indices_src.size() != cloud_tgt.
size()) {
80 PCL_ERROR(
"[pcl::TransformationSVD::estimateRigidTransformation] Number or points "
81 "in source (%zu) differs than target (%zu)!\n",
83 static_cast<std::size_t
>(cloud_tgt.
size()));
89 estimateRigidTransformation(source_it, target_it, transformation_matrix);
92 template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
99 Matrix4& transformation_matrix)
const
101 if (indices_src.size() != indices_tgt.size()) {
102 PCL_ERROR(
"[pcl::TransformationEstimationSVD::estimateRigidTransformation] Number "
103 "or points in source (%zu) differs than target (%zu)!\n",
111 estimateRigidTransformation(source_it, target_it, transformation_matrix);
114 template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
124 estimateRigidTransformation(source_it, target_it, transformation_matrix);
127 template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
135 const int npts =
static_cast<int>(source_it.
size());
138 Eigen::Matrix<Scalar, 3, Eigen::Dynamic> cloud_src(3, npts);
139 Eigen::Matrix<Scalar, 3, Eigen::Dynamic> cloud_tgt(3, npts);
141 for (
int i = 0; i < npts; ++i) {
142 cloud_src(0, i) = source_it->x;
143 cloud_src(1, i) = source_it->y;
144 cloud_src(2, i) = source_it->z;
147 cloud_tgt(0, i) = target_it->x;
148 cloud_tgt(1, i) = target_it->y;
149 cloud_tgt(2, i) = target_it->z;
154 transformation_matrix =
pcl::umeyama(cloud_src, cloud_tgt,
false);
160 transformation_matrix.setIdentity();
162 Eigen::Matrix<Scalar, 4, 1> centroid_src, centroid_tgt;
170 Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> cloud_src_demean,
175 getTransformationFromCorrelation(cloud_src_demean,
179 transformation_matrix);
183 template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
187 const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& cloud_src_demean,
188 const Eigen::Matrix<Scalar, 4, 1>& centroid_src,
189 const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& cloud_tgt_demean,
190 const Eigen::Matrix<Scalar, 4, 1>& centroid_tgt,
191 Matrix4& transformation_matrix)
const
193 transformation_matrix.setIdentity();
196 Eigen::Matrix<Scalar, 3, 3> H =
197 (cloud_src_demean * cloud_tgt_demean.transpose()).
template topLeftCorner<3, 3>();
200 Eigen::JacobiSVD<Eigen::Matrix<Scalar, 3, 3>> svd(
201 H, Eigen::ComputeFullU | Eigen::ComputeFullV);
202 Eigen::Matrix<Scalar, 3, 3> u = svd.matrixU();
203 Eigen::Matrix<Scalar, 3, 3> v = svd.matrixV();
206 if (u.determinant() * v.determinant() < 0) {
207 for (
int x = 0; x < 3; ++x)
211 Eigen::Matrix<Scalar, 3, 3> R = v * u.transpose();
214 transformation_matrix.template topLeftCorner<3, 3>() = R;
215 const Eigen::Matrix<Scalar, 3, 1> Rc(R * centroid_src.template head<3>());
216 transformation_matrix.template block<3, 1>(0, 3) =
217 centroid_tgt.template head<3>() - Rc;
220 size_t N = cloud_src_demean.cols();
221 PCL_DEBUG(
"[pcl::registration::TransformationEstimationSVD::"
222 "getTransformationFromCorrelation] Loss: %.10e\n",
223 (cloud_tgt_demean - R * cloud_src_demean).squaredNorm() / N);
Iterator class for point clouds with or without given indices.
std::size_t size() const
Size of the range the iterator is going through.
void demeanPointCloud(ConstCloudIterator< PointT > &cloud_iterator, const Eigen::Matrix< Scalar, 4, 1 > ¢roid, pcl::PointCloud< PointT > &cloud_out, int npts=0)
Subtract a centroid from a point cloud and return the de-meaned representation.
unsigned int compute3DCentroid(ConstCloudIterator< PointT > &cloud_iterator, Eigen::Matrix< Scalar, 4, 1 > ¢roid)
Compute the 3D (X-Y-Z) centroid of a set of points and return it as a 3D vector.
PCL_EXPORTS bool isVerbosityLevelEnabled(VERBOSITY_LEVEL severity)
is verbosity level enabled?
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
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.
IndicesAllocator<> Indices
Type used for indices in PCL.