-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the visualization in the two versions
- Loading branch information
1 parent
a5ddb13
commit 2e9dea6
Showing
7 changed files
with
440 additions
and
121 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
}; | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ] ); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) { | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
Oops, something went wrong.