Skip to content

Commit

Permalink
refactor: ♻️ refactor GroundPosition from Sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
thkruz committed Jan 11, 2024
1 parent 4c2d701 commit 65a6782
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 29 deletions.
8 changes: 8 additions & 0 deletions src/interfaces/GroundPositionParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Degrees, Kilometers } from './../types/types';

export interface GroundPositionParams {
name?: string;
lat: Degrees;
lon: Degrees;
alt: Kilometers;
}
75 changes: 75 additions & 0 deletions src/objects/GroundPosition.ts
Original file line number Diff line number Diff line change
@@ -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<Kilometers, Degrees> {
return sat.raeOpt(this, date);
}

/**
* Calculates ECF position at a given time.
*
* @optimized
*/
ecf(): EcfVec3<Kilometers> {
return llaRad2ecf(this.llaRad());
}

eci(date: Date = this.time): EciVec3<Kilometers> {
const { gmst } = calcGmst(date);

return lla2eci(this.llaRad(), gmst);
}

setTime(date: Date): this {
this.time = date;

return this;
}

llaRad(): LlaVec3<Radians, Kilometers> {
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_<T>(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);
}
}
}
35 changes: 6 additions & 29 deletions src/objects/Sensor.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -122,10 +111,6 @@ export class Sensor extends BaseObject {
return msnPlanPasses;
}

getRae(sat: Satellite, date: Date = this.time): RaeVec3<Kilometers, Degrees> {
return sat.raeOpt(this, date);
}

isRaeInFov(rae: RaeVec3<Kilometers, Degrees>): boolean {
if (rae.el < this.minEl || rae.el > this.maxEl) {
return false;
Expand All @@ -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 {
Expand All @@ -158,14 +143,6 @@ export class Sensor extends BaseObject {
return this;
}

getLlaRad(): LlaVec3<Radians, Kilometers> {
return {
lat: (this.lat * DEG2RAD) as Radians,
lon: (this.lon * DEG2RAD) as Radians,
alt: this.alt,
};
}

isDeepSpace(): boolean {
return this.maxRng > 6000;
}
Expand Down

0 comments on commit 65a6782

Please sign in to comment.