forked from KeckCAVES/LidarViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LidarSubtractor.cpp
322 lines (298 loc) · 10.2 KB
/
LidarSubtractor.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
/***********************************************************************
LidarSubtractor - Post-processing filter to subtract a (relatively
small) point set from a LiDAR data set and create a new LiDAR data set.
Copyright (c) 2009-2013 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 <stdlib.h>
#include <stdio.h>
#include <vector>
#include <string>
#include <iostream>
#include <Misc/StandardValueCoders.h>
#include <Misc/ConfigurationFile.h>
#include <IO/File.h>
#include <IO/OpenFile.h>
#include <Geometry/ArrayKdTree.h>
#include "LidarTypes.h"
#include "LidarProcessOctree.h"
#include "PointAccumulator.h"
#include "LidarOctreeCreator.h"
#include "SubtractorHelper.h"
class NodePointSubtractor
{
/* Elements: */
private:
LidarProcessOctree& basePoints;
Geometry::ArrayKdTree<Point>& subtractPoints;
Scalar epsilon;
PointAccumulator& pa;
/* Constructors and destructors: */
public:
NodePointSubtractor(LidarProcessOctree& sBasePoints,Geometry::ArrayKdTree<Point>& sSubtractPoints,Scalar sEpsilon,PointAccumulator& sPa)
:basePoints(sBasePoints),subtractPoints(sSubtractPoints),
epsilon(sEpsilon),
pa(sPa)
{
}
/* Methods: */
void operator()(LidarProcessOctree::Node& node,unsigned int nodeLevel)
{
/* Check if this node is a leaf: */
if(node.isLeaf())
{
/* Look for each node's point in the subtract set: */
for(unsigned int i=0;i<node.getNumPoints();++i)
{
MatchingPointFinder mpf(node[i],epsilon);
subtractPoints.traverseTreeDirected(mpf);
if(!mpf.isFound())
{
/* Point has no match in subtract set; add it to point accumulator: */
pa.addPoint(PointAccumulator::Point(node[i].getComponents()),PointAccumulator::Color(node[i].value.getRgba()));
}
}
}
}
};
int main(int argc,char* argv[])
{
/* Set default values for all parameters: */
unsigned int memoryCacheSize=512;
unsigned int tempOctreeMaxNumPointsPerNode=4096;
std::string tempOctreeFileNameTemplate="/tmp/LidarPreprocessorTempOctree";
unsigned int maxNumPointsPerNode=4096;
int numThreads=1;
std::string tempPointFileNameTemplate="/tmp/LidarPreprocessorTempPoints";
try
{
/* Open LidarViewer's configuration file: */
Misc::ConfigurationFile configFile(LIDARVIEWER_CONFIGFILENAME);
Misc::ConfigurationFileSection cfg=configFile.getSection("/LidarPreprocessor");
/* Override program settings from configuration file: */
memoryCacheSize=cfg.retrieveValue<unsigned int>("./memoryCacheSize",memoryCacheSize);
tempOctreeMaxNumPointsPerNode=cfg.retrieveValue<unsigned int>("./tempOctreeMaxNumPointsPerNode",tempOctreeMaxNumPointsPerNode);
tempOctreeFileNameTemplate=cfg.retrieveValue<std::string>("./tempOctreeFileNameTemplate",tempOctreeFileNameTemplate);
maxNumPointsPerNode=cfg.retrieveValue<unsigned int>("./maxNumPointsPerNode",maxNumPointsPerNode);
numThreads=cfg.retrieveValue<int>("./numThreads",numThreads);
tempPointFileNameTemplate=cfg.retrieveValue<std::string>("./tempPointFileNameTemplate",tempPointFileNameTemplate);
}
catch(std::runtime_error err)
{
/* Just ignore the error */
}
/* Parse the command line and load the input files: */
const char* baseFileName=0; // Name of the base LiDAR file
unsigned baseMemoryCacheSize=64; // Memory cache size for base octree in MB
const char* subtractFileName=0; // Name of ASCII or binary file containing the point set to subtract
int asciiColumnIndices[3]={0,1,2}; // Column indices of x, y, z point components in ASCII file
Scalar epsilon=Scalar(1.0e-7); // Maximum match point distance
PointAccumulator::Vector pointOffset=PointAccumulator::Vector::zero; // Offset vector added to points during octree creation
const char* outputFileName=0; // Name of resulting LiDAR octree file
bool haveOffset=false; // Flag whether an explicit point offset was specified on the command line
bool offsetSubtractPoints=true; // Flag whether to offset subtraction points to native octree coordinates
for(int i=1;i<argc;++i)
{
if(argv[i][0]=='-')
{
if(strcasecmp(argv[i]+1,"o")==0)
{
++i;
if(i<argc)
outputFileName=argv[i];
else
std::cerr<<"Dangling -o flag on command line"<<std::endl;
}
else if(strcasecmp(argv[i]+1,"np")==0)
{
++i;
if(i<argc)
maxNumPointsPerNode=(unsigned int)(atoi(argv[i]));
else
std::cerr<<"Dangling -np flag on command line"<<std::endl;
}
else if(strcasecmp(argv[i]+1,"nt")==0)
{
++i;
if(i<argc)
numThreads=atoi(argv[i]);
else
std::cerr<<"Dangling -nt flag on command line"<<std::endl;
}
else if(strcasecmp(argv[i]+1,"ooc")==0)
{
++i;
if(i<argc)
memoryCacheSize=(unsigned int)(atoi(argv[i]));
else
std::cerr<<"Dangling -ooc flag on command line"<<std::endl;
}
else if(strcasecmp(argv[i]+1,"to")==0)
{
++i;
if(i<argc)
tempOctreeFileNameTemplate=argv[i];
else
std::cerr<<"Dangling -to flag on command line"<<std::endl;
}
else if(strcasecmp(argv[i]+1,"tp")==0)
{
++i;
if(i<argc)
tempPointFileNameTemplate=argv[i];
else
std::cerr<<"Dangling -tp flag on command line"<<std::endl;
}
else if(strcasecmp(argv[i]+1,"lasOffset")==0)
{
if(i+3<argc)
{
for(int j=0;j<3;++j)
{
++i;
pointOffset[j]=atof(argv[i]);
}
haveOffset=true;
}
else
std::cerr<<"Dangling -lasOffset flag on command line"<<std::endl;
}
else if(strcasecmp(argv[i]+1,"lasOffsetFile")==0)
{
++i;
if(i<argc)
{
/* Read the point offset from a binary file: */
try
{
IO::FilePtr offsetFile=IO::openFile(argv[i]);
offsetFile->setEndianness(Misc::LittleEndian);
offsetFile->read(pointOffset.getComponents(),3);
haveOffset=true;
}
catch(std::runtime_error err)
{
/* Print a warning and carry on: */
std::cerr<<"Ignoring lasOffsetFile argument due to error "<<err.what()<<" when reading file "<<argv[i]<<std::endl;
}
}
else
std::cerr<<"Dangling -lasOffsetFile flag on command line"<<std::endl;
}
else if(strcasecmp(argv[i]+1,"noOffset")==0)
offsetSubtractPoints=false;
else if(strcasecmp(argv[i]+1,"eps")==0)
{
++i;
if(i<argc)
epsilon=Scalar(atof(argv[i]));
else
std::cerr<<"Dangling -eps flag on command line"<<std::endl;
}
else if(strcasecmp(argv[i]+1,"booc")==0)
{
++i;
if(i<argc)
baseMemoryCacheSize=(unsigned int)(atoi(argv[i]));
else
std::cerr<<"Dangling -booc flag on command line"<<std::endl;
}
else if(strcasecmp(argv[i]+1,"columns")==0)
{
if(i+3<argc)
{
for(int j=0;j<3;++j)
{
++i;
asciiColumnIndices[j]=atoi(argv[i]);
}
}
else
std::cerr<<"Dangling -columns flag on command line"<<std::endl;
}
}
else if(baseFileName==0)
{
/* Store the LiDAR file name: */
baseFileName=argv[i];
}
else if(subtractFileName==0)
{
/* Store the subtraction file name: */
subtractFileName=argv[i];
}
else
std::cerr<<"Ignoring command line argument "<<argv[i]<<std::endl;
}
/* Open the base LiDAR file: */
LidarProcessOctree* basePoints;
try
{
/* Create a processing octree: */
basePoints=new LidarProcessOctree(baseFileName,size_t(baseMemoryCacheSize)*size_t(1024*1024));
}
catch(std::runtime_error err)
{
std::cerr<<"Cannot open LiDAR file "<<baseFileName<<" due to exception "<<err.what()<<"; terminating"<<std::endl;
return 1;
}
/* Create a point accumulator: */
PointAccumulator pa;
pa.setMemorySize(memoryCacheSize,tempOctreeMaxNumPointsPerNode);
pa.setTempOctreeFileNameTemplate(tempOctreeFileNameTemplate+"XXXXXX");
if(!haveOffset)
{
/* Use the base point set's point offset for the resulting file: */
pointOffset=-basePoints->getOffset();
}
else
{
PointAccumulator::Vector totalOffset=pointOffset-PointAccumulator::Vector(basePoints->getOffset());
if(totalOffset!=PointAccumulator::Vector::zero)
pa.setPointOffset(totalOffset);
}
/* Load the subtraction point set into a kd-tree: */
PointKdTree* subtractPointTree=loadSubtractSet(subtractFileName,offsetSubtractPoints?Geometry::Vector<double,3>(basePoints->getOffset()):Geometry::Vector<double,3>::zero);
if(subtractPointTree==0)
return 1;
/* Process the base point set: */
{
std::cout<<"Subtracting points..."<<std::flush;
NodePointSubtractor nps(*basePoints,*subtractPointTree,epsilon,pa);
basePoints->processNodesPostfix(nps);
pa.finishReading();
std::cout<<" done"<<std::endl;
}
/* Clear input data structures: */
delete basePoints;
delete subtractPointTree;
/* Construct an octree with less than maxPointsPerNode points per leaf: */
LidarOctreeCreator tree(pa.getMaxNumCacheablePoints(),maxNumPointsPerNode,numThreads,pa.getTempOctrees(),tempPointFileNameTemplate+"XXXXXX");
/* Delete the temporary point octrees: */
pa.deleteTempOctrees();
/* Write the octree structure and data to the destination LiDAR file: */
tree.write(size_t(memoryCacheSize)*size_t(1024*1024),outputFileName);
/* Check if a point offset was defined: */
if(pointOffset!=PointAccumulator::Vector::zero)
{
/* Write the point offsets to an offset file: */
std::string offsetFileName=outputFileName;
offsetFileName.append("/Offset");
IO::FilePtr offsetFile(IO::openFile(offsetFileName.c_str(),IO::File::WriteOnly));
offsetFile->setEndianness(Misc::LittleEndian);
offsetFile->write(pointOffset.getComponents(),3);
}
return 0;
}