forked from KeckCAVES/LidarViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LidarExporter.cpp
278 lines (255 loc) · 7.22 KB
/
LidarExporter.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
/***********************************************************************
LidarExporter - Utility to export points from LiDAR files to ASCII files
in xyzrgb format.
Copyright (c) 2010-2012 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 <Misc/File.h>
#include <IO/SeekableFile.h>
#include <IO/OpenFile.h>
#include "LidarTypes.h"
#include "LidarProcessOctree.h"
class PointSaver
{
/* Elements: */
private:
LidarProcessOctree::OffsetVector offset;
Misc::File resultFile;
size_t numPoints;
/* Constructors and destructors: */
public:
PointSaver(const LidarProcessOctree::OffsetVector& sOffset,const char* resultFileName)
:offset(sOffset),
resultFile(resultFileName,"wb"),
numPoints(0)
{
}
/* Methods: */
void operator()(const LidarPoint& point)
{
double pos[3];
for(int i=0;i<3;++i)
pos[i]=double(point[i])+double(offset[i]);
fprintf(resultFile.getFilePtr(),"%.12g %.12g %.12g %u %u %u\n",pos[0],pos[1],pos[2],point.value[0],point.value[1],point.value[2]);
++numPoints;
}
size_t getNumPoints(void) const
{
return 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;
}
size_t getNumPoints(void) const
{
return numPoints;
}
};
int main(int argc,char* argv[])
{
/* Process command line: */
const char* lidarFile=0;
bool writeLas=false;
const char* outputFile=0;
int cacheSize=512;
bool haveBox=false;
double box[6];
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,"box")==0)
{
haveBox=true;
for(int j=0;j<6;++j)
{
++i;
box[j]=atof(argv[i]);
}
}
else if(strcasecmp(argv[i]+1,"las")==0)
writeLas=true;
else
std::cerr<<"Ignoring command line option "<<argv[i]<<std::endl;
}
else if(lidarFile==0)
lidarFile=argv[i];
else if(outputFile==0)
outputFile=argv[i];
else
std::cerr<<"Ignoring command line argument "<<argv[i]<<std::endl;
}
if(lidarFile==0||outputFile==0)
{
std::cerr<<"Usage: "<<argv[0]<<" [-cache <cache size>] [-box <box spec>] <LiDAR file name> [-las] <output file name>"<<std::endl;
std::cerr<<" -cache <cache size> sets the size of the LiDAR memory cache in MB (default: 512)"<<std::endl;
std::cerr<<" -box <box spec> specifies a box in source coordinates from which to export points (default: export all points)"<<std::endl;
std::cerr<<" box specification: <min_x> <min_y> <min_z> <max_x> <max_y> <max_z>"<<std::endl;
std::cerr<<" -las requests to write exported points into a LAS-like file (default: write into ASCII file)"<<std::endl;
return 1;
}
/* Open the input and output files: */
LidarProcessOctree lpo(lidarFile,size_t(cacheSize)*1024*1024);
Box lbox;
if(haveBox)
{
/* Transform the box to LiDAR coordinates: */
for(int i=0;i<3;++i)
{
lbox.min[i]=Box::Scalar(box[i]-lpo.getOffset()[i]);
lbox.max[i]=Box::Scalar(box[3+i]-lpo.getOffset()[i]);
}
}
if(writeLas)
{
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 ps(outputFile,scale,offset);
/* Process the LiDAR file: */
if(haveBox)
{
/* Exports points from inside the box: */
lpo.processPointsInBox(lbox,ps);
}
else
{
/* Export all points: */
lpo.processPoints(ps);
}
/* Print statistics: */
std::cout<<ps.getNumPoints()<<" points saved"<<std::endl;
}
else
{
PointSaver ps(lpo.getOffset(),outputFile);
/* Process the LiDAR file: */
if(haveBox)
{
/* Exports points from inside the box: */
lpo.processPointsInBox(lbox,ps);
}
else
{
/* Export all points: */
lpo.processPoints(ps);
}
/* Print statistics: */
std::cout<<ps.getNumPoints()<<" points saved"<<std::endl;
}
return 0;
}