Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 118 additions & 5 deletions crates/map/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ impl Camera {
/// fetched) coarser. A flat view is one band: itself.
pub fn tilt_bands(&self, w: f64, h: f64) -> Vec<Band> {
let Some(t) = self.tilt(h) else {
return vec![Band { camera: *self, width: w, height: h, depth: (1.0, 1.0) }];
return vec![Band::flat(*self, w, h)];
};
let z_of = |v: f64| t.d - v * t.s;
let v_of = |z: f64| (t.d - z) / t.s;
Expand All @@ -371,9 +371,10 @@ impl Camera {
let pad = 2.0 * k;
let (hw0, hw1) = (w / 2.0 * z0 / t.d + pad, w / 2.0 * z1 / t.d + pad);
let (v0, v1) = (v_of(z0) + pad, v_of(z1) - pad);
let ground = [(-hw1, v1), (hw1, v1), (hw0, v0), (-hw0, v0)].map(|(u, v)| self.ground_to_map(u, v));
let mut e = Extent::EMPTY;
for (u, v) in [(-hw1, v1), (hw1, v1), (-hw0, v0), (hw0, v0)] {
e.expand(self.ground_to_map(u, v));
for c in ground {
e.expand(c);
}
let mut res = self.res * k;
// A band squeezed on screen (far ground at a steep pitch is foreshortened up and
Expand All @@ -393,7 +394,7 @@ impl Camera {
let camera = Camera { center: e.center(), res, bearing: 0.0, pitch: 0.0 };
let (width, height) = ((e.width() / res).ceil().max(1.0), (e.height() / res).ceil().max(1.0));
let depth = (z_of(v0) / t.d, z_of(v1) / t.d);
bands.push(Band { camera, width, height, depth });
bands.push(Band { camera, width, height, depth, ground });
z0 = z1;
}
// A view too small to have any depth to it (or whose near and far ends meet) is one band
Expand All @@ -406,6 +407,7 @@ impl Camera {
width: (e.width() / self.res).ceil().max(1.0),
height: (e.height() / self.res).ceil().max(1.0),
depth: (1.0, 1.0),
ground: corners(&e),
});
}
bands
Expand Down Expand Up @@ -505,6 +507,76 @@ pub struct Band {
/// The nearest and farthest ground it holds, as multiples of the tilted camera's distance
/// from the view's centre.
pub depth: (f64, f64),
/// The ground it holds, map units: a convex quadrilateral (far edge first), within the
/// camera's extent, which is its bounding box.
pub ground: [Coord; 4],
}

impl Band {
/// A flat view's one band: `camera` itself at `width`×`height`, all its ground.
pub fn flat(camera: Camera, width: f64, height: f64) -> Band {
Band { camera, width, height, depth: (1.0, 1.0), ground: corners(&camera.extent(width, height)) }
}

/// Whether `e` touches the band's ground (its edges count), as two convex shapes meet: no
/// line through an axis or an edge of the ground's parts them.
pub fn ground_touches(&self, e: &Extent) -> bool {
let rect = corners(e);
let apart = |n: (f64, f64)| {
let span = |ps: &[Coord; 4]| {
ps.iter().map(|p| p.x * n.0 + p.y * n.1).fold((f64::INFINITY, f64::NEG_INFINITY), |(lo, hi), d| (lo.min(d), hi.max(d)))
};
let (a, b) = (span(&self.ground), span(&rect));
a.1 < b.0 || b.1 < a.0
};
let edges = (0..4).filter_map(|i| {
let (a, b) = (self.ground.get(i)?, self.ground.get((i + 1) % 4)?);
Some((a.y - b.y, b.x - a.x))
});
![(1.0, 0.0), (0.0, 1.0)].into_iter().chain(edges).any(apart)
}

/// The least and greatest x of the band's ground between `y0` and `y1` (its edges count):
/// the columns a row of tiles there touches it in, as [`Band::ground_touches`] finds tile
/// by tile (the ground is convex, so they run unbroken). `None` when it doesn't reach there.
pub fn ground_across(&self, y0: f64, y1: f64) -> Option<(f64, f64)> {
let mut lo = f64::INFINITY;
let mut hi = f64::NEG_INFINITY;
let mut take = |x: f64| {
lo = lo.min(x);
hi = hi.max(x);
};
for i in 0..4 {
let (Some(a), Some(b)) = (self.ground.get(i), self.ground.get((i + 1) % 4)) else { continue };
if (y0..=y1).contains(&a.y) {
take(a.x);
}
// Where the edge crosses the row's bottom and top.
for y in [y0, y1] {
if (a.y - y) * (b.y - y) < 0.0 {
take(a.x + (b.x - a.x) * (y - a.y) / (b.y - a.y));
}
}
}
(lo <= hi).then_some((lo, hi))
}

/// How much farther (as [`Band::depth`] counts) the ground is a map unit past the band's far
/// edge, away from the camera: the depth grows evenly with the distance there. 0 in a flat
/// view's band.
pub fn depth_per_unit(&self) -> f64 {
let [f0, f1, n0, n1] = self.ground;
let mid = |a: Coord, b: Coord| Coord::new((a.x + b.x) / 2.0, (a.y + b.y) / 2.0);
let (far, near) = (mid(f0, f1), mid(n0, n1));
let apart = (far.x - near.x).hypot(far.y - near.y);
let per = (self.depth.1 - self.depth.0) / apart;
if per.is_finite() && per > 0.0 { per } else { 0.0 }
}
}

/// An extent's corners, in turn round it.
fn corners(e: &Extent) -> [Coord; 4] {
[Coord::new(e.min_x, e.max_y), Coord::new(e.max_x, e.max_y), Coord::new(e.max_x, e.min_y), Coord::new(e.min_x, e.min_y)]
}

/// A pitch within 0 to [`MAX_PITCH`]; one too small to see, or no number, looks straight down
Expand Down Expand Up @@ -672,6 +744,31 @@ mod tests {
assert!(c.project_raised(eu, ev, eh, w, h).is_none());
}

#[test]
fn a_band_touches_what_meets_its_ground_not_only_its_box() {
// A diamond in the box 0..10.
let ground = [Coord::new(5.0, 10.0), Coord::new(10.0, 5.0), Coord::new(5.0, 0.0), Coord::new(0.0, 5.0)];
let b = Band { camera: Camera::new(Coord::new(5.0, 5.0), 1.0), width: 10.0, height: 10.0, depth: (1.0, 2.0), ground };
assert!(b.ground_touches(&Extent::new(4.0, 4.0, 6.0, 6.0)), "inside");
assert!(b.ground_touches(&Extent::new(-5.0, -5.0, 15.0, 15.0)), "round it");
assert!(b.ground_touches(&Extent::new(7.0, 7.0, 9.0, 9.0)), "over its north-east edge");
assert!(!b.ground_touches(&Extent::new(8.0, 8.0, 10.0, 10.0)), "in its box's corner, off the ground");
assert!(!b.ground_touches(&Extent::new(11.0, 4.0, 12.0, 6.0)), "east of it");
// A row across it holds the ground's x there, as the tiles touching it find it.
assert_eq!(b.ground_across(7.0, 8.0), Some((2.0, 8.0)));
assert_eq!(b.ground_across(4.0, 6.0), Some((0.0, 10.0)));
assert_eq!(b.ground_across(10.0, 11.0), Some((5.0, 5.0)));
assert_eq!(b.ground_across(11.0, 12.0), None);
for row in -2..12 {
let (y0, y1) = (f64::from(row), f64::from(row) + 1.0);
for column in -2..12 {
let (x0, x1) = (f64::from(column), f64::from(column) + 1.0);
let across = b.ground_across(y0, y1).is_some_and(|(lo, hi)| x0 <= hi && lo <= x1);
assert_eq!(across, b.ground_touches(&Extent::new(x0, y0, x1, y1)), "{column}, {row}");
}
}
}

#[test]
fn tilted_bands_cover_the_ground_nearest_and_sharpest_first() {
let (w, h) = (800.0, 600.0);
Expand All @@ -687,6 +784,15 @@ mod tests {
for b in &bands {
let (near, far) = (c.depth_y(b.depth.0, h), c.depth_y(b.depth.1, h));
assert!(far < near, "{pitch}: rows {far}..{near}");
// Past its far edge, away from the camera, the depth grows as it says.
let [f0, f1, n0, n1] = b.ground;
let (fx, fy) = ((f0.x + f1.x) / 2.0, (f0.y + f1.y) / 2.0);
let (dx, dy) = (fx - (n0.x + n1.x) / 2.0, fy - (n0.y + n1.y) / 2.0);
let s = 0.05 * dx.hypot(dy);
let past = Coord::new(fx + dx * 0.05, fy + dy * 0.05);
let depth = 1.0 / c.depth_scale(past, h).unwrap();
let want = b.depth.1 + b.depth_per_unit() * s;
assert!((depth - want).abs() < 1e-9 * want, "{pitch}: {depth} vs {want}");
}
// The fast lookup agrees with the camera.
let look = c.ground_lookup(w, h);
Expand All @@ -705,6 +811,9 @@ mod tests {
let Some(m) = c.try_to_map(w * f64::from(i) / 10.0, h * f64::from(j) / 10.0, w, h) else { continue };
let inside = bands.iter().any(|b| b.camera.extent(b.width, b.height).padded(0.0, 1e-6).contains(m));
assert!(inside, "{pitch}: ({i}, {j}) uncovered");
// And in a band's own ground, within its box.
let at = Extent::new(m.x, m.y, m.x, m.y);
assert!(bands.iter().any(|b| b.ground_touches(&at)), "{pitch}: ({i}, {j}) off the bands' ground");
}
}
}
Expand All @@ -716,7 +825,11 @@ mod tests {
}
// A flat view is one band: itself.
c.set_pitch(0.0);
assert_eq!(c.tilt_bands(w, h), vec![Band { camera: c, width: w, height: h, depth: (1.0, 1.0) }]);
let e = c.extent(w, h);
let ground =
[Coord::new(e.min_x, e.max_y), Coord::new(e.max_x, e.max_y), Coord::new(e.max_x, e.min_y), Coord::new(e.min_x, e.min_y)];
assert_eq!(c.tilt_bands(w, h), vec![Band { camera: c, width: w, height: h, depth: (1.0, 1.0), ground }]);
assert_eq!(c.tilt_bands(w, h)[0].depth_per_unit(), 0.0);
}

#[test]
Expand Down
Loading
Loading