some dem optimizations

This commit is contained in:
m-schuetz 2017-06-15 14:25:28 +02:00
parent d44f763251
commit d2c11fdb2e
2 changed files with 207 additions and 25 deletions

View File

@ -12,6 +12,51 @@ Potree.DEMNode = class DEMNode{
this.data = new Float32Array(tileSize * tileSize);
this.data.fill(-Infinity);
this.children = [];
this.mipMap = [this.data];
this.mipMapNeedsUpdate = true;
}
createMipMap(){
this.mipMap = [this.data];
let sourceSize = this.tileSize;
let mipSize = parseInt(sourceSize / 2);
let mipSource = this.data;
while(mipSize > 1){
let mipData = new Float32Array(mipSize * mipSize);
for(let i = 0; i < mipSize; i++){
for(let j = 0; j < mipSize; j++){
let h00 = mipSource[2*i+0 + 2*j*sourceSize];
let h01 = mipSource[2*i+0 + 2*j*sourceSize + sourceSize];
let h10 = mipSource[2*i+1 + 2*j*sourceSize];
let h11 = mipSource[2*i+1 + 2*j*sourceSize + sourceSize];
let [height, weight] = [0, 0];
if(isFinite(h00)){height += h00; weight += 1};
if(isFinite(h01)){height += h01; weight += 1};
if(isFinite(h10)){height += h10; weight += 1};
if(isFinite(h11)){height += h11; weight += 1};
height = height / weight;
//let hs = [h00, h01, h10, h11].filter(h => isFinite(h));
//let height = hs.reduce( (a, v, i) => a + v, 0) / hs.length;
mipData[i + j * mipSize] = height;
}
}
this.mipMap.push(mipData);
mipSource = mipData;
sourceSize = mipSize;
mipSize = parseInt(mipSize / 2);
}
this.mipMapNeedsUpdate = false;
}
uv(position){
@ -23,39 +68,104 @@ Potree.DEMNode = class DEMNode{
return [u, v];
}
height(position){
heightAtMipMapLevel(position, mipMapLevel){
let uv = this.uv(position);
let i = Math.min(uv[0] * this.tileSize, this.tileSize - 1);
let j = Math.min(uv[1] * this.tileSize, this.tileSize - 1);
let tileSize = parseInt(this.tileSize / parseInt(2 ** mipMapLevel));
let data = this.mipMap[mipMapLevel];
let [i0, i1] = [Math.floor(i), Math.ceil(i)];
let [j0, j1] = [Math.floor(j), Math.ceil(j)];
let h00 = this.data[i0 + this.tileSize * j0];
let h01 = this.data[i0 + this.tileSize * j1];
let h10 = this.data[i1 + this.tileSize * j0];
let h11 = this.data[i1 + this.tileSize * j1];
if([h00, h01, h10, h11].some(n => n === -Infinity)){
return null;
}
let i = Math.min(uv[0] * tileSize, tileSize - 1);
let j = Math.min(uv[1] * tileSize, tileSize - 1);
let a = i % 1;
let b = j % 1;
let h = h00 * (1 - a) * (1 - b) +
h01 * (1 - a) * b +
h10 * a * (1 - b) +
h11 * a * b;
let [i0, i1] = [Math.floor(i), Math.ceil(i)];
let [j0, j1] = [Math.floor(j), Math.ceil(j)];
let h00 = data[i0 + tileSize * j0];
let h01 = data[i0 + tileSize * j1];
let h10 = data[i1 + tileSize * j0];
let h11 = data[i1 + tileSize * j1];
let wh00 = isFinite(h00) ? (1 - a) * (1 - b) : 0;
let wh01 = isFinite(h01) ? (1 - a) * b : 0;
let wh10 = isFinite(h10) ? a * (1 - b) : 0;
let wh11 = isFinite(h11) ? a * b : 0;
let wsum = wh00 + wh01 + wh10 + wh11;
wh00 = wh00 / wsum;
wh01 = wh01 / wsum;
wh10 = wh10 / wsum;
wh11 = wh11 / wsum;
if(wsum === 0){
return null;
}
let h = 0;
if(isFinite(h00)) h += h00 * wh00;
if(isFinite(h01)) h += h01 * wh01;
if(isFinite(h10)) h += h10 * wh10;
if(isFinite(h11)) h += h11 * wh11;
return h;
}
height(position){
let h = null;
for(let i = 0; i < this.mipMap.length; i++){
h = this.heightAtMipMapLevel(position, i);
if(h !== null){
return h;
}
}
return h;
//let index = i + this.tileSize * j;
//let uv = this.uv(position);
//
//let height = this.data[index];
//return height;
//let i = Math.min(uv[0] * this.tileSize, this.tileSize - 1);
//let j = Math.min(uv[1] * this.tileSize, this.tileSize - 1);
//
//let a = i % 1;
//let b = j % 1;
//
//let [i0, i1] = [Math.floor(i), Math.ceil(i)];
//let [j0, j1] = [Math.floor(j), Math.ceil(j)];
//
//let h00 = this.data[i0 + this.tileSize * j0];
//let h01 = this.data[i0 + this.tileSize * j1];
//let h10 = this.data[i1 + this.tileSize * j0];
//let h11 = this.data[i1 + this.tileSize * j1];
//
//let wh00 = isFinite(h00) ? (1 - a) * (1 - b) : 0;
//let wh01 = isFinite(h01) ? (1 - a) * b : 0;
//let wh10 = isFinite(h10) ? a * (1 - b) : 0;
//let wh11 = isFinite(h11) ? a * b : 0;
//
//let wsum = wh00 + wh01 + wh10 + wh11;
//wh00 = wh00 / wsum;
//wh01 = wh01 / wsum;
//wh10 = wh10 / wsum;
//wh11 = wh11 / wsum;
//
//if(wsum === 0){
// return null;
//}
//
//let h = 0;
//
//if(isFinite(h00)) h += h00 * wh00;
//if(isFinite(h01)) h += h01 * wh01;
//if(isFinite(h10)) h += h10 * wh10;
//if(isFinite(h11)) h += h11 * wh11;
//
//return h;
}
traverse(callback, level = 0){
@ -263,9 +373,13 @@ Potree.DEM = class DEM{
}
}
demNode.createMipMap();
demNode.mipMapNeedsUpdate = true;
Potree.getDEMWorkerInstance().working = false;
//createDEMMesh(this, demNode);
}

View File

@ -383,7 +383,7 @@ function createDEMMesh(dem){
function createDEMMeshNode(dem, demNode){
let box = demNode.box;
let tileSize = dem.tileSize;
let tileSize = dem.tileSize * 1;
let triangles = [];
for(let i = 0; i < tileSize; i++){
@ -433,6 +433,74 @@ function createDEMMeshNode(dem, demNode){
mesh.position.copy(box.min);
//mesh.position.copy(pointcloud.position);
viewer.scene.scene.add(mesh);
{ // DEBUG code
//let data = demNode.data;
let steps = 64;
let data = new Float32Array(steps * steps);
let imgData = new Uint8Array(data.length * 4);
let box = demNode.box;
let boxSize = box.getSize();
for(let i = 0; i < steps; i++){
for(let j = 0; j < steps; j++){
let [u, v] = [i / (steps - 1), j / (steps - 1)];
let pos = new THREE.Vector3(
u * boxSize.x + box.min.x,
v * boxSize.y + box.min.y,
0
);
let height = demNode.height(pos);
let index = i + steps * j;
data[index] = height
//let index = i + steps * j;
//imgData[4*index + 0] = 255 * (height - min) / (max - min);
//imgData[4*index + 1] = 100;
//imgData[4*index + 2] = 0;
//imgData[4*index + 3] = 255;
}
}
let [min, max] = [Infinity, -Infinity];
for(let height of data){
if(!isFinite(height)){
continue;
}
min = Math.min(min, height);
max = Math.max(max, height);
}
for(let i = 0; i < data.length; i++){
imgData[4*i + 0] = 255 * (data[i] - min) / (max - min);
imgData[4*i + 1] = 100;
imgData[4*i + 2] = 0;
imgData[4*i + 3] = 255;
}
let img = Potree.utils.pixelsArrayToImage(imgData, steps, steps);
let screenshot = img.src;
if(!this.debugDIV){
this.debugDIV = $(`
<div id="pickDebug"
style="position: absolute;
right: 400px; width: 300px;
bottom: 44px; width: 300px;
z-index: 1000;
"></div>`);
$(document.body).append(this.debugDIV);
}
this.debugDIV.empty();
this.debugDIV.append($(`<img src="${screenshot}"
style="transform: scaleY(-1); width: 256px; height: 256px;"/>`));
}
}
Potree.updateVisibility = function(pointclouds, camera, renderer){
@ -588,7 +656,7 @@ Potree.updateVisibility = function(pointclouds, camera, renderer){
}// end priority queue loop
{ // update DEM
let maxDEMLevel = 1;
let maxDEMLevel = 4;
let candidates = pointclouds
.filter(p => (p.generateDEM && p.dem instanceof Potree.DEM));
for(let pointcloud of candidates){