-
Notifications
You must be signed in to change notification settings - Fork 5
/
las.cpp
394 lines (368 loc) · 9.86 KB
/
las.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
/******************************************************/
/* */
/* las.cpp - laser point cloud files */
/* */
/******************************************************/
/* Copyright 2019-2021 Pierre Abbat.
* This file is part of PerfectTIN.
*
* PerfectTIN is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* PerfectTIN 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 and Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* and Lesser General Public License along with PerfectTIN. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <cstring>
#include "las.h"
#include "binio.h"
#include "octagon.h"
#include "angle.h"
#include "cloud.h"
const int MASK_GPSTIME=0x7fa;
const int MASK_RGB=0x5ac;
const int MASK_NIR=0x500;
const int MASK_WAVE=0x630;
using namespace std;
string read16(istream &file)
{
char buf[24];
memset(buf,0,24);
file.read(buf,16);
return string(buf);
}
string read32(ifstream &file)
{
char buf[40];
memset(buf,0,40);
file.read(buf,32);
return string(buf);
}
VariableLengthRecord::VariableLengthRecord()
{
reserved=recordId=0;
}
void VariableLengthRecord::setUserId(string uid)
{
userId=uid;
}
void VariableLengthRecord::setRecordId(int rid)
{
recordId=rid;
}
void VariableLengthRecord::setDescription(string desc)
{
description=desc;
}
void VariableLengthRecord::setData(string dat)
{
data=dat;
}
string VariableLengthRecord::getUserId()
{
return userId;
}
int VariableLengthRecord::getRecordId()
{
return recordId;
}
string VariableLengthRecord::getDescription()
{
return description;
}
string VariableLengthRecord::getData()
{
return data;
}
LasHeader::LasHeader()
{
lasfile=nullptr;
versionMajor=versionMinor=0;
}
LasHeader::~LasHeader()
{
close();
}
void LasHeader::open(std::string fileName)
{
int magicBytes;
int headerSize;
int i;
size_t total;
unsigned int legacyNPoints[6];
int whichNPoints=15;
if (lasfile)
close();
lasfile=new ifstream(fileName,ios::binary);
classHisto.clear();
magicBytes=readbeint(*lasfile);
if (magicBytes==0x4c415346)
{
sourceId=readleshort(*lasfile);
globalEncoding=readleshort(*lasfile);
guid1=readleint(*lasfile);
guid2=readleshort(*lasfile);
guid3=readleshort(*lasfile);
lasfile->read(guid4,8);
versionMajor=lasfile->get();
versionMinor=lasfile->get();
systemId=read32(*lasfile);
softwareName=read32(*lasfile);
creationDay=readleshort(*lasfile);
creationYear=readleshort(*lasfile);
headerSize=readleshort(*lasfile);
pointOffset=readleint(*lasfile);
nVariableLength=readleint(*lasfile);
pointFormat=lasfile->get();
pointLength=readleshort(*lasfile);
for (i=0;i<6;i++)
legacyNPoints[i]=readleint(*lasfile);
xScale=readledouble(*lasfile);
yScale=readledouble(*lasfile);
zScale=readledouble(*lasfile);
xOffset=readledouble(*lasfile);
yOffset=readledouble(*lasfile);
zOffset=readledouble(*lasfile);
maxX=readledouble(*lasfile);
minX=readledouble(*lasfile);
maxY=readledouble(*lasfile);
minY=readledouble(*lasfile);
maxZ=readledouble(*lasfile);
minZ=readledouble(*lasfile);
// Here ends the LAS 1.2 header (length 0xe3). The LAS 1.4 header (length 0x177) continues.
if (headerSize>0xe3)
{
startWaveform=readlelong(*lasfile);
startExtendedVariableLength=readlelong(*lasfile);
nExtendedVariableLength=readleint(*lasfile);
for (i=0;i<16;i++)
nPoints[i]=readlelong(*lasfile);
}
else
{
startWaveform=startExtendedVariableLength=nExtendedVariableLength=0;
for (i=0;i<16;i++)
nPoints[i]=0;
}
/* Check the numbers of points by return. They should add up to the total:
* 986 377 233 144 89 55 34 21 13 8 5 3 2 1 1 0
* In some files, the total (nPoints[0], legacyNPoints[0]) is nonzero,
* but the numbers of points by return are all 0. This is invalid, but
* seen in the wild in files produced by photogrammetry.
*/
for (i=1,total=0;i<6;i++)
{
total+=legacyNPoints[i];
if (legacyNPoints[i]>legacyNPoints[0])
whichNPoints&=~5;
}
if (total!=legacyNPoints[0])
whichNPoints&=~1;
if (total!=0 || legacyNPoints[0]==0)
whichNPoints&=~4;
for (i=1,total=0;i<16;i++)
{
total+=nPoints[i];
if (nPoints[i]>nPoints[0])
whichNPoints&=~10;
}
if (total!=nPoints[0])
whichNPoints&=~2;
if (total!=0 || nPoints[0]==0)
whichNPoints&=~8;
for (i=0;i<6;i++)
{
if (legacyNPoints[i]!=nPoints[i] && legacyNPoints[i]!=0)
whichNPoints&=~10;
if (nPoints[i]!=legacyNPoints[i] && nPoints[i]!=0)
whichNPoints&=~5;
}
if (whichNPoints>0 && (whichNPoints&3)==0)
{
cerr<<"Number of points by return are all 0. Setting first return to all points.\n";
nPoints[1]=nPoints[0];
legacyNPoints[1]=legacyNPoints[0];
}
if (whichNPoints==1 || whichNPoints==4)
for (i=0;i<6;i++)
nPoints[i]=legacyNPoints[i];
if (whichNPoints==0)
for (i=0;i<6;i++)
nPoints[i]=0;
if (pointLength==0)
versionMajor=versionMinor=nPoints[0]=0;
readPos=headerSize;
extReadPos=startExtendedVariableLength;
}
else // file does not begin with "LASF"
versionMajor=versionMinor=nPoints[0]=0;
}
bool LasHeader::isValid()
{
return versionMajor>0 && versionMinor>0;
}
void LasHeader::close()
{
delete(lasfile);
lasfile=nullptr;
}
size_t LasHeader::numberPoints(int r)
{
return nPoints[r];
}
size_t LasHeader::numberRecords()
{
return nVariableLength;
}
size_t LasHeader::numberExtRecords()
{
return nExtendedVariableLength;
}
LasPoint LasHeader::readPoint(size_t num)
{
LasPoint ret;
int xInt,yInt,zInt;
int temp;
lasfile->seekg(num*pointLength+pointOffset,ios_base::beg);
xInt=readleint(*lasfile);
yInt=readleint(*lasfile);
zInt=readleint(*lasfile);
ret.intensity=readleshort(*lasfile);
if (pointFormat<6)
{
temp=lasfile->get();
ret.returnNum=temp&7;
ret.nReturns=(temp>>3)&7;
ret.scanDirection=(temp>>6)&1;
ret.edgeLine=(temp>>7)&1;
ret.classification=(unsigned char)lasfile->get();
ret.scanAngle=degtobin((signed char)lasfile->get());
ret.userData=(unsigned char)lasfile->get();
ret.pointSource=readleshort(*lasfile);
}
else // formats 6 through 10
{
temp=lasfile->get();
ret.returnNum=temp&15;
ret.nReturns=(temp>>4)&15;
temp=lasfile->get();
ret.classificationFlags=temp&15;
ret.scannerChannel=(temp>>4)&3;
ret.scanDirection=(temp>>6)&1;
ret.edgeLine=(temp>>7)&1;
ret.classification=(unsigned char)lasfile->get();
ret.userData=(unsigned char)lasfile->get();
ret.scanAngle=degtobin(readleshort(*lasfile)*0.006);
ret.pointSource=readleshort(*lasfile);
}
if ((1<<pointFormat)&MASK_GPSTIME) // 10-5, 4, 3, or 1
ret.gpsTime=readledouble(*lasfile);
if ((1<<pointFormat)&MASK_RGB) // 10, 8, 7, 5, 3, or 2
{
ret.red=readleshort(*lasfile);
ret.green=readleshort(*lasfile);
ret.blue=readleshort(*lasfile);
}
if ((1<<pointFormat)&MASK_NIR) // 10 or 8
ret.nir=readleshort(*lasfile);
if ((1<<pointFormat)&MASK_WAVE) // 10, 9, 5 or 4
{
ret.waveIndex=(unsigned char)lasfile->get();
ret.waveformOffset=readlelong(*lasfile);
ret.waveformSize=readleint(*lasfile);
ret.waveformTime=readlefloat(*lasfile);
ret.xDir=readlefloat(*lasfile);
ret.yDir=readlefloat(*lasfile);
ret.zDir=readlefloat(*lasfile);
}
ret.location=xyz(xOffset+xScale*xInt,yOffset+yScale*yInt,zOffset+zScale*zInt);
if (!lasfile->good())
throw -1;
classHisto[ret.classification]++;
return ret;
}
VariableLengthRecord LasHeader::readRecord()
{
VariableLengthRecord ret;
size_t length,i;
int ch;
lasfile->seekg(readPos,ios::beg);
ret.reserved=readleshort(*lasfile);
ret.userId=read16(*lasfile);
ret.recordId=readleshort(*lasfile);
length=(unsigned short)readleshort(*lasfile);
ret.description=read32(*lasfile);
for (i=0;i<length;i++)
{
ch=lasfile->get();
if (ch>=0)
ret.data+=(char)ch;
else
{
ret.reserved=-1;
break;
}
}
readPos=lasfile->tellg();
if (!lasfile->good())
throw -1;
return ret;
}
VariableLengthRecord LasHeader::readExtRecord()
{
VariableLengthRecord ret;
size_t length,i;
int ch;
lasfile->seekg(extReadPos,ios::beg);
ret.reserved=readleshort(*lasfile);
ret.userId=read16(*lasfile);
ret.recordId=readleshort(*lasfile);
length=readlelong(*lasfile);
ret.description=read32(*lasfile);
for (i=0;i<length;i++)
{
ch=lasfile->get();
if (ch>=0)
ret.data+=(char)ch;
else
{
ret.reserved=-1;
break;
}
}
extReadPos=lasfile->tellg();
if (!lasfile->good())
throw -1;
return ret;
}
void readLas(string fileName,int flags)
{
size_t i;
LasHeader header;
LasPoint pnt;
vector<VariableLengthRecord> records;
header.open(fileName);
for (i=0;i<header.numberRecords();i++)
records.push_back(header.readRecord());
for (i=0;i<header.numberExtRecords();i++)
records.push_back(header.readExtRecord());
cout<<"File contains "<<records.size()<<" variable-length records\n";
//for (i=0;i<records.size();i++)
//if (records[i].getRecordId()==2112) // WKT
//cout<<records[i].getData();
//cout<<"File contains "<<header.numberPoints()<<" dots\n";
for (i=0;i<header.numberPoints();i++)
{
pnt=header.readPoint(i);
if (pnt.classification==2 || (flags&4)==0) // Read only ground points if bit 2 is set
cloud.push_back(pnt.location);
}
}