@johnmorrisdotca/wakusei-planets 0.53.0 → 0.55.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.
- package/CHANGELOG.md +23 -0
- package/README.md +3 -3
- package/dist/scene/textures.d.ts +2 -2
- package/dist/space/element.d.ts +19 -0
- package/dist/space/element.js +111 -6
- package/dist/space/hold.d.ts +217 -0
- package/dist/space/hold.js +248 -0
- package/dist/space/index.d.ts +2 -1
- package/dist/space/index.js +2 -1
- package/dist/space/scene.d.ts +32 -0
- package/dist/space/scene.js +69 -6
- package/dist/space/travel.d.ts +62 -4
- package/dist/space/travel.js +761 -51
- package/dist/surface/drive.d.ts +22 -4
- package/dist/surface/drive.js +11 -5
- package/dist/surface/element.d.ts +15 -1
- package/dist/surface/element.js +27 -2
- package/dist/surface/index.d.ts +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/dist/space/travel.js
CHANGED
|
@@ -17,11 +17,40 @@
|
|
|
17
17
|
// - the destination directly behind the body being left, or the two in one direction: the pivot puts the body left to one side, backing up until the straight line to the destination clears it;
|
|
18
18
|
// - the destination is the Sun, or a planet that is a point until the last stretch (the true scale): its place on the screen is reported at every moment, so a marker can stand for it;
|
|
19
19
|
// - the camera starts inside a beside-a-moon view: the moon, or its planet for a move within the planet's family, is the pivot;
|
|
20
|
+
// - a move within one planet's family (two moons, or the planet and a moon) whose first and last views (the whole body from the side the camera comes from) leave no turn about the planet or the body left that keeps one of them in the picture: the turn
|
|
21
|
+
// is made by an AIM POINT instead (see `Aim`), the camera backing off from the moon left, looking at a point that goes from it to the destination, until both are in the picture together, and coming in; it takes
|
|
22
|
+
// the same share of the move and ends in the same place, so the way on is the same, and it is used only when it leaves fewer empty pictures and keeps to the pace limits (`PACE_*`);
|
|
23
|
+
// - a move within a planet's family whose straight way on would pass close to the planet (the camera slides over the planet's clearance and the destination swings across the picture): the way on is made again as a
|
|
24
|
+
// swing from a stand that clears the planet, the nearest one, or one beyond the destination on the far side from the planet, and the way with the fewest faults that has none of any kind more than the first is taken;
|
|
20
25
|
// - the two views almost the same: no turn stage, a short move;
|
|
21
26
|
// - a view of a region (a set of orbits round a body): a straight pull back or push in with no turn of its own.
|
|
22
|
-
import { add, cross, dot,
|
|
27
|
+
import { add, cross, dot, RAD, scale, sub } from "../vec.js";
|
|
28
|
+
// The planner measures a very large number of lengths. This is `Math.hypot(x, y, z)` worked out in the same steps as the engine does (scaled by the largest, compensated sum, square root), so it gives the same
|
|
29
|
+
// numbers, without the cost of calling the built-in with a list of arguments.
|
|
30
|
+
const length = (a) => {
|
|
31
|
+
const x = Math.abs(a[0]);
|
|
32
|
+
const y = Math.abs(a[1]);
|
|
33
|
+
const z = Math.abs(a[2]);
|
|
34
|
+
const max = x > y ? (x > z ? x : z) : y > z ? y : z;
|
|
35
|
+
if (max === 0)
|
|
36
|
+
return 0;
|
|
37
|
+
let sum = 0;
|
|
38
|
+
let comp = 0;
|
|
39
|
+
for (let i = 0; i < 3; i += 1) {
|
|
40
|
+
const n = Math.abs(a[i]) / max;
|
|
41
|
+
const summand = n * n - comp;
|
|
42
|
+
const preliminary = sum + summand;
|
|
43
|
+
comp = preliminary - sum - summand;
|
|
44
|
+
sum = preliminary;
|
|
45
|
+
}
|
|
46
|
+
return Math.sqrt(sum) * max;
|
|
47
|
+
};
|
|
48
|
+
const unit = (a) => {
|
|
49
|
+
const n = length(a);
|
|
50
|
+
return n === 0 ? [0, 0, 0] : [a[0] / n, a[1] / n, a[2] / n];
|
|
51
|
+
};
|
|
23
52
|
/**
|
|
24
|
-
* How near a move may come to a body, in that body's radii above its surface:
|
|
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.
|
|
25
54
|
*
|
|
26
55
|
* @example
|
|
27
56
|
* ```ts
|
|
@@ -30,13 +59,42 @@ import { add, cross, dot, length, RAD, scale, sub, unit } from "../vec.js";
|
|
|
30
59
|
* console.log(TRAVEL_CLEARANCE);
|
|
31
60
|
* ```
|
|
32
61
|
*/
|
|
33
|
-
export const TRAVEL_CLEARANCE =
|
|
62
|
+
export const TRAVEL_CLEARANCE = 0.35;
|
|
63
|
+
/**
|
|
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
|
+
* 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
|
|
66
|
+
* 10 ms and at most 37 ms; with this budget a plan takes a median of 4.5 ms (up to 9.5 under load), 90 percent under 36 to 45 ms and at most 75 to 105 ms in quiet runs (126 ms in one loaded run), against the same median, 34 ms and 105 to 119 ms with no budget. A phone is several times
|
|
67
|
+
* slower, so there the planner stops sooner and keeps the best way found, and the same move can be planned differently on two devices (never with more faults than the first way). On this machine the budget costs
|
|
68
|
+
* little in faults, and what it costs depends on the load: over the 360 walks, 61 to 69 drifting samples, 32 to 70 empty and 12 to 15 lost in six runs (three of them with two other copies running at once),
|
|
69
|
+
* against 54, 27 and 12 with no budget and 605, 394 and 45 before the other ways; never more faults of any kind than the first way.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* import { TRAVEL_BUDGET_MS } from "@johnmorrisdotca/wakusei-planets/space";
|
|
74
|
+
*
|
|
75
|
+
* console.log(TRAVEL_BUDGET_MS);
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export const TRAVEL_BUDGET_MS = 60;
|
|
34
79
|
const clamp01 = (x) => Math.min(1, Math.max(0, x));
|
|
35
80
|
/** Slow out, fast between, slow in, with no jerk at either end. */
|
|
36
81
|
const ease = (x) => {
|
|
37
82
|
const t = clamp01(x);
|
|
38
83
|
return t * t * t * (t * (t * 6 - 15) + 10);
|
|
39
84
|
};
|
|
85
|
+
/** The share of a turn that `ease` has taken to `p`. */
|
|
86
|
+
const unease = (p) => {
|
|
87
|
+
let lo = 0;
|
|
88
|
+
let hi = 1;
|
|
89
|
+
for (let i = 0; i < 30; i += 1) {
|
|
90
|
+
const mid = (lo + hi) / 2;
|
|
91
|
+
if (ease(mid) < p)
|
|
92
|
+
lo = mid;
|
|
93
|
+
else
|
|
94
|
+
hi = mid;
|
|
95
|
+
}
|
|
96
|
+
return (lo + hi) / 2;
|
|
97
|
+
};
|
|
40
98
|
const smooth = (x) => {
|
|
41
99
|
const t = clamp01(x);
|
|
42
100
|
return t * t * (3 - 2 * t);
|
|
@@ -159,8 +217,46 @@ function leftOf(q, l, ea, eb) {
|
|
|
159
217
|
const m = smooth(q);
|
|
160
218
|
return l * ((1 - m) * (1 - gone) + m * left);
|
|
161
219
|
}
|
|
220
|
+
const AIM_STEPS = 96;
|
|
221
|
+
/** How far beyond the destination the far-side stands are, in the distance from the planet to the destination. */
|
|
222
|
+
const FAR_SIDE = [0.5, 1, 2, 4, 8];
|
|
223
|
+
/** How far the aim point has gone from the moon left to the destination at `p` of the turn: still on the moon left until 0.15, a smooth step to the destination by 0.85, and on the destination after. */
|
|
224
|
+
const aimPan = (p) => smooth((p - 0.15) / 0.7);
|
|
225
|
+
/** The direction from the aim point to the camera, as the aim goes `w` of the way from the moon left to the destination. */
|
|
226
|
+
function aimSide(a, w) {
|
|
227
|
+
const n = arc(a.n0, a.n1, w);
|
|
228
|
+
return a.bend === 0 ? n : unit(add(n, scale(a.lean, a.bend * Math.sin(Math.PI * w) ** 2)));
|
|
229
|
+
}
|
|
230
|
+
/** The camera's distance from the aim point at `p` of the turn: a smooth step up from the first distance to `far` times the larger of the two ends by 0.5, and a smooth step down to the last from 0.5 to 1. */
|
|
231
|
+
function aimDistance(a, p) {
|
|
232
|
+
const top = Math.max(a.r0, a.r1) * a.far;
|
|
233
|
+
return Math.exp(Math.log(a.r0) + (Math.log(top) - Math.log(a.r0)) * smooth(p / 0.5) + (Math.log(a.r1) - Math.log(top)) * smooth((p - 0.5) / 0.5));
|
|
234
|
+
}
|
|
235
|
+
/** The backing off to try, in turn, and how far from the middle of the picture the nearer moon may get (1 is the edge), and how many moments of the turn are looked at. */
|
|
236
|
+
const AIM_FAR = Array.from({ length: 19 }, (_, k) => 1.5 ** k);
|
|
237
|
+
const AIM_BEND = [0, 0.5, 1, 2, 4];
|
|
238
|
+
const AIM_SLOW = [1, 1.4, 2, 3, 4.5];
|
|
239
|
+
const AIM_EDGE = 0.95;
|
|
240
|
+
const AIM_CHECKS = 240;
|
|
241
|
+
/** The pace the aim point's turn keeps to, a little inside the limits the move is walked against: degrees the look direction turns in a 240th of the move, the share of the frame the destination moves in it, the degrees it may turn back, and the share of the way the camera goes in it. */
|
|
242
|
+
const PACE_DEGREES = 5;
|
|
243
|
+
const PACE_SCREEN = 0.25;
|
|
244
|
+
const PACE_UNTURN = 0.04;
|
|
245
|
+
const PACE_WAY = 0.045;
|
|
162
246
|
const LOOK_BACK_TURN = 0.4;
|
|
163
|
-
function turnAt(turn, p, t) {
|
|
247
|
+
function turnAt(turn, p, t, share) {
|
|
248
|
+
if (turn.aim !== undefined) {
|
|
249
|
+
const a = turn.aim;
|
|
250
|
+
// the aim point's turn goes at its own pace, the share of the turn gone and not the eased share
|
|
251
|
+
const q = share ?? unease(p);
|
|
252
|
+
const x = q * AIM_STEPS;
|
|
253
|
+
const i = Math.min(AIM_STEPS - 1, Math.floor(x));
|
|
254
|
+
const f = unit(mix(a.facings[i].f, a.facings[i + 1].f, x - i));
|
|
255
|
+
const u = mix(a.facings[i].u, a.facings[i + 1].u, x - i);
|
|
256
|
+
const w = aimPan(q);
|
|
257
|
+
const aim = mix(placeOf(a.from, t), placeOf(a.to, t), w);
|
|
258
|
+
return { position: add(aim, scale(aimSide(a, w), aimDistance(a, q))), o: { f, u: unit(sub(u, scale(f, dot(u, f)))) }, fov: fovBetween(turn.fov0, turn.fov1, p) };
|
|
259
|
+
}
|
|
164
260
|
const o = swingAt(turn.swing, p);
|
|
165
261
|
// the lift is early: the camera backs off as it starts to turn, so the body left goes into the picture, not out of its side
|
|
166
262
|
const w = Math.sqrt(p);
|
|
@@ -211,7 +307,19 @@ function clearOf(route, position, t) {
|
|
|
211
307
|
}
|
|
212
308
|
function along(route, t) {
|
|
213
309
|
const inTurn = route.turn !== null && t < route.share;
|
|
214
|
-
|
|
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);
|
|
215
323
|
return { position: clearOf(route, at.position, t), o: at.o, fov: at.fov, stage: inTurn ? "turn" : t < route.arrive ? "travel" : "arrive" };
|
|
216
324
|
}
|
|
217
325
|
function sightOf(body, t, position, o, frame) {
|
|
@@ -223,7 +331,12 @@ function sightOf(body, t, position, o, frame) {
|
|
|
223
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 };
|
|
224
332
|
}
|
|
225
333
|
/** The pivot's stand at the end of the turn, for a moment `tm` of the move. */
|
|
226
|
-
function standAt(input, tm, family) {
|
|
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
|
+
}
|
|
227
340
|
const o0 = facingOf(input.start);
|
|
228
341
|
const o1 = facingOf(input.end);
|
|
229
342
|
const clearance = input.clearance ?? TRAVEL_CLEARANCE;
|
|
@@ -275,8 +388,15 @@ function standAt(input, tm, family) {
|
|
|
275
388
|
break search;
|
|
276
389
|
}
|
|
277
390
|
}
|
|
278
|
-
if
|
|
279
|
-
|
|
391
|
+
// a stand that is nearest to a good one is taken for two bodies of a family; with `far`, only if the straight way from it to the last view clears the planet, and if not the way on is made as a swing (below), from
|
|
392
|
+
// a stand where the way to the destination clears it
|
|
393
|
+
if (straight === null && family && fallback !== null) {
|
|
394
|
+
const stand = sub(v, toScene(o1, fallback));
|
|
395
|
+
if (variant === 0 || missOf(stand, last, v) >= Math.min(1.52 * (1 + clearance) * pivot.radius, 0.9 * length(fallback), 0.98 * length(apex)))
|
|
396
|
+
straight = fallback;
|
|
397
|
+
}
|
|
398
|
+
if (variant >= 2)
|
|
399
|
+
straight = null;
|
|
280
400
|
if (straight !== null) {
|
|
281
401
|
const dm = Math.max(1e-12, length(straight));
|
|
282
402
|
const stand = sub(v, toScene(o1, straight));
|
|
@@ -311,16 +431,36 @@ function standAt(input, tm, family) {
|
|
|
311
431
|
}
|
|
312
432
|
const first0 = intoFrame(toCamera(om, toScene(o0, u0)), frame0, 0.85);
|
|
313
433
|
let chosen = null;
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
434
|
+
if (variant >= 2) {
|
|
435
|
+
// the far side: the camera stands on the line back from the destination along a look direction of the turn, as far from it as the variant says, and the look direction that puts the planet furthest from that line
|
|
436
|
+
// is taken, so the way in to the destination never goes near the planet; the turn is along the arc as before, and the destination is in the middle of the picture at the stand
|
|
437
|
+
const dist = FAR_SIDE[variant - 2] * Math.max(length(sub(b, v)), 1e-12);
|
|
438
|
+
let bestK = 0;
|
|
439
|
+
let bestMiss = -1;
|
|
440
|
+
for (let k = 0; k <= 50; k += 1) {
|
|
441
|
+
const o = swingAt(whole, k / 50);
|
|
442
|
+
const miss = missOf(sub(b, scale(o.f, dist)), b, v);
|
|
443
|
+
if (miss > bestMiss + 1e-12) {
|
|
444
|
+
bestMiss = miss;
|
|
445
|
+
bestK = k;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
om = swingAt(whole, bestK / 50);
|
|
449
|
+
const stand = sub(b, scale(om.f, dist));
|
|
450
|
+
chosen = { stand, u: unit(toCamera(om, sub(v, stand))), d: Math.max(1e-12, length(sub(v, stand))), miss: bestMiss };
|
|
451
|
+
}
|
|
452
|
+
else {
|
|
453
|
+
search: for (const far of tries) {
|
|
454
|
+
for (const spot of spots(first0, frame0, 0.85)) {
|
|
455
|
+
const stand = sub(v, scale(toScene(om, spot), far * d0));
|
|
456
|
+
const miss = missOf(stand, b, v);
|
|
457
|
+
const seen = within(toCamera(om, sub(b, stand)), frame0, 0.9);
|
|
458
|
+
if (seen && (chosen === null || miss > chosen.miss))
|
|
459
|
+
chosen = { stand, u: spot, d: far * d0, miss };
|
|
460
|
+
if (seen && miss >= Math.min(1.52 * (1 + clearance) * pivot.radius, 0.9 * far * d0)) {
|
|
461
|
+
chosen = { stand, u: spot, d: far * d0, miss };
|
|
462
|
+
break search;
|
|
463
|
+
}
|
|
324
464
|
}
|
|
325
465
|
}
|
|
326
466
|
}
|
|
@@ -336,6 +476,135 @@ function standAt(input, tm, family) {
|
|
|
336
476
|
},
|
|
337
477
|
};
|
|
338
478
|
}
|
|
479
|
+
/** 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. */
|
|
480
|
+
const FRAME_HEIGHT = 0.5;
|
|
481
|
+
const FRAME_LEAST = 0.4;
|
|
482
|
+
const FRAME_MOST = 0.6;
|
|
483
|
+
/** The most the line between the two bodies may be tilted from the picture's long axis at the stand, in radians. */
|
|
484
|
+
const FRAME_TILT = 0.6;
|
|
485
|
+
/** 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. */
|
|
486
|
+
const FRAME_SPLIT = 0.45;
|
|
487
|
+
/** The distance at which a body of radius `r` keeps `share` of the picture's height. */
|
|
488
|
+
const distanceFor = (r, share, tv) => {
|
|
489
|
+
const t = Math.max(1e-6, share * tv);
|
|
490
|
+
return r / (t / Math.sqrt(1 + t * t));
|
|
491
|
+
};
|
|
492
|
+
const FRAME_SPHERE = (() => {
|
|
493
|
+
const points = [];
|
|
494
|
+
const n = 240;
|
|
495
|
+
for (let i = 0; i < n; i += 1) {
|
|
496
|
+
const z = 1 - (2 * (i + 0.5)) / n;
|
|
497
|
+
const r = Math.sqrt(1 - z * z);
|
|
498
|
+
const a = i * 2.399963229728653;
|
|
499
|
+
points.push([r * Math.cos(a), r * Math.sin(a), z]);
|
|
500
|
+
}
|
|
501
|
+
return points;
|
|
502
|
+
})();
|
|
503
|
+
/**
|
|
504
|
+
* 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
|
|
505
|
+
* 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
|
|
506
|
+
* 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).
|
|
507
|
+
*/
|
|
508
|
+
function frameStand(input, tm) {
|
|
509
|
+
const pivot = input.leaving;
|
|
510
|
+
if (pivot.reach !== undefined || input.destination.reach !== undefined)
|
|
511
|
+
return null;
|
|
512
|
+
const o0 = facingOf(input.start);
|
|
513
|
+
const o1 = facingOf(input.end);
|
|
514
|
+
const frame = frameOf(input.end.fov, input.aspect);
|
|
515
|
+
const wide = frame.th >= frame.tv;
|
|
516
|
+
const clearance = input.clearance ?? TRAVEL_CLEARANCE;
|
|
517
|
+
const centre = placeOf(pivot, tm);
|
|
518
|
+
const b = placeOf(input.destination, tm);
|
|
519
|
+
const e1 = sub(input.end.position, placeOf(input.destination, 1));
|
|
520
|
+
const last = add(b, e1);
|
|
521
|
+
const fromPivot = sub(pivot.from, input.start.position);
|
|
522
|
+
const d0 = Math.max(1e-12, length(fromPivot));
|
|
523
|
+
const near = distanceFor(pivot.radius, FRAME_MOST, frame.tv);
|
|
524
|
+
const far = distanceFor(pivot.radius, FRAME_LEAST, frame.tv);
|
|
525
|
+
const dm = d0 < near ? distanceFor(pivot.radius, FRAME_HEIGHT, frame.tv) : d0;
|
|
526
|
+
const share = pivot.radius / dm;
|
|
527
|
+
const tanL = share / Math.sqrt(Math.max(1e-12, 1 - share * share));
|
|
528
|
+
const n0 = unit(scale(fromPivot, -1));
|
|
529
|
+
// 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
|
|
530
|
+
const standAtN = (n) => {
|
|
531
|
+
const stand = add(centre, scale(n, dm));
|
|
532
|
+
const toB = sub(b, stand);
|
|
533
|
+
if (length(toB) < 1e-9)
|
|
534
|
+
return null;
|
|
535
|
+
const dirL = scale(n, -1);
|
|
536
|
+
const dirB = unit(toB);
|
|
537
|
+
const apart = angleOf(dirL, dirB);
|
|
538
|
+
if (apart < 1e-6)
|
|
539
|
+
return null;
|
|
540
|
+
const f = arc(dirL, dirB, FRAME_SPLIT);
|
|
541
|
+
const pick = o0.u;
|
|
542
|
+
const across = sub(pick, scale(f, dot(pick, f)));
|
|
543
|
+
const o = { f, u: length(across) < 1e-6 ? squareTo(f) : unit(across) };
|
|
544
|
+
const sl = toCamera(o, sub(centre, stand));
|
|
545
|
+
const sb = toCamera(o, toB);
|
|
546
|
+
if (!(sl[2] > 0) || !(sb[2] > 0))
|
|
547
|
+
return null;
|
|
548
|
+
const xl = sl[0] / (sl[2] * frame.th);
|
|
549
|
+
const yl = sl[1] / (sl[2] * frame.tv);
|
|
550
|
+
const xb = sb[0] / (sb[2] * frame.th);
|
|
551
|
+
const yb = sb[1] / (sb[2] * frame.tv);
|
|
552
|
+
// the disc of the body left inside the picture, the destination's place inside it, and the destination clear of the disc
|
|
553
|
+
if (Math.abs(xl) + tanL / frame.th > 1 || Math.abs(yl) + tanL / frame.tv > 1)
|
|
554
|
+
return null;
|
|
555
|
+
if (Math.abs(xb) > 0.85 || Math.abs(yb) > 0.85)
|
|
556
|
+
return null;
|
|
557
|
+
const dx = (xb - xl) * frame.th;
|
|
558
|
+
const dy = (yb - yl) * frame.tv;
|
|
559
|
+
const gap = Math.hypot(dx, dy);
|
|
560
|
+
if (gap < 1.2 * tanL)
|
|
561
|
+
return null;
|
|
562
|
+
const tilt = wide ? Math.atan2(Math.abs(dy), Math.abs(dx)) : Math.atan2(Math.abs(dx), Math.abs(dy));
|
|
563
|
+
if (tilt > FRAME_TILT)
|
|
564
|
+
return null;
|
|
565
|
+
if (missOf(stand, last, centre) < Math.min(1.05 * (1 + clearance) * pivot.radius, 0.9 * dm))
|
|
566
|
+
return null;
|
|
567
|
+
return { stand, o, cost: angleOf(n0, n) + 0.8 * tilt };
|
|
568
|
+
};
|
|
569
|
+
let best = null;
|
|
570
|
+
const here = standAtN(n0);
|
|
571
|
+
if (here !== null)
|
|
572
|
+
best = { n: n0, cost: here.cost };
|
|
573
|
+
else {
|
|
574
|
+
for (const n of FRAME_SPHERE) {
|
|
575
|
+
const found = standAtN(n);
|
|
576
|
+
if (found !== null && (best === null || found.cost < best.cost))
|
|
577
|
+
best = { n, cost: found.cost };
|
|
578
|
+
}
|
|
579
|
+
if (best === null)
|
|
580
|
+
return null;
|
|
581
|
+
// 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
|
|
582
|
+
let lo = 0;
|
|
583
|
+
let hi = 1;
|
|
584
|
+
const goal = best.n;
|
|
585
|
+
for (let i = 0; i < 14; i += 1) {
|
|
586
|
+
const mid = (lo + hi) / 2;
|
|
587
|
+
if (standAtN(arc(n0, goal, mid)) !== null)
|
|
588
|
+
hi = mid;
|
|
589
|
+
else
|
|
590
|
+
lo = mid;
|
|
591
|
+
}
|
|
592
|
+
best = { n: arc(n0, goal, hi), cost: 0 };
|
|
593
|
+
}
|
|
594
|
+
const at = standAtN(best.n);
|
|
595
|
+
if (at === null)
|
|
596
|
+
return null;
|
|
597
|
+
const { stand, o } = at;
|
|
598
|
+
const from = sub(stand, b);
|
|
599
|
+
const lengthOn = length(sub(e1, from));
|
|
600
|
+
const leaving = placeOf(input.leaving, tm);
|
|
601
|
+
const d1 = Math.max(1e-12, length(e1));
|
|
602
|
+
void far;
|
|
603
|
+
return {
|
|
604
|
+
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 },
|
|
605
|
+
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 },
|
|
606
|
+
};
|
|
607
|
+
}
|
|
339
608
|
function lineLeg(input, about, watch) {
|
|
340
609
|
const from = sub(input.start.position, about.from);
|
|
341
610
|
const to = sub(input.end.position, placeOf(about, 1));
|
|
@@ -347,11 +616,174 @@ function lineLeg(input, about, watch) {
|
|
|
347
616
|
}
|
|
348
617
|
/** The way on is never hurried by a long turn: it takes at least 45 percent of the turn's time. */
|
|
349
618
|
const legMs = (leg, turn = 0) => Math.max(0.45 * turn, 900 + 320 * Math.max(Math.log1p(leg.l / (leg.ea + leg.eb)), Math.abs(Math.log(Math.max(1e-12, leg.d1) / Math.max(1e-12, leg.d0)))) + 16 * swingDegrees(leg.swing));
|
|
619
|
+
/** The aim point's turn without its backing off: the look directions and ups, from the first view's to the last, for the stand `end` of a turn that was a swing about the planet. */
|
|
620
|
+
function aimOf(input, turn, tm, bend, slow) {
|
|
621
|
+
const o0 = turn.swing.from;
|
|
622
|
+
const end = turnAt(turn, 1, tm);
|
|
623
|
+
const from = sub(input.start.position, input.leaving.from);
|
|
624
|
+
const to = sub(end.position, placeOf(input.destination, tm));
|
|
625
|
+
const r0 = Math.max(1e-12, length(from));
|
|
626
|
+
const r1 = Math.max(1e-12, length(to));
|
|
627
|
+
const n0 = length(from) < 1e-12 ? scale(o0.f, -1) : unit(from);
|
|
628
|
+
const n1 = length(to) < 1e-12 ? scale(end.o.f, -1) : unit(to);
|
|
629
|
+
// out from the planet, square to the line between the two moons, for the middle of the turn
|
|
630
|
+
const middle = sub(mix(placeOf(input.leaving, tm / 2), placeOf(input.destination, tm / 2), 0.5), placeOf(turn.pivot, tm / 2));
|
|
631
|
+
const line = unit(sub(placeOf(input.destination, tm / 2), placeOf(input.leaving, tm / 2)));
|
|
632
|
+
const square = sub(middle, scale(line, dot(middle, line)));
|
|
633
|
+
const lean = length(square) < 1e-9 * Math.max(1, length(middle)) ? squareTo(line) : unit(square);
|
|
634
|
+
// the small turns that join the direction to the aim point to the first and the last views
|
|
635
|
+
const first = angleOf(scale(n0, -1), o0.f);
|
|
636
|
+
const last = angleOf(scale(n1, -1), end.o.f);
|
|
637
|
+
const axis0 = length(cross(scale(n0, -1), o0.f)) < 1e-9 ? squareTo(o0.f) : unit(cross(scale(n0, -1), o0.f));
|
|
638
|
+
const axis1 = length(cross(scale(n1, -1), end.o.f)) < 1e-9 ? squareTo(end.o.f) : unit(cross(scale(n1, -1), end.o.f));
|
|
639
|
+
const ups = [];
|
|
640
|
+
const looks = [];
|
|
641
|
+
for (let k = 0; k <= AIM_STEPS; k += 1) {
|
|
642
|
+
const p = k / AIM_STEPS;
|
|
643
|
+
const g = aimPan(p);
|
|
644
|
+
looks.push(unit(spin(spin(scale(aimSide({ n0, n1, lean, bend }, g), -1), axis1, last * g), axis0, first * (1 - g))));
|
|
645
|
+
}
|
|
646
|
+
// the up carried along the looks by the least turn, and the roll that is left spread evenly
|
|
647
|
+
ups.push(o0.u);
|
|
648
|
+
for (let k = 1; k <= AIM_STEPS; k += 1) {
|
|
649
|
+
const a = looks[k - 1];
|
|
650
|
+
const b = looks[k];
|
|
651
|
+
const turned = angleOf(a, b);
|
|
652
|
+
const carried = turned < 1e-12 ? ups[k - 1] : spin(ups[k - 1], unit(cross(a, b)), turned);
|
|
653
|
+
ups.push(unit(sub(carried, scale(b, dot(carried, b)))));
|
|
654
|
+
}
|
|
655
|
+
const final = ups[AIM_STEPS];
|
|
656
|
+
const roll = Math.atan2(dot(cross(final, end.o.u), looks[AIM_STEPS]), dot(final, end.o.u));
|
|
657
|
+
const facings = looks.map((f, k) => {
|
|
658
|
+
const rolled = spin(ups[k], f, roll * (k / AIM_STEPS));
|
|
659
|
+
return { f, u: unit(sub(rolled, scale(f, dot(rolled, f)))) };
|
|
660
|
+
});
|
|
661
|
+
let degrees = 0;
|
|
662
|
+
for (let k = 1; k <= AIM_STEPS; k += 1)
|
|
663
|
+
degrees += angleOf(facings[k - 1].f, facings[k].f) / RAD;
|
|
664
|
+
return { from: input.leaving, to: input.destination, n0, n1, r0, r1, lean, bend, slow, degrees, facings };
|
|
665
|
+
}
|
|
350
666
|
const turnMs = (turn) => {
|
|
667
|
+
if (turn.aim !== undefined) {
|
|
668
|
+
const a = turn.aim;
|
|
669
|
+
return Math.max(500, 24 * a.slow * a.degrees + 400 * (Math.log(Math.max(a.r0, a.r1) * a.far / a.r0) + Math.log(Math.max(a.r0, a.r1) * a.far / a.r1)) / 2);
|
|
670
|
+
}
|
|
351
671
|
const ms = 16 * swingDegrees(turn.swing) + 8 * (angleOf(turn.u0, turn.u1) / RAD) + 400 * Math.abs(Math.log(turn.d1 / turn.d0)) + 600 * turn.lift;
|
|
352
672
|
return ms < 40 ? 0 : Math.max(500, ms);
|
|
353
673
|
};
|
|
354
|
-
|
|
674
|
+
/**
|
|
675
|
+
* How a turn made by an aim point does when walked at the moments a walk of the move samples (a 240th of the move apart), with the turn taking `sc` of the move: the furthest from the middle of the picture
|
|
676
|
+
* the nearer of the two moons gets (1 is the edge, infinity is neither in the picture at some moment), and whether it keeps to the pace limits: the look direction turns less than `PACE_DEGREES` between two
|
|
677
|
+
* moments, the destination moves less than `PACE_SCREEN` of the frame, the look direction does not turn away from the last view's by more than `PACE_UNTURN` degrees, the camera goes less than `PACE_WAY` of the
|
|
678
|
+
* way, and the whole turn is under 1.2 times the least plus 1.5 degrees.
|
|
679
|
+
*/
|
|
680
|
+
function aimFlaws(input, candidate, shells, leg, sc) {
|
|
681
|
+
const last = unit(input.end.forward);
|
|
682
|
+
const least = angleOf(unit(input.start.forward), last) / RAD;
|
|
683
|
+
let edge = 0;
|
|
684
|
+
let fair = true;
|
|
685
|
+
let before = null;
|
|
686
|
+
let way = 0;
|
|
687
|
+
let turned = 0;
|
|
688
|
+
const seen = [];
|
|
689
|
+
for (let i = 0; i <= Math.floor(AIM_CHECKS * sc) + 1; i += 1) {
|
|
690
|
+
const x = Math.min(1, i / (AIM_CHECKS * sc));
|
|
691
|
+
const t = sc * x;
|
|
692
|
+
const at = turnAt(candidate, ease(x), t, x);
|
|
693
|
+
const position = clearOf({ shells, leg }, at.position, t);
|
|
694
|
+
const frame = frameOf(at.fov, input.aspect);
|
|
695
|
+
const l = sightOf(input.leaving, t, position, at.o, frame);
|
|
696
|
+
const d = sightOf(input.destination, t, position, at.o, frame);
|
|
697
|
+
const off = (sight) => (sight.inside ? Math.max(Math.abs(sight.x), Math.abs(sight.y)) : Number.POSITIVE_INFINITY);
|
|
698
|
+
edge = Math.max(edge, Math.min(off(l), off(d)));
|
|
699
|
+
// a candidate is only wanted if the nearer moon stays inside `AIM_EDGE` and the pace is kept, so the walk stops at the first moment that is not
|
|
700
|
+
if (edge > AIM_EDGE)
|
|
701
|
+
return { edge, fair: false };
|
|
702
|
+
const now = { position, f: at.o.f, dest: d };
|
|
703
|
+
if (before !== null) {
|
|
704
|
+
const step = angleOf(before.f, at.o.f) / RAD;
|
|
705
|
+
turned += step;
|
|
706
|
+
way += length(sub(position, before.position));
|
|
707
|
+
if (step > PACE_DEGREES)
|
|
708
|
+
fair = false;
|
|
709
|
+
if (before.dest.inside && d.inside && Math.hypot(d.x - before.dest.x, d.y - before.dest.y) > PACE_SCREEN)
|
|
710
|
+
fair = false;
|
|
711
|
+
if (angleOf(at.o.f, last) - angleOf(before.f, last) > PACE_UNTURN * RAD)
|
|
712
|
+
fair = false;
|
|
713
|
+
if (!fair)
|
|
714
|
+
return { edge, fair };
|
|
715
|
+
}
|
|
716
|
+
seen.push(now);
|
|
717
|
+
before = now;
|
|
718
|
+
if (x >= 1)
|
|
719
|
+
break;
|
|
720
|
+
}
|
|
721
|
+
if (before !== null) {
|
|
722
|
+
const rest = length(sub(input.end.position, before.position));
|
|
723
|
+
for (let k = 1; k < seen.length; k += 1)
|
|
724
|
+
if (length(sub(seen[k].position, seen[k - 1].position)) > PACE_WAY * (way + rest))
|
|
725
|
+
fair = false;
|
|
726
|
+
if (turned + angleOf(before.f, last) / RAD > 1.2 * least + 1.5)
|
|
727
|
+
fair = false;
|
|
728
|
+
}
|
|
729
|
+
return { edge, fair };
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* The count of moments of a turn, among 68 from its first moments (sampled finely: the body left is nearest then, and nearest to being lost) to its last, at which neither the body left nor the destination is
|
|
733
|
+
* inside 0.9 of the picture, as a function of the lift and the hold that are tried on the turn: what does not depend on them (the look direction, the field of view, the places of the bodies, the pivot's
|
|
734
|
+
* direction) is worked out once for the turn. Every hold tried on a turn has the same direction `u` and body, as the search gives it. It gives the numbers `turnAt` and `sightOf` give, and stops counting at `cap`; the moments that missed before are looked at first.
|
|
735
|
+
*/
|
|
736
|
+
function backOffKit(input, turn, share) {
|
|
737
|
+
const xs = [];
|
|
738
|
+
for (let i = 1; i < 64; i += 1)
|
|
739
|
+
for (const x of i === 1 ? [0.0005, 0.001, 0.002, 0.004, 0.008, 1 / 64] : [i / 64])
|
|
740
|
+
xs.push(x);
|
|
741
|
+
const moments = xs.map((x) => {
|
|
742
|
+
const t = share * x;
|
|
743
|
+
const p = ease(x);
|
|
744
|
+
const o = swingAt(turn.swing, p);
|
|
745
|
+
return {
|
|
746
|
+
t, p, o,
|
|
747
|
+
w: Math.sqrt(p),
|
|
748
|
+
base: Math.log(turn.d0) + (Math.log(turn.d1) - Math.log(turn.d0)) * p,
|
|
749
|
+
dir: toScene(o, arc(turn.u0, turn.u1, p)),
|
|
750
|
+
pivot: placeOf(turn.pivot, t),
|
|
751
|
+
frame: frameOf(fovBetween(turn.fov0, turn.fov1, p), input.aspect),
|
|
752
|
+
holdDir: null,
|
|
753
|
+
holdAt: null,
|
|
754
|
+
};
|
|
755
|
+
});
|
|
756
|
+
// the moments that missed are moved to the front, so that a candidate that misses is found out early
|
|
757
|
+
const order = moments.map((_, i) => i);
|
|
758
|
+
const seen = (body, m, position) => {
|
|
759
|
+
const sight = sightOf(body, m.t, position, m.o, m.frame);
|
|
760
|
+
return sight.inside && Math.abs(sight.x) <= 0.9 && Math.abs(sight.y) <= 0.9;
|
|
761
|
+
};
|
|
762
|
+
return (lift, hold, cap) => {
|
|
763
|
+
let missed = 0;
|
|
764
|
+
for (let k = 0; k < order.length && missed < cap; k += 1) {
|
|
765
|
+
const m = moments[order[k]];
|
|
766
|
+
const d = Math.exp(m.base + lift * 4 * m.w * (1 - m.w));
|
|
767
|
+
let position = sub(m.pivot, scale(m.dir, d));
|
|
768
|
+
if (hold !== undefined) {
|
|
769
|
+
m.holdDir ??= toScene(m.o, hold.u);
|
|
770
|
+
m.holdAt ??= placeOf(hold.body, m.t);
|
|
771
|
+
const near = sub(m.holdAt, scale(m.holdDir, hold.d * (1 + (hold.far - 1) * smooth(m.p / hold.q))));
|
|
772
|
+
position = mix(near, position, smooth(m.p / hold.q));
|
|
773
|
+
}
|
|
774
|
+
if (!seen(input.leaving, m, position) && !seen(input.destination, m, position)) {
|
|
775
|
+
missed += 1;
|
|
776
|
+
if (k > 0) {
|
|
777
|
+
const at = order[k];
|
|
778
|
+
order.splice(k, 1);
|
|
779
|
+
order.unshift(at);
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
return missed;
|
|
784
|
+
};
|
|
785
|
+
}
|
|
786
|
+
function routeBase(input, variant = 0, framed = false) {
|
|
355
787
|
const wanted = input.reduced ? "cut" : (input.kind ?? "turn-travel");
|
|
356
788
|
const kind = wanted === "auto" ? "turn-travel" : wanted;
|
|
357
789
|
const clearance = input.clearance ?? TRAVEL_CLEARANCE;
|
|
@@ -363,7 +795,7 @@ function routeOf(input) {
|
|
|
363
795
|
const easing = { turn: "smooth-step", distance: "geometric" };
|
|
364
796
|
if (kind === "cut") {
|
|
365
797
|
const leg = lineLeg(input, input.destination, undefined);
|
|
366
|
-
return { route: { ...base, cut: true, turn: null, share: 0, arrive: 0, leg }, plan: { kind, path: "cut", stages: { turn: 0, travel: 0, arrive: 1 }, duration: 0, turn: least, settle: 0, easing } };
|
|
798
|
+
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 } };
|
|
367
799
|
}
|
|
368
800
|
let turn = null;
|
|
369
801
|
let leg;
|
|
@@ -381,7 +813,7 @@ function routeOf(input) {
|
|
|
381
813
|
const d0 = length(sub(input.leaving.from, input.start.position));
|
|
382
814
|
const facing = swingDegrees(swingOf(o0, facingOf(input.end))) < 1 && within(toCamera(o0, sub(input.destination.from, input.start.position)), frameOf(input.start.fov, input.aspect), 0.93);
|
|
383
815
|
const open = missOf(input.start.position, input.end.position, input.leaving.from) >= Math.min(1.52 * (1 + clearance) * input.leaving.radius, 0.9 * d0);
|
|
384
|
-
let stand = facing && open ? { turn: { pivot: input.leaving, swing: swingOf(o0, o0), u0: [0, 0, 1], u1: [0, 0, 1], d0: 1, d1: 1, lift: 0, fov0: input.start.fov, fov1: input.start.fov }, leg: lineLeg(input, input.destination, undefined) } : standAt(input, 0.25, family);
|
|
816
|
+
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);
|
|
385
817
|
// the stand is worked out for the moment the turn ends, which is the share of the move its own length gives: found by trying, until the two agree (the bodies move, so a stand for another moment would not meet the way on)
|
|
386
818
|
let tm = 0.25;
|
|
387
819
|
for (let pass = 0; pass < 8; pass += 1) {
|
|
@@ -390,7 +822,7 @@ function routeOf(input) {
|
|
|
390
822
|
if ((facing && open) || share === 0 || Math.abs(share - tm) < 0.002)
|
|
391
823
|
break;
|
|
392
824
|
tm = pass === 0 ? share : (tm + share) / 2;
|
|
393
|
-
stand = standAt(input, tm, family);
|
|
825
|
+
stand = standAt(input, tm, family, variant, framed);
|
|
394
826
|
}
|
|
395
827
|
if (share > 0 && !(facing && open))
|
|
396
828
|
share = tm;
|
|
@@ -409,30 +841,14 @@ function routeOf(input) {
|
|
|
409
841
|
}
|
|
410
842
|
else if (family && turn.pivot !== input.leaving) {
|
|
411
843
|
// the planet is held, not the moon left: back the turn off in its middle until one of the two bodies is in the picture at every moment of it
|
|
412
|
-
const frameAt = (fov) => frameOf(fov, input.aspect);
|
|
413
844
|
// the stand is for the moment the turn ends, so when the backed-off turn takes another share of the move the stand is worked out again for that moment, until the two agree
|
|
414
845
|
for (let round = 0; round < 4; round += 1) {
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
// the first moments are sampled finely: the body left is nearest then, and nearest to being lost
|
|
419
|
-
for (const x of i === 1 ? [0.0005, 0.001, 0.002, 0.004, 0.008, 1 / 64] : [i / 64]) {
|
|
420
|
-
const t = share * x;
|
|
421
|
-
const at = turnAt(candidate, ease(x), t);
|
|
422
|
-
const frame = frameAt(at.fov);
|
|
423
|
-
const seen = (body) => {
|
|
424
|
-
const sight = sightOf(body, t, at.position, at.o, frame);
|
|
425
|
-
return sight.inside && Math.abs(sight.x) <= 0.9 && Math.abs(sight.y) <= 0.9;
|
|
426
|
-
};
|
|
427
|
-
if (!seen(input.leaving) && !seen(input.destination))
|
|
428
|
-
missed += 1;
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
return missed;
|
|
432
|
-
};
|
|
846
|
+
// `cap`: the count is only wanted if it is under the best so far, so it stops counting when it is not
|
|
847
|
+
const kit = backOffKit(input, turn, share);
|
|
848
|
+
const missing = (candidate, cap = Number.POSITIVE_INFINITY) => kit(candidate.lift, candidate.hold, cap);
|
|
433
849
|
let best = { turn, missed: Number.POSITIVE_INFINITY };
|
|
434
850
|
for (let lift = 0; lift <= 3.31 && best.missed > 0; lift += 0.3) {
|
|
435
|
-
const missed = missing({ ...turn, lift });
|
|
851
|
+
const missed = missing({ ...turn, lift }, best.missed);
|
|
436
852
|
if (missed < best.missed)
|
|
437
853
|
best = { turn: { ...turn, lift }, missed };
|
|
438
854
|
}
|
|
@@ -444,7 +860,7 @@ function routeOf(input) {
|
|
|
444
860
|
for (const q of [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]) {
|
|
445
861
|
for (let lift = 0; lift <= 3.31 && best.missed > 0; lift += 0.3) {
|
|
446
862
|
const candidate = { ...turn, lift, hold: { body: input.leaving, u, d, q, far } };
|
|
447
|
-
const missed = missing(candidate);
|
|
863
|
+
const missed = missing(candidate, best.missed);
|
|
448
864
|
if (missed < best.missed)
|
|
449
865
|
best = { turn: candidate, missed };
|
|
450
866
|
}
|
|
@@ -457,7 +873,7 @@ function routeOf(input) {
|
|
|
457
873
|
if (Math.abs(next - share) < 0.002 || round === 3)
|
|
458
874
|
break;
|
|
459
875
|
share = (share + next) / 2;
|
|
460
|
-
const again = standAt(input, share, family);
|
|
876
|
+
const again = standAt(input, share, family, variant);
|
|
461
877
|
turn = again.turn;
|
|
462
878
|
leg = again.leg;
|
|
463
879
|
}
|
|
@@ -476,8 +892,264 @@ function routeOf(input) {
|
|
|
476
892
|
hi = mid;
|
|
477
893
|
}
|
|
478
894
|
const arrive = share + (1 - share) * Math.min(0.9, Math.max(0.6, hi));
|
|
479
|
-
const route = { ...base, cut: false, turn, share, arrive, leg };
|
|
480
|
-
return { route, plan: { kind, path, stages: { turn: share, travel: arrive - share, arrive: 1 - arrive }, duration: Math.round(Math.min(9000, Math.max(1200, total)) * Math.max(0.05, input.pace ?? 1)), turn: least, settle: leg.type === "swing" ? swingDegrees(leg.swing) : 0, easing } };
|
|
895
|
+
const route = { ...base, cut: false, turn, share, legStart: share, arrive, leg };
|
|
896
|
+
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 } };
|
|
897
|
+
}
|
|
898
|
+
const FAULT_DRIFT = 0.02;
|
|
899
|
+
const FAULT_STEP = 0.3;
|
|
900
|
+
function faultsOf(route) {
|
|
901
|
+
const input = route.input;
|
|
902
|
+
const plan = { route };
|
|
903
|
+
const at = (t) => travelPose(input, t, plan);
|
|
904
|
+
const last = at(1);
|
|
905
|
+
const faults = { empty: 0, lost: 0, drift: 0, driftWorst: 0, step: 0, pace: 0, planet: 0, held: 0 };
|
|
906
|
+
const lastF = unit(input.end.forward);
|
|
907
|
+
let turned = 0;
|
|
908
|
+
let way = 0;
|
|
909
|
+
const positions = [at(0).position];
|
|
910
|
+
let before = at(0);
|
|
911
|
+
const afterTurn = route.turn !== null ? Math.max(1, Math.ceil(route.share * 240 - 1e-9)) : 0;
|
|
912
|
+
for (let i = 1; i <= 240; i += 1) {
|
|
913
|
+
const s = at(i / 240);
|
|
914
|
+
if (!s.leaving.inside && !s.destination.inside)
|
|
915
|
+
faults.empty += 1;
|
|
916
|
+
if (s.around !== undefined && !s.around.inside)
|
|
917
|
+
faults.planet += 1;
|
|
918
|
+
if (i / 240 < route.share && !s.leaving.inside && !(s.around?.inside === true))
|
|
919
|
+
faults.held += 1;
|
|
920
|
+
if (i >= afterTurn && !s.destination.inside)
|
|
921
|
+
faults.lost += 1;
|
|
922
|
+
if (i > afterTurn) {
|
|
923
|
+
const off = Math.hypot(s.destination.x - last.destination.x, s.destination.y - last.destination.y);
|
|
924
|
+
const offBefore = Math.hypot(before.destination.x - last.destination.x, before.destination.y - last.destination.y);
|
|
925
|
+
if (before.destination.inside && off > offBefore + FAULT_DRIFT) {
|
|
926
|
+
faults.drift += 1;
|
|
927
|
+
faults.driftWorst = Math.max(faults.driftWorst, off - offBefore);
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
if (s.destination.inside && before.destination.inside && Math.hypot(s.destination.x - before.destination.x, s.destination.y - before.destination.y) >= FAULT_STEP)
|
|
931
|
+
faults.step += 1;
|
|
932
|
+
const turn = angleOf(before.forward, s.forward) / RAD;
|
|
933
|
+
turned += turn;
|
|
934
|
+
if (turn >= 6 || angleOf(s.forward, lastF) - angleOf(before.forward, lastF) > 0.05 * RAD)
|
|
935
|
+
faults.pace += 1;
|
|
936
|
+
way += length(sub(s.position, before.position));
|
|
937
|
+
positions.push(s.position);
|
|
938
|
+
before = s;
|
|
939
|
+
}
|
|
940
|
+
for (let i = 1; i < positions.length; i += 1)
|
|
941
|
+
if (length(sub(positions[i], positions[i - 1])) > 0.06 * way)
|
|
942
|
+
faults.pace += 1;
|
|
943
|
+
if (turned > 1.25 * (angleOf(unit(input.start.forward), lastF) / RAD) + 2)
|
|
944
|
+
faults.pace += 1;
|
|
945
|
+
return faults;
|
|
946
|
+
}
|
|
947
|
+
export const total = (f) => f.empty + f.lost + f.drift + f.step;
|
|
948
|
+
/**
|
|
949
|
+
* `now` has no kind of fault more than `was` (and no more of the drift in its worst sample) and fewer in all. A way that keeps the planet in the picture all through (`was.planet` is 0: the views promised it) must go
|
|
950
|
+
* on keeping it; where the first way did not (plain views never promised it) the planet out of the picture is not a fault, and only breaks a tie between two ways.
|
|
951
|
+
*/
|
|
952
|
+
export const betterThan = (now, was) => now.empty <= was.empty && now.lost <= was.lost && now.drift <= was.drift && now.step <= was.step && now.pace <= was.pace && now.driftWorst <= was.driftWorst + 1e-12 && (was.planet > 0 || now.planet === 0) && total(now) < total(was);
|
|
953
|
+
/** The samples of a walk of the move (a 240th of it apart) at which neither the moon left nor the destination is in the picture. */
|
|
954
|
+
function emptyMoments(route) {
|
|
955
|
+
const input = route.input;
|
|
956
|
+
const frameFor = (fov) => frameOf(fov, input.aspect);
|
|
957
|
+
let empty = 0;
|
|
958
|
+
for (let i = 1; i <= 240; i += 1) {
|
|
959
|
+
const t = i / 240;
|
|
960
|
+
const at = along(route, t);
|
|
961
|
+
const frame = frameFor(at.fov);
|
|
962
|
+
if (!sightOf(input.leaving, t, at.position, at.o, frame).inside && !sightOf(input.destination, t, at.position, at.o, frame).inside)
|
|
963
|
+
empty += 1;
|
|
964
|
+
}
|
|
965
|
+
return empty;
|
|
966
|
+
}
|
|
967
|
+
function withAim(input, first, stop) {
|
|
968
|
+
const route = first.route;
|
|
969
|
+
if (route.turn === null || first.plan.path !== "circle" || route.cut || route.share <= 0)
|
|
970
|
+
return first;
|
|
971
|
+
const was = emptyMoments(route);
|
|
972
|
+
if (was === 0)
|
|
973
|
+
return first;
|
|
974
|
+
const share = route.share;
|
|
975
|
+
const swing = route.turn;
|
|
976
|
+
const flawsOf = (candidate) => aimFlaws(input, candidate, route.shells, route.leg, share);
|
|
977
|
+
// each way of leaning out and each pace, with the least backing off that keeps the nearer moon inside 0.95 of the way to the edge and every pace of the walk inside its limit; of those the one
|
|
978
|
+
// that costs least (the log of the backing off, the lean and the slowing) is taken, and none if none will do
|
|
979
|
+
let cost = Number.POSITIVE_INFINITY;
|
|
980
|
+
let best = null;
|
|
981
|
+
const kinds = AIM_SLOW.flatMap((slow) => AIM_BEND.map((bend) => ({ slow, bend, spent: 0.5 * bend + 2 * Math.log(slow) }))).sort((a, b) => a.spent - b.spent);
|
|
982
|
+
const bases = new Map();
|
|
983
|
+
for (const { slow, bend, spent: lean } of kinds) {
|
|
984
|
+
if (best !== null && lean >= cost)
|
|
985
|
+
break;
|
|
986
|
+
if (stop?.() === true)
|
|
987
|
+
break;
|
|
988
|
+
// the look directions and ups are the same at every pace: they are worked out once for each lean
|
|
989
|
+
if (!bases.has(bend))
|
|
990
|
+
bases.set(bend, aimOf(input, swing, share, bend, 1));
|
|
991
|
+
const base = { ...bases.get(bend), slow };
|
|
992
|
+
for (const far of AIM_FAR) {
|
|
993
|
+
const spent = Math.log(far) + lean;
|
|
994
|
+
if (best !== null && spent >= cost)
|
|
995
|
+
break;
|
|
996
|
+
if (stop?.() === true)
|
|
997
|
+
break;
|
|
998
|
+
const candidate = { ...swing, aim: { ...base, far } };
|
|
999
|
+
const flaws = flawsOf(candidate);
|
|
1000
|
+
if (flaws.edge <= AIM_EDGE && flaws.fair) {
|
|
1001
|
+
best = candidate;
|
|
1002
|
+
cost = spent;
|
|
1003
|
+
break;
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
if (best === null)
|
|
1008
|
+
return first;
|
|
1009
|
+
const aimed = { ...route, turn: best };
|
|
1010
|
+
if (!flawsOf(best).fair || emptyMoments(aimed) >= was)
|
|
1011
|
+
return first;
|
|
1012
|
+
// the same shares of the move, and a length that is not less than it was
|
|
1013
|
+
const t0 = turnMs(best);
|
|
1014
|
+
const total = Math.max(t0 / share, first.plan.duration / Math.max(0.05, input.pace ?? 1));
|
|
1015
|
+
return { route: aimed, plan: { ...first.plan, duration: Math.round(Math.min(9000, Math.max(1200, total)) * Math.max(0.05, input.pace ?? 1)) } };
|
|
1016
|
+
}
|
|
1017
|
+
/** 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. */
|
|
1018
|
+
const BLENDS = [1, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1];
|
|
1019
|
+
const BLEND_SAVES = 0.6;
|
|
1020
|
+
/** The moment the arrival starts for a way on that starts at `legStart`. */
|
|
1021
|
+
const arriveFor = (leg, legStart) => {
|
|
1022
|
+
const near = leg.type === "line" ? 2 * leg.eb : leg.dn - leg.d1;
|
|
1023
|
+
let lo = 0;
|
|
1024
|
+
let hi = 1;
|
|
1025
|
+
for (let i = 0; i < 30; i += 1) {
|
|
1026
|
+
const mid = (lo + hi) / 2;
|
|
1027
|
+
if (leftOf(ease(mid), leg.l, leg.ea, leg.eb) > near)
|
|
1028
|
+
lo = mid;
|
|
1029
|
+
else
|
|
1030
|
+
hi = mid;
|
|
1031
|
+
}
|
|
1032
|
+
return legStart + (1 - legStart) * Math.min(0.9, Math.max(0.6, hi));
|
|
1033
|
+
};
|
|
1034
|
+
/** 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. */
|
|
1035
|
+
function steepest(route) {
|
|
1036
|
+
const plan = { route };
|
|
1037
|
+
let before = travelPose(route.input, 0, plan);
|
|
1038
|
+
let worst = 0;
|
|
1039
|
+
for (let i = 1; i <= 240; i += 1) {
|
|
1040
|
+
const s = travelPose(route.input, i / 240, plan);
|
|
1041
|
+
worst = Math.max(worst, angleOf(before.forward, s.forward) / RAD, angleOf(before.up, s.up) / RAD);
|
|
1042
|
+
before = s;
|
|
1043
|
+
}
|
|
1044
|
+
return worst;
|
|
1045
|
+
}
|
|
1046
|
+
/** 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). */
|
|
1047
|
+
function turnEdge(route) {
|
|
1048
|
+
const plan = { route };
|
|
1049
|
+
const off = (sight) => (sight.inside ? Math.max(Math.abs(sight.x), Math.abs(sight.y)) : Number.POSITIVE_INFINITY);
|
|
1050
|
+
let edge = 0;
|
|
1051
|
+
for (let i = 0; i <= 240; i += 1) {
|
|
1052
|
+
if (i / 240 >= route.share)
|
|
1053
|
+
break;
|
|
1054
|
+
const s = travelPose(route.input, i / 240, plan);
|
|
1055
|
+
edge = Math.max(edge, Math.min(off(s.leaving), off(s.destination)));
|
|
1056
|
+
}
|
|
1057
|
+
return edge;
|
|
1058
|
+
}
|
|
1059
|
+
/** `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). */
|
|
1060
|
+
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);
|
|
1061
|
+
/**
|
|
1062
|
+
* 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
|
|
1063
|
+
* 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
|
|
1064
|
+
* 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
|
|
1065
|
+
* time the two stages now share.
|
|
1066
|
+
*/
|
|
1067
|
+
function blended(first) {
|
|
1068
|
+
const route = first.route;
|
|
1069
|
+
// 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
|
|
1070
|
+
if (route.cut || route.turn === null || route.leg.type !== "line" || route.share <= 0 || route.share >= 1)
|
|
1071
|
+
return first;
|
|
1072
|
+
const was = faultsOf(route);
|
|
1073
|
+
const rate = Math.max(1.25 * steepest(route), 3);
|
|
1074
|
+
for (const share of BLENDS) {
|
|
1075
|
+
const legStart = route.share * (1 - share);
|
|
1076
|
+
const candidate = { ...route, legStart, arrive: arriveFor(route.leg, legStart) };
|
|
1077
|
+
if (!noWorse(faultsOf(candidate), was) || steepest(candidate) > rate)
|
|
1078
|
+
continue;
|
|
1079
|
+
// 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
|
|
1080
|
+
if (route.turn.aim !== undefined && turnEdge(candidate) > AIM_EDGE)
|
|
1081
|
+
continue;
|
|
1082
|
+
const pace = Math.max(0.05, route.input.pace ?? 1);
|
|
1083
|
+
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);
|
|
1084
|
+
return { route: candidate, plan: { ...first.plan, duration, blend: share, stages: { turn: route.share, travel: candidate.arrive - route.share, arrive: 1 - candidate.arrive } } };
|
|
1085
|
+
}
|
|
1086
|
+
return first;
|
|
1087
|
+
}
|
|
1088
|
+
/**
|
|
1089
|
+
* 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
|
|
1090
|
+
* 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
|
|
1091
|
+
* it leaves fewer such pictures and keeps to the pace limits. Every other move, and every such move whose walk has no empty picture, is the one it always was.
|
|
1092
|
+
*/
|
|
1093
|
+
const routes = new WeakMap();
|
|
1094
|
+
/** The ways to try in turn, the likeliest to be taken first (by how often each was in the survey of 360 walks): the nearest stand and a swing, a stand beyond the destination at 2, 4 and then 1 and 8 times the planet's distance from it. */
|
|
1095
|
+
const WAYS = [1, 2, 4, 5, 3, 6];
|
|
1096
|
+
function routeOf(input) {
|
|
1097
|
+
const kept = routes.get(input);
|
|
1098
|
+
if (kept !== undefined)
|
|
1099
|
+
return kept;
|
|
1100
|
+
// 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)
|
|
1101
|
+
Object.freeze(input);
|
|
1102
|
+
const made = blended(routeMade(input));
|
|
1103
|
+
routes.set(input, made);
|
|
1104
|
+
return made;
|
|
1105
|
+
}
|
|
1106
|
+
function routeMade(input) {
|
|
1107
|
+
// the time is counted from the start of the plan, and when it is up the other ways stop looking and the best so far is kept; the way a move is first made, with its aim turn, is always worked out, as it is the way
|
|
1108
|
+
// before the other ways were tried and what they are measured against
|
|
1109
|
+
const started = input.budget?.now();
|
|
1110
|
+
const stop = input.budget === undefined || started === undefined ? undefined : () => input.budget.now() - started >= input.budget.ms;
|
|
1111
|
+
let first = withAim(input, routeBase(input));
|
|
1112
|
+
if (first.plan.path === "pivot" && first.route.turn !== null && first.route.share > 0) {
|
|
1113
|
+
// 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
|
|
1114
|
+
const framed = routeBase(input, 0, true);
|
|
1115
|
+
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))
|
|
1116
|
+
first = framed;
|
|
1117
|
+
}
|
|
1118
|
+
if (first.route.turn === null || first.plan.path !== "circle" || first.route.cut || first.route.share <= 0)
|
|
1119
|
+
return first;
|
|
1120
|
+
const was = faultsOf(first.route);
|
|
1121
|
+
if (total(was) === 0)
|
|
1122
|
+
return first;
|
|
1123
|
+
// other ways of making the way on, each given the aim turn if it needs one: the nearest stand if its way clears the planet and a swing if not, and stands beyond the destination; the one with the fewest faults
|
|
1124
|
+
// that has none of any kind more than the way it was made is taken, and of two with the same faults the one that keeps the planet in the picture longer; the first with no fault stops the search, and so does the
|
|
1125
|
+
// budget, keeping the best so far
|
|
1126
|
+
let best = first;
|
|
1127
|
+
let least = was;
|
|
1128
|
+
for (const variant of WAYS) {
|
|
1129
|
+
if (stop?.() === true)
|
|
1130
|
+
break;
|
|
1131
|
+
const raw = routeBase(input, variant);
|
|
1132
|
+
if (stop?.() === true)
|
|
1133
|
+
break;
|
|
1134
|
+
if (raw.plan.path !== "circle" || raw.route.turn === null)
|
|
1135
|
+
continue;
|
|
1136
|
+
// the aim turn cures only the empty pictures of the turn: a way made with more of any other fault than the way it was is not taken further
|
|
1137
|
+
const rough = faultsOf(raw.route);
|
|
1138
|
+
if (rough.lost > was.lost || rough.drift > was.drift || rough.step > was.step || rough.pace > was.pace || rough.driftWorst > was.driftWorst + 1e-12)
|
|
1139
|
+
continue;
|
|
1140
|
+
const other = rough.empty > 0 ? withAim(input, raw, stop) : raw;
|
|
1141
|
+
if (other.route.turn === null)
|
|
1142
|
+
continue;
|
|
1143
|
+
const faults = rough.empty > 0 ? faultsOf(other.route) : rough;
|
|
1144
|
+
const wins = (a, b) => total(a) < total(b) || (total(a) === total(b) && (a.driftWorst < b.driftWorst - 1e-12 || (a.driftWorst <= b.driftWorst + 1e-12 && a.planet < b.planet)));
|
|
1145
|
+
if (betterThan(faults, was) && (best === first || wins(faults, least))) {
|
|
1146
|
+
best = other;
|
|
1147
|
+
least = faults;
|
|
1148
|
+
}
|
|
1149
|
+
if (total(least) === 0)
|
|
1150
|
+
break;
|
|
1151
|
+
}
|
|
1152
|
+
return best;
|
|
481
1153
|
}
|
|
482
1154
|
/**
|
|
483
1155
|
* The plan of a move: its stages and the share of the time each takes, how long it should last, and how it is eased. The length is a suggestion from how far the picture changes (16 milliseconds a degree
|
|
@@ -561,14 +1233,20 @@ export function travelPose(input, t, plan) {
|
|
|
561
1233
|
sample.around = sightOf(input.around, time, view.position, o, frame);
|
|
562
1234
|
return sample;
|
|
563
1235
|
}
|
|
1236
|
+
/** 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. */
|
|
1237
|
+
const BEHIND_LIT = 0.5;
|
|
564
1238
|
/**
|
|
565
1239
|
* The side of a body to stand on so a companion is in the picture, and the body stays at least half lit.
|
|
566
1240
|
* 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
1241
|
* 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
1242
|
* 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
1243
|
*
|
|
570
|
-
*
|
|
571
|
-
*
|
|
1244
|
+
* `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
|
|
1245
|
+
* (`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),
|
|
1246
|
+
* the rules above stand: the lit side wins, with a moon where one fits, and the body left is not in the last view.
|
|
1247
|
+
*
|
|
1248
|
+
* @param input - The body, where the move comes from, the Sun, the companions in rank order, and optionally `behind`.
|
|
1249
|
+
* @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.
|
|
572
1250
|
*
|
|
573
1251
|
* @example
|
|
574
1252
|
* ```ts
|
|
@@ -611,11 +1289,43 @@ export function arrivalSide(input) {
|
|
|
611
1289
|
const limb = Math.atan(input.radius / distance);
|
|
612
1290
|
return angleOf(forward, unit(v)) > limb * 1.15;
|
|
613
1291
|
};
|
|
1292
|
+
// 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
|
|
1293
|
+
const level = (side, at) => {
|
|
1294
|
+
const forward = scale(side, -1);
|
|
1295
|
+
const north = sub([0, 1, 0], scale(forward, forward[1]));
|
|
1296
|
+
const up = length(north) < 1e-6 ? squareTo(forward) : unit(north);
|
|
1297
|
+
const v = sub(at, add(input.body, scale(side, distance)));
|
|
1298
|
+
const z = dot(v, forward);
|
|
1299
|
+
const x = dot(v, cross(forward, up)) / (tv * aspect * z);
|
|
1300
|
+
const y = dot(v, up) / (tv * z);
|
|
1301
|
+
// a near-square picture has no room beside the disc: any side of it will do
|
|
1302
|
+
return aspect > 0.77 && aspect < 1.3 ? true : aspect >= 1 ? Math.abs(y) <= 0.45 : Math.abs(x) <= 0.45;
|
|
1303
|
+
};
|
|
614
1304
|
const found = [];
|
|
615
1305
|
const half = Math.PI / 2 + 1e-4;
|
|
616
1306
|
const companions = input.companions ?? [];
|
|
1307
|
+
const behind = input.behind;
|
|
617
1308
|
const consider = (side) => {
|
|
618
1309
|
const u = length(side) < 1e-9 ? approach : unit(side);
|
|
1310
|
+
// 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
|
|
1311
|
+
// ...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)
|
|
1312
|
+
if (behind !== undefined && (sunDir === null || litOf(u) >= BEHIND_LIT) && frames(u, behind) && level(u, behind)) {
|
|
1313
|
+
// 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
|
|
1314
|
+
let moon = null;
|
|
1315
|
+
let framed = false;
|
|
1316
|
+
for (const companion of companions) {
|
|
1317
|
+
if (companion.rank === 1 && frames(u, companion.at)) {
|
|
1318
|
+
framed = true;
|
|
1319
|
+
moon = companion.id ?? null;
|
|
1320
|
+
break;
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
const near = angleOf(u, approach);
|
|
1324
|
+
const top = found[0];
|
|
1325
|
+
if (top === undefined || top.rank > 0 || (framed && !top.both) || (framed === (top.both === true) && near < top.near - 1e-6))
|
|
1326
|
+
found[0] = { side: u, rank: 0, near, id: moon, both: framed };
|
|
1327
|
+
return;
|
|
1328
|
+
}
|
|
619
1329
|
if (sunDir && angleOf(u, sunDir) > half)
|
|
620
1330
|
return;
|
|
621
1331
|
let rank = 99;
|
|
@@ -634,7 +1344,7 @@ export function arrivalSide(input) {
|
|
|
634
1344
|
found[0] = { side: u, rank, near, id };
|
|
635
1345
|
};
|
|
636
1346
|
consider(approach);
|
|
637
|
-
for (const companion of companions) {
|
|
1347
|
+
for (const companion of behind === undefined ? companions : [{ at: behind, rank: 2 }, ...companions]) {
|
|
638
1348
|
const away = sub(input.body, companion.at);
|
|
639
1349
|
if (length(away) < 1e-6)
|
|
640
1350
|
continue;
|
|
@@ -657,7 +1367,7 @@ export function arrivalSide(input) {
|
|
|
657
1367
|
}
|
|
658
1368
|
const picked = found[0];
|
|
659
1369
|
const side = picked?.side ?? swing(approach);
|
|
660
|
-
return { side, lit: litOf(side), companion: picked?.id ?? null };
|
|
1370
|
+
return { side, lit: litOf(side), companion: picked?.id ?? null, behind: picked?.rank === 0 };
|
|
661
1371
|
}
|
|
662
1372
|
/**
|
|
663
1373
|
* 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
|
|
@@ -682,5 +1392,5 @@ export function travelArrival(input) {
|
|
|
682
1392
|
const forward = scale(side, -1);
|
|
683
1393
|
const north = input.north ?? [0, 1, 0];
|
|
684
1394
|
const across = sub(north, scale(forward, dot(north, forward)));
|
|
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 };
|
|
1395
|
+
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 };
|
|
686
1396
|
}
|