From 65a67823cbcd5b01d6c20a65ff3965965062f77a Mon Sep 17 00:00:00 2001 From: Theodore Kruczek Date: Wed, 10 Jan 2024 21:44:08 -0500 Subject: [PATCH] refactor: :recycle: refactor GroundPosition from Sensor --- src/interfaces/GroundPositionParams.ts | 8 +++ src/objects/GroundPosition.ts | 75 ++++++++++++++++++++++++++ src/objects/Sensor.ts | 35 +++--------- 3 files changed, 89 insertions(+), 29 deletions(-) create mode 100644 src/interfaces/GroundPositionParams.ts create mode 100644 src/objects/GroundPosition.ts diff --git a/src/interfaces/GroundPositionParams.ts b/src/interfaces/GroundPositionParams.ts new file mode 100644 index 0000000..d01c853 --- /dev/null +++ b/src/interfaces/GroundPositionParams.ts @@ -0,0 +1,8 @@ +import { Degrees, Kilometers } from './../types/types'; + +export interface GroundPositionParams { + name?: string; + lat: Degrees; + lon: Degrees; + alt: Kilometers; +} diff --git a/src/objects/GroundPosition.ts b/src/objects/GroundPosition.ts new file mode 100644 index 0000000..d5f7ad8 --- /dev/null +++ b/src/objects/GroundPosition.ts @@ -0,0 +1,75 @@ +import { calcGmst, lla2eci, llaRad2ecf } from '../transforms'; +import { Degrees, EcfVec3, EciVec3, Kilometers, LlaVec3, Radians, RaeVec3 } from '../types/types'; +import { DEG2RAD } from '../utils/constants'; +import { GroundPositionParams } from './../interfaces/GroundPositionParams'; + +import { BaseObject } from './BaseObject'; +import { Satellite } from './Satellite'; + +export class GroundPosition extends BaseObject { + name = 'Unknown Ground Position'; + lat: Degrees; + lon: Degrees; + alt: Kilometers; + + constructor(info: GroundPositionParams) { + super(info); + + this.validateInputData_(info); + Object.keys(info).forEach((key) => { + this[key] = info[key]; + }); + } + + isSensor(): boolean { + return false; + } + + rae(sat: Satellite, date: Date = this.time): RaeVec3 { + return sat.raeOpt(this, date); + } + + /** + * Calculates ECF position at a given time. + * + * @optimized + */ + ecf(): EcfVec3 { + return llaRad2ecf(this.llaRad()); + } + + eci(date: Date = this.time): EciVec3 { + const { gmst } = calcGmst(date); + + return lla2eci(this.llaRad(), gmst); + } + + setTime(date: Date): this { + this.time = date; + + return this; + } + + llaRad(): LlaVec3 { + return { + lat: (this.lat * DEG2RAD) as Radians, + lon: (this.lon * DEG2RAD) as Radians, + alt: this.alt, + }; + } + + private validateInputData_(info: GroundPositionParams) { + this.validateParameter_(info.lat, -90, 90, 'Invalid latitude - must be between -90 and 90'); + 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'); + } + + private validateParameter_(value: T, minValue: T, maxValue: T, errorMessage: string): void { + if (minValue !== null && value < minValue) { + throw new Error(errorMessage); + } + if (maxValue !== null && value > maxValue) { + throw new Error(errorMessage); + } + } +} diff --git a/src/objects/Sensor.ts b/src/objects/Sensor.ts index b9b3d7e..bff2232 100644 --- a/src/objects/Sensor.ts +++ b/src/objects/Sensor.ts @@ -1,20 +1,9 @@ -import { - Degrees, - Kilometers, - LlaVec3, - Lookangle, - PassType, - Radians, - RaeVec3, - SensorParams, - SpaceObjectType, -} from '../types/types'; -import { DEG2RAD } from '../utils/constants'; - -import { BaseObject } from './BaseObject'; +import { Degrees, Kilometers, Lookangle, PassType, RaeVec3, SensorParams, SpaceObjectType } from '../types/types'; + +import { GroundPosition } from './GroundPosition'; import { Satellite } from './Satellite'; -export class Sensor extends BaseObject { +export class Sensor extends GroundPosition { name: string; type: SpaceObjectType; lat: Degrees; @@ -83,7 +72,7 @@ export class Sensor extends BaseObject { for (let timeOffset = 0; timeOffset < planningInterval; timeOffset++) { const curTime = new Date(startTime + timeOffset * 1000); - const rae = this.getRae(sat, curTime); + const rae = this.rae(sat, curTime); const isInView = this.isRaeInFov(rae); @@ -122,10 +111,6 @@ export class Sensor extends BaseObject { return msnPlanPasses; } - getRae(sat: Satellite, date: Date = this.time): RaeVec3 { - return sat.raeOpt(this, date); - } - isRaeInFov(rae: RaeVec3): boolean { if (rae.el < this.minEl || rae.el > this.maxEl) { return false; @@ -149,7 +134,7 @@ export class Sensor extends BaseObject { } isSatInFov(sat: Satellite, date: Date = this.time): boolean { - return this.isRaeInFov(this.getRae(sat, date)); + return this.isRaeInFov(this.rae(sat, date)); } setTime(date: Date): this { @@ -158,14 +143,6 @@ export class Sensor extends BaseObject { return this; } - getLlaRad(): LlaVec3 { - return { - lat: (this.lat * DEG2RAD) as Radians, - lon: (this.lon * DEG2RAD) as Radians, - alt: this.alt, - }; - } - isDeepSpace(): boolean { return this.maxRng > 6000; }