petekio 0.3.14

Subsurface data ingestion + structure layer: surfaces, wells, points, polygons with loading, interpolation, and statistics.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
//! `PointSet` — scattered 3-D points (N×3 coords) with named `f64` attribute
//! columns, a spatial index for nearest-neighbour queries, and gridding onto a
//! `Surface` (`to_surface`). `NaN` = undefined. Imports from `foundation`,
//! `io`, and (for gridding) `core::surface`.
//!
//! Gridding is **delegated to the shared petekTools kernels** (`grid` cold,
//! `grid_min_curvature_seeded` warm). petekTools' `Lattice` is field-for-field
//! identical to our `GridGeometry`, so the seam is a 1:1 map (`to_lattice`); the
//! kernels themselves were lifted from petekIO 0.2.0 and are held at parity.

use crate::core::shell::{fit_grid_from_coords, fit_grid_from_indexed};
use crate::core::{PolygonSet, StructuredMeshSurface, Surface};
use crate::foundation::{
    BBox, GeoError, GridGeometry, HasHistory, OperationHistory, Point3, Result, Stats,
};
use crate::io::PointData;
use indexmap::IndexMap;
use ndarray::Array2;
use petektools::{grid as pt_grid, grid_min_curvature_seeded, GridMethod as PtGridMethod};
use rstar::primitives::GeomWithData;
use rstar::RTree;
use std::collections::HashMap;
use std::path::Path;

/// A gridding method for [`PointSet::to_surface`] — see
/// `dev-docs/designs/gridding-method.md`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GridMethod {
    /// Value of the single areally-closest sample (blocky, exact at data).
    Nearest,
    /// Inverse-distance weighting, `wᵢ = 1/dᵢ²` (power p=2), exact at d=0.
    InverseDistance,
    /// Briggs minimum-curvature (biharmonic SOR relaxation, data-anchored).
    MinimumCurvature,
}

/// Edge polygon to attach when inferring a grid geometry from points.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GeometryEdge {
    /// Outline of the occupied lattice nodes — the true data footprint, which
    /// follows interior holes and a non-rectangular outer boundary.
    Occupied,
    /// Convex hull of the input point cloud.
    ConvexHull,
    /// The full rectangular lattice footprint. Cheap and always four corners, but
    /// it claims the whole bounding lattice even where no node carries data.
    FullRect,
}

impl GridMethod {
    /// Map onto petekTools' identically-named method enum at the kernel seam.
    pub(crate) fn to_petektools(self) -> PtGridMethod {
        match self {
            GridMethod::Nearest => PtGridMethod::Nearest,
            GridMethod::InverseDistance => PtGridMethod::InverseDistance,
            GridMethod::MinimumCurvature => PtGridMethod::MinimumCurvature,
        }
    }
}

/// An areal R*-tree entry: a 2-D `[x, y]` position carrying the point's index.
pub(crate) type AerialEntry = GeomWithData<[f64; 2], usize>;

/// Scattered points with attribute columns. Coordinates are stored as `[x, y,
/// z]`; each attribute is a `f64` column aligned 1:1 with `coords`.
#[derive(Clone, serde::Serialize, serde::Deserialize)]
pub struct PointSet {
    pub(crate) coords: Vec<[f64; 3]>,
    pub(crate) attrs: IndexMap<String, Vec<f64>>,
    #[serde(default)]
    history: OperationHistory,
}

impl PointSet {
    /// Build a `PointSet` from raw coordinates and attribute columns. Each
    /// attribute column must match `coords.len()` (callers within the crate
    /// guarantee this).
    pub(crate) fn from_parts(coords: Vec<[f64; 3]>, attrs: IndexMap<String, Vec<f64>>) -> PointSet {
        PointSet {
            coords,
            attrs,
            history: OperationHistory::new(),
        }
    }

    pub(crate) fn from_point_data(data: PointData) -> PointSet {
        let (coords, attrs) = data.into_parts();
        PointSet::from_parts(coords, attrs)
    }

    /// Build an in-memory `PointSet` from `[x, y, z]` coordinates (no named
    /// attributes) — construct scattered points directly, without a file.
    pub fn from_coords(coords: Vec<[f64; 3]>) -> PointSet {
        let mut out = PointSet::from_parts(coords, IndexMap::new());
        out.history = OperationHistory::from_entry("points.from_coords");
        out
    }

    /// Read a headered CSV, taking X/Y/Z from the named columns. Every other
    /// column whose values all parse as `f64` becomes an attribute; columns
    /// with any non-numeric cell are skipped. Rows with a non-numeric X/Y/Z are
    /// an error (readers validate on load).
    pub fn load_csv(path: impl AsRef<Path>, x: &str, y: &str, z: &str) -> Result<PointSet> {
        let mut out =
            PointSet::from_point_data(crate::io::csv_points::load(path.as_ref(), x, y, z)?);
        out.history = OperationHistory::from_entry(format!(
            "points.load_csv(path={})",
            path.as_ref().display()
        ));
        Ok(out)
    }

    /// Load point features from a GeoJSON file. Each feature's numeric
    /// `properties{}` become attribute columns (the union of all features'
    /// numeric property names, NaN-filling features that lack one); string and
    /// other non-numeric properties are ignored.
    pub fn load_geojson(path: impl AsRef<Path>) -> Result<PointSet> {
        let mut out =
            PointSet::from_point_data(crate::io::vector::load_point_set_geojson(path.as_ref())?);
        out.history = OperationHistory::from_entry(format!(
            "points.load_geojson(path={})",
            path.as_ref().display()
        ));
        Ok(out)
    }

    /// Load scattered points from an IRAP/RMS plain `X Y Z` file. No named
    /// attributes (the format carries none). Format-sniffed: a foreign header
    /// (EarthVision grid / CPS-3 / LAS) is rejected with `GeoError::Format`.
    pub fn load_irap_points(path: impl AsRef<Path>) -> Result<PointSet> {
        let mut out = PointSet::from_point_data(crate::io::xyz::load_points(path.as_ref())?);
        out.history = OperationHistory::from_entry(format!(
            "points.load_irap_points(path={})",
            path.as_ref().display()
        ));
        Ok(out)
    }

    /// Load plain IRAP/RMS `X Y Z` points and transfer Petrel `column`/`row`
    /// topology from a matching EarthVision grid export. This is intentionally
    /// a project-loader helper: the IRAP file owns the returned coordinates,
    /// while `topology_path` contributes only grid indices for exact geometry
    /// inference.
    pub fn load_irap_points_with_topology(
        path: impl AsRef<Path>,
        topology_path: impl AsRef<Path>,
    ) -> Result<PointSet> {
        let points = crate::io::xyz::load_points(path.as_ref())?;
        let topology = crate::io::earthvision::load_earthvision_grid(topology_path.as_ref())?;
        let mut out =
            PointSet::from_point_data(points.with_topology_from_ordered_subset(&topology, 1e-3)?);
        out.history = OperationHistory::from_entry(format!(
            "points.load_irap_points_with_topology(path={}, topology_path={})",
            path.as_ref().display(),
            topology_path.as_ref().display()
        ));
        Ok(out)
    }

    /// Legacy finite-node compatibility view of an EarthVision grid ASCII file
    /// (`.EarthVisionGrid`) — `x y z` nodes with a directive header; null nodes
    /// dropped (see [`crate::io::earthvision`]). New code should use
    /// [`StructuredMeshSurface::load_earthvision_grid`](crate::StructuredMeshSurface::load_earthvision_grid).
    /// Petrel `column`/`row` fields,
    /// when present, are preserved as attributes so geometry inference can use
    /// the exported grid topology instead of guessing from XY alone.
    pub fn load_earthvision_grid(path: impl AsRef<Path>) -> Result<PointSet> {
        let mut out = PointSet::from_point_data(crate::io::earthvision::load_earthvision_grid(
            path.as_ref(),
        )?);
        out.history = OperationHistory::from_entry(format!(
            "points.load_earthvision_grid(path={})",
            path.as_ref().display()
        ));
        Ok(out)
    }

    /// Number of points.
    pub fn len(&self) -> usize {
        self.coords.len()
    }

    /// Whether the set is empty.
    pub fn is_empty(&self) -> bool {
        self.coords.is_empty()
    }

    /// The raw `[x, y, z]` coordinates of every point, in load order (`NaN` =
    /// undefined, carried through as stored). The read side of
    /// [`from_coords`](Self::from_coords): a downstream consumer that grids the
    /// scatter itself (rather than through [`to_surface`](Self::to_surface)) reads
    /// the points here.
    pub fn coords(&self) -> &[[f64; 3]] {
        &self.coords
    }

    /// A new `PointSet` keeping only points for which `pred` is true. Attribute
    /// columns are carried over for the retained rows.
    pub fn filter(&self, pred: impl Fn(Point3) -> bool) -> PointSet {
        let keep: Vec<usize> = (0..self.coords.len())
            .filter(|&i| {
                let c = self.coords[i];
                pred(Point3::new(c[0], c[1], c[2]))
            })
            .collect();
        let coords = keep.iter().map(|&i| self.coords[i]).collect();
        let attrs = self
            .attrs
            .iter()
            .map(|(name, col)| (name.clone(), keep.iter().map(|&i| col[i]).collect()))
            .collect();
        let mut out = PointSet::from_parts(coords, attrs);
        out.history = self.history_with("points.filter");
        out
    }

    /// A named attribute column, if present.
    pub fn attr(&self, name: &str) -> Option<&[f64]> {
        self.attrs.get(name).map(Vec::as_slice)
    }

    /// Set (or replace) a named attribute column. The column must be aligned
    /// 1:1 with this point set's rows.
    pub fn set_attr(&mut self, name: &str, values: Vec<f64>) -> Result<()> {
        if values.len() != self.coords.len() {
            return Err(GeoError::Parse(format!(
                "point attribute '{name}' has {} rows, expected {}",
                values.len(),
                self.coords.len()
            )));
        }
        self.attrs.insert(name.to_string(), values);
        self.record_history(format!("points.set_attr(name={name})"));
        Ok(())
    }

    /// The names of all attribute columns, in insertion order.
    pub fn attr_names(&self) -> Vec<&str> {
        self.attrs.keys().map(String::as_str).collect()
    }

    /// Human-readable operation history for this point set.
    pub fn history(&self) -> &[String] {
        self.history.entries()
    }

    pub(crate) fn history_with(&self, entry: impl Into<String>) -> OperationHistory {
        self.history.with_entry(entry)
    }

    pub(crate) fn record_history(&mut self, entry: impl Into<String>) {
        self.history.push(entry.into());
    }

    /// NaN-skipping statistics over a named attribute column, or `None` if the
    /// attribute is absent.
    pub fn stats(&self, attr: &str) -> Option<Stats> {
        self.attrs.get(attr).map(|col| Stats::of(col))
    }

    /// NaN-skipping statistics over the points' **z** coordinate — the horizon
    /// depth/elevation range of a scattered set loaded as `X Y Z` (which stores
    /// z as a coordinate, not a named attribute).
    pub fn z_stats(&self) -> Stats {
        let z: Vec<f64> = self.coords.iter().map(|c| c[2]).collect();
        Stats::of(&z)
    }

    /// Axis-aligned bounding box of the points' XY. Empty set → a degenerate
    /// box of `NaN`s.
    pub fn bbox(&self) -> BBox {
        let mut b = BBox {
            xmin: f64::INFINITY,
            ymin: f64::INFINITY,
            xmax: f64::NEG_INFINITY,
            ymax: f64::NEG_INFINITY,
        };
        for c in &self.coords {
            b.xmin = b.xmin.min(c[0]);
            b.xmax = b.xmax.max(c[0]);
            b.ymin = b.ymin.min(c[1]);
            b.ymax = b.ymax.max(c[1]);
        }
        if self.coords.is_empty() {
            b = BBox {
                xmin: f64::NAN,
                ymin: f64::NAN,
                xmax: f64::NAN,
                ymax: f64::NAN,
            };
        }
        b
    }

    /// Index of the areally-nearest point to `(x, y)` (Euclidean in XY; Z is
    /// ignored). `None` for an empty set.
    pub fn nearest(&self, x: f64, y: f64) -> Option<usize> {
        if self.coords.is_empty() {
            return None;
        }
        let tree = self.rtree_xy();
        tree.nearest_neighbor([x, y]).map(|e| e.data)
    }

    /// Infer a regular grid geometry from point coordinates. The returned
    /// geometry spans the occupied lattice extents; use
    /// [`infer_geometry_with_edge`](Self::infer_geometry_with_edge) when the
    /// caller also needs the modelling edge polygon — in particular
    /// [`GeometryEdge::Occupied`] when the data footprint is not rectangular.
    pub fn infer_geometry(&self, tolerance: f64) -> Result<GridGeometry> {
        self.infer_geometry_with_edge(tolerance, GeometryEdge::FullRect)
            .map(|(geom, _edge)| geom)
    }

    /// Infer a regular grid geometry and an edge polygon. This is intentionally
    /// strict: genuinely scattered points, ambiguous axes, duplicate lattice
    /// nodes, or coordinates that miss the inferred lattice by more than
    /// `tolerance` return `GeoError::GeometryInference`. A curvilinear mesh — one
    /// carrying `column`/`row` whose nodes do not sit on any regular lattice — is
    /// likewise rejected; represent it with [`to_structured_surface`](Self::to_structured_surface).
    pub fn infer_geometry_with_edge(
        &self,
        tolerance: f64,
        edge: GeometryEdge,
    ) -> Result<(GridGeometry, PolygonSet)> {
        // The topology path resolves occupancy from `column`/`row`; the coordinate path
        // hands back the lattice indices it derived. Either way the occupied footprint is
        // already known — no point cloud needs triangulating to recover it.
        let (geom, occupancy) =
            match infer_grid_geometry_from_index_attrs(&self.coords, &self.attrs, tolerance)? {
                Some(geom) => (geom, None),
                None => {
                    let (geom, occupancy) =
                        fit_grid_from_coords(&self.coords, tolerance).map_err(|err| {
                            add_xy_only_inference_hint(err, &self.coords, &self.attrs, tolerance)
                        })?;
                    (geom, Some(occupancy))
                }
            };
        let edge_polygon = match edge {
            GeometryEdge::Occupied => match &occupancy {
                Some(occupancy) => occupied_edge_from_lattice_indices(&geom, occupancy)?,
                None => topology_occupied_edge_from_points(&self.coords, &self.attrs)?,
            },
            GeometryEdge::FullRect => PolygonSet::from_grid_geometry(&geom),
            GeometryEdge::ConvexHull => {
                let pts = self
                    .coords
                    .iter()
                    .filter(|c| c[0].is_finite() && c[1].is_finite())
                    .map(|c| [c[0], c[1]])
                    .collect();
                PolygonSet::convex_hull_xy(pts).ok_or_else(|| {
                    GeoError::GeometryInference(
                        "convex hull edge requires at least three non-collinear points".into(),
                    )
                })?
            }
        };
        Ok((geom, edge_polygon))
    }

    /// Grid the points' Z values onto `geom` using `method`, returning a new
    /// `Surface`. See `dev-docs/designs/gridding-method.md`.
    pub fn to_surface(&self, geom: GridGeometry, method: GridMethod) -> Result<Surface> {
        let values = pt_grid(&self.coords, &geom.to_lattice(), method.to_petektools())?;
        let mut out = Surface::new(geom, values)?;
        let mut history = self.history.clone();
        history.push(format!("points.to_surface(method={method:?})"));
        out.set_history(history);
        Ok(out)
    }

    /// Promote topology-bearing points to a structured mesh surface. This keeps
    /// the exported logical `(column, row)` topology and the actual per-node XY
    /// coordinates, instead of forcing the nodes onto a single affine
    /// [`GridGeometry`].
    ///
    /// Requires `column`/`row` attributes. Use [`to_surface`](Self::to_surface)
    /// when the desired result is a regular grid on an explicit model geometry.
    pub fn to_structured_surface(
        &self,
        tolerance: f64,
        edge: GeometryEdge,
    ) -> Result<StructuredMeshSurface> {
        let indexed = topology_indexed_points(&self.coords, &self.attrs)?;
        if indexed.len() < 4 {
            return Err(GeoError::GeometryInference(
                "structured surface conversion requires at least four indexed points".into(),
            ));
        }

        let min_col = indexed.iter().map(|p| p.col).min().unwrap();
        let max_col = indexed.iter().map(|p| p.col).max().unwrap();
        let min_row = indexed.iter().map(|p| p.row).min().unwrap();
        let max_row = indexed.iter().map(|p| p.row).max().unwrap();
        if max_col <= min_col || max_row <= min_row {
            return Err(GeoError::GeometryInference(
                "column/row attributes do not span a two-dimensional structured surface".into(),
            ));
        }

        let ncol = (max_col - min_col + 1) as usize;
        let nrow = (max_row - min_row + 1) as usize;
        let mut x = Array2::from_elem((ncol, nrow), f64::NAN);
        let mut y = Array2::from_elem((ncol, nrow), f64::NAN);
        let mut values = Array2::from_elem((ncol, nrow), f64::NAN);
        let mut occupied = vec![false; ncol * nrow];

        for p in &indexed {
            let i = (p.col - min_col) as usize;
            let j = (p.row - min_row) as usize;
            let slot = i + j * ncol;
            if occupied[slot] {
                return Err(GeoError::GeometryInference(format!(
                    "multiple points map to structured node column={}, row={}",
                    p.col, p.row
                )));
            }
            occupied[slot] = true;
            x[[i, j]] = p.x;
            y[[i, j]] = p.y;
            values[[i, j]] = p.z;
        }

        let nominal_geometry =
            infer_grid_geometry_from_index_attrs(&self.coords, &self.attrs, tolerance)
                .ok()
                .flatten();
        let edge_polygon = structured_edge(&x, &y, Some(&values), nominal_geometry.as_ref(), edge)?;
        let mut out = StructuredMeshSurface::new(x, y, values, nominal_geometry, edge_polygon)?;
        let mut history = self.history.clone();
        history.push(format!("points.to_structured_surface(edge={edge:?})"));
        out.set_history(history);
        Ok(out)
    }

    /// Warm-started minimum-curvature re-grid onto `prior`'s lattice, relaxing
    /// from `prior`'s values instead of a cold IDW seed. For an incremental
    /// re-grid (control points nudged, a point added) this converges much faster
    /// than [`to_surface`](Self::to_surface) with `MinimumCurvature` while giving
    /// the same converged field. Honours the points as hard constraints, as the
    /// cold path does.
    pub fn regrid_min_curvature(&self, prior: &Surface) -> Result<Surface> {
        let values = grid_min_curvature_seeded(
            &self.coords,
            &prior.geom.to_lattice(),
            Some(prior.values()),
        )?;
        let mut out = Surface::new(prior.geom.clone(), values)?;
        let mut history = self.history.clone();
        history.extend_prefixed("prior", prior.operation_history());
        history.push("points.regrid_min_curvature(prior)".to_string());
        out.set_history(history);
        Ok(out)
    }

    /// Build an areal R*-tree over the points' XY, payloaded with their index.
    pub(crate) fn rtree_xy(&self) -> RTree<AerialEntry> {
        let entries: Vec<AerialEntry> = self
            .coords
            .iter()
            .enumerate()
            .map(|(i, c)| GeomWithData::new([c[0], c[1]], i))
            .collect();
        RTree::bulk_load(entries)
    }
}

#[derive(Debug, Clone, Copy)]
struct IndexedPoint {
    col: isize,
    row: isize,
    x: f64,
    y: f64,
    z: f64,
}

fn topology_indexed_points(
    coords: &[[f64; 3]],
    attrs: &IndexMap<String, Vec<f64>>,
) -> Result<Vec<IndexedPoint>> {
    let columns = find_attr(attrs, &["column", "col"]).ok_or_else(|| {
        GeoError::GeometryInference(
            "structured surface conversion requires column/row topology attributes".into(),
        )
    })?;
    let rows = find_attr(attrs, &["row"]).ok_or_else(|| {
        GeoError::GeometryInference(
            "structured surface conversion requires column/row topology attributes".into(),
        )
    })?;
    if columns.len() != coords.len() || rows.len() != coords.len() {
        return Err(GeoError::GeometryInference(
            "column/row attributes must match point count".into(),
        ));
    }

    let mut indexed = Vec::new();
    for (idx, c) in coords.iter().enumerate() {
        if !c[0].is_finite() || !c[1].is_finite() {
            continue;
        }
        let Some(col) = integer_attr(columns[idx], "column")? else {
            continue;
        };
        let Some(row) = integer_attr(rows[idx], "row")? else {
            continue;
        };
        indexed.push(IndexedPoint {
            col,
            row,
            x: c[0],
            y: c[1],
            z: c[2],
        });
    }
    Ok(indexed)
}

fn topology_occupied_edge_from_points(
    coords: &[[f64; 3]],
    attrs: &IndexMap<String, Vec<f64>>,
) -> Result<PolygonSet> {
    let indexed = topology_indexed_points(coords, attrs)?;
    if indexed.len() < 4 {
        return Err(GeoError::GeometryInference(
            "topology-aware occupied edge requires at least four indexed points".into(),
        ));
    }

    let min_col = indexed.iter().map(|p| p.col).min().unwrap();
    let max_col = indexed.iter().map(|p| p.col).max().unwrap();
    let min_row = indexed.iter().map(|p| p.row).min().unwrap();
    let max_row = indexed.iter().map(|p| p.row).max().unwrap();
    if max_col <= min_col || max_row <= min_row {
        return Err(GeoError::GeometryInference(
            "column/row attributes do not span a two-dimensional occupied edge".into(),
        ));
    }

    let ncol = (max_col - min_col + 1) as usize;
    let nrow = (max_row - min_row + 1) as usize;
    let mut x = Array2::from_elem((ncol, nrow), f64::NAN);
    let mut y = Array2::from_elem((ncol, nrow), f64::NAN);
    let mut occupied = vec![false; ncol * nrow];

    for p in indexed {
        let i = (p.col - min_col) as usize;
        let j = (p.row - min_row) as usize;
        let slot = i + j * ncol;
        if occupied[slot] {
            return Err(GeoError::GeometryInference(format!(
                "multiple points map to topology node column={}, row={}",
                p.col, p.row
            )));
        }
        occupied[slot] = true;
        x[[i, j]] = p.x;
        y[[i, j]] = p.y;
    }

    occupied_edge_from_node_arrays(&x, &y, None)
        .or_else(|_| perimeter_edge_from_node_arrays(&x, &y))
        .or_else(|_| convex_hull_from_node_arrays(&x, &y))
}

pub(crate) fn structured_edge(
    x: &Array2<f64>,
    y: &Array2<f64>,
    values: Option<&Array2<f64>>,
    nominal_geometry: Option<&GridGeometry>,
    edge: GeometryEdge,
) -> Result<PolygonSet> {
    match edge {
        GeometryEdge::ConvexHull => convex_hull_from_node_arrays(x, y),
        GeometryEdge::FullRect => nominal_geometry
            .map(PolygonSet::from_grid_geometry)
            .ok_or_else(|| {
                GeoError::GeometryInference(
                    "full_rect edge requires a nominal regular geometry; this mesh is \
                     curvilinear — use the occupied edge"
                        .into(),
                )
            }),
        GeometryEdge::Occupied => occupied_edge_from_node_arrays(x, y, values)
            .or_else(|_| perimeter_edge_from_node_arrays(x, y))
            .or_else(|_| convex_hull_from_node_arrays(x, y)),
    }
}

fn convex_hull_from_node_arrays(x: &Array2<f64>, y: &Array2<f64>) -> Result<PolygonSet> {
    let pts = x
        .iter()
        .zip(y.iter())
        .filter(|(x, y)| x.is_finite() && y.is_finite())
        .map(|(x, y)| [*x, *y])
        .collect();
    PolygonSet::convex_hull_xy(pts).ok_or_else(|| {
        GeoError::GeometryInference(
            "structured surface edge requires at least three non-collinear nodes".into(),
        )
    })
}

fn perimeter_edge_from_node_arrays(x: &Array2<f64>, y: &Array2<f64>) -> Result<PolygonSet> {
    let (ncol, nrow) = x.dim();
    if ncol < 2 || nrow < 2 {
        return Err(GeoError::GeometryInference(
            "structured surface perimeter edge requires at least 2x2 nodes".into(),
        ));
    }
    let mut ring = Vec::new();
    for i in 0..ncol {
        push_finite_node(&mut ring, x, y, i, 0)?;
    }
    for j in 1..nrow {
        push_finite_node(&mut ring, x, y, ncol - 1, j)?;
    }
    for i in (0..ncol.saturating_sub(1)).rev() {
        push_finite_node(&mut ring, x, y, i, nrow - 1)?;
    }
    for j in (1..nrow.saturating_sub(1)).rev() {
        push_finite_node(&mut ring, x, y, 0, j)?;
    }
    if ring.len() < 3 {
        return Err(GeoError::GeometryInference(
            "structured surface perimeter edge has fewer than three finite nodes".into(),
        ));
    }
    Ok(PolygonSet::from_rings(vec![ring]))
}

fn occupied_edge_from_node_arrays(
    x: &Array2<f64>,
    y: &Array2<f64>,
    values: Option<&Array2<f64>>,
) -> Result<PolygonSet> {
    let (ncol, nrow) = x.dim();
    if ncol < 2 || nrow < 2 {
        return Err(GeoError::GeometryInference(
            "occupied edge requires at least 2x2 nodes".into(),
        ));
    }
    if let Some(z) = values {
        if z.dim() != (ncol, nrow) {
            return Err(GeoError::GeometryInference(
                "occupied edge value array does not match node geometry".into(),
            ));
        }
    }

    let mut node_present = vec![false; ncol * nrow];
    for j in 0..nrow {
        for i in 0..ncol {
            let xy_present = x[[i, j]].is_finite() && y[[i, j]].is_finite();
            let value_present = values.map(|z| z[[i, j]].is_finite()).unwrap_or(true);
            node_present[i + j * ncol] = xy_present && value_present;
        }
    }

    let cell_ncol = ncol - 1;
    let cell_nrow = nrow - 1;
    let mut cell_present = vec![false; cell_ncol * cell_nrow];
    for j in 0..cell_nrow {
        for i in 0..cell_ncol {
            cell_present[i + j * cell_ncol] = node_present[i + j * ncol]
                && node_present[i + 1 + j * ncol]
                && node_present[i + 1 + (j + 1) * ncol]
                && node_present[i + (j + 1) * ncol];
        }
    }

    let has_cell = |cells: &[bool], i: isize, j: isize| -> bool {
        if i < 0 || j < 0 || i >= cell_ncol as isize || j >= cell_nrow as isize {
            return false;
        }
        cells[i as usize + j as usize * cell_ncol]
    };

    let mut next: HashMap<(usize, usize), Vec<(usize, usize)>> = HashMap::new();
    let mut edge_count = 0usize;
    for j in 0..cell_nrow {
        for i in 0..cell_ncol {
            if !cell_present[i + j * cell_ncol] {
                continue;
            }
            if !has_cell(&cell_present, i as isize, j as isize - 1) {
                add_boundary_edge(&mut next, &mut edge_count, (i, j), (i + 1, j));
            }
            if !has_cell(&cell_present, i as isize + 1, j as isize) {
                add_boundary_edge(&mut next, &mut edge_count, (i + 1, j), (i + 1, j + 1));
            }
            if !has_cell(&cell_present, i as isize, j as isize + 1) {
                add_boundary_edge(&mut next, &mut edge_count, (i + 1, j + 1), (i, j + 1));
            }
            if !has_cell(&cell_present, i as isize - 1, j as isize) {
                add_boundary_edge(&mut next, &mut edge_count, (i, j + 1), (i, j));
            }
        }
    }

    if edge_count == 0 {
        return Err(GeoError::GeometryInference(
            "occupied edge has no complete occupied cells".into(),
        ));
    }

    let mut rings = Vec::new();
    while let Some((start, mut current)) = pop_any_boundary_edge(&mut next) {
        let mut ring_nodes = vec![start];
        while current != start {
            ring_nodes.push(current);
            if ring_nodes.len() > edge_count + 1 {
                return Err(GeoError::GeometryInference(
                    "occupied edge tracing did not close".into(),
                ));
            }
            current = pop_boundary_edge(&mut next, current).ok_or_else(|| {
                GeoError::GeometryInference("occupied edge boundary is not closed".into())
            })?;
        }
        if ring_nodes.len() >= 3 {
            rings.push(
                ring_nodes
                    .into_iter()
                    .map(|(i, j)| [x[[i, j]], y[[i, j]], 0.0])
                    .collect::<Vec<_>>(),
            );
        }
    }

    if rings.is_empty() {
        return Err(GeoError::GeometryInference(
            "occupied edge has fewer than three boundary vertices".into(),
        ));
    }
    Ok(PolygonSet::from_rings(rings))
}

fn add_boundary_edge(
    next: &mut HashMap<(usize, usize), Vec<(usize, usize)>>,
    edge_count: &mut usize,
    from: (usize, usize),
    to: (usize, usize),
) {
    next.entry(from).or_default().push(to);
    *edge_count += 1;
}

fn pop_any_boundary_edge(
    next: &mut HashMap<(usize, usize), Vec<(usize, usize)>>,
) -> Option<((usize, usize), (usize, usize))> {
    let from = *next
        .iter()
        .find(|(_, outs)| !outs.is_empty())
        .map(|(k, _)| k)?;
    let to = pop_boundary_edge(next, from)?;
    Some((from, to))
}

fn pop_boundary_edge(
    next: &mut HashMap<(usize, usize), Vec<(usize, usize)>>,
    from: (usize, usize),
) -> Option<(usize, usize)> {
    let out = {
        let outs = next.get_mut(&from)?;
        outs.pop()
    };
    if next.get(&from).is_some_and(Vec::is_empty) {
        next.remove(&from);
    }
    out
}

fn push_finite_node(
    ring: &mut Vec<[f64; 3]>,
    x: &Array2<f64>,
    y: &Array2<f64>,
    i: usize,
    j: usize,
) -> Result<()> {
    let xi = x[[i, j]];
    let yi = y[[i, j]];
    if !xi.is_finite() || !yi.is_finite() {
        return Err(GeoError::GeometryInference(
            "structured surface perimeter edge has missing boundary nodes".into(),
        ));
    }
    ring.push([xi, yi, 0.0]);
    Ok(())
}

impl HasHistory for PointSet {
    fn operation_history(&self) -> &OperationHistory {
        &self.history
    }

    fn operation_history_mut(&mut self) -> &mut OperationHistory {
        &mut self.history
    }
}

/// Infer a regular lattice from bare coordinates, returning it alongside the lattice
/// index of every finite point — the occupancy the edge builders need.
/// The occupied-node footprint of a lattice, from the node indices inference already
/// resolved. Feeds the same edge builder the topology path uses, so a point set without
/// `column`/`row` gets an identical footprint without triangulating the cloud.
fn occupied_edge_from_lattice_indices(
    geom: &GridGeometry,
    occupancy: &[(usize, usize)],
) -> Result<PolygonSet> {
    let mut x = Array2::from_elem((geom.ncol, geom.nrow), f64::NAN);
    let mut y = Array2::from_elem((geom.ncol, geom.nrow), f64::NAN);
    for &(i, j) in occupancy {
        let (nx, ny) = geom.node_xy(i, j);
        x[[i, j]] = nx;
        y[[i, j]] = ny;
    }
    occupied_edge_from_node_arrays(&x, &y, None)
        .or_else(|_| perimeter_edge_from_node_arrays(&x, &y))
}

fn infer_grid_geometry_from_index_attrs(
    coords: &[[f64; 3]],
    attrs: &IndexMap<String, Vec<f64>>,
    tolerance: f64,
) -> Result<Option<GridGeometry>> {
    if !tolerance.is_finite() || tolerance <= 0.0 {
        return Err(GeoError::GeometryInference(
            "tolerance must be a finite positive number".into(),
        ));
    }
    let Some(columns) = find_attr(attrs, &["column", "col"]) else {
        return Ok(None);
    };
    let Some(rows) = find_attr(attrs, &["row"]) else {
        return Ok(None);
    };
    if columns.len() != coords.len() || rows.len() != coords.len() {
        return Err(GeoError::GeometryInference(
            "column/row attributes must match point count".into(),
        ));
    }

    let mut indexed = Vec::new();
    for (idx, c) in coords.iter().enumerate() {
        if !c[0].is_finite() || !c[1].is_finite() {
            continue;
        }
        let Some(col) = integer_attr(columns[idx], "column")? else {
            continue;
        };
        let Some(row) = integer_attr(rows[idx], "row")? else {
            continue;
        };
        indexed.push((col, row, c[0], c[1]));
    }
    // The fit itself is single-homed in `core::shell::fit` (shared with
    // `StructuredShell::infer_grid` / `MeshShell::infer_grid`).
    fit_grid_from_indexed(&indexed, tolerance).map(Some)
}

fn find_attr<'a>(attrs: &'a IndexMap<String, Vec<f64>>, names: &[&str]) -> Option<&'a [f64]> {
    attrs.iter().find_map(|(key, values)| {
        let normalized = crate::io::normalize_attr_name(key);
        names
            .iter()
            .any(|name| normalized == *name)
            .then_some(values.as_slice())
    })
}

fn integer_attr(value: f64, name: &str) -> Result<Option<isize>> {
    if value.is_nan() {
        return Ok(None);
    }
    if !value.is_finite() {
        return Err(GeoError::GeometryInference(format!(
            "{name} attribute contains a non-finite value"
        )));
    }
    let rounded = value.round();
    if (value - rounded).abs() > 1e-6 {
        return Err(GeoError::GeometryInference(format!(
            "{name} attribute contains non-integer grid indices"
        )));
    }
    Ok(Some(rounded as isize))
}

fn add_xy_only_inference_hint(
    err: GeoError,
    coords: &[[f64; 3]],
    attrs: &IndexMap<String, Vec<f64>>,
    tolerance: f64,
) -> GeoError {
    let GeoError::GeometryInference(message) = err else {
        return err;
    };
    let has_index_attrs =
        find_attr(attrs, &["column", "col"]).is_some() || find_attr(attrs, &["row"]).is_some();
    if has_index_attrs || !has_duplicate_xy(coords, tolerance) {
        return GeoError::GeometryInference(message);
    }
    GeoError::GeometryInference(format!(
        "{message}; duplicate XY nodes were found and no column/row topology attributes are \
         available. Petrel surface point exports can lose grid topology in plain IRAP-points \
         form; an EarthVisionGrid export (or a point file carrying column/row) restores it. Note \
         that topology alone does not make a mesh regular — if the recovered mesh is curvilinear \
         it has no GridGeometry at all, and to_structured_surface is the exact representation"
    ))
}

fn has_duplicate_xy(coords: &[[f64; 3]], tolerance: f64) -> bool {
    let scale = 1.0 / tolerance.max(1e-9);
    let mut seen = std::collections::HashSet::new();
    for c in coords {
        if !c[0].is_finite() || !c[1].is_finite() {
            continue;
        }
        let key = ((c[0] * scale).round() as i64, (c[1] * scale).round() as i64);
        if !seen.insert(key) {
            return true;
        }
    }
    false
}

#[cfg(test)]
mod tests {
    use super::*;

    fn pts() -> PointSet {
        let coords = vec![[0.0, 0.0, 1.0], [10.0, 0.0, 2.0], [0.0, 10.0, 3.0]];
        let mut attrs = IndexMap::new();
        attrs.insert("poro".to_string(), vec![0.1, 0.2, 0.3]);
        PointSet::from_parts(coords, attrs)
    }

    #[test]
    fn len_and_attr_and_stats() {
        let p = pts();
        assert_eq!(p.len(), 3);
        assert!(!p.is_empty());
        assert_eq!(p.attr("poro").unwrap(), &[0.1, 0.2, 0.3]);
        assert!(p.attr("missing").is_none());
        let s = p.stats("poro").unwrap();
        assert_eq!(s.count, 3);
        approx::assert_relative_eq!(s.mean, 0.2);
        assert!(p.stats("nope").is_none());
    }

    #[test]
    fn from_coords_and_z_stats() {
        let p = PointSet::from_coords(vec![
            [0.0, 0.0, -100.0],
            [1.0, 1.0, -120.0],
            [2.0, 2.0, -140.0],
        ]);
        assert_eq!(p.len(), 3);
        assert!(p.attr("z").is_none()); // z is a coordinate, not a named attr
        let z = p.z_stats();
        assert_eq!(z.count, 3);
        approx::assert_relative_eq!(z.mean, -120.0);
        approx::assert_relative_eq!(z.min, -140.0);
        approx::assert_relative_eq!(z.max, -100.0);
    }

    #[test]
    fn coords_round_trips_from_coords() {
        // The read side of `from_coords`: identical slice, load order preserved,
        // `NaN` carried through unchanged (undefined convention).
        let raw = vec![[0.0, 0.0, -100.0], [1.0, 1.0, f64::NAN], [2.0, 2.0, -140.0]];
        let p = PointSet::from_coords(raw.clone());
        let got = p.coords();
        assert_eq!(got.len(), raw.len());
        for (g, r) in got.iter().zip(&raw) {
            assert_eq!(g[0], r[0]);
            assert_eq!(g[1], r[1]);
            if r[2].is_nan() {
                assert!(g[2].is_nan(), "NaN must carry through");
            } else {
                assert_eq!(g[2], r[2]);
            }
        }
    }

    #[test]
    fn bbox_covers_points() {
        let b = pts().bbox();
        approx::assert_relative_eq!(b.xmin, 0.0);
        approx::assert_relative_eq!(b.xmax, 10.0);
        approx::assert_relative_eq!(b.ymin, 0.0);
        approx::assert_relative_eq!(b.ymax, 10.0);
    }

    #[test]
    fn nearest_matches_brute_force() {
        let p = pts();
        // brute-force nearest to a few query points
        let queries = [(1.0, 1.0), (9.0, 1.0), (1.0, 9.0), (5.0, 5.0)];
        for (qx, qy) in queries {
            let brute = (0..p.len())
                .min_by(|&a, &b| {
                    let da = (p.coords[a][0] - qx).powi(2) + (p.coords[a][1] - qy).powi(2);
                    let db = (p.coords[b][0] - qx).powi(2) + (p.coords[b][1] - qy).powi(2);
                    da.total_cmp(&db)
                })
                .unwrap();
            assert_eq!(p.nearest(qx, qy), Some(brute));
        }
    }

    #[test]
    fn filter_keeps_matching_rows_and_attrs() {
        let p = pts().filter(|pt| pt.x < 5.0);
        assert_eq!(p.len(), 2); // (0,0) and (0,10)
        assert_eq!(p.attr("poro").unwrap(), &[0.1, 0.3]);
    }

    #[test]
    fn empty_nearest_is_none() {
        let p = PointSet::from_parts(Vec::new(), IndexMap::new());
        assert!(p.is_empty());
        assert!(p.nearest(0.0, 0.0).is_none());
    }

    #[test]
    fn infer_geometry_recovers_rotated_lattice() {
        let source = GridGeometry {
            xori: 456_123.5,
            yori: 6_712_345.25,
            xinc: 37.0,
            yinc: 83.0,
            ncol: 5,
            nrow: 4,
            rotation_deg: 27.5,
            yflip: false,
        };
        let mut coords = Vec::new();
        for j in 0..source.nrow {
            for i in 0..source.ncol {
                let (x, y) = source.node_xy(i, j);
                coords.push([x, y, 1000.0 + i as f64 + j as f64]);
            }
        }

        let p = PointSet::from_coords(coords);
        let (geom, edge) = p
            .infer_geometry_with_edge(1e-6, GeometryEdge::FullRect)
            .unwrap();
        approx::assert_relative_eq!(geom.xori, source.xori, epsilon = 1e-6);
        approx::assert_relative_eq!(geom.yori, source.yori, epsilon = 1e-6);
        approx::assert_relative_eq!(geom.xinc, source.xinc, epsilon = 1e-9);
        approx::assert_relative_eq!(geom.yinc, source.yinc, epsilon = 1e-9);
        assert_eq!(geom.ncol, source.ncol);
        assert_eq!(geom.nrow, source.nrow);
        approx::assert_relative_eq!(geom.rotation_deg, source.rotation_deg, epsilon = 1e-9);
        approx::assert_relative_eq!(edge.area(), (4.0 * 37.0) * (3.0 * 83.0), epsilon = 1e-6);
    }

    #[test]
    fn infer_geometry_uses_explicit_column_row_topology() {
        let source = GridGeometry {
            xori: 1000.0,
            yori: 2000.0,
            xinc: 50.0,
            yinc: 25.0,
            ncol: 4,
            nrow: 3,
            rotation_deg: 35.0,
            yflip: false,
        };
        let mut coords = Vec::new();
        let mut columns = Vec::new();
        let mut rows = Vec::new();
        for j in 0..source.nrow {
            for i in 0..source.ncol {
                let (mut x, mut y) = source.node_xy(i, j);
                if i == 2 && j == 1 {
                    x += 3.0;
                    y -= 2.0;
                }
                coords.push([x, y, 1000.0 + i as f64 + j as f64]);
                columns.push((i + 1) as f64);
                rows.push((j + 1) as f64);
            }
        }
        // A repeated XY node can happen in Petrel point exports around collapsed
        // or clipped grid nodes. With column/row present, topology still wins.
        coords[2] = coords[1];

        let mut attrs = IndexMap::new();
        attrs.insert("column".to_string(), columns);
        attrs.insert("row".to_string(), rows);
        let p = PointSet::from_parts(coords, attrs);
        let (geom, edge) = p
            .infer_geometry_with_edge(1e-3, GeometryEdge::ConvexHull)
            .unwrap();

        assert_eq!(geom.ncol, source.ncol);
        assert_eq!(geom.nrow, source.nrow);
        approx::assert_relative_eq!(geom.xinc, source.xinc, epsilon = 1e-9);
        approx::assert_relative_eq!(geom.yinc, source.yinc, epsilon = 1e-9);
        approx::assert_relative_eq!(geom.rotation_deg, source.rotation_deg, epsilon = 1e-9);
        assert!(edge.area() > 0.0);
    }

    #[test]
    fn infer_geometry_rejects_curvilinear_mesh_carrying_column_row() {
        // A curvilinear mesh: regular column/row topology, but the node spacing swells
        // across the grid so no single (xinc, yinc, rotation) lattice fits it. The
        // median-delta estimator will still produce a plausible GridGeometry; inference
        // must refuse it rather than hand back a lattice the nodes do not sit on.
        let (ncol, nrow) = (12usize, 12usize);
        let mut coords = Vec::new();
        let mut columns = Vec::new();
        let mut rows = Vec::new();
        for j in 0..nrow {
            for i in 0..ncol {
                let swell = 1.0 + 0.06 * i as f64;
                let x = 1000.0 + 50.0 * i as f64 * swell;
                let y = 2000.0 + 50.0 * j as f64 * (1.0 + 0.04 * j as f64);
                coords.push([x, y, 10.0]);
                columns.push((i + 1) as f64);
                rows.push((j + 1) as f64);
            }
        }
        let mut attrs = IndexMap::new();
        attrs.insert("column".to_string(), columns);
        attrs.insert("row".to_string(), rows);
        let p = PointSet::from_parts(coords, attrs);

        let msg = match p.infer_geometry_with_edge(1e-3, GeometryEdge::FullRect) {
            Ok(_) => panic!("a curvilinear mesh has no regular GridGeometry"),
            Err(err) => err.to_string(),
        };
        assert!(msg.contains("curvilinear"), "unexpected message: {msg}");
        assert!(
            msg.contains("to_structured_surface"),
            "the error must point at the exact representation: {msg}"
        );

        let tri = p
            .to_tri_surface(None, None)
            .expect("a rejected regular lattice must remain representable as a TIN");
        assert!(!tri.triangles().is_empty());
        assert!(tri.points().len() <= p.len());

        // ...and the structured path, which stores explicit per-node XY, still works:
        // a curvilinear mesh simply has no nominal regular geometry.
        let mesh = p
            .to_structured_surface(1e-3, GeometryEdge::Occupied)
            .expect("structured surface represents a curvilinear mesh exactly");
        assert_eq!(mesh.ncol(), ncol);
        assert_eq!(mesh.nrow(), nrow);
        assert!(
            mesh.nominal_geometry().is_none(),
            "a curvilinear mesh must not advertise a regular nominal geometry"
        );
    }

    #[test]
    fn curvilinear_structured_edge_modes_remain_distinct() {
        // Curvilinear L-shaped topology: strict regular inference must fail,
        // while the exact structured representation supports both the concave
        // occupied footprint and its broader convex envelope.
        let mut coords = Vec::new();
        let mut columns = Vec::new();
        let mut rows = Vec::new();
        for j in 0..6 {
            for i in 0..6 {
                if i > 2 && j > 2 {
                    continue;
                }
                let x = 1000.0 + 40.0 * i as f64 * (1.0 + 0.055 * i as f64) + 0.8 * (i * j) as f64;
                let y = 2000.0 + 35.0 * j as f64 * (1.0 + 0.045 * j as f64) + 0.35 * (i * j) as f64;
                coords.push([x, y, 100.0 + (i + j) as f64]);
                columns.push((i + 1) as f64);
                rows.push((j + 1) as f64);
            }
        }
        let mut attrs = IndexMap::new();
        attrs.insert("column".to_string(), columns);
        attrs.insert("row".to_string(), rows);
        let points = PointSet::from_parts(coords, attrs);

        assert!(points
            .infer_geometry_with_edge(1e-3, GeometryEdge::ConvexHull)
            .is_err());
        let occupied = points
            .to_structured_surface(1e-3, GeometryEdge::Occupied)
            .unwrap();
        let hull = points
            .to_structured_surface(1e-3, GeometryEdge::ConvexHull)
            .unwrap();
        assert!(occupied.edge().area() < hull.edge().area());
        approx::assert_relative_eq!(occupied.edge().area(), 33_384.96, epsilon = 1e-8);
        approx::assert_relative_eq!(hull.edge().area(), 45_348.642_5, epsilon = 1e-8);
    }

    #[test]
    fn occupied_edge_follows_topology_cells() {
        let mut coords = Vec::new();
        let mut columns = Vec::new();
        let mut rows = Vec::new();
        for j in 0..4 {
            for i in 0..4 {
                if i <= 1 || j <= 1 {
                    coords.push([i as f64, j as f64, 100.0 + i as f64 + j as f64]);
                    columns.push((i + 1) as f64);
                    rows.push((j + 1) as f64);
                }
            }
        }

        let mut attrs = IndexMap::new();
        attrs.insert("column".to_string(), columns);
        attrs.insert("row".to_string(), rows);
        let p = PointSet::from_parts(coords, attrs);

        let (_, occupied) = p
            .infer_geometry_with_edge(1e-6, GeometryEdge::Occupied)
            .unwrap();
        let (_, hull) = p
            .infer_geometry_with_edge(1e-6, GeometryEdge::ConvexHull)
            .unwrap();
        let (_, full_rect) = p
            .infer_geometry_with_edge(1e-6, GeometryEdge::FullRect)
            .unwrap();

        // The L-shaped footprint: `occupied` tracks it, the rectangle over-claims.
        approx::assert_relative_eq!(occupied.area(), 5.0, epsilon = 1e-12);
        approx::assert_relative_eq!(full_rect.area(), 9.0, epsilon = 1e-12);
        assert!(hull.area() > occupied.area());
        assert!(full_rect.area() > hull.area());
    }

    #[test]
    fn occupied_and_full_rect_agree_on_a_fully_populated_lattice() {
        // When every node of the bounding lattice carries data there is no footprint to
        // track, and the two edges must coincide. They diverge only where nodes are
        // missing — which is precisely the case `full_rect` cannot express.
        let source = GridGeometry {
            xori: 100.0,
            yori: 200.0,
            xinc: 10.0,
            yinc: 25.0,
            ncol: 5,
            nrow: 4,
            rotation_deg: 20.0,
            yflip: false,
        };
        let mut coords = Vec::new();
        for j in 0..source.nrow {
            for i in 0..source.ncol {
                let (x, y) = source.node_xy(i, j);
                coords.push([x, y, 1.0]);
            }
        }
        let p = PointSet::from_coords(coords);

        let (geom, occupied) = p
            .infer_geometry_with_edge(1e-9, GeometryEdge::Occupied)
            .unwrap();
        let (_, full_rect) = p
            .infer_geometry_with_edge(1e-9, GeometryEdge::FullRect)
            .unwrap();

        assert_eq!(geom.ncol, source.ncol);
        assert_eq!(geom.nrow, source.nrow);
        let expected = (4.0 * 10.0) * (3.0 * 25.0);
        approx::assert_relative_eq!(full_rect.area(), expected, epsilon = 1e-9);
        approx::assert_relative_eq!(occupied.area(), expected, epsilon = 1e-9);
    }

    #[test]
    fn occupied_edge_without_topology_matches_the_topology_footprint() {
        // Same L-shaped lattice as `occupied_edge_follows_topology_cells`, but with no
        // column/row attributes. The coordinate path derives each point's lattice index
        // anyway, so both paths must agree on the footprint — the occupancy is the same
        // fact, however it arrives. (Before unification this path triangulated the cloud
        // and answered 5.5.)
        let mut coords = Vec::new();
        for j in 0..4 {
            for i in 0..4 {
                if i <= 1 || j <= 1 {
                    coords.push([i as f64, j as f64, 100.0 + i as f64 + j as f64]);
                }
            }
        }
        let p = PointSet::from_coords(coords);

        let (_, occupied) = p
            .infer_geometry_with_edge(1e-6, GeometryEdge::Occupied)
            .unwrap();
        let (_, full_rect) = p
            .infer_geometry_with_edge(1e-6, GeometryEdge::FullRect)
            .unwrap();

        approx::assert_relative_eq!(occupied.area(), 5.0, epsilon = 1e-12);
        approx::assert_relative_eq!(full_rect.area(), 9.0, epsilon = 1e-12);
    }

    #[test]
    fn to_structured_surface_preserves_locally_shifted_nodes() {
        let coords = vec![
            [0.0, 0.0, 100.0],
            [10.0, 0.0, 101.0],
            [0.0, 10.0, 102.0],
            [12.0, 10.0, 103.0],
        ];
        let mut attrs = IndexMap::new();
        attrs.insert("column".to_string(), vec![1.0, 2.0, 1.0, 2.0]);
        attrs.insert("row".to_string(), vec![1.0, 1.0, 2.0, 2.0]);
        let p = PointSet::from_parts(coords, attrs);

        let s = p
            .to_structured_surface(1e-3, GeometryEdge::Occupied)
            .unwrap();

        assert_eq!(s.kind(), "structured_mesh");
        assert_eq!(s.ncol(), 2);
        assert_eq!(s.nrow(), 2);
        assert_eq!(s.node_xy(1, 1).unwrap(), (12.0, 10.0));
        assert_eq!(s.z(1, 1).unwrap(), 103.0);
        assert_eq!(s.stats().count, 4);
        assert!(s.edge().area() > 0.0);
        assert!(s
            .history()
            .iter()
            .any(|h| h == "points.to_structured_surface(edge=Occupied)"));
    }

    #[test]
    fn structured_surface_occupied_edge_tracks_concave_cells() {
        let mut coords = Vec::new();
        let mut columns = Vec::new();
        let mut rows = Vec::new();
        for j in 0..4 {
            for i in 0..4 {
                if i <= 1 || j <= 1 {
                    coords.push([i as f64, j as f64, 100.0 + i as f64 + j as f64]);
                    columns.push((i + 1) as f64);
                    rows.push((j + 1) as f64);
                }
            }
        }

        let mut attrs = IndexMap::new();
        attrs.insert("column".to_string(), columns);
        attrs.insert("row".to_string(), rows);
        let p = PointSet::from_parts(coords, attrs);

        let s = p
            .to_structured_surface(1e-6, GeometryEdge::Occupied)
            .unwrap();

        approx::assert_relative_eq!(s.edge().area(), 5.0, epsilon = 1e-12);
    }

    #[test]
    fn to_structured_surface_requires_topology() {
        let p = PointSet::from_coords(vec![
            [0.0, 0.0, 100.0],
            [10.0, 0.0, 101.0],
            [0.0, 10.0, 102.0],
            [10.0, 10.0, 103.0],
        ]);

        let err = match p.to_structured_surface(1e-3, GeometryEdge::Occupied) {
            Ok(_) => panic!("expected missing topology error"),
            Err(err) => err,
        };
        assert!(format!("{err}").contains("requires column/row topology"));
    }

    #[test]
    fn infer_geometry_errors_for_scattered_points() {
        let p = PointSet::from_coords(vec![
            [0.0, 0.0, 1.0],
            [11.0, 0.2, 2.0],
            [3.0, 8.7, 3.0],
            [19.0, 4.1, 4.0],
            [7.0, 17.3, 5.0],
        ]);
        assert!(matches!(
            p.infer_geometry(1e-3),
            Err(GeoError::GeometryInference(_))
        ));
    }

    fn grid5() -> crate::foundation::GridGeometry {
        crate::foundation::GridGeometry {
            xori: 0.0,
            yori: 0.0,
            xinc: 2.5,
            yinc: 2.5,
            ncol: 5,
            nrow: 5,
            rotation_deg: 0.0,
            yflip: false,
        }
    }

    #[test]
    fn warm_start_honours_constraints_and_converges() {
        let p = pts();
        let cold = p.to_surface(grid5(), GridMethod::MinimumCurvature).unwrap();
        let warm = p.regrid_min_curvature(&cold).unwrap();
        assert_eq!(warm.geom, cold.geom);
        // Hard constraints: each input point snaps to a node held at its z.
        // Points (0,0,1)→node[0,0], (10,0,2)→node[4,0], (0,10,3)→node[0,4].
        approx::assert_relative_eq!(warm.values()[[0, 0]], 1.0, epsilon = 1e-9);
        approx::assert_relative_eq!(warm.values()[[4, 0]], 2.0, epsilon = 1e-9);
        approx::assert_relative_eq!(warm.values()[[0, 4]], 3.0, epsilon = 1e-9);
        // A second warm pass is a near-fixed point (the field has converged).
        let warm2 = p.regrid_min_curvature(&warm).unwrap();
        for (a, b) in warm2.values().iter().zip(warm.values().iter()) {
            approx::assert_relative_eq!(a, b, epsilon = 1e-6);
        }
    }

    #[test]
    fn regrid_empty_errors() {
        let empty = PointSet::from_parts(Vec::new(), IndexMap::new());
        let prior = pts()
            .to_surface(grid5(), GridMethod::MinimumCurvature)
            .unwrap();
        assert!(empty.regrid_min_curvature(&prior).is_err());
    }
}