@johnmorrisdotca/wakusei-planets 0.54.0 → 0.56.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.
@@ -50,7 +50,7 @@ const unit = (a) => {
50
50
  return n === 0 ? [0, 0, 0] : [a[0] / n, a[1] / n, a[2] / n];
51
51
  };
52
52
  /**
53
- * 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.
53
+ * How near a move may come to a body, in that body's radii above its surface: a third of a radius (0.35), a margin so the camera never flies through a body. Bodies near the way are passed, not routed round: a body passed close is one more thing to see on the way. A move that starts or ends nearer than one and a half times that keeps two thirds of the nearer distance.
54
54
  *
55
55
  * @example
56
56
  * ```ts
@@ -59,7 +59,7 @@ const unit = (a) => {
59
59
  * console.log(TRAVEL_CLEARANCE);
60
60
  * ```
61
61
  */
62
- export const TRAVEL_CLEARANCE = 1;
62
+ export const TRAVEL_CLEARANCE = 0.35;
63
63
  /**
64
64
  * The time in milliseconds, counted from the start of the plan, that the space view lets the planner spend on the other ways of a move within a planet's family (`TravelInput.budget`); the first way, with its aim
65
65
  * turn, is always worked out. Measured on the author's Mac mini over the 360 surveyed plain-view moves of six pairs of moons (most need no other way): the first way alone takes a median of 4 ms, 90 percent under
@@ -307,7 +307,19 @@ function clearOf(route, position, t) {
307
307
  }
308
308
  function along(route, t) {
309
309
  const inTurn = route.turn !== null && t < route.share;
310
- const at = inTurn ? turnAt(route.turn, ease(t / route.share), t, clamp01(t / route.share)) : legAt(route.leg, ease(route.share >= 1 ? 1 : (t - route.share) / (1 - route.share)), t);
310
+ let at;
311
+ if (inTurn) {
312
+ at = turnAt(route.turn, ease(t / route.share), t, clamp01(t / route.share));
313
+ if (t > route.legStart) {
314
+ // the way on has started while the turn is still going: the camera goes where the turn takes it and, as well, as far along the way on as that has got, so the two motions add and there is no stop and no corner where one gives way to the other
315
+ const q = ease((t - route.legStart) / (1 - route.legStart));
316
+ const now = legAt(route.leg, q, t);
317
+ const start = legAt(route.leg, 0, t);
318
+ at = { ...at, position: add(at.position, sub(now.position, start.position)) };
319
+ }
320
+ }
321
+ else
322
+ at = legAt(route.leg, ease(route.legStart >= 1 ? 1 : (t - route.legStart) / (1 - route.legStart)), t);
311
323
  return { position: clearOf(route, at.position, t), o: at.o, fov: at.fov, stage: inTurn ? "turn" : t < route.arrive ? "travel" : "arrive" };
312
324
  }
313
325
  function sightOf(body, t, position, o, frame) {
@@ -319,7 +331,12 @@ function sightOf(body, t, position, o, frame) {
319
331
  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 };
320
332
  }
321
333
  /** The pivot's stand at the end of the turn, for a moment `tm` of the move. */
322
- function standAt(input, tm, family, variant = 0) {
334
+ function standAt(input, tm, family, variant = 0, framed = false) {
335
+ if (framed && !family) {
336
+ const picture = frameStand(input, tm);
337
+ if (picture !== null)
338
+ return picture;
339
+ }
323
340
  const o0 = facingOf(input.start);
324
341
  const o1 = facingOf(input.end);
325
342
  const clearance = input.clearance ?? TRAVEL_CLEARANCE;
@@ -378,7 +395,8 @@ function standAt(input, tm, family, variant = 0) {
378
395
  if (variant === 0 || missOf(stand, last, v) >= Math.min(1.52 * (1 + clearance) * pivot.radius, 0.9 * length(fallback), 0.98 * length(apex)))
379
396
  straight = fallback;
380
397
  }
381
- if (variant >= 2)
398
+ // variant 1 of a move that is not within a planet's family is the swing: straight at the destination from the nearest place the turn leaves the camera in, the look direction turned round it at the end, with no backing off to a stand on the line of the last view
399
+ if (variant >= 2 || (variant === 1 && !family))
382
400
  straight = null;
383
401
  if (straight !== null) {
384
402
  const dm = Math.max(1e-12, length(straight));
@@ -459,6 +477,135 @@ function standAt(input, tm, family, variant = 0) {
459
477
  },
460
478
  };
461
479
  }
480
+ /** The share of the picture's height the body left keeps at the stand of a framed move (`frameStand`), and the least and most it may keep. */
481
+ const FRAME_HEIGHT = 0.5;
482
+ const FRAME_LEAST = 0.4;
483
+ const FRAME_MOST = 0.6;
484
+ /** The most the line between the two bodies may be tilted from the picture's long axis at the stand, in radians. */
485
+ const FRAME_TILT = 0.6;
486
+ /** Where the stand frames the pair along their line in the picture: the body left this share of the way from the middle to the far edge, so the other body is on the other side. */
487
+ const FRAME_SPLIT = 0.45;
488
+ /** The distance at which a body of radius `r` keeps `share` of the picture's height. */
489
+ const distanceFor = (r, share, tv) => {
490
+ const t = Math.max(1e-6, share * tv);
491
+ return r / (t / Math.sqrt(1 + t * t));
492
+ };
493
+ const FRAME_SPHERE = (() => {
494
+ const points = [];
495
+ const n = 240;
496
+ for (let i = 0; i < n; i += 1) {
497
+ const z = 1 - (2 * (i + 0.5)) / n;
498
+ const r = Math.sqrt(1 - z * z);
499
+ const a = i * 2.399963229728653;
500
+ points.push([r * Math.cos(a), r * Math.sin(a), z]);
501
+ }
502
+ return points;
503
+ })();
504
+ /**
505
+ * The stand of a framed move (B1): the camera backs up a little from the body left, until it keeps about half the picture's height, and goes round it only as far as it takes for the other body to be in the picture
506
+ * too, on the other side of the middle: the body left large on one side, the destination small on the other, whichever side needs less movement. The look direction is the one that gives that picture, so the whole turn is
507
+ * made before the way on and the way on starts from a picture that holds both. Null where there is no such picture (the destination too near the body left to leave its disc, or the picture too narrow).
508
+ */
509
+ function frameStand(input, tm) {
510
+ const pivot = input.leaving;
511
+ if (pivot.reach !== undefined || input.destination.reach !== undefined)
512
+ return null;
513
+ const o0 = facingOf(input.start);
514
+ const o1 = facingOf(input.end);
515
+ const frame = frameOf(input.end.fov, input.aspect);
516
+ const wide = frame.th >= frame.tv;
517
+ const clearance = input.clearance ?? TRAVEL_CLEARANCE;
518
+ const centre = placeOf(pivot, tm);
519
+ const b = placeOf(input.destination, tm);
520
+ const e1 = sub(input.end.position, placeOf(input.destination, 1));
521
+ const last = add(b, e1);
522
+ const fromPivot = sub(pivot.from, input.start.position);
523
+ const d0 = Math.max(1e-12, length(fromPivot));
524
+ const near = distanceFor(pivot.radius, FRAME_MOST, frame.tv);
525
+ const far = distanceFor(pivot.radius, FRAME_LEAST, frame.tv);
526
+ const dm = d0 < near ? distanceFor(pivot.radius, FRAME_HEIGHT, frame.tv) : d0;
527
+ const share = pivot.radius / dm;
528
+ const tanL = share / Math.sqrt(Math.max(1e-12, 1 - share * share));
529
+ const n0 = unit(scale(fromPivot, -1));
530
+ // the camera at `n` round the body left, looking so that the two bodies are either side of the middle: its picture, or null if the picture does not do
531
+ const standAtN = (n) => {
532
+ const stand = add(centre, scale(n, dm));
533
+ const toB = sub(b, stand);
534
+ if (length(toB) < 1e-9)
535
+ return null;
536
+ const dirL = scale(n, -1);
537
+ const dirB = unit(toB);
538
+ const apart = angleOf(dirL, dirB);
539
+ if (apart < 1e-6)
540
+ return null;
541
+ const f = arc(dirL, dirB, FRAME_SPLIT);
542
+ const pick = o0.u;
543
+ const across = sub(pick, scale(f, dot(pick, f)));
544
+ const o = { f, u: length(across) < 1e-6 ? squareTo(f) : unit(across) };
545
+ const sl = toCamera(o, sub(centre, stand));
546
+ const sb = toCamera(o, toB);
547
+ if (!(sl[2] > 0) || !(sb[2] > 0))
548
+ return null;
549
+ const xl = sl[0] / (sl[2] * frame.th);
550
+ const yl = sl[1] / (sl[2] * frame.tv);
551
+ const xb = sb[0] / (sb[2] * frame.th);
552
+ const yb = sb[1] / (sb[2] * frame.tv);
553
+ // the disc of the body left inside the picture, the destination's place inside it, and the destination clear of the disc
554
+ if (Math.abs(xl) + tanL / frame.th > 1 || Math.abs(yl) + tanL / frame.tv > 1)
555
+ return null;
556
+ if (Math.abs(xb) > 0.85 || Math.abs(yb) > 0.85)
557
+ return null;
558
+ const dx = (xb - xl) * frame.th;
559
+ const dy = (yb - yl) * frame.tv;
560
+ const gap = Math.hypot(dx, dy);
561
+ if (gap < 1.2 * tanL)
562
+ return null;
563
+ const tilt = wide ? Math.atan2(Math.abs(dy), Math.abs(dx)) : Math.atan2(Math.abs(dx), Math.abs(dy));
564
+ if (tilt > FRAME_TILT)
565
+ return null;
566
+ if (missOf(stand, last, centre) < Math.min(1.05 * (1 + clearance) * pivot.radius, 0.9 * dm))
567
+ return null;
568
+ return { stand, o, cost: angleOf(n0, n) + 0.8 * tilt };
569
+ };
570
+ let best = null;
571
+ const here = standAtN(n0);
572
+ if (here !== null)
573
+ best = { n: n0, cost: here.cost };
574
+ else {
575
+ for (const n of FRAME_SPHERE) {
576
+ const found = standAtN(n);
577
+ if (found !== null && (best === null || found.cost < best.cost))
578
+ best = { n, cost: found.cost };
579
+ }
580
+ if (best === null)
581
+ return null;
582
+ // the least turn about the body left that is still a good picture: along the way from where the camera is to the best found
583
+ let lo = 0;
584
+ let hi = 1;
585
+ const goal = best.n;
586
+ for (let i = 0; i < 14; i += 1) {
587
+ const mid = (lo + hi) / 2;
588
+ if (standAtN(arc(n0, goal, mid)) !== null)
589
+ hi = mid;
590
+ else
591
+ lo = mid;
592
+ }
593
+ best = { n: arc(n0, goal, hi), cost: 0 };
594
+ }
595
+ const at = standAtN(best.n);
596
+ if (at === null)
597
+ return null;
598
+ const { stand, o } = at;
599
+ const from = sub(stand, b);
600
+ const lengthOn = length(sub(e1, from));
601
+ const leaving = placeOf(input.leaving, tm);
602
+ const d1 = Math.max(1e-12, length(e1));
603
+ void far;
604
+ return {
605
+ turn: { pivot, swing: swingOf(o0, o), u0: unit(toCamera(o0, fromPivot)), u1: unit(toCamera(o, sub(centre, stand))), d0, d1: dm, lift: 0, fov0: input.start.fov, fov1: input.end.fov, framed: true },
606
+ leg: { about: input.destination, type: "line", swing: swingOf(o, o1), fov0: input.end.fov, fov1: input.end.fov, from, to: e1, d0: length(from), d1, dn: d1, u0: unit(toCamera(o0, fromPivot)), u1: unit(toCamera(o0, fromPivot)), l: lengthOn, ea: Math.max(1e-9, Math.min(dm, length(sub(leaving, stand)))), eb: d1 },
607
+ };
608
+ }
462
609
  function lineLeg(input, about, watch) {
463
610
  const from = sub(input.start.position, about.from);
464
611
  const to = sub(input.end.position, placeOf(about, 1));
@@ -637,7 +784,7 @@ function backOffKit(input, turn, share) {
637
784
  return missed;
638
785
  };
639
786
  }
640
- function routeBase(input, variant = 0) {
787
+ function routeBase(input, variant = 0, framed = false) {
641
788
  const wanted = input.reduced ? "cut" : (input.kind ?? "turn-travel");
642
789
  const kind = wanted === "auto" ? "turn-travel" : wanted;
643
790
  const clearance = input.clearance ?? TRAVEL_CLEARANCE;
@@ -649,7 +796,7 @@ function routeBase(input, variant = 0) {
649
796
  const easing = { turn: "smooth-step", distance: "geometric" };
650
797
  if (kind === "cut") {
651
798
  const leg = lineLeg(input, input.destination, undefined);
652
- 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 } };
799
+ return { route: { ...base, cut: true, turn: null, share: 0, legStart: 0, arrive: 0, leg }, plan: { kind, path: "cut", stages: { turn: 0, travel: 0, arrive: 1 }, duration: 0, turn: least, settle: 0, blend: 0, easing } };
653
800
  }
654
801
  let turn = null;
655
802
  let leg;
@@ -667,7 +814,7 @@ function routeBase(input, variant = 0) {
667
814
  const d0 = length(sub(input.leaving.from, input.start.position));
668
815
  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);
669
816
  const open = missOf(input.start.position, input.end.position, input.leaving.from) >= Math.min(1.52 * (1 + clearance) * input.leaving.radius, 0.9 * d0);
670
- 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, variant);
817
+ 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, variant, framed);
671
818
  // 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)
672
819
  let tm = 0.25;
673
820
  for (let pass = 0; pass < 8; pass += 1) {
@@ -676,7 +823,7 @@ function routeBase(input, variant = 0) {
676
823
  if ((facing && open) || share === 0 || Math.abs(share - tm) < 0.002)
677
824
  break;
678
825
  tm = pass === 0 ? share : (tm + share) / 2;
679
- stand = standAt(input, tm, family, variant);
826
+ stand = standAt(input, tm, family, variant, framed);
680
827
  }
681
828
  if (share > 0 && !(facing && open))
682
829
  share = tm;
@@ -746,8 +893,8 @@ function routeBase(input, variant = 0) {
746
893
  hi = mid;
747
894
  }
748
895
  const arrive = share + (1 - share) * Math.min(0.9, Math.max(0.6, hi));
749
- const route = { ...base, cut: false, turn, share, arrive, leg };
750
- 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 } };
896
+ const route = { ...base, cut: false, turn, share, legStart: share, arrive, leg };
897
+ 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, blend: 0, easing } };
751
898
  }
752
899
  const FAULT_DRIFT = 0.02;
753
900
  const FAULT_STEP = 0.3;
@@ -756,7 +903,7 @@ function faultsOf(route) {
756
903
  const plan = { route };
757
904
  const at = (t) => travelPose(input, t, plan);
758
905
  const last = at(1);
759
- const faults = { empty: 0, lost: 0, drift: 0, driftWorst: 0, step: 0, pace: 0, planet: 0 };
906
+ const faults = { empty: 0, lost: 0, drift: 0, driftWorst: 0, step: 0, pace: 0, planet: 0, held: 0 };
760
907
  const lastF = unit(input.end.forward);
761
908
  let turned = 0;
762
909
  let way = 0;
@@ -769,6 +916,8 @@ function faultsOf(route) {
769
916
  faults.empty += 1;
770
917
  if (s.around !== undefined && !s.around.inside)
771
918
  faults.planet += 1;
919
+ if (i / 240 < route.share && !s.leaving.inside && !(s.around?.inside === true))
920
+ faults.held += 1;
772
921
  if (i >= afterTurn && !s.destination.inside)
773
922
  faults.lost += 1;
774
923
  if (i > afterTurn) {
@@ -866,6 +1015,90 @@ function withAim(input, first, stop) {
866
1015
  const total = Math.max(t0 / share, first.plan.duration / Math.max(0.05, input.pace ?? 1));
867
1016
  return { route: aimed, plan: { ...first.plan, duration: Math.round(Math.min(9000, Math.max(1200, total)) * Math.max(0.05, input.pace ?? 1)) } };
868
1017
  }
1018
+ /** How much of the turn the way on may overlap, tried from the most, and how much of the shorter stage's time the overlap saves. */
1019
+ const BLENDS = [1, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1];
1020
+ const BLEND_SAVES = 0.6;
1021
+ /** The moment the arrival starts for a way on that starts at `legStart`. */
1022
+ const arriveFor = (leg, legStart) => {
1023
+ const near = leg.type === "line" ? 2 * leg.eb : leg.dn - leg.d1;
1024
+ let lo = 0;
1025
+ let hi = 1;
1026
+ for (let i = 0; i < 30; i += 1) {
1027
+ const mid = (lo + hi) / 2;
1028
+ if (leftOf(ease(mid), leg.l, leg.ea, leg.eb) > near)
1029
+ lo = mid;
1030
+ else
1031
+ hi = mid;
1032
+ }
1033
+ return legStart + (1 - legStart) * Math.min(0.9, Math.max(0.6, hi));
1034
+ };
1035
+ /** The most the look direction or the picture's up turns between two moments of a walk of the move (a 240th apart), in degrees. */
1036
+ function steepest(route) {
1037
+ const plan = { route };
1038
+ let before = travelPose(route.input, 0, plan);
1039
+ let worst = 0;
1040
+ for (let i = 1; i <= 240; i += 1) {
1041
+ const s = travelPose(route.input, i / 240, plan);
1042
+ worst = Math.max(worst, angleOf(before.forward, s.forward) / RAD, angleOf(before.up, s.up) / RAD);
1043
+ before = s;
1044
+ }
1045
+ return worst;
1046
+ }
1047
+ /** The most, in times its distance at the start, that the camera is from the body left (or from the planet circled) at any moment of a walk of the move: 1 or less is a move that never goes out beyond where it began. */
1048
+ function backOff(route) {
1049
+ const input = route.input;
1050
+ const plan = { route };
1051
+ const pivot = input.around ?? input.leaving;
1052
+ const d0 = Math.max(1e-12, length(sub(input.start.position, pivot.from)));
1053
+ let most = 0;
1054
+ for (let i = 0; i <= 120; i += 1)
1055
+ most = Math.max(most, length(sub(travelPose(input, i / 120, plan).position, placeOf(pivot, i / 120))) / d0);
1056
+ return most;
1057
+ }
1058
+ /** The most, in times its distance from the body left at the start, that the camera may back off from it before the move is taken as a B3 (a pull back to a system that holds both) and the straight way is tried. */
1059
+ const BACK_UP = 1.2;
1060
+ /** The furthest from the middle of the picture the nearer of the two bodies gets in the turn (1 is the edge of the picture, infinity is neither in it at some moment). */
1061
+ function turnEdge(route) {
1062
+ const plan = { route };
1063
+ const off = (sight) => (sight.inside ? Math.max(Math.abs(sight.x), Math.abs(sight.y)) : Number.POSITIVE_INFINITY);
1064
+ let edge = 0;
1065
+ for (let i = 0; i <= 240; i += 1) {
1066
+ if (i / 240 >= route.share)
1067
+ break;
1068
+ const s = travelPose(route.input, i / 240, plan);
1069
+ edge = Math.max(edge, Math.min(off(s.leaving), off(s.destination)));
1070
+ }
1071
+ return edge;
1072
+ }
1073
+ /** `now` has none of any kind of fault more than `was` (the planet out of the picture only where it was in all through, and the drift's worst no worse). */
1074
+ const noWorse = (now, was) => now.empty <= was.empty && now.lost <= was.lost && now.drift <= was.drift && now.step <= was.step && now.pace <= was.pace && now.held <= was.held && now.driftWorst <= was.driftWorst + 1e-12 && (was.planet > 0 || now.planet === 0) && total(now) <= total(was);
1075
+ /**
1076
+ * The way on starts while the turn is still going, so the camera never stops between the two and never turns a corner: its place is where the turn takes it plus as far along the way on as that has got, and both
1077
+ * stages ease in and out, so the speed does not drop to nothing in between and the direction of travel bends over the overlap instead of changing at once. The turn ends where it did, so the look direction, the
1078
+ * stand and every picture from the end of the turn on are the same shape; the overlap is the largest of `BLENDS` that leaves no kind of fault more than the move had, and none if none does. The move is shorter by the
1079
+ * time the two stages now share.
1080
+ */
1081
+ function blended(first) {
1082
+ const route = first.route;
1083
+ // a way on that turns round the destination as it goes keeps its own timing: its look direction follows the distance, which an earlier start would make jump
1084
+ if (route.cut || route.turn === null || route.leg.type !== "line" || route.share <= 0 || route.share >= 1)
1085
+ return first;
1086
+ const was = faultsOf(route);
1087
+ const rate = Math.max(1.25 * steepest(route), 3);
1088
+ for (const share of BLENDS) {
1089
+ const legStart = route.share * (1 - share);
1090
+ const candidate = { ...route, legStart, arrive: arriveFor(route.leg, legStart) };
1091
+ if (!noWorse(faultsOf(candidate), was) || steepest(candidate) > rate)
1092
+ continue;
1093
+ // a turn made by an aim point keeps the nearer body inside `AIM_EDGE` of the way to the edge, and the overlap must not take it out
1094
+ if (route.turn.aim !== undefined && turnEdge(candidate) > AIM_EDGE)
1095
+ continue;
1096
+ const pace = Math.max(0.05, route.input.pace ?? 1);
1097
+ const duration = Math.round(Math.max(1200, Math.round(first.plan.duration / pace) * (1 - BLEND_SAVES * share * Math.min(route.share, 1 - route.share))) * pace);
1098
+ return { route: candidate, plan: { ...first.plan, duration, blend: share, stages: { turn: route.share, travel: candidate.arrive - route.share, arrive: 1 - candidate.arrive } } };
1099
+ }
1100
+ return first;
1101
+ }
869
1102
  /**
870
1103
  * The route of a move. A move within one planet's family (the path `circle`) is first made as it always was. If a walk of it has a picture with neither the body left nor the destination in it, its turn is made again by an
871
1104
  * aim point (see `Aim`): the turn takes the same share of the move and ends in the same place, so the way on and the arrival are the same, sample for sample, and only the turn is new. The new turn is taken when
@@ -880,7 +1113,7 @@ function routeOf(input) {
880
1113
  return kept;
881
1114
  // a plan is kept for the input it was made for, so the input is frozen (its top level: a field set after planning would be a plan of something else; the views and bodies are not to be changed either)
882
1115
  Object.freeze(input);
883
- const made = routeMade(input);
1116
+ const made = blended(routeMade(input));
884
1117
  routes.set(input, made);
885
1118
  return made;
886
1119
  }
@@ -889,7 +1122,20 @@ function routeMade(input) {
889
1122
  // before the other ways were tried and what they are measured against
890
1123
  const started = input.budget?.now();
891
1124
  const stop = input.budget === undefined || started === undefined ? undefined : () => input.budget.now() - started >= input.budget.ms;
892
- const first = withAim(input, routeBase(input));
1125
+ let first = withAim(input, routeBase(input));
1126
+ if (first.plan.path === "pivot" && first.route.turn !== null && first.route.share > 0) {
1127
+ // a move that turns beside the body left is framed (B1) where there is a picture that holds both bodies, its look direction and stand being those of the picture; it is taken if it has no fault more than the other
1128
+ const framed = routeBase(input, 0, true);
1129
+ if (framed.route.turn?.framed === true && framed.route.share > 0 && noWorse(faultsOf(framed.route), faultsOf(first.route)) && steepest(framed.route) <= Math.max(1.25 * steepest(first.route), 3))
1130
+ first = framed;
1131
+ // a stand far beyond the body left (B3: the whole system in the picture) is taken only where the straight way, a swing, has a fault that it does not have; the Earth's own pull out to a planet is the straight way
1132
+ const backed = backOff(first.route);
1133
+ if (backed > BACK_UP) {
1134
+ const swung = routeBase(input, 1);
1135
+ if (swung.plan.path === "pivot" && swung.route.turn !== null && swung.route.share > 0 && noWorse(faultsOf(swung.route), faultsOf(first.route)) && backOff(swung.route) < backed)
1136
+ first = swung;
1137
+ }
1138
+ }
893
1139
  if (first.route.turn === null || first.plan.path !== "circle" || first.route.cut || first.route.share <= 0)
894
1140
  return first;
895
1141
  const was = faultsOf(first.route);
@@ -1008,14 +1254,20 @@ export function travelPose(input, t, plan) {
1008
1254
  sample.around = sightOf(input.around, time, view.position, o, frame);
1009
1255
  return sample;
1010
1256
  }
1257
+ /** The least share of the body gone to that must be lit for the body left to be kept behind it in the last view (`arrivalSide`'s `behind`): half, the same as the rule for every other arrival. */
1258
+ const BEHIND_LIT = 0.5;
1011
1259
  /**
1012
1260
  * The side of a body to stand on so a companion is in the picture, and the body stays at least half lit.
1013
1261
  * 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.
1014
1262
  * 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
1015
1263
  * 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).
1016
1264
  *
1017
- * @param input - The body, where the move comes from, the Sun, and the companions in rank order.
1018
- * @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).
1265
+ * `behind` is the body the move comes from: a side that puts it in the picture at the back left or right of the body, clear of the disc, is taken before any companion where the body is still at least half lit
1266
+ * (`BEHIND_LIT`); a side that also frames a moon of the body is preferred, and that moon is the companion returned. Where the body would show its night side, or no side frames the body left (a picture too narrow),
1267
+ * the rules above stand: the lit side wins, with a moon where one fits, and the body left is not in the last view.
1268
+ *
1269
+ * @param input - The body, where the move comes from, the Sun, the companions in rank order, and optionally `behind`.
1270
+ * @returns A unit side from the body toward the camera, how much of the disc is lit, the companion's id (null when none was framed), and whether the side is the one `behind` asked for.
1019
1271
  *
1020
1272
  * @example
1021
1273
  * ```ts
@@ -1058,11 +1310,43 @@ export function arrivalSide(input) {
1058
1310
  const limb = Math.atan(input.radius / distance);
1059
1311
  return angleOf(forward, unit(v)) > limb * 1.15;
1060
1312
  };
1313
+ // the body left at the back left or right (above or below in a tall picture): not far from the line across the picture's middle
1314
+ const level = (side, at) => {
1315
+ const forward = scale(side, -1);
1316
+ const north = sub([0, 1, 0], scale(forward, forward[1]));
1317
+ const up = length(north) < 1e-6 ? squareTo(forward) : unit(north);
1318
+ const v = sub(at, add(input.body, scale(side, distance)));
1319
+ const z = dot(v, forward);
1320
+ const x = dot(v, cross(forward, up)) / (tv * aspect * z);
1321
+ const y = dot(v, up) / (tv * z);
1322
+ // a near-square picture has no room beside the disc: any side of it will do
1323
+ return aspect > 0.77 && aspect < 1.3 ? true : aspect >= 1 ? Math.abs(y) <= 0.45 : Math.abs(x) <= 0.45;
1324
+ };
1061
1325
  const found = [];
1062
1326
  const half = Math.PI / 2 + 1e-4;
1063
1327
  const companions = input.companions ?? [];
1328
+ const behind = input.behind;
1064
1329
  const consider = (side) => {
1065
1330
  const u = length(side) < 1e-9 ? approach : unit(side);
1331
+ // the body left, behind the body gone to at its back left or right, comes first and is kept even where that gives a dark side: it is what keeps the body left in the picture
1332
+ // ...but only where the body gone to is still at least half lit: the lit side wins over the body left (an outward move to a planet shows its sunlit side, with a moon, and the body left is not in the last view)
1333
+ if (behind !== undefined && (sunDir === null || litOf(u) >= BEHIND_LIT) && frames(u, behind) && level(u, behind)) {
1334
+ // a side that also frames a moon of the body (or its planet) is taken before one that does not: the arrival keeps both, and names the moon
1335
+ let moon = null;
1336
+ let framed = false;
1337
+ for (const companion of companions) {
1338
+ if (companion.rank === 1 && frames(u, companion.at)) {
1339
+ framed = true;
1340
+ moon = companion.id ?? null;
1341
+ break;
1342
+ }
1343
+ }
1344
+ const near = angleOf(u, approach);
1345
+ const top = found[0];
1346
+ if (top === undefined || top.rank > 0 || (framed && !top.both) || (framed === (top.both === true) && near < top.near - 1e-6))
1347
+ found[0] = { side: u, rank: 0, near, id: moon, both: framed };
1348
+ return;
1349
+ }
1066
1350
  if (sunDir && angleOf(u, sunDir) > half)
1067
1351
  return;
1068
1352
  let rank = 99;
@@ -1081,7 +1365,7 @@ export function arrivalSide(input) {
1081
1365
  found[0] = { side: u, rank, near, id };
1082
1366
  };
1083
1367
  consider(approach);
1084
- for (const companion of companions) {
1368
+ for (const companion of behind === undefined ? companions : [{ at: behind, rank: 2 }, ...companions]) {
1085
1369
  const away = sub(input.body, companion.at);
1086
1370
  if (length(away) < 1e-6)
1087
1371
  continue;
@@ -1104,7 +1388,7 @@ export function arrivalSide(input) {
1104
1388
  }
1105
1389
  const picked = found[0];
1106
1390
  const side = picked?.side ?? swing(approach);
1107
- return { side, lit: litOf(side), companion: picked?.id ?? null };
1391
+ return { side, lit: litOf(side), companion: picked?.id ?? null, behind: picked?.rank === 0 };
1108
1392
  }
1109
1393
  /**
1110
1394
  * 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
@@ -1129,5 +1413,5 @@ export function travelArrival(input) {
1129
1413
  const forward = scale(side, -1);
1130
1414
  const north = input.north ?? [0, 1, 0];
1131
1415
  const across = sub(north, scale(forward, dot(north, forward)));
1132
- 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 };
1416
+ 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, behind: picked.behind };
1133
1417
  }
@@ -157,25 +157,43 @@ export interface DriveView {
157
157
  azimuth: number;
158
158
  pitch: number;
159
159
  }
160
+ /**
161
+ * What a drag does in the chase view of the rover: `head` (the default of the element, its `drive-drag`) turns the view where the camera is, like turning your head, so the camera keeps its place 6.5 m behind the rover
162
+ * and the scene turns round it; `orbit` takes the camera round the rover as it looks about, so the rover stays in the same place in the picture and is seen from every side, as the chase view was up to 0.55.
163
+ *
164
+ * @example
165
+ * ```ts
166
+ * import type { DriveDrag } from "@johnmorrisdotca/wakusei-planets/surface";
167
+ *
168
+ * const drags: DriveDrag[] = ["head", "orbit"];
169
+ * console.log(drags.join(" or "));
170
+ * ```
171
+ */
172
+ export type DriveDrag = "head" | "orbit";
160
173
  /**
161
174
  * The eyes for a view of the rover. The chase view stands 6.5 m behind it and 2.8 m up (never under the ground), looking at it; the seat view is the commander's eyes in the left seat, looking along the rover's heading
162
- * and tilting with it. `offset` is the driver's own turn of the view from a drag (degrees), added to the direction either looks in.
175
+ * and tilting with it. `offset` is the driver's own turn of the view from a drag (degrees), added to the direction either looks in. In the chase view `drag` says what that turn does: with `head` the eyes stay where the rover
176
+ * puts them with no drag and only the way they look turns, so the rover leaves the picture as the view turns away; with `orbit` (what a call without it does, as before) the eyes go round the rover as they look about.
177
+ * The seat view turns in place either way.
163
178
  *
164
179
  * @param state - The rover.
165
180
  * @param view - `chase` or `seat`.
166
181
  * @param offset - The driver's turn of the view, degrees right and up.
167
182
  * @param ground - The ground's height over the site's ground at a place.
183
+ * @param drag - What a drag does in the chase view: `head` or `orbit` (the default).
168
184
  * @returns The eyes.
169
185
  *
170
186
  * @example
171
187
  * ```ts
172
188
  * import { driveView, startDrive } from "@johnmorrisdotca/wakusei-planets/surface";
173
189
  *
174
- * const view = driveView(startDrive(0, 0, 0, 0), "chase", { azimuth: 0, pitch: 0 }, () => 0);
175
- * console.log(view.eye.north.toFixed(1), view.eye.up.toFixed(1));
190
+ * const rover = startDrive(0, 0, 0, 0);
191
+ * const view = driveView(rover, "chase", { azimuth: 0, pitch: 0 }, () => 0);
192
+ * const turned = driveView(rover, "chase", { azimuth: 90, pitch: 0 }, () => 0, "head");
193
+ * console.log(view.eye.north.toFixed(1), turned.eye.north.toFixed(1), turned.azimuth);
176
194
  * ```
177
195
  */
178
196
  export declare function driveView(state: DriveState, view: "chase" | "seat", offset: {
179
197
  azimuth: number;
180
198
  pitch: number;
181
- }, ground: (east: number, north: number) => number): DriveView;
199
+ }, ground: (east: number, north: number) => number, drag?: DriveDrag): DriveView;
@@ -218,28 +218,34 @@ export function driveParts(state) {
218
218
  }
219
219
  /**
220
220
  * The eyes for a view of the rover. The chase view stands 6.5 m behind it and 2.8 m up (never under the ground), looking at it; the seat view is the commander's eyes in the left seat, looking along the rover's heading
221
- * and tilting with it. `offset` is the driver's own turn of the view from a drag (degrees), added to the direction either looks in.
221
+ * and tilting with it. `offset` is the driver's own turn of the view from a drag (degrees), added to the direction either looks in. In the chase view `drag` says what that turn does: with `head` the eyes stay where the rover
222
+ * puts them with no drag and only the way they look turns, so the rover leaves the picture as the view turns away; with `orbit` (what a call without it does, as before) the eyes go round the rover as they look about.
223
+ * The seat view turns in place either way.
222
224
  *
223
225
  * @param state - The rover.
224
226
  * @param view - `chase` or `seat`.
225
227
  * @param offset - The driver's turn of the view, degrees right and up.
226
228
  * @param ground - The ground's height over the site's ground at a place.
229
+ * @param drag - What a drag does in the chase view: `head` or `orbit` (the default).
227
230
  * @returns The eyes.
228
231
  *
229
232
  * @example
230
233
  * ```ts
231
234
  * import { driveView, startDrive } from "@johnmorrisdotca/wakusei-planets/surface";
232
235
  *
233
- * const view = driveView(startDrive(0, 0, 0, 0), "chase", { azimuth: 0, pitch: 0 }, () => 0);
234
- * console.log(view.eye.north.toFixed(1), view.eye.up.toFixed(1));
236
+ * const rover = startDrive(0, 0, 0, 0);
237
+ * const view = driveView(rover, "chase", { azimuth: 0, pitch: 0 }, () => 0);
238
+ * const turned = driveView(rover, "chase", { azimuth: 90, pitch: 0 }, () => 0, "head");
239
+ * console.log(view.eye.north.toFixed(1), turned.eye.north.toFixed(1), turned.azimuth);
235
240
  * ```
236
241
  */
237
- export function driveView(state, view, offset, ground) {
242
+ export function driveView(state, view, offset, ground, drag = "orbit") {
238
243
  if (view === "seat") {
239
244
  const eye = toWorld(state, ROVER.seat);
240
245
  return { eye: { east: eye[0], north: eye[2], up: eye[1] }, azimuth: (((state.heading + offset.azimuth) % 360) + 360) % 360, pitch: state.pitch / RAD + offset.pitch };
241
246
  }
242
- const a = (state.heading + offset.azimuth) * RAD;
247
+ // the eyes stand behind the rover along the heading turned by the drag (`orbit`: they go round it) or along the heading alone (`head`: they keep their place and only the look turns)
248
+ const a = (state.heading + (drag === "head" ? 0 : offset.azimuth)) * RAD;
243
249
  const [east, north] = [state.east - Math.sin(a) * 6.5, state.north - Math.cos(a) * 6.5];
244
250
  const up = Math.max(state.y + 2.8, ground(east, north) + 1.4);
245
251
  const drop = (up - (state.y + 1.0)) / 6.5;
@@ -1,7 +1,7 @@
1
1
  import type { SharedRenderer } from "@johnmorrisdotca/kazoku-ui/renderer";
2
2
  import type { ThreeParts } from "../scene/body.ts";
3
3
  import { setThreeLoader } from "../scene/shared.ts";
4
- import { type DriveState } from "./drive.ts";
4
+ import { type DriveDrag, type DriveState } from "./drive.ts";
5
5
  import { type HeightLevel, type TerrainManifest } from "./height.ts";
6
6
  import { type TerrainProgress } from "./load.ts";
7
7
  import { type SurfaceScene } from "./scene.ts";
@@ -102,6 +102,20 @@ export declare class WakuseiSurface extends HTMLElement {
102
102
  /** Which view of the rover: `chase` (from behind and above, the default) or `seat` (the commander's). The key C turns from one to the other. */
103
103
  get driveCamera(): "chase" | "seat";
104
104
  set driveCamera(value: "chase" | "seat");
105
+ /**
106
+ * What a drag does in the chase view: `head` (the default; the attribute `drive-drag`) turns the view where the camera is, like turning your head, so the camera keeps its place 6.5 m behind the rover and the rover leaves
107
+ * the picture as you look away and comes back when you look back; `orbit` takes the camera round the rover as it looks about, as the chase view did up to 0.55. The seat view turns in place either way. Set while driving it
108
+ * moves the camera at once and keeps the drive.
109
+ *
110
+ * @example
111
+ * ```ts
112
+ * const surface = document.querySelector("wakusei-surface") as HTMLElement & { driveDrag: "head" | "orbit" };
113
+ * surface.driveDrag = "orbit";
114
+ * console.log(surface.driveDrag);
115
+ * ```
116
+ */
117
+ get driveDrag(): DriveDrag;
118
+ set driveDrag(value: DriveDrag);
105
119
  /** The rover being driven, or `undefined` when it is not. */
106
120
  get vehicle(): DriveState | undefined;
107
121
  /**
@@ -66,7 +66,7 @@ const num = (value, fallback) => {
66
66
  * ```
67
67
  */
68
68
  export class WakuseiSurface extends HTMLElement {
69
- static observedAttributes = ["quality", "shadows", "detail", "exposure", "site", "time", "heading", "pitch", "fov", "tiles", "lang", "body", "standby", "scale", "stick", "objects", "drive", "drive-camera", "watch", "watch-from", "watch-camera"];
69
+ static observedAttributes = ["quality", "shadows", "detail", "exposure", "site", "time", "heading", "pitch", "fov", "tiles", "lang", "body", "standby", "scale", "stick", "objects", "drive", "drive-camera", "drive-drag", "watch", "watch-from", "watch-camera"];
70
70
  #root;
71
71
  #stage;
72
72
  #noteEl;
@@ -259,6 +259,13 @@ export class WakuseiSurface extends HTMLElement {
259
259
  this.driveCamera = this.getAttribute("drive-camera") === "seat" ? "seat" : "chase";
260
260
  return;
261
261
  }
262
+ if (name === "drive-drag") {
263
+ if (this.#vehicle !== undefined) {
264
+ this.#syncDriveCamera();
265
+ this.#changed();
266
+ }
267
+ return;
268
+ }
262
269
  if (name === "stick") {
263
270
  this.#showStick();
264
271
  return;
@@ -507,6 +514,24 @@ export class WakuseiSurface extends HTMLElement {
507
514
  this.#syncDriveCamera();
508
515
  this.#changed();
509
516
  }
517
+ /**
518
+ * What a drag does in the chase view: `head` (the default; the attribute `drive-drag`) turns the view where the camera is, like turning your head, so the camera keeps its place 6.5 m behind the rover and the rover leaves
519
+ * the picture as you look away and comes back when you look back; `orbit` takes the camera round the rover as it looks about, as the chase view did up to 0.55. The seat view turns in place either way. Set while driving it
520
+ * moves the camera at once and keeps the drive.
521
+ *
522
+ * @example
523
+ * ```ts
524
+ * const surface = document.querySelector("wakusei-surface") as HTMLElement & { driveDrag: "head" | "orbit" };
525
+ * surface.driveDrag = "orbit";
526
+ * console.log(surface.driveDrag);
527
+ * ```
528
+ */
529
+ get driveDrag() {
530
+ return this.getAttribute("drive-drag") === "orbit" ? "orbit" : "head";
531
+ }
532
+ set driveDrag(value) {
533
+ this.setAttribute("drive-drag", value === "orbit" ? "orbit" : "head");
534
+ }
510
535
  /** The rover being driven, or `undefined` when it is not. */
511
536
  get vehicle() {
512
537
  return this.#vehicle === undefined ? undefined : { ...this.#vehicle, spin: [...this.#vehicle.spin] };
@@ -545,7 +570,7 @@ export class WakuseiSurface extends HTMLElement {
545
570
  const v = this.#vehicle;
546
571
  if (v === undefined)
547
572
  return;
548
- const view = driveView(v, this.#driveCamera, this.#camOffset, (e, n) => this.#groundAt(e, n));
573
+ const view = driveView(v, this.#driveCamera, this.#camOffset, (e, n) => this.#groundAt(e, n), this.driveDrag);
549
574
  this.#at = { east: view.eye.east, north: view.eye.north };
550
575
  this.#driveEyeUp = view.eye.up;
551
576
  this.#look = clampLook({ azimuth: view.azimuth, pitch: view.pitch, fov: this.#look.fov });