Skip to main content

hillshade_core

Function hillshade_core 

Source
pub fn hillshade_core(
    dem: ArrayView2<'_, f32>,
    cellsize: f32,
    azimuth: f32,
    altitude: f32,
    out: &mut Array2<f32>,
)
Expand description

Compute a hillshade (simulated illumination) of a DEM.

§Arguments

  • dem - ArrayView2<f32>, shape (H, W): elevations. Must be NaN-free (see module docs). Any memory layout. H and W should each be at least 2; an axis of length 1 is treated as flat, where numpy would raise.
  • cellsize - f32: grid spacing, in the same units as the elevations.
  • azimuth - f32, degrees: direction the light comes from, clockwise from north (e.g. 315.0 for north-west). Converted internally with derivatives.py’s az = 360 - azimuth + 90.
  • altitude - f32, degrees: height of the light above the horizon (0.0 = horizon, 90.0 = directly overhead).
  • out - &mut Array2<f32>, shape (H, W): overwritten with the hillshade, every element in [0, 1] (0 = fully shaded, 1 = fully lit).

The four trig values are computed once per call, not per pixel. Runs serially below PARALLEL_THRESHOLD elements and in parallel at or above it.

§Panics

  • If out does not have the same shape as dem.
  • If exactly one of H and W is 0.

§Example

use ndarray::Array2;
use terra_texture_rs::hillshade_core;

// flat ground lit from directly overhead is fully lit
let dem = Array2::<f32>::zeros((4, 4));
let mut out = Array2::<f32>::zeros(dem.raw_dim());
hillshade_core(dem.view(), 1.0, 315.0, 90.0, &mut out);

assert!(out.iter().all(|&v| (v - 1.0).abs() < 1e-6));