From e62e20796cf500b647587b105f4bb46dc3ac5e36 Mon Sep 17 00:00:00 2001 From: mschuetz Date: Fri, 21 Apr 2017 18:05:04 +0200 Subject: [PATCH 1/6] draw profile with three.js instead of Canvas2D. Much faster! WIP --- src/viewer/profile.html | 4 +- src/viewer/profile.js | 576 +++++++++++----------------------------- 2 files changed, 153 insertions(+), 427 deletions(-) diff --git a/src/viewer/profile.html b/src/viewer/profile.html index 6737fd29..6d489325 100644 --- a/src/viewer/profile.html +++ b/src/viewer/profile.html @@ -52,7 +52,7 @@ width: 100%; height: 100%; "> - + ">
diff --git a/src/viewer/profile.js b/src/viewer/profile.js index 5b033958..c9af46cf 100644 --- a/src/viewer/profile.js +++ b/src/viewer/profile.js @@ -12,8 +12,59 @@ Potree.Viewer.Profile = class ProfileWindow{ this.maximized = false; this.threshold = 20*1000; + this.elRoot = $("#profile_window"); + + this.renderArea = this.elRoot.find("#profileCanvasContainer"); + this.renderer = new THREE.WebGLRenderer({premultipliedAlpha: false}); + this.renderer.setSize(10, 10); + this.renderer.autoClear = false; + this.renderArea.append($(this.renderer.domElement)); + this.renderer.domElement.tabIndex = "2222"; + this.renderer.context.getExtension("EXT_frag_depth"); + $(this.renderer.domElement).css("width", "100%"); + $(this.renderer.domElement).css("height", "100%"); + + this.camera = new THREE.OrthographicCamera(-10, 10, 10, -10, -1000, 1000); + + this.scene = new THREE.Scene(); + this.scene.background = new THREE.Color( 0x00ff00 ); + + let sg = new THREE.SphereGeometry(5, 32, 32); + let sm = new THREE.MeshNormalMaterial(); + let s = new THREE.Mesh(sg, sm); + s.position.set(100, 100, 0); + this.scene.add(s); + + this.maxPoints = 50*1000; + this.geometry = new THREE.BufferGeometry(); + this.buffers = { + position: new Float32Array(3 * this.maxPoints), + color: new Uint8Array(3 * this.maxPoints), + }; + this.numPoints = 0; + this.geometry.addAttribute("position", new THREE.BufferAttribute(this.buffers.position, 3)); + this.geometry.addAttribute("color", new THREE.BufferAttribute(this.buffers.color, 3, true)); + this.geometry.setDrawRange(0, 100); + this.material = new THREE.PointsMaterial( { size: 0.001, vertexColors: THREE.VertexColors } ); + this.tPoints = new THREE.Points(this.geometry, this.material); + this.scene.add(this.tPoints); + + this.min = new THREE.Vector3(0, 0, 0); + this.max = new THREE.Vector3(0, 0, 0); + + //this.renderArea.mousemove(event => { + // this.render(); + //}); + + this.scheduledDraw = null; + this.redraw = () => { + if(this.currentProfile){ + this.draw(this.currentProfile); + } + }; + $('#closeProfileContainer').click(() => { this.hide(); this.enabled = false; @@ -29,34 +80,22 @@ Potree.Viewer.Profile = class ProfileWindow{ } }); - //this.drawOnChange = (event) => { - // this.redraw(); - //}; - - this.redraw = () => { - if(this.currentProfile){ - this.draw(this.currentProfile); - } - }; - - viewer.addEventListener("height_range_changed", this.redraw); - - let width = document.getElementById('profile_window').clientWidth; - let height = document.getElementById('profile_window').clientHeight; - let resizeLoop = () => { - requestAnimationFrame(resizeLoop); - let newWidth = document.getElementById('profile_window').clientWidth; - let newHeight = document.getElementById('profile_window').clientHeight; - - if(newWidth !== width || newHeight !== height){ - setTimeout(this.redraw, 50, {profile: this.currentProfile}); - } - - width = newWidth; - height = newHeight; - }; - requestAnimationFrame(resizeLoop); + } + + render(){ + let width = this.renderArea[0].clientWidth; + let height = this.renderArea[0].clientHeight; + + this.camera.left = -width/2; + this.camera.right = width/2; + this.camera.top = height/2; + this.camera.bottom = -height/2; + this.camera.updateProjectionMatrix(); + + this.renderer.setSize(width, height); + + this.renderer.render(this.scene, this.camera); } show(){ @@ -76,40 +115,68 @@ Potree.Viewer.Profile = class ProfileWindow{ this.requests = []; }; - preparePoints(profileProgress){ + handleNewPoints(profileProgress){ + let prepared = this.preparePoints(profileProgress); + + if(prepared.numPoints === 0){ + return; + } + + if(this.numPoints === 0){ + this.min.fromArray(prepared.buffers.position, 3); + this.max.fromArray(prepared.buffers.position, 3); + } + + for(let i = 0; i < prepared.numPoints; i++){ + let pos = new THREE.Vector3().fromArray( + prepared.buffers.position.slice(3*i, 3*i+3)); + this.min.min(pos); + this.max.max(pos); + } + + let newNumPoints = this.numPoints + prepared.numPoints; + + let position = this.buffers.position; + let color = this.buffers.color; + + position.set(prepared.buffers.position, 3 * this.numPoints); + color.set(prepared.buffers.color, 3 * this.numPoints); + + for(let i = 1; i < 100; i+=3){ + position[i] = position[i] - 700; + } + + this.geometry.attributes.position.needsUpdate = true; + this.geometry.attributes.color.needsUpdate = true; + + this.geometry.setDrawRange(0, newNumPoints); + + this.numPoints = newNumPoints; + + let center = new THREE.Vector3().addVectors(this.min, this.max) + .multiplyScalar(0.5); + this.camera.position.copy(center); + } + preparePoints(profileProgress){ + let segments = profileProgress.segments; if (segments.length === 0){ return false; } - let data = []; + let numPoints = profileProgress.size(); + + let position = new Float32Array(3 * numPoints); + let color = new Uint8Array(3 * numPoints); + let distance = 0; let totalDistance = 0; let min = new THREE.Vector3(Math.max()); let max = new THREE.Vector3(0); - // Get the same color map as Three - let hr = this.viewer.getHeightRange(); - - let heightRange = hr.max - hr.min; - let colorRange = []; - let colorDomain = []; - - // Read the altitude gradient used in 3D scene - let gradient = viewer.scene.pointclouds[0].material.gradient; - for (let c = 0; c < gradient.length; c++){ - colorDomain.push(hr.min + heightRange * gradient[c][0]); - colorRange.push('#' + gradient[c][1].getHexString()); - } - - // Altitude color map scale - let colorRamp = d3.scale.linear() - .domain(colorDomain) - .range(colorRange) - .clamp(true); - // Iterate the profile's segments + let i = 0; for(let segment of segments){ let sv = new THREE.Vector3().subVectors(segment.end, segment.start).setZ(0) let segmentLength = sv.length(); @@ -123,168 +190,43 @@ Potree.Viewer.Profile = class ProfileWindow{ min.min(p); max.max(p); + // TODO length is probably wrong here let distance = totalDistance + pl.length(); - let d = { - distance: distance, - x: p.x, - y: p.y, - z: p.z, - altitude: p.z, - heightColor: colorRamp(p.z), - color: points.color ? points.color[j] : [0, 0, 0], - intensity: points.intensity ? points.intensity[j] : 0, - classification: points.classification ? points.classification[j] : 0, - returnNumber: points.returnNumber ? points.returnNumber[j] : 0, - numberOfReturns: points.numberOfReturns ? points.numberOfReturns[j] : 0, - pointSourceID: points.pointSourceID ? points.pointSourceID[j] : 0, - }; + // important are + // distance + // p.z + // points.color ? points.color[j] : [0, 0, 0] - data.push(d); + position[3*i + 0] = distance; + position[3*i + 1] = p.z; + position[3*i + 2] = 0; + + let c = points.color ? points.color[j] : [0, 0, 0]; + color[3*i + 0] = c[0]; + color[3*i + 1] = c[1]; + color[3*i + 2] = c[2]; + + + i++; } - // Increment distance from the profile start point totalDistance += segmentLength; } - - let output = { - 'data': data, - 'minX': min.x, - 'minY': min.y, - 'minZ': min.z, - 'maxX': max.x, - 'maxY': max.y, - 'maxZ': max.z + + let result = { + numPoints: numPoints, + min: min, + max: max, + buffers: { + position: position, + color: color + } }; - return output; + return result; }; - pointHighlight(coordinates){ - - let pointSize = 6; - - let svg = d3.select("svg#profileSVG"); - - // Find the hovered point if applicable - let d = this.points; - let sx = this.scaleX; - let sy = this.scaleY; - let xs = coordinates[0]; - let ys = coordinates[1]; - - // Fix FF vs Chrome discrepancy - //if(navigator.userAgent.indexOf("Firefox") == -1 ) { - // xs = xs - this.margin.left; - // ys = ys - this.margin.top; - //} - let hP = []; - let tol = pointSize; - - for (let i=0; i < d.length; i++){ - if(sx(d[i].distance) < xs + tol && sx(d[i].distance) > xs - tol && sy(d[i].altitude) < ys + tol && sy(d[i].altitude) > ys -tol){ - hP.push(d[i]); - } - } - - if(hP.length > 0){ - let p = hP[0]; - this.hoveredPoint = hP[0]; - let cx, cy; - if(navigator.userAgent.indexOf("Firefox") == -1 ) { - cx = this.scaleX(p.distance) + this.margin.left; - cy = this.scaleY(p.altitude) + this.margin.top; - } else { - cx = this.scaleX(p.distance); - cy = this.scaleY(p.altitude); - } - - //cx -= pointSize / 2; - cy -= pointSize / 2; - - //let svg = d3.select("svg#profileSVG"); - d3.selectAll("rect").remove(); - let rectangle = svg.append("rect") - .attr("x", cx) - .attr("y", cy) - .attr("id", p.id) - .attr("width", pointSize) - .attr("height", pointSize) - .style("fill", 'yellow'); - - - let marker = $("#profile_selection_marker"); - marker.css("display", "initial"); - marker.css("left", cx + "px"); - marker.css("top", cy + "px"); - marker.css("width", pointSize + "px"); - marker.css("height", pointSize + "px"); - marker.css("background-color", "yellow"); - - //let html = 'x: ' + Math.round(10 * p.x) / 10 + ' y: ' + Math.round(10 * p.y) / 10 + ' z: ' + Math.round( 10 * p.altitude) / 10 + ' - '; - //html += i18n.t('tools.classification') + ': ' + p.classificationCode + ' - '; - //html += i18n.t('tools.intensity') + ': ' + p.intensityCode; - - let html = 'x: ' + Math.round(10 * p.x) / 10 + ' y: ' + Math.round(10 * p.y) / 10 + ' z: ' + Math.round( 10 * p.altitude) / 10 + ' - '; - html += "offset: " + p.distance.toFixed(3) + ' - '; - html += "Classification: " + p.classification + ' - '; - html += "Intensity: " + p.intensity; - - $('#profileInfo').css('color', 'yellow'); - $('#profileInfo').html(html); - - } else { - d3.selectAll("rect").remove(); - $('#profileInfo').html(""); - - let marker = $("#profile_selection_marker"); - marker.css("display", "none"); - } - }; - - strokeColor(d){ - let material = this.viewer.getMaterial(); - if (material === Potree.PointColorType.RGB) { - let [r, g, b] = d.color.map(e => parseInt(e)); - - return `rgb( ${r}, ${g}, ${b})`; - } else if (material === Potree.PointColorType.INTENSITY) { - let irange = viewer.getIntensityRange(); - let i = parseInt(255 * (d.intensity - irange[0]) / (irange[1] - irange[0])); - - return `rgb(${i}, ${i}, ${i})`; - } else if (material === Potree.PointColorType.CLASSIFICATION) { - let classif = this.viewer.scene.pointclouds[0].material.classification; - if (typeof classif[d.classification] != 'undefined'){ - let color = 'rgb(' + classif[d.classification].x * 100 + '%,'; - color += classif[d.classification].y * 100 + '%,'; - color += classif[d.classification].z * 100 + '%)'; - return color; - } else { - return 'rgb(255,255,255)'; - } - } else if (material === Potree.PointColorType.HEIGHT) { - return d.heightColor; - } else if (material === Potree.PointColorType.RETURN_NUMBER) { - - if(d.numberOfReturns === 1){ - return 'rgb(255, 255, 0)'; - }else{ - if(d.returnNumber === 1){ - return 'rgb(255, 0, 0)'; - }else if(d.returnNumber === d.numberOfReturns){ - return 'rgb(0, 0, 255)'; - }else{ - return 'rgb(0, 255, 0)'; - } - } - - return d.heightColor; - } else { - return d.color; - } - } - draw(profile){ if(!this.enabled){ return; @@ -302,7 +244,7 @@ Potree.Viewer.Profile = class ProfileWindow{ // 2d profile draws don't need frame-by-frame granularity, it's // fine to handle a redraw once every 100ms let executeScheduledDraw = () => { - this.actuallyDraw(this.scheduledDraw); + this.prepareDraw(this.scheduledDraw); this.scheduledDraw = null; }; @@ -312,16 +254,9 @@ Potree.Viewer.Profile = class ProfileWindow{ this.scheduledDraw = profile; } - actuallyDraw(profile){ - - if(this.context){ - let containerWidth = this.element.clientWidth; - let containerHeight = this.element.clientHeight; - - let width = containerWidth - (this.margin.left + this.margin.right); - let height = containerHeight - (this.margin.top + this.margin.bottom); - this.context.clearRect(0, 0, width, height); - } + prepareDraw(profile){ + this.numPoints = 0; + this.geometry.setDrawRange(0, this.numPoints); if(this.currentProfile){ this.currentProfile.removeEventListener("marker_moved", this.redraw); @@ -345,200 +280,6 @@ Potree.Viewer.Profile = class ProfileWindow{ viewer.addEventListener("height_range_changed", this.redraw); viewer.addEventListener("intensity_range_changed", this.redraw); } - - if(!this.__drawData){ - this.__drawData = {}; - } - this.points = []; - this.rangeX = [Infinity, -Infinity]; - this.rangeY = [Infinity, -Infinity]; - - this.pointsProcessed = 0; - - for(let request of this.requests){ - request.cancel(); - } - this.requests = []; - - let drawPoints = (points, rangeX, rangeY) => { - - let mileage = 0; - for(let i = 0; i < profile.points.length; i++){ - let point = profile.points[i]; - - if(i > 0){ - let previous = profile.points[i-1]; - let dx = point.x - previous.x; - let dy = point.y - previous.y; - let distance = Math.sqrt(dx * dx + dy * dy); - mileage += distance; - } - - let radius = 4; - - let cx = this.scaleX(mileage); - let cy = this.context.canvas.clientHeight; - - this.context.beginPath(); - this.context.arc(cx, cy, radius, 0, 2 * Math.PI, false); - this.context.fillStyle = '#a22'; - this.context.fill(); - }; - - - let pointSize = 2; - let i = -1, n = points.length, d, cx, cy; - while (++i < n) { - d = points[i]; - cx = this.scaleX(d.distance); - cy = this.scaleY(d.altitude); - this.context.beginPath(); - this.context.moveTo(cx, cy); - this.context.fillStyle = this.strokeColor(d); - this.context.fillRect(cx, cy, pointSize, pointSize); - } - }; - - let projectedBoundingBox = null; - - let setupAndDraw = () => { - let containerWidth = this.element.clientWidth; - let containerHeight = this.element.clientHeight; - - let width = containerWidth - (this.margin.left + this.margin.right); - let height = containerHeight - (this.margin.top + this.margin.bottom); - - this.scaleX = d3.scale.linear(); - this.scaleY = d3.scale.linear(); - - let domainProfileWidth = this.rangeX[1] - this.rangeX[0]; - let domainProfileHeight = this.rangeY[1] - this.rangeY[0]; - let domainRatio = domainProfileWidth / domainProfileHeight; - let rangeProfileWidth = width; - let rangeProfileHeight = height; - let rangeRatio = rangeProfileWidth / rangeProfileHeight; - - if(domainRatio < rangeRatio){ - // canvas scale - let targetWidth = domainProfileWidth * (rangeProfileHeight / domainProfileHeight); - this.scaleY.range([height, 0]); - this.scaleX.range([width / 2 - targetWidth / 2, width / 2 + targetWidth / 2]); - - // axis scale - let domainScale = rangeRatio / domainRatio; - let domainScaledWidth = domainProfileWidth * domainScale; - this.axisScaleX = d3.scale.linear() - .domain([ - domainProfileWidth / 2 - domainScaledWidth / 2 , - domainProfileWidth / 2 + domainScaledWidth / 2 ]) - .range([0, width]); - this.axisScaleY = d3.scale.linear() - .domain(this.rangeY) - .range([height, 0]); - }else{ - // canvas scale - let targetHeight = domainProfileHeight* (rangeProfileWidth / domainProfileWidth); - this.scaleX.range([0, width]); - this.scaleY.range([height / 2 + targetHeight / 2, height / 2 - targetHeight / 2]); - - // axis scale - let domainScale = domainRatio / rangeRatio; - let domainScaledHeight = domainProfileHeight * domainScale; - let domainHeightCentroid = (this.rangeY[1] + this.rangeY[0]) / 2; - this.axisScaleX = d3.scale.linear() - .domain(this.rangeX) - .range([0, width]); - this.axisScaleY = d3.scale.linear() - .domain([ - domainHeightCentroid - domainScaledHeight / 2 , - domainHeightCentroid + domainScaledHeight / 2 ]) - .range([height, 0]); - } - this.scaleX.domain(this.rangeX); - this.scaleY.domain(this.rangeY); - - - - this.axisZoom = d3.behavior.zoom() - .x(this.axisScaleX) - .y(this.axisScaleY) - .scaleExtent([0,128]) - .size([width, height]); - - this.zoom = d3.behavior.zoom() - .x(this.scaleX) - .y(this.scaleY) - .scaleExtent([0,128]) - .size([width, height]) - .on("zoom", () => { - this.axisZoom.translate(this.zoom.translate()); - this.axisZoom.scale(this.zoom.scale()); - - let svg = d3.select("svg#profileSVG"); - svg.select(".x.axis").call(xAxis); - svg.select(".y.axis").call(yAxis); - - this.context.clearRect(0, 0, width, height); - drawPoints(this.points, this.rangeX, this.rangeY); - }); - - this.context = d3.select("#profileCanvas") - .attr("width", width) - .attr("height", height) - .call(this.zoom) - .node().getContext("2d"); - - d3.select("svg#profileSVG").selectAll("*").remove(); - - let svg = d3.select("svg#profileSVG") - .call(this.zoom) - .attr("width", (width + this.margin.left + this.margin.right).toString()) - .attr("height", (height + this.margin.top + this.margin.bottom).toString()) - .attr("transform", "translate(" + this.margin.left + "," + this.margin.top + ")"); - - let scope = this; - d3.select("#profileCanvas").on("mousemove", function(){ - let coord = d3.mouse(this); - scope.pointHighlight(coord); - }); - - // Create x axis - let xAxis = d3.svg.axis() - .scale(this.axisScaleX) - .innerTickSize(-height) - .outerTickSize(5) - .orient("bottom") - .ticks(10, "m"); - - // Create y axis - let yAxis = d3.svg.axis() - .scale(this.axisScaleY) - .innerTickSize(-width) - .outerTickSize(5) - .orient("left") - .ticks(10, "m"); - - // Append axis to the chart - let gx = svg.append("g") - .attr("class", "x axis") - .call(xAxis); - - svg.append("g") - .attr("class", "y axis") - .call(yAxis); - - if(navigator.userAgent.indexOf("Firefox") == -1 ) { - svg.select(".y.axis").attr("transform", "translate("+ (this.margin.left).toString() + "," + this.margin.top.toString() + ")"); - svg.select(".x.axis").attr("transform", "translate(" + this.margin.left.toString() + "," + (height + this.margin.top).toString() + ")"); - } else { - svg.select(".x.axis").attr("transform", "translate( 0 ," + height.toString() + ")"); - } - - drawPoints(this.points, this.rangeX, this.rangeY); - - document.getElementById("profile_num_points").innerHTML = Potree.utils.addCommas(this.pointsProcessed) + " "; - }; - for(let pointcloud of this.viewer.scene.pointclouds.filter(p => p.visible)){ @@ -548,27 +289,11 @@ Potree.Viewer.Profile = class ProfileWindow{ return; } - if(!projectedBoundingBox){ - projectedBoundingBox = event.points.projectedBoundingBox; - }else{ - projectedBoundingBox.union(event.points.projectedBoundingBox); - } + this.handleNewPoints(event.points); - let result = this.preparePoints(event.points); - let points = result.data; - this.points = this.points.concat(points); + this.render(); - let batchRangeX = [d3.min(points, function(d) { return d.distance; }), d3.max(points, function(d) { return d.distance; })]; - let batchRangeY = [d3.min(points, function(d) { return d.altitude; }), d3.max(points, function(d) { return d.altitude; })]; - - this.rangeX = [ Math.min(this.rangeX[0], batchRangeX[0]), Math.max(this.rangeX[1], batchRangeX[1]) ]; - this.rangeY = [ Math.min(this.rangeY[0], batchRangeY[0]), Math.max(this.rangeY[1], batchRangeY[1]) ]; - - this.pointsProcessed += result.data.length; - - setupAndDraw(); - - if(this.pointsProcessed > this.threshold){ + if(this.numPoints > this.threshold){ this.cancel(); } }, @@ -588,6 +313,7 @@ Potree.Viewer.Profile = class ProfileWindow{ } } + setThreshold(value){ this.threshold = value; @@ -685,7 +411,7 @@ Potree.Viewer.Profile = class ProfileWindow{ if(diagonal > 100*1000){ scale = new THREE.Vector3(0.01, 0.01, 0.01); }else{ - scale = new THREE.Vector3(0.001, 0.001, 0.001); + scale = new THREE.Vector3(1.1, 0.001, 0.001); } let setString = function(string, offset, buffer){ From c534f002b22308dd66f6c188a561cb4fc495e8bf Mon Sep 17 00:00:00 2001 From: mschuetz Date: Mon, 24 Apr 2017 19:10:43 +0200 Subject: [PATCH 2/6] progress on profile --- src/ProfileRequest.js | 34 ++- src/navigation/InputHandler.js | 2 +- src/viewer/potree.css | 53 +++- src/viewer/profile.html | 2 +- src/viewer/profile.js | 460 +++++++++++++++++++++++++++------ 5 files changed, 451 insertions(+), 100 deletions(-) diff --git a/src/ProfileRequest.js b/src/ProfileRequest.js index d7cb4a9e..e995b9fd 100644 --- a/src/ProfileRequest.js +++ b/src/ProfileRequest.js @@ -78,7 +78,7 @@ Potree.ProfileData = function(profile){ var size = 0; for(var i = 0; i < this.segments.length; i++){ if(this.segments[i].points){ - size += this.segments[i].points.numPoints; + size += this.segments[i].numPoints; } } return size; @@ -94,6 +94,7 @@ Potree.ProfileRequest = function(pointcloud, profile, maxDepth, callback){ this.callback = callback; this.temporaryResult = new Potree.ProfileData(this.profile); this.pointsServed = 0; + this.highestLevelServed = 0; this.priorityQueue = new BinaryHeap(function(x){return 1 / x.weight;}); @@ -149,6 +150,7 @@ Potree.ProfileRequest = function(pointcloud, profile, maxDepth, callback){ // add points to result intersectedNodes.push(node); Potree.getLRU().touch(node); + this.highestLevelServed = node.getLevel(); if((node.level % node.pcoGeometry.hierarchyStepSize) === 0 && node.hasChildren){ this.traverse(node); @@ -189,6 +191,7 @@ Potree.ProfileRequest = function(pointcloud, profile, maxDepth, callback){ this.getPointsInsideProfile = function(nodes, target){ + let totalMileage = 0; for(var pi = 0; pi < target.segments.length; pi++){ var segment = target.segments[pi]; @@ -202,7 +205,7 @@ Potree.ProfileRequest = function(pointcloud, profile, maxDepth, callback){ if(!segment.points){ segment.points = {}; - segment.points.boundingBox = new THREE.Box3(); + segment.boundingBox = new THREE.Box3(); for (var property in geometry.attributes) { if (geometry.attributes.hasOwnProperty(property)) { @@ -213,8 +216,13 @@ Potree.ProfileRequest = function(pointcloud, profile, maxDepth, callback){ } } } + + segment.points["mileage"] = []; } + let sv = new THREE.Vector3().subVectors(segment.end, segment.start).setZ(0); + let segmentDir = sv.clone().normalize(); + for(var i = 0; i < numPoints; i++){ var pos = new THREE.Vector3(p[3*i], p[3*i+1], p[3*i+2]); pos.applyMatrix4(pointcloud.matrixWorld); @@ -222,7 +230,13 @@ Potree.ProfileRequest = function(pointcloud, profile, maxDepth, callback){ var centerDistance = Math.abs(segment.halfPlane.distanceToPoint(pos)); if(distance < profile.width / 2 && centerDistance < segment.length / 2){ - segment.points.boundingBox.expandByPoint(pos); + segment.boundingBox.expandByPoint(pos); + + let svp = new THREE.Vector3().subVectors(pos, segment.start); + let localMileage = segmentDir.dot(svp); + + let mileage = localMileage + totalMileage; + segment.points["mileage"].push(mileage); for (var property in geometry.attributes) { if (geometry.attributes.hasOwnProperty(property)) { @@ -252,18 +266,24 @@ Potree.ProfileRequest = function(pointcloud, profile, maxDepth, callback){ } } - segment.points.numPoints = segment.points.position.length; + segment.numPoints = segment.points.position.length; - if(segment.points.numPoints > 0){ - target.boundingBox.expandByPoint(segment.points.boundingBox.min); - target.boundingBox.expandByPoint(segment.points.boundingBox.max); + if(segment.numPoints > 0){ + target.boundingBox.expandByPoint(segment.boundingBox.min); + target.boundingBox.expandByPoint(segment.boundingBox.max); target.projectedBoundingBox.expandByPoint(new THREE.Vector2(0, target.boundingBox.min.y)); target.projectedBoundingBox.expandByPoint(new THREE.Vector2(0, target.boundingBox.max.y)); } + + totalMileage += segment.length; } }; + this.finishLevelThenCancel = function(){ + this.maxDepth = this.highestLevelServed; + }; + this.cancel = function(){ callback.onCancel(); diff --git a/src/navigation/InputHandler.js b/src/navigation/InputHandler.js index 13883a49..e9ca2ec1 100644 --- a/src/navigation/InputHandler.js +++ b/src/navigation/InputHandler.js @@ -184,7 +184,7 @@ Potree.InputHandler = class InputHandler extends THREE.EventDispatcher{ this.pressedKeys[e.keyCode] = true; - e.preventDefault(); + //e.preventDefault(); } onKeyUp(e){ diff --git a/src/viewer/potree.css b/src/viewer/potree.css index 08e0519f..6728c542 100644 --- a/src/viewer/potree.css +++ b/src/viewer/potree.css @@ -265,17 +265,21 @@ body{ } .axis path{ - fill: none; - stroke: var(--font-color); - shape-rendering: crispEdges; - opacity: 0.7; + fill: rgba(255, 255, 255, 0.5); + stroke: var(--font-color); + shape-rendering: crispEdges; + opacity: 0.7; } .axis line { - fill: none; - stroke: var(--font-color); - shape-rendering: crispEdges; - opacity: 0.1; + fill: rgba(255, 255, 255, 0.5); + stroke: var(--font-color); + shape-rendering: crispEdges; + opacity: 0.1; +} + +.tick text{ + font-size: 12px; } .measurement_item{ @@ -702,3 +706,36 @@ body{ bottom: -5px; opacity: 1.0; } + + + + + + + + + + + + +/* +.axis path, + .axis line{ + fill: none; + stroke: white; + } + + .line{ + fill: none; + stroke: blue; + stroke-width: 2px; + } + + .tick text{ + font-size: 12px; + } + + .tick line{ + opacity: 0.2; + } + */ \ No newline at end of file diff --git a/src/viewer/profile.html b/src/viewer/profile.html index 6d489325..69f0fd15 100644 --- a/src/viewer/profile.html +++ b/src/viewer/profile.html @@ -34,7 +34,7 @@ width: 100%; flex-grow: 1; position: relative; height: 100%; - box-sizing: border-box;"> + box-sizing: border-box; user-select: none">
{ - // this.render(); - //}); + this.mouseIsDown = false; + + $(window).resize( () => { + this.render(); + }); + + this.renderArea.mousedown(e => { + this.mouseIsDown = true; + }); + + this.renderArea.mouseup(e => { + this.mouseIsDown = false; + }); + + this.renderArea.mousemove( e => { + let rect = this.renderArea[0].getBoundingClientRect(); + let x = e.clientX - rect.left; + let y = e.clientY - rect.top; + + let newMouse = new THREE.Vector2(x, y); + + + if(this.mouseIsDown){ + let cPos = this.toCamSpace(new THREE.Vector3(this.mouse.x, this.mouse.y, 0)); + let ncPos = this.toCamSpace(new THREE.Vector3(newMouse.x, newMouse.y, 0)); + + let diff = new THREE.Vector3().subVectors(ncPos, cPos); + this.camera.position.sub(diff); + this.render(); + } + + this.mouse.copy(newMouse); + }); + + let onWheel = e => { + this.autoFit = false; + let delta = 0; + if( e.wheelDelta !== undefined ) { // WebKit / Opera / Explorer 9 + delta = e.wheelDelta; + } else if ( e.detail !== undefined ) { // Firefox + delta = -e.detail; + } + + let ndelta = Math.sign(delta); + + let sPos = new THREE.Vector3(this.mouse.x, this.mouse.y, 0); + let cPos = this.toCamSpace(sPos); + + if(ndelta > 0){ + // + 10% + this.scale.multiplyScalar(1.1); + }else{ + // - 10% + this.scale.multiplyScalar(100/110); + } + + this.scale.max(new THREE.Vector3(0.5, 0.5, 0.5)); + this.scale.min(new THREE.Vector3(100, 100, 100)); + + let ncPos = this.toCamSpace(sPos); + + let diff = new THREE.Vector3().subVectors(ncPos, cPos); + this.camera.position.sub(diff); + + this.render(); + }; + $(this.renderArea)[0].addEventListener("mousewheel", onWheel, false ); + $(this.renderArea)[0].addEventListener("DOMMouseScroll", onWheel, false ); // Firefox this.scheduledDraw = null; @@ -78,24 +240,104 @@ Potree.Viewer.Profile = class ProfileWindow{ }else{ $('#profile_window').css("height", "30%"); } + + this.render(); }); } + toCamSpace(coord){ + let width = this.renderArea[0].clientWidth; + let height = this.renderArea[0].clientHeight; + + let cam = new THREE.Vector3( + coord.x - width / 2, + -coord.y + height / 2, + 0); + + cam.divide(this.scale); + cam.add(this.camera.position); + + return cam; + } + + toScreenSpace(coord){ + let width = this.renderArea[0].clientWidth; + let height = this.renderArea[0].clientHeight; + + let v = new THREE.Vector3().subVectors(coord, this.camera.position); + v.multiply(this.scale); + + let screen = new THREE.Vector3( + width / 2 + v.x, + height / 2 - v.y, + 0); + + return screen; + } + render(){ let width = this.renderArea[0].clientWidth; let height = this.renderArea[0].clientHeight; - this.camera.left = -width/2; - this.camera.right = width/2; - this.camera.top = height/2; - this.camera.bottom = -height/2; - this.camera.updateProjectionMatrix(); + { // THREEJS + + let left = (-width / 2) / this.scale.x; + let right = (+width / 2) / this.scale.x; + let top = (+height / 2) / this.scale.y; + let bottom = (-height / 2) / this.scale.y; + + this.camera.left = left; + this.camera.right = right; + this.camera.top = top; + this.camera.bottom = bottom; + this.camera.updateProjectionMatrix(); + + this.renderer.setSize(width, height); + + this.renderer.render(this.scene, this.camera); + } - this.renderer.setSize(width, height); + { // SVG SCALES + let marginLeft = viewer._2dprofile.renderArea[0].offsetLeft; - this.renderer.render(this.scene, this.camera); + let svg = d3.select("svg#profileSVG"); + svg.selectAll("*").remove(); + + this.scaleX = d3.scale.linear() + .domain([this.camera.left + this.camera.position.x, this.camera.right + this.camera.position.x]) + .range([0, width]); + this.scaleY = d3.scale.linear() + .domain([this.camera.bottom + this.camera.position.y, this.camera.top + this.camera.position.y]) + .range([height, 0]); + + let xAxis = d3.svg.axis() + .scale(this.scaleX) + .orient("bottom") + .innerTickSize(-height) + .outerTickSize(0) + .tickPadding(10) + .ticks(width / 50); + + let yAxis = d3.svg.axis() + .scale(this.scaleY) + .orient("left") + .innerTickSize(-width) + .outerTickSize(0) + .tickPadding(10) + .ticks(height / 20); + + svg.append("g") + .attr("class", "x axis") + .attr("transform", `translate(${marginLeft}, ${height})`) + .call(xAxis); + + svg.append("g") + .attr("class", "y axis") + .attr("transform", `translate(${marginLeft}, 0)`) + .call(yAxis) + } } show(){ @@ -116,6 +358,8 @@ Potree.Viewer.Profile = class ProfileWindow{ }; handleNewPoints(profileProgress){ + this.points.onProgress(profileProgress); + let prepared = this.preparePoints(profileProgress); if(prepared.numPoints === 0){ @@ -133,18 +377,16 @@ Potree.Viewer.Profile = class ProfileWindow{ this.min.min(pos); this.max.max(pos); } + let newSize = new THREE.Vector3().subVectors(this.max, this.min); + this.size.copy(newSize); let newNumPoints = this.numPoints + prepared.numPoints; let position = this.buffers.position; let color = this.buffers.color; - position.set(prepared.buffers.position, 3 * this.numPoints); - color.set(prepared.buffers.color, 3 * this.numPoints); - - for(let i = 1; i < 100; i+=3){ - position[i] = position[i] - 700; - } + position.set(this.points.buffers.position.slice(3 * this.numPoints, 3 * newNumPoints), 3 * this.numPoints); + color.set(this.points.buffers.color.slice(3 * this.numPoints, 3 * newNumPoints), 3 * this.numPoints); this.geometry.attributes.position.needsUpdate = true; this.geometry.attributes.color.needsUpdate = true; @@ -153,9 +395,25 @@ Potree.Viewer.Profile = class ProfileWindow{ this.numPoints = newNumPoints; - let center = new THREE.Vector3().addVectors(this.min, this.max) + this.center = new THREE.Vector3().addVectors(this.min, this.max) .multiplyScalar(0.5); - this.camera.position.copy(center); + + if(this.autoFit){ // SCALE + let width = this.renderArea[0].clientWidth; + let height = this.renderArea[0].clientHeight; + + let sx = width / this.size.x; + let sy = height / this.size.y; + let scale = Math.min(sx, sy); + + this.scale.set(scale, scale, 1); + this.camera.position.copy(this.center); + } + + this.firstUpdateFollows = false; + + this.elRoot.find("#profile_num_points").html( + Potree.utils.addCommas(this.numPoints.toFixed(0))); } preparePoints(profileProgress){ @@ -179,26 +437,21 @@ Potree.Viewer.Profile = class ProfileWindow{ let i = 0; for(let segment of segments){ let sv = new THREE.Vector3().subVectors(segment.end, segment.start).setZ(0) - let segmentLength = sv.length(); + let segmentDir = sv.clone().normalize(); let points = segment.points; // Iterate the segments' points - for(let j = 0; j < points.numPoints; j++){ + for(let j = 0; j < segment.numPoints; j++){ let p = points.position[j]; - let pl = new THREE.Vector3().subVectors(p, segment.start).setZ(0); + + //let svp = new THREE.Vector3().subVectors(p, segment.start); + //let distance = segmentDir.dot(svp); min.min(p); max.max(p); - // TODO length is probably wrong here - let distance = totalDistance + pl.length(); - - // important are - // distance - // p.z - // points.color ? points.color[j] : [0, 0, 0] - - position[3*i + 0] = distance; + //position[3*i + 0] = distance + totalDistance; + position[3*i + 0] = points.mileage[j]; position[3*i + 1] = p.z; position[3*i + 2] = 0; @@ -211,7 +464,7 @@ Potree.Viewer.Profile = class ProfileWindow{ i++; } - totalDistance += segmentLength; + totalDistance += segment.length; } let result = { @@ -255,6 +508,7 @@ Potree.Viewer.Profile = class ProfileWindow{ } prepareDraw(profile){ + this.autoFit = true; this.numPoints = 0; this.geometry.setDrawRange(0, this.numPoints); @@ -266,10 +520,15 @@ Potree.Viewer.Profile = class ProfileWindow{ viewer.removeEventListener("material_changed", this.redraw); viewer.removeEventListener("height_range_changed", this.redraw); viewer.removeEventListener("intensity_range_changed", this.redraw); + + for(let request of this.requests){ + request.cancel(); + } } this.currentProfile = profile; + this.points = new Potree.ProfilePointStore(this.currentProfile); { this.currentProfile.addEventListener("marker_moved", this.redraw); @@ -326,70 +585,80 @@ Potree.Viewer.Profile = class ProfileWindow{ } let file = ""; - let points = this.points.slice(); - points.sort((a, b) => (a.distance - b.distance)); + let pointsPerSegment = this.points.segments.map(s => s.points); + let points = {}; + let attributes = Object.keys(pointsPerSegment[0]); + for(let attribute of attributes){ + let arrays = pointsPerSegment.map(p => p[attribute]); + points[attribute] = arrays.reduce( (a, v) => a.concat(v)); + } { // header-line let header = "x, y"; - if(points[0].hasOwnProperty("color")){ + if(points.hasOwnProperty("color")){ header += ", r, g, b"; } - if(points[0].hasOwnProperty("intensity")){ + if(points.hasOwnProperty("intensity")){ header += ", intensity"; } - if(points[0].hasOwnProperty("classification")){ + if(points.hasOwnProperty("classification")){ header += ", classification"; } - if(points[0].hasOwnProperty("numberOfReturns")){ + if(points.hasOwnProperty("numberOfReturns")){ header += ", numberOfReturns"; } - if(points[0].hasOwnProperty("pointSourceID")){ + if(points.hasOwnProperty("pointSourceID")){ header += ", pointSourceID"; } - if(points[0].hasOwnProperty("returnNumber")){ + if(points.hasOwnProperty("returnNumber")){ header += ", returnNumber"; } file += header + "\n"; } + + let numPoints = points.position.length; // actual data - for(let point of points){ - let line = point.distance.toFixed(4) + ", "; - line += point.altitude.toFixed(4) + ", "; + for(let i = 0; i < numPoints; i++){ - if(point.hasOwnProperty("color")){ - line += point.color.join(", "); + let values = []; + + values.push(points.mileage[i].toFixed(4)); + values.push(points.position[i].z.toFixed(4)); + + if(attributes.includes("color")){ + values.push(points.color[i].join(", ")); } - if(point.hasOwnProperty("intensity")){ - line += ", " + point.intensity; + if(attributes.includes("intensity")){ + values.push(points.intensity[i]); } - if(point.hasOwnProperty("classification")){ - line += ", " + point.classification; + if(attributes.includes("classification")){ + values.push(points.classification[i]); } - if(point.hasOwnProperty("numberOfReturns")){ - line += ", " + point.numberOfReturns; + if(attributes.includes("numberOfReturns")){ + values.push(points.numberOfReturns[i]); } - if(point.hasOwnProperty("pointSourceID")){ - line += ", " + point.pointSourceID; + if(attributes.includes("pointSourceID")){ + values.push(points.pointSourceID[i]); } - if(point.hasOwnProperty("returnNumber")){ - line += ", " + point.returnNumber; + if(attributes.includes("returnNumber")){ + values.push(points.returnNumber[i]); } - line += "\n"; + let line = values.join(", ") + "\n"; file = file + line; } @@ -398,20 +667,31 @@ Potree.Viewer.Profile = class ProfileWindow{ } getPointsInProfileAsLas(){ - let points = this.points; + + let pointsPerSegment = this.points.segments.map(s => s.points); + let points = {}; + let attributes = Object.keys(pointsPerSegment[0]); + for(let attribute of attributes){ + let arrays = pointsPerSegment.map(p => p[attribute]); + points[attribute] = arrays.reduce( (a, v) => a.concat(v)); + } + let numPoints = points.position.length; + let boundingBox = new THREE.Box3(); - for(let point of points){ - boundingBox.expandByPoint(point); + for(let i = 0; i < numPoints; i++){ + let position = points.position[i]; + boundingBox.expandByPoint(position); } + let offset = boundingBox.min.clone(); let diagonal = boundingBox.min.distanceTo(boundingBox.max); let scale = new THREE.Vector3(0.01, 0.01, 0.01); if(diagonal > 100*1000){ scale = new THREE.Vector3(0.01, 0.01, 0.01); }else{ - scale = new THREE.Vector3(1.1, 0.001, 0.001); + scale = new THREE.Vector3(0.001, 0.001, 0.001); } let setString = function(string, offset, buffer){ @@ -423,10 +703,9 @@ Potree.Viewer.Profile = class ProfileWindow{ } } - let buffer = new ArrayBuffer(227 + 28 * points.length); + let buffer = new ArrayBuffer(227 + 28 * numPoints); let view = new DataView(buffer); let u8View = new Uint8Array(buffer); - //let u16View = new Uint16Array(buffer); setString("LASF", 0, buffer); u8View[24] = 1; @@ -455,7 +734,7 @@ Potree.Viewer.Profile = class ProfileWindow{ view.setUint16(105, 28, true); // number of point records 107 4 - view.setUint32(107, points.length, true); + view.setUint32(107, numPoints, true); // number of points by return 111 20 @@ -477,10 +756,16 @@ Potree.Viewer.Profile = class ProfileWindow{ // z offset 171 8 view.setFloat64(171, offset.z, true); + let hasIntensity = attributes.includes("intensity"); + let hasReturnNumber = attributes.includes("returnNumber"); + let hasClassification = attributes.includes("classification"); + let hasPointSourceID = attributes.includes("pointSourceID"); + let boffset = 227; - for(let i = 0; i < points.length; i++){ - let point = points[i]; - let position = new THREE.Vector3(point.x, point.y, point.z); + for(let i = 0; i < numPoints; i++){ + //let point = points[i]; + //let position = new THREE.Vector3(point.x, point.y, point.z); + let position = points.position[i]; let ux = parseInt((position.x - offset.x) / scale.x); let uy = parseInt((position.y - offset.y) / scale.y); @@ -489,21 +774,30 @@ Potree.Viewer.Profile = class ProfileWindow{ view.setUint32(boffset + 0, ux, true); view.setUint32(boffset + 4, uy, true); view.setUint32(boffset + 8, uz, true); + + if(hasIntensity){ + view.setUint16(boffset + 12, (points.intensity[i]), true); + } - view.setUint16(boffset + 12, (point.intensity), true); - let rt = point.returnNumber; - rt += (point.numberOfReturns << 3); - view.setUint8(boffset + 14, rt); + if(hasReturnNumber){ + let rt = points.returnNumber[i]; + rt += (points.numberOfReturns[i] << 3); + view.setUint8(boffset + 14, rt); + } + + if(hasClassification){ + view.setUint8(boffset + 15, points.classification[i]); + } - // classification - view.setUint8(boffset + 15, point.classification); // scan angle rank // user data // point source id - view.setUint16(boffset + 18, point.pointSourceID); - view.setUint16(boffset + 20, (point.color[0] * 255), true); - view.setUint16(boffset + 22, (point.color[1] * 255), true); - view.setUint16(boffset + 24, (point.color[2] * 255), true); + if(hasPointSourceID){ + view.setUint16(boffset + 18, points.pointSourceID[i]); + } + view.setUint16(boffset + 20, (points.color[i][0] * 255), true); + view.setUint16(boffset + 22, (points.color[i][1] * 255), true); + view.setUint16(boffset + 24, (points.color[i][2] * 255), true); boffset += 28; } From 68464ce342bdd53579317a2b4eeceac2eb3dd9b9 Mon Sep 17 00:00:00 2001 From: mschuetz Date: Tue, 25 Apr 2017 14:36:34 +0200 Subject: [PATCH 3/6] make profile window movable and resizable --- build/potree/potree.css | 56 +- build/potree/potree.js | 1197 ++++++++++++++++++------------- build/potree/profile.html | 32 +- examples/elevation_profile.html | 2 +- src/ProfileRequest.js | 1 - src/viewer/potree.css | 3 + src/viewer/profile.html | 26 +- src/viewer/profile.js | 296 ++++++-- src/viewer/viewer.js | 12 +- 9 files changed, 1043 insertions(+), 582 deletions(-) diff --git a/build/potree/potree.css b/build/potree/potree.css index 08e0519f..8b4463ad 100644 --- a/build/potree/potree.css +++ b/build/potree/potree.css @@ -257,6 +257,9 @@ canvas { body{ margin: 0; padding: 0; + position: absolute; + width: 100%; + height: 100%; } .axis { @@ -265,17 +268,21 @@ body{ } .axis path{ - fill: none; - stroke: var(--font-color); - shape-rendering: crispEdges; - opacity: 0.7; + fill: rgba(255, 255, 255, 0.5); + stroke: var(--font-color); + shape-rendering: crispEdges; + opacity: 0.7; } .axis line { - fill: none; - stroke: var(--font-color); - shape-rendering: crispEdges; - opacity: 0.1; + fill: rgba(255, 255, 255, 0.5); + stroke: var(--font-color); + shape-rendering: crispEdges; + opacity: 0.1; +} + +.tick text{ + font-size: 12px; } .measurement_item{ @@ -702,3 +709,36 @@ body{ bottom: -5px; opacity: 1.0; } + + + + + + + + + + + + +/* +.axis path, + .axis line{ + fill: none; + stroke: white; + } + + .line{ + fill: none; + stroke: blue; + stroke-width: 2px; + } + + .tick text{ + font-size: 12px; + } + + .tick line{ + opacity: 0.2; + } + */ \ No newline at end of file diff --git a/build/potree/potree.js b/build/potree/potree.js index abb7a3c5..288f5bed 100644 --- a/build/potree/potree.js +++ b/build/potree/potree.js @@ -3968,7 +3968,7 @@ Potree.InputHandler = class InputHandler extends THREE.EventDispatcher{ this.pressedKeys[e.keyCode] = true; - e.preventDefault(); + //e.preventDefault(); } onKeyUp(e){ @@ -6508,7 +6508,7 @@ Potree.ProfileData = function(profile){ var size = 0; for(var i = 0; i < this.segments.length; i++){ if(this.segments[i].points){ - size += this.segments[i].points.numPoints; + size += this.segments[i].numPoints; } } return size; @@ -6524,6 +6524,7 @@ Potree.ProfileRequest = function(pointcloud, profile, maxDepth, callback){ this.callback = callback; this.temporaryResult = new Potree.ProfileData(this.profile); this.pointsServed = 0; + this.highestLevelServed = 0; this.priorityQueue = new BinaryHeap(function(x){return 1 / x.weight;}); @@ -6579,12 +6580,12 @@ Potree.ProfileRequest = function(pointcloud, profile, maxDepth, callback){ // add points to result intersectedNodes.push(node); Potree.getLRU().touch(node); + this.highestLevelServed = node.getLevel(); if((node.level % node.pcoGeometry.hierarchyStepSize) === 0 && node.hasChildren){ this.traverse(node); } }else{ - console.log("loading " + node.name); node.load(); this.priorityQueue.push(element); } @@ -6619,6 +6620,7 @@ Potree.ProfileRequest = function(pointcloud, profile, maxDepth, callback){ this.getPointsInsideProfile = function(nodes, target){ + let totalMileage = 0; for(var pi = 0; pi < target.segments.length; pi++){ var segment = target.segments[pi]; @@ -6632,7 +6634,7 @@ Potree.ProfileRequest = function(pointcloud, profile, maxDepth, callback){ if(!segment.points){ segment.points = {}; - segment.points.boundingBox = new THREE.Box3(); + segment.boundingBox = new THREE.Box3(); for (var property in geometry.attributes) { if (geometry.attributes.hasOwnProperty(property)) { @@ -6643,8 +6645,13 @@ Potree.ProfileRequest = function(pointcloud, profile, maxDepth, callback){ } } } + + segment.points["mileage"] = []; } + let sv = new THREE.Vector3().subVectors(segment.end, segment.start).setZ(0); + let segmentDir = sv.clone().normalize(); + for(var i = 0; i < numPoints; i++){ var pos = new THREE.Vector3(p[3*i], p[3*i+1], p[3*i+2]); pos.applyMatrix4(pointcloud.matrixWorld); @@ -6652,7 +6659,13 @@ Potree.ProfileRequest = function(pointcloud, profile, maxDepth, callback){ var centerDistance = Math.abs(segment.halfPlane.distanceToPoint(pos)); if(distance < profile.width / 2 && centerDistance < segment.length / 2){ - segment.points.boundingBox.expandByPoint(pos); + segment.boundingBox.expandByPoint(pos); + + let svp = new THREE.Vector3().subVectors(pos, segment.start); + let localMileage = segmentDir.dot(svp); + + let mileage = localMileage + totalMileage; + segment.points["mileage"].push(mileage); for (var property in geometry.attributes) { if (geometry.attributes.hasOwnProperty(property)) { @@ -6682,18 +6695,24 @@ Potree.ProfileRequest = function(pointcloud, profile, maxDepth, callback){ } } - segment.points.numPoints = segment.points.position.length; + segment.numPoints = segment.points.position.length; - if(segment.points.numPoints > 0){ - target.boundingBox.expandByPoint(segment.points.boundingBox.min); - target.boundingBox.expandByPoint(segment.points.boundingBox.max); + if(segment.numPoints > 0){ + target.boundingBox.expandByPoint(segment.boundingBox.min); + target.boundingBox.expandByPoint(segment.boundingBox.max); target.projectedBoundingBox.expandByPoint(new THREE.Vector2(0, target.boundingBox.min.y)); target.projectedBoundingBox.expandByPoint(new THREE.Vector2(0, target.boundingBox.max.y)); } + + totalMileage += segment.length; } }; + this.finishLevelThenCancel = function(){ + this.maxDepth = this.highestLevelServed; + }; + this.cancel = function(){ callback.onCancel(); @@ -14083,14 +14102,22 @@ Potree.Viewer = class PotreeViewer extends THREE.EventDispatcher{ }); let elProfile = $('
').load(new URL(Potree.scriptPath + "/profile.html").href, () => { - $('#potree_render_area').append(elProfile.children()); - this._2dprofile = new Potree.Viewer.Profile(this, document.getElementById("profile_draw_container")); + $(document.body).append(elProfile.children()); + this._2dprofile = new Potree.Viewer.Profile(this); + + $("#profile_window").draggable({ + handle: $("#profile_titlebar"), + containment: $(document.body) + }); + $("#profile_window").resizable(); if(callback){ $(callback); } }); + + }); @@ -15224,64 +15251,505 @@ class EDLRenderer{ }; +Potree.ProfilePointStore = class ProfilePointStore{ + + constructor(profile){ + this.profile = profile; + this.boundingBox = new THREE.Box3(); + this.projectedBoundingBox = new THREE.Box3(); + this.maxPoints = 50*1000; + this.numPoints = 0; + + this.buffers = { + position: new Float32Array(3 * this.maxPoints), + color: new Uint8Array(3 * this.maxPoints) + }; + + this.segments = []; + for(let i = 0; i < profile.points.length - 1; i++){ + let start = profile.points[i + 0]; + let end = profile.points[i + 1]; + + let startGround = new THREE.Vector3(start.x, start.y, 0); + let endGround = new THREE.Vector3(end.x, end.y, 0); + + let center = new THREE.Vector3().addVectors(endGround, startGround).multiplyScalar(0.5); + let length = startGround.distanceTo(endGround); + + let segment = { + start: start, + end: end, + points: {}, + length: length, + numPoints: 0 + }; + + this.segments.push(segment); + } + } + + onProgress(progress){ + + if(this.segments.length !== progress.segments.length){ + return; + } + + + for(let i = 0; i < progress.segments.length; i++){ + let sourceSegment = progress.segments[i]; + let targetSegment = this.segments[i]; + + for(let attribute of Object.keys(sourceSegment.points)){ + if(targetSegment.points[attribute] === undefined){ + targetSegment.points[attribute] = new Array(targetSegment.numPoints).fill(0); + } + + targetSegment.points[attribute] = targetSegment.points[attribute] + .concat(sourceSegment.points[attribute]); + + if(attribute === "position"){ + for(let j = 0; j < sourceSegment.numPoints; j++){ + + let x = sourceSegment.points.mileage[j]; + let y = sourceSegment.points.position[j].z; + let z = 0; + + let offset = 3 * (this.numPoints + j); + + this.buffers.position[offset + 0] = x; + this.buffers.position[offset + 1] = y; + this.buffers.position[offset + 2] = z; + } + }else if(attribute === "color"){ + for(let j = 0; j < sourceSegment.numPoints; j++){ + + let r = sourceSegment.points.color[j][0]; + let g = sourceSegment.points.color[j][1]; + let b = sourceSegment.points.color[j][2]; + + let offset = 3 * (this.numPoints + j); + + this.buffers.color[offset + 0] = r; + this.buffers.color[offset + 1] = g; + this.buffers.color[offset + 2] = b; + } + } + + } + + targetSegment.numPoints += sourceSegment.numPoints; + this.numPoints += sourceSegment.numPoints; + } + + } + + selectPoint(mileage, elevation, radius){ + + let closest = { + distance: Infinity, + segment: null, + index: null + }; + + for(let segment of this.segments){ + for(let i = 0; i < segment.numPoints; i++){ + + let m = segment.points.mileage[i] - mileage; + let e = segment.points.position[i].z - elevation; + + let r = Math.sqrt(m * m + e * e); + + if(r < radius && r < closest.distance){ + closest = { + distance: r, + segment: segment, + index: i + }; + } + } + } + + if(closest.distance < Infinity){ + let point = {}; + + let attributes = Object.keys(closest.segment.points); + for(let attribute of attributes){ + point[attribute] = closest.segment.points[attribute][closest.index]; + } + + return point; + }else{ + return null; + } + } + + +} + Potree.Viewer.Profile = class ProfileWindow{ - constructor(viewer, element){ + constructor(viewer){ this.viewer = viewer; this.enabled = true; - this.element = element; this.currentProfile = null; this.requests = []; - this.pointsProcessed = 0; - this.margin = {top: 0, right: 0, bottom: 20, left: 40}; this.maximized = false; this.threshold = 20*1000; + this.autoFit = true; + this.numPoints = 0; + this.mouse = new THREE.Vector2(); + this.points = null; + this.selectedPoint = null; + + this.elRoot = $("#profile_window"); + + this.renderArea = this.elRoot.find("#profileCanvasContainer"); + + { // THREE.JS + this.renderer = new THREE.WebGLRenderer({alpha: true, premultipliedAlpha: false}); + this.renderer.setClearColor( 0x000000, 0 ); + this.renderer.setSize(10, 10); + this.renderer.autoClear = true; + this.renderArea.append($(this.renderer.domElement)); + this.renderer.domElement.tabIndex = "2222"; + this.renderer.context.getExtension("EXT_frag_depth"); + $(this.renderer.domElement).css("width", "100%"); + $(this.renderer.domElement).css("height", "100%"); + + this.camera = new THREE.OrthographicCamera(-10, 10, 10, -10, -1000, 1000); + + this.scene = new THREE.Scene(); + + this.maxPoints = 50*1000; + this.geometry = new THREE.BufferGeometry(); + this.buffers = { + position: new Float32Array(3 * this.maxPoints), + color: new Uint8Array(3 * this.maxPoints), + }; + + this.geometry.addAttribute("position", new THREE.BufferAttribute(this.buffers.position, 3)); + this.geometry.addAttribute("color", new THREE.BufferAttribute(this.buffers.color, 3, true)); + this.geometry.setDrawRange(0, 100); + //this.material = new THREE.PointsMaterial( { size: 0.001, vertexColors: THREE.VertexColors } ); + this.material = new Potree.PointCloudMaterial({size: 2.0}); + this.material.pointSizeType = Potree.PointSizeType.FIXED; + this.tPoints = new THREE.Points(this.geometry, this.material); + this.scene.add(this.tPoints); + + + let sg = new THREE.SphereGeometry(1, 16, 16); + let sm = new THREE.MeshNormalMaterial(); + this.pickSphere = new THREE.Mesh(sg, sm); + this.pickSphere.visible = false; + this.scene.add(this.pickSphere); + } + + { // SVG + let width = this.renderArea[0].clientWidth; + let height = this.renderArea[0].clientHeight; + let marginLeft = this.renderArea[0].offsetLeft; + + this.svg = d3.select("svg#profileSVG"); + this.svg.selectAll("*").remove(); + + this.scaleX = d3.scale.linear() + .domain([this.camera.left + this.camera.position.x, this.camera.right + this.camera.position.x]) + .range([0, width]); + this.scaleY = d3.scale.linear() + .domain([this.camera.bottom + this.camera.position.y, this.camera.top + this.camera.position.y]) + .range([height, 0]); + + this.xAxis = d3.svg.axis() + .scale(this.scaleX) + .orient("bottom") + .innerTickSize(-height) + .outerTickSize(1) + .tickPadding(10) + .ticks(width / 50); + + this.yAxis = d3.svg.axis() + .scale(this.scaleY) + .orient("left") + .innerTickSize(-width) + .outerTickSize(1) + .tickPadding(10) + .ticks(height / 20); + + this.svg.append("g") + .attr("class", "x axis") + .attr("transform", `translate(${marginLeft}, ${height})`) + .call(this.xAxis); + + this.svg.append("g") + .attr("class", "y axis") + .attr("transform", `translate(${marginLeft}, 0)`) + .call(this.yAxis); + } + + this.min = new THREE.Vector3(0, 0, 0); + this.max = new THREE.Vector3(0, 0, 0); + this.size = new THREE.Vector3(0, 0, 0); + this.center = new THREE.Vector3(0, 0, 0); + this.scale = new THREE.Vector3(1, 1, 1); + + this.mouseIsDown = false; + + $(window).resize( () => { + this.render(); + }); + + this.renderArea.mousedown(e => { + this.mouseIsDown = true; + }); + + this.renderArea.mouseup(e => { + this.mouseIsDown = false; + }); + + this.renderArea.mousemove( e => { + if(this.points === null || this.points.numPoints === 0){ + return; + } + + let rect = this.renderArea[0].getBoundingClientRect(); + let x = e.clientX - rect.left; + let y = e.clientY - rect.top; + + let newMouse = new THREE.Vector2(x, y); + + if(this.mouseIsDown){ + // DRAG + let cPos = this.toCamSpace(new THREE.Vector3(this.mouse.x, this.mouse.y, 0)); + let ncPos = this.toCamSpace(new THREE.Vector3(newMouse.x, newMouse.y, 0)); + + let diff = new THREE.Vector3().subVectors(ncPos, cPos); + this.camera.position.sub(diff); + this.render(); + }else if(this.points){ + // FIND HOVERED POINT + let pixelRadius = 10; + let ncPos = this.toCamSpace(new THREE.Vector3(newMouse.x, newMouse.y, 0)); + let ncPosBorder = this.toCamSpace(new THREE.Vector3(newMouse.x + pixelRadius, newMouse.y, 0)); + let mileage = ncPos.x; + let elevation = ncPos.y; + let radius = Math.abs(ncPosBorder.x - ncPos.x); + let point = this.points.selectPoint(mileage, elevation, radius); + + if(point){ + this.elRoot.find("#profileSelectionProperties").fadeIn(200); + this.pickSphere.visible = true; + this.pickSphere.scale.set(0.5 * radius, 0.5 * radius, 0.5 * radius); + this.pickSphere.position.set(point.mileage, point.position.z, 0); + + let info = this.elRoot.find("#profileSelectionProperties"); + let html = ""; + for(let attribute of Object.keys(point)){ + let value = point[attribute]; + if(attribute === "position"){ + let values = value.toArray().map(v => Potree.utils.addCommas(v.toFixed(3))); + html += ` + + + + + + + + + + + + `; + }else if(attribute === "color"){ + html += ` + + + + `; + }else if(attribute === "normal"){ + continue; + }else if(attribute === "mileage"){ + html += ` + + + + `; + }else{ + html += ` + + + + `; + } + + } + html += "
x${values[0]}
y${values[1]}
z${values[2]}
${attribute}${value.join(", ")}
${attribute}${value.toFixed(3)}
${attribute}${value}
" + info.html(html); + + this.selectedPoint = point; + }else{ + //this.pickSphere.visible = false; + //this.selectedPoint = null; + } + this.render(); + + + } + + this.mouse.copy(newMouse); + }); + + let onWheel = e => { + this.autoFit = false; + let delta = 0; + if( e.wheelDelta !== undefined ) { // WebKit / Opera / Explorer 9 + delta = e.wheelDelta; + } else if ( e.detail !== undefined ) { // Firefox + delta = -e.detail; + } + + let ndelta = Math.sign(delta); + + let sPos = new THREE.Vector3(this.mouse.x, this.mouse.y, 0); + let cPos = this.toCamSpace(sPos); + + if(ndelta > 0){ + // + 10% + this.scale.multiplyScalar(1.1); + }else{ + // - 10% + this.scale.multiplyScalar(100/110); + } + + this.scale.max(new THREE.Vector3(0.5, 0.5, 0.5)); + this.scale.min(new THREE.Vector3(100, 100, 100)); + + let ncPos = this.toCamSpace(sPos); + + let diff = new THREE.Vector3().subVectors(ncPos, cPos); + this.camera.position.sub(diff); + + this.render(); + }; + $(this.renderArea)[0].addEventListener("mousewheel", onWheel, false ); + $(this.renderArea)[0].addEventListener("DOMMouseScroll", onWheel, false ); // Firefox + this.scheduledDraw = null; - $('#closeProfileContainer').click(() => { - this.hide(); - this.enabled = false; - }); - - $('#profile_toggle_size_button').click(() => { - this.maximized = !this.maximized; - - if(this.maximized){ - $('#profile_window').css("height", "100%"); - }else{ - $('#profile_window').css("height", "30%"); - } - }); - - //this.drawOnChange = (event) => { - // this.redraw(); - //}; - this.redraw = () => { if(this.currentProfile){ this.draw(this.currentProfile); } }; - viewer.addEventListener("height_range_changed", this.redraw); + $('#closeProfileContainer').click(() => { + this.hide(); + this.enabled = false; + }); + + //$('#profile_toggle_size_button').click(() => { + // this.maximized = !this.maximized; + // + // if(this.maximized){ + // $('#profile_window').css("height", "100%"); + // }else{ + // $('#profile_window').css("height", "30%"); + // } + // + // this.render(); + //}); - let width = document.getElementById('profile_window').clientWidth; - let height = document.getElementById('profile_window').clientHeight; - let resizeLoop = () => { - requestAnimationFrame(resizeLoop); - let newWidth = document.getElementById('profile_window').clientWidth; - let newHeight = document.getElementById('profile_window').clientHeight; + } + + toCamSpace(coord){ + let width = this.renderArea[0].clientWidth; + let height = this.renderArea[0].clientHeight; + + let cam = new THREE.Vector3( + coord.x - width / 2, + -coord.y + height / 2, + 0); + + cam.divide(this.scale); + cam.add(this.camera.position); + + return cam; + } + + toScreenSpace(coord){ + let width = this.renderArea[0].clientWidth; + let height = this.renderArea[0].clientHeight; + + let v = new THREE.Vector3().subVectors(coord, this.camera.position); + v.multiply(this.scale); + + let screen = new THREE.Vector3( + width / 2 + v.x, + height / 2 - v.y, + 0); + + return screen; + } + + render(){ + let width = this.renderArea[0].clientWidth; + let height = this.renderArea[0].clientHeight; + + { // THREEJS + + let left = (-width / 2) / this.scale.x; + let right = (+width / 2) / this.scale.x; + let top = (+height / 2) / this.scale.y; + let bottom = (-height / 2) / this.scale.y; + + this.camera.left = left; + this.camera.right = right; + this.camera.top = top; + this.camera.bottom = bottom; + this.camera.updateProjectionMatrix(); - if(newWidth !== width || newHeight !== height){ - setTimeout(this.redraw, 50, {profile: this.currentProfile}); - } - - width = newWidth; - height = newHeight; - }; - requestAnimationFrame(resizeLoop); + let radius = + this.toCamSpace(new THREE.Vector3(0, 0, 0)).distanceTo( + this.toCamSpace(new THREE.Vector3(5, 0, 0))); + this.pickSphere.scale.set(radius, radius, radius); + this.pickSphere.position.z = this.camera.far - radius; + + + this.renderer.setSize(width, height); + + this.renderer.render(this.scene, this.camera); + } + + { // SVG SCALES + let marginLeft = this.renderArea[0].offsetLeft; + + this.scaleX.domain([this.camera.left + this.camera.position.x, this.camera.right + this.camera.position.x]) + .range([0, width]); + this.scaleY.domain([this.camera.bottom + this.camera.position.y, this.camera.top + this.camera.position.y]) + .range([height, 0]); + + this.xAxis.scale(this.scaleX) + .orient("bottom") + .innerTickSize(-height) + .outerTickSize(1) + .tickPadding(10) + .ticks(width / 50); + this.yAxis.scale(this.scaleY) + .orient("left") + .innerTickSize(-width) + .outerTickSize(1) + .tickPadding(10) + .ticks(height / 20); + + d3.select(".x,axis") + .attr("transform", `translate(${marginLeft}, ${height})`) + .call(this.xAxis); + d3.select(".y,axis") + .attr("transform", `translate(${marginLeft}, 0)`) + .call(this.yAxis); + } } show(){ @@ -15301,215 +15769,129 @@ Potree.Viewer.Profile = class ProfileWindow{ this.requests = []; }; - preparePoints(profileProgress){ + handleNewPoints(profileProgress){ + this.points.onProgress(profileProgress); + + let prepared = this.preparePoints(profileProgress); + + if(prepared.numPoints === 0){ + return; + } + + if(this.numPoints === 0){ + this.min.fromArray(prepared.buffers.position, 3); + this.max.fromArray(prepared.buffers.position, 3); + } + + for(let i = 0; i < prepared.numPoints; i++){ + let pos = new THREE.Vector3().fromArray( + prepared.buffers.position.slice(3*i, 3*i+3)); + this.min.min(pos); + this.max.max(pos); + } + let newSize = new THREE.Vector3().subVectors(this.max, this.min); + this.size.copy(newSize); + + let newNumPoints = this.numPoints + prepared.numPoints; + + let position = this.buffers.position; + let color = this.buffers.color; + + position.set(this.points.buffers.position.slice(3 * this.numPoints, 3 * newNumPoints), 3 * this.numPoints); + color.set(this.points.buffers.color.slice(3 * this.numPoints, 3 * newNumPoints), 3 * this.numPoints); + + this.geometry.attributes.position.needsUpdate = true; + this.geometry.attributes.color.needsUpdate = true; + + this.geometry.setDrawRange(0, newNumPoints); + + this.numPoints = newNumPoints; + + this.center = new THREE.Vector3().addVectors(this.min, this.max) + .multiplyScalar(0.5); + + if(this.autoFit){ // SCALE + let width = this.renderArea[0].clientWidth; + let height = this.renderArea[0].clientHeight; + + let sx = width / this.size.x; + let sy = height / this.size.y; + let scale = Math.min(sx, sy); + + this.scale.set(scale, scale, 1); + this.camera.position.copy(this.center); + } + + this.firstUpdateFollows = false; + + this.elRoot.find("#profile_num_points").html( + Potree.utils.addCommas(this.numPoints.toFixed(0))); + } + preparePoints(profileProgress){ + let segments = profileProgress.segments; if (segments.length === 0){ return false; } - let data = []; + let numPoints = profileProgress.size(); + + let position = new Float32Array(3 * numPoints); + let color = new Uint8Array(3 * numPoints); + let distance = 0; let totalDistance = 0; let min = new THREE.Vector3(Math.max()); let max = new THREE.Vector3(0); - // Get the same color map as Three - let hr = this.viewer.getHeightRange(); - - let heightRange = hr.max - hr.min; - let colorRange = []; - let colorDomain = []; - - // Read the altitude gradient used in 3D scene - let gradient = viewer.scene.pointclouds[0].material.gradient; - for (let c = 0; c < gradient.length; c++){ - colorDomain.push(hr.min + heightRange * gradient[c][0]); - colorRange.push('#' + gradient[c][1].getHexString()); - } - - // Altitude color map scale - let colorRamp = d3.scale.linear() - .domain(colorDomain) - .range(colorRange) - .clamp(true); - // Iterate the profile's segments + let i = 0; for(let segment of segments){ let sv = new THREE.Vector3().subVectors(segment.end, segment.start).setZ(0) - let segmentLength = sv.length(); + let segmentDir = sv.clone().normalize(); let points = segment.points; // Iterate the segments' points - for(let j = 0; j < points.numPoints; j++){ + for(let j = 0; j < segment.numPoints; j++){ let p = points.position[j]; - let pl = new THREE.Vector3().subVectors(p, segment.start).setZ(0); + + //let svp = new THREE.Vector3().subVectors(p, segment.start); + //let distance = segmentDir.dot(svp); min.min(p); max.max(p); - let distance = totalDistance + pl.length(); + //position[3*i + 0] = distance + totalDistance; + position[3*i + 0] = points.mileage[j]; + position[3*i + 1] = p.z; + position[3*i + 2] = 0; - let d = { - distance: distance, - x: p.x, - y: p.y, - z: p.z, - altitude: p.z, - heightColor: colorRamp(p.z), - color: points.color ? points.color[j] : [0, 0, 0], - intensity: points.intensity ? points.intensity[j] : 0, - classification: points.classification ? points.classification[j] : 0, - returnNumber: points.returnNumber ? points.returnNumber[j] : 0, - numberOfReturns: points.numberOfReturns ? points.numberOfReturns[j] : 0, - pointSourceID: points.pointSourceID ? points.pointSourceID[j] : 0, - }; + let c = points.color ? points.color[j] : [0, 0, 0]; + color[3*i + 0] = c[0]; + color[3*i + 1] = c[1]; + color[3*i + 2] = c[2]; - data.push(d); + + i++; } - // Increment distance from the profile start point - totalDistance += segmentLength; + totalDistance += segment.length; } - - let output = { - 'data': data, - 'minX': min.x, - 'minY': min.y, - 'minZ': min.z, - 'maxX': max.x, - 'maxY': max.y, - 'maxZ': max.z + + let result = { + numPoints: numPoints, + min: min, + max: max, + buffers: { + position: position, + color: color + } }; - return output; + return result; }; - pointHighlight(coordinates){ - - let pointSize = 6; - - let svg = d3.select("svg#profileSVG"); - - // Find the hovered point if applicable - let d = this.points; - let sx = this.scaleX; - let sy = this.scaleY; - let xs = coordinates[0]; - let ys = coordinates[1]; - - // Fix FF vs Chrome discrepancy - //if(navigator.userAgent.indexOf("Firefox") == -1 ) { - // xs = xs - this.margin.left; - // ys = ys - this.margin.top; - //} - let hP = []; - let tol = pointSize; - - for (let i=0; i < d.length; i++){ - if(sx(d[i].distance) < xs + tol && sx(d[i].distance) > xs - tol && sy(d[i].altitude) < ys + tol && sy(d[i].altitude) > ys -tol){ - hP.push(d[i]); - } - } - - if(hP.length > 0){ - let p = hP[0]; - this.hoveredPoint = hP[0]; - let cx, cy; - if(navigator.userAgent.indexOf("Firefox") == -1 ) { - cx = this.scaleX(p.distance) + this.margin.left; - cy = this.scaleY(p.altitude) + this.margin.top; - } else { - cx = this.scaleX(p.distance); - cy = this.scaleY(p.altitude); - } - - //cx -= pointSize / 2; - cy -= pointSize / 2; - - //let svg = d3.select("svg#profileSVG"); - d3.selectAll("rect").remove(); - let rectangle = svg.append("rect") - .attr("x", cx) - .attr("y", cy) - .attr("id", p.id) - .attr("width", pointSize) - .attr("height", pointSize) - .style("fill", 'yellow'); - - - let marker = $("#profile_selection_marker"); - marker.css("display", "initial"); - marker.css("left", cx + "px"); - marker.css("top", cy + "px"); - marker.css("width", pointSize + "px"); - marker.css("height", pointSize + "px"); - marker.css("background-color", "yellow"); - - //let html = 'x: ' + Math.round(10 * p.x) / 10 + ' y: ' + Math.round(10 * p.y) / 10 + ' z: ' + Math.round( 10 * p.altitude) / 10 + ' - '; - //html += i18n.t('tools.classification') + ': ' + p.classificationCode + ' - '; - //html += i18n.t('tools.intensity') + ': ' + p.intensityCode; - - let html = 'x: ' + Math.round(10 * p.x) / 10 + ' y: ' + Math.round(10 * p.y) / 10 + ' z: ' + Math.round( 10 * p.altitude) / 10 + ' - '; - html += "offset: " + p.distance.toFixed(3) + ' - '; - html += "Classification: " + p.classification + ' - '; - html += "Intensity: " + p.intensity; - - $('#profileInfo').css('color', 'yellow'); - $('#profileInfo').html(html); - - } else { - d3.selectAll("rect").remove(); - $('#profileInfo').html(""); - - let marker = $("#profile_selection_marker"); - marker.css("display", "none"); - } - }; - - strokeColor(d){ - let material = this.viewer.getMaterial(); - if (material === Potree.PointColorType.RGB) { - let [r, g, b] = d.color.map(e => parseInt(e)); - - return `rgb( ${r}, ${g}, ${b})`; - } else if (material === Potree.PointColorType.INTENSITY) { - let irange = viewer.getIntensityRange(); - let i = parseInt(255 * (d.intensity - irange[0]) / (irange[1] - irange[0])); - - return `rgb(${i}, ${i}, ${i})`; - } else if (material === Potree.PointColorType.CLASSIFICATION) { - let classif = this.viewer.scene.pointclouds[0].material.classification; - if (typeof classif[d.classification] != 'undefined'){ - let color = 'rgb(' + classif[d.classification].x * 100 + '%,'; - color += classif[d.classification].y * 100 + '%,'; - color += classif[d.classification].z * 100 + '%)'; - return color; - } else { - return 'rgb(255,255,255)'; - } - } else if (material === Potree.PointColorType.HEIGHT) { - return d.heightColor; - } else if (material === Potree.PointColorType.RETURN_NUMBER) { - - if(d.numberOfReturns === 1){ - return 'rgb(255, 255, 0)'; - }else{ - if(d.returnNumber === 1){ - return 'rgb(255, 0, 0)'; - }else if(d.returnNumber === d.numberOfReturns){ - return 'rgb(0, 0, 255)'; - }else{ - return 'rgb(0, 255, 0)'; - } - } - - return d.heightColor; - } else { - return d.color; - } - } - draw(profile){ if(!this.enabled){ return; @@ -15527,7 +15909,7 @@ Potree.Viewer.Profile = class ProfileWindow{ // 2d profile draws don't need frame-by-frame granularity, it's // fine to handle a redraw once every 100ms let executeScheduledDraw = () => { - this.actuallyDraw(this.scheduledDraw); + this.prepareDraw(this.scheduledDraw); this.scheduledDraw = null; }; @@ -15537,16 +15919,12 @@ Potree.Viewer.Profile = class ProfileWindow{ this.scheduledDraw = profile; } - actuallyDraw(profile){ - - if(this.context){ - let containerWidth = this.element.clientWidth; - let containerHeight = this.element.clientHeight; - - let width = containerWidth - (this.margin.left + this.margin.right); - let height = containerHeight - (this.margin.top + this.margin.bottom); - this.context.clearRect(0, 0, width, height); - } + prepareDraw(profile){ + this.autoFit = true; + this.numPoints = 0; + this.geometry.setDrawRange(0, this.numPoints); + this.pickSphere.visible = false; + this.elRoot.find("#profileSelectionProperties").hide(); if(this.currentProfile){ this.currentProfile.removeEventListener("marker_moved", this.redraw); @@ -15556,10 +15934,15 @@ Potree.Viewer.Profile = class ProfileWindow{ viewer.removeEventListener("material_changed", this.redraw); viewer.removeEventListener("height_range_changed", this.redraw); viewer.removeEventListener("intensity_range_changed", this.redraw); + + for(let request of this.requests){ + request.cancel(); + } } this.currentProfile = profile; + this.points = new Potree.ProfilePointStore(this.currentProfile); { this.currentProfile.addEventListener("marker_moved", this.redraw); @@ -15570,200 +15953,6 @@ Potree.Viewer.Profile = class ProfileWindow{ viewer.addEventListener("height_range_changed", this.redraw); viewer.addEventListener("intensity_range_changed", this.redraw); } - - if(!this.__drawData){ - this.__drawData = {}; - } - this.points = []; - this.rangeX = [Infinity, -Infinity]; - this.rangeY = [Infinity, -Infinity]; - - this.pointsProcessed = 0; - - for(let request of this.requests){ - request.cancel(); - } - this.requests = []; - - let drawPoints = (points, rangeX, rangeY) => { - - let mileage = 0; - for(let i = 0; i < profile.points.length; i++){ - let point = profile.points[i]; - - if(i > 0){ - let previous = profile.points[i-1]; - let dx = point.x - previous.x; - let dy = point.y - previous.y; - let distance = Math.sqrt(dx * dx + dy * dy); - mileage += distance; - } - - let radius = 4; - - let cx = this.scaleX(mileage); - let cy = this.context.canvas.clientHeight; - - this.context.beginPath(); - this.context.arc(cx, cy, radius, 0, 2 * Math.PI, false); - this.context.fillStyle = '#a22'; - this.context.fill(); - }; - - - let pointSize = 2; - let i = -1, n = points.length, d, cx, cy; - while (++i < n) { - d = points[i]; - cx = this.scaleX(d.distance); - cy = this.scaleY(d.altitude); - this.context.beginPath(); - this.context.moveTo(cx, cy); - this.context.fillStyle = this.strokeColor(d); - this.context.fillRect(cx, cy, pointSize, pointSize); - } - }; - - let projectedBoundingBox = null; - - let setupAndDraw = () => { - let containerWidth = this.element.clientWidth; - let containerHeight = this.element.clientHeight; - - let width = containerWidth - (this.margin.left + this.margin.right); - let height = containerHeight - (this.margin.top + this.margin.bottom); - - this.scaleX = d3.scale.linear(); - this.scaleY = d3.scale.linear(); - - let domainProfileWidth = this.rangeX[1] - this.rangeX[0]; - let domainProfileHeight = this.rangeY[1] - this.rangeY[0]; - let domainRatio = domainProfileWidth / domainProfileHeight; - let rangeProfileWidth = width; - let rangeProfileHeight = height; - let rangeRatio = rangeProfileWidth / rangeProfileHeight; - - if(domainRatio < rangeRatio){ - // canvas scale - let targetWidth = domainProfileWidth * (rangeProfileHeight / domainProfileHeight); - this.scaleY.range([height, 0]); - this.scaleX.range([width / 2 - targetWidth / 2, width / 2 + targetWidth / 2]); - - // axis scale - let domainScale = rangeRatio / domainRatio; - let domainScaledWidth = domainProfileWidth * domainScale; - this.axisScaleX = d3.scale.linear() - .domain([ - domainProfileWidth / 2 - domainScaledWidth / 2 , - domainProfileWidth / 2 + domainScaledWidth / 2 ]) - .range([0, width]); - this.axisScaleY = d3.scale.linear() - .domain(this.rangeY) - .range([height, 0]); - }else{ - // canvas scale - let targetHeight = domainProfileHeight* (rangeProfileWidth / domainProfileWidth); - this.scaleX.range([0, width]); - this.scaleY.range([height / 2 + targetHeight / 2, height / 2 - targetHeight / 2]); - - // axis scale - let domainScale = domainRatio / rangeRatio; - let domainScaledHeight = domainProfileHeight * domainScale; - let domainHeightCentroid = (this.rangeY[1] + this.rangeY[0]) / 2; - this.axisScaleX = d3.scale.linear() - .domain(this.rangeX) - .range([0, width]); - this.axisScaleY = d3.scale.linear() - .domain([ - domainHeightCentroid - domainScaledHeight / 2 , - domainHeightCentroid + domainScaledHeight / 2 ]) - .range([height, 0]); - } - this.scaleX.domain(this.rangeX); - this.scaleY.domain(this.rangeY); - - - - this.axisZoom = d3.behavior.zoom() - .x(this.axisScaleX) - .y(this.axisScaleY) - .scaleExtent([0,128]) - .size([width, height]); - - this.zoom = d3.behavior.zoom() - .x(this.scaleX) - .y(this.scaleY) - .scaleExtent([0,128]) - .size([width, height]) - .on("zoom", () => { - this.axisZoom.translate(this.zoom.translate()); - this.axisZoom.scale(this.zoom.scale()); - - let svg = d3.select("svg#profileSVG"); - svg.select(".x.axis").call(xAxis); - svg.select(".y.axis").call(yAxis); - - this.context.clearRect(0, 0, width, height); - drawPoints(this.points, this.rangeX, this.rangeY); - }); - - this.context = d3.select("#profileCanvas") - .attr("width", width) - .attr("height", height) - .call(this.zoom) - .node().getContext("2d"); - - d3.select("svg#profileSVG").selectAll("*").remove(); - - let svg = d3.select("svg#profileSVG") - .call(this.zoom) - .attr("width", (width + this.margin.left + this.margin.right).toString()) - .attr("height", (height + this.margin.top + this.margin.bottom).toString()) - .attr("transform", "translate(" + this.margin.left + "," + this.margin.top + ")"); - - let scope = this; - d3.select("#profileCanvas").on("mousemove", function(){ - let coord = d3.mouse(this); - scope.pointHighlight(coord); - }); - - // Create x axis - let xAxis = d3.svg.axis() - .scale(this.axisScaleX) - .innerTickSize(-height) - .outerTickSize(5) - .orient("bottom") - .ticks(10, "m"); - - // Create y axis - let yAxis = d3.svg.axis() - .scale(this.axisScaleY) - .innerTickSize(-width) - .outerTickSize(5) - .orient("left") - .ticks(10, "m"); - - // Append axis to the chart - let gx = svg.append("g") - .attr("class", "x axis") - .call(xAxis); - - svg.append("g") - .attr("class", "y axis") - .call(yAxis); - - if(navigator.userAgent.indexOf("Firefox") == -1 ) { - svg.select(".y.axis").attr("transform", "translate("+ (this.margin.left).toString() + "," + this.margin.top.toString() + ")"); - svg.select(".x.axis").attr("transform", "translate(" + this.margin.left.toString() + "," + (height + this.margin.top).toString() + ")"); - } else { - svg.select(".x.axis").attr("transform", "translate( 0 ," + height.toString() + ")"); - } - - drawPoints(this.points, this.rangeX, this.rangeY); - - document.getElementById("profile_num_points").innerHTML = Potree.utils.addCommas(this.pointsProcessed) + " "; - }; - for(let pointcloud of this.viewer.scene.pointclouds.filter(p => p.visible)){ @@ -15773,27 +15962,11 @@ Potree.Viewer.Profile = class ProfileWindow{ return; } - if(!projectedBoundingBox){ - projectedBoundingBox = event.points.projectedBoundingBox; - }else{ - projectedBoundingBox.union(event.points.projectedBoundingBox); - } + this.handleNewPoints(event.points); - let result = this.preparePoints(event.points); - let points = result.data; - this.points = this.points.concat(points); + this.render(); - let batchRangeX = [d3.min(points, function(d) { return d.distance; }), d3.max(points, function(d) { return d.distance; })]; - let batchRangeY = [d3.min(points, function(d) { return d.altitude; }), d3.max(points, function(d) { return d.altitude; })]; - - this.rangeX = [ Math.min(this.rangeX[0], batchRangeX[0]), Math.max(this.rangeX[1], batchRangeX[1]) ]; - this.rangeY = [ Math.min(this.rangeY[0], batchRangeY[0]), Math.max(this.rangeY[1], batchRangeY[1]) ]; - - this.pointsProcessed += result.data.length; - - setupAndDraw(); - - if(this.pointsProcessed > this.threshold){ + if(this.numPoints > this.threshold){ this.cancel(); } }, @@ -15813,6 +15986,7 @@ Potree.Viewer.Profile = class ProfileWindow{ } } + setThreshold(value){ this.threshold = value; @@ -15825,70 +15999,80 @@ Potree.Viewer.Profile = class ProfileWindow{ } let file = ""; - let points = this.points.slice(); - points.sort((a, b) => (a.distance - b.distance)); + let pointsPerSegment = this.points.segments.map(s => s.points); + let points = {}; + let attributes = Object.keys(pointsPerSegment[0]); + for(let attribute of attributes){ + let arrays = pointsPerSegment.map(p => p[attribute]); + points[attribute] = arrays.reduce( (a, v) => a.concat(v)); + } { // header-line let header = "x, y"; - if(points[0].hasOwnProperty("color")){ + if(points.hasOwnProperty("color")){ header += ", r, g, b"; } - if(points[0].hasOwnProperty("intensity")){ + if(points.hasOwnProperty("intensity")){ header += ", intensity"; } - if(points[0].hasOwnProperty("classification")){ + if(points.hasOwnProperty("classification")){ header += ", classification"; } - if(points[0].hasOwnProperty("numberOfReturns")){ + if(points.hasOwnProperty("numberOfReturns")){ header += ", numberOfReturns"; } - if(points[0].hasOwnProperty("pointSourceID")){ + if(points.hasOwnProperty("pointSourceID")){ header += ", pointSourceID"; } - if(points[0].hasOwnProperty("returnNumber")){ + if(points.hasOwnProperty("returnNumber")){ header += ", returnNumber"; } file += header + "\n"; } + + let numPoints = points.position.length; // actual data - for(let point of points){ - let line = point.distance.toFixed(4) + ", "; - line += point.altitude.toFixed(4) + ", "; + for(let i = 0; i < numPoints; i++){ - if(point.hasOwnProperty("color")){ - line += point.color.join(", "); + let values = []; + + values.push(points.mileage[i].toFixed(4)); + values.push(points.position[i].z.toFixed(4)); + + if(attributes.includes("color")){ + values.push(points.color[i].join(", ")); } - if(point.hasOwnProperty("intensity")){ - line += ", " + point.intensity; + if(attributes.includes("intensity")){ + values.push(points.intensity[i]); } - if(point.hasOwnProperty("classification")){ - line += ", " + point.classification; + if(attributes.includes("classification")){ + values.push(points.classification[i]); } - if(point.hasOwnProperty("numberOfReturns")){ - line += ", " + point.numberOfReturns; + if(attributes.includes("numberOfReturns")){ + values.push(points.numberOfReturns[i]); } - if(point.hasOwnProperty("pointSourceID")){ - line += ", " + point.pointSourceID; + if(attributes.includes("pointSourceID")){ + values.push(points.pointSourceID[i]); } - if(point.hasOwnProperty("returnNumber")){ - line += ", " + point.returnNumber; + if(attributes.includes("returnNumber")){ + values.push(points.returnNumber[i]); } - line += "\n"; + let line = values.join(", ") + "\n"; file = file + line; } @@ -15897,13 +16081,24 @@ Potree.Viewer.Profile = class ProfileWindow{ } getPointsInProfileAsLas(){ - let points = this.points; + + let pointsPerSegment = this.points.segments.map(s => s.points); + let points = {}; + let attributes = Object.keys(pointsPerSegment[0]); + for(let attribute of attributes){ + let arrays = pointsPerSegment.map(p => p[attribute]); + points[attribute] = arrays.reduce( (a, v) => a.concat(v)); + } + let numPoints = points.position.length; + let boundingBox = new THREE.Box3(); - for(let point of points){ - boundingBox.expandByPoint(point); + for(let i = 0; i < numPoints; i++){ + let position = points.position[i]; + boundingBox.expandByPoint(position); } + let offset = boundingBox.min.clone(); let diagonal = boundingBox.min.distanceTo(boundingBox.max); let scale = new THREE.Vector3(0.01, 0.01, 0.01); @@ -15922,10 +16117,9 @@ Potree.Viewer.Profile = class ProfileWindow{ } } - let buffer = new ArrayBuffer(227 + 28 * points.length); + let buffer = new ArrayBuffer(227 + 28 * numPoints); let view = new DataView(buffer); let u8View = new Uint8Array(buffer); - //let u16View = new Uint16Array(buffer); setString("LASF", 0, buffer); u8View[24] = 1; @@ -15954,7 +16148,7 @@ Potree.Viewer.Profile = class ProfileWindow{ view.setUint16(105, 28, true); // number of point records 107 4 - view.setUint32(107, points.length, true); + view.setUint32(107, numPoints, true); // number of points by return 111 20 @@ -15976,10 +16170,16 @@ Potree.Viewer.Profile = class ProfileWindow{ // z offset 171 8 view.setFloat64(171, offset.z, true); + let hasIntensity = attributes.includes("intensity"); + let hasReturnNumber = attributes.includes("returnNumber"); + let hasClassification = attributes.includes("classification"); + let hasPointSourceID = attributes.includes("pointSourceID"); + let boffset = 227; - for(let i = 0; i < points.length; i++){ - let point = points[i]; - let position = new THREE.Vector3(point.x, point.y, point.z); + for(let i = 0; i < numPoints; i++){ + //let point = points[i]; + //let position = new THREE.Vector3(point.x, point.y, point.z); + let position = points.position[i]; let ux = parseInt((position.x - offset.x) / scale.x); let uy = parseInt((position.y - offset.y) / scale.y); @@ -15988,21 +16188,30 @@ Potree.Viewer.Profile = class ProfileWindow{ view.setUint32(boffset + 0, ux, true); view.setUint32(boffset + 4, uy, true); view.setUint32(boffset + 8, uz, true); + + if(hasIntensity){ + view.setUint16(boffset + 12, (points.intensity[i]), true); + } - view.setUint16(boffset + 12, (point.intensity), true); - let rt = point.returnNumber; - rt += (point.numberOfReturns << 3); - view.setUint8(boffset + 14, rt); + if(hasReturnNumber){ + let rt = points.returnNumber[i]; + rt += (points.numberOfReturns[i] << 3); + view.setUint8(boffset + 14, rt); + } + + if(hasClassification){ + view.setUint8(boffset + 15, points.classification[i]); + } - // classification - view.setUint8(boffset + 15, point.classification); // scan angle rank // user data // point source id - view.setUint16(boffset + 18, point.pointSourceID); - view.setUint16(boffset + 20, (point.color[0] * 255), true); - view.setUint16(boffset + 22, (point.color[1] * 255), true); - view.setUint16(boffset + 24, (point.color[2] * 255), true); + if(hasPointSourceID){ + view.setUint16(boffset + 18, points.pointSourceID[i]); + } + view.setUint16(boffset + 20, (points.color[i][0] * 255), true); + view.setUint16(boffset + 22, (points.color[i][1] * 255), true); + view.setUint16(boffset + 24, (points.color[i][2] * 255), true); boffset += 28; } diff --git a/build/potree/profile.html b/build/potree/profile.html index 6737fd29..50e5bf35 100644 --- a/build/potree/profile.html +++ b/build/potree/profile.html @@ -1,9 +1,15 @@ -