Fichiers
moteur-terrain/eau/examples/demo.rs
T
2026-10-30 00:18:03 +01:00

114 lignes
6.1 KiB
Rust

//! Lit mnt.tif (float32 non compresse, bandes) + eau.npy (uint8) et produit les PNG de dessus et de coupe.
//! Usage : cargo run --release --example demo -- <donnees_dir> <out_dir>
use eau::*;
fn u16le(b: &[u8], o: usize) -> usize { u16::from_le_bytes([b[o], b[o + 1]]) as usize }
fn u32le(b: &[u8], o: usize) -> usize { u32::from_le_bytes([b[o], b[o + 1], b[o + 2], b[o + 3]]) as usize }
fn lire_tif(p: &str) -> (Vec<f32>, usize, usize) {
let b = std::fs::read(p).unwrap();
assert_eq!(&b[0..2], b"II");
let ifd = u32le(&b, 4);
let n = u16le(&b, ifd);
let (mut w, mut h, mut rps) = (0, 0, 0);
let mut offs = vec![];
for k in 0..n {
let e = ifd + 2 + 12 * k;
let (tag, typ, cnt, val) = (u16le(&b, e), u16le(&b, e + 2), u32le(&b, e + 4), e + 8);
let get = |i: usize, base: usize| if typ == 3 { u16le(&b, base + 2 * i) } else { u32le(&b, base + 4 * i) };
let base = if cnt * if typ == 3 { 2 } else { 4 } > 4 { u32le(&b, val) } else { val };
match tag {
256 => w = get(0, val), 257 => h = get(0, val), 278 => rps = get(0, val),
273 => offs = (0..cnt).map(|i| get(i, base)).collect(),
259 => assert_eq!(get(0, val), 1, "compression non supportee"),
_ => {}
}
}
let mut out = Vec::with_capacity(w * h);
for (s, &o) in offs.iter().enumerate() {
let rows = rps.min(h - s * rps);
for i in 0..rows * w { out.push(f32::from_le_bytes(b[o + 4 * i..o + 4 * i + 4].try_into().unwrap())); }
}
(out, w, h)
}
fn lire_npy(p: &str) -> Vec<u8> {
let b = std::fs::read(p).unwrap();
let hl = u16le(&b, 8);
b[10 + hl..].to_vec()
}
fn main() {
let a: Vec<String> = std::env::args().collect();
let (dd, od) = (&a[1], &a[2]);
let (mnt, w, h) = lire_tif(&format!("{dd}/ign-mnt-lans/mnt.tif"));
let e = lire_npy(&format!("{dd}/raster-sol-eau/eau.npy"));
assert_eq!(e.len(), w * h);
let t = std::time::Instant::now();
let c = construire(&mnt, &e, w, h, &Params { alt_base: 900.0, ..Default::default() });
println!("construit en {:?} : {} rivieres, {} lacs, {} pixels ecretes, largeur max {:.1} m",
t.elapsed(), c.stats.composantes_rivieres, c.stats.lacs, c.stats.pixels_ecretes, c.stats.largeur_max_m);
std::fs::create_dir_all(od).unwrap();
// fenetre autour du plus gros bloc d'eau
let pix: Vec<usize> = (0..w * h).filter(|&i| c.nature[i] == Nature::Riviere).collect();
let (cx, cy) = (pix.iter().map(|i| i % w).sum::<usize>() / pix.len(), pix.iter().map(|i| i / w).sum::<usize>() / pix.len());
let (x0, y0, ww, hh) = (cx.saturating_sub(100).min(w - 200), cy.saturating_sub(100).min(h - 200), 200usize, 200usize);
let k = 4usize;
let (pw, ph) = (ww * k, hh * k);
let mut img = vec![0u8; pw * ph * 3];
for y in 0..hh { for x in 0..ww {
let i = (y0 + y) * w + x0 + x;
let (gx, gy) = (c.sol[(i + 1).min(w * h - 1)] as f32 - c.sol[i.saturating_sub(1)] as f32, c.sol[(i + w).min(w * h - 1)] as f32 - c.sol[i.saturating_sub(w)] as f32);
let sh = (150.0 + 25.0 * (gx - gy)).clamp(60.0, 230.0) as u8;
let col = match c.nature[i] {
Nature::Riviere | Nature::Lac => { let d = (c.niveau[i] - c.fond[i]) as f32; [20, (90.0 - 12.0 * d) as u8, (230.0 - 25.0 * d) as u8] }
Nature::Berge => [200, 180, 120],
Nature::Aucune => [sh / 2, sh, sh / 2],
};
for dy in 0..k { for dx in 0..k { let o = ((y * k + dy) * pw + x * k + dx) * 3; img[o..o + 3].copy_from_slice(&col); } }
} }
png::ecrire(&format!("{od}/eau-dessus.png"), pw, ph, &img).unwrap();
// coupe transversale : ligne passant par le centre (largeur 61 colonnes, 8 px/bloc)
let row = pix.iter().map(|i| i / w).min_by_key(|r| (*r as i64 - cy as i64).abs()).unwrap();
let xs: Vec<usize> = (0..w).filter(|&x| c.nature[row * w + x] == Nature::Riviere).collect();
let xc = xs[xs.len() / 2];
let (xa, nx) = (xc.saturating_sub(30), 61usize);
let ymin = (xa..xa + nx).map(|x| c.sol[row * w + x]).min().unwrap() - 2;
let ymax = (xa..xa + nx).map(|x| c.sol[row * w + x].max(c.niveau[row * w + x])).max().unwrap() + 4;
let nb = (ymax - ymin) as usize;
let s = 10usize;
let (cw, ch) = (nx * s, nb * s);
let mut cp = vec![0u8; cw * ch * 3];
for x in 0..nx { for b in 0..nb {
let y = ymax - 1 - b as i16;
let col_ = c.colonne(xa + x, row, &EtatEau::default());
let col = if y <= col_.sol_y { if y == col_.sol_y { [90, 140, 60] } else { [110, 80, 50] } }
else if col_.eau_haut.map_or(false, |t| y <= t) { [40, 90, 210] } else { [190, 220, 250] };
for dy in 0..s { for dx in 0..s { let edge = dx == 0 || dy == 0; let o = ((b * s + dy) * cw + x * s + dx) * 3;
for q in 0..3 { img_set(&mut cp, o + q, col[q], edge); } } }
} }
png::ecrire(&format!("{od}/eau-coupe.png"), cw, ch, &cp).unwrap();
// profil en long : de la source (point le plus haut) a l'exutoire
let haut = *pix.iter().max_by_key(|&&i| (c.niveau[i], i)).unwrap();
let mut chaine = vec![haut];
while c.parent[*chaine.last().unwrap()] != u32::MAX { chaine.push(c.parent[*chaine.last().unwrap()] as usize); }
let n = chaine.len();
let (lo, hi) = (c.fond[*chaine.last().unwrap()] - 3, c.niveau[haut] + 5);
let (lw, lh) = (n.max(1) * 4, ((hi - lo) as usize) * 6);
let mut lg = vec![255u8; lw * lh * 3];
let mut set = |x: usize, y: i16, col: [u8; 3]| { if y >= lo && y < hi { let r = lh - 1 - ((y - lo) as usize * 6 + 3); for dy in 0..6 { for dx in 0..4 { let o = ((r.saturating_sub(3) + dy).min(lh - 1) * lw + x * 4 + dx) * 3; lg[o..o + 3].copy_from_slice(&col); } } } };
for (k, &i) in chaine.iter().enumerate() {
let x = n - 1 - k; // amont a droite
set(x, (mnt[i] - 900.0).round() as i16, [120, 120, 120]);
set(x, c.niveau[i], [30, 90, 220]);
set(x, c.fond[i], [110, 70, 40]);
}
png::ecrire(&format!("{od}/eau-profil-long.png"), lw, lh, &lg).unwrap();
println!("chaine {} pixels, niveau {} -> {}", n, c.niveau[haut], c.niveau[*chaine.last().unwrap()]);
}
fn img_set(v: &mut [u8], o: usize, c: u8, edge: bool) { v[o] = if edge { c.saturating_sub(25) } else { c }; }