Skip to content

Commit

Permalink
feat: ✨ improve isStatic logic
Browse files Browse the repository at this point in the history
  • Loading branch information
thkruz committed Jan 15, 2024
1 parent 90bdd3e commit 56ce400
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 43 deletions.
30 changes: 9 additions & 21 deletions src/objects/BaseObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,19 @@ export class BaseObject {
this.id = info.id;
this.active = info.active ?? true;

// Default to the center of the earth until position is calculated
this.position = info.position ?? {
x: <Kilometers>0,
y: <Kilometers>0,
z: <Kilometers>0,
}; // Default to the center of the earth until position is calculated
};

// Default to 0 velocity until velocity is calculated
this.velocity = info.velocity ?? {
x: <Kilometers>0,
y: <Kilometers>0,
z: <Kilometers>0,
}; // Default to 0 velocity until velocity is calculated
};
this.totalVelocity = Math.sqrt(this.velocity.x ** 2 + this.velocity.y ** 2 + this.velocity.z ** 2);
}

Expand All @@ -62,25 +64,11 @@ export class BaseObject {
}

/**
* Checks if the object is a land object.
* @returns True if the object is a land object, false otherwise.
* Checks if the object is a ground object.
* @returns True if the object is a ground object, false otherwise.
*/
isLandObject(): boolean {
switch (this.type) {
case SpaceObjectType.INTERGOVERNMENTAL_ORGANIZATION:
case SpaceObjectType.SUBORBITAL_PAYLOAD_OPERATOR:
case SpaceObjectType.PAYLOAD_OWNER:
case SpaceObjectType.METEOROLOGICAL_ROCKET_LAUNCH_AGENCY_OR_MANUFACTURER:
case SpaceObjectType.PAYLOAD_MANUFACTURER:
case SpaceObjectType.LAUNCH_VEHICLE_MANUFACTURER:
case SpaceObjectType.ENGINE_MANUFACTURER:
case SpaceObjectType.LAUNCH_AGENCY:
case SpaceObjectType.LAUNCH_SITE:
case SpaceObjectType.LAUNCH_POSITION:
return true;
default:
return false;
}
isGroundObject(): boolean {
return false;
}

/**
Expand All @@ -104,7 +92,7 @@ export class BaseObject {
* @returns True if the object is static, false otherwise.
*/
isStatic(): boolean {
return true;
return this.velocity.x === 0 && this.velocity.y === 0 && this.velocity.z === 0;
}

isPayload(): boolean {
Expand Down
19 changes: 19 additions & 0 deletions src/objects/GroundObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
DEG2RAD,
Geodetic,
BaseObjectParams,
SpaceObjectType,
} from '../main';

import { BaseObject } from './BaseObject';
Expand Down Expand Up @@ -133,4 +134,22 @@ export class GroundObject extends BaseObject {
this.validateParameter(info.lon, -180, 180, 'Invalid longitude - must be between -180 and 180');
this.validateParameter(info.alt, 0, null, 'Invalid altitude - must be greater than 0');
}

isGroundObject(): boolean {
switch (this.type) {
case SpaceObjectType.INTERGOVERNMENTAL_ORGANIZATION:
case SpaceObjectType.SUBORBITAL_PAYLOAD_OPERATOR:
case SpaceObjectType.PAYLOAD_OWNER:
case SpaceObjectType.METEOROLOGICAL_ROCKET_LAUNCH_AGENCY_OR_MANUFACTURER:
case SpaceObjectType.PAYLOAD_MANUFACTURER:
case SpaceObjectType.LAUNCH_VEHICLE_MANUFACTURER:
case SpaceObjectType.ENGINE_MANUFACTURER:
case SpaceObjectType.LAUNCH_AGENCY:
case SpaceObjectType.LAUNCH_SITE:
case SpaceObjectType.LAUNCH_POSITION:
return true;
default:
return false;
}
}
}
41 changes: 19 additions & 22 deletions test/objects/BaseObject.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseObject, BaseObjectParams, EciVec3, SpaceObjectType } from '../../src/main';
import { BaseObject, BaseObjectParams, EciVec3, Kilometers, SpaceObjectType } from '../../src/main';
const mockVelocity = {
x: 8000,
y: 0,
Expand All @@ -18,7 +18,7 @@ describe('BaseObject', () => {
name: 'Satellite',
position: mockPosition,
velocity: mockVelocity,
time: new Date(),

active: true,
};

Expand Down Expand Up @@ -69,7 +69,6 @@ describe('BaseObject', () => {
name: 'Satellite',
position: mockPosition,
velocity: mockVelocity,
time: new Date(),
active: true,
};

Expand All @@ -87,7 +86,6 @@ describe('BaseObject', () => {
name: 'Rocket',
position: mockPosition,
velocity: mockVelocity,
time: new Date(),
active: true,
};

Expand All @@ -105,7 +103,6 @@ describe('BaseObject', () => {
name: 'Debris',
position: mockPosition,
velocity: mockVelocity,
time: new Date(),
active: true,
};

Expand All @@ -123,7 +120,6 @@ describe('BaseObject', () => {
name: 'Star Object',
position: mockPosition,
velocity: mockVelocity,
time: new Date(),
active: true,
};

Expand All @@ -141,7 +137,6 @@ describe('BaseObject', () => {
name: 'Missile',
position: mockPosition,
velocity: mockVelocity,
time: new Date(),
active: true,
};

Expand All @@ -157,7 +152,6 @@ describe('BaseObject', () => {
name: 'Notional Object',
position: mockPosition,
velocity: mockVelocity,
time: new Date(),
active: true,
};

Expand All @@ -173,7 +167,6 @@ describe('BaseObject', () => {
name: 'Satellite',
position: mockPosition,
velocity: mockVelocity,
time: new Date(),
active: true,
};

Expand All @@ -191,7 +184,6 @@ describe('BaseObject', () => {
name: 'Satellite',
position: mockPosition,
velocity: mockVelocity,
time: new Date(),
active: true,
};

Expand All @@ -209,7 +201,6 @@ describe('BaseObject', () => {
name: 'Satellite',
position: mockPosition,
velocity: mockVelocity,
time: new Date(),
active: true,
};

Expand All @@ -221,21 +212,28 @@ describe('BaseObject', () => {
});

// check if BaseObject is static
it('should return true when calling isStatic() method', () => {
const info: BaseObjectParams = {
it('should return coorect value when calling isStatic() method', () => {
const movingObject = new BaseObject({
type: SpaceObjectType.PAYLOAD,
name: 'Satellite',
position: mockPosition,
velocity: mockVelocity,
time: new Date(),
active: true,
};

const baseObject = new BaseObject(info);

const result = baseObject.isStatic();
});
const staticObject = new BaseObject({
type: SpaceObjectType.PAYLOAD,
name: 'Satellite',
position: mockPosition,
velocity: {
x: 0 as Kilometers,
y: 0 as Kilometers,
z: 0 as Kilometers,
},
active: true,
});

expect(result).toBe(true);
expect(movingObject.isStatic()).toBe(false);
expect(staticObject.isStatic()).toBe(true);
});

// check if BaseObject is a land object
Expand All @@ -245,13 +243,12 @@ describe('BaseObject', () => {
name: 'Ground Sensor Station',
position: mockPosition,
velocity: mockVelocity,
time: new Date(),
active: true,
};

const baseObject = new BaseObject(info);

const result = baseObject.isLandObject();
const result = baseObject.isGroundObject();

expect(result).toBe(false);
});
Expand Down
4 changes: 4 additions & 0 deletions test/utils/__snapshots__/functions.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Array [

exports[`functions should be calculate csch 1`] = `0.8509181282393216`;

exports[`functions should be calculate deg2rad 1`] = `0.017453292519943295`;

exports[`functions should be calculate derivative 1`] = `[Function]`;

exports[`functions should be calculate gamma 1`] = `1`;
Expand All @@ -68,6 +70,8 @@ exports[`functions should be calculate log10 1`] = `2`;

exports[`functions should be calculate mean 1`] = `2`;

exports[`functions should be calculate rad2deg 1`] = `57.29577951308232`;

exports[`functions should be calculate sech 1`] = `0.6480542736638855`;

exports[`functions should be calculate sign 1`] = `1`;
Expand Down

0 comments on commit 56ce400

Please sign in to comment.