@johnmorrisdotca/wakusei-planets 0.35.0 → 0.37.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.
Files changed (49) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/NOTICE.md +31 -1
  3. package/README.md +5 -4
  4. package/dist/data/moon-catalogue.d.ts +31 -0
  5. package/dist/data/moon-catalogue.js +471 -0
  6. package/dist/data/orbiters.d.ts +368 -0
  7. package/dist/data/orbiters.js +370 -0
  8. package/dist/data/places-planned.d.ts +3 -0
  9. package/dist/data/places-planned.js +51 -0
  10. package/dist/element.d.ts +28 -0
  11. package/dist/element.js +172 -1
  12. package/dist/moon-catalogue.d.ts +296 -0
  13. package/dist/moon-catalogue.js +234 -0
  14. package/dist/orbiter-facts.d.ts +480 -0
  15. package/dist/orbiter-facts.js +189 -0
  16. package/dist/orbiters.d.ts +256 -0
  17. package/dist/orbiters.js +275 -0
  18. package/dist/place-marks.d.ts +182 -0
  19. package/dist/place-marks.js +194 -0
  20. package/dist/places/mars.d.ts +1 -1
  21. package/dist/places/mars.js +4 -2
  22. package/dist/places/moon.d.ts +1 -1
  23. package/dist/places/moon.js +4 -2
  24. package/dist/places.d.ts +4 -2
  25. package/dist/places.js +2 -0
  26. package/dist/scene/place-layer.d.ts +68 -0
  27. package/dist/scene/place-layer.js +213 -0
  28. package/dist/scene/textures.d.ts +2 -2
  29. package/dist/space/all-moons.d.ts +30 -0
  30. package/dist/space/all-moons.js +61 -0
  31. package/dist/space/bodies.d.ts +18 -0
  32. package/dist/space/bodies.js +18 -0
  33. package/dist/space/element.d.ts +182 -6
  34. package/dist/space/element.js +763 -26
  35. package/dist/space/frame.d.ts +14 -0
  36. package/dist/space/frame.js +16 -0
  37. package/dist/space/index.d.ts +5 -2
  38. package/dist/space/index.js +5 -2
  39. package/dist/space/regions.d.ts +111 -0
  40. package/dist/space/regions.js +84 -0
  41. package/dist/space/scene.d.ts +131 -8
  42. package/dist/space/scene.js +312 -15
  43. package/dist/space/sight.d.ts +106 -0
  44. package/dist/space/sight.js +84 -0
  45. package/dist/space/travel.d.ts +256 -0
  46. package/dist/space/travel.js +597 -0
  47. package/dist/version.d.ts +1 -1
  48. package/dist/version.js +1 -1
  49. package/package.json +13 -2
@@ -0,0 +1,597 @@
1
+ // The camera's move from one body to another, as one pure function of time: the two views and the bodies' places in, the camera at any moment out. No clock, no DOM, no state, so a test can walk the
2
+ // whole move without a browser. The move is the one a camera operator makes: TURN (or back up) beside the body being left until the destination is in the picture, APPROACH it in a straight line with it
3
+ // in view the whole way, and ARRIVE by settling it into its framed view.
4
+ //
5
+ // How the path is built (`turn-travel`, the default):
6
+ // 1. The turn pivots about one body, which is held in the picture by its place on the screen and its distance: the body being left, or the planet when both ends are of one planet's family (`around`).
7
+ // The look direction swings along the one shortest arc from the first view's to the last view's, and the roll about it is spread evenly, so no turn is made that is not needed and none is undone.
8
+ // 2. Where the last view's own direction can see the destination from beside the pivot, the whole turn is made there and the rest is one straight line with the camera's direction fixed: whatever is in
9
+ // the picture at both ends of a straight line stays in it between them, so the destination, and its planet for a moon, never leave.
10
+ // 3. Where it cannot (the last view looks at the destination from another side), the turn stops with the destination just inside the picture, the camera goes straight at it, and the rest of the turn
11
+ // is made round the destination over the last stretch, with the destination held in the picture by its place on the screen.
12
+ // 4. Distance goes geometrically: the camera leaves at a speed in proportion to how near the body left is and arrives at one in proportion to how near the destination is, so both are slow and the
13
+ // middle is fast, whatever the scale. Turning is eased on its own.
14
+ // 5. Every place is then kept clear of every body by a shell round it that the path slides over.
15
+ //
16
+ // The awkward cases, by name:
17
+ // - the destination directly behind the body being left, or the two in one direction: the pivot puts the body left to one side, backing up until the straight line to the destination clears it;
18
+ // - the destination is the Sun, or a planet that is a point until the last stretch (the true scale): its place on the screen is reported at every moment, so a marker can stand for it;
19
+ // - the camera starts inside a beside-a-moon view: the moon, or its planet for a move within the planet's family, is the pivot;
20
+ // - the two views almost the same: no turn stage, a short move;
21
+ // - a view of a region (a set of orbits round a body): a straight pull back or push in with no turn of its own.
22
+ import { add, cross, dot, length, RAD, scale, sub, unit } from "../vec.js";
23
+ /**
24
+ * How near a move may come to a body, in that body's radii above its surface: 1, so a body passed is never more than 60 degrees across. A move that starts or ends nearer than one and a half times that keeps two thirds of the nearer distance.
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * import { TRAVEL_CLEARANCE } from "@johnmorrisdotca/wakusei-planets/space";
29
+ *
30
+ * console.log(TRAVEL_CLEARANCE);
31
+ * ```
32
+ */
33
+ export const TRAVEL_CLEARANCE = 1;
34
+ const clamp01 = (x) => Math.min(1, Math.max(0, x));
35
+ /** Slow out, fast between, slow in, with no jerk at either end. */
36
+ const ease = (x) => {
37
+ const t = clamp01(x);
38
+ return t * t * t * (t * (t * 6 - 15) + 10);
39
+ };
40
+ const smooth = (x) => {
41
+ const t = clamp01(x);
42
+ return t * t * (3 - 2 * t);
43
+ };
44
+ const mix = (a, b, t) => [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t];
45
+ const placeOf = (body, t) => (body.to === undefined ? body.from : mix(body.from, body.to, clamp01(t)));
46
+ const angleOf = (a, b) => Math.atan2(length(cross(a, b)), dot(a, b));
47
+ /** `v` turned about the unit vector `axis` by `angle` radians. */
48
+ function spin(v, axis, angle) {
49
+ const c = Math.cos(angle);
50
+ const s = Math.sin(angle);
51
+ const kv = cross(axis, v);
52
+ const d = dot(axis, v) * (1 - c);
53
+ return [v[0] * c + kv[0] * s + axis[0] * d, v[1] * c + kv[1] * s + axis[1] * d, v[2] * c + kv[2] * s + axis[2] * d];
54
+ }
55
+ /** Any unit vector square to `f`. */
56
+ function squareTo(f) {
57
+ const pick = Math.abs(f[1]) < 0.9 ? [0, 1, 0] : [1, 0, 0];
58
+ return unit(sub(pick, scale(f, dot(pick, f))));
59
+ }
60
+ function facingOf(view) {
61
+ const f = length(view.forward) < 1e-12 ? [0, 0, -1] : unit(view.forward);
62
+ const across = sub(view.up, scale(f, dot(view.up, f)));
63
+ return { f, u: length(across) < 1e-9 ? squareTo(f) : unit(across) };
64
+ }
65
+ /** A vector of the scene in the camera's own axes: x to the picture's right, y up, z along the line of sight. */
66
+ function toCamera(o, w) {
67
+ return [dot(w, cross(o.f, o.u)), dot(w, o.u), dot(w, o.f)];
68
+ }
69
+ function toScene(o, v) {
70
+ const r = cross(o.f, o.u);
71
+ return [r[0] * v[0] + o.u[0] * v[1] + o.f[0] * v[2], r[1] * v[0] + o.u[1] * v[1] + o.f[1] * v[2], r[2] * v[0] + o.u[2] * v[1] + o.f[2] * v[2]];
72
+ }
73
+ const frameOf = (fov, aspect) => {
74
+ const tv = Math.tan((Math.min(170, Math.max(1, fov)) / 2) * RAD);
75
+ return { th: tv * Math.max(1e-3, aspect), tv };
76
+ };
77
+ /** A field of view between two, even in the size things are drawn at. */
78
+ const fovBetween = (a, b, t) => (a === b ? a : (2 * Math.atan(Math.tan((a / 2) * RAD) + (Math.tan((b / 2) * RAD) - Math.tan((a / 2) * RAD)) * t)) / RAD);
79
+ const within = (v, frame, margin) => v[2] > 0 && Math.abs(v[0]) <= margin * frame.th * v[2] && Math.abs(v[1]) <= margin * frame.tv * v[2];
80
+ /** The direction inside the picture (by `margin` of its half width and height) nearest to `v`, in the camera's axes. */
81
+ function intoFrame(v, frame, margin) {
82
+ const mh = margin * frame.th;
83
+ const mv = margin * frame.tv;
84
+ if (v[2] > 1e-9 * (Math.abs(v[0]) + Math.abs(v[1]) + Math.abs(v[2])))
85
+ return unit([Math.min(mh, Math.max(-mh, v[0] / v[2])), Math.min(mv, Math.max(-mv, v[1] / v[2])), 1]);
86
+ // beside or behind the camera: the edge on the side it is off
87
+ const k = Math.max(Math.abs(v[0]) / mh, Math.abs(v[1]) / mv);
88
+ return k < 1e-12 ? unit([mh, 0, 1]) : unit([v[0] / k, v[1] / k, 1]);
89
+ }
90
+ /** One direction turned to another along the shortest arc. */
91
+ function arc(a, b, t) {
92
+ const angle = angleOf(a, b);
93
+ if (angle < 1e-9)
94
+ return a;
95
+ const axis = cross(a, b);
96
+ return spin(a, length(axis) < 1e-9 ? squareTo(a) : unit(axis), angle * t);
97
+ }
98
+ function swingOf(a, b) {
99
+ const c = cross(a.f, b.f);
100
+ const angle = angleOf(a.f, b.f);
101
+ // looking the opposite way has no shortest arc: turn about the picture's own up, as a head turns
102
+ const axis = length(c) < 1e-9 ? a.u : unit(c);
103
+ const carried = spin(a.u, axis, angle);
104
+ return { from: a, axis, angle: angle < 1e-9 ? 0 : angle, roll: Math.atan2(dot(cross(carried, b.u), b.f), dot(carried, b.u)) };
105
+ }
106
+ function swingAt(s, t) {
107
+ const f = unit(spin(s.from.f, s.axis, s.angle * t));
108
+ const carried = spin(s.from.u, s.axis, s.angle * t);
109
+ const rolled = spin(carried, f, s.roll * t);
110
+ return { f, u: unit(sub(rolled, scale(f, dot(rolled, f)))) };
111
+ }
112
+ const swingDegrees = (s) => (s.angle + Math.abs(s.roll)) / RAD;
113
+ /** The nearest point to `p` that is inside two pictures at once: the pivot inside the frame (`fv`), and the camera backed off from the last view inside the frame (`fb`, about `apex`). Dykstra's projections on the eight planes. */
114
+ function project(p, apex, fv, mv, fb, mb) {
115
+ const planes = [];
116
+ const cone = (frame, margin, at) => {
117
+ for (const n of [[1, 0, -margin * frame.th], [-1, 0, -margin * frame.th], [0, 1, -margin * frame.tv], [0, -1, -margin * frame.tv]]) {
118
+ const nn = unit(n);
119
+ planes.push({ n: nn, c: dot(nn, at) });
120
+ }
121
+ };
122
+ cone(fv, mv, [0, 0, 0]);
123
+ cone(fb, mb, apex);
124
+ const kept = planes.map(() => [0, 0, 0]);
125
+ let x = p;
126
+ for (let round = 0; round < 80; round += 1) {
127
+ for (let i = 0; i < planes.length; i += 1) {
128
+ const y = add(x, kept[i]);
129
+ const over = dot(planes[i].n, y) - planes[i].c;
130
+ const next = over > 0 ? sub(y, scale(planes[i].n, over)) : y;
131
+ kept[i] = sub(y, next);
132
+ x = next;
133
+ }
134
+ }
135
+ return x;
136
+ }
137
+ /** How near the straight way from `a` to `b` comes to `x`. */
138
+ function missOf(a, b, x) {
139
+ const ab = sub(b, a);
140
+ const l2 = dot(ab, ab);
141
+ const t = l2 < 1e-30 ? 0 : clamp01(dot(sub(x, a), ab) / l2);
142
+ return length(sub(x, add(a, scale(ab, t))));
143
+ }
144
+ /** Directions inside the picture to try for the pivot, nearest to `first` first. */
145
+ function spots(first, frame, margin) {
146
+ const grid = [];
147
+ for (const i of [-1, -0.5, 0, 0.5, 1])
148
+ for (const j of [-1, -0.5, 0, 0.5, 1])
149
+ grid.push(unit([i * margin * frame.th, j * margin * frame.tv, 1]));
150
+ grid.sort((a, b) => angleOf(a, first) - angleOf(b, first));
151
+ return [first, ...grid];
152
+ }
153
+ /** Distance left to go, of `l`, at `q` from 0 to 1: it grows geometrically out of the start (from a length `ea`) and shrinks geometrically into the end (to a length `eb`), so both are slow and the middle is fast. */
154
+ function leftOf(q, l, ea, eb) {
155
+ if (l <= 0)
156
+ return 0;
157
+ const gone = (ea * Math.expm1(q * Math.log1p(l / ea))) / l;
158
+ const left = (eb * Math.expm1((1 - q) * Math.log1p(l / eb))) / l;
159
+ const m = smooth(q);
160
+ return l * ((1 - m) * (1 - gone) + m * left);
161
+ }
162
+ const LOOK_BACK_TURN = 0.4;
163
+ function turnAt(turn, p, t) {
164
+ const o = swingAt(turn.swing, p);
165
+ // the lift is early: the camera backs off as it starts to turn, so the body left goes into the picture, not out of its side
166
+ const w = Math.sqrt(p);
167
+ const d = Math.exp(Math.log(turn.d0) + (Math.log(turn.d1) - Math.log(turn.d0)) * p + turn.lift * 4 * w * (1 - w));
168
+ const position = sub(placeOf(turn.pivot, t), scale(toScene(o, arc(turn.u0, turn.u1, p)), d));
169
+ if (turn.hold === undefined)
170
+ return { position, o, fov: fovBetween(turn.fov0, turn.fov1, p) };
171
+ // the turn about the body left, kept where it was in the picture, handing over to the circle of the pivot
172
+ const near = sub(placeOf(turn.hold.body, t), scale(toScene(o, turn.hold.u), turn.hold.d * (1 + (turn.hold.far - 1) * smooth(p / turn.hold.q))));
173
+ return { position: mix(near, position, smooth(p / turn.hold.q)), o, fov: fovBetween(turn.fov0, turn.fov1, p) };
174
+ }
175
+ function legAt(leg, q, t) {
176
+ const centre = placeOf(leg.about, t);
177
+ if (leg.type === "line") {
178
+ const left = leftOf(q, leg.l, leg.ea, leg.eb);
179
+ const position = add(centre, leg.l <= 0 ? leg.to : add(leg.to, scale(sub(leg.from, leg.to), left / leg.l)));
180
+ let o = swingAt(leg.swing, q);
181
+ if (leg.watch) {
182
+ // look-back: turn to the body being left, hold it, and turn to the last view over the last part
183
+ const toBody = sub(placeOf(leg.watch, t), position);
184
+ const held = length(toBody) < 1e-12 ? leg.swing.from : swingAt(swingOf(leg.swing.from, { f: unit(toBody), u: leg.swing.from.u }), 1);
185
+ const watching = swingAt(swingOf(leg.swing.from, held), smooth(q / 0.15));
186
+ o = swingAt(swingOf(watching, swingAt(leg.swing, 1)), smooth((q - (1 - LOOK_BACK_TURN)) / LOOK_BACK_TURN));
187
+ }
188
+ return { position, o, fov: fovBetween(leg.fov0, leg.fov1, q) };
189
+ }
190
+ const d = leg.l > 0 ? leg.d1 + leftOf(q, leg.l, leg.ea, leg.eb) : Math.exp(Math.log(leg.d0) + (Math.log(leg.d1) - Math.log(leg.d0)) * q);
191
+ const k = leg.l > 0 && leg.dn > leg.d1 * 1.0001 ? smooth(Math.log(leg.dn / d) / Math.log(leg.dn / leg.d1)) : q;
192
+ const o = swingAt(leg.swing, k);
193
+ return { position: sub(centre, scale(toScene(o, arc(leg.u0, leg.u1, k)), d)), o, fov: fovBetween(leg.fov0, leg.fov1, k) };
194
+ }
195
+ /** A place kept clear of every body: inside one and a half times a body's clear distance the place is moved out along the line from the body's centre, smoothly, so a path slides over a shell and never goes in. */
196
+ function clearOf(route, position, t) {
197
+ let p = position;
198
+ for (const { body, clear } of route.shells) {
199
+ if (clear <= 0)
200
+ continue;
201
+ const centre = placeOf(body, t);
202
+ const away = sub(p, centre);
203
+ const d = length(away);
204
+ if (d >= 1.5 * clear)
205
+ continue;
206
+ const out = d < 1e-12 ? squareTo(route.leg.swing.from.f) : scale(away, 1 / d);
207
+ // from the clear distance at the centre to one and a half of it, where it meets the untouched path with no corner
208
+ p = add(centre, scale(out, clear + (4 * d * d * d) / (27 * clear * clear)));
209
+ }
210
+ return p;
211
+ }
212
+ function along(route, t) {
213
+ const inTurn = route.turn !== null && t < route.share;
214
+ const at = inTurn ? turnAt(route.turn, ease(t / route.share), t) : legAt(route.leg, ease(route.share >= 1 ? 1 : (t - route.share) / (1 - route.share)), t);
215
+ return { position: clearOf(route, at.position, t), o: at.o, fov: at.fov, stage: inTurn ? "turn" : t < route.arrive ? "travel" : "arrive" };
216
+ }
217
+ function sightOf(body, t, position, o, frame) {
218
+ const v = toCamera(o, sub(placeOf(body, t), position));
219
+ const d = length(v);
220
+ const z = Math.abs(v[2]) < 1e-12 * Math.max(1, d) ? 1e-12 * Math.max(1, d) : Math.abs(v[2]);
221
+ const x = v[0] / (z * frame.th);
222
+ const y = v[1] / (z * frame.tv);
223
+ return { inside: v[2] > 0 && Math.abs(x) <= 1 && Math.abs(y) <= 1, x, y, radius: d <= body.radius ? 90 : Math.asin(body.radius / d) / RAD };
224
+ }
225
+ /** The pivot's stand at the end of the turn, for a moment `tm` of the move. */
226
+ function standAt(input, tm, family) {
227
+ const o0 = facingOf(input.start);
228
+ const o1 = facingOf(input.end);
229
+ const clearance = input.clearance ?? TRAVEL_CLEARANCE;
230
+ const pivot = family ? input.around : input.leaving;
231
+ const b = placeOf(input.destination, tm);
232
+ const v = placeOf(pivot, tm);
233
+ const e1 = sub(input.end.position, placeOf(input.destination, 1));
234
+ const d1 = Math.max(1e-12, length(e1));
235
+ const last = add(b, e1);
236
+ const fromPivot = sub(pivot.from, input.start.position);
237
+ const d0 = Math.max(1e-12, length(fromPivot));
238
+ const u0 = unit(toCamera(o0, fromPivot));
239
+ const frame0 = frameOf(input.start.fov, input.aspect);
240
+ const frame1 = frameOf(input.end.fov, input.aspect);
241
+ const whole = swingOf(o0, o1);
242
+ const still = swingOf(o1, o1);
243
+ const tries = [1, 1.6, 2.5, 4, 6.5];
244
+ // the whole turn at the pivot, then one straight line: the camera backs off from the last view inside its own picture, to a place that sees the pivot too
245
+ const apex = toCamera(o1, sub(v, last));
246
+ const first = intoFrame(toCamera(o1, toScene(o0, u0)), frame1, 0.85);
247
+ let straight = null;
248
+ let fallback = null;
249
+ // first choice: back off along the line of sight to the destination, so it keeps its place on the screen all the way in
250
+ const sight = unit(toCamera(o1, scale(e1, -1)));
251
+ const lines = [];
252
+ for (const far of tries) {
253
+ const along = dot(apex, sight);
254
+ const root = along * along - dot(apex, apex) + far * d0 * (far * d0);
255
+ if (root >= 0 && Math.sqrt(root) - along > 0)
256
+ lines.push(add(apex, scale(sight, Math.sqrt(root) - along)));
257
+ }
258
+ search: for (const far of tries) {
259
+ for (const spot of [...(far === tries[0] ? lines : []), ...spots(first, frame1, 0.85)]) {
260
+ const fixed = lines.includes(spot);
261
+ const wanted = fixed ? spot : scale(spot, far * d0);
262
+ const p = fixed ? spot : project(wanted, apex, frame1, 0.9, frame1, 0.8);
263
+ if (!fixed)
264
+ fallback ??= p;
265
+ const size = length(p);
266
+ if (size < 1e-12 || (!family && length(sub(p, wanted)) > 0.4 * far * d0))
267
+ continue;
268
+ const stand = sub(v, toScene(o1, p));
269
+ if (!within(p, frame1, 0.93) || !within(toCamera(o1, sub(b, stand)), frame1, 0.93))
270
+ continue;
271
+ const need = Math.min(1.52 * (1 + clearance) * pivot.radius, 0.9 * size, 0.98 * length(apex));
272
+ if (missOf(stand, last, v) < need)
273
+ continue;
274
+ straight = p;
275
+ break search;
276
+ }
277
+ }
278
+ if (straight === null && family)
279
+ straight = fallback;
280
+ if (straight !== null) {
281
+ const dm = Math.max(1e-12, length(straight));
282
+ const stand = sub(v, toScene(o1, straight));
283
+ const from = sub(stand, b);
284
+ const l = length(sub(e1, from));
285
+ const leaving = placeOf(input.leaving, tm);
286
+ return {
287
+ turn: { pivot, swing: whole, u0, u1: scale(straight, 1 / dm), d0, d1: dm, lift: 0, fov0: input.start.fov, fov1: input.end.fov },
288
+ leg: { about: input.destination, type: "line", swing: still, fov0: input.end.fov, fov1: input.end.fov, from, to: e1, d0: length(from), d1, dn: d1, u0, u1: u0, l, ea: Math.max(1e-9, Math.min(dm, length(sub(leaving, stand)))), eb: d1 },
289
+ };
290
+ }
291
+ // the last view looks at the destination from another side: turn until the destination is just inside the picture, go straight at it, and make the rest of the turn round it
292
+ const way = unit(sub(b, add(input.start.position, sub(v, pivot.from))));
293
+ let om = null;
294
+ for (let k = 50; k >= 0 && om === null; k -= 1) {
295
+ const o = swingAt(whole, k / 50);
296
+ if (within(toCamera(o, way), frame0, 0.8))
297
+ om = o;
298
+ }
299
+ if (om === null) {
300
+ // the arc never shows the destination: leave it where it comes nearest, by the least turn that brings the destination inside
301
+ let best = o0;
302
+ for (let k = 0; k <= 50; k += 1) {
303
+ const o = swingAt(whole, k / 50);
304
+ if (angleOf(o.f, way) < angleOf(best.f, way))
305
+ best = o;
306
+ }
307
+ const room = Math.atan(0.75 * Math.min(frame0.th, frame0.tv));
308
+ const off = angleOf(best.f, way);
309
+ const axis = length(cross(best.f, way)) < 1e-9 ? best.u : unit(cross(best.f, way));
310
+ om = { f: unit(spin(best.f, axis, Math.max(0, off - room))), u: unit(spin(best.u, axis, Math.max(0, off - room))) };
311
+ }
312
+ const first0 = intoFrame(toCamera(om, toScene(o0, u0)), frame0, 0.85);
313
+ let chosen = null;
314
+ search: for (const far of tries) {
315
+ for (const spot of spots(first0, frame0, 0.85)) {
316
+ const stand = sub(v, scale(toScene(om, spot), far * d0));
317
+ const miss = missOf(stand, b, v);
318
+ const seen = within(toCamera(om, sub(b, stand)), frame0, 0.9);
319
+ if (seen && (chosen === null || miss > chosen.miss))
320
+ chosen = { stand, u: spot, d: far * d0, miss };
321
+ if (seen && miss >= Math.min(1.52 * (1 + clearance) * pivot.radius, 0.9 * far * d0)) {
322
+ chosen = { stand, u: spot, d: far * d0, miss };
323
+ break search;
324
+ }
325
+ }
326
+ }
327
+ chosen ??= { stand: sub(v, scale(toScene(om, first0), d0)), u: first0, d: d0, miss: 0 };
328
+ const from = sub(chosen.stand, b);
329
+ const dm = Math.max(1e-12, length(from));
330
+ const rest = swingOf(om, o1);
331
+ return {
332
+ turn: { pivot, swing: swingOf(o0, om), u0, u1: chosen.u, d0, d1: chosen.d, lift: 0, fov0: input.start.fov, fov1: input.start.fov },
333
+ leg: {
334
+ about: input.destination, type: "swing", swing: rest, fov0: input.start.fov, fov1: input.end.fov, from, to: e1, d0: dm, d1, dn: Math.min(dm, d1 * Math.exp(2 + swingDegrees(rest) / 15)),
335
+ u0: unit(toCamera(om, scale(from, -1))), u1: unit(toCamera(o1, scale(e1, -1))), l: dm - d1, ea: Math.max(1e-9, chosen.d), eb: d1,
336
+ },
337
+ };
338
+ }
339
+ function lineLeg(input, about, watch) {
340
+ const from = sub(input.start.position, about.from);
341
+ const to = sub(input.end.position, placeOf(about, 1));
342
+ const near = (view, t) => Math.max(1e-9, Math.min(...[input.leaving, input.destination, ...(input.around ? [input.around] : [])].map((body) => length(sub(view, placeOf(body, t))))));
343
+ return {
344
+ about, type: "line", swing: swingOf(facingOf(input.start), facingOf(input.end)), fov0: input.start.fov, fov1: input.end.fov, from, to, d0: length(from), d1: length(to), dn: length(to),
345
+ u0: [0, 0, 1], u1: [0, 0, 1], l: length(sub(to, from)), ea: near(input.start.position, 0), eb: near(input.end.position, 1), watch,
346
+ };
347
+ }
348
+ /** The way on is never hurried by a long turn: it takes at least 45 percent of the turn's time. */
349
+ const legMs = (leg, turn = 0) => Math.max(0.45 * turn, 900 + 320 * Math.max(Math.log1p(leg.l / (leg.ea + leg.eb)), Math.abs(Math.log(Math.max(1e-12, leg.d1) / Math.max(1e-12, leg.d0)))) + 16 * swingDegrees(leg.swing));
350
+ const turnMs = (turn) => {
351
+ const ms = 16 * swingDegrees(turn.swing) + 8 * (angleOf(turn.u0, turn.u1) / RAD) + 400 * Math.abs(Math.log(turn.d1 / turn.d0)) + 600 * turn.lift;
352
+ return ms < 40 ? 0 : Math.max(500, ms);
353
+ };
354
+ function routeOf(input) {
355
+ const wanted = input.reduced ? "cut" : (input.kind ?? "turn-travel");
356
+ const kind = wanted === "auto" ? "turn-travel" : wanted;
357
+ const clearance = input.clearance ?? TRAVEL_CLEARANCE;
358
+ const bodies = [...new Set([input.leaving, input.destination, ...(input.around ? [input.around] : []), ...(input.others ?? [])])];
359
+ const shells = bodies.map((body) => ({ body, clear: Math.min((1 + clearance) * body.radius, length(sub(input.start.position, body.from)) / 1.5, length(sub(input.end.position, placeOf(body, 1))) / 1.5) }));
360
+ const least = angleOf(facingOf(input.start).f, facingOf(input.end).f) / RAD;
361
+ const region = input.leaving.reach !== undefined || input.destination.reach !== undefined;
362
+ const base = { shells, frameAspect: input.aspect, input };
363
+ const easing = { turn: "smooth-step", distance: "geometric" };
364
+ if (kind === "cut") {
365
+ const leg = lineLeg(input, input.destination, undefined);
366
+ return { route: { ...base, cut: true, turn: null, share: 0, arrive: 0, leg }, plan: { kind, path: "cut", stages: { turn: 0, travel: 0, arrive: 1 }, duration: 0, turn: least, settle: 0, easing } };
367
+ }
368
+ let turn = null;
369
+ let leg;
370
+ let share = 0;
371
+ let path = "line";
372
+ if (kind !== "turn-travel" || region) {
373
+ // one straight line: a region's view is held on the body at its middle (or the planet circled), so that body stays in the picture
374
+ leg = lineLeg(input, region ? (input.around ?? input.destination) : input.destination, kind === "look-back" ? input.leaving : undefined);
375
+ }
376
+ else {
377
+ const family = input.around !== undefined;
378
+ path = family ? "circle" : "pivot";
379
+ // already facing the way the last view faces, with the destination in the picture and the straight way clear of the body left: there is nothing to turn, and a straight line keeps in the picture all that is in it at both ends
380
+ const o0 = facingOf(input.start);
381
+ const d0 = length(sub(input.leaving.from, input.start.position));
382
+ const facing = swingDegrees(swingOf(o0, facingOf(input.end))) < 1 && within(toCamera(o0, sub(input.destination.from, input.start.position)), frameOf(input.start.fov, input.aspect), 0.93);
383
+ const open = missOf(input.start.position, input.end.position, input.leaving.from) >= Math.min(1.52 * (1 + clearance) * input.leaving.radius, 0.9 * d0);
384
+ let stand = facing && open ? { turn: { pivot: input.leaving, swing: swingOf(o0, o0), u0: [0, 0, 1], u1: [0, 0, 1], d0: 1, d1: 1, lift: 0, fov0: input.start.fov, fov1: input.start.fov }, leg: lineLeg(input, input.destination, undefined) } : standAt(input, 0.25, family);
385
+ // the stand is worked out for the moment the turn ends, which is the share of the move its own length gives: found by trying, until the two agree (the bodies move, so a stand for another moment would not meet the way on)
386
+ let tm = 0.25;
387
+ for (let pass = 0; pass < 8; pass += 1) {
388
+ const t0 = turnMs(stand.turn);
389
+ share = t0 === 0 ? 0 : t0 / (t0 + legMs(stand.leg, t0));
390
+ if ((facing && open) || share === 0 || Math.abs(share - tm) < 0.002)
391
+ break;
392
+ tm = pass === 0 ? share : (tm + share) / 2;
393
+ stand = standAt(input, tm, family);
394
+ }
395
+ if (share > 0 && !(facing && open))
396
+ share = tm;
397
+ turn = stand.turn;
398
+ leg = stand.leg;
399
+ if (share === 0) {
400
+ // nothing to turn: the way on starts from the camera's own place
401
+ turn = null;
402
+ if (leg.type === "line")
403
+ leg = { ...leg, from: sub(input.start.position, input.destination.from), l: length(sub(leg.to, sub(input.start.position, input.destination.from))), swing: swingOf(facingOf(input.start), facingOf(input.end)), fov0: input.start.fov };
404
+ else {
405
+ const from = sub(input.start.position, input.destination.from);
406
+ const o0 = facingOf(input.start);
407
+ leg = { ...leg, from, d0: length(from), l: length(from) - leg.d1, dn: Math.min(length(from), leg.dn), u0: unit(toCamera(o0, scale(from, -1))), swing: swingOf(o0, facingOf(input.end)) };
408
+ }
409
+ }
410
+ else if (family && turn.pivot !== input.leaving) {
411
+ // the planet is held, not the moon left: back the turn off in its middle until one of the two bodies is in the picture at every moment of it
412
+ const frameAt = (fov) => frameOf(fov, input.aspect);
413
+ // the stand is for the moment the turn ends, so when the backed-off turn takes another share of the move the stand is worked out again for that moment, until the two agree
414
+ for (let round = 0; round < 4; round += 1) {
415
+ const missing = (candidate) => {
416
+ let missed = 0;
417
+ for (let i = 1; i < 64; i += 1) {
418
+ // the first moments are sampled finely: the body left is nearest then, and nearest to being lost
419
+ for (const x of i === 1 ? [0.0005, 0.001, 0.002, 0.004, 0.008, 1 / 64] : [i / 64]) {
420
+ const t = share * x;
421
+ const at = turnAt(candidate, ease(x), t);
422
+ const frame = frameAt(at.fov);
423
+ const seen = (body) => {
424
+ const sight = sightOf(body, t, at.position, at.o, frame);
425
+ return sight.inside && Math.abs(sight.x) <= 0.9 && Math.abs(sight.y) <= 0.9;
426
+ };
427
+ if (!seen(input.leaving) && !seen(input.destination))
428
+ missed += 1;
429
+ }
430
+ }
431
+ return missed;
432
+ };
433
+ let best = { turn, missed: Number.POSITIVE_INFINITY };
434
+ for (let lift = 0; lift <= 3.31 && best.missed > 0; lift += 0.3) {
435
+ const missed = missing({ ...turn, lift });
436
+ if (missed < best.missed)
437
+ best = { turn: { ...turn, lift }, missed };
438
+ }
439
+ if (best.missed > 0) {
440
+ // the body left is near the camera and the planet far off, so the circle of the planet carries the camera away from the body left at once: begin by turning about the body left instead
441
+ const u = unit(toCamera(facingOf(input.start), sub(input.leaving.from, input.start.position)));
442
+ const d = Math.max(1e-12, length(sub(input.leaving.from, input.start.position)));
443
+ for (const far of [1, 2, 4, 8, 16]) {
444
+ for (const q of [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]) {
445
+ for (let lift = 0; lift <= 3.31 && best.missed > 0; lift += 0.3) {
446
+ const candidate = { ...turn, lift, hold: { body: input.leaving, u, d, q, far } };
447
+ const missed = missing(candidate);
448
+ if (missed < best.missed)
449
+ best = { turn: candidate, missed };
450
+ }
451
+ }
452
+ }
453
+ }
454
+ turn = best.turn;
455
+ const t0 = turnMs(turn);
456
+ const next = t0 / (t0 + legMs(leg, t0));
457
+ if (Math.abs(next - share) < 0.002 || round === 3)
458
+ break;
459
+ share = (share + next) / 2;
460
+ const again = standAt(input, share, family);
461
+ turn = again.turn;
462
+ leg = again.leg;
463
+ }
464
+ }
465
+ }
466
+ const total = (turn ? turnMs(turn) : 0) + legMs(leg, turn ? turnMs(turn) : 0);
467
+ // the arrival is the last stretch: within two of the last distances on a line, or the turn round the destination
468
+ const near = leg.type === "line" ? 2 * leg.eb : leg.dn - leg.d1;
469
+ let lo = 0;
470
+ let hi = 1;
471
+ for (let i = 0; i < 30; i += 1) {
472
+ const mid = (lo + hi) / 2;
473
+ if (leftOf(ease(mid), leg.l, leg.ea, leg.eb) > near)
474
+ lo = mid;
475
+ else
476
+ hi = mid;
477
+ }
478
+ const arrive = share + (1 - share) * Math.min(0.9, Math.max(0.6, hi));
479
+ const route = { ...base, cut: false, turn, share, arrive, leg };
480
+ return { route, plan: { kind, path, stages: { turn: share, travel: arrive - share, arrive: 1 - arrive }, duration: Math.round(Math.min(9000, Math.max(1200, total)) * Math.max(0.05, input.pace ?? 1)), turn: least, settle: leg.type === "swing" ? swingDegrees(leg.swing) : 0, easing } };
481
+ }
482
+ /**
483
+ * The plan of a move: its stages and the share of the time each takes, how long it should last, and how it is eased. The length is a suggestion from how far the picture changes (16 milliseconds a degree
484
+ * turned, 320 for each time the distance changes by a factor of e, between 1.2 and 9 seconds, times `pace`); a page may use its own, since `travelPose` takes the moment as a share of the move.
485
+ *
486
+ * @param input - The two views, the bodies and the picture's shape.
487
+ * @returns The plan. Hand it to `travelPose` as its third argument to save working the path out at every frame.
488
+ *
489
+ * @example
490
+ * ```ts
491
+ * import { travelPlan } from "@johnmorrisdotca/wakusei-planets/space";
492
+ *
493
+ * const plan = travelPlan({
494
+ * start: { position: [0, 0, 4.1], forward: [0, 0, -1], up: [0, 1, 0], fov: 35 },
495
+ * end: { position: [297.6, 0, 0], forward: [1, 0, 0], up: [0, 1, 0], fov: 35 },
496
+ * leaving: { from: [0, 0, 0], radius: 1 },
497
+ * destination: { from: [300, 0, 0], radius: 0.6 },
498
+ * aspect: 16 / 9,
499
+ * });
500
+ * console.log(plan.path, plan.duration, plan.stages.turn.toFixed(2));
501
+ * ```
502
+ */
503
+ export function travelPlan(input) {
504
+ const { route, plan } = routeOf(input);
505
+ return { ...plan, route };
506
+ }
507
+ /**
508
+ * The camera at the moment `t` of a move, from 0 (the first view) to 1 (the last view), with where the two bodies are in the picture. The same input and moment always give the same camera. The default move,
509
+ * `turn-travel`, never shows an empty picture between two framed views: the body being left (or the planet circled) is held in the picture through the turn, and the destination from the end of the turn on,
510
+ * where it only grows; the look direction turns one way only; no place is nearer a body than `TRAVEL_CLEARANCE`. `direct` and `look-back` are a straight eased glide and promise only the clearance. `cut`, or
511
+ * `reduced`, gives the last view for every `t` above 0.
512
+ *
513
+ * The bodies' places are taken at the two ends of the move (`from` and `to`): work out `end` for the destination's place at the end, and start a new move if the clock's speed changes on the way.
514
+ *
515
+ * @param input - The two views, the bodies and the picture's shape.
516
+ * @param t - The moment, as a share of the move from 0 to 1.
517
+ * @param plan - The plan from `travelPlan(input)`, to save working it out again.
518
+ * @returns The camera, the stage, and where the bodies are in the picture.
519
+ *
520
+ * @example
521
+ * ```ts
522
+ * import { travelPlan, travelPose, type TravelInput } from "@johnmorrisdotca/wakusei-planets/space";
523
+ *
524
+ * const input: TravelInput = {
525
+ * start: { position: [0, 0, 4.1], forward: [0, 0, -1], up: [0, 1, 0], fov: 35 },
526
+ * end: { position: [297.6, 0, 0], forward: [1, 0, 0], up: [0, 1, 0], fov: 35 },
527
+ * leaving: { from: [0, 0, 0], radius: 1 },
528
+ * destination: { from: [300, 0, 0], radius: 0.6 },
529
+ * aspect: 16 / 9,
530
+ * };
531
+ * const plan = travelPlan(input);
532
+ * for (const t of [0, 0.25, 0.5, 0.75, 1]) {
533
+ * const camera = travelPose(input, t, plan);
534
+ * console.log(camera.stage, camera.leaving.inside || camera.destination.inside);
535
+ * }
536
+ * ```
537
+ */
538
+ export function travelPose(input, t, plan) {
539
+ const route = plan?.route?.input === input ? plan.route : routeOf(input).route;
540
+ const at = Number.isFinite(t) ? clamp01(t) : 1;
541
+ let view;
542
+ let stage;
543
+ if (at <= 0) {
544
+ view = { position: input.start.position, forward: facingOf(input.start).f, up: facingOf(input.start).u, fov: input.start.fov };
545
+ stage = route.turn ? "turn" : "travel";
546
+ }
547
+ else if (at >= 1 || route.cut) {
548
+ view = { position: input.end.position, forward: facingOf(input.end).f, up: facingOf(input.end).u, fov: input.end.fov };
549
+ stage = "arrive";
550
+ }
551
+ else {
552
+ const now = along(route, at);
553
+ view = { position: now.position, forward: now.o.f, up: now.o.u, fov: now.fov };
554
+ stage = now.stage;
555
+ }
556
+ const time = route.cut && at > 0 ? 1 : at;
557
+ const o = { f: view.forward, u: view.up };
558
+ const frame = frameOf(view.fov, input.aspect);
559
+ const sample = { ...view, stage, leaving: sightOf(input.leaving, time, view.position, o, frame), destination: sightOf(input.destination, time, view.position, o, frame) };
560
+ if (input.around)
561
+ sample.around = sightOf(input.around, time, view.position, o, frame);
562
+ return sample;
563
+ }
564
+ /**
565
+ * A framed view of a whole body to arrive at, from the side the move comes from, so the approach is straight and nothing has to be turned back: the camera stands on the line towards `from`, moved round
566
+ * towards the Sun where that side would show less than about three fifths of the body lit (never more than 75 degrees off the Sun's direction), at `fit` radii away (4.1, and as much again as a tall picture is
567
+ * taller than wide), looking at the body with `north` up.
568
+ *
569
+ * @param input - The body's place and radius, where the move comes from, the Sun's place (left out for the Sun itself), the picture's shape, and optionally `fov`, `fit` and `north`.
570
+ * @returns The view, and `lit`, the share of the disc that is lit from there (1 when no Sun is given).
571
+ *
572
+ * @example
573
+ * ```ts
574
+ * import { travelArrival } from "@johnmorrisdotca/wakusei-planets/space";
575
+ *
576
+ * const view = travelArrival({ body: [300, 0, 0], radius: 0.6, from: [0, 0, 0], sun: [100, 0, 150], aspect: 16 / 9 });
577
+ * console.log(view.position.map((n) => n.toFixed(1)).join(", "), view.lit.toFixed(2));
578
+ * ```
579
+ */
580
+ export function travelArrival(input) {
581
+ let side = sub(input.from, input.body);
582
+ side = length(side) < 1e-12 ? [0, 0, 1] : unit(side);
583
+ let lit = 1;
584
+ if (input.sun && length(sub(input.sun, input.body)) > 1e-12) {
585
+ const sun = unit(sub(input.sun, input.body));
586
+ const off = angleOf(side, sun);
587
+ const most = 75 * RAD;
588
+ if (off > most)
589
+ side = arc(side, sun, (off - most) / off);
590
+ lit = (1 + Math.cos(Math.min(off, most))) / 2;
591
+ }
592
+ const distance = input.radius * (input.fit ?? 4.1) * Math.max(1, 1 / Math.max(1e-3, input.aspect));
593
+ const forward = scale(side, -1);
594
+ const north = input.north ?? [0, 1, 0];
595
+ const across = sub(north, scale(forward, dot(north, forward)));
596
+ return { position: add(input.body, scale(side, distance)), forward, up: length(across) < 1e-6 ? squareTo(forward) : unit(across), fov: input.fov ?? 35, lit };
597
+ }
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "0.35.0";
1
+ export declare const VERSION = "0.37.0";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "0.35.0";
1
+ export const VERSION = "0.37.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnmorrisdotca/wakusei-planets",
3
- "version": "0.35.0",
3
+ "version": "0.37.0",
4
4
  "description": "Wakusei Planets 惑星 — The Sun, planets and moons for the web: each as a clean line drawing, a shaded realistic drawing or a painted surface map, with real sizes, axial tilt and rotation as data, and positions of the moons for any date and time. SVG for the lines, three.js (optional) for the rest.",
5
5
  "keywords": [
6
6
  "planets",
@@ -358,6 +358,11 @@
358
358
  "import": "./dist/places/saturn.js",
359
359
  "default": "./dist/places/saturn.js"
360
360
  },
361
+ "./orbiters": {
362
+ "types": "./dist/orbiters.d.ts",
363
+ "import": "./dist/orbiters.js",
364
+ "default": "./dist/orbiters.js"
365
+ },
361
366
  "./suncalc": {
362
367
  "types": "./dist/suncalc.d.ts",
363
368
  "import": "./dist/suncalc.js",
@@ -533,6 +538,11 @@
533
538
  "types": "./dist/approaches.d.ts",
534
539
  "import": "./dist/approaches.js",
535
540
  "default": "./dist/approaches.js"
541
+ },
542
+ "./moon-catalogue": {
543
+ "types": "./dist/moon-catalogue.d.ts",
544
+ "import": "./dist/moon-catalogue.js",
545
+ "default": "./dist/moon-catalogue.js"
536
546
  }
537
547
  },
538
548
  "files": [
@@ -547,6 +557,7 @@
547
557
  },
548
558
  "scripts": {
549
559
  "export:data": "node scripts/export-data.ts",
560
+ "places:planned": "node scripts/check-planned.ts",
550
561
  "build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.json && node scripts/copy-textures.mjs",
551
562
  "lint": "eslint .",
552
563
  "typecheck": "tsc -p tsconfig.check.json && tsc -p tsconfig.tools.json && tsc -p tsconfig.demo.json",
@@ -611,7 +622,7 @@
611
622
  },
612
623
  "packageManager": "pnpm@10.28.0",
613
624
  "dependencies": {
614
- "@johnmorrisdotca/kazoku-ui": "^0.37.0"
625
+ "@johnmorrisdotca/kazoku-ui": "^0.41.0"
615
626
  },
616
627
  "peerDependencies": {
617
628
  "@johnmorrisdotca/chikyu-earth": ">=0.10.0",