mirror of
https://github.com/Krafpy/KSP-MGA-Planner.git
synced 2025-12-12 15:49:59 -08:00
Commented trajectory.ts
This commit is contained in:
3
dist/main/solvers/trajectory.js
vendored
3
dist/main/solvers/trajectory.js
vendored
@@ -88,8 +88,7 @@ export class Trajectory {
|
|||||||
const { x, y, z } = maneuvre.position;
|
const { x, y, z } = maneuvre.position;
|
||||||
const pos = new THREE.Vector3(x, y, z);
|
const pos = new THREE.Vector3(x, y, z);
|
||||||
addSprite(i, sprite, pos);
|
addSprite(i, sprite, pos);
|
||||||
const { type } = maneuvre.context;
|
if (maneuvre.context.type == "ejection") {
|
||||||
if (type == "ejection") {
|
|
||||||
const sprite = createSprite(escapeSprite, 0xFFFFFF, false, spritesSize);
|
const sprite = createSprite(escapeSprite, 0xFFFFFF, false, spritesSize);
|
||||||
const pos = orbit.positionFromTrueAnomaly(step.drawAngles.end);
|
const pos = orbit.positionFromTrueAnomaly(step.drawAngles.end);
|
||||||
addSprite(i, sprite, pos);
|
addSprite(i, sprite, pos);
|
||||||
|
|||||||
@@ -29,6 +29,9 @@ export class Trajectory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Preloads all the sprite materials in the `sprites` map.
|
||||||
|
*/
|
||||||
public static preloadSpriteMaterials(){
|
public static preloadSpriteMaterials(){
|
||||||
const textureLoader = new THREE.TextureLoader();
|
const textureLoader = new THREE.TextureLoader();
|
||||||
const loaded = (name: string) => {
|
const loaded = (name: string) => {
|
||||||
@@ -45,6 +48,10 @@ export class Trajectory {
|
|||||||
textureLoader.load("sprites/maneuver.png", loaded("maneuver"));
|
textureLoader.load("sprites/maneuver.png", loaded("maneuver"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draws the tajectory to the scene.
|
||||||
|
* @param resolution The resolution of the canvas
|
||||||
|
*/
|
||||||
public draw(resolution: {width: number, height: number}){
|
public draw(resolution: {width: number, height: number}){
|
||||||
const numSteps = this.steps.length;
|
const numSteps = this.steps.length;
|
||||||
this._displayedSteps = Array(numSteps).fill(true);
|
this._displayedSteps = Array(numSteps).fill(true);
|
||||||
@@ -55,6 +62,10 @@ export class Trajectory {
|
|||||||
this._calculateFlybyDetails();
|
this._calculateFlybyDetails();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Computes all the leg arcs orbits, adds them to the scene and stores them.
|
||||||
|
* @param resolution The resolution of the canvas
|
||||||
|
*/
|
||||||
private _createTrajectoryArcs(resolution: {width: number, height: number}){
|
private _createTrajectoryArcs(resolution: {width: number, height: number}){
|
||||||
this._orbitObjects = [];
|
this._orbitObjects = [];
|
||||||
|
|
||||||
@@ -80,7 +91,12 @@ export class Trajectory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds the various sprites to the scene and the collections for managment.
|
||||||
|
*/
|
||||||
private _createManeuvreSprites(){
|
private _createManeuvreSprites(){
|
||||||
|
// Empty the sprite collection, for each step there is a list
|
||||||
|
// of sprites (initally empty).
|
||||||
this._spriteObjects = [];
|
this._spriteObjects = [];
|
||||||
for(let i = 0; i < this.steps.length; i++){
|
for(let i = 0; i < this.steps.length; i++){
|
||||||
this._spriteObjects.push([]);
|
this._spriteObjects.push([]);
|
||||||
@@ -94,6 +110,7 @@ export class Trajectory {
|
|||||||
const maneuverSprite = <THREE.SpriteMaterial>Trajectory.sprites.get("maneuver");
|
const maneuverSprite = <THREE.SpriteMaterial>Trajectory.sprites.get("maneuver");
|
||||||
|
|
||||||
const addSprite = (i: number, sprite: THREE.Sprite, pos: THREE.Vector3) => {
|
const addSprite = (i: number, sprite: THREE.Sprite, pos: THREE.Vector3) => {
|
||||||
|
// adds a sprite to the sprite collection and the corresponding group
|
||||||
sprite.position.set(pos.x, pos.y, pos.z);
|
sprite.position.set(pos.x, pos.y, pos.z);
|
||||||
sprite.position.multiplyScalar(scale);
|
sprite.position.multiplyScalar(scale);
|
||||||
const group = this.system.objectsOfBody(this.steps[i].attractorId);
|
const group = this.system.objectsOfBody(this.steps[i].attractorId);
|
||||||
@@ -107,18 +124,20 @@ export class Trajectory {
|
|||||||
const {maneuvre, flyby} = step;
|
const {maneuvre, flyby} = step;
|
||||||
|
|
||||||
if(maneuvre){
|
if(maneuvre){
|
||||||
|
// if there is a maneuver, add the maneuver sprite
|
||||||
const sprite = createSprite(maneuverSprite, 0xFFFFFF, false, spritesSize);
|
const sprite = createSprite(maneuverSprite, 0xFFFFFF, false, spritesSize);
|
||||||
const {x, y, z} = maneuvre.position;
|
const {x, y, z} = maneuvre.position;
|
||||||
const pos = new THREE.Vector3(x, y, z);
|
const pos = new THREE.Vector3(x, y, z);
|
||||||
addSprite(i, sprite, pos);
|
addSprite(i, sprite, pos);
|
||||||
const {type} = maneuvre.context;
|
if(maneuvre.context.type == "ejection"){
|
||||||
if(type == "ejection"){
|
// if the maneuver is an ejection, add the ejection maneuver sprite
|
||||||
const sprite = createSprite(escapeSprite, 0xFFFFFF, false, spritesSize);
|
const sprite = createSprite(escapeSprite, 0xFFFFFF, false, spritesSize);
|
||||||
const pos = orbit.positionFromTrueAnomaly(step.drawAngles.end);
|
const pos = orbit.positionFromTrueAnomaly(step.drawAngles.end);
|
||||||
addSprite(i, sprite, pos);
|
addSprite(i, sprite, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if(flyby){
|
} else if(flyby){
|
||||||
|
// if the maneuver is a flyby, add the encounter and escape sprites
|
||||||
const sprite1 = createSprite(encounterSprite, 0xFFFFFF, false, spritesSize);
|
const sprite1 = createSprite(encounterSprite, 0xFFFFFF, false, spritesSize);
|
||||||
const sprite2 = createSprite(escapeSprite, 0xFFFFFF, false, spritesSize);
|
const sprite2 = createSprite(escapeSprite, 0xFFFFFF, false, spritesSize);
|
||||||
const pos1 = orbit.positionFromTrueAnomaly(step.drawAngles.begin);
|
const pos1 = orbit.positionFromTrueAnomaly(step.drawAngles.begin);
|
||||||
@@ -127,6 +146,7 @@ export class Trajectory {
|
|||||||
addSprite(i, sprite2, pos2);
|
addSprite(i, sprite2, pos2);
|
||||||
|
|
||||||
} else if(i == this.steps.length - 2){
|
} else if(i == this.steps.length - 2){
|
||||||
|
// if it's insertion orbit, add the circularization burn maneuver sprite
|
||||||
const sprite = createSprite(encounterSprite, 0xFFFFFF, false, spritesSize);
|
const sprite = createSprite(encounterSprite, 0xFFFFFF, false, spritesSize);
|
||||||
const pos = orbit.positionFromTrueAnomaly(step.drawAngles.begin);
|
const pos = orbit.positionFromTrueAnomaly(step.drawAngles.begin);
|
||||||
addSprite(i, sprite, pos);
|
addSprite(i, sprite, pos);
|
||||||
@@ -134,6 +154,9 @@ export class Trajectory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const updateSpritesDisplay = (camController: CameraController) => {
|
const updateSpritesDisplay = (camController: CameraController) => {
|
||||||
|
// display maneuver sprites only if we are close enough to the body
|
||||||
|
// that contains them, and if the steps to which belong these maneuvers is
|
||||||
|
// actually displayed
|
||||||
const camPos = camController.camera.position;
|
const camPos = camController.camera.position;
|
||||||
const {scale} = this.config.rendering;
|
const {scale} = this.config.rendering;
|
||||||
const {spriteDispSOIMul} = this.config.solarSystem;
|
const {spriteDispSOIMul} = this.config.solarSystem;
|
||||||
@@ -156,10 +179,15 @@ export class Trajectory {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Add the display update as a callback of the system's update
|
||||||
|
// function which is called once each frame.
|
||||||
const id = this.system.addCustomUpdate(updateSpritesDisplay);
|
const id = this.system.addCustomUpdate(updateSpritesDisplay);
|
||||||
this._spritesUpdateFunId = id;
|
this._spritesUpdateFunId = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Computes the maneuver details from the data given in the steps.
|
||||||
|
*/
|
||||||
private _calculateManeuvresDetails(){
|
private _calculateManeuvresDetails(){
|
||||||
const departureDate = this.steps[0].dateOfStart;
|
const departureDate = this.steps[0].dateOfStart;
|
||||||
|
|
||||||
@@ -196,6 +224,9 @@ export class Trajectory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Computes the flyby details from the data given in the steps.
|
||||||
|
*/
|
||||||
private _calculateFlybyDetails(){
|
private _calculateFlybyDetails(){
|
||||||
const departureDate = this.steps[0].dateOfStart;
|
const departureDate = this.steps[0].dateOfStart;
|
||||||
for(const {flyby} of this.steps){
|
for(const {flyby} of this.steps){
|
||||||
@@ -216,10 +247,18 @@ export class Trajectory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fills the various HTML elements of the result panel with the data and events needed.
|
||||||
|
* @param resultItems The list of HTML items attached to the result panel
|
||||||
|
* @param systemTime The system time manager
|
||||||
|
* @param controls The camera controller
|
||||||
|
*/
|
||||||
public fillResultControls(resultItems: ResultPannelItems, systemTime: TimeSelector, controls: CameraController){
|
public fillResultControls(resultItems: ResultPannelItems, systemTime: TimeSelector, controls: CameraController){
|
||||||
const depDate = new KSPTime(this.steps[0].dateOfStart, this.config.time);
|
const depDate = new KSPTime(this.steps[0].dateOfStart, this.config.time);
|
||||||
|
|
||||||
|
// total delta-V
|
||||||
resultItems.totalDVSpan.innerHTML = this._totalDeltaV.toFixed(1);
|
resultItems.totalDVSpan.innerHTML = this._totalDeltaV.toFixed(1);
|
||||||
|
// departure date
|
||||||
resultItems.depDateSpan.innerHTML = depDate.stringYDHMS("hms", "ut");
|
resultItems.depDateSpan.innerHTML = depDate.stringYDHMS("hms", "ut");
|
||||||
|
|
||||||
const onDateClick = (date: number) => () => {
|
const onDateClick = (date: number) => () => {
|
||||||
@@ -228,17 +267,19 @@ export class Trajectory {
|
|||||||
systemTime.time.dateSeconds = date;
|
systemTime.time.dateSeconds = date;
|
||||||
systemTime.update();
|
systemTime.update();
|
||||||
};
|
};
|
||||||
|
// set the system time to the departure date time when the span is clicked
|
||||||
resultItems.depDateSpan.onclick = onDateClick(depDate.dateSeconds);
|
resultItems.depDateSpan.onclick = onDateClick(depDate.dateSeconds);
|
||||||
|
|
||||||
|
// configure the step slider
|
||||||
const {stepSlider} = resultItems;
|
const {stepSlider} = resultItems;
|
||||||
stepSlider.setMinMax(0, this.steps.length - 1);
|
stepSlider.setMinMax(0, this.steps.length - 1);
|
||||||
stepSlider.input((index: number) => this._displayStepsUpTo(index));
|
stepSlider.input((index: number) => this._displayStepsUpTo(index));
|
||||||
stepSlider.value = this.steps.length - 1;
|
stepSlider.value = this.steps.length - 1;
|
||||||
|
|
||||||
|
// information about each selector options
|
||||||
const selectorOptions: DetailsSelectorOption[] = [];
|
const selectorOptions: DetailsSelectorOption[] = [];
|
||||||
|
|
||||||
|
// keep index for the numbering of each maneuver and flybys
|
||||||
let maneuvreIdx = 0, flybyIdx = 0;
|
let maneuvreIdx = 0, flybyIdx = 0;
|
||||||
let optionNumber = 0;
|
let optionNumber = 0;
|
||||||
|
|
||||||
@@ -249,6 +290,7 @@ export class Trajectory {
|
|||||||
const step = this.steps[details.stepIndex];
|
const step = this.steps[details.stepIndex];
|
||||||
const context = (<ManeuvreInfo>step.maneuvre).context;
|
const context = (<ManeuvreInfo>step.maneuvre).context;
|
||||||
|
|
||||||
|
// name of the options to display in the selector, if it's a maneuver
|
||||||
let optionName: string;
|
let optionName: string;
|
||||||
if(context.type == "ejection") {
|
if(context.type == "ejection") {
|
||||||
const startBodyName = this.system.bodyFromId(step.attractorId).name;
|
const startBodyName = this.system.bodyFromId(step.attractorId).name;
|
||||||
@@ -271,6 +313,7 @@ export class Trajectory {
|
|||||||
selectorOptions.push(option);
|
selectorOptions.push(option);
|
||||||
|
|
||||||
} else if(flyby){
|
} else if(flyby){
|
||||||
|
// details the options in the selector, if it's a flyby
|
||||||
const details = this._flybys[flybyIdx];
|
const details = this._flybys[flybyIdx];
|
||||||
const bodyName = this.system.bodyFromId(details.bodyId).name;
|
const bodyName = this.system.bodyFromId(details.bodyId).name;
|
||||||
const optionName = `${++optionNumber}: ${bodyName} flyby`;
|
const optionName = `${++optionNumber}: ${bodyName} flyby`;
|
||||||
@@ -285,11 +328,14 @@ export class Trajectory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// retrieve the list of option names only from their details
|
||||||
const optionNames = selectorOptions.map(opt => opt.text);
|
const optionNames = selectorOptions.map(opt => opt.text);
|
||||||
|
|
||||||
const {detailsSelector} = resultItems;
|
const {detailsSelector} = resultItems;
|
||||||
detailsSelector.fill(optionNames);
|
detailsSelector.fill(optionNames);
|
||||||
detailsSelector.change((_: string, index: number) => {
|
detailsSelector.change((_: string, index: number) => {
|
||||||
|
// Fill the result panel with the correct data depending on what option
|
||||||
|
// is selected in the selector
|
||||||
const option = selectorOptions[index];
|
const option = selectorOptions[index];
|
||||||
|
|
||||||
if(option.type == "maneuver"){
|
if(option.type == "maneuver"){
|
||||||
@@ -334,6 +380,10 @@ export class Trajectory {
|
|||||||
}*/
|
}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the visibility of the steps up to the provided indedx.
|
||||||
|
* @param index The index of the last step to display
|
||||||
|
*/
|
||||||
private _displayStepsUpTo(index: number){
|
private _displayStepsUpTo(index: number){
|
||||||
for(let i = 0; i < this.steps.length; i++){
|
for(let i = 0; i < this.steps.length; i++){
|
||||||
const orbitLine = this._orbitObjects[i];
|
const orbitLine = this._orbitObjects[i];
|
||||||
@@ -347,6 +397,9 @@ export class Trajectory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Computes and returns the total delta-V of the trajectory
|
||||||
|
*/
|
||||||
private get _totalDeltaV(){
|
private get _totalDeltaV(){
|
||||||
let total = 0;
|
let total = 0;
|
||||||
for(const details of this._maneuvres){
|
for(const details of this._maneuvres){
|
||||||
@@ -358,6 +411,9 @@ export class Trajectory {
|
|||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes all objects/events relative to this trajectory from the scene.
|
||||||
|
*/
|
||||||
public remove() {
|
public remove() {
|
||||||
for(const object of this._orbitObjects) {
|
for(const object of this._orbitObjects) {
|
||||||
if(object.parent) object.parent.remove(object);
|
if(object.parent) object.parent.remove(object);
|
||||||
|
|||||||
Reference in New Issue
Block a user