Point Cloud Library (PCL)
1.7.0
|
00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Point Cloud Library (PCL) - www.pointclouds.org 00005 * Copyright (c) 2012-, Open Perception, 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 the copyright holder(s) 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 */ 00037 00038 #ifndef PCL_SEARCH_SEARCH_IMPL_HPP_ 00039 #define PCL_SEARCH_SEARCH_IMPL_HPP_ 00040 00041 #include <pcl/search/search.h> 00042 00043 /////////////////////////////////////////////////////////////////////////////////////////// 00044 template <typename PointT> 00045 pcl::search::Search<PointT>::Search (const std::string& name, bool sorted) 00046 : input_ () 00047 , indices_ () 00048 , sorted_results_ (sorted) 00049 , name_ (name) 00050 { 00051 } 00052 00053 /////////////////////////////////////////////////////////////////////////////////////////// 00054 template <typename PointT> const std::string& 00055 pcl::search::Search<PointT>::getName () const 00056 { 00057 return (name_); 00058 } 00059 00060 /////////////////////////////////////////////////////////////////////////////////////////// 00061 template <typename PointT> void 00062 pcl::search::Search<PointT>::setSortedResults (bool sorted) 00063 { 00064 sorted_results_ = sorted; 00065 } 00066 00067 /////////////////////////////////////////////////////////////////////////////////////////// 00068 template <typename PointT> bool 00069 pcl::search::Search<PointT>::getSortedResults () 00070 { 00071 return (sorted_results_); 00072 } 00073 00074 /////////////////////////////////////////////////////////////////////////////////////////// 00075 template <typename PointT> void 00076 pcl::search::Search<PointT>::setInputCloud ( 00077 const PointCloudConstPtr& cloud, const IndicesConstPtr &indices) 00078 { 00079 input_ = cloud; 00080 indices_ = indices; 00081 } 00082 00083 00084 /////////////////////////////////////////////////////////////////////////////////////////// 00085 template <typename PointT> int 00086 pcl::search::Search<PointT>::nearestKSearch ( 00087 const PointCloud &cloud, int index, int k, 00088 std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) const 00089 { 00090 assert (index >= 0 && index < static_cast<int> (cloud.points.size ()) && "Out-of-bounds error in nearestKSearch!"); 00091 return (nearestKSearch (cloud.points[index], k, k_indices, k_sqr_distances)); 00092 } 00093 00094 /////////////////////////////////////////////////////////////////////////////////////////// 00095 template <typename PointT> int 00096 pcl::search::Search<PointT>::nearestKSearch ( 00097 int index, int k, 00098 std::vector<int> &k_indices, 00099 std::vector<float> &k_sqr_distances) const 00100 { 00101 if (indices_ == NULL) 00102 { 00103 assert (index >= 0 && index < static_cast<int> (input_->points.size ()) && "Out-of-bounds error in nearestKSearch!"); 00104 return (nearestKSearch (input_->points[index], k, k_indices, k_sqr_distances)); 00105 } 00106 else 00107 { 00108 assert (index >= 0 && index < static_cast<int> (indices_->size ()) && "Out-of-bounds error in nearestKSearch!"); 00109 if (index >= static_cast<int> (indices_->size ()) || index < 0) 00110 return (0); 00111 return (nearestKSearch (input_->points[(*indices_)[index]], k, k_indices, k_sqr_distances)); 00112 } 00113 } 00114 00115 /////////////////////////////////////////////////////////////////////////////////////////// 00116 template <typename PointT> void 00117 pcl::search::Search<PointT>::nearestKSearch ( 00118 const PointCloud& cloud, const std::vector<int>& indices, 00119 int k, std::vector< std::vector<int> >& k_indices, 00120 std::vector< std::vector<float> >& k_sqr_distances) const 00121 { 00122 if (indices.empty ()) 00123 { 00124 k_indices.resize (cloud.size ()); 00125 k_sqr_distances.resize (cloud.size ()); 00126 for (size_t i = 0; i < cloud.size (); i++) 00127 nearestKSearch (cloud, static_cast<int> (i), k, k_indices[i], k_sqr_distances[i]); 00128 } 00129 else 00130 { 00131 k_indices.resize (indices.size ()); 00132 k_sqr_distances.resize (indices.size ()); 00133 for (size_t i = 0; i < indices.size (); i++) 00134 nearestKSearch (cloud, indices[i], k, k_indices[i], k_sqr_distances[i]); 00135 } 00136 } 00137 00138 /////////////////////////////////////////////////////////////////////////////////////////// 00139 template <typename PointT> int 00140 pcl::search::Search<PointT>::radiusSearch ( 00141 const PointCloud &cloud, int index, double radius, 00142 std::vector<int> &k_indices, std::vector<float> &k_sqr_distances, 00143 unsigned int max_nn) const 00144 { 00145 assert (index >= 0 && index < static_cast<int> (cloud.points.size ()) && "Out-of-bounds error in radiusSearch!"); 00146 return (radiusSearch(cloud.points[index], radius, k_indices, k_sqr_distances, max_nn)); 00147 } 00148 00149 /////////////////////////////////////////////////////////////////////////////////////////// 00150 template <typename PointT> int 00151 pcl::search::Search<PointT>::radiusSearch ( 00152 int index, double radius, std::vector<int> &k_indices, 00153 std::vector<float> &k_sqr_distances, unsigned int max_nn ) const 00154 { 00155 if (indices_ == NULL) 00156 { 00157 assert (index >= 0 && index < static_cast<int> (input_->points.size ()) && "Out-of-bounds error in radiusSearch!"); 00158 return (radiusSearch (input_->points[index], radius, k_indices, k_sqr_distances, max_nn)); 00159 } 00160 else 00161 { 00162 assert (index >= 0 && index < static_cast<int> (indices_->size ()) && "Out-of-bounds error in radiusSearch!"); 00163 return (radiusSearch (input_->points[(*indices_)[index]], radius, k_indices, k_sqr_distances, max_nn)); 00164 } 00165 } 00166 00167 /////////////////////////////////////////////////////////////////////////////////////////// 00168 template <typename PointT> void 00169 pcl::search::Search<PointT>::radiusSearch ( 00170 const PointCloud& cloud, 00171 const std::vector<int>& indices, 00172 double radius, 00173 std::vector< std::vector<int> >& k_indices, 00174 std::vector< std::vector<float> > &k_sqr_distances, 00175 unsigned int max_nn) const 00176 { 00177 if (indices.empty ()) 00178 { 00179 k_indices.resize (cloud.size ()); 00180 k_sqr_distances.resize (cloud.size ()); 00181 for (size_t i = 0; i < cloud.size (); i++) 00182 radiusSearch (cloud, static_cast<int> (i), radius,k_indices[i], k_sqr_distances[i], max_nn); 00183 } 00184 else 00185 { 00186 k_indices.resize (indices.size ()); 00187 k_sqr_distances.resize (indices.size ()); 00188 for (size_t i = 0; i < indices.size (); i++) 00189 radiusSearch (cloud,indices[i],radius,k_indices[i],k_sqr_distances[i], max_nn); 00190 } 00191 } 00192 00193 /////////////////////////////////////////////////////////////////////////////////////////// 00194 template <typename PointT> void 00195 pcl::search::Search<PointT>::sortResults ( 00196 std::vector<int>& indices, std::vector<float>& distances) const 00197 { 00198 std::vector<int> order (indices.size ()); 00199 for (size_t idx = 0; idx < order.size (); ++idx) 00200 order [idx] = static_cast<int> (idx); 00201 00202 Compare compare (distances); 00203 sort (order.begin (), order.end (), compare); 00204 00205 std::vector<int> sorted (indices.size ()); 00206 for (size_t idx = 0; idx < order.size (); ++idx) 00207 sorted [idx] = indices[order [idx]]; 00208 00209 indices = sorted; 00210 00211 // sort the according distances. 00212 sort (distances.begin (), distances.end ()); 00213 } 00214 00215 #define PCL_INSTANTIATE_Search(T) template class PCL_EXPORTS pcl::search::Search<T>; 00216 00217 #endif //#ifndef _PCL_SEARCH_SEARCH_IMPL_HPP_ 00218 00219