-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathstereo-map.ts
439 lines (378 loc) · 15.3 KB
/
stereo-map.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
*!
*stereo-map.ts was created by Theodore Kruczek using the work of
*Julius Tens' "projections" library (https://github.com/juliuste/projections).
*This file is exclusively released under the same license as the original author.
*The license only applies to stereo-map.ts
*
*The MIT License
*Copyright (c) 2017, Julius Tens
*
*Permission is hereby granted, free of charge, to any person obtaining a
*copy of this software and associated documentation files (the "Software"),
*to deal in the Software without restriction, including without limitation
*the rights to use, copy, modify, merge, publish, distribute, sublicense,
*and/or sell copies of the Software, and to permit persons to whom the
*Software is furnished to do so, subject to the following conditions:
*The above copyright notice and this permission notice shall be included
*in all copies or substantial portions of the Software.
*
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
*FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
*COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
*IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
*CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { KeepTrackApiEvents } from '@app/interfaces';
import { keepTrackApi } from '@app/keepTrackApi';
import { getEl, hideEl, showEl } from '@app/lib/get-el';
import { errorManagerInstance } from '@app/singletons/errorManager';
import mapPng from '@public/img/icons/map.png';
import radar1 from '@public/img/radar-1.png';
import redSquare from '@public/img/red-square.png';
import satellite2 from '@public/img/satellite-2.png';
import yellowSquare from '@public/img/yellow-square.png';
import { dateFormat } from '@app/lib/dateFormat';
import { SatMath } from '@app/static/sat-math';
import { BaseObject, Degrees, DetailedSatellite, DetailedSensor, Kilometers, LlaVec3, calcGmst, eci2lla } from 'ootk';
import { KeepTrackPlugin } from '../KeepTrackPlugin';
import { SelectSatManager } from '../select-sat-manager/select-sat-manager';
import { SoundNames } from '../sounds/SoundNames';
interface GroundTracePoint {
x: number;
y: number;
inView: boolean;
}
export class StereoMap extends KeepTrackPlugin {
dependencies_ = [SelectSatManager.name];
private selectSatManager_: SelectSatManager;
constructor() {
super();
this.selectSatManager_ = keepTrackApi.getPlugin(SelectSatManager);
}
/** The size of half of the dot used in the stereo map. (See CSS) */
private readonly halfDotSize_ = 6;
private canvas_: HTMLCanvasElement;
private satCrunchNow_ = 0;
private isMapUpdateOverride_ = false;
private earthImg = new Image();
isRequireSatelliteSelected = true;
isIconDisabled = true;
isIconDisabledOnLoad = true;
bottomIconElementName = 'menu-map';
bottomIconImg = mapPng;
bottomIconLabel = 'Stereo Map';
bottomIconCallback: () => void = () => {
if (!this.isMenuButtonActive) {
return;
}
this.updateMap();
};
helpTitle = 'Stereographic Map Menu';
helpBody = keepTrackApi.html`The Stereographic Map Menu is used for visualizing satellite ground traces in a stereographic projection.
<br/><br/>
You can click on a spot along the ground trace to change the time of the simulation to when the satellite reaches that spot.
<br/><br/>
The yellow dots indicate when the satellite is in view of the sensor. The red dots indicate when the satellite is not in view of the sensor. The dot closest to the satellite is the current time.
`;
sideMenuElementName = 'map-menu';
sideMenuElementHtml =
`${keepTrackApi.html`
<div id="map-menu" class="side-menu-parent start-hidden side-menu valign-wrapper">
<canvas id="map-2d"></canvas>
<img id="map-sat" class="map-item map-look" src=${satellite2} width="20px" height="20px"/>
<img id="map-sensor" class="map-item map-look start-hidden" src=${radar1} width="20px" height="20px"/>
` +
StereoMap.generateMapLooks_(50)
}</div>`;
static generateMapLooks_(count: number): string {
let html = '';
for (let i = 1; i <= count; i++) {
html += `<img id="map-look${i}" class="map-item map-look"/>`;
}
return html;
}
addHtml(): void {
super.addHtml();
import('./stereo-map.css');
keepTrackApi.register({
event: KeepTrackApiEvents.uiManagerFinal,
cbName: this.constructor.name,
cb: () => {
this.canvas_ = <HTMLCanvasElement>getEl('map-2d');
this.resize2DMap_();
window.addEventListener('resize', () => {
if (!settingsManager.disableUI) {
this.resize2DMap_();
}
});
getEl('fullscreen-icon')?.addEventListener('click', () => {
this.resize2DMap_();
});
getEl('map-menu').addEventListener('click', (evt: Event) => {
if (!(<HTMLElement>evt.target).classList.contains('map-look')) {
return;
}
this.mapMenuClick(evt);
});
},
});
}
addJs(): void {
super.addJs();
keepTrackApi.register({
event: KeepTrackApiEvents.onCruncherMessage,
cbName: this.constructor.name,
cb: this.onCruncherMessage.bind(this),
});
keepTrackApi.register({
event: KeepTrackApiEvents.selectSatData,
cbName: this.constructor.name,
cb: (sat: BaseObject) => {
if (!this.isMenuButtonActive) {
return;
}
if (sat) {
this.updateMap();
}
},
});
const keyboardManager = keepTrackApi.getInputManager().keyboard;
keyboardManager.registerKeyUpEvent({
key: 'M',
callback: () => {
if (keepTrackApi.getPlugin(SelectSatManager).selectedSat === -1) {
return;
}
if (!this.isMenuButtonActive) {
this.openSideMenu();
this.setBottomIconToSelected();
this.updateMap();
keepTrackApi.getSoundManager().play(SoundNames.TOGGLE_ON);
} else {
this.closeSideMenu();
this.setBottomIconToUnselected();
keepTrackApi.getSoundManager().play(SoundNames.TOGGLE_OFF);
}
},
});
}
updateMap(): void {
try {
if (this.selectSatManager_.selectedSat === -1) {
return;
}
if (!this.isMenuButtonActive) {
return;
}
this.updateSatPosition_();
StereoMap.updateSensorPosition_();
this.drawEarthLayer();
this.drawGroundTrace_();
this.addTextToMap_();
} catch (e) {
errorManagerInstance.info(e);
}
}
static getMapPoints_(now: Date, sat: DetailedSatellite, sensor: DetailedSensor): { lla: LlaVec3<Degrees, Kilometers>; inView: boolean; time: string } {
const time = dateFormat(now, 'isoDateTime', true);
const { gmst } = calcGmst(now);
const lla = eci2lla(sat.eci(now).position, gmst);
const inView = sensor.isSatInFov(sat, now);
return { lla, inView, time };
}
private drawGroundTrace_() {
const groundTracePoints: GroundTracePoint[] = [];
const pointPerOrbit = 512;
// We only have 50 clickable markers
const selectableInterval = Math.ceil(pointPerOrbit / 50);
let selectableIdx = 1;
const sat = keepTrackApi.getCatalogManager().getSat(this.selectSatManager_.selectedSat);
const sensor = keepTrackApi.getSensorManager().currentSensors[0];
// Start at 1 so that the first point is NOT the satellite
for (let i = 1; i < pointPerOrbit; i++) {
const now = new Date(keepTrackApi.getTimeManager().simulationTimeObj.getTime() + ((i * sat.period * 1.15) / pointPerOrbit) * 60 * 1000);
const mapPoints = StereoMap.getMapPoints_(now, sat, sensor);
groundTracePoints.push({
x: ((mapPoints.lla.lon + 180) / 360) * settingsManager.mapWidth,
y: settingsManager.mapHeight - ((mapPoints.lla.lat + 90) / 180) * settingsManager.mapHeight,
inView: mapPoints.inView,
});
if (i % selectableInterval === 0) {
const dotDom = <HTMLImageElement>getEl(`map-look${selectableIdx}`);
dotDom.src = mapPoints.inView ? yellowSquare : redSquare;
dotDom.style.left = `${groundTracePoints[i - 1].x - this.halfDotSize_}px`;
dotDom.style.top = `${groundTracePoints[i - 1].y - this.halfDotSize_}px`;
dotDom.dataset.time = mapPoints.time;
selectableIdx++;
}
}
// Draw ground trace
const ctx = this.canvas_.getContext('2d');
const bigJumpSize = 0.2 * settingsManager.mapWidth;
ctx.strokeStyle = groundTracePoints[0].inView ? '#ffff00' : '#ff0000';
ctx.lineWidth = 4;
ctx.beginPath();
ctx.moveTo(groundTracePoints[0].x, groundTracePoints[0].y);
for (let i = 1; i < groundTracePoints.length; i++) {
if (!groundTracePoints[i].inView && groundTracePoints[i - 1].inView) {
// We are now out of view
ctx.stroke();
ctx.beginPath();
ctx.strokeStyle = '#ff0000';
ctx.moveTo(groundTracePoints[i - 1].x, groundTracePoints[i - 1].y);
ctx.lineTo(groundTracePoints[i].x, groundTracePoints[i].y);
} else if (groundTracePoints[i].inView && !groundTracePoints[i - 1].inView) {
// We are now in view
ctx.stroke();
ctx.beginPath();
ctx.strokeStyle = '#ffff00';
ctx.moveTo(groundTracePoints[i - 1].x, groundTracePoints[i - 1].y);
ctx.lineTo(groundTracePoints[i].x, groundTracePoints[i].y);
} else if (groundTracePoints[i].x - groundTracePoints[i - 1].x > bigJumpSize || groundTracePoints[i - 1].x - groundTracePoints[i].x > bigJumpSize) {
/*
* If there is a big jump assume we crossed a pole and should
* jump to the next point to continue drawing the line
*/
ctx.stroke();
ctx.beginPath();
ctx.moveTo(groundTracePoints[i].x, groundTracePoints[i].y);
} else {
ctx.lineTo(groundTracePoints[i].x, groundTracePoints[i].y);
}
}
ctx.stroke();
}
private addTextToMap_() {
const ctx = this.canvas_.getContext('2d');
const d = new Date();
const n = d.getUTCFullYear();
const copyrightStr = !settingsManager.copyrightOveride ? `©${n} KEEPTRACK.SPACE` : '';
const cw = this.canvas_.width;
const ch = this.canvas_.height;
ctx.font = '24px nasalization';
let textWidth = ctx.measureText(copyrightStr).width;
ctx.globalAlpha = 1.0;
ctx.fillStyle = 'black';
ctx.fillText(copyrightStr, cw - textWidth - 30, ch - 30);
if (settingsManager.classificationStr !== '') {
ctx.font = '24px nasalization';
textWidth = ctx.measureText(settingsManager.classificationStr).width;
ctx.globalAlpha = 1.0;
switch (settingsManager.classificationStr) {
case 'Top Secret//SCI':
ctx.fillStyle = '#fce93a';
break;
case 'Top Secret':
ctx.fillStyle = '#ff8c00';
break;
case 'Secret':
ctx.fillStyle = '#ff0000';
break;
case 'Confidential':
ctx.fillStyle = '#0033a0';
break;
case 'CUI':
ctx.fillStyle = '#512b85';
break;
case 'Unclassified':
ctx.fillStyle = '#007a33';
break;
default:
throw new Error('Invalid classification');
}
ctx.fillText(settingsManager.classificationStr, cw / 2 - textWidth, ch - 20);
ctx.fillText(settingsManager.classificationStr, cw / 2 - textWidth, 34);
}
}
private static updateSensorPosition_() {
const sensorManagerInstance = keepTrackApi.getSensorManager();
if (sensorManagerInstance.isSensorSelected()) {
const map = {
x: ((sensorManagerInstance.currentSensors[0].lon + 180) / 360) * settingsManager.mapWidth,
y: settingsManager.mapHeight - ((sensorManagerInstance.currentSensors[0].lat + 90) / 180) * settingsManager.mapHeight,
};
const sensorDom = <HTMLImageElement>getEl('map-sensor');
showEl('map-sensor');
getEl('map-sensor').style.left = `${map.x - sensorDom.width / 2}px`;
getEl('map-sensor').style.top = `${map.y - sensorDom.height / 2}px`;
} else {
hideEl('map-sensor');
}
}
private updateSatPosition_() {
const catalogManagerInstance = keepTrackApi.getCatalogManager();
const timeManagerInstance = keepTrackApi.getTimeManager();
const sat = catalogManagerInstance.getObject(this.selectSatManager_.selectedSat);
const gmst = SatMath.calculateTimeVariables(timeManagerInstance.simulationTimeObj).gmst;
const lla = eci2lla(sat.position, gmst);
const map = {
x: ((lla.lon + 180) / 360) * settingsManager.mapWidth,
y: settingsManager.mapHeight - ((lla.lat + 90) / 180) * settingsManager.mapHeight,
};
const satDom = <HTMLImageElement>getEl('map-sat');
getEl('map-sat').style.left = `${map.x - satDom.width / 2}px`;
getEl('map-sat').style.top = `${map.y - satDom.height / 2}px`;
}
private resize2DMap_(isForceWidescreen?: boolean): void {
isForceWidescreen ??= false;
const mapMenuDOM = getEl('map-menu');
if (isForceWidescreen || window.innerWidth > window.innerHeight) {
// If widescreen
settingsManager.mapWidth = window.innerWidth;
settingsManager.mapHeight = settingsManager.mapWidth / 2;
mapMenuDOM.style.width = `${window.innerWidth}px`;
this.canvas_.width = settingsManager.mapWidth;
this.canvas_.height = settingsManager.mapHeight;
} else {
settingsManager.mapHeight = window.innerHeight - 100; // Subtract 100 portrait (mobile)
settingsManager.mapWidth = settingsManager.mapHeight * 2;
mapMenuDOM.style.width = `${settingsManager.mapWidth}px`;
this.canvas_.width = settingsManager.mapWidth;
this.canvas_.style.width = `${settingsManager.mapWidth}px`;
this.canvas_.height = settingsManager.mapHeight;
this.canvas_.style.height = `${settingsManager.mapHeight}px`;
}
this.drawEarthLayer();
}
drawEarthLayer(): void {
const ctx = this.canvas_.getContext('2d');
if (this.earthImg.src) {
ctx.drawImage(this.earthImg, 0, 0, settingsManager.mapWidth, settingsManager.mapHeight);
} else {
this.earthImg.src = `${settingsManager.installDirectory}textures/earthmap4k.jpg`;
this.earthImg.onload = () => {
ctx.drawImage(this.earthImg, 0, 0, settingsManager.mapWidth, settingsManager.mapHeight);
};
}
}
onCruncherMessage(): void {
if (this.isMenuButtonActive || this.isMapUpdateOverride_) {
this.satCrunchNow_ = Date.now();
if (this.satCrunchNow_ > settingsManager.lastMapUpdateTime + 30000 || this.isMapUpdateOverride_) {
this.updateMap();
settingsManager.lastMapUpdateTime = this.satCrunchNow_;
this.isMapUpdateOverride_ = false;
} else {
// Update these two items even if we don't update the whole map
this.updateSatPosition_();
StereoMap.updateSensorPosition_();
}
}
}
mapMenuClick(evt: any) {
const timeManagerInstance = keepTrackApi.getTimeManager();
this.isMapUpdateOverride_ = true;
if (!evt.target?.dataset.time) {
return;
}
let time = evt.target.dataset.time;
if (time !== null) {
time = time.split(' ');
time = new Date(`${time[0]}T${time[1]}Z`);
const today = new Date(); // Need to know today for offset calculation
timeManagerInstance.changeStaticOffset(time.getTime() - today.getTime()); // Find the offset from today
}
}
}