@johnmorrisdotca/chikyu-earth 0.57.0 → 0.58.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.
@@ -3,7 +3,8 @@ import { satelliteEmphasis } from "./satellite-focus.js";
3
3
  import { BUILT_IN_PATH_STYLES, capPaths, nearestLoop, resolvePathId, styleOf, styleTable } from "./paths.js";
4
4
  import { shadowFlags, sunDirectionScene } from "../flat/track-state.js";
5
5
  import { TRACK_SHADOW, TRACK_SUNLIT } from "../flat/colours.js";
6
- import { createPathRibbons, RIDE_PATH_FADE_KM } from "./path-ribbons.js";
6
+ import { createPathRibbons, PATH_POINTS, RIDE_PATH_FADE_KM } from "./path-ribbons.js";
7
+ import { createTrailBuilder, TRAIL_MAX_POINTS, TRAIL_SLOTS } from "./trail.js";
7
8
  /**
8
9
  * The longest a streak may be along its orbit, in Earth radii: 0.025, about 160 km, which is what a low satellite covers in a frame at 10 minutes a second (150 km). On the screen that is a share of the Earth's drawn radius, so a streak
9
10
  * is a few millimetres when the Earth fills the view, half as long when the Earth is half as large, and a dot when the Earth is tiny. A cap of so many pixels alone would leave the same length on a small Earth, where it looks long.
@@ -58,7 +59,7 @@ export function longestStreak(segments, count, earthPx) {
58
59
  }
59
60
  return longest * earthPx;
60
61
  }
61
- export const newSatelliteState = () => ({ groups: new Map(), selected: null, marked: new Set(), pinned: [], orbitTint: false, focus: null, modelOver: null, hover: { ref: null, fade: 0, enabled: true, color: "#ffffff", opacity: HOVER_PATH_OPACITY }, paths: { requests: [], overrides: {}, selectedOn: true, light: { on: true, sun: TRACK_SUNLIT, shadow: TRACK_SHADOW }, version: 0, info: [], dropped: 0, unresolved: 0 }, ridden: null, streakMax: DEFAULT_STREAK_MAX, drawn: { placed: 0, segments: 0, tooFast: false, dotScale: 1, longestStreakPx: 0, streakCapPx: 0, weight: 1, fade: 1, earthPx: 0, updateMs: 0, maxUpdateMs: 0, updates: 0, totalMs: 0, selectedRing: false, hoverPath: false, hoverOpacity: 0, selectedOpacity: 0, orbitLines: 0, pathsDrawn: 0, pathDraws: 0, estimated: 0 } });
62
+ export const newSatelliteState = () => ({ groups: new Map(), selected: null, marked: new Set(), pinned: [], orbitTint: false, focus: null, modelOver: null, hover: { ref: null, fade: 0, enabled: true, color: "#ffffff", opacity: HOVER_PATH_OPACITY }, paths: { requests: [], overrides: {}, selectedOn: true, light: { on: true, sun: TRACK_SUNLIT, shadow: TRACK_SHADOW }, version: 0, info: [], dropped: 0, unresolved: 0 }, ridden: null, trail: { ref: null, orbits: 3, colour: "#8fe3ff", version: 0 }, streakMax: DEFAULT_STREAK_MAX, drawn: { placed: 0, segments: 0, tooFast: false, dotScale: 1, longestStreakPx: 0, streakCapPx: 0, weight: 1, fade: 1, earthPx: 0, updateMs: 0, maxUpdateMs: 0, updates: 0, totalMs: 0, selectedRing: false, hoverPath: false, hoverOpacity: 0, selectedOpacity: 0, orbitLines: 0, pathsDrawn: 0, pathDraws: 0, estimated: 0, trailPoints: 0, trailBuilds: 0 } });
62
63
  /** The most orbits a group may draw all at once, one line each. */
63
64
  export const MAX_ORBIT_GROUP = 40;
64
65
  const ORBIT_POINTS = 120;
@@ -260,6 +261,71 @@ export function createSatelliteLayer(three, scene, state, ratio) {
260
261
  let hoverKey = "";
261
262
  // The page's paths and the chosen satellite's own orbit: one mesh, one draw call, buffers made once (see path-ribbons.ts).
262
263
  const ribbons = createPathRibbons(three, scene);
264
+ // The Observe view's trail: its own ribbon mesh (one draw call), made the first time a trail is asked for and never otherwise; its buffers are made once.
265
+ let trailRibbons = null;
266
+ const trailBuilder = createTrailBuilder();
267
+ const trailPoints = new Float32Array(TRAIL_MAX_POINTS * 3);
268
+ const trailChunks = Array.from({ length: TRAIL_SLOTS }, () => new Float32Array(PATH_POINTS * 3));
269
+ const trailPaths = [];
270
+ const trailStyle = { color: "#8fe3ff", width: 2.5, opacity: 0.95, dash: null };
271
+ let trailAt = Number.NaN;
272
+ let trailKey = "";
273
+ const updateTrail = (time, size, radius) => {
274
+ // the trail is on while its satellite is the one ridden: any other ride, or none, takes it away
275
+ const wanted = state.trail.ref;
276
+ const want = wanted !== null && state.ridden !== null && state.ridden.group === wanted.group && state.ridden.index === wanted.index ? wanted : null;
277
+ if (want === null) {
278
+ if (trailRibbons && trailRibbons.count > 0) {
279
+ trailRibbons.set([]);
280
+ state.drawn.trailPoints = 0;
281
+ }
282
+ trailKey = "";
283
+ return;
284
+ }
285
+ const set = find(want);
286
+ const info = set?.sets[want.index];
287
+ if (!set || !info)
288
+ return;
289
+ trailRibbons ??= createPathRibbons(three, scene);
290
+ if (size)
291
+ trailRibbons.frame(size.width, size.height, radius);
292
+ const key = `${refKey(want)}|${state.trail.orbits}|${state.trail.colour}|${state.trail.version}`;
293
+ if (key === trailKey && time === trailAt)
294
+ return;
295
+ if (key !== trailKey) {
296
+ trailBuilder.reset();
297
+ trailStyle.color = state.trail.colour;
298
+ }
299
+ trailKey = key;
300
+ trailAt = time;
301
+ const periodMs = (1440 / (info.MEAN_MOTION || 15)) * 60_000;
302
+ const n = trailBuilder.build((ms, out) => set.exact(want.index, ms, out), periodMs, state.trail.orbits, time, trailPoints);
303
+ state.drawn.trailBuilds += 1;
304
+ state.drawn.trailPoints = n;
305
+ if (n < 2) {
306
+ trailRibbons.set([]);
307
+ return;
308
+ }
309
+ // the points are laid in ribbon slots of 120, each sharing its end point with the next, the last padded with its own end point (a segment of no length)
310
+ const chunks = Math.ceil((n - 1) / (PATH_POINTS - 1));
311
+ trailPaths.length = chunks;
312
+ for (let c = 0; c < chunks; c += 1) {
313
+ const buffer = trailChunks[c];
314
+ const from = c * (PATH_POINTS - 1);
315
+ for (let i = 0; i < PATH_POINTS; i += 1) {
316
+ const p = Math.min(n - 1, from + i) * 3;
317
+ buffer[i * 3] = trailPoints[p];
318
+ buffer[i * 3 + 1] = trailPoints[p + 1];
319
+ buffer[i * 3 + 2] = trailPoints[p + 2];
320
+ }
321
+ const path = trailPaths[c];
322
+ if (path)
323
+ path.positions = buffer;
324
+ else
325
+ trailPaths[c] = { positions: buffer, style: trailStyle, open: true };
326
+ }
327
+ trailRibbons.set(trailPaths);
328
+ };
263
329
  let slots = [];
264
330
  let slotsSignature = "";
265
331
  let resolvedFor = [];
@@ -637,6 +703,7 @@ export function createSatelliteLayer(three, scene, state, ratio) {
637
703
  state.drawn.orbitLines = lines;
638
704
  if (size)
639
705
  ribbons.frame(size.width, size.height, radius);
706
+ updateTrail(time, size, radius);
640
707
  const chosen = state.selected;
641
708
  const chosenGroup = chosen ? state.groups.get(chosen.group) : undefined;
642
709
  const chosenDrawn = chosen !== null && chosenGroup !== undefined && satelliteEmphasis(chosen.group, chosenGroup.visible, state.focus).drawn;
@@ -846,6 +913,7 @@ export function createSatelliteLayer(three, scene, state, ratio) {
846
913
  drawn.clear();
847
914
  scene.remove(selectedPoints, hoverLoop);
848
915
  ribbons.dispose();
916
+ trailRibbons?.dispose();
849
917
  selectedGeometry.dispose();
850
918
  selectedMaterial.dispose();
851
919
  hoverLine.geometry.dispose();
@@ -0,0 +1,117 @@
1
+ /**
2
+ * How many orbits the Observe view's trail reaches back when the page does not say (3).
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import { TRAIL_DEFAULT_ORBITS } from "@johnmorrisdotca/chikyu-earth/space";
7
+ *
8
+ * console.log(TRAIL_DEFAULT_ORBITS); // 3
9
+ * ```
10
+ */
11
+ export declare const TRAIL_DEFAULT_ORBITS = 3;
12
+ /**
13
+ * The most orbits a trail reaches back (20): about 31 hours for the ISS.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { TRAIL_MAX_ORBITS } from "@johnmorrisdotca/chikyu-earth/space";
18
+ *
19
+ * console.log(TRAIL_MAX_ORBITS); // 20
20
+ * ```
21
+ */
22
+ export declare const TRAIL_MAX_ORBITS = 20;
23
+ /**
24
+ * Points to an orbit on the trail's grid: 3.75 degrees of the orbit between two.
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * import { TRAIL_POINTS_PER_ORBIT } from "@johnmorrisdotca/chikyu-earth/space";
29
+ *
30
+ * console.log(TRAIL_POINTS_PER_ORBIT); // 96
31
+ * ```
32
+ */
33
+ export declare const TRAIL_POINTS_PER_ORBIT = 96;
34
+ /**
35
+ * The most points a trail holds: the grid points of the longest window, the satellite's own place and one over. The size of the array `TrailBuilder.build` writes into is this many points of 3 numbers.
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * import { TRAIL_MAX_POINTS } from "@johnmorrisdotca/chikyu-earth/space";
40
+ *
41
+ * console.log(new Float32Array(TRAIL_MAX_POINTS * 3).length); // 5766
42
+ * ```
43
+ */
44
+ export declare const TRAIL_MAX_POINTS: number;
45
+ /** The ribbon slots a trail needs, 120 points each with one shared between neighbours. */
46
+ export declare const TRAIL_SLOTS: number;
47
+ /**
48
+ * The number of orbits a trail reaches back, read from what a page gave: a number from a quarter of an orbit to {@link TRAIL_MAX_ORBITS} (anything else is {@link TRAIL_DEFAULT_ORBITS}).
49
+ *
50
+ * @param value - A number, a numeric string, or anything else.
51
+ * @returns Orbits, 0.25 to 20.
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * import { trailOrbits } from "@johnmorrisdotca/chikyu-earth/space";
56
+ *
57
+ * console.log(trailOrbits(5), trailOrbits(100), trailOrbits("x")); // 5 20 3
58
+ * ```
59
+ */
60
+ export declare function trailOrbits(value: unknown): number;
61
+ /**
62
+ * Where a satellite is at a moment (Earth radii in the space view's axes into `out[0..2]`; km/s in `out[3..5]`): false when it is not placed then.
63
+ *
64
+ * @example
65
+ * ```ts
66
+ * import type { PlaceAt } from "@johnmorrisdotca/chikyu-earth/space";
67
+ *
68
+ * const nowhere: PlaceAt = () => false;
69
+ * console.log(nowhere(0, new Float64Array(6))); // false
70
+ * ```
71
+ */
72
+ export type PlaceAt = (ms: number, out: Float64Array) => boolean;
73
+ /**
74
+ * A builder of the Observe view's trail: it keeps the points of the past between calls, so a frame costs only what is new.
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * import { createTrailBuilder, type TrailBuilder } from "@johnmorrisdotca/chikyu-earth/space";
79
+ *
80
+ * const builder: TrailBuilder = createTrailBuilder();
81
+ * console.log(builder.calls); // 0
82
+ * ```
83
+ */
84
+ export interface TrailBuilder {
85
+ /**
86
+ * Work out the trail for `orbits` orbits (one is `periodMs`) ending at `ms` into `out` (3 numbers a point, at least {@link TRAIL_MAX_POINTS} points of room): the kept grid points inside the window, oldest first,
87
+ * then the satellite's own place at `ms`. Returns how many points are valid, 0 when the satellite is not placed at `ms`. A grid point where it was not placed (before its launch) is left out.
88
+ */
89
+ build(place: PlaceAt, periodMs: number, orbits: number, ms: number, out: Float32Array): number;
90
+ /** Forget the kept points: the satellite changed. */
91
+ reset(): void;
92
+ /** SGP4 calls (calls of `place`) made so far, for a test to count. */
93
+ readonly calls: number;
94
+ }
95
+ /**
96
+ * A builder of trails. It keeps the points of the past between calls; give it a satellite's `place` and call `reset()` when the satellite is another.
97
+ *
98
+ * @returns The builder.
99
+ *
100
+ * @example
101
+ * ```ts
102
+ * import { createTrailBuilder, TRAIL_MAX_POINTS } from "@johnmorrisdotca/chikyu-earth/space";
103
+ *
104
+ * const builder = createTrailBuilder();
105
+ * const out = new Float32Array(TRAIL_MAX_POINTS * 3);
106
+ * // a satellite going round the Earth's equator once in 90 minutes
107
+ * const place = (ms: number, o: Float64Array): boolean => {
108
+ * const a = (ms / 5_400_000) * 2 * Math.PI;
109
+ * o[0] = 1.07 * Math.cos(a);
110
+ * o[1] = 0;
111
+ * o[2] = -1.07 * Math.sin(a);
112
+ * return true;
113
+ * };
114
+ * console.log(builder.build(place, 5_400_000, 2, 10_000_000, out)); // 98
115
+ * ```
116
+ */
117
+ export declare function createTrailBuilder(): TrailBuilder;
@@ -0,0 +1,152 @@
1
+ // The Observe view's trail: the last N orbits of a satellite's path as points in the space view's axes. Pure numbers and no allocation in a frame: the points of the past are worked out once, on a grid a
2
+ // forty-eighth of an orbit apart, and kept, so a frame costs one SGP4 call for the satellite's own place and the points that have come into the window since the last frame.
3
+ import { PATH_POINTS } from "./path-ribbons.js";
4
+ /**
5
+ * How many orbits the Observe view's trail reaches back when the page does not say (3).
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { TRAIL_DEFAULT_ORBITS } from "@johnmorrisdotca/chikyu-earth/space";
10
+ *
11
+ * console.log(TRAIL_DEFAULT_ORBITS); // 3
12
+ * ```
13
+ */
14
+ export const TRAIL_DEFAULT_ORBITS = 3;
15
+ /**
16
+ * The most orbits a trail reaches back (20): about 31 hours for the ISS.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * import { TRAIL_MAX_ORBITS } from "@johnmorrisdotca/chikyu-earth/space";
21
+ *
22
+ * console.log(TRAIL_MAX_ORBITS); // 20
23
+ * ```
24
+ */
25
+ export const TRAIL_MAX_ORBITS = 20;
26
+ /**
27
+ * Points to an orbit on the trail's grid: 3.75 degrees of the orbit between two.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * import { TRAIL_POINTS_PER_ORBIT } from "@johnmorrisdotca/chikyu-earth/space";
32
+ *
33
+ * console.log(TRAIL_POINTS_PER_ORBIT); // 96
34
+ * ```
35
+ */
36
+ export const TRAIL_POINTS_PER_ORBIT = 96;
37
+ /**
38
+ * The most points a trail holds: the grid points of the longest window, the satellite's own place and one over. The size of the array `TrailBuilder.build` writes into is this many points of 3 numbers.
39
+ *
40
+ * @example
41
+ * ```ts
42
+ * import { TRAIL_MAX_POINTS } from "@johnmorrisdotca/chikyu-earth/space";
43
+ *
44
+ * console.log(new Float32Array(TRAIL_MAX_POINTS * 3).length); // 5766
45
+ * ```
46
+ */
47
+ export const TRAIL_MAX_POINTS = TRAIL_MAX_ORBITS * TRAIL_POINTS_PER_ORBIT + 2;
48
+ /** The ribbon slots a trail needs, 120 points each with one shared between neighbours. */
49
+ export const TRAIL_SLOTS = Math.ceil((TRAIL_MAX_POINTS - 1) / (PATH_POINTS - 1));
50
+ /**
51
+ * The number of orbits a trail reaches back, read from what a page gave: a number from a quarter of an orbit to {@link TRAIL_MAX_ORBITS} (anything else is {@link TRAIL_DEFAULT_ORBITS}).
52
+ *
53
+ * @param value - A number, a numeric string, or anything else.
54
+ * @returns Orbits, 0.25 to 20.
55
+ *
56
+ * @example
57
+ * ```ts
58
+ * import { trailOrbits } from "@johnmorrisdotca/chikyu-earth/space";
59
+ *
60
+ * console.log(trailOrbits(5), trailOrbits(100), trailOrbits("x")); // 5 20 3
61
+ * ```
62
+ */
63
+ export function trailOrbits(value) {
64
+ const n = typeof value === "string" ? Number(value) : value;
65
+ if (typeof n !== "number" || !Number.isFinite(n) || n <= 0)
66
+ return TRAIL_DEFAULT_ORBITS;
67
+ return Math.min(TRAIL_MAX_ORBITS, Math.max(0.25, n));
68
+ }
69
+ const RING = 1024;
70
+ /**
71
+ * A builder of trails. It keeps the points of the past between calls; give it a satellite's `place` and call `reset()` when the satellite is another.
72
+ *
73
+ * @returns The builder.
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * import { createTrailBuilder, TRAIL_MAX_POINTS } from "@johnmorrisdotca/chikyu-earth/space";
78
+ *
79
+ * const builder = createTrailBuilder();
80
+ * const out = new Float32Array(TRAIL_MAX_POINTS * 3);
81
+ * // a satellite going round the Earth's equator once in 90 minutes
82
+ * const place = (ms: number, o: Float64Array): boolean => {
83
+ * const a = (ms / 5_400_000) * 2 * Math.PI;
84
+ * o[0] = 1.07 * Math.cos(a);
85
+ * o[1] = 0;
86
+ * o[2] = -1.07 * Math.sin(a);
87
+ * return true;
88
+ * };
89
+ * console.log(builder.build(place, 5_400_000, 2, 10_000_000, out)); // 98
90
+ * ```
91
+ */
92
+ export function createTrailBuilder() {
93
+ const keys = new Float64Array(RING).fill(Number.NaN);
94
+ const kept = new Float32Array(RING * 3);
95
+ const good = new Uint8Array(RING);
96
+ const here = new Float64Array(6);
97
+ let step = Number.NaN;
98
+ let calls = 0;
99
+ const slot = (g) => ((g % RING) + RING) % RING;
100
+ const reset = () => {
101
+ keys.fill(Number.NaN);
102
+ step = Number.NaN;
103
+ };
104
+ return {
105
+ build(place, periodMs, orbits, ms, out) {
106
+ if (!(periodMs > 0) || !(orbits > 0))
107
+ return 0;
108
+ const dt = periodMs / TRAIL_POINTS_PER_ORBIT;
109
+ if (dt !== step) {
110
+ keys.fill(Number.NaN);
111
+ step = dt;
112
+ }
113
+ calls += 1;
114
+ if (!place(ms, here))
115
+ return 0;
116
+ const live0 = here[0];
117
+ const live1 = here[1];
118
+ const live2 = here[2];
119
+ const last = Math.floor(ms / dt);
120
+ const first = Math.max(last - (TRAIL_MAX_POINTS - 3), Math.ceil((ms - Math.min(orbits, TRAIL_MAX_ORBITS) * periodMs) / dt));
121
+ let count = 0;
122
+ for (let g = first; g <= last; g += 1) {
123
+ const s = slot(g);
124
+ if (keys[s] !== g) {
125
+ keys[s] = g;
126
+ calls += 1;
127
+ const ok = place(g * dt, here);
128
+ good[s] = ok ? 1 : 0;
129
+ if (ok) {
130
+ kept[s * 3] = here[0];
131
+ kept[s * 3 + 1] = here[1];
132
+ kept[s * 3 + 2] = here[2];
133
+ }
134
+ }
135
+ if (good[s] !== 1)
136
+ continue;
137
+ out[count * 3] = kept[s * 3];
138
+ out[count * 3 + 1] = kept[s * 3 + 1];
139
+ out[count * 3 + 2] = kept[s * 3 + 2];
140
+ count += 1;
141
+ }
142
+ out[count * 3] = live0;
143
+ out[count * 3 + 1] = live1;
144
+ out[count * 3 + 2] = live2;
145
+ return count + 1;
146
+ },
147
+ reset,
148
+ get calls() {
149
+ return calls;
150
+ },
151
+ };
152
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnmorrisdotca/chikyu-earth",
3
- "version": "0.57.0",
3
+ "version": "0.58.0",
4
4
  "description": "Chikyu Earth 地球 — A line-drawn globe for the web: country outlines that turn at the Earth's own rate, centred on the place you give it, with an optional realistic Earth from space (three.js), the Sun, Moon and planets, satellites on their orbits, and markers for any place. A web component and an SVG string.",
5
5
  "keywords": [
6
6
  "earth",