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