Commit 26c1a5dc authored by legoc's avatar legoc
Browse files

First version of implementation of HLLLine with LineSegment

parent aecfbbe2
......@@ -3,6 +3,7 @@ const LineSegment = require('./line-segment');
const {scale, vector3WorldToView, scalarWorldToView, vector3ViewToWorld} = require('../conversion');
const Color = require('../colors');
const {mapColor} = require('../map-color');
const { mode } = require('../../app-state');
const THREE = require('three');
class EnergyLine extends TrajectoryLine {
......@@ -11,7 +12,7 @@ class EnergyLine extends TrajectoryLine {
this._data = [];
// Define the line that will be a cylinder.
// The line is a segment.
this._line = null;
// Create a group for the data.
......@@ -29,10 +30,6 @@ class EnergyLine extends TrajectoryLine {
this._data = data;
}
highlightSpheres(value) {
this._deltaSphereGroup.visible = value;
}
updateDataGroup() {
// Reset the group transform.
......@@ -45,7 +42,7 @@ class EnergyLine extends TrajectoryLine {
this._dataGroup.applyMatrix(matrix4);
// Remove old spheres.
for (var i = this._deltaSphereGroup.children.length - 1; i >= 0; i--) {
for (let i = this._deltaSphereGroup.children.length - 1; i >= 0; i--) {
this._deltaSphereGroup.remove(this._deltaSphereGroup.children[i]);
}
......@@ -92,9 +89,7 @@ class EnergyLine extends TrajectoryLine {
// Create the line.
let lineMaterial = new THREE.LineBasicMaterial({ color: Color.Q, linewidth: 5, depthTest: true, transparent: false});
// Set the color.
this.highlight(true);
lineMaterial.color.set(this.getColor(true));
// Remove the line from the data group.
if (this._line !== null) {
......@@ -115,6 +110,20 @@ class EnergyLine extends TrajectoryLine {
this.updateDataGroup();
}
highlightLines(color) {
}
highlightSpheres(value) {
// The spheres are always visible in spy mode.
if (!mode.spyMode) {
this._deltaSphereGroup.visible = value;
}
else {
this._deltaSphereGroup.visible = true;
}
}
/**
* Set line visibility.
*/
......
const TrajectoryLine = require('./trajectory-line');
const LineSegment = require('./line-segment');
const {scale, vector3WorldToView, scalarWorldToView, vector3ViewToWorld} = require('../conversion');
const Color = require('../colors');
const TrajectorySegmentIterator = require('./trajectory-segment-iterator');
......@@ -8,47 +9,43 @@ const THREE = require('three');
class HKLLine extends TrajectoryLine {
constructor(type, parent, start, end, center, steps) {
super(type, parent, start, end, center, steps);
this._group.add(this._trajectoryLine);
let geometry = new THREE.SphereGeometry(1/scale, 8, 8);
let material = new THREE.MeshBasicMaterial({color: 0x0000ff, opacity: 0.5, depthTest: true, transparent: false});
// Create the trajectory start sphere
this._trajSSphere = new THREE.Mesh(geometry, material);
this._trajSSphere.renderOrder = 1;
this._startSphere = new THREE.Mesh(geometry, material);
this._startSphere.renderOrder = 1;
// Create the trajectory end sphere.
this._trajESphere = new THREE.Mesh(geometry, material);
this._trajESphere.renderOrder = 1;
this._endSphere = new THREE.Mesh(geometry, material);
this._endSphere.renderOrder = 1;
// The line is an array of segments.
this._line = [];
this._lineGroup = new THREE.Group();
this._group.add(this._lineGroup);
this._deltaSphereGroup = new THREE.Group();
this._group.add(this._trajSSphere);
this._group.add(this._trajESphere);
this._group.add(this._startSphere);
this._group.add(this._endSphere);
this._group.add(this._deltaSphereGroup);
this.highlight(false);
this.updateLine();
}
highlightSpheres(value) {
this._deltaSphereGroup.visible = value;
}
/**
* Update line position and delta spheres.
*/
updateLine() {
// First copy the start and end position if they changed.
this._trajSSphere.position.copy(this._start.clone());
this._trajESphere.position.copy(this._end.clone());
// Remove the line from the scene before rebuilding it.
// Indeed trying to update the line by updating the vertices does not work.
this._group.remove(this._trajectoryLine);
this._startSphere.position.copy(this._start.clone());
this._endSphere.position.copy(this._end.clone());
let geometry = new THREE.SphereGeometry(0.25/scale, 8, 8);
let geometry = new THREE.SphereGeometry(1 / scale, 8, 8);
let material = new THREE.MeshBasicMaterial({color: 0xffffff, depthTest: true, transparent: false});
let iterator = null;
......@@ -59,47 +56,87 @@ class HKLLine extends TrajectoryLine {
iterator = new TrajectoryCircleIterator(this._center, this._start, this._end, this._intervals);
}
// Empty the line.
this._line = [];
// Remove old line segments.
for (let i = this._lineGroup.children.length - 1; i >= 0; i--) {
this._lineGroup.remove(this._lineGroup.children[i]);
}
// Remove old spheres.
for (var i = this._deltaSphereGroup.children.length - 1; i >= 0; i--) {
for (let i = this._deltaSphereGroup.children.length - 1; i >= 0; i--) {
this._deltaSphereGroup.remove(this._deltaSphereGroup.children[i]);
}
// Create the line.
let lineMaterial = new THREE.LineBasicMaterial({ color: Color.Q, linewidth: 5, depthTest: true, transparent: false});
lineMaterial.color.set(this.getColor(true));
// Define the previous point for the line segments.
let previousPoint = null;
this._trajectoryLine = new THREE.Line(new THREE.Geometry(), lineMaterial);
this._trajectoryLine.geometry.verticesNeedUpdate = true;
this._trajectoryLine.renderOrder = 2;
// Place new spheres and segments.
for (let i = 0; i < this._intervals; i++) {
// Set the color.
this.highlight(true);
// Get the current point.
let currentPoint = iterator.value().clone();
// Place new spheres.
for(let i = 0; i < this._intervals; i++) {
// Add the segment when the previous point exists.
if (previousPoint !== null) {
let length = currentPoint.clone().sub(previousPoint).length();
// Add the segment if start and end points are different (bad drawing otherwise).
if (length !== 0) {
let segment = new LineSegment(previousPoint, currentPoint, 0.5 / scale, lineMaterial);
this._line.push(segment);
this._lineGroup.add(segment.object3D);
}
}
// Define a sphere.
let stepSphere = new THREE.Mesh(geometry, material);
stepSphere.renderOrder = 3;
stepSphere.position.copy(iterator.value());
this._trajectoryLine.geometry.vertices.push(iterator.value());
this._deltaSphereGroup.add(stepSphere);
// Set the previous point.
previousPoint = currentPoint;
// Next iteration.
iterator.next();
}
this._trajectoryLine.geometry.vertices.push(iterator.value());
this._trajectoryLine.geometry.verticesNeedUpdate = true;
// Last segment.
if (previousPoint !== null) {
let lastPoint = iterator.value().clone();
let length = lastPoint.clone().sub(previousPoint).length();
// Add the segment if start and end points are different (bad drawing otherwise).
if (length !== 0) {
let segment = new LineSegment(previousPoint, lastPoint, 0.5 / scale, lineMaterial);
this._lineGroup.add(segment.object3D);
}
}
}
highlightLines(color) {
// Change the color of the segments.
for (let i = 0; i < this._line.length; i++) {
this._line[i].material.color.set(color);
}
}
// Add the newly created line.
this._group.add(this._trajectoryLine);
highlightSpheres(value) {
this._deltaSphereGroup.visible = value;
}
/**
* Set line visibility.
*/
setVisibility(visibility) {
this._trajectoryLine.visible = visibility;
this._trajSSphere.visible = visibility;
this._trajESphere.visible = visibility;
this._startSphere.visible = visibility;
this._endSphere.visible = visibility;
this._lineGroup.visible = visibility;
this._deltaSphereGroup.visible = visibility;
}
......@@ -107,13 +144,10 @@ class HKLLine extends TrajectoryLine {
* Remove this trajectory properly.
*/
destroy() {
this._trajSSphere.geometry.dispose();
this._trajESphere.geometry.dispose();
this._trajSSphere.material.dispose();
this._trajESphere.material.dispose();
this._trajectoryLine.geometry.dispose();
this._trajectoryLine.material.dispose();
this._startSphere.geometry.dispose();
this._endSphere.geometry.dispose();
this._startSphere.material.dispose();
this._endSphere.material.dispose();
for (let d of this._deltaSphereGroup.children) {
d.geometry.dispose();
......
......@@ -33,6 +33,10 @@ class LineSegment {
return this._group;
}
get material() {
return this._material;
}
destroy() {
this._material.dispose();
this._geometry.dispose();
......
......@@ -16,14 +16,6 @@ class TrajectoryLine {
this._type = type;
this._center = center.clone();
this._scale = 1.0;
// Create the line between start and end points.
let lineGeo = new THREE.Geometry();
let lineMaterial = new THREE.LineBasicMaterial({ color: Color.Q, linewidth: 5, depthTest: true, transparent: false});
this._trajectoryLine = new THREE.Line(lineGeo, lineMaterial);
this._trajectoryLine.geometry.verticesNeedUpdate = true;
this._trajectoryLine.renderOrder = 2;
// Create a group for the elements of the line.
this._group = new THREE.Group();
......@@ -72,32 +64,39 @@ class TrajectoryLine {
this.updateLine();
}
highlight(value) {
/**
* Update line position and delta spheres.
*/
updateLine() {
throw new Error('updateLine must be redefined');
}
// Change the color of the line.
getColor(highlight) {
// Get the color of the line.
if (this._type == 'q') {
this._trajectoryLine.material.color.set(value ? Color.Q_HIGHLIGHT : Color.Q);
return (highlight ? Color.Q_HIGHLIGHT : Color.Q);
}
else if (this._type == 'ki') {
this._trajectoryLine.material.color.set(value ? Color.KI_HIGHLIGHT : Color.KI);
return (highlight ? Color.KI_HIGHLIGHT : Color.KI);
}
else if (this._type == 'kf') {
this._trajectoryLine.material.color.set(value ? Color.KF_HIGHLIGHT : Color.KF);
return (highlight ? Color.KF_HIGHLIGHT : Color.KF);
}
}
// Set the points visible or not.
highlight(value) {
let color = this.getColor(value);
this.highlightLines(color);
this.highlightSpheres(value);
}
highlightSpheres(value) {
throw new Error('updateSpheres must be redefined');
highlightLines(color) {
throw new Error('highlightLines must be redefined');
}
/**
* Update line position and delta spheres.
*/
updateLine() {
throw new Error('updateLine must be redefined');
highlightSpheres(value) {
throw new Error('highlightSpheres must be redefined');
}
scale(value) {
......
......@@ -46,6 +46,8 @@ class TrajectoryView3D extends View3D {
this._drawLines = false;
// Sequence of events is: dragstart, mousedown, drag, dragend, mouseup.
dragControls.addEventListener('dragstart', (event) => {
// Drag is only possible on q and ki points.
......@@ -87,10 +89,6 @@ class TrajectoryView3D extends View3D {
this._hklLine.type = this._type;
});
dragControls.addEventListener('dragend', (event) => {
this._isDragging = false;
});
document.addEventListener('mousedown', (event) => {
if (event.button === 2 && this._isDragging && this._allowedTrajectory) {
......@@ -132,6 +130,10 @@ class TrajectoryView3D extends View3D {
}
});
dragControls.addEventListener('dragend', (event) => {
this._isDragging = false;
});
document.addEventListener('mouseup', (event) => {
if (event.button === 2 && this._start !== null && this._allowedTrajectory) {
......@@ -531,7 +533,7 @@ class TrajectoryView3D extends View3D {
let energyStart = this.loadEnergyPoint(trajectory.start, bMatrix, uMatrix);
let energyEnd = this.loadEnergyPoint(trajectory.end, bMatrix, uMatrix);
energyLine = new EnergyLineLine('q', this._energyTranslationGroup,
energyLine = new EnergyLine('q', this._energyTranslationGroup,
energyStart,
energyEnd,
energyStart,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment