Point Cloud Library (PCL)
1.3.1
|
00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Point Cloud Library (PCL) - www.pointclouds.org 00005 * Copyright (c) 2010-2011, Willow Garage, Inc 00006 * 00007 * All rights reserved. 00008 * 00009 * Redistribution and use in source and binary forms, with or without 00010 * modification, are permitted provided that the following conditions 00011 * are met: 00012 * 00013 * * Redistributions of source code must retain the above copyright 00014 * notice, this list of conditions and the following disclaimer. 00015 * * Redistributions in binary form must reproduce the above 00016 * copyright notice, this list of conditions and the following 00017 * disclaimer in the documentation and/or other materials provided 00018 * with the distribution. 00019 * * Neither the name of Willow Garage, Inc. nor the names of its 00020 * contributors may be used to endorse or promote products derived 00021 * from this software without specific prior written permission. 00022 * 00023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00024 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00025 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00026 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00027 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00028 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00029 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00030 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00031 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00032 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00033 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00034 * POSSIBILITY OF SUCH DAMAGE. 00035 * 00036 * $Id: registration.hpp 2532 2011-09-20 20:39:18Z bouffa $ 00037 * 00038 */ 00039 00041 template <typename PointSource, typename PointTarget> inline void 00042 pcl::Registration<PointSource, PointTarget>::setInputTarget (const PointCloudTargetConstPtr &cloud) 00043 { 00044 if (cloud->points.empty ()) 00045 { 00046 PCL_ERROR ("[pcl::%s::setInputTarget] Invalid or empty point cloud dataset given!\n", getClassName ().c_str ()); 00047 return; 00048 } 00049 PointCloudTarget target = *cloud; 00050 // Set all the point.data[3] values to 1 to aid the rigid transformation 00051 for (size_t i = 0; i < target.points.size (); ++i) 00052 target.points[i].data[3] = 1.0; 00053 00054 //target_ = cloud; 00055 target_ = target.makeShared (); 00056 tree_->setInputCloud (target_); 00057 } 00058 00060 template <typename PointSource, typename PointTarget> inline double 00061 pcl::Registration<PointSource, PointTarget>::getFitnessScore (const std::vector<float> &distances_a, 00062 const std::vector<float> &distances_b) 00063 { 00064 unsigned int nr_elem = std::min (distances_a.size (), distances_b.size ()); 00065 Eigen::VectorXf map_a = Eigen::VectorXf::MapAligned (&distances_a[0], nr_elem); 00066 Eigen::VectorXf map_b = Eigen::VectorXf::MapAligned (&distances_b[0], nr_elem); 00067 return ((map_a - map_b).sum () / nr_elem); 00068 } 00069 00071 template <typename PointSource, typename PointTarget> inline double 00072 pcl::Registration<PointSource, PointTarget>::getFitnessScore (double max_range) 00073 { 00074 double fitness_score = 0.0; 00075 00076 // Transform the input dataset using the final transformation 00077 PointCloudSource input_transformed; 00078 transformPointCloud (*input_, input_transformed, final_transformation_); 00079 00080 std::vector<int> nn_indices (1); 00081 std::vector<float> nn_dists (1); 00082 00083 // For each point in the source dataset 00084 int nr = 0; 00085 for (size_t i = 0; i < input_transformed.points.size (); ++i) 00086 { 00087 Eigen::Vector4f p1 = Eigen::Vector4f (input_transformed.points[i].x, 00088 input_transformed.points[i].y, 00089 input_transformed.points[i].z, 0); 00090 // Find its nearest neighbor in the target 00091 tree_->nearestKSearch (input_transformed.points[i], 1, nn_indices, nn_dists); 00092 00093 // Deal with occlusions (incomplete targets) 00094 if (nn_dists[0] > max_range) 00095 continue; 00096 00097 Eigen::Vector4f p2 = Eigen::Vector4f (target_->points[nn_indices[0]].x, 00098 target_->points[nn_indices[0]].y, 00099 target_->points[nn_indices[0]].z, 0); 00100 // Calculate the fitness score 00101 fitness_score += fabs ((p1-p2).squaredNorm ()); 00102 nr++; 00103 } 00104 00105 if (nr > 0) 00106 return (fitness_score / nr); 00107 else 00108 return (std::numeric_limits<double>::max ()); 00109 } 00110 00112 template <typename PointSource, typename PointTarget> inline void 00113 pcl::Registration<PointSource, PointTarget>::align (PointCloudSource &output) 00114 { 00115 if (!initCompute ()) return; 00116 00117 if (!target_) 00118 { 00119 PCL_WARN ("[pcl::%s::compute] No input target dataset was given!\n", getClassName ().c_str ()); 00120 return; 00121 } 00122 00123 // Resize the output dataset 00124 if (output.points.size () != indices_->size ()) 00125 output.points.resize (indices_->size ()); 00126 // Copy the header 00127 output.header = input_->header; 00128 // Check if the output will be computed for all points or only a subset 00129 if (indices_->size () != input_->points.size ()) 00130 { 00131 output.width = (int) indices_->size (); 00132 output.height = 1; 00133 } 00134 else 00135 { 00136 output.width = input_->width; 00137 output.height = input_->height; 00138 } 00139 output.is_dense = input_->is_dense; 00140 00141 // Copy the point data to output 00142 for (size_t i = 0; i < indices_->size (); ++i) 00143 output.points[i] = input_->points[(*indices_)[i]]; 00144 00145 // Set the internal point representation of choice 00146 if (point_representation_) 00147 tree_->setPointRepresentation (point_representation_); 00148 00149 // Perform the actual transformation computation 00150 converged_ = false; 00151 final_transformation_ = transformation_ = previous_transformation_ = Eigen::Matrix4f::Identity (); 00152 00153 // Right before we estimate the transformation, we set all the point.data[3] values to 1 to aid the rigid 00154 // transformation 00155 for (size_t i = 0; i < indices_->size (); ++i) 00156 output.points[i].data[3] = 1.0; 00157 00158 computeTransformation (output); 00159 00160 deinitCompute (); 00161 } 00162 00164 template <typename PointSource, typename PointTarget> inline void 00165 pcl::Registration<PointSource, PointTarget>::align (PointCloudSource &output, const Eigen::Matrix4f& guess) 00166 { 00167 if (!initCompute ()) return; 00168 00169 if (!target_) 00170 { 00171 PCL_WARN ("[pcl::%s::compute] No input target dataset was given!\n", getClassName ().c_str ()); 00172 return; 00173 } 00174 00175 // Resize the output dataset 00176 if (output.points.size () != indices_->size ()) 00177 output.points.resize (indices_->size ()); 00178 // Copy the header 00179 output.header = input_->header; 00180 // Check if the output will be computed for all points or only a subset 00181 if (indices_->size () != input_->points.size ()) 00182 { 00183 output.width = indices_->size (); 00184 output.height = 1; 00185 } 00186 else 00187 { 00188 output.width = input_->width; 00189 output.height = input_->height; 00190 } 00191 output.is_dense = input_->is_dense; 00192 00193 // Copy the point data to output 00194 for (size_t i = 0; i < indices_->size (); ++i) 00195 output.points[i] = input_->points[(*indices_)[i]]; 00196 00197 // Set the internal point representation of choice 00198 if (point_representation_) 00199 tree_->setPointRepresentation (point_representation_); 00200 00201 // Perform the actual transformation computation 00202 converged_ = false; 00203 final_transformation_ = transformation_ = previous_transformation_ = Eigen::Matrix4f::Identity (); 00204 00205 // Right before we estimate the transformation, we set all the point.data[3] values to 1 to aid the rigid 00206 // transformation 00207 for (size_t i = 0; i < indices_->size (); ++i) 00208 output.points[i].data[3] = 1.0; 00209 00210 computeTransformation (output, guess); 00211 00212 deinitCompute (); 00213 } 00214