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
00033 #include "door_handle_detector/kdtree/kdtree_flann.h"
00034
00035 namespace cloud_kdtree
00036 {
00038
00044 void
00045 KdTreeFLANN::nearestKSearch (const geometry_msgs::Point32 &p_q, int k, std::vector<int> &k_indices, std::vector<float> &k_distances)
00046 {
00047 k_indices.resize (k);
00048 k_distances.resize (k);
00049
00050 float* p = (float*)malloc (3 * sizeof (float));
00051 p[0] = p_q.x; p[1] = p_q.y; p[2] = p_q.z;
00052
00053
00054 m_lock_.lock ();
00055
00056
00057 flann_find_nearest_neighbors_index (index_id_, p, 1, &k_indices[0], &k_distances[0], k, flann_param_.checks, &flann_param_);
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00072
00077
00078 for (int i = 0 ; i < 10; i++)
00079 std::cerr << k_indices[i] << " ";
00080 std::cerr << std::endl;
00081
00082
00083
00084 m_lock_.unlock ();
00085
00086 free (p);
00087 return;
00088 }
00089
00091
00098 void
00099 KdTreeFLANN::nearestKSearch (const sensor_msgs::PointCloud &points, int index, int k, std::vector<int> &k_indices, std::vector<float> &k_distances)
00100 {
00101 if (index >= (int)points.points.size ())
00102 return;
00103
00104 k_indices.resize (k);
00105 k_distances.resize (k);
00106
00107 float* p = (float*)malloc (3 * sizeof (float));
00108 p[0] = points.points.at (index).x; p[1] = points.points.at (index).y; p[2] = points.points.at (index).z;
00109
00110 m_lock_.lock ();
00111 flann_find_nearest_neighbors_index (index_id_, p, 1, &k_indices[0], &k_distances[0], k, flann_param_.checks, &flann_param_);
00112 m_lock_.unlock ();
00113
00114 free (p);
00115 return;
00116 }
00117
00119
00126 bool
00127 KdTreeFLANN::radiusSearch (const geometry_msgs::Point32 &p_q, double radius, std::vector<int> &k_indices, std::vector<float> &k_distances,
00128 int max_nn)
00129 {
00130 float* p = (float*)malloc (3 * sizeof (float));
00131 p[0] = p_q.x; p[1] = p_q.y; p[2] = p_q.z;
00132 radius *= radius;
00133
00134 int neighbors_in_radius_ = flann_param_.checks;
00135
00136
00137
00138
00139 if (neighbors_in_radius_ > max_nn) neighbors_in_radius_ = max_nn;
00140 k_indices.resize (neighbors_in_radius_);
00141 k_distances.resize (neighbors_in_radius_);
00142
00143 m_lock_.lock ();
00144 int neighbors_found = flann_radius_search (index_id_, p, &k_indices[0], &k_distances[0], neighbors_in_radius_, radius, flann_param_.checks, &flann_param_);
00145 m_lock_.unlock ();
00146 free (p);
00147
00148 if (neighbors_found == 0) {
00149 return (false);
00150 }
00151
00152 k_indices.resize(neighbors_found);
00153 k_distances.resize(neighbors_found);
00154
00155 return (true);
00156 }
00157
00159
00167 bool
00168 KdTreeFLANN::radiusSearch (const sensor_msgs::PointCloud &points, int index, double radius, std::vector<int> &k_indices, std::vector<float> &k_distances,
00169 int max_nn)
00170 {
00171 float* p = (float*)malloc (3 * sizeof (float));
00172 p[0] = points.points.at (index).x; p[1] = points.points.at (index).y; p[2] = points.points.at (index).z;
00173 radius *= radius;
00174
00175 int neighbors_in_radius_ = flann_param_.checks;
00176
00177
00178
00179
00180 if (neighbors_in_radius_ > max_nn) neighbors_in_radius_ = max_nn;
00181 k_indices.resize (neighbors_in_radius_);
00182 k_distances.resize (neighbors_in_radius_);
00183
00184 m_lock_.lock ();
00185 int neighbors_found = flann_radius_search (index_id_, p, &k_indices[0], &k_distances[0], neighbors_in_radius_,
00186 radius, flann_param_.checks, &flann_param_);
00187 m_lock_.unlock ();
00188 free (p);
00189
00190 if (neighbors_found == 0) {
00191 return (false);
00192 }
00193
00194 k_indices.resize(neighbors_found);
00195 k_distances.resize(neighbors_found);
00196
00197 return (true);
00198 }
00199
00201
00205 int
00206 KdTreeFLANN::convertCloudToArray (const sensor_msgs::PointCloud &ros_cloud)
00207 {
00208
00209 if (ros_cloud.points.size () == 0)
00210 {
00211 m_lock_.lock ();
00212 points_ = NULL;
00213 m_lock_.unlock ();
00214 return (0);
00215 }
00216
00217 m_lock_.lock ();
00218 points_ = (float*)malloc (ros_cloud.points.size () * 3 * sizeof (float));
00219
00220 for (unsigned int cp = 0; cp < ros_cloud.points.size (); cp++)
00221 {
00222 points_[cp * 3 + 0] = ros_cloud.points[cp].x;
00223 points_[cp * 3 + 1] = ros_cloud.points[cp].y;
00224 points_[cp * 3 + 2] = ros_cloud.points[cp].z;
00225 }
00226 m_lock_.unlock ();
00227
00228 return (ros_cloud.points.size ());
00229 }
00230
00232
00240 int
00241 KdTreeFLANN::convertCloudToArray (const sensor_msgs::PointCloud &ros_cloud, const std::vector<int> &indices)
00242 {
00243
00244 if (ros_cloud.points.size () == 0 || indices.size () > ros_cloud.points.size ())
00245 {
00246 m_lock_.lock ();
00247 points_ = NULL;
00248 m_lock_.unlock ();
00249 return (0);
00250 }
00251
00252 m_lock_.lock ();
00253 points_ = (float*)malloc (indices.size () * 3 * sizeof (float));
00254
00255 for (unsigned int cp = 0; cp < indices.size (); cp++)
00256 {
00257 points_[cp * 3 + 0] = ros_cloud.points[indices.at (cp)].x;
00258 points_[cp * 3 + 1] = ros_cloud.points[indices.at (cp)].y;
00259 points_[cp * 3 + 2] = ros_cloud.points[indices.at (cp)].z;
00260 }
00261 m_lock_.unlock ();
00262
00263 return (indices.size ());
00264 }
00265 }