Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions crates/analysis/src/rasterops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Typo in the doc comment: "a square root a / cell" should read "a square root per cell".

Suggested change
/// (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

Copy link
Copy Markdown
Member Author

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.

/// 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The 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

Copy link
Copy Markdown
Member Author

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. 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)
})
}
Expand Down
Loading