diff --git a/crates/engine/src/cmd/raster.rs b/crates/engine/src/cmd/raster.rs index 3960b0f0a0..25b286223b 100644 --- a/crates/engine/src/cmd/raster.rs +++ b/crates/engine/src/cmd/raster.rs @@ -36,7 +36,7 @@ pub fn commands() -> Vec { C::new("layer.addDataBytes", "Add Data (from file contents)", "Home › Add", add_bytes).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"fileName": "name.tif|.tiff|.asc|.geojson|.csv|.zip|.kml|.kmz|.gpx|.parquet|.fgb|.gpkg|.mbtiles|.jp2|.j2k (JPEG 2000: desktop, through GDAL)", "data": base64, "zoom"?: bool, "layer"?: "one layer of a zip, KML or GPX by name", "name"?: text, "x"?, "y"?, "wkt"?, "crs"?, "delimiter"?, "paperSpace"?, "stream"?, "as"? (as layer.addData)}"#), C::new("raster.symbology", "Symbology", "Style › Rendering", symbology).when(need_raster).roles(&[]).effects(crate::policy::Effects::NONE).params( - r#"{"layer"?, "type": "stretch"|"rgb"|"classify"|"unique"|"colormap"|"shadedRelief", "band"?: 1.., "ramp"?, "invert"?: bool, "classes"?: 1..32, "method"?, "azimuth"?, "altitude"?, "zFactor"?, "shadow"?: 0..1 (shadedRelief: how dark the shadows the terrain casts are; left out, kept; 0 casts none), "grey"?: bool (shadedRelief without its colour ramp)}"#, + r#"{"layer"?, "type": "stretch"|"rgb"|"classify"|"unique"|"colormap"|"shadedRelief", "band"?: 1.., "ramp"?, "invert"?: bool, "classes"?: 1..32, "method"?, "azimuth"?, "altitude"?, "zFactor"?, "shadow"?: 0..1 (shadedRelief: how dark the shadows the terrain casts are; left out, kept; 0 casts none), "sky"?: 0..1 (shadedRelief: how much the share of the sky each cell sees dims its ambient light; left out, kept; 0 leaves it whole), "grey"?: bool (shadedRelief without its colour ramp)}"#, ), C::new("raster.stretch", "Stretch Type", "Style › Rendering", stretch).inputs(stretch_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_raster) @@ -778,10 +778,10 @@ fn symbology(s: &mut Session, p: &Value) -> CmdResult { RasterRenderer::Colormap } "shadedrelief" | "hillshade" => { - // Left out, a Shaded Relief keeps its own shadows; a new one casts none. - let cur_shadow = match rl.style.renderer { - RasterRenderer::ShadedRelief { shadow, .. } => shadow, - _ => 0.0, + // Left out, a Shaded Relief keeps its own shadows and sky; a new one has neither. + let (cur_shadow, cur_sky) = match rl.style.renderer { + RasterRenderer::ShadedRelief { shadow, sky, .. } => (shadow, sky), + _ => (0.0, 0.0), }; RasterRenderer::ShadedRelief { band: bi, @@ -790,6 +790,7 @@ fn symbology(s: &mut Session, p: &Value) -> CmdResult { altitude: f(p, "altitude").unwrap_or(45.0).clamp(0.0, 90.0), z_factor: f(p, "zFactor").unwrap_or(1.0).clamp(0.001, 1000.0), shadow: f(p, "shadow").unwrap_or(cur_shadow).clamp(0.0, 1.0), + sky: f(p, "sky").unwrap_or(cur_sky).clamp(0.0, 1.0), } } other => return Err(format!("unknown raster symbology '{other}'")), diff --git a/crates/engine/src/cmd/share.rs b/crates/engine/src/cmd/share.rs index 6d927274de..a38a85e3a0 100644 --- a/crates/engine/src/cmd/share.rs +++ b/crates/engine/src/cmd/share.rs @@ -233,7 +233,7 @@ pub(crate) fn shadow_warnings(what: &str, said: &rustgis_render::ShadowsSaid) -> let mut out: Vec = said.notices.iter().map(|e| format!("{what}: {e}")).collect(); if said.to_come { out.push(format!( - "{what}: made without the cast shadows still being worked out, as the window doesn't wait for them: once the map shows them, draw or export it again" + "{what}: made without the cast shadows or sky visibility still being worked out, as the window doesn't wait for them: once the map shows them, draw or export it again" )); } out diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 8a66bdacc4..5f90f17402 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -3576,6 +3576,64 @@ fn shaded_relief_keeps_its_cast_shadows() { assert_eq!(shadow(&s), 0.0, "a new Shaded Relief casts none"); } +/// Shaded Relief's sky visibility is set, clamped, kept when left out (the shadows' too), saved +/// with the project only when there is some, and drawn in an export (waited for) with nothing to +/// say. +#[test] +fn shaded_relief_keeps_its_sky_visibility() { + let mut s = with_raster(); + let light = |s: &Session| match s.map().layers.first().and_then(|l| l.raster.as_ref()).map(|r| &r.style.renderer) { + Some(rustgis_map::RasterRenderer::ShadedRelief { shadow, sky, .. }) => (*shadow, *sky), + other => panic!("{other:?}"), + }; + s.run("raster.symbology", &json!({"type": "shadedRelief"})).unwrap(); + assert_eq!(light(&s), (0.0, 0.0)); + let skies_saved = |s: &mut Session| s.project.saved_text(None).unwrap().0.matches("\"sky\"").count(); + let before = skies_saved(&mut s); + s.run("raster.symbology", &json!({"type": "shadedRelief", "sky": 0.6, "shadow": 0.5})).unwrap(); + s.run("raster.symbology", &json!({"type": "shadedRelief", "azimuth": 200})).unwrap(); + assert_eq!(light(&s), (0.5, 0.6), "left out, kept"); + s.run("raster.symbology", &json!({"type": "shadedRelief", "sky": -3})).unwrap(); + assert_eq!(light(&s), (0.5, 0.0)); + assert_eq!(skies_saved(&mut s), before, "saved only when there is some"); + s.run("raster.symbology", &json!({"type": "shadedRelief", "sky": 4})).unwrap(); + assert_eq!(light(&s), (0.5, 1.0)); + assert_eq!(skies_saved(&mut s), before + 1); + s.notices.clear(); + assert!(crate::cmd::share::map_png(&mut s, Some(300.0), Some(200.0), None).unwrap().len() > 1000); + assert!(s.notices.iter().all(|n| !n.text.contains("sky")), "{:?}", s.notices); +} + +/// An export draws the sky a pit hides: its floor darker with `sky` 1 than with 0, the ground +/// around it (which sees the whole sky) the same. The first export with the sky, from a cold +/// cache, waits for the grid: it is the same image as one drawn once the grid is kept. +#[test] +fn an_export_draws_the_sky_a_pit_hides_and_waits_for_it() { + use rustgis_data::raster::{ColorHint, Crs, GeoTransform, Raster, geotiff}; + let mut s = world(); + // A round pit 16 cells across and 400 m deep, in flat ground 41 cells of 30 m a side. + let pit: Vec = + (0..41 * 41).map(|i| if f64::from(i % 41 - 20).hypot(f64::from(i / 41 - 20)) <= 8.0 { -400.0 } else { 0.0 }).collect(); + let gt = GeoTransform { origin_x: 500_000.0, origin_y: 7_000_000.0, pixel_width: 30.0, pixel_height: -30.0 }; + let r = Raster::new("pit", 41, 41, vec![pit], gt, Crs::from_epsg(31983).unwrap(), None, ColorHint::Gray, "Float32").unwrap(); + let bytes = geotiff::write(&r, 0).unwrap(); + crate::cmd::raster::add_raster(&mut s, "pit", rustgis_map::RasterLayer::from_bytes(r, "pit.tif".to_string(), bytes)).unwrap(); + s.run("layer.zoomTo", &json!({"layer": "pit"})).unwrap(); + let drawn = |s: &mut Session, sky: f64| { + s.run("raster.symbology", &json!({"layer": "pit", "type": "shadedRelief", "grey": true, "sky": sky})).unwrap(); + let png = crate::cmd::share::map_png(s, Some(200.0), Some(200.0), None).unwrap(); + crate::render::decode_png(&png).unwrap() + }; + let open = drawn(&mut s, 0.0); + let cold = drawn(&mut s, 1.0); + let ready = drawn(&mut s, 1.0); + assert!(cold.data() == ready.data(), "the first export waited for the sky"); + let red = |p: &tiny_skia::Pixmap, x: u32, y: u32| p.pixel(x, y).unwrap().red(); + // The pit's floor, in the middle, sees less sky; flat ground well outside it, all of it. + assert!(red(&cold, 100, 100) < red(&open, 100, 100), "{} vs {}", red(&cold, 100, 100), red(&open, 100, 100)); + assert_eq!(red(&cold, 170, 100), red(&open, 170, 100)); +} + /// A map export that draws a layer without its cast shadows says so, as such (not as a tile layer /// that failed), in a PNG, a GeoTIFF and the app's map image alike; one with nothing missing says /// nothing. diff --git a/crates/map/src/qgis/raster.rs b/crates/map/src/qgis/raster.rs index 0bf86b4335..8cf2350131 100644 --- a/crates/map/src/qgis/raster.rs +++ b/crates/map/src/qgis/raster.rs @@ -95,6 +95,7 @@ fn renderer_style(node: Node, pipe: Node, r: Node, kind: &str, layer: &str, repo altitude: num("angle", 45.0).clamp(0.0, 90.0), z_factor: num("zfactor", 1.0).clamp(0.0001, 10_000.0), shadow: 0.0, + sky: 0.0, }; (renderer, Stretch::None) } diff --git a/crates/map/src/qgis/tests.rs b/crates/map/src/qgis/tests.rs index b5755baad6..68fd5941e4 100644 --- a/crates/map/src/qgis/tests.rs +++ b/crates/map/src/qgis/tests.rs @@ -861,7 +861,7 @@ fn raster_renderers_read_as_rustgis_raster_styles() { ); assert_eq!( s.raster.unwrap().renderer, - RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 300.0, altitude: 35.0, z_factor: 2.0, shadow: 0.0 } + RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 300.0, altitude: 35.0, z_factor: 2.0, shadow: 0.0, sky: 0.0 } ); assert!(report.notes.iter().any(|n| n.text.contains("saturation"))); let (s, report) = read(r#""#, ""); diff --git a/crates/map/src/raster.rs b/crates/map/src/raster.rs index 68c80590b5..ae615d091d 100644 --- a/crates/map/src/raster.rs +++ b/crates/map/src/raster.rs @@ -58,7 +58,8 @@ pub struct RasterLayer { /// Statistics of the pixels the style's mask leaves, worked out on the first draw. #[serde(skip)] masked: MaskedStats, - /// The light its cast shadows leave, worked out off the frame (see [`RasterLayer::cast_shadows`]). + /// The light its cast shadows leave and the sky it sees, worked out off the frame (see + /// [`RasterLayer::cast_shadows`] and [`RasterLayer::sky_view`]). #[serde(skip)] cast: CastLight, /// A remote reload shared with render snapshots, never serialized. @@ -726,14 +727,14 @@ impl RasterLayer { Some(stats) } - /// Whether it is drawn with cast shadows (Shaded Relief with a shadow above 0): its frames are - /// drawn again as they land (see [`RasterLayer::cast_shadows`]). + /// Whether it is drawn with cast shadows or sky visibility (Shaded Relief with a shadow or a + /// sky above 0): its frames are drawn again as they land (see [`RasterLayer::cast_shadows`]). pub fn casts_shadows(&self) -> bool { - matches!(self.style.renderer, RasterRenderer::ShadedRelief { shadow, .. } if shadow > 0.0) + matches!(self.style.renderer, RasterRenderer::ShadedRelief { shadow, sky, .. } if shadow > 0.0 || sky > 0.0) } - /// How many wait for its cast shadows now (exports), its copies' too: for tests of what an - /// export waits for. + /// How many wait for its cast shadows or sky now (exports), its copies' too: for tests of what + /// an export waits for. #[doc(hidden)] pub fn cast_shadow_waiters(&self) -> usize { let Ok(jobs) = self.cast.0.lock() else { return 0 }; @@ -751,35 +752,55 @@ impl RasterLayer { /// export does, this waits for it. The browser, which has one thread, works it out here, for /// at most [`crate::shadow::MAX_BROWSER_CELLS`] cells. pub fn cast_shadows(&self, wait: bool, owner: (u64, u64), landed: impl FnOnce() + Send + 'static) -> CastShadows { - let RasterRenderer::ShadedRelief { band, azimuth, altitude, z_factor, shadow, .. } = self.style.renderer else { + let RasterRenderer::ShadedRelief { azimuth, altitude, shadow, .. } = self.style.renderer else { return CastShadows::None; }; - let finite = [azimuth, altitude, z_factor].iter().all(|v| v.is_finite()); - let Some(r) = self.data.as_ref().filter(|_| shadow > 0.0 && finite) else { return CastShadows::None }; + let finite = azimuth.is_finite() && altitude.is_finite(); + self.light_grid(Grid::Shadows { azimuth, altitude }, shadow > 0.0 && finite, wait, owner, landed) + } + + /// The share of the sky each cell sees (see [`crate::shadow::sky`]), when it is drawn as + /// Shaded Relief with a sky above 0: worked out once per pixels, band, Z factor and mask, then + /// kept, waited for and said as [`RasterLayer::cast_shadows`] has its light (each owner's + /// latest is kept, whatever its sun). + pub fn sky_view(&self, wait: bool, owner: (u64, u64), landed: impl FnOnce() + Send + 'static) -> CastShadows { + let RasterRenderer::ShadedRelief { sky, .. } = self.style.renderer else { + return CastShadows::None; + }; + self.light_grid(Grid::Sky, sky > 0.0, wait, owner, landed) + } + + /// The `grid` of [`RasterLayer::cast_shadows`] or [`RasterLayer::sky_view`], when `on`. + fn light_grid(&self, grid: Grid, on: bool, wait: bool, owner: (u64, u64), landed: impl FnOnce() + Send + 'static) -> CastShadows { + let RasterRenderer::ShadedRelief { band, z_factor, .. } = self.style.renderer else { + return CastShadows::None; + }; + let Some(r) = self.data.as_ref().filter(|_| on && z_factor.is_finite()) else { return CastShadows::None }; + let not_drawn = grid.not_drawn(); // Read in place, the layer holds an overview; the part in view is read as the map draws. if self.in_place && r.overview.is_some_and(|(w, _)| w > r.width) { - return CastShadows::Unavailable("its cast shadows aren't drawn: it is read in place, and only its overview is held".into()); + return CastShadows::Unavailable(format!("{not_drawn}: it is read in place, and only its overview is held")); } let cells = r.width as usize * r.height as usize; if cfg!(target_arch = "wasm32") && cells > crate::shadow::MAX_BROWSER_CELLS { return CastShadows::Unavailable(format!( - "its cast shadows aren't drawn: in the browser they are worked out for rasters of at most {} cells, and it has {cells}", + "{not_drawn}: in the browser it is worked out for rasters of at most {} cells, and it has {cells}", crate::shadow::MAX_BROWSER_CELLS )); } - let key = LightKey { band, azimuth, altitude, z_factor, mask: self.style.mask.clone() }; + let key = LightKey { band, grid, z_factor, mask: self.style.mask.clone() }; let mut start = None; let work = { let Ok(mut jobs) = self.cast.0.lock() else { return CastShadows::None }; // Grids of pixels no longer held go (their work stops as it is dropped), and so does // work stopped part-way (a cancelled run), to be started again. - jobs.retain(|j| j.pixels.strong_count() > 0 && !j.work.was_stopped()); + jobs.retain(|j| j.pixels.strong_count() > 0 && !j.work.was_stopped(j.key.grid.stopped())); let pixels = Arc::downgrade(r); let same = |j: &LightJob| j.key == key && Weak::ptr_eq(&j.pixels, &pixels); // The same owner's work for another sun, not done and not waited for, is superseded // (going back to a sun kept too): it stops, as it is dropped. Grids done are kept, for - // going back. - jobs.retain(|j| same(j) || j.owner != owner || j.work.is_done() || j.work.waited()); + // going back. Its other kind of grid (its sky, or its shadows) is left alone. + jobs.retain(|j| same(j) || !j.rivals(owner, &key) || j.work.is_done() || j.work.waited()); match jobs.iter().position(same) { Some(i) => { // The latest used last, so the oldest goes first. @@ -801,7 +822,7 @@ impl RasterLayer { let left: Vec = (0..jobs.len()) .filter(|i| { jobs.get(*i).is_some_and(|j| { - j.work.is_done() && !j.work.waited() && jobs.iter().skip(i + 1).any(|later| later.owner == j.owner) + j.work.is_done() && !j.work.waited() && jobs.iter().skip(i + 1).any(|later| later.rivals(j.owner, &j.key)) }) }) .collect(); @@ -826,12 +847,13 @@ impl RasterLayer { } } -/// [`RasterLayer::cast_shadows`]: what the map draws its cast shadows with. +/// [`RasterLayer::cast_shadows`] and [`RasterLayer::sky_view`]: what the map draws its cast +/// shadows, or its sky, with. #[derive(Clone, Debug, PartialEq)] pub enum CastShadows { - /// It casts none (another renderer, or a shadow of 0). + /// It casts none (another renderer, or a shadow or sky of 0). None, - /// The light left on each cell (0–255), row by row as the pixels are. + /// The light left on each cell, or the sky it sees (0–255), row by row as the pixels are. Ready(Arc>), /// Being worked out: drawn without, and again once it has landed. Pending, @@ -843,12 +865,36 @@ pub enum CastShadows { #[derive(Clone, Debug, PartialEq)] struct LightKey { band: usize, - azimuth: f64, - altitude: f64, + grid: Grid, z_factor: f64, mask: RasterMask, } +/// Which grid: the light cast shadows leave for a sun, or the sky seen. +#[derive(Clone, Copy, Debug, PartialEq)] +enum Grid { + Shadows { azimuth: f64, altitude: f64 }, + Sky, +} + +impl Grid { + /// What the relief is drawn without when there is no grid. + fn not_drawn(self) -> &'static str { + match self { + Grid::Shadows { .. } => "its cast shadows aren't drawn", + Grid::Sky => "its sky visibility isn't drawn", + } + } + + /// Why a grid stopped part-way has none: superseded, or its run cancelled. + fn stopped(self) -> &'static str { + match self { + Grid::Shadows { .. } => "its cast shadows were stopped", + Grid::Sky => "its sky visibility was stopped", + } + } +} + /// A light grid's work, shared with the thread doing it. #[derive(Default)] struct LightWork { @@ -889,9 +935,9 @@ impl LightWork { self.done.lock().is_ok_and(|d| d.is_some()) } - /// Whether it was stopped part-way, rather than done. - fn was_stopped(&self) -> bool { - self.done.lock().is_ok_and(|d| matches!(d.as_ref(), Some(Err(e)) if e == STOPPED)) + /// Whether it was stopped part-way, rather than done (`stopped` is why, see [`Grid::stopped`]). + fn was_stopped(&self, stopped: &str) -> bool { + self.done.lock().is_ok_and(|d| matches!(d.as_ref(), Some(Err(e)) if e == stopped)) } fn waited(&self) -> bool { @@ -954,6 +1000,13 @@ struct LightJob { work: Arc, } +impl LightJob { + /// Whether it is `owner`'s grid of the kind `key` is: a sun's light, or the sky. + fn rivals(&self, owner: (u64, u64), key: &LightKey) -> bool { + self.owner == owner && std::mem::discriminant(&self.key.grid) == std::mem::discriminant(&key.grid) + } +} + impl Drop for LightJob { /// Replaced, or its layer gone: the work for it stops. fn drop(&mut self) { @@ -965,11 +1018,8 @@ impl Drop for LightJob { #[derive(Clone, Default)] struct CastLight(Arc>>); -/// Why a light grid stopped part-way has none: superseded, or its run cancelled. -const STOPPED: &str = "its cast shadows were stopped"; - /// The most grids of suns their owners have left that a layer and its copies keep, besides -/// each owner's latest. +/// each owner's latest of each kind. const KEPT_LIGHTS: usize = 3; /// Held by [`hold_cast_shadows`]: light work waits for it before it traces. @@ -988,26 +1038,30 @@ fn start_light(r: Arc, key: LightKey, work: Arc) { let run = move || { drop(LIGHT_HOLD.read().unwrap_or_else(|e| e.into_inner())); let threads = rustgis_data::threads::max(); - let traced = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { - crate::shadow::light(&r, key.band, &key.mask, key.azimuth, key.altitude, key.z_factor, threads, &work.stop) + let traced = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| match key.grid { + Grid::Shadows { azimuth, altitude } => { + crate::shadow::light(&r, key.band, &key.mask, azimuth, altitude, key.z_factor, threads, &work.stop) + } + Grid::Sky => crate::shadow::sky(&r, key.band, &key.mask, key.z_factor, threads, &work.stop), })); + let not_drawn = key.grid.not_drawn(); match traced { // Stopped (superseded, or its export cancelled): a frame or page drawn while it was // to come is drawn again, and asks for what it draws now. Ok(Err(crate::shadow::Unlit::Stopped)) => { - work.finish(Err(STOPPED.into())); + work.finish(Err(key.grid.stopped().into())); work.land(); } Ok(out) => { work.finish(out.map(Arc::new).map_err(|e| match e { - crate::shadow::Unlit::Failed => "its cast shadows aren't drawn: working them out failed".into(), - _ => "its cast shadows aren't drawn: there isn't the memory for them".into(), + crate::shadow::Unlit::Failed => format!("{not_drawn}: working it out failed"), + _ => format!("{not_drawn}: there isn't the memory for it"), })); work.land(); } // Said, rather than leaving an export waiting. Err(_) => { - work.finish(Err("its cast shadows aren't drawn: working them out failed".into())); + work.finish(Err(format!("{not_drawn}: working it out failed"))); work.land(); } } @@ -1016,7 +1070,7 @@ fn start_light(r: Arc, key: LightKey, work: Arc) { { let run = std::sync::Arc::new(Mutex::new(Some(run))); let on_thread = run.clone(); - let spawned = std::thread::Builder::new().name("cast shadows".into()).spawn(move || { + let spawned = std::thread::Builder::new().name("relief light".into()).spawn(move || { if let Some(f) = on_thread.lock().ok().and_then(|mut f| f.take()) { f(); } @@ -1300,7 +1354,9 @@ pub enum RasterRenderer { /// The file's own colour table. Colormap, /// Hillshade, optionally tinted by elevation. `shadow` (0 to 1) is how dark the shadows the - /// terrain casts are drawn (see [`crate::shadow`]); 0, the default, casts none. + /// terrain casts are drawn (see [`crate::shadow`]); 0, the default, casts none. `sky` (0 to 1) + /// is how much the share of the sky each cell sees scales its ambient light + /// ([`crate::shadow::sky`]); 0, the default, leaves it whole. ShadedRelief { band: usize, ramp: Option, @@ -1309,6 +1365,8 @@ pub enum RasterRenderer { z_factor: f64, #[serde(default, skip_serializing_if = "is_no_shadow")] shadow: f64, + #[serde(default, skip_serializing_if = "is_no_shadow")] + sky: f64, }, } @@ -1855,10 +1913,16 @@ impl<'a> Colorizer<'a> { /// The colour of a pixel from its renderer band values (`NaN` = no data), or `None` for /// transparent. `hillshade` (0–255) is used by Shaded Relief. pub fn color(&self, vals: &[f32], hillshade: Option) -> Option<[u8; 4]> { + self.color_lit(vals, hillshade, None) + } + + /// [`Colorizer::color`], with Shaded Relief's ambient light scaled by `ambient` (0–255, the + /// share of it the sky a cell sees leaves, see [`RasterRenderer::ShadedRelief`]'s `sky`). + pub fn color_lit(&self, vals: &[f32], hillshade: Option, ambient: Option) -> Option<[u8; 4]> { // The renderer masks values as it samples them (they arrive as NaN); this is for other callers. let mask = &self.style.mask; if mask.is_empty() || !vals.iter().any(|v| mask.hides(*v)) { - return self.paint(vals, hillshade); + return self.paint(vals, hillshade, ambient); } // Masked values are no data. The renderer bands are at most three. let mut kept = [f32::NAN; 3]; @@ -1867,10 +1931,10 @@ impl<'a> Colorizer<'a> { *k = *v; } } - self.paint(kept.get(..vals.len().min(3)).unwrap_or(&kept), hillshade) + self.paint(kept.get(..vals.len().min(3)).unwrap_or(&kept), hillshade, ambient) } - fn paint(&self, vals: &[f32], hillshade: Option) -> Option<[u8; 4]> { + fn paint(&self, vals: &[f32], hillshade: Option, ambient: Option) -> Option<[u8; 4]> { let v0 = *vals.first()?; let c = match &self.style.renderer { RasterRenderer::Rgb { .. } => { @@ -1900,8 +1964,10 @@ impl<'a> Colorizer<'a> { RasterRenderer::ShadedRelief { ramp, .. } => { let h = hillshade.unwrap_or(180) as f64 / 255.0; let base = if ramp.is_some() { *self.ramp_lut.get(self.byte(0, v0) as usize)? } else { [255, 255, 255, 255] }; - // Multiply the tint by the hillshade, keeping some ambient light. - let m = |c: u8| (c as f64 * (0.25 + 0.75 * h)).round().clamp(0.0, 255.0) as u8; + // Multiply the tint by the hillshade, keeping some ambient light (less of it where + // the terrain hides part of the sky). + let a = 0.25 * ambient.map_or(1.0, |a| f64::from(a) / 255.0); + let m = |c: u8| (c as f64 * (a + 0.75 * h)).round().clamp(0.0, 255.0) as u8; [m(base[0]), m(base[1]), m(base[2]), 255] } }; @@ -2680,7 +2746,8 @@ mod tests { let data: Vec = (0..400).map(|i| if i == 10 * 20 + 10 { 900.0 } else { 0.0 }).collect(); let r = Raster::new("tower", 20, 20, vec![data], gt, Crs::WebMercator, None, ColorHint::Gray, "Float32").unwrap(); let mut rl = RasterLayer::from_path(r, "tower.tif"); - rl.style.renderer = RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 90.0, altitude: 30.0, z_factor: 1.0, shadow }; + rl.style.renderer = + RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 90.0, altitude: 30.0, z_factor: 1.0, shadow, sky: 0.0 }; rl } @@ -2739,14 +2806,22 @@ mod tests { let mut rl = RasterLayer::from_path(r, "hills.tif"); let lit = |rl: &mut RasterLayer, az: f64| { rl.style.renderer = - RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: az, altitude: 10.0, z_factor: 1.0, shadow: 1.0 }; + RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: az, altitude: 10.0, z_factor: 1.0, shadow: 1.0, sky: 0.0 }; }; let other = |az: f64, rl: &RasterLayer| { let mut c = rl.clone(); lit(&mut c, az); c }; - let work_of = |rl: &RasterLayer, az: f64| rl.cast.0.lock().unwrap().iter().find(|j| j.key.azimuth == az).map(|j| j.work.clone()); + let work_of = |rl: &RasterLayer, az: f64| { + rl.cast + .0 + .lock() + .unwrap() + .iter() + .find(|j| matches!(j.key.grid, Grid::Shadows { azimuth, .. } if azimuth == az)) + .map(|j| j.work.clone()) + }; lit(&mut rl, 10.0); rl.cast_shadows(false, (0, 1), || {}); let copy = other(20.0, &rl); @@ -2790,7 +2865,8 @@ mod tests { let data: Vec = (0..2000 * 2000).map(|i| ((i % 2000) as f32 * 0.01) + ((i / 2000) as f32 * 0.01)).collect(); let r = Raster::new("plain", 2000, 2000, vec![data], gt, Crs::WebMercator, None, ColorHint::Gray, "Float32").unwrap(); let mut rl = RasterLayer::from_path(r, "plain.tif"); - rl.style.renderer = RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 135.0, altitude: 2.0, z_factor: 1.0, shadow: 1.0 }; + rl.style.renderer = + RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 135.0, altitude: 2.0, z_factor: 1.0, shadow: 1.0, sky: 0.0 }; let (sent, landings) = std::sync::mpsc::channel::<()>(); { let _cancelled = rustgis_data::progress::install(Some(std::sync::Arc::new(Cancelled))); @@ -2803,7 +2879,7 @@ mod tests { // The worker stops and says so (the map draws again), with no grid. assert!(landings.recv_timeout(std::time::Duration::from_secs(60)).is_ok(), "landed"); let work = rl.cast.0.lock().unwrap().first().map(|j| j.work.clone()).unwrap(); - assert!(work.was_stopped()); + assert!(work.was_stopped(Grid::Shadows { azimuth: 0.0, altitude: 0.0 }.stopped())); // Asked again, it starts over. rl.cast_shadows(false, (0, 1), || {}); let again = rl.cast.0.lock().unwrap().first().map(|j| j.work.clone()).unwrap(); @@ -2818,7 +2894,8 @@ mod tests { let data: Vec = (0..800 * 800).map(|i| ((i % 800) as f32 * 0.01) + ((i / 800) as f32 * 0.01)).collect(); let r = Raster::new("plain", 800, 800, vec![data], gt, Crs::WebMercator, None, ColorHint::Gray, "Float32").unwrap(); let mut rl = RasterLayer::from_path(r, "plain.tif"); - rl.style.renderer = RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 135.0, altitude: 3.0, z_factor: 1.0, shadow: 1.0 }; + rl.style.renderer = + RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 135.0, altitude: 3.0, z_factor: 1.0, shadow: 1.0, sky: 0.0 }; let (sent, landings) = std::sync::mpsc::channel::<&'static str>(); let told = |who: &'static str| { let tx = sent.clone(); @@ -2852,4 +2929,40 @@ mod tests { let CastShadows::Unavailable(why) = rl.cast_shadows(true, (0, 1), || {}) else { panic!("said") }; assert!(why.contains("read in place"), "{why}"); } + + /// The sky is worked out once whatever the sun, beside the owner's shadows (neither stops the + /// other's work); read in place with only its overview held, it is drawn without, and said. + #[test] + fn the_sky_is_kept_whatever_the_sun() { + let mut rl = tower(1.0); + assert_eq!(rl.sky_view(true, (0, 1), || {}), CastShadows::None, "a sky of 0 has none"); + if let RasterRenderer::ShadedRelief { sky, .. } = &mut rl.style.renderer { + *sky = 1.0; + } + let hold = hold_cast_shadows(); + assert_eq!(rl.sky_view(false, (0, 1), || {}), CastShadows::Pending); + assert_eq!(rl.cast_shadows(false, (0, 1), || {}), CastShadows::Pending); + { + let jobs = rl.cast.0.lock().unwrap(); + assert_eq!(jobs.len(), 2, "both kept"); + assert!(jobs.iter().all(|j| !j.work.stop.load(std::sync::atomic::Ordering::SeqCst)), "neither stopped"); + } + drop(hold); + let CastShadows::Ready(sky) = rl.sky_view(true, (0, 1), || {}) else { panic!("waited for") }; + assert!(matches!(rl.cast_shadows(true, (0, 1), || {}), CastShadows::Ready(_))); + // Beside the tower, the directions toward it are hidden (about 3 of 16); its top sees all. + assert!((160..240).contains(&sky[10 * 20 + 9]), "{}", sky[10 * 20 + 9]); + assert_eq!(sky[10 * 20 + 10], 255); + // Another sun, or another strength: the same sky. + if let RasterRenderer::ShadedRelief { azimuth, sky, .. } = &mut rl.style.renderer { + (*azimuth, *sky) = (200.0, 0.4); + } + assert!(matches!(rl.sky_view(false, (0, 1), || {}), CastShadows::Ready(g) if Arc::ptr_eq(&g, &sky))); + let mut r = (**rl.data.as_ref().unwrap()).clone(); + r.overview = Some((80, 80)); + rl.in_place = true; + rl.data = Some(Arc::new(r)); + let CastShadows::Unavailable(why) = rl.sky_view(true, (0, 1), || {}) else { panic!("said") }; + assert!(why.contains("sky visibility") && why.contains("read in place"), "{why}"); + } } diff --git a/crates/map/src/shadow.rs b/crates/map/src/shadow.rs index e30eb07195..d17fa1e3e4 100644 --- a/crates/map/src/shadow.rs +++ b/crates/map/src/shadow.rs @@ -1,5 +1,6 @@ -//! Cast shadows for Shaded Relief (opengeos/RustGIS#1782): how much of the sun each cell of an -//! elevation raster sees over the terrain between it and the sun. +//! Cast shadows and sky visibility for Shaded Relief (opengeos/RustGIS#1782): how much of the sun +//! each cell of an elevation raster sees over the terrain between it and the sun ([`light`]), and +//! how much of the sky it sees over the terrain around it ([`sky`]). //! //! The sun is a disc [`SOFT_EDGE`] degrees either side of its altitude, so a shadow has a soft //! edge. Each cell traces the terrain toward the sun's azimuth one column or row at a time, @@ -7,7 +8,9 @@ //! suns, franciscoparrao/surtgis#153), and keeps the steepest angle up to the terrain it passes. //! The share of the disc above that angle is the light the cell keeps. The trace stops once //! nothing farther can change it: the terrain left can't rise above the angle kept (or the disc's -//! lower edge), or the disc is already hidden. Cells outside the raster cast no shadow on it. +//! lower edge), or the disc is already hidden. The sky is traced likewise, in [`SKY_DIRECTIONS`] +//! directions, reading the two cells either side of each step. Cells outside the raster cast no shadow on it, and hide none of +//! its sky. use std::sync::atomic::{AtomicBool, Ordering}; @@ -19,14 +22,14 @@ use crate::raster::RasterMask; /// real sun's quarter of a degree, for a soft edge at map scale. pub const SOFT_EDGE: f64 = 5.0; -/// The most cells a raster's shadows are worked out for in the browser, which has one thread: past -/// it, the relief is drawn without them, and said. +/// The most cells a raster's shadows, or its sky, are worked out for in the browser, which has one +/// thread: past it, the relief is drawn without them, and said. pub const MAX_BROWSER_CELLS: usize = 500_000; /// Cells below which the work stays on one thread: starting threads costs more. const ONE_THREAD_BELOW: usize = 64 * 1024; -/// Why there is no light grid. +/// Why there is no light or sky grid. #[derive(Clone, Debug, PartialEq, Eq)] pub enum Unlit { /// `stop` was set: a newer sun or raster replaced it. @@ -54,31 +57,136 @@ pub fn light( z_factor: f64, threads: usize, stop: &AtomicBool, +) -> Result, Unlit> { + let sun = Sun::new(r, azimuth, altitude); + each_row(r, band, mask, z_factor, threads, stop, |trace, row, out| { + for (col, cell) in out.iter_mut().enumerate() { + if let Some(z0) = trace.at(col, row) { + *cell = sun.lit(trace.horizon(&sun.way, (col, row, z0), sun.low, sun.high)); + } + } + }) +} + +/// The directions, evenly spread from north, a cell's horizon is looked for in, for its sky. +pub const SKY_DIRECTIONS: usize = 16; + +/// How far a cell's horizon is looked for, in cells (the longer side of one): terrain farther +/// hides none of its sky. +pub const SKY_REACH: f64 = 100.0; + +/// The sky's trace takes one step (a row or a column) at a time up to 31, then the steps taken +/// over this many (2 at 32, 6 at 96): half the reads, the horizon found to within a sixteenth of +/// its distance, and a peak narrower than that far away can be missed. +pub const SKY_SPREAD: usize = 16; + +/// The share of the sky each cell of `band` sees (`0` none to `255` all of it, as open flat ground +/// does), row by row as the raster's pixels are, heights scaled by `z_factor`: the share of the +/// half sphere above its horizon, `1 − mean(sin γ)` over [`SKY_DIRECTIONS`] directions, `γ` being +/// the angle up to the horizon in each (0 where the terrain falls away, so a peak sees the whole +/// sky), looked for up to [`SKY_REACH`] cells away, in steps [`SKY_SPREAD`] spreads. Heights are +/// read from the two cells either side of each step, the nearer one where the other is no data; +/// cells that are no data or masked see the whole sky and hide none of it. Threads, `stop` and +/// Cancel are as [`light`] has them. +/// +/// This is the sky-view factor of Zakšek, Oštir and Kokalj, "Sky-View Factor as a Relief +/// Visualization Technique", Remote Sensing 3(2), 398–415, 2011 (doi:10.3390/rs3020398), its +/// horizon traced here; no other code was followed. +pub fn sky(r: &Raster, band: usize, mask: &RasterMask, z_factor: f64, threads: usize, stop: &AtomicBool) -> Result, Unlit> { + let (cx, cy) = r.cell_size_m(); + let reach = SKY_REACH * cx.max(cy); + let longest = r.width.max(r.height) as usize; + let ways: Vec> = + (0..SKY_DIRECTIONS).map(|i| reads(&Way::new(r, i as f64 * 360.0 / SKY_DIRECTIONS as f64), reach, longest)).collect(); + each_row(r, band, mask, z_factor, threads, stop, |trace, row, out| { + for (col, cell) in out.iter_mut().enumerate() { + let Some(z0) = trace.at(col, row) else { continue }; + let hidden: f64 = ways + .iter() + .map(|way| { + let t = trace.skyline(way, col, row, z0); + // sin(atan t) + t / (1.0 + t * t).sqrt() + }) + .sum(); + *cell = ((1.0 - hidden / ways.len() as f64).clamp(0.0, 1.0) * 255.0).round() as u8; + } + }) +} + +/// One read of a cell's sky trace, the same for every cell: the cell before the point read, in +/// columns and rows from the cell traced; the next one's weight, along the row (`along_row`) or +/// down the column (each step of a trace is on one or the other); 1 / the distance in metres. +struct Read { + dx: isize, + dy: isize, + weight: f64, + along_row: bool, + per_metre: f64, +} + +/// The reads of a sky trace along `way`, up to `reach` metres and at most `steps` steps (no trace +/// goes past the raster's longer side, whatever its cells' size says): one step at a time, then +/// steps of the steps taken over [`SKY_SPREAD`]. +fn reads(way: &Way, reach: f64, steps: usize) -> Vec { + let mut out = Vec::new(); + let mut n = 1usize; + while n <= steps && n as f64 * way.metres <= reach { + let k = n as f64; + let (x, y) = (way.step.0 * k, -way.step.1 * k); + let (fx, fy) = (x - x.floor(), y - y.floor()); + out.push(Read { + dx: x.floor() as isize, + dy: y.floor() as isize, + weight: if fy == 0.0 { fx } else { fy }, + along_row: fy == 0.0, + per_metre: 1.0 / (k * way.metres), + }); + n += (n / SKY_SPREAD).max(1); + } + out +} + +/// A grid of `255` the size of `r`, each row filled by `row` (the trace, the row, its cells), +/// rows split over up to `threads` threads; filled no further when the band isn't there or every +/// cell is no data or masked. Looks at `stop` and the run's Cancel between rows (see [`light`]). +fn each_row( + r: &Raster, + band: usize, + mask: &RasterMask, + z_factor: f64, + threads: usize, + stop: &AtomicBool, + row: impl Fn(&Trace, usize, &mut [u8]) + Sync, ) -> Result, Unlit> { let (w, h) = (r.width as usize, r.height as usize); let cells = w.checked_mul(h).ok_or(Unlit::NoMemory)?; let mut out = Vec::new(); out.try_reserve_exact(cells).map_err(|_| Unlit::NoMemory)?; out.resize(cells, 255u8); - let Some(data) = r.bands.get(band).map(|b| b.data.as_slice()).filter(|d| d.len() >= cells) else { + let Some(data) = r.bands.get(band).and_then(|b| b.data.get(..cells)) else { return Ok(out); }; - let height = |v: f32| (r.is_valid(v) && !mask.hides(v)).then(|| f64::from(v) * z_factor); - let top = data.iter().filter_map(|v| height(*v)).fold(f64::NEG_INFINITY, f64::max); - if !top.is_finite() || w == 0 { + // The heights read, NaN where the cell is no data or masked: checked once per cell here, not + // at each of the many reads a cell's trace makes (4 bytes a cell while it is worked out). + let mut heights = Vec::new(); + heights.try_reserve_exact(cells).map_err(|_| Unlit::NoMemory)?; + heights.extend(data.iter().map(|v| if r.is_valid(*v) && !mask.hides(*v) { *v } else { f32::NAN })); + let mut trace = Trace { heights: &heights, w, h, top: f64::NEG_INFINITY, z_factor }; + trace.top = (0..cells).filter_map(|i| trace.height(i)).fold(f64::NEG_INFINITY, f64::max); + if !trace.top.is_finite() || w == 0 { return Ok(out); } - let sun = Sun::new(r, azimuth, altitude); - let trace = Trace { data, w, h, top, sun: &sun, height: &height }; + let trace = &trace; let threads = if cells < ONE_THREAD_BELOW { 1 } else { threads.clamp(1, h) }; let rows = h.div_ceil(threads); let chunks: Vec<(usize, &mut [u8])> = out.chunks_mut(rows * w).enumerate().collect(); let run = |(i, chunk): (usize, &mut [u8])| -> Result<(), Unlit> { - for (dy, row) in chunk.chunks_mut(w).enumerate() { + for (dy, cells) in chunk.chunks_mut(w).enumerate() { if stop.load(Ordering::Relaxed) || rustgis_data::progress::check().is_err() { return Err(Unlit::Stopped); } - trace.row(i * rows + dy, row); + row(trace, i * rows + dy, cells); } Ok(()) }; @@ -112,20 +220,17 @@ fn take(slot: &std::sync::Mutex>) -> Option { slot.lock().ok().and_then(|mut c| c.take()) } -/// The sun as each cell's trace needs it. -struct Sun { - /// A step toward the sun, in columns and rows: one column or one row, whichever is more. +/// A direction a cell's trace takes. +struct Way { + /// A step along it, in columns and rows: one column or one row, whichever is more. step: (f64, f64), /// That step's length on the ground, in metres. metres: f64, - /// The tangents of the disc's lower and upper edges. - low: f64, - high: f64, - altitude: f64, } -impl Sun { - fn new(r: &Raster, azimuth: f64, altitude: f64) -> Self { +impl Way { + /// Toward `azimuth`, degrees clockwise from north. + fn new(r: &Raster, azimuth: f64) -> Self { let (cx, cy) = r.cell_size_m(); let (cx, cy) = (cx.max(1e-9), cy.max(1e-9)); let (sin, cos) = azimuth.to_radians().sin_cos(); @@ -136,9 +241,24 @@ impl Sun { let longest = east.abs().max(north.abs()).max(1e-12); let step = (east / longest, north / longest); let metres = ((step.0 * cx).powi(2) + (step.1 * cy).powi(2)).sqrt(); + Way { step, metres } + } +} + +/// The sun as each cell's trace needs it. +struct Sun { + way: Way, + /// The tangents of the disc's lower and upper edges. + low: f64, + high: f64, + altitude: f64, +} + +impl Sun { + fn new(r: &Raster, azimuth: f64, altitude: f64) -> Self { let altitude = altitude.clamp(0.0, 90.0); let edge = |a: f64| a.clamp(-89.9, 89.9).to_radians().tan(); - Sun { step, metres, low: edge(altitude - SOFT_EDGE), high: edge(altitude + SOFT_EDGE), altitude } + Sun { way: Way::new(r, azimuth), low: edge(altitude - SOFT_EDGE), high: edge(altitude + SOFT_EDGE), altitude } } /// The share of the disc above the steepest tangent `t` seen toward it, 0 to 255. @@ -155,27 +275,32 @@ impl Sun { } } -/// One raster's trace toward the sun. -struct Trace<'a, H: Fn(f32) -> Option> { - data: &'a [f32], +/// One raster's heights, as each cell's trace reads them. +struct Trace<'a> { + /// Row by row, NaN where the cell is no data or masked. + heights: &'a [f32], w: usize, h: usize, /// The highest cell, scaled. top: f64, - sun: &'a Sun, - /// A cell's height, scaled, unless it is no data or masked. - height: &'a H, + z_factor: f64, } -impl Option> Trace<'_, H> { +impl Trace<'_> { + /// The height of the cell at index `i`, scaled, unless it is no data or masked. + fn height(&self, i: usize) -> Option { + self.heights.get(i).filter(|v| !v.is_nan()).map(|v| f64::from(*v) * self.z_factor) + } + fn at(&self, col: usize, row: usize) -> Option { - (self.height)(*self.data.get(row * self.w + col)?) + self.height(row * self.w + col) } /// The height between cell centres at column `x` and row `y` (fractional, inside the grid), /// bilinear between the four around it; with one of them missing, the nearest one. fn between(&self, x: f64, y: f64) -> Option { - let (i, j) = (x.floor() as usize, y.floor() as usize); + // Truncating is flooring here (both are at least 0), and much cheaper. + let (i, j) = (x as usize, y as usize); let (fx, fy) = (x - i as f64, y - j as f64); let (i1, j1) = ((i + 1).min(self.w - 1), (j + 1).min(self.h - 1)); match (self.at(i, j), self.at(i1, j), self.at(i, j1), self.at(i1, j1)) { @@ -188,32 +313,64 @@ impl Option> Trace<'_, H> { } } - /// Fills `out` with the light left on each cell of row `row`. - fn row(&self, row: usize, out: &mut [u8]) { - let (sx, sy) = self.sun.step; + /// The steepest tangent up to the terrain seen along `way` from the cell at column `col`, row + /// `row` and height `z0` (`-∞` with nothing seen). The trace stops once nothing farther can + /// change what it is for: the terrain left can't rise above the tangent kept or `floor`, or the + /// tangent is already `ceiling`. + fn horizon(&self, way: &Way, (col, row, z0): (usize, usize, f64), floor: f64, ceiling: f64) -> f64 { + let (sx, sy) = way.step; let (wmax, hmax) = ((self.w - 1) as f64, (self.h - 1) as f64); - for (col, cell) in out.iter_mut().enumerate() { - let Some(z0) = self.at(col, row) else { continue }; - let mut steepest = f64::NEG_INFINITY; - let mut k = 1.0; - loop { - let (x, y) = (col as f64 + sx * k, row as f64 - sy * k); - if !(0.0..=wmax).contains(&x) || !(0.0..=hmax).contains(&y) { - break; - } - let d = k * self.sun.metres; - if let Some(z) = self.between(x, y) { - steepest = steepest.max((z - z0) / d); - } - // Nothing farther rises above what is kept: the angle seen, or the disc's lower - // edge (below it the cell has the whole sun); or the whole disc is hidden. - if (self.top - z0) / d <= steepest.max(self.sun.low) || steepest >= self.sun.high { - break; + let mut steepest = f64::NEG_INFINITY; + let mut k = 1.0; + loop { + let (x, y) = (col as f64 + sx * k, row as f64 - sy * k); + let d = k * way.metres; + if !(0.0..=wmax).contains(&x) || !(0.0..=hmax).contains(&y) { + break; + } + if let Some(z) = self.between(x, y) { + steepest = steepest.max((z - z0) / d); + } + if (self.top - z0) / d <= steepest.max(floor) || steepest >= ceiling { + break; + } + k += 1.0; + } + steepest + } + + /// The steepest tangent, at least 0, up to the terrain seen from the cell at column `col`, row + /// `row` and height `z0` through `reads` (see [`reads`]): [`Trace::horizon`] for the sky, its + /// reads worked out once for every cell. The trace stops at the raster's edge, or once the + /// terrain left can't rise above the tangent kept. + fn skyline(&self, reads: &[Read], col: usize, row: usize, z0: f64) -> f64 { + let (w, h) = (self.w as isize, self.h as isize); + let mut steepest = 0.0f64; + for read in reads { + let (i, j) = (col as isize + read.dx, row as isize + read.dy); + let next = isize::from(read.weight > 0.0); + let (i1, j1) = if read.along_row { (i + next, j) } else { (i, j + next) }; + if i < 0 || j < 0 || i1 >= w || j1 >= h { + break; + } + let z = match (self.at(i as usize, j as usize), self.at(i1 as usize, j1 as usize)) { + (Some(a), Some(b)) => Some(a + (b - a) * read.weight), + (a, b) => { + if read.weight < 0.5 { + a + } else { + b + } } - k += 1.0; + }; + if let Some(z) = z { + steepest = steepest.max((z - z0) * read.per_metre); + } + if (self.top - z0) * read.per_metre <= steepest { + break; } - *cell = self.sun.lit(steepest); } + steepest } } @@ -242,6 +399,10 @@ mod tests { light(r, 0, &RasterMask::default(), azimuth, altitude, 1.0, 4, &AtomicBool::new(false)).unwrap_or_else(|e| panic!("{e:?}")) } + fn sky_of(r: &Raster, threads: usize) -> Vec { + sky(r, 0, &RasterMask::default(), 1.0, threads, &AtomicBool::new(false)).unwrap_or_else(|e| panic!("{e:?}")) + } + /// A plane rising toward the sun shades itself only when it is steeper than the sun is high /// (past the soft edge): the same at every azimuth, the diagonals too (surtgis#153). #[test] @@ -300,19 +461,27 @@ mod tests { let flat = dem(300, 300, |_, _| 100.0); let stop = AtomicBool::new(true); assert_eq!(light(&flat, 0, &RasterMask::default(), 315.0, 30.0, 1.0, 4, &stop), Err(Unlit::Stopped)); + assert_eq!(sky(&flat, 0, &RasterMask::default(), 1.0, 4, &stop), Err(Unlit::Stopped)); assert!(lit(&flat, 315.0, 30.0).iter().all(|v| *v == 255)); - // A tower masked out casts no shadow. + assert!(sky_of(&flat, 4).iter().all(|v| *v == 255), "flat ground sees the whole sky"); + // A tower masked out casts no shadow and hides no sky. let tower = dem(40, 40, |e, n| if (e / CELL).round() == 20.0 && (n / CELL).round() == -20.0 { 1000.0 } else { 0.0 }); assert!(lit(&tower, 90.0, 30.0).contains(&0)); + assert!(sky_of(&tower, 1).iter().any(|v| *v < 255)); + assert_eq!(sky_of(&tower, 1)[20 * 40 + 20], 255, "its top sees the whole sky"); let masked = RasterMask { values: vec![1000.0], ..RasterMask::default() }; let g = light(&tower, 0, &masked, 90.0, 30.0, 1.0, 1, &AtomicBool::new(false)).unwrap_or_default(); assert!(g.iter().all(|v| *v == 255)); + let g = sky(&tower, 0, &masked, 1.0, 1, &AtomicBool::new(false)).unwrap_or_default(); + assert!(g.iter().all(|v| *v == 255)); // A band that isn't there leaves every cell lit. let g = light(&tower, 3, &RasterMask::default(), 90.0, 30.0, 1.0, 1, &AtomicBool::new(false)).unwrap_or_default(); assert!(g.iter().all(|v| *v == 255)); + let g = sky(&tower, 3, &RasterMask::default(), 1.0, 1, &AtomicBool::new(false)).unwrap_or_default(); + assert!(g.iter().all(|v| *v == 255)); } - /// The grid is the same on one thread as on several, bit for bit. + /// The grids are the same on one thread as on several, bit for bit. #[test] fn threads_give_the_same_grid() { let r = dem(300, 300, |e, n| 400.0 * ((e / 900.0).sin() * (n / 700.0).cos()) + e * 0.05); @@ -320,5 +489,80 @@ mod tests { let many = light(&r, 0, &RasterMask::default(), 315.0, 25.0, 1.0, 7, &AtomicBool::new(false)); assert_eq!(one, many); assert!(one.unwrap_or_default().iter().any(|v| *v < 255)); + let one = sky_of(&r, 1); + assert_eq!(one, sky_of(&r, 7)); + assert!(one.iter().any(|v| *v < 200)); + } + + /// The bottom of a round pit sees the sky its depth gives: in every direction, the horizon is + /// the rim, `depth / radius` up, to within the cell the rim is drawn to; the sky is the share + /// of the half sphere above that, `1 − sin γ`. + #[test] + fn a_pit_has_the_sky_its_depth_gives() { + const RADIUS: f64 = 50.0 * CELL; + let (n, mid) = (121, 60.0 * CELL); + for depth in [0.25 * RADIUS, RADIUS, 2.0 * RADIUS] { + let r = dem(n, n, |e, s| if (e - mid).hypot(s + mid) <= RADIUS { -depth } else { 0.0 }); + let trace = Trace { heights: &r.bands[0].data, w: n as usize, h: n as usize, top: 0.0, z_factor: 1.0 }; + let reach = SKY_REACH * CELL; + // The rim is between the last cell inside and the first outside: the horizon is there, + // or a step farther (3 cells at 50), where the trace reads the ground outside whole. + let (near, far) = (depth / (RADIUS - 0.1 * CELL), depth / (RADIUS + 5.0 * CELL)); + for azimuth in (0..72).map(|i| f64::from(i) * 5.0) { + let t = trace.skyline(&reads(&Way::new(&r, azimuth), reach, n as usize), 60, 60, -depth); + assert!((far..=near).contains(&t), "depth {depth}, azimuth {azimuth}: {t} not in {far}..{near}"); + } + let sin = |t: f64| t / (1.0 + t * t).sqrt(); + let (most, least) = ((1.0 - sin(far)) * 255.0, (1.0 - sin(near)) * 255.0); + let seen = f64::from(sky_of(&r, 1)[60 * n as usize + 60]); + assert!((least.floor()..=most.ceil()).contains(&seen), "depth {depth}: {seen} not in {least}..{most}"); + } + } + + /// Cells too large for their size to be a number of metres (1e308 m, past `f64` once + /// squared) give no endless trace: the reads stop at the raster's side, and the sky is seen. + #[test] + fn huge_cells_end_the_sky_trace() { + let gt = GeoTransform { origin_x: 0.0, origin_y: 0.0, pixel_width: 1e308, pixel_height: -1e308 }; + let data: Vec = (0..400).map(|i| if i == 210 { 900.0 } else { 0.0 }).collect(); + let r = Raster::new( + "huge", + 20, + 20, + vec![data], + gt, + Crs::from_epsg(31983).unwrap_or(Crs::WebMercator), + None, + ColorHint::Gray, + "Float32", + ) + .unwrap_or_else(|e| panic!("{e}")); + let way = Way::new(&r, 30.0); + assert!(reads(&way, f64::INFINITY, 20).len() <= 20); + assert!(sky_of(&r, 1).iter().all(|v| *v == 255), "the sky is seen whole"); + } + + /// A tilted plane hides the sky uphill by its slope in each direction, and none of it + /// downhill: exact at every one of the directions (heights between cells are exact on a plane). + #[test] + fn a_planes_sky_is_its_slope_in_each_direction() { + let slope = f64::to_radians(30.0).tan(); + for uphill in [0.0, 20.0, 45.0, 110.0, 200.0, 315.0] { + let (sin, cos) = f64::to_radians(uphill).sin_cos(); + let r = dem(60, 60, |e, n| (e * sin + n * cos) * slope); + let hidden: f64 = (0..SKY_DIRECTIONS) + .map(|i| { + let t = (slope * f64::to_radians(i as f64 * 360.0 / SKY_DIRECTIONS as f64 - uphill).cos()).max(0.0); + t / (1.0 + t * t).sqrt() + }) + .sum(); + let want = ((1.0 - hidden / SKY_DIRECTIONS as f64) * 255.0).round() as u8; + let g = sky_of(&r, 1); + for row in 25..35 { + for col in 25..35 { + assert_eq!(g[row * 60 + col], want, "uphill {uphill}, cell {col},{row}"); + } + } + } } } diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index ed7f0c54e7..0a3d06d90c 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -3680,6 +3680,7 @@ mod tests { altitude: 30.0, z_factor: 1.0, shadow, + sky: 0.0, }; for shadow in [0.0, 1.0] { let map = ramp_map(relief(shadow), rustgis_map::raster::Stretch::MinMax); @@ -3728,6 +3729,7 @@ mod tests { altitude: 10.0, z_factor: 1.0, shadow: 1.0, + sky: 0.0, }; let mut m = Map::new(name.clone()); assert_eq!(m.add_raster("dem", copy), 1); diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 4d14071d9d..209901b301 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -4051,10 +4051,11 @@ fn render_frame( }; let stats = rl.masked_stats(); let stats = stats.as_ref().map(|s| s.as_slice()); - // Its cast shadows, never waited for here (an export waits before it draws, see - // `complete`): drawn without them meanwhile, and again once they land. A map's frame - // says so as tiles to come; a page's, as shadows to come. - let light = match rl.cast_shadows(false, (map_instance, layer.instance_id()), shadows_landed) { + // Its cast shadows and sky, never waited for here (an export waits before it draws, + // see `complete`): drawn without them meanwhile, and again once they land. A map's + // frame says so as tiles to come; a page's, as shadows to come. + let owner = (map_instance, layer.instance_id()); + let mut lit = |got: rustgis_map::raster::CastShadows| match got { rustgis_map::raster::CastShadows::Ready(grid) => Some(grid), rustgis_map::raster::CastShadows::Pending => { if full_res_rasters { @@ -4080,7 +4081,8 @@ fn render_frame( } rustgis_map::raster::CastShadows::None => None, }; - let light = light.as_deref().map(Vec::as_slice); + let (shadow, sky) = (lit(rl.cast_shadows(false, owner, shadows_landed)), lit(rl.sky_view(false, owner, shadows_landed))); + let light = raster::Lights { shadow: shadow.as_deref().map(Vec::as_slice), sky: sky.as_deref().map(Vec::as_slice) }; // Read in place: the part in view, at the level the view needs. let address = rl.path.as_deref().filter(|_| rl.in_place); let mut draw_it = |pm: &mut Pixmap, opacity: f32| match address { @@ -6452,7 +6454,7 @@ pub(crate) fn frame_with_shadows_to_come(f: impl FnOnce() -> T) -> (T, bool) (out, to_come) } -/// Waits for the cast shadows of the map's Shaded Relief layers drawn in `view` (visible, in +/// Waits for the cast shadows and sky of the map's Shaded Relief layers drawn in `view` (visible, in /// their and their groups' scale range at the view's centre or, tilted, at one of its bands, as /// `render_pass` draws them), as an export does (it runs off the interface's thread); the wait /// gives up when the run is cancelled. @@ -6463,7 +6465,9 @@ fn wait_for_cast_shadows(map: &Map, view: &View) { let scales = drawn_scales(map, &camera, view); for layer in map.layers.iter().filter(|l| scales.iter().any(|s| map.draws_at(l, *s))) { if let Some(rl) = &layer.raster { - rl.cast_shadows(true, (map.instance_id(), layer.instance_id()), shadows_landed); + let owner = (map.instance_id(), layer.instance_id()); + rl.cast_shadows(true, owner, shadows_landed); + rl.sky_view(true, owner, shadows_landed); } } } @@ -6645,7 +6649,7 @@ pub fn render_placed(map: &mut Map, view: &View) -> Result { let mut gaps = shadows; if to_come { gaps.push( - "made without the cast shadows still being worked out, as the window doesn't wait for them: once the map shows them, draw or export it again" + "made without the cast shadows or sky visibility still being worked out, as the window doesn't wait for them: once the map shows them, draw or export it again" .to_string(), ); } @@ -8763,8 +8767,15 @@ mod tests { let lit = |m: &mut Map, shadow: f64| { let l = m.layer_mut(id).unwrap(); l.visible = true; - l.raster.as_mut().unwrap().style.renderer = - rustgis_map::RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 315.0, altitude: 5.0, z_factor: 1.0, shadow }; + l.raster.as_mut().unwrap().style.renderer = rustgis_map::RasterRenderer::ShadedRelief { + band: 0, + ramp: None, + azimuth: 315.0, + altitude: 5.0, + z_factor: 1.0, + shadow, + sky: 0.0, + }; }; let camera = Camera { center: lonlat_to_merc(Coord::new(0.0, 0.0)), res: 1200.0, bearing: 0.0, pitch: 0.0 }; let view = View { width: 200.0, height: 200.0, pixels_per_point: 1.0, camera, sky: false }; @@ -8789,8 +8800,15 @@ mod tests { assert!(farthest > centre * 1.01, "a far band is drawn at a smaller scale: {scales:?}"); let l = m.layer_mut(id).unwrap(); l.visible = true; - l.raster.as_mut().unwrap().style.renderer = - rustgis_map::RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 210.0, altitude: 5.0, z_factor: 1.0, shadow: 1.0 }; + l.raster.as_mut().unwrap().style.renderer = rustgis_map::RasterRenderer::ShadedRelief { + band: 0, + ramp: None, + azimuth: 210.0, + altitude: 5.0, + z_factor: 1.0, + shadow: 1.0, + sky: 0.0, + }; // Drawn only zoomed out past a scale between the centre's and the far band's. l.max_scale = Some((centre + farthest) / 2.0); // Its work held before it traces: an export that waits for it can't end until it is let @@ -8832,6 +8850,7 @@ mod tests { altitude: 5.0, z_factor: 1.0, shadow: 1.0, + sky: 0.0, }; // Drawn only zoomed in past 1:1: or in a group drawn only there. if grouped { diff --git a/crates/render/src/raster.rs b/crates/render/src/raster.rs index 237d55d0cf..a05a407960 100644 --- a/crates/render/src/raster.rs +++ b/crates/render/src/raster.rs @@ -85,10 +85,18 @@ fn place_is_flat(p: &Place) -> bool { matches!(p, Place::Flat(..)) } +/// The grids a Shaded Relief layer is lit with, worked out off the frame, one value per cell: the +/// light its cast shadows leave (see [`rustgis_map::raster::RasterLayer::cast_shadows`]) and the +/// share of the sky it sees ([`rustgis_map::raster::RasterLayer::sky_view`]). +#[derive(Clone, Copy, Default)] +pub(crate) struct Lights<'a> { + pub shadow: Option<&'a [u8]>, + pub sky: Option<&'a [u8]>, +} + /// Draws a raster layer onto `pm` at `opacity`. `stats` are the renderer bands' statistics -/// without the masked pixels, when the style has a mask. `light` is the light its cast shadows -/// leave on each of its cells, for Shaded Relief with shadows (see -/// [`rustgis_map::raster::RasterLayer::cast_shadows`]). +/// without the masked pixels, when the style has a mask. `light` is what it is lit with, for +/// Shaded Relief with shadows or sky. #[allow(clippy::too_many_arguments)] pub(crate) fn draw( pm: &mut Pixmap, @@ -99,7 +107,7 @@ pub(crate) fn draw( opacity: f32, full_res: bool, place: Place, - light: Option<&[u8]>, + light: Lights, ) { let threads = threads_for(pm.width() as usize * pm.height() as usize); draw_with(pm, r, style, stats, view, opacity, full_res, place, threads, light); @@ -143,7 +151,7 @@ pub(crate) fn draw_in_place( opacity: f32, full_res: bool, place: Place, - light: Option<&[u8]>, + light: Lights, ) -> Result { let bands = style.renderer.bands(); let own: Vec = match stats { @@ -263,12 +271,12 @@ pub(crate) fn draw_in_place( if opacity >= 1.0 { whole(pm); for part in &parts { - draw(pm, part, &own_style, Some(stats), view, 1.0, full_res, place, None); + draw(pm, part, &own_style, Some(stats), view, 1.0, full_res, place, Lights::default()); } } else if let Some(mut both) = Pixmap::new(pm.width(), pm.height()) { - draw(&mut both, r, style, Some(stats), view, 1.0, full_res, place, None); + draw(&mut both, r, style, Some(stats), view, 1.0, full_res, place, Lights::default()); for part in &parts { - draw(&mut both, part, &own_style, Some(stats), view, 1.0, full_res, place, None); + draw(&mut both, part, &own_style, Some(stats), view, 1.0, full_res, place, Lights::default()); } pm.draw_pixmap(0, 0, both.as_ref(), &PixmapPaint { opacity, ..PixmapPaint::default() }, Transform::identity(), None); } else { @@ -279,7 +287,7 @@ pub(crate) fn draw_in_place( } } else { for part in &parts { - draw(pm, part, &own_style, Some(stats), view, opacity, full_res, place, None); + draw(pm, part, &own_style, Some(stats), view, opacity, full_res, place, Lights::default()); } } match (pending, failed) { @@ -388,7 +396,7 @@ fn draw_with( full_res: bool, place: Place, threads: usize, - light: Option<&[u8]>, + light: Lights, ) { // On high-density screens, compute at logical-pixel resolution and scale up (unless asked // for full resolution, as exports are): a quarter of the work at 2×. @@ -477,12 +485,11 @@ fn draw_with( RasterRenderer::ShadedRelief { azimuth, altitude, z_factor, .. } => Some((azimuth, altitude, z_factor)), _ => None, }; - // Cast shadows darken the direct light by their strength, the ambient light left as it is. - let shadow = match style.renderer { - RasterRenderer::ShadedRelief { shadow, .. } if shadow > 0.0 => { - light.filter(|g| g.len() == r.width as usize * r.height as usize).map(|g| (g, shadow.min(1.0))) - } - _ => None, + // Cast shadows darken the direct light by their strength; the sky a cell sees, the ambient. + let cells = r.width as usize * r.height as usize; + let (shadow, sky) = match style.renderer { + RasterRenderer::ShadedRelief { shadow, sky, .. } => (lit_by(light.shadow, shadow, cells), lit_by(light.sky, sky, cells)), + _ => (None, None), }; // Blending neighbours would invent values that aren't classes or palette entries. let resampling = match style.renderer { @@ -513,6 +520,7 @@ fn draw_with( data: bands.iter().map(|b| r.bands.get(*b).map(|b| b.data.as_slice())).collect(), relief, shadow, + sky, resampling, alpha_band, alpha_max: rustgis_data::raster::alpha_max(&r.data_type), @@ -523,12 +531,17 @@ fn draw_with( let mut vals = vec![f32::NAN; bw * bh * nb]; let mut shade = if relief.is_some() { vec![0u8; bw * bh] } else { Vec::new() }; let mut alpha = if alpha_band.is_some() { vec![0u8; bw * bh] } else { Vec::new() }; + let mut ambient = if sky.is_some() { vec![255u8; bw * bh] } else { Vec::new() }; let threads = threads.clamp(1, bh); let rows = bh.div_ceil(threads.max(1)); each_chunk( threads, - vals.chunks_mut(rows * bw * nb).zip(chunks_or_empty(&mut shade, rows * bw)).zip(chunks_or_empty(&mut alpha, rows * bw)).enumerate(), - |(i, ((v, sh), al))| pass1.rows(y0 + i * rows, v, sh, al), + vals.chunks_mut(rows * bw * nb) + .zip(chunks_or_empty(&mut shade, rows * bw)) + .zip(chunks_or_empty(&mut alpha, rows * bw)) + .zip(chunks_or_empty(&mut ambient, rows * bw)) + .enumerate(), + |(i, (((v, sh), al), am))| pass1.rows(y0 + i * rows, v, sh, al, am), ); // Dynamic range adjustment: statistics of the values in view. @@ -538,19 +551,19 @@ fn draw_with( // Pass 2: colours, premultiplied, into a pixmap the size of the raster's screen box. let Some(mut lay) = Pixmap::new(bw as u32, bh as u32) else { return }; - let (vals, shade, alpha) = (&vals, &shade, &alpha); + let (vals, shade, alpha, ambient) = (&vals, &shade, &alpha, &ambient); each_chunk(threads, lay.data_mut().chunks_mut(rows * bw * 4).enumerate(), |(ci, out)| { let first = ci * rows * bw; // Neighbouring pixels often share their values (zoomed in, or classes): colour once. - let mut last: Option<(&[f32], Option, Option, Option<[u8; 4]>)> = None; + let mut last: Option<(&[f32], [Option; 3], Option<[u8; 4]>)> = None; for (j, px) in out.as_chunks_mut::<4>().0.iter_mut().enumerate() { let i = first + j; let Some(v) = vals.get(i * nb..(i + 1) * nb) else { continue }; - let (sh, al) = (shade.get(i).copied(), alpha.get(i).copied()); + let (sh, al, am) = (shade.get(i).copied(), alpha.get(i).copied(), ambient.get(i).copied()); let c = match last { - Some((lv, ls, la, c)) if ls == sh && la == al && same(lv, v) => c, + Some((lv, l, c)) if l == [sh, al, am] && same(lv, v) => c, _ => { - let c = colorizer.color(v, sh).map(|mut c| { + let c = colorizer.color_lit(v, sh, am).map(|mut c| { if let Some(a) = al { c[3] = ((c[3] as u16 * a as u16 + 127) / 255) as u8; } @@ -558,7 +571,7 @@ fn draw_with( let pm = |x: u8| ((x as u16 * a + 127) / 255) as u8; [pm(c[0]), pm(c[1]), pm(c[2]), c[3]] }); - last = Some((v, sh, al, c)); + last = Some((v, [sh, al, am], c)); c } }; @@ -580,6 +593,11 @@ fn draw_with( draw_scaled(pm, &lay, &PixmapPaint { opacity, quality, ..PixmapPaint::default() }, at, rows, threads); } +/// A light `grid` drawn at `strength` (up to 1), unless that is 0 or the grid isn't `cells` long. +fn lit_by(grid: Option<&[u8]>, strength: f64, cells: usize) -> Option<(&[u8], f64)> { + grid.filter(|g| strength > 0.0 && g.len() == cells).map(|g| (g, strength.min(1.0))) +} + /// Rows of the screen drawn as one band when a raster is scaled up (see [`draw_scaled`]). const BAND: usize = 64; @@ -637,6 +655,8 @@ struct Pass1<'a> { relief: Option<(f64, f64, f64)>, /// The light cast shadows leave on each cell, and how dark they are drawn (0 to 1). shadow: Option<(&'a [u8], f64)>, + /// The share of the sky each cell sees, and how much it scales the ambient light (0 to 1). + sky: Option<(&'a [u8], f64)>, resampling: Resampling, alpha_band: Option, /// The alpha band's opaque value (its pixel type's: 65535 for 16 bits). @@ -650,9 +670,9 @@ struct Pass1<'a> { } impl Pass1<'_> { - /// Fills the band values (and hillshade and alpha, when kept) of the screen rows from `py0` - /// on, as many as `vals` holds. - fn rows(&self, py0: usize, vals: &mut [f32], shade: &mut [u8], alpha: &mut [u8]) { + /// Fills the band values (and hillshade, alpha and ambient light, when kept) of the screen rows + /// from `py0` on, as many as `vals` holds. + fn rows(&self, py0: usize, vals: &mut [f32], shade: &mut [u8], alpha: &mut [u8], ambient: &mut [u8]) { let (r, bw, nb) = (self.r, self.bw, self.bands.len().max(1)); let lerp = |p: f64, q: f64, t: f64| p + (q - p) * t; // Each grid column's source position at this row, interpolated down the grid. @@ -755,6 +775,12 @@ impl Pass1<'_> { *s = (f64::from(*s) * left).round().clamp(0.0, 255.0) as u8; } } + if let Some((grid, strength)) = self.sky + && let Some(seen) = grid.get(ri as usize * r.width as usize + ci as usize) + && let Some(a) = ambient.get_mut(i) + { + *a = ((1.0 - strength * (1.0 - f64::from(*seen) / 255.0)) * 255.0).round().clamp(0.0, 255.0) as u8; + } } } } @@ -902,10 +928,11 @@ mod tests { .collect(); let r = Raster::new("relief", 10, 10, vec![data], gt, Crs::LonLat, None, ColorHint::Gray, "Float32").unwrap(); let mut style = RasterStyle::default_for(&r); - style.renderer = RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth, altitude: 45.0, z_factor: 1.0, shadow: 0.0 }; + style.renderer = + RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth, altitude: 45.0, z_factor: 1.0, shadow: 0.0, sky: 0.0 }; style.resampling = Resampling::Nearest; let mut pm = Pixmap::new(1, 1).unwrap(); - draw(&mut pm, &r, &style, None, &view, 1.0, false, Place::Flat(&Flat::Mercator, "mercator"), None); + draw(&mut pm, &r, &style, None, &view, 1.0, false, Place::Flat(&Flat::Mercator, "mercator"), Lights::default()); let p = pm.pixel(0, 0).unwrap(); [p.red(), p.green(), p.blue(), p.alpha()] }; @@ -935,7 +962,8 @@ mod tests { let data = [0.0, 1_000_000.0, 2_000_000.0, 3_000_000.0].repeat(18); let r = Raster::new("regional relief", 4, 18, vec![data], gt, Crs::LonLat, None, ColorHint::Gray, "Float32").unwrap(); let mut style = RasterStyle::default_for(&r); - style.renderer = RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 270.0, altitude: 45.0, z_factor: 1.0, shadow: 0.0 }; + style.renderer = + RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 270.0, altitude: 45.0, z_factor: 1.0, shadow: 0.0, sky: 0.0 }; style.resampling = Resampling::Nearest; let view = View { width: 1.0, @@ -945,7 +973,7 @@ mod tests { sky: false, }; let mut pm = Pixmap::new(1, 1).unwrap(); - draw(&mut pm, &r, &style, None, &view, 1.0, false, Place::Flat(&Flat::Mercator, "mercator"), None); + draw(&mut pm, &r, &style, None, &view, 1.0, false, Place::Flat(&Flat::Mercator, "mercator"), Lights::default()); let z = [0.0, 0.0, 1_000_000.0, 0.0, 0.0, 1_000_000.0, 0.0, 0.0, 1_000_000.0]; let (cell_x, cell_y) = r.cell_size_m(); let shade = hillshade(&z, cell_x, cell_y, 270.0, 45.0, 1.0); @@ -974,10 +1002,21 @@ mod tests { let dark = vec![0u8; 4 * 18]; let drawn = |shadow: f64, light: Option<&[u8]>| { let mut style = RasterStyle::default_for(&r); - style.renderer = RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 270.0, altitude: 45.0, z_factor: 1.0, shadow }; + style.renderer = + RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 270.0, altitude: 45.0, z_factor: 1.0, shadow, sky: 0.0 }; style.resampling = Resampling::Nearest; let mut pm = Pixmap::new(1, 1).unwrap(); - draw(&mut pm, &r, &style, None, &view, 1.0, false, Place::Flat(&Flat::Mercator, "mercator"), light); + draw( + &mut pm, + &r, + &style, + None, + &view, + 1.0, + false, + Place::Flat(&Flat::Mercator, "mercator"), + Lights { shadow: light, sky: None }, + ); let p = pm.pixel(0, 0).unwrap(); let want = |shade: u8| Colorizer::new(&style, &r, None).color(&[0.0], Some(shade)).unwrap(); ([p.red(), p.green(), p.blue(), p.alpha()], want(lit), want(0), want((f64::from(lit) * 0.5).round() as u8)) @@ -996,6 +1035,54 @@ mod tests { assert_eq!(pixel, lit); } + /// The sky a cell sees scales its ambient light by the sky's strength, the direct light left + /// as it is; a sky of 0 draws today's pixels, with a sky grid or without. + #[test] + fn the_sky_scales_the_ambient_light_by_its_strength() { + let gt = GeoTransform { origin_x: 0.0, origin_y: 90.0, pixel_width: 10.0, pixel_height: -10.0 }; + let data = [0.0, 1_000_000.0, 2_000_000.0, 3_000_000.0].repeat(18); + let r = Raster::new("relief", 4, 18, vec![data], gt, Crs::LonLat, None, ColorHint::Gray, "Float32").unwrap(); + let view = View { + width: 1.0, + height: 1.0, + pixels_per_point: 1.0, + camera: Camera { center: lonlat_to_merc(Coord::new(5.0, 0.0)), res: 1000.0, bearing: 0.0, pitch: 0.0 }, + sky: false, + }; + let z = [0.0, 0.0, 1_000_000.0, 0.0, 0.0, 1_000_000.0, 0.0, 0.0, 1_000_000.0]; + let (cell_x, cell_y) = r.cell_size_m(); + let lit = hillshade(&z, cell_x, cell_y, 270.0, 45.0, 1.0); + let (none, dark) = (vec![0u8; 4 * 18], vec![0u8; 4 * 18]); + let drawn = |shadow: f64, sky: f64, light: Lights| { + let mut style = RasterStyle::default_for(&r); + style.renderer = + RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 270.0, altitude: 45.0, z_factor: 1.0, shadow, sky }; + style.resampling = Resampling::Nearest; + let mut pm = Pixmap::new(1, 1).unwrap(); + draw(&mut pm, &r, &style, None, &view, 1.0, false, Place::Flat(&Flat::Mercator, "mercator"), light); + let p = pm.pixel(0, 0).unwrap(); + [p.red(), p.green(), p.blue(), p.alpha()] + }; + let today = drawn(0.0, 0.0, Lights::default()); + let hidden = Lights { shadow: None, sky: Some(&none) }; + assert_eq!(drawn(0.0, 0.0, hidden), today, "a sky of 0"); + assert_eq!(drawn(0.0, 1.0, Lights::default()), today, "no grid"); + // No sky at all: the direct light alone. + let mut style = RasterStyle::default_for(&r); + style.renderer = + RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 270.0, altitude: 45.0, z_factor: 1.0, shadow: 0.0, sky: 1.0 }; + let want = Colorizer::new(&style, &r, None).color_lit(&[0.0], Some(lit), Some(0)).unwrap(); + assert_eq!(drawn(0.0, 1.0, hidden), want); + assert!(want[0] < today[0]); + // In full shadow only the ambient light is left: a quarter of the colour, scaled by the + // sky by its strength. + let half_seen = vec![128u8; 4 * 18]; + for (sky, grid, want) in [(1.0, &dark, 0), (0.5, &dark, 32), (1.0, &half_seen, 32), (0.0, &dark, 64)] { + let light = Lights { shadow: Some(&dark), sky: Some(grid.as_slice()) }; + assert_eq!(drawn(1.0, sky, light), [want, want, want, 255], "sky {sky}, seen {}", grid[0]); + } + } + /// A 0–360° grid seen across its seam: the part in view is two strips, one at each end of /// the grid, not the whole width. #[test] @@ -1037,10 +1124,10 @@ mod tests { let r = Raster::new("relief", 36, 18, vec![data], gt, Crs::LonLat, None, ColorHint::Gray, "Float32").unwrap(); let mut style = RasterStyle::default_for(&r); style.renderer = - RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 270.0, altitude: 45.0, z_factor: 1.0, shadow: 0.0 }; + RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 270.0, altitude: 45.0, z_factor: 1.0, shadow: 0.0, sky: 0.0 }; style.resampling = Resampling::Nearest; let mut pm = Pixmap::new(80, 40).unwrap(); - draw(&mut pm, &r, &style, None, &view, 1.0, false, Place::Flat(&Flat::Mercator, "mercator"), None); + draw(&mut pm, &r, &style, None, &view, 1.0, false, Place::Flat(&Flat::Mercator, "mercator"), Lights::default()); pm }; let seam = render(0.0, 0); @@ -1065,7 +1152,7 @@ mod tests { style.resampling = Resampling::Bilinear; let draw_it = |style: &RasterStyle, stats: Option<&[BandStats]>| { let mut pm = Pixmap::new(200, 200).unwrap(); - draw(&mut pm, &r, style, stats, &view, 1.0, false, flat, None); + draw(&mut pm, &r, style, stats, &view, 1.0, false, flat, Lights::default()); pm }; let px = |pm: &Pixmap, x: u32| pm.pixel(x, 100).map(|p| (p.alpha(), p.red())).unwrap_or((0, 0)); @@ -1109,7 +1196,7 @@ mod tests { let camera = Camera { center: lonlat_to_merc(Coord::new(-81.0, 42.0)), res: 2000.0, bearing: 0.0, pitch: 0.0 }; let view = View { width: 200.0, height: 200.0, pixels_per_point: 1.0, camera, sky: false }; let mut pm = Pixmap::new(200, 200).unwrap(); - draw(&mut pm, &r, &style, None, &view, 1.0, false, Place::Flat(&Flat::Mercator, "mercator"), None); + draw(&mut pm, &r, &style, None, &view, 1.0, false, Place::Flat(&Flat::Mercator, "mercator"), Lights::default()); let alpha = |x: u32, y: u32| pm.pixel(x, y).map(|p| p.alpha()).unwrap_or(0); assert_eq!(alpha(100, 100), 255, "the centre is covered"); assert_eq!(alpha(2, 2), 0, "the corner of the view is outside the raster"); @@ -1123,16 +1210,24 @@ mod tests { "rgb" => RasterRenderer::Rgb { red: 0, green: 0, blue: 0 }, "classify" => rustgis_map::raster::classify(&r, 0, 5, Default::default(), Default::default()), "unique" => rustgis_map::raster::unique_values(&r, 0, Default::default()), - _ => RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 315.0, altitude: 45.0, z_factor: 1.0, shadow: 0.0 }, + _ => RasterRenderer::ShadedRelief { + band: 0, + ramp: None, + azimuth: 315.0, + altitude: 45.0, + z_factor: 1.0, + shadow: 0.0, + sky: 0.0, + }, }; s.dra = true; s.resampling = Resampling::Bilinear; - draw(&mut pm, &r, &s, None, &view, 0.5, false, Place::Flat(&Flat::Mercator, "mercator"), None); + draw(&mut pm, &r, &s, None, &view, 0.5, false, Place::Flat(&Flat::Mercator, "mercator"), Lights::default()); } // High-density screens draw at logical resolution, scaled up. let hi = View { pixels_per_point: 2.0, ..view }; let mut pm2 = Pixmap::new(400, 400).unwrap(); - draw(&mut pm2, &r, &style, None, &hi, 1.0, false, Place::Flat(&Flat::Mercator, "mercator"), None); + draw(&mut pm2, &r, &style, None, &hi, 1.0, false, Place::Flat(&Flat::Mercator, "mercator"), Lights::default()); assert_eq!(pm2.pixel(200, 200).map(|p| p.alpha()), Some(255)); assert_eq!(pm2.pixel(4, 4).map(|p| p.alpha()), Some(0)); // Spread over threads, every pixel comes out as drawn on one, for each renderer. @@ -1141,21 +1236,27 @@ mod tests { let mut s = style.clone(); s.renderer = match kind { "rgb" => RasterRenderer::Rgb { red: 0, green: 0, blue: 0 }, - "shadedRelief" => { - RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 315.0, altitude: 45.0, z_factor: 1.0, shadow: 0.0 } - } + "shadedRelief" => RasterRenderer::ShadedRelief { + band: 0, + ramp: None, + azimuth: 315.0, + altitude: 45.0, + z_factor: 1.0, + shadow: 0.0, + sky: 0.0, + }, _ => s.renderer, }; let mut one = Pixmap::new(420, 300).unwrap(); let mut many = Pixmap::new(420, 300).unwrap(); - draw_with(&mut one, &r, &s, None, &wide, 1.0, false, Place::Flat(&Flat::Mercator, "mercator"), 1, None); - draw_with(&mut many, &r, &s, None, &wide, 1.0, false, Place::Flat(&Flat::Mercator, "mercator"), 7, None); + draw_with(&mut one, &r, &s, None, &wide, 1.0, false, Place::Flat(&Flat::Mercator, "mercator"), 1, Lights::default()); + draw_with(&mut many, &r, &s, None, &wide, 1.0, false, Place::Flat(&Flat::Mercator, "mercator"), 7, Lights::default()); assert!(one.data() == many.data(), "{kind}"); assert!(one.pixels().iter().any(|p| p.alpha() == 255), "{kind} draws"); } let far = View { width: 3.0, height: 1.0, pixels_per_point: 2.0, camera: Camera::world(), sky: false }; let mut tiny = Pixmap::new(6, 2).unwrap(); - draw(&mut tiny, &r, &style, None, &far, 1.0, true, Place::Flat(&Flat::Mercator, "mercator"), None); + draw(&mut tiny, &r, &style, None, &far, 1.0, true, Place::Flat(&Flat::Mercator, "mercator"), Lights::default()); } /// #1792: a projected raster scaled up to a high-density screen, its reprojection grid and @@ -1179,8 +1280,30 @@ mod tests { let mut one = Pixmap::new(size.0, size.1).unwrap(); one.fill(tiny_skia::Color::from_rgba8(20, 120, 40, 255)); let mut many = one.clone(); - draw_with(&mut one, &r, &style, None, &view, opacity, false, Place::Flat(&Flat::Mercator, "mercator"), 1, None); - draw_with(&mut many, &r, &style, None, &view, opacity, false, Place::Flat(&Flat::Mercator, "mercator"), 7, None); + draw_with( + &mut one, + &r, + &style, + None, + &view, + opacity, + false, + Place::Flat(&Flat::Mercator, "mercator"), + 1, + Lights::default(), + ); + draw_with( + &mut many, + &r, + &style, + None, + &view, + opacity, + false, + Place::Flat(&Flat::Mercator, "mercator"), + 7, + Lights::default(), + ); assert!(one.data() == many.data(), "res {res} ppp {ppp} opacity {opacity} {resampling:?}"); assert!(one.pixels().iter().any(|p| p.green() != 120), "res {res} ppp {ppp}: the raster is drawn"); } @@ -1199,7 +1322,7 @@ mod tests { let camera = Camera { center: Coord::new(0.0, 0.0), res: 100_000.0, bearing: 0.0, pitch: 0.0 }; let view = View { width: 400.0, height: 200.0, pixels_per_point: 1.0, camera, sky: false }; let mut pm = Pixmap::new(400, 200).unwrap(); - draw(&mut pm, &r, &style, None, &view, 1.0, false, Place::Flat(&Flat::Mercator, "mercator"), None); + draw(&mut pm, &r, &style, None, &view, 1.0, false, Place::Flat(&Flat::Mercator, "mercator"), Lights::default()); let at = |lon: f64| { let (x, y) = view.to_screen(lonlat_to_merc(Coord::new(lon, 10.0))); pm.pixel(x as u32, y as u32).map(|p| (p.alpha(), p.red())).unwrap_or((0, 0)) @@ -1226,7 +1349,7 @@ mod tests { let camera = Camera { center: Coord::new(0.0, 0.0), res: 100_000.0, bearing: 0.0, pitch: 0.0 }; let view = View { width: 400.0, height: 200.0, pixels_per_point: 1.0, camera, sky: false }; let mut pm = Pixmap::new(400, 200).unwrap(); - draw(&mut pm, &r, &style, None, &view, 1.0, false, Place::Flat(&flat, "robinson +lon_0=150"), None); + draw(&mut pm, &r, &style, None, &view, 1.0, false, Place::Flat(&flat, "robinson +lon_0=150"), Lights::default()); let screen = |lon: f64| view.to_screen(flat.forward(Coord::new(lon, 10.0)).unwrap()); let red = |x: f64, y: f64| pm.pixel(x as u32, y as u32).map(|p| (p.alpha(), p.red())).unwrap_or((0, 0)); let ((xa, y), (xb, _)) = (screen(170.5), screen(-170.5)); diff --git a/crates/render/src/raster_export.rs b/crates/render/src/raster_export.rs index c39a4e39b0..d538fad541 100644 --- a/crates/render/src/raster_export.rs +++ b/crates/render/src/raster_export.rs @@ -59,21 +59,20 @@ pub fn export(layer: &RasterLayer, min_zoom: u8, max_zoom: u8, opacity: f32) -> ranges.push((z, x0, x1, y0, y1)); } let stats = layer.masked_stats(); - // Its cast shadows, waited for; ones that can't be drawn leave the archive unwritten, as a - // part drawn from less than it needs does. - let light = match layer.cast_shadows(true, (u64::MAX, u64::MAX), || {}) { - rustgis_map::raster::CastShadows::Ready(grid) => Some(grid), - rustgis_map::raster::CastShadows::Unavailable(why) => return Err(format!("{}: {why}, so the archive isn't written", r.name)), + // Its cast shadows and sky, waited for; ones that can't be drawn leave the archive unwritten, + // as a part drawn from less than it needs does. + let lit = |got: rustgis_map::raster::CastShadows, what: &str| match got { + rustgis_map::raster::CastShadows::Ready(grid) => Ok(Some(grid)), + rustgis_map::raster::CastShadows::Unavailable(why) => Err(format!("{}: {why}, so the archive isn't written", r.name)), // Only when the run is cancelled: nothing is written. rustgis_map::raster::CastShadows::Pending => { - return Err(format!( - "{}: its cast shadows weren't worked out (the export was cancelled), so the archive isn't written", - r.name - )); + Err(format!("{}: its {what} worked out (the export was cancelled), so the archive isn't written", r.name)) } - rustgis_map::raster::CastShadows::None => None, + rustgis_map::raster::CastShadows::None => Ok(None), }; - let light = light.as_deref().map(Vec::as_slice); + let shadow = lit(layer.cast_shadows(true, (u64::MAX, u64::MAX), || {}), "cast shadows weren't")?; + let sky = lit(layer.sky_view(true, (u64::MAX, u64::MAX), || {}), "sky visibility wasn't")?; + let light = raster::Lights { shadow: shadow.as_deref().map(Vec::as_slice), sky: sky.as_deref().map(Vec::as_slice) }; // A raster read in place waits for its tiles natively, two minutes at most in all. let deadline = web_time::Instant::now() + rustgis_data::raster::IN_PLACE_WAIT; let mut tiles = Vec::new(); @@ -191,8 +190,15 @@ mod tests { assert!(e.contains("the archive isn't written"), "{e}"); // With cast shadows, which only the whole image could have: refused for them, and said. let mut layer = layer; - layer.style.renderer = - rustgis_map::RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 315.0, altitude: 30.0, z_factor: 1.0, shadow: 1.0 }; + layer.style.renderer = rustgis_map::RasterRenderer::ShadedRelief { + band: 0, + ramp: None, + azimuth: 315.0, + altitude: 30.0, + z_factor: 1.0, + shadow: 1.0, + sky: 0.0, + }; let e = export(&layer, 2, 2, 1.0).err().unwrap_or_else(|| panic!("an archive without its shadows")); assert!(e.contains("cast shadows") && e.contains("read in place"), "{e}"); } @@ -215,8 +221,15 @@ mod tests { let raster = Raster::new("plain", n, n, vec![data], gt, Crs::LonLat, None, ColorHint::Gray, "Float32").unwrap_or_else(|e| panic!("{e}")); let mut layer = RasterLayer::from_path(raster, "plain.tif"); - layer.style.renderer = - rustgis_map::RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 90.0, altitude: 2.0, z_factor: 1.0, shadow: 1.0 }; + layer.style.renderer = rustgis_map::RasterRenderer::ShadedRelief { + band: 0, + ramp: None, + azimuth: 90.0, + altitude: 2.0, + z_factor: 1.0, + shadow: 1.0, + sky: 0.0, + }; let _cancelled = rustgis_data::progress::install(Some(std::sync::Arc::new(Cancelled))); let e = export(&layer, 0, 0, 1.0).err().unwrap_or_else(|| panic!("an archive without its shadows")); assert!(e.contains("cancelled") && e.contains("isn't written"), "{e}"); diff --git a/crates/ui-egui/src/mapview.rs b/crates/ui-egui/src/mapview.rs index 0fee0c7b49..fac5fc64eb 100644 --- a/crates/ui-egui/src/mapview.rs +++ b/crates/ui-egui/src/mapview.rs @@ -4909,7 +4909,8 @@ mod tests { let r = Raster::new("dem", 4, 4, vec![vec![0.0; 16]], gt, Crs::WebMercator, None, ColorHint::Gray, "Float32").unwrap(); let mut map = Map::new("Relief"); let id = map.add_raster("dem", RasterLayer::from_path(r, "dem.tif")); - let relief = |shadow| RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 315.0, altitude: 30.0, z_factor: 1.0, shadow }; + let relief = + |shadow| RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 315.0, altitude: 30.0, z_factor: 1.0, shadow, sky: 0.0 }; let set = |map: &mut Map, shadow: f64, visible: bool| { let l = map.layer_mut(id).unwrap(); l.visible = visible; diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index 59afaffe73..55e0059ff1 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -6742,10 +6742,10 @@ fn raster_symbology(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, t: &Tokens) { RasterRenderer::Colormap => { ui.label(egui::RichText::new(tl!("Colors from the raster's own color table.")).size(11.5).color(t.text_dim)); } - RasterRenderer::ShadedRelief { band, ramp, azimuth, altitude, z_factor, shadow } => { - let (az, alt, zf, sh, rp) = (*azimuth, *altitude, *z_factor, *shadow, ramp.as_ref().map(|r| r.name().to_string())); + RasterRenderer::ShadedRelief { band, ramp, azimuth, altitude, z_factor, shadow, sky } => { + let (az, alt, zf, sh, sk, rp) = (*azimuth, *altitude, *z_factor, *shadow, *sky, ramp.as_ref().map(|r| r.name().to_string())); let relief = move |band: usize, az: f64, alt: f64, zf: f64, sh: f64| { - let mut p = json!({"type": "shadedRelief", "band": band + 1, "azimuth": az, "altitude": alt, "zFactor": zf, "shadow": sh}); + let mut p = json!({"type": "shadedRelief", "band": band + 1, "azimuth": az, "altitude": alt, "zFactor": zf, "shadow": sh, "sky": sk}); match &rp { Some(r) => p["ramp"] = json!(r), None => p["grey"] = json!(true), @@ -6759,6 +6759,7 @@ fn raster_symbology(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, t: &Tokens) { ("Altitude", alt, 0.0..=90.0, 0.5, 1), ("Z factor", zf, 0.001..=1000.0, 0.01, 2), ("Cast shadows", sh, 0.0..=1.0, 0.01, 3), + ("Sky visibility", sk, 0.0..=1.0, 0.01, 4), ] { let input_label = ui.label(tl!(label)); let mut x = v; @@ -6767,19 +6768,26 @@ fn raster_symbology(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, t: &Tokens) { let r = ui .add(egui::DragValue::new(&mut x).update_while_editing(false).range(range).speed(speed).max_decimals(3)) .labelled_by(input_label.id); - let r = if which == 3 { - r.on_hover_text(tl!("How dark the shadows the terrain casts are, from 0 (none) to 1, for the sun set above")) - } else { - r + let r = match which { + 3 => r.on_hover_text(tl!("How dark the shadows the terrain casts are, from 0 (none) to 1, for the sun set above")), + 4 => r.on_hover_text(tl!( + "How much the sky each cell sees, over the terrain around it, scales its ambient light, from 0 (not at all) to 1" + )), + _ => r, }; if drag.settled(ui, &r, &mut x) && x != v { let (a, b, c, d) = match which { 0 => (x, alt, zf, sh), 1 => (az, x, zf, sh), 2 => (az, alt, x, sh), - _ => (az, alt, zf, x), + 3 => (az, alt, zf, x), + _ => (az, alt, zf, sh), }; - let _ = app.run("raster.symbology", relief(*band, a, b, c, d)); + let mut p = relief(*band, a, b, c, d); + if which == 4 { + p["sky"] = json!(x); + } + let _ = app.run("raster.symbology", p); } ui.end_row(); } diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index 863430b984..922ad9b36f 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -1217,6 +1217,20 @@ whole image is held: either way the layer is drawn without them, and a notice sa outside the raster casts nothing on it, and a grid spanning 0 to 360° casts nothing across its seam. +**Sky visibility**, on a Shaded Relief layer (`raster.symbology` `"sky"`, 0 to 1), dims the +relief's ambient light where the terrain around a cell hides part of the sky: valley floors, pits +and the foot of a cliff get less of it than a plain or a ridge, whatever the sun. Each cell's sky is +the share of the half sphere above its horizon, looked for in 16 directions up to 100 cells away +(past 32 cells, in steps of a sixteenth of the distance, so a peak narrower than that far away can +be missed); 1 scales the ambient light by it, 0, the default, draws the relief as before, and the +direct light (and its cast shadows) is left as it is. It is worked out once for the layer's band, +Z factor and mask, not for the sun, so moving the sun doesn't work it out again: off the map's +frame on the desktop, about a second for two million cells on a multi-core computer, nine or so on +one thread. It is drawn, waited for by +exports, and said when it can't be drawn (in the browser over 500,000 cells, or a cloud-optimised +GeoTIFF read in place) as cast shadows are. Terrain outside the raster, or farther than 100 cells, +hides none of its sky. + **Masking**, at the foot of the Symbology pane (`raster.mask`), hides pixel values from view without changing the data: values to draw as no data (a background of 0 or −9999 the file doesn't flag, QA flags, land cover classes such as `0, 254, 255`), and values outside, or inside, a range. The