@antti5/ship-tool-common 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/bin/curlTime.sh +11 -0
- package/bin/jpretty +27 -0
- package/bin/repot +69 -0
- package/bin/tag +24 -0
- package/eslint.config.js +44 -0
- package/lib/Semaphore.js +120 -0
- package/lib/Util.js +789 -0
- package/lib/UtilNode.js +82 -0
- package/lib/trajectory/BulletSim.js +52 -0
- package/lib/trajectory/Trajectory.js +52 -0
- package/lib/trajectory/TrajectoryPoint.js +18 -0
- package/lib/trajectory/Vector3.js +123 -0
- package/package.json +28 -0
package/lib/UtilNode.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import zlib from 'zlib';
|
|
4
|
+
import dotenv from 'dotenv';
|
|
5
|
+
import findConfig from 'find-config';
|
|
6
|
+
import Util from './Util.js';
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
/* Additional utility functions that only work in Node.js */
|
|
10
|
+
|
|
11
|
+
class UtilNode extends Util {
|
|
12
|
+
|
|
13
|
+
/* Return environment variable. The given .env file is loaded with find-config
|
|
14
|
+
so that it is also loaded from a parent directory.
|
|
15
|
+
|
|
16
|
+
If the environment variable cannot be found the process exits with exit value 1. */
|
|
17
|
+
|
|
18
|
+
static #envFileLoaded = {};
|
|
19
|
+
|
|
20
|
+
static env(name, envFile = '.env') {
|
|
21
|
+
if (envFile in UtilNode.#envFileLoaded === false) {
|
|
22
|
+
dotenv.config({
|
|
23
|
+
path: findConfig(envFile),
|
|
24
|
+
quiet: true
|
|
25
|
+
});
|
|
26
|
+
UtilNode.#envFileLoaded[envFile] = true;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const value = process.env[name];
|
|
30
|
+
if (value)
|
|
31
|
+
return value;
|
|
32
|
+
else {
|
|
33
|
+
console.warn(`Environment variable ${name} not defined`);
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
/* Return a list of files under the given directory,
|
|
40
|
+
recursively including subdirectories. */
|
|
41
|
+
|
|
42
|
+
static walkSync(dir, filelist = []) {
|
|
43
|
+
for (const file of fs.readdirSync(dir)) {
|
|
44
|
+
filelist = fs.statSync(path.join(dir, file)).isDirectory() ?
|
|
45
|
+
UtilNode.walkSync(path.join(dir, file), filelist) :
|
|
46
|
+
filelist.concat(path.join(dir, file));
|
|
47
|
+
}
|
|
48
|
+
return filelist;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
/* Read JSON file and return it as an Object.
|
|
53
|
+
|
|
54
|
+
If the filename ends with ".gz" or ".br", it is decompressed first. If the filename
|
|
55
|
+
contains ".jsonc", then comments are removed before the JSON is parsed. */
|
|
56
|
+
|
|
57
|
+
static jsonRead(filename) {
|
|
58
|
+
let input = fs.readFileSync(filename);
|
|
59
|
+
if (filename.endsWith('.gz'))
|
|
60
|
+
input = zlib.gunzipSync(input);
|
|
61
|
+
if (filename.endsWith('.br'))
|
|
62
|
+
input = zlib.brotliDecompressSync(input);
|
|
63
|
+
if (filename.includes('.jsonc'))
|
|
64
|
+
input = input.toString().replaceAll(/\/\*.*?\*\//gs, '').replaceAll(/\/\/.*$/gm, '');
|
|
65
|
+
return JSON.parse(input);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
/* Write object to file as JSON. If the filename ends with ".gz" or ".br", then the
|
|
70
|
+
file content is compressed before being written. */
|
|
71
|
+
|
|
72
|
+
static jsonWrite(filename, object, replacer = null, space = 2) {
|
|
73
|
+
let output = JSON.stringify(object, replacer, space);
|
|
74
|
+
if (filename.endsWith('.gz'))
|
|
75
|
+
output = zlib.gzipSync(output);
|
|
76
|
+
if (filename.endsWith('.br'))
|
|
77
|
+
output = zlib.brotliCompressSync(output);
|
|
78
|
+
fs.writeFileSync(filename, output);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export default UtilNode;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import Vector3 from './Vector3.js';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
const G = 9.8;
|
|
5
|
+
const G_VEC = new Vector3(0, -G, 0);
|
|
6
|
+
const GAS_CST_R = 8.31447; // N.m / (mol.K)
|
|
7
|
+
const AIR_MOLAR_MASS = 0.0289644; // kg / mol
|
|
8
|
+
const SEALEVEL_TEMPERATURE = 288.15; // K
|
|
9
|
+
const STATIC_PRESSURE_LVL0 = 101325.0; // Pa
|
|
10
|
+
const TEMPERATURE_LAPSE_RATE_LVL0 = -0.0065; // K / m
|
|
11
|
+
const PRESSURE_CST_H0 = G * AIR_MOLAR_MASS / (GAS_CST_R * TEMPERATURE_LAPSE_RATE_LVL0);
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class BulletSim {
|
|
15
|
+
constructor(mass, diam, dragCoeff, vel, pos, dir) {
|
|
16
|
+
this.mass = mass;
|
|
17
|
+
this.dragCoeff = dragCoeff;
|
|
18
|
+
this.pos = pos;
|
|
19
|
+
this.vel = dir.normalized().mul(vel);
|
|
20
|
+
this.area = 0.25 * diam * diam * Math.PI;
|
|
21
|
+
this.time = 0.0;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
altitudeTemperature() {
|
|
25
|
+
return SEALEVEL_TEMPERATURE + this.pos.y * TEMPERATURE_LAPSE_RATE_LVL0;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
altitudePressure() {
|
|
29
|
+
return STATIC_PRESSURE_LVL0 * Math.pow(SEALEVEL_TEMPERATURE / this.altitudeTemperature(), PRESSURE_CST_H0);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
step() {
|
|
33
|
+
const temperature = this.altitudeTemperature();
|
|
34
|
+
const pressure = this.altitudePressure();
|
|
35
|
+
const airDensity = AIR_MOLAR_MASS * pressure / (GAS_CST_R * temperature);
|
|
36
|
+
const drag = this.area * 0.5 * this.dragCoeff * airDensity * this.vel.sqnorm();
|
|
37
|
+
const acceleration = this.vel.normalized().mul(-drag / this.mass).add(G_VEC);
|
|
38
|
+
|
|
39
|
+
/* Start with a time step of 640 ms, and when shell is about to land start halving
|
|
40
|
+
the time step down to 10 ms. This ends the trajectory very close to the surface. */
|
|
41
|
+
let dt = 0.64;
|
|
42
|
+
while (this.pos.y + this.vel.y * dt * 2 < 0 && dt > 0.01)
|
|
43
|
+
dt = Math.max(dt / 2, 0.01);
|
|
44
|
+
|
|
45
|
+
this.pos.iadd(this.vel.mul(dt));
|
|
46
|
+
this.vel.iadd(acceleration.mul(dt));
|
|
47
|
+
this.time += dt;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
export default BulletSim;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import Vector3 from './Vector3.js';
|
|
2
|
+
import BulletSim from './BulletSim.js';
|
|
3
|
+
import TrajectoryPoint from './TrajectoryPoint.js';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Trajectory {
|
|
7
|
+
|
|
8
|
+
constructor(
|
|
9
|
+
{
|
|
10
|
+
bulletMass: mass,
|
|
11
|
+
bulletDiametr: diameter,
|
|
12
|
+
bulletAirDrag: dragCoeff,
|
|
13
|
+
bulletSpeed: initialVelocity
|
|
14
|
+
},
|
|
15
|
+
elevation,
|
|
16
|
+
{ flatten = false } = {}
|
|
17
|
+
) {
|
|
18
|
+
|
|
19
|
+
this._elevation = elevation;
|
|
20
|
+
|
|
21
|
+
const pos = new Vector3(0.0, 10.0, 0.0);
|
|
22
|
+
const dir = new Vector3(1.0, Math.tan(elevation), 0.0);
|
|
23
|
+
const vel = new Vector3(Math.cos(elevation) * initialVelocity, Math.sin(elevation) * initialVelocity, 0.0);
|
|
24
|
+
|
|
25
|
+
this.points = [
|
|
26
|
+
new TrajectoryPoint(pos, vel, 0, flatten)
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
const bullet = new BulletSim(mass, diameter, dragCoeff, initialVelocity, pos, dir);
|
|
30
|
+
while (bullet.pos.y > 0) {
|
|
31
|
+
bullet.step();
|
|
32
|
+
this.points.push(new TrajectoryPoint(pos, bullet.vel, bullet.time, flatten));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
getPoints() {
|
|
37
|
+
return this.points;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
getParameters() {
|
|
41
|
+
const lastPoint = this.getPoints().at(-1);
|
|
42
|
+
return {
|
|
43
|
+
elevation: this._elevation,
|
|
44
|
+
range: lastPoint.pos.x,
|
|
45
|
+
flightTime: lastPoint.time / 2.75,
|
|
46
|
+
impactAngle: Math.abs(Math.atan(lastPoint.vel.y / lastPoint.vel.x)),
|
|
47
|
+
impactVelocity: lastPoint.vel.len()
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default Trajectory;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import Vector3 from './Vector3.js';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
const BALLISTIC_FLATTENING = 1.0;
|
|
5
|
+
|
|
6
|
+
class TrajectoryPoint {
|
|
7
|
+
constructor(pos, vel, time, flatten = false) {
|
|
8
|
+
this.pos = Vector3.copy(pos);
|
|
9
|
+
this.vel = Vector3.copy(vel);
|
|
10
|
+
this.time = time;
|
|
11
|
+
|
|
12
|
+
if (flatten)
|
|
13
|
+
if (BALLISTIC_FLATTENING !== 0.0 && this.pos.y > 0)
|
|
14
|
+
this.pos.y = Math.log(BALLISTIC_FLATTENING * this.pos.y + 1.0) / BALLISTIC_FLATTENING;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default TrajectoryPoint;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
class Vector3 {
|
|
2
|
+
constructor(x = 0.0, y = 0.0, z = 0.0) {
|
|
3
|
+
this.x = x;
|
|
4
|
+
this.y = y;
|
|
5
|
+
this.z = z;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
static copy(other) {
|
|
9
|
+
return new Vector3(
|
|
10
|
+
other.x,
|
|
11
|
+
other.y,
|
|
12
|
+
other.z
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
iadd(other) {
|
|
17
|
+
this.x += other.x;
|
|
18
|
+
this.y += other.y;
|
|
19
|
+
this.z += other.z;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
add(other) {
|
|
23
|
+
return new Vector3(
|
|
24
|
+
this.x + other.x,
|
|
25
|
+
this.y + other.y,
|
|
26
|
+
this.z + other.z
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
isub(other) {
|
|
31
|
+
this.x -= other.x;
|
|
32
|
+
this.y -= other.y;
|
|
33
|
+
this.z -= other.z;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
sub(other) {
|
|
37
|
+
return new Vector3(
|
|
38
|
+
this.x - other.x,
|
|
39
|
+
this.y - other.y,
|
|
40
|
+
this.z - other.z
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
imul(other) {
|
|
45
|
+
if (other instanceof Vector3) {
|
|
46
|
+
this.x *= other.x;
|
|
47
|
+
this.y *= other.y;
|
|
48
|
+
this.z *= other.z;
|
|
49
|
+
} else {
|
|
50
|
+
this.x *= other;
|
|
51
|
+
this.y *= other;
|
|
52
|
+
this.z *= other;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
mul(other) {
|
|
57
|
+
if (other instanceof Vector3)
|
|
58
|
+
return new Vector3(
|
|
59
|
+
this.x * other.x,
|
|
60
|
+
this.y * other.y,
|
|
61
|
+
this.z * other.z
|
|
62
|
+
);
|
|
63
|
+
else
|
|
64
|
+
return new Vector3(
|
|
65
|
+
this.x * other,
|
|
66
|
+
this.y * other,
|
|
67
|
+
this.z * other
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
idiv(other) {
|
|
72
|
+
if (other instanceof Vector3) {
|
|
73
|
+
this.x /= other.x;
|
|
74
|
+
this.y /= other.y;
|
|
75
|
+
this.z /= other.z;
|
|
76
|
+
} else {
|
|
77
|
+
this.x /= other;
|
|
78
|
+
this.y /= other;
|
|
79
|
+
this.z /= other;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
div(other) {
|
|
84
|
+
if (other instanceof Vector3)
|
|
85
|
+
return new Vector3(
|
|
86
|
+
this.x / other.x,
|
|
87
|
+
this.y / other.y,
|
|
88
|
+
this.z / other.z
|
|
89
|
+
);
|
|
90
|
+
else
|
|
91
|
+
return new Vector3(
|
|
92
|
+
this.x / other,
|
|
93
|
+
this.y / other,
|
|
94
|
+
this.z / other
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
dot(other) {
|
|
99
|
+
return this.x * other.x + this.y * other.y + this.z * other.z;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
sqnorm() {
|
|
103
|
+
return this.dot(this);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
len() {
|
|
107
|
+
return Math.sqrt(this.sqnorm());
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
normalize() {
|
|
111
|
+
const n = this.len();
|
|
112
|
+
this.x /= n;
|
|
113
|
+
this.y /= n;
|
|
114
|
+
this.z /= n;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
normalized() {
|
|
118
|
+
return this.div(this.len());
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
export default Vector3;
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@antti5/ship-tool-common",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Ship Tool common utils and library code",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/antti5/ship-tool-common.git"
|
|
8
|
+
},
|
|
9
|
+
"author": "Antti Salonen",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": {
|
|
13
|
+
"./Util": "./lib/Util.js",
|
|
14
|
+
"./Semaphore": "./lib/Semaphore.js",
|
|
15
|
+
"./Trajectory": "./lib/trajectory/Trajectory.js",
|
|
16
|
+
"./node/Util": "./lib/UtilNode.js"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"eslint": "eslint . --ignore-pattern \"node_modules/*\""
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@eslint/js": "9.39.2",
|
|
23
|
+
"dotenv": "17.2.3",
|
|
24
|
+
"eslint": "9.36.0",
|
|
25
|
+
"find-config": "1.0.0",
|
|
26
|
+
"globals": "16.4.0"
|
|
27
|
+
}
|
|
28
|
+
}
|