forked from m-schuetz/SimLOD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlas2simlod.mjs
175 lines (135 loc) · 5.7 KB
/
las2simlod.mjs
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
// converts LAS point cloud files to XYZRGBA binary files.
// - Smallest las format with RGB data requires 26 bytes per point
// - The bin format stores points as 3xfloat and 4xuint8 => 16 bytes per point
// - To improve float precision, the point cloud is moved towards the origin. Coordinates start at 0.0
// - A small header stores the bounding box as min_x, min_y, min_z, max_x, max_y, max_z, for a total of 6 * 4 = 24 bytes
// <header, 24 bytes> <points, 16 byte each>
// [min_x, min_y, min_z, max_x, max_y, max_z][XYZRGBA, XYZRGBA, XYZRGBA, ...]
import {promises as fsp} from "fs";
class LasHeader{
constructor(){
this.versionMajor = 0;
this.versionMinor = 0;
this.headerSize = 0;
this.offsetToPointData = 0;
this.format = 0;
this.bytesPerPoint = 0;
this.numPoints = 0;
this.scale = {x: 0, y: 0, z: 0};
this.offset = {x: 0, y: 0, z: 0};
this.min = {x: 0, y: 0, z: 0};
this.max = {x: 0, y: 0, z: 0};
}
};
async function readHeader(file){
let handle = await fsp.open(file);
let buffer = Buffer.alloc(375);
await handle.read(buffer, 0, 375);
let header = new LasHeader();
header.versionMajor = buffer.readUint8(24);
header.versionMinor = buffer.readUint8(25);
header.headerSize = buffer.readUint16LE(94);
header.offsetToPointData = buffer.readUint32LE(96);
header.format = buffer.readUint8(104);
header.bytesPerPoint = buffer.readUint16LE(105);
if(header.versionMajor === 1 && header.versionMinor <= 2){
header.numPoints = buffer.readUint32LE(107);
}else{
header.numPoints = buffer.readUint64LE(247);
}
header.scale = {
x: buffer.readDoubleLE(131),
y: buffer.readDoubleLE(139),
z: buffer.readDoubleLE(147),
};
header.offset = {
x: buffer.readDoubleLE(155),
y: buffer.readDoubleLE(163),
z: buffer.readDoubleLE(171),
};
header.min = {
x: buffer.readDoubleLE(187),
y: buffer.readDoubleLE(203),
z: buffer.readDoubleLE(219),
};
header.max = {
x: buffer.readDoubleLE(179),
y: buffer.readDoubleLE(195),
z: buffer.readDoubleLE(211),
};
handle.close();
return header;
};
async function main(file, outPath){
const MAX_BATCH_SIZE = 1_000_000;
let header = await readHeader(file);
console.log(header);
let lines = [];
let outBufferHeader = Buffer.alloc(24);
let outBufferBatch = Buffer.alloc(16 * MAX_BATCH_SIZE);
let numPointsProcessed = 0;
let handle = await fsp.open(file);
let buffer = Buffer.alloc(MAX_BATCH_SIZE * header.bytesPerPoint);
let rgbOffset = 0;
if(header.format === 2) rgbOffset = 20;
if(header.format === 3) rgbOffset = 28;
if(header.format === 5) rgbOffset = 28;
if(header.format === 7) rgbOffset = 30;
// header storing min and max bounding box in 6 * 4 = 24 bytes
outBufferHeader.writeFloatLE(0.0, 0);
outBufferHeader.writeFloatLE(0.0, 4);
outBufferHeader.writeFloatLE(0.0, 8);
outBufferHeader.writeFloatLE(header.max.x - header.min.x, 12);
outBufferHeader.writeFloatLE(header.max.y - header.min.y, 16);
outBufferHeader.writeFloatLE(header.max.z - header.min.z, 20);
try{
await fsp.unlink(outPath);
}catch(e){}
await fsp.appendFile(outPath, outBufferHeader);
while(numPointsProcessed < header.numPoints){
let pointsLeft = header.numPoints - numPointsProcessed;
let batchSize = Math.min(pointsLeft, MAX_BATCH_SIZE);
let byteOffset = header.offsetToPointData + numPointsProcessed * header.bytesPerPoint;
let byteSize = batchSize * header.bytesPerPoint;
let outByteSize = batchSize * 16
if(outBufferBatch.byteLength !== outByteSize){
outBufferBatch = Buffer.alloc(outByteSize);
}
await handle.read(buffer, 0, byteSize, byteOffset);
for(let i = 0; i < batchSize; i++){
let X = buffer.readInt32LE(i * header.bytesPerPoint + 0);
let Y = buffer.readInt32LE(i * header.bytesPerPoint + 4);
let Z = buffer.readInt32LE(i * header.bytesPerPoint + 8);
let x = X * header.scale.x + header.offset.x - header.min.x;
let y = Y * header.scale.y + header.offset.y - header.min.y;
let z = Z * header.scale.z + header.offset.z - header.min.z;
let R = buffer.readUint16LE(i * header.bytesPerPoint + rgbOffset + 0);
let G = buffer.readUint16LE(i * header.bytesPerPoint + rgbOffset + 2);
let B = buffer.readUint16LE(i * header.bytesPerPoint + rgbOffset + 4);
let r = Math.floor(R > 255 ? R / 256 : R);
let g = Math.floor(G > 255 ? G / 256 : G);
let b = Math.floor(B > 255 ? B / 256 : B);
outBufferBatch.writeFloatLE(x, 16 * i + 0);
outBufferBatch.writeFloatLE(y, 16 * i + 4);
outBufferBatch.writeFloatLE(z, 16 * i + 8);
outBufferBatch.writeUint8(r, 16 * i + 12);
outBufferBatch.writeUint8(g, 16 * i + 13);
outBufferBatch.writeUint8(b, 16 * i + 14);
outBufferBatch.writeUint8(255, 16 * i + 15);
numPointsProcessed++;
}
await fsp.appendFile(outPath, outBufferBatch);
console.log(`points processed: ${numPointsProcessed.toLocaleString()}`);
}
// let csv = lines.join("\n");
// fsp.writeFile("E:/resources/pointclouds/simlod/test.csv", csv);
// fsp.writeFile(outPath, outBuffer);
// await fsp.appendFile(outPath, outBuffer);
}
// let file = "E:/resources/pointclouds/simlod/morro_bay.las";
// let outPath = "E:/resources/pointclouds/simlod/morro_bay.simlod";
// let file = "E:/resources/pointclouds/simlod/meroe.las";
// let outPath = "E:/resources/pointclouds/simlod/meroe.simlod";
let file = "E:/resources/pointclouds/simlod/endeavor_p2.las";
let outPath = "E:/resources/pointclouds/simlod/endeavor_p2.simlod";
main(file, outPath);