use mln_style::error::Error;
use mln_style::geo::{LatLng, WrapMode};
use mln_style::geometry::Point;
use super::{Transform, flip_y};
use crate::camera::{VerticalPerspectiveProjection, View};
use crate::geo::ScreenCoordinate;
const NEAR_CENTER: f64 = 1e-2;
const MAX_ANCHOR_RISE: f64 = 0.9;
const MAX_PAN_VERTICAL_GAIN: f64 = 3.0;
const MAX_PAN_HORIZONTAL_GAIN: f64 = 2.0;
impl Transform {
pub(super) fn anchor_plane(&self, grabbed: &ScreenCoordinate, anchor: &ScreenCoordinate) -> Option<f64> {
let view = self.view();
let altitude = view.center_altitude();
let ceiling = altitude + MAX_ANCHOR_RISE * (view.eye_position().z - altitude);
let height = self.grabbed_height(grabbed)?.min(ceiling);
let center = view.padding().center_in(view.size());
let pivot = grabbed == anchor && (anchor.x - center.x).powi(2) + (anchor.y - center.y).powi(2) < NEAR_CENTER;
let sees = |point: &ScreenCoordinate| sees_plane(view, &flip_y(view, point), height);
(!pivot && sees(anchor) && sees(grabbed)).then_some(height)
}
pub(super) fn grabbed_height(&self, grabbed: &ScreenCoordinate) -> Option<f64> {
let Some(terrain) = &self.terrain else { return Some(0.0) };
let view = self.view();
Some(view.ground_under(&flip_y(view, grabbed), terrain.elevation.as_deref()?)?.height)
}
pub(super) fn clamp_anchor_height(&self, height: f64) -> f64 {
let view = self.view();
let altitude = view.center_altitude();
let ceiling = altitude + MAX_ANCHOR_RISE * (view.eye_position().z - altitude);
height.min(ceiling)
}
pub(super) fn anchor_height(&self, grabbed: &ScreenCoordinate, anchor: &ScreenCoordinate) -> f64 {
self.anchor_plane(grabbed, anchor).unwrap_or(self.view().center_altitude())
}
pub(super) fn center_moving_on_plane(
&self,
from: &ScreenCoordinate,
to: &ScreenCoordinate,
height: f64,
) -> Result<LatLng, Error> {
let location = self.screen_coordinate_to_lat_lng_at(from, height, WrapMode::Unwrapped)?;
self.state.center_for_lat_lng_at(&location, &flip_y(self.view(), to), height)
}
pub(super) fn center_panning_on_plane(
&self,
from: &ScreenCoordinate,
to: &ScreenCoordinate,
height: f64,
) -> Result<LatLng, Error> {
let view = self.view();
let size = view.size();
if !from.x.is_finite()
|| !from.y.is_finite()
|| !to.x.is_finite()
|| !to.y.is_finite()
|| size.is_empty()
|| view.params().axonometric
|| view.is_globe()
{
return self.center_moving_on_plane(from, to, height);
}
let pitch = view.pitch();
let d = 0.5 * f64::from(size.height) / (view.perspective_fov() / 2.0).tan();
let eye_height_px = (view.eye_position().z - height) / view.meters_per_pixel();
let h0 = eye_height_px / d;
let center = view.padding().center_in(size);
let (sin_psi, cos_psi) = (view.roll() - view.north_orientation_angle()).sin_cos();
let unrolled = |p: &ScreenCoordinate| {
let sx = p.x - center.x;
let sy = center.y - p.y;
(sx * cos_psi + sy * sin_psi, -sx * sin_psi + sy * cos_psi)
};
let (u1, v1) = unrolled(from);
let (u2, v2) = unrolled(to);
let (sin_p, cos_p) = pitch.sin_cos();
let (dx_ground, dy_ground) = if !h0.is_finite() || h0 <= 0.0 {
(u2 - u1, (v2 - v1) * MAX_PAN_VERTICAL_GAIN)
} else if sin_p.abs() < 1e-9 {
if sees_plane(view, &flip_y(view, from), height) && sees_plane(view, &flip_y(view, to), height) {
return self.center_moving_on_plane(from, to, height);
}
let scale = h0 / cos_p;
((u2 - u1) * scale, (v2 - v1) * scale)
} else {
let q = |v: f64| cos_p - (v / d) * sin_p;
let (q1, q2) = (q(v1), q(v2));
let q_y_min = (h0 / MAX_PAN_VERTICAL_GAIN).sqrt();
let q_x_min = h0 / MAX_PAN_HORIZONTAL_GAIN;
let q_exact_min = q_y_min.max(q_x_min);
if q1 >= q_exact_min
&& q2 >= q_exact_min
&& sees_plane(view, &flip_y(view, from), height)
&& sees_plane(view, &flip_y(view, to), height)
{
return self.center_moving_on_plane(from, to, height);
}
let v_y0 = d * (cos_p - q_y_min) / sin_p;
let y_at = |v: f64, qv: f64| {
if qv >= q_y_min {
h0 * d / (sin_p * qv)
} else {
h0 * d / (sin_p * q_y_min) + MAX_PAN_VERTICAL_GAIN * (v - v_y0)
}
};
let dy = y_at(v2, q2) - y_at(v1, q1);
let gx = |qv: f64| h0 / qv.max(q_x_min);
let dx_horiz = f64::midpoint(gx(q1), gx(q2)) * (u2 - u1);
let conv_at = |qv: f64| {
if qv >= q_y_min {
h0 / qv
} else if qv > 0.0 {
h0 / q_y_min + 0.5 * MAX_PAN_VERTICAL_GAIN * (q_y_min * q_y_min - qv * qv) / q_y_min
} else {
1.5 * h0 / q_y_min
}
};
let dx_conv = f64::midpoint(u1, u2) * (conv_at(q2) - conv_at(q1));
(dx_horiz + dx_conv, dy)
};
let (sin_a, cos_a) = view.angle().sin_cos();
let world_dx = -dx_ground * cos_a + dy_ground * sin_a;
let world_dy = dx_ground * sin_a + dy_ground * cos_a;
let scale = view.scale();
let world = view.world();
let center_coord = world.project(&view.center(WrapMode::Unwrapped), scale);
world.unproject(&Point::new(center_coord.x + world_dx, center_coord.y + world_dy), scale, WrapMode::Unwrapped)
}
}
pub(super) fn sees_plane(view: &View, point: &ScreenCoordinate, height: f64) -> bool {
if view.is_globe() {
let above =
view.globe_camera_position().length() > 1.0 + height / VerticalPerspectiveProjection::GLOBE_RADIUS_METERS;
return above && VerticalPerspectiveProjection::screen_coordinate_hits_globe(view, point, height);
}
let Some((near, far)) = view.line_of_sight(point) else { return false };
far.z < near.z && far.z <= height && height < near.z && view.has_lat_lng_at(point, height)
}