Point Cloud Library (PCL)  1.14.1-dev
normal_3d_omp.hpp
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  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 
41 #ifndef PCL_FEATURES_IMPL_NORMAL_3D_OMP_H_
42 #define PCL_FEATURES_IMPL_NORMAL_3D_OMP_H_
43 
44 #include <pcl/features/normal_3d_omp.h>
45 
46 ///////////////////////////////////////////////////////////////////////////////////////////
47 template <typename PointInT, typename PointOutT> void
49 {
50 #ifdef _OPENMP
51  if (nr_threads == 0)
52  threads_ = omp_get_num_procs();
53  else
54  threads_ = nr_threads;
55  PCL_DEBUG ("[pcl::NormalEstimationOMP::setNumberOfThreads] Setting number of threads to %u.\n", threads_);
56 #else
57  threads_ = 1;
58  if (nr_threads != 1)
59  PCL_WARN ("[pcl::NormalEstimationOMP::setNumberOfThreads] Parallelization is requested, but OpenMP is not available! Continuing without parallelization.\n");
60 #endif // _OPENMP
61 }
62 
63 ///////////////////////////////////////////////////////////////////////////////////////////
64 template <typename PointInT, typename PointOutT> void
66 {
67  // Allocate enough space to hold the results
68  // \note This resize is irrelevant for a radiusSearch ().
69  pcl::Indices nn_indices (k_);
70  std::vector<float> nn_dists (k_);
71 
72  output.is_dense = true;
73  // Save a few cycles by not checking every point for NaN/Inf values if the cloud is set to dense
74  if (input_->is_dense)
75  {
76 #pragma omp parallel for \
77  default(none) \
78  shared(output) \
79  firstprivate(nn_indices, nn_dists) \
80  num_threads(threads_) \
81  schedule(dynamic, chunk_size_)
82  // Iterating over the entire index vector
83  for (std::ptrdiff_t idx = 0; idx < static_cast<std::ptrdiff_t> (indices_->size ()); ++idx)
84  {
85  Eigen::Vector4f n;
86  if (this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0 ||
87  !pcl::computePointNormal (*surface_, nn_indices, n, output[idx].curvature))
88  {
89  output[idx].normal[0] = output[idx].normal[1] = output[idx].normal[2] = output[idx].curvature = std::numeric_limits<float>::quiet_NaN ();
90 
91  output.is_dense = false;
92  continue;
93  }
94 
95  output[idx].normal_x = n[0];
96  output[idx].normal_y = n[1];
97  output[idx].normal_z = n[2];
98 
99  flipNormalTowardsViewpoint ((*input_)[(*indices_)[idx]], vpx_, vpy_, vpz_,
100  output[idx].normal[0], output[idx].normal[1], output[idx].normal[2]);
101 
102  }
103  }
104  else
105  {
106 #pragma omp parallel for \
107  default(none) \
108  shared(output) \
109  firstprivate(nn_indices, nn_dists) \
110  num_threads(threads_) \
111  schedule(dynamic, chunk_size_)
112  // Iterating over the entire index vector
113  for (std::ptrdiff_t idx = 0; idx < static_cast<std::ptrdiff_t> (indices_->size ()); ++idx)
114  {
115  Eigen::Vector4f n;
116  if (!isFinite ((*input_)[(*indices_)[idx]]) ||
117  this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0 ||
118  !pcl::computePointNormal (*surface_, nn_indices, n, output[idx].curvature))
119  {
120  output[idx].normal[0] = output[idx].normal[1] = output[idx].normal[2] = output[idx].curvature = std::numeric_limits<float>::quiet_NaN ();
121 
122  output.is_dense = false;
123  continue;
124  }
125 
126  output[idx].normal_x = n[0];
127  output[idx].normal_y = n[1];
128  output[idx].normal_z = n[2];
129 
130  flipNormalTowardsViewpoint ((*input_)[(*indices_)[idx]], vpx_, vpy_, vpz_,
131  output[idx].normal[0], output[idx].normal[1], output[idx].normal[2]);
132 
133  }
134  }
135 }
136 
137 #define PCL_INSTANTIATE_NormalEstimationOMP(T,NT) template class PCL_EXPORTS pcl::NormalEstimationOMP<T,NT>;
138 
139 #endif // PCL_FEATURES_IMPL_NORMAL_3D_OMP_H_
140 
NormalEstimationOMP estimates local surface properties at each 3D point, such as surface normals and ...
Definition: normal_3d_omp.h:54
void setNumberOfThreads(unsigned int nr_threads=0)
Initialize the scheduler and set the number of threads to use.
void flipNormalTowardsViewpoint(const PointT &point, float vp_x, float vp_y, float vp_z, Eigen::Matrix< Scalar, 4, 1 > &normal)
Flip (in place) the estimated normal of a point towards a given viewpoint.
Definition: normal_3d.h:122
bool computePointNormal(const pcl::PointCloud< PointT > &cloud, Eigen::Vector4f &plane_parameters, float &curvature)
Compute the Least-Squares plane fit for a given set of points, and return the estimated plane paramet...
Definition: normal_3d.h:61
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133