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
00038 #include <gtest/gtest.h>
00039
00040 #include <iostream>
00041 #include <map>
00042 using namespace std;
00043
00044 #include <pcl/common/time.h>
00045
00046 #include <pcl/kdtree/kdtree_flann.h>
00047 #include <pcl/kdtree/tree_types.h>
00048 #include <pcl/point_cloud.h>
00049 #include <pcl/point_types.h>
00050 #include <boost/make_shared.hpp>
00051 using namespace pcl;
00052
00053 struct MyPoint : public PointXYZ
00054 {
00055 MyPoint(float x, float y, float z) {this->x=x; this->y=y; this->z=z;}
00056 };
00057
00058 PointCloud<MyPoint> cloud, cloud_big;
00059
00060
00061
00062 #include <pcl/kdtree/impl/kdtree_flann.hpp>
00063
00064
00065 void
00066 init ()
00067 {
00068 float resolution = 0.1;
00069 for (float z = -0.5f; z <= 0.5f; z += resolution)
00070 for (float y = -0.5f; y <= 0.5f; y += resolution)
00071 for (float x = -0.5f; x <= 0.5f; x += resolution)
00072 cloud.points.push_back (MyPoint (x, y, z));
00073 cloud.width = cloud.points.size ();
00074 cloud.height = 1;
00075
00076 cloud_big.width = 640;
00077 cloud_big.height = 480;
00078 srand (time (NULL));
00079
00080 for (size_t i = 0; i < cloud_big.width * cloud_big.height; ++i)
00081 cloud_big.points.push_back (MyPoint (1024 * rand () / (RAND_MAX + 1.0),
00082 1024 * rand () / (RAND_MAX + 1.0),
00083 1024 * rand () / (RAND_MAX + 1.0)));
00084 }
00085
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00132 TEST (PCL, KdTreeFLANN_radiusSearch)
00133 {
00134 KdTreeFLANN<MyPoint> kdtree;
00135 kdtree.setInputCloud (cloud.makeShared ());
00136 MyPoint test_point(0.0f, 0.0f, 0.0f);
00137 double max_dist = 0.15;
00138 set<int> brute_force_result;
00139 for (unsigned int i=0; i<cloud.points.size(); ++i)
00140 if (euclideanDistance(cloud.points[i], test_point) < max_dist)
00141 brute_force_result.insert(i);
00142 vector<int> k_indices;
00143 vector<float> k_distances;
00144 kdtree.radiusSearch (test_point, max_dist, k_indices, k_distances);
00145
00146
00147
00148 for (size_t i = 0; i < k_indices.size (); ++i)
00149 {
00150 set<int>::iterator brute_force_result_it = brute_force_result.find (k_indices[i]);
00151 bool ok = brute_force_result_it != brute_force_result.end ();
00152
00153
00154 EXPECT_EQ (ok, true);
00155 if (ok)
00156 brute_force_result.erase (brute_force_result_it);
00157 }
00158
00159
00160
00161 bool error = brute_force_result.size () > 0;
00162
00163 EXPECT_EQ (error, false);
00164
00165 {
00166 KdTreeFLANN<MyPoint> kdtree;
00167 kdtree.setInputCloud (cloud_big.makeShared ());
00168
00169 k_indices.resize(cloud_big.points.size ());
00170 k_distances.resize(cloud_big.points.size ());
00171
00172 ScopeTime scopeTime ("FLANN radiusSearch");
00173 {
00174 for (size_t i = 0; i < cloud_big.points.size (); ++i)
00175 kdtree.radiusSearch (cloud_big.points[i], 0.1, k_indices, k_distances);
00176 }
00177 }
00178
00179 {
00180 KdTreeFLANN<MyPoint> kdtree (false);
00181 kdtree.setInputCloud (cloud_big.makeShared ());
00182
00183 k_indices.resize(cloud_big.points.size ());
00184 k_distances.resize(cloud_big.points.size ());
00185 ScopeTime scopeTime ("FLANN radiusSearch (unsorted results)");
00186 {
00187 for (size_t i = 0; i < cloud_big.points.size (); ++i)
00188 kdtree.radiusSearch (cloud_big.points[i], 0.1, k_indices, k_distances);
00189 }
00190 }
00191 }
00192
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00244 TEST (PCL, KdTreeFLANN_nearestKSearch)
00245 {
00246 KdTreeFLANN<MyPoint> kdtree;
00247 kdtree.setInputCloud (cloud.makeShared ());
00248 MyPoint test_point (0.01f, 0.01f, 0.01f);
00249 unsigned int no_of_neighbors = 20;
00250 multimap<float, int> sorted_brute_force_result;
00251 for (size_t i = 0; i < cloud.points.size (); ++i)
00252 {
00253 float distance = euclideanDistance (cloud.points[i], test_point);
00254 sorted_brute_force_result.insert (make_pair (distance, i));
00255 }
00256 float max_dist = 0.0f;
00257 unsigned int counter = 0;
00258 for (multimap<float, int>::iterator it = sorted_brute_force_result.begin (); it != sorted_brute_force_result.end () && counter < no_of_neighbors; ++it)
00259 {
00260 max_dist = max (max_dist, it->first);
00261 ++counter;
00262 }
00263
00264 vector<int> k_indices;
00265 k_indices.resize (no_of_neighbors);
00266 vector<float> k_distances;
00267 k_distances.resize (no_of_neighbors);
00268 kdtree.nearestKSearch (test_point, no_of_neighbors, k_indices, k_distances);
00269
00270 EXPECT_EQ (k_indices.size (), no_of_neighbors);
00271
00272
00273 for (size_t i = 0; i < k_indices.size (); ++i)
00274 {
00275 const MyPoint& point = cloud.points[k_indices[i]];
00276 bool ok = euclideanDistance (test_point, point) <= max_dist;
00277 if (!ok)
00278 ok = (fabs (euclideanDistance (test_point, point)) - max_dist) <= 1e-6;
00279
00280
00281 EXPECT_EQ (ok, true);
00282 }
00283
00284 ScopeTime scopeTime ("FLANN nearestKSearch");
00285 {
00286 KdTreeFLANN<MyPoint> kdtree;
00287 kdtree.setInputCloud (cloud_big.makeShared ());
00288 for (size_t i = 0; i < cloud_big.points.size (); ++i)
00289 kdtree.nearestKSearch (cloud_big.points[i], no_of_neighbors, k_indices, k_distances);
00290 }
00291 }
00292
00294 class MyPointRepresenationXY : public PointRepresentation<MyPoint>
00295 {
00296 public:
00297 MyPointRepresenationXY ()
00298 {
00299 this->nr_dimensions_ = 2;
00300 }
00301
00302 void copyToFloatArray (const MyPoint &p, float *out) const
00303 {
00304 out[0] = p.x;
00305 out[1] = p.y;
00306 }
00307 };
00308
00309 TEST (PCL, KdTreeFLANN_setPointRepresentation)
00310 {
00311 PointCloud<MyPoint>::Ptr random_cloud (new PointCloud<MyPoint> ());
00312 random_cloud->points.push_back (MyPoint (86.6, 42.1, 92.4));
00313 random_cloud->points.push_back (MyPoint (63.1, 18.4, 22.3));
00314 random_cloud->points.push_back (MyPoint (35.5, 72.5, 37.3));
00315 random_cloud->points.push_back (MyPoint (99.7, 37.0, 8.7));
00316 random_cloud->points.push_back (MyPoint (22.4, 84.1, 64.0));
00317 random_cloud->points.push_back (MyPoint (65.2, 73.4, 18.0));
00318 random_cloud->points.push_back (MyPoint (60.4, 57.1, 4.5));
00319 random_cloud->points.push_back (MyPoint (38.7, 17.6, 72.3));
00320 random_cloud->points.push_back (MyPoint (14.2, 95.7, 34.7));
00321 random_cloud->points.push_back (MyPoint ( 2.5, 26.5, 66.0));
00322
00323 KdTreeFLANN<MyPoint> kdtree;
00324 kdtree.setInputCloud (random_cloud);
00325 MyPoint p (50.0, 50.0, 50.0);
00326
00327
00328 const int k = 10;
00329 vector<int> k_indices (k);
00330 vector<float> k_distances (k);
00331 kdtree.nearestKSearch (p, k, k_indices, k_distances);
00332 for (int i = 0; i < k; ++i)
00333 {
00334
00335 static const int gt_indices[10] = {2, 7, 5, 1, 4, 6, 9, 0, 8, 3};
00336 static const float gt_distances[10] =
00337 {877.8, 1674.7, 1802.6, 1937.5, 2120.6, 2228.8, 3064.5, 3199.7, 3604.2, 4344.8};
00338 EXPECT_EQ (k_indices[i], gt_indices[i]);
00339 EXPECT_NEAR (k_distances[i], gt_distances[i], 0.1);
00340 }
00341
00342
00343 kdtree.setPointRepresentation (boost::make_shared <MyPointRepresenationXY>());
00344 kdtree.nearestKSearch (p, k, k_indices, k_distances);
00345 for (int i = 0; i < k; ++i)
00346 {
00347
00348 static const int gt_indices[10] = {6, 2, 5, 1, 7, 0, 4, 3, 9, 8};
00349 static const float gt_distances[10] =
00350 {158.6, 716.5, 778.6, 1170.2, 1177.5, 1402.0, 1924.6, 2639.1, 2808.5, 3370.1};
00351 EXPECT_EQ (k_indices[i], gt_indices[i]);
00352 EXPECT_NEAR (k_distances[i], gt_distances[i], 0.1);
00353 }
00354
00355
00356 DefaultPointRepresentation<MyPoint> point_rep;
00357 float alpha[3] = {1.0, 2.0, 3.0};
00358 point_rep.setRescaleValues(alpha);
00359 kdtree.setPointRepresentation (point_rep.makeShared ());
00360 kdtree.nearestKSearch (p, k, k_indices, k_distances);
00361 for (int i = 0; i < k; ++i)
00362 {
00363
00364 static const int gt_indices[10] = {2, 9, 4, 7, 1, 5, 8, 0, 3, 6};
00365 static const float gt_distances[10] =
00366 {3686.9, 6769.2, 7177.0, 8802.3, 11071.5, 11637.3, 11742.4, 17769.0, 18497.3, 18942.0};
00367 EXPECT_EQ (k_indices[i], gt_indices[i]);
00368 EXPECT_NEAR (k_distances[i], gt_distances[i], 0.1);
00369 }
00370 }
00371
00372
00373 int
00374 main (int argc, char** argv)
00375 {
00376 testing::InitGoogleTest (&argc, argv);
00377 init ();
00378 return (RUN_ALL_TESTS ());
00379 }
00380