@johnmorrisdotca/chikyu-earth 0.61.0 → 0.63.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.
@@ -0,0 +1,292 @@
1
+ import { type Vec3 } from "./ephemeris.ts";
2
+ /**
3
+ * The height a descent stops at when the page names none, in kilometres: the whole of a city in view, still sharp with the finer 30 m pictures of the land.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import { FLY_HEIGHT_KM } from "@johnmorrisdotca/chikyu-earth/space";
8
+ *
9
+ * console.log(FLY_HEIGHT_KM); // 30
10
+ * ```
11
+ */
12
+ export declare const FLY_HEIGHT_KM = 30;
13
+ /**
14
+ * The lowest height a descent goes to, in kilometres: the camera stays above the clouds' shell.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { FLY_MIN_KM } from "@johnmorrisdotca/chikyu-earth/space";
19
+ *
20
+ * console.log(FLY_MIN_KM); // 15
21
+ * ```
22
+ */
23
+ export declare const FLY_MIN_KM = 15;
24
+ /**
25
+ * The height a descent stops at where no finer picture lies under it (the sea, Antarctica, the far north, or no `tiles-finer`), in kilometres: as low as the package's own pictures stay sharp.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * import { FLY_FLOOR_KM } from "@johnmorrisdotca/chikyu-earth/space";
30
+ *
31
+ * console.log(FLY_FLOOR_KM); // 400
32
+ * ```
33
+ */
34
+ export declare const FLY_FLOOR_KM = 400;
35
+ /**
36
+ * The parts of a fly-to: `across` turns the Earth under the camera at the height it has until the place is below, `down` comes down to the height over the place, `both` (the default) one after the other.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * import type { FlyPhase } from "@johnmorrisdotca/chikyu-earth/space";
41
+ *
42
+ * const phases: FlyPhase[] = ["across", "down", "both"];
43
+ * console.log(phases.length);
44
+ * ```
45
+ */
46
+ export type FlyPhase = "across" | "down" | "both";
47
+ /**
48
+ * Where a move has the camera at one moment, and how it is going: `aim` is the point under it (a unit vector in the Earth's axes: x toward longitude 90, y north, z toward longitude 0), `v` and `a` its speed and
49
+ * acceleration a millisecond, `height` the natural log of the height in kilometres, `hv` and `ha` its speed and acceleration.
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * import type { FlyState } from "@johnmorrisdotca/chikyu-earth/space";
54
+ *
55
+ * const still: FlyState = { aim: [0, 0, 1], v: [0, 0, 0], a: [0, 0, 0], height: Math.log(1000), hv: 0, ha: 0 };
56
+ * console.log(Math.exp(still.height));
57
+ * ```
58
+ */
59
+ export interface FlyState {
60
+ aim: Vec3;
61
+ v: Vec3;
62
+ a: Vec3;
63
+ height: number;
64
+ hv: number;
65
+ ha: number;
66
+ }
67
+ /** The turn of a move: from `from` with the speed `v` and acceleration `a` it had, to `to`, over `ms` from `start`; `axis` and `angle` are the great circle between them. */
68
+ interface Turn {
69
+ start: number;
70
+ ms: number;
71
+ from: Vec3;
72
+ to: Vec3;
73
+ axis: Vec3;
74
+ angle: number;
75
+ v: Vec3;
76
+ a: Vec3;
77
+ }
78
+ /** One part of a move's height, in the log of kilometres: from `from` with the speed and acceleration it had to `to`, over `ms` from `start`. */
79
+ interface Lift {
80
+ start: number;
81
+ ms: number;
82
+ from: number;
83
+ to: number;
84
+ v: number;
85
+ a: number;
86
+ }
87
+ /**
88
+ * A move worked out by `planFly`: its turn, the parts of its height one after the other, the height it starts at (the log of kilometres), when the turn is over (`across`) and when the whole move is
89
+ * (`end`), on the clock it was planned with.
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * import { planFly, type FlyPlan } from "@johnmorrisdotca/chikyu-earth/space";
94
+ *
95
+ * const plan: FlyPlan = planFly({ aim: [0, 0, 1], v: [0, 0, 0], a: [0, 0, 0], height: Math.log(1000), hv: 0, ha: 0 }, [1, 0, 0], { at: 0, phase: "across" });
96
+ * console.log(plan.end > plan.turn.start);
97
+ * ```
98
+ */
99
+ export interface FlyPlan {
100
+ readonly turn: Readonly<Turn>;
101
+ readonly lifts: readonly Readonly<Lift>[];
102
+ readonly height: number;
103
+ readonly across: number;
104
+ readonly end: number;
105
+ }
106
+ /**
107
+ * Where a move has the camera at one moment: `aim`, the unit vector of the point under it, and `heightKm`.
108
+ *
109
+ * @example
110
+ * ```ts
111
+ * import type { FlyPoint } from "@johnmorrisdotca/chikyu-earth/space";
112
+ *
113
+ * const over: FlyPoint = { aim: [0, 0, 1], heightKm: 30 };
114
+ * console.log(over.heightKm);
115
+ * ```
116
+ */
117
+ export interface FlyPoint {
118
+ aim: Vec3;
119
+ heightKm: number;
120
+ }
121
+ /**
122
+ * The unit vector of a longitude and latitude in degrees, in the Earth's axes (x toward longitude 90, y north, z toward longitude 0).
123
+ *
124
+ * @param lon - Degrees east.
125
+ * @param lat - Degrees north.
126
+ * @returns The unit vector.
127
+ *
128
+ * @example
129
+ * ```ts
130
+ * import { flyAim } from "@johnmorrisdotca/chikyu-earth/space";
131
+ *
132
+ * console.log(flyAim(0, 90).map((n) => Math.round(n))); // [0, 1, 0]
133
+ * ```
134
+ */
135
+ export declare function flyAim(lon: number, lat: number): Vec3;
136
+ /**
137
+ * The longitude and latitude in degrees of a unit vector in the Earth's axes, the inverse of `flyAim`.
138
+ *
139
+ * @param aim - The unit vector.
140
+ * @returns `{ lon, lat }`.
141
+ *
142
+ * @example
143
+ * ```ts
144
+ * import { flyAim, flyLonLat } from "@johnmorrisdotca/chikyu-earth/space";
145
+ *
146
+ * console.log(flyLonLat(flyAim(139.69, 35.69)));
147
+ * ```
148
+ */
149
+ export declare function flyLonLat(aim: Vec3): {
150
+ lon: number;
151
+ lat: number;
152
+ };
153
+ /**
154
+ * How long the turn across takes, in milliseconds: 900 for a short way, up to 2,400 for the far side of the Earth.
155
+ *
156
+ * @param angle - The angle between the two points, in radians.
157
+ * @returns Milliseconds.
158
+ *
159
+ * @example
160
+ * ```ts
161
+ * import { flyAcrossMs } from "@johnmorrisdotca/chikyu-earth/space";
162
+ *
163
+ * console.log(flyAcrossMs(0), flyAcrossMs(Math.PI)); // 900 2400
164
+ * ```
165
+ */
166
+ export declare const flyAcrossMs: (angle: number) => number;
167
+ /**
168
+ * How long a descent takes, in milliseconds: 1,200 and half a second more for each time the height is divided by e, at most 5 seconds.
169
+ *
170
+ * @param fromKm - The height it starts from.
171
+ * @param toKm - The height it ends at.
172
+ * @returns Milliseconds.
173
+ *
174
+ * @example
175
+ * ```ts
176
+ * import { flyDownMs } from "@johnmorrisdotca/chikyu-earth/space";
177
+ *
178
+ * console.log(Math.round(flyDownMs(20_000, 30)));
179
+ * ```
180
+ */
181
+ export declare const flyDownMs: (fromKm: number, toKm: number) => number;
182
+ /**
183
+ * Where a move has the camera at a moment of its clock. Before it starts it is where it began; after it ends, where it ended.
184
+ *
185
+ * @param plan - What `planFly` returned.
186
+ * @param t - The moment, in milliseconds of the clock the plan was made with.
187
+ * @returns The point under the camera and its height.
188
+ *
189
+ * @example
190
+ * ```ts
191
+ * import { flyAt, flyAim, flyLonLat, planFly } from "@johnmorrisdotca/chikyu-earth/space";
192
+ *
193
+ * const plan = planFly({ aim: flyAim(0, 0), v: [0, 0, 0], a: [0, 0, 0], height: Math.log(20_000), hv: 0, ha: 0 }, flyAim(139.69, 35.69), { at: 0, phase: "both", heightKm: 30 });
194
+ * const end = flyAt(plan, plan.end);
195
+ * console.log(flyLonLat(end.aim), Math.round(end.heightKm)); // Tokyo, 30
196
+ * ```
197
+ */
198
+ export declare function flyAt(plan: FlyPlan, t: number): FlyPoint;
199
+ /**
200
+ * Where a move has the camera at a moment and how it is going there: what a new move starts from, so that it carries on with the same speed and turn.
201
+ *
202
+ * @param plan - What `planFly` returned.
203
+ * @param t - The moment, in milliseconds of the plan's clock.
204
+ * @returns The state, with its speeds and accelerations a millisecond.
205
+ *
206
+ * @example
207
+ * ```ts
208
+ * import { flyAim, flyStateAt, planFly } from "@johnmorrisdotca/chikyu-earth/space";
209
+ *
210
+ * const plan = planFly({ aim: flyAim(0, 0), v: [0, 0, 0], a: [0, 0, 0], height: Math.log(1000), hv: 0, ha: 0 }, flyAim(90, 0), { at: 0, phase: "across" });
211
+ * const halfway = flyStateAt(plan, plan.end / 2);
212
+ * console.log(Math.hypot(...halfway.v) > 0);
213
+ * ```
214
+ */
215
+ export declare function flyStateAt(plan: FlyPlan, t: number): FlyState;
216
+ /**
217
+ * The state of a camera that is not moving: over `aim` at `heightKm`.
218
+ *
219
+ * @param aim - The unit vector of the point under the camera.
220
+ * @param heightKm - Its height in kilometres.
221
+ * @returns The state, with no speed.
222
+ *
223
+ * @example
224
+ * ```ts
225
+ * import { flyAim, flyStill } from "@johnmorrisdotca/chikyu-earth/space";
226
+ *
227
+ * console.log(flyStill(flyAim(0, 0), 1000).hv); // 0
228
+ * ```
229
+ */
230
+ export declare const flyStill: (aim: Vec3, heightKm: number) => FlyState;
231
+ /**
232
+ * Plan a move from where the camera is and how it is going (`now`) to the point `to`. The turn starts with the camera's own speed and turn and curves to the place along a great circle, slowing to rest over
233
+ * it; the height does the same on a log scale. A height still moving when the move is planned (a descent that a new move cuts short) slows to rest first, without turning back. `across` keeps the height,
234
+ * `down` comes to `heightKm` at once (the turn, if any, beside it), `both` comes down once the turn is over.
235
+ *
236
+ * @param now - Where the camera is and how it is going: `flyStateAt` of the move running, else `flyStill`.
237
+ * @param to - The unit vector of the place.
238
+ * @param options - `at`, the moment on the move's clock; `phase` (`both` by default); `heightKm`, the height to come down to (`FLY_HEIGHT_KM` by default); `acrossMs`, the time of the turn (`flyAcrossMs` of its angle by default).
239
+ * @returns The plan, for `flyAt` and `flyStateAt`.
240
+ *
241
+ * @example
242
+ * ```ts
243
+ * import { flyAim, flyStateAt, planFly } from "@johnmorrisdotca/chikyu-earth/space";
244
+ *
245
+ * const start = { aim: flyAim(0, 0), v: [0, 0, 0] as [number, number, number], a: [0, 0, 0] as [number, number, number], height: Math.log(20_000), hv: 0, ha: 0 };
246
+ * const toTokyo = planFly(start, flyAim(139.69, 35.69), { at: 0 });
247
+ * // half way, the reader asks for London: the new move starts with the speed the first one has
248
+ * const toLondon = planFly(flyStateAt(toTokyo, 800), flyAim(-0.13, 51.51), { at: 800 });
249
+ * console.log(toLondon.end > 800);
250
+ * ```
251
+ */
252
+ export declare function planFly(now: FlyState, to: Vec3, options: {
253
+ at: number;
254
+ phase?: FlyPhase;
255
+ heightKm?: number;
256
+ acrossMs?: number;
257
+ }): FlyPlan;
258
+ /**
259
+ * The height a descent over a place is held at, in kilometres, or `null` when nothing holds it: a descent comes all the way down only where the finer pictures lie (`tiles-finer` on, over land, from 60
260
+ * degrees south to 80 degrees north, as the survey of the layer found), and stops at `FLY_FLOOR_KM` elsewhere, as low as the package's own pictures stay sharp.
261
+ *
262
+ * @param place - `finer`, whether finer pictures are asked for; `land`, whether the place is on land or near its coast; `lat`, its latitude.
263
+ * @returns The height to hold at, or `null`.
264
+ *
265
+ * @example
266
+ * ```ts
267
+ * import { descentFloorKm } from "@johnmorrisdotca/chikyu-earth/space";
268
+ *
269
+ * console.log(descentFloorKm({ finer: true, land: true, lat: 35.69 })); // null: down to the height asked for
270
+ * console.log(descentFloorKm({ finer: true, land: false, lat: 30 })); // 400: the sea
271
+ * ```
272
+ */
273
+ export declare function descentFloorKm(place: {
274
+ finer: boolean;
275
+ land: boolean;
276
+ lat: number;
277
+ }): number | null;
278
+ /**
279
+ * The camera's distance from the Earth's centre, in Earth radii, for a height above the ground in kilometres.
280
+ *
281
+ * @param heightKm - The height.
282
+ * @returns The distance.
283
+ *
284
+ * @example
285
+ * ```ts
286
+ * import { flyDistance } from "@johnmorrisdotca/chikyu-earth/space";
287
+ *
288
+ * console.log(flyDistance(6371)); // 2
289
+ * ```
290
+ */
291
+ export declare const flyDistance: (heightKm: number) => number;
292
+ export {};
@@ -0,0 +1,293 @@
1
+ // A fly-to move over the Earth, for `flyTo` and `goToPlace`: the point under the camera goes along a great circle and the height along a log scale, each a quintic that starts with the speed and
2
+ // the turn the camera has now, so a new move bends the running one instead of stopping it. Pure.
3
+ import { EARTH_RADIUS_KM } from "./ephemeris.js";
4
+ /**
5
+ * The height a descent stops at when the page names none, in kilometres: the whole of a city in view, still sharp with the finer 30 m pictures of the land.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { FLY_HEIGHT_KM } from "@johnmorrisdotca/chikyu-earth/space";
10
+ *
11
+ * console.log(FLY_HEIGHT_KM); // 30
12
+ * ```
13
+ */
14
+ export const FLY_HEIGHT_KM = 30;
15
+ /**
16
+ * The lowest height a descent goes to, in kilometres: the camera stays above the clouds' shell.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * import { FLY_MIN_KM } from "@johnmorrisdotca/chikyu-earth/space";
21
+ *
22
+ * console.log(FLY_MIN_KM); // 15
23
+ * ```
24
+ */
25
+ export const FLY_MIN_KM = 15;
26
+ /**
27
+ * The height a descent stops at where no finer picture lies under it (the sea, Antarctica, the far north, or no `tiles-finer`), in kilometres: as low as the package's own pictures stay sharp.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * import { FLY_FLOOR_KM } from "@johnmorrisdotca/chikyu-earth/space";
32
+ *
33
+ * console.log(FLY_FLOOR_KM); // 400
34
+ * ```
35
+ */
36
+ export const FLY_FLOOR_KM = 400;
37
+ const RAD = Math.PI / 180;
38
+ const dot = (p, q) => p[0] * q[0] + p[1] * q[1] + p[2] * q[2];
39
+ const cross = (p, q) => [p[1] * q[2] - p[2] * q[1], p[2] * q[0] - p[0] * q[2], p[0] * q[1] - p[1] * q[0]];
40
+ const scale = (p, k) => [p[0] * k, p[1] * k, p[2] * k];
41
+ const unit = (p) => {
42
+ const length = Math.hypot(p[0], p[1], p[2]);
43
+ return length > 0 ? scale(p, 1 / length) : [0, 0, 1];
44
+ };
45
+ /** `p` less its part along the unit vector `n`: what lies in the plane that touches the sphere at `n`. */
46
+ const along = (p, n) => {
47
+ const k = dot(p, n);
48
+ return [p[0] - n[0] * k, p[1] - n[1] * k, p[2] - n[2] * k];
49
+ };
50
+ // The quintic's three parts on 0 to 1: `ease` goes from 0 to 1 still at both ends, `push` starts with a slope of 1 and `bend` with a second slope of 1, both ending flat at 0.
51
+ const ease = (s) => s * s * s * (10 - 15 * s + 6 * s * s);
52
+ const push = (s) => s - 6 * s ** 3 + 8 * s ** 4 - 3 * s ** 5;
53
+ const bend = (s) => 0.5 * s * s - 1.5 * s ** 3 + 1.5 * s ** 4 - 0.5 * s ** 5;
54
+ /**
55
+ * The unit vector of a longitude and latitude in degrees, in the Earth's axes (x toward longitude 90, y north, z toward longitude 0).
56
+ *
57
+ * @param lon - Degrees east.
58
+ * @param lat - Degrees north.
59
+ * @returns The unit vector.
60
+ *
61
+ * @example
62
+ * ```ts
63
+ * import { flyAim } from "@johnmorrisdotca/chikyu-earth/space";
64
+ *
65
+ * console.log(flyAim(0, 90).map((n) => Math.round(n))); // [0, 1, 0]
66
+ * ```
67
+ */
68
+ export function flyAim(lon, lat) {
69
+ const c = Math.cos(lat * RAD);
70
+ return [c * Math.sin(lon * RAD), Math.sin(lat * RAD), c * Math.cos(lon * RAD)];
71
+ }
72
+ /**
73
+ * The longitude and latitude in degrees of a unit vector in the Earth's axes, the inverse of `flyAim`.
74
+ *
75
+ * @param aim - The unit vector.
76
+ * @returns `{ lon, lat }`.
77
+ *
78
+ * @example
79
+ * ```ts
80
+ * import { flyAim, flyLonLat } from "@johnmorrisdotca/chikyu-earth/space";
81
+ *
82
+ * console.log(flyLonLat(flyAim(139.69, 35.69)));
83
+ * ```
84
+ */
85
+ export function flyLonLat(aim) {
86
+ return { lon: Math.atan2(aim[0], aim[2]) / RAD, lat: Math.asin(Math.max(-1, Math.min(1, aim[1]))) / RAD };
87
+ }
88
+ /**
89
+ * How long the turn across takes, in milliseconds: 900 for a short way, up to 2,400 for the far side of the Earth.
90
+ *
91
+ * @param angle - The angle between the two points, in radians.
92
+ * @returns Milliseconds.
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * import { flyAcrossMs } from "@johnmorrisdotca/chikyu-earth/space";
97
+ *
98
+ * console.log(flyAcrossMs(0), flyAcrossMs(Math.PI)); // 900 2400
99
+ * ```
100
+ */
101
+ export const flyAcrossMs = (angle) => 900 + 1500 * Math.min(1, Math.abs(angle) / Math.PI);
102
+ /**
103
+ * How long a descent takes, in milliseconds: 1,200 and half a second more for each time the height is divided by e, at most 5 seconds.
104
+ *
105
+ * @param fromKm - The height it starts from.
106
+ * @param toKm - The height it ends at.
107
+ * @returns Milliseconds.
108
+ *
109
+ * @example
110
+ * ```ts
111
+ * import { flyDownMs } from "@johnmorrisdotca/chikyu-earth/space";
112
+ *
113
+ * console.log(Math.round(flyDownMs(20_000, 30)));
114
+ * ```
115
+ */
116
+ export const flyDownMs = (fromKm, toKm) => Math.min(5000, 1200 + 500 * Math.abs(Math.log(Math.max(1e-3, fromKm) / Math.max(1e-3, toKm))));
117
+ /** The time a height still moving takes to come to rest, in milliseconds: it goes on as far as half its speed over that time, so it slows without turning back. */
118
+ const COAST_MS = 900;
119
+ const turnAt = (turn, t) => {
120
+ const s = Math.max(0, Math.min(1, (t - turn.start) / turn.ms));
121
+ const angle = turn.angle * ease(s);
122
+ const side = cross(turn.axis, turn.from);
123
+ const c = Math.cos(angle);
124
+ const n = Math.sin(angle);
125
+ const k = turn.ms * push(s);
126
+ const k2 = turn.ms * turn.ms * bend(s);
127
+ return unit([
128
+ turn.from[0] * c + side[0] * n + turn.v[0] * k + turn.a[0] * k2,
129
+ turn.from[1] * c + side[1] * n + turn.v[1] * k + turn.a[1] * k2,
130
+ turn.from[2] * c + side[2] * n + turn.v[2] * k + turn.a[2] * k2,
131
+ ]);
132
+ };
133
+ const liftAt = (lifts, t) => {
134
+ let value = lifts[0].from;
135
+ for (const lift of lifts) {
136
+ if (t < lift.start)
137
+ break;
138
+ const s = Math.min(1, (t - lift.start) / lift.ms);
139
+ value = lift.from + (lift.to - lift.from) * ease(s) + lift.v * lift.ms * push(s) + lift.a * lift.ms * lift.ms * bend(s);
140
+ }
141
+ return value;
142
+ };
143
+ /**
144
+ * Where a move has the camera at a moment of its clock. Before it starts it is where it began; after it ends, where it ended.
145
+ *
146
+ * @param plan - What `planFly` returned.
147
+ * @param t - The moment, in milliseconds of the clock the plan was made with.
148
+ * @returns The point under the camera and its height.
149
+ *
150
+ * @example
151
+ * ```ts
152
+ * import { flyAt, flyAim, flyLonLat, planFly } from "@johnmorrisdotca/chikyu-earth/space";
153
+ *
154
+ * const plan = planFly({ aim: flyAim(0, 0), v: [0, 0, 0], a: [0, 0, 0], height: Math.log(20_000), hv: 0, ha: 0 }, flyAim(139.69, 35.69), { at: 0, phase: "both", heightKm: 30 });
155
+ * const end = flyAt(plan, plan.end);
156
+ * console.log(flyLonLat(end.aim), Math.round(end.heightKm)); // Tokyo, 30
157
+ * ```
158
+ */
159
+ export function flyAt(plan, t) {
160
+ return { aim: turnAt(plan.turn, t), heightKm: Math.exp(plan.lifts.length > 0 ? liftAt(plan.lifts, t) : plan.height) };
161
+ }
162
+ /** The step the speed and acceleration of a move are read over, in milliseconds. */
163
+ const STEP_MS = 0.5;
164
+ /**
165
+ * Where a move has the camera at a moment and how it is going there: what a new move starts from, so that it carries on with the same speed and turn.
166
+ *
167
+ * @param plan - What `planFly` returned.
168
+ * @param t - The moment, in milliseconds of the plan's clock.
169
+ * @returns The state, with its speeds and accelerations a millisecond.
170
+ *
171
+ * @example
172
+ * ```ts
173
+ * import { flyAim, flyStateAt, planFly } from "@johnmorrisdotca/chikyu-earth/space";
174
+ *
175
+ * const plan = planFly({ aim: flyAim(0, 0), v: [0, 0, 0], a: [0, 0, 0], height: Math.log(1000), hv: 0, ha: 0 }, flyAim(90, 0), { at: 0, phase: "across" });
176
+ * const halfway = flyStateAt(plan, plan.end / 2);
177
+ * console.log(Math.hypot(...halfway.v) > 0);
178
+ * ```
179
+ */
180
+ export function flyStateAt(plan, t) {
181
+ // read forward, so a move that has only just begun is read from its own start, not the moment before it
182
+ const p0 = flyAt(plan, t);
183
+ const p1 = flyAt(plan, t + STEP_MS);
184
+ const p2 = flyAt(plan, t + 2 * STEP_MS);
185
+ const aim = p0.aim;
186
+ const v = [0, 1, 2].map((i) => (-3 * p0.aim[i] + 4 * p1.aim[i] - p2.aim[i]) / (2 * STEP_MS));
187
+ const a = [0, 1, 2].map((i) => (p0.aim[i] - 2 * p1.aim[i] + p2.aim[i]) / (STEP_MS * STEP_MS));
188
+ const [h0, h1, h2] = [p0, p1, p2].map((p) => Math.log(p.heightKm));
189
+ return { aim, v: along(v, aim), a: along(a, aim), height: h0, hv: (-3 * h0 + 4 * h1 - h2) / (2 * STEP_MS), ha: (h0 - 2 * h1 + h2) / (STEP_MS * STEP_MS) };
190
+ }
191
+ /**
192
+ * The state of a camera that is not moving: over `aim` at `heightKm`.
193
+ *
194
+ * @param aim - The unit vector of the point under the camera.
195
+ * @param heightKm - Its height in kilometres.
196
+ * @returns The state, with no speed.
197
+ *
198
+ * @example
199
+ * ```ts
200
+ * import { flyAim, flyStill } from "@johnmorrisdotca/chikyu-earth/space";
201
+ *
202
+ * console.log(flyStill(flyAim(0, 0), 1000).hv); // 0
203
+ * ```
204
+ */
205
+ export const flyStill = (aim, heightKm) => ({ aim: unit(aim), v: [0, 0, 0], a: [0, 0, 0], height: Math.log(Math.max(1e-3, heightKm)), hv: 0, ha: 0 });
206
+ /**
207
+ * Plan a move from where the camera is and how it is going (`now`) to the point `to`. The turn starts with the camera's own speed and turn and curves to the place along a great circle, slowing to rest over
208
+ * it; the height does the same on a log scale. A height still moving when the move is planned (a descent that a new move cuts short) slows to rest first, without turning back. `across` keeps the height,
209
+ * `down` comes to `heightKm` at once (the turn, if any, beside it), `both` comes down once the turn is over.
210
+ *
211
+ * @param now - Where the camera is and how it is going: `flyStateAt` of the move running, else `flyStill`.
212
+ * @param to - The unit vector of the place.
213
+ * @param options - `at`, the moment on the move's clock; `phase` (`both` by default); `heightKm`, the height to come down to (`FLY_HEIGHT_KM` by default); `acrossMs`, the time of the turn (`flyAcrossMs` of its angle by default).
214
+ * @returns The plan, for `flyAt` and `flyStateAt`.
215
+ *
216
+ * @example
217
+ * ```ts
218
+ * import { flyAim, flyStateAt, planFly } from "@johnmorrisdotca/chikyu-earth/space";
219
+ *
220
+ * const start = { aim: flyAim(0, 0), v: [0, 0, 0] as [number, number, number], a: [0, 0, 0] as [number, number, number], height: Math.log(20_000), hv: 0, ha: 0 };
221
+ * const toTokyo = planFly(start, flyAim(139.69, 35.69), { at: 0 });
222
+ * // half way, the reader asks for London: the new move starts with the speed the first one has
223
+ * const toLondon = planFly(flyStateAt(toTokyo, 800), flyAim(-0.13, 51.51), { at: 800 });
224
+ * console.log(toLondon.end > 800);
225
+ * ```
226
+ */
227
+ export function planFly(now, to, options) {
228
+ const at = options.at;
229
+ const phase = options.phase ?? "both";
230
+ const from = unit(now.aim);
231
+ const target = unit(to);
232
+ const angle = Math.acos(Math.max(-1, Math.min(1, dot(from, target))));
233
+ // the great circle's axis; for two points opposite each other any axis will do, and the way the camera is already going is the one that bends least
234
+ let axis = cross(from, target);
235
+ if (Math.hypot(...axis) < 1e-9)
236
+ axis = cross(from, Math.hypot(...now.v) > 1e-12 ? unit(now.v) : Math.abs(from[1]) < 0.9 ? [0, 1, 0] : [1, 0, 0]);
237
+ const moving = Math.hypot(...now.v) > 1e-12 || Math.hypot(...now.a) > 1e-12;
238
+ const acrossMs = angle < 1e-9 && !moving ? 0 : (options.acrossMs ?? flyAcrossMs(angle));
239
+ const turn = { start: at, ms: Math.max(1, acrossMs), from, to: target, axis: unit(axis), angle, v: along(now.v, from), a: along(now.a, from) };
240
+ const lifts = [];
241
+ let held = now.height;
242
+ let free = at;
243
+ // a height still moving, with no descent to carry on in: it slows to rest first, going on half as far as its speed would carry it over that time
244
+ if (phase !== "down" && (Math.abs(now.hv) > 1e-9 || Math.abs(now.ha) > 1e-9)) {
245
+ held = now.height + 0.5 * now.hv * COAST_MS;
246
+ lifts.push({ start: at, ms: COAST_MS, from: now.height, to: held, v: now.hv, a: now.ha });
247
+ free = at + COAST_MS;
248
+ }
249
+ const across = at + acrossMs;
250
+ if (phase !== "across") {
251
+ const goal = Math.log(Math.max(1e-3, options.heightKm ?? FLY_HEIGHT_KM));
252
+ if (phase === "down")
253
+ lifts.push({ start: at, ms: flyDownMs(Math.exp(now.height), Math.exp(goal)), from: now.height, to: goal, v: now.hv, a: now.ha });
254
+ else if (Math.abs(goal - held) > 1e-9) {
255
+ const start = Math.max(across, free);
256
+ lifts.push({ start, ms: flyDownMs(Math.exp(held), Math.exp(goal)), from: held, to: goal, v: 0, a: 0 });
257
+ }
258
+ }
259
+ const last = lifts[lifts.length - 1];
260
+ return { turn, lifts, height: now.height, across, end: Math.max(across, last ? last.start + last.ms : at) };
261
+ }
262
+ /**
263
+ * The height a descent over a place is held at, in kilometres, or `null` when nothing holds it: a descent comes all the way down only where the finer pictures lie (`tiles-finer` on, over land, from 60
264
+ * degrees south to 80 degrees north, as the survey of the layer found), and stops at `FLY_FLOOR_KM` elsewhere, as low as the package's own pictures stay sharp.
265
+ *
266
+ * @param place - `finer`, whether finer pictures are asked for; `land`, whether the place is on land or near its coast; `lat`, its latitude.
267
+ * @returns The height to hold at, or `null`.
268
+ *
269
+ * @example
270
+ * ```ts
271
+ * import { descentFloorKm } from "@johnmorrisdotca/chikyu-earth/space";
272
+ *
273
+ * console.log(descentFloorKm({ finer: true, land: true, lat: 35.69 })); // null: down to the height asked for
274
+ * console.log(descentFloorKm({ finer: true, land: false, lat: 30 })); // 400: the sea
275
+ * ```
276
+ */
277
+ export function descentFloorKm(place) {
278
+ return place.finer && place.land && place.lat >= -60 && place.lat <= 80 ? null : FLY_FLOOR_KM;
279
+ }
280
+ /**
281
+ * The camera's distance from the Earth's centre, in Earth radii, for a height above the ground in kilometres.
282
+ *
283
+ * @param heightKm - The height.
284
+ * @returns The distance.
285
+ *
286
+ * @example
287
+ * ```ts
288
+ * import { flyDistance } from "@johnmorrisdotca/chikyu-earth/space";
289
+ *
290
+ * console.log(flyDistance(6371)); // 2
291
+ * ```
292
+ */
293
+ export const flyDistance = (heightKm) => 1 + heightKm / EARTH_RADIUS_KM;
@@ -10,3 +10,4 @@ export { BEAM_ATTRIBUTES, BEAM_COLOUR, BEAM_ELEVATION, beamCells, footprintAngle
10
10
  export type { BeamStats } from "../beam/layer.ts";
11
11
  export { RIDE_PATH_FADE_KM } from "./path-ribbons.ts";
12
12
  export { createTrailBuilder, TRAIL_DEFAULT_ORBITS, TRAIL_MAX_ORBITS, TRAIL_MAX_POINTS, TRAIL_POINTS_PER_ORBIT, trailOrbits, type PlaceAt, type TrailBuilder } from "./trail.ts";
13
+ export { descentFloorKm, FLY_FLOOR_KM, FLY_HEIGHT_KM, FLY_MIN_KM, flyAcrossMs, flyAim, flyAt, flyDistance, flyDownMs, flyLonLat, flyStateAt, flyStill, planFly, type FlyPhase, type FlyPlan, type FlyPoint, type FlyState } from "./fly.ts";
@@ -11,3 +11,4 @@ export { genericSatellite, satelliteKind } from "./generic-satellite.js";
11
11
  export { BEAM_ATTRIBUTES, BEAM_COLOUR, BEAM_ELEVATION, beamCells, footprintAngle, footprintKm, readBeam } from "../beam/geometry.js";
12
12
  export { RIDE_PATH_FADE_KM } from "./path-ribbons.js";
13
13
  export { createTrailBuilder, TRAIL_DEFAULT_ORBITS, TRAIL_MAX_ORBITS, TRAIL_MAX_POINTS, TRAIL_POINTS_PER_ORBIT, trailOrbits } from "./trail.js";
14
+ export { descentFloorKm, FLY_FLOOR_KM, FLY_HEIGHT_KM, FLY_MIN_KM, flyAcrossMs, flyAim, flyAt, flyDistance, flyDownMs, flyLonLat, flyStateAt, flyStill, planFly } from "./fly.js";
@@ -176,6 +176,9 @@ export interface SpaceModel {
176
176
  airThickness: number;
177
177
  /** Degrees an hour the cloud picture is made to move eastward against the ground: 0, the default (it is one snapshot, not a forecast). */
178
178
  cloudDrift: number;
179
+ /** The texel width in device pixels at which the clouds are gone (`cloud-fade`), 0 for no fade; and the share of the clouds shown at the last draw. */
180
+ cloudFade: number;
181
+ cloudShown: number;
179
182
  /** For the `space` camera: the Earth's turn against the stars (degrees) at the moment the camera was set, so it stays in the stars while the Earth turns beneath; null until the next draw sets it. */
180
183
  cameraSpin: number | null;
181
184
  /** How much of the Earth's turn the space camera takes: 0 holds the stars, 1 turns with the Earth. */
@@ -188,6 +191,8 @@ export interface SpaceModel {
188
191
  };
189
192
  /** The camera's distance at zoom 1 in radii of the body it looks at, as the last draw worked it out from the view's shape and `earth.size`. */
190
193
  fit: number;
194
+ /** The nearest the camera comes to the Earth's centre while it looks at the Earth, in Earth radii: 1.05 (about 320 km up), less once a fly-to has come lower. */
195
+ nearest: number;
191
196
  orbits: boolean;
192
197
  labels: boolean;
193
198
  /** Milliseconds of game time one second of wall time covers (the ephemeris is looked up once per such span). */