-
Notifications
You must be signed in to change notification settings - Fork 39
/
PaulBunyan.cpp
459 lines (411 loc) · 12.3 KB
/
PaulBunyan.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
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <iomanip>
#include <Misc/ThrowStdErr.h>
#include <Threads/Thread.h>
#include <Threads/Barrier.h>
#include <Math/Math.h>
#include "LidarTypes.h"
#include "LidarProcessOctree.h"
class Axe
{
/* Elements: */
private:
Scalar radius,radius2;
Scalar tolerance;
Scalar maxAngle;
Point candidate;
bool haveD0;
Vector d0;
Vector dl,dr;
/* Constructors and destructors: */
public:
Axe(Scalar sRadius,Scalar sTolerance,Scalar sMaxAngle)
:radius(sRadius),radius2(Math::sqr(radius)),tolerance(sTolerance),maxAngle(sMaxAngle)
{
}
/* Methods: */
void prepareTraversal(const Point& sCandidate)
{
candidate=sCandidate;
haveD0=false;
}
Box getBox(void) const
{
Box result;
for(int i=0;i<2;++i)
{
result.min[i]=candidate[i]-radius;
result.max[i]=candidate[i]+radius;
}
result.min[2]=Box::full.min[2];
result.max[2]=candidate[2]-tolerance;
return result;
}
void operator()(const LidarPoint& lp)
{
/* Check if the point is inside the search cylinder: */
Scalar r2=Math::sqr(lp[0]-candidate[0])+Math::sqr(lp[1]-candidate[1]);
Scalar cyl=candidate[2]-tolerance-radius;
if(r2<=radius2&&(lp[2]<=cyl||r2+Math::sqr(lp[2]-cyl)<=radius2))
{
/* Check if this is the first encountered point: */
Vector d=lp-candidate;
d[2]=Scalar(0);
if(haveD0)
{
#if 1
/* Check if the point is on the left or right side of d0: */
if(d*d0>=Scalar(0))
{
/* Check the left vector: */
if(d*dl>Scalar(0))
dl=Vector(-d[1],d[0],0);
}
else
{
/* Check the right vector: */
if(d*dr<Scalar(0))
dr=Vector(-d[1],d[0],0);
}
#endif
}
else
{
/* Initialize the area finder: */
d0=Vector(-d[1],d[0],0);
d0.normalize();
dl=dr=d0;
haveD0=true;
}
}
}
bool isGround(void) const
{
if(haveD0)
{
#if 1
/* Check that the angle formed by the left and right vectors is less than maxAngle: */
double cosAlpha1=(dl*d0)/dl.mag();
if(cosAlpha1>Scalar(1))
cosAlpha1=Scalar(1);
if(cosAlpha1<Scalar(-1))
cosAlpha1=Scalar(-1);
double cosAlpha2=(dr*d0)/dr.mag();
if(cosAlpha2>Scalar(1))
cosAlpha2=Scalar(1);
if(cosAlpha2<Scalar(-1))
cosAlpha2=Scalar(-1);
return Math::acos(cosAlpha1)+Math::acos(cosAlpha2)<maxAngle;
#else
return false;
#endif
}
else
return true;
}
};
class PaulBunyan
{
/* Elements: */
private:
LidarProcessOctree& lpo; // The processed LiDAR octree
Axe axe; // The axe
Color* colorBuffer; // Array to hold colors for a node during processing
Color* childColorBuffers[8]; // Arrays to hold colors for a node's children during processing
LidarFile::Offset colorDataSize; // Size of each record in the color file
LidarFile colorFile; // The file to which to write the color data
FILE* pointFile; // Optional ASCII file to store ground points
unsigned int numThreads; // Number of processing threads
bool shutdownThreads; // Flag to shut down threads at the end
Threads::Thread* calcThreads; // Array of threads to calculate normal vectors from point neighborhoods
Threads::Barrier calcBarrier;
Threads::Thread* subsampleThreads; // Array of threads to subsample normal vectors from node children
Threads::Barrier subsampleBarrier;
LidarProcessOctree::Node* currentNode; // Pointer to currently processed node
LidarProcessOctree::Node* currentChildren[8]; // Pointer to children of currently processed node
size_t numProcessedNodes; // Number of already processed nodes
/* Private methods: */
void* calcThreadMethod(unsigned int threadIndex);
void* subsampleThreadMethod(unsigned int threadIndex);
/* Constructors and destructors: */
public:
PaulBunyan(LidarProcessOctree& sLpo,const Axe& sAxe,const char* colorFileName,const char* pointFileName,unsigned int sNumThreads =1); // Creates an axe wielder with the given axe
~PaulBunyan(void);
/* Methods: */
void operator()(LidarProcessOctree::Node& node,unsigned int nodeLevel);
};
/***************************
Methods of class PaulBunyan:
***************************/
void* PaulBunyan::calcThreadMethod(unsigned int threadIndex)
{
Threads::Thread::setCancelState(Threads::Thread::CANCEL_ENABLE);
while(true)
{
/* Wait on the calculation barrier until there is a job: */
calcBarrier.synchronize();
if(shutdownThreads)
break;
/* Process the current node: */
unsigned int firstI=(threadIndex*currentNode->getNumPoints())/numThreads;
unsigned int lastI=((threadIndex+1)*currentNode->getNumPoints())/numThreads;
for(unsigned int index=firstI;index<lastI;++index)
{
/* Apply the axe to the point: */
axe.prepareTraversal((*currentNode)[index]);
lpo.processPointsInBox(axe.getBox(),axe);
/* Color the point based on its classification: */
float intensity=float((*currentNode)[index].value[0])*0.299f+float((*currentNode)[index].value[1])*0.587f+float((*currentNode)[index].value[2])*0.114f;
for(int i=0;i<4;++i)
colorBuffer[index][i]=Color::Scalar(0);
if(axe.isGround())
{
/* Color the point brown: */
colorBuffer[index][0]=Color::Scalar(intensity*0.5f+0.5f);
colorBuffer[index][1]=Color::Scalar(intensity*0.333f+0.5f);
if(pointFile!=0)
{
/* Write the original point to a file: */
fprintf(pointFile,"%.10g %.10g %.10g %3u %3u %3u\n",
(*currentNode)[index][0],(*currentNode)[index][1],(*currentNode)[index][2],
(*currentNode)[index].value[0],(*currentNode)[index].value[1],(*currentNode)[index].value[2]);
}
}
else
{
/* Color the point green: */
colorBuffer[index][1]=Color::Scalar(intensity+0.5f);
}
}
/* Synchronize on the calculation barrier to signal job completion: */
calcBarrier.synchronize();
}
return 0;
}
namespace {
/**************
Helper classes:
**************/
class FindPoint // Class to find a point inside an octree node
{
/* Elements: */
private:
Point queryPoint; // The position of the point to find
const LidarPoint* foundPoint; // The found LiDAR point
/* Constructors and destructors: */
public:
FindPoint(const Point& sQueryPoint)
:queryPoint(sQueryPoint),
foundPoint(0)
{
}
/* Methods: */
void operator()(const LidarPoint& lp)
{
if(Geometry::sqrDist(lp,queryPoint)==Scalar(0))
foundPoint=&lp;
}
const Point& getQueryPoint(void) const
{
return queryPoint;
}
Scalar getQueryRadius2(void) const
{
return Scalar(0);
}
const LidarPoint* getFoundPoint(void) const
{
return foundPoint;
}
};
}
void* PaulBunyan::subsampleThreadMethod(unsigned int threadIndex)
{
Threads::Thread::setCancelState(Threads::Thread::CANCEL_ENABLE);
while(true)
{
/* Wait on the subsampling barrier until there is a job: */
subsampleBarrier.synchronize();
if(shutdownThreads)
break;
/* Find the ancestor points of each node point and copy their classified colors: */
unsigned int firstI=(threadIndex*currentNode->getNumPoints())/numThreads;
unsigned int lastI=((threadIndex+1)*currentNode->getNumPoints())/numThreads;
for(unsigned int i=firstI;i<lastI;++i)
{
/* Find the child node containing this point's ancestor: */
int pointChildIndex=currentNode->getDomain().findChild((*currentNode)[i]);
LidarProcessOctree::Node* pointChild=currentChildren[pointChildIndex];
/* Find the point's ancestor: */
FindPoint fp((*currentNode)[i]);
lpo.processNodePointsDirected(pointChild,fp);
if(fp.getFoundPoint()==0)
Misc::throwStdErr("Things are fucked up!");
/* Copy the ancestor's color: */
colorBuffer[i]=childColorBuffers[pointChildIndex][fp.getFoundPoint()-pointChild->getPoints()];
}
/* Synchronize on the subsampling barrier to signal job completion: */
subsampleBarrier.synchronize();
}
return 0;
}
PaulBunyan::PaulBunyan(LidarProcessOctree& sLpo,const Axe& sAxe,const char* colorFileName,const char* pointFileName,unsigned int sNumThreads)
:lpo(sLpo),axe(sAxe),
colorBuffer(new Color[lpo.getMaxNumPointsPerNode()]),
colorDataSize(sizeof(Color)),
colorFile(colorFileName,LidarFile::ReadWrite),
pointFile(0),
numThreads(sNumThreads),shutdownThreads(false),
calcThreads(new Threads::Thread[numThreads]),calcBarrier(numThreads+1),
subsampleThreads(new Threads::Thread[numThreads]),subsampleBarrier(numThreads+1),
currentNode(0),
numProcessedNodes(0)
{
/* Allocate the child color buffers: */
for(int i=0;i<8;++i)
childColorBuffers[i]=new Color[lpo.getMaxNumPointsPerNode()];
/* Write the color file's header: */
colorFile.setEndianness(Misc::LittleEndian);
LidarDataFileHeader dfh((unsigned int)(colorDataSize));
dfh.write(colorFile);
if(pointFileName!=0)
{
/* Open the point file: */
pointFile=fopen(pointFileName,"wt");
}
/* Start the worker threads: */
for(unsigned int i=0;i<numThreads;++i)
{
calcThreads[i].start(this,&PaulBunyan::calcThreadMethod,i);
subsampleThreads[i].start(this,&PaulBunyan::subsampleThreadMethod,i);
}
}
PaulBunyan::~PaulBunyan(void)
{
/* Shut down all threads: */
shutdownThreads=true;
calcBarrier.synchronize();
subsampleBarrier.synchronize();
for(unsigned int i=0;i<numThreads;++i)
{
calcThreads[i].join();
subsampleThreads[i].join();
}
delete[] calcThreads;
delete[] subsampleThreads;
delete[] colorBuffer;
for(int i=0;i<8;++i)
delete[] childColorBuffers[i];
if(pointFile!=0)
fclose(pointFile);
}
void PaulBunyan::operator()(LidarProcessOctree::Node& node,unsigned int nodeLevel)
{
currentNode=&node;
if(currentNode->getNumPoints()>0)
{
if(currentNode->isLeaf())
{
/* Wake up the calculation threads: */
calcBarrier.synchronize();
/* Wait for their completion: */
calcBarrier.synchronize();
}
else
{
/* Get pointers to the node's children and load their classified color arrays: */
for(int childIndex=0;childIndex<8;++childIndex)
{
currentChildren[childIndex]=lpo.getChild(currentNode,childIndex);
if(currentChildren[childIndex]->getNumPoints()>0)
{
colorFile.setReadPosAbs(LidarDataFileHeader::getFileSize()+colorDataSize*currentChildren[childIndex]->getDataOffset());
colorFile.read(childColorBuffers[childIndex],currentChildren[childIndex]->getNumPoints());
}
}
/* Wake up the subsampling threads: */
subsampleBarrier.synchronize();
/* Wait for their completion: */
subsampleBarrier.synchronize();
}
}
/* Write the node's colors to the color file: */
colorFile.setWritePosAbs(LidarDataFileHeader::getFileSize()+colorDataSize*currentNode->getDataOffset());
colorFile.write(colorBuffer,currentNode->getNumPoints());
/* Update the progress counter: */
++numProcessedNodes;
int percent=int((numProcessedNodes*100+lpo.getNumNodes()/2)/lpo.getNumNodes());
std::cout<<"\b\b\b\b"<<std::setw(3)<<percent<<"%"<<std::flush;
}
int main(int argc,char* argv[])
{
const char* lidarFileName=0;
int cacheSize=512;
unsigned int numThreads=1;
const char* colorFileName=0;
const char* groundPointFileName=0;
Scalar radius(1);
Scalar tolerance(0);
Scalar maxAngle(270);
for(int i=1;i<argc;++i)
{
if(argv[i][0]=='-')
{
if(strcasecmp(argv[i]+1,"cache")==0)
{
++i;
cacheSize=atoi(argv[i]);
}
else if(strcasecmp(argv[i]+1,"threads")==0)
{
++i;
numThreads=atoi(argv[i]);
}
else if(strcasecmp(argv[i]+1,"radius")==0)
{
++i;
radius=Scalar(atof(argv[i]));
}
else if(strcasecmp(argv[i]+1,"tolerance")==0)
{
++i;
tolerance=Scalar(atof(argv[i]));
}
else if(strcasecmp(argv[i]+1,"maxAngle")==0)
{
++i;
maxAngle=Math::rad(Scalar(atof(argv[i])));
}
}
else if(lidarFileName==0)
lidarFileName=argv[i];
else if(colorFileName==0)
colorFileName=argv[i];
else if(groundPointFileName==0)
groundPointFileName=argv[i];
}
if(lidarFileName==0)
{
std::cerr<<"No LiDAR file name provided"<<std::endl;
return 1;
}
if(colorFileName==0)
colorFileName="Colors";
/* Create a processing octree: */
LidarProcessOctree lpo(lidarFileName,size_t(cacheSize)*size_t(1024*1024));
/* Create an axe: */
Axe axe(radius,tolerance,maxAngle);
/* Hand the axe to Paul Bunyan: */
std::string lidarColorFileName=lidarFileName;
lidarColorFileName.push_back('/');
lidarColorFileName.append(colorFileName);
PaulBunyan paulBunyan(lpo,axe,lidarColorFileName.c_str(),groundPointFileName,numThreads);
std::cout<<"Wielding axe... 0%"<<std::flush;
lpo.processNodesPostfix(paulBunyan);
std::cout<<std::endl;
return 0;
}