Sure! Here's the equivalent code in Rust:
extern crate ndarray;
extern crate ndarray_stats;
use ndarray::{Array, Array2};
use ndarray_stats::errors::MinMaxError;
fn clamp(z: &Array2<f64>, vmin: f64, vmax: f64) -> Result<Array2<f64>, MinMaxError> {
z.mapv(|val| val.max(vmin).min(vmax))
}
fn gradient_angle(z: &Array2<f64>) -> Array2<f64> {
let dx = nd::gradient::gradient(&z, None, None, None, Some(1))[0].clone();
let dy = nd::gradient::gradient(&z, None, None, Some(1), None)[1].clone();
dy.atan2(&dx)
}
fn gradient_norm(z: &Array2<f64>) -> Array2<f64> {
let dx = nd::gradient::gradient(&z, None, None, None, Some(1))[0].clone();
let dy = nd::gradient::gradient(&z, None, None, Some(1), None)[1].clone();
let dx_sq = dx.mapv(|val| val.powi(2));
let dy_sq = dy.mapv(|val| val.powi(2));
dx_sq + dy_sq
}
fn gaussian_curvature(z: &Array2<f64>, sigma: f64) -> Result<(Array2<f64>, Array2<f64>), MinMaxError> {
let z = ndarray::Array2::<f64>::from(ndarray::ArrayD::<f64>::from(z.clone().to_owned()).gaussian_filter(sigma));
let zx = nd::gradient::gradient(&z, None, None, None, Some(1))[0].clone();
let zy = nd::gradient::gradient(&z, None, None, Some(1), None)[1].clone();
let zxx = nd::gradient::gradient(&zx, None, None, None, Some(1))[0].clone();
let zxy = nd::gradient::gradient(&zx, None, None, Some(1), None)[1].clone();
let (_, zyy) = nd::gradient::gradient(&zy, None, None, Some(1), None);
// Gaussian curvature = K1 * K2
let k = (zxx * zyy - (zxy.powi(2))) / (1.0 + (zx.powi(2)) + (zy.powi(2))).powi(2);
// mean curvature = (K1 + K2)/2
let h = (zxx * (1.0 + zy.powi(2)) - 2.0 * zxy * zx * zy + zyy *
(1.0 + zx.powi(2))) / 2.0 / (1.0 + zx.powi(2) + zy.powi(2)).powi(1);
Ok((k, h))
}
fn hillshade(z: &Array2<f64>, azimuth: f64, zenith: f64, talus_ref: f64) -> Result<Array2<f64>, MinMaxError> {
let azimuth_rad = std::f64::consts::PI * azimuth / 180.0;
let zenith_rad = std::f64::consts::PI * zenith / 180.0;
let aspect = gradient_angle(z);
let dn = gradient_norm(z).mapv(|val| val / talus_ref);
let slope = dn.atan();
let sh = (zenith_rad.cos() * slope.cos()) + (zenith_rad.sin() * slope.sin() * (azimuth_rad - aspect).cos());
let sh_min = sh.fold(std::f64::INFINITY, f64::min);
let sh_ptp = sh.fold(std::f64::NEG_INFINITY, f64::max) - sh_min;
Ok((sh - sh_min) / sh_ptp)
}
fn main() {
let z = Array::from_shape_vec((3, 3), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]).unwrap();
let clamped = clamp(&z, 0.0, 1.0).unwrap();
let grad_angle = gradient_angle(&z);
let grad_norm = gradient_norm(&z);
let gaussian_curvature = gaussian_curvature(&z, 2.0).unwrap();
let hillshade = hillshade(&z, 45.0, 30.0, 2.0).unwrap();
println!("Clamped: \n{}", clamped);
println!("Gradient Angle: \n{}", grad_angle);
println!("Gradient Norm: \n{}", grad_norm);
println!("Gaussian Curvature: \n{:?}", gaussian_curvature);
println!("Hillshade: \n{}", hillshade);
}
Please note that the above code requires adding the ndarray and ndarray-stats dependencies to your Cargo.toml file. Also, since Rust does not have a built-in gaussian filter function like scipy, you will need to use a separate library like ndarray-stats to perform the gaussian filtering in your code.