proc-relief: generateur procedural multi-echelle (bruit, ridges, warp, terrasses, dolines, vallees, erosion par tuile 512 m avec marge et raccord)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Cette révision appartient à :
1 Parent
21a50dc5b2
révision
a25690363d
13 fichiers modifiés
+1703
Aucun fichier ne correspond à votre recherche
@@ -0,0 +1 @@
|
|||||||
|
target/
|
||||||
Générée
+260
@@ -0,0 +1,260 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "adler2"
|
||||||
|
version = "2.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bitflags"
|
||||||
|
version = "1.3.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg-if"
|
||||||
|
version = "1.0.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crc32fast"
|
||||||
|
version = "1.5.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0ba106e844045ddc02c20768ac4b0b7cb45f2cc2ecbd833ac9f5dacb183c2055"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-deque"
|
||||||
|
version = "0.8.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-epoch",
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-epoch"
|
||||||
|
version = "0.9.21"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-utils"
|
||||||
|
version = "0.8.23"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "either"
|
||||||
|
version = "1.19.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fdeflate"
|
||||||
|
version = "0.3.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1e6853b52649d4ac5c0bd02320cddc5ba956bdb407c4b75a2c6b75bf51500f8c"
|
||||||
|
dependencies = [
|
||||||
|
"simd-adler32",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "flate2"
|
||||||
|
version = "1.1.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6e634e2e0ebac1ee034020da1ca582e17ffe4e0f5e985823721e168928136dcb"
|
||||||
|
dependencies = [
|
||||||
|
"crc32fast",
|
||||||
|
"miniz_oxide 0.9.1",
|
||||||
|
"zlib-rs",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itoa"
|
||||||
|
version = "1.0.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memchr"
|
||||||
|
version = "2.8.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "miniz_oxide"
|
||||||
|
version = "0.8.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316"
|
||||||
|
dependencies = [
|
||||||
|
"adler2",
|
||||||
|
"simd-adler32",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "miniz_oxide"
|
||||||
|
version = "0.9.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b63fbc4a50860e98e7b2aa7804ded1db5cbc3aff9193adaff57a6931bf7c4b4c"
|
||||||
|
dependencies = [
|
||||||
|
"adler2",
|
||||||
|
"simd-adler32",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "png"
|
||||||
|
version = "0.17.16"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "82151a2fc869e011c153adc57cf2789ccb8d9906ce52c0b39a6b5697749d7526"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags",
|
||||||
|
"crc32fast",
|
||||||
|
"fdeflate",
|
||||||
|
"flate2",
|
||||||
|
"miniz_oxide 0.8.9",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.107"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-relief"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"png",
|
||||||
|
"rayon",
|
||||||
|
"rt-core",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.47"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rayon"
|
||||||
|
version = "1.12.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d"
|
||||||
|
dependencies = [
|
||||||
|
"either",
|
||||||
|
"rayon-core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rayon-core"
|
||||||
|
version = "1.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-deque",
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rt-core"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde"
|
||||||
|
version = "1.0.229"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"
|
||||||
|
dependencies = [
|
||||||
|
"serde_core",
|
||||||
|
"serde_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_core"
|
||||||
|
version = "1.0.229"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48"
|
||||||
|
dependencies = [
|
||||||
|
"serde_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_derive"
|
||||||
|
version = "1.0.229"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_json"
|
||||||
|
version = "1.0.154"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6"
|
||||||
|
dependencies = [
|
||||||
|
"itoa",
|
||||||
|
"memchr",
|
||||||
|
"serde",
|
||||||
|
"serde_core",
|
||||||
|
"zmij",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "simd-adler32"
|
||||||
|
version = "0.3.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "3.0.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-ident"
|
||||||
|
version = "1.0.27"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zlib-rs"
|
||||||
|
version = "0.6.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b268e58e7c693d7c271f93ffc4ba3b380412554231c85bf61ca7af91042a4112"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zmij"
|
||||||
|
version = "1.0.23"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b"
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
[package]
|
||||||
|
name = "proc-relief"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
license = "GPL-3.0-only"
|
||||||
|
description = "Générateur procédural de relief multi-échelle (bruit, ridges, warp, terrasses, dolines, vallées, érosion par tuiles de 512 m)"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
rt-core = { path = "../relief/rt-core" }
|
||||||
|
serde = { version = "1", features = ["derive"] }
|
||||||
|
serde_json = "1"
|
||||||
|
rayon = "1.10"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
png = "0.17"
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
debug = false
|
||||||
|
|
||||||
|
[profile.test]
|
||||||
|
opt-level = 2
|
||||||
|
|
||||||
|
[workspace]
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
//! Rendu ombré par biome (3 × 3 tuiles = 1536 m, 1 px = 1 m) + mesures de vitesse
|
||||||
|
//! + export des ParamsRelief initiaux en JSON.
|
||||||
|
//! Usage : cargo run --release --example rendu -- <dossier_png> <dossier_params>
|
||||||
|
use std::io::BufWriter;
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
use proc_relief::*;
|
||||||
|
|
||||||
|
fn teinte(h: f32, lo: f32, hi: f32) -> [f32; 3] {
|
||||||
|
if h < 0.0 {
|
||||||
|
let t = (-h / 200.0).clamp(0.0, 1.0);
|
||||||
|
return [0.35 - 0.2 * t, 0.55 - 0.25 * t, 0.80 - 0.25 * t];
|
||||||
|
}
|
||||||
|
let t = ((h - lo) / (hi - lo).max(1.0)).clamp(0.0, 1.0);
|
||||||
|
let c = [[0.42, 0.60, 0.33], [0.78, 0.74, 0.50], [0.62, 0.48, 0.36], [0.85, 0.85, 0.85]];
|
||||||
|
let s = t * 3.0;
|
||||||
|
let i = (s as usize).min(2);
|
||||||
|
let f = s - i as f32;
|
||||||
|
[0, 1, 2].map(|k| c[i][k] + (c[i + 1][k] - c[i][k]) * f)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let args: Vec<String> = std::env::args().collect();
|
||||||
|
let sortie = args.get(1).cloned().unwrap_or_else(|| "sorties".into());
|
||||||
|
let dparams = args.get(2).cloned();
|
||||||
|
std::fs::create_dir_all(&sortie).unwrap();
|
||||||
|
let seed = 20261030u64;
|
||||||
|
let nt = 3usize;
|
||||||
|
let w = nt * 512;
|
||||||
|
println!("| biome | µs/colonne brute | µs/chunk brut | ms érosion/tuile | ms tuile finale (parallèle) | min | max |");
|
||||||
|
println!("|---|---|---|---|---|---|---|");
|
||||||
|
for b in BiomeRelief::TOUS {
|
||||||
|
let p = params_initiaux(b);
|
||||||
|
if let Some(d) = &dparams {
|
||||||
|
std::fs::create_dir_all(d).unwrap();
|
||||||
|
std::fs::write(format!("{d}/{}.json", b.id()), serde_json::to_string_pretty(&p).unwrap()).unwrap();
|
||||||
|
}
|
||||||
|
// vitesse colonne brute
|
||||||
|
let t = Instant::now();
|
||||||
|
let mut acc = 0.0f32;
|
||||||
|
let n = 200_000;
|
||||||
|
for i in 0..n {
|
||||||
|
acc += hauteur_brute(b, &p, seed, (i % 977) as f64 * 1.3, (i / 977) as f64 * 1.7);
|
||||||
|
}
|
||||||
|
let us_col = t.elapsed().as_secs_f64() * 1e6 / n as f64;
|
||||||
|
let g = Generateur::biome_unique(seed, b);
|
||||||
|
// érosion d'une tuile (mono-thread, sans cache)
|
||||||
|
let t = Instant::now();
|
||||||
|
let e = g.eroder_tuile(TileId { tx: 50, tz: 50 });
|
||||||
|
let ms_ero = t.elapsed().as_secs_f64() * 1e3;
|
||||||
|
// chunk final, tuiles déjà en cache
|
||||||
|
for dz in -1..=1 {
|
||||||
|
for dx in -1..=1 {
|
||||||
|
g.erodee(TileId { tx: 50 + dx, tz: 50 + dz });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let t = Instant::now();
|
||||||
|
for k in 0..64 {
|
||||||
|
acc += g.chunk(50 * 32 + k % 8, 50 * 32 + k / 8)[0];
|
||||||
|
}
|
||||||
|
let us_chunk = t.elapsed().as_secs_f64() * 1e6 / 64.0;
|
||||||
|
let dmax = e.delta.iter().fold(0.0f32, |a, d| a.max(d.abs()));
|
||||||
|
let dmoy = e.delta.iter().map(|d| d.abs()).sum::<f32>() / e.delta.len() as f32;
|
||||||
|
eprintln!("{} delta max {dmax:.1} m, moyen {dmoy:.2} m", b.id());
|
||||||
|
std::hint::black_box(acc);
|
||||||
|
// rendu 3×3 tuiles
|
||||||
|
let t = Instant::now();
|
||||||
|
let tuiles: Vec<TuileRelief> =
|
||||||
|
(0..nt * nt).map(|k| g.tuile(TileId { tx: (k % nt) as i32, tz: (k / nt) as i32 })).collect();
|
||||||
|
let ms_tuile = t.elapsed().as_secs_f64() * 1e3 / (nt * nt) as f64;
|
||||||
|
let mut h = vec![0.0f32; w * w];
|
||||||
|
for (k, tr) in tuiles.iter().enumerate() {
|
||||||
|
let (ox, oz) = ((k % nt) * 512, (k / nt) * 512);
|
||||||
|
for j in 0..512 {
|
||||||
|
h[(oz + j) * w + ox..(oz + j) * w + ox + 512].copy_from_slice(&tr.h[j * 512..j * 512 + 512]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let lo = h.iter().cloned().fold(f32::MAX, f32::min);
|
||||||
|
let hi = h.iter().cloned().fold(f32::MIN, f32::max);
|
||||||
|
println!("| {} | {:.2} | {:.0} | {:.0} | {:.0} | {:.0} | {:.0} |", b.id(), us_col, us_chunk, ms_ero, ms_tuile, lo, hi);
|
||||||
|
let (az, el) = (315f32.to_radians(), 45f32.to_radians());
|
||||||
|
let (lx, lz, ly) = (el.cos() * az.sin(), -el.cos() * az.cos(), el.sin());
|
||||||
|
let mut img = vec![0u8; w * w * 3];
|
||||||
|
for z in 0..w {
|
||||||
|
for x in 0..w {
|
||||||
|
let hx = h[z * w + (x + 1).min(w - 1)] - h[z * w + x.saturating_sub(1)];
|
||||||
|
let hz = h[(z + 1).min(w - 1) * w + x] - h[z.saturating_sub(1) * w + x];
|
||||||
|
let (nx, nz, ny) = (-hx / 2.0, -hz / 2.0, 1.0f32);
|
||||||
|
let l = (nx * nx + nz * nz + ny * ny).sqrt();
|
||||||
|
let s = ((nx * lx + nz * lz + ny * ly) / l).clamp(0.0, 1.0);
|
||||||
|
let c = teinte(h[z * w + x], lo.max(0.0), hi);
|
||||||
|
let o = 0.35 + 0.65 * s;
|
||||||
|
for k in 0..3 {
|
||||||
|
img[(z * w + x) * 3 + k] = (c[k] * o * 255.0).clamp(0.0, 255.0) as u8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let f = std::fs::File::create(format!("{sortie}/{}.png", b.id())).unwrap();
|
||||||
|
let mut enc = png::Encoder::new(BufWriter::new(f), w as u32, w as u32);
|
||||||
|
enc.set_color(png::ColorType::Rgb);
|
||||||
|
enc.set_depth(png::BitDepth::Eight);
|
||||||
|
enc.write_header().unwrap().write_image_data(&img).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
//! Statistiques du bruit brut (pour SIGMA_FBM, MOY/SIGMA_RIDGED) et plages d'altitude.
|
||||||
|
use proc_relief::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let p = params_initiaux(BiomeRelief::Prealpes);
|
||||||
|
let b = Simplex::new(7);
|
||||||
|
let (mut f, mut r) = (Vec::new(), Vec::new());
|
||||||
|
for i in 0..200_000u64 {
|
||||||
|
let x = (bruit::unif(bruit::melange64(i)) as f64) * 2.0e6;
|
||||||
|
let z = (bruit::unif(bruit::melange64(i ^ 0xabc)) as f64) * 2.0e6;
|
||||||
|
f.push(fbm(&b, x, z, &p));
|
||||||
|
r.push(ridged(&b, x, z, &p));
|
||||||
|
}
|
||||||
|
let st = |v: &[f32]| {
|
||||||
|
let m = v.iter().sum::<f32>() / v.len() as f32;
|
||||||
|
let s = (v.iter().map(|a| (a - m) * (a - m)).sum::<f32>() / v.len() as f32).sqrt();
|
||||||
|
(m, s)
|
||||||
|
};
|
||||||
|
println!("fbm {:?} ridged {:?}", st(&f), st(&r));
|
||||||
|
for b in BiomeRelief::TOUS {
|
||||||
|
let p = params_initiaux(b);
|
||||||
|
let mut v: Vec<f32> = (0..20_000u64)
|
||||||
|
.map(|i| {
|
||||||
|
let x = (bruit::unif(bruit::melange64(i)) as f64) * 1.0e5;
|
||||||
|
let z = (bruit::unif(bruit::melange64(i ^ 0x77)) as f64) * 1.0e5;
|
||||||
|
hauteur_brute(b, &p, 42, x, z)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
v.sort_by(|a, b| a.total_cmp(b));
|
||||||
|
let q = |k: f32| v[((v.len() - 1) as f32 * k) as usize];
|
||||||
|
println!("{:14} min {:7.0} p5 {:7.0} p50 {:7.0} p95 {:7.0} max {:7.0}", b.id(), q(0.0), q(0.05), q(0.5), q(0.95), q(1.0));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,172 @@
|
|||||||
|
//! Bruit déterministe sans table (hachage entier) : simplex 2D, fBm, ridged, warp.
|
||||||
|
//! Tout est pur et `Send + Sync`.
|
||||||
|
|
||||||
|
use crate::relief::ParamsRelief;
|
||||||
|
|
||||||
|
pub trait Bruit2: Send + Sync {
|
||||||
|
fn val(&self, x: f64, z: f64) -> f32;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub struct Simplex {
|
||||||
|
pub seed: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Simplex {
|
||||||
|
pub fn new(seed: u64) -> Self {
|
||||||
|
Simplex { seed }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Bruit2 for Simplex {
|
||||||
|
#[inline]
|
||||||
|
fn val(&self, x: f64, z: f64) -> f32 {
|
||||||
|
simplex2(self.seed, x, z)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn melange64(mut h: u64) -> u64 {
|
||||||
|
h ^= h >> 33;
|
||||||
|
h = h.wrapping_mul(0xff51_afd7_ed55_8ccd);
|
||||||
|
h ^= h >> 33;
|
||||||
|
h = h.wrapping_mul(0xc4ce_b9fe_1a85_ec53);
|
||||||
|
h ^= h >> 33;
|
||||||
|
h
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn hash2(seed: u64, i: i64, j: i64) -> u64 {
|
||||||
|
melange64(
|
||||||
|
seed ^ (i as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15)
|
||||||
|
^ (j as u64).wrapping_mul(0xC2B2_AE3D_27D4_EB4F).rotate_left(17),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sous-graine dérivée (une par composante du relief).
|
||||||
|
#[inline]
|
||||||
|
pub fn sous_graine(seed: u64, k: u64) -> u64 {
|
||||||
|
melange64(seed.wrapping_add(k.wrapping_mul(0xA076_1D64_78BD_642F)))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Nombre flottant [0,1) à partir d'un hash.
|
||||||
|
#[inline]
|
||||||
|
pub fn unif(h: u64) -> f32 {
|
||||||
|
(h >> 40) as f32 / (1u64 << 24) as f32
|
||||||
|
}
|
||||||
|
|
||||||
|
const F2: f64 = 0.366_025_403_784_438_6;
|
||||||
|
const G2: f64 = 0.211_324_865_405_187_1;
|
||||||
|
const D: f64 = std::f64::consts::FRAC_1_SQRT_2;
|
||||||
|
const GRAD: [(f64, f64); 8] =
|
||||||
|
[(1.0, 0.0), (-1.0, 0.0), (0.0, 1.0), (0.0, -1.0), (D, D), (-D, D), (D, -D), (-D, -D)];
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn coin(seed: u64, i: i64, j: i64, x: f64, y: f64) -> f64 {
|
||||||
|
let t = 0.5 - x * x - y * y;
|
||||||
|
if t <= 0.0 {
|
||||||
|
0.0
|
||||||
|
} else {
|
||||||
|
let g = GRAD[(hash2(seed, i, j) & 7) as usize];
|
||||||
|
let t2 = t * t;
|
||||||
|
t2 * t2 * (g.0 * x + g.1 * y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Simplex 2D, sortie ≈ [-1, 1].
|
||||||
|
#[inline]
|
||||||
|
pub fn simplex2(seed: u64, x: f64, y: f64) -> f32 {
|
||||||
|
let s = (x + y) * F2;
|
||||||
|
let i = (x + s).floor();
|
||||||
|
let j = (y + s).floor();
|
||||||
|
let t = (i + j) * G2;
|
||||||
|
let x0 = x - (i - t);
|
||||||
|
let y0 = y - (j - t);
|
||||||
|
let (i1, j1) = if x0 > y0 { (1i64, 0i64) } else { (0, 1) };
|
||||||
|
let x1 = x0 - i1 as f64 + G2;
|
||||||
|
let y1 = y0 - j1 as f64 + G2;
|
||||||
|
let x2 = x0 - 1.0 + 2.0 * G2;
|
||||||
|
let y2 = y0 - 1.0 + 2.0 * G2;
|
||||||
|
let (ii, jj) = (i as i64, j as i64);
|
||||||
|
let n = coin(seed, ii, jj, x0, y0) + coin(seed, ii + i1, jj + j1, x1, y1) + coin(seed, ii + 1, jj + 1, x2, y2);
|
||||||
|
((n * 99.0) as f32).clamp(-1.0, 1.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Nombre d'octaves effectif : on s'arrête quand la longueur d'onde passe sous `detail_min_m`.
|
||||||
|
#[inline]
|
||||||
|
pub fn octaves_effectives(p: &ParamsRelief, detail_min_m: f32) -> u32 {
|
||||||
|
let mut l = p.echelle_m;
|
||||||
|
let mut k = 0;
|
||||||
|
while k < p.octaves as u32 && l >= detail_min_m {
|
||||||
|
l /= p.lacunarite;
|
||||||
|
k += 1;
|
||||||
|
}
|
||||||
|
k.max(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn fbm_n(b: &dyn Bruit2, x: f64, z: f64, p: &ParamsRelief, n: u32) -> f32 {
|
||||||
|
let mut f = 1.0 / p.echelle_m as f64;
|
||||||
|
let (mut a, mut s, mut v) = (1.0f32, 0.0f32, 0.0f32);
|
||||||
|
for k in 0..n {
|
||||||
|
let o = k as f64 * 37.13;
|
||||||
|
v += a * b.val(x * f + o, z * f - o);
|
||||||
|
s += a;
|
||||||
|
a *= p.gain;
|
||||||
|
f *= p.lacunarite as f64;
|
||||||
|
}
|
||||||
|
v / s
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn ridged_n(b: &dyn Bruit2, x: f64, z: f64, p: &ParamsRelief, n: u32) -> f32 {
|
||||||
|
let mut f = 1.0 / p.echelle_m as f64;
|
||||||
|
let (mut a, mut s, mut v, mut w) = (1.0f32, 0.0f32, 0.0f32, 1.0f32);
|
||||||
|
for k in 0..n {
|
||||||
|
let o = k as f64 * 53.71 + 1000.0;
|
||||||
|
let r = 1.0 - b.val(x * f + o, z * f - o).abs();
|
||||||
|
let r = r * r;
|
||||||
|
v += a * r * w;
|
||||||
|
s += a;
|
||||||
|
w = (r * 1.6).clamp(0.0, 1.0);
|
||||||
|
a *= p.gain;
|
||||||
|
f *= p.lacunarite as f64;
|
||||||
|
}
|
||||||
|
v / s
|
||||||
|
}
|
||||||
|
|
||||||
|
/// fBm normalisé (≈ gaussien centré, σ ≈ `SIGMA_FBM`).
|
||||||
|
pub fn fbm(b: &dyn Bruit2, x: f64, z: f64, p: &ParamsRelief) -> f32 {
|
||||||
|
fbm_n(b, x, z, p, p.octaves as u32)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ridged multifractal normalisé [0, 1] (moyenne ≈ `MOY_RIDGED`).
|
||||||
|
pub fn ridged(b: &dyn Bruit2, x: f64, z: f64, p: &ParamsRelief) -> f32 {
|
||||||
|
ridged_n(b, x, z, p, p.octaves as u32)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn fbm_lod(b: &dyn Bruit2, x: f64, z: f64, p: &ParamsRelief, lod: f32) -> f32 {
|
||||||
|
fbm_n(b, x, z, p, octaves_effectives(p, lod))
|
||||||
|
}
|
||||||
|
pub(crate) fn ridged_lod(b: &dyn Bruit2, x: f64, z: f64, p: &ParamsRelief, lod: f32) -> f32 {
|
||||||
|
ridged_n(b, x, z, p, octaves_effectives(p, lod))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Domain warping à 3 octaves : déplace (x, z) d'au plus ≈ `amp_m`.
|
||||||
|
pub fn warp(x: f64, z: f64, seed: u64, amp_m: f32, ech_m: f32) -> (f64, f64) {
|
||||||
|
if amp_m <= 0.0 {
|
||||||
|
return (x, z);
|
||||||
|
}
|
||||||
|
let (s1, s2) = (sous_graine(seed, 101), sous_graine(seed, 102));
|
||||||
|
let mut f = 1.0 / ech_m as f64;
|
||||||
|
let (mut a, mut dx, mut dz, mut s) = (1.0f64, 0.0f64, 0.0f64, 0.0f64);
|
||||||
|
for k in 0..3 {
|
||||||
|
let o = k as f64 * 17.7;
|
||||||
|
dx += a * simplex2(s1, x * f + o, z * f) as f64;
|
||||||
|
dz += a * simplex2(s2, x * f, z * f + o) as f64;
|
||||||
|
s += a;
|
||||||
|
a *= 0.5;
|
||||||
|
f *= 2.0;
|
||||||
|
}
|
||||||
|
(x + amp_m as f64 * dx / s, z + amp_m as f64 * dz / s)
|
||||||
|
}
|
||||||
@@ -0,0 +1,153 @@
|
|||||||
|
//! Érosion hydraulique (gouttelettes, d'après S. Lague) et thermique (talus),
|
||||||
|
//! sur une grille carrée `n × n` de pas `pas` m. Entièrement déterministe :
|
||||||
|
//! les gouttelettes sont traitées dans l'ordre, chacune avec sa graine `hash(seed, i)`.
|
||||||
|
|
||||||
|
use crate::bruit::{hash2, unif};
|
||||||
|
use crate::relief::ParamsErosion;
|
||||||
|
|
||||||
|
const INERTIE: f32 = 0.05;
|
||||||
|
const PENTE_MIN: f32 = 0.01;
|
||||||
|
const VITESSE_EROSION: f32 = 0.3;
|
||||||
|
const GRAVITE: f32 = 4.0;
|
||||||
|
const PAS_MAX: usize = 64;
|
||||||
|
/// Limites anti-ravines sur pentes très raides (fjord, glaciaire) [À VÉRIFIER].
|
||||||
|
const EROSION_MAX_PAS: f32 = 0.5;
|
||||||
|
const VITESSE_MAX: f32 = 6.0;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn hauteur_grad(h: &[f32], n: usize, px: f32, pz: f32) -> (f32, f32, f32) {
|
||||||
|
let (ix, iz) = (px as usize, pz as usize);
|
||||||
|
let (fx, fz) = (px - ix as f32, pz - iz as f32);
|
||||||
|
let i = iz * n + ix;
|
||||||
|
let (a, b, c, d) = (h[i], h[i + 1], h[i + n], h[i + n + 1]);
|
||||||
|
let gx = (b - a) * (1.0 - fz) + (d - c) * fz;
|
||||||
|
let gz = (c - a) * (1.0 - fx) + (d - b) * fx;
|
||||||
|
let hh = a * (1.0 - fx) * (1.0 - fz) + b * fx * (1.0 - fz) + c * (1.0 - fx) * fz + d * fx * fz;
|
||||||
|
(hh, gx, gz)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn deposer(h: &mut [f32], n: usize, px: f32, pz: f32, q: f32) {
|
||||||
|
let (ix, iz) = (px as usize, pz as usize);
|
||||||
|
let (fx, fz) = (px - ix as f32, pz - iz as f32);
|
||||||
|
let i = iz * n + ix;
|
||||||
|
h[i] += q * (1.0 - fx) * (1.0 - fz);
|
||||||
|
h[i + 1] += q * fx * (1.0 - fz);
|
||||||
|
h[i + n] += q * (1.0 - fx) * fz;
|
||||||
|
h[i + n + 1] += q * fx * fz;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `h` en mètres ; les pentes sont exprimées en m par maille.
|
||||||
|
pub fn hydraulique(h: &mut [f32], n: usize, e: &ParamsErosion, seed: u64) {
|
||||||
|
let lim = (n - 1) as f32;
|
||||||
|
for g in 0..e.iterations_hydro {
|
||||||
|
let r = hash2(seed, g as i64, 0x51);
|
||||||
|
let mut px = unif(r) * (lim - 1.0);
|
||||||
|
let mut pz = unif(r.rotate_left(29)) * (lim - 1.0);
|
||||||
|
let (mut dx, mut dz) = (0.0f32, 0.0f32);
|
||||||
|
let (mut v, mut eau, mut sed) = (1.0f32, e.pluie, 0.0f32);
|
||||||
|
for _ in 0..PAS_MAX {
|
||||||
|
let (h0, gx, gz) = hauteur_grad(h, n, px, pz);
|
||||||
|
dx = dx * INERTIE - gx * (1.0 - INERTIE);
|
||||||
|
dz = dz * INERTIE - gz * (1.0 - INERTIE);
|
||||||
|
let l = (dx * dx + dz * dz).sqrt();
|
||||||
|
if l < 1e-6 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
dx /= l;
|
||||||
|
dz /= l;
|
||||||
|
let (nx, nz) = (px + dx, pz + dz);
|
||||||
|
if nx < 0.0 || nz < 0.0 || nx >= lim || nz >= lim {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let (h1, _, _) = hauteur_grad(h, n, nx, nz);
|
||||||
|
let dh = h1 - h0;
|
||||||
|
let cap = (-dh).max(PENTE_MIN) * v * eau * e.capacite;
|
||||||
|
if sed > cap || dh > 0.0 {
|
||||||
|
let q = if dh > 0.0 { dh.min(sed) } else { (sed - cap) * e.depot };
|
||||||
|
sed -= q;
|
||||||
|
deposer(h, n, px, pz, q);
|
||||||
|
} else {
|
||||||
|
let q = ((cap - sed) * VITESSE_EROSION).min(-dh).min(EROSION_MAX_PAS);
|
||||||
|
sed += q;
|
||||||
|
deposer(h, n, px, pz, -q);
|
||||||
|
}
|
||||||
|
v = (v * v - dh * GRAVITE).max(0.0).sqrt().min(VITESSE_MAX);
|
||||||
|
eau *= 1.0 - e.evaporation;
|
||||||
|
px = nx;
|
||||||
|
pz = nz;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Glissement thermique (Jacobi, donc indépendant de l'ordre de parcours).
|
||||||
|
pub fn thermique(h: &mut [f32], n: usize, pas: f32, iterations: u32, angle_deg: f32) {
|
||||||
|
let talus = angle_deg.to_radians().tan() * pas;
|
||||||
|
let mut d = vec![0.0f32; n * n];
|
||||||
|
for _ in 0..iterations {
|
||||||
|
d.iter_mut().for_each(|v| *v = 0.0);
|
||||||
|
for z in 1..n - 1 {
|
||||||
|
for x in 1..n - 1 {
|
||||||
|
let i = z * n + x;
|
||||||
|
let h0 = h[i];
|
||||||
|
let vois = [i - 1, i + 1, i - n, i + n];
|
||||||
|
let (mut tot, mut maxd) = (0.0f32, 0.0f32);
|
||||||
|
for &j in &vois {
|
||||||
|
let df = h0 - h[j] - talus;
|
||||||
|
if df > 0.0 {
|
||||||
|
tot += df;
|
||||||
|
maxd = maxd.max(df);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if tot > 0.0 {
|
||||||
|
let mv = 0.25 * maxd;
|
||||||
|
for &j in &vois {
|
||||||
|
let df = h0 - h[j] - talus;
|
||||||
|
if df > 0.0 {
|
||||||
|
let s = mv * df / tot;
|
||||||
|
d[i] -= s;
|
||||||
|
d[j] += s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (a, b) in h.iter_mut().zip(&d) {
|
||||||
|
*a += *b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Accumulation de flux D8 (en nombre de mailles drainées, y compris la maille).
|
||||||
|
pub fn accumulation_d8(h: &[f32], n: usize) -> Vec<f32> {
|
||||||
|
let mut ordre: Vec<u32> = (0..(n * n) as u32).collect();
|
||||||
|
ordre.sort_unstable_by(|&a, &b| h[b as usize].total_cmp(&h[a as usize]).then(a.cmp(&b)));
|
||||||
|
let mut acc = vec![1.0f32; n * n];
|
||||||
|
const D: [(i32, i32, f32); 8] = [
|
||||||
|
(1, 0, 1.0), (-1, 0, 1.0), (0, 1, 1.0), (0, -1, 1.0),
|
||||||
|
(1, 1, std::f32::consts::SQRT_2), (-1, 1, std::f32::consts::SQRT_2),
|
||||||
|
(1, -1, std::f32::consts::SQRT_2), (-1, -1, std::f32::consts::SQRT_2),
|
||||||
|
];
|
||||||
|
for &c in &ordre {
|
||||||
|
let c = c as usize;
|
||||||
|
let (x, z) = ((c % n) as i32, (c / n) as i32);
|
||||||
|
let mut best = None;
|
||||||
|
let mut bp = 0.0f32;
|
||||||
|
for &(dx, dz, l) in &D {
|
||||||
|
let (xx, zz) = (x + dx, z + dz);
|
||||||
|
if xx < 0 || zz < 0 || xx >= n as i32 || zz >= n as i32 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let j = zz as usize * n + xx as usize;
|
||||||
|
let p = (h[c] - h[j]) / l;
|
||||||
|
if p > bp {
|
||||||
|
bp = p;
|
||||||
|
best = Some(j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(j) = best {
|
||||||
|
acc[j] += acc[c];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
acc
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
//! Générateur procédural de relief (agent 5 du cadrage 20).
|
||||||
|
//!
|
||||||
|
//! Chaîne : warp → fBm / ridged → remappage hypsométrique → plateau → terrasses
|
||||||
|
//! → auges glaciaires / incision → dolines → érosion hydraulique + thermique par
|
||||||
|
//! tuile de 512 m (marge 128 m, raccord 32 m). Tout est déterministe (graine) et
|
||||||
|
//! `Send + Sync`.
|
||||||
|
|
||||||
|
pub mod bruit;
|
||||||
|
pub mod erosion;
|
||||||
|
pub mod params;
|
||||||
|
pub mod relief;
|
||||||
|
pub mod tuile;
|
||||||
|
pub mod types;
|
||||||
|
|
||||||
|
pub use bruit::{fbm, ridged, warp, Bruit2, Simplex};
|
||||||
|
pub use params::{params_initiaux, table_initiale};
|
||||||
|
pub use relief::*;
|
||||||
|
pub use tuile::{generer_tuile, Generateur, TileCache, TuileErodee, TuileRelief};
|
||||||
|
pub use types::*;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn _verif_send_sync() {
|
||||||
|
fn ok<T: Send + Sync>() {}
|
||||||
|
ok::<Generateur>();
|
||||||
|
ok::<TileCache>();
|
||||||
|
ok::<Simplex>();
|
||||||
|
ok::<ParamsRelief>();
|
||||||
|
}
|
||||||
@@ -0,0 +1,188 @@
|
|||||||
|
//! Jeu de paramètres initial par biome (règles du § 4.2 du cadrage, estimées
|
||||||
|
//! à partir des altitudes typiques et de la connaissance des régions de référence).
|
||||||
|
//! Tout est [À VÉRIFIER] : l'agent 6 calera ces valeurs sur les signatures mesurées.
|
||||||
|
|
||||||
|
use crate::relief::*;
|
||||||
|
use crate::types::BiomeRelief;
|
||||||
|
|
||||||
|
const LIN: [f32; 11] = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0];
|
||||||
|
|
||||||
|
fn ero(hydro: u32, therm: u32, angle: f32) -> ParamsErosion {
|
||||||
|
ParamsErosion {
|
||||||
|
iterations_hydro: hydro,
|
||||||
|
pluie: 1.0,
|
||||||
|
capacite: 4.0,
|
||||||
|
depot: 0.3,
|
||||||
|
evaporation: 0.02,
|
||||||
|
iterations_therm: therm,
|
||||||
|
angle_talus_deg: angle,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn params_initiaux(b: BiomeRelief) -> ParamsRelief {
|
||||||
|
let base = ParamsRelief {
|
||||||
|
base_m: 0.0,
|
||||||
|
amplitude_m: 100.0,
|
||||||
|
octaves: 10,
|
||||||
|
lacunarite: 2.0,
|
||||||
|
gain: 0.5,
|
||||||
|
echelle_m: 4000.0,
|
||||||
|
ridge: 0.0,
|
||||||
|
warp_m: 0.0,
|
||||||
|
warp_echelle_m: 2000.0,
|
||||||
|
courbe_hypso: LIN,
|
||||||
|
anisotropie: None,
|
||||||
|
terrasses: None,
|
||||||
|
plateau: None,
|
||||||
|
dolines: None,
|
||||||
|
incision: None,
|
||||||
|
glaciaire: None,
|
||||||
|
erosion: ero(20_000, 10, 35.0),
|
||||||
|
};
|
||||||
|
match b {
|
||||||
|
// 400-2400 m, falaises étagées (Urgonien / Tithonique), hauts plateaux lapiazés
|
||||||
|
BiomeRelief::Prealpes => ParamsRelief {
|
||||||
|
base_m: 400.0,
|
||||||
|
amplitude_m: 2000.0,
|
||||||
|
octaves: 11,
|
||||||
|
gain: 0.5,
|
||||||
|
echelle_m: 6000.0,
|
||||||
|
ridge: 0.35,
|
||||||
|
warp_m: 350.0,
|
||||||
|
warp_echelle_m: 2500.0,
|
||||||
|
courbe_hypso: [0.0, 0.06, 0.13, 0.21, 0.30, 0.40, 0.52, 0.64, 0.76, 0.88, 1.0],
|
||||||
|
anisotropie: Some(Anisotropie { angle_deg: 30.0, rapport: 1.6 }),
|
||||||
|
terrasses: Some(Terrasses { hauteur_banc_m: 70.0, raideur: 0.85, melange: 0.65, bruit_m: 20.0 }),
|
||||||
|
plateau: Some(Plateau { seuil: 0.75, douceur: 0.45 }),
|
||||||
|
dolines: Some(Dolines { densite_km2: 4.0, rayon_m: 25.0, profondeur_m: 6.0 }),
|
||||||
|
incision: Some(Incision { largeur_m: 300.0, profondeur_m: 250.0, echelle_m: 5000.0, raideur_parois: 2.5 }),
|
||||||
|
erosion: ero(30_000, 25, 37.0),
|
||||||
|
..base.clone()
|
||||||
|
},
|
||||||
|
// 1500-4000+ m, arêtes vives, auges en U
|
||||||
|
BiomeRelief::Glaciaire => ParamsRelief {
|
||||||
|
base_m: 1300.0,
|
||||||
|
amplitude_m: 3000.0,
|
||||||
|
octaves: 11,
|
||||||
|
gain: 0.53,
|
||||||
|
echelle_m: 8000.0,
|
||||||
|
ridge: 0.8,
|
||||||
|
warp_m: 400.0,
|
||||||
|
warp_echelle_m: 3000.0,
|
||||||
|
courbe_hypso: [0.0, 0.07, 0.15, 0.24, 0.34, 0.45, 0.56, 0.67, 0.78, 0.89, 1.0],
|
||||||
|
glaciaire: Some(Glaciaire { largeur_m: 1400.0, profondeur_m: 700.0, echelle_m: 9000.0, fond_plat: 0.3 }),
|
||||||
|
erosion: ero(40_000, 35, 36.0),
|
||||||
|
..base.clone()
|
||||||
|
},
|
||||||
|
// 300-1200 m, causse plat, dolines, canyons
|
||||||
|
BiomeRelief::Karst => ParamsRelief {
|
||||||
|
base_m: 300.0,
|
||||||
|
amplitude_m: 900.0,
|
||||||
|
octaves: 10,
|
||||||
|
gain: 0.45,
|
||||||
|
echelle_m: 7000.0,
|
||||||
|
ridge: 0.1,
|
||||||
|
warp_m: 250.0,
|
||||||
|
warp_echelle_m: 3000.0,
|
||||||
|
courbe_hypso: [0.0, 0.18, 0.36, 0.52, 0.64, 0.73, 0.80, 0.86, 0.91, 0.96, 1.0],
|
||||||
|
plateau: Some(Plateau { seuil: 0.6, douceur: 0.25 }),
|
||||||
|
dolines: Some(Dolines { densite_km2: 25.0, rayon_m: 40.0, profondeur_m: 12.0 }),
|
||||||
|
incision: Some(Incision { largeur_m: 400.0, profondeur_m: 350.0, echelle_m: 12000.0, raideur_parois: 6.0 }),
|
||||||
|
erosion: ero(12_000, 15, 38.0),
|
||||||
|
..base.clone()
|
||||||
|
},
|
||||||
|
// 400-1500 m, plateaux entaillés de canyons à parois verticales
|
||||||
|
BiomeRelief::Gorges => ParamsRelief {
|
||||||
|
base_m: 400.0,
|
||||||
|
amplitude_m: 1000.0,
|
||||||
|
octaves: 10,
|
||||||
|
gain: 0.48,
|
||||||
|
echelle_m: 6000.0,
|
||||||
|
ridge: 0.2,
|
||||||
|
warp_m: 300.0,
|
||||||
|
warp_echelle_m: 1500.0,
|
||||||
|
courbe_hypso: [0.0, 0.15, 0.30, 0.45, 0.58, 0.68, 0.76, 0.83, 0.89, 0.95, 1.0],
|
||||||
|
plateau: Some(Plateau { seuil: 0.5, douceur: 0.3 }),
|
||||||
|
terrasses: Some(Terrasses { hauteur_banc_m: 45.0, raideur: 0.85, melange: 0.5, bruit_m: 10.0 }),
|
||||||
|
dolines: Some(Dolines { densite_km2: 6.0, rayon_m: 30.0, profondeur_m: 8.0 }),
|
||||||
|
incision: Some(Incision { largeur_m: 500.0, profondeur_m: 500.0, echelle_m: 7000.0, raideur_parois: 8.0 }),
|
||||||
|
erosion: ero(20_000, 20, 40.0),
|
||||||
|
..base.clone()
|
||||||
|
},
|
||||||
|
// 50-400 m, collines douces, vallons en V
|
||||||
|
BiomeRelief::Bocage => ParamsRelief {
|
||||||
|
base_m: 50.0,
|
||||||
|
amplitude_m: 340.0,
|
||||||
|
octaves: 10,
|
||||||
|
gain: 0.5,
|
||||||
|
echelle_m: 2500.0,
|
||||||
|
warp_m: 250.0,
|
||||||
|
warp_echelle_m: 1500.0,
|
||||||
|
courbe_hypso: [0.0, 0.08, 0.17, 0.27, 0.37, 0.48, 0.58, 0.69, 0.79, 0.90, 1.0],
|
||||||
|
incision: Some(Incision { largeur_m: 160.0, profondeur_m: 45.0, echelle_m: 1800.0, raideur_parois: 1.0 }),
|
||||||
|
erosion: ero(25_000, 8, 33.0),
|
||||||
|
..base.clone()
|
||||||
|
},
|
||||||
|
// -30-120 m, falaises côtières, côte découpée
|
||||||
|
BiomeRelief::CoteRocheuse => ParamsRelief {
|
||||||
|
base_m: -40.0,
|
||||||
|
amplitude_m: 170.0,
|
||||||
|
octaves: 10,
|
||||||
|
gain: 0.55,
|
||||||
|
echelle_m: 1500.0,
|
||||||
|
ridge: 0.3,
|
||||||
|
warp_m: 350.0,
|
||||||
|
warp_echelle_m: 800.0,
|
||||||
|
// marche de 0,24 → 0,45 : falaise de ≈ 35 m au niveau de la mer
|
||||||
|
courbe_hypso: [0.0, 0.08, 0.15, 0.20, 0.235, 0.47, 0.60, 0.70, 0.79, 0.89, 1.0],
|
||||||
|
incision: Some(Incision { largeur_m: 120.0, profondeur_m: 30.0, echelle_m: 1500.0, raideur_parois: 1.5 }),
|
||||||
|
erosion: ero(15_000, 15, 40.0),
|
||||||
|
..base.clone()
|
||||||
|
},
|
||||||
|
// -300-1700 m, auges profondes noyées, parois raides
|
||||||
|
BiomeRelief::Fjord => ParamsRelief {
|
||||||
|
base_m: 0.0,
|
||||||
|
amplitude_m: 1700.0,
|
||||||
|
octaves: 11,
|
||||||
|
gain: 0.52,
|
||||||
|
echelle_m: 7000.0,
|
||||||
|
ridge: 0.55,
|
||||||
|
warp_m: 500.0,
|
||||||
|
warp_echelle_m: 4000.0,
|
||||||
|
courbe_hypso: [0.0, 0.10, 0.22, 0.36, 0.50, 0.62, 0.72, 0.80, 0.87, 0.94, 1.0],
|
||||||
|
glaciaire: Some(Glaciaire { largeur_m: 1800.0, profondeur_m: 1300.0, echelle_m: 8000.0, fond_plat: 0.25 }),
|
||||||
|
erosion: ero(30_000, 30, 38.0),
|
||||||
|
..base.clone()
|
||||||
|
},
|
||||||
|
// 0-150 m, très plat, méandres
|
||||||
|
BiomeRelief::Plaine => ParamsRelief {
|
||||||
|
base_m: 5.0,
|
||||||
|
amplitude_m: 140.0,
|
||||||
|
octaves: 9,
|
||||||
|
gain: 0.42,
|
||||||
|
echelle_m: 5000.0,
|
||||||
|
warp_m: 600.0,
|
||||||
|
warp_echelle_m: 2000.0,
|
||||||
|
courbe_hypso: [0.0, 0.02, 0.05, 0.08, 0.12, 0.17, 0.23, 0.31, 0.43, 0.62, 1.0],
|
||||||
|
incision: Some(Incision { largeur_m: 300.0, profondeur_m: 8.0, echelle_m: 6000.0, raideur_parois: 2.0 }),
|
||||||
|
erosion: ero(10_000, 5, 30.0),
|
||||||
|
..base.clone()
|
||||||
|
},
|
||||||
|
// plateau continental : -200 à +20 m
|
||||||
|
BiomeRelief::Mer => ParamsRelief {
|
||||||
|
base_m: -200.0,
|
||||||
|
amplitude_m: 210.0,
|
||||||
|
octaves: 8,
|
||||||
|
gain: 0.45,
|
||||||
|
echelle_m: 8000.0,
|
||||||
|
courbe_hypso: [0.0, 0.15, 0.30, 0.45, 0.58, 0.68, 0.76, 0.83, 0.89, 0.95, 1.0],
|
||||||
|
erosion: ero(0, 4, 30.0),
|
||||||
|
..base
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Table des 9 biomes (indexée par `BiomeRelief::index`).
|
||||||
|
pub fn table_initiale() -> Vec<ParamsRelief> {
|
||||||
|
BiomeRelief::TOUS.iter().map(|&b| params_initiaux(b)).collect()
|
||||||
|
}
|
||||||
@@ -0,0 +1,218 @@
|
|||||||
|
//! `ParamsRelief` et hauteur brute (avant érosion) d'une colonne.
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::bruit::{fbm_lod, ridged_lod, simplex2, sous_graine, unif, hash2, warp, Simplex};
|
||||||
|
use crate::types::BiomeRelief;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
||||||
|
pub struct ParamsRelief {
|
||||||
|
pub base_m: f32,
|
||||||
|
pub amplitude_m: f32,
|
||||||
|
pub octaves: u8,
|
||||||
|
pub lacunarite: f32,
|
||||||
|
pub gain: f32,
|
||||||
|
pub echelle_m: f32,
|
||||||
|
pub ridge: f32,
|
||||||
|
pub warp_m: f32,
|
||||||
|
pub warp_echelle_m: f32,
|
||||||
|
pub courbe_hypso: [f32; 11],
|
||||||
|
#[serde(default)]
|
||||||
|
pub anisotropie: Option<Anisotropie>,
|
||||||
|
pub terrasses: Option<Terrasses>,
|
||||||
|
pub plateau: Option<Plateau>,
|
||||||
|
pub dolines: Option<Dolines>,
|
||||||
|
pub incision: Option<Incision>,
|
||||||
|
pub glaciaire: Option<Glaciaire>,
|
||||||
|
pub erosion: ParamsErosion,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
|
||||||
|
pub struct ParamsErosion {
|
||||||
|
/// Nombre de gouttelettes par tuile élargie (768 × 768 m).
|
||||||
|
pub iterations_hydro: u32,
|
||||||
|
pub pluie: f32,
|
||||||
|
pub capacite: f32,
|
||||||
|
pub depot: f32,
|
||||||
|
pub evaporation: f32,
|
||||||
|
pub iterations_therm: u32,
|
||||||
|
pub angle_talus_deg: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Étirement du domaine : direction des chaînes (aspect_hist).
|
||||||
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
|
||||||
|
pub struct Anisotropie {
|
||||||
|
pub angle_deg: f32,
|
||||||
|
/// > 1 : structures allongées dans la direction `angle_deg`.
|
||||||
|
pub rapport: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Bancs calcaires : falaises étagées.
|
||||||
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
|
||||||
|
pub struct Terrasses {
|
||||||
|
pub hauteur_banc_m: f32,
|
||||||
|
/// 0..1 : part du banc occupée par la vire plate (1 = falaise verticale).
|
||||||
|
pub raideur: f32,
|
||||||
|
/// 0..1 : mélange avec la hauteur lisse.
|
||||||
|
pub melange: f32,
|
||||||
|
/// Bruit (m) sur la position des bancs, pour éviter des courbes de niveau parfaites.
|
||||||
|
pub bruit_m: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Écrêtage doux : au-dessus de `seuil` (hauteur normalisée), la pente est multipliée par `douceur`.
|
||||||
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
|
||||||
|
pub struct Plateau {
|
||||||
|
pub seuil: f32,
|
||||||
|
pub douceur: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Champ de cuvettes (Worley).
|
||||||
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
|
||||||
|
pub struct Dolines {
|
||||||
|
pub densite_km2: f32,
|
||||||
|
pub rayon_m: f32,
|
||||||
|
pub profondeur_m: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Vallées fluviales creusées le long d'un réseau de lignes de bruit.
|
||||||
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
|
||||||
|
pub struct Incision {
|
||||||
|
pub largeur_m: f32,
|
||||||
|
pub profondeur_m: f32,
|
||||||
|
pub echelle_m: f32,
|
||||||
|
/// Exposant du profil : 1 = V, 2 = U, ≥ 6 = canyon à parois verticales.
|
||||||
|
pub raideur_parois: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Auges glaciaires (profil en U, fond plat) et épaulements.
|
||||||
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
|
||||||
|
pub struct Glaciaire {
|
||||||
|
pub largeur_m: f32,
|
||||||
|
pub profondeur_m: f32,
|
||||||
|
pub echelle_m: f32,
|
||||||
|
/// 0..1 : part plate du fond d'auge.
|
||||||
|
pub fond_plat: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Statistiques mesurées du bruit (examples/stats.rs) pour la fonction de répartition.
|
||||||
|
pub const SIGMA_FBM: f32 = 0.31;
|
||||||
|
pub const MOY_RIDGED: f32 = 0.27;
|
||||||
|
pub const SIGMA_RIDGED: f32 = 0.19;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn repartition(x: f32) -> f32 {
|
||||||
|
// approximation logistique de la loi normale
|
||||||
|
1.0 / (1.0 + (-1.702 * x).exp())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn courbe(c: &[f32; 11], u: f32) -> f32 {
|
||||||
|
let t = (u.clamp(0.0, 1.0)) * 10.0;
|
||||||
|
let i = (t as usize).min(9);
|
||||||
|
let f = t - i as f32;
|
||||||
|
c[i] + (c[i + 1] - c[i]) * f
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn lisse(a: f32, b: f32, x: f32) -> f32 {
|
||||||
|
let t = ((x - a) / (b - a)).clamp(0.0, 1.0);
|
||||||
|
t * t * (3.0 - 2.0 * t)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Distance approximative (m) au réseau de lignes de crête « zéro » d'un bruit : ce réseau sert de vallées.
|
||||||
|
#[inline]
|
||||||
|
fn distance_reseau(seed: u64, x: f64, z: f64, ech_m: f32) -> f32 {
|
||||||
|
let f = 1.0 / ech_m as f64;
|
||||||
|
let v = simplex2(seed, x * f, z * f) + 0.35 * simplex2(seed ^ 0x55, x * f * 2.3 + 7.1, z * f * 2.3);
|
||||||
|
// |∇simplex| ≈ 2,5 par unité de domaine [À VÉRIFIER : calé à l'œil]
|
||||||
|
v.abs() / 1.35 * ech_m * 0.4
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn dolines(seed: u64, x: f64, z: f64, d: &Dolines) -> f32 {
|
||||||
|
let cell = (1000.0 / d.densite_km2.max(0.01).sqrt()) as f64;
|
||||||
|
let (cx, cz) = ((x / cell).floor() as i64, (z / cell).floor() as i64);
|
||||||
|
let mut creux = 0.0f32;
|
||||||
|
for dz in -1..=1 {
|
||||||
|
for dx in -1..=1 {
|
||||||
|
let (i, j) = (cx + dx, cz + dz);
|
||||||
|
let h = hash2(seed, i, j);
|
||||||
|
let px = (i as f64 + unif(h) as f64) * cell;
|
||||||
|
let pz = (j as f64 + unif(h.rotate_left(24)) as f64) * cell;
|
||||||
|
let r = d.rayon_m * (0.6 + 0.8 * unif(h.rotate_left(48)));
|
||||||
|
let dist = (((x - px).powi(2) + (z - pz).powi(2)).sqrt()) as f32;
|
||||||
|
if dist < r {
|
||||||
|
let q = 1.0 - (dist / r).powi(2);
|
||||||
|
creux = creux.max(d.profondeur_m * (r / d.rayon_m) * q * q);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
creux
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Hauteur brute (m) d'une colonne, sans érosion, au niveau de détail maximal.
|
||||||
|
pub fn hauteur_brute(biome: BiomeRelief, p: &ParamsRelief, seed: u64, x: f64, z: f64) -> f32 {
|
||||||
|
hauteur_brute_lod(biome, p, seed, x, z, 1.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Variante à niveau de détail : les octaves de longueur d'onde < `lod_m` sont omises
|
||||||
|
/// (utilisée pour la grille d'érosion à 2 m).
|
||||||
|
pub fn hauteur_brute_lod(_biome: BiomeRelief, p: &ParamsRelief, seed: u64, x: f64, z: f64, lod_m: f32) -> f32 {
|
||||||
|
// anisotropie : rotation + étirement
|
||||||
|
let (mut x, mut z) = (x, z);
|
||||||
|
if let Some(a) = p.anisotropie {
|
||||||
|
let (s, c) = (a.angle_deg.to_radians() as f64).sin_cos();
|
||||||
|
let (u, v) = (x * c + z * s, -x * s + z * c);
|
||||||
|
x = u / a.rapport as f64;
|
||||||
|
z = v;
|
||||||
|
}
|
||||||
|
let (ox, oz) = (x, z);
|
||||||
|
let (wx, wz) = warp(x, z, sous_graine(seed, 1), p.warp_m, p.warp_echelle_m);
|
||||||
|
|
||||||
|
let b = Simplex::new(sous_graine(seed, 2));
|
||||||
|
let mut u = repartition(fbm_lod(&b, wx, wz, p, lod_m) / SIGMA_FBM);
|
||||||
|
if p.ridge > 0.0 {
|
||||||
|
let br = Simplex::new(sous_graine(seed, 3));
|
||||||
|
let ur = repartition((ridged_lod(&br, wx, wz, p, lod_m) - MOY_RIDGED) / SIGMA_RIDGED);
|
||||||
|
u += (ur - u) * p.ridge;
|
||||||
|
}
|
||||||
|
let mut hn = courbe(&p.courbe_hypso, u);
|
||||||
|
|
||||||
|
if let Some(pl) = p.plateau {
|
||||||
|
if hn > pl.seuil {
|
||||||
|
hn = pl.seuil + (hn - pl.seuil) * pl.douceur;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut h = p.base_m + p.amplitude_m * hn;
|
||||||
|
|
||||||
|
if let Some(t) = p.terrasses {
|
||||||
|
let j = t.bruit_m * simplex2(sous_graine(seed, 4), ox / 300.0, oz / 300.0);
|
||||||
|
let k = (h - p.base_m + j) / t.hauteur_banc_m;
|
||||||
|
let fl = k.floor();
|
||||||
|
let fr = k - fl;
|
||||||
|
let w = (1.0 - t.raideur).clamp(0.03, 1.0);
|
||||||
|
let ht = p.base_m + (fl + lisse(1.0 - w, 1.0, fr)) * t.hauteur_banc_m - j;
|
||||||
|
h += (ht - h) * t.melange;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(g) = p.glaciaire {
|
||||||
|
let d = distance_reseau(sous_graine(seed, 6), wx, wz, g.echelle_m);
|
||||||
|
let l = g.largeur_m * 0.5;
|
||||||
|
if d < l {
|
||||||
|
let r = ((d / l - g.fond_plat) / (1.0 - g.fond_plat)).max(0.0);
|
||||||
|
// profil en U : y = a·d², fond plat
|
||||||
|
h -= g.profondeur_m * (1.0 - r * r) * (0.6 + 0.4 * hn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(i) = p.incision {
|
||||||
|
let d = distance_reseau(sous_graine(seed, 7), wx, wz, i.echelle_m);
|
||||||
|
let l = i.largeur_m * 0.5;
|
||||||
|
if d < l {
|
||||||
|
h -= i.profondeur_m * (1.0 - (d / l).powf(i.raideur_parois)) * (0.3 + 0.7 * hn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(d) = p.dolines {
|
||||||
|
h -= dolines(sous_graine(seed, 5), ox, oz, &d);
|
||||||
|
}
|
||||||
|
h
|
||||||
|
}
|
||||||
@@ -0,0 +1,301 @@
|
|||||||
|
//! Tuiles de 512 m avec 128 m de marge, érosion, cache, et hauteur finale continue.
|
||||||
|
//!
|
||||||
|
//! Principe de continuité : chaque tuile `T` est érodée sur son emprise élargie
|
||||||
|
//! [-128, 640[ m (grille de 2 m). On garde la *différence* érodé − brut (`delta`).
|
||||||
|
//! La hauteur finale en (x, z) est `brut_1m(x, z) + Σ_T w_T(x, z) · delta_T(x, z)`,
|
||||||
|
//! où les poids `w_T` sont des trapèzes (rampe linéaire de 32 m centrée sur chaque
|
||||||
|
//! bord) qui forment une partition de l'unité. Le résultat ne dépend que de (x, z)
|
||||||
|
//! et de la graine : peu importe quelle tuile ou quel chunk le demande, et dans quel ordre.
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::sync::atomic::{AtomicU64, Ordering};
|
||||||
|
use std::sync::{Arc, Mutex, OnceLock};
|
||||||
|
|
||||||
|
use rayon::prelude::*;
|
||||||
|
|
||||||
|
use crate::bruit::{hash2, sous_graine};
|
||||||
|
use crate::erosion;
|
||||||
|
use crate::relief::{hauteur_brute_lod, ParamsErosion, ParamsRelief};
|
||||||
|
use crate::types::{BiomeRelief, PoidsBiomes, Roche, SourcePoids, TileId};
|
||||||
|
|
||||||
|
pub const TAILLE: usize = 512;
|
||||||
|
pub const TAILLE_M: f64 = 512.0;
|
||||||
|
pub const MARGE_M: f64 = 128.0;
|
||||||
|
pub const PAS_EROSION: f64 = 2.0;
|
||||||
|
/// Côté de la grille d'érosion élargie : (512 + 2·128) / 2.
|
||||||
|
pub const N_EXT: usize = ((TAILLE_M + 2.0 * MARGE_M) / PAS_EROSION) as usize;
|
||||||
|
/// Demi-largeur de la bande de raccord (32 m au total).
|
||||||
|
pub const DEMI_BANDE_M: f64 = 16.0;
|
||||||
|
|
||||||
|
/// Résultat de l'érosion d'une tuile élargie (pas de 2 m).
|
||||||
|
pub struct TuileErodee {
|
||||||
|
pub id: TileId,
|
||||||
|
/// érodé − brut, N_EXT², en m.
|
||||||
|
pub delta: Vec<f32>,
|
||||||
|
/// Accumulation D8 (mailles de 2 m) sur la grille érodée.
|
||||||
|
pub accumulation: Vec<f32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Tuile finale 512 × 512 au pas de 1 m.
|
||||||
|
pub struct TuileRelief {
|
||||||
|
pub id: TileId,
|
||||||
|
pub h: Vec<f32>,
|
||||||
|
/// Aire drainée en m² (D8 sur la grille de 2 m, rééchantillonnée).
|
||||||
|
pub accumulation: Vec<f32>,
|
||||||
|
pub roche: Vec<Roche>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cache LRU approximatif, 16 éclats (pas de verrou global). Chaque entrée est un
|
||||||
|
/// `OnceLock` : deux threads qui demandent la même tuile ne la calculent qu'une fois.
|
||||||
|
pub struct TileCache {
|
||||||
|
eclats: Vec<Mutex<HashMap<TileId, (Arc<OnceLock<Arc<TuileErodee>>>, u64)>>>,
|
||||||
|
capacite_eclat: usize,
|
||||||
|
horloge: AtomicU64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TileCache {
|
||||||
|
pub fn new(capacite: usize) -> Self {
|
||||||
|
let n = 16;
|
||||||
|
TileCache {
|
||||||
|
eclats: (0..n).map(|_| Mutex::new(HashMap::new())).collect(),
|
||||||
|
capacite_eclat: (capacite / n).max(2),
|
||||||
|
horloge: AtomicU64::new(0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_or_gen(&self, id: TileId, f: impl FnOnce() -> TuileErodee) -> Arc<TuileErodee> {
|
||||||
|
let k = (hash2(0, id.tx as i64, id.tz as i64) % self.eclats.len() as u64) as usize;
|
||||||
|
let t = self.horloge.fetch_add(1, Ordering::Relaxed);
|
||||||
|
let cell = {
|
||||||
|
let mut m = self.eclats[k].lock().unwrap();
|
||||||
|
let c = {
|
||||||
|
let e = m.entry(id).or_insert_with(|| (Arc::new(OnceLock::new()), t));
|
||||||
|
e.1 = t;
|
||||||
|
e.0.clone()
|
||||||
|
};
|
||||||
|
if m.len() > self.capacite_eclat {
|
||||||
|
if let Some(vieux) = m.iter().filter(|(k2, _)| **k2 != id).min_by_key(|(_, v)| v.1).map(|(k2, _)| *k2) {
|
||||||
|
m.remove(&vieux);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c
|
||||||
|
};
|
||||||
|
cell.get_or_init(|| Arc::new(f())).clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.eclats.iter().map(|e| e.lock().unwrap().len()).sum()
|
||||||
|
}
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.len() == 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Générateur complet : carte de poids + paramètres par biome + graine + cache.
|
||||||
|
pub struct Generateur {
|
||||||
|
pub seed: u64,
|
||||||
|
pub monde: Arc<dyn SourcePoids>,
|
||||||
|
pub params: Vec<ParamsRelief>,
|
||||||
|
pub cache: TileCache,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn trapeze(l: f64) -> f64 {
|
||||||
|
let b = DEMI_BANDE_M;
|
||||||
|
((l + b) / (2.0 * b)).clamp(0.0, 1.0) * ((TAILLE_M + b - l) / (2.0 * b)).clamp(0.0, 1.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Generateur {
|
||||||
|
pub fn new(seed: u64, monde: Arc<dyn SourcePoids>, params: Vec<ParamsRelief>) -> Self {
|
||||||
|
assert_eq!(params.len(), BiomeRelief::TOUS.len());
|
||||||
|
Generateur { seed, monde, params, cache: TileCache::new(256) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn biome_unique(seed: u64, b: BiomeRelief) -> Self {
|
||||||
|
Self::new(seed, Arc::new(crate::types::BiomeUnique(b)), crate::params::table_initiale())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Hauteur brute mélangée (mélange des hauteurs, pas des paramètres).
|
||||||
|
#[inline]
|
||||||
|
pub fn brut(&self, x: f64, z: f64, lod_m: f32) -> f32 {
|
||||||
|
let w = self.monde.poids(x, z);
|
||||||
|
self.brut_poids(&w, x, z, lod_m)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn brut_poids(&self, w: &PoidsBiomes, x: f64, z: f64, lod_m: f32) -> f32 {
|
||||||
|
let mut h = 0.0;
|
||||||
|
let mut s = 0.0;
|
||||||
|
for &(b, p) in &w.b {
|
||||||
|
if p > 0.0 {
|
||||||
|
h += p * hauteur_brute_lod(b, &self.params[b.index()], self.seed, x, z, lod_m);
|
||||||
|
s += p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
h / s.max(1e-6)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn erosion_tuile(&self, id: TileId) -> ParamsErosion {
|
||||||
|
let (cx, cz) = ((id.tx as f64 + 0.5) * TAILLE_M, (id.tz as f64 + 0.5) * TAILLE_M);
|
||||||
|
let w = self.monde.poids(cx, cz);
|
||||||
|
let mut e = self.params[w.dominant().index()].erosion;
|
||||||
|
// nombres moyennés selon les poids
|
||||||
|
let (mut hy, mut th, mut an, mut s) = (0.0f32, 0.0f32, 0.0f32, 0.0f32);
|
||||||
|
for &(b, p) in &w.b {
|
||||||
|
if p > 0.0 {
|
||||||
|
let pe = &self.params[b.index()].erosion;
|
||||||
|
hy += p * pe.iterations_hydro as f32;
|
||||||
|
th += p * pe.iterations_therm as f32;
|
||||||
|
an += p * pe.angle_talus_deg;
|
||||||
|
s += p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
e.iterations_hydro = (hy / s).round() as u32;
|
||||||
|
e.iterations_therm = (th / s).round() as u32;
|
||||||
|
e.angle_talus_deg = an / s;
|
||||||
|
e
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Calcule (sans cache) l'érosion de la tuile élargie.
|
||||||
|
pub fn eroder_tuile(&self, id: TileId) -> TuileErodee {
|
||||||
|
let n = N_EXT;
|
||||||
|
let ox = id.tx as f64 * TAILLE_M - MARGE_M;
|
||||||
|
let oz = id.tz as f64 * TAILLE_M - MARGE_M;
|
||||||
|
let mut brut = vec![0.0f32; n * n];
|
||||||
|
// volontairement séquentiel : l'initialisation d'un OnceLock ne doit pas
|
||||||
|
// voler de travail rayon (risque d'interblocage entre tuiles).
|
||||||
|
brut.chunks_mut(n).enumerate().for_each(|(j, ligne)| {
|
||||||
|
let z = oz + j as f64 * PAS_EROSION;
|
||||||
|
for (i, v) in ligne.iter_mut().enumerate() {
|
||||||
|
*v = self.brut(ox + i as f64 * PAS_EROSION, z, 2.0 * PAS_EROSION as f32);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let e = self.erosion_tuile(id);
|
||||||
|
let mut h = brut.clone();
|
||||||
|
let graine = sous_graine(hash2(self.seed, id.tx as i64, id.tz as i64), 9);
|
||||||
|
erosion::hydraulique(&mut h, n, &e, graine);
|
||||||
|
erosion::thermique(&mut h, n, PAS_EROSION as f32, e.iterations_therm, e.angle_talus_deg);
|
||||||
|
let accumulation = erosion::accumulation_d8(&h, n);
|
||||||
|
let delta = h.iter().zip(&brut).map(|(a, b)| a - b).collect();
|
||||||
|
TuileErodee { id, delta, accumulation }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn erodee(&self, id: TileId) -> Arc<TuileErodee> {
|
||||||
|
self.cache.get_or_gen(id, || self.eroder_tuile(id))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn delta_en(t: &TuileErodee, x: f64, z: f64) -> f32 {
|
||||||
|
let ox = t.id.tx as f64 * TAILLE_M - MARGE_M;
|
||||||
|
let oz = t.id.tz as f64 * TAILLE_M - MARGE_M;
|
||||||
|
let gx = ((x - ox) / PAS_EROSION).clamp(0.0, (N_EXT - 1) as f64 - 1e-6);
|
||||||
|
let gz = ((z - oz) / PAS_EROSION).clamp(0.0, (N_EXT - 1) as f64 - 1e-6);
|
||||||
|
let (ix, iz) = (gx as usize, gz as usize);
|
||||||
|
let (fx, fz) = ((gx - ix as f64) as f32, (gz - iz as f64) as f32);
|
||||||
|
let i = iz * N_EXT + ix;
|
||||||
|
let d = &t.delta;
|
||||||
|
d[i] * (1.0 - fx) * (1.0 - fz) + d[i + 1] * fx * (1.0 - fz) + d[i + N_EXT] * (1.0 - fx) * fz + d[i + N_EXT + 1] * fx * fz
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Correction d'érosion raccordée en (x, z) : Σ w_T · delta_T.
|
||||||
|
pub fn correction(&self, x: f64, z: f64) -> f32 {
|
||||||
|
let tx0 = ((x - DEMI_BANDE_M) / TAILLE_M).floor() as i32;
|
||||||
|
let tx1 = ((x + DEMI_BANDE_M) / TAILLE_M).floor() as i32;
|
||||||
|
let tz0 = ((z - DEMI_BANDE_M) / TAILLE_M).floor() as i32;
|
||||||
|
let tz1 = ((z + DEMI_BANDE_M) / TAILLE_M).floor() as i32;
|
||||||
|
let mut c = 0.0f32;
|
||||||
|
for tz in tz0..=tz1 {
|
||||||
|
let wz = trapeze(z - tz as f64 * TAILLE_M);
|
||||||
|
if wz <= 0.0 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for tx in tx0..=tx1 {
|
||||||
|
let wx = trapeze(x - tx as f64 * TAILLE_M);
|
||||||
|
if wx <= 0.0 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let t = self.erodee(TileId { tx, tz });
|
||||||
|
c += (wx * wz) as f32 * Self::delta_en(&t, x, z);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Hauteur finale (m) d'une colonne : fonction pure de (x, z, graine).
|
||||||
|
pub fn hauteur(&self, x: f64, z: f64) -> f32 {
|
||||||
|
self.brut(x, z, 1.0) + self.correction(x, z)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Hauteurs d'un chunk 16 × 16 (blocs = 1 m), ligne par ligne (z puis x).
|
||||||
|
pub fn chunk(&self, cx: i32, cz: i32) -> [f32; 256] {
|
||||||
|
let mut out = [0.0f32; 256];
|
||||||
|
let (bx, bz) = (cx as f64 * 16.0, cz as f64 * 16.0);
|
||||||
|
for j in 0..16 {
|
||||||
|
for i in 0..16 {
|
||||||
|
out[j * 16 + i] = self.hauteur(bx + i as f64 + 0.5, bz + j as f64 + 0.5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Tuile finale 512 × 512 au pas de 1 m (centres de blocs).
|
||||||
|
pub fn tuile(&self, id: TileId) -> TuileRelief {
|
||||||
|
// pré-charge les 9 tuiles érodées voisines (en parallèle)
|
||||||
|
let voisins: Vec<TileId> =
|
||||||
|
(-1..=1).flat_map(|dz| (-1..=1).map(move |dx| TileId { tx: id.tx + dx, tz: id.tz + dz })).collect();
|
||||||
|
voisins.par_iter().for_each(|v| {
|
||||||
|
self.erodee(*v);
|
||||||
|
});
|
||||||
|
let ox = id.tx as f64 * TAILLE_M;
|
||||||
|
let oz = id.tz as f64 * TAILLE_M;
|
||||||
|
let mut h = vec![0.0f32; TAILLE * TAILLE];
|
||||||
|
h.par_chunks_mut(TAILLE).enumerate().for_each(|(j, ligne)| {
|
||||||
|
let z = oz + j as f64 + 0.5;
|
||||||
|
for (i, v) in ligne.iter_mut().enumerate() {
|
||||||
|
*v = self.hauteur(ox + i as f64 + 0.5, z);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let centre = self.erodee(id);
|
||||||
|
let m = (MARGE_M / PAS_EROSION) as usize;
|
||||||
|
let mut accumulation = vec![0.0f32; TAILLE * TAILLE];
|
||||||
|
let mut roche = vec![Roche::Calcaire; TAILLE * TAILLE];
|
||||||
|
let aire = (PAS_EROSION * PAS_EROSION) as f32;
|
||||||
|
for j in 0..TAILLE {
|
||||||
|
for i in 0..TAILLE {
|
||||||
|
let g = (m + j / 2) * N_EXT + m + i / 2;
|
||||||
|
let a = centre.accumulation[g] * aire;
|
||||||
|
accumulation[j * TAILLE + i] = a;
|
||||||
|
let k = j * TAILLE + i;
|
||||||
|
roche[k] = if a > 200_000.0 && h[k] < 1500.0 {
|
||||||
|
Roche::Alluvions
|
||||||
|
} else {
|
||||||
|
roche_biome(self.monde.poids(ox + i as f64, oz + j as f64).dominant(), h[k])
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TuileRelief { id, h, accumulation, roche }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn roche_biome(b: BiomeRelief, h: f32) -> Roche {
|
||||||
|
use BiomeRelief::*;
|
||||||
|
match b {
|
||||||
|
Prealpes | Karst | Gorges => Roche::Calcaire,
|
||||||
|
Glaciaire | Fjord => Roche::Granite,
|
||||||
|
Bocage => Roche::Schiste,
|
||||||
|
CoteRocheuse => if h < 2.0 { Roche::Sable } else { Roche::Granite },
|
||||||
|
Plaine => Roche::Alluvions,
|
||||||
|
Mer => Roche::Sable,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Raccourci de l'API du cadrage : génère une tuile finale sans cache partagé.
|
||||||
|
pub fn generer_tuile(
|
||||||
|
id: TileId,
|
||||||
|
monde: Arc<dyn SourcePoids>,
|
||||||
|
params: &dyn Fn(BiomeRelief) -> ParamsRelief,
|
||||||
|
seed: u64,
|
||||||
|
) -> TuileRelief {
|
||||||
|
let table = BiomeRelief::TOUS.iter().map(|&b| params(b)).collect();
|
||||||
|
Generateur::new(seed, monde, table).tuile(id)
|
||||||
|
}
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
//! Types partagés provisoires. `BiomeRelief`, `Roche`, `PoidsBiomes`, `SourcePoids`
|
||||||
|
//! sont définis ici en attendant leur version commune (crate `signature` / `rt-core`,
|
||||||
|
//! agents 3 et 7). Ce module est le seul à remplacer par des `pub use` le moment venu.
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
pub use rt_core::TileId;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
pub enum BiomeRelief {
|
||||||
|
Prealpes,
|
||||||
|
Glaciaire,
|
||||||
|
Karst,
|
||||||
|
Gorges,
|
||||||
|
Bocage,
|
||||||
|
CoteRocheuse,
|
||||||
|
Fjord,
|
||||||
|
Plaine,
|
||||||
|
Mer,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BiomeRelief {
|
||||||
|
pub const TOUS: [BiomeRelief; 9] = [
|
||||||
|
Self::Prealpes,
|
||||||
|
Self::Glaciaire,
|
||||||
|
Self::Karst,
|
||||||
|
Self::Gorges,
|
||||||
|
Self::Bocage,
|
||||||
|
Self::CoteRocheuse,
|
||||||
|
Self::Fjord,
|
||||||
|
Self::Plaine,
|
||||||
|
Self::Mer,
|
||||||
|
];
|
||||||
|
pub fn id(self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
Self::Prealpes => "prealpes",
|
||||||
|
Self::Glaciaire => "glaciaire",
|
||||||
|
Self::Karst => "karst",
|
||||||
|
Self::Gorges => "gorges",
|
||||||
|
Self::Bocage => "bocage",
|
||||||
|
Self::CoteRocheuse => "cote_rocheuse",
|
||||||
|
Self::Fjord => "fjord",
|
||||||
|
Self::Plaine => "plaine",
|
||||||
|
Self::Mer => "mer",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn index(self) -> usize {
|
||||||
|
self as usize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
pub enum Roche {
|
||||||
|
Calcaire,
|
||||||
|
Granite,
|
||||||
|
Schiste,
|
||||||
|
Alluvions,
|
||||||
|
Sable,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Poids des biomes en un point (au plus 3 non nuls, somme = 1).
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub struct PoidsBiomes {
|
||||||
|
pub b: [(BiomeRelief, f32); 3],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PoidsBiomes {
|
||||||
|
pub fn unique(b: BiomeRelief) -> Self {
|
||||||
|
PoidsBiomes { b: [(b, 1.0), (b, 0.0), (b, 0.0)] }
|
||||||
|
}
|
||||||
|
pub fn dominant(&self) -> BiomeRelief {
|
||||||
|
let mut m = self.b[0];
|
||||||
|
for e in self.b {
|
||||||
|
if e.1 > m.1 {
|
||||||
|
m = e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait SourcePoids: Send + Sync {
|
||||||
|
fn poids(&self, x: f64, z: f64) -> PoidsBiomes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Mode `forcer_biome` : un seul biome partout.
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub struct BiomeUnique(pub BiomeRelief);
|
||||||
|
|
||||||
|
impl SourcePoids for BiomeUnique {
|
||||||
|
fn poids(&self, _x: f64, _z: f64) -> PoidsBiomes {
|
||||||
|
PoidsBiomes::unique(self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use proc_relief::*;
|
||||||
|
|
||||||
|
fn pts(n: u64, etendue: f64) -> Vec<(f64, f64)> {
|
||||||
|
(0..n)
|
||||||
|
.map(|i| {
|
||||||
|
(
|
||||||
|
bruit::unif(bruit::melange64(i)) as f64 * etendue - etendue / 2.0,
|
||||||
|
bruit::unif(bruit::melange64(i ^ 0x5bd1)) as f64 * etendue - etendue / 2.0,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn determinisme_hauteur_brute() {
|
||||||
|
for b in BiomeRelief::TOUS {
|
||||||
|
let p = params_initiaux(b);
|
||||||
|
for (x, z) in pts(500, 1.0e6) {
|
||||||
|
assert_eq!(hauteur_brute(b, &p, 99, x, z).to_bits(), hauteur_brute(b, &p, 99, x, z).to_bits());
|
||||||
|
}
|
||||||
|
let diff = pts(200, 1.0e5).iter().filter(|&&(x, z)| hauteur_brute(b, &p, 1, x, z) != hauteur_brute(b, &p, 2, x, z)).count();
|
||||||
|
assert!(diff > 150, "{b:?} : la graine doit changer le relief");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn determinisme_erosion_et_ordre() {
|
||||||
|
let g1 = Generateur::biome_unique(7, BiomeRelief::Prealpes);
|
||||||
|
let g2 = Generateur::biome_unique(7, BiomeRelief::Prealpes);
|
||||||
|
let id = TileId { tx: 3, tz: -2 };
|
||||||
|
let a = g1.eroder_tuile(id);
|
||||||
|
let b = g2.eroder_tuile(id);
|
||||||
|
assert!(a.delta.iter().zip(&b.delta).all(|(x, y)| x.to_bits() == y.to_bits()));
|
||||||
|
assert!(a.delta.iter().any(|d| d.abs() > 0.05), "l'érosion doit modifier le relief");
|
||||||
|
// ordre de génération différent → mêmes hauteurs
|
||||||
|
let pts = [(1536.3, -1024.7), (1600.0, -700.0), (2047.5, -1023.5)];
|
||||||
|
let h1: Vec<f32> = pts.iter().map(|&(x, z)| g1.hauteur(x, z)).collect();
|
||||||
|
let g3 = Generateur::biome_unique(7, BiomeRelief::Prealpes);
|
||||||
|
g3.erodee(TileId { tx: 4, tz: -2 });
|
||||||
|
g3.erodee(TileId { tx: 3, tz: -3 });
|
||||||
|
let h3: Vec<f32> = pts.iter().rev().map(|&(x, z)| g3.hauteur(x, z)).rev().collect();
|
||||||
|
assert_eq!(h1, h3);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn continuite_bords_de_tuiles() {
|
||||||
|
for b in [BiomeRelief::Glaciaire, BiomeRelief::Gorges, BiomeRelief::Bocage] {
|
||||||
|
let g = Generateur::biome_unique(11, b);
|
||||||
|
let ta = g.tuile(TileId { tx: 0, tz: 0 });
|
||||||
|
let tb = g.tuile(TileId { tx: 1, tz: 0 });
|
||||||
|
let tc = g.tuile(TileId { tx: 0, tz: 1 });
|
||||||
|
let n = 512;
|
||||||
|
// valeurs de bord égales à la fonction ponctuelle (calculée sans passer par la tuile)
|
||||||
|
let g2 = Generateur::biome_unique(11, b);
|
||||||
|
for j in (0..n).step_by(37) {
|
||||||
|
assert_eq!(ta.h[j * n + 511], g2.hauteur(511.5, j as f64 + 0.5));
|
||||||
|
assert_eq!(tb.h[j * n], g2.hauteur(512.5, j as f64 + 0.5));
|
||||||
|
}
|
||||||
|
// pas de couture : saut au bord comparable aux sauts intérieurs
|
||||||
|
let mut saut_bord = 0.0f64;
|
||||||
|
let mut saut_int = 0.0f64;
|
||||||
|
for j in 0..n {
|
||||||
|
saut_bord += (tb.h[j * n] - ta.h[j * n + 511]).abs() as f64;
|
||||||
|
saut_int += (ta.h[j * n + 256] - ta.h[j * n + 255]).abs() as f64;
|
||||||
|
saut_bord += (tc.h[j] - ta.h[511 * n + j]).abs() as f64;
|
||||||
|
saut_int += (ta.h[256 * n + j] - ta.h[255 * n + j]).abs() as f64;
|
||||||
|
}
|
||||||
|
assert!(saut_bord < 2.0 * saut_int + 0.5 * n as f64, "{b:?} couture : bord {saut_bord} intérieur {saut_int}");
|
||||||
|
// le saut maximal au bord reste plausible (pas de marche)
|
||||||
|
let max_bord = (0..n).map(|j| (tb.h[j * n] - ta.h[j * n + 511]).abs()).fold(0.0f32, f32::max);
|
||||||
|
let max_int = (0..n).map(|j| (ta.h[j * n + 101] - ta.h[j * n + 100]).abs()).fold(0.0f32, f32::max);
|
||||||
|
assert!(max_bord < max_int.max(1.0) * 4.0, "{b:?} marche {max_bord} vs {max_int}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn plages_altitude() {
|
||||||
|
// [p5 min, p95 max] tolérés, d'après le tableau du cadrage (§ 2), avec marge.
|
||||||
|
let attendu: [(BiomeRelief, f32, f32); 9] = [
|
||||||
|
(BiomeRelief::Prealpes, 300.0, 2500.0),
|
||||||
|
(BiomeRelief::Glaciaire, 900.0, 4400.0),
|
||||||
|
(BiomeRelief::Karst, 200.0, 1300.0),
|
||||||
|
(BiomeRelief::Gorges, 250.0, 1600.0),
|
||||||
|
(BiomeRelief::Bocage, 0.0, 450.0),
|
||||||
|
(BiomeRelief::CoteRocheuse, -60.0, 150.0),
|
||||||
|
(BiomeRelief::Fjord, -800.0, 1900.0),
|
||||||
|
(BiomeRelief::Plaine, -5.0, 170.0),
|
||||||
|
(BiomeRelief::Mer, -250.0, 30.0),
|
||||||
|
];
|
||||||
|
for (b, lo, hi) in attendu {
|
||||||
|
let p = params_initiaux(b);
|
||||||
|
let mut v: Vec<f32> = pts(4000, 2.0e5).iter().map(|&(x, z)| hauteur_brute(b, &p, 5, x, z)).collect();
|
||||||
|
v.sort_by(|a, b| a.total_cmp(b));
|
||||||
|
let (p5, p95) = (v[200], v[3800]);
|
||||||
|
assert!(p5 >= lo && p95 <= hi, "{b:?} : p5 {p5} p95 {p95} hors [{lo}, {hi}]");
|
||||||
|
assert!(p95 - p5 > 0.3 * (hi - lo) * 0.5, "{b:?} : relief trop plat ({p5}..{p95})");
|
||||||
|
assert!(v.iter().all(|h| h.is_finite()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tuile_altitudes_finies_et_send_sync() {
|
||||||
|
let g = Arc::new(Generateur::biome_unique(3, BiomeRelief::Fjord));
|
||||||
|
let hs: Vec<_> = (0..4)
|
||||||
|
.map(|k| {
|
||||||
|
let g = g.clone();
|
||||||
|
std::thread::spawn(move || g.chunk(k * 7, -k * 3))
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
for h in hs {
|
||||||
|
let c = h.join().unwrap();
|
||||||
|
assert!(c.iter().all(|v| v.is_finite() && *v > -2000.0 && *v < 3000.0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn params_serde_aller_retour() {
|
||||||
|
for b in BiomeRelief::TOUS {
|
||||||
|
let p = params_initiaux(b);
|
||||||
|
let s = serde_json::to_string(&p).unwrap();
|
||||||
|
let q: ParamsRelief = serde_json::from_str(&s).unwrap();
|
||||||
|
assert_eq!(p, q);
|
||||||
|
}
|
||||||
|
}
|
||||||
Référencer dans un nouveau ticket
Bloquer un utilisateur