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