mln-rs-engine 0.0.18

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. 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
//! Animated camera commands: easing and flying to a camera.

use std::f64::consts::PI;
use std::time::Duration;

use mln_style::geo::{LatLngBounds, WrapMode};
use mln_style::interpolate::interpolate;

use super::transition::{AlongFn, FrameFn};
use super::{
    CameraCommandId, Transform, frame_edge_insets, frame_unproject, lat_lng, nanos_from_secs, normalize_angle,
};
use crate::camera::{
    AnimationOptions, CameraOptions, TransformState, TransitionOutcome, VerticalPerspectiveProjection,
};
use crate::geo::EdgeInsets;
use crate::geo::projection::Projection;

/// An animation frame's padding, `t` of the way from `start` to `end`; `None`
/// when it has none (see [`frame_edge_insets`]). Insets that don't change are
/// kept as they are, so that infinite insets don't log on every frame.
fn interpolate_edge_insets(start: &EdgeInsets, end: &EdgeInsets, t: f64) -> Option<EdgeInsets> {
    if start == end {
        return Some(*end);
    }
    frame_edge_insets(
        interpolate(&start.top, &end.top, t),
        interpolate(&start.left, &end.left, t),
        interpolate(&start.bottom, &end.bottom, t),
        interpolate(&start.right, &end.right, t),
    )
}

impl Transform {
    /// Change any combination of center, zoom, bearing, pitch and edgeInsets,
    /// with a smooth animation between old and new values. Omitted options
    /// keep their current animations and timing.
    pub fn ease_to(&mut self, camera: &CameraOptions, animation: &AnimationOptions) -> CameraCommandId {
        self.command(|transform| transform.ease(camera, animation))
    }

    pub(super) fn ease(&mut self, input_camera: &CameraOptions, animation: &AnimationOptions) {
        let mut camera = *input_camera;
        let globe = self.state.view().is_globe();

        let duration = animation.duration.unwrap_or(Duration::ZERO);
        if !globe
            && self.state.limits().bounds == LatLngBounds::default()
            && !self.is_gesture_in_progress()
            && duration != Duration::ZERO
            && animation.easing.is_none()
        {
            // A plain timed ease flies without the zoom-out. The flight's
            // ground speed rises and falls along its path even at a constant
            // zoom, so an ease given an easing interpolates instead, and
            // moves as that easing says, as GL JS's easeTo does.
            self.fly_to(&camera, animation, true);
            return;
        }

        let mut zoom = camera.zoom.unwrap_or(self.view().zoom());
        if camera.zoom.is_none()
            && let Some(center) = camera.center
            && globe
        {
            zoom += VerticalPerspectiveProjection::zoom_adjustment(
                self.view().center(WrapMode::Wrapped).latitude(),
                center.latitude(),
            );
        }
        self.state.constrain_camera_and_zoom_to_bounds(&mut camera, &mut zoom);

        let padding = camera.padding.unwrap_or(self.state.view().padding());
        let mut start_lat_lng = self.view().center(WrapMode::Unwrapped);
        let unwrapped_lat_lng = camera.center.unwrap_or(start_lat_lng);
        // On the globe the ease heads for the center the zoom it was given
        // allows, or else the zoom it starts from, as GL JS's does; its last
        // frame is bounded by the zoom it ends on.
        let lat_lng_end = self.state.constrained_center(
            &if self.state.limits().bounds == LatLngBounds::default() {
                unwrapped_lat_lng.wrapped()
            } else {
                unwrapped_lat_lng
            },
            if camera.zoom.is_some() { zoom } else { self.view().zoom() },
        );

        let mut bearing = match camera.bearing {
            Some(b) => mln_style::math::deg2rad(-b),
            None => self.view().angle(),
        };
        let mut pitch = match camera.pitch {
            Some(p) => mln_style::math::deg2rad(p),
            None => self.view().pitch(),
        };
        let mut fov = match camera.fov {
            Some(f) => mln_style::math::deg2rad(f),
            None => self.view().fov(),
        };
        let center_alt = camera.center_altitude.unwrap_or(self.state.view().center_altitude());
        let roll = match camera.roll {
            Some(r) => mln_style::math::deg2rad(r),
            None => self.view().roll(),
        };

        if zoom.is_nan() || bearing.is_nan() || pitch.is_nan() || roll.is_nan() || fov.is_nan() {
            self.end_unstarted(animation, TransitionOutcome::Cancelled);
            return;
        }

        if self.state.limits().bounds == LatLngBounds::default() {
            if self.is_gesture_in_progress() {
                // If gesture in progress, we transfer the wrap rounds from the
                // end longitude into start, so the "scroll effect" of rounding
                // the world is the same while assuring the end longitude
                // remains wrapped.
                let wrap = unwrapped_lat_lng.longitude() - lat_lng_end.longitude();
                start_lat_lng = lat_lng(start_lat_lng.latitude(), start_lat_lng.longitude() - wrap);
            } else {
                // Find the shortest path otherwise.
                start_lat_lng.unwrap_for_shortest_path(&lat_lng_end);
            }
        }
        let start_center_alt = self.state.view().center_altitude();

        let start_scale = self.state.view().scale();
        let start_point = Projection::project(&start_lat_lng, start_scale);
        let end_point = Projection::project(&lat_lng_end, start_scale);

        // Constrain camera options.
        // The target is drawn in the projection the style gives at its zoom,
        // which the animation may cross into.
        let target_globe = match &self.projection_for_zoom {
            Some(projection_for_zoom) => projection_for_zoom(zoom).transition_state() > 0.0,
            None => globe,
        };
        zoom = mln_style::math::clamp_f64(
            zoom,
            self.state.min_zoom_at_latitude_with(lat_lng_end.latitude(), target_globe),
            self.state.limits().max_zoom,
        );
        pitch = mln_style::math::clamp_f64(pitch, self.state.limits().min_pitch, self.state.limits().max_pitch);
        fov = mln_style::math::clamp_f64(fov, crate::camera::MIN_FIELD_OF_VIEW, crate::camera::MAX_FIELD_OF_VIEW);

        // Minimize rotation by taking the shorter path around the circle.
        bearing = normalize_angle(bearing, self.state.view().angle());

        let start_zoom = self.state.view().zoom();
        let start_bearing = normalize_angle(self.state.view().angle(), bearing);
        let start_pitch = self.state.view().pitch();
        let start_roll = self.state.view().roll();
        let start_fov = self.state.view().fov();
        let start_edge_insets = self.state.view().padding();

        // On the globe the zoom is eased in equator terms so the planet holds
        // its size as the latitude changes, and the center moves the way GL
        // JS moves it: longitude paced at 1 / cos(latitude), sped up ahead of
        // a zoom in.
        let normalized_start_zoom = if globe {
            start_zoom + VerticalPerspectiveProjection::zoom_adjustment(start_lat_lng.latitude(), 0.0)
        } else {
            start_zoom
        };
        let normalized_zoom = if globe {
            zoom + VerticalPerspectiveProjection::zoom_adjustment(lat_lng_end.latitude(), 0.0)
        } else {
            zoom
        };
        let final_scale = crate::camera::zoom_scale(normalized_zoom - normalized_start_zoom);
        let base = if normalized_zoom > normalized_start_zoom {
            f64::min(2.0, final_scale)
        } else {
            f64::max(0.5, final_scale)
        };
        let delta_latitude = if globe {
            VerticalPerspectiveProjection::difference_of_angles_degrees(
                start_lat_lng.latitude(),
                lat_lng_end.latitude(),
            )
        } else {
            0.0
        };
        let delta_longitude = if globe {
            VerticalPerspectiveProjection::difference_of_angles_degrees(
                start_lat_lng.longitude(),
                lat_lng_end.longitude(),
            )
        } else {
            0.0
        };
        let moves_around_anchor = camera.anchor.is_some() && camera.center.is_none();

        let frame: FrameFn = Box::new(move |state: &TransformState, t: f64| {
            let (frame_lat_lng, frame_zoom) = if globe {
                // An anchor moves the center a frame at a time (see
                // apply_transitions), so it stays, and the zoom follows its
                // latitude to keep the planet's size.
                let mut frame_lat_lng =
                    if moves_around_anchor { state.view().center(WrapMode::Unwrapped) } else { lat_lng_end };
                let mut frame_zoom = zoom;
                if moves_around_anchor {
                    frame_zoom = interpolate(&normalized_start_zoom, &normalized_zoom, t)
                        + VerticalPerspectiveProjection::zoom_adjustment(0.0, frame_lat_lng.latitude());
                } else if t < 1.0 {
                    let factor = t * base.powf(1.0 - t);
                    frame_lat_lng = VerticalPerspectiveProjection::interpolate_lat_lng(
                        &start_lat_lng,
                        delta_latitude,
                        delta_longitude,
                        factor,
                    )
                    .wrapped();
                    frame_zoom = if zoom == start_zoom {
                        start_zoom
                    } else {
                        interpolate(&normalized_start_zoom, &normalized_zoom, t)
                            + VerticalPerspectiveProjection::zoom_adjustment(0.0, frame_lat_lng.latitude())
                    };
                }
                (Some(frame_lat_lng), frame_zoom)
            } else if t == 1.0 {
                (Some(lat_lng_end), zoom)
            } else {
                let frame_point = interpolate(&start_point, &end_point, t);
                (frame_unproject(&frame_point, start_scale, WrapMode::Unwrapped), interpolate(&start_zoom, &zoom, t))
            };
            CameraOptions {
                center: frame_lat_lng,
                zoom: Some(frame_zoom),
                center_altitude: Some(interpolate(&start_center_alt, &center_alt, t)),
                bearing: Some(-mln_style::math::rad2deg(mln_style::math::wrap(
                    interpolate(&start_bearing, &bearing, t),
                    -PI,
                    PI,
                ))),
                padding: interpolate_edge_insets(&start_edge_insets, &padding, t),
                pitch: Some(mln_style::math::rad2deg(interpolate(&start_pitch, &pitch, t))),
                roll: Some(mln_style::math::rad2deg(interpolate(&start_roll, &roll, t))),
                fov: Some(mln_style::math::rad2deg(interpolate(&start_fov, &fov, t))),
                ..Default::default()
            }
        });

        // On the globe the zoom keeps the planet's size as the center's
        // latitude changes, and a zoom moves the center when it bounds it
        // closer to the equator, so an ease that moves the center sets both
        // (an anchored one gets its center from the anchor).
        let moves_center = input_camera.center.is_some() || lat_lng_end.wrapped() != start_lat_lng.wrapped();
        let fields = if globe && moves_around_anchor {
            CameraOptions { zoom: Some(zoom), ..*input_camera }
        } else if globe && moves_center {
            CameraOptions { center: Some(lat_lng_end), zoom: Some(zoom), ..*input_camera }
        } else {
            *input_camera
        };
        self.start_transition(&fields, animation, frame, None, duration, false);
    }

    /// This method implements an “optimal path” animation, as detailed in:
    ///
    /// Van Wijk, Jarke J.; Nuij, Wim A. A. “Smooth and efficient zooming and
    /// panning.” INFOVIS ’03. pp. 15–22.
    /// <https://www.win.tue.nl/~vanwijk/zoompan.pdf#page=5>.
    ///
    /// Where applicable, local variable documentation begins with the
    /// associated variable or function in van Wijk (2003).
    pub(crate) fn fly_to(
        &mut self,
        camera: &CameraOptions,
        animation: &AnimationOptions,
        linear_zoom_interpolation: bool,
    ) -> CameraCommandId {
        self.command(|transform| transform.fly(camera, animation, linear_zoom_interpolation))
    }

    pub(super) fn fly(
        &mut self,
        input_camera: &CameraOptions,
        animation: &AnimationOptions,
        linear_zoom_interpolation: bool,
    ) {
        let mut camera = *input_camera;
        let globe = self.state.view().is_globe();

        let mut zoom = camera.zoom.unwrap_or(self.view().zoom());
        if camera.zoom.is_none()
            && let Some(center) = camera.center
            && globe
        {
            zoom += VerticalPerspectiveProjection::zoom_adjustment(
                self.view().center(WrapMode::Wrapped).latitude(),
                center.latitude(),
            );
        }
        self.state.constrain_camera_and_zoom_to_bounds(&mut camera, &mut zoom);

        let padding = camera.padding.unwrap_or(self.state.view().padding());
        // As in easeTo, the globe's flight heads for the center the zoom it
        // was given, or else starts from, allows.
        let lat_lng_end = self.state.constrained_center(
            &camera.center.unwrap_or(self.view().center(WrapMode::Unwrapped)).wrapped(),
            if camera.zoom.is_some() { zoom } else { self.view().zoom() },
        );
        let center_alt = camera.center_altitude.unwrap_or(self.state.view().center_altitude());
        let mut bearing = match camera.bearing {
            Some(b) => mln_style::math::deg2rad(-b),
            None => self.view().angle(),
        };
        let mut pitch = match camera.pitch {
            Some(p) => mln_style::math::deg2rad(p),
            None => self.view().pitch(),
        };
        let roll = match camera.roll {
            Some(r) => mln_style::math::deg2rad(r),
            None => self.view().roll(),
        };
        let mut fov = match camera.fov {
            Some(f) => mln_style::math::deg2rad(f),
            None => self.view().fov(),
        };

        if zoom.is_nan()
            || bearing.is_nan()
            || pitch.is_nan()
            || roll.is_nan()
            || fov.is_nan()
            || self.state.view().size().is_empty()
        {
            self.end_unstarted(animation, TransitionOutcome::Cancelled);
            return;
        }

        // Determine endpoints.
        let mut start_lat_lng = self.view().center(WrapMode::Unwrapped).wrapped();
        start_lat_lng.unwrap_for_shortest_path(&lat_lng_end);
        let start_center_alt = self.state.view().center_altitude();

        let start_point = Projection::project(&start_lat_lng, self.state.view().scale());
        let end_point = Projection::project(&lat_lng_end, self.state.view().scale());

        // Constrain camera options.
        // The target is drawn in the projection the style gives at its zoom,
        // which the animation may cross into.
        let target_globe = match &self.projection_for_zoom {
            Some(projection_for_zoom) => projection_for_zoom(zoom).transition_state() > 0.0,
            None => globe,
        };
        zoom = mln_style::math::clamp_f64(
            zoom,
            self.state.min_zoom_at_latitude_with(lat_lng_end.latitude(), target_globe),
            self.state.limits().max_zoom,
        );
        pitch = mln_style::math::clamp_f64(pitch, self.state.limits().min_pitch, self.state.limits().max_pitch);
        fov = mln_style::math::clamp_f64(fov, crate::camera::MIN_FIELD_OF_VIEW, crate::camera::MAX_FIELD_OF_VIEW);

        // Minimize rotation by taking the shorter path around the circle.
        bearing = normalize_angle(bearing, self.state.view().angle());
        let start_zoom = crate::camera::scale_zoom(self.state.view().scale());
        let start_bearing = normalize_angle(self.state.view().angle(), bearing);
        let start_pitch = self.state.view().pitch();
        let start_roll = self.state.view().roll();
        let start_fov = self.state.view().fov();

        // w₀: Initial visible span, measured in pixels at the initial scale.
        // Known henceforth as a <i>screenful</i>.
        let size = self.state.view().size();
        let w0 = f64::max(
            f64::from(size.width) - padding.left - padding.right,
            f64::from(size.height) - padding.top - padding.bottom,
        );
        // The globe counts zoom in equator terms and distance along the
        // surface, so the arc is the same at any latitude.
        let normalized_start_zoom = if globe {
            start_zoom + VerticalPerspectiveProjection::zoom_adjustment(start_lat_lng.latitude(), 0.0)
        } else {
            start_zoom
        };
        let normalized_zoom = if globe {
            zoom + VerticalPerspectiveProjection::zoom_adjustment(lat_lng_end.latitude(), 0.0)
        } else {
            zoom
        };
        // w₁: Final visible span, measured in pixels with respect to the
        // initial scale.
        let w1 = w0 / crate::camera::zoom_scale(normalized_zoom - normalized_start_zoom);
        // Length of the flight path as projected onto the ground plane,
        // measured in pixels from the world image origin at the initial scale.
        let u1 = if globe {
            VerticalPerspectiveProjection::surface_distance_pixels(
                Projection::world_size(self.state.view().scale()),
                start_lat_lng.latitude(),
                &start_lat_lng,
                &lat_lng_end,
            )
        } else {
            (end_point.x - start_point.x).hypot(end_point.y - start_point.y)
        };

        // ρ: The relative amount of zooming that takes place along the flight
        // path. A high value maximizes zooming for an exaggerated animation,
        // while a low value minimizes zooming for something closer to
        // easeTo().
        //
        // 1.42 is the average value selected by participants in the user
        // study in van Wijk (2003). A value of 6<sup>¼</sup> would be
        // equivalent to the root mean squared average velocity,
        // V<sub>RMS</sub>. A value of 1 produces a circular motion.
        let mut rho = 1.42;
        if globe {
            // GL JS: the arc may not dip below the map's or the flight's
            // minimum zoom, and that only ever lowers ρ.
            let floor_zoom = f64::max(animation.min_zoom.unwrap_or(self.state.min_zoom()), self.state.min_zoom());
            let normalized_floor_zoom = f64::min(
                f64::min(
                    floor_zoom + VerticalPerspectiveProjection::zoom_adjustment(lat_lng_end.latitude(), 0.0),
                    normalized_start_zoom,
                ),
                normalized_zoom,
            );
            let min_zoom = mln_style::math::clamp_f64(
                normalized_floor_zoom + VerticalPerspectiveProjection::zoom_adjustment(0.0, lat_lng_end.latitude()),
                self.state.min_zoom_at_latitude(lat_lng_end.latitude()),
                self.state.limits().max_zoom,
            );
            let w_max = w0
                / crate::camera::zoom_scale(
                    min_zoom + VerticalPerspectiveProjection::zoom_adjustment(lat_lng_end.latitude(), 0.0)
                        - normalized_start_zoom,
                );
            if u1 != 0.0 {
                rho = f64::min(rho, (w_max / u1 * 2.0).sqrt());
            }
        } else if animation.min_zoom.is_some() || linear_zoom_interpolation {
            // util::min(a, b, c) = min(a, min(b, c)), fmin on doubles
            let mut min_zoom = mln_style::math::fmin(
                animation.min_zoom.unwrap_or(start_zoom),
                mln_style::math::fmin(start_zoom, zoom),
            );
            min_zoom = mln_style::math::clamp_f64(
                min_zoom,
                self.state.min_zoom_at_latitude(lat_lng_end.latitude()),
                self.state.limits().max_zoom,
            );
            // w<sub>m</sub>: Maximum visible span, measured in pixels with
            // respect to the initial scale.
            let w_max = w0 / crate::camera::zoom_scale(min_zoom - start_zoom);
            rho = if u1 == 0.0 { 1.0 } else { (w_max / u1 * 2.0).sqrt() };
        }
        // ρ²
        let rho2 = rho * rho;

        // rᵢ: Returns the zoom-out factor at one end of the animation.
        //
        // @param i 0 for the ascent or 1 for the descent.
        let r = |i: f64| -> f64 {
            // bᵢ
            let b = (w1 * w1 - w0 * w0 + if i == 0.0 { 1.0 } else { -1.0 } * rho2 * rho2 * u1 * u1)
                / (2.0 * if i == 0.0 { w0 } else { w1 } * rho2 * u1);
            ((b * b + 1.0).sqrt() - b).ln()
        };

        // r₀: Zoom-out factor during ascent.
        let r0 = if u1 == 0.0 { f64::INFINITY } else { r(0.0) }; // Silence division by 0 on sanitize bot.
        let r1 = if u1 == 0.0 { f64::INFINITY } else { r(1.0) };

        // When u₀ = u₁, the optimal path doesn’t require both ascent and
        // descent.
        let is_close = u1.abs() < 0.000_001 || !r0.is_finite() || !r1.is_finite();

        // w(s): Returns the visible span on the ground, measured in pixels
        // with respect to the initial scale.
        //
        // Assumes an angular field of view of 2 arctan ½ ≈ 53°.
        let w = move |s: f64| -> f64 {
            if is_close {
                ((if w1 < w0 { -1.0 } else { 1.0 }) * rho * s).exp()
            } else {
                r0.cosh() / (r0 + rho * s).cosh()
            }
        };
        // u(s): Returns the distance along the flight path as projected onto
        // the ground plane, measured in pixels from the world image origin at
        // the initial scale.
        let u = move |s: f64| -> f64 {
            if is_close { 0.0 } else { w0 * (r0.cosh() * (r0 + rho * s).tanh() - r0.sinh()) / rho2 / u1 }
        };
        // S: Total length of the flight path, measured in ρ-screenfuls.
        let total_s = if is_close { (w1 / w0).ln().abs() / rho } else { (r1 - r0) / rho };
        // Where the center stays, the share of the time stands in.
        let along: Option<AlongFn> = if is_close { None } else { Some(Box::new(move |k| u(k * total_s))) };

        let duration = if let Some(duration) = animation.duration {
            duration
        } else {
            // V: Average velocity, measured in ρ-screenfuls per second.
            let mut velocity = 1.2;
            if let Some(v) = animation.velocity {
                velocity = v / rho;
            }
            // A duration that a `Duration` can't hold jumps, as one past
            // max_duration does, rather than saturating to a flight that
            // never ends.
            nanos_from_secs(total_s / velocity).unwrap_or_default()
        };
        let duration = match animation.max_duration {
            Some(max) if !linear_zoom_interpolation && duration > max => Duration::ZERO,
            _ => duration,
        };
        let start_scale = self.state.view().scale();
        let start_edge_insets = self.state.view().padding();
        let delta_latitude = if globe {
            VerticalPerspectiveProjection::difference_of_angles_degrees(
                start_lat_lng.latitude(),
                lat_lng_end.latitude(),
            )
        } else {
            0.0
        };
        let delta_longitude = if globe {
            VerticalPerspectiveProjection::difference_of_angles_degrees(
                start_lat_lng.longitude(),
                lat_lng_end.longitude(),
            )
        } else {
            0.0
        };

        let frame: FrameFn = Box::new(move |_: &TransformState, k: f64| {
            // s: The distance traveled along the flight path, measured in
            // ρ-screenfuls.
            let s = k * total_s;
            let us = if k == 1.0 { 1.0 } else { u(s) };

            let (frame_lat_lng, frame_zoom) = if globe {
                let mut frame_lat_lng = lat_lng_end;
                let mut frame_zoom = zoom;
                if k < 1.0 {
                    frame_lat_lng = VerticalPerspectiveProjection::interpolate_lat_lng(
                        &start_lat_lng,
                        delta_latitude,
                        delta_longitude,
                        us,
                    )
                    .wrapped();
                    let frame_normalized_zoom = if linear_zoom_interpolation {
                        interpolate(&normalized_start_zoom, &normalized_zoom, k)
                    } else {
                        normalized_start_zoom + crate::camera::scale_zoom(1.0 / w(s))
                    };
                    frame_zoom = frame_normalized_zoom
                        + VerticalPerspectiveProjection::zoom_adjustment(0.0, frame_lat_lng.latitude());
                    if frame_zoom.is_nan() {
                        frame_zoom = zoom;
                    }
                }
                (Some(frame_lat_lng), frame_zoom)
            } else {
                // Calculate the current point and zoom level along the flight
                // path.
                let frame_point = interpolate(&start_point, &end_point, us);
                let mut frame_zoom = if linear_zoom_interpolation {
                    interpolate(&start_zoom, &zoom, k)
                } else {
                    start_zoom + crate::camera::scale_zoom(1.0 / w(s))
                };

                // Zoom can be NaN if size is empty.
                if frame_zoom.is_nan() {
                    frame_zoom = zoom;
                }

                // Convert to geographic coordinates and set the new viewpoint.
                (frame_unproject(&frame_point, start_scale, WrapMode::Unwrapped), frame_zoom)
            };
            CameraOptions {
                center: frame_lat_lng,
                zoom: Some(frame_zoom),
                center_altitude: Some(interpolate(&start_center_alt, &center_alt, us)),
                bearing: Some(-mln_style::math::rad2deg(mln_style::math::wrap(
                    interpolate(&start_bearing, &bearing, k),
                    -PI,
                    PI,
                ))),
                padding: interpolate_edge_insets(&start_edge_insets, &padding, k),
                pitch: Some(mln_style::math::rad2deg(interpolate(&start_pitch, &pitch, k))),
                roll: Some(mln_style::math::rad2deg(interpolate(&start_roll, &roll, k))),
                fov: Some(mln_style::math::rad2deg(interpolate(&start_fov, &fov, k))),
                ..Default::default()
            }
        });

        self.start_transition(input_camera, animation, frame, along, duration, !linear_zoom_interpolation);
    }
}