diff --git a/examples/ca13.html b/examples/ca13.html index a5f29625..7ca6e277 100644 --- a/examples/ca13.html +++ b/examples/ca13.html @@ -87,6 +87,34 @@ viewer.fitToScreen(); }); + + viewer.onGUILoaded(() => { + let boxSelectTool = new Potree.ScreenBoxSelectTool(viewer); + window.boxSelectTool = boxSelectTool; + + let createToolIcon = function (icon, title, callback) { + let element = $(` + + `); + + element.click(callback); + + return element; + }; + + let clippingToolBar = $("#clipping_tools"); + clippingToolBar.append(createToolIcon( + Potree.resourcePath + "/icons/clip-polygon.svg", + "[title]tt.clip_polygon", + function(){ + let item = boxSelectTool.startInsertion(); + } + )); + + }); diff --git a/examples/clipping_volume.html b/examples/clipping_volume.html index 58f885e5..c46f05b2 100644 --- a/examples/clipping_volume.html +++ b/examples/clipping_volume.html @@ -99,11 +99,14 @@ }); function test(){ - let volume = viewer.scene.volumes[0]; + let volume = viewer.scene.volumes[1]; let pointcloud = viewer.scene.pointclouds[0]; - let points = pointcloud.root.getPointsInBox(volume); - pointcloud.root.getPointsInBox(viewer.scene.volumes[1]); + let fitted = pointcloud.root.getFittedBox(volume); + console.log(fitted); + volume.scale.copy(fitted.scale); + volume.position.copy(fitted.position); + } diff --git a/examples/heidentor.html b/examples/heidentor.html index e4d982d2..992c8bfe 100644 --- a/examples/heidentor.html +++ b/examples/heidentor.html @@ -95,12 +95,6 @@ viewer.onGUILoaded(() => { let boxSelectTool = new Potree.ScreenBoxSelectTool(viewer); - let sg = new THREE.SphereGeometry(1, 32, 32); - let sm = new THREE.MeshNormalMaterial(); - let s = new THREE.Mesh(sg, sm); - s.position.set(-0.068, -2.112, 8.060); - boxSelectTool.scene.add(s); - window.boxSelectTool = boxSelectTool; let createToolIcon = function (icon, title, callback) { diff --git a/examples/sorvilier_feature_test.html b/examples/sorvilier_feature_test.html index 5a1b8a44..19cb142c 100644 --- a/examples/sorvilier_feature_test.html +++ b/examples/sorvilier_feature_test.html @@ -33,6 +33,7 @@ + @@ -252,6 +253,52 @@ viewer.scene.addVolume(volume); } + { + let material = new MeshLineMaterial({sizeAttenuation: 1, color: new THREE.Color(0xff0000)}); + let line = new MeshLine(); + let line2 = new MeshLine(); + + let mesh = new THREE.Mesh( line.geometry, material ); + let mesh2 = new THREE.Mesh( line2.geometry, material ); + + viewer.measuringTool.sceneMeasurement.add(mesh); + viewer.measuringTool.sceneMeasurement.add(mesh2); + + let setCoordinates = (mesh, line, p1, p2) => { + let us = [0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.75, 0.1, 0.5, 0.925, 0.95, 0.96, 0.97, 0.98, 0.99, 1]; + let geometry = new THREE.Geometry(); + + let pl1 = new THREE.Vector3(0, 0, 0); + let pl2 = p2.clone().sub(p1); + for(let u of us){ + let p = pl2.clone().multiplyScalar(u); + + geometry.vertices.push(p); + } + + line.setGeometry( geometry, w => { + return 4 + 10 * (0.1 + 200 * Math.pow(Math.abs(w - 0.5), 8)); + }); + mesh.position.copy(p1); + } + + viewer.addEventListener("update", () => { + material.uniforms.near.value = viewer.scene.getActiveCamera().near; + material.uniforms.far.value = viewer.scene.getActiveCamera().far; + material.uniforms.sizeAttenuation.value = 0; + + let width = viewer.renderer.getSize().width; + let height = viewer.renderer.getSize().height; + material.uniforms.resolution.value.set(width, height); + + let points = viewer.scene.measurements[0].points.map(p => p.position); + + setCoordinates(mesh, line, points[0], points[1]); + setCoordinates(mesh2, line2, points[1], points[2]); + }); + + } + { let manager = new THREE.LoadingManager(); manager.onProgress = function ( item, loaded, total ) { diff --git a/libs/MeshLine/LICENSE b/libs/MeshLine/LICENSE new file mode 100644 index 00000000..2913a08c --- /dev/null +++ b/libs/MeshLine/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016 Jaume Sanchez + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/libs/MeshLine/README.md b/libs/MeshLine/README.md new file mode 100644 index 00000000..f8cfeafb --- /dev/null +++ b/libs/MeshLine/README.md @@ -0,0 +1,137 @@ +# MeshLine +Mesh replacement for ```THREE.Line``` + +Instead of using GL_LINE, it uses a strip of triangles billboarded. Some examples: + +[![Demo](screenshots/demo.jpg)](https://www.clicktorelease.com/code/THREE.MeshLine/demo/index.html) +[![Graph](screenshots/graph.jpg)](https://www.clicktorelease.com/code/THREE.MeshLine/demo/graph.html) +[![Spinner](screenshots/spinner.jpg)](https://www.clicktorelease.com/code/THREE.MeshLine/demo/spinner.html) +[![SVG](screenshots/svg.jpg)](https://www.clicktorelease.com/code/THREE.MeshLine/demo/svg.html) +[![Shape](screenshots/shape.jpg)](https://www.clicktorelease.com/code/THREE.MeshLine/demo/shape.html) +[![Shape](screenshots/birds.jpg)](https://www.clicktorelease.com/code/THREE.MeshLine/demo/birds.html) + +* [Demo](https://www.clicktorelease.com/code/THREE.MeshLine/demo/index.html): play with the different settings of materials +* [Graph](https://www.clicktorelease.com/code/THREE.MeshLine/demo/graph.html): example of using ```MeshLine``` to plot graphs +* [Spinner](https://www.clicktorelease.com/code/THREE.MeshLine/demo/spinner.html): example of dynamic ```MeshLine``` with texture +* [SVG](https://www.clicktorelease.com/code/THREE.MeshLine/demo/svg.html): example of ```MeshLine``` rendering SVG Paths +* [Shape](https://www.clicktorelease.com/code/THREE.MeshLine/demo/shape.html): example of ```MeshLine``` created from a mesh +* [Birds](https://www.clicktorelease.com/code/THREE.MeshLine/demo/birds.html): example of ```MeshLine.advance()``` by @caramelcode (Jared Sprague) and @mwcz (Michael Clayton) + +### How to use #### + +* Include script +* Create and populate a geometry +* Create a MeshLine and assign the geometry +* Create a MeshLineMaterial +* Use MeshLine and MeshLineMaterial to create a THREE.Mesh + +#### Include the script + +Include script after THREE is included +```js + +``` +or use npm to install it +``` +npm i three.meshline +``` +and include it in your code (don't forget to require three.js) +```js +var THREE = require( 'three' ); +var MeshLine = require( 'three.meshline' ); +``` + +##### Create and populate a geometry ##### + +First, create the list of vertices that will define the line. ```MeshLine``` accepts ```THREE.Geometry``` (looking up the ```.vertices``` in it) and ```Array```/```Float32Array```. ```THREE.BufferGeometry``` coming soon, and may be others like ```Array``` of ```THREE.Vector3```. + +```js +var geometry = new THREE.Geometry(); +for( var j = 0; j < Math.PI; j += 2 * Math.PI / 100 ) { + var v = new THREE.Vector3( Math.cos( j ), Math.sin( j ), 0 ); + geometry.vertices.push( v ); +} +``` + +##### Create a MeshLine and assign the geometry ##### + +Once you have that, you can create a new ```MeshLine```, and call ```.setGeometry()``` passing the vertices. + +```js +var line = new MeshLine(); +line.setGeometry( geometry ); +``` + +Note: ```.setGeometry``` accepts a second parameter, which is a function to define the width in each point along the line. By default that value is 1, making the line width 1 * lineWidth. + +```js +line.setGeometry( geometry, function( p ) { return 2; } ); // makes width 2 * lineWidth +line.setGeometry( geometry, function( p ) { return 1 - p; } ); // makes width taper +line.setGeometry( geometry, function( p ) { return 2 + Math.sin( 50 * p ); } ); // makes width sinusoidal +``` + +##### Create a MeshLineMaterial ##### + +A ```MeshLine``` needs a ```MeshLineMaterial```: + +```js +var material = new MeshLineMaterial(); +``` + +By default it's a white material of width 1 unit. + +```MeshLineMaterial``` has several attributes to control the appereance of the ```MeshLine```: + +* ```map``` - a ```THREE.Texture``` to paint along the line (requires ```useMap``` set to true) +* ```useMap``` - tells the material to use ```map``` (0 - solid color, 1 use texture) +* ```alphaMap``` - a ```THREE.Texture``` to use as alpha along the line (requires ```useAlphaMap``` set to true) +* ```useAlphaMap``` - tells the material to use ```alphaMap``` (0 - no alpha, 1 modulate alpha) +* ```repeat``` - THREE.Vector2 to define the texture tiling (applies to map and alphaMap - MIGHT CHANGE IN THE FUTURE) +* ```color``` - ```THREE.Color``` to paint the line width, or tint the texture with +* ```opacity``` - alpha value from 0 to 1 (requires ```transparent``` set to ```true```) +* ```alphaTest``` - cutoff value from 0 to 1 +* ```dashArray``` - THREE.Vector2 to define the dashing (NOT IMPLEMENTED YET) +* ```resolution``` - ```THREE.Vector2``` specifying the canvas size (REQUIRED) +* ```sizeAttenuation``` - makes the line width constant regardless distance (1 unit is 1px on screen) (0 - attenuate, 1 - don't attenuate) +* ```lineWidth``` - float defining width (if ```sizeAttenuation``` is true, it's world units; else is screen pixels) +* ```near``` - camera near clip plane distance (REQUIRED if ```sizeAttenuation``` set to false) +* ```far``` - camera far clip plane distance (REQUIRED if ```sizeAttenuation``` set to false) + +If you're rendering transparent lines or using a texture with alpha map, you should set ```depthTest``` to ```false```, ```transparent``` to ```true``` and ```blending``` to an appropriate blending mode, or use ```alphaTest```. + +##### Use MeshLine and MeshLineMaterial to create a THREE.Mesh ##### + +Finally, we create a mesh and add it to the scene: + +```js +var mesh = new THREE.Mesh( line.geometry, material ); // this syntax could definitely be improved! +scene.add( mesh ); +``` + +### TODO ### + +* Better miters +* Proper sizes +* Support for dashArray + +### Support ### + +Tested successfully on + +* Chrome OSX, Windows, Android +* Firefox OSX, Windows, Anroid +* Safari OSX, iOS +* Internet Explorer 11 (SVG and Shape demo won't work because they use Promises) +* Opera OSX, Windows + +### References ### + +* [Drawing lines is hard](http://mattdesl.svbtle.com/drawing-lines-is-hard) +* [WebGL rendering of solid trails](http://codeflow.org/entries/2012/aug/05/webgl-rendering-of-solid-trails/) +* [Drawing Antialiased Lines with OpenGL](https://www.mapbox.com/blog/drawing-antialiased-lines/) + +#### License #### + +MIT licensed + +Copyright (C) 2015-2016 Jaume Sanchez Elias, http://www.clicktorelease.com diff --git a/libs/MeshLine/src/THREE.MeshLine.js b/libs/MeshLine/src/THREE.MeshLine.js new file mode 100644 index 00000000..30cd068a --- /dev/null +++ b/libs/MeshLine/src/THREE.MeshLine.js @@ -0,0 +1,477 @@ +;(function() { + +"use strict"; + +var root = this + +var has_require = typeof require !== 'undefined' + +var THREE = root.THREE || has_require && require('three') +if( !THREE ) + throw new Error( 'MeshLine requires three.js' ) + +function MeshLine() { + + this.positions = []; + + this.previous = []; + this.next = []; + this.side = []; + this.width = []; + this.indices_array = []; + this.uvs = []; + this.counters = []; + this.geometry = new THREE.BufferGeometry(); + + this.widthCallback = null; + +} + +MeshLine.prototype.setGeometry = function( g, c ) { + + this.widthCallback = c; + + this.positions = []; + this.counters = []; + + if( g instanceof THREE.Geometry ) { + for( var j = 0; j < g.vertices.length; j++ ) { + var v = g.vertices[ j ]; + var c = j/g.vertices.length; + this.positions.push( v.x, v.y, v.z ); + this.positions.push( v.x, v.y, v.z ); + this.counters.push(c); + this.counters.push(c); + } + } + + if( g instanceof THREE.BufferGeometry ) { + // read attribute positions ? + } + + if( g instanceof Float32Array || g instanceof Array ) { + for( var j = 0; j < g.length; j += 3 ) { + var c = j/g.length; + this.positions.push( g[ j ], g[ j + 1 ], g[ j + 2 ] ); + this.positions.push( g[ j ], g[ j + 1 ], g[ j + 2 ] ); + this.counters.push(c); + this.counters.push(c); + } + } + + this.process(); + +} + +MeshLine.prototype.compareV3 = function( a, b ) { + + var aa = a * 6; + var ab = b * 6; + return ( this.positions[ aa ] === this.positions[ ab ] ) && ( this.positions[ aa + 1 ] === this.positions[ ab + 1 ] ) && ( this.positions[ aa + 2 ] === this.positions[ ab + 2 ] ); + +} + +MeshLine.prototype.copyV3 = function( a ) { + + var aa = a * 6; + return [ this.positions[ aa ], this.positions[ aa + 1 ], this.positions[ aa + 2 ] ]; + +} + +MeshLine.prototype.process = function() { + + var l = this.positions.length / 6; + + this.previous = []; + this.next = []; + this.side = []; + this.width = []; + this.indices_array = []; + this.uvs = []; + + for( var j = 0; j < l; j++ ) { + this.side.push( 1 ); + this.side.push( -1 ); + } + + var w; + for( var j = 0; j < l; j++ ) { + if( this.widthCallback ) w = this.widthCallback( j / ( l -1 ) ); + else w = 1; + this.width.push( w ); + this.width.push( w ); + } + + for( var j = 0; j < l; j++ ) { + this.uvs.push( j / ( l - 1 ), 0 ); + this.uvs.push( j / ( l - 1 ), 1 ); + } + + var v; + + if( this.compareV3( 0, l - 1 ) ){ + v = this.copyV3( l - 2 ); + } else { + v = this.copyV3( 0 ); + } + this.previous.push( v[ 0 ], v[ 1 ], v[ 2 ] ); + this.previous.push( v[ 0 ], v[ 1 ], v[ 2 ] ); + for( var j = 0; j < l - 1; j++ ) { + v = this.copyV3( j ); + this.previous.push( v[ 0 ], v[ 1 ], v[ 2 ] ); + this.previous.push( v[ 0 ], v[ 1 ], v[ 2 ] ); + } + + for( var j = 1; j < l; j++ ) { + v = this.copyV3( j ); + this.next.push( v[ 0 ], v[ 1 ], v[ 2 ] ); + this.next.push( v[ 0 ], v[ 1 ], v[ 2 ] ); + } + + if( this.compareV3( l - 1, 0 ) ){ + v = this.copyV3( 1 ); + } else { + v = this.copyV3( l - 1 ); + } + this.next.push( v[ 0 ], v[ 1 ], v[ 2 ] ); + this.next.push( v[ 0 ], v[ 1 ], v[ 2 ] ); + + for( var j = 0; j < l - 1; j++ ) { + var n = j * 2; + this.indices_array.push( n, n + 1, n + 2 ); + this.indices_array.push( n + 2, n + 1, n + 3 ); + } + + if (!this.attributes) { + this.attributes = { + position: new THREE.BufferAttribute( new Float32Array( this.positions ), 3 ), + previous: new THREE.BufferAttribute( new Float32Array( this.previous ), 3 ), + next: new THREE.BufferAttribute( new Float32Array( this.next ), 3 ), + side: new THREE.BufferAttribute( new Float32Array( this.side ), 1 ), + width: new THREE.BufferAttribute( new Float32Array( this.width ), 1 ), + uv: new THREE.BufferAttribute( new Float32Array( this.uvs ), 2 ), + index: new THREE.BufferAttribute( new Uint16Array( this.indices_array ), 1 ), + counters: new THREE.BufferAttribute( new Float32Array( this.counters ), 1 ) + } + } else { + this.attributes.position.copyArray(new Float32Array(this.positions)); + this.attributes.position.needsUpdate = true; + this.attributes.previous.copyArray(new Float32Array(this.previous)); + this.attributes.previous.needsUpdate = true; + this.attributes.next.copyArray(new Float32Array(this.next)); + this.attributes.next.needsUpdate = true; + this.attributes.side.copyArray(new Float32Array(this.side)); + this.attributes.side.needsUpdate = true; + this.attributes.width.copyArray(new Float32Array(this.width)); + this.attributes.width.needsUpdate = true; + this.attributes.uv.copyArray(new Float32Array(this.uvs)); + this.attributes.uv.needsUpdate = true; + this.attributes.index.copyArray(new Uint16Array(this.indices_array)); + this.attributes.index.needsUpdate = true; + } + + this.geometry.addAttribute( 'position', this.attributes.position ); + this.geometry.addAttribute( 'previous', this.attributes.previous ); + this.geometry.addAttribute( 'next', this.attributes.next ); + this.geometry.addAttribute( 'side', this.attributes.side ); + this.geometry.addAttribute( 'width', this.attributes.width ); + this.geometry.addAttribute( 'uv', this.attributes.uv ); + this.geometry.addAttribute( 'counters', this.attributes.counters ); + + this.geometry.setIndex( this.attributes.index ); + +} + +function memcpy (src, srcOffset, dst, dstOffset, length) { + var i + + src = src.subarray || src.slice ? src : src.buffer + dst = dst.subarray || dst.slice ? dst : dst.buffer + + src = srcOffset ? src.subarray ? + src.subarray(srcOffset, length && srcOffset + length) : + src.slice(srcOffset, length && srcOffset + length) : src + + if (dst.set) { + dst.set(src, dstOffset) + } else { + for (i=0; i { - let result = gen.next(); - - if(!result.done){ - setTimeout(process, 2); - } - }; - process(); - - - - - + return inBox; } get name () { @@ -821,6 +783,56 @@ Potree.PointCloudOctree = class extends Potree.PointCloudTree { }; + getFittedBox(boxNode, maxLevel = Infinity){ + + let shrinkedLocalBounds = new THREE.Box3(); + let worldToBox = new THREE.Matrix4().getInverse(boxNode.matrixWorld); + + for(let node of this.visibleNodes){ + if(!node.sceneNode || node.getLevel() > maxLevel){ + continue; + } + + let buffer = node.geometryNode.buffer; + + let posOffset = buffer.offset("position"); + let stride = buffer.stride; + let view = new DataView(buffer.data); + + let objectToBox = new THREE.Matrix4().multiplyMatrices(worldToBox, node.sceneNode.matrixWorld); + + let pos = new THREE.Vector4(); + for(let i = 0; i < buffer.numElements; i++){ + let x = view.getFloat32(i * stride + posOffset + 0, true); + let y = view.getFloat32(i * stride + posOffset + 4, true); + let z = view.getFloat32(i * stride + posOffset + 8, true); + + pos.set(x, y, z, 1); + pos.applyMatrix4(objectToBox); + + if(-0.5 < pos.x && pos.x < 0.5){ + if(-0.5 < pos.y && pos.y < 0.5){ + if(-0.5 < pos.z && pos.z < 0.5){ + shrinkedLocalBounds.expandByPoint(pos); + } + } + } + } + } + + let fittedPosition = shrinkedLocalBounds.getCenter().applyMatrix4(boxNode.matrixWorld); + + let fitted = new THREE.Object3D(); + fitted.position.copy(fittedPosition); + fitted.scale.copy(boxNode.scale); + fitted.rotation.copy(boxNode.rotation); + + let ds = new THREE.Vector3().subVectors(shrinkedLocalBounds.max, shrinkedLocalBounds.min); + fitted.scale.multiply(ds); + + return fitted; + } + get progress () { return this.visibleNodes.length / this.visibleGeometry.length; } diff --git a/src/utils/ScreenBoxSelectTool.js b/src/utils/ScreenBoxSelectTool.js index b2a683e3..7aff889f 100644 --- a/src/utils/ScreenBoxSelectTool.js +++ b/src/utils/ScreenBoxSelectTool.js @@ -40,12 +40,11 @@ Potree.ScreenBoxSelectTool = class ScreenBoxSelectTool extends THREE.EventDispat let diff = new THREE.Vector2().subVectors(e.drag.end, e.drag.start); diff.divide(size).multiply(frustumSize); - volume.position.copy(ray.origin); volume.up.copy(camera.up); volume.rotation.copy(camera.rotation); - volume.scale.set(diff.x, diff.y, 100); + volume.scale.set(diff.x, diff.y, 1000 * 1000); e.consume(); }; @@ -56,6 +55,23 @@ Potree.ScreenBoxSelectTool = class ScreenBoxSelectTool extends THREE.EventDispat this.viewer.inputHandler.deselectAll(); this.viewer.inputHandler.toggleSelection(volume); + let camera = e.viewer.scene.getActiveCamera(); + let size = new THREE.Vector2( + e.viewer.renderer.getSize().width, + e.viewer.renderer.getSize().height); + let screenCentroid = new THREE.Vector2().addVectors(e.drag.end, e.drag.start).multiplyScalar(0.5); + let ray = Potree.utils.mouseToRay(screenCentroid, camera, size.width, size.height); + + let line = new THREE.Line3(ray.origin, new THREE.Vector3().addVectors(ray.origin, ray.direction)); + + for(let pointcloud of this.viewer.scene.pointclouds){ + let fitted = pointcloud.getFittedBox(volume, 3); + volume.scale.z = fitted.scale.z * 1.25; + + let position = line.closestPointToPoint(fitted.position, false); + volume.position.copy(position); + } + this.removeEventListener("drag", drag); this.removeEventListener("drop", drop); };