From d067231d01df8e5cb9d07a3921bd391247e6e855 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sun, 29 Nov 2026 03:49:07 -0300 Subject: [PATCH] feat(ui): a "Terrain type" field in a Shaded Relief's style pane (#3142, part 2) The style pane's Shaded Relief settings gain Terrain type (-1 to 1), after Aerial perspective; it runs raster.symbology with "terrain", the setting part 1 (#3304) added. The user guide describes it after Aerial perspective. A layout render test draws a frame over gentle hills and a colour bar at 0, +0.6 and -0.6: the frame's contrast grows at +0.6 and falls at -0.6, and the colour bar is the same at all three. Co-Authored-By: Claude Opus 5.5 --- crates/render/src/layout/tests.rs | 82 +++++++++++++++++++++++++++++++ crates/ui-egui/src/panes.rs | 9 +++- docs/user-guide/data.md | 14 ++++++ 3 files changed, 103 insertions(+), 2 deletions(-) diff --git a/crates/render/src/layout/tests.rs b/crates/render/src/layout/tests.rs index 81b793cf20..235583b1c8 100644 --- a/crates/render/src/layout/tests.rs +++ b/crates/render/src/layout/tests.rs @@ -3213,6 +3213,88 @@ fn a_reliefs_aerial_perspective_lightens_its_page() { assert!(none < some && some < full, "{none} {some} {full}"); } +/// A page whose frame draws gentle hills (#3142) has more contrast with a positive terrain type and +/// less with a negative one; its colour bar, which shows flat ground, is the same at any setting. +#[test] +fn a_reliefs_terrain_type_changes_its_frames_contrast_not_its_colour_bar() { + use rustgis_data::raster::{ColorHint, Crs, GeoTransform, Raster}; + let n = 300usize; + // Hills 800 m high and about 60 km across, on cells of about 740 m: slopes of a few degrees. + let heights: Vec = (0..n * n).map(|i| (((i % n) as f32 / 9.0).sin() + ((i / n) as f32 / 7.0).cos()) * 400.0).collect(); + let gt = GeoTransform { origin_x: -1.0, origin_y: 1.0, pixel_width: 2.0 / n as f64, pixel_height: -2.0 / n as f64 }; + let dem = Raster::new("dem", n as u32, n as u32, vec![heights], gt, Crs::LonLat, None, ColorHint::Gray, "Float32").unwrap(); + // The frame's grey levels, from 20 to 110 pixels down and 20 to 170 across, and the colour bar's pixels. + let page = |terrain_type: f64| { + let mut rl = rustgis_map::raster::RasterLayer::from_path(dem.clone(), "dem.tif"); + rl.style.renderer = rustgis_map::raster::RasterRenderer::ShadedRelief { + band: 0, + ramp: Some(rustgis_map::Ramp::Terrain), + azimuth: 315.0, + altitude: 45.0, + z_factor: 1.0, + shadow: 0.0, + sky: 0.0, + fill: 0.0, + rock: None, + ambient: None, + balanced: false, + shadow_softness: None, + aerial: 0.0, + terrain_type, + }; + let mut m = Map::new("m"); + assert_eq!(m.add_raster("dem", rl), 1); + let mut l = Layout::new("Layout", 100.0, 100.0); + let frame = ElementKind::MapFrame { + map: "m".into(), + center: Coord::new(0.0, 0.0), + scale: 2.5e6, + shape: FrameShape::Rectangle, + grid: None, + sections: Vec::new(), + extent_of: None, + extent_color: rustgis_map::layout::INDICATOR_COLOR, + extent_width: rustgis_map::layout::INDICATOR_WIDTH, + border_color: rustgis_map::layout::FRAME_BORDER_COLOR, + border_width: rustgis_map::layout::FRAME_BORDER_WIDTH, + bearing: 0.0, + pitch: 0.0, + }; + let frame = l.add("m", (5.0, 5.0, 90.0, 60.0), frame); + let bar = ElementKind::Colorbar { + frame, + layer: Some("dem".into()), + title: String::new(), + orientation: Orientation::Horizontal, + ticks: 5, + tick_side: TickSide::End, + reverse: false, + color: None, + }; + l.add("Colorbar", (10.0, 72.0, 80.0, 20.0), bar); + let drawn = render(&l, &mut [m], 48.0, &PageInfo::default()).unwrap(); + let w = drawn.width() as usize; + let px = |x: usize, y: usize| &drawn.data()[(y * w + x) * 4..(y * w + x) * 4 + 4]; + let greys: Vec = (20..110) + .flat_map(|y| (20..170).map(move |x| (y, x))) + .map(|(y, x)| px(x, y)[..3].iter().map(|c| f64::from(*c)).sum::() / 3.0) + .collect(); + let bar: Vec = (136..175).flat_map(|y| (0..w).flat_map(move |x| px(x, y).to_vec())).collect(); + (greys, bar) + }; + let spread = |greys: &[f64]| { + let mean = greys.iter().sum::() / greys.len() as f64; + (greys.iter().map(|g| (g - mean).powi(2)).sum::() / greys.len() as f64).sqrt() + }; + let (today, today_bar) = page(0.0); + let (hills, hills_bar) = page(0.6); + let (steep, steep_bar) = page(-0.6); + assert!(today_bar.iter().any(|c| *c < 200), "the colour bar is drawn"); + assert!(hills_bar == today_bar && steep_bar == today_bar, "the colour bar is the same at any terrain type"); + let (today, hills, steep) = (spread(&today), spread(&hills), spread(&steep)); + assert!(steep < today && today < hills, "{steep} {today} {hills}"); +} + /// A map with one 100 × 1 raster valued 0–99 by column, styled by `renderer`. fn ramp_map(renderer: rustgis_map::raster::RasterRenderer, stretch: rustgis_map::raster::Stretch) -> Map { use rustgis_data::raster::{ColorHint, Crs, GeoTransform, Raster}; diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index 75602f79e7..fda02deb8a 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -7529,14 +7529,14 @@ fn raster_symbology(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, t: &Tokens) { balanced, shadow_softness, aerial, - // Not in this pane yet (#3142). - terrain_type: _, + terrain_type, } => { use rustgis_engine::map::shadow::{SOFT_EDGE_MAX, SOFT_EDGE_MIN, soft_edge}; let (az, alt, zf, sh, sk, fl) = (*azimuth, *altitude, *z_factor, *shadow, *sky, *fill); let ss = soft_edge(*shadow_softness); let am = relief_ambient(*ambient, *balanced); let ae = *aerial; + let tt = *terrain_type; let rp = 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!({ @@ -7560,6 +7560,7 @@ fn raster_symbology(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, t: &Tokens) { ("Fill light", fl, 0.0..=1.0, 0.01, 5), ("Ambient", am, 0.0..=1.0, 0.01, 6), ("Aerial perspective", ae, 0.0..=1.0, 0.01, 8), + ("Terrain type", tt, -1.0..=1.0, 0.01, 9), ] { let input_label = ui.label(tl!(label)); let mut x = v; @@ -7583,6 +7584,9 @@ fn raster_symbology(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, t: &Tokens) { 8 => r.on_hover_text(tl!( "Low ground, by its height on the stretch, lit with less contrast and hazed, so the high ground stands out, from 0 (none) to 1" )), + 9 => r.on_hover_text(tl!( + "How the sun lights slopes gentler or steeper than 30°: above 0 brings out low hills, below 0 stresses steep ground, from -1 to 1" + )), _ => r, }; if drag.settled(ui, &r, &mut x) && x != v { @@ -7600,6 +7604,7 @@ fn raster_symbology(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, t: &Tokens) { 6 => p["ambient"] = json!(x), 7 => p["shadowSoftness"] = json!(x), 8 => p["aerial"] = json!(x), + 9 => p["terrain"] = json!(x), _ => {} } let _ = app.run("raster.symbology", p); diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index ddd3e58480..7b07a2b9cc 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -1828,6 +1828,20 @@ pit. A layout's colour bar shows the haze but not the lowered contrast, which is value outside 0 to 1 is refused; `"aerial": null` or 0 removes it. It is saved with the project only when set. +**Terrain type**, on a Shaded Relief layer (`raster.symbology` `"terrain"`, -1 to 1), changes how +the sun lights gentle and steep slopes, so one setting suits a plain or a mountain range. Above 0, +gentle slopes are lit as if steeper and slopes steeper than 30° as if gentler: low hills and +shallow valleys show, while the mountains keep their tones. Below 0, steep ground is stressed and +gentle ground calmed. Slopes of 30° are lit the same at any setting, and the direction a slope +faces never changes. With `g` a cell's gradient (the tangent of its own slope, before the Z +factor) and `g₀ = tan 30°`, the sun lights it as if its gradient were `g₀ × (g / g₀)^p`, with +`p = 1 − 0.5 × terrain`; a slope is made at most 8 times steeper. Only the sun's light uses it: +cast shadows, sky visibility, the fill light and the rock tint keep the ground's own slope, and a +layout's colour bar, which shows flat ground, is the same at any setting. 0, the default, draws +the relief as before; about 0.6 is a good start for a plateau or a plain with low hills. A value +outside -1 to 1 is refused; `"terrain": null` or 0 removes it. It is saved with the project only +when set. + With **Resampling** set to bilinear, a Shaded Relief layer's light (its hillshade, cast shadows, sky visibility and fill light) and its rock tint are blended between the cells' centres, as its values are, so a cell drawn over many pixels shades smoothly instead of in squares; nearest keeps