Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
correspondence_estimation_normal_shooting.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-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 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  *
37  */
38 #ifndef PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_NORMAL_SHOOTING_H_
39 #define PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_NORMAL_SHOOTING_H_
40 
42 
44 template <typename PointSource, typename PointTarget, typename NormalT> void
46  pcl::Correspondences &correspondences, float max_distance)
47 {
48  if (!initCompute ())
49  return;
50 
51  if (!target_)
52  {
53  PCL_WARN ("[pcl::%s::compute] No input target dataset was given!\n", getClassName ().c_str ());
54  return;
55  }
56  correspondences.resize (indices_->size ());
57 
58  float min_dist = std::numeric_limits<float>::max ();
59  float dist = 0.0;
60  int min_index = 0;
61  tree_->setInputCloud (target_);
62 
63  std::vector<int> nn_indices (k_);
64  std::vector<float> nn_dists (k_);
65 
67 
68  for (size_t i = 0; i < source_normals_->points.size (); i++)
69  {
70  float *normal = source_normals_->points[i].normal;
71 
72  tree_->nearestKSearch (input_->points[i], k_, nn_indices, nn_dists);
73 
74  // Among the K nearest neighbours find the one with minimum perpendicular distance to the normal
75  min_dist = std::numeric_limits<float>::max ();
76  for (size_t j = 0; j < nn_indices.size (); j++)
77  {
78  int q = nn_indices[j];
79  // computing the distance between a point and a line in 3d.
80  // Reference - http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
81  PointTarget pt;
82  pt.x = target_->points[q].x - input_->points[i].x;
83  pt.y = target_->points[q].y - input_->points[i].y;
84  pt.z = target_->points[q].z - input_->points[i].z;
85  Eigen::Vector3d N (normal[0], normal[1], normal[2]);
86  Eigen::Vector3d V (pt.x, pt.y, pt.z);
87  Eigen::Vector3d C = N.cross (V);
88  dist = C.dot (C);
89  if (dist < min_dist)
90  {
91  min_dist = dist;
92  min_index = q;
93  }
94  }
95  if (min_dist > max_distance)
96  continue;
97  corr.index_query = i;
98  corr.index_match = min_index;
99  corr.distance = min_dist;
100  correspondences[i] = corr;
101  }
102  deinitCompute ();
103 }
104 
105 #endif /* PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_NORMAL_SHOOTING_H_ */