Repository navigation
perf(analysis): hillshade lights each cell with a square root, no trigonometry - #2941
Conversation
…gonometry A cell's light is cos zenith · cos slope + sin zenith · sin slope · cos(azimuth − aspect) (Burrough and McDonnell, Principles of Geographical Information Systems, 1998), with the slope atan p, p² = gx² + gy², and the aspect atan2(−gx, −gy). Expanding the cosine of the difference with cos slope = 1/√(1 + p²), sin slope = p/√(1 + p²), cos aspect = −gy/p and sin aspect = −gx/p gives (cos zenith − sin zenith · (gy · cos azimuth + gx · sin azimuth)) / √(1 + p²) so each cell takes one square root instead of hypot, atan, cos, sin, atan2 and cos. The sine and cosine of the azimuth are taken once. Where 1 + p² overflows, the angles are taken as before.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
| let v = if q.is_finite() { | ||
| (cos_zen - sin_zen * (gy * cos_az + gx * sin_az)) / q.sqrt() | ||
| } else { | ||
| let slope = gx.hypot(gy).atan(); | ||
| let aspect = (-gx).atan2(-gy); | ||
| cos_zen * slope.cos() + sin_zen * slope.sin() * (az - aspect).cos() | ||
| }; |
There was a problem hiding this comment.
The overflow branch (q not finite, angle form) is not exercised by any test. The existing tests use gradients far below the f64 overflow point, so this path could break without a failing test. I checked the algebra of the square-root form against the angle form by hand, and it matches; the fallback is the part with no coverage.
Suggested test: a small raster with a steep ramp run with a huge Z factor (for example zf = 1e200, so gx * gx overflows), asserting every cell is in 1..=255 and equals the angle form's rounded value. Also a grid of moderate gradients over several azimuths and altitudes, comparing the new form with the angle form within one grey level, which guards the math for the normal path.
Category: quality
There was a problem hiding this comment.
Left for the release quality pass. The fallback is the previous angle form, unchanged. The 115-million-cell comparison covers the algebraic form at one light direction; a test over several azimuths and altitudes and an overflowing Z factor is a reasonable addition then.
| /// cos zenith · cos slope + sin zenith · sin slope · cos(azimuth − aspect), the slope atan p with | ||
| /// p² = gx² + gy² and the aspect atan2(−gx, −gy). With cos slope = 1/√(1 + p²), | ||
| /// sin slope = p/√(1 + p²), cos aspect = −gy/p and sin aspect = −gx/p, that is | ||
| /// (cos zenith − sin zenith · (gy · cos azimuth + gx · sin azimuth)) / √(1 + p²): a square root a |
There was a problem hiding this comment.
Typo in the doc comment: "a square root a / cell" should read "a square root per cell".
| /// (cos zenith − sin zenith · (gy · cos azimuth + gx · sin azimuth)) / √(1 + p²): a square root a | |
| /// (cos zenith − sin zenith · (gy · cos azimuth + gx · sin azimuth)) / √(1 + p²): a square root per |
Category: quality
There was a problem hiding this comment.
Left for the release quality pass. "A square root a cell" is meant as "per cell" (as in "once a day"), not a typo; the wording can be revisited then.
Code reviewI checked the change against the algebra and the surrounding code. I found no bugs, no security issues and no performance problems. I could not run the build or the benchmark (no shell here), so the byte-identical output claim rests on the author's measurement. Bugs: none found.
Security: none found. Performance: none found. The change drops four transcendental calls per cell, and the sine and cosine of the azimuth are computed once per run. Threading still goes through Quality (both buffered as inline comments):
AGENTS.md: nothing found. No new command, no new memory or thread path, and no surface change that needs a schema, command-surface or limit update. Prior reviews: no existing threads, so no concerns were left alone. Unverified: the claim that all 115,491,649 cells match Change sizeCompared with the base branch: the whole PR against main (merge base e7f6bcf to PR head d0aab2c).
|
📖 Docs preview
GitHub Pages takes a minute or two to publish a new commit. Newer commits from the fork aren't published until a maintainer adds the |
Summary
hillshade_onmade six transcendental calls per cell:hypot,atan,cos,sin,atan2andcos.atan2alone was about a fifth of the run. The illumination formula (Burrough and McDonnell, Principles of Geographical Information Systems, 1998) iscos zenith · cos slope + sin zenith · sin slope · cos(azimuth − aspect)
with the slope atan p, p² = gx² + gy², and the aspect atan2(−gx, −gy). The terms can be rewritten algebraically: cos slope = 1/√(1 + p²), sin slope = p/√(1 + p²), cos aspect = −gy/p and sin aspect = −gx/p. Expanding cos(azimuth − aspect) with these gives
(cos zenith − sin zenith · (gy · cos azimuth + gx · sin azimuth)) / √(1 + p²)
So each cell now takes one square root. The sine and cosine of the azimuth are taken once per run. A flat cell (p = 0) gives cos zenith either way. Where 1 + p² overflows, the cell is computed with the angles as before. The gradient (Horn's), the rounding and the 1–255 range are unchanged.
Timing
This is the 25 Nov benchmark case: the 115-million-cell DEM (
dem.tif), azimuth 315, altitude 45, Z factor 1. Each figure is the median of 3 runs of the wholerustgis-cli run(read,raster.functionhillshade, write) on the same box, through the host queue.3a5dd41d4(benchmark baseline)mainat e7f6bcfThat is 26% less wall time and 37% less CPU time for the whole command. In a profile of the new build, the terrain loop is about 22% of the run and the rounding to whole numbers is another 6%. The rest is reading the GeoTIFF, the band statistics and writing it.
Output
All 115,491,649 cells are the same as
main's output, byte for byte. Rounding to whole numbers 1–255 absorbs the last-bit differences between the two forms.Not changed
slope_onandaspect_ondon't have the same waste. They return angles in degrees, so the oneatanof slope and the oneatan2of aspect are the result itself, not an intermediate. The per-cellrustgis_map::raster::hillshadethat the renderer uses for Shaded Relief still uses the angle form. It is a separate path, left for its own measurement.Surfaces
The change is in
rustgis_analysis::rasterops::hillshade_on. It reaches everything that makes a hillshade raster throughraster.functionand the Function Editor: desktop, browser, command line, Python, and the Assistant and MCP. Nothing saved changes. The map renderer's own shading is not affected.Testing
cargo fmt --all --check, the private preflight, and the terrain tests inrustgis-analysis:terrain_rows_match_scalar_edges_nodata_and_orientations,terrain_faces_the_right_way_on_a_bottom_up_grid(light from the south and north on a bottom-up grid),hillshade_keeps_zero_for_no_data,terrain_keeps_edge_and_nodata_resultsandterrain_math_and_classes. All passed.UI evidence
No visible UI change.
Related issue
Part of #1111.
Summary by CodeRabbit
Bug Fixes
Documentation