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 #ifndef PCL_KDTREE_KDTREE_IMPL_FLANN_H_
00037 #define PCL_KDTREE_KDTREE_IMPL_FLANN_H_
00038
00039 #include "pcl/kdtree/kdtree_flann.h"
00040
00041 namespace pcl
00042 {
00044 template <typename PointT>
00045 void KdTreeFLANN<PointT>::setInputCloud (const PointCloudConstPtr &cloud, const IndicesConstPtr &indices)
00046 {
00047 cleanup ();
00048
00049 if (!initParameters())
00050 return;
00051
00052 input_ = cloud;
00053 indices_ = indices;
00054
00055 if (input_ == NULL)
00056 return;
00057
00058 m_lock_.lock ();
00059
00060 if (!input_)
00061 {
00062 ROS_ERROR ("[pcl::KdTreeANN::setInputCloud] Invalid input!");
00063 return;
00064 }
00065 if (indices != NULL)
00066 convertCloudToArray (*input_, *indices_);
00067 else
00068 convertCloudToArray (*input_);
00069
00070 initData ();
00071 m_lock_.unlock ();
00072 }
00073
00075 template <typename PointT> int
00076 KdTreeFLANN<PointT>::nearestKSearch (const PointT &point, int k,
00077 std::vector<int> &k_indices,
00078 std::vector<float> &k_distances)
00079 {
00080 if (!point_representation_->isValid (point))
00081 {
00082
00083 return (0);
00084 }
00085
00086 std::vector<float> tmp (dim_);
00087 point_representation_->vectorize ((PointT)point, tmp);
00088
00089 flann::Matrix<int> k_indices_mat (&k_indices[0], 1, k);
00090 flann::Matrix<float> k_distances_mat (&k_distances[0], 1, k);
00091 flann_index_->knnSearch (flann::Matrix<float>(&tmp[0], 1, dim_), k_indices_mat, k_distances_mat, k, flann::SearchParams (-1 ,epsilon_));
00092
00093
00094 for (size_t i = 0; i < k_indices.size (); ++i)
00095 {
00096 int& neighbor_index = k_indices[i];
00097 neighbor_index = index_mapping_[neighbor_index];
00098 }
00099
00100 return (k);
00101 }
00102
00104 template <typename PointT>
00105 int KdTreeFLANN<PointT>::radiusSearch (const PointT &point, double radius, std::vector<int> &k_indices,
00106 std::vector<float> &k_squared_distances, int max_nn) const
00107 {
00108 static flann::Matrix<int> indices_empty;
00109 static flann::Matrix<float> dists_empty;
00110
00111 if (!point_representation_->isValid (point))
00112 {
00113
00114 return 0;
00115 }
00116
00117 std::vector<float> tmp(dim_);
00118 point_representation_->vectorize ((PointT)point, tmp);
00119 radius *= radius;
00120
00121 size_t size;
00122 if (indices_ == NULL)
00123 size = input_->points.size ();
00124 else
00125 size = indices_->size ();
00126
00127 int neighbors_in_radius = 0;
00128 if (k_indices.size () == size && k_squared_distances.size () == size)
00129 {
00130 flann::Matrix<int> k_indices_mat (&k_indices[0], 1, k_indices.size());
00131 flann::Matrix<float> k_distances_mat (&k_squared_distances[0], 1, k_squared_distances.size());
00132 neighbors_in_radius = flann_index_->radiusSearch (flann::Matrix<float>(&tmp[0], 1, dim_),
00133 k_indices_mat, k_distances_mat, radius, flann::SearchParams (-1, epsilon_, sorted_));
00134 }
00135 else
00136 {
00137 neighbors_in_radius = flann_index_->radiusSearch (flann::Matrix<float>(&tmp[0], 1, dim_),
00138 indices_empty, dists_empty, radius, flann::SearchParams (-1, epsilon_, sorted_));
00139 k_indices.resize (neighbors_in_radius);
00140 k_squared_distances.resize (neighbors_in_radius);
00141
00142 if (neighbors_in_radius == 0)
00143 {
00144 return (0);
00145 }
00146 flann::Matrix<int> k_indices_mat (&k_indices[0], 1, k_indices.size());
00147 flann::Matrix<float> k_distances_mat (&k_squared_distances[0], 1, k_squared_distances.size());
00148 flann_index_->radiusSearch (flann::Matrix<float>(&tmp[0], 1, dim_),
00149 k_indices_mat, k_distances_mat, radius, flann::SearchParams (-1, epsilon_, sorted_));
00150
00151 }
00152
00153
00154 for (int i = 0; i < neighbors_in_radius; ++i)
00155 {
00156 int& neighbor_index = k_indices[i];
00157 neighbor_index = index_mapping_[neighbor_index];
00158 }
00159
00160 return (neighbors_in_radius);
00161 }
00162
00164 template <typename PointT>
00165 void KdTreeFLANN<PointT>::cleanup ()
00166 {
00167 delete flann_index_;
00168
00169 m_lock_.lock ();
00170
00171 free (cloud_);
00172 cloud_ = NULL;
00173 index_mapping_.clear();
00174
00175 if (indices_)
00176 indices_.reset ();
00177
00178 m_lock_.unlock ();
00179 }
00180
00182 template <typename PointT>
00183 bool KdTreeFLANN<PointT>::initParameters ()
00184 {
00185 epsilon_ = 0.0;
00186 dim_ = point_representation_->getNumberOfDimensions ();
00187
00188 return (true);
00189 }
00190
00192 template <typename PointT>
00193 void KdTreeFLANN<PointT>::initData ()
00194 {
00195 flann_index_ = new FLANNIndex(flann::Matrix<float>(cloud_, index_mapping_.size(), dim_),
00196 flann::KDTreeSingleIndexParams(15));
00197 flann_index_->buildIndex();
00198 }
00199
00201 template <typename PointT>
00202 void KdTreeFLANN<PointT>::convertCloudToArray (const PointCloud &ros_cloud)
00203 {
00204
00205 if (ros_cloud.points.empty ())
00206 {
00207 cloud_ = NULL;
00208 return;
00209 }
00210
00211 int original_no_of_points = ros_cloud.points.size();
00212
00213 cloud_ = (float*)malloc (original_no_of_points * dim_ * sizeof(float));
00214 float* cloud_ptr = cloud_;
00215 index_mapping_.reserve(original_no_of_points);
00216
00217 for (int cloud_index = 0; cloud_index < original_no_of_points; ++cloud_index)
00218 {
00219 const PointT point = ros_cloud.points[cloud_index];
00220
00221 if (!point_representation_->isValid(point))
00222 continue;
00223
00224 index_mapping_.push_back(cloud_index);
00225
00226 point_representation_->vectorize(point, cloud_ptr);
00227 cloud_ptr += dim_;
00228 }
00229 }
00230
00232 template <typename PointT>
00233 void KdTreeFLANN<PointT>::convertCloudToArray (const PointCloud &ros_cloud, const std::vector<int> &indices)
00234 {
00235
00236 if (ros_cloud.points.empty ())
00237 {
00238 cloud_ = NULL;
00239 return;
00240 }
00241
00242 int original_no_of_points = indices.size();
00243
00244 cloud_ = (float*)malloc (original_no_of_points * dim_ * sizeof (float));
00245 float* cloud_ptr = cloud_;
00246 index_mapping_.reserve(original_no_of_points);
00247
00248 for (int indices_index = 0; indices_index < original_no_of_points; ++indices_index)
00249 {
00250 int cloud_index = indices[indices_index];
00251 const PointT point = ros_cloud.points[cloud_index];
00252
00253 if (!point_representation_->isValid(point))
00254 continue;
00255
00256 index_mapping_.push_back(indices_index);
00257
00258
00259 point_representation_->vectorize(point, cloud_ptr);
00260 cloud_ptr += dim_;
00261 }
00262 }
00263
00264
00265 }
00266
00267 #define PCL_INSTANTIATE_KdTreeFLANN(T) template class pcl::KdTreeFLANN<T>;
00268
00269 #endif //#ifndef _PCL_KDTREE_KDTREE_IMPL_FLANN_H_
00270