use crate::output::Output;
use crate::profile::OutputSpec;
use crate::projection::Projector;
use crate::warp::Warps;
use geo::Coord;
#[derive(Debug, Clone)]
pub struct Frame {
pub output: Output,
projector: Projector,
warp: Warps,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Bounds {
pub north: f64,
pub south: f64,
pub east: f64,
pub west: f64,
}
impl Frame {
pub fn new(spec: &OutputSpec, center: (f64, f64)) -> Frame {
let output = Output::new(spec, center);
let projector = Projector::new(&output);
Frame { output, projector, warp: spec.warp.clone() }
}
pub fn warp(&self) -> &Warps {
&self.warp
}
pub fn to_page(&self, lon: f64, lat: f64) -> (f64, f64) {
let (x, y) = self.projector.project(lon, lat);
let c = self.warp.point_at(self.output.width_mm, self.output.height_mm, Coord { x, y });
(c.x, c.y)
}
pub fn to_world(&self, x: f64, y: f64) -> Option<(f64, f64)> {
let (ux, uy) = self.unwarp(x, y)?;
self.projector.locate(ux, uy)
}
pub fn to_world_drawn(&self, x: f64, y: f64) -> Option<(f64, f64)> {
let (ux, uy) = self.unwarp(x, y)?;
const EPS: f64 = 1e-6;
let (w, h) = (self.output.width_mm, self.output.height_mm);
if self.warp.moves_edge() && !((-EPS..=w + EPS).contains(&ux) && (-EPS..=h + EPS).contains(&uy)) {
return None;
}
self.projector.locate(ux, uy)
}
fn unwarp(&self, x: f64, y: f64) -> Option<(f64, f64)> {
if self.warp.is_empty() {
return Some((x, y));
}
let (w, h) = (self.output.width_mm, self.output.height_mm);
let at = |x: f64, y: f64| {
let c = self.warp.point_at(w, h, Coord { x, y });
(c.x, c.y)
};
let (mut ux, mut uy) = (x, y);
const H: f64 = 1e-6;
for _ in 0..60 {
let (px, py) = at(ux, uy);
let (ex, ey) = (px - x, py - y);
if ex.hypot(ey) < 1e-9 {
return Some((ux, uy));
}
let (ax, ay) = at(ux + H, uy);
let (bx, by) = at(ux, uy + H);
let (j11, j21, j12, j22) = ((ax - px) / H, (ay - py) / H, (bx - px) / H, (by - py) / H);
let det = j11 * j22 - j12 * j21;
if !det.is_finite() || det.abs() < 1e-12 {
return None;
}
ux -= (j22 * ex - j12 * ey) / det;
uy -= (-j21 * ex + j11 * ey) / det;
if !(ux.is_finite() && uy.is_finite()) {
return None;
}
}
let (px, py) = at(ux, uy);
((px - x).hypot(py - y) < 1e-5).then_some((ux, uy))
}
pub fn pole(&self) -> Option<f64> {
let (w, h) = (self.output.width_mm, self.output.height_mm);
[90.0, -90.0].into_iter().find(|&lat| {
let (x, y) = self.to_page(0.0, lat);
x.is_finite() && y.is_finite() && (0.0..=w).contains(&x) && (0.0..=h).contains(&y)
})
}
pub fn bounds(&self) -> Bounds {
const PER_SIDE: usize = 32;
let (w, h) = (self.output.width_mm, self.output.height_mm);
let mut b = Bounds { north: f64::MIN, south: f64::MAX, east: f64::MIN, west: f64::MAX };
for i in 0..PER_SIDE * 4 {
let t = (i % PER_SIDE) as f64 / PER_SIDE as f64;
let (x, y) = match i / PER_SIDE {
0 => (w * t, 0.0),
1 => (w, h * t),
2 => (w * (1.0 - t), h),
_ => (0.0, h * (1.0 - t)),
};
if let Some((lon, lat)) = self.to_world(x, y) {
b.north = b.north.max(lat);
b.south = b.south.min(lat);
b.east = b.east.max(lon);
b.west = b.west.min(lon);
}
}
if b.north < b.south {
let (lon, lat) = self.output.center;
b = Bounds { north: lat, south: lat, east: lon, west: lon };
}
if let Some(pole) = self.pole() {
(b.west, b.east) = (-180.0, 180.0);
if pole > 0.0 { b.north = 90.0 } else { b.south = -90.0 }
}
b
}
}
#[cfg(test)]
mod tests {
use super::*;
fn spec(json: &str) -> OutputSpec {
serde_json::from_str(json).unwrap()
}
#[test]
fn the_centre_is_the_middle_of_the_page() {
let f = Frame::new(&spec(r#"{ "width": "200mm", "height": "100mm", "scale": 10000 }"#), (-71.06, 42.36));
let (x, y) = f.to_page(-71.06, 42.36);
assert!((x - 100.0).abs() < 1e-6 && (y - 50.0).abs() < 1e-6, "{x} {y}");
}
#[test]
fn to_world_undoes_to_page_under_a_bearing_and_a_warp() {
let f = Frame::new(
&spec(r#"{ "width": "200mm", "height": "100mm", "scale": 25000, "bearing": 30, "warp": { "fisheye": 0.5 } }"#),
(12.49, 41.89),
);
for (x, y) in [(10.0, 10.0), (150.0, 80.0), (100.0, 50.0), (199.0, 1.0), (200.0, 0.0), (0.0, 100.0)] {
let (lon, lat) = f.to_world(x, y).expect("on the page");
let (bx, by) = f.to_page(lon, lat);
assert!((bx - x).hypot(by - y) < 1e-4, "({x}, {y}) came back as ({bx}, {by})");
}
}
#[test]
fn a_polar_page_reaches_the_pole() {
let f = Frame::new(
&spec(r#"{ "width": "297mm", "height": "210mm", "scale": 30000000, "projection": "+proj=stere +lat_0=90 +lon_0={lng} +lat_ts=90" }"#),
(20.0, 80.0),
);
assert_eq!(f.pole(), Some(90.0));
let b = f.bounds();
assert_eq!((b.north, b.west, b.east), (90.0, -180.0, 180.0));
assert!(b.south < 70.0, "{b:?}");
let (x, y) = f.to_page(20.0, 90.0);
assert!((x - 148.5).abs() < 1e-6 && y < 105.0, "{x} {y}");
let q = f.output.query_polygon();
use geo::BoundingRect;
let r = q.bounding_rect().unwrap();
assert_eq!((r.min().x, r.max().x, r.max().y), (-180.0, 180.0, 90.0));
}
#[test]
fn north_is_up_and_bounds_hold_the_centre() {
let f = Frame::new(&spec(r#"{ "width": "297mm", "height": "210mm", "scale": 750000 }"#), (-70.54, 41.95));
let b = f.bounds();
assert!(b.south < 41.95 && 41.95 < b.north && b.west < -70.54 && -70.54 < b.east, "{b:?}");
assert!(((b.north - b.south) - 157.5 / 111.1).abs() < 0.05, "{b:?}");
let (_, y_north) = f.to_page(-70.54, b.north);
assert!(y_north.abs() < 1e-3, "the north edge is the top of the page: {y_north}");
}
}