1use std::f64::consts::PI;
4use std::time::Duration;
5
6use mln_style::geo::{LatLngBounds, WrapMode};
7use mln_style::interpolate::interpolate;
8
9use super::transition::{AlongFn, FrameFn};
10use super::{
11 CameraCommandId, Transform, frame_edge_insets, frame_unproject, lat_lng, nanos_from_secs, normalize_angle,
12};
13use crate::camera::{
14 AnimationOptions, CameraOptions, TransformState, TransitionOutcome, VerticalPerspectiveProjection,
15};
16use crate::geo::EdgeInsets;
17use crate::geo::projection::Projection;
18
19fn interpolate_edge_insets(start: &EdgeInsets, end: &EdgeInsets, t: f64) -> Option<EdgeInsets> {
23 if start == end {
24 return Some(*end);
25 }
26 frame_edge_insets(
27 interpolate(&start.top, &end.top, t),
28 interpolate(&start.left, &end.left, t),
29 interpolate(&start.bottom, &end.bottom, t),
30 interpolate(&start.right, &end.right, t),
31 )
32}
33
34impl Transform {
35 pub fn ease_to(&mut self, camera: &CameraOptions, animation: &AnimationOptions) -> CameraCommandId {
39 self.command(|transform| transform.ease(camera, animation))
40 }
41
42 pub(super) fn ease(&mut self, input_camera: &CameraOptions, animation: &AnimationOptions) {
43 let mut camera = *input_camera;
44 let globe = self.state.view().is_globe();
45
46 let duration = animation.duration.unwrap_or(Duration::ZERO);
47 if !globe
48 && self.state.limits().bounds == LatLngBounds::default()
49 && !self.is_gesture_in_progress()
50 && duration != Duration::ZERO
51 && animation.easing.is_none()
52 {
53 self.fly_to(&camera, animation, true);
58 return;
59 }
60
61 let mut zoom = camera.zoom.unwrap_or(self.view().zoom());
62 if camera.zoom.is_none()
63 && let Some(center) = camera.center
64 && globe
65 {
66 zoom += VerticalPerspectiveProjection::zoom_adjustment(
67 self.view().center(WrapMode::Wrapped).latitude(),
68 center.latitude(),
69 );
70 }
71 self.state.constrain_camera_and_zoom_to_bounds(&mut camera, &mut zoom);
72
73 let padding = camera.padding.unwrap_or(self.state.view().padding());
74 let mut start_lat_lng = self.view().center(WrapMode::Unwrapped);
75 let unwrapped_lat_lng = camera.center.unwrap_or(start_lat_lng);
76 let lat_lng_end = self.state.constrained_center(
80 &if self.state.limits().bounds == LatLngBounds::default() {
81 unwrapped_lat_lng.wrapped()
82 } else {
83 unwrapped_lat_lng
84 },
85 if camera.zoom.is_some() { zoom } else { self.view().zoom() },
86 );
87
88 let mut bearing = match camera.bearing {
89 Some(b) => mln_style::math::deg2rad(-b),
90 None => self.view().angle(),
91 };
92 let mut pitch = match camera.pitch {
93 Some(p) => mln_style::math::deg2rad(p),
94 None => self.view().pitch(),
95 };
96 let mut fov = match camera.fov {
97 Some(f) => mln_style::math::deg2rad(f),
98 None => self.view().fov(),
99 };
100 let center_alt = camera.center_altitude.unwrap_or(self.state.view().center_altitude());
101 let roll = match camera.roll {
102 Some(r) => mln_style::math::deg2rad(r),
103 None => self.view().roll(),
104 };
105
106 if zoom.is_nan() || bearing.is_nan() || pitch.is_nan() || roll.is_nan() || fov.is_nan() {
107 self.end_unstarted(animation, TransitionOutcome::Cancelled);
108 return;
109 }
110
111 if self.state.limits().bounds == LatLngBounds::default() {
112 if self.is_gesture_in_progress() {
113 let wrap = unwrapped_lat_lng.longitude() - lat_lng_end.longitude();
118 start_lat_lng = lat_lng(start_lat_lng.latitude(), start_lat_lng.longitude() - wrap);
119 } else {
120 start_lat_lng.unwrap_for_shortest_path(&lat_lng_end);
122 }
123 }
124 let start_center_alt = self.state.view().center_altitude();
125
126 let start_scale = self.state.view().scale();
127 let start_point = Projection::project(&start_lat_lng, start_scale);
128 let end_point = Projection::project(&lat_lng_end, start_scale);
129
130 let target_globe = match &self.projection_for_zoom {
134 Some(projection_for_zoom) => projection_for_zoom(zoom).transition_state() > 0.0,
135 None => globe,
136 };
137 zoom = mln_style::math::clamp_f64(
138 zoom,
139 self.state.min_zoom_at_latitude_with(lat_lng_end.latitude(), target_globe),
140 self.state.limits().max_zoom,
141 );
142 pitch = mln_style::math::clamp_f64(pitch, self.state.limits().min_pitch, self.state.limits().max_pitch);
143 fov = mln_style::math::clamp_f64(fov, crate::camera::MIN_FIELD_OF_VIEW, crate::camera::MAX_FIELD_OF_VIEW);
144
145 bearing = normalize_angle(bearing, self.state.view().angle());
147
148 let start_zoom = self.state.view().zoom();
149 let start_bearing = normalize_angle(self.state.view().angle(), bearing);
150 let start_pitch = self.state.view().pitch();
151 let start_roll = self.state.view().roll();
152 let start_fov = self.state.view().fov();
153 let start_edge_insets = self.state.view().padding();
154
155 let normalized_start_zoom = if globe {
160 start_zoom + VerticalPerspectiveProjection::zoom_adjustment(start_lat_lng.latitude(), 0.0)
161 } else {
162 start_zoom
163 };
164 let normalized_zoom = if globe {
165 zoom + VerticalPerspectiveProjection::zoom_adjustment(lat_lng_end.latitude(), 0.0)
166 } else {
167 zoom
168 };
169 let final_scale = crate::camera::zoom_scale(normalized_zoom - normalized_start_zoom);
170 let base = if normalized_zoom > normalized_start_zoom {
171 f64::min(2.0, final_scale)
172 } else {
173 f64::max(0.5, final_scale)
174 };
175 let delta_latitude = if globe {
176 VerticalPerspectiveProjection::difference_of_angles_degrees(
177 start_lat_lng.latitude(),
178 lat_lng_end.latitude(),
179 )
180 } else {
181 0.0
182 };
183 let delta_longitude = if globe {
184 VerticalPerspectiveProjection::difference_of_angles_degrees(
185 start_lat_lng.longitude(),
186 lat_lng_end.longitude(),
187 )
188 } else {
189 0.0
190 };
191 let moves_around_anchor = camera.anchor.is_some() && camera.center.is_none();
192
193 let frame: FrameFn = Box::new(move |state: &TransformState, t: f64| {
194 let (frame_lat_lng, frame_zoom) = if globe {
195 let mut frame_lat_lng =
199 if moves_around_anchor { state.view().center(WrapMode::Unwrapped) } else { lat_lng_end };
200 let mut frame_zoom = zoom;
201 if moves_around_anchor {
202 frame_zoom = interpolate(&normalized_start_zoom, &normalized_zoom, t)
203 + VerticalPerspectiveProjection::zoom_adjustment(0.0, frame_lat_lng.latitude());
204 } else if t < 1.0 {
205 let factor = t * base.powf(1.0 - t);
206 frame_lat_lng = VerticalPerspectiveProjection::interpolate_lat_lng(
207 &start_lat_lng,
208 delta_latitude,
209 delta_longitude,
210 factor,
211 )
212 .wrapped();
213 frame_zoom = if zoom == start_zoom {
214 start_zoom
215 } else {
216 interpolate(&normalized_start_zoom, &normalized_zoom, t)
217 + VerticalPerspectiveProjection::zoom_adjustment(0.0, frame_lat_lng.latitude())
218 };
219 }
220 (Some(frame_lat_lng), frame_zoom)
221 } else if t == 1.0 {
222 (Some(lat_lng_end), zoom)
223 } else {
224 let frame_point = interpolate(&start_point, &end_point, t);
225 (frame_unproject(&frame_point, start_scale, WrapMode::Unwrapped), interpolate(&start_zoom, &zoom, t))
226 };
227 CameraOptions {
228 center: frame_lat_lng,
229 zoom: Some(frame_zoom),
230 center_altitude: Some(interpolate(&start_center_alt, ¢er_alt, t)),
231 bearing: Some(-mln_style::math::rad2deg(mln_style::math::wrap(
232 interpolate(&start_bearing, &bearing, t),
233 -PI,
234 PI,
235 ))),
236 padding: interpolate_edge_insets(&start_edge_insets, &padding, t),
237 pitch: Some(mln_style::math::rad2deg(interpolate(&start_pitch, &pitch, t))),
238 roll: Some(mln_style::math::rad2deg(interpolate(&start_roll, &roll, t))),
239 fov: Some(mln_style::math::rad2deg(interpolate(&start_fov, &fov, t))),
240 ..Default::default()
241 }
242 });
243
244 let moves_center = input_camera.center.is_some() || lat_lng_end.wrapped() != start_lat_lng.wrapped();
249 let fields = if globe && moves_around_anchor {
250 CameraOptions { zoom: Some(zoom), ..*input_camera }
251 } else if globe && moves_center {
252 CameraOptions { center: Some(lat_lng_end), zoom: Some(zoom), ..*input_camera }
253 } else {
254 *input_camera
255 };
256 self.start_transition(&fields, animation, frame, None, duration, false);
257 }
258
259 pub(crate) fn fly_to(
268 &mut self,
269 camera: &CameraOptions,
270 animation: &AnimationOptions,
271 linear_zoom_interpolation: bool,
272 ) -> CameraCommandId {
273 self.command(|transform| transform.fly(camera, animation, linear_zoom_interpolation))
274 }
275
276 pub(super) fn fly(
277 &mut self,
278 input_camera: &CameraOptions,
279 animation: &AnimationOptions,
280 linear_zoom_interpolation: bool,
281 ) {
282 let mut camera = *input_camera;
283 let globe = self.state.view().is_globe();
284
285 let mut zoom = camera.zoom.unwrap_or(self.view().zoom());
286 if camera.zoom.is_none()
287 && let Some(center) = camera.center
288 && globe
289 {
290 zoom += VerticalPerspectiveProjection::zoom_adjustment(
291 self.view().center(WrapMode::Wrapped).latitude(),
292 center.latitude(),
293 );
294 }
295 self.state.constrain_camera_and_zoom_to_bounds(&mut camera, &mut zoom);
296
297 let padding = camera.padding.unwrap_or(self.state.view().padding());
298 let lat_lng_end = self.state.constrained_center(
301 &camera.center.unwrap_or(self.view().center(WrapMode::Unwrapped)).wrapped(),
302 if camera.zoom.is_some() { zoom } else { self.view().zoom() },
303 );
304 let center_alt = camera.center_altitude.unwrap_or(self.state.view().center_altitude());
305 let mut bearing = match camera.bearing {
306 Some(b) => mln_style::math::deg2rad(-b),
307 None => self.view().angle(),
308 };
309 let mut pitch = match camera.pitch {
310 Some(p) => mln_style::math::deg2rad(p),
311 None => self.view().pitch(),
312 };
313 let roll = match camera.roll {
314 Some(r) => mln_style::math::deg2rad(r),
315 None => self.view().roll(),
316 };
317 let mut fov = match camera.fov {
318 Some(f) => mln_style::math::deg2rad(f),
319 None => self.view().fov(),
320 };
321
322 if zoom.is_nan()
323 || bearing.is_nan()
324 || pitch.is_nan()
325 || roll.is_nan()
326 || fov.is_nan()
327 || self.state.view().size().is_empty()
328 {
329 self.end_unstarted(animation, TransitionOutcome::Cancelled);
330 return;
331 }
332
333 let mut start_lat_lng = self.view().center(WrapMode::Unwrapped).wrapped();
335 start_lat_lng.unwrap_for_shortest_path(&lat_lng_end);
336 let start_center_alt = self.state.view().center_altitude();
337
338 let start_point = Projection::project(&start_lat_lng, self.state.view().scale());
339 let end_point = Projection::project(&lat_lng_end, self.state.view().scale());
340
341 let target_globe = match &self.projection_for_zoom {
345 Some(projection_for_zoom) => projection_for_zoom(zoom).transition_state() > 0.0,
346 None => globe,
347 };
348 zoom = mln_style::math::clamp_f64(
349 zoom,
350 self.state.min_zoom_at_latitude_with(lat_lng_end.latitude(), target_globe),
351 self.state.limits().max_zoom,
352 );
353 pitch = mln_style::math::clamp_f64(pitch, self.state.limits().min_pitch, self.state.limits().max_pitch);
354 fov = mln_style::math::clamp_f64(fov, crate::camera::MIN_FIELD_OF_VIEW, crate::camera::MAX_FIELD_OF_VIEW);
355
356 bearing = normalize_angle(bearing, self.state.view().angle());
358 let start_zoom = crate::camera::scale_zoom(self.state.view().scale());
359 let start_bearing = normalize_angle(self.state.view().angle(), bearing);
360 let start_pitch = self.state.view().pitch();
361 let start_roll = self.state.view().roll();
362 let start_fov = self.state.view().fov();
363
364 let size = self.state.view().size();
367 let w0 = f64::max(
368 f64::from(size.width) - padding.left - padding.right,
369 f64::from(size.height) - padding.top - padding.bottom,
370 );
371 let normalized_start_zoom = if globe {
374 start_zoom + VerticalPerspectiveProjection::zoom_adjustment(start_lat_lng.latitude(), 0.0)
375 } else {
376 start_zoom
377 };
378 let normalized_zoom = if globe {
379 zoom + VerticalPerspectiveProjection::zoom_adjustment(lat_lng_end.latitude(), 0.0)
380 } else {
381 zoom
382 };
383 let w1 = w0 / crate::camera::zoom_scale(normalized_zoom - normalized_start_zoom);
386 let u1 = if globe {
389 VerticalPerspectiveProjection::surface_distance_pixels(
390 Projection::world_size(self.state.view().scale()),
391 start_lat_lng.latitude(),
392 &start_lat_lng,
393 &lat_lng_end,
394 )
395 } else {
396 (end_point.x - start_point.x).hypot(end_point.y - start_point.y)
397 };
398
399 let mut rho = 1.42;
409 if globe {
410 let floor_zoom = f64::max(animation.min_zoom.unwrap_or(self.state.min_zoom()), self.state.min_zoom());
413 let normalized_floor_zoom = f64::min(
414 f64::min(
415 floor_zoom + VerticalPerspectiveProjection::zoom_adjustment(lat_lng_end.latitude(), 0.0),
416 normalized_start_zoom,
417 ),
418 normalized_zoom,
419 );
420 let min_zoom = mln_style::math::clamp_f64(
421 normalized_floor_zoom + VerticalPerspectiveProjection::zoom_adjustment(0.0, lat_lng_end.latitude()),
422 self.state.min_zoom_at_latitude(lat_lng_end.latitude()),
423 self.state.limits().max_zoom,
424 );
425 let w_max = w0
426 / crate::camera::zoom_scale(
427 min_zoom + VerticalPerspectiveProjection::zoom_adjustment(lat_lng_end.latitude(), 0.0)
428 - normalized_start_zoom,
429 );
430 if u1 != 0.0 {
431 rho = f64::min(rho, (w_max / u1 * 2.0).sqrt());
432 }
433 } else if animation.min_zoom.is_some() || linear_zoom_interpolation {
434 let mut min_zoom = mln_style::math::fmin(
436 animation.min_zoom.unwrap_or(start_zoom),
437 mln_style::math::fmin(start_zoom, zoom),
438 );
439 min_zoom = mln_style::math::clamp_f64(
440 min_zoom,
441 self.state.min_zoom_at_latitude(lat_lng_end.latitude()),
442 self.state.limits().max_zoom,
443 );
444 let w_max = w0 / crate::camera::zoom_scale(min_zoom - start_zoom);
447 rho = if u1 == 0.0 { 1.0 } else { (w_max / u1 * 2.0).sqrt() };
448 }
449 let rho2 = rho * rho;
451
452 let r = |i: f64| -> f64 {
456 let b = (w1 * w1 - w0 * w0 + if i == 0.0 { 1.0 } else { -1.0 } * rho2 * rho2 * u1 * u1)
458 / (2.0 * if i == 0.0 { w0 } else { w1 } * rho2 * u1);
459 ((b * b + 1.0).sqrt() - b).ln()
460 };
461
462 let r0 = if u1 == 0.0 { f64::INFINITY } else { r(0.0) }; let r1 = if u1 == 0.0 { f64::INFINITY } else { r(1.0) };
465
466 let is_close = u1.abs() < 0.000_001 || !r0.is_finite() || !r1.is_finite();
469
470 let w = move |s: f64| -> f64 {
475 if is_close {
476 ((if w1 < w0 { -1.0 } else { 1.0 }) * rho * s).exp()
477 } else {
478 r0.cosh() / (r0 + rho * s).cosh()
479 }
480 };
481 let u = move |s: f64| -> f64 {
485 if is_close { 0.0 } else { w0 * (r0.cosh() * (r0 + rho * s).tanh() - r0.sinh()) / rho2 / u1 }
486 };
487 let total_s = if is_close { (w1 / w0).ln().abs() / rho } else { (r1 - r0) / rho };
489 let along: Option<AlongFn> = if is_close { None } else { Some(Box::new(move |k| u(k * total_s))) };
491
492 let duration = if let Some(duration) = animation.duration {
493 duration
494 } else {
495 let mut velocity = 1.2;
497 if let Some(v) = animation.velocity {
498 velocity = v / rho;
499 }
500 nanos_from_secs(total_s / velocity).unwrap_or_default()
504 };
505 let duration = match animation.max_duration {
506 Some(max) if !linear_zoom_interpolation && duration > max => Duration::ZERO,
507 _ => duration,
508 };
509 let start_scale = self.state.view().scale();
510 let start_edge_insets = self.state.view().padding();
511 let delta_latitude = if globe {
512 VerticalPerspectiveProjection::difference_of_angles_degrees(
513 start_lat_lng.latitude(),
514 lat_lng_end.latitude(),
515 )
516 } else {
517 0.0
518 };
519 let delta_longitude = if globe {
520 VerticalPerspectiveProjection::difference_of_angles_degrees(
521 start_lat_lng.longitude(),
522 lat_lng_end.longitude(),
523 )
524 } else {
525 0.0
526 };
527
528 let frame: FrameFn = Box::new(move |_: &TransformState, k: f64| {
529 let s = k * total_s;
532 let us = if k == 1.0 { 1.0 } else { u(s) };
533
534 let (frame_lat_lng, frame_zoom) = if globe {
535 let mut frame_lat_lng = lat_lng_end;
536 let mut frame_zoom = zoom;
537 if k < 1.0 {
538 frame_lat_lng = VerticalPerspectiveProjection::interpolate_lat_lng(
539 &start_lat_lng,
540 delta_latitude,
541 delta_longitude,
542 us,
543 )
544 .wrapped();
545 let frame_normalized_zoom = if linear_zoom_interpolation {
546 interpolate(&normalized_start_zoom, &normalized_zoom, k)
547 } else {
548 normalized_start_zoom + crate::camera::scale_zoom(1.0 / w(s))
549 };
550 frame_zoom = frame_normalized_zoom
551 + VerticalPerspectiveProjection::zoom_adjustment(0.0, frame_lat_lng.latitude());
552 if frame_zoom.is_nan() {
553 frame_zoom = zoom;
554 }
555 }
556 (Some(frame_lat_lng), frame_zoom)
557 } else {
558 let frame_point = interpolate(&start_point, &end_point, us);
561 let mut frame_zoom = if linear_zoom_interpolation {
562 interpolate(&start_zoom, &zoom, k)
563 } else {
564 start_zoom + crate::camera::scale_zoom(1.0 / w(s))
565 };
566
567 if frame_zoom.is_nan() {
569 frame_zoom = zoom;
570 }
571
572 (frame_unproject(&frame_point, start_scale, WrapMode::Unwrapped), frame_zoom)
574 };
575 CameraOptions {
576 center: frame_lat_lng,
577 zoom: Some(frame_zoom),
578 center_altitude: Some(interpolate(&start_center_alt, ¢er_alt, us)),
579 bearing: Some(-mln_style::math::rad2deg(mln_style::math::wrap(
580 interpolate(&start_bearing, &bearing, k),
581 -PI,
582 PI,
583 ))),
584 padding: interpolate_edge_insets(&start_edge_insets, &padding, k),
585 pitch: Some(mln_style::math::rad2deg(interpolate(&start_pitch, &pitch, k))),
586 roll: Some(mln_style::math::rad2deg(interpolate(&start_roll, &roll, k))),
587 fov: Some(mln_style::math::rad2deg(interpolate(&start_fov, &fov, k))),
588 ..Default::default()
589 }
590 });
591
592 self.start_transition(input_camera, animation, frame, along, duration, !linear_zoom_interpolation);
593 }
594}