00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef PCL_REGISTRATION_CORRESPONDENCE_ESTIMATION_H_
00038 #define PCL_REGISTRATION_CORRESPONDENCE_ESTIMATION_H_
00039
00040 #include <map>
00041 #include <string>
00042 #include <boost/function.hpp>
00043 #include <boost/bind.hpp>
00044
00045 #include <pcl/pcl_base.h>
00046 #include <pcl/kdtree/kdtree.h>
00047 #include <pcl/kdtree/kdtree_flann.h>
00048 #include "pcl/win32_macros.h"
00049
00050 #include <pcl/registration/correspondence_types.h>
00051
00052
00053
00054 namespace pcl
00055 {
00056 namespace registration
00057 {
00062 template <typename PointSource, typename PointTarget>
00063 class CorrespondenceEstimation : public PCLBase<PointSource>
00064 {
00065
00066 class FeatureContainerInterface;
00067
00068 public:
00069 using PCLBase<PointSource>::initCompute;
00070 using PCLBase<PointSource>::deinitCompute;
00071 using PCLBase<PointSource>::input_;
00072 using PCLBase<PointSource>::indices_;
00073
00074 typedef typename pcl::KdTree<PointTarget> KdTree;
00075 typedef typename pcl::KdTree<PointTarget>::Ptr KdTreePtr;
00076
00077 typedef pcl::PointCloud<PointSource> PointCloudSource;
00078 typedef typename PointCloudSource::Ptr PointCloudSourcePtr;
00079 typedef typename PointCloudSource::ConstPtr PointCloudSourceConstPtr;
00080
00081 typedef pcl::PointCloud<PointTarget> PointCloudTarget;
00082 typedef typename PointCloudTarget::Ptr PointCloudTargetPtr;
00083 typedef typename PointCloudTarget::ConstPtr PointCloudTargetConstPtr;
00084
00085 typedef typename KdTree::PointRepresentationConstPtr PointRepresentationConstPtr;
00086
00087 typedef std::map<std::string, boost::shared_ptr<FeatureContainerInterface> > FeaturesMap;
00088
00089
00091 CorrespondenceEstimation () : target_ (),
00092 corr_dist_threshold_ (std::numeric_limits<double>::max()),
00093 point_representation_ ()
00094 {
00095 tree_ = boost::make_shared<pcl::KdTreeFLANN<PointTarget> > ();
00096 }
00097
00101 virtual inline void setInputTarget (const PointCloudTargetConstPtr &cloud);
00102
00104 inline PointCloudTargetConstPtr const getInputTarget () { return (target_ ); }
00105
00110 template <typename FeatureType>
00111 inline void setSourceFeature (const typename pcl::PointCloud<FeatureType>::ConstPtr &source_feature, std::string key);
00112
00116 template <typename FeatureType>
00117 inline typename pcl::PointCloud<FeatureType>::ConstPtr getSourceFeature (std::string key);
00118
00123 template <typename FeatureType>
00124 inline void setTargetFeature (const typename pcl::PointCloud<FeatureType>::ConstPtr &target_feature, std::string key);
00125
00129 template <typename FeatureType>
00130 inline typename pcl::PointCloud<FeatureType>::ConstPtr getTargetFeature (std::string key);
00131
00138 template <typename FeatureType>
00139 inline void setRadiusSearch (const typename pcl::KdTree<FeatureType>::Ptr &tree, float r, std::string key);
00140
00147 template <typename FeatureType>
00148 inline void setKSearch (const typename pcl::KdTree<FeatureType>::Ptr &tree, int k, std::string key);
00149
00155 inline void setMaxCorrespondenceDistance (double distance_threshold) { corr_dist_threshold_ = distance_threshold; }
00156
00160 inline double getMaxCorrespondenceDistance () { return (corr_dist_threshold_); }
00161
00165 inline void
00166 setPointRepresentation (const PointRepresentationConstPtr &point_representation)
00167 {
00168 point_representation_ = point_representation;
00169 }
00170
00177 inline bool
00178 searchForNeighbors (const PointCloudSource &cloud, int index, std::vector<int> &indices, std::vector<float> &distances)
00179 {
00180 int k = tree_->nearestKSearch (cloud, index, 1, indices, distances);
00181 if (k == 0)
00182 return (false);
00183 return (true);
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00252 virtual void determineCorrespondences (std::vector<pcl::registration::Correspondence> &correspondences, float max_distance = std::numeric_limits<float>::max());
00253
00257 virtual void determineReciprocalCorrespondences(std::vector<pcl::registration::Correspondence> &correspondences);
00258
00259
00260 protected:
00262 std::string corr_name_;
00263
00265 KdTreePtr tree_;
00266
00268 PointCloudTargetConstPtr target_;
00269
00273 double corr_dist_threshold_;
00274
00278 inline bool hasValidFeatures ();
00279
00285 inline void findFeatureCorrespondences (int index, std::vector<int> &correspondence_indices);
00286
00288 inline const std::string& getClassName () const { return (corr_name_); }
00289
00290 private:
00291
00293 PointRepresentationConstPtr point_representation_;
00294
00296 FeaturesMap features_map_;
00297
00303 template <typename FeatureType>
00304 class FeatureContainer : public pcl::registration::CorrespondenceEstimation<PointSource, PointTarget>::FeatureContainerInterface
00305 {
00306 public:
00307 typedef typename pcl::PointCloud<FeatureType>::ConstPtr FeatureCloudConstPtr;
00308 typedef typename pcl::KdTree<FeatureType> KdTree;
00309 typedef typename pcl::KdTree<FeatureType>::Ptr KdTreePtr;
00310 typedef boost::function<bool (const pcl::PointCloud<FeatureType> &, int, std::vector<int> &,
00311 std::vector<float> &)> SearchMethod;
00312
00313 FeatureContainer () : k_(0), radius_(0) {}
00314
00315 void setSourceFeature (const FeatureCloudConstPtr &source_features)
00316 {
00317 source_features_ = source_features;
00318 }
00319
00320 FeatureCloudConstPtr getSourceFeature ()
00321 {
00322 return (source_features_);
00323 }
00324
00325 void setTargetFeature (const FeatureCloudConstPtr &target_features)
00326 {
00327 target_features_ = target_features;
00328 if (tree_)
00329 {
00330 tree_->setInputCloud (target_features_);
00331 }
00332 }
00333
00334 FeatureCloudConstPtr getTargetFeature ()
00335 {
00336 return (target_features_);
00337 }
00338
00339 void setRadiusSearch (KdTreePtr tree, float r)
00340 {
00341 tree_ = tree;
00342 radius_ = r;
00343 k_ = 0;
00344 if (target_features_)
00345 {
00346 tree_->setInputCloud (target_features_);
00347 }
00348 }
00349
00350 void setKSearch (KdTreePtr tree, int k)
00351 {
00352 tree_ = tree;
00353 k_ = k;
00354 radius_ = 0.0;
00355 if (target_features_)
00356 {
00357 tree_->setInputCloud (target_features_);
00358 }
00359 }
00360
00361 virtual bool isValid ()
00362 {
00363 if (!source_features_ || !target_features_ || !tree_)
00364 {
00365 return (false);
00366 }
00367 else
00368 {
00369 return (source_features_->points.size () > 0 &&
00370 target_features_->points.size () > 0 &&
00371 (k_ > 0 || radius_ > 0.0));
00372 }
00373 }
00374
00375 virtual void findFeatureCorrespondences (int index, std::vector<int> &correspondence_indices,
00376 std::vector<float> &distances)
00377 {
00378 if (k_ > 0)
00379 {
00380 correspondence_indices.resize (k_);
00381 distances.resize (k_);
00382 tree_->nearestKSearch (*source_features_, index, k_, correspondence_indices, distances);
00383 }
00384 else
00385 {
00386 tree_->radiusSearch (*source_features_, index, radius_, correspondence_indices, distances);
00387 }
00388 }
00389
00390 private:
00391 FeatureCloudConstPtr source_features_, target_features_;
00392 KdTreePtr tree_;
00393 SearchMethod search_method_;
00394 int k_;
00395 double radius_;
00396 };
00397
00398 class FeatureContainerInterface
00399 {
00400 public:
00401 virtual bool isValid () = 0;
00402 virtual void findFeatureCorrespondences (int index, std::vector<int> &correspondence_indices,
00403 std::vector<float> &distances) = 0;
00404 };
00405
00406 };
00407
00408
00409 }
00410 }
00411
00412 #include "pcl/registration/impl/correspondence_estimation.hpp"
00413
00414 #endif