Point Cloud Library (PCL)  1.11.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
mls.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009-2011, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id$
37  *
38  */
39 
40 #pragma once
41 
42 #include <functional>
43 #include <map>
44 #include <random>
45 
46 // PCL includes
47 #include <pcl/memory.h>
48 #include <pcl/pcl_base.h>
49 #include <pcl/pcl_macros.h>
50 #include <pcl/search/pcl_search.h>
51 #include <pcl/common/common.h>
52 
53 #include <pcl/surface/boost.h>
54 #include <pcl/surface/eigen.h>
55 #include <pcl/surface/processing.h>
56 
57 namespace pcl
58 {
59 
60  /** \brief Data structure used to store the results of the MLS fitting */
61  struct MLSResult
62  {
64  {
65  NONE, /**< \brief Project to the mls plane. */
66  SIMPLE, /**< \brief Project along the mls plane normal to the polynomial surface. */
67  ORTHOGONAL /**< \brief Project to the closest point on the polynonomial surface. */
68  };
69 
70  /** \brief Data structure used to store the MLS polynomial partial derivatives */
72  {
73  double z; /**< \brief The z component of the polynomial evaluated at z(u, v). */
74  double z_u; /**< \brief The partial derivative dz/du. */
75  double z_v; /**< \brief The partial derivative dz/dv. */
76  double z_uu; /**< \brief The partial derivative d^2z/du^2. */
77  double z_vv; /**< \brief The partial derivative d^2z/dv^2. */
78  double z_uv; /**< \brief The partial derivative d^2z/dudv. */
79  };
80 
81  /** \brief Data structure used to store the MLS projection results */
83  {
84  MLSProjectionResults () : u (0), v (0) {}
85 
86  double u; /**< \brief The u-coordinate of the projected point in local MLS frame. */
87  double v; /**< \brief The u-coordinate of the projected point in local MLS frame. */
88  Eigen::Vector3d point; /**< \brief The projected point. */
89  Eigen::Vector3d normal; /**< \brief The projected point's normal. */
91  };
92 
93  inline
94  MLSResult () : num_neighbors (0), curvature (0.0f), order (0), valid (false) {}
95 
96  inline
97  MLSResult (const Eigen::Vector3d &a_query_point,
98  const Eigen::Vector3d &a_mean,
99  const Eigen::Vector3d &a_plane_normal,
100  const Eigen::Vector3d &a_u,
101  const Eigen::Vector3d &a_v,
102  const Eigen::VectorXd &a_c_vec,
103  const int a_num_neighbors,
104  const float a_curvature,
105  const int a_order);
106 
107  /** \brief Given a point calculate it's 3D location in the MLS frame.
108  * \param[in] pt The point
109  * \param[out] u The u-coordinate of the point in local MLS frame.
110  * \param[out] v The v-coordinate of the point in local MLS frame.
111  * \param[out] w The w-coordinate of the point in local MLS frame.
112  */
113  inline void
114  getMLSCoordinates (const Eigen::Vector3d &pt, double &u, double &v, double &w) const;
115 
116  /** \brief Given a point calculate it's 2D location in the MLS frame.
117  * \param[in] pt The point
118  * \param[out] u The u-coordinate of the point in local MLS frame.
119  * \param[out] v The v-coordinate of the point in local MLS frame.
120  */
121  inline void
122  getMLSCoordinates (const Eigen::Vector3d &pt, double &u, double &v) const;
123 
124  /** \brief Calculate the polynomial
125  * \param[in] u The u-coordinate of the point in local MLS frame.
126  * \param[in] v The v-coordinate of the point in local MLS frame.
127  * \return The polynomial value at the provide uv coordinates.
128  */
129  inline double
130  getPolynomialValue (const double u, const double v) const;
131 
132  /** \brief Calculate the polynomial's first and second partial derivatives.
133  * \param[in] u The u-coordinate of the point in local MLS frame.
134  * \param[in] v The v-coordinate of the point in local MLS frame.
135  * \return The polynomial partial derivatives at the provide uv coordinates.
136  */
137  inline PolynomialPartialDerivative
138  getPolynomialPartialDerivative (const double u, const double v) const;
139 
140  /** \brief Calculate the principle curvatures using the polynomial surface.
141  * \param[in] u The u-coordinate of the point in local MLS frame.
142  * \param[in] v The v-coordinate of the point in local MLS frame.
143  * \return The principle curvature [k1, k2] at the provided ub coordinates.
144  * \note If an error occurs the MLS_MINIMUM_PRINCIPLE_CURVATURE is returned.
145  */
146  inline Eigen::Vector2f
147  calculatePrincipleCurvatures (const double u, const double v) const;
148 
149  /** \brief Project a point orthogonal to the polynomial surface.
150  * \param[in] u The u-coordinate of the point in local MLS frame.
151  * \param[in] v The v-coordinate of the point in local MLS frame.
152  * \param[in] w The w-coordinate of the point in local MLS frame.
153  * \return The MLSProjectionResults for the input data.
154  * \note If the MLSResults does not contain polynomial data it projects the point onto the mls plane.
155  * \note If the optimization diverges it performs a simple projection on to the polynomial surface.
156  * \note This was implemented based on this https://math.stackexchange.com/questions/1497093/shortest-distance-between-point-and-surface
157  */
158  inline MLSProjectionResults
159  projectPointOrthogonalToPolynomialSurface (const double u, const double v, const double w) const;
160 
161  /** \brief Project a point onto the MLS plane.
162  * \param[in] u The u-coordinate of the point in local MLS frame.
163  * \param[in] v The v-coordinate of the point in local MLS frame.
164  * \return The MLSProjectionResults for the input data.
165  */
166  inline MLSProjectionResults
167  projectPointToMLSPlane (const double u, const double v) const;
168 
169  /** \brief Project a point along the MLS plane normal to the polynomial surface.
170  * \param[in] u The u-coordinate of the point in local MLS frame.
171  * \param[in] v The v-coordinate of the point in local MLS frame.
172  * \return The MLSProjectionResults for the input data.
173  * \note If the MLSResults does not contain polynomial data it projects the point onto the mls plane.
174  */
175  inline MLSProjectionResults
176  projectPointSimpleToPolynomialSurface (const double u, const double v) const;
177 
178  /**
179  * \brief Project a point using the specified method.
180  * \param[in] pt The point to be project.
181  * \param[in] method The projection method to be used.
182  * \param[in] required_neighbors The minimum number of neighbors required.
183  * \note If required_neighbors then any number of neighbors is allowed.
184  * \note If required_neighbors is not satisfied it projects to the mls plane.
185  * \return The MLSProjectionResults for the input data.
186  */
187  inline MLSProjectionResults
188  projectPoint (const Eigen::Vector3d &pt, ProjectionMethod method, int required_neighbors = 0) const;
189 
190  /**
191  * \brief Project the query point used to generate the mls surface about using the specified method.
192  * \param[in] method The projection method to be used.
193  * \param[in] required_neighbors The minimum number of neighbors required.
194  * \note If required_neighbors then any number of neighbors is allowed.
195  * \note If required_neighbors is not satisfied it projects to the mls plane.
196  * \return The MLSProjectionResults for the input data.
197  */
198  inline MLSProjectionResults
199  projectQueryPoint (ProjectionMethod method, int required_neighbors = 0) const;
200 
201  /** \brief Smooth a given point and its neighborghood using Moving Least Squares.
202  * \param[in] index the index of the query point in the input cloud
203  * \param[in] nn_indices the set of nearest neighbors indices for pt
204  * \param[in] search_radius the search radius used to find nearest neighbors for pt
205  * \param[in] polynomial_order the order of the polynomial to fit to the nearest neighbors
206  * \param[in] weight_func defines the weight function for the polynomial fit
207  */
208  template <typename PointT> void
210  int index,
211  const std::vector<int> &nn_indices,
212  double search_radius,
213  int polynomial_order = 2,
214  std::function<double(const double)> weight_func = {});
215 
216  Eigen::Vector3d query_point; /**< \brief The query point about which the mls surface was generated */
217  Eigen::Vector3d mean; /**< \brief The mean point of all the neighbors. */
218  Eigen::Vector3d plane_normal; /**< \brief The normal of the local plane of the query point. */
219  Eigen::Vector3d u_axis; /**< \brief The axis corresponding to the u-coordinates of the local plane of the query point. */
220  Eigen::Vector3d v_axis; /**< \brief The axis corresponding to the v-coordinates of the local plane of the query point. */
221  Eigen::VectorXd c_vec; /**< \brief The polynomial coefficients Example: z = c_vec[0] + c_vec[1]*v + c_vec[2]*v^2 + c_vec[3]*u + c_vec[4]*u*v + c_vec[5]*u^2 */
222  int num_neighbors; /**< \brief The number of neighbors used to create the mls surface. */
223  float curvature; /**< \brief The curvature at the query point. */
224  int order; /**< \brief The order of the polynomial. If order > 1 then use polynomial fit */
225  bool valid; /**< \brief If True, the mls results data is valid, otherwise False. */
227  private:
228  /**
229  * \brief The default weight function used when fitting a polynomial surface
230  * \param sq_dist the squared distance from a point to origin of the mls frame
231  * \param sq_mls_radius the squraed mls search radius used
232  * \return The weight for a point at squared distance from the origin of the mls frame
233  */
234  inline
235  double computeMLSWeight (const double sq_dist, const double sq_mls_radius) { return (std::exp (-sq_dist / sq_mls_radius)); }
236 
237  };
238 
239  /** \brief MovingLeastSquares represent an implementation of the MLS (Moving Least Squares) algorithm
240  * for data smoothing and improved normal estimation. It also contains methods for upsampling the
241  * resulting cloud based on the parametric fit.
242  * Reference paper: "Computing and Rendering Point Set Surfaces" by Marc Alexa, Johannes Behr,
243  * Daniel Cohen-Or, Shachar Fleishman, David Levin and Claudio T. Silva
244  * www.sci.utah.edu/~shachar/Publications/crpss.pdf
245  * \note There is a parallelized version of the processing step, using the OpenMP standard.
246  * Compared to the standard version, an overhead is incurred in terms of runtime and memory usage.
247  * The upsampling methods DISTINCT_CLOUD and VOXEL_GRID_DILATION are not parallelized completely,
248  * i.e. parts of the algorithm run on a single thread only.
249  * \author Zoltan Csaba Marton, Radu B. Rusu, Alexandru E. Ichim, Suat Gedikli, Robert Huitl
250  * \ingroup surface
251  */
252  template <typename PointInT, typename PointOutT>
253  class MovingLeastSquares : public CloudSurfaceProcessing<PointInT, PointOutT>
254  {
255  public:
256  typedef shared_ptr<MovingLeastSquares<PointInT, PointOutT> > Ptr;
257  typedef shared_ptr<const MovingLeastSquares<PointInT, PointOutT> > ConstPtr;
258 
264 
266  using KdTreePtr = typename KdTree::Ptr;
269 
273 
277 
278  using SearchMethod = std::function<int (int, double, std::vector<int> &, std::vector<float> &)>;
279 
281  {
282  NONE, /**< \brief No upsampling will be done, only the input points will be projected
283  to their own MLS surfaces. */
284  DISTINCT_CLOUD, /**< \brief Project the points of the distinct cloud to the MLS surface. */
285  SAMPLE_LOCAL_PLANE, /**< \brief The local plane of each input point will be sampled in a circular fashion
286  using the \ref upsampling_radius_ and the \ref upsampling_step_ parameters. */
287  RANDOM_UNIFORM_DENSITY, /**< \brief The local plane of each input point will be sampled using an uniform random
288  distribution such that the density of points is constant throughout the
289  cloud - given by the \ref desired_num_points_in_radius_ parameter. */
290  VOXEL_GRID_DILATION /**< \brief The input cloud will be inserted into a voxel grid with voxels of
291  size \ref voxel_size_; this voxel grid will be dilated \ref dilation_iteration_num_
292  times and the resulting points will be projected to the MLS surface
293  of the closest point in the input cloud; the result is a point cloud
294  with filled holes and a constant point density. */
295  };
296 
297  /** \brief Empty constructor. */
298  MovingLeastSquares () : CloudSurfaceProcessing<PointInT, PointOutT> (),
299  distinct_cloud_ (),
300  tree_ (),
301  order_ (2),
302  search_radius_ (0.0),
303  sqr_gauss_param_ (0.0),
304  compute_normals_ (false),
306  upsampling_radius_ (0.0),
307  upsampling_step_ (0.0),
309  cache_mls_results_ (true),
310  projection_method_ (MLSResult::SIMPLE),
311  threads_ (1),
312  voxel_size_ (1.0),
314  nr_coeff_ (),
315  rng_uniform_distribution_ ()
316  {};
317 
318  /** \brief Empty destructor */
320 
321 
322  /** \brief Set whether the algorithm should also store the normals computed
323  * \note This is optional, but need a proper output cloud type
324  */
325  inline void
326  setComputeNormals (bool compute_normals) { compute_normals_ = compute_normals; }
327 
328  /** \brief Provide a pointer to the search object.
329  * \param[in] tree a pointer to the spatial search object.
330  */
331  inline void
333  {
334  tree_ = tree;
335  // Declare the search locator definition
336  search_method_ = [this] (int index, double radius, std::vector<int>& k_indices, std::vector<float>& k_sqr_distances)
337  {
338  return tree_->radiusSearch (index, radius, k_indices, k_sqr_distances, 0);
339  };
340  }
341 
342  /** \brief Get a pointer to the search method used. */
343  inline KdTreePtr
344  getSearchMethod () const { return (tree_); }
345 
346  /** \brief Set the order of the polynomial to be fit.
347  * \param[in] order the order of the polynomial
348  * \note Setting order > 1 indicates using a polynomial fit.
349  */
350  inline void
351  setPolynomialOrder (int order) { order_ = order; }
352 
353  /** \brief Get the order of the polynomial to be fit. */
354  inline int
355  getPolynomialOrder () const { return (order_); }
356 
357  /** \brief Sets whether the surface and normal are approximated using a polynomial, or only via tangent estimation.
358  * \param[in] polynomial_fit set to true for polynomial fit
359  */
360  PCL_DEPRECATED(1, 12, "use setPolynomialOrder() instead")
361  inline void
362  setPolynomialFit (bool polynomial_fit)
363  {
364  if (polynomial_fit)
365  {
366  if (order_ < 2)
367  {
368  order_ = 2;
369  }
370  }
371  else
372  {
373  order_ = 0;
374  }
375  }
376 
377  /** \brief Get the polynomial_fit value (true if the surface and normal are approximated using a polynomial). */
378  PCL_DEPRECATED(1, 12, "use getPolynomialOrder() instead")
379  inline bool
380  getPolynomialFit () const { return (order_ > 1); }
381 
382  /** \brief Set the sphere radius that is to be used for determining the k-nearest neighbors used for fitting.
383  * \param[in] radius the sphere radius that is to contain all k-nearest neighbors
384  * \note Calling this method resets the squared Gaussian parameter to radius * radius !
385  */
386  inline void
388 
389  /** \brief Get the sphere radius used for determining the k-nearest neighbors. */
390  inline double
391  getSearchRadius () const { return (search_radius_); }
392 
393  /** \brief Set the parameter used for distance based weighting of neighbors (the square of the search radius works
394  * best in general).
395  * \param[in] sqr_gauss_param the squared Gaussian parameter
396  */
397  inline void
398  setSqrGaussParam (double sqr_gauss_param) { sqr_gauss_param_ = sqr_gauss_param; }
399 
400  /** \brief Get the parameter for distance based weighting of neighbors. */
401  inline double
402  getSqrGaussParam () const { return (sqr_gauss_param_); }
403 
404  /** \brief Set the upsampling method to be used
405  * \param method
406  */
407  inline void
409 
410  /** \brief Set the distinct cloud used for the DISTINCT_CLOUD upsampling method. */
411  inline void
412  setDistinctCloud (PointCloudInConstPtr distinct_cloud) { distinct_cloud_ = distinct_cloud; }
413 
414  /** \brief Get the distinct cloud used for the DISTINCT_CLOUD upsampling method. */
415  inline PointCloudInConstPtr
416  getDistinctCloud () const { return (distinct_cloud_); }
417 
418 
419  /** \brief Set the radius of the circle in the local point plane that will be sampled
420  * \note Used only in the case of SAMPLE_LOCAL_PLANE upsampling
421  * \param[in] radius the radius of the circle
422  */
423  inline void
424  setUpsamplingRadius (double radius) { upsampling_radius_ = radius; }
425 
426  /** \brief Get the radius of the circle in the local point plane that will be sampled
427  * \note Used only in the case of SAMPLE_LOCAL_PLANE upsampling
428  */
429  inline double
431 
432  /** \brief Set the step size for the local plane sampling
433  * \note Used only in the case of SAMPLE_LOCAL_PLANE upsampling
434  * \param[in] step_size the step size
435  */
436  inline void
437  setUpsamplingStepSize (double step_size) { upsampling_step_ = step_size; }
438 
439 
440  /** \brief Get the step size for the local plane sampling
441  * \note Used only in the case of SAMPLE_LOCAL_PLANE upsampling
442  */
443  inline double
445 
446  /** \brief Set the parameter that specifies the desired number of points within the search radius
447  * \note Used only in the case of RANDOM_UNIFORM_DENSITY upsampling
448  * \param[in] desired_num_points_in_radius the desired number of points in the output cloud in a sphere of
449  * radius \ref search_radius_ around each point
450  */
451  inline void
452  setPointDensity (int desired_num_points_in_radius) { desired_num_points_in_radius_ = desired_num_points_in_radius; }
453 
454 
455  /** \brief Get the parameter that specifies the desired number of points within the search radius
456  * \note Used only in the case of RANDOM_UNIFORM_DENSITY upsampling
457  */
458  inline int
460 
461  /** \brief Set the voxel size for the voxel grid
462  * \note Used only in the VOXEL_GRID_DILATION upsampling method
463  * \param[in] voxel_size the edge length of a cubic voxel in the voxel grid
464  */
465  inline void
466  setDilationVoxelSize (float voxel_size) { voxel_size_ = voxel_size; }
467 
468 
469  /** \brief Get the voxel size for the voxel grid
470  * \note Used only in the VOXEL_GRID_DILATION upsampling method
471  */
472  inline float
473  getDilationVoxelSize () const { return (voxel_size_); }
474 
475  /** \brief Set the number of dilation steps of the voxel grid
476  * \note Used only in the VOXEL_GRID_DILATION upsampling method
477  * \param[in] iterations the number of dilation iterations
478  */
479  inline void
480  setDilationIterations (int iterations) { dilation_iteration_num_ = iterations; }
481 
482  /** \brief Get the number of dilation steps of the voxel grid
483  * \note Used only in the VOXEL_GRID_DILATION upsampling method
484  */
485  inline int
487 
488  /** \brief Set whether the mls results should be stored for each point in the input cloud
489  * \param[in] cache_mls_results True if the mls results should be stored, otherwise false.
490  * \note The cache_mls_results_ is forced to true when using upsampling method VOXEL_GRID_DILATION or DISTINCT_CLOUD.
491  * \note If memory consumption is a concern set to false when not using upsampling method VOXEL_GRID_DILATION or DISTINCT_CLOUD.
492  */
493  inline void
494  setCacheMLSResults (bool cache_mls_results) { cache_mls_results_ = cache_mls_results; }
495 
496  /** \brief Get the cache_mls_results_ value (True if the mls results should be stored, otherwise false). */
497  inline bool
498  getCacheMLSResults () const { return (cache_mls_results_); }
499 
500  /** \brief Set the method to be used when projection the point on to the MLS surface.
501  * \param method
502  * \note This is only used when polynomial fit is enabled.
503  */
504  inline void
506 
507 
508  /** \brief Get the current projection method being used. */
511 
512  /** \brief Get the MLSResults for input cloud
513  * \note The results are only stored if setCacheMLSResults(true) was called or when using the upsampling method DISTINCT_CLOUD or VOXEL_GRID_DILATION.
514  * \note This vector is align with the input cloud indices, so use getCorrespondingIndices to get the correct results when using output cloud indices.
515  */
516  inline const std::vector<MLSResult>&
517  getMLSResults () const { return (mls_results_); }
518 
519  /** \brief Set the maximum number of threads to use
520  * \param threads the maximum number of hardware threads to use (0 sets the value to 1)
521  */
522  inline void
523  setNumberOfThreads (unsigned int threads = 1)
524  {
525  threads_ = threads;
526  }
527 
528  /** \brief Base method for surface reconstruction for all points given in <setInputCloud (), setIndices ()>
529  * \param[out] output the resultant reconstructed surface model
530  */
531  void
532  process (PointCloudOut &output) override;
533 
534 
535  /** \brief Get the set of indices with each point in output having the
536  * corresponding point in input */
537  inline PointIndicesPtr
539 
540  protected:
541  /** \brief The point cloud that will hold the estimated normals, if set. */
543 
544  /** \brief The distinct point cloud that will be projected to the MLS surface. */
546 
547  /** \brief The search method template for indices. */
549 
550  /** \brief A pointer to the spatial search object. */
552 
553  /** \brief The order of the polynomial to be fit. */
554  int order_;
555 
556  /** \brief The nearest neighbors search radius for each point. */
558 
559  /** \brief Parameter for distance based weighting of neighbors (search_radius_ * search_radius_ works fine) */
561 
562  /** \brief Parameter that specifies whether the normals should be computed for the input cloud or not */
564 
565  /** \brief Parameter that specifies the upsampling method to be used */
567 
568  /** \brief Radius of the circle in the local point plane that will be sampled
569  * \note Used only in the case of SAMPLE_LOCAL_PLANE upsampling
570  */
572 
573  /** \brief Step size for the local plane sampling
574  * \note Used only in the case of SAMPLE_LOCAL_PLANE upsampling
575  */
577 
578  /** \brief Parameter that specifies the desired number of points within the search radius
579  * \note Used only in the case of RANDOM_UNIFORM_DENSITY upsampling
580  */
582 
583  /** \brief True if the mls results for the input cloud should be stored
584  * \note This is forced to true when using upsampling methods VOXEL_GRID_DILATION or DISTINCT_CLOUD.
585  */
587 
588  /** \brief Stores the MLS result for each point in the input cloud
589  * \note Used only in the case of VOXEL_GRID_DILATION or DISTINCT_CLOUD upsampling
590  */
591  std::vector<MLSResult> mls_results_;
592 
593  /** \brief Parameter that specifies the projection method to be used. */
595 
596  /** \brief The maximum number of threads the scheduler should use. */
597  unsigned int threads_;
598 
599 
600  /** \brief A minimalistic implementation of a voxel grid, necessary for the point cloud upsampling
601  * \note Used only in the case of VOXEL_GRID_DILATION upsampling
602  */
604  {
605  public:
606  struct Leaf { Leaf () : valid (true) {} bool valid; };
607 
609  IndicesPtr &indices,
610  float voxel_size);
611 
612  void
613  dilate ();
614 
615  inline void
616  getIndexIn1D (const Eigen::Vector3i &index, std::uint64_t &index_1d) const
617  {
618  index_1d = index[0] * data_size_ * data_size_ +
619  index[1] * data_size_ + index[2];
620  }
621 
622  inline void
623  getIndexIn3D (std::uint64_t index_1d, Eigen::Vector3i& index_3d) const
624  {
625  index_3d[0] = static_cast<Eigen::Vector3i::Scalar> (index_1d / (data_size_ * data_size_));
626  index_1d -= index_3d[0] * data_size_ * data_size_;
627  index_3d[1] = static_cast<Eigen::Vector3i::Scalar> (index_1d / data_size_);
628  index_1d -= index_3d[1] * data_size_;
629  index_3d[2] = static_cast<Eigen::Vector3i::Scalar> (index_1d);
630  }
631 
632  inline void
633  getCellIndex (const Eigen::Vector3f &p, Eigen::Vector3i& index) const
634  {
635  for (int i = 0; i < 3; ++i)
636  index[i] = static_cast<Eigen::Vector3i::Scalar> ((p[i] - bounding_min_ (i)) / voxel_size_);
637  }
638 
639  inline void
640  getPosition (const std::uint64_t &index_1d, Eigen::Vector3f &point) const
641  {
642  Eigen::Vector3i index_3d;
643  getIndexIn3D (index_1d, index_3d);
644  for (int i = 0; i < 3; ++i)
645  point[i] = static_cast<Eigen::Vector3f::Scalar> (index_3d[i]) * voxel_size_ + bounding_min_[i];
646  }
647 
648  typedef std::map<std::uint64_t, Leaf> HashMap;
649  HashMap voxel_grid_;
650  Eigen::Vector4f bounding_min_, bounding_max_;
651  std::uint64_t data_size_;
652  float voxel_size_;
654  };
655 
656 
657  /** \brief Voxel size for the VOXEL_GRID_DILATION upsampling method */
658  float voxel_size_;
659 
660  /** \brief Number of dilation steps for the VOXEL_GRID_DILATION upsampling method */
662 
663  /** \brief Number of coefficients, to be computed from the requested order.*/
665 
666  /** \brief Collects for each point in output the corrseponding point in the input. */
668 
669  /** \brief Search for the closest nearest neighbors of a given point using a radius search
670  * \param[in] index the index of the query point
671  * \param[out] indices the resultant vector of indices representing the k-nearest neighbors
672  * \param[out] sqr_distances the resultant squared distances from the query point to the k-nearest neighbors
673  */
674  inline int
675  searchForNeighbors (int index, std::vector<int> &indices, std::vector<float> &sqr_distances) const
676  {
677  return (search_method_ (index, search_radius_, indices, sqr_distances));
678  }
679 
680  /** \brief Smooth a given point and its neighborghood using Moving Least Squares.
681  * \param[in] index the index of the query point in the input cloud
682  * \param[in] nn_indices the set of nearest neighbors indices for pt
683  * \param[out] projected_points the set of points projected points around the query point
684  * (in the case of upsampling method NONE, only the query point projected to its own fitted surface will be returned,
685  * in the case of the other upsampling methods, multiple points will be returned)
686  * \param[out] projected_points_normals the normals corresponding to the projected points
687  * \param[out] corresponding_input_indices the set of indices with each point in output having the corresponding point in input
688  * \param[out] mls_result stores the MLS result for each point in the input cloud
689  * (used only in the case of VOXEL_GRID_DILATION or DISTINCT_CLOUD upsampling)
690  */
691  void
692  computeMLSPointNormal (int index,
693  const std::vector<int> &nn_indices,
694  PointCloudOut &projected_points,
695  NormalCloud &projected_points_normals,
696  PointIndices &corresponding_input_indices,
697  MLSResult &mls_result) const;
698 
699 
700  /** \brief This is a helper function for add projected points
701  * \param[in] index the index of the query point in the input cloud
702  * \param[in] point the projected point to be added
703  * \param[in] normal the projected point's normal to be added
704  * \param[in] curvature the projected point's curvature
705  * \param[out] projected_points the set of projected points around the query point
706  * \param[out] projected_points_normals the normals corresponding to the projected points
707  * \param[out] corresponding_input_indices the set of indices with each point in output having the corresponding point in input
708  */
709  void
710  addProjectedPointNormal (int index,
711  const Eigen::Vector3d &point,
712  const Eigen::Vector3d &normal,
713  double curvature,
714  PointCloudOut &projected_points,
715  NormalCloud &projected_points_normals,
716  PointIndices &corresponding_input_indices) const;
717 
718 
719  void
720  copyMissingFields (const PointInT &point_in,
721  PointOutT &point_out) const;
722 
723  /** \brief Abstract surface reconstruction method.
724  * \param[out] output the result of the reconstruction
725  */
726  void
727  performProcessing (PointCloudOut &output) override;
728 
729  /** \brief Perform upsampling for the distinct-cloud and voxel-grid methods
730  * \param[out] output the result of the reconstruction
731  */
732  void
734 
735  private:
736  /** \brief Random number generator algorithm. */
737  mutable std::mt19937 rng_;
738 
739  /** \brief Random number generator using an uniform distribution of floats
740  * \note Used only in the case of RANDOM_UNIFORM_DENSITY upsampling
741  */
742  std::unique_ptr<std::uniform_real_distribution<>> rng_uniform_distribution_;
743 
744  /** \brief Abstract class get name method. */
745  std::string
746  getClassName () const { return ("MovingLeastSquares"); }
747  };
748 
749  template <typename PointInT, typename PointOutT>
750  using MovingLeastSquaresOMP PCL_DEPRECATED(1, 12, "use MovingLeastSquares instead, it supports OpenMP now") = MovingLeastSquares<PointInT, PointOutT>;
751 }
752 
753 #ifdef PCL_NO_PRECOMPILE
754 #include <pcl/surface/impl/mls.hpp>
755 #endif
Data structure used to store the MLS polynomial partial derivatives.
Definition: mls.h:71
bool valid
If True, the mls results data is valid, otherwise False.
Definition: mls.h:225
int nr_coeff_
Number of coefficients, to be computed from the requested order.
Definition: mls.h:664
Eigen::Vector3d plane_normal
The normal of the local plane of the query point.
Definition: mls.h:218
PointCloudInConstPtr getDistinctCloud() const
Get the distinct cloud used for the DISTINCT_CLOUD upsampling method.
Definition: mls.h:416
int getPolynomialOrder() const
Get the order of the polynomial to be fit.
Definition: mls.h:355
double getSqrGaussParam() const
Get the parameter for distance based weighting of neighbors.
Definition: mls.h:402
double z_u
The partial derivative dz/du.
Definition: mls.h:74
const std::vector< MLSResult > & getMLSResults() const
Get the MLSResults for input cloud.
Definition: mls.h:517
MLSResult()
Definition: mls.h:94
double getSearchRadius() const
Get the sphere radius used for determining the k-nearest neighbors.
Definition: mls.h:391
typename PointCloudIn::ConstPtr PointCloudInConstPtr
Definition: mls.h:276
NormalCloudPtr normals_
The point cloud that will hold the estimated normals, if set.
Definition: mls.h:542
void setDilationVoxelSize(float voxel_size)
Set the voxel size for the voxel grid.
Definition: mls.h:466
void setSearchRadius(double radius)
Set the sphere radius that is to be used for determining the k-nearest neighbors used for fitting...
Definition: mls.h:387
Eigen::VectorXd c_vec
The polynomial coefficients Example: z = c_vec[0] + c_vec[1]*v + c_vec[2]*v^2 + c_vec[3]*u + c_vec[4]...
Definition: mls.h:221
int dilation_iteration_num_
Number of dilation steps for the VOXEL_GRID_DILATION upsampling method.
Definition: mls.h:661
double u
The u-coordinate of the projected point in local MLS frame.
Definition: mls.h:86
void setUpsamplingMethod(UpsamplingMethod method)
Set the upsampling method to be used.
Definition: mls.h:408
PointIndicesPtr getCorrespondingIndices() const
Get the set of indices with each point in output having the corresponding point in input...
Definition: mls.h:538
MLSProjectionResults projectPointOrthogonalToPolynomialSurface(const double u, const double v, const double w) const
Project a point orthogonal to the polynomial surface.
Definition: mls.hpp:565
PointIndicesPtr corresponding_input_indices_
Collects for each point in output the corrseponding point in the input.
Definition: mls.h:667
PointIndices::Ptr PointIndicesPtr
Definition: pcl_base.h:76
MLSProjectionResults projectPointToMLSPlane(const double u, const double v) const
Project a point onto the MLS plane.
Definition: mls.hpp:630
void setPolynomialOrder(int order)
Set the order of the polynomial to be fit.
Definition: mls.h:351
typename PointCloudOut::ConstPtr PointCloudOutConstPtr
Definition: mls.h:272
MovingLeastSquares()
Empty constructor.
Definition: mls.h:298
The input cloud will be inserted into a voxel grid with voxels of size voxel_size_; this voxel grid w...
Definition: mls.h:290
MovingLeastSquares represent an implementation of the MLS (Moving Least Squares) algorithm for data s...
Definition: mls.h:253
shared_ptr< Indices > IndicesPtr
Definition: pcl_base.h:61
ProjectionMethod
Definition: mls.h:63
double z_vv
The partial derivative d^2z/dv^2.
Definition: mls.h:77
~MovingLeastSquares()
Empty destructor.
Definition: mls.h:319
Eigen::Vector4f bounding_max_
Definition: mls.h:650
float getDilationVoxelSize() const
Get the voxel size for the voxel grid.
Definition: mls.h:473
SearchMethod search_method_
The search method template for indices.
Definition: mls.h:548
double z_uv
The partial derivative d^2z/dudv.
Definition: mls.h:78
int getPointDensity() const
Get the parameter that specifies the desired number of points within the search radius.
Definition: mls.h:459
void setCacheMLSResults(bool cache_mls_results)
Set whether the mls results should be stored for each point in the input cloud.
Definition: mls.h:494
std::function< int(int, double, std::vector< int > &, std::vector< float > &)> SearchMethod
Definition: mls.h:278
CloudSurfaceProcessing represents the base class for algorithms that takes a point cloud as input and...
Definition: processing.h:56
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: memory.h:63
double getUpsamplingStepSize() const
Get the step size for the local plane sampling.
Definition: mls.h:444
void setUpsamplingRadius(double radius)
Set the radius of the circle in the local point plane that will be sampled.
Definition: mls.h:424
void process(PointCloudOut &output) override
Base method for surface reconstruction for all points given in ...
Definition: mls.hpp:57
Project to the closest point on the polynonomial surface.
Definition: mls.h:67
bool getPolynomialFit() const
Get the polynomial_fit value (true if the surface and normal are approximated using a polynomial)...
Definition: mls.h:380
int order
The order of the polynomial.
Definition: mls.h:224
std::map< std::uint64_t, Leaf > HashMap
Definition: mls.h:648
Eigen::Vector4f bounding_min_
Definition: mls.h:650
bool cache_mls_results_
True if the mls results for the input cloud should be stored.
Definition: mls.h:586
double z_uu
The partial derivative d^2z/du^2.
Definition: mls.h:76
std::vector< MLSResult > mls_results_
Stores the MLS result for each point in the input cloud.
Definition: mls.h:591
double getPolynomialValue(const double u, const double v) const
Calculate the polynomial.
Definition: mls.hpp:466
Data structure used to store the MLS projection results.
Definition: mls.h:82
Eigen::Vector3d point
The projected point.
Definition: mls.h:88
double v
The u-coordinate of the projected point in local MLS frame.
Definition: mls.h:87
double z_v
The partial derivative dz/dv.
Definition: mls.h:75
void setProjectionMethod(MLSResult::ProjectionMethod method)
Set the method to be used when projection the point on to the MLS surface.
Definition: mls.h:505
shared_ptr< PointCloud< pcl::Normal > > Ptr
Definition: point_cloud.h:428
Eigen::Vector3d normal
The projected point's normal.
Definition: mls.h:89
double upsampling_radius_
Radius of the circle in the local point plane that will be sampled.
Definition: mls.h:571
void getIndexIn1D(const Eigen::Vector3i &index, std::uint64_t &index_1d) const
Definition: mls.h:616
The local plane of each input point will be sampled in a circular fashion using the upsampling_radius...
Definition: mls.h:285
typename PointCloudOut::Ptr PointCloudOutPtr
Definition: mls.h:271
shared_ptr< const MovingLeastSquares< PointInT, PointOutT > > ConstPtr
Definition: mls.h:257
pcl::PointCloud< pcl::Normal > NormalCloud
Definition: mls.h:267
MLSProjectionResults projectPoint(const Eigen::Vector3d &pt, ProjectionMethod method, int required_neighbors=0) const
Project a point using the specified method.
Definition: mls.hpp:665
KdTreePtr getSearchMethod() const
Get a pointer to the search method used.
Definition: mls.h:344
PolynomialPartialDerivative getPolynomialPartialDerivative(const double u, const double v) const
Calculate the polynomial's first and second partial derivatives.
Definition: mls.hpp:488
Data structure used to store the results of the MLS fitting.
Definition: mls.h:61
PointCloudInConstPtr distinct_cloud_
The distinct point cloud that will be projected to the MLS surface.
Definition: mls.h:545
PCL base class.
Definition: pcl_base.h:69
int num_neighbors
The number of neighbors used to create the mls surface.
Definition: mls.h:222
void performUpsampling(PointCloudOut &output)
Perform upsampling for the distinct-cloud and voxel-grid methods.
Definition: mls.hpp:366
typename PointCloudIn::Ptr PointCloudInPtr
Definition: mls.h:275
void setDistinctCloud(PointCloudInConstPtr distinct_cloud)
Set the distinct cloud used for the DISTINCT_CLOUD upsampling method.
Definition: mls.h:412
double search_radius_
The nearest neighbors search radius for each point.
Definition: mls.h:557
Eigen::Vector3d u_axis
The axis corresponding to the u-coordinates of the local plane of the query point.
Definition: mls.h:219
Project the points of the distinct cloud to the MLS surface.
Definition: mls.h:284
void setUpsamplingStepSize(double step_size)
Set the step size for the local plane sampling.
Definition: mls.h:437
shared_ptr< MovingLeastSquares< PointInT, PointOutT > > Ptr
Definition: mls.h:256
void setPolynomialFit(bool polynomial_fit)
Sets whether the surface and normal are approximated using a polynomial, or only via tangent estimati...
Definition: mls.h:362
Eigen::Vector3d mean
The mean point of all the neighbors.
Definition: mls.h:217
double upsampling_step_
Step size for the local plane sampling.
Definition: mls.h:576
Eigen::Vector3d v_axis
The axis corresponding to the v-coordinates of the local plane of the query point.
Definition: mls.h:220
unsigned int threads_
The maximum number of threads the scheduler should use.
Definition: mls.h:597
Project to the mls plane.
Definition: mls.h:65
void setSqrGaussParam(double sqr_gauss_param)
Set the parameter used for distance based weighting of neighbors (the square of the search radius wor...
Definition: mls.h:398
No upsampling will be done, only the input points will be projected to their own MLS surfaces...
Definition: mls.h:282
Eigen::Vector3d query_point
The query point about which the mls surface was generated.
Definition: mls.h:216
PointCloud represents the base class in PCL for storing collections of 3D points. ...
KdTreePtr tree_
A pointer to the spatial search object.
Definition: mls.h:551
shared_ptr< const PointCloud< PointOutT > > ConstPtr
Definition: point_cloud.h:429
int searchForNeighbors(int index, std::vector< int > &indices, std::vector< float > &sqr_distances) const
Search for the closest nearest neighbors of a given point using a radius search.
Definition: mls.h:675
void performProcessing(PointCloudOut &output) override
Abstract surface reconstruction method.
Definition: mls.hpp:280
int getDilationIterations() const
Get the number of dilation steps of the voxel grid.
Definition: mls.h:486
A minimalistic implementation of a voxel grid, necessary for the point cloud upsampling.
Definition: mls.h:603
MLSVoxelGrid(PointCloudInConstPtr &cloud, IndicesPtr &indices, float voxel_size)
Definition: mls.hpp:834
int order_
The order of the polynomial to be fit.
Definition: mls.h:554
void setSearchMethod(const KdTreePtr &tree)
Provide a pointer to the search object.
Definition: mls.h:332
bool getCacheMLSResults() const
Get the cache_mls_results_ value (True if the mls results should be stored, otherwise false)...
Definition: mls.h:498
float voxel_size_
Voxel size for the VOXEL_GRID_DILATION upsampling method.
Definition: mls.h:658
bool compute_normals_
Parameter that specifies whether the normals should be computed for the input cloud or not...
Definition: mls.h:563
void getMLSCoordinates(const Eigen::Vector3d &pt, double &u, double &v, double &w) const
Given a point calculate it's 3D location in the MLS frame.
Definition: mls.hpp:449
int desired_num_points_in_radius_
Parameter that specifies the desired number of points within the search radius.
Definition: mls.h:581
void computeMLSSurface(const pcl::PointCloud< PointT > &cloud, int index, const std::vector< int > &nn_indices, double search_radius, int polynomial_order=2, std::function< double(const double)> weight_func={})
Smooth a given point and its neighborghood using Moving Least Squares.
Definition: mls.hpp:718
void setDilationIterations(int iterations)
Set the number of dilation steps of the voxel grid.
Definition: mls.h:480
float curvature
The curvature at the query point.
Definition: mls.h:223
shared_ptr< pcl::search::Search< PointInT > > Ptr
Definition: search.h:81
MLSProjectionResults projectPointSimpleToPolynomialSurface(const double u, const double v) const
Project a point along the MLS plane normal to the polynomial surface.
Definition: mls.hpp:642
MLSProjectionResults projectQueryPoint(ProjectionMethod method, int required_neighbors=0) const
Project the query point used to generate the mls surface about using the specified method...
Definition: mls.hpp:687
MLSResult::ProjectionMethod projection_method_
Parameter that specifies the projection method to be used.
Definition: mls.h:594
void setPointDensity(int desired_num_points_in_radius)
Set the parameter that specifies the desired number of points within the search radius.
Definition: mls.h:452
void setComputeNormals(bool compute_normals)
Set whether the algorithm should also store the normals computed.
Definition: mls.h:326
UpsamplingMethod upsample_method_
Parameter that specifies the upsampling method to be used.
Definition: mls.h:566
NormalCloud::Ptr NormalCloudPtr
Definition: mls.h:268
double z
The z component of the polynomial evaluated at z(u, v).
Definition: mls.h:73
void getCellIndex(const Eigen::Vector3f &p, Eigen::Vector3i &index) const
Definition: mls.h:633
MLSResult::ProjectionMethod getProjectionMethod() const
Get the current projection method being used.
Definition: mls.h:510
void copyMissingFields(const PointInT &point_in, PointOutT &point_out) const
Definition: mls.hpp:889
The local plane of each input point will be sampled using an uniform random distribution such that th...
Definition: mls.h:287
void addProjectedPointNormal(int index, const Eigen::Vector3d &point, const Eigen::Vector3d &normal, double curvature, PointCloudOut &projected_points, NormalCloud &projected_points_normals, PointIndices &corresponding_input_indices) const
This is a helper function for add projected points.
Definition: mls.hpp:248
void getPosition(const std::uint64_t &index_1d, Eigen::Vector3f &point) const
Definition: mls.h:640
typename KdTree::Ptr KdTreePtr
Definition: mls.h:266
pcl::PointCloud< PointOutT > PointCloudOut
Definition: mls.h:270
void computeMLSPointNormal(int index, const std::vector< int > &nn_indices, 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.
Definition: mls.hpp:170
Eigen::Vector2f calculatePrincipleCurvatures(const double u, const double v) const
Calculate the principle curvatures using the polynomial surface.
Definition: mls.hpp:533
double getUpsamplingRadius() const
Get the radius of the circle in the local point plane that will be sampled.
Definition: mls.h:430
void getIndexIn3D(std::uint64_t index_1d, Eigen::Vector3i &index_3d) const
Definition: mls.h:623
double sqr_gauss_param_
Parameter for distance based weighting of neighbors (search_radius_ * search_radius_ works fine) ...
Definition: mls.h:560
void setNumberOfThreads(unsigned int threads=1)
Set the maximum number of threads to use.
Definition: mls.h:523
Project along the mls plane normal to the polynomial surface.
Definition: mls.h:66
PointIndices::Ptr PointIndicesPtr
Definition: PointIndices.h:26