mln-rs-engine 0.0.23

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
//! Where the flat map draws a location and which location a point shows:
//! world copies, padding, anchors, moves, tile matrices and the Mercator
//! transform's answers.

use approx::{assert_abs_diff_eq, assert_relative_eq, assert_ulps_eq};
use glam::{DVec4, dvec3};
use mln_style::constants::{EXTENT, LATITUDE_MAX};
use mln_style::geo::LatLngBounds;
use mln_style::geometry::Point;
use mln_style::tile_id::CanonicalTileID;

use super::*;
use crate::camera::TileProjector;
use crate::tile::UnwrappedTileID;

#[test]
fn the_integer_zoom_is_the_zoom_rounded_down() {
    let mut transform = sized(1, 1);
    for (zoom, integer) in [(0.0, 0), (0.5, 0), (0.99, 0), (1.0, 1), (7.25, 7), (19.99, 19), (20.0, 20)] {
        transform.jump_to(&CameraOptions { zoom: Some(zoom), ..Default::default() });
        assert_eq!(integer, transform.view().integer_zoom(), "{zoom}");
    }
}

#[test]
fn a_location_behind_a_pitched_camera_draws_with_a_negative_w() {
    let mut transform = sized(1000, 1000);
    let camera = |zoom| CameraOptions {
        center: Some(ll(38.0, -77.0)),
        zoom: Some(zoom),
        pitch: Some(51.57),
        ..Default::default()
    };
    let location = ll(37.69, -76.76);
    transform.jump_to(&camera(10.0));
    assert!(transform.view().lat_lng_to_screen_coordinate_with_clip(&location).1.w > 0.0);
    transform.jump_to(&camera(18.0));
    let behind = ll(7.69, -76.76);
    assert!(transform.view().lat_lng_to_screen_coordinate_with_clip(&behind).1.w < 0.0);
}

/// A location on another world copy converts to the screen and back on that
/// copy.
#[test]
fn a_location_keeps_its_world_copy_through_the_screen() {
    let mut transform = sized(1000, 1000);
    transform.jump_to(&CameraOptions {
        center: Some(ll(38.0, -77.0)),
        zoom: Some(10.0),
        pitch: Some(51.57),
        ..Default::default()
    });
    let view = transform.view();
    for longitude in [-77.0, 283.0, -437.0] {
        let point = view.lat_lng_to_screen_coordinate(&ll(38.0, longitude));
        let back = view.screen_coordinate_to_lat_lng(&point, WrapMode::Unwrapped).unwrap();
        assert_abs_diff_eq!(38.0, back.latitude(), epsilon = 1e-8);
        assert_abs_diff_eq!(longitude, back.longitude(), epsilon = 1e-8);
    }
    let middle = view.screen_coordinate_to_lat_lng(&sc(500.0, 500.0), WrapMode::Unwrapped).unwrap();
    assert_abs_diff_eq!(38.0, middle.latitude(), epsilon = 1e-8);
    assert_abs_diff_eq!(-77.0, middle.longitude(), epsilon = 1e-8);
}

/// The copy of a location nearest the center: across the antimeridian from
/// it, and in the center's own world when bounds keep the center past the
/// antimeridian.
#[test]
fn the_copy_of_a_location_near_the_center_is_the_nearest() {
    let mut transform = sized(512, 512);
    transform.jump_to(&CameraOptions { center: Some(ll(0.0, -10.0)), zoom: Some(4.0), ..Default::default() });
    for (longitude, near) in [(179.0, -181.0), (170.0, 170.0)] {
        assert_abs_diff_eq!(
            near,
            transform.view().lat_lng_near_center(&ll(0.0, longitude)).longitude(),
            epsilon = 1e-9
        );
    }

    limit(&mut transform, |limits| limits.bounds = LatLngBounds::hull(ll(-10.0, 170.0), ll(10.0, 190.0))).unwrap();
    transform.jump_to(&CameraOptions { center: Some(ll(0.0, 181.0)), ..Default::default() });
    assert_abs_diff_eq!(181.0, unwrapped_center(&transform).longitude(), epsilon = 1e-9);
    for (longitude, near) in [(179.0, 179.0), (-179.8, 180.2), (-10.0, 350.0)] {
        assert_abs_diff_eq!(
            near,
            transform.view().lat_lng_near_center(&ll(0.0, longitude)).longitude(),
            epsilon = 1e-9
        );
    }
}

/// With the center across the antimeridian from a location, the location
/// draws a world away, and its copy nearest the center draws beside it;
/// both convert back to the location.
#[test]
fn a_location_across_the_antimeridian_draws_a_world_away() {
    let san_francisco = ll(37.7833, -122.4167);
    let mut transform = sized(1000, 1000);
    transform.jump_to(&CameraOptions { center: Some(ll(0.0, 0.0)), zoom: Some(1.0), ..Default::default() });
    let pixel = transform.lat_lng_to_screen_coordinate(&san_francisco);
    assert_relative_eq!(151.792_494_371_764_32, pixel.x, max_relative = 1e-7);
    assert_relative_eq!(383.767_207_825_276_6, pixel.y, max_relative = 1e-7);

    for center in [ll(0.0, -181.0), ll(0.0, 179.0)] {
        transform.jump_to(&CameraOptions { center: Some(center), ..Default::default() });
        let longest = transform.lat_lng_to_screen_coordinate(&san_francisco);
        assert_relative_eq!(-357.363_066_164_128_16, longest.x, max_relative = 1e-7);
        assert_ulps_eq!(pixel.y, longest.y);
        let mut nearest = san_francisco.wrapped();
        nearest.unwrap_for_shortest_path(&super::center(&transform));
        let shortest = transform.lat_lng_to_screen_coordinate(&nearest);
        assert_relative_eq!(666.636_943_852_191_7, shortest.x, max_relative = 1e-7);
        assert_ulps_eq!(pixel.y, shortest.y);
    }

    let waikiri = ll(-16.9310, 179.9787);
    transform.jump_to(&CameraOptions {
        center: Some(ll(waikiri.latitude(), 180.0213)),
        zoom: Some(10.0),
        ..Default::default()
    });
    let longest = transform.lat_lng_to_screen_coordinate(&waikiri);
    assert_relative_eq!(524_725.964_381_082_3, longest.x, max_relative = 1e-7);
    let mut nearest = waikiri.wrapped();
    nearest.unwrap_for_shortest_path(&center(&transform));
    assert_relative_eq!(
        437.959_252_726_483_44,
        transform.lat_lng_to_screen_coordinate(&nearest).x,
        max_relative = 1e-7
    );
    let back = transform.screen_coordinate_to_lat_lng(&longest, WrapMode::Wrapped).unwrap();
    assert_abs_diff_eq!(waikiri.latitude(), back.latitude(), epsilon = 1e-4);
    assert_abs_diff_eq!(waikiri.longitude(), back.longitude(), epsilon = 1e-4);
}

/// A zoom around a point across the antimeridian leaves the center wrapped;
/// bounds across the antimeridian keep it in their range.
#[test]
fn a_zoom_around_a_point_across_the_antimeridian_wraps_the_center() {
    for (bounded, (west, east)) in [(false, (-180.0, -178.0)), (true, (180.0, 190.0))] {
        let mut transform = sized(256, 256);
        if bounded {
            limit(&mut transform, |limits| limits.bounds = LatLngBounds::hull(ll(-10.0, 170.0), ll(10.0, 190.0)))
                .unwrap();
        }
        transform.jump_to(&CameraOptions { center: Some(ll(0.0, 179.9)), zoom: Some(5.0), ..Default::default() });
        transform.jump_to(&CameraOptions { zoom: Some(6.0), anchor: Some(sc(230.0, 128.0)), ..Default::default() });
        let longitude = unwrapped_center(&transform).longitude();
        assert!(west < longitude && longitude < east, "bounded {bounded}: {longitude}");
    }
}

/// A change around an anchor applies the change and holds the location under
/// the anchor; the same change without one keeps the center.
#[test]
fn a_jump_around_an_anchor_holds_the_location_under_it() {
    let anchor = sc(150.0, 150.0);
    for change in [
        CameraOptions { zoom: Some(5.5), ..Default::default() },
        CameraOptions { bearing: Some(45.0), ..Default::default() },
        CameraOptions { pitch: Some(20.0), ..Default::default() },
    ] {
        let start = || {
            let mut transform = sized(1000, 1000);
            transform.jump_to(&CameraOptions {
                center: Some(ll(10.0, -100.0)),
                zoom: Some(10.0),
                ..Default::default()
            });
            transform
        };
        let mut centered = start();
        centered.jump_to(&change);
        assert_abs_diff_eq!(10.0, center(&centered).latitude(), epsilon = 1e-9);
        assert_abs_diff_eq!(-100.0, center(&centered).longitude(), epsilon = 1e-9);

        let mut anchored = start();
        let location = anchored.screen_coordinate_to_lat_lng(&anchor, WrapMode::Unwrapped).unwrap();
        anchored.jump_to(&CameraOptions { anchor: Some(anchor), ..change });
        let point = anchored.lat_lng_to_screen_coordinate(&location);
        assert_abs_diff_eq!(anchor.x, point.x, epsilon = 1e-6);
        assert_abs_diff_eq!(anchor.y, point.y, epsilon = 1e-6);

        for transform in [&centered, &anchored] {
            let options = transform.camera_options(None);
            assert_abs_diff_eq!(change.zoom.unwrap_or(10.0), options.zoom.unwrap(), epsilon = 1e-9);
            assert_abs_diff_eq!(change.bearing.unwrap_or(0.0), options.bearing.unwrap(), epsilon = 1e-9);
            assert_abs_diff_eq!(change.pitch.unwrap_or(0.0), options.pitch.unwrap(), epsilon = 1e-9);
        }
    }
}

/// Padding moves the point that shows the center to the middle of the area
/// it leaves, and keeps the center.
#[test]
fn padding_moves_the_center_to_the_middle_of_the_padded_area() {
    let mut transform = sized(1000, 1000);
    transform.jump_to(&CameraOptions { center: Some(ll(10.0, -100.0)), zoom: Some(10.0), ..Default::default() });
    let at =
        |transform: &Transform, y| transform.screen_coordinate_to_lat_lng(&sc(500.0, y), WrapMode::Wrapped).unwrap();
    let (middle, upper) = (at(&transform, 500.0), at(&transform, 250.0));

    transform.jump_to(&CameraOptions { padding: Some(insets(500.0, 0.0, 0.0, 0.0)), ..Default::default() });
    assert_ulps_eq!(10.0, center(&transform).latitude());
    assert_ulps_eq!(-100.0, center(&transform).longitude());
    for (expected, y) in [(middle, 750.0), (upper, 500.0)] {
        let actual = at(&transform, y);
        assert_abs_diff_eq!(expected.latitude(), actual.latitude(), epsilon = 1e-10);
        assert_abs_diff_eq!(expected.longitude(), actual.longitude(), epsilon = 1e-10);
    }
}

/// Padding that would animate through NaN, from infinite padding, keeps the
/// insets it has.
#[test]
fn infinite_padding_stays_where_animating_it_would_reach_nan() {
    let mut transform = sized(1000, 1000);
    let infinite = insets(f64::INFINITY, 0.0, 0.0, 0.0);
    transform.jump_to(&CameraOptions { padding: Some(infinite), ..Default::default() });
    assert_eq!(infinite, transform.state().view().padding());
    transform.jump_to(&CameraOptions { padding: Some(EdgeInsets::default()), ..Default::default() });
    assert_eq!(infinite, transform.state().view().padding());
    transform.fly_to(
        &CameraOptions { padding: Some(EdgeInsets::default()), ..Default::default() },
        &AnimationOptions::default(),
        false,
    );
    assert_eq!(infinite, transform.state().view().padding());
}

/// A move by an offset brings the location that offset from the middle to
/// it, step after step.
#[test]
fn a_move_brings_the_location_at_its_offset_to_the_middle() {
    let mut transform = sized(1000, 1000);
    transform.jump_to(&CameraOptions { center: Some(ll(0.0, 0.0)), zoom: Some(10.0), ..Default::default() });
    for step in 0u8..20 {
        let x = if step % 2 != 0 { 100.0 } else { -100.0 };
        let y = if step % 10 != 0 { 100.0 } else { -100.0 };
        let location = transform.screen_coordinate_to_lat_lng(&sc(500.0 - x, 500.0 - y), WrapMode::Wrapped).unwrap();
        transform.move_by(&sc(x, y), &AnimationOptions::default()).unwrap();
        assert_abs_diff_eq!(location.latitude(), center(&transform).latitude(), epsilon = 1e-8);
        assert_abs_diff_eq!(location.longitude(), center(&transform).longitude(), epsilon = 1e-8);
    }
}

/// On a pitched map an offset toward the horizon moves the center on, at
/// least as far as a shorter one, however large, but stops short of the
/// horizon instead of reversing.
#[test]
fn a_large_move_toward_the_horizon_stops_short_of_it() {
    let mut transform = sized(1000, 1000);
    let start = CameraOptions { center: Some(ll(0.0, 0.0)), zoom: Some(10.0), pitch: Some(60.0), ..Default::default() };
    let moved = |transform: &mut Transform, offset: f64| {
        transform.jump_to(&start);
        transform.move_by(&sc(0.0, offset), &AnimationOptions::default()).unwrap();
        center(transform).latitude()
    };
    let moderate = moved(&mut transform, 300.0);
    let fling = moved(&mut transform, 100_000.0);
    assert!(moderate > 0.0, "{moderate}");
    assert!(fling >= moderate, "{fling} {moderate}");
    assert!(fling < LATITUDE_MAX, "{fling}");
}

/// A point without a location is an error, and a move to it changes
/// nothing; an anchor without one is ignored.
#[test]
fn a_nan_point_has_no_location() {
    let mut transform = sized(1000, 1000);
    let nan = sc(f64::NAN, 500.0);
    let error = transform.screen_coordinate_to_lat_lng(&nan, WrapMode::Wrapped).unwrap_err();
    assert_eq!(error.kind, mln_style::error::ErrorKind::InvalidArgument);
    assert!(error.message.ends_with("NaN, NaN"), "{error}");
    let before = center(&transform);
    assert!(transform.move_by(&nan, &AnimationOptions::default()).is_err());
    assert_eq!(before, center(&transform));
    transform.jump_to(&CameraOptions { anchor: Some(nan), zoom: Some(2.0), ..Default::default() });
    assert_eq!(2.0, transform.view().zoom());
    assert_eq!(before, center(&transform));
}

/// On Mercator a move across the antimeridian during a gesture keeps the
/// center on the world copy it moved to, and the camera reports that copy,
/// which draws in the middle of the view.
#[test]
fn a_gestures_camera_reports_the_world_copy_it_moved_to() {
    let mut transform = sized(800, 600);
    transform.jump_to(&CameraOptions { center: Some(ll(0.0, 179.0)), zoom: Some(5.0), ..Default::default() });
    let degree = 512.0 * 32.0 / 360.0;
    transform.set_gesture_in_progress(true);
    let start = start_clock(&mut transform);
    transform.move_by(&sc(-2.0 * degree, 0.0), &AnimationOptions::new(Duration::from_secs(10))).unwrap();
    transform.update_transitions(Some(start));
    assert!(unwrapped_center(&transform).longitude() < -180.0);
    let reported = transform.camera_options(None).center.unwrap();
    assert_abs_diff_eq!(unwrapped_center(&transform).longitude(), reported.longitude(), epsilon = 1e-9);
    let point = transform.lat_lng_to_screen_coordinate(&reported);
    assert_abs_diff_eq!(400.0, point.x, epsilon = 1e-6);
    assert_abs_diff_eq!(300.0, point.y, epsilon = 1e-6);
}

/// A tile's matrix takes its corners to their world pixels, its world copy
/// included, and the projection data draws it through the projection
/// matrix, with the tile's place in the world square.
#[test]
fn a_tiles_matrix_places_it_in_the_world() {
    let mut transform = sized(512, 512);
    transform.jump_to(&CameraOptions {
        center: Some(ll(10.0, 20.0)),
        zoom: Some(5.0),
        pitch: Some(20.0),
        bearing: Some(30.0),
        roll: Some(15.0),
        ..Default::default()
    });
    let view = transform.view();
    let world_size = Projection::world_size(view.scale());
    for tile_id in [
        UnwrappedTileID::new(0, 0, 0),
        UnwrappedTileID::new(3, 5, 2),
        UnwrappedTileID::new(14, 16000, 8000),
        UnwrappedTileID::with_wrap(-1, CanonicalTileID::new(2, 1, 1)),
    ] {
        let tiles = f64::from(1u32 << tile_id.canonical.z);
        let x = f64::from(tile_id.canonical.x) + f64::from(tile_id.wrap) * tiles;
        let y = f64::from(tile_id.canonical.y);
        let matrix = view.tile_matrix(&tile_id);
        for (corner, (cx, cy)) in [(0.0, (x, y)), (f64::from(EXTENT), (x + 1.0, y + 1.0))] {
            let world = matrix.transform_point3(dvec3(corner, corner, 0.0));
            assert_relative_eq!(cx / tiles * world_size, world.x, max_relative = 1e-12);
            assert_relative_eq!(cy / tiles * world_size, world.y, max_relative = 1e-12);
        }

        let data = view.projection_data(&tile_id);
        assert_eq!(view.projection_matrix() * matrix, data.main_matrix);
        assert_eq!(data.main_matrix, data.fallback_matrix);
        let aligned = view.projection_matrix_with(1.0, true);
        assert_eq!(aligned * matrix, view.projection_data_with(&tile_id, &aligned).main_matrix);
        assert_eq!(f64::from(tile_id.canonical.x) / tiles, data.tile_mercator_coords[0]);
        assert_eq!(y / tiles, data.tile_mercator_coords[1]);
        assert_eq!(1.0 / tiles / f64::from(EXTENT), data.tile_mercator_coords[2]);
        assert_eq!(1.0 / tiles / f64::from(EXTENT), data.tile_mercator_coords[3]);
        assert_eq!(DVec4::ZERO, data.clipping_plane);
        assert_eq!(0.0, data.projection_transition);
    }
}

/// On Mercator the tile projector draws a point where the tile's projection
/// data does, nothing is occluded, and sizes need no correction.
#[test]
fn the_mercator_tile_projector_draws_through_the_tile_matrix() {
    let mut transform = sized(512, 512);
    transform.jump_to(&CameraOptions {
        center: Some(ll(0.0, 0.0)),
        zoom: Some(1.0),
        bearing: Some(30.0),
        pitch: Some(20.0),
        ..Default::default()
    });
    let view = transform.view();
    assert!(!view.is_globe());

    // The bottom-right corner of tile 1/0/0 is lat 0, lng 0: the map center.
    let tile_id = UnwrappedTileID::new(1, 0, 0);
    let projector = TileProjector::new(view, &tile_id);
    let corner = Point::new(f64::from(EXTENT), f64::from(EXTENT));
    let pos = view.projection_data(&tile_id).main_matrix * DVec4::new(corner.x, corner.y, 0.0, 1.0);
    let projected = projector.project(&corner, 0.0);
    assert_abs_diff_eq!(pos.x / pos.w, projected.point.x, epsilon = 1e-9);
    assert_abs_diff_eq!(pos.y / pos.w, projected.point.y, epsilon = 1e-9);
    assert_abs_diff_eq!(0.0, projected.point.x, epsilon = 1e-6);
    assert_abs_diff_eq!(0.0, projected.point.y, epsilon = 1e-6);
    assert_ulps_eq!(pos.w, projected.signed_distance_from_camera);
    assert!(!projected.occluded);
    assert!(!projector.project(&Point::new(0.0, 0.0), 0.0).occluded);
    assert_ulps_eq!(1.0, projector.map_aligned_scale());
    assert_ulps_eq!(1.0, projector.map_aligned_scale_at(&Point::new(100.0, 100.0)));
    assert_ulps_eq!(1.0, view.projection().pixel_scale(view));
}

/// The answers of the Mercator transform on three cameras, each labeled by
/// its camera and what it answers, for [`mercator_keeps_its_answers`].
fn mercator_answers() -> Vec<(String, f64)> {
    let cameras = [
        ("San Francisco", Size::new(512, 512), ll(37.7749, -122.4194), 12.5, 30.0, 45.0, EdgeInsets::default()),
        ("Sydney", Size::new(800, 600), ll(-33.8688, 151.2093), 3.2, -110.0, 0.0, insets(10.0, 20.0, 30.0, 40.0)),
        ("Arctic", Size::new(1024, 768), ll(79.5, 170.25), 1.7, 10.0, 60.0, EdgeInsets::default()),
    ];
    let mut answers = Vec::new();
    for (name, size, center_ll, zoom, bearing, pitch, padding) in cameras {
        let mut add = |label: &str, value: f64| answers.push((format!("{name} {label}"), value));
        let mut transform = new_transform();
        transform.resize(size).unwrap();
        let options = CameraOptions {
            center: Some(center_ll),
            zoom: Some(zoom),
            bearing: Some(bearing),
            pitch: Some(pitch),
            padding: Some(padding),
            ..CameraOptions::default()
        };
        transform.jump_to(&options);
        let state = transform.state().clone();
        let view = state.view();
        add("center latitude", view.center(WrapMode::Unwrapped).latitude());
        add("center longitude", view.center(WrapMode::Unwrapped).longitude());
        let position = view.free_camera_options().position.unwrap();
        add("camera x", position.x);
        add("camera y", position.y);
        add("camera z", position.z);
        let screen =
            view.lat_lng_to_screen_coordinate(&ll(center_ll.latitude() + 0.001, center_ll.longitude() - 0.002));
        add("screen x", screen.x);
        add("screen y", screen.y);
        let at_screen = view.screen_coordinate_to_lat_lng(&sc(50.0, 60.0), WrapMode::Unwrapped).unwrap();
        add("at screen latitude", at_screen.latitude());
        add("at screen longitude", at_screen.longitude());
        add("max pitch scale factor", view.max_pitch_scale_factor());

        let mut moved = state.clone();
        moved.move_lat_lng(&ll(center_ll.latitude() + 0.01, center_ll.longitude() + 0.02), &sc(100.0, 150.0)).unwrap();
        add("moved latitude", moved.view().center(WrapMode::Unwrapped).latitude());
        add("moved longitude", moved.view().center(WrapMode::Unwrapped).longitude());

        let mut from_camera = state.clone();
        from_camera.set_free_camera_options(&view.free_camera_options());
        add("from camera latitude", from_camera.view().center(WrapMode::Unwrapped).latitude());
        add("from camera longitude", from_camera.view().center(WrapMode::Unwrapped).longitude());
        add("from camera zoom", from_camera.view().zoom());

        let target = CameraOptions {
            center: Some(ll(center_ll.latitude() + 2.0, center_ll.longitude() - 3.0)),
            zoom: Some(zoom + 1.5),
            bearing: Some(bearing + 20.0),
            pitch: Some(10.0),
            ..CameraOptions::default()
        };
        let second = AnimationOptions::new(Duration::from_secs(1));
        let start = start_clock(&mut transform);
        transform.ease_to(&target, &second);
        transform.update_transitions(Some(start + Duration::from_millis(500)));
        add("eased latitude", transform.view().center(WrapMode::Unwrapped).latitude());
        add("eased longitude", transform.view().center(WrapMode::Unwrapped).longitude());
        add("eased zoom", transform.view().zoom());
        transform.update_transitions(Some(start + Duration::from_secs(1)));

        transform.jump_to(&options);
        let start = start + Duration::from_secs(10);
        transform.frame_clock().hold(Some(start));
        transform.fly_to(&target, &second, false);
        transform.update_transitions(Some(start + Duration::from_millis(500)));
        add("flown latitude", transform.view().center(WrapMode::Unwrapped).latitude());
        add("flown longitude", transform.view().center(WrapMode::Unwrapped).longitude());
        add("flown zoom", transform.view().zoom());
        transform.update_transitions(Some(start + Duration::from_secs(1)));
    }
    answers
}

/// The Mercator transform's answers on three cameras: its center, free
/// camera, conversions, moves, and an ease's and a flight's middle. The
/// tolerances take in libm and fused multiply-add differences between
/// platforms, not a change to the math: an answer that goes through the
/// screen passes through the projection matrix or its inverse, which
/// magnifies them 1e3 to 1e4 times.
#[test]
fn mercator_keeps_its_answers() {
    const MERCATOR: f64 = 1e-12;
    const THROUGH_SCREEN: f64 = 1e-10;
    // The max pitch scale factor is up to 1.5e-8 off on these cameras.
    const MAX_PITCH_SCALE_FACTOR: f64 = 2e-8;
    let expected = [
        ("San Francisco center latitude", 37.7749, MERCATOR),
        ("San Francisco center longitude", -122.4194, MERCATOR),
        ("San Francisco camera x", 0.159_854_558_376_736_15, MERCATOR),
        ("San Francisco camera y", 0.386_679_510_709_237_56, MERCATOR),
        ("San Francisco camera z", 0.000_183_105_468_75, MERCATOR),
        ("San Francisco screen x", 236.533_398_455_225_52, THROUGH_SCREEN),
        ("San Francisco screen y", 256.556_825_795_493_86, THROUGH_SCREEN),
        ("San Francisco at screen latitude", 37.764_423_616_805_36, THROUGH_SCREEN),
        ("San Francisco at screen longitude", -122.450_054_415_613_64, THROUGH_SCREEN),
        ("San Francisco max pitch scale factor", 1.5, MAX_PITCH_SCALE_FACTOR),
        ("San Francisco moved latitude", 37.789_268_353_564_324, THROUGH_SCREEN),
        ("San Francisco moved longitude", -122.376_995_417_827_84, THROUGH_SCREEN),
        ("San Francisco from camera latitude", 37.7749, MERCATOR),
        ("San Francisco from camera longitude", -122.4194, MERCATOR),
        ("San Francisco from camera zoom", 12.5, MERCATOR),
        ("San Francisco eased latitude", 39.700_872_193_482_97, MERCATOR),
        ("San Francisco eased longitude", -125.306_835_595_921_81, MERCATOR),
        ("San Francisco eased zoom", 13.734_648_613_495_04, MERCATOR),
        ("San Francisco flown latitude", 39.774_242_507_916_44, MERCATOR),
        ("San Francisco flown longitude", -125.418_399_705_294_96, MERCATOR),
        ("San Francisco flown zoom", 11.258_193_199_328_915, MERCATOR),
        ("Sydney center latitude", -33.868_800_000_000_02, MERCATOR),
        ("Sydney center longitude", 151.209_299_999_999_98, MERCATOR),
        ("Sydney camera x", 0.920_025_833_333_333_2, MERCATOR),
        ("Sydney camera y", 0.600_092_251_458_776_2, MERCATOR),
        ("Sydney camera z", 0.191_283_082_755_496_02, MERCATOR),
        ("Sydney screen x", 390.023_731_355_705_93, THROUGH_SCREEN),
        ("Sydney screen y", 310.019_179_322_483_1, THROUGH_SCREEN),
        ("Sydney at screen latitude", -47.379_211_768_724_836, THROUGH_SCREEN),
        ("Sydney at screen longitude", 178.081_512_635_435_4, THROUGH_SCREEN),
        ("Sydney max pitch scale factor", 1.0, MAX_PITCH_SCALE_FACTOR),
        ("Sydney moved latitude", -18.999_444_839_858_203, THROUGH_SCREEN),
        ("Sydney moved longitude", 132.136_443_484_529_14, THROUGH_SCREEN),
        ("Sydney from camera latitude", -33.868_800_000_000_02, MERCATOR),
        ("Sydney from camera longitude", 151.209_299_999_999_98, MERCATOR),
        ("Sydney from camera zoom", 3.2, MERCATOR),
        ("Sydney eased latitude", -31.944_653_580_490_49, MERCATOR),
        ("Sydney eased longitude", 148.321_864_404_078_1, MERCATOR),
        ("Sydney eased zoom", 4.434_648_613_495_041, MERCATOR),
        ("Sydney flown latitude", -31.996_632_184_023_433, MERCATOR),
        ("Sydney flown longitude", 148.399_052_722_951_35, MERCATOR),
        ("Sydney flown zoom", 4.431_189_851_753_509, MERCATOR),
        ("Arctic center latitude", 69.115_018_594_564_19, MERCATOR),
        ("Arctic center longitude", 170.25, MERCATOR),
        ("Arctic camera x", 0.868_773_114_374_953_6, MERCATOR),
        ("Arctic camera y", 0.821_467_012_244_570_4, MERCATOR),
        ("Arctic camera z", 0.346_259_366_253_257_8, MERCATOR),
        ("Arctic screen x", 483.831_518_150_223_64, THROUGH_SCREEN),
        ("Arctic screen y", 463.852_284_095_300_9, THROUGH_SCREEN),
        ("Arctic at screen latitude", 15.513_337_760_683_484, THROUGH_SCREEN),
        ("Arctic at screen longitude", 87.665_272_005_597_84, THROUGH_SCREEN),
        ("Arctic max pitch scale factor", 1.169_744_133_949_279_8, MAX_PITCH_SCALE_FACTOR),
        ("Arctic moved latitude", 69.115_018_594_564_19, THROUGH_SCREEN),
        ("Arctic moved longitude", -111.765_303_625_466_56, THROUGH_SCREEN),
        ("Arctic from camera latitude", 69.115_018_594_564_19, MERCATOR),
        ("Arctic from camera longitude", 170.25, MERCATOR),
        ("Arctic from camera zoom", 1.7, MERCATOR),
        ("Arctic eased latitude", 80.847_775_144_839_19, MERCATOR),
        ("Arctic eased longitude", 167.362_564_404_078_1, MERCATOR),
        ("Arctic eased zoom", 2.934_648_613_495_041, MERCATOR),
        ("Arctic flown latitude", 80.691_007_366_241_85, MERCATOR),
        ("Arctic flown longitude", 167.431_466_191_662_6, MERCATOR),
        ("Arctic flown zoom", 2.895_264_021_176_888, MERCATOR),
    ];
    let answers = mercator_answers();
    assert_eq!(expected.len(), answers.len());
    let mut off = Vec::new();
    for ((label, value, relative), (answer_label, answer)) in expected.into_iter().zip(answers) {
        assert_eq!(label, answer_label);
        if (value - answer).abs() > relative * value.abs() {
            off.push(format!("{label}: {answer} is not {value}"));
        }
    }
    assert!(off.is_empty(), "{off:#?}");
}