mln-rs-engine 0.0.22

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
//! Gestures on the globe: drags with the bearing held or free, flings, and
//! zooms and turns around an anchor.

use approx::assert_abs_diff_eq;
use mln_style::constants::LATITUDE_MAX;

use super::*;

#[test]
fn move_by_puts_the_dragged_point_at_the_center() {
    let mut transform = globe(ll(10.0, 20.0), 3.0, 0.0, 0.0);
    for i in 0..8 {
        let offset = sc(if i % 2 != 0 { 60.0 } else { -45.0 }, if i % 3 != 0 { 30.0 } else { -50.0 });
        let expected =
            transform.screen_coordinate_to_lat_lng(&sc(400.0 - offset.x, 300.0 - offset.y), WrapMode::Wrapped).unwrap();
        let zoom_before = transform.view().zoom();
        let latitude_before = transform.view().center(WrapMode::Wrapped).latitude();
        transform.move_by(&offset, &AnimationOptions::default()).unwrap();
        assert_abs_diff_eq!(expected.latitude(), transform.view().center(WrapMode::Wrapped).latitude(), epsilon = 1e-6);
        assert_abs_diff_eq!(
            expected.longitude(),
            transform.view().center(WrapMode::Wrapped).longitude(),
            epsilon = 1e-6
        );
        // The planet keeps its apparent size: zoom follows the latitude.
        assert_abs_diff_eq!(
            zoom_before + VerticalPerspectiveProjection::zoom_adjustment(latitude_before, expected.latitude()),
            transform.view().zoom(),
            epsilon = 1e-6
        );
    }
}

#[test]
fn anchored_zoom_keeps_the_anchor_in_place() {
    let mut transform = globe(ll(30.0, 15.0), 3.0, 0.0, 0.0);
    let anchor = sc(250.0, 180.0);
    let anchor_lat_lng = transform.screen_coordinate_to_lat_lng(&anchor, WrapMode::Wrapped).unwrap();
    transform.jump_to(&CameraOptions { zoom: Some(4.5), anchor: Some(anchor), ..Default::default() });
    assert_ne!(30.0, transform.view().center(WrapMode::Wrapped).latitude());
    let after = transform.lat_lng_to_screen_coordinate(&anchor_lat_lng);
    assert_abs_diff_eq!(anchor.x, after.x, epsilon = 0.5);
    assert_abs_diff_eq!(anchor.y, after.y, epsilon = 0.5);
}

#[test]
fn zooming_out_around_an_anchor_near_the_horizon_stays_continuous() {
    // A fast pinch-out with the focal point drifting off the planet: near the
    // horizon a damped heuristic takes over from the exact re-anchoring, so
    // no frame jumps the center by degrees of latitude or the planet's size.
    let mut transform = globe(ll(48.0, 10.0), 6.0, 0.0, 0.0);
    let anchor = sc(600.0, 200.0);
    let mut last_radius = globe_radius(transform.state());
    let mut last_center = transform.view().center(WrapMode::Wrapped);
    for i in 1..=60 {
        transform.ease_to(
            &CameraOptions { zoom: Some(6.0 - f64::from(i) * 0.1), anchor: Some(anchor), ..Default::default() },
            &AnimationOptions::default(),
        );
        if i % 3 == 0 {
            transform.move_by(&sc(-7.0, 4.0), &AnimationOptions::default()).unwrap();
        }
        let radius = globe_radius(transform.state());
        let center = transform.view().center(WrapMode::Wrapped);
        assert!(radius <= last_radius * 1.005, "step {i}");
        assert!((center.latitude() - last_center.latitude()).abs() < 5.0, "step {i}");
        last_radius = radius;
        last_center = center;
    }
}

// Near the limb the zoom around a point leans on a damped heuristic, which
// adds up step by step: an animated zoom moves the center about as far
// as the same zoom in one jump.
#[test]
fn animated_zoom_around_a_point_near_the_limb_moves_like_a_jump() {
    let anchor = sc(256.0 + 159.0, 256.0);
    let mut jumped = square_globe(ll(0.0, 0.0), 1.0);
    jumped.jump_to(&CameraOptions { zoom: Some(2.0), anchor: Some(anchor), ..Default::default() });
    let jump_move = jumped.view().center(WrapMode::Wrapped).longitude();
    assert!(jump_move > 1.0, "{jump_move}");

    let mut animated = square_globe(ll(0.0, 0.0), 1.0);
    let start = start_clock(&mut animated);
    animated.ease_to(&CameraOptions { zoom: Some(2.0), anchor: Some(anchor), ..Default::default() }, &linear_second());
    for frame in 1..=10 {
        run_to(&mut animated, start, f64::from(frame) / 10.0);
    }
    assert_abs_diff_eq!(2.0, animated.view().zoom(), epsilon = 1e-9);
    let animated_move = animated.view().center(WrapMode::Wrapped).longitude();
    assert!(animated_move > 0.75 * jump_move, "{animated_move} {jump_move}");
    assert!(animated_move < 1.25 * jump_move, "{animated_move} {jump_move}");
}

// An animation around an anchor ends where the same change in one jump does,
// zoom included: the planet keeps its size as the anchor carries the center
// to another latitude.
#[test]
fn animations_around_an_anchor_end_like_a_jump() {
    let anchor = sc(256.0, 256.0 - 150.0);
    for rotate in [false, true] {
        let change = if rotate {
            CameraOptions { bearing: Some(60.0), anchor: Some(anchor), ..Default::default() }
        } else {
            CameraOptions { zoom: Some(4.0), anchor: Some(anchor), ..Default::default() }
        };
        let mut jumped = square_globe(ll(45.0, 0.0), 3.0);
        jumped.jump_to(&change);
        assert!((jumped.view().center(WrapMode::Wrapped).latitude() - 45.0).abs() > 0.5);

        let mut animated = square_globe(ll(45.0, 0.0), 3.0);
        let start = start_clock(&mut animated);
        animated.ease_to(&change, &linear_second());
        for frame in 1..=10 {
            run_to(&mut animated, start, f64::from(frame) / 10.0);
        }
        assert_abs_diff_eq!(
            jumped.view().center(WrapMode::Wrapped).latitude(),
            animated.view().center(WrapMode::Wrapped).latitude(),
            epsilon = 1e-6
        );
        assert_abs_diff_eq!(
            jumped.view().center(WrapMode::Wrapped).longitude(),
            animated.view().center(WrapMode::Wrapped).longitude(),
            epsilon = 1e-6
        );
        assert_abs_diff_eq!(jumped.view().zoom(), animated.view().zoom(), epsilon = 1e-6);
    }
}

// A fling turns the planet by degrees per pixel at the center's latitude,
// and the zoom follows the latitude to keep the planet's size.
#[test]
fn a_globe_fling_turns_the_planet_by_degrees_per_pixel() {
    struct Pan {
        center: LatLng,
        zoom: f64,
        bearing: f64,
        offset: ScreenCoordinate,
        expected: LatLng,
    }
    for pan in [
        Pan {
            center: ll(30.0, -20.0),
            zoom: 4.0,
            bearing: 30.0,
            offset: sc(120.0, -80.0),
            expected: ll(29.646_746_670_134_75, -26.324_743_340_269_5),
        },
        Pan {
            center: ll(-45.0, 170.0),
            zoom: 8.0,
            bearing: -100.0,
            offset: sc(-50.0, 25.0),
            expected: ll(-44.912_800_093_917_55, 169.908_531_669_806_56),
        },
        Pan {
            center: ll(70.0, 0.0),
            zoom: 5.0,
            bearing: 0.0,
            offset: sc(200.0, 0.0),
            expected: ll(70.0, -4.237_578_154_240_85),
        },
    ] {
        let mut transform = globe(pan.center, pan.zoom, pan.bearing, 0.0);
        let start = start_clock(&mut transform);
        transform.move_by(&pan.offset, &linear_second()).unwrap();
        transform.update_transitions(Some(start + Duration::from_millis(1000)));
        assert_abs_diff_eq!(
            pan.expected.latitude(),
            transform.view().center(WrapMode::Wrapped).latitude(),
            epsilon = 1e-9
        );
        assert_abs_diff_eq!(
            pan.expected.longitude(),
            transform.view().center(WrapMode::Wrapped).longitude(),
            epsilon = 1e-9
        );
        assert_abs_diff_eq!(
            pan.zoom + VerticalPerspectiveProjection::zoom_adjustment(pan.center.latitude(), pan.expected.latitude()),
            transform.view().zoom(),
            epsilon = 1e-9
        );
    }
}

// A fling toward a pole eases along its own meridian, as far toward the pole
// as the zoom it starts from lets the center go: onto the pole from zoom 1.5
// on this 800 by 600 view, short of it from zoom 3. It doesn't carry the
// center over the pole.
#[test]
fn globe_fling_toward_a_pole_stays_on_its_meridian() {
    struct Fling {
        zoom: f64,
        /// The latitude and zoom halfway and at the end.
        halves: [(f64, f64); 2],
    }
    for fling in [
        Fling { zoom: 1.5, halves: [(75.0, 0.550_015_686_523_503_5), (90.0, -1.035_051_843_768_116_9)] },
        Fling {
            zoom: 3.0,
            halves: [(73.217_160_109_142_3, 2.207_868_201_499_963_8), (86.434_320_218_284_63, 0.464_948_156_231_883_1)],
        },
    ] {
        let mut transform = globe(ll(60.0, 10.0), fling.zoom, 0.0, 0.0);
        let start = start_clock(&mut transform);
        transform.move_by(&sc(0.0, 3000.0), &linear_second()).unwrap();
        let mut latitude = 60.0;
        for frame in 1..=20 {
            transform.update_transitions(Some(start + Duration::from_millis(50 * frame)));
            assert_abs_diff_eq!(10.0, transform.view().center(WrapMode::Wrapped).longitude(), epsilon = 1e-9);
            assert!(transform.view().center(WrapMode::Wrapped).latitude() >= latitude - 1e-9, "frame {frame}");
            latitude = transform.view().center(WrapMode::Wrapped).latitude();
            if frame % 10 == 0 {
                let (expected_latitude, expected_zoom) = fling.halves[(frame / 10 - 1) as usize];
                assert_abs_diff_eq!(expected_latitude, latitude, epsilon = 1e-6);
                assert_abs_diff_eq!(expected_zoom, transform.view().zoom(), epsilon = 1e-6);
            }
        }
    }
}

// A fling worth more than half a turn stops at 179.5° the way it was going,
// rather than easing the shorter way round, against the fling.
#[test]
fn globe_fling_stops_short_of_half_a_turn() {
    let mut transform = globe(ll(0.0, 0.0), 0.5, 0.0, 0.0);
    let start = start_clock(&mut transform);
    transform.move_by(&sc(-5000.0, 0.0), &linear_second()).unwrap();
    let mut longitude = 0.0;
    for frame in 1..=20 {
        transform.update_transitions(Some(start + Duration::from_millis(50 * frame)));
        assert!(transform.view().center(WrapMode::Unwrapped).longitude() >= longitude - 1e-9, "frame {frame}");
        longitude = transform.view().center(WrapMode::Unwrapped).longitude();
    }
    assert_abs_diff_eq!(179.5, longitude, epsilon = 1e-9);
    assert_abs_diff_eq!(0.0, transform.view().center(WrapMode::Wrapped).latitude(), epsilon = 1e-9);
}

// Where one step of a drag with the bearing held takes the center of a 640
// by 480 globe: around its middle, off center, into, at and around a pole,
// off the planet, pitched and turned, and across the antimeridian.
#[test]
fn a_bearing_held_drag_step_moves_the_center() {
    // (name, center, zoom, bearing, pitch, point, panDelta, expected)
    type Step = (&'static str, (f64, f64), f64, f64, f64, (f64, f64), (f64, f64), (f64, f64));
    let steps: [Step; 11] = [
        (
            "center-drag-east 0",
            (20.0, 10.0),
            1.0,
            0.0,
            0.0,
            (330.0, 240.0),
            (10.0, 0.0),
            (19.965_282_208_712_78, 6.481_528_881_844_27),
        ),
        (
            "off-center-drag 2",
            (16.074_405_186_537_916, 4.212_860_800_653_175),
            1.032_201_791_216_781_6,
            0.0,
            0.0,
            (400.0, 180.0),
            (6.0, -4.0),
            (14.115_311_037_321_156, 1.360_949_627_755_758_2),
        ),
        (
            "drag-north-into-pole 0",
            (70.0, -100.0),
            1.0,
            0.0,
            0.0,
            (320.0, 300.0),
            (0.0, 6.0),
            (70.737_287_200_838_54, -100.000_000_000_002_05),
        ),
        (
            "drag-north-into-pole 28",
            (90.0, -100.000_000_000_000_68),
            -0.987_205_044_226_623_8,
            0.0,
            0.0,
            (320.0, 300.0),
            (0.0, 6.0),
            (90.0, -100.000_000_000_000_68),
        ),
        (
            "side-drag-at-pole 0",
            (85.051_128_779_806_6, -100.0),
            1.0,
            0.0,
            0.0,
            (380.0, 240.0),
            (6.0, 0.0),
            (85.047_765_611_396_3, -101.871_378_175_903_71),
        ),
        (
            "side-drag-centred-pole 0",
            (89.0, 0.0),
            0.5,
            0.0,
            0.0,
            (340.0, 200.0),
            (0.0, 8.0),
            (89.344_388_290_579_13, 13.508_337_366_700_857),
        ),
        (
            "drag-off-globe 0",
            (10.0, 5.0),
            1.0,
            0.0,
            0.0,
            (620.0, 240.0),
            (20.0, 0.0),
            (9.982_236_219_514_714, 1.549_461_030_267_593_7),
        ),
        (
            "drag-bearing-pitch 0",
            (40.0, 30.0),
            2.5,
            30.0,
            40.0,
            (300.0, 330.0),
            (-7.0, 5.0),
            (40.100_686_468_998_106, 30.988_488_451_527_928),
        ),
        (
            "drag-across-antimeridian 0",
            (0.0, 175.0),
            1.5,
            0.0,
            0.0,
            (300.0, 240.0),
            (-12.0, 0.0),
            (0.0, 177.994_624_839_919_3),
        ),
        (
            "drag-south-pole 0",
            (-80.0, 40.0),
            0.8,
            0.0,
            0.0,
            (320.0, 200.0),
            (0.0, -7.0),
            (-80.493_057_112_485_56, 40.000_000_000_001_48),
        ),
        (
            "drag-south-pole 20",
            (-89.861_142_249_709_03, 40.000_000_000_001_93),
            -0.209_291_019_464_406_45,
            0.0,
            0.0,
            (320.0, 200.0),
            (0.0, -7.0),
            (-90.0, 40.000_000_000_001_93),
        ),
    ];
    for (name, center, zoom, bearing, pitch, point, pan_delta, expected) in steps {
        let mut transform = sized(640, 480);
        transform.set_projection(ProjectionDefinition::from_name("vertical-perspective").into());
        transform.jump_to(&CameraOptions {
            center: Some(ll(center.0, center.1)),
            zoom: Some(zoom),
            bearing: Some(bearing),
            pitch: Some(pitch),
            ..Default::default()
        });
        let center = VerticalPerspectiveProjection::drag_center(
            transform.state().view(),
            &sc(point.0, 480.0 - point.1),
            &sc(pan_delta.0, -pan_delta.1),
            0.0,
        );
        assert!((expected.0 - center.latitude()).abs() < 1e-7, "{name}: {center:?}");
        assert_abs_diff_eq!(
            0.0,
            VerticalPerspectiveProjection::difference_of_angles_degrees(expected.1, center.longitude()),
            epsilon = 1e-4
        );
    }
}

// Dragging sideways at the pole turns the globe around it with the latitude
// held: the center doesn't slide away from the pole, nor the planet spin.
#[test]
fn drag_around_the_pole_holds_its_latitude() {
    let mut transform = sized(640, 480);
    transform.set_projection(ProjectionDefinition::from_name("vertical-perspective").into());
    transform.jump_to(&CameraOptions { center: Some(ll(LATITUDE_MAX, -100.0)), zoom: Some(1.0), ..Default::default() });
    for _ in 0..60 {
        transform.move_by_with_anchor(&sc(6.0, 0.0), Some(sc(380.0, 240.0)), &AnimationOptions::default()).unwrap();
    }
    assert_abs_diff_eq!(84.853_246_506_926_23, transform.view().center(WrapMode::Wrapped).latitude(), epsilon = 1e-6);
    assert_abs_diff_eq!(
        0.0,
        VerticalPerspectiveProjection::difference_of_angles_degrees(
            149.332_807_102_710_83,
            transform.view().center(WrapMode::Wrapped).longitude(),
        ),
        epsilon = 1e-6
    );
    assert_abs_diff_eq!(1.056_416_783_971_354_3, transform.view().zoom(), epsilon = 1e-6);
}

// Dragging north over the pole stops on it, the zoom holding the planet's
// size from the edge of the Mercator range on.
#[test]
fn drag_over_the_pole_stops_on_it() {
    let mut transform = sized(640, 480);
    transform.set_projection(ProjectionDefinition::from_name("vertical-perspective").into());
    transform.jump_to(&CameraOptions { center: Some(ll(70.0, -100.0)), zoom: Some(1.0), ..Default::default() });
    for _ in 0..40 {
        transform.move_by_with_anchor(&sc(0.0, 6.0), Some(sc(320.0, 300.0)), &AnimationOptions::default()).unwrap();
    }
    assert_eq!(90.0, transform.view().center(WrapMode::Wrapped).latitude());
    assert_abs_diff_eq!(-100.0, transform.view().center(WrapMode::Wrapped).longitude(), epsilon = 1e-6);
    assert_abs_diff_eq!(-0.987_205_044_226_623_8, transform.view().zoom(), epsilon = 1e-6);
}

// Where one step of a drag with a free bearing takes the center and the
// bearing of a 640 by 480 globe with a 45 degree field of view, the anchor
// the cursor while it is on the planet and the center otherwise: around its
// middle, over and beside the pole, off the planet and back, pitched and
// turned.
#[test]
fn a_free_bearing_drag_step_moves_the_center_and_the_bearing() {
    // (name, center, zoom, bearing, pitch, anchor, panDelta, expected,
    // expectedBearing)
    type Step = (&'static str, (f64, f64), f64, f64, f64, (f64, f64), (f64, f64), (f64, f64), f64);
    let steps: [Step; 8] = [
        (
            "mid-globe-east 0",
            (20.0, 10.0),
            1.0,
            0.0,
            0.0,
            (340.0, 240.0),
            (10.0, 0.0),
            (19.964_831_035_703_163, 6.458_738_472_277_901),
            -1.210_501_841_080_827_3,
        ),
        (
            "over-north-pole 13",
            (79.415_519_352_724_37, -99.999_999_999_999_89),
            0.103_153_326_393_601_52,
            3.410_605_131_648_481e-13,
            0.0,
            (320.0, 284.0),
            (0.0, 6.0),
            (80.146_390_714_591_33, -99.999_999_999_998_07),
            2.046_363_078_989_088_5e-12,
        ),
        (
            "over-north-pole 39",
            (78.968_703_400_624_05, 79.999_999_999_992_84),
            0.162_096_112_429_317_93,
            179.999_999_999_995,
            0.0,
            (320.0, 440.0),
            (0.0, 6.0),
            (77.908_291_660_738_7, 79.999_999_999_991_59),
            179.999_999_999_993_63,
        ),
        (
            "across-pole-on-globe 20",
            (89.838_065_021_078_38, -100.000_000_000_077_14),
            -0.009_291_019_464_406_594,
            -7.651_124_178_664_759e-11,
            0.0,
            (320.0, 328.0),
            (0.0, 8.0),
            (89.661_755_795_874_2, 80.000_000_000_030_92),
            -179.999_999_999_966_58,
        ),
        (
            "beside-north-pole 0",
            (85.05, -100.0),
            1.0,
            0.0,
            0.0,
            (386.0, 240.0),
            (6.0, 0.0),
            (85.046_622_784_836_69, -102.121_158_153_155_76),
            -2.113_243_435_839_649,
        ),
        (
            "off-globe-and-back 5",
            (59.631_823_345_711_61, 0.0),
            0.015_938_893_735_801_596,
            0.0,
            0.0,
            (320.0, 240.0),
            (0.0, 25.0),
            (68.485_536_141_423_99, 0.0),
            0.0,
        ),
        (
            "off-globe-and-back 15",
            (84.953_325_471_447_43, 180.0),
            -2.506_889_874_048_112_3,
            180.0,
            0.0,
            (320.0, 240.0),
            (0.0, -25.0),
            (86.192_961_732_847_01, 0.0),
            0.0,
        ),
        (
            "bearing-pitch 0",
            (40.0, 30.0),
            1.5,
            30.0,
            20.0,
            (308.0, 255.0),
            (8.0, -5.0),
            (39.905_229_314_899_54, 27.677_367_870_127_796),
            28.548_889_585_142_32,
        ),
    ];
    for (name, center, zoom, bearing, pitch, anchor, pan_delta, expected, expected_bearing) in steps {
        let mut transform = sized(640, 480);
        transform.set_projection(ProjectionDefinition::from_name("vertical-perspective").into());
        transform.jump_to(&CameraOptions {
            center: Some(ll(center.0, center.1)),
            zoom: Some(zoom),
            bearing: Some(bearing),
            pitch: Some(pitch),
            fov: Some(45.0),
            ..Default::default()
        });
        let drag = VerticalPerspectiveProjection::drag_camera(
            transform.state().view(),
            &sc(anchor.0, 480.0 - anchor.1),
            &sc(pan_delta.0, -pan_delta.1),
            0.0,
        );
        assert!((expected.0 - drag.center.latitude()).abs() < 1e-6, "{name}: {:?}", drag.center);
        assert_abs_diff_eq!(
            0.0,
            VerticalPerspectiveProjection::difference_of_angles_degrees(expected.1, drag.center.longitude()),
            epsilon = 1e-6
        );
        assert_abs_diff_eq!(
            0.0,
            VerticalPerspectiveProjection::difference_of_angles_degrees(expected_bearing, drag.bearing),
            epsilon = 1e-6
        );
    }
}

// With a free bearing the location under the cursor stays under it, over the
// pole too, where the bearing turns round; with the bearing held the drag
// stops on the pole and the location stays behind.
#[test]
fn free_bearing_drag_keeps_the_location_under_the_cursor() {
    for free_bearing in [true, false] {
        let mut transform = globe(ll(80.0, -100.0), 1.0, 0.0, 0.0);
        transform.set_globe_free_bearing(free_bearing);
        let mut cursor = sc(400.0, 220.0);
        let mut max_slip = 0.0;
        for _ in 0..40 {
            let grabbed = transform.screen_coordinate_to_lat_lng(&cursor, WrapMode::Wrapped).unwrap();
            cursor.y += 8.0;
            transform.move_by_with_anchor(&sc(0.0, 8.0), Some(cursor), &AnimationOptions::default()).unwrap();
            let point = transform.lat_lng_to_screen_coordinate(&grabbed);
            max_slip = f64::max(max_slip, (point.x - cursor.x).hypot(point.y - cursor.y));
        }
        if free_bearing {
            assert!(max_slip < 1e-6, "{max_slip}");
            assert_abs_diff_eq!(80.0, transform.view().center(WrapMode::Wrapped).longitude(), epsilon = 1e-6);
            assert_abs_diff_eq!(180.0, mln_style::math::rad2deg(transform.view().angle()).abs(), epsilon = 1e-6);
        } else {
            assert!(max_slip > 4.0, "{max_slip}");
            assert_eq!(90.0, transform.view().center(WrapMode::Wrapped).latitude());
        }
    }
}

// With the bearing held, the steps of a drag hold the location
// the first of them grabbed, until something else moves the camera. The step
// after that grabs the location then under the cursor, and doesn't bring the
// first one back.
#[test]
fn bearing_held_drag_holds_its_location_until_the_camera_moves() {
    let mut transform = globe(ll(30.0, 20.0), 2.0, 0.0, 0.0);
    let slip = |transform: &Transform, location: &LatLng, cursor: &ScreenCoordinate| {
        let point = transform.lat_lng_to_screen_coordinate(location);
        (point.x - cursor.x).hypot(point.y - cursor.y)
    };
    let mut cursor = sc(300.0, 200.0);
    let grabbed = transform.screen_coordinate_to_lat_lng(&cursor, WrapMode::Unwrapped).unwrap();
    for _ in 0..40 {
        cursor = sc(cursor.x + 5.0, cursor.y + 3.0);
        transform.move_by_with_anchor(&sc(5.0, 3.0), Some(cursor), &AnimationOptions::default()).unwrap();
        assert!(slip(&transform, &grabbed, &cursor) < 1e-6);
    }

    transform.jump_to(&CameraOptions { center: Some(ll(10.0, 60.0)), ..Default::default() });
    let regrabbed = transform.screen_coordinate_to_lat_lng(&cursor, WrapMode::Unwrapped).unwrap();
    for _ in 0..10 {
        cursor = sc(cursor.x - 5.0, cursor.y);
        transform.move_by_with_anchor(&sc(-5.0, 0.0), Some(cursor), &AnimationOptions::default()).unwrap();
        assert!(slip(&transform, &regrabbed, &cursor) < 1e-6);
    }
    assert!(slip(&transform, &grabbed, &cursor) > 50.0);
}

// A zoom around a point beside the pole: with the bearing held the center's
// longitude swings towards the point's and the planet turns about the pole;
// with a free bearing the planet only grows around the
// point.
#[test]
fn free_bearing_zoom_around_the_pole_keeps_the_views_orientation() {
    for free_bearing in [true, false] {
        let mut transform = globe(ll(89.0, 0.0), 1.0, 0.0, 0.0);
        transform.set_globe_free_bearing(free_bearing);
        let anchor = sc(520.0, 220.0);
        let location = transform.screen_coordinate_to_lat_lng(&anchor, WrapMode::Wrapped).unwrap();
        let beside = transform.screen_coordinate_to_lat_lng(&sc(anchor.x + 20.0, anchor.y), WrapMode::Wrapped).unwrap();
        transform.jump_to(&CameraOptions { zoom: Some(1.6), anchor: Some(anchor), ..Default::default() });
        let point = transform.lat_lng_to_screen_coordinate(&location);
        let beside_point = transform.lat_lng_to_screen_coordinate(&beside);
        let turn = mln_style::math::rad2deg((beside_point.y - point.y).atan2(beside_point.x - point.x));
        if free_bearing {
            assert_abs_diff_eq!(anchor.x, point.x, epsilon = 1e-6);
            assert_abs_diff_eq!(anchor.y, point.y, epsilon = 1e-6);
            assert_abs_diff_eq!(0.0, turn, epsilon = 0.5);
        } else {
            assert!(turn.abs() > 10.0, "{turn}");
        }
    }
}

// A fling over the pole with the bearing free turns the planet on about one
// axis: the center moves away from where it started at every frame, over the
// pole and down the far side, and the bearing turns round. With the bearing
// held it stops on the pole.
#[test]
fn free_bearing_fling_carries_on_over_the_pole() {
    for free_bearing in [true, false] {
        let mut transform = globe(ll(85.0, -100.0), 1.0, 0.0, 0.0);
        transform.set_globe_free_bearing(free_bearing);
        let start = VerticalPerspectiveProjection::surface_vector(&transform.view().center(WrapMode::Wrapped));
        let begin = start_clock(&mut transform);
        transform.move_by(&sc(0.0, 400.0), &linear_second()).unwrap();
        let mut previous = 0.0;
        for frame in 1..=20 {
            transform.update_transitions(Some(begin + Duration::from_millis(50 * frame)));
            let center = VerticalPerspectiveProjection::surface_vector(&transform.view().center(WrapMode::Wrapped));
            let distance = start.dot(center).clamp(-1.0, 1.0).acos();
            assert!(distance >= previous - 1e-9, "frame {frame}");
            previous = distance;
        }
        if free_bearing {
            assert!(transform.view().center(WrapMode::Wrapped).latitude() < 85.0);
            assert_abs_diff_eq!(80.0, transform.view().center(WrapMode::Wrapped).longitude(), epsilon = 1e-6);
            assert_abs_diff_eq!(180.0, mln_style::math::rad2deg(transform.view().angle()).abs(), epsilon = 1e-6);
        } else {
            assert_eq!(90.0, transform.view().center(WrapMode::Wrapped).latitude());
            assert_abs_diff_eq!(0.0, mln_style::math::rad2deg(transform.view().angle()), epsilon = 1e-9);
        }
    }
}