@johnmorrisdotca/wakusei-planets 0.40.0 → 0.42.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.
@@ -226,13 +226,62 @@ export declare function travelPlan(input: TravelInput): TravelPlan;
226
226
  * ```
227
227
  */
228
228
  export declare function travelPose(input: TravelInput, t: number, plan?: TravelPlan): TravelSample;
229
+ /**
230
+ * One body that might keep an arrival from being an empty black picture: where it is, and which rule it belongs to.
231
+ * `1` is the body's own moon, or its planet when the body is a moon. `2` is the body the camera came from, or the nearest planet in that direction. `3` is the Sun.
232
+ *
233
+ * @example
234
+ * ```ts
235
+ * import type { ArrivalCompanion } from "@johnmorrisdotca/wakusei-planets/space";
236
+ *
237
+ * const moon: ArrivalCompanion = { id: "io", at: [10, 0, 0], rank: 1 };
238
+ * console.log(moon.rank);
239
+ * ```
240
+ */
241
+ export interface ArrivalCompanion {
242
+ /** A name the page can put on the marker. Left out, the side is still chosen. */
243
+ id?: string;
244
+ at: Vec3;
245
+ rank: 1 | 2 | 3;
246
+ }
247
+ /**
248
+ * The side of a body to stand on so a companion is in the picture, and the body stays at least half lit.
249
+ * Companions are tried in rank order (1, then 2, then 3). The first rank that fits wins; within a rank the side nearest the approach is kept.
250
+ * A side more than 90 degrees from the Sun is refused, so a lit globe is never traded for a black one. With no companion that fits, the side is the
251
+ * approach, swung toward the Sun when it would show less than about three fifths of the body lit (never more than 75 degrees off the Sun).
252
+ *
253
+ * @param input - The body, where the move comes from, the Sun, and the companions in rank order.
254
+ * @returns A unit side from the body toward the camera, how much of the disc is lit, and the companion's id (null when none was framed).
255
+ *
256
+ * @example
257
+ * ```ts
258
+ * import { arrivalSide } from "@johnmorrisdotca/wakusei-planets/space";
259
+ *
260
+ * const side = arrivalSide({ body: [0, 0, 0], radius: 1, from: [0, 0, 20], sun: [0, 0, 100], companions: [{ id: "io", at: [8, 1, 0], rank: 1 }] });
261
+ * console.log(side.companion, side.lit >= 0.5);
262
+ * ```
263
+ */
264
+ export declare function arrivalSide(input: {
265
+ body: Vec3;
266
+ radius: number;
267
+ from: Vec3;
268
+ sun?: Vec3;
269
+ companions?: readonly ArrivalCompanion[];
270
+ fov?: number;
271
+ aspect?: number;
272
+ fit?: number;
273
+ }): {
274
+ side: Vec3;
275
+ lit: number;
276
+ companion: string | null;
277
+ };
229
278
  /**
230
279
  * 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
231
- * 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
232
- * taller than wide), looking at the body with `north` up.
280
+ * 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), and further round, still at least half lit, when that puts a
281
+ * companion in the picture (`arrivalSide`). It stands at `fit` radii away (4.1, and as much again as a tall picture is taller than wide), looking at the body with `north` up.
233
282
  *
234
- * @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`.
235
- * @returns The view, and `lit`, the share of the disc that is lit from there (1 when no Sun is given).
283
+ * @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, the companions to keep in the picture, and optionally `fov`, `fit` and `north`.
284
+ * @returns The view, `lit` (the share of the disc that is lit from there, 1 when no Sun is given), and `companion` (the id framed beside it, or null).
236
285
  *
237
286
  * @example
238
287
  * ```ts
@@ -251,6 +300,8 @@ export declare function travelArrival(input: {
251
300
  fov?: number;
252
301
  fit?: number;
253
302
  north?: Vec3;
303
+ companions?: readonly ArrivalCompanion[];
254
304
  }): TravelView & {
255
305
  lit: number;
306
+ companion: string | null;
256
307
  };
@@ -561,13 +561,111 @@ export function travelPose(input, t, plan) {
561
561
  sample.around = sightOf(input.around, time, view.position, o, frame);
562
562
  return sample;
563
563
  }
564
+ /**
565
+ * The side of a body to stand on so a companion is in the picture, and the body stays at least half lit.
566
+ * Companions are tried in rank order (1, then 2, then 3). The first rank that fits wins; within a rank the side nearest the approach is kept.
567
+ * A side more than 90 degrees from the Sun is refused, so a lit globe is never traded for a black one. With no companion that fits, the side is the
568
+ * approach, swung toward the Sun when it would show less than about three fifths of the body lit (never more than 75 degrees off the Sun).
569
+ *
570
+ * @param input - The body, where the move comes from, the Sun, and the companions in rank order.
571
+ * @returns A unit side from the body toward the camera, how much of the disc is lit, and the companion's id (null when none was framed).
572
+ *
573
+ * @example
574
+ * ```ts
575
+ * import { arrivalSide } from "@johnmorrisdotca/wakusei-planets/space";
576
+ *
577
+ * const side = arrivalSide({ body: [0, 0, 0], radius: 1, from: [0, 0, 20], sun: [0, 0, 100], companions: [{ id: "io", at: [8, 1, 0], rank: 1 }] });
578
+ * console.log(side.companion, side.lit >= 0.5);
579
+ * ```
580
+ */
581
+ export function arrivalSide(input) {
582
+ let approach = sub(input.from, input.body);
583
+ approach = length(approach) < 1e-9 ? [0, 0, 1] : unit(approach);
584
+ const sunFar = input.sun ? sub(input.sun, input.body) : null;
585
+ const sunDir = sunFar && length(sunFar) > 1e-9 ? unit(sunFar) : null;
586
+ const litOf = (side) => (sunDir ? (1 + Math.cos(Math.min(Math.PI, angleOf(side, sunDir)))) / 2 : 1);
587
+ const swing = (side) => {
588
+ if (!sunDir)
589
+ return side;
590
+ const off = angleOf(side, sunDir);
591
+ const most = 75 * RAD;
592
+ return off > most ? arc(side, sunDir, (off - most) / off) : side;
593
+ };
594
+ const distance = Math.max(1e-6, input.radius) * (input.fit ?? 4.1) * Math.max(1, 1 / Math.max(1e-3, input.aspect ?? 1));
595
+ const fov = ((input.fov ?? 35) / 2) * RAD;
596
+ const tv = Math.tan(fov);
597
+ const aspect = Math.max(1e-3, input.aspect ?? 1);
598
+ const frames = (side, at) => {
599
+ const forward = scale(side, -1);
600
+ const north = sub([0, 1, 0], scale(forward, forward[1]));
601
+ const up = length(north) < 1e-6 ? squareTo(forward) : unit(north);
602
+ const camera = add(input.body, scale(side, distance));
603
+ const v = sub(at, camera);
604
+ const z = dot(v, forward);
605
+ if (!(z > 0))
606
+ return false;
607
+ if (Math.abs(dot(v, cross(forward, up))) > 0.92 * tv * aspect * z)
608
+ return false;
609
+ if (Math.abs(dot(v, up)) > 0.92 * tv * z)
610
+ return false;
611
+ const limb = Math.atan(input.radius / distance);
612
+ return angleOf(forward, unit(v)) > limb * 1.15;
613
+ };
614
+ const found = [];
615
+ const half = Math.PI / 2 + 1e-4;
616
+ const companions = input.companions ?? [];
617
+ const consider = (side) => {
618
+ const u = length(side) < 1e-9 ? approach : unit(side);
619
+ if (sunDir && angleOf(u, sunDir) > half)
620
+ return;
621
+ let rank = 99;
622
+ let id = null;
623
+ for (const companion of companions) {
624
+ if (companion.rank < rank && frames(u, companion.at)) {
625
+ rank = companion.rank;
626
+ id = companion.id ?? null;
627
+ }
628
+ }
629
+ if (rank === 99)
630
+ return;
631
+ const near = angleOf(u, approach);
632
+ const top = found[0];
633
+ if (top === undefined || rank < top.rank || (rank === top.rank && near < top.near - 1e-6))
634
+ found[0] = { side: u, rank, near, id };
635
+ };
636
+ consider(approach);
637
+ for (const companion of companions) {
638
+ const away = sub(input.body, companion.at);
639
+ if (length(away) < 1e-6)
640
+ continue;
641
+ const u = unit(away);
642
+ const perp = Math.abs(u[1]) < 0.9 ? unit(cross(u, [0, 1, 0])) : unit(cross(u, [1, 0, 0]));
643
+ const perp2 = unit(cross(u, perp));
644
+ for (const deg of [8, 14, 22, 32, 48, 70]) {
645
+ const e = deg * RAD;
646
+ for (const axis of [perp, perp2]) {
647
+ consider(add(scale(u, Math.cos(e)), scale(axis, Math.sin(e))));
648
+ consider(add(scale(u, Math.cos(e)), scale(axis, -Math.sin(e))));
649
+ }
650
+ }
651
+ }
652
+ for (let lat = -75; lat <= 75; lat += 15) {
653
+ for (let lon = 0; lon < 360; lon += 15) {
654
+ const c = Math.cos(lat * RAD);
655
+ consider([c * Math.cos(lon * RAD), Math.sin(lat * RAD), c * Math.sin(lon * RAD)]);
656
+ }
657
+ }
658
+ const picked = found[0];
659
+ const side = picked?.side ?? swing(approach);
660
+ return { side, lit: litOf(side), companion: picked?.id ?? null };
661
+ }
564
662
  /**
565
663
  * 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.
664
+ * 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), and further round, still at least half lit, when that puts a
665
+ * companion in the picture (`arrivalSide`). It stands at `fit` radii away (4.1, and as much again as a tall picture is taller than wide), looking at the body with `north` up.
568
666
  *
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).
667
+ * @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, the companions to keep in the picture, and optionally `fov`, `fit` and `north`.
668
+ * @returns The view, `lit` (the share of the disc that is lit from there, 1 when no Sun is given), and `companion` (the id framed beside it, or null).
571
669
  *
572
670
  * @example
573
671
  * ```ts
@@ -578,20 +676,11 @@ export function travelPose(input, t, plan) {
578
676
  * ```
579
677
  */
580
678
  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
- }
679
+ const picked = arrivalSide(input);
680
+ const side = picked.side;
592
681
  const distance = input.radius * (input.fit ?? 4.1) * Math.max(1, 1 / Math.max(1e-3, input.aspect));
593
682
  const forward = scale(side, -1);
594
683
  const north = input.north ?? [0, 1, 0];
595
684
  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 };
685
+ return { position: add(input.body, scale(side, distance)), forward, up: length(across) < 1e-6 ? squareTo(forward) : unit(across), fov: input.fov ?? 35, lit: picked.lit, companion: picked.companion };
597
686
  }
Binary file
Binary file
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "0.40.0";
1
+ export declare const VERSION = "0.42.0";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "0.40.0";
1
+ export const VERSION = "0.42.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnmorrisdotca/wakusei-planets",
3
- "version": "0.40.0",
3
+ "version": "0.42.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",