refactoring profile tool; profiles can be created and modified programmatically

This commit is contained in:
mschuetz 2015-04-27 17:04:12 +02:00
parent 21db936974
commit e608e9af5a
2 changed files with 395 additions and 304 deletions

View File

@ -3,34 +3,104 @@
If you're missing some functionality in the interface, you can still achieve certain things by using your browsers developer tools as a scripting console. Open the developer tools with ctrl + shift + i, paste code into the console and execute it. If you're missing some functionality in the interface, you can still achieve certain things by using your browsers developer tools as a scripting console. Open the developer tools with ctrl + shift + i, paste code into the console and execute it.
## Creating a height profile
A height profile can be created with code as follows:
```
var p = new Potree.HeightProfile();
p.addMarker(new THREE.Vector3(-100,0,0));
p.addMarker(new THREE.Vector3(100,0,0));
p.addMarker(new THREE.Vector3(100,0,100));
p.addMarker(new THREE.Vector3(-100,0,100));
p.setWidth(5);
profileTool.addProfile(p);
```
This will create a profile with 4 points / 3 segments.
With this method, the height of the markers is not necessarly aligned with the point
cloud. Height markers can be removed with
```
p.modifiable = false;
```
The positions can still be modified programmatically through
```
var markerIndex = 0;
var position = new THREE.Vector3(10, 10, 0);
p.setPosition(markerIndex, position);
```
In order to remove a marker, call
```
p.removeMarker(index);
```
## Get points inside height profile ## Get points inside height profile
Use this to get a list of points inside the profile. Use this to get a list of points inside the profile.
Right now, an array of point coordinates will be returned. Points are grouped in segments.
In a future version, all point attributes (intensity, classification, ...)
will be returned.
``` ```
var maxOctreeDepth = 2; var maxOctreeDepth = 2;
var points = pointcloud.getPointsInProfile(profileTool.profiles[0], maxOctreeDepth); var segments = pointcloud.getPointsInProfile(profileTool.profiles[0], maxOctreeDepth);
``` ```
The following code will create a sphere at each point location in the profile: The following code will create a sphere at each point location in the profile:
``` ```
var maxOctreeDepth = 2; var maxOctreeDepth = 2;
var points = pointcloud.getPointsInProfile(profileTool.profiles[0], maxOctreeDepth); var segments = pointcloud.getPointsInProfile(profileTool.profiles[0], maxOctreeDepth);
var sg = new THREE.SphereGeometry(1, 8, 8); var sg = new THREE.SphereGeometry(1, 8, 8);
for(var i = 0; i < points.length; i++){ for(var i = 0; i < segments.length; i++){
var sphere = new THREE.Mesh(sg); var segment = segments[i];
sphere.position.copy(points[i]); for(var j = 0; j < segment.points.numPoints; j++){
scene.add(sphere); var position = segment.points.position[j];
var sphere = new THREE.Mesh(sg);
sphere.position.copy(position);
scene.add(sphere);
}
} }
``` ```
![](./images/scripting_profile_spheres.png) ![](./images/scripting_profile_spheres.png)
### Projecting a profile to 2D
Each segment also contains a project() function which aligns the points along the x-axis.
This can be used, for example, to generate 2D height profile images.
```
position = segment.project(position);
```
Consecutive segments are placed next to each other. The following code
creates spheres on the x-axis projected point coordinates:
```
var maxOctreeDepth = 1;
var segments = pointcloud.getPointsInProfile(profileTool.profiles[0], maxOctreeDepth);
var sg = new THREE.SphereGeometry(1, 8, 8);
for(var i = 0; i < segments.length; i++){
var segment = segments[i];
for(var j = 0; j < segment.points.numPoints; j++){
var position = segment.points.position[j];
position = segment.project(position);
var sphere = new THREE.Mesh(sg);
sphere.position.copy(position);
scene.add(sphere);
}
}
```
## Set width of height profile ## Set width of height profile
The profile width can be changed either by holding ctrl while dragging one of the The profile width can be changed either by holding ctrl while dragging one of the
@ -38,9 +108,6 @@ endpoints up and down or using this code:
``` ```
profileTool.profiles[0].setWidth(2); profileTool.profiles[0].setWidth(2);
profileTool.profiles[0].update();
``` ```
(The call to update() is required now but won't be in a future version.)
![](./images/scripting_profile_width.png) ![](./images/scripting_profile_width.png)

View File

@ -1,4 +1,296 @@
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;
this.camera = camera;
this.renderer = renderer;
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);
}
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();
};
/**
* 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);
this.update();
};
this.setWidth = function(width){
this.width = width;
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: // calculating area of a polygon:
// http://www.mathopenref.com/coordpolygonarea2.html // http://www.mathopenref.com/coordpolygonarea2.html
@ -39,292 +331,6 @@ Potree.ProfileTool = function(scene, camera, renderer){
this.hoveredElement = null; this.hoveredElement = null;
var moveEvent = function(event){
event.target.material.emissive.setHex(0x888888);
};
var leaveEvent = function(event){
event.target.material.emissive.setHex(0x000000);
};
var dragEvent = function(event){
if(event.event.ctrlKey){
var mouseStart = new THREE.Vector3(scope.dragstart.mousePos.x, scope.dragstart.mousePos.y, 0);
var mouseEnd = new THREE.Vector3(scope.mouse.x, scope.mouse.y, 0);
var widthStart = scope.dragstart.widthStart;
var scale = 1 - 10 * (mouseStart.y - mouseEnd.y);
scale = Math.max(0.01, scale);
if(widthStart){
for(var i = 0; i < scope.profiles.length; i++){
var m = scope.profiles[i];
var index = m.spheres.indexOf(scope.dragstart.object);
if(index >= 0){
m.setWidth(widthStart * scale);
m.update();
break;
}
}
}
}else{
var I = getMousePointCloudIntersection();
if(I){
for(var i = 0; i < scope.profiles.length; i++){
var m = scope.profiles[i];
var index = m.spheres.indexOf(scope.dragstart.object);
if(index >= 0){
scope.profiles[i].setPosition(index, I);
break;
}
}
//scope.dragstart.object.position.copy(I);
}
}
event.event.stopImmediatePropagation();
};
var dropEvent = function(event){
};
function Profile(){
THREE.Object3D.call( this );
this.points = [];
this.spheres = [];
this.edges = [];
this.boxes = [];
this.width = 1;
this.height = 20;
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;
};
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);
}
this.setPosition(this.points.length-1, point);
};
this.removeMarker = function(index){
this.points.splice(index, 1);
this.remove(this.spheres[index]);
if(index > 0){
this.remove(this.edges[index-1]);
this.edges.splice(index-1, 1);
this.remove(this.boxes[index-1]);
this.boxes.splice(index-1, 1);
}
this.spheres.splice(index, 1);
this.update();
};
/**
* 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);
this.update();
};
this.setWidth = function(width){
this.width = width;
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(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;} );
}
}
Profile.prototype = Object.create( THREE.Object3D.prototype );
function createSphereMaterial(){ function createSphereMaterial(){
var sphereMaterial = new THREE.MeshLambertMaterial({ var sphereMaterial = new THREE.MeshLambertMaterial({
shading: THREE.SmoothShading, shading: THREE.SmoothShading,
@ -341,7 +347,7 @@ Potree.ProfileTool = function(scene, camera, renderer){
function onClick(event){ function onClick(event){
if(state === STATE.INSERT){ if(state === STATE.INSERT){
var I = getMousePointCloudIntersection(); var I = scope.getMousePointCloudIntersection();
if(I){ if(I){
var pos = I.clone(); var pos = I.clone();
@ -363,11 +369,15 @@ Potree.ProfileTool = function(scene, camera, renderer){
scope.mouse.y = -((event.clientY - rect.top) / scope.domElement.clientHeight) * 2 + 1; scope.mouse.y = -((event.clientY - rect.top) / scope.domElement.clientHeight) * 2 + 1;
if(scope.dragstart){ if(scope.dragstart){
var arg = {
scope.dragstart.object.dispatchEvent({type: "mousedrag", event: event}); type: "mousedrag",
event: event,
tool: scope
};
scope.dragstart.object.dispatchEvent(arg);
}else if(state == STATE.INSERT && scope.activeProfile){ }else if(state == STATE.INSERT && scope.activeProfile){
var I = getMousePointCloudIntersection(); var I = scope.getMousePointCloudIntersection();
if(I){ if(I){
@ -472,7 +482,7 @@ Potree.ProfileTool = function(scene, camera, renderer){
} }
}; };
function getMousePointCloudIntersection(){ this.getMousePointCloudIntersection = function(){
var vector = new THREE.Vector3( scope.mouse.x, scope.mouse.y, 0.5 ); var vector = new THREE.Vector3( scope.mouse.x, scope.mouse.y, 0.5 );
vector.unproject(scope.camera); vector.unproject(scope.camera);
@ -515,7 +525,7 @@ Potree.ProfileTool = function(scene, camera, renderer){
var clip = args.clip || false; var clip = args.clip || false;
var width = args.width || 1.0; var width = args.width || 1.0;
this.activeProfile = new Profile(); this.activeProfile = new Potree.HeightProfile();
this.activeProfile.clip = clip; this.activeProfile.clip = clip;
this.activeProfile.setWidth(width); this.activeProfile.setWidth(width);
this.sceneProfile.add(this.activeProfile); this.sceneProfile.add(this.activeProfile);
@ -529,6 +539,20 @@ Potree.ProfileTool = function(scene, camera, renderer){
state = STATE.DEFAULT; state = STATE.DEFAULT;
}; };
this.addProfile = function(profile){
this.profiles.push(profile);
this.sceneProfile.add(profile);
profile.update();
};
this.removeProfile = function(profile){
this.sceneProfile.remove(profile);
var index = this.profiles.indexOf(profile);
if(index >= 0){
this.profiles.splice(index, 1);
}
}
this.update = function(){ this.update = function(){
for(var i = 0; i < this.profiles.length; i++){ for(var i = 0; i < this.profiles.length; i++){