Point Cloud Library (PCL)  1.14.0-dev
normal_refinement.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_FILTERS_IMPL_NORMAL_REFINEMENT_H_
42 #define PCL_FILTERS_IMPL_NORMAL_REFINEMENT_H_
43 
44 #include <pcl/filters/normal_refinement.h>
45 
46 ///////////////////////////////////////////////////////////////////////////////////////////
47 template <typename NormalT> void
49 {
50  // Check input
51  if (input_->empty ())
52  {
53  PCL_ERROR ("[pcl::%s::applyFilter] No source was input!\n",
54  getClassName ().c_str ());
55  }
56 
57  // Copy to output
58  output = *input_;
59 
60  // Check that correspondences are non-empty
61  if (k_indices_.empty () || k_sqr_distances_.empty ())
62  {
63  PCL_ERROR ("[pcl::%s::applyFilter] No point correspondences given! Returning original input.\n",
64  getClassName ().c_str ());
65  return;
66  }
67 
68  // Check that correspondences are OK
69  const unsigned int size = k_indices_.size ();
70  if (k_sqr_distances_.size () != size || input_->size () != size)
71  {
72  PCL_ERROR ("[pcl::%s::applyFilter] Inconsistency between size of correspondence indices/distances or input! Returning original input.\n",
73  getClassName ().c_str ());
74  return;
75  }
76 
77  // Run refinement while monitoring convergence
78  for (unsigned int i = 0; i < max_iterations_; ++i)
79  {
80  // Output of the current iteration
81  PointCloud tmp = output;
82 
83  // Mean change in direction, measured by dot products
84  float ddot = 0.0f;
85 
86  // Loop over all points in current output and write refined normal to tmp
87  int num_valids = 0;
88  for(unsigned int j = 0; j < size; ++j)
89  {
90  // Point to write to
91  NormalT& tmpj = tmp[j];
92 
93  // Refine
94  if (refineNormal (output, j, k_indices_[j], k_sqr_distances_[j], tmpj))
95  {
96  // Accumulate using similarity in direction between previous iteration and current
97  const NormalT& outputj = output[j];
98  ddot += tmpj.normal_x * outputj.normal_x + tmpj.normal_y * outputj.normal_y + tmpj.normal_z * outputj.normal_z;
99  ++num_valids;
100  }
101  }
102 
103  // Take mean of similarities
104  ddot /= static_cast<float> (num_valids);
105 
106  // Negate to since we want an error measure to approach zero
107  ddot = 1.0f - ddot;
108 
109  // Update output
110  output = tmp;
111 
112  // Break if converged
113  if (ddot < convergence_threshold_)
114  {
115  PCL_DEBUG("[pcl::%s::applyFilter] Converged after %i iterations with mean error of %f.\n",
116  getClassName ().c_str (), i+1, ddot);
117  break;
118  }
119  }
120 }
121 
122 #endif
void applyFilter(PointCloud &output) override
Filter a Point Cloud.
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
bool refineNormal(const PointCloud< NormalT > &cloud, int index, const Indices &k_indices, const std::vector< float > &k_sqr_distances, NormalT &point)
Refine an indexed point based on its neighbors, this function only writes to the normal_* fields.
A point structure representing normal coordinates and the surface curvature estimate.