From e23be2fc6ddef055e33275c170febed82962065f Mon Sep 17 00:00:00 2001 From: Lukas Gersthofer Date: Tue, 11 Jul 2017 12:49:34 +0200 Subject: [PATCH] clip volumes: local rotation, local translation, display of local coordinate system --- src/utils/ClipVolume.js | 220 +++++++++++++++++++++++++++++++++------- src/viewer/sidebar.js | 122 ++++++++++++++++++++-- src/viewer/viewer.js | 4 +- 3 files changed, 302 insertions(+), 44 deletions(-) diff --git a/src/utils/ClipVolume.js b/src/utils/ClipVolume.js index 229cbb8f..6a40b1e8 100644 --- a/src/utils/ClipVolume.js +++ b/src/utils/ClipVolume.js @@ -11,8 +11,6 @@ Potree.ClipVolume = class extends THREE.Object3D{ this.clipOffset = 0.001; this.clipRotOffset = 1; - this.scale.z = 0.1; - let alpha = args.alpha || 0; let beta = args.beta || 0; let gamma = args.gamma || 0; @@ -53,6 +51,30 @@ Potree.ClipVolume = class extends THREE.Object3D{ boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, 0.5, -0.5)); boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, -0.5, -0.5)); boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, 0.5, -0.5)); + // middle line + /*boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, -0.5, 0.0)); + boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, 0.5, 0.0)); + boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, 0.5, 0.0)); + boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, -0.5, 0.0)); + boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, 0.5, 0.0)); + boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, 0.5, 0.0)); + boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, -0.5, 0.0)); + boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, -0.5, 0.0));*/ + + } + + let planeFrameGeometry = new THREE.Geometry(); + { + // middle line + boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, -0.5, 0.0)); + boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, 0.5, 0.0)); + boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, 0.5, 0.0)); + boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, -0.5, 0.0)); + boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, 0.5, 0.0)); + boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, 0.5, 0.0)); + boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, -0.5, 0.0)); + boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, -0.5, 0.0)); + } this.dimension = new THREE.Vector3(1,1,1); @@ -68,34 +90,129 @@ Potree.ClipVolume = class extends THREE.Object3D{ this.add(this.box); this.frame = new THREE.LineSegments( boxFrameGeometry, new THREE.LineBasicMaterial({color: 0x000000})); - //this.frame.mode = THREE.Lines; this.add(this.frame); - - /*this.label = new Potree.TextSprite("0"); - this.label.setBorderColor({r:0, g:255, b:0, a:0.0}); - this.label.setBackgroundColor({r:0, g:255, b:0, a:0.0}); - this.label.material.depthTest = false; - this.label.material.depthWrite = false; - this.label.material.transparent = true; - this.label.position.y -= 0.5; - this.add(this.label); - - this.label.updateMatrixWorld = () => { - var volumeWorldPos = new THREE.Vector3(); - volumeWorldPos.setFromMatrixPosition( this.matrixWorld ); - this.label.position.copy(volumeWorldPos); - this.label.updateMatrix(); - this.label.matrixWorld.copy(this.label.matrix); - this.label.matrixWorldNeedsUpdate = false; + + // set default thickness + this.setScaleZ(0.1); + + // create local coordinate system + let createArrow = (name, direction, color) => { + let material = new THREE.MeshBasicMaterial({ + color: color, + depthTest: false, + depthWrite: false}); + + let shaftGeometry = new THREE.Geometry(); + shaftGeometry.vertices.push(new THREE.Vector3(0, 0, 0)); + shaftGeometry.vertices.push(new THREE.Vector3(0, 1, 0)); - for ( var i = 0, l = this.label.children.length; i < l; i ++ ) { - this.label.children[ i ].updateMatrixWorld( true ); - } + let shaftMaterial = new THREE.LineBasicMaterial({ + color: color, + depthTest: true, + depthWrite: true, + transparent: true + }); + let shaft = new THREE.Line(shaftGeometry, shaftMaterial); + shaft.name = name + "_shaft"; + + let headGeometry = new THREE.CylinderGeometry(0, 0.04, 0.1, 10, 1, false); + let headMaterial = material; + let head = new THREE.Mesh(headGeometry, headMaterial); + head.name = name + "_head"; + head.position.y = 1; + + let arrow = new THREE.Object3D(); + arrow.name = name; + arrow.add(shaft); + arrow.add(head); + + /*let mouseover = e => { + let c = new THREE.Color(0xFFFF00); + shaftMaterial.color = c; + headMaterial.color = c; + }; + + let mouseleave = e => { + let c = new THREE.Color(color); + shaftMaterial.color = c; + headMaterial.color = c; + }; + + let drag = e => { + + let camera = this.viewer.scene.getActiveCamera(); + + if(!e.drag.intersectionStart){ + e.drag.intersectionStart = e.drag.location; + e.drag.objectStart = e.drag.object.getWorldPosition(); + + let start = this.sceneTransform.position.clone(); + let end = direction.clone().applyMatrix4(this.sceneTransform.matrixWorld); + //let end = start.clone().add(direction); + let line = new THREE.Line3(start, end); + e.drag.line = line; + + let camOnLine = line.closestPointToPoint(camera.position, false); + let normal = new THREE.Vector3().subVectors( + camera.position, camOnLine); + let plane = new THREE.Plane() + .setFromNormalAndCoplanarPoint(normal, e.drag.intersectionStart); + + e.drag.dragPlane = plane; + e.drag.pivot = e.drag.intersectionStart; + } + + { + let mouse = e.drag.end; + let domElement = viewer.renderer.domElement; + let nmouse = { + x: (mouse.x / domElement.clientWidth ) * 2 - 1, + y: - (mouse.y / domElement.clientHeight ) * 2 + 1 + }; + + let vector = new THREE.Vector3( nmouse.x, nmouse.y, 0.5 ); + vector.unproject(camera); + + let ray = new THREE.Ray(camera.position, vector.sub( camera.position)); + let I = ray.intersectPlane(e.drag.dragPlane); + + if(I){ + + let iOnLine = e.drag.line.closestPointToPoint(I, false); + + let diff = new THREE.Vector3().subVectors( + iOnLine, e.drag.pivot); + + for(let selection of this.selection){ + selection.position.add(diff); + } + + e.drag.pivot = e.drag.pivot.add(diff); + } + } + }; + + shaft.addEventListener("mouseover", mouseover); + shaft.addEventListener("mouseleave", mouseleave); + shaft.addEventListener("drag", drag);*/ + + return arrow; }; - */ + + let arrowX = createArrow("arrow_x", new THREE.Vector3(1, 0, 0), 0xFF0000); + let arrowY = createArrow("arrow_y", new THREE.Vector3(0, 1, 0), 0x00FF00); + let arrowZ = createArrow("arrow_z", new THREE.Vector3(0, 0, 1), 0x0000FF); + + arrowX.rotation.z = -Math.PI/2; + arrowZ.rotation.x = Math.PI/2; + + this.add(arrowX); + this.add(arrowY); + this.add(arrowZ); + { // event listeners - this.addEventListener("select", e => {}); - this.addEventListener("deselect", e => {}); + //this.addEventListener("select", e => {}); + //this.addEventListener("deselect", e => {}); } this.update(); @@ -105,14 +222,29 @@ Potree.ClipVolume = class extends THREE.Object3D{ if(this.clipOffset == offset) return; this.clipOffset = offset; - this.dispatchEvent({"type": "clip_volume_changed", "viewer": viewer, "volume": this}); + //this.dispatchEvent({"type": "clip_volume_changed", "viewer": viewer, "volume": this}); } setClipRotOffset(offset) { if(this.clipRotOffset == offset) return; this.clipRotOffset = offset; - this.dispatchEvent({"type": "clip_volume_changed", "viewer": viewer, "volume": this}); + //this.dispatchEvent({"type": "clip_volume_changed", "viewer": viewer, "volume": this}); + } + + setScaleX(x) { + this.children[0].scale.x = x; + this.children[1].scale.x = x; + } + + setScaleY(y) { + this.children[0].scale.y = y; + this.children[1].scale.y = y; + } + + setScaleZ(z) { + this.children[0].scale.z = z; + this.children[1].scale.z = z; } offset(args) { @@ -124,23 +256,24 @@ Potree.ClipVolume = class extends THREE.Object3D{ if(axis == "x") { if(cs == "local") { - + this.position.add(this.localX.clone().multiplyScalar(dir * this.clipOffset)); } else if(cs == "global") { this.position.x = this.position.x + dir * this.clipOffset; } }else if(axis == "y") { if(cs == "local") { - + this.position.add(this.localY.clone().multiplyScalar(dir * this.clipOffset)); } else if(cs == "global") { this.position.y = this.position.y + dir * this.clipOffset; } }else if(axis == "z") { if(cs == "local") { - + this.position.add(this.localZ.clone().multiplyScalar(dir * this.clipOffset)); } else if(cs == "global") { this.position.z = this.position.z + dir * this.clipOffset; } } + this.dispatchEvent({"type": "clip_volume_changed", "viewer": viewer, "volume": this}); } @@ -153,23 +286,26 @@ Potree.ClipVolume = class extends THREE.Object3D{ if(axis == "x") { if(cs == "local") { - - } else if(cs == "global") { this.rotation.x = this.rotation.x + dir * this.clipRotOffset * Math.PI/180; + } else if(cs == "global") { + } }else if(axis == "y") { if(cs == "local") { - - } else if(cs == "global") { this.rotation.y = this.rotation.y + dir * this.clipRotOffset * Math.PI/180; + } else if(cs == "global") { + } }else if(axis == "z") { if(cs == "local") { - - } else if(cs == "global") { this.rotation.z = this.rotation.z + dir * this.clipRotOffset * Math.PI/180; + } else if(cs == "global") { + } } + + this.updateLocalSystem(); + this.dispatchEvent({"type": "clip_volume_changed", "viewer": viewer, "volume": this}); } @@ -179,7 +315,17 @@ Potree.ClipVolume = class extends THREE.Object3D{ this.box.visible = false; //this.label.visible = false; + + this.updateLocalSystem(); }; + + updateLocalSystem() { + // extract local coordinate axes + let rotQuat = this.getWorldQuaternion(); + this.localX = new THREE.Vector3(1, 0, 0).applyQuaternion(rotQuat).normalize(); + this.localY = new THREE.Vector3(0, 1, 0).applyQuaternion(rotQuat).normalize(); + this.localZ = new THREE.Vector3(0, 0, 1).applyQuaternion(rotQuat).normalize(); + } raycast(raycaster, intersects){ diff --git a/src/viewer/sidebar.js b/src/viewer/sidebar.js index b721966e..fb74fa92 100644 --- a/src/viewer/sidebar.js +++ b/src/viewer/sidebar.js @@ -1493,7 +1493,7 @@ function initClippingTool() { Potree.resourcePath + "/icons/profile.svg", "[title]tt.height_profile", function(){ - viewer.clippingTool.startInsertion({alpha: 0, beta: 0, gamma: Math.PI/2}); + viewer.clippingTool.startInsertion({alpha: 0, beta: 0, gamma: 0}); } )); @@ -1518,9 +1518,9 @@ function initClippingTool() { let rot_alpha = (volume.rotation.x * 180/Math.PI).toFixed(0); let rot_beta = (volume.rotation.y * 180/Math.PI).toFixed(0); let rot_gamma = (volume.rotation.z * 180/Math.PI).toFixed(0); - let sc_width = Potree.utils.addCommas(volume.scale.x.toFixed(3)); - let sc_length = Potree.utils.addCommas(volume.scale.y.toFixed(3)); - let sc_thickness = Potree.utils.addCommas(volume.scale.z.toFixed(3)); + let sc_width = Potree.utils.addCommas(volume.children[0].scale.x.toFixed(3)); + let sc_length = Potree.utils.addCommas(volume.children[0].scale.y.toFixed(3)); + let sc_thickness = Potree.utils.addCommas(volume.children[0].scale.z.toFixed(3)); let removeIconPath = Potree.resourcePath + "/icons/remove.svg"; let cvItem = $(` @@ -1660,14 +1660,67 @@ function initClippingTool() { `); - + + // spinner style cvItem.find(".clip_volume_spinner").spinner(); + // button style let cv_btn_offset = cvItem.find(".cv_btn_offset"); cv_btn_offset.button(); cv_btn_offset.css("padding", "2px"); cv_btn_offset.css("margin", "0px 3px"); + // scale spinner + let cv_sp_width = cvItem.find("#cv_sp_width_" + volume.id); + let cv_sp_length = cvItem.find("#cv_sp_length_" + volume.id); + let cv_sp_thickness = cvItem.find("#cv_sp_thickness_" + volume.id); + + cv_sp_width.spinner({ + min: 0.001, + max: 100, + step: 0.1, + numberFormat: "n", + spin: (event, ui) => { + let value = cv_sp_width.spinner("value"); + volume.setScaleX(value); + }, + stop: (event, ui) => { + let value = cv_sp_width.spinner("value"); + volume.setScaleX(value); + } + }); + + cv_sp_length.spinner({ + min: 0.001, + max: 100, + step: 0.1, + numberFormat: "n", + spin: (event, ui) => { + let value = cv_sp_length.spinner("value"); + volume.setScaleY(value); + }, + stop: (event, ui) => { + let value = cv_sp_length.spinner("value"); + volume.setScaleY(value); + } + }); + + cv_sp_thickness.spinner({ + min: 0.001, + max: 100, + step: 0.1, + numberFormat: "n", + spin: (event, ui) => { + let value = cv_sp_thickness.spinner("value"); + volume.setScaleZ(value); + }, + stop: (event, ui) => { + let value = cv_sp_thickness.spinner("value"); + volume.setScaleZ(value); + } + }); + + // offset spinner let cv_sp_offset = cvItem.find("#cv_sp_offset_" + volume.id); cv_sp_offset.spinner({ min: 0.001, @@ -1684,6 +1737,7 @@ function initClippingTool() { } }); + // angle offset spinner let cv_sp_rot_offset = cvItem.find("#cv_sp_rot_offset_" + volume.id); cv_sp_rot_offset.spinner({ min: 1, @@ -1700,6 +1754,7 @@ function initClippingTool() { } }); + // global offset buttons let cv_btn_global_x_decr = cvItem.find("#cv_btn_global_x_decr_" + volume.id); let cv_btn_global_x_incr = cvItem.find("#cv_btn_global_x_incr_" + volume.id); let cv_btn_global_y_decr = cvItem.find("#cv_btn_global_y_decr_" + volume.id); @@ -1726,7 +1781,34 @@ function initClippingTool() { volume.offset({cs: "global", axis: "z", dir: 1}); }); + // local offset buttons + let cv_btn_local_x_decr = cvItem.find("#cv_btn_local_x_decr_" + volume.id); + let cv_btn_local_x_incr = cvItem.find("#cv_btn_local_x_incr_" + volume.id); + let cv_btn_local_y_decr = cvItem.find("#cv_btn_local_y_decr_" + volume.id); + let cv_btn_local_y_incr = cvItem.find("#cv_btn_local_y_incr_" + volume.id); + let cv_btn_local_z_decr = cvItem.find("#cv_btn_local_z_decr_" + volume.id); + let cv_btn_local_z_incr = cvItem.find("#cv_btn_local_z_incr_" + volume.id); + cv_btn_local_x_decr.click(function(event) { + volume.offset({cs: "local", axis: "x", dir: -1}); + }); + cv_btn_local_x_incr.click(function(event) { + volume.offset({cs: "local", axis: "x", dir: 1}); + }); + cv_btn_local_y_decr.click(function(event) { + volume.offset({cs: "local", axis: "y", dir: -1}); + }); + cv_btn_local_y_incr.click(function(event) { + volume.offset({cs: "local", axis: "y", dir: 1}); + }); + cv_btn_local_z_decr.click(function(event) { + volume.offset({cs: "local", axis: "z", dir: -1}); + }); + cv_btn_local_z_incr.click(function(event) { + volume.offset({cs: "local", axis: "z", dir: 1}); + }); + + // global angle offset let cv_btn_global_alpha_decr = cvItem.find("#cv_btn_global_alpha_decr_" + volume.id); let cv_btn_global_alpha_incr = cvItem.find("#cv_btn_global_alpha_incr_" + volume.id); let cv_btn_global_beta_decr = cvItem.find("#cv_btn_global_beta_decr_" + volume.id); @@ -1753,14 +1835,41 @@ function initClippingTool() { volume.rotate({cs: "global", axis: "z", dir: 1}); }); + // local angle offset + let cv_btn_local_alpha_decr = cvItem.find("#cv_btn_local_alpha_decr_" + volume.id); + let cv_btn_local_alpha_incr = cvItem.find("#cv_btn_local_alpha_incr_" + volume.id); + let cv_btn_local_beta_decr = cvItem.find("#cv_btn_local_beta_decr_" + volume.id); + let cv_btn_local_beta_incr = cvItem.find("#cv_btn_local_beta_incr_" + volume.id); + let cv_btn_local_gamma_decr = cvItem.find("#cv_btn_local_gamma_decr_" + volume.id); + let cv_btn_local_gamma_incr = cvItem.find("#cv_btn_local_gamma_incr_" + volume.id); + cv_btn_local_alpha_decr.click(function(event) { + volume.rotate({cs: "local", axis: "x", dir: -1}); + }); + cv_btn_local_alpha_incr.click(function(event) { + volume.rotate({cs: "local", axis: "x", dir: 1}); + }); + cv_btn_local_beta_decr.click(function(event) { + volume.rotate({cs: "local", axis: "y", dir: -1}); + }); + cv_btn_local_beta_incr.click(function(event) { + volume.rotate({cs: "local", axis: "y", dir: 1}); + }); + cv_btn_local_gamma_decr.click(function(event) { + volume.rotate({cs: "local", axis: "z", dir: -1}); + }); + cv_btn_local_gamma_incr.click(function(event) { + volume.rotate({cs: "local", axis: "z", dir: 1}); + }); + + // remove button let cvRemove = cvItem.find(".clipping_volume_action_remove"); cvRemove.click(() => {viewer.scene.removeClipVolume(cv)}); return cvItem; }; - + // update body when clip volume changes cv.addEventListener("clip_volume_changed", function(event) { let cv = event.volume; let prevCVItem = $("span#" + cv.name + " .clip_volume_body"); @@ -1769,6 +1878,7 @@ function initClippingTool() { prevCVItem.remove(); }); + // wrappe which doesnt change let icon = "/icons/clip_volume.svg"; let cvWrapper = $(` diff --git a/src/viewer/viewer.js b/src/viewer/viewer.js index 0160bd90..947805b5 100644 --- a/src/viewer/viewer.js +++ b/src/viewer/viewer.js @@ -1517,8 +1517,10 @@ Potree.Viewer = class PotreeViewer extends THREE.EventDispatcher{ let clipBoxes = boxes.map( box => { box.updateMatrixWorld(); - let boxInverse = new THREE.Matrix4().getInverse(box.matrixWorld); + //let boxInverse = new THREE.Matrix4().getInverse(box.matrixWorld); let boxPosition = box.getWorldPosition(); + let boxMatrixWorld = new THREE.Matrix4().compose(boxPosition, box.getWorldQuaternion(), box.children[0].scale); + let boxInverse = new THREE.Matrix4().getInverse(boxMatrixWorld); return {inverse: boxInverse, position: boxPosition}; });