forked from KeckCAVES/LidarViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LidarProcessTest.cpp
864 lines (775 loc) · 21.5 KB
/
LidarProcessTest.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
/***********************************************************************
LidarProcessTest - Test program for the LiDAR processing octree data
structure.
Copyright (c) 2008-2009 Oliver Kreylos
This file is part of the LiDAR processing and analysis package.
The LiDAR processing and analysis package is free software; you can
redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
The LiDAR processing and analysis package is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with the LiDAR processing and analysis package; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
***********************************************************************/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <Misc/File.h>
#include <Misc/Timer.h>
#include <IO/File.h>
#include <IO/OpenFile.h>
#include <Threads/Thread.h>
#include <Math/Constants.h>
#include <Math/Random.h>
#include <Geometry/ArrayKdTree.h>
#include "LidarTypes.h"
#include "LidarProcessOctree.h"
#if 0
int main(int argc,char* argv[])
{
LidarProcessOctree lpo(argv[1],512*1024*1024);
Misc::Timer t;
size_t numLeafPoints=0;
for(LidarProcessOctree::PointIterator pIt=lpo.beginNodes();pIt!=lpo.endNodes();++pIt)
++numLeafPoints;
t.elapse();
std::cout<<numLeafPoints<<" leaf points, ";
std::cout<<"done in "<<t.getTime()*1000.0<<" ms"<<std::endl;
return 0;
}
#elif 1
class PointCounter // Functor class to count the number of points stored in an octree file
{
/* Elements: */
private:
size_t numPoints;
/* Constructors and destructors: */
public:
PointCounter(void)
:numPoints(0)
{
}
/* Methods: */
void operator()(const LidarPoint& p)
{
++numPoints;
}
size_t getNumPoints(void) const
{
return numPoints;
}
};
class DirectedProcessFunctor
{
/* Elements: */
protected:
Point queryPoint;
Scalar queryRadius2;
/* Constructors and destructors: */
public:
DirectedProcessFunctor(const Point& sQueryPoint,Scalar sQueryRadius2)
:queryPoint(sQueryPoint),queryRadius2(sQueryRadius2)
{
}
/* Methods: */
const Point& getQueryPoint(void) const
{
return queryPoint;
}
Scalar getQueryRadius2(void) const
{
return queryRadius2;
}
};
class NeighborCounter:public DirectedProcessFunctor
{
public:
size_t numPoints;
NeighborCounter(const Point& queryPoint,Scalar queryRadius2)
:DirectedProcessFunctor(queryPoint,queryRadius2),
numPoints(0)
{
}
void operator()(const LidarPoint& point)
{
++numPoints;
}
};
class PointDensityCalculator
{
struct ThreadArgs
{
unsigned int numPoints;
const LidarPoint* points;
size_t numNeighbors;
};
LidarProcessOctree& lpo;
Scalar neighborhoodRadius;
int numThreads;
Threads::Thread* threads;
ThreadArgs* threadArgs;
public:
size_t numPoints;
size_t totalNumNeighbors;
void* processThreadMethod(ThreadArgs* args)
{
args->numNeighbors=0;
for(unsigned int i=0;i<args->numPoints;++i)
{
NeighborCounter nc(args->points[i],neighborhoodRadius*neighborhoodRadius);
lpo.processPointsDirected(nc);
args->numNeighbors+=nc.numPoints;
}
}
PointDensityCalculator(LidarProcessOctree& sLpo,Scalar sNeighborhoodRadius,int sNumThreads)
:lpo(sLpo),
neighborhoodRadius(sNeighborhoodRadius),
numThreads(sNumThreads),threads(new Threads::Thread[numThreads]),threadArgs(new ThreadArgs[numThreads]),
numPoints(0),totalNumNeighbors(0)
{
}
~PointDensityCalculator(void)
{
delete[] threads;
delete[] threadArgs;
}
void operator()(LidarProcessOctree::Node& node,unsigned int nodeLevel)
{
if(!node.isLeaf()||node.getNumPoints()==0)
return;
for(int i=0;i<numThreads;++i)
{
unsigned int firstPoint=(node.getNumPoints()*i)/numThreads;
unsigned int lastPoint=(node.getNumPoints()*(i+1))/numThreads;
threadArgs[i].numPoints=lastPoint-firstPoint;
threadArgs[i].points=node.getPoints()+firstPoint;
threads[i].start(this,&PointDensityCalculator::processThreadMethod,&threadArgs[i]);
}
for(int i=0;i<numThreads;++i)
{
threads[i].join();
totalNumNeighbors+=threadArgs[i].numNeighbors;
}
numPoints+=node.getNumPoints();
}
void operator()(const LidarPoint& point)
{
++numPoints;
NeighborCounter nc(point,neighborhoodRadius*neighborhoodRadius);
lpo.processPointsDirected(nc);
totalNumNeighbors+=nc.numPoints;
}
};
class PointSaver
{
/* Elements: */
private:
Misc::File resultFile;
/* Constructors and destructors: */
public:
PointSaver(const char* resultFileName)
:resultFile(resultFileName,"wb")
{
}
/* Methods: */
void operator()(const LidarPoint& point)
{
fprintf(resultFile.getFilePtr(),"%.10f %.10f %.10f %03d %03d %03d\n",point[0],point[1],point[2],point.value[0],point.value[1],point.value[2]);
}
};
class BinaryPointSaver
{
/* Elements: */
private:
IO::SeekableFilePtr resultFile;
size_t numPoints;
/* Constructors and destructors: */
public:
BinaryPointSaver(const char* resultFileName)
:resultFile(IO::openSeekableFile(resultFileName,IO::File::WriteOnly)),
numPoints(0)
{
/* Write the initial number of points: */
resultFile->setEndianness(Misc::LittleEndian);
resultFile->write<unsigned int>(0);
}
~BinaryPointSaver(void)
{
/* Write the final number of points: */
resultFile->setWritePosAbs(0);
resultFile->write<unsigned int>(numPoints);
std::cout<<numPoints<<" points written to binary file"<<std::endl;
}
/* Methods: */
void operator()(const LidarPoint& point)
{
float pd[4];
for(int i=0;i<3;++i)
pd[i]=float(point[i]);
pd[3]=float(point.value[0])*0.3f+float(point.value[1])*0.59f+float(point.value[2])*0.11f;
resultFile->write<float>(pd,4);
++numPoints;
}
};
class LasPointSaver
{
/* Elements: */
private:
IO::SeekableFilePtr lasFile;
double scale[3],offset[3];
double min[3],max[3];
size_t numPoints;
/* Constructors and destructors: */
public:
LasPointSaver(const char* lasFileName,const double sScale[3],const double sOffset[3])
:lasFile(IO::openSeekableFile(lasFileName,IO::File::WriteOnly)),
numPoints(0)
{
for(int i=0;i<3;++i)
{
scale[i]=sScale[i];
offset[i]=sOffset[i];
min[i]=Math::Constants<double>::max;
max[i]=Math::Constants<double>::min;
}
/* Create the initial LAS file header: */
char signature[5]="LASF";
lasFile->write<char>(signature,4);
lasFile->write<unsigned short>(0);
lasFile->write<unsigned short>(0);
lasFile->write<unsigned int>(0);
lasFile->write<unsigned short>(0);
lasFile->write<unsigned short>(0);
char dummy[32]="";
lasFile->write<char>(dummy,8);
lasFile->write<unsigned char>(1);
lasFile->write<unsigned char>(2);
lasFile->write<char>(dummy,32);
lasFile->write<char>(dummy,32);
lasFile->write<unsigned short>(1);
lasFile->write<unsigned short>(2011);
lasFile->write<unsigned short>(227);
lasFile->write<unsigned int>(227);
lasFile->write<unsigned int>(0);
lasFile->write<unsigned char>(0);
lasFile->write<unsigned short>(20);
lasFile->write<unsigned int>(0);
lasFile->write<unsigned int>(0);
lasFile->write<unsigned int>(0);
lasFile->write<unsigned int>(0);
lasFile->write<unsigned int>(0);
lasFile->write<unsigned int>(0);
lasFile->write<double>(scale,3);
lasFile->write<double>(offset,3);
for(int i=0;i<3;++i)
{
lasFile->write<double>(max[i]);
lasFile->write<double>(min[i]);
}
}
~LasPointSaver(void)
{
/* Write the final LAS header: */
lasFile->setWritePosAbs(107);
lasFile->write<unsigned int>(numPoints);
lasFile->write<unsigned int>(numPoints);
lasFile->setWritePosAbs(179);
for(int i=0;i<3;++i)
{
lasFile->write<double>(max[i]);
lasFile->write<double>(min[i]);
}
}
/* Methods: */
void operator()(const LidarPoint& point)
{
/* Quantize the point position: */
int p[3];
for(int i=0;i<3;++i)
p[i]=int(Math::floor((double(point[i])-offset[i])/scale[i]+0.5));
/* Write the point record: */
lasFile->write<int>(p,3);
lasFile->write<unsigned short>(0);
lasFile->write<char>(0);
lasFile->write<char>(0);
lasFile->write<unsigned char>(0);
lasFile->write<unsigned char>(0);
lasFile->write<unsigned short>(0);
/* Update LAS header: */
for(int i=0;i<3;++i)
{
if(min[i]>point[i])
min[i]=point[i];
if(max[i]<point[i])
max[i]=point[i];
}
++numPoints;
}
};
int main(int argc,char* argv[])
{
LidarProcessOctree lpo(argv[1],512*1024*1024);
#if 0
Misc::Timer t;
{
// BinaryPointSaver bps(argv[2]);
double scale[3]={0.001,0.001,0.001};
double offset[3];
for(int i=0;i<3;++i)
offset[i]=lpo.getDomain().getCenter(i);
LasPointSaver lps(argv[2],scale,offset);
// PointCounter pc;
Box box(Box::Point(2.04446e6,617053.0,-1000.0),Box::Point(2.04446e6+100.0,617053.0+100.0,1000.0));
lpo.processPointsInBox(box,lps);
// std::cout<<lps.getNumPoints()<<std::endl;
}
t.elapse();
std::cout<<"Done in "<<t.getTime()*1000.0<<" ms"<<std::endl;
#elif 0
PointSaver ps(argv[2]);
Box box=Box::full;
for(int i=0;i<2;++i)
{
box.min[i]=atof(argv[3+i]);
box.max[i]=atof(argv[5+i]);
}
lpo.processPointsInBox(box,ps);
#elif 0
PointCounter pc;
lpo.processPoints(pc);
std::cout<<pc.getNumPoints()<<std::endl;
#else
Scalar radius=Scalar(0.1);
PointDensityCalculator pdc(lpo,radius,4);
#if 0
Box box;
box.min=Point(930,530,0);
box.max=Point(970,570,100);
lpo.processPointsInBox(box,pdc);
#else
// lpo.processPoints(pdc);
lpo.processNodesPostfix(pdc);
#endif
std::cout<<"Number of processed points: "<<pdc.numPoints<<std::endl;
std::cout<<"Total number of found neighbors: "<<pdc.totalNumNeighbors<<std::endl;
std::cout<<"Total number of loaded octree nodes: "<<lpo.getNumSubdivideCalls()<<", "<<lpo.getNumLoadedNodes()<<std::endl;
std::cout<<"Average point density in 1/m^3: "<<pdc.totalNumNeighbors/(pdc.numPoints*4.0/3.0*3.141592654*radius*radius*radius)<<std::endl;
#endif
return 0;
}
#else
class NearestNeighborFinder // Functor class to find the nearest non-duplicate neighbor of a point in an octree file
{
/* Elements: */
private:
Point queryPoint; // The query point position
Scalar dist2; // Squared distance to current nearest neighbor candidate
const LidarPoint* nearest; // Pointer to nearest neighbor
/* Constructors and destructors: */
public:
NearestNeighborFinder(const Point& sQueryPoint)
:queryPoint(sQueryPoint),
dist2(Math::Constants<Scalar>::max),
nearest(0)
{
}
/* Methods: */
void operator()(const LidarPoint& point)
{
Scalar pDist2=Geometry::sqrDist(point,queryPoint);
if(pDist2>Scalar(0)&&pDist2<dist2)
{
nearest=&point;
dist2=pDist2;
}
}
const Point& getQueryPoint(void) const
{
return queryPoint;
}
Scalar getQueryRadius2(void) const
{
return dist2;
}
const LidarPoint* getNearest(void) const
{
return nearest;
}
Scalar getNearestDistance2(void) const
{
return dist2;
}
};
class KNearestNeighborFinder // Functor class to find the k nearest neighbors of a point in an octree file
{
/* Embedded classes: */
private:
struct Neighbor // Structure to store a neighbor
{
/* Elements: */
public:
const LidarPoint* point; // Pointer to neighbor
Scalar dist2; // Squared distance from query position to neighbor
};
/* Elements: */
private:
Point queryPoint; // The query point position
int maxNumNeighbors; // Maximum number of neighbors to find
Neighbor* neighbors; // Array of current neighbor candidates
int numNeighbors; // Current number of neighbor candidates
Scalar maxDist2; // Current maximum distance to any neighbor candidate
/* Constructors and destructors: */
public:
KNearestNeighborFinder(const Point& sQueryPoint,int sMaxNumNeighbors)
:queryPoint(sQueryPoint),
maxNumNeighbors(sMaxNumNeighbors),
neighbors(new Neighbor[maxNumNeighbors]),
numNeighbors(0),
maxDist2(Math::Constants<Scalar>::max)
{
}
~KNearestNeighborFinder(void)
{
delete[] neighbors;
}
/* Methods: */
void operator()(const LidarPoint& point)
{
Scalar dist2=Geometry::sqrDist(point,queryPoint);
if(numNeighbors<maxNumNeighbors)
{
/* Insert the new point into the heap: */
int insertionPos=numNeighbors;
while(insertionPos>0)
{
int parent=(insertionPos-1)>>1;
if(neighbors[parent].dist2>=dist2)
break;
neighbors[insertionPos]=neighbors[parent];
insertionPos=parent;
}
neighbors[insertionPos].point=&point;
neighbors[insertionPos].dist2=dist2;
++numNeighbors;
if(numNeighbors==maxNumNeighbors)
maxDist2=neighbors[0].dist2;
}
else if(dist2<maxDist2)
{
/* Replace the currently farthest-away neighbor in the heap: */
int insertionPos=0;
while(true)
{
int biggestIndex=insertionPos;
Scalar biggest=dist2;
int child=(insertionPos<<1);
for(int i=0;i<2;++i)
{
++child;
if(child<maxNumNeighbors&&neighbors[child].dist2>biggest)
{
biggestIndex=child;
biggest=neighbors[child].dist2;
}
}
if(biggestIndex==insertionPos)
break;
neighbors[insertionPos]=neighbors[biggestIndex];
insertionPos=biggestIndex;
}
neighbors[insertionPos].point=&point;
neighbors[insertionPos].dist2=dist2;
maxDist2=neighbors[0].dist2;
}
}
const Point& getQueryPoint(void) const
{
return queryPoint;
}
Scalar getQueryRadius2(void) const
{
return maxDist2;
}
int getNumNeighbors(void) const
{
return numNeighbors;
}
const LidarPoint* getNeighbor(int index) const
{
return neighbors[index].point;
}
Scalar getNeighborDistance2(int index) const
{
return neighbors[index].dist2;
}
};
class PointNearestNeighborFinder // Functor class to find the nearest neighbors of all points in an octree file
{
/* Elements: */
private:
LidarProcessOctree& lpo;
size_t numPoints;
double totalDistance2;
/* Constructors and destructors: */
public:
PointNearestNeighborFinder(LidarProcessOctree& sLpo)
:lpo(sLpo),
numPoints(0),totalDistance2(0.0)
{
}
/* Methods: */
void operator()(const LidarPoint& p)
{
/* Find the nearest neighbor of this point: */
NearestNeighborFinder nnf(p);
lpo.processPointsDirected(nnf);
++numPoints;
totalDistance2+=double(nnf.getNearestDistance2());
}
size_t getNumPoints(void) const
{
return numPoints;
}
Scalar getAverageDist(void) const
{
return Scalar(Math::sqrt(totalDistance2/double(numPoints)));
}
};
class PointKNearestNeighborFinder // Functor class to find the k nearest neighbors of all points in an octree file
{
/* Elements: */
private:
LidarProcessOctree& lpo;
int maxNumNeighbors;
size_t numPoints;
double totalDistance2;
/* Constructors and destructors: */
public:
PointKNearestNeighborFinder(LidarProcessOctree& sLpo,int sMaxNumNeighbors)
:lpo(sLpo),
maxNumNeighbors(sMaxNumNeighbors),
numPoints(0),totalDistance2(0.0)
{
}
/* Methods: */
void operator()(const LidarPoint& p)
{
/* Find the k nearest neighbors of this point: */
KNearestNeighborFinder knnf(p,maxNumNeighbors);
lpo.processPointsDirected(knnf);
++numPoints;
totalDistance2+=double(knnf.getNeighborDistance2(0));
}
size_t getNumPoints(void) const
{
return numPoints;
}
Scalar getAverageDist(void) const
{
return Scalar(Math::sqrt(totalDistance2/double(numPoints)));
}
};
class PointExtractor // Functor class to load all points from an octree file into a std::vector
{
/* Elements: */
private:
std::vector<LidarPoint> points; // Point array
/* Constructors and destructors: */
public:
PointExtractor(void)
{
}
/* Methods: */
void operator()(const LidarPoint& p)
{
points.push_back(p);
}
const std::vector<LidarPoint>& getPoints(void) const
{
return points;
}
};
class NearestNeighborFinderKdtree // Functor class to find the non-duplicate nearest neighbor of a point in an in-memory kd-tree
{
/* Elements: */
private:
Point queryPoint; // The query point position
Scalar dist2; // Squared distance to current nearest neighbor candidate
const LidarPoint* nearest; // Pointer to nearest neighbor
/* Constructors and destructors: */
public:
NearestNeighborFinderKdtree(const Point& sQueryPoint)
:queryPoint(sQueryPoint),
dist2(Math::Constants<Scalar>::max),
nearest(0)
{
}
/* Methods: */
bool operator()(const LidarPoint& point,int splitDimension)
{
Scalar pDist2=Geometry::sqrDist(point,queryPoint);
if(pDist2>Scalar(0)&&pDist2<dist2)
{
nearest=&point;
dist2=pDist2;
}
/* Stop traversal if split plane is farther away than closest point: */
return dist2>Math::sqr(point[splitDimension]-queryPoint[splitDimension]);
}
const Point& getQueryPosition(void) const
{
return queryPoint;
}
Scalar getQueryRadius2(void) const
{
return dist2;
}
const LidarPoint* getNearest(void) const
{
return nearest;
}
Scalar getNearestDistance2(void) const
{
return dist2;
}
};
int main(int argc,char* argv[])
{
const char* fileName=0;
int maxNumNeighbors=10;
int cacheSize=512;
for(int i=1;i<argc;++i)
{
if(argv[i][0]=='-')
{
if(strcasecmp(argv[i]+1,"numNeighbors")==0)
{
++i;
maxNumNeighbors=atoi(argv[i]);
}
else if(strcasecmp(argv[i]+1,"cache")==0)
{
++i;
cacheSize=atoi(argv[i]);
}
}
else if(fileName==0)
fileName=argv[i];
}
if(fileName==0)
{
std::cerr<<"No file name provided"<<std::endl;
return 1;
}
Misc::Timer t;
/* Create a processing octree: */
LidarProcessOctree lpo(fileName,size_t(cacheSize)*size_t(1024*1024));
#if 0
/* Count the number of points in the octree: */
PointCounter pc;
const Cube& c=lpo.getDomain();
// Box box(c.getMin(),c.getMax());
Box box(Point(700.0,300.0,-200.0),Point(1200.0,800.0,300.0));
lpo.processPointsInBox(box,pc);
std::cout<<"Octree contains "<<pc.getNumPoints()<<" points"<<std::endl;
#elif 1
if(maxNumNeighbors==1)
{
/* Find the nearest neighbor of each point in the octree: */
PointNearestNeighborFinder pnnf(lpo);
lpo.processPoints(pnnf);
std::cout<<"Octree contains "<<pnnf.getNumPoints()<<" points, average point density is "<<pnnf.getAverageDist()<<std::endl;
}
else
{
/* Find the k nearest neighbors of each point in the octree: */
PointKNearestNeighborFinder pknnf(lpo,maxNumNeighbors);
lpo.processPoints(pknnf);
std::cout<<"Octree contains "<<pknnf.getNumPoints()<<" points, average point density is "<<pknnf.getAverageDist()<<std::endl;
}
#elif 0
/* Extract the points from the octree: */
PointExtractor pe;
lpo.processPoints(pe);
/* Build a kd-tree from the points: */
Geometry::ArrayKdTree<LidarPoint> kdTree;
LidarPoint* points=kdTree.createTree(pe.getPoints().size());
for(size_t i=0;i<pe.getPoints().size();++i)
points[i]=pe.getPoints()[i];
kdTree.releasePoints();
#if 0
/* Find the nearest neighbor of all points in the kd-tree: */
double nearestDistance2=0.0;
for(size_t i=0;i<pe.getPoints().size();++i)
{
NearestNeighborFinderKdtree nnfkd(points[i]);
kdTree.traverseTreeDirected(nnfkd);
nearestDistance2+=double(nnfkd.getNearestDistance2());
}
std::cout<<"Octree contains "<<pe.getPoints().size()<<" points, average point density is "<<Math::sqrt(nearestDistance2/double(pe.getPoints().size()))<<std::endl;
#else
/* Search points randomly to test for correctness: */
for(int index=0;index<pe.getPoints().size();++index)
{
/* Use the kd-tree: */
NearestNeighborFinderKdtree nnfkd(points[index]);
kdTree.traverseTreeDirected(nnfkd);
/* Use the out-of-core search algorithm: */
NearestNeighborFinder nnf(points[index]);
lpo.processPointsDirected(nnf);
if(nnfkd.getNearestDistance2()!=nnf.getNearestDistance2())
{
/* Whoopsie! */
std::cout<<"Mismatch for search point "<<points[index][0]<<", "<<points[index][1]<<", "<<points[index][2]<<":"<<std::endl;
std::cout<<"Kd-tree: "<<nnfkd.getNearestDistance2()<<" "<<(*nnfkd.getNearest())[0]<<", "<<(*nnfkd.getNearest())[1]<<", "<<(*nnfkd.getNearest())[0]<<std::endl;
std::cout<<"Octree : "<<nnf.getNearestDistance2()<<" "<<(*nnf.getNearest())[0]<<", "<<(*nnf.getNearest())[1]<<", "<<(*nnf.getNearest())[0]<<std::endl;
}
}
#endif
#else
/* Extract the points from the octree: */
PointExtractor pe;
lpo.processPoints(pe);
/* Compare a brute-force search against the octree-based algorithm: */
const std::vector<LidarPoint>& points=pe.getPoints();
for(int i=0;i<100000;++i)
{
size_t index=Math::randUniformCO(0,points.size());
/* Use the out-of-core search algorithm: */
NearestNeighborFinder nnf(points[index]);
lpo.processPointsDirected(nnf);
/* Use brute force: */
const LidarPoint* nearest=0;
Scalar minDist2=Math::Constants<Scalar>::max;
for(size_t i=0;i<points.size();++i)
{
Scalar dist2=Geometry::sqrDist(points[index],points[i]);
if(dist2>Scalar(0)&&dist2<minDist2)
{
nearest=&points[i];
minDist2=dist2;
}
}
if(nnf.getNearestDistance2()!=minDist2)
{
/* Whoopsie! */
std::cout<<"Mismatch for search point "<<points[index][0]<<", "<<points[index][1]<<", "<<points[index][2]<<":"<<std::endl;
std::cout<<"Octree : "<<nnf.getNearestDistance2()<<" "<<(*nnf.getNearest())[0]<<", "<<(*nnf.getNearest())[1]<<", "<<(*nnf.getNearest())[0]<<std::endl;
std::cout<<"Brute force: "<<minDist2<<" "<<(*nearest)[0]<<", "<<(*nearest)[1]<<", "<<(*nearest)[2]<<std::endl;
}
}
#endif
t.elapse();
/* Clean up and exit: */
std::cout<<"Loaded "<<lpo.getNumLoadedNodes()<<" nodes"<<std::endl;
std::cout<<"Total runtime: "<<t.getTime()<<" s"<<std::endl;
return 0;
}
#endif