Repository navigation
perf(analysis): hillshade lights each cell with a square root, no trigonometry #2941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -269,14 +269,28 @@ pub fn hillshade(r: &Raster, b: usize, azimuth: f64, altitude: f64, zf: f64) -> | |
| } | ||
|
|
||
| /// [`hillshade`], as [`slope_on`]. | ||
| /// | ||
| /// A cell's light is the cosine of the angle between the sun and the ground's normal (Burrough | ||
| /// and McDonnell, *Principles of Geographical Information Systems*, 1998): | ||
| /// 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 | ||
| /// cell and no trigonometry. Where 1 + p² overflows, the angles are taken instead. | ||
| pub fn hillshade_on(r: &Raster, b: usize, azimuth: f64, altitude: f64, zf: f64, ground: Option<&Extent>) -> Result<Vec<f32>, String> { | ||
| let d = band(r, b)?; | ||
| let (zen, az) = ((90.0 - altitude).to_radians(), azimuth.to_radians()); | ||
| let (cos_zen, sin_zen) = (zen.cos(), zen.sin()); | ||
| let (cos_az, sin_az) = (az.cos(), az.sin()); | ||
| terrain_on(None, r, d, zf, ground, |gx, gy| { | ||
| let slope = gx.hypot(gy).atan(); | ||
| let aspect = (-gx).atan2(-gy); | ||
| let v = cos_zen * slope.cos() + sin_zen * slope.sin() * (az - aspect).cos(); | ||
| let q = 1.0 + gx * gx + gy * gy; | ||
| 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() | ||
| }; | ||
|
Comment on lines
+287
to
+293
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The overflow branch ( Suggested test: a small raster with a steep ramp run with a huge Z factor (for example Category: quality
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. |
||
| (v.max(0.0) * 255.0).round().max(1.0) | ||
| }) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo in the doc comment: "a square root a / cell" should read "a square root per cell".
Category: quality
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.