Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
99 lignes
3.1 KiB
Rust
99 lignes
3.1 KiB
Rust
//! Types de base sans dépendance : repère Lambert 93 -> bloc, tuiles, hauteur du monde.
|
||
pub mod proc;
|
||
|
||
/// Paramètres de la dimension haute (rapport 09).
|
||
/// Contrainte vanilla : min_y et height multiples de 16, height ∈ [16, 4064],
|
||
/// min_y ∈ [-2032, 2031], min_y + height ≤ 2032.
|
||
pub const DIM_MIN_Y: i32 = -1024;
|
||
pub const DIM_HEIGHT: i32 = 2400;
|
||
|
||
pub const fn dimension_valide(min_y: i32, height: i32) -> bool {
|
||
min_y % 16 == 0
|
||
&& height % 16 == 0
|
||
&& height >= 16
|
||
&& height <= 4064
|
||
&& min_y >= -2032
|
||
&& min_y + height <= 2032
|
||
}
|
||
|
||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||
pub struct WorldOrigin {
|
||
pub l93_x: f64,
|
||
pub l93_y: f64,
|
||
pub alt_base_m: i32,
|
||
}
|
||
|
||
impl WorldOrigin {
|
||
/// Coin nord-ouest du MNT de Lans (géoréférencement du GeoTIFF : 903000, 6451500).
|
||
pub const LANS: WorldOrigin = WorldOrigin { l93_x: 903_000.0, l93_y: 6_451_500.0, alt_base_m: 1000 };
|
||
|
||
/// Le nord Lambert (+Y) correspond au nord Minecraft (−Z).
|
||
pub fn l93_to_block(&self, x: f64, y: f64) -> (i32, i32) {
|
||
((x - self.l93_x).floor() as i32, (self.l93_y - y).floor() as i32)
|
||
}
|
||
/// Centre du bloc.
|
||
pub fn block_to_l93(&self, bx: i32, bz: i32) -> (f64, f64) {
|
||
(self.l93_x + bx as f64 + 0.5, self.l93_y - bz as f64 - 0.5)
|
||
}
|
||
pub fn alt_to_y(&self, alt_m: f32) -> i32 {
|
||
alt_m.floor() as i32 - self.alt_base_m
|
||
}
|
||
/// Altitude (m) correspondant à ground_dm = 0.
|
||
pub fn alt_zero_dm(&self) -> i32 {
|
||
self.alt_base_m - 1000
|
||
}
|
||
}
|
||
|
||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
||
pub struct TileId {
|
||
pub tx: i32,
|
||
pub tz: i32,
|
||
}
|
||
|
||
impl TileId {
|
||
pub const SIZE: i32 = 512;
|
||
pub fn of_block(bx: i32, bz: i32) -> Self {
|
||
TileId { tx: bx.div_euclid(Self::SIZE), tz: bz.div_euclid(Self::SIZE) }
|
||
}
|
||
pub fn of_chunk(cx: i32, cz: i32) -> Self {
|
||
TileId { tx: cx.div_euclid(32), tz: cz.div_euclid(32) }
|
||
}
|
||
pub fn file_name(&self) -> String {
|
||
format!("{}_{}.rtt", self.tx, self.tz)
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn dimension_retenue_valide() {
|
||
assert!(dimension_valide(DIM_MIN_Y, DIM_HEIGHT));
|
||
assert!(!dimension_valide(0, 2432)); // 2432 > 2032
|
||
assert!(!dimension_valide(-2032, 4080));
|
||
assert!(dimension_valide(-2032, 4064));
|
||
assert!(!dimension_valide(-1000, 2400)); // pas multiple de 16
|
||
}
|
||
|
||
#[test]
|
||
fn aller_retour_l93() {
|
||
let o = WorldOrigin::LANS;
|
||
let (bx, bz) = o.l93_to_block(903_430.2, 6_450_969.7);
|
||
assert_eq!((bx, bz), (430, 530));
|
||
let (x, y) = o.block_to_l93(bx, bz);
|
||
assert!((x - 903_430.2).abs() <= 0.5 && (y - 6_450_969.7).abs() <= 0.5);
|
||
// nord = -z
|
||
assert!(o.l93_to_block(903_000.0, 6_451_600.0).1 < 0);
|
||
}
|
||
|
||
#[test]
|
||
fn tuiles() {
|
||
assert_eq!(TileId::of_block(-1, 0), TileId { tx: -1, tz: 0 });
|
||
assert_eq!(TileId::of_block(511, 512), TileId { tx: 0, tz: 1 });
|
||
assert_eq!(TileId::of_chunk(-1, 31), TileId { tx: -1, tz: 0 });
|
||
assert_eq!(TileId { tx: -2, tz: 3 }.file_name(), "-2_3.rtt");
|
||
assert_eq!(WorldOrigin::LANS.alt_to_y(1020.4), 20);
|
||
}
|
||
}
|