Point Cloud Library (PCL)  1.14.1-dev
convolution_3d.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, 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 the copyright holder(s) 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 #ifndef PCL_FILTERS_CONVOLUTION_3D_IMPL_HPP
41 #define PCL_FILTERS_CONVOLUTION_3D_IMPL_HPP
42 
43 #include <pcl/search/organized.h>
44 #include <pcl/search/kdtree.h>
45 #include <pcl/pcl_config.h>
46 #include <pcl/point_types.h>
47 #include <pcl/common/point_tests.h>
48 
49 #include <cmath>
50 #include <cstdint>
51 #include <limits>
52 #include <vector>
53 
54 ///////////////////////////////////////////////////////////////////////////////////////////////////
55 namespace pcl
56 {
57  namespace filters
58  {
59  template <typename PointT>
61  {
62  void
64  {
65  n.normal_x = n.normal_y = n.normal_z = std::numeric_limits<float>::quiet_NaN ();
66  }
67  };
68 
69  template <typename PointT> class
71  {
72  void
73  makeInfinite (pcl::PointXY& p)
74  {
75  p.x = p.y = std::numeric_limits<float>::quiet_NaN ();
76  }
77  };
78  }
79 }
80 
81 ///////////////////////////////////////////////////////////////////////////////////////////////////
82 template<typename PointInT, typename PointOutT> bool
84 {
85  if (sigma_ == 0)
86  {
87  PCL_ERROR ("Sigma is not set or equal to 0!\n", sigma_);
88  return (false);
89  }
90  sigma_sqr_ = sigma_ * sigma_;
91 
92  if (sigma_coefficient_)
93  {
94  if ((*sigma_coefficient_) > 6 || (*sigma_coefficient_) < 3)
95  {
96  PCL_ERROR ("Sigma coefficient (%f) out of [3..6]!\n", (*sigma_coefficient_));
97  return (false);
98  }
99  else
100  threshold_ = (*sigma_coefficient_) * (*sigma_coefficient_) * sigma_sqr_;
101  }
102 
103  return (true);
104 }
105 
106 ///////////////////////////////////////////////////////////////////////////////////////////////////
107 template<typename PointInT, typename PointOutT> PointOutT
109  const std::vector<float>& distances)
110 {
111  using namespace pcl::common;
112  PointOutT result;
113  float total_weight = 0;
114  std::vector<float>::const_iterator dist_it = distances.begin ();
115 
116  for (Indices::const_iterator idx_it = indices.begin ();
117  idx_it != indices.end ();
118  ++idx_it, ++dist_it)
119  {
120  if (*dist_it <= threshold_ && isFinite ((*input_) [*idx_it]))
121  {
122  float weight = std::exp (-0.5f * (*dist_it) / sigma_sqr_);
123  result += weight * (*input_) [*idx_it];
124  total_weight += weight;
125  }
126  }
127  if (total_weight != 0)
128  result /= total_weight;
129  else
130  makeInfinite (result);
131 
132  return (result);
133 }
134 
135 ///////////////////////////////////////////////////////////////////////////////////////////////////////
136 template<typename PointInT, typename PointOutT> PointOutT
137 pcl::filters::GaussianKernelRGB<PointInT, PointOutT>::operator() (const Indices& indices, const std::vector<float>& distances)
138 {
139  using namespace pcl::common;
140  PointOutT result;
141  float total_weight = 0;
142  float r = 0, g = 0, b = 0;
143  std::vector<float>::const_iterator dist_it = distances.begin ();
144 
145  for (Indices::const_iterator idx_it = indices.begin ();
146  idx_it != indices.end ();
147  ++idx_it, ++dist_it)
148  {
149  if (*dist_it <= threshold_ && isFinite ((*input_) [*idx_it]))
150  {
151  float weight = std::exp (-0.5f * (*dist_it) / sigma_sqr_);
152  result.x += weight * (*input_) [*idx_it].x;
153  result.y += weight * (*input_) [*idx_it].y;
154  result.z += weight * (*input_) [*idx_it].z;
155  r += weight * static_cast<float> ((*input_) [*idx_it].r);
156  g += weight * static_cast<float> ((*input_) [*idx_it].g);
157  b += weight * static_cast<float> ((*input_) [*idx_it].b);
158  total_weight += weight;
159  }
160  }
161  if (total_weight != 0)
162  {
163  total_weight = 1.f/total_weight;
164  r*= total_weight; g*= total_weight; b*= total_weight;
165  result.x*= total_weight; result.y*= total_weight; result.z*= total_weight;
166  result.r = static_cast<std::uint8_t> (r);
167  result.g = static_cast<std::uint8_t> (g);
168  result.b = static_cast<std::uint8_t> (b);
169  }
170  else
171  makeInfinite (result);
172 
173  return (result);
174 }
175 
176 ///////////////////////////////////////////////////////////////////////////////////////////////////
177 template <typename PointInT, typename PointOutT, typename KernelT>
179  : PCLBase <PointInT> ()
180  , surface_ ()
181  , tree_ ()
182  , search_radius_ (0)
183 {}
184 
185 ///////////////////////////////////////////////////////////////////////////////////////////////////
186 template <typename PointInT, typename PointOutT, typename KernelT> bool
188 {
190  {
191  PCL_ERROR ("[pcl::filters::Convlution3D::initCompute] init failed!\n");
192  return (false);
193  }
194  // Initialize the spatial locator
195  if (!tree_)
196  {
197  if (input_->isOrganized ())
198  tree_.reset (new pcl::search::OrganizedNeighbor<PointInT> ());
199  else
200  tree_.reset (new pcl::search::KdTree<PointInT> (false));
201  }
202  // If no search surface has been defined, use the input dataset as the search surface itself
203  if (!surface_)
204  surface_ = input_;
205  // Send the surface dataset to the spatial locator
206  tree_->setInputCloud (surface_);
207  // Do a fast check to see if the search parameters are well defined
208  if (search_radius_ <= 0.0)
209  {
210  PCL_ERROR ("[pcl::filters::Convlution3D::initCompute] search radius (%f) must be > 0\n",
211  search_radius_);
212  return (false);
213  }
214  // Make sure the provided kernel implements the required interface
215  if (dynamic_cast<ConvolvingKernel<PointInT, PointOutT>* > (&kernel_) == 0)
216  {
217  PCL_ERROR ("[pcl::filters::Convlution3D::initCompute] init failed : ");
218  PCL_ERROR ("kernel_ must implement ConvolvingKernel interface\n!");
219  return (false);
220  }
221  kernel_.setInputCloud (surface_);
222  // Initialize convolving kernel
223  if (!kernel_.initCompute ())
224  {
225  PCL_ERROR ("[pcl::filters::Convlution3D::initCompute] kernel initialization failed!\n");
226  return (false);
227  }
228  return (true);
229 }
230 
231 ///////////////////////////////////////////////////////////////////////////////////////////////////
232 template <typename PointInT, typename PointOutT, typename KernelT> void
234 {
235  if (!initCompute ())
236  {
237  PCL_ERROR ("[pcl::filters::Convlution3D::convolve] init failed!\n");
238  return;
239  }
240  output.resize (surface_->size ());
241  output.width = surface_->width;
242  output.height = surface_->height;
243  output.is_dense = surface_->is_dense;
244  Indices nn_indices;
245  std::vector<float> nn_distances;
246 
247 #pragma omp parallel for \
248  default(none) \
249  shared(output) \
250  firstprivate(nn_indices, nn_distances) \
251  num_threads(threads_)
252  for (std::int64_t point_idx = 0; point_idx < static_cast<std::int64_t> (surface_->size ()); ++point_idx)
253  {
254  const PointInT& point_in = surface_->points [point_idx];
255  PointOutT& point_out = output [point_idx];
256  if (isFinite (point_in) &&
257  tree_->radiusSearch (point_in, search_radius_, nn_indices, nn_distances))
258  {
259  point_out = kernel_ (nn_indices, nn_distances);
260  }
261  else
262  {
263  kernel_.makeInfinite (point_out);
264  output.is_dense = false;
265  }
266  }
267 }
268 
269 #endif
PCL base class.
Definition: pcl_base.h:70
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields).
Definition: point_cloud.h:403
void resize(std::size_t count)
Resizes the container to contain count elements.
Definition: point_cloud.h:462
std::uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:398
std::uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:400
bool initCompute()
initialize computation
void convolve(PointCloudOut &output)
Convolve point cloud.
Class ConvolvingKernel base class for all convolving kernels.
static void makeInfinite(PointOutT &p)
Utility function that annihilates a point making it fail the pcl::isFinite test.
virtual PointOutT operator()(const Indices &indices, const std::vector< float > &distances)
Convolve point at the center of this local information.
bool initCompute()
Must call this method before doing any computation.
PointOutT operator()(const Indices &indices, const std::vector< float > &distances)
Convolve point at the center of this local information.
search::KdTree is a wrapper class which inherits the pcl::KdTree class for performing search function...
Definition: kdtree.h:62
OrganizedNeighbor is a class for optimized nearest neighbor search in organized projectable point clo...
Definition: organized.h:65
Defines all the PCL implemented PointT point type structures.
bool isFinite(const PointT &pt)
Tests if the 3D components of a point are all finite param[in] pt point to be tested return true if f...
Definition: point_tests.h:55
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
A point structure representing normal coordinates and the surface curvature estimate.
A 2D point structure representing Euclidean xy coordinates.
A point structure representing Euclidean xyz coordinates, and the RGB color.