660 lines
16 KiB
JavaScript
660 lines
16 KiB
JavaScript
|
|
|
|
Potree.HeightProfile = function(){
|
|
var scope = this;
|
|
|
|
THREE.Object3D.call( this );
|
|
|
|
this.points = [];
|
|
this.spheres = [];
|
|
this.edges = [];
|
|
this.boxes = [];
|
|
this.width = 1;
|
|
this.height = 20;
|
|
this._modifiable = true;
|
|
|
|
var sphereGeometry = new THREE.SphereGeometry(0.4, 10, 10);
|
|
var lineColor = new THREE.Color( 0xff0000 );
|
|
|
|
var createSphereMaterial = function(){
|
|
var sphereMaterial = new THREE.MeshLambertMaterial({
|
|
shading: THREE.SmoothShading,
|
|
color: 0xff0000,
|
|
ambient: 0xaaaaaa,
|
|
depthTest: false,
|
|
depthWrite: false}
|
|
);
|
|
|
|
return sphereMaterial;
|
|
};
|
|
|
|
var moveEvent = function(event){
|
|
event.target.material.emissive.setHex(0x888888);
|
|
};
|
|
|
|
var leaveEvent = function(event){
|
|
event.target.material.emissive.setHex(0x000000);
|
|
};
|
|
|
|
var dragEvent = function(event){
|
|
|
|
var tool = event.tool;
|
|
var dragstart = tool.dragstart;
|
|
var mouse = tool.mouse;
|
|
|
|
if(event.event.ctrlKey){
|
|
|
|
var mouseStart = new THREE.Vector3(dragstart.mousePos.x, dragstart.mousePos.y, 0);
|
|
var mouseEnd = new THREE.Vector3(mouse.x, mouse.y, 0);
|
|
var widthStart = dragstart.widthStart;
|
|
|
|
var scale = 1 - 10 * (mouseStart.y - mouseEnd.y);
|
|
scale = Math.max(0.01, scale);
|
|
if(widthStart){
|
|
scope.setWidth(widthStart * scale);
|
|
}
|
|
|
|
}else{
|
|
|
|
var I = tool.getMousePointCloudIntersection();
|
|
|
|
if(I){
|
|
var index = scope.spheres.indexOf(tool.dragstart.object);
|
|
scope.setPosition(index, I);
|
|
}
|
|
}
|
|
|
|
event.event.stopImmediatePropagation();
|
|
};
|
|
|
|
var dropEvent = function(event){
|
|
|
|
};
|
|
|
|
this.addMarker = function(point){
|
|
|
|
this.points.push(point);
|
|
|
|
// sphere
|
|
var sphere = new THREE.Mesh(sphereGeometry, createSphereMaterial());
|
|
sphere.addEventListener("mousemove", moveEvent);
|
|
sphere.addEventListener("mouseleave", leaveEvent);
|
|
sphere.addEventListener("mousedrag", dragEvent);
|
|
sphere.addEventListener("drop", dropEvent);
|
|
|
|
this.add(sphere);
|
|
this.spheres.push(sphere);
|
|
|
|
// edges & boxes
|
|
if(this.points.length > 1){
|
|
|
|
var lineGeometry = new THREE.Geometry();
|
|
lineGeometry.vertices.push(new THREE.Vector3(), new THREE.Vector3());
|
|
lineGeometry.colors.push(lineColor, lineColor, lineColor);
|
|
var lineMaterial = new THREE.LineBasicMaterial( {
|
|
vertexColors: THREE.VertexColors,
|
|
linewidth: 2,
|
|
transparent: true,
|
|
opacity: 0.4
|
|
});
|
|
lineMaterial.depthTest = false;
|
|
var edge = new THREE.Line(lineGeometry, lineMaterial);
|
|
edge.visible = false;
|
|
|
|
this.add(edge);
|
|
this.edges.push(edge);
|
|
|
|
|
|
var boxGeometry = new THREE.BoxGeometry(1, 1, 1);
|
|
var boxMaterial = new THREE.MeshBasicMaterial({color: 0xff0000, transparent: true, opacity: 0.2});
|
|
var box = new THREE.Mesh(boxGeometry, boxMaterial);
|
|
box.visible = false;
|
|
|
|
this.add(box);
|
|
this.boxes.push(box);
|
|
|
|
}
|
|
|
|
|
|
var event = {
|
|
"type": "marker_added",
|
|
"profile": this
|
|
};
|
|
this.dispatchEvent(event);
|
|
|
|
this.setPosition(this.points.length-1, point);
|
|
};
|
|
|
|
this.removeMarker = function(index){
|
|
this.points.splice(index, 1);
|
|
|
|
this.remove(this.spheres[index]);
|
|
|
|
var edgeIndex = (index === 0) ? 0 : (index - 1);
|
|
this.remove(this.edges[edgeIndex]);
|
|
this.edges.splice(edgeIndex, 1);
|
|
this.remove(this.boxes[edgeIndex]);
|
|
this.boxes.splice(edgeIndex, 1);
|
|
|
|
this.spheres.splice(index, 1);
|
|
|
|
this.update();
|
|
|
|
var event = {
|
|
"type": "marker_removed",
|
|
"profile": this
|
|
};
|
|
this.dispatchEvent(event);
|
|
};
|
|
|
|
/**
|
|
* see http://www.mathopenref.com/coordpolygonarea2.html
|
|
*/
|
|
this.getArea = function(){
|
|
var area = 0;
|
|
var j = this.points.length - 1;
|
|
|
|
for(var i = 0; i < this.points.length; i++){
|
|
var p1 = this.points[i];
|
|
var p2 = this.points[j];
|
|
area += (p2.x + p1.x) * (p1.z - p2.z);
|
|
j = i;
|
|
}
|
|
|
|
return Math.abs(area / 2);
|
|
};
|
|
|
|
this.setPosition = function(index, position){
|
|
var point = this.points[index];
|
|
point.copy(position);
|
|
|
|
var event = {
|
|
type: 'marker_moved',
|
|
profile: this,
|
|
index: index,
|
|
position: position.clone()
|
|
};
|
|
this.dispatchEvent(event);
|
|
|
|
this.update();
|
|
};
|
|
|
|
this.setWidth = function(width){
|
|
this.width = width;
|
|
|
|
var event = {
|
|
type: 'width_changed',
|
|
profile: this,
|
|
width: width
|
|
};
|
|
this.dispatchEvent(event);
|
|
|
|
this.update();
|
|
};
|
|
|
|
this.update = function(){
|
|
|
|
if(this.points.length === 0){
|
|
return;
|
|
}else if(this.points.length === 1){
|
|
var point = this.points[0];
|
|
this.spheres[0].position.copy(point);
|
|
|
|
return;
|
|
}
|
|
|
|
var min = this.points[0].clone();
|
|
var max = this.points[0].clone();
|
|
var centroid = new THREE.Vector3();
|
|
var lastIndex = this.points.length - 1;
|
|
for(var i = 0; i <= lastIndex; i++){
|
|
var point = this.points[i];
|
|
var sphere = this.spheres[i];
|
|
var leftIndex = (i === 0) ? lastIndex : i - 1;
|
|
var rightIndex = (i === lastIndex) ? 0 : i + 1;
|
|
var leftVertex = this.points[leftIndex];
|
|
var rightVertex = this.points[rightIndex];
|
|
var leftEdge = this.edges[leftIndex];
|
|
var rightEdge = this.edges[i];
|
|
var leftBox = this.boxes[leftIndex];
|
|
var rightBox = this.boxes[i];
|
|
|
|
var leftEdgeLength = point.distanceTo(leftVertex);
|
|
var rightEdgeLength = point.distanceTo(rightVertex);
|
|
var leftEdgeCenter = new THREE.Vector3().addVectors(leftVertex, point).multiplyScalar(0.5);
|
|
var rightEdgeCenter = new THREE.Vector3().addVectors(point, rightVertex).multiplyScalar(0.5);
|
|
|
|
sphere.position.copy(point);
|
|
|
|
if(this._modifiable){
|
|
sphere.visible = true;
|
|
}else{
|
|
sphere.visible = false;
|
|
}
|
|
|
|
if(leftEdge){
|
|
leftEdge.geometry.vertices[1].copy(point);
|
|
leftEdge.geometry.verticesNeedUpdate = true;
|
|
leftEdge.geometry.computeBoundingSphere();
|
|
}
|
|
|
|
if(rightEdge){
|
|
rightEdge.geometry.vertices[0].copy(point);
|
|
rightEdge.geometry.verticesNeedUpdate = true;
|
|
rightEdge.geometry.computeBoundingSphere();
|
|
}
|
|
|
|
if(leftBox){
|
|
var start = leftVertex;
|
|
var end = point;
|
|
var length = start.clone().setY(0).distanceTo(end.clone().setY(0));
|
|
leftBox.scale.set(length, this.height, this.width);
|
|
|
|
var center = new THREE.Vector3().addVectors(start, end).multiplyScalar(0.5);
|
|
var diff = new THREE.Vector3().subVectors(end, start);
|
|
var target = new THREE.Vector3(diff.z, 0, -diff.x);
|
|
|
|
leftBox.position.set(0,0,0);
|
|
leftBox.lookAt(target);
|
|
leftBox.position.copy(center);
|
|
}
|
|
|
|
|
|
|
|
|
|
centroid.add(point);
|
|
min.min(point);
|
|
max.max(point);
|
|
}
|
|
centroid.multiplyScalar(1 / this.points.length);
|
|
|
|
for(var i = 0; i < this.boxes.length; i++){
|
|
var box = this.boxes[i];
|
|
|
|
box.position.y = min.y + (max.y - min.y) / 2;
|
|
//box.scale.y = max.y - min.y + 50;
|
|
box.scale.y = 1000000;
|
|
}
|
|
|
|
};
|
|
|
|
this.raycast = function(raycaster, intersects){
|
|
|
|
for(var i = 0; i < this.points.length; i++){
|
|
var sphere = this.spheres[i];
|
|
|
|
sphere.raycast(raycaster, intersects);
|
|
}
|
|
|
|
// recalculate distances because they are not necessarely correct
|
|
// for scaled objects.
|
|
// see https://github.com/mrdoob/three.js/issues/5827
|
|
// TODO: remove this once the bug has been fixed
|
|
for(var i = 0; i < intersects.length; i++){
|
|
var I = intersects[i];
|
|
I.distance = raycaster.ray.origin.distanceTo(I.point);
|
|
}
|
|
intersects.sort( function ( a, b ) { return a.distance - b.distance;} );
|
|
};
|
|
|
|
|
|
};
|
|
|
|
Potree.HeightProfile.prototype = Object.create( THREE.Object3D.prototype );
|
|
|
|
Object.defineProperty(Potree.HeightProfile.prototype, "modifiable", {
|
|
get: function(){
|
|
return this.modifiable;
|
|
},
|
|
set: function(value){
|
|
this._modifiable = value;
|
|
this.update();
|
|
}
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
// calculating area of a polygon:
|
|
// http://www.mathopenref.com/coordpolygonarea2.html
|
|
//
|
|
//
|
|
//
|
|
|
|
Potree.ProfileTool = function(scene, camera, renderer){
|
|
|
|
var scope = this;
|
|
this.enabled = false;
|
|
|
|
this.scene = scene;
|
|
this.camera = camera;
|
|
this.renderer = renderer;
|
|
this.domElement = renderer.domElement;
|
|
this.mouse = {x: 0, y: 0};
|
|
|
|
var STATE = {
|
|
DEFAULT: 0,
|
|
INSERT: 1
|
|
};
|
|
|
|
var state = STATE.DEFAULT;
|
|
|
|
var sphereGeometry = new THREE.SphereGeometry(0.4, 10, 10);
|
|
|
|
this.activeProfile = null;
|
|
this.profiles = [];
|
|
this.sceneProfile = new THREE.Scene();
|
|
this.sceneRoot = new THREE.Object3D();
|
|
this.sceneProfile.add(this.sceneRoot);
|
|
|
|
this.light = new THREE.DirectionalLight( 0xffffff, 1 );
|
|
this.light.position.set( 0, 0, 10 );
|
|
this.light.lookAt(new THREE.Vector3(0,0,0));
|
|
this.sceneProfile.add( this.light );
|
|
|
|
this.hoveredElement = null;
|
|
|
|
function createSphereMaterial(){
|
|
var sphereMaterial = new THREE.MeshLambertMaterial({
|
|
shading: THREE.SmoothShading,
|
|
color: 0xff0000,
|
|
ambient: 0xaaaaaa,
|
|
depthTest: false,
|
|
depthWrite: false}
|
|
);
|
|
|
|
return sphereMaterial;
|
|
};
|
|
|
|
|
|
function onClick(event){
|
|
|
|
if(state === STATE.INSERT){
|
|
var I = scope.getMousePointCloudIntersection();
|
|
if(I){
|
|
var pos = I.clone();
|
|
|
|
scope.activeProfile.addMarker(pos);
|
|
|
|
var event = {
|
|
type: 'newpoint',
|
|
position: pos.clone()
|
|
};
|
|
scope.dispatchEvent(event);
|
|
|
|
}
|
|
}
|
|
};
|
|
|
|
function onMouseMove(event){
|
|
var rect = scope.domElement.getBoundingClientRect();
|
|
scope.mouse.x = ((event.clientX - rect.left) / scope.domElement.clientWidth) * 2 - 1;
|
|
scope.mouse.y = -((event.clientY - rect.top) / scope.domElement.clientHeight) * 2 + 1;
|
|
|
|
if(scope.dragstart){
|
|
var arg = {
|
|
type: "mousedrag",
|
|
event: event,
|
|
tool: scope
|
|
};
|
|
scope.dragstart.object.dispatchEvent(arg);
|
|
|
|
}else if(state == STATE.INSERT && scope.activeProfile){
|
|
var I = scope.getMousePointCloudIntersection();
|
|
|
|
if(I){
|
|
|
|
var lastIndex = scope.activeProfile.points.length-1;
|
|
scope.activeProfile.setPosition(lastIndex, I);
|
|
}
|
|
|
|
}else{
|
|
var I = getHoveredElement();
|
|
|
|
if(I){
|
|
|
|
I.object.dispatchEvent({type: "mousemove", target: I.object, event: event});
|
|
|
|
if(scope.hoveredElement && scope.hoveredElement !== I.object){
|
|
scope.hoveredElement.dispatchEvent({type: "mouseleave", target: scope.hoveredElement, event: event});
|
|
}
|
|
|
|
scope.hoveredElement = I.object;
|
|
|
|
}else{
|
|
|
|
if(scope.hoveredElement){
|
|
scope.hoveredElement.dispatchEvent({type: "mouseleave", target: scope.hoveredElement, event: event});
|
|
}
|
|
|
|
scope.hoveredElement = null;
|
|
|
|
}
|
|
}
|
|
};
|
|
|
|
function onRightClick(event){
|
|
if(state == STATE.INSERT){
|
|
scope.finishInsertion();
|
|
}
|
|
}
|
|
|
|
function onMouseDown(event){
|
|
|
|
if(state !== STATE.DEFAULT){
|
|
event.stopImmediatePropagation();
|
|
}
|
|
|
|
if(event.which === 1){
|
|
|
|
var I = getHoveredElement();
|
|
|
|
if(I){
|
|
|
|
var widthStart = null;
|
|
for(var i = 0; i < scope.profiles.length; i++){
|
|
var profile = scope.profiles[i];
|
|
for(var j = 0; j < profile.spheres.length; j++){
|
|
var sphere = profile.spheres[j];
|
|
|
|
if(sphere === I.object){
|
|
widthStart = profile.width;
|
|
}
|
|
}
|
|
}
|
|
|
|
scope.dragstart = {
|
|
object: I.object,
|
|
sceneClickPos: I.point,
|
|
sceneStartPos: scope.sceneRoot.position.clone(),
|
|
mousePos: {x: scope.mouse.x, y: scope.mouse.y},
|
|
widthStart: widthStart
|
|
};
|
|
event.stopImmediatePropagation();
|
|
|
|
}
|
|
|
|
}else if(event.which === 3){
|
|
onRightClick(event);
|
|
}
|
|
}
|
|
|
|
function onDoubleClick(event){
|
|
|
|
// fix move event after double click
|
|
// see: http://stackoverflow.com/questions/8125165/event-listener-for-dblclick-causes-event-for-mousemove-to-not-work-and-show-a-ci
|
|
if (window.getSelection){
|
|
window.getSelection().removeAllRanges();
|
|
}else if (document.selection){
|
|
document.selection.empty();
|
|
}
|
|
|
|
if(scope.activeProfile && state === STATE.INSERT){
|
|
scope.activeProfile.removeMarker(scope.activeProfile.points.length-1);
|
|
scope.finishInsertion();
|
|
event.stopImmediatePropagation();
|
|
}
|
|
}
|
|
|
|
function onMouseUp(event){
|
|
|
|
if(scope.dragstart){
|
|
scope.dragstart.object.dispatchEvent({type: "drop", event: event});
|
|
scope.dragstart = null;
|
|
}
|
|
|
|
}
|
|
|
|
function getHoveredElement(){
|
|
|
|
var vector = new THREE.Vector3( scope.mouse.x, scope.mouse.y, 0.5 );
|
|
vector.unproject(scope.camera);
|
|
|
|
var raycaster = new THREE.Raycaster();
|
|
raycaster.ray.set( scope.camera.position, vector.sub( scope.camera.position ).normalize() );
|
|
|
|
var intersections = raycaster.intersectObjects(scope.profiles);
|
|
|
|
if(intersections.length > 0){
|
|
return intersections[0];
|
|
}else{
|
|
return false;
|
|
}
|
|
};
|
|
|
|
this.getMousePointCloudIntersection = function(){
|
|
var vector = new THREE.Vector3( scope.mouse.x, scope.mouse.y, 0.5 );
|
|
vector.unproject(scope.camera);
|
|
|
|
var direction = vector.sub(scope.camera.position).normalize();
|
|
var ray = new THREE.Ray(scope.camera.position, direction);
|
|
|
|
var pointClouds = [];
|
|
scope.scene.traverse(function(object){
|
|
if(object instanceof Potree.PointCloudOctree || object instanceof Potree.PointCloudArena4D){
|
|
pointClouds.push(object);
|
|
}
|
|
});
|
|
|
|
var closestPoint = null;
|
|
var closestPointDistance = null;
|
|
|
|
for(var i = 0; i < pointClouds.length; i++){
|
|
var pointcloud = pointClouds[i];
|
|
var point = pointcloud.pick(scope.renderer, scope.camera, ray);
|
|
|
|
if(!point){
|
|
continue;
|
|
}
|
|
|
|
var distance = scope.camera.position.distanceTo(point.position);
|
|
|
|
if(!closestPoint || distance < closestPointDistance){
|
|
closestPoint = point;
|
|
closestPointDistance = distance;
|
|
}
|
|
}
|
|
|
|
return closestPoint ? closestPoint.position : null;
|
|
};
|
|
|
|
this.startInsertion = function(args){
|
|
state = STATE.INSERT;
|
|
|
|
var args = args || {};
|
|
var clip = args.clip || false;
|
|
var width = args.width || 1.0;
|
|
|
|
this.activeProfile = new Potree.HeightProfile();
|
|
this.activeProfile.clip = clip;
|
|
this.activeProfile.setWidth(width);
|
|
this.addProfile(this.activeProfile);
|
|
this.activeProfile.addMarker(new THREE.Vector3(0,0,0));
|
|
|
|
return this.activeProfile;
|
|
};
|
|
|
|
this.finishInsertion = function(){
|
|
this.activeProfile.removeMarker(this.activeProfile.points.length-1);
|
|
|
|
var event = {
|
|
type: "insertion_finished",
|
|
profile: this.activeProfile
|
|
};
|
|
this.dispatchEvent(event);
|
|
|
|
this.activeProfile = null;
|
|
state = STATE.DEFAULT;
|
|
};
|
|
|
|
this.addProfile = function(profile){
|
|
this.profiles.push(profile);
|
|
this.sceneProfile.add(profile);
|
|
profile.update();
|
|
|
|
this.dispatchEvent({"type": "profile_added", profile: profile});
|
|
profile.addEventListener("marker_added", function(event){
|
|
scope.dispatchEvent(event);
|
|
});
|
|
profile.addEventListener("marker_removed", function(event){
|
|
scope.dispatchEvent(event);
|
|
});
|
|
profile.addEventListener("marker_moved", function(event){
|
|
scope.dispatchEvent(event);
|
|
});
|
|
};
|
|
|
|
this.removeProfile = function(profile){
|
|
this.sceneProfile.remove(profile);
|
|
var index = this.profiles.indexOf(profile);
|
|
if(index >= 0){
|
|
this.profiles.splice(index, 1);
|
|
}
|
|
|
|
this.dispatchEvent({"type": "profile_removed", profile: profile});
|
|
};
|
|
|
|
this.reset = function(){
|
|
for(var i = this.profiles.length - 1; i >= 0; i--){
|
|
var profile = this.profiles[i];
|
|
this.removeProfile(profile);
|
|
}
|
|
};
|
|
|
|
this.update = function(){
|
|
|
|
for(var i = 0; i < this.profiles.length; i++){
|
|
var profile = this.profiles[i];
|
|
for(var j = 0; j < profile.spheres.length; j++){
|
|
var sphere = profile.spheres[j];
|
|
|
|
var distance = scope.camera.position.distanceTo(sphere.getWorldPosition());
|
|
var pr = projectedRadius(1, scope.camera.fov * Math.PI / 180, distance, renderer.domElement.clientHeight);
|
|
var scale = (15 / pr);
|
|
sphere.scale.set(scale, scale, scale);
|
|
}
|
|
}
|
|
|
|
this.light.position.copy(this.camera.position);
|
|
this.light.lookAt(this.camera.getWorldDirection().add(this.camera.position));
|
|
|
|
};
|
|
|
|
this.render = function(){
|
|
this.update();
|
|
renderer.render(this.sceneProfile, this.camera);
|
|
};
|
|
|
|
this.domElement.addEventListener( 'click', onClick, false);
|
|
this.domElement.addEventListener( 'dblclick', onDoubleClick, false);
|
|
this.domElement.addEventListener( 'mousemove', onMouseMove, false );
|
|
this.domElement.addEventListener( 'mousedown', onMouseDown, false );
|
|
this.domElement.addEventListener( 'mouseup', onMouseUp, true );
|
|
|
|
};
|
|
|
|
|
|
Potree.ProfileTool.prototype = Object.create( THREE.EventDispatcher.prototype );
|