Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mls_omp.hpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-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: mls_omp.hpp 5835 2012-06-04 05:27:21Z holzers $
37  *
38  */
39 
40 #ifndef PCL_SURFACE_IMPL_MLS_OMP_H_
41 #define PCL_SURFACE_IMPL_MLS_OMP_H_
42 
43 #include <cstddef>
44 #include <pcl/surface/mls_omp.h>
45 
47 template <typename PointInT, typename PointOutT> void
49 {
50  typedef std::size_t size_t;
51  // Compute the number of coefficients
52  nr_coeff_ = (order_ + 1) * (order_ + 2) / 2;
53 
54 #pragma omp parallel for schedule (dynamic, threads_)
55  // For all points
56  for (int cp = 0; cp < static_cast<int> (indices_->size ()); ++cp)
57  {
58  // Allocate enough space to hold the results of nearest neighbor searches
59  // \note resize is irrelevant for a radiusSearch ().
60  std::vector<int> nn_indices;
61  std::vector<float> nn_sqr_dists;
62 
63  // Get the initial estimates of point positions and their neighborhoods
64  if (!searchForNeighbors (cp, nn_indices, nn_sqr_dists))
65  continue;
66 
67 
68  // Check the number of nearest neighbors for normal estimation (and later
69  // for polynomial fit as well)
70  if (nn_indices.size () < 3)
71  continue;
72 
73 
74  PointCloudOut projected_points;
75  NormalCloud projected_points_normals;
76 
77  // Get a plane approximating the local surface's tangent and project point onto it
78  this->computeMLSPointNormal (cp, *input_, nn_indices, nn_sqr_dists, projected_points, projected_points_normals);
79 
80 #pragma omp critical
81  {
82  // Append projected points to output
83  output.insert (output.end (), projected_points.begin (), projected_points.end ());
84  if (compute_normals_)
85  normals_->insert (normals_->end (), projected_points_normals.begin (), projected_points_normals.end ());
86  }
87  }
88 
89 
90  // For the voxel grid upsampling method, generate the voxel grid and dilate it
91  // Then, project the newly obtained points to the MLS surface
92  if (upsample_method_ == MovingLeastSquares<PointInT, PointOutT>::VOXEL_GRID_DILATION)
93  {
94  MLSVoxelGrid voxel_grid (input_, indices_, voxel_size_);
95 
96  for (int iteration = 0; iteration < dilation_iteration_num_; ++iteration)
97  voxel_grid.dilate ();
98 
99 #if /*defined(_WIN32) ||*/ ((__GNUC__ > 4) && (__GNUC_MINOR__ > 2))
100 #pragma omp parallel for schedule (dynamic, threads_)
101 #endif
102  for (typename MLSVoxelGrid::HashMap::iterator h_it = voxel_grid.voxel_grid_.begin (); h_it != voxel_grid.voxel_grid_.end (); ++h_it)
103  {
104  typename MLSVoxelGrid::HashMap::value_type voxel = *h_it;
105 
106  // Get 3D position of point
107  Eigen::Vector3f pos;
108  voxel_grid.getPosition (voxel.first, pos);
109 
110  PointInT p;
111  p.x = pos[0];
112  p.y = pos[1];
113  p.z = pos[2];
114 
115  std::vector<int> nn_indices;
116  std::vector<float> nn_dists;
117  tree_->nearestKSearch (p, 1, nn_indices, nn_dists);
118  int input_index = nn_indices.front ();
119 
120  // If the closest point did not have a valid MLS fitting result
121  // OR if it is too far away from the sampled point
122  if (mls_results_[input_index].valid == false)
123  continue;
124 
125  Eigen::Vector3f add_point = p.getVector3fMap (),
126  input_point = input_->points[input_index].getVector3fMap ();
127 
128  Eigen::Vector3d aux = mls_results_[input_index].u;
129  Eigen::Vector3f u = aux.cast<float> ();
130  aux = mls_results_[input_index].v;
131  Eigen::Vector3f v = aux.cast<float> ();
132 
133  float u_disp = (add_point - input_point).dot (u),
134  v_disp = (add_point - input_point).dot (v);
135 
136  PointOutT result_point;
137  pcl::Normal result_normal;
138  this->projectPointToMLSSurface (u_disp, v_disp,
139  mls_results_[input_index].u, mls_results_[input_index].v,
140  mls_results_[input_index].plane_normal,
141  mls_results_[input_index].curvature,
142  input_point,
143  mls_results_[input_index].c_vec,
144  mls_results_[input_index].num_neighbors,
145  result_point, result_normal);
146 
147  float d_before = (pos - input_point).norm (),
148  d_after = (result_point.getVector3fMap () - input_point). norm();
149  if (d_after > d_before)
150  continue;
151 
152 #pragma critical
153  {
154  output.push_back (result_point);
155  if (compute_normals_)
156  normals_->push_back (result_normal);
157  }
158  }
159  }
160 }
161 
162 #define PCL_INSTANTIATE_MovingLeastSquaresOMP(T,OutT) template class PCL_EXPORTS pcl::MovingLeastSquaresOMP<T,OutT>;
163 
164 #endif // PCL_SURFACE_IMPL_MLS_OMP_H_
165