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
22 changes: 21 additions & 1 deletion crates/map/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,17 @@ impl Band {
![(1.0, 0.0), (0.0, 1.0)].into_iter().chain(edges).any(apart)
}

/// Whether `e` lies wholly on the band's ground (its edges count): each of its corners on the
/// inner side of every edge of the convex ground, whichever way round the ground runs.
pub fn ground_holds(&self, e: &Extent) -> bool {
let side = |p: Coord, i: usize| {
let (a, b) = (self.ground[i], self.ground[(i + 1) % 4]);
(b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x)
};
let all = |sign: f64| corners(e).iter().all(|&p| (0..4).all(|i| side(p, i) * sign >= 0.0));
!e.is_empty() && (all(1.0) || all(-1.0))
}

/// 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.
Expand Down Expand Up @@ -755,7 +766,7 @@ mod tests {
}

#[test]
fn a_band_touches_what_meets_its_ground_not_only_its_box() {
fn a_band_touches_what_meets_its_ground_and_holds_what_lies_on_it() {
// 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 {
Expand Down Expand Up @@ -784,6 +795,15 @@ mod tests {
assert_eq!(across, b.ground_touches(&Extent::new(x0, y0, x1, y1)), "{column}, {row}");
}
}
// It holds a box wholly on the ground, its edges counting, whichever way round it runs.
let reversed = Band { ground: [ground[3], ground[2], ground[1], ground[0]], ..b };
for b in [b, reversed] {
assert!(b.ground_holds(&Extent::new(4.0, 4.0, 6.0, 6.0)), "inside");
assert!(b.ground_holds(&Extent::new(2.5, 2.5, 7.5, 7.5)), "corners on its edges");
assert!(!b.ground_holds(&Extent::new(7.0, 7.0, 9.0, 9.0)), "over its north-east edge");
assert!(!b.ground_holds(&Extent::new(-5.0, -5.0, 15.0, 15.0)), "round it");
assert!(!b.ground_holds(&Extent::EMPTY));
}
}

#[test]
Expand Down
16 changes: 15 additions & 1 deletion crates/map/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5403,10 +5403,24 @@ mod tests {
}
}
assert_eq!(lod::DISPLAY_BUILDS.get(), before + 3);
assert_eq!(l.display_rejected.len(), lod::LEVELS_KEPT);
assert_eq!(l.display_rejected.len(), 3);
l.touch_geometry();
assert!(!l.prepare_display(&flat, 0.001, false));
assert_eq!(lod::DISPLAY_BUILDS.get(), before + 4);
// One more than `LEVELS_KEPT`: the least recently tried goes, the rest stay.
l.touch_geometry();
let tols: Vec<f64> = (0..=lod::LEVELS_KEPT).map(|i| 0.001 * f64::powi(2.0, i as i32)).collect();
let before = lod::DISPLAY_BUILDS.get();
for &tol in &tols {
assert!(!l.prepare_display(&flat, tol, false));
}
assert_eq!(l.display_rejected.len(), lod::LEVELS_KEPT);
for &tol in &tols[1..] {
assert!(!l.prepare_display(&flat, tol, false));
}
assert_eq!(lod::DISPLAY_BUILDS.get(), before + tols.len());
assert!(!l.prepare_display(&flat, tols[0], false));
assert_eq!(lod::DISPLAY_BUILDS.get(), before + tols.len() + 1);
}

/// A layer held in another system keeps its globe copy in longitude/latitude beside a flat
Expand Down
9 changes: 7 additions & 2 deletions crates/map/src/lod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ pub const THIN_ABOVE: usize = 200_000;
/// it draws from the full geometry (zoomed in past the vertex spacing, or many tiny features).
pub const MIN_SAVING: f64 = 0.25;

/// How many levels a layer keeps.
pub const LEVELS_KEPT: usize = 3;
/// How many levels a layer keeps: as many as a tilted view has bands
/// ([`crate::camera::MAX_TILT_BANDS`]), each drawn at the level of its own scale, so the bands of
/// one view don't push each other's copies out and thin the layer again every frame. The copies
/// stay within [`CACHE_BYTES`], and a far band's are small. It also bounds, for every layer, a
/// streamed source's thinning decisions and the levels found not worth a copy: each as small as
/// a level's key.
pub const LEVELS_KEPT: usize = crate::camera::MAX_TILT_BANDS;

/// Additional geometry retained for display levels per layer, excluding original/projected data.
pub const CACHE_BYTES: usize = 64 * 1024 * 1024;
Expand Down
54 changes: 54 additions & 0 deletions crates/map/src/symbology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,21 @@ impl Renderer {
Classer { expr: rustgis_data::query::parse_fields(text, ds).ok(), env }
}

/// The widest stroke any feature of a layer of `g` it styles is drawn with (points): every
/// symbol it holds, the one for no class among them. A layer's stacked strokes
/// ([`Renderer::strokes_for`]) and expressions ([`crate::Layer::symbol_by`]) aren't counted.
pub fn widest_stroke(&self, g: GeometryType) -> f32 {
let widest = |symbols: &mut dyn Iterator<Item = &Symbol>| symbols.map(|s| s.stroke_width).fold(0f32, f32::max);
match self {
Renderer::Single(s) | Renderer::Proportional { base: s, .. } => s.stroke_width,
Renderer::Unique { classes, other, .. } => widest(&mut classes.iter().map(|c| &c.1).chain(std::iter::once(other))),
// A feature in no class takes the first symbol's stroke, or the default's without one
// (`symbol_in`).
Renderer::Graduated { symbols, .. } if symbols.is_empty() => Symbol::default_for(g, 0).stroke_width,
Renderer::Graduated { symbols, .. } => widest(&mut symbols.iter()),
}
}

/// The first symbol (the single symbol, or a representative).
pub fn base_symbol(&self) -> Symbol {
match self {
Expand Down Expand Up @@ -3979,6 +3994,45 @@ mod tests {
use super::*;
use rustgis_data::{Field, Value};

/// The widest stroke a renderer draws is that of any of its symbols, the one for no class
/// among them.
#[test]
fn a_renderers_widest_stroke_is_any_of_its_symbols() {
let line = |w: f32| Symbol { stroke_width: w, ..Symbol::default_for(GeometryType::Polyline, 0) };
assert_eq!(Renderer::Single(line(2.5)).widest_stroke(GeometryType::Polyline), 2.5);
let unique = |other: f32| Renderer::Unique {
field: "kind".into(),
classes: vec![("a".into(), line(1.0)), ("b".into(), line(3.0))],
other: line(other),
ramp: Ramp::default(),
class_labels: BTreeMap::new(),
other_label: None,
class_strokes: BTreeMap::new(),
other_strokes: Vec::new(),
class_fills: BTreeMap::new(),
other_fills: Vec::new(),
expression: None,
keep_order: false,
};
assert_eq!(unique(0.5).widest_stroke(GeometryType::Polyline), 3.0);
assert_eq!(unique(6.0).widest_stroke(GeometryType::Polyline), 6.0, "the stroke of no class");
let graduated = |symbols: Vec<Symbol>| Renderer::Graduated {
field: "n".into(),
breaks: vec![1.0, 2.0],
symbols,
ramp: Ramp::default(),
method: ClassMethod::default(),
min: 0.0,
classes: 2,
class_labels: BTreeMap::new(),
class_strokes: BTreeMap::new(),
class_fills: BTreeMap::new(),
};
assert_eq!(graduated(vec![line(4.0), line(2.0)]).widest_stroke(GeometryType::Polyline), 4.0);
let default = Symbol::default_for(GeometryType::Polyline, 0).stroke_width;
assert_eq!(graduated(Vec::new()).widest_stroke(GeometryType::Polyline), default, "no symbols: the default's");
}

/// Labels saved before label classes (#618) read as the Default class alone, and are written
/// back the same, so older versions still read a project without classes.
#[test]
Expand Down
5 changes: 3 additions & 2 deletions crates/render/examples/vector_layers_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! out (the way out finds the levels of detail built on the way in, those still kept);
//! - **pan 10 m / pan 3 m:** west to east across it at 10 and 3 Web Mercator metres a point, a
//! quarter of the view a step, so every frame brings new features in;
//! - **tilt 10 m:** the same pan at 10 m, tilted 60°;
//! - **tilt 10 m:** the same pan at 10 m, tilted 60° (`BENCH_PITCH=80` another pitch);
//! - **restyle:** the top layer's fill colour changed in each of 8 frames of one view, over the
//! whole map and at 10 m a point (#1057), then its transparency instead, then the fill with
//! every layer renamed in each frame too, so that none looks as it did: what each frame drew
Expand Down Expand Up @@ -182,7 +182,8 @@ fn main() {
}
let back: Vec<Camera> = zoom.iter().rev().skip(1).copied().collect();
zoom.extend(back);
let tilt: Vec<Camera> = pan(10.0).into_iter().map(|c| Camera { pitch: 60.0, ..c }).collect();
let pitch: f64 = std::env::var("BENCH_PITCH").ok().and_then(|p| p.parse().ok()).filter(|p: &f64| p.is_finite()).unwrap_or(60.0);
let tilt: Vec<Camera> = pan(10.0).into_iter().map(|c| Camera { pitch, ..c }).collect();
let sequences: [(&str, Vec<Camera>); 5] = [
("whole", (0..4).map(|_| camera(centre, whole)).collect()),
("zoom", zoom),
Expand Down
74 changes: 71 additions & 3 deletions crates/render/src/layer_meshes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,18 @@ pub(crate) struct BandMade {
/// How far its strokes reach ([`crate::mesh_tiles::Tiling::grow`] and `spread`).
pub grow: f64,
pub spread: f64,
/// How far past its ground its edge tiles hold features ([`crate::mesh_tiles::Tiling::margin`]).
pub margin: f64,
/// Its ground: which of its tiles there are, as the cull says for a screen scene.
pub ground: [rustgis_geom::Coord; 4],
}

impl BandMade {
fn same_tiles(&self, other: &BandMade) -> bool {
self.slot == other.slot && self.grow.to_bits() == other.grow.to_bits() && self.spread.to_bits() == other.spread.to_bits()
self.slot == other.slot
&& self.grow.to_bits() == other.grow.to_bits()
&& self.spread.to_bits() == other.spread.to_bits()
&& self.margin.to_bits() == other.margin.to_bits()
}

fn same(&self, other: &BandMade) -> bool {
Expand Down Expand Up @@ -512,6 +517,36 @@ mod tests {
}
}

/// Whether `scene` draws all `afresh` draws, mesh by mesh in the same place: a tilted view's
/// band tile taken again may hold features past its ground too, which its depth clipping
/// leaves out, so each of `afresh`'s meshes' triangles (by their vertices' values) need only
/// be among `scene`'s there.
fn draws_all_of(scene: &GpuScene, afresh: &GpuScene) -> bool {
type Key = ([u64; 2], Option<[u32; 4]>);
type Triangle = [[u32; 4]; 3];
let placed = |s: &GpuScene| -> Vec<(Key, std::collections::HashSet<Triangle>)> {
s.cmds
.iter()
.filter_map(|c| match c {
DrawCmd::Mesh { origin, square, vertices, indices, .. } => {
let at = |i: u32| {
let v = &vertices[i as usize];
[v.position[0].to_bits(), v.position[1].to_bits(), v.color[0].to_bits(), v.stroke_px.to_bits()]
};
Some((
(origin.map(f64::to_bits), square.map(|q| q.map(f32::to_bits))),
indices.as_chunks::<3>().0.iter().map(|t| [at(t[0]), at(t[1]), at(t[2])]).collect(),
))
}
_ => None,
})
.collect()
};
let (have, want) = (placed(scene), placed(afresh));
let keys = |m: &[(Key, std::collections::HashSet<Triangle>)]| m.iter().map(|(k, _)| *k).collect::<std::collections::BTreeSet<_>>();
keys(&have) == keys(&want) && want.iter().all(|(k, w)| have.iter().any(|(h, v)| h == k && w.is_subset(v)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc above draws_all_of says the kept scene only needs to draw everything afresh draws, and that a taken tile may hold extra triangles. This check is stricter than that: keys(&have) == keys(&want) also requires the kept scene to hold exactly the same meshes as the fresh one, so a valid kept scene with one extra mesh (a taken tile whose features are no longer on the ground) would fail. Either the doc should say the mesh sets must match, or this clause should go if extra meshes are meant to be allowed. The current test passes, so this is only about what the helper promises.

Category: quality

}

/// Each Mesh command's vertices and indices, by address and by value.
fn meshes(s: &GpuScene) -> Vec<(*const Vec<MeshVertex>, Vec<[u32; 4]>, Vec<u32>)> {
s.cmds
Expand Down Expand Up @@ -639,8 +674,7 @@ mod tests {
assert!(taken > 0 && taken < tiled(&panned), "{taken} of {} tiles taken", tiled(&panned));
super::clear();
let afresh = scene(&mut m, &at(1.5));
let values = |s: &GpuScene| meshes(s).into_iter().map(|m| (m.1, m.2)).collect::<Vec<_>>();
assert!(values(&panned) == values(&afresh), "the panned scene is the one drawn afresh");
assert!(draws_all_of(&panned, &afresh), "the panned scene draws all the one drawn afresh does");
// The same view again takes every band's mesh of the layer whole.
let bands = at(1.5).camera.tilt_bands(1600.0, 1000.0).len();
let _ = super::taken();
Expand All @@ -650,6 +684,40 @@ mod tests {
assert_eq!(meshes(&again).into_iter().map(|m| m.0).collect::<Vec<_>>(), shared);
}

/// A steep view's band tiles on its ground's edge hold only the features reaching the ground,
/// grown by the band's margin: a pan, which moves the ground over them, takes those whose
/// ground stays within that and makes the rest again, and the scene draws all the one drawn
/// afresh does.
#[test]
fn a_panned_steep_view_takes_its_bands_edge_tiles_while_they_hold_its_ground() {
super::clear();
let mut m = Map::new("Map");
m.basemap = rustgis_map::Basemap::Blank;
m.graticule = false;
let mut d = Dataset::new("lines", GeometryType::Polyline, vec![]);
// Short lines every 3 km over 200 km: edge tiles hold some, and leave out others.
for n in 0u32..4900 {
let (x, y) = (f64::from(n % 70) * 0.03, f64::from(n / 70) * 0.03);
d.push(Some(Geometry::Line(vec![vec![Coord::new(6.9 + x, 46.0 + y), Coord::new(6.905 + x, 46.005 + y)]])), vec![]);
}
m.add_dataset(d);
let at = |x: f64| View {
width: 3200.0,
height: 2400.0,
pixels_per_point: 1.0,
camera: Camera { center: rustgis_geom::lonlat_to_merc(Coord::new(7.9, x)), res: 4.0, bearing: 0.0, pitch: 75.0 },
sky: false,
};
// North by about 11 km: past the margin (an eighth of the view, 1.6 km) for some edge tiles.
let first = scene(&mut m, &at(46.9));
let panned = scene(&mut m, &at(47.0));
let before: Vec<_> = meshes(&first).into_iter().map(|m| m.0).collect();
assert!(meshes(&panned).iter().any(|m| before.contains(&m.0)), "a whole tile taken");
super::clear();
let afresh = scene(&mut m, &at(47.0));
assert!(draws_all_of(&panned, &afresh), "the panned scene draws all the one drawn afresh does");
}

/// A tilted view's bands keep the layer's meshes one a band, sharing one copy of the rows
/// they drew; each band keeps its last two views ([`VIEWS`]), not its first.
#[test]
Expand Down
Loading
Loading