diff --git a/src/Potree.js b/src/Potree.js index 39e6aa3f..0d164ef8 100644 --- a/src/Potree.js +++ b/src/Potree.js @@ -459,6 +459,13 @@ Potree.updateVisibility = function(pointclouds, camera, renderer){ let parent = element.parent; let pointcloud = pointclouds[element.pointcloud]; + //{ // restrict to certain nodes for debugging + // let allowedNodes = ["r", "r0", "r4"]; + // if(!allowedNodes.includes(node.name)){ + // continue; + // } + //} + let box = node.getBoundingBox(); let frustum = frustums[element.pointcloud]; let camObjPos = camObjPositions[element.pointcloud]; @@ -595,11 +602,7 @@ Potree.updateVisibility = function(pointclouds, camera, renderer){ } //for(let node of visibleNodes){ - // //let allowedNodes = ["r", "r0", "r4", "r04", "r40", "r402", "r4020", "r4022", "r40206", "r40224", "r40202", "r40220", "r00", "r042"]; - // //let allowedNodes = ["r", "r0", "r4", "r04", "r40", "r402", "r4020", "r4022", "r00", "r042"]; - // //let allowedNodes = ["r", "r0", "r04", "r042"]; - // let allowedNodes = ["r", "r4", "r40", "r402", "r4020", "r40206"]; - // //let allowedNodes = ["r", "r4", "r40", "r402", "r4020"]; + // let allowedNodes = ["r", "r0", "r4"]; // node.sceneNode.visible = allowedNodes.includes(node.geometryNode.name); // // if(node.boundingBoxNode){ diff --git a/src/loader/GreyhoundBinaryLoader.js b/src/loader/GreyhoundBinaryLoader.js index 02ffb8f3..886030e0 100644 --- a/src/loader/GreyhoundBinaryLoader.js +++ b/src/loader/GreyhoundBinaryLoader.js @@ -116,7 +116,11 @@ Potree.GreyhoundBinaryLoader.prototype.parse = function(node, buffer){ } } - addAttribute('indices', data.indices, 1); + //addAttribute('indices', data.indices, 1); + + let indicesAttribute = new THREE.Uint8BufferAttribute(data.indices, 4); + indicesAttribute.normalized = true; + geometry.addAttribute("indices", indicesAttribute); if (!geometry.attributes.normal) { addAttribute('normal', new Float32Array(numPoints * 3), 3); @@ -134,9 +138,10 @@ Potree.GreyhoundBinaryLoader.prototype.parse = function(node, buffer){ var pco = node.pcoGeometry; - //let nodeOffset = node.boundingBox.getSize().multiplyScalar(0.5); + //let nodeOffset = node.boundingBox.getCenter(); //let nodeOffset = new THREE.Vector3(0, 0, 0); - let nodeOffset = node.pcoGeometry.boundingBox.getCenter(); + let nodeOffset = node.pcoGeometry.boundingBox.getCenter().sub(node.boundingBox.min); + //let nodeOffset = node.pcoGeometry.boundingBox.min; var message = { buffer: buffer, diff --git a/src/loader/LasLazLoader.js b/src/loader/LasLazLoader.js index 14d2e1b0..9b22c338 100644 --- a/src/loader/LasLazLoader.js +++ b/src/loader/LasLazLoader.js @@ -7,182 +7,178 @@ * Thanks to Uday Verma and Howard Butler * */ - -Potree.LasLazLoader = function(version){ - if(typeof(version) === "string"){ - this.version = new Potree.Version(version); - }else{ - this.version = version; - } -}; - -Potree.LasLazLoader.prototype.load = function(node){ - - if(node.loaded){ - return; - } + +Potree.LasLazLoader = class LasLazLoader{ - //var url = node.pcoGeometry.octreeDir + "/" + node.name; - var pointAttributes = node.pcoGeometry.pointAttributes; - //var url = node.pcoGeometry.octreeDir + "/" + node.name + "." + pointAttributes.toLowerCase() - - var url = node.getURL(); - - if(this.version.equalOrHigher("1.4")){ - url += "." + pointAttributes.toLowerCase(); - } - - var scope = this; - - var xhr = new XMLHttpRequest(); - xhr.open('GET', url, true); - xhr.responseType = 'arraybuffer'; - xhr.overrideMimeType('text/plain; charset=x-user-defined'); - xhr.onreadystatechange = function() { - if (xhr.readyState === 4) { - if (xhr.status === 200) { - var buffer = xhr.response; - //LasLazLoader.loadData(buffer, handler); - scope.parse(node, buffer); - } else { - console.log('Failed to load file! HTTP status: ' + xhr.status + ", file: " + url); - } + constructor(version){ + if(typeof(version) === "string"){ + this.version = new Potree.Version(version); + }else{ + this.version = version; } - }; + } - xhr.send(null); -}; - -Potree.LasLazLoader.progressCB = function(arg){ - -}; - -Potree.LasLazLoader.prototype.parse = function loadData(node, buffer){ - var lf = new LASFile(buffer); - var handler = new Potree.LasLazBatcher(node); - - return Promise.resolve(lf).cancellable().then(function(lf) { - return lf.open().then(function() { - lf.isOpen = true; - return lf; - }) - .catch(Promise.CancellationError, function(e) { - // open message was sent at this point, but then handler was not called - // because the operation was cancelled, explicitly close the file - return lf.close().then(function() { - throw e; - }); - }); - }).then(function(lf) { - return lf.getHeader().then(function(h) { - return [lf, h]; - }); - }).then(function(v) { - var lf = v[0]; - var header = v[1]; + static progressCB(){ - var skip = 1; - var totalRead = 0; - var totalToRead = (skip <= 1 ? header.pointsCount : header.pointsCount / skip); - var reader = function() { - var p = lf.readData(1000000, 0, skip); - return p.then(function(data) { - handler.push(new LASDecoder(data.buffer, - header.pointsFormatId, - header.pointsStructSize, - data.count, - header.scale, - header.offset, - header.mins, header.maxs)); + } + + load(node){ + + if(node.loaded){ + return; + } + + let pointAttributes = node.pcoGeometry.pointAttributes; - totalRead += data.count; - Potree.LasLazLoader.progressCB(totalRead / totalToRead); - - if (data.hasMoreData) - return reader(); - else { - - header.totalRead = totalRead; - header.versionAsString = lf.versionAsString; - header.isCompressed = lf.isCompressed; - return [lf, header, handler]; + let url = node.getURL(); + + if(this.version.equalOrHigher("1.4")){ + url += "." + pointAttributes.toLowerCase(); + } + + let scope = this; + + let xhr = new XMLHttpRequest(); + xhr.open('GET', url, true); + xhr.responseType = 'arraybuffer'; + xhr.overrideMimeType('text/plain; charset=x-user-defined'); + xhr.onreadystatechange = function() { + if (xhr.readyState === 4) { + if (xhr.status === 200) { + let buffer = xhr.response; + scope.parse(node, buffer); + } else { + console.log('Failed to load file! HTTP status: ' + xhr.status + ", file: " + url); } - }); + } }; - return reader(); - }).then(function(v) { - var lf = v[0]; - // we're done loading this file - // - Potree.LasLazLoader.progressCB(1); + xhr.send(null); + } + + parse(node, buffer){ + let lf = new LASFile(buffer); + let handler = new Potree.LasLazBatcher(node); + + return Promise.resolve(lf).cancellable().then(function(lf) { + return lf.open().then(function() { + lf.isOpen = true; + return lf; + }) + .catch(Promise.CancellationError, function(e) { + // open message was sent at this point, but then handler was not called + // because the operation was cancelled, explicitly close the file + return lf.close().then(function() { + throw e; + }); + }); + }).then(function(lf) { + return lf.getHeader().then(function(h) { + return [lf, h]; + }); + }).then(function(v) { + let lf = v[0]; + let header = v[1]; + + let skip = 1; + let totalRead = 0; + let totalToRead = (skip <= 1 ? header.pointsCount : header.pointsCount / skip); + let reader = function() { + let p = lf.readData(1000000, 0, skip); + return p.then(function(data) { + handler.push(new LASDecoder(data.buffer, + header.pointsFormatId, + header.pointsStructSize, + data.count, + header.scale, + header.offset, + header.mins, header.maxs)); - // Close it - return lf.close().then(function() { - lf.isOpen = false; - // Delay this a bit so that the user sees 100% completion + totalRead += data.count; + Potree.LasLazLoader.progressCB(totalRead / totalToRead); + + if (data.hasMoreData) + return reader(); + else { + + header.totalRead = totalRead; + header.versionAsString = lf.versionAsString; + header.isCompressed = lf.isCompressed; + return [lf, header, handler]; + } + }); + }; + + return reader(); + }).then(function(v) { + let lf = v[0]; + // we're done loading this file // - return Promise.delay(200).cancellable(); - }).then(function() { - // trim off the first element (our LASFile which we don't really want to pass to the user) - // - return v.slice(1); - }); - }).catch(Promise.CancellationError, function(e) { - // If there was a cancellation, make sure the file is closed, if the file is open - // close and then fail - if (lf.isOpen) + Potree.LasLazLoader.progressCB(1); + + // Close it return lf.close().then(function() { lf.isOpen = false; - throw e; + // Delay this a bit so that the user sees 100% completion + // + return Promise.delay(200).cancellable(); + }).then(function() { + // trim off the first element (our LASFile which we don't really want to pass to the user) + // + return v.slice(1); }); - throw e; - }); + }).catch(Promise.CancellationError, function(e) { + // If there was a cancellation, make sure the file is closed, if the file is open + // close and then fail + if (lf.isOpen) + return lf.close().then(function() { + lf.isOpen = false; + throw e; + }); + throw e; + }); + } + + handle(node, url){ + + } + }; -Potree.LasLazLoader.prototype.handle = function(node, url){ - -}; - - - - - - -Potree.LasLazBatcher = function(node){ - this.push = function(lasBuffer){ +Potree.LasLazBatcher = class LasLazBatcher{ + + constructor(node){ + this.node = node; + } + + push(lasBuffer){ let workerPath = Potree.scriptPath + "/workers/LASDecoderWorker.js"; let worker = Potree.workerPool.getWorker(workerPath); - var mins = new THREE.Vector3(lasBuffer.mins[0], lasBuffer.mins[1], lasBuffer.mins[2]); - var maxs = new THREE.Vector3(lasBuffer.maxs[0], lasBuffer.maxs[1], lasBuffer.maxs[2]); - mins.add(node.pcoGeometry.offset); - maxs.add(node.pcoGeometry.offset); - - worker.onmessage = function(e){ - var geometry = new THREE.BufferGeometry(); - var numPoints = lasBuffer.pointsCount; + worker.onmessage = (e) => { + let geometry = new THREE.BufferGeometry(); + let numPoints = lasBuffer.pointsCount; - var endsWith = function(str, suffix) { + let endsWith = function(str, suffix) { return str.indexOf(suffix, str.length - suffix.length) !== -1; }; - var positions = e.data.position; - var colors = new Uint8Array(e.data.color); - var intensities = e.data.intensity; - var classifications = new Uint8Array(e.data.classification); - var returnNumbers = new Uint8Array(e.data.returnNumber); - var numberOfReturns = new Uint8Array(e.data.numberOfReturns); - var pointSourceIDs = new Uint16Array(e.data.pointSourceID); - var indices = new ArrayBuffer(numPoints*4); - var iIndices = new Uint32Array(indices); + let positions = e.data.position; + let colors = new Uint8Array(e.data.color); + let intensities = e.data.intensity; + let classifications = new Uint8Array(e.data.classification); + let returnNumbers = new Uint8Array(e.data.returnNumber); + let numberOfReturns = new Uint8Array(e.data.numberOfReturns); + let pointSourceIDs = new Uint16Array(e.data.pointSourceID); + //let indices = new ArrayBuffer(numPoints*4); + //let iIndices = new Uint32Array(indices); - var box = new THREE.Box3(); + let box = new THREE.Box3(); - var fPositions = new Float32Array(positions); - for(var i = 0; i < numPoints; i++){ - iIndices[i] = i; + let fPositions = new Float32Array(positions); + for(let i = 0; i < numPoints; i++){ + //iIndices[i] = i; box.expandByPoint(new THREE.Vector3(fPositions[3*i+0], fPositions[3*i+1], fPositions[3*i+2])); } @@ -194,37 +190,38 @@ Potree.LasLazBatcher = function(node){ geometry.addAttribute('returnNumber', new THREE.BufferAttribute(returnNumbers, 1)); geometry.addAttribute('numberOfReturns', new THREE.BufferAttribute(numberOfReturns, 1)); geometry.addAttribute('pointSourceID', new THREE.BufferAttribute(pointSourceIDs, 1)); - geometry.addAttribute('indices', new THREE.BufferAttribute(indices, 1)); + //geometry.addAttribute('indices', new THREE.BufferAttribute(indices, 1)); geometry.addAttribute("normal", new THREE.BufferAttribute(new Float32Array(numPoints*3), 3)); - var tightBoundingBox = new THREE.Box3( + let indicesAttribute = new THREE.Uint8BufferAttribute(e.data.indices, 4); + indicesAttribute.normalized = true; + geometry.addAttribute("indices", indicesAttribute); + + let tightBoundingBox = new THREE.Box3( new THREE.Vector3().fromArray(e.data.tightBoundingBox.min), new THREE.Vector3().fromArray(e.data.tightBoundingBox.max) ); - geometry.boundingBox = new THREE.Box3(mins, maxs); - //geometry.boundingBox = tightBoundingBox; - //node.boundingBox = geometry.boundingBox; - node.tightBoundingBox = tightBoundingBox; + geometry.boundingBox = this.node.boundingBox; + this.node.tightBoundingBox = tightBoundingBox; - node.geometry = geometry; - node.loaded = true; - node.loading = false; - node.pcoGeometry.numNodesLoading--; + this.node.geometry = geometry; + this.node.loaded = true; + this.node.loading = false; + this.node.pcoGeometry.numNodesLoading--; Potree.workerPool.returnWorker(workerPath, worker); }; - var message = { + let message = { buffer: lasBuffer.arrayb, numPoints: lasBuffer.pointsCount, pointSize: lasBuffer.pointSize, pointFormatID: 2, scale: lasBuffer.scale, offset: lasBuffer.offset, - mins: [node.pcoGeometry.boundingBox.min.x, node.pcoGeometry.boundingBox.min.y, node.pcoGeometry.boundingBox.min.z], - maxs: [node.pcoGeometry.boundingBox.max.x, node.pcoGeometry.boundingBox.max.y, node.pcoGeometry.boundingBox.max.z], - bbOffset: [node.pcoGeometry.offset.x, node.pcoGeometry.offset.y, node.pcoGeometry.offset.z] + mins: lasBuffer.mins, + maxs: lasBuffer.maxs }; worker.postMessage(message, [message.buffer]); }; diff --git a/src/workers/BinaryDecoderWorker.js b/src/workers/BinaryDecoderWorker.js index fedc84d2..5712d925 100644 --- a/src/workers/BinaryDecoderWorker.js +++ b/src/workers/BinaryDecoderWorker.js @@ -207,11 +207,9 @@ onmessage = function(event){ } let indices = new ArrayBuffer(numPoints*4); - //let fIndices = new Float32Array(indices); let iIndices = new Uint32Array(indices); for(let i = 0; i < numPoints; i++){ iIndices[i] = i; - //fIndices[i] = i; } if(attributeBuffers[Potree.PointAttribute.CLASSIFICATION.name] === undefined){ diff --git a/src/workers/LASDecoderWorker.js b/src/workers/LASDecoderWorker.js index fe1e90ee..37306353 100644 --- a/src/workers/LASDecoderWorker.js +++ b/src/workers/LASDecoderWorker.js @@ -62,7 +62,6 @@ onmessage = function(event){ var offset = event.data.offset; var mins = event.data.mins; var maxs = event.data.maxs; - var bbOffset = event.data.bbOffset; var temp = new ArrayBuffer(4); var tempUint8 = new Uint8Array(temp); @@ -115,9 +114,9 @@ onmessage = function(event){ tempUint8[3] = bufferView[i*pointSize+11]; var z = tempInt32[0]; - positions[3*i+0] = x * scale[0] + offset[0] - bbOffset[0]; - positions[3*i+1] = y * scale[1] + offset[1] - bbOffset[1]; - positions[3*i+2] = z * scale[2] + offset[2] - bbOffset[2]; + positions[3*i+0] = x * scale[0] + offset[0] - offset[0] - event.data.mins[0]; + positions[3*i+1] = y * scale[1] + offset[1] - offset[1] - event.data.mins[1]; + positions[3*i+2] = z * scale[2] + offset[2] - offset[2] - event.data.mins[2]; tightBoundingBox.min[0] = Math.min(tightBoundingBox.min[0], positions[3*i+0]); tightBoundingBox.min[1] = Math.min(tightBoundingBox.min[1], positions[3*i+1]); @@ -170,6 +169,12 @@ onmessage = function(event){ } } + var indices = new ArrayBuffer(numPoints*4); + var iIndices = new Uint32Array(indices); + for(var i = 0; i < numPoints; i++){ + iIndices[i] = i; + } + var message = { position: pBuff, color: cBuff, @@ -178,7 +183,8 @@ onmessage = function(event){ returnNumber: rnBuff, numberOfReturns: nrBuff, pointSourceID: psBuff, - tightBoundingBox: tightBoundingBox + tightBoundingBox: tightBoundingBox, + indices: indices }; var transferables = [ @@ -188,7 +194,8 @@ onmessage = function(event){ message.classification, message.returnNumber, message.numberOfReturns, - message.pointSourceID]; + message.pointSourceID, + message.indices]; postMessage(message, transferables); };