Skip to content

Commit

Permalink
Adding the visualization in the two versions
Browse files Browse the repository at this point in the history
  • Loading branch information
ferrari212 committed Jan 17, 2022
1 parent a5ddb13 commit 2e9dea6
Show file tree
Hide file tree
Showing 7 changed files with 440 additions and 121 deletions.
205 changes: 121 additions & 84 deletions source/classes/WaveMotion.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions source/jsm/fileIO/loadShip.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Vessel.loadShip(filePath, function(ship) {
});
*/
import Ship from "../ship/Ship.js";

export function loadShip( url, callback ) {

Expand Down
218 changes: 218 additions & 0 deletions source/jsm/ship/StateModule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
export default class StateModule {

constructor( ship, states ) {

this.ship = ship;
this.states = states;

}

returnOutput() {

let resObj = {};
for ( let i = 0; i < this.output.length; i ++ ) {

let output = this[ this.output[ i ] ];
if ( typeof output === "number" ) {

resObj[ this.output[ i ] ] = output;

} else if ( typeof output === "object" ) {

Object.assign( resObj, output );

}

}

return resObj;

}

// write getter output to shipState
writeOutput() {

let stateName = this.constructor.name;
if ( this.states.discrete[ stateName ] === undefined ) {

this.states.discrete[ stateName ] = {
state: {},
thisStateVer: 0
};

}

for ( let i = 0; i < this.output.length; i ++ ) {

let output = this[ this.output[ i ] ];
if ( typeof output === "number" ) {

this.states.discrete[ stateName ].state[ this.output[ i ] ] = output;

} else if ( typeof output === "object" ) {

Object.assign( this.states.discrete[ stateName ].state, output );

}

}

this.states.discrete[ stateName ].thisStateVer ++;

}

setDraft() {

let draft = this.ship.calculateDraft( this.states );
if ( this.states.discrete.FloatingCondition === undefined ) {

this.states.discrete.FloatingCondition = {
state: {},
thisStateVer: 0
};

}

Object.assign( this.states.discrete.FloatingCondition.state, this.ship.structure.hull.calculateAttributesAtDraft( draft ) );
Object.assign( this.states.discrete.FloatingCondition.state, this.ship.calculateStability( this.states ) );
this.states.discrete.FloatingCondition.thisStateVer ++;


}

// write argument speed to vessel state. If undefined, use vessel's design speed
setSpeed( speed ) {

if ( this.states.discrete.Speed === undefined ) {

this.states.discrete.Speed = {
state: {},
thisStateVer: 0
};

}

if ( typeof speed === "undefined" && typeof this.ship.designState.calculationParameters.speed !== "undefined" ) {

speed = this.ship.designState.calculationParameters.speed;

}

this.states.discrete.Speed.state.speed = speed; // knots
this.states.discrete.Speed.thisStateVer ++;

}

// write argument heading angle to vessel state. if undefined, use 0 degrees
// 0 degrees corresponds to vessel pointing to north. clockwise orientation.
setHeading( angle ) {

if ( this.states.discrete.Heading === undefined ) {

this.states.discrete.Heading = {
state: {},
thisStateVer: 0
};

}

if ( typeof angle === "undefined" ) {

angle = 0;

}

this.states.discrete.Heading.state.heading = angle;
this.states.discrete.Heading.thisStateVer ++;

}

// cache memoization pattern adapted from http://b-studios.de/blog/2013/11/18/lazy-attributes-in-ecmascript-5/
// in the future, expand version comparison also to parameters stored inside each constructor
memoized( init, cacheName ) {

return {
enumerable: true,
configurable: false,
get: function cache() {

// if state module uses a wave creation object
let hasWave;
if ( this.wavCre !== undefined ) {

hasWave = true;

} else { // if it has only a ship module

hasWave = false;

}

if ( this.cache[ cacheName ] === undefined ) {

this.cache[ cacheName ] = {};
for ( let i = 0; i < this.cacheDependence.length; i ++ ) {

let dependenceName = this.cacheDependence[ i ];
this.cache[ cacheName ][ dependenceName + "Version" ] = 0;

}

if ( hasWave ) {

this.cache[ cacheName ].waveStateVersion = 0;

}

}

let needsUpdate = false;
for ( let i = 0; i < this.cacheDependence.length; i ++ ) {

let dependenceName = this.cacheDependence[ i ];
if ( this.cache[ cacheName ][ dependenceName + "Version" ] !== this.states.discrete[ dependenceName ].thisStateVer ) {

needsUpdate = true;
break;

}

}

if ( hasWave && needsUpdate === false ) {

if ( this.cache[ cacheName ].waveStateVersion !== this.wavCre.version ) {

needsUpdate = true;

}

}

if ( needsUpdate ) {

this.cache[ cacheName ].value = init.call( this );
for ( let i = 0; i < this.cacheDependence.length; i ++ ) {

let dependenceName = this.cacheDependence[ i ];
this.cache[ cacheName ][ dependenceName + "Version" ] = this.states.discrete[ dependenceName ].thisStateVer;

}

if ( hasWave ) {

this.cache[ cacheName ].waveStateVersion = this.wavCre.version;

}

}

return this.cache[ cacheName ].value;

}
};

}


}
50 changes: 50 additions & 0 deletions source/jsm/ship/WaveCreator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export default class WaveCreator {

constructor( defList, waveDuration = 3600 ) {

this.waveDef = {}; // store wave definition
this.version = 0; // version counter for memorisation pattern
this.defList = defList || [ // list wave definitions
[ 0.6, 2.25, 180 ],
[ 1.1, 1.75, 150 ]
];
this.waveDuration = waveDuration;

}

// set a regular wave definition
setWaveDef( freq, amp, head ) {

var newWaveDef;
newWaveDef = {
waveFreq: freq, // angular frequency
waveAmplitude: amp,
heading: head // 0 to 360. 180 corresponds to head seas
};
if ( JSON.stringify( this.waveDef ) !== JSON.stringify( newWaveDef ) ) {

this.waveDef = newWaveDef;
this.version ++;

}

}

setRandom() { // set a wave definition randomly

var rand = this.defList[ Math.floor( Math.random() * this.defList.length ) ];
this.setWaveDef( rand[ 0 ], rand[ 1 ], rand[ 2 ] );

}

setTime( time ) {

// change wave definition along time
var noSpans = time / this.waveDuration;
var coord = Math.floor( noSpans % this.defList.length );

this.setWaveDef( this.defList[ coord ][ 0 ], this.defList[ coord ][ 1 ], this.defList[ coord ][ 2 ] );

}

}
7 changes: 7 additions & 0 deletions source/jsm/ship/WaveMotion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default class WaveMotion extends StateModule {

constructor( ship, states, wavCre, position = 0, critDampPercentage = 20, g = 9.81, rho = 1025 ) {

}

}
16 changes: 12 additions & 4 deletions source/jsm/vessel.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
class vessel {
import Ship from "./ship/Ship.js";

constructor() {
// export default class Vessel {

}
// constructor() {

}
// this.Ship = Ship;

// }

// }

export default Vessel = {
Ship: Ship,
};
Loading

0 comments on commit 2e9dea6

Please sign in to comment.