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