Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
transformation_validation_euclidean.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: transformation_validation_euclidean.hpp 3828 2012-01-05 22:51:04Z svn $
37  *
38  */
39 #ifndef PCL_REGISTRATION_TRANSFORMATION_VALIDATION_EUCLIDEAN_IMPL_H_
40 #define PCL_REGISTRATION_TRANSFORMATION_VALIDATION_EUCLIDEAN_IMPL_H_
41 
43 
45 template <typename PointSource, typename PointTarget> double
47  const PointCloudSourceConstPtr &cloud_src,
48  const PointCloudTargetConstPtr &cloud_tgt,
49  const Eigen::Matrix4f &transformation_matrix)
50 {
51  double fitness_score = 0.0;
52 
53  // Transform the input dataset using the final transformation
54  pcl::PointCloud<PointSource> input_transformed;
55  transformPointCloud (*cloud_src, input_transformed, transformation_matrix);
56 
57  // Just in case
58  if (!tree_)
59  tree_.reset (new pcl::KdTreeFLANN<PointTarget>);
60 
61  tree_->setInputCloud (cloud_tgt);
62 
63  std::vector<int> nn_indices (1);
64  std::vector<float> nn_dists (1);
65 
66  // For each point in the source dataset
67  int nr = 0;
68  for (size_t i = 0; i < input_transformed.points.size (); ++i)
69  {
70  // Find its nearest neighbor in the target
71  tree_->nearestKSearch (input_transformed.points[i], 1, nn_indices, nn_dists);
72 
73  // Deal with occlusions (incomplete targets)
74  if (nn_dists[0] > max_range_)
75  continue;
76 
77  // Optimization: use getVector4fMap instead, but make sure that the last coordinate is 0!
78  Eigen::Vector4f p1 (input_transformed.points[i].x,
79  input_transformed.points[i].y,
80  input_transformed.points[i].z, 0);
81  Eigen::Vector4f p2 (cloud_tgt->points[nn_indices[0]].x,
82  cloud_tgt->points[nn_indices[0]].y,
83  cloud_tgt->points[nn_indices[0]].z, 0);
84  // Calculate the fitness score
85  fitness_score += fabs ((p1-p2).squaredNorm ());
86  nr++;
87  }
88 
89  if (nr > 0)
90  return (fitness_score / nr);
91  else
92  return (std::numeric_limits<double>::max ());
93 }
94 
95 #endif /* PCL_REGISTRATION_TRANSFORMATION_VALIDATION_EUCLIDEAN_IMPL_H_ */