Skip to main content

qualia_geospatial/
corridor.rs

1//! Vector Corridor Conformer (Hermite Cubic Spline Roadbed & Creek Bed Excavation).
2//!
3//! Conforms terrain elevation and subsurface hydrology to linear vector features
4//! without cracking or seam artefacts.
5
6use qualia_core_db::q42::m64_matter::M64StrataColumnRecord;
7
8#[derive(Debug, Clone, Copy, PartialEq)]
9pub enum CorridorKind {
10    Road { width_mm: u32, crown_height_mm: i16 },
11    Creek { width_mm: u32, depth_mm: u16, flow_velocity_xy: [i16; 2] },
12}
13
14/// A 2D control point on a corridor centerline.
15#[derive(Debug, Clone, Copy, PartialEq)]
16pub struct SplineControlPoint {
17    pub pos_xy_mm: [f64; 2],
18    pub elevation_mm: i32,
19    pub tangent_xy: [f64; 2],
20}
21
22/// Hermite cubic spline evaluation for a corridor segment.
23pub struct HermiteCorridorSegment {
24    pub p0: SplineControlPoint,
25    pub p1: SplineControlPoint,
26    pub kind: CorridorKind,
27}
28
29impl HermiteCorridorSegment {
30    /// Evaluates point along the spline at parameter $t \in [0, 1]$.
31    pub fn sample_centerline(&self, t: f64) -> ([f64; 2], f64) {
32        let t2 = t * t;
33        let t3 = t2 * t;
34
35        // Hermite basis functions
36        let h00 = 2.0 * t3 - 3.0 * t2 + 1.0;
37        let h10 = t3 - 2.0 * t2 + t;
38        let h01 = -2.0 * t3 + 3.0 * t2;
39        let h11 = t3 - t2;
40
41        let x = h00 * self.p0.pos_xy_mm[0]
42            + h10 * self.p0.tangent_xy[0]
43            + h01 * self.p1.pos_xy_mm[0]
44            + h11 * self.p1.tangent_xy[0];
45
46        let y = h00 * self.p0.pos_xy_mm[1]
47            + h10 * self.p0.tangent_xy[1]
48            + h01 * self.p1.pos_xy_mm[1]
49            + h11 * self.p1.tangent_xy[1];
50
51        let elev = h00 * (self.p0.elevation_mm as f64) + h01 * (self.p1.elevation_mm as f64);
52
53        ([x, y], elev)
54    }
55
56    /// Evaluates shortest distance from a terrain point $(px, py)$ to the segment.
57    pub fn distance_to_point(&self, px: f64, py: f64) -> (f64, f64) {
58        // Discrete search over spline to find closest point and parameter t
59        let mut min_dist = f64::MAX;
60        let mut best_t = 0.0;
61        for step in 0..=20 {
62            let t = (step as f64) / 20.0;
63            let (pt, _) = self.sample_centerline(t);
64            let dx = pt[0] - px;
65            let dy = pt[1] - py;
66            let d = (dx * dx + dy * dy).sqrt();
67            if d < min_dist {
68                min_dist = d;
69                best_t = t;
70            }
71        }
72        (min_dist, best_t)
73    }
74
75    /// Conforms a strata column record to this corridor.
76    pub fn conform_strata_column(
77        &self,
78        col_x_mm: f64,
79        col_y_mm: f64,
80        record: &mut M64StrataColumnRecord,
81    ) -> bool {
82        let (dist_mm, t) = self.distance_to_point(col_x_mm, col_y_mm);
83        let (_, target_elev) = self.sample_centerline(t);
84
85        match self.kind {
86            CorridorKind::Road { width_mm, crown_height_mm } => {
87                let half_width = (width_mm as f64) * 0.5;
88                if dist_mm <= half_width {
89                    // Parabolic crown cross-section
90                    let rel_dist = dist_mm / half_width;
91                    let crown = (1.0 - rel_dist * rel_dist) * (crown_height_mm as f64);
92                    record.surface_elevation_mm = (target_elev + crown).round() as i32;
93
94                    // Compaction increases under roadbed (Layer 0)
95                    record.layer_compaction_pct[0] = 240;
96                    record.update_crc();
97                    return true;
98                }
99            }
100            CorridorKind::Creek { width_mm, depth_mm, flow_velocity_xy } => {
101                let half_width = (width_mm as f64) * 0.5;
102                if dist_mm <= half_width {
103                    // Parabolic riverbed excavation
104                    let rel_dist = dist_mm / half_width;
105                    let bed_depth = (1.0 - rel_dist * rel_dist) * (depth_mm as f64);
106                    record.surface_elevation_mm = (target_elev - bed_depth).round() as i32;
107
108                    // Flowing surface fluid
109                    record.surface_fluid_depth_mm = (bed_depth * 0.8) as u16;
110                    record.fluid_velocity_xy = flow_velocity_xy;
111                    record.layer_moisture_pct[0] = 255; // 100% saturation
112                    record.update_crc();
113                    return true;
114                }
115            }
116        }
117        false
118    }
119}