kdtree_flann.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008 Radu Bogdan Rusu <rusu -=- cs.tum.edu>
00003  *
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  *     * Redistributions of source code must retain the above copyright
00010  *       notice, this list of conditions and the following disclaimer.
00011  *     * Redistributions in binary form must reproduce the above copyright
00012  *       notice, this list of conditions and the following disclaimer in the
00013  *       documentation and/or other materials provided with the distribution.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00025  * POSSIBILITY OF SUCH DAMAGE.
00026  *
00027  * $Id$
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 //    std::cerr << p[0] <<  " " << p[1] << " " << p[2] << std::endl;
00054     m_lock_.lock ();
00055 //    int* nn_idx_ = (int*) malloc (k * sizeof (int));
00056 //    float* nn_dists_ = (float*) malloc (k * sizeof (float));
00057     flann_find_nearest_neighbors_index (index_id_, p, 1, &k_indices[0], &k_distances[0], k, flann_param_.checks, &flann_param_);
00058 //    flann_find_nearest_neighbors_index (index_id_, p, 1, nn_idx_, nn_dists_, k, flann_param_.checks, &flann_param_);
00059 
00060 //    EXPECT_EQ (indices[0], 0);
00061 //    EXPECT_EQ (indices[1], 12);
00062 //    EXPECT_EQ (indices[2], 198);
00063 //    EXPECT_EQ (indices[3], 1);
00064 //    EXPECT_EQ (indices[4], 127);
00065 //    EXPECT_EQ (indices[5], 18);
00066 //    EXPECT_EQ (indices[6], 132);
00067 //    EXPECT_EQ (indices[7], 10);
00068 //    EXPECT_EQ (indices[8], 11);
00069 //    EXPECT_EQ (indices[9], 197);
00070 
00072 
00077 
00078     for (int i = 0 ; i < 10; i++)
00079       std::cerr << k_indices[i] << " ";
00080     std::cerr << std::endl;
00081 
00082 //    free (nn_idx_);
00083 //    free (nn_dists_);
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 //    m_lock_.lock ();
00136 //    int neighbors_in_radius_ = ann_kd_tree_->annkFRSearch (p, radius, 0, NULL, NULL, epsilon_);
00137 //    m_lock_.unlock ();
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 //    m_lock_.lock ();
00177 //    int neighbors_in_radius_ = ann_kd_tree_->annkFRSearch (p, radius, 0, NULL, NULL, epsilon_);
00178 //    m_lock_.unlock ();
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     // No point in doing anything if the array is empty
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));    // default number of dimensions (3 = xyz)
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     // No point in doing anything if the array is empty
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));    // default number of dimensions (3 = xyz)
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 }


door_handle_detector
Author(s): Radu Bogdan Rusu, Marius
autogenerated on Wed Dec 11 2013 14:17:00