@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,275 @@
1
+ // The spacecraft in orbit round the Moon and Mars: where each is at a moment, from mean orbital elements with secular rates fitted to JPL Horizons' track (src/data/orbiters.ts, made by
2
+ // scripts/make-orbiter-data.py), and what is known about each (src/orbiter-facts.ts). Pure: no DOM, no clock reads. Positions are from the centre of the body, in kilometres, ecliptic and
3
+ // equinox J2000, the same axes as `/roadster` and the ten deep-space craft; `eclipticToScene` from `/space` turns them into the space view's axes.
4
+ import { ORBITER_FITS } from "./data/orbiters.js";
5
+ import { ORBITER_FACTS } from "./orbiter-facts.js";
6
+ export { ORBITER_FACTS } from "./orbiter-facts.js";
7
+ /**
8
+ * The orbiters the package knows: six round the Moon and seven round Mars.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * import { ORBITER_IDS } from "@johnmorrisdotca/wakusei-planets/orbiters";
13
+ *
14
+ * console.log(ORBITER_IDS.join(", "));
15
+ * ```
16
+ */
17
+ export const ORBITER_IDS = Object.keys(ORBITER_FITS);
18
+ /** GM of the Moon and Mars in km^3/s^2 (JPL DE440). */
19
+ const GM = { moon: 4902.800118, mars: 42828.375816 };
20
+ const UNIX_EPOCH_JD = 2440587.5;
21
+ /**
22
+ * TDB minus UTC in seconds. The fitted epochs and Horizons' vectors are in TDB; a `Date` is UTC. TAI - UTC is 37 s (since 2017-01-01; a later leap second would add one) and TT - TAI is 32.184 s; TDB differs
23
+ * from TT by under 2 ms, which this package ignores. Left out, the craft would lag its true place by about 69 s of its motion: 110 km for LRO, 235 km for MRO.
24
+ */
25
+ const TDB_MINUS_UTC_S = 37 + 32.184;
26
+ const RAD = Math.PI / 180;
27
+ const hasElements = (fit) => "a" in fit && typeof fit.a === "number";
28
+ function idOf(id) {
29
+ if (!(id in ORBITER_FITS))
30
+ throw new Error(`wakusei-planets: no orbiter called ${id}`);
31
+ return id;
32
+ }
33
+ /**
34
+ * The body an orbiter goes round.
35
+ *
36
+ * @param id - The orbiter.
37
+ * @returns `moon` or `mars`.
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * import { orbiterCentre } from "@johnmorrisdotca/wakusei-planets/orbiters";
42
+ *
43
+ * console.log(orbiterCentre("hope"));
44
+ * ```
45
+ */
46
+ export function orbiterCentre(id) {
47
+ return ORBITER_FITS[idOf(id)].center;
48
+ }
49
+ /**
50
+ * The fitted elements of an orbiter, or `undefined` for one with no path (`orbiterStatus` says `none`).
51
+ *
52
+ * @param id - The orbiter.
53
+ * @returns The elements, or `undefined`.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * import { orbiterElements } from "@johnmorrisdotca/wakusei-planets/orbiters";
58
+ *
59
+ * console.log(orbiterElements("capstone"), orbiterElements("mro")?.e.toFixed(4));
60
+ * ```
61
+ */
62
+ export function orbiterElements(id) {
63
+ const fit = ORBITER_FITS[idOf(id)];
64
+ if (!hasElements(fit))
65
+ return undefined;
66
+ const f = fit;
67
+ return { a: f.a, e: f.e, i: f.i, raan: f.raan, argp: f.argp, m0: f.m0, raanRate: f.raanRate, argpRate: f.argpRate, meanMotionCorrection: f.meanMotionCorrection, periodDays: f.periodDays, epochJd: f.epochJd, validFrom: f.validFrom, fitUntil: f.fitUntil, validUntil: f.validUntil, retrieved: f.retrieved, trackedUntil: f.trackedUntil, fitErrorKm: { ...f.fitErrorKm }, predictedErrorKm: f.predictedErrorKm === null ? null : { ...f.predictedErrorKm }, basis: f.basis };
68
+ }
69
+ const msOf = (when) => (when instanceof Date ? when.getTime() : when);
70
+ /** The Julian date in TDB of a moment given in UTC (see `TDB_MINUS_UTC_S`). */
71
+ const tdbJdOf = (when) => (msOf(when) / 1000 + TDB_MINUS_UTC_S) / 86_400 + UNIX_EPOCH_JD;
72
+ /**
73
+ * Where an orbiter's path comes from at a moment: see `OrbiterStatus`. `tracked` ends with the fit's window (`fitUntil`); after it, up to `validUntil`, a path is `predicted`. A moment before the window or after
74
+ * `validUntil` is `modelled` (the ellipse is extended, not measured there). `trackedUntil`, how far Horizons' own tracking data reached, is not used: where Horizons' track is itself an agency's prediction, "tracked" says the
75
+ * ellipse follows that track.
76
+ *
77
+ * @param id - The orbiter.
78
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
79
+ * @returns The status.
80
+ *
81
+ * @example
82
+ * ```ts
83
+ * import { orbiterStatus } from "@johnmorrisdotca/wakusei-planets/orbiters";
84
+ *
85
+ * console.log(orbiterStatus("mro", Date.UTC(2026, 10, 1)), orbiterStatus("mro", Date.UTC(2030, 0, 1)));
86
+ * ```
87
+ */
88
+ export function orbiterStatus(id, when) {
89
+ const fit = ORBITER_FITS[idOf(id)];
90
+ if (!hasElements(fit))
91
+ return "none";
92
+ const f = fit;
93
+ if (f.basis === "modelled")
94
+ return "modelled";
95
+ const ms = msOf(when);
96
+ if (ms < Date.parse(f.validFrom) || ms > Date.parse(f.validUntil))
97
+ return "modelled";
98
+ return ms > Date.parse(f.fitUntil) ? "predicted" : "tracked";
99
+ }
100
+ /**
101
+ * Where an orbiter is at a moment, in kilometres from the centre of the body it goes round, ecliptic and equinox J2000. The mean ellipse is propagated with its drifting node and periapsis: good to the
102
+ * `fitErrorKm` of its elements within the fit's window and the larger `predictedErrorKm` in the days after it, rougher beyond (`orbiterStatus`). `when` is UTC; the elements' TDB epoch is allowed for (69.184 s). Throws for a craft with no path.
103
+ *
104
+ * @param id - The orbiter.
105
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
106
+ * @returns x, y, z in kilometres.
107
+ *
108
+ * @example
109
+ * ```ts
110
+ * import { orbiterPosition } from "@johnmorrisdotca/wakusei-planets/orbiters";
111
+ *
112
+ * const [x, y, z] = orbiterPosition("lro", Date.UTC(2026, 10, 11));
113
+ * console.log(Math.hypot(x, y, z).toFixed(0), "km from the Moon's centre");
114
+ * ```
115
+ */
116
+ export function orbiterPosition(id, when) {
117
+ return stateOf(id, when).position;
118
+ }
119
+ /**
120
+ * How fast an orbiter moves at a moment, in kilometres a second relative to the body it goes round (from the ellipse).
121
+ *
122
+ * @param id - The orbiter.
123
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
124
+ * @returns The speed in km/s.
125
+ *
126
+ * @example
127
+ * ```ts
128
+ * import { orbiterSpeed } from "@johnmorrisdotca/wakusei-planets/orbiters";
129
+ *
130
+ * console.log(orbiterSpeed("lro", Date.UTC(2026, 10, 11)).toFixed(2), "km/s");
131
+ * ```
132
+ */
133
+ export function orbiterSpeed(id, when) {
134
+ const { velocity } = stateOf(id, when);
135
+ return Math.hypot(velocity[0], velocity[1], velocity[2]);
136
+ }
137
+ /**
138
+ * The numbers a page shows for an orbiter at a moment: its height above the body's mean surface, its speed, its period and inclination, where its path comes from, and how far wrong it may be (`errorKm`: the worst error
139
+ * measured against Horizons for that status, `fitErrorKm.max` when `tracked`, `predictedErrorKm.max` when `predicted`; `null` when `modelled`).
140
+ *
141
+ * @param id - The orbiter.
142
+ * @param when - A `Date` or milliseconds since 1970 (UTC).
143
+ * @returns The numbers, or `undefined` for a craft with no path.
144
+ *
145
+ * @example
146
+ * ```ts
147
+ * import { orbiterNumbers } from "@johnmorrisdotca/wakusei-planets/orbiters";
148
+ *
149
+ * const mro = orbiterNumbers("mro", Date.UTC(2026, 10, 11))!;
150
+ * console.log(mro.altitudeKm.toFixed(0), "km up,", mro.speedKmS.toFixed(2), "km/s,", (mro.periodDays * 24 * 60).toFixed(0), "minutes round,", mro.status, "within", mro.errorKm, "km");
151
+ * ```
152
+ */
153
+ export function orbiterNumbers(id, when) {
154
+ const elements = orbiterElements(id);
155
+ if (elements === undefined)
156
+ return undefined;
157
+ const { position, velocity } = stateOf(id, when);
158
+ const status = orbiterStatus(id, when);
159
+ const errorKm = status === "tracked" ? elements.fitErrorKm.max : status === "predicted" ? (elements.predictedErrorKm?.max ?? null) : null;
160
+ return { altitudeKm: Math.hypot(position[0], position[1], position[2]) - MEAN_RADIUS_KM[orbiterCentre(id)], speedKmS: Math.hypot(velocity[0], velocity[1], velocity[2]), periodDays: elements.periodDays, inclinationDeg: elements.i, status, errorKm };
161
+ }
162
+ /** The mean radius of the Moon and Mars in kilometres (IAU), for heights above the surface. */
163
+ const MEAN_RADIUS_KM = { moon: 1737.4, mars: 3389.5 };
164
+ /**
165
+ * The orbit as points round one trip, in kilometres from the body's centre, ecliptic J2000, at the moment's node and periapsis: for an orbit line.
166
+ *
167
+ * @param id - The orbiter.
168
+ * @param when - A `Date` or milliseconds since 1970 (UTC): the ellipse's orientation then.
169
+ * @param points - How many points round the trip (the first is repeated at the end).
170
+ * @returns The points, or an empty list for a craft with no path.
171
+ *
172
+ * @example
173
+ * ```ts
174
+ * import { orbiterPath } from "@johnmorrisdotca/wakusei-planets/orbiters";
175
+ *
176
+ * console.log(orbiterPath("hope", Date.UTC(2026, 10, 11), 8).length);
177
+ * ```
178
+ */
179
+ export function orbiterPath(id, when, points = 96) {
180
+ const elements = orbiterElements(id);
181
+ if (elements === undefined)
182
+ return [];
183
+ const dt = tdbJdOf(when) - elements.epochJd;
184
+ const out = [];
185
+ // evenly spaced in eccentric anomaly, not mean: a lopsided orbit (ARTEMIS P1, e about 0.8) is then sampled fastest where it bends least, and has no sharp corner at periapsis
186
+ for (let k = 0; k <= points; k += 1)
187
+ out.push(placeAt(elements, dt, (2 * Math.PI * k) / points));
188
+ return out;
189
+ }
190
+ function stateOf(id, when) {
191
+ const elements = orbiterElements(id);
192
+ if (elements === undefined)
193
+ throw new RangeError(`wakusei-planets: ${id} has no path to follow (${orbiterStatus(id, when)})`);
194
+ const centre = orbiterCentre(id);
195
+ const dt = tdbJdOf(when) - elements.epochJd;
196
+ const n = meanMotion(elements, centre);
197
+ const m = (elements.m0 + n * dt) % 360;
198
+ const position = placeOf(elements, centre, dt, undefined, m);
199
+ // the velocity from the ellipse: a small step along it in time
200
+ const later = placeOf(elements, centre, dt + 1 / 86_400, undefined, (elements.m0 + n * (dt + 1 / 86_400)) % 360);
201
+ return { position, velocity: [later[0] - position[0], later[1] - position[1], later[2] - position[2]] };
202
+ }
203
+ /** The mean motion in degrees a day: Kepler's for `a`, corrected by the fit. */
204
+ const meanMotion = (elements, centre) => (Math.sqrt(GM[centre] / elements.a ** 3) * 86_400) / RAD + elements.meanMotionCorrection;
205
+ /** A point of the ellipse `dt` days after the epoch: at the true anomaly of mean anomaly `meanAnomaly` (degrees), or at the given mean anomaly. */
206
+ function placeOf(elements, centre, dt, meanAnomalyDeg, mDeg) {
207
+ void centre;
208
+ const e = elements.e;
209
+ const m = ((meanAnomalyDeg ?? mDeg ?? 0) * RAD) % (2 * Math.PI);
210
+ let ea = m;
211
+ for (let k = 0; k < 30; k += 1)
212
+ ea -= (ea - e * Math.sin(ea) - m) / (1 - e * Math.cos(ea));
213
+ return placeAt(elements, dt, ea);
214
+ }
215
+ /** A point of the ellipse `dt` days after the epoch at the eccentric anomaly `ea` (radians). */
216
+ function placeAt(elements, dt, ea) {
217
+ const e = elements.e;
218
+ const nu = 2 * Math.atan2(Math.sqrt(1 + e) * Math.sin(ea / 2), Math.sqrt(1 - e) * Math.cos(ea / 2));
219
+ const r = elements.a * (1 - e * Math.cos(ea));
220
+ const raan = (elements.raan + elements.raanRate * dt) * RAD;
221
+ const argp = (elements.argp + elements.argpRate * dt) * RAD;
222
+ const inc = elements.i * RAD;
223
+ const u = argp + nu;
224
+ return [
225
+ r * (Math.cos(raan) * Math.cos(u) - Math.sin(raan) * Math.sin(u) * Math.cos(inc)),
226
+ r * (Math.sin(raan) * Math.cos(u) + Math.cos(raan) * Math.sin(u) * Math.cos(inc)),
227
+ r * Math.sin(u) * Math.sin(inc),
228
+ ];
229
+ }
230
+ /**
231
+ * What is known about an orbiter: names, operator, launch, instruments, a line about it, and the state of its path.
232
+ *
233
+ * @param id - The orbiter.
234
+ * @returns The facts.
235
+ *
236
+ * @example
237
+ * ```ts
238
+ * import { orbiterFacts } from "@johnmorrisdotca/wakusei-planets/orbiters";
239
+ *
240
+ * const lro = orbiterFacts("lro");
241
+ * console.log(lro.name.en, lro.operator, lro.launch.date, lro.launch.vehicle);
242
+ * ```
243
+ */
244
+ export function orbiterFacts(id) {
245
+ return ORBITER_FACTS[idOf(id)];
246
+ }
247
+ /** An ecliptic J2000 vector in the space view's axes (equatorial J2000, y north): the same turn `eclipticToScene` from `/space` makes, kept here so `/orbiters` imports nothing of the view. */
248
+ const OBLIQUITY = 23.4392911 * RAD;
249
+ const toScene = (v) => [v[0], v[1] * Math.sin(OBLIQUITY) + v[2] * Math.cos(OBLIQUITY), -(v[1] * Math.cos(OBLIQUITY) - v[2] * Math.sin(OBLIQUITY))];
250
+ /**
251
+ * An orbiter as a point the space view can ride along with (`space.ride(orbiterRidePoint("lro"), { around: "moon" })`): where it is and which way it goes at a moment, in kilometres and kilometres a
252
+ * second from the centre of the body it goes round, in the view's axes (equatorial J2000, y north). It answers `null` for a craft with no path.
253
+ *
254
+ * @param id - The orbiter.
255
+ * @returns The point: its `at` gives the place and velocity, or `null` for a craft with no path (`orbiterStatus` says `none`), which the view then does not ride.
256
+ *
257
+ * @example
258
+ * ```ts
259
+ * import { orbiterRidePoint } from "@johnmorrisdotca/wakusei-planets/orbiters";
260
+ *
261
+ * const point = orbiterRidePoint("mro").at(Date.UTC(2026, 10, 11));
262
+ * console.log(point === null ? "no path" : Math.hypot(...point.velocity).toFixed(2) + " km/s");
263
+ * ```
264
+ */
265
+ export function orbiterRidePoint(id) {
266
+ idOf(id);
267
+ return {
268
+ at(ms) {
269
+ if (orbiterElements(id) === undefined)
270
+ return null;
271
+ const { position, velocity } = stateOf(id, ms);
272
+ return { position: toScene(position), velocity: toScene(velocity) };
273
+ },
274
+ };
275
+ }
@@ -0,0 +1,182 @@
1
+ import type { Place, PlaceKind } from "./places.ts";
2
+ import { type Vec3 } from "./vec.ts";
3
+ /**
4
+ * The layers of places a view can show, each switched on by itself: `landing` (the crewed landings, the landers and the rovers), `crash` (the crash, impact and atmospheric-entry sites), `planned` (announced landings) and `feature`
5
+ * (the named features of the surface).
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { PLACE_LAYERS } from "@johnmorrisdotca/wakusei-planets/places";
10
+ *
11
+ * console.log(PLACE_LAYERS.join(" "));
12
+ * ```
13
+ */
14
+ export declare const PLACE_LAYERS: readonly ["landing", "crash", "planned", "feature"];
15
+ /**
16
+ * One layer of places.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * import type { PlaceLayer } from "@johnmorrisdotca/wakusei-planets/places";
21
+ *
22
+ * const layer: PlaceLayer = "landing";
23
+ * console.log(layer);
24
+ * ```
25
+ */
26
+ export type PlaceLayer = (typeof PLACE_LAYERS)[number];
27
+ /**
28
+ * The layer a kind of place is shown in.
29
+ *
30
+ * @param kind - The place's kind.
31
+ * @returns Its layer.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * import { layerOf } from "@johnmorrisdotca/wakusei-planets/places";
36
+ *
37
+ * console.log(layerOf("rover"), layerOf("impact"), layerOf("feature"));
38
+ * ```
39
+ */
40
+ export declare function layerOf(kind: PlaceKind): PlaceLayer;
41
+ /**
42
+ * The layers an attribute asks for: a list of layer names with spaces or commas between (`"landing crash"`), `all` (or an empty value) for every layer, and `none` or `off` for none. Unknown words are left out.
43
+ *
44
+ * @param value - The attribute's value, or `null` when it is absent (no layers).
45
+ * @returns The layers, in `PLACE_LAYERS` order.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * import { readPlaceLayers } from "@johnmorrisdotca/wakusei-planets/places";
50
+ *
51
+ * console.log(readPlaceLayers("crash, landing"), readPlaceLayers(""), readPlaceLayers(null));
52
+ * ```
53
+ */
54
+ export declare function readPlaceLayers(value: string | null): PlaceLayer[];
55
+ /**
56
+ * A body as a view draws it, for laying marks over it: its centre and radius in the scene's units, its axes there (`pole` north, `meridian` towards longitude 0 on the equator), the stretch of its globe along each
57
+ * (east, pole, meridian: 1, 1, 1 for a sphere), where the viewer is (`eye`, a point, for a camera with perspective; `toward`, a direction, for one without), the projection of a point in the scene to CSS pixels
58
+ * (`null` when it cannot be drawn), the view's size in CSS pixels, and the body's radius on the screen in CSS pixels.
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * import type { SurfaceView } from "@johnmorrisdotca/wakusei-planets/places";
63
+ *
64
+ * const view: SurfaceView = { centre: [0, 0, 0], radius: 1, pole: [0, 1, 0], meridian: [0, 0, 1], scale: [1, 1, 1], toward: [0, 0, 1], project: (p) => ({ x: 160 + p[0] * 100, y: 160 - p[1] * 100 }), width: 320, height: 320, radiusPx: 100 };
65
+ * console.log(view.radiusPx);
66
+ * ```
67
+ */
68
+ export interface SurfaceView {
69
+ centre: Vec3;
70
+ radius: number;
71
+ pole: Vec3;
72
+ meridian: Vec3;
73
+ scale: Vec3;
74
+ eye?: Vec3;
75
+ toward?: Vec3;
76
+ project(point: Vec3): {
77
+ x: number;
78
+ y: number;
79
+ } | null;
80
+ width: number;
81
+ height: number;
82
+ radiusPx: number;
83
+ }
84
+ /**
85
+ * A place's point on the body's surface in the scene, and the outward normal there: the planetocentric latitude and east longitude laid along the body's own axes onto its ellipsoid.
86
+ *
87
+ * @param view - The body as drawn.
88
+ * @param lat - Planetocentric latitude, degrees.
89
+ * @param lon - East longitude, degrees.
90
+ * @returns The point and the unit normal.
91
+ *
92
+ * @example
93
+ * ```ts
94
+ * import { surfacePoint } from "@johnmorrisdotca/wakusei-planets/places";
95
+ *
96
+ * const view = { centre: [0, 0, 0] as [number, number, number], radius: 2, pole: [0, 1, 0] as [number, number, number], meridian: [0, 0, 1] as [number, number, number], scale: [1, 1, 1] as [number, number, number], project: () => null, width: 1, height: 1, radiusPx: 1 };
97
+ * console.log(surfacePoint(view, 0, 90).point.map((n) => n.toFixed(1)).join(", "));
98
+ * ```
99
+ */
100
+ export declare function surfacePoint(view: SurfaceView, lat: number, lon: number): {
101
+ point: Vec3;
102
+ normal: Vec3;
103
+ };
104
+ /**
105
+ * One mark to lay over the view: the place, its layer, and where it is in CSS pixels from the view's top left.
106
+ *
107
+ * @example
108
+ * ```ts
109
+ * import type { PlaceMark } from "@johnmorrisdotca/wakusei-planets/places";
110
+ *
111
+ * const mark: Pick<PlaceMark, "layer" | "x" | "y"> = { layer: "landing", x: 120, y: 80 };
112
+ * console.log(mark.layer);
113
+ * ```
114
+ */
115
+ export interface PlaceMark {
116
+ place: Place;
117
+ layer: PlaceLayer;
118
+ x: number;
119
+ y: number;
120
+ }
121
+ /**
122
+ * The rules that keep the marks quiet, in CSS pixels: nothing is marked on a body under `fromPx` of radius on the screen; a named feature is marked once it is `featureFromPx` across (a famous one at any size);
123
+ * no two marks are closer than `gapPx` (the one chosen first keeps its place); and a body gets one mark for every `sharePx` of its radius, 8 at least and `most` at most.
124
+ *
125
+ * @example
126
+ * ```ts
127
+ * import { MARK_RULES } from "@johnmorrisdotca/wakusei-planets/places";
128
+ *
129
+ * console.log(MARK_RULES.gapPx, MARK_RULES.most);
130
+ * ```
131
+ */
132
+ export declare const MARK_RULES: {
133
+ readonly fromPx: 40;
134
+ readonly featureFromPx: 14;
135
+ readonly gapPx: 12;
136
+ readonly sharePx: 4;
137
+ readonly most: 60;
138
+ };
139
+ /**
140
+ * The marks to lay over a body: the places of the layers asked for that face the viewer (not too near the edge of the disc, where they would crowd), are inside the view, and are not crowded out by a mark
141
+ * that comes first. The visited sites come first, the famous before the rest, then the named features by size, and a feature is marked only once it is big enough on the screen to show (`MARK_RULES`;
142
+ * any famous one). Nothing is marked on a body too small on the screen, and a small body gets fewer marks than a big one.
143
+ *
144
+ * @param places - The body's list.
145
+ * @param layers - The layers to show.
146
+ * @param view - The body as drawn.
147
+ * @param radiusKm - The body's radius in kilometres, to tell a feature's size on the screen.
148
+ * @returns The marks, in the order they were chosen.
149
+ *
150
+ * @example
151
+ * ```ts
152
+ * import { placeMarks } from "@johnmorrisdotca/wakusei-planets/places";
153
+ * import { PLACES_MOON } from "@johnmorrisdotca/wakusei-planets/places/moon";
154
+ *
155
+ * const view = { centre: [0, 0, 0] as [number, number, number], radius: 1, pole: [0, 1, 0] as [number, number, number], meridian: [0, 0, 1] as [number, number, number], scale: [1, 1, 1] as [number, number, number], toward: [0, 0, 1] as [number, number, number],
156
+ * project: (p: [number, number, number]) => ({ x: 200 + p[0] * 180, y: 200 - p[1] * 180 }), width: 400, height: 400, radiusPx: 180 };
157
+ * console.log(placeMarks(PLACES_MOON, ["landing"], view, 1737.4).slice(0, 3).map((mark) => mark.place.name.en));
158
+ * ```
159
+ */
160
+ export declare function placeMarks(places: readonly Place[], layers: readonly PlaceLayer[], view: SurfaceView, radiusKm: number): PlaceMark[];
161
+ /**
162
+ * The turn of a body that brings a place to the middle of the disc, for a view that is turned by a yaw about the body's pole and then a tilt about the screen's x axis, looking along -z (the `<wakusei-body>` view):
163
+ * the yaw and the tilt in degrees. Where the pole is tipped so far that no yaw puts the place in the plane the tilt turns, it comes as near as it can.
164
+ *
165
+ * @param pole - The body's north pole in the view before the turn.
166
+ * @param meridian - Its longitude 0 on the equator.
167
+ * @param lat - The place's latitude, degrees.
168
+ * @param lon - Its east longitude, degrees.
169
+ * @returns `{ yaw, tilt }`: the yaw about the pole, and the turn about the x axis (positive brings the place's y down to the middle).
170
+ *
171
+ * @example
172
+ * ```ts
173
+ * import { turnToPlace } from "@johnmorrisdotca/wakusei-planets/places";
174
+ *
175
+ * const turn = turnToPlace([0, 1, 0], [0, 0, 1], 0, 90);
176
+ * console.log(turn.yaw.toFixed(0), turn.tilt.toFixed(0));
177
+ * ```
178
+ */
179
+ export declare function turnToPlace(pole: Vec3, meridian: Vec3, lat: number, lon: number): {
180
+ yaw: number;
181
+ tilt: number;
182
+ };
@@ -0,0 +1,194 @@
1
+ import { dot } from "./vec.js";
2
+ /**
3
+ * The layers of places a view can show, each switched on by itself: `landing` (the crewed landings, the landers and the rovers), `crash` (the crash, impact and atmospheric-entry sites), `planned` (announced landings) and `feature`
4
+ * (the named features of the surface).
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import { PLACE_LAYERS } from "@johnmorrisdotca/wakusei-planets/places";
9
+ *
10
+ * console.log(PLACE_LAYERS.join(" "));
11
+ * ```
12
+ */
13
+ export const PLACE_LAYERS = ["landing", "crash", "planned", "feature"];
14
+ /**
15
+ * The layer a kind of place is shown in.
16
+ *
17
+ * @param kind - The place's kind.
18
+ * @returns Its layer.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * import { layerOf } from "@johnmorrisdotca/wakusei-planets/places";
23
+ *
24
+ * console.log(layerOf("rover"), layerOf("impact"), layerOf("feature"));
25
+ * ```
26
+ */
27
+ export function layerOf(kind) {
28
+ return kind === "crewed" || kind === "landing" || kind === "rover" ? "landing" : kind === "crash" || kind === "impact" || kind === "entry" ? "crash" : kind === "planned" ? "planned" : "feature";
29
+ }
30
+ /**
31
+ * The layers an attribute asks for: a list of layer names with spaces or commas between (`"landing crash"`), `all` (or an empty value) for every layer, and `none` or `off` for none. Unknown words are left out.
32
+ *
33
+ * @param value - The attribute's value, or `null` when it is absent (no layers).
34
+ * @returns The layers, in `PLACE_LAYERS` order.
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * import { readPlaceLayers } from "@johnmorrisdotca/wakusei-planets/places";
39
+ *
40
+ * console.log(readPlaceLayers("crash, landing"), readPlaceLayers(""), readPlaceLayers(null));
41
+ * ```
42
+ */
43
+ export function readPlaceLayers(value) {
44
+ if (value === null)
45
+ return [];
46
+ const words = value.toLowerCase().split(/[\s,]+/).filter((word) => word !== "");
47
+ if (words.length === 0 || words.includes("all") || words.includes("on"))
48
+ return [...PLACE_LAYERS];
49
+ return PLACE_LAYERS.filter((layer) => words.includes(layer));
50
+ }
51
+ const RAD = Math.PI / 180;
52
+ /**
53
+ * A place's point on the body's surface in the scene, and the outward normal there: the planetocentric latitude and east longitude laid along the body's own axes onto its ellipsoid.
54
+ *
55
+ * @param view - The body as drawn.
56
+ * @param lat - Planetocentric latitude, degrees.
57
+ * @param lon - East longitude, degrees.
58
+ * @returns The point and the unit normal.
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * import { surfacePoint } from "@johnmorrisdotca/wakusei-planets/places";
63
+ *
64
+ * const view = { centre: [0, 0, 0] as [number, number, number], radius: 2, pole: [0, 1, 0] as [number, number, number], meridian: [0, 0, 1] as [number, number, number], scale: [1, 1, 1] as [number, number, number], project: () => null, width: 1, height: 1, radiusPx: 1 };
65
+ * console.log(surfacePoint(view, 0, 90).point.map((n) => n.toFixed(1)).join(", "));
66
+ * ```
67
+ */
68
+ export function surfacePoint(view, lat, lon) {
69
+ const { pole: p, meridian: m, scale: s } = view;
70
+ const e = [p[1] * m[2] - p[2] * m[1], p[2] * m[0] - p[0] * m[2], p[0] * m[1] - p[1] * m[0]];
71
+ // the direction in the body's own frame (east, pole, meridian), stretched onto the ellipsoid along that direction: planetocentric, so the point is where that line from the centre meets the surface
72
+ const ux = Math.cos(lat * RAD) * Math.sin(lon * RAD);
73
+ const uy = Math.sin(lat * RAD);
74
+ const uz = Math.cos(lat * RAD) * Math.cos(lon * RAD);
75
+ const k = 1 / Math.hypot(ux / s[0], uy / s[1], uz / s[2]);
76
+ const [lx, ly, lz] = [ux * k, uy * k, uz * k];
77
+ const r = view.radius;
78
+ const point = [view.centre[0] + r * (e[0] * lx + p[0] * ly + m[0] * lz), view.centre[1] + r * (e[1] * lx + p[1] * ly + m[1] * lz), view.centre[2] + r * (e[2] * lx + p[2] * ly + m[2] * lz)];
79
+ const [nx, ny, nz] = [lx / (s[0] * s[0]), ly / (s[1] * s[1]), lz / (s[2] * s[2])];
80
+ const n = [e[0] * nx + p[0] * ny + m[0] * nz, e[1] * nx + p[1] * ny + m[1] * nz, e[2] * nx + p[2] * ny + m[2] * nz];
81
+ const len = Math.hypot(n[0], n[1], n[2]) || 1;
82
+ return { point, normal: [n[0] / len, n[1] / len, n[2] / len] };
83
+ }
84
+ /**
85
+ * The rules that keep the marks quiet, in CSS pixels: nothing is marked on a body under `fromPx` of radius on the screen; a named feature is marked once it is `featureFromPx` across (a famous one at any size);
86
+ * no two marks are closer than `gapPx` (the one chosen first keeps its place); and a body gets one mark for every `sharePx` of its radius, 8 at least and `most` at most.
87
+ *
88
+ * @example
89
+ * ```ts
90
+ * import { MARK_RULES } from "@johnmorrisdotca/wakusei-planets/places";
91
+ *
92
+ * console.log(MARK_RULES.gapPx, MARK_RULES.most);
93
+ * ```
94
+ */
95
+ export const MARK_RULES = { fromPx: 40, featureFromPx: 14, gapPx: 12, sharePx: 4, most: 60 };
96
+ /**
97
+ * The marks to lay over a body: the places of the layers asked for that face the viewer (not too near the edge of the disc, where they would crowd), are inside the view, and are not crowded out by a mark
98
+ * that comes first. The visited sites come first, the famous before the rest, then the named features by size, and a feature is marked only once it is big enough on the screen to show (`MARK_RULES`;
99
+ * any famous one). Nothing is marked on a body too small on the screen, and a small body gets fewer marks than a big one.
100
+ *
101
+ * @param places - The body's list.
102
+ * @param layers - The layers to show.
103
+ * @param view - The body as drawn.
104
+ * @param radiusKm - The body's radius in kilometres, to tell a feature's size on the screen.
105
+ * @returns The marks, in the order they were chosen.
106
+ *
107
+ * @example
108
+ * ```ts
109
+ * import { placeMarks } from "@johnmorrisdotca/wakusei-planets/places";
110
+ * import { PLACES_MOON } from "@johnmorrisdotca/wakusei-planets/places/moon";
111
+ *
112
+ * const view = { centre: [0, 0, 0] as [number, number, number], radius: 1, pole: [0, 1, 0] as [number, number, number], meridian: [0, 0, 1] as [number, number, number], scale: [1, 1, 1] as [number, number, number], toward: [0, 0, 1] as [number, number, number],
113
+ * project: (p: [number, number, number]) => ({ x: 200 + p[0] * 180, y: 200 - p[1] * 180 }), width: 400, height: 400, radiusPx: 180 };
114
+ * console.log(placeMarks(PLACES_MOON, ["landing"], view, 1737.4).slice(0, 3).map((mark) => mark.place.name.en));
115
+ * ```
116
+ */
117
+ export function placeMarks(places, layers, view, radiusKm) {
118
+ if (layers.length === 0 || !(view.radiusPx >= MARK_RULES.fromPx))
119
+ return [];
120
+ const wanted = [];
121
+ for (const place of places) {
122
+ const layer = layerOf(place.kind);
123
+ if (!layers.includes(layer))
124
+ continue;
125
+ if (layer === "feature" && place.famous !== true && ((place.feature?.diameterKm ?? 0) / radiusKm) * view.radiusPx < MARK_RULES.featureFromPx)
126
+ continue;
127
+ wanted.push(place);
128
+ }
129
+ const rank = (place) => (place.kind === "feature" ? 2 : 0) + (place.famous === true ? 0 : 1);
130
+ wanted.sort((a, b) => rank(a) - rank(b) || (b.feature?.diameterKm ?? 0) - (a.feature?.diameterKm ?? 0));
131
+ const most = Math.max(8, Math.min(MARK_RULES.most, Math.round(view.radiusPx / MARK_RULES.sharePx)));
132
+ const out = [];
133
+ for (const place of wanted) {
134
+ if (out.length >= most)
135
+ break;
136
+ const { point, normal } = surfacePoint(view, place.lat, place.lon);
137
+ const sight = view.eye !== undefined ? [view.eye[0] - point[0], view.eye[1] - point[1], view.eye[2] - point[2]] : (view.toward ?? [0, 0, 1]);
138
+ const len = Math.hypot(sight[0], sight[1], sight[2]) || 1;
139
+ // a little short of the limb: a mark seen edge-on is a crowd of dots on the rim, and the surface there is a sliver
140
+ if (dot(normal, sight) / len < 0.12)
141
+ continue;
142
+ const at = view.project(point);
143
+ if (at === null || at.x < 0 || at.y < 0 || at.x > view.width || at.y > view.height)
144
+ continue;
145
+ if (out.some((mark) => Math.hypot(mark.x - at.x, mark.y - at.y) < MARK_RULES.gapPx))
146
+ continue;
147
+ out.push({ place, layer: layerOf(place.kind), x: at.x, y: at.y });
148
+ }
149
+ return out;
150
+ }
151
+ /**
152
+ * The turn of a body that brings a place to the middle of the disc, for a view that is turned by a yaw about the body's pole and then a tilt about the screen's x axis, looking along -z (the `<wakusei-body>` view):
153
+ * the yaw and the tilt in degrees. Where the pole is tipped so far that no yaw puts the place in the plane the tilt turns, it comes as near as it can.
154
+ *
155
+ * @param pole - The body's north pole in the view before the turn.
156
+ * @param meridian - Its longitude 0 on the equator.
157
+ * @param lat - The place's latitude, degrees.
158
+ * @param lon - Its east longitude, degrees.
159
+ * @returns `{ yaw, tilt }`: the yaw about the pole, and the turn about the x axis (positive brings the place's y down to the middle).
160
+ *
161
+ * @example
162
+ * ```ts
163
+ * import { turnToPlace } from "@johnmorrisdotca/wakusei-planets/places";
164
+ *
165
+ * const turn = turnToPlace([0, 1, 0], [0, 0, 1], 0, 90);
166
+ * console.log(turn.yaw.toFixed(0), turn.tilt.toFixed(0));
167
+ * ```
168
+ */
169
+ export function turnToPlace(pole, meridian, lat, lon) {
170
+ const e = [pole[1] * meridian[2] - pole[2] * meridian[1], pole[2] * meridian[0] - pole[0] * meridian[2], pole[0] * meridian[1] - pole[1] * meridian[0]];
171
+ const c = Math.cos(lat * RAD);
172
+ const d = [0, 1, 2].map((i) => c * Math.cos(lon * RAD) * meridian[i] + c * Math.sin(lon * RAD) * e[i] + Math.sin(lat * RAD) * pole[i]);
173
+ // turning d about the pole by `yaw` (Rodrigues): x = A cos + B sin + C. Choose the yaw that makes x zero and leaves the place on the near side.
174
+ const pd = dot(pole, d);
175
+ const A = d[0] - pole[0] * pd;
176
+ const B = pole[1] * d[2] - pole[2] * d[1];
177
+ const C = pole[0] * pd;
178
+ const R = Math.hypot(A, B) || 1e-12;
179
+ const phi = Math.atan2(B, A);
180
+ const spread = Math.acos(Math.max(-1, Math.min(1, -C / R)));
181
+ const turned = (yaw) => {
182
+ const cs = Math.cos(yaw);
183
+ const sn = Math.sin(yaw);
184
+ const k = pole;
185
+ const kv = [k[1] * d[2] - k[2] * d[1], k[2] * d[0] - k[0] * d[2], k[0] * d[1] - k[1] * d[0]];
186
+ return [d[0] * cs + kv[0] * sn + k[0] * pd * (1 - cs), d[1] * cs + kv[1] * sn + k[1] * pd * (1 - cs), d[2] * cs + kv[2] * sn + k[2] * pd * (1 - cs)];
187
+ };
188
+ const a = turned(phi + spread);
189
+ const b = turned(phi - spread);
190
+ const [yaw, d1] = a[2] >= b[2] ? [phi + spread, a] : [phi - spread, b];
191
+ const tilt = Math.atan2(d1[1], d1[2]);
192
+ const wrap = (deg) => ((((deg + 180) % 360) + 360) % 360) - 180;
193
+ return { yaw: wrap(yaw / RAD), tilt: tilt / RAD };
194
+ }
@@ -1,6 +1,6 @@
1
1
  import type { Place } from "../places.ts";
2
2
  /**
3
- * Every place on the Mars the package knows: the sites first, by date, then the named features by size. A place's `kind` says what it is (`placesOfKind` from `/places` picks by it).
3
+ * Every place on the Mars the package knows: the sites first, by date, then the planned landings, then the named features by size. A place's `kind` says what it is (`placesOfKind` from `/places` picks by it).
4
4
  *
5
5
  * @example
6
6
  * ```ts
@@ -1,7 +1,9 @@
1
1
  // The places on the Mars: landing, rover, crash and impact sites, and the named features, as one list. See `/places` for the record's shape and the lookups.
2
2
  import { PLACES_MARS as DATA } from "../data/places-mars.js";
3
+ import { PLANNED_MARS } from "../data/places-planned.js";
4
+ const ALL = DATA;
3
5
  /**
4
- * Every place on the Mars the package knows: the sites first, by date, then the named features by size. A place's `kind` says what it is (`placesOfKind` from `/places` picks by it).
6
+ * Every place on the Mars the package knows: the sites first, by date, then the planned landings, then the named features by size. A place's `kind` says what it is (`placesOfKind` from `/places` picks by it).
5
7
  *
6
8
  * @example
7
9
  * ```ts
@@ -10,4 +12,4 @@ import { PLACES_MARS as DATA } from "../data/places-mars.js";
10
12
  * console.log(PLACES_MARS.length, "places;", PLACES_MARS.filter((place) => place.kind === "landing").length, "landings");
11
13
  * ```
12
14
  */
13
- export const PLACES_MARS = DATA;
15
+ export const PLACES_MARS = [...ALL.filter((place) => place.kind !== "feature"), ...PLANNED_MARS, ...ALL.filter((place) => place.kind === "feature")];