40 #ifndef PCL_SURFACE_IMPL_MLS_H_
41 #define PCL_SURFACE_IMPL_MLS_H_
43 #include <pcl/point_traits.h>
44 #include <pcl/surface/mls.h>
45 #include <pcl/common/io.h>
46 #include <pcl/common/centroid.h>
47 #include <pcl/common/eigen.h>
48 #include <pcl/common/geometry.h>
55 template <
typename Po
intInT,
typename Po
intOutT>
void
66 normals_->header = input_->header;
68 normals_->width = normals_->height = 0;
69 normals_->points.clear ();
74 output.
header = input_->header;
78 if (search_radius_ <= 0 || sqr_gauss_param_ <= 0)
80 PCL_ERROR (
"[pcl::%s::process] Invalid search radius (%f) or Gaussian parameter (%f)!\n", getClassName ().c_str (), search_radius_, sqr_gauss_param_);
85 if (upsample_method_ == DISTINCT_CLOUD && !distinct_cloud_)
87 PCL_ERROR (
"[pcl::%s::process] Upsample method was set to DISTINCT_CLOUD, but no distinct cloud was specified.\n", getClassName ().c_str ());
99 if (input_->isOrganized ())
103 setSearchMethod (tree);
107 tree_->setInputCloud (input_);
109 switch (upsample_method_)
112 case (RANDOM_UNIFORM_DENSITY):
114 rng_alg_.seed (static_cast<unsigned> (std::time (0)));
115 float tmp =
static_cast<float> (search_radius_ / 2.0f);
116 boost::uniform_real<float> uniform_distrib (-tmp, tmp);
117 rng_uniform_distribution_.reset (
new boost::variate_generator<boost::mt19937&, boost::uniform_real<float> > (rng_alg_, uniform_distrib));
121 case (VOXEL_GRID_DILATION):
122 case (DISTINCT_CLOUD):
124 mls_results_.resize (input_->size ());
132 performProcessing (output);
134 if (compute_normals_)
136 normals_->height = 1;
137 normals_->width =
static_cast<uint32_t
> (normals_->size ());
139 for (
unsigned int i = 0; i < output.
size (); ++i)
152 output.
width =
static_cast<uint32_t
> (output.
size ());
158 template <
typename Po
intInT,
typename Po
intOutT>
void
160 const std::vector<int> &nn_indices,
161 std::vector<float> &nn_sqr_dists,
172 Eigen::Vector4d xyz_centroid;
181 Eigen::Vector4d model_coefficients;
182 pcl::eigen33 (covariance_matrix, eigen_value, eigen_vector);
183 model_coefficients.head<3> ().matrix () = eigen_vector;
184 model_coefficients[3] = 0;
185 model_coefficients[3] = -1 * model_coefficients.dot (xyz_centroid);
188 Eigen::Vector3d point = input_->points[index].getVector3fMap ().template cast<double> ();
189 double distance = point.dot (model_coefficients.head<3> ()) + model_coefficients[3];
190 point -= distance * model_coefficients.head<3> ();
192 float curvature =
static_cast<float> (covariance_matrix.trace ());
195 curvature = fabsf (
float (eigen_value /
double (curvature)));
199 Eigen::Vector3d plane_normal = model_coefficients.head<3> ();
201 Eigen::VectorXd c_vec;
203 Eigen::Vector3d v_axis (0.0f, 0.0f, 0.0f), u_axis (0.0f, 0.0f, 0.0f);
209 if (polynomial_fit_ && static_cast<int> (nn_indices.size ()) >= nr_coeff_)
213 std::vector<Eigen::Vector3d> de_meaned (nn_indices.size ());
214 for (
size_t ni = 0; ni < nn_indices.size (); ++ni)
216 de_meaned[ni][0] = input_->points[nn_indices[ni]].x - point[0];
217 de_meaned[ni][1] = input_->points[nn_indices[ni]].y - point[1];
218 de_meaned[ni][2] = input_->points[nn_indices[ni]].z - point[2];
219 nn_sqr_dists[ni] =
static_cast<float> (de_meaned[ni].dot (de_meaned[ni]));
223 Eigen::VectorXd weight_vec (nn_indices.size ());
224 Eigen::MatrixXd P (nr_coeff_, nn_indices.size ());
225 Eigen::VectorXd f_vec (nn_indices.size ());
226 Eigen::MatrixXd P_weight;
227 Eigen::MatrixXd P_weight_Pt (nr_coeff_, nr_coeff_);
230 v_axis = plane_normal.unitOrthogonal ();
231 u_axis = plane_normal.cross (v_axis);
235 double u_coord, v_coord, u_pow, v_pow;
236 for (
size_t ni = 0; ni < nn_indices.size (); ++ni)
239 weight_vec (ni) = exp (-nn_sqr_dists[ni] / sqr_gauss_param_);
242 u_coord = de_meaned[ni].dot (u_axis);
243 v_coord = de_meaned[ni].dot (v_axis);
244 f_vec (ni) = de_meaned[ni].dot (plane_normal);
249 for (
int ui = 0; ui <= order_; ++ui)
252 for (
int vi = 0; vi <= order_ - ui; ++vi)
254 P (j++, ni) = u_pow * v_pow;
262 P_weight = P * weight_vec.asDiagonal ();
263 P_weight_Pt = P_weight * P.transpose ();
264 c_vec = P_weight * f_vec;
265 P_weight_Pt.llt ().solveInPlace (c_vec);
268 switch (upsample_method_)
272 Eigen::Vector3d normal = plane_normal;
274 if (polynomial_fit_ && static_cast<int> (nn_indices.size ()) >= nr_coeff_ && pcl_isfinite (c_vec[0]))
276 point += (c_vec[0] * plane_normal);
279 if (compute_normals_)
280 normal = plane_normal - c_vec[order_ + 1] * u_axis - c_vec[1] * v_axis;
284 aux.x =
static_cast<float> (point[0]);
285 aux.y =
static_cast<float> (point[1]);
286 aux.z =
static_cast<float> (point[2]);
289 if (compute_normals_)
292 aux_normal.normal_x =
static_cast<float> (normal[0]);
293 aux_normal.normal_y =
static_cast<float> (normal[1]);
294 aux_normal.normal_z =
static_cast<float> (normal[2]);
296 projected_points_normals.
push_back (aux_normal);
297 corresponding_input_indices.
indices.push_back (index);
303 case (SAMPLE_LOCAL_PLANE):
306 for (
float u_disp = -static_cast<float> (upsampling_radius_); u_disp <= upsampling_radius_; u_disp += static_cast<float> (upsampling_step_))
307 for (
float v_disp = -static_cast<float> (upsampling_radius_); v_disp <= upsampling_radius_; v_disp += static_cast<float> (upsampling_step_))
308 if (u_disp*u_disp + v_disp*v_disp < upsampling_radius_*upsampling_radius_)
310 PointOutT projected_point;
312 projectPointToMLSSurface (u_disp, v_disp, u_axis, v_axis, plane_normal, point,
314 static_cast<int> (nn_indices.size ()),
315 projected_point, projected_normal);
317 projected_points.
push_back (projected_point);
318 corresponding_input_indices.
indices.push_back (index);
319 if (compute_normals_)
320 projected_points_normals.
push_back (projected_normal);
325 case (RANDOM_UNIFORM_DENSITY):
328 int num_points_to_add =
static_cast<int> (floor (desired_num_points_in_radius_ / 2.0 / static_cast<double> (nn_indices.size ())));
331 if (num_points_to_add <= 0)
334 Eigen::Vector3d normal = plane_normal;
335 if (polynomial_fit_ && static_cast<int> (nn_indices.size ()) >= nr_coeff_ && pcl_isfinite (c_vec[0]))
338 point += (c_vec[0] * plane_normal);
340 if (compute_normals_)
341 normal = plane_normal - c_vec[order_ + 1] * u_axis - c_vec[1] * v_axis;
344 aux.x =
static_cast<float> (point[0]);
345 aux.y =
static_cast<float> (point[1]);
346 aux.z =
static_cast<float> (point[2]);
348 corresponding_input_indices.
indices.push_back (index);
350 if (compute_normals_)
353 aux_normal.normal_x =
static_cast<float> (normal[0]);
354 aux_normal.normal_y =
static_cast<float> (normal[1]);
355 aux_normal.normal_z =
static_cast<float> (normal[2]);
357 projected_points_normals.
push_back (aux_normal);
363 for (
int num_added = 0; num_added < num_points_to_add;)
365 float u_disp = (*rng_uniform_distribution_) (),
366 v_disp = (*rng_uniform_distribution_) ();
368 if (u_disp * u_disp + v_disp * v_disp > search_radius_ * search_radius_/4)
372 PointOutT projected_point;
374 projectPointToMLSSurface (u_disp, v_disp, u_axis, v_axis, plane_normal, point,
376 static_cast<int> (nn_indices.size ()),
377 projected_point, projected_normal);
379 projected_points.
push_back (projected_point);
380 corresponding_input_indices.
indices.push_back (index);
381 if (compute_normals_)
382 projected_points_normals.
push_back (projected_normal);
390 case (VOXEL_GRID_DILATION):
391 case (DISTINCT_CLOUD):
395 mls_result =
MLSResult (point, plane_normal, u_axis, v_axis, c_vec, static_cast<int> (nn_indices.size ()), curvature);
402 template <
typename Po
intInT,
typename Po
intOutT>
void
404 Eigen::Vector3d &u, Eigen::Vector3d &v,
405 Eigen::Vector3d &plane_normal,
406 Eigen::Vector3d &mean,
408 Eigen::VectorXd &c_vec,
410 PointOutT &result_point,
413 double n_disp = 0.0f;
414 double d_u = 0.0f, d_v = 0.0f;
417 if (polynomial_fit_ && num_neighbors >= 5*nr_coeff_ && pcl_isfinite (c_vec[0]))
422 float u_pow = 1.0f, v_pow = 1.0f, u_pow_prev = 1.0f, v_pow_prev = 1.0f;
423 for (
int ui = 0; ui <= order_; ++ui)
426 for (
int vi = 0; vi <= order_ - ui; ++vi)
429 n_disp += u_pow * v_pow * c_vec[j++];
433 d_u += c_vec[j-1] * ui * u_pow_prev * v_pow;
435 d_v += c_vec[j-1] * vi * u_pow * v_pow_prev;
445 result_point.x =
static_cast<float> (mean[0] + u[0] * u_disp + v[0] * v_disp + plane_normal[0] * n_disp);
446 result_point.y =
static_cast<float> (mean[1] + u[1] * u_disp + v[1] * v_disp + plane_normal[1] * n_disp);
447 result_point.z =
static_cast<float> (mean[2] + u[2] * u_disp + v[2] * v_disp + plane_normal[2] * n_disp);
449 Eigen::Vector3d normal = plane_normal - d_u * u - d_v * v;
452 result_normal.normal_x =
static_cast<float> (normal[0]);
453 result_normal.normal_y =
static_cast<float> (normal[1]);
454 result_normal.normal_z =
static_cast<float> (normal[2]);
459 template <
typename Po
intInT,
typename Po
intOutT>
void
463 nr_coeff_ = (order_ + 1) * (order_ + 2) / 2;
467 std::vector<int> nn_indices;
468 std::vector<float> nn_sqr_dists;
471 for (
size_t cp = 0; cp < indices_->size (); ++cp)
474 if (!searchForNeighbors ((*indices_)[cp], nn_indices, nn_sqr_dists))
480 if (nn_indices.size () < 3)
487 int index = (*indices_)[cp];
488 computeMLSPointNormal (index, nn_indices, nn_sqr_dists, projected_points, projected_points_normals, *corresponding_input_indices_, mls_results_[index]);
492 for (
size_t pp = 0; pp < projected_points.
size (); ++pp)
493 copyMissingFields (input_->points[(*indices_)[cp]], projected_points[pp]);
497 output.
insert (output.
end (), projected_points.
begin (), projected_points.
end ());
498 if (compute_normals_)
499 normals_->insert (normals_->end (), projected_points_normals.
begin (), projected_points_normals.
end ());
503 performUpsampling (output);
508 template <
typename Po
intInT,
typename Po
intOutT>
void
509 pcl::MovingLeastSquaresOMP<PointInT, PointOutT>::performProcessing (PointCloudOut &output)
512 nr_coeff_ = (order_ + 1) * (order_ + 2) / 2;
515 unsigned int threads = threads_ == 0 ? 1 : threads_;
518 typename PointCloudOut::CloudVectorType projected_points (threads);
519 typename NormalCloud::CloudVectorType projected_points_normals (threads);
520 std::vector<PointIndices> corresponding_input_indices (threads);
523 #pragma omp parallel for schedule (dynamic,1000) num_threads (threads)
524 for (
int cp = 0; cp < static_cast<int> (indices_->size ()); ++cp)
528 std::vector<int> nn_indices;
529 std::vector<float> nn_sqr_dists;
532 if (this->searchForNeighbors ((*indices_)[cp], nn_indices, nn_sqr_dists))
536 if (nn_indices.size () >= 3)
539 int tn = omp_get_thread_num ();
542 size_t pp_size = projected_points[tn].size ();
545 int index = (*indices_)[cp];
546 this->computeMLSPointNormal (index, nn_indices, nn_sqr_dists, projected_points[tn], projected_points_normals[tn], corresponding_input_indices[tn], this->mls_results_[index]);
549 for (
size_t pp = pp_size; pp < projected_points[tn].size (); ++pp)
550 this->copyMissingFields (input_->points[(*indices_)[cp]], projected_points[tn][pp]);
557 for (
unsigned int tn = 0; tn < threads; ++tn)
559 output.insert (output.end (), projected_points[tn].begin (), projected_points[tn].end ());
560 corresponding_input_indices_->indices.insert (corresponding_input_indices_->indices.end (),
561 corresponding_input_indices[tn].indices.begin (), corresponding_input_indices[tn].indices.end ());
562 if (compute_normals_)
563 normals_->insert (normals_->end (), projected_points_normals[tn].begin (), projected_points_normals[tn].end ());
567 this->performUpsampling (output);
572 template <
typename Po
intInT,
typename Po
intOutT>
void
575 if (upsample_method_ == DISTINCT_CLOUD)
577 for (
size_t dp_i = 0; dp_i < distinct_cloud_->size (); ++dp_i)
580 if (!pcl_isfinite (distinct_cloud_->points[dp_i].x))
585 std::vector<int> nn_indices;
586 std::vector<float> nn_dists;
587 tree_->nearestKSearch (distinct_cloud_->points[dp_i], 1, nn_indices, nn_dists);
588 int input_index = nn_indices.front ();
592 if (mls_results_[input_index].valid ==
false)
595 Eigen::Vector3d add_point = distinct_cloud_->points[dp_i].getVector3fMap ().template cast<double> ();
597 float u_disp =
static_cast<float> ((add_point - mls_results_[input_index].mean).dot (mls_results_[input_index].u_axis)),
598 v_disp = static_cast<float> ((add_point - mls_results_[input_index].mean).dot (mls_results_[input_index].v_axis));
600 PointOutT result_point;
602 projectPointToMLSSurface (u_disp, v_disp,
603 mls_results_[input_index].u_axis, mls_results_[input_index].v_axis,
604 mls_results_[input_index].plane_normal,
605 mls_results_[input_index].mean,
606 mls_results_[input_index].curvature,
607 mls_results_[input_index].c_vec,
608 mls_results_[input_index].num_neighbors,
609 result_point, result_normal);
612 copyMissingFields (input_->points[input_index], result_point);
615 corresponding_input_indices_->indices.push_back (input_index);
618 if (compute_normals_)
619 normals_->push_back (result_normal);
625 if (upsample_method_ == VOXEL_GRID_DILATION)
627 MLSVoxelGrid voxel_grid (input_, indices_, voxel_size_);
628 for (
int iteration = 0; iteration < dilation_iteration_num_; ++iteration)
631 for (
typename MLSVoxelGrid::HashMap::iterator m_it = voxel_grid.
voxel_grid_.begin (); m_it != voxel_grid.
voxel_grid_.end (); ++m_it)
642 std::vector<int> nn_indices;
643 std::vector<float> nn_dists;
644 tree_->nearestKSearch (p, 1, nn_indices, nn_dists);
645 int input_index = nn_indices.front ();
649 if (mls_results_[input_index].valid ==
false)
652 Eigen::Vector3d add_point = p.getVector3fMap ().template cast<double> ();
653 float u_disp =
static_cast<float> ((add_point - mls_results_[input_index].mean).dot (mls_results_[input_index].u_axis)),
654 v_disp = static_cast<float> ((add_point - mls_results_[input_index].mean).dot (mls_results_[input_index].v_axis));
656 PointOutT result_point;
658 projectPointToMLSSurface (u_disp, v_disp,
659 mls_results_[input_index].u_axis, mls_results_[input_index].v_axis,
660 mls_results_[input_index].plane_normal,
661 mls_results_[input_index].mean,
662 mls_results_[input_index].curvature,
663 mls_results_[input_index].c_vec,
664 mls_results_[input_index].num_neighbors,
665 result_point, result_normal);
668 copyMissingFields (input_->points[input_index], result_point);
671 corresponding_input_indices_->indices.push_back (input_index);
675 if (compute_normals_)
676 normals_->push_back (result_normal);
682 template <
typename Po
intInT,
typename Po
intOutT>
684 const Eigen::Vector3d &a_plane_normal,
685 const Eigen::Vector3d &a_u,
686 const Eigen::Vector3d &a_v,
687 const Eigen::VectorXd a_c_vec,
688 const int a_num_neighbors,
689 const float &a_curvature) :
690 mean (a_mean), plane_normal (a_plane_normal), u_axis (a_u), v_axis (a_v), c_vec (a_c_vec), num_neighbors (a_num_neighbors),
691 curvature (a_curvature), valid (true)
696 template <
typename Po
intInT,
typename Po
intOutT>
700 voxel_grid_ (), bounding_min_ (), bounding_max_ (), data_size_ (),
voxel_size_ (voxel_size)
705 double max_size = (std::max) ((std::max)(bounding_box_size.x (), bounding_box_size.y ()), bounding_box_size.z ());
708 for (
unsigned int i = 0; i < indices->size (); ++i)
709 if (pcl_isfinite (cloud->points[(*indices)[i]].x))
712 getCellIndex (cloud->points[(*indices)[i]].getVector3fMap (), pos);
722 template <
typename Po
intInT,
typename Po
intOutT>
void
725 HashMap new_voxel_grid = voxel_grid_;
726 for (
typename MLSVoxelGrid::HashMap::iterator m_it = voxel_grid_.begin (); m_it != voxel_grid_.end (); ++m_it)
728 Eigen::Vector3i index;
729 getIndexIn3D (m_it->first, index);
732 for (
int x = -1; x <= 1; ++x)
733 for (
int y = -1; y <= 1; ++y)
734 for (
int z = -1; z <= 1; ++z)
735 if (x != 0 || y != 0 || z != 0)
737 Eigen::Vector3i new_index;
738 new_index = index + Eigen::Vector3i (x, y, z);
741 getIndexIn1D (new_index, index_1d);
743 new_voxel_grid[index_1d] = leaf;
746 voxel_grid_ = new_voxel_grid;
751 template <
typename Po
intInT,
typename Po
intOutT>
void
753 PointOutT &point_out)
const
759 PointOutT temp = point_out;
762 point_out.x = temp.x;
763 point_out.y = temp.y;
764 point_out.z = temp.z;
768 #define PCL_INSTANTIATE_MovingLeastSquares(T,OutT) template class PCL_EXPORTS pcl::MovingLeastSquares<T,OutT>;
771 #define PCL_INSTANTIATE_MovingLeastSquaresOMP(T,OutT) template class PCL_EXPORTS pcl::MovingLeastSquaresOMP<T,OutT>;
774 #endif // PCL_SURFACE_IMPL_MLS_H_
A point structure representing normal coordinates and the surface curvature estimate.
search::KdTree is a wrapper class which inherits the pcl::KdTree class for performing search function...
A helper functor that can set a specific value in a field if the field exists.
struct pcl::_PointXYZHSV EIGEN_ALIGN16
virtual void performProcessing(PointCloudOut &output)
Abstract surface reconstruction method.
boost::shared_ptr< std::vector< int > > IndicesPtr
Eigen::Vector4f bounding_max_
std::vector< int > indices
uint32_t width
The point cloud width (if organized as an image-structure).
pcl::PCLHeader header
The point cloud header.
void projectPointToMLSSurface(float &u_disp, float &v_disp, Eigen::Vector3d &u_axis, Eigen::Vector3d &v_axis, Eigen::Vector3d &n_axis, Eigen::Vector3d &mean, float &curvature, Eigen::VectorXd &c_vec, int num_neighbors, PointOutT &result_point, pcl::Normal &result_normal) const
Fits a point (sample point) given in the local plane coordinates of an input point (query point) to t...
void push_back(const PointT &pt)
Insert a new point in the cloud, at the end of the container.
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Eigen::Vector4f bounding_min_
iterator insert(iterator position, const PointT &pt)
Insert a new point in the cloud, given an iterator.
void computeMLSPointNormal(int index, const std::vector< int > &nn_indices, std::vector< float > &nn_sqr_dists, PointCloudOut &projected_points, NormalCloud &projected_points_normals, PointIndices &corresponding_input_indices, MLSResult &mls_result) const
Smooth a given point and its neighborghood using Moving Least Squares.
unsigned int computeCovarianceMatrix(const pcl::PointCloud< PointT > &cloud, const Eigen::Matrix< Scalar, 4, 1 > ¢roid, Eigen::Matrix< Scalar, 3, 3 > &covariance_matrix)
Compute the 3x3 covariance matrix of a given set of points.
pcl::search::Search< PointInT >::Ptr KdTreePtr
void performUpsampling(PointCloudOut &output)
Perform upsampling for the distinct-cloud and voxel-grid methods.
void getMinMax3D(const pcl::PointCloud< PointT > &cloud, PointT &min_pt, PointT &max_pt)
Get the minimum and maximum values on each of the 3 (x-y-z) dimensions in a given pointcloud...
boost::mpl::remove_if< Sequence1, boost::mpl::not_< boost::mpl::contains< Sequence2, boost::mpl::_1 > > >::type type
void process(PointCloudOut &output)
Base method for surface reconstruction for all points given in <setInputCloud (), setIndices ()> ...
PointCloudIn::ConstPtr PointCloudInConstPtr
PointCloud represents the base class in PCL for storing collections of 3D points. ...
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...
A minimalistic implementation of a voxel grid, necessary for the point cloud upsampling.
MLSVoxelGrid(PointCloudInConstPtr &cloud, IndicesPtr &indices, float voxel_size)
void getIndexIn1D(const Eigen::Vector3i &index, uint64_t &index_1d) const
float voxel_size_
Voxel size for the VOXEL_GRID_DILATION upsampling method.
OrganizedNeighbor is a class for optimized nearest neigbhor search in organized point clouds...
void getPosition(const uint64_t &index_1d, Eigen::Vector3f &point) const
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.
void getCellIndex(const Eigen::Vector3f &p, Eigen::Vector3i &index) const
Helper functor structure for concatenate.
void copyMissingFields(const PointInT &point_in, PointOutT &point_out) const
Data structure used to store the results of the MLS fitting.
std::map< uint64_t, Leaf > HashMap
uint32_t height
The point cloud height (if organized as an image-structure).