Commit 53f92bfd authored by legoc's avatar legoc
Browse files

Separated HKL line and Energy line

parent e7c56ebd
const TrajectoryLine = require('./trajectory-line');
const {scale, vector3WorldToView, scalarWorldToView, vector3ViewToWorld} = require('../conversion');
const Color = require('../colors');
const {mapColor} = require('../map-color');
const TrajectorySegmentIterator = require('./trajectory-segment-iterator');
const TrajectoryCircleIterator = require('./trajectory-circle-iterator');
const THREE = require('three');
class EnergyLine extends TrajectoryLine {
constructor(type, parent, start, end, center, steps) {
super(type, parent, start, end, center, steps);
this._data = [];
// Create a group for the data.
this._dataGroup = new THREE.Group();
this._group.add(this._dataGroup);
this.updateLine();
}
setData(data) {
this._data = data;
}
updateDataGroup() {
// Change the group.
let inv = new THREE.Matrix4();
inv.getInverse(this._dataGroup.matrix);
this._dataGroup.applyMatrix(inv);
// Then we can apply the new matrix.
let matrix4 = this.calculateSegmentMatrix(this._start, this._end);
this._dataGroup.applyMatrix(matrix4);
// Remove old spheres.
for (var i = this._dataGroup.children.length - 1; i >= 0; i--) {
this._dataGroup.remove(this._dataGroup.children[i]);
}
// Calculate delta between spheres.
let length = this._end.clone().sub(this._start).length();
let delta = length / this._intervals;
// Place new spheres.
let geometry = new THREE.SphereGeometry(0.5 * delta, 8, 8);
for (let i = 0; i < this._data.length; i++) {
let material = new THREE.MeshBasicMaterial({color: 0xffffff, depthTest: true, transparent: false});
const color = mapColor(this._data[i], 0, 500);
material.color.setRGB(color.r, color.g, color.b);
let sphere = new THREE.Mesh(geometry, material);
sphere.renderOrder = 4;
sphere.position.copy(new THREE.Vector3(0, 0, i * delta));
this._dataGroup.add(sphere);
}
}
/**
* 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);
let geometry = new THREE.SphereGeometry(0.25/scale, 8, 8);
let material = new THREE.MeshBasicMaterial({color: 0xffffff, depthTest: true, transparent: false});
let iterator = null;
if (this._type == 'q') {
iterator = new TrajectorySegmentIterator(this._trajSSphere.position, this._trajESphere.position, this._intervals);
}
else if (this._type == 'ki' || this._type == 'kf') {
iterator = new TrajectoryCircleIterator(this._center, this._trajSSphere.position, this._trajESphere.position, this._intervals);
}
// Remove old spheres.
for (var 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});
this._trajectoryLine = new THREE.Line(new THREE.Geometry(), lineMaterial);
this._trajectoryLine.geometry.verticesNeedUpdate = true;
this._trajectoryLine.renderOrder = 2;
// Set the color.
this.highlight(true);
// Place new spheres.
for(let i = 0; i < this._intervals; i++) {
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);
iterator.next();
}
this._trajectoryLine.geometry.vertices.push(iterator.value());
this._trajectoryLine.geometry.verticesNeedUpdate = true;
// Add the newly created line.
this._group.add(this._trajectoryLine);
// Update the data.
this.updateDataGroup();
}
}
module.exports = EnergyLine;
\ No newline at end of file
const TrajectoryLine = require('./trajectory-line');
const {scale, vector3WorldToView, scalarWorldToView, vector3ViewToWorld} = require('../conversion');
const Color = require('../colors');
const TrajectorySegmentIterator = require('./trajectory-segment-iterator');
const TrajectoryCircleIterator = require('./trajectory-circle-iterator');
const THREE = require('three');
class HKLLine extends TrajectoryLine {
constructor(type, parent, start, end, center, steps) {
super(type, parent, start, end, center, steps);
this.updateLine();
}
/**
* 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);
let geometry = new THREE.SphereGeometry(0.25/scale, 8, 8);
let material = new THREE.MeshBasicMaterial({color: 0xffffff, depthTest: true, transparent: false});
let iterator = null;
if (this._type == 'q') {
iterator = new TrajectorySegmentIterator(this._trajSSphere.position, this._trajESphere.position, this._intervals);
}
else if (this._type == 'ki' || this._type == 'kf') {
iterator = new TrajectoryCircleIterator(this._center, this._trajSSphere.position, this._trajESphere.position, this._intervals);
}
// Remove old spheres.
for (var 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});
this._trajectoryLine = new THREE.Line(new THREE.Geometry(), lineMaterial);
this._trajectoryLine.geometry.verticesNeedUpdate = true;
this._trajectoryLine.renderOrder = 2;
// Set the color.
this.highlight(true);
// Place new spheres.
for(let i = 0; i < this._intervals; i++) {
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);
iterator.next();
}
this._trajectoryLine.geometry.vertices.push(iterator.value());
this._trajectoryLine.geometry.verticesNeedUpdate = true;
// Add the newly created line.
this._group.add(this._trajectoryLine);
}
}
module.exports = HKLLine;
\ No newline at end of file
const {scale, vector3WorldToView, scalarWorldToView, vector3ViewToWorld} = require('../../utils/conversion');
const {scale} = require('../../utils/conversion');
const Color = require('../../utils/colors');
const { mapColor } = require('../../utils/map-color');
const TrajectorySegmentIterator = require('./trajectory-segment-iterator');
const TrajectoryCircleIterator = require('./trajectory-circle-iterator');
const THREE = require('three');
/**
......@@ -18,7 +15,6 @@ class TrajectoryLine {
this._setIntervals();
this._type = type;
this._center = center.clone();
this._data = [];
this._scale = 1.0;
this._deltaSphereGroup = new THREE.Group();
......@@ -28,12 +24,10 @@ class TrajectoryLine {
// Create the trajectory start sphere
this._trajSSphere = new THREE.Mesh(geometry, material);
this._trajSSphere.position.copy(start.clone());
this._trajSSphere.renderOrder = 1;
// Create the trajectory end sphere.
this._trajESphere = new THREE.Mesh(geometry, material);
this._trajESphere.position.copy(end.clone());
this._trajESphere.renderOrder = 1;
// Create the line between start and end points.
......@@ -59,12 +53,6 @@ class TrajectoryLine {
this._group.add(this._trajESphere);
this._group.add(this._trajectoryLine);
this._group.add(this._deltaSphereGroup);
// Create a group for the data.
this._dataGroup = new THREE.Group();
this._group.add(this._dataGroup);
this.updateLine();
}
_setIntervals() {
......@@ -96,20 +84,14 @@ class TrajectoryLine {
set start(value) {
this._start = value;
this._trajSSphere.position.copy(value.clone());
this.updateLine();
}
set end(value) {
this._end = value;
this._trajESphere.position.copy(value.clone());
this.updateLine();
}
setData(data) {
this._data = data;
}
highlight(value) {
// Change the color of the line.
......@@ -131,57 +113,7 @@ class TrajectoryLine {
* Update line position and delta spheres.
*/
updateLine() {
// 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);
let geometry = new THREE.SphereGeometry(0.25/scale, 8, 8);
let material = new THREE.MeshBasicMaterial({color: 0xffffff, depthTest: true, transparent: false});
let iterator = null;
if (this._type == 'q') {
iterator = new TrajectorySegmentIterator(this._trajSSphere.position, this._trajESphere.position, this._intervals);
}
else if (this._type == 'ki' || this._type == 'kf') {
iterator = new TrajectoryCircleIterator(this._center, this._trajSSphere.position, this._trajESphere.position, this._intervals);
}
// Remove old spheres.
for (var 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});
this._trajectoryLine = new THREE.Line(new THREE.Geometry(), lineMaterial);
this._trajectoryLine.geometry.verticesNeedUpdate = true;
this._trajectoryLine.renderOrder = 2;
// Set the color.
this.highlight(true);
// Place new spheres.
for(let i = 0; i < this._intervals; i++) {
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);
iterator.next();
}
this._trajectoryLine.geometry.vertices.push(iterator.value());
this._trajectoryLine.geometry.verticesNeedUpdate = true;
// Add the newly created line.
this._group.add(this._trajectoryLine);
// Update the data.
this.updateDataGroup();
throw new Error('updateLine must be redefined');
}
scale(value) {
......@@ -203,42 +135,6 @@ class TrajectoryLine {
return sMatrix;
}
updateDataGroup() {
// Change the group.
let inv = new THREE.Matrix4();
inv.getInverse(this._dataGroup.matrix);
this._dataGroup.applyMatrix(inv);
// Then we can apply the new matrix.
let matrix4 = this.calculateSegmentMatrix(this._start, this._end);
this._dataGroup.applyMatrix(matrix4);
// Remove old spheres.
for (var i = this._dataGroup.children.length - 1; i >= 0; i--) {
this._dataGroup.remove(this._dataGroup.children[i]);
}
// Calculate delta between spheres.
let length = this._end.clone().sub(this._start).length();
let delta = length / this._intervals;
// Place new spheres.
let geometry = new THREE.SphereGeometry(0.5 * delta, 8, 8);
for (let i = 0; i < this._data.length; i++) {
let material = new THREE.MeshBasicMaterial({color: 0xffffff, depthTest: true, transparent: false});
const color = mapColor(this._data[i], 0, 500);
material.color.setRGB(color.r, color.g, color.b);
let sphere = new THREE.Mesh(geometry, material);
sphere.renderOrder = 4;
sphere.position.copy(new THREE.Vector3(0, 0, i * delta));
this._dataGroup.add(sphere);
}
}
/**
* Set line visibility.
*/
......
const View3D = require('./view3d');
const TrajectoryLine = require('../../utils/shapes/trajectory-line');
const HKLLine = require('../../utils/shapes/hkl-line');
const EnergyLine = require('../../utils/shapes/energy-line');
const { calculateKiKf } = require('../../utils/calculations');
const THREE = require('three');
const { displayMode } = require('../../main/context');
......@@ -36,10 +37,10 @@ class TrajectoryView3D extends View3D {
this._energyScaleGroup.add(this._energyTranslationGroup);
// Create fake trajectory lines to visualize.
this._hklLine = new TrajectoryLine(this._type, this._hklViewGroup, new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), 11);
this._hklLine = new HKLLine(this._type, this._hklViewGroup, new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), 11);
this._hklLine.setVisibility(false);
this._energyLine = new TrajectoryLine(this._type, this._energyTranslationGroup, new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), 11);
this._energyLine = new EnergyLine(this._type, this._energyTranslationGroup, new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), 11);
this._energyLine.setVisibility(false);
this._energyLine.type = 'q';
......@@ -372,7 +373,7 @@ class TrajectoryView3D extends View3D {
// Define the energy line only if the plane hasn't changed.
if (uMatrix.equals(matrix.uMatrix)) {
hklLine = new TrajectoryLine(trajectory.type, this._hklViewGroup,
hklLine = new HKLLine(trajectory.type, this._hklViewGroup,
start,
end,
center,
......@@ -381,7 +382,7 @@ class TrajectoryView3D extends View3D {
let energyStart = this.loadEnergyPoint(trajectory.start, bMatrix, uMatrix);
let energyEnd = this.loadEnergyPoint(trajectory.end, bMatrix, uMatrix);
energyLine = new TrajectoryLine('q', this._energyTranslationGroup,
energyLine = new EnergyLine('q', this._energyTranslationGroup,
energyStart,
energyEnd,
energyStart,
......@@ -450,7 +451,7 @@ class TrajectoryView3D extends View3D {
// Define the energy lines only if the plane hasn't changed.
if (uMatrix.equals(matrix.uMatrix)) {
hklLine = new TrajectoryLine(trajectory.type, this._hklViewGroup,
hklLine = new HKLLine(trajectory.type, this._hklViewGroup,
start,
end,
center,
......@@ -459,7 +460,7 @@ class TrajectoryView3D extends View3D {
let energyStart = this.loadEnergyPoint(trajectory.start, bMatrix, uMatrix);
let energyEnd = this.loadEnergyPoint(trajectory.end, bMatrix, uMatrix);
energyLine = new TrajectoryLine('q', this._energyTranslationGroup,
energyLine = new EnergyLine('q', this._energyTranslationGroup,
energyStart,
energyEnd,
energyStart,
......@@ -521,7 +522,7 @@ class TrajectoryView3D extends View3D {
// Define the energy line only if the plane hasn't changed.
if (uMatrix.equals(matrix.uMatrix)) {
hklLine = new TrajectoryLine(trajectory.type, this._hklViewGroup,
hklLine = new HKLLine(trajectory.type, this._hklViewGroup,
start,
end,
center,
......@@ -530,7 +531,7 @@ class TrajectoryView3D extends View3D {
let energyStart = this.loadEnergyPoint(trajectory.start, bMatrix, uMatrix);
let energyEnd = this.loadEnergyPoint(trajectory.end, bMatrix, uMatrix);
energyLine = new TrajectoryLine('q', this._energyTranslationGroup,
energyLine = new EnergyLineLine('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