|
Point Cloud Library (PCL)
1.5.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: search.h 4502 2012-02-17 00:44:05Z gedikli $ 00037 * 00038 */ 00039 00040 #ifndef PCL_SEARCH_SEARCH_H_ 00041 #define PCL_SEARCH_SEARCH_H_ 00042 00043 #include <pcl/point_cloud.h> 00044 #include <pcl/common/io.h> 00045 00046 namespace pcl 00047 { 00048 namespace search 00049 { 00072 template<typename PointT> 00073 class Search 00074 { 00075 public: 00076 typedef pcl::PointCloud<PointT> PointCloud; 00077 typedef typename PointCloud::Ptr PointCloudPtr; 00078 typedef typename PointCloud::ConstPtr PointCloudConstPtr; 00079 00080 typedef boost::shared_ptr<pcl::search::Search<PointT> > Ptr; 00081 typedef boost::shared_ptr<const pcl::search::Search<PointT> > ConstPtr; 00082 00083 typedef boost::shared_ptr<std::vector<int> > IndicesPtr; 00084 typedef boost::shared_ptr<const std::vector<int> > IndicesConstPtr; 00085 00087 Search (const std::string& name = "", bool sorted = false) 00088 : sorted_results_ (sorted) 00089 , name_ (name) 00090 { 00091 } 00092 00094 virtual 00095 ~Search () 00096 { 00097 } 00098 00101 virtual const std::string& getName () const 00102 { 00103 return name_; 00104 } 00109 virtual void setSortedResults (bool sorted) 00110 { 00111 sorted_results_ = sorted; 00112 } 00113 00118 virtual void 00119 setInputCloud (const PointCloudConstPtr& cloud, const IndicesConstPtr &indices = IndicesConstPtr ()) 00120 { 00121 input_ = cloud; 00122 indices_ = indices; 00123 } 00124 00126 virtual PointCloudConstPtr 00127 getInputCloud () 00128 { 00129 return (input_); 00130 } 00131 00133 virtual IndicesConstPtr const 00134 getIndices () 00135 { 00136 return (indices_); 00137 } 00138 00147 virtual int 00148 nearestKSearch (const PointT &point, int k, std::vector<int> &k_indices, 00149 std::vector<float> &k_sqr_distances) const = 0; 00150 00160 template <typename PointTDiff> inline int 00161 nearestKSearchT (const PointTDiff &point, int k, 00162 std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) const 00163 { 00164 PointT p; 00165 // Copy all the data fields from the input cloud to the output one 00166 typedef typename pcl::traits::fieldList<PointT>::type FieldListInT; 00167 typedef typename pcl::traits::fieldList<PointTDiff>::type FieldListOutT; 00168 typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList; 00169 pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (point, p)); 00170 return (nearestKSearch (p, k, k_indices, k_sqr_distances)); 00171 } 00172 00189 virtual int 00190 nearestKSearch (const PointCloud &cloud, int index, int k, 00191 std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) const 00192 { 00193 assert (index >= 0 && index < (int)cloud.points.size () && "Out-of-bounds error in nearestKSearch!"); 00194 return (nearestKSearch (cloud.points[index], k, k_indices, k_sqr_distances)); 00195 } 00196 00214 virtual int 00215 nearestKSearch (int index, int k, 00216 std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) const 00217 { 00218 if (indices_ == NULL) 00219 { 00220 assert (index >= 0 && index < (int)input_->points.size () && "Out-of-bounds error in nearestKSearch!"); 00221 return (nearestKSearch (input_->points[index], k, k_indices, k_sqr_distances)); 00222 } 00223 else 00224 { 00225 assert (index >= 0 && index < (int)indices_->size () && "Out-of-bounds error in nearestKSearch!"); 00226 if (index >= (int)indices_->size() || index < 0) 00227 return (0); 00228 return (nearestKSearch (input_->points[(*indices_)[index]], k, k_indices, k_sqr_distances)); 00229 } 00230 } 00231 00239 virtual void 00240 nearestKSearch (const PointCloud& cloud, const std::vector<int>& indices, int k, std::vector< std::vector<int> >& k_indices, 00241 std::vector< std::vector<float> >& k_sqr_distances) const 00242 { 00243 if (indices.empty ()) 00244 { 00245 k_indices.resize (cloud.size ()); 00246 k_sqr_distances.resize (cloud.size ()); 00247 for (size_t i = 0; i < cloud.size (); i++) 00248 nearestKSearch (cloud,(int) i,k,k_indices[i],k_sqr_distances[i]); 00249 } 00250 else 00251 { 00252 k_indices.resize (indices.size ()); 00253 k_sqr_distances.resize (indices.size ()); 00254 for (size_t i = 0; i < indices.size (); i++) 00255 nearestKSearch (cloud,indices[i],k,k_indices[i],k_sqr_distances[i]); 00256 } 00257 } 00258 00267 template <typename PointTDiff> void 00268 nearestKSearchT (const pcl::PointCloud<PointTDiff> &cloud, const std::vector<int>& indices, int k, std::vector< std::vector<int> > &k_indices, 00269 std::vector< std::vector<float> > &k_sqr_distances) const 00270 { 00271 // Copy all the data fields from the input cloud to the output one 00272 typedef typename pcl::traits::fieldList<PointT>::type FieldListInT; 00273 typedef typename pcl::traits::fieldList<PointTDiff>::type FieldListOutT; 00274 typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList; 00275 00276 pcl::PointCloud<PointT> pc; 00277 if (indices.empty ()) 00278 { 00279 pc.resize (cloud.size()); 00280 for (size_t i = 0; i < cloud.size(); i++) 00281 { 00282 pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> ( 00283 cloud[i], pc[i])); 00284 } 00285 nearestKSearch (pc,std::vector<int>(),k,k_indices,k_sqr_distances); 00286 } 00287 else 00288 { 00289 pc.resize (indices.size()); 00290 for (size_t i = 0; i < indices.size(); i++) 00291 { 00292 pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> ( 00293 cloud[indices[i]], pc[i])); 00294 } 00295 nearestKSearch (pc,std::vector<int>(),k,k_indices,k_sqr_distances); 00296 } 00297 } 00298 00309 virtual int 00310 radiusSearch (const PointT& point, double radius, std::vector<int>& k_indices, 00311 std::vector<float>& k_sqr_distances, unsigned int max_nn = 0) const = 0; 00312 00323 template <typename PointTDiff> inline int 00324 radiusSearchT (const PointTDiff &point, double radius, std::vector<int> &k_indices, 00325 std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const 00326 { 00327 PointT p; 00328 // Copy all the data fields from the input cloud to the output one 00329 typedef typename pcl::traits::fieldList<PointT>::type FieldListInT; 00330 typedef typename pcl::traits::fieldList<PointTDiff>::type FieldListOutT; 00331 typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList; 00332 pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (point, p)); 00333 return (radiusSearch (p, radius, k_indices, k_sqr_distances, max_nn)); 00334 } 00335 00353 virtual int 00354 radiusSearch (const PointCloud &cloud, int index, double radius, 00355 std::vector<int> &k_indices, std::vector<float> &k_sqr_distances, 00356 unsigned int max_nn = 0) const 00357 { 00358 assert (index >= 0 && index < (int)cloud.points.size () && "Out-of-bounds error in radiusSearch!"); 00359 return (radiusSearch(cloud.points[index], radius, k_indices, k_sqr_distances, max_nn)); 00360 } 00361 00381 virtual int 00382 radiusSearch (int index, double radius, std::vector<int> &k_indices, 00383 std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const 00384 { 00385 if (indices_ == NULL) 00386 { 00387 assert (index >= 0 && index < (int)input_->points.size () && "Out-of-bounds error in radiusSearch!"); 00388 return (radiusSearch (input_->points[index], radius, k_indices, k_sqr_distances, max_nn)); 00389 } 00390 else 00391 { 00392 assert (index >= 0 && index < (int)indices_->size () && "Out-of-bounds error in radiusSearch!"); 00393 return (radiusSearch (input_->points[(*indices_)[index]], radius, k_indices, k_sqr_distances, max_nn)); 00394 } 00395 } 00396 00407 virtual void 00408 radiusSearch (const PointCloud& cloud, 00409 const std::vector<int>& indices, 00410 double radius, 00411 std::vector< std::vector<int> >& k_indices, 00412 std::vector< std::vector<float> > &k_sqr_distances, 00413 unsigned int max_nn = 0) const 00414 { 00415 if (indices.empty ()) 00416 { 00417 k_indices.resize (cloud.size ()); 00418 k_sqr_distances.resize (cloud.size ()); 00419 for (size_t i = 0; i < cloud.size (); i++) 00420 radiusSearch (cloud,(int) i,radius,k_indices[i],k_sqr_distances[i], max_nn); 00421 } 00422 else 00423 { 00424 k_indices.resize (indices.size ()); 00425 k_sqr_distances.resize (indices.size ()); 00426 for (size_t i = 0; i < indices.size (); i++) 00427 radiusSearch (cloud,indices[i],radius,k_indices[i],k_sqr_distances[i], max_nn); 00428 } 00429 } 00430 00431 00443 template <typename PointTDiff> void 00444 radiusSearchT (const pcl::PointCloud<PointTDiff> &cloud, 00445 const std::vector<int>& indices, 00446 double radius, 00447 std::vector< std::vector<int> > &k_indices, 00448 std::vector< std::vector<float> > &k_sqr_distances, 00449 unsigned int max_nn = 0) const 00450 { 00451 // Copy all the data fields from the input cloud to the output one 00452 typedef typename pcl::traits::fieldList<PointT>::type FieldListInT; 00453 typedef typename pcl::traits::fieldList<PointTDiff>::type FieldListOutT; 00454 typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList; 00455 00456 pcl::PointCloud<PointT> pc; 00457 if (indices.empty ()) 00458 { 00459 pc.resize (cloud.size ()); 00460 for (size_t i = 0; i < cloud.size (); ++i) 00461 pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (cloud[i], pc[i])); 00462 radiusSearch (pc, std::vector<int> (), radius, k_indices, k_sqr_distances, max_nn); 00463 } 00464 else 00465 { 00466 pc.resize (indices.size ()); 00467 for (size_t i = 0; i < indices.size (); ++i) 00468 pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (cloud[indices[i]], pc[i])); 00469 radiusSearch (pc, std::vector<int>(), radius, k_indices, k_sqr_distances, max_nn); 00470 } 00471 } 00472 00473 protected: 00474 void sortResults (std::vector<int>& indices, std::vector<float>& distances) const; 00475 PointCloudConstPtr input_; 00476 IndicesConstPtr indices_; 00477 bool sorted_results_; 00478 std::string name_; 00479 00480 private: 00481 struct Compare 00482 { 00483 Compare (const std::vector<float>& distances) 00484 : distances_ (distances) 00485 { 00486 } 00487 00488 bool operator () (int first, int second) const 00489 { 00490 return distances_ [first] < distances_[second]; 00491 } 00492 00493 const std::vector<float>& distances_; 00494 }; 00495 }; // class Search 00496 00497 // implementation 00498 template<typename PointT> void 00499 Search<PointT>::sortResults (std::vector<int>& indices, std::vector<float>& distances) const 00500 { 00501 std::vector<int> order (indices.size ()); 00502 for (size_t idx = 0; idx < order.size (); ++idx) 00503 order [idx] = idx; 00504 00505 Compare compare (distances); 00506 sort (order.begin (), order.end (), compare); 00507 00508 std::vector<int> sorted (indices.size ()); 00509 for (size_t idx = 0; idx < order.size (); ++idx) 00510 sorted [idx] = indices[order [idx]]; 00511 00512 indices = sorted; 00513 00514 // sort the according distances. 00515 sort (distances.begin (), distances.end ()); 00516 } 00517 } // namespace search 00518 } // namespace pcl 00519 00520 #endif //#ifndef _PCL_SEARCH_GENERIC_SEARCH_H_
1.8.0