zhongrj
2025-11-25 b89962006164a462404b79a738bee8cbb6d7fe7e
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
// laslaz.js
// LAS/LAZ loading
//
 
//var common = require("./common"),
//    Promise = require("bluebird");
 
(function(scope) {
    "use strict";
 
    var pointFormatReaders = {
        0: function(dv) {
            return {
                "position": [ dv.getInt32(0, true), dv.getInt32(4, true), dv.getInt32(8, true)],
                "intensity": dv.getUint16(12, true),
                "classification": dv.getUint8(16, true)
            };
        },
        1: function(dv) {
            return {
                "position": [ dv.getInt32(0, true), dv.getInt32(4, true), dv.getInt32(8, true)],
                "intensity": dv.getUint16(12, true),
                "classification": dv.getUint8(16, true)
            };
        },
        2: function(dv) {
            return {
                "position": [ dv.getInt32(0, true), dv.getInt32(4, true), dv.getInt32(8, true)],
                "intensity": dv.getUint16(12, true),
                "classification": dv.getUint8(16, true),
                "color": [dv.getUint16(20, true), dv.getUint16(22, true), dv.getUint16(24, true)]
            };
        },
        3: function(dv) {
            return {
                "position": [ dv.getInt32(0, true), dv.getInt32(4, true), dv.getInt32(8, true)],
                "intensity": dv.getUint16(12, true),
                "classification": dv.getUint8(16, true),
                "color": [dv.getUint16(28, true), dv.getUint16(30, true), dv.getUint16(32, true)]
            };
        }
    };
 
    function readAs(buf, Type, offset, count) {
        count = (count === undefined || count === 0 ? 1 : count);
        var sub = buf.slice(offset, offset + Type.BYTES_PER_ELEMENT * count);
 
        var r = new Type(sub);
        if (count === undefined || count === 1)
            return r[0];
 
        var ret = [];
        for (var i = 0 ; i < count ; i ++) {
            ret.push(r[i]);
        }
 
        return ret;
    }
 
    function parseLASHeader(arraybuffer) {
        var o = {};
 
        o.pointsOffset = readAs(arraybuffer, Uint32Array, 32*3);
        o.pointsFormatId = readAs(arraybuffer, Uint8Array, 32*3+8);
        o.pointsStructSize = readAs(arraybuffer, Uint16Array, 32*3+8+1);
        o.pointsCount = readAs(arraybuffer, Uint32Array, 32*3 + 11);
 
 
        var start = 32*3 + 35;
        o.scale = readAs(arraybuffer, Float64Array, start, 3); start += 24; // 8*3
        o.offset = readAs(arraybuffer, Float64Array, start, 3); start += 24;
 
 
 
        var bounds = readAs(arraybuffer, Float64Array, start, 6); start += 48; // 8*6;
        o.maxs = [bounds[0], bounds[2], bounds[4]];
        o.mins = [bounds[1], bounds[3], bounds[5]];
 
        return o;
    }
 
    var msgIndex = 0;
    var waitHandlers = {};
 
    // This method is scope-wide since the nacl module uses this fuction to notify
    // us of events
    scope.handleMessage = function(message_event) {
        var msg = message_event.data;
        var resolver = waitHandlers[msg.id];
        delete waitHandlers[msg.id];
 
        // call the callback in a separate context, make sure we've cleaned our
        // state out before the callback is invoked since it may queue more doExchanges
        setTimeout(function() { 
            if (msg.error)
                return resolver.reject(new Error(msg.message || "Unknown Error"));
 
            if (msg.hasOwnProperty('count') && msg.hasOwnProperty('hasMoreData')) {
                return resolver.resolve({
                    buffer: msg.result,
                    count: msg.count,
                    hasMoreData: msg.hasMoreData});
            }
 
            resolver.resolve(msg.result);
        }, 0);
    };
 
    var doDataExchange = function(cmd, callback) {
        cmd.id = msgIndex.toString();
        msgIndex ++;
 
        var resolver = Promise.defer();
        waitHandlers[cmd.id] = resolver;
 
        nacl_module.postMessage(cmd);
 
        return resolver.promise.cancellable();
    };
 
    // LAS Loader
    // Loads uncompressed files
    //
    var LASLoader = function(arraybuffer) {
        this.arraybuffer = arraybuffer;
    };
 
    LASLoader.prototype.open = function() {
        // nothing needs to be done to open this file
        //
        this.readOffset = 0;
        return new Promise(function(res, rej) {
            setTimeout(res, 0);
        });
    };
 
    LASLoader.prototype.getHeader = function() {
        var o = this;
 
        return new Promise(function(res, rej) {
            setTimeout(function() {
                o.header = parseLASHeader(o.arraybuffer);
                res(o.header);
            }, 0);
        });
    };
 
    LASLoader.prototype.readData = function(count, offset, skip) {
        var o = this;
 
        return new Promise(function(res, rej) {
            setTimeout(function() {
                if (!o.header)
                    return rej(new Error("Cannot start reading data till a header request is issued"));
 
                var start;
                if (skip <= 1) {
                    count = Math.min(count, o.header.pointsCount - o.readOffset);
                    start = o.header.pointsOffset + o.readOffset * o.header.pointsStructSize;
                    var end = start + count * o.header.pointsStructSize;
                    res({
                        buffer: o.arraybuffer.slice(start, end),
                        count: count,
                        hasMoreData: o.readOffset + count < o.header.pointsCount});
                    o.readOffset += count;
                }
                else {
                    var pointsToRead = Math.min(count * skip, o.header.pointsCount - o.readOffset);
                    var bufferSize = Math.ceil(pointsToRead / skip);
                    var pointsRead = 0;
 
                    var buf = new Uint8Array(bufferSize * o.header.pointsStructSize);
                    for (var i = 0 ; i < pointsToRead ; i ++) {
                        if (i % skip === 0) {
                            start = o.header.pointsOffset + o.readOffset * o.header.pointsStructSize;
                            var src = new Uint8Array(o.arraybuffer, start, o.header.pointsStructSize);
 
                            buf.set(src, pointsRead * o.header.pointsStructSize);
                            pointsRead ++;
                        }
 
                        o.readOffset ++;
                    }
 
                    res({
                        buffer: buf.buffer,
                        count: pointsRead,
                        hasMoreData: o.readOffset < o.header.pointsCount
                    });
                }
            }, 0);
        });
    };
 
    LASLoader.prototype.close = function() {
        var o = this;
        return new Promise(function(res, rej) {
            o.arraybuffer = null;
            setTimeout(res, 0);
        });
    };
 
    // LAZ Loader
    // Uses NaCL module to load LAZ files
    //
    var LAZLoader = function(arraybuffer) {
        this.arraybuffer = arraybuffer;
        
        let workerPath = Potree.scriptPath + "/workers/LASLAZWorker.js";
        this.ww = Potree.workerPool.getWorker(workerPath);
 
        this.nextCB = null;
        var o = this;
 
        this.ww.onmessage = function(e) {
            if (o.nextCB !== null) {
                o.nextCB(e.data);
                o.nextCB = null;
            }
        };
 
        this.dorr = function(req, cb) {
            o.nextCB = cb;
            o.ww.postMessage(req);
        };
    };
 
    LAZLoader.prototype.open = function() {
 
        // nothing needs to be done to open this file
        //
        var o = this;
        return new Promise(function(res, rej) {
            o.dorr({type:"open", arraybuffer: o.arraybuffer}, function(r) {
                if (r.status !== 1)
                    return rej(new Error("Failed to open file"));
 
                res(true);
            });
        });
    };
 
    LAZLoader.prototype.getHeader = function() {
        var o = this;
 
        return new Promise(function(res, rej) {
            o.dorr({type:'header'}, function(r) {
                if (r.status !== 1)
                    return rej(new Error("Failed to get header"));
 
                res(r.header);
            });
        });
    };
 
    LAZLoader.prototype.readData = function(count, offset, skip) {
        var o = this;
 
        return new Promise(function(res, rej) {
            o.dorr({type:'read', count: count, offset: offset, skip: skip}, function(r) {
                if (r.status !== 1)
                    return rej(new Error("Failed to read data"));
                res({
                    buffer: r.buffer,
                    count: r.count,
                    hasMoreData: r.hasMoreData
                });
            });
        });
    };
 
    LAZLoader.prototype.close = function() {
        var o = this;
 
        return new Promise(function(res, rej) {
            o.dorr({type:'close'}, function(r) {
                let workerPath = Potree.scriptPath + "/workers/LASLAZWorker.js";
                Potree.workerPool.returnWorker(workerPath, o.ww);
            
                if (r.status !== 1)
                    return rej(new Error("Failed to close file"));
 
                res(true);
            });
        });
    };
 
    // A single consistent interface for loading LAS/LAZ files
    var LASFile = function(arraybuffer) {
        this.arraybuffer = arraybuffer;
 
        this.determineVersion();
        if (this.version > 12)
            throw new Error("Only file versions <= 1.2 are supported at this time");
 
        this.determineFormat();
        if (pointFormatReaders[this.formatId] === undefined)
            throw new Error("The point format ID is not supported");
 
        this.loader = this.isCompressed ?
            new LAZLoader(this.arraybuffer) :
            new LASLoader(this.arraybuffer);
    };
 
    LASFile.prototype.determineFormat = function() {
        var formatId = readAs(this.arraybuffer, Uint8Array, 32*3+8);
        var bit_7 = (formatId & 0x80) >> 7;
        var bit_6 = (formatId & 0x40) >> 6;
 
        if (bit_7 === 1 && bit_6 === 1)
            throw new Error("Old style compression not supported");
 
        this.formatId = formatId & 0x3f;
        this.isCompressed = (bit_7 === 1 || bit_6 === 1);
    };
 
    LASFile.prototype.determineVersion = function() {
        var ver = new Int8Array(this.arraybuffer, 24, 2);
        this.version = ver[0] * 10 + ver[1];
        this.versionAsString = ver[0] + "." + ver[1];
    };
 
    LASFile.prototype.open = function() {
        return this.loader.open();
    };
 
    LASFile.prototype.getHeader = function() {
        return this.loader.getHeader();
    };
 
    LASFile.prototype.readData = function(count, start, skip) {
        return this.loader.readData(count, start, skip);
    };
 
    LASFile.prototype.close = function() {
        return this.loader.close();
    };
 
    // Decodes LAS records into points
    //
    var LASDecoder = function(buffer, pointFormatID, pointSize, pointsCount, scale, offset, mins, maxs) {
        this.arrayb = buffer;
        this.decoder = pointFormatReaders[pointFormatID];
        this.pointsCount = pointsCount;
        this.pointSize = pointSize;
        this.scale = scale;
        this.offset = offset;
        this.mins = mins;
        this.maxs = maxs;
    };
 
    LASDecoder.prototype.getPoint = function(index) {
        if (index < 0 || index >= this.pointsCount)
            throw new Error("Point index out of range");
 
        var dv = new DataView(this.arrayb, index * this.pointSize, this.pointSize);
        return this.decoder(dv);
    };
 
    // NACL Module support
    // Called by the common.js module.
    //
    //window.startNaCl = function(name, tc, config, width, height) {
    //    // check browser support for nacl
    //    //
    //    if(!common.browserSupportsNaCl()) {
    //        return $.event.trigger({
    //            type: "plasio.nacl.error",
    //            message: "NaCl support is not available"
    //        });
    //    }
 
    //    navigator.webkitPersistentStorage.requestQuota(2048 * 2048, function(bytes) {
    //        common.updateStatus(
    //            'Allocated ' + bytes + ' bytes of persistant storage.');
    //            common.attachDefaultListeners();
    //            common.createNaClModule(name, tc, config, width, height);
    //    },
    //    function(e) { 
    //        $.event.trigger({
    //            type: "plasio.nacl.error",
    //            message: "Could not allocate persistant storage"
    //        });
    //    });
 
    //    $(document).on("plasio.nacl.available", function() {
    //        scope.LASModuleWasLoaded = true;
    //    });
    //};
 
    scope.LAZLoader = LAZLoader;
    scope.LASLoader = LASLoader;
    scope.LASFile = LASFile;
    scope.LASDecoder = LASDecoder;
    scope.LASModuleWasLoaded = false;
//})(module.exports);
})(this);