From db9f3139f49f96aea40e0a4a11c8b17f3b4cf713 Mon Sep 17 00:00:00 2001 From: mschuetz Date: Mon, 19 May 2014 14:33:12 +0200 Subject: [PATCH] added disposal of least recently used nodes --- src/LRU.js | 143 +++++++++++++++++++ src/PointCloudOctree.js | 234 +++++++------------------------- src/PointCloudOctreeGeometry.js | 97 +++++++++++++ src/Potree.js | 73 +++++++++- 4 files changed, 361 insertions(+), 186 deletions(-) create mode 100644 src/LRU.js create mode 100644 src/PointCloudOctreeGeometry.js diff --git a/src/LRU.js b/src/LRU.js new file mode 100644 index 00000000..d2e1b317 --- /dev/null +++ b/src/LRU.js @@ -0,0 +1,143 @@ + + +/** + * + * @param node + * @class an item in the lru list. + */ +function LRUItem(node){ + this.previous = null; + this.next = null; + this.node = node; +} + +/** + * + * @class A doubly-linked-list of the least recently used elements. + */ +function LRU(){ + // the least recently used item + this.first = null; + // the most recently used item + this.last = null; + // a list of all items in the lru list + this.items = {}; + this.elements = 0; + this.numPoints = 0; +} + +/** + * number of elements in the list + * + * @returns {Number} + */ +LRU.prototype.size = function(){ + return this.elements; +}; + +LRU.prototype.contains = function(node){ + return this.items[node.id] == null; +}; + +/** + * makes node the most recently used item. if the list does not contain node, it will be added. + * + * @param node + */ +LRU.prototype.touch = function(node){ + var item; + if(this.items[node.id] == null){ + // add to list + item = new LRUItem(node); + item.previous = this.last; + this.last = item; + if(item.previous !== null){ + item.previous.next = item; + } + + this.items[node.id] = item; + this.elements++; + + if(this.first === null){ + this.first = item; + } + this.numPoints += node.numPoints; + }else{ + // update in list + item = this.items[node.id]; + if(item.previous === null){ + // handle touch on first element + if(item.next !== null){ + this.first = item.next; + this.first.previous = null; + item.previous = this.last; + item.next = null; + this.last = item; + item.previous.next = item; + } + }else if(item.next === null){ + // handle touch on last element + }else{ + // handle touch on any other element + item.previous.next = item.next; + item.next.previous = item.previous; + item.previous = this.last; + item.next = null; + this.last = item; + item.previous.next = item; + } + + + } +}; + +/** + * removes the least recently used item from the list and returns it. + * if the list was empty, null will be returned. + */ +LRU.prototype.remove = function remove(){ + if(this.first === null){ + return null; + } + var lru = this.first; + + // if the lru list contains at least 2 items, the item after the least recently used elemnt will be the new lru item. + if(lru.next !== null){ + this.first = lru.next; + this.first.previous = null; + }else{ + this.first = null; + this.last = null; + } + + delete this.items[lru.node.id]; + this.elements--; + this.numPoints -= lru.node.numPoints; + +// Logger.info("removed node: " + lru.node.id); + return lru.node; +}; + +LRU.prototype.getLRUItem = function(){ + if(this.first === null){ + return null; + } + var lru = this.first; + + return lru.node; +}; + +LRU.prototype.toString = function(){ + var string = "{ "; + var curr = this.first; + while(curr !== null){ + string += curr.node.id; + if(curr.next !== null){ + string += ", "; + } + curr = curr.next; + } + string += "}"; + string += "(" + this.size() + ")"; + return string; +}; diff --git a/src/PointCloudOctree.js b/src/PointCloudOctree.js index 4374b3c1..bcd85d04 100644 --- a/src/PointCloudOctree.js +++ b/src/PointCloudOctree.js @@ -1,92 +1,5 @@ - - -Potree.PointCloudOctreeGeometry = function(){ - this.numNodesLoading = 0; -} - -Potree.PointCloudOctreeGeometryNode = function(name, pcoGeometry, boundingBox){ - this.name = name; - this.index = parseInt(name.charAt(name.length-1)); - this.pcoGeometry = pcoGeometry; - this.boundingBox = boundingBox; - this.children = {}; -} - -Potree.PointCloudOctreeGeometryNode.prototype.addChild = function(child){ - this.children[child.index] = child; - child.parent = this; -} - -Potree.PointCloudOctreeGeometryNode.prototype.load = function(){ - if(this.loading === true || this.pcoGeometry.numNodesLoading > 3){ - return; - } - this.pcoGeometry.numNodesLoading++; - this.loading = true; - var url = this.pcoGeometry.octreeDir + "/" + this.name; - var node = 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 || xhr.status === 0) { - var buffer = xhr.response; - node.bufferLoaded(buffer); - } else { - console.log('Failed to load file! HTTP status: ' + xhr.status + ", file: " + url); - } - } - }; - try{ - xhr.send(null); - }catch(e){ - console.log("fehler beim laden der punktwolke: " + e); - } -} - -Potree.PointCloudOctreeGeometryNode.prototype.bufferLoaded = function(buffer){ - - var geometry = new THREE.BufferGeometry(); - var numPoints = buffer.byteLength / 16; - - var positions = new Float32Array(numPoints*3); - var colors = new Float32Array(numPoints*3); - var color = new THREE.Color(); - - var fView = new Float32Array(buffer); - var uiView = new Uint8Array(buffer); - - for(var i = 0; i < numPoints; i++){ - positions[3*i] = fView[4*i]; - positions[3*i+1] = fView[4*i+1]; - positions[3*i+2] = fView[4*i+2]; - - color.setRGB(uiView[16*i+12], uiView[16*i+13], uiView[16*i+14]); - colors[3*i] = color.r /255; - colors[3*i+1] = color.g / 255; - colors[3*i+2] = color.b / 255; - } - - geometry.addAttribute('position', new THREE.Float32Attribute(positions, 3)); - geometry.addAttribute('color', new THREE.Float32Attribute(colors, 3)); - geometry.boundingBox = this.boundingBox; - this.geometry = geometry; - this.loaded = true; - this.loading = false; - this.pcoGeometry.numNodesLoading--; -} - - - - - - - - /** * Stands in place for invisible or unloaded octree nodes. * If a proxy node becomes visible and its geometry has not been loaded, @@ -99,6 +12,9 @@ Potree.PointCloudOctreeProxyNode = function(geometryNode){ this.geometryNode = geometryNode; this.boundingBox = geometryNode.boundingBox; + this.name = geometryNode.name; + this.level = geometryNode.level; + this.numPoints = geometryNode.numPoints; } Potree.PointCloudOctreeProxyNode.prototype = Object.create(THREE.Object3D.prototype); @@ -110,97 +26,80 @@ Potree.PointCloudOctreeProxyNode.prototype = Object.create(THREE.Object3D.protot - - - Potree.PointCloudOctree = function(geometry, material){ THREE.Object3D.call( this ); this.pcoGeometry = geometry; this.boundingBox = this.pcoGeometry.boundingBox; this.material = material; - this.maxVisibleNodes = 300; + this.maxVisibleNodes = 500; + this.maxVisiblePoints = 20*1000*1000; this.level = 0; this.LODDistance = 20; this.LODFalloff = 1.3; this.LOD = 4; + + + var rootProxy = new Potree.PointCloudOctreeProxyNode(this.pcoGeometry.root); + this.add(rootProxy); } Potree.PointCloudOctree.prototype = Object.create(THREE.Object3D.prototype); -abcd = null; - Potree.PointCloudOctree.prototype.update = function(camera){ this.numVisibleNodes = 0; this.numVisiblePoints = 0; var frustum = new THREE.Frustum(); frustum.setFromMatrix( new THREE.Matrix4().multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse ) ); - var groot = this.pcoGeometry.root; - if(groot != undefined && groot.loaded == true && this.root === undefined){ - var root = new THREE.PointCloud(groot.geometry, this.material); - root.level = 0; - root.name = groot.name; - root.numPoints = groot.numPoints; - root.boundingBox = groot.boundingBox; - this.add(root); - this.root = root; + // check visibility + var stack = []; + stack.push(this); + while(stack.length > 0){ + var object = stack.shift(); - for(var i = 0; i < 8; i++){ - if(groot.children[i] !== undefined){ - var child = groot.children[i]; - var childProxy = new Potree.PointCloudOctreeProxyNode(child); - childProxy.level = child.level; - childProxy.numPoints = child.numPoints; - childProxy.name = child.name; - root.add(childProxy); - } - } - }else{ - var _this = this; + var boxWorld = Potree.utils.computeTransformedBoundingBox(object.boundingBox, object.matrixWorld); + var camWorldPos = new THREE.Vector3().setFromMatrixPosition( camera.matrixWorld ); + var distance = boxWorld.center().distanceTo(camWorldPos); + var radius = boxWorld.size().length() * 0.5; - var stack = []; - stack.push(this); - - while(stack.length > 0){ - var object = stack.shift(); - - - var boxWorld = Potree.utils.computeTransformedBoundingBox(object.boundingBox, object.matrixWorld); - var camWorldPos = new THREE.Vector3().setFromMatrixPosition( camera.matrixWorld ); - var distance = object.level; - distance = boxWorld.center().distanceTo(camWorldPos); - var radius = boxWorld.size().length() * 0.5; - - var visible = true; - visible = visible && frustum.intersectsBox(boxWorld); + var visible = true; + visible = visible && frustum.intersectsBox(boxWorld); + if(object.level > 2){ visible = visible && radius / distance > (1 / this.LOD); - object.visible = visible; - - if(!visible){ - this.hideDescendants(object); - continue; - } - - if(object instanceof THREE.PointCloud){ - _this.numVisibleNodes++; - _this.numVisiblePoints += object.numPoints; - }else if (object instanceof Potree.PointCloudOctreeProxyNode) { - _this.replaceProxy(object); - } - - - - - for(var i = 0; i < object.children.length; i++){ - stack.push(object.children[i]); - } + visible = visible && (this.numVisiblePoints + object.numPoints < Potree.pointLoadLimit); + visible = visible && (this.numVisibleNodes <= this.maxVisibleNodes); + visible = visible && (this.numVisiblePoints <= this.maxVisiblePoints); + }else{ + visible = true; } + + + + object.visible = visible; + + if(!visible){ + this.hideDescendants(object); + continue; + } + + if(object instanceof THREE.PointCloud){ + this.numVisibleNodes++; + this.numVisiblePoints += object.numPoints; + Potree.lru.touch(object); + }else if (object instanceof Potree.PointCloudOctreeProxyNode) { + this.replaceProxy(object); + } + + for(var i = 0; i < object.children.length; i++){ + stack.push(object.children[i]); + } } } + Potree.PointCloudOctree.prototype.replaceProxy = function(proxy){ var geometryNode = proxy.geometryNode; @@ -211,6 +110,7 @@ Potree.PointCloudOctree.prototype.replaceProxy = function(proxy){ node.level = proxy.level; node.numPoints = proxy.numPoints; node.boundingBox = geometry.boundingBox; + node.pcoGeometry = geometryNode; var parent = proxy.parent; parent.remove(proxy); parent.add(node); @@ -219,9 +119,6 @@ Potree.PointCloudOctree.prototype.replaceProxy = function(proxy){ if(geometryNode.children[i] !== undefined){ var child = geometryNode.children[i]; var childProxy = new Potree.PointCloudOctreeProxyNode(child); - childProxy.name = child.name; - childProxy.level = child.level; - childProxy.numPoints = child.numPoints; node.add(childProxy); } } @@ -252,36 +149,3 @@ Potree.PointCloudOctree.prototype.hideDescendants = function(object){ } } } - -//THREE.Object3D.prototype.traverseBreadthFirst = function(callback){ -// var stack = []; -// stack.push(this); -// -// while(stack.length > 0){ -// var current = stack.shift(); -// -// var accepted = callback(current); -// if(!accepted){ -// continue; -// } -// -// for(var i = 0; i < current.children.length; i++){ -// stack.push(current.children[i]); -// } -// } -//} - - - - - - - - - - - - - - - diff --git a/src/PointCloudOctreeGeometry.js b/src/PointCloudOctreeGeometry.js new file mode 100644 index 00000000..353563de --- /dev/null +++ b/src/PointCloudOctreeGeometry.js @@ -0,0 +1,97 @@ + + + +Potree.PointCloudOctreeGeometry = function(){ + this.numNodesLoading = 0; +} + +Potree.PointCloudOctreeGeometryNode = function(name, pcoGeometry, boundingBox){ + this.name = name; + this.index = parseInt(name.charAt(name.length-1)); + this.pcoGeometry = pcoGeometry; + this.boundingBox = boundingBox; + this.children = {}; + this.numPoints = 0; + this.level = null; + +} + +Potree.PointCloudOctreeGeometryNode.prototype.addChild = function(child){ + this.children[child.index] = child; + child.parent = this; +} + +Potree.PointCloudOctreeGeometryNode.prototype.load = function(){ + if(this.loading === true || this.pcoGeometry.numNodesLoading > 3){ + return; + } + + if(Potree.lru.numPoints + this.numPoints >= Potree.pointLoadLimit){ + Potree.removeLeastRecentlyUsed(this.numPoints); + } + + + this.pcoGeometry.numNodesLoading++; + this.loading = true; + var url = this.pcoGeometry.octreeDir + "/" + this.name; + var node = 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 || xhr.status === 0) { + var buffer = xhr.response; + node.bufferLoaded(buffer); + } else { + console.log('Failed to load file! HTTP status: ' + xhr.status + ", file: " + url); + } + } + }; + try{ + xhr.send(null); + }catch(e){ + console.log("fehler beim laden der punktwolke: " + e); + } +} + +Potree.PointCloudOctreeGeometryNode.prototype.bufferLoaded = function(buffer){ + //console.log("loaded: " + this.name); + + var geometry = new THREE.BufferGeometry(); + var numPoints = buffer.byteLength / 16; + + var positions = new Float32Array(numPoints*3); + var colors = new Float32Array(numPoints*3); + var color = new THREE.Color(); + + var fView = new Float32Array(buffer); + var uiView = new Uint8Array(buffer); + + for(var i = 0; i < numPoints; i++){ + positions[3*i] = fView[4*i]; + positions[3*i+1] = fView[4*i+1]; + positions[3*i+2] = fView[4*i+2]; + + color.setRGB(uiView[16*i+12], uiView[16*i+13], uiView[16*i+14]); + colors[3*i] = color.r /255; + colors[3*i+1] = color.g / 255; + colors[3*i+2] = color.b / 255; + } + + geometry.addAttribute('position', new THREE.Float32Attribute(positions, 3)); + geometry.addAttribute('color', new THREE.Float32Attribute(colors, 3)); + geometry.boundingBox = this.boundingBox; + this.geometry = geometry; + this.loaded = true; + this.loading = false; + this.pcoGeometry.numNodesLoading--; +} + +Potree.PointCloudOctreeGeometryNode.prototype.dispose = function(){ + delete this.geometry; + this.loaded = false; + + //console.log("dispose: " + this.name); +} \ No newline at end of file diff --git a/src/Potree.js b/src/Potree.js index 6243d57d..367997bd 100644 --- a/src/Potree.js +++ b/src/Potree.js @@ -1,5 +1,76 @@ function Potree(){ - + } + +Potree.init = function(args){ + var args_ = args || {}; + + Potree.pointLoadLimit = args_.pointLoadLimit || 50*1000*1000; + Potree.lru = new LRU(); +} + + +/** + * + * amount: minimum number of points to remove + */ +Potree.removeLeastRecentlyUsed = function(amount){ + + + var freed = 0; + do{ + var node = this.lru.first.node; + var parent = node.parent; + var geometry = node.geometry; + var pcoGeometry = node.pcoGeometry; + var proxy = new Potree.PointCloudOctreeProxyNode(pcoGeometry); + + var result = Potree.removeNode(node); + freed += result.freed; + + parent.add(proxy); + + if(result.numDeletedNodes == 0){ + break; + } + }while(freed < amount); +} + +Potree.removeNode = function(node){ + //console.log("removeNode(" + node.name + ")"); + + var freed = 0; + var numDeletedNodes = 0; + var descendants = []; + + node.traverse(function(object){ + descendants.push(object); + }); + + for(var i = 0; i < descendants.length; i++){ + var descendant = descendants[i]; + if(descendant instanceof THREE.PointCloud){ + freed += descendant.pcoGeometry.numPoints; + descendant.pcoGeometry.dispose(); + descendant.geometry.dispose(); + Potree.lru.remove(descendant); + numDeletedNodes++; + } + } + + Potree.lru.remove(node); + node.parent.remove(node); + + return { + "freed": freed, + "numDeletedNodes": numDeletedNodes + }; +} + + + + + +