Commit aecfbbe2 authored by legoc's avatar legoc
Browse files

Added line segment class

parent e3455c83
const TrajectoryLine = require('./trajectory-line');
const LineSegment = require('./line-segment');
const {scale, vector3WorldToView, scalarWorldToView, vector3ViewToWorld} = require('../conversion');
const Color = require('../colors');
const {mapColor} = require('../map-color');
......@@ -17,10 +18,6 @@ class EnergyLine extends TrajectoryLine {
this._dataGroup = new THREE.Group();
this._group.add(this._dataGroup);
this._lineGroup = new THREE.Group();
this._lineGroup.rotateX(Math.PI/2);
this._dataGroup.add(this._lineGroup);
this._deltaSphereGroup = new THREE.Group();
this._dataGroup.add(this._deltaSphereGroup);
......@@ -99,18 +96,20 @@ class EnergyLine extends TrajectoryLine {
// Set the color.
this.highlight(true);
// Remove the line from the line group.
this._lineGroup.remove(this._line);
// Remove the line from the data group.
if (this._line !== null) {
this._dataGroup.remove(this._line.object3D);
}
// Calculate length of the segment.
let length = this._end.clone().sub(this._start).length();
// Define the line as a cylinder.
let geometry = new THREE.CylinderGeometry(0.5/scale, 0.5/scale, length, 32);
this._line = new THREE.Mesh(geometry, lineMaterial);
// As the line group is rotated on the x axis, we translate the cylinder on the y axis.
this._line.position.y = 0.5 * length;
this._lineGroup.add(this._line);
// Create a line segment for the line.
let segment = new LineSegment(new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 0, length), 0.5/scale, lineMaterial);
this._line = segment;
// Add the line to the data group.
this._dataGroup.add(this._line.object3D);
// Update the data.
this.updateDataGroup();
......@@ -120,16 +119,15 @@ class EnergyLine extends TrajectoryLine {
* Set line visibility.
*/
setVisibility(visibility) {
this._line.visible = visibility;
//this._dataGroup.visible = visibility;
this._line.object3D.visible = visibility;
this._dataGroup.visible = visibility;
}
/**
* Remove this trajectory properly.
*/
destroy() {
this._line.geometry.dispose();
this._line.material.dispose();
this._line.destroy();
for (let d of this._deltaSphereGroup.children) {
d.geometry.dispose();
......
......@@ -115,7 +115,7 @@ class HKLLine extends TrajectoryLine {
this._trajectoryLine.geometry.dispose();
this._trajectoryLine.material.dispose();
for(let d of this._deltaSphereGroup.children) {
for (let d of this._deltaSphereGroup.children) {
d.geometry.dispose();
d.material.dispose();
}
......
const THREE = require('three');
/**
* 3D shape of line segment implemented with a cylinder.
*/
class LineSegment {
constructor(start, end, radius, material) {
this._material = material;
this._intermediateGroup = new THREE.Group();
this._intermediateGroup.rotateX(Math.PI/2);
this._group = new THREE.Group();
this._group.add(this._intermediateGroup);
// Calculate length of the segment.
let length = end.clone().sub(start).length();
// Define the segment as a cylinder.
this._geometry = new THREE.CylinderGeometry(radius, radius, length, 32);
let cylinder = new THREE.Mesh(this._geometry, this._material);
// As the segment group is rotated on the x axis, we translate the cylinder on the y axis.
cylinder.position.y = 0.5 * length;
this._intermediateGroup.add(cylinder);
this._group.position.copy(start.clone());
this._group.lookAt(end);
}
get object3D() {
return this._group;
}
destroy() {
this._material.dispose();
this._geometry.dispose();
}
}
// Export the constructor.
module.exports = LineSegment;
\ No newline at end of file
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