forked from PointCloudLibrary/pcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_octree.cpp
372 lines (310 loc) · 12.6 KB
/
test_octree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* Software License Agreement (BSD License)
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2010-2011, Willow Garage, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the copyright holder(s) nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*
*/
#include <gtest/gtest.h>
#include <vector>
#include <stdio.h>
#include <pcl/common/time.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/search/pcl_search.h>
using namespace std;
using namespace pcl;
using namespace octree;
// helper class for priority queue
class prioPointQueueEntry
{
public:
prioPointQueueEntry ()
{
}
prioPointQueueEntry (PointXYZ& point_arg, double pointDistance_arg, int pointIdx_arg)
{
point_ = point_arg;
pointDistance_ = pointDistance_arg;
pointIdx_ = pointIdx_arg;
}
bool
operator< (const prioPointQueueEntry& rhs_arg) const
{
return (this->pointDistance_ < rhs_arg.pointDistance_);
}
PointXYZ point_;
double pointDistance_;int pointIdx_;
};
TEST (PCL, Octree_Pointcloud_Nearest_K_Neighbour_Search)
{
const unsigned int test_runs = 1;
unsigned int test_id;
// instantiate point cloud
PointCloud<PointXYZ>::Ptr cloudIn (new PointCloud<PointXYZ> ());
size_t i;
srand (static_cast<unsigned int> (time (NULL)));
unsigned int K;
std::priority_queue<prioPointQueueEntry, pcl::PointCloud<prioPointQueueEntry>::VectorType> pointCandidates;
// create octree
pcl::search::Search<PointXYZ>* octree = new pcl::search::Octree<PointXYZ> (0.1);
std::vector<int> k_indices;
std::vector<float> k_sqr_distances;
std::vector<int> k_indices_bruteforce;
std::vector<float> k_sqr_distances_bruteforce;
for (test_id = 0; test_id < test_runs; test_id++)
{
// define a random search point
PointXYZ searchPoint (static_cast<float> (10.0 * (rand () / static_cast<double> (RAND_MAX))),
static_cast<float> (10.0 * (rand () / static_cast<double> (RAND_MAX))),
static_cast<float> (10.0 * (rand () / static_cast<double> (RAND_MAX))));
K = 1 + rand () % 10;
// generate point cloud
cloudIn->width = 1000;
cloudIn->height = 1;
cloudIn->points.resize (cloudIn->width * cloudIn->height);
for (i = 0; i < 1000; i++)
{
cloudIn->points[i] = PointXYZ (static_cast<float> (5.0 * (rand () / static_cast<double> (RAND_MAX))),
static_cast<float> (10.0 * (rand () / static_cast<double> (RAND_MAX))),
static_cast<float> (10.0 * (rand () / static_cast<double> (RAND_MAX))));
}
double pointDist;
k_indices.clear ();
k_sqr_distances.clear ();
k_indices_bruteforce.clear ();
k_sqr_distances_bruteforce.clear ();
// push all points and their distance to the search point into a priority queue - bruteforce approach.
for (i = 0; i < cloudIn->points.size (); i++)
{
pointDist = ((cloudIn->points[i].x - searchPoint.x) * (cloudIn->points[i].x - searchPoint.x)
+ (cloudIn->points[i].y - searchPoint.y) * (cloudIn->points[i].y - searchPoint.y) + (cloudIn->points[i].z
- searchPoint.z) * (cloudIn->points[i].z - searchPoint.z));
prioPointQueueEntry pointEntry (cloudIn->points[i], pointDist, static_cast<int> (i));
pointCandidates.push (pointEntry);
}
// pop priority queue until we have the nearest K elements
while (pointCandidates.size () > K)
pointCandidates.pop ();
// copy results into vectors
unsigned idx = static_cast<unsigned> (pointCandidates.size ());
k_indices_bruteforce.resize (idx);
k_sqr_distances_bruteforce.resize (idx);
while (pointCandidates.size ())
{
--idx;
k_indices_bruteforce [idx] = pointCandidates.top ().pointIdx_;
k_sqr_distances_bruteforce [idx] = static_cast<float> (pointCandidates.top ().pointDistance_);
pointCandidates.pop ();
}
// octree nearest neighbor search
octree->setInputCloud (cloudIn);
octree->nearestKSearch (searchPoint, static_cast<int> (K), k_indices, k_sqr_distances);
ASSERT_EQ ( k_indices.size() , k_indices_bruteforce.size() );
// compare nearest neighbor results of octree with bruteforce search
i = 0;
while (k_indices_bruteforce.size ())
{
ASSERT_EQ ( k_indices.back() , k_indices_bruteforce.back() );
EXPECT_NEAR (k_sqr_distances.back(), k_sqr_distances_bruteforce.back(), 1e-4);
k_indices_bruteforce.pop_back();
k_indices.pop_back();
k_sqr_distances_bruteforce.pop_back();
k_sqr_distances.pop_back();
}
}
}
#if 0
TEST (PCL, Octree_Pointcloud_Approx_Nearest_Neighbour_Search)
{
const unsigned int test_runs = 100;
unsigned int test_id;
unsigned int bestMatchCount = 0;
// instantiate point cloud
PointCloud<PointXYZ>::Ptr cloudIn (new PointCloud<PointXYZ> ());
size_t i;
srand (time (NULL));
double voxelResolution = 0.1;
// create octree
pcl::search::Search<PointXYZ>* octree = new pcl::search::Octree<PointXYZ> (voxelResolution);
for (test_id = 0; test_id < test_runs; test_id++)
{
// define a random search point
PointXYZ searchPoint (10.0 * ((double)rand () / (double)RAND_MAX), 10.0 * ((double)rand () / (double)RAND_MAX),
10.0 * ((double)rand () / (double)RAND_MAX));
// generate point cloud
cloudIn->width = 1000;
cloudIn->height = 1;
cloudIn->points.resize (cloudIn->width * cloudIn->height);
for (i = 0; i < 1000; i++)
cloudIn->points[i] = PointXYZ (5.0 * ((double)rand () / (double)RAND_MAX),
10.0 * ((double)rand () / (double)RAND_MAX),
10.0 * ((double)rand () / (double)RAND_MAX));
// brute force search
double pointDist;
double BFdistance = numeric_limits<double>::max ();
int BFindex = 0;
for (i = 0; i < cloudIn->points.size (); i++)
{
pointDist = ((cloudIn->points[i].x - searchPoint.x) * (cloudIn->points[i].x - searchPoint.x)
+ (cloudIn->points[i].y - searchPoint.y) * (cloudIn->points[i].y - searchPoint.y) + (cloudIn->points[i].z
- searchPoint.z) * (cloudIn->points[i].z - searchPoint.z));
if (pointDist < BFdistance)
{
BFindex = i;
BFdistance = pointDist;
}
}
int ANNindex;
float ANNdistance;
octree->setInputCloud (cloudIn);
octree->approxNearestSearch (searchPoint, ANNindex, ANNdistance);
if (BFindex == ANNindex)
{
EXPECT_NEAR (ANNdistance, BFdistance, 1e-4);
bestMatchCount++;
}
}
// we should have found the absolute nearest neighbor at least once
//ASSERT_EQ ( (bestMatchCount > 0) , true);
}
#endif
#if 0
TEST (PCL, Octree_RadiusSearch_GPU)
{
PointCloud<PointXYZ>::Ptr cloudIn (new PointCloud<PointXYZ> ());
// generate point cloud data
cloudIn->width = 1000;
cloudIn->height = 1;
cloudIn->points.resize (cloudIn->width * cloudIn->height);
int i=0;
for (i = 0; i < 1000; i++)
{
cloudIn->points[i] = PointXYZ (10.0 * ((double)rand () / (double)RAND_MAX),
10.0 * ((double)rand () / (double)RAND_MAX),
5.0 * ((double)rand () / (double)RAND_MAX));
}
Search<PointXYZ>* octree = new pcl::octree::OctreeWrapper<PointXYZ>(0.1f);
octree->setInputCloud(cloudIn);
std::vector <PointXYZ > point;
const PointXYZ searchPoint (10.0 * ((double)rand () / (double)RAND_MAX), 10.0 * ((double)rand () / (double)RAND_MAX),
10.0 * ((double)rand () / (double)RAND_MAX));
point.push_back(searchPoint);
point.push_back(searchPoint);
point.push_back(searchPoint);
double searchRadius = 5.0 * ((double)rand () / (double)RAND_MAX);
double radius =5;
vector < double > radiuses;
radiuses.push_back(radius);
radiuses.push_back(radius);
radiuses.push_back(radius);
std::vector<std::vector<int> > k_indices;
std::vector<std::vector<float> > k_distances;
int max_nn = -1;
octree->radiusSearch (point, radiuses, k_indices,k_distances,max_nn );
}
#endif
TEST (PCL, Octree_Pointcloud_Neighbours_Within_Radius_Search)
{
const unsigned int test_runs = 100;
unsigned int test_id;
// instantiate point clouds
PointCloud<PointXYZ>::Ptr cloudIn (new PointCloud<PointXYZ> ());
PointCloud<PointXYZ>::Ptr cloudOut (new PointCloud<PointXYZ> ());
size_t i;
srand (static_cast<unsigned int> (time (NULL)));
for (test_id = 0; test_id < test_runs; test_id++)
{
// define a random search point
PointXYZ searchPoint (static_cast<float> (10.0 * (rand () / static_cast<double> (RAND_MAX))),
static_cast<float> (10.0 * (rand () / static_cast<double> (RAND_MAX))),
static_cast<float> (10.0 * (rand () / static_cast<double> (RAND_MAX))));
cloudIn->width = 1000;
cloudIn->height = 1;
cloudIn->points.resize (cloudIn->width * cloudIn->height);
// generate point cloud data
for (i = 0; i < 1000; i++)
{
cloudIn->points[i] = PointXYZ (static_cast<float> (10.0 * (rand () / static_cast<double> (RAND_MAX))),
static_cast<float> (10.0 * (rand () / static_cast<double> (RAND_MAX))),
static_cast<float> (5.0 * (rand () / static_cast<double> (RAND_MAX))));
}
pcl::search::Search<PointXYZ>* octree = new pcl::search::Octree<PointXYZ> (0.001);
// build octree
double pointDist;
double searchRadius = 5.0 * rand () / static_cast<double> (RAND_MAX);
// bruteforce radius search
vector<int> cloudSearchBruteforce;
for (i = 0; i < cloudIn->points.size (); i++)
{
pointDist = sqrt (
(cloudIn->points[i].x - searchPoint.x) * (cloudIn->points[i].x - searchPoint.x)
+ (cloudIn->points[i].y - searchPoint.y) * (cloudIn->points[i].y - searchPoint.y)
+ (cloudIn->points[i].z - searchPoint.z) * (cloudIn->points[i].z - searchPoint.z));
if (pointDist <= searchRadius)
{
// add point candidates to vector list
cloudSearchBruteforce.push_back (static_cast<int> (i));
}
}
vector<int> cloudNWRSearch;
vector<float> cloudNWRRadius;
// execute octree radius search
octree->setInputCloud (cloudIn);
octree->radiusSearch (searchPoint, searchRadius, cloudNWRSearch, cloudNWRRadius);
ASSERT_EQ ( cloudNWRRadius.size() , cloudSearchBruteforce.size());
// check if result from octree radius search can be also found in bruteforce search
std::vector<int>::const_iterator current = cloudNWRSearch.begin();
while (current != cloudNWRSearch.end())
{
pointDist = sqrt (
(cloudIn->points[*current].x-searchPoint.x) * (cloudIn->points[*current].x-searchPoint.x) +
(cloudIn->points[*current].y-searchPoint.y) * (cloudIn->points[*current].y-searchPoint.y) +
(cloudIn->points[*current].z-searchPoint.z) * (cloudIn->points[*current].z-searchPoint.z)
);
ASSERT_EQ ( (pointDist<=searchRadius) , true);
++current;
}
// check if result limitation works
octree->radiusSearch(searchPoint, searchRadius, cloudNWRSearch, cloudNWRRadius, 5);
ASSERT_EQ ( cloudNWRRadius.size() <= 5, true);
}
}
/* ---[ */
int
main (int argc, char** argv)
{
testing::InitGoogleTest (&argc, argv);
return (RUN_ALL_TESTS ());
}
/* ]--- */