46 lignes
2.7 KiB
Rust
46 lignes
2.7 KiB
Rust
//! Arbres reels de Lans depuis le MNH. Usage :
|
|
//! cargo run --release --example arbres_mnh -- <mnt.npy> <mnh.npy> <dossier raster-sol-eau> <sortie.png>
|
|
use biomes::*;
|
|
use std::io::{BufWriter, Read};
|
|
fn npy(path: &str) -> Vec<u8> {
|
|
let mut b = Vec::new();
|
|
std::fs::File::open(path).unwrap().read_to_end(&mut b).unwrap();
|
|
let hl = u16::from_le_bytes([b[8], b[9]]) as usize;
|
|
b[10 + hl..].to_vec()
|
|
}
|
|
fn f32s(b: &[u8]) -> Vec<f32> { b.chunks_exact(4).map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]])).collect() }
|
|
fn main() {
|
|
let a: Vec<String> = std::env::args().collect();
|
|
let n = 1000usize;
|
|
let z = f32s(&npy(&a[1]));
|
|
let mnh = f32s(&npy(&a[2]));
|
|
let rd = |k: &str| npy(&format!("{}/{}.npy", a[3], k));
|
|
let (sol, eau, routes, bati) = (rd("sol"), rd("eau"), rd("routes"), rd("bati"));
|
|
let g = |r: isize, c: isize| z[r.clamp(0, n as isize - 1) as usize * n + c.clamp(0, n as isize - 1) as usize];
|
|
let col = |x: i32, zz: i32| {
|
|
let (r, c) = (zz as isize, x as isize);
|
|
let dx = (g(r, c + 1) - g(r, c - 1)) / 2.0;
|
|
let dy = (g(r + 1, c) - g(r - 1, c)) / 2.0;
|
|
let i = zz as usize * n + x as usize;
|
|
Colonne { alt_m: z[i], pente_deg: dx.hypot(dy).atan().to_degrees(), aspect_deg: (-dx).atan2(dy).to_degrees().rem_euclid(360.0), sol: sol[i], eau: eau[i], route: routes[i], bati: bati[i] }
|
|
};
|
|
let arbres = arbres_depuis_mnh(&mnh, n, n, &col, 42);
|
|
let proc_ = (0..n as i32).flat_map(|zz| (0..n as i32).map(move |x| (x, zz))).filter(|&(x, zz)| arbre_en(&col(x, zz), x, zz, 42).is_some()).count();
|
|
let hm = arbres.iter().map(|t| t.2.hauteur as f32).sum::<f32>() / arbres.len() as f32;
|
|
let conif = arbres.iter().filter(|t| t.2.essence.coniferes()).count();
|
|
println!("arbres MNH : {} (hauteur moyenne {:.1} m, coniferes {:.0} %) ; procedural : {}", arbres.len(), hm, 100.0 * conif as f32 / arbres.len() as f32, proc_);
|
|
let mut img = vec![0u8; n * n * 3];
|
|
for i in 0..n * n { let v = (60.0 + z[i] - 972.0).clamp(0., 255.) as u8; img[i * 3..i * 3 + 3].copy_from_slice(&[v / 2 + 60, v / 2 + 80, v / 3 + 40]); }
|
|
for (x, zz, t) in &arbres {
|
|
let c: [u8; 3] = if t.essence.coniferes() { [15, 60, 35] } else { [40, 120, 30] };
|
|
let r = if t.hauteur > 20 { 2 } else { 1 };
|
|
for dz in 0..r { for dx in 0..r {
|
|
let (xx, zz2) = (*x as usize + dx, *zz as usize + dz);
|
|
if xx < n && zz2 < n { let j = (zz2 * n + xx) * 3; img[j..j + 3].copy_from_slice(&c); }
|
|
} }
|
|
}
|
|
let mut e = png::Encoder::new(BufWriter::new(std::fs::File::create(&a[4]).unwrap()), n as u32, n as u32);
|
|
e.set_color(png::ColorType::Rgb); e.set_depth(png::BitDepth::Eight);
|
|
e.write_header().unwrap().write_image_data(&img).unwrap();
|
|
}
|