pyrucast 0.1.2

Finite-element library in Rust, inspired by cast3m, exposed to Python
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
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
//! Volumetric mesher: fill the interior of a closed triangular surface
//! envelope with size-controlled tetrahedra.
//!
//! This is the 3-D companion of [`crate::ops::mesher::triangulate_surface()`]
//! (which meshes the interior of a closed 2-D contour), built on the same
//! **constrained-Delaunay + flood-fill excavation** pipeline lifted to 3-D
//! (rather than a centroid carve, which let tetrahedra cross concavities and
//! holes — the "stray triangle" artefact):
//!
//! 1. **Boundary Delaunay** — the boundary nodes alone are tetrahedralized with
//!    the incremental Bowyer–Watson algorithm. A tiny deterministic jitter is
//!    applied to the *connectivity* computation so degenerate, cospherical
//!    inputs (a cube's eight corners, say) are handled without ambiguity; the
//!    output keeps the exact coordinates.
//! 2. **Boundary recovery** — every skin face missing from that Delaunay (a
//!    quad diagonal taken the other way, a concavity edge) is recovered by
//!    re-tetrahedralizing the small corridor it crosses, then marked
//!    constrained. A skin face left unrecoverable raises a clear error
//!    (Schönhardt-type polyhedron) rather than a silently wrong mesh.
//! 3. **Interior points** — a grid of candidate nodes at the target spacing is
//!    inserted into the *already-constrained* triangulation (none when the
//!    target size exceeds the geometry), each Bowyer–Watson cavity clipped at
//!    constrained faces so an interior node can never carve a skin face away.
//! 4. **Excavation** — a flood fill from an interior tetrahedron keeps exactly
//!    what the skin encloses, never crossing the surface, so concavities, holes
//!    and the far side of a thin part are excavated exactly.
//!
//! The envelope must be a **closed, consistently oriented TRI3 surface** (one
//! or more submeshes, all TRI3) attached to a **3-D** `Coords`. The target
//! size is **uniform**. The output is a [`Mesh`] with a single TET4 submesh:
//! the original surface nodes are reused, interior nodes are created in the
//! same `Coords`. Convex and mildly concave envelopes mesh directly; strongly
//! non-convex surfaces rich in **reflex edges** (a faceted hole's rim, say) can
//! still trip the recovery error — full exact-predicate 3-D boundary recovery,
//! a variable density field and QUA4 input are left to later steps.

use crate::containers::mesh::{ElementType, Mesh, Node, NodeId, Point3, SubMesh, Vector3};
use crate::error::{PyrucastError, Result};
use crate::interrupt::{Cancel, NoCancel};
use crate::store::read;
use std::collections::{HashMap, HashSet};

/// Geometric tolerance factor applied to the model size to decide degeneracy
/// (zero-volume tetrahedra, collapsed faces).
const EPS_FACTOR: f64 = 1e-9;

/// Fill the interior of a closed **TRI3** surface envelope with tetrahedra
/// using the Delaunay method described in the module docs.
///
/// `envelope` must be a [`Mesh`] whose submeshes are **all TRI3**, forming a
/// single closed, consistently oriented surface attached to a **3-D**
/// `Coords`. `target_size` sets the desired element edge length; `None` uses
/// the mean edge length of the envelope's faces.
///
/// The original surface nodes are reused (and re-referenced); interior nodes
/// are created in the same `Coords`. Output tetrahedra follow the
/// [`ElementType::TET4`] convention (positive signed volume — face 0-1-2 CCW
/// seen from node 3).
///
/// This is the uninterruptible convenience form; for a long mesh that a
/// caller may want to stop early, use [`volume_cancellable`].
pub fn volume(envelope: &Mesh, target_size: Option<f64>) -> Result<Mesh> {
    volume_cancellable(envelope, target_size, &NoCancel)
}

/// Like [`volume`], but polls `cancel` periodically so the meshing can be
/// stopped early (returning [`PyrucastError::Interrupted`]). The frontend
/// chooses what `cancel` means — a timeout, an external flag, or, in the
/// Python binding, a `Ctrl+C` via `Python::check_signals`.
pub fn volume_cancellable(
    envelope: &Mesh,
    target_size: Option<f64>,
    cancel: &dyn Cancel,
) -> Result<Mesh> {
    if let Some(h) = target_size {
        if h <= 0.0 || h.is_nan() {
            return Err(PyrucastError::Message(format!(
                "volume: target_size must be > 0, got {}",
                h
            )));
        }
    }

    let coords = envelope.coords()?;
    let dim = read(&coords)?.dim();
    if dim != 3 {
        return Err(PyrucastError::Message(format!(
            "volume: envelope must be 3-D, got dim={}",
            dim
        )));
    }

    // 1. Trace the surface into a compact point list (boundary nodes first)
    //    and its triangular faces as index triples into that list.
    let (mut points, node_ids, faces) = trace_surface(envelope)?;
    let n0 = points.len();

    // 2. Validate: a closed, consistently oriented triangular manifold.
    validate_closed_oriented(&faces)?;

    // 3. Mesh: generate interior points, Delaunay, then carve to the domain.
    let tets = pave_volume(&mut points, &faces, target_size, cancel)?;
    if tets.is_empty() {
        return Err(PyrucastError::Message(
            "volume: produced no tetrahedron (degenerate or non-closed envelope?)".into(),
        ));
    }

    // 4. Materialise: reuse boundary nodes, create one node per interior
    //    point, then build the TET4 mesh.
    let mut flat_to_node: Vec<NodeId> = Vec::with_capacity(points.len());
    flat_to_node.extend_from_slice(&node_ids);
    // Keep the interior `Node`s alive until the cells reference them.
    let mut _interior: Vec<Node> = Vec::with_capacity(points.len() - n0);
    for p in &points[n0..] {
        let node = Node::create_in(coords.clone(), &[p.x, p.y, p.z])?;
        flat_to_node.push(node.id());
        _interior.push(node);
    }

    let mut mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::TET4));
    for [i, j, k, l] in &tets {
        mesh.add_cell(&[
            flat_to_node[*i],
            flat_to_node[*j],
            flat_to_node[*k],
            flat_to_node[*l],
        ])?;
    }
    Ok(mesh)
}

/// Trace the envelope into `(points, node_ids, faces)`:
/// - `points[i]` is the coordinate of boundary node `i` (compact 0-based);
/// - `node_ids[i]` is that node's [`NodeId`] (for reuse on output);
/// - `faces` lists each TRI3 as a triple of indices into `points`.
///
/// Errors if a submesh is not TRI3 or if there are fewer than four faces.
type TracedSurface = (Vec<Point3>, Vec<NodeId>, Vec<[usize; 3]>);
fn trace_surface(envelope: &Mesh) -> Result<TracedSurface> {
    let coords = envelope.coords()?;
    let c = read(&coords)?;

    let mut index_of: HashMap<NodeId, usize> = HashMap::new();
    let mut node_ids: Vec<NodeId> = Vec::new();
    let mut points: Vec<Point3> = Vec::new();
    let mut faces: Vec<[usize; 3]> = Vec::new();

    let compact = |id: NodeId,
                   index_of: &mut HashMap<NodeId, usize>,
                   node_ids: &mut Vec<NodeId>,
                   points: &mut Vec<Point3>|
     -> Result<usize> {
        if let Some(&i) = index_of.get(&id) {
            return Ok(i);
        }
        let s = c.coord(id)?;
        let i = points.len();
        points.push(Point3::new(s[0], s[1], s[2]));
        node_ids.push(id);
        index_of.insert(id, i);
        Ok(i)
    };

    for sm in envelope {
        let (et, conn) = {
            let s = read(sm)?;
            (s.element_type(), s.connectivity().to_vec())
        };
        if et != ElementType::TRI3 {
            return Err(PyrucastError::Message(format!(
                "volume: envelope must be a TRI3 surface, got {}",
                et
            )));
        }
        for tri in conn.chunks(3) {
            let a = compact(tri[0], &mut index_of, &mut node_ids, &mut points)?;
            let b = compact(tri[1], &mut index_of, &mut node_ids, &mut points)?;
            let d = compact(tri[2], &mut index_of, &mut node_ids, &mut points)?;
            faces.push([a, b, d]);
        }
    }

    if faces.len() < 4 {
        return Err(PyrucastError::Message(format!(
            "volume: envelope must have ≥ 4 triangular faces, got {}",
            faces.len()
        )));
    }
    Ok((points, node_ids, faces))
}

/// Check that the faces form a **closed, consistently oriented** triangular
/// manifold: every directed edge occurs exactly once and its reverse occurs
/// exactly once (so every undirected edge is shared by exactly two faces with
/// opposite orientation). Errors otherwise — an open boundary, a non-manifold
/// edge, or an inconsistent winding.
fn validate_closed_oriented(faces: &[[usize; 3]]) -> Result<()> {
    let mut dir: HashMap<(usize, usize), u32> = HashMap::new();
    for f in faces {
        for &(u, v) in &[(f[0], f[1]), (f[1], f[2]), (f[2], f[0])] {
            *dir.entry((u, v)).or_insert(0) += 1;
        }
    }
    for (&(u, v), &count) in &dir {
        if count != 1 {
            return Err(PyrucastError::Message(format!(
                "volume: surface is non-manifold — directed edge ({}, {}) used {} times \
                 (each must appear once)",
                u, v, count
            )));
        }
        if dir.get(&(v, u)) != Some(&1) {
            return Err(PyrucastError::Message(format!(
                "volume: surface is open or inconsistently oriented — edge ({}, {}) has no \
                 matching opposite face",
                u, v
            )));
        }
    }
    Ok(())
}

/// Signed volume (×6) of tetrahedron `(a, b, c, d)`; positive iff `d` lies on
/// the side of face `(a, b, c)` its normal `cross(b-a, c-a)` points to.
#[inline]
fn tet_vol6(a: Point3, b: Point3, c: Point3, d: Point3) -> f64 {
    (b - a).cross(&(c - a)).dot(&(d - a))
}

/// Core mesher (no store access — a clean target for future intra-operator
/// parallelism). `points` starts with the boundary nodes (unchanged, in
/// order) and is extended in place with the generated interior points; the
/// returned tetrahedra index into it, each oriented to positive signed volume.
fn pave_volume(
    points: &mut Vec<Point3>,
    faces: &[[usize; 3]],
    target_size: Option<f64>,
    cancel: &dyn Cancel,
) -> Result<Vec<[usize; 4]>> {
    let n0 = points.len();

    // Bounding box and characteristic scale of the boundary.
    let mut lo = Vector3::repeat(f64::INFINITY);
    let mut hi = Vector3::repeat(f64::NEG_INFINITY);
    for p in points.iter() {
        lo = lo.zip_map(&p.coords, f64::min);
        hi = hi.zip_map(&p.coords, f64::max);
    }
    let diag = (hi - lo).norm();
    if diag <= 0.0 {
        return Err(PyrucastError::Message(
            "volume: envelope is degenerate (zero extent)".into(),
        ));
    }

    // Uniform target size: given, else the mean boundary edge length.
    let mut perim = 0.0;
    let mut nedges = 0usize;
    for f in faces {
        let a = points[f[0]];
        let b = points[f[1]];
        let c = points[f[2]];
        perim += (b - a).norm() + (c - b).norm() + (a - c).norm();
        nedges += 3;
    }
    let h = target_size.unwrap_or(perim / nedges as f64);
    if h <= 0.0 || h.is_nan() {
        return Err(PyrucastError::Message(
            "volume: could not determine a positive element size".into(),
        ));
    }
    let eps = diag * EPS_FACTOR;

    // 1. Interior points: a grid at spacing `h`, kept when strictly inside the
    //    envelope and not too close to a boundary node (avoids slivers). They
    //    are held aside — not added to `points` yet — and inserted only *after*
    //    the boundary is recovered and constrained, so an interior point can
    //    never carve away a skin face (its Bowyer–Watson cavity is clipped at
    //    constrained faces in step 4).
    let nx = ((hi.x - lo.x) / h).floor() as i64;
    let ny = ((hi.y - lo.y) / h).floor() as i64;
    let nz = ((hi.z - lo.z) / h).floor() as i64;
    let min_sep = 0.5 * h;
    let mut interior: Vec<Point3> = Vec::new();
    for ix in 1..=nx.max(0) {
        cancel.check()?;
        for iy in 1..=ny.max(0) {
            for iz in 1..=nz.max(0) {
                let p = Point3::new(
                    lo.x + ix as f64 * h,
                    lo.y + iy as f64 * h,
                    lo.z + iz as f64 * h,
                );
                if !point_inside_envelope(p, faces, points, eps) {
                    continue;
                }
                // Reject if it nearly coincides with a boundary node.
                if points[..n0].iter().any(|q| (p - q).norm() < min_sep) {
                    continue;
                }
                interior.push(p);
            }
        }
    }

    // 2. Delaunay tetrahedralization of the boundary nodes alone
    //    (Bowyer–Watson). A tiny deterministic jitter on the connectivity
    //    coordinates removes cospherical degeneracies; the output indexes the
    //    un-jittered points.
    let jitter_amp = diag * 1e-7;
    let jpts: Vec<Point3> = points[..n0]
        .iter()
        .enumerate()
        .map(|(i, p)| *p + jitter(i) * jitter_amp)
        .collect();
    let center = Point3::from((lo + hi) * 0.5);
    let all_tets = bowyer_watson(&jpts, center, diag, cancel)?;

    // 3. Constrained boundary recovery + flood-fill excavation. The
    //    unconstrained boundary Delaunay is loaded into a face-adjacency
    //    structure; every skin face missing from it is recovered; each skin
    //    face is marked constrained; then a flood-fill from an interior
    //    tetrahedron keeps exactly what the skin encloses, never crossing the
    //    surface. Concavities, holes and the far side of a thin part are
    //    excavated exactly instead of being trimmed by a per-tet centroid test
    //    (which leaks across concavities — the "stray triangle" artefact).
    // Tetrahedra spanning four coplanar boundary points (a face quadruple on a
    // cospherical input, say) come back degenerate in the exact coordinates;
    // they carry no volume, so dropping them keeps the output strictly
    // positively oriented without changing it.
    let vol6_eps = diag * diag * diag * 1e-9;
    let mut cdt = Cdt3::from_tets(all_tets, jpts);
    cdt.recover_boundary(faces, cancel)?;
    cdt.mark_and_verify(faces)?;

    // 4. Insert the interior points into the constrained triangulation, one at
    //    a time, each Bowyer–Watson cavity clipped at constrained faces so no
    //    skin face is ever removed. `points` is extended in step order.
    cdt.insert_interior_points(&interior, points, jitter_amp, cancel)?;

    // 5. Excavate: flood-fill from an interior tetrahedron, never crossing the
    //    (now constrained) skin, and emit the enclosed tetrahedra.
    let seed = cdt.interior_seed(faces, points, eps, vol6_eps)?;
    cdt.flood_fill(seed, cancel)?;
    Ok(cdt.collect_inside(points, vol6_eps))
}

/// A tetrahedron in the boundary-recovery structure [`Cdt3`]: four vertices
/// (indices into the point list), the four face-neighbours (`nbr[i]` shares the
/// face **opposite** `v[i]`, `-1` on the hull), a constrained flag per face (a
/// skin face the flood-fill must never cross), and a liveness flag.
#[derive(Clone, Copy)]
struct Tet {
    v: [u32; 4],
    nbr: [i32; 4],
    cons: [bool; 4],
    dead: bool,
}

/// The three vertices of tetrahedron face `i` (opposite `v[i]`), as a plain
/// triple; adjacency keys sort it, so the order here is irrelevant.
#[inline]
fn tet_face(v: [u32; 4], i: usize) -> [u32; 3] {
    match i {
        0 => [v[1], v[2], v[3]],
        1 => [v[0], v[2], v[3]],
        2 => [v[0], v[1], v[3]],
        _ => [v[0], v[1], v[2]],
    }
}

#[inline]
fn sorted3(mut f: [u32; 3]) -> [u32; 3] {
    f.sort_unstable();
    f
}

/// Boundary-conforming tetrahedralization — the 3-D companion of the 2-D
/// [`triangulate_surface`](crate::ops::mesher::triangulate_surface) excavation
/// engine. The unconstrained boundary Delaunay is loaded ([`Cdt3::from_tets`]),
/// every skin face is recovered and marked constrained
/// ([`Cdt3::recover_boundary`], [`Cdt3::mark_and_verify`]), then a flood-fill
/// from an interior seed ([`Cdt3::flood_fill`]) keeps exactly the tetrahedra
/// the skin encloses. Geometry decisions run on the same jittered coordinates
/// `jpts` the Delaunay used, so they stay consistent with the input connectivity.
struct Cdt3 {
    tets: Vec<Tet>,
    inside: Vec<bool>,
    jpts: Vec<Point3>,
    /// Sorted vertex triple → the `(tet, local face)` slots carrying that face:
    /// one entry for a hull face, two for an interior face. Rebuilt whenever
    /// recovery changes the connectivity.
    faces: HashMap<[u32; 3], Vec<(usize, usize)>>,
}

impl Cdt3 {
    fn from_tets(tets: Vec<[usize; 4]>, jpts: Vec<Point3>) -> Self {
        let tets: Vec<Tet> = tets
            .into_iter()
            .map(|t| Tet {
                v: [t[0] as u32, t[1] as u32, t[2] as u32, t[3] as u32],
                nbr: [-1; 4],
                cons: [false; 4],
                dead: false,
            })
            .collect();
        let inside = vec![false; tets.len()];
        let mut cdt = Cdt3 {
            tets,
            inside,
            jpts,
            faces: HashMap::new(),
        };
        cdt.rebuild_adjacency();
        cdt
    }

    /// Rebuild `nbr` pointers and the `faces` index from the live tets: an
    /// interior face is shared by exactly two tets (their opposite local faces
    /// point at each other), a hull face by one (`nbr = -1`).
    fn rebuild_adjacency(&mut self) {
        let mut faces: HashMap<[u32; 3], Vec<(usize, usize)>> = HashMap::new();
        for (ti, t) in self.tets.iter().enumerate() {
            if t.dead {
                continue;
            }
            for i in 0..4 {
                faces
                    .entry(sorted3(tet_face(t.v, i)))
                    .or_default()
                    .push((ti, i));
            }
        }
        for t in self.tets.iter_mut() {
            if !t.dead {
                t.nbr = [-1; 4];
            }
        }
        for occ in faces.values() {
            if occ.len() == 2 {
                let (t0, i0) = occ[0];
                let (t1, i1) = occ[1];
                self.tets[t0].nbr[i0] = t1 as i32;
                self.tets[t1].nbr[i1] = t0 as i32;
            }
        }
        self.faces = faces;
    }

    /// Mark every skin face constrained on its incident tets; error if any skin
    /// face is still absent (it would let the flood-fill leak across the
    /// surface — a shape no boundary-preserving fill can tetrahedralize without
    /// an added node).
    fn mark_and_verify(&mut self, skin: &[[usize; 3]]) -> Result<()> {
        for f in skin {
            let key = sorted3([f[0] as u32, f[1] as u32, f[2] as u32]);
            match self.faces.get(&key) {
                Some(occ) if !occ.is_empty() => {
                    for &(ti, li) in occ {
                        self.tets[ti].cons[li] = true;
                    }
                }
                _ => {
                    return Err(PyrucastError::Message(format!(
                        "volume: cannot conform the surface without adding a boundary node — \
                         face ({}, {}, {}) is unrecoverable (Schönhardt-type polyhedron, with no \
                         tetrahedralization without a Steiner point).",
                        f[0], f[1], f[2]
                    )));
                }
            }
        }
        Ok(())
    }

    /// A live, non-degenerate tetrahedron whose centroid lies strictly inside
    /// the envelope — the flood-fill seed. Degenerate slivers are skipped: a
    /// rectangular skin face whose Delaunay diagonal differs from the skin's
    /// leaves a flat, four-coplanar-corner tet straddling the surface; it has no
    /// interior and, being walled by the constrained skin faces, must not seed.
    fn interior_seed(
        &self,
        skin: &[[usize; 3]],
        points: &[Point3],
        eps: f64,
        vol6_eps: f64,
    ) -> Result<usize> {
        for (ti, t) in self.tets.iter().enumerate() {
            if t.dead {
                continue;
            }
            if tet_vol6(
                points[t.v[0] as usize],
                points[t.v[1] as usize],
                points[t.v[2] as usize],
                points[t.v[3] as usize],
            )
            .abs()
                < vol6_eps
            {
                continue;
            }
            let c = Point3::from(
                (points[t.v[0] as usize].coords
                    + points[t.v[1] as usize].coords
                    + points[t.v[2] as usize].coords
                    + points[t.v[3] as usize].coords)
                    / 4.0,
            );
            if point_inside_envelope(c, skin, points, eps) {
                return Ok(ti);
            }
        }
        Err(PyrucastError::Message(
            "volume: found no interior tetrahedron to seed the fill (degenerate envelope?)".into(),
        ))
    }

    /// Flood the interior from `seed`, propagating across every non-constrained
    /// face and stopping at constrained (skin) faces and the hull.
    fn flood_fill(&mut self, seed: usize, cancel: &dyn Cancel) -> Result<()> {
        self.inside = vec![false; self.tets.len()];
        let mut stack = vec![seed];
        self.inside[seed] = true;
        while let Some(ti) = stack.pop() {
            cancel.check()?;
            let t = self.tets[ti];
            for i in 0..4 {
                if t.cons[i] {
                    continue;
                }
                let nb = t.nbr[i];
                if nb < 0 {
                    continue;
                }
                let nb = nb as usize;
                if !self.inside[nb] {
                    self.inside[nb] = true;
                    stack.push(nb);
                }
            }
        }
        Ok(())
    }

    /// Emit the kept tetrahedra as index quadruples, dropping degenerate slivers
    /// and reorienting each to positive signed volume.
    fn collect_inside(&self, points: &[Point3], vol6_eps: f64) -> Vec<[usize; 4]> {
        let mut out = Vec::new();
        for (ti, t) in self.tets.iter().enumerate() {
            if t.dead || !self.inside[ti] {
                continue;
            }
            let mut q = [
                t.v[0] as usize,
                t.v[1] as usize,
                t.v[2] as usize,
                t.v[3] as usize,
            ];
            let v6 = tet_vol6(points[q[0]], points[q[1]], points[q[2]], points[q[3]]);
            if v6.abs() < vol6_eps {
                continue; // degenerate sliver
            }
            if v6 < 0.0 {
                q.swap(1, 2);
            }
            out.push(q);
        }
        out
    }

    /// Insert each interior point into the constrained triangulation by a
    /// constrained Bowyer–Watson step: the cavity (tets whose circumsphere
    /// contains the point) is grown from the containing tet **without ever
    /// crossing a constrained face**, so a skin face can never be carved away.
    /// The point, its real and jittered coordinates appended to `points` /
    /// `self.jpts`, is then fanned to every cavity-boundary face. Adjacency is
    /// rebuilt after each insertion.
    fn insert_interior_points(
        &mut self,
        interior: &[Point3],
        points: &mut Vec<Point3>,
        jitter_amp: f64,
        cancel: &dyn Cancel,
    ) -> Result<()> {
        for &pr in interior {
            cancel.check()?;
            let pidx = points.len() as u32;
            let pj = pr + jitter(pidx as usize) * jitter_amp;
            points.push(pr);
            self.jpts.push(pj);

            let start = match self.find_containing(pj) {
                Some(t) => t,
                None => continue, // point not located (degenerate) — skip it
            };

            // Grow the cavity across non-constrained faces only.
            let mut cavity: Vec<usize> = Vec::new();
            let mut seen: HashSet<usize> = HashSet::new();
            let mut stack = vec![start];
            seen.insert(start);
            while let Some(ti) = stack.pop() {
                let t = self.tets[ti];
                let in_sphere = match circumsphere(
                    self.jpts[t.v[0] as usize],
                    self.jpts[t.v[1] as usize],
                    self.jpts[t.v[2] as usize],
                    self.jpts[t.v[3] as usize],
                ) {
                    Some((c, r2)) => (pj - c).norm_squared() <= r2 * (1.0 + 1e-12),
                    None => true, // degenerate tet: absorb into the cavity
                };
                if ti != start && !in_sphere {
                    continue;
                }
                cavity.push(ti);
                for i in 0..4 {
                    if t.cons[i] {
                        continue;
                    }
                    let nb = t.nbr[i];
                    if nb < 0 {
                        continue;
                    }
                    let nb = nb as usize;
                    if seen.insert(nb) {
                        stack.push(nb);
                    }
                }
            }

            // Cavity-boundary faces: a face is on the boundary when its
            // neighbour is outside the cavity, is the hull, or is constrained.
            let cavset: HashSet<usize> = cavity.iter().copied().collect();
            let mut bfaces: Vec<([u32; 3], bool)> = Vec::new();
            for &ti in &cavity {
                let t = self.tets[ti];
                for i in 0..4 {
                    let neighbour_in =
                        t.nbr[i] >= 0 && cavset.contains(&(t.nbr[i] as usize)) && !t.cons[i];
                    if !neighbour_in {
                        bfaces.push((tet_face(t.v, i), t.cons[i]));
                    }
                }
            }

            for &ti in &cavity {
                self.tets[ti].dead = true;
            }
            for (f, cons) in bfaces {
                let mut nv = [f[0], f[1], f[2], pidx];
                if tet_vol6(
                    self.jpts[nv[0] as usize],
                    self.jpts[nv[1] as usize],
                    self.jpts[nv[2] as usize],
                    self.jpts[nv[3] as usize],
                ) < 0.0
                {
                    nv.swap(1, 2);
                }
                self.tets.push(Tet {
                    v: nv,
                    nbr: [-1; 4],
                    // The face opposite the new point (local index 3) is the
                    // original cavity-boundary face; it keeps its constraint.
                    cons: [false, false, false, cons],
                    dead: false,
                });
            }
            self.rebuild_adjacency();
        }
        Ok(())
    }

    /// The live tetrahedron containing `p` (jittered space), or `None`. Linear
    /// scan — the interior grids that reach here are small.
    fn find_containing(&self, p: Point3) -> Option<usize> {
        for (ti, t) in self.tets.iter().enumerate() {
            if t.dead {
                continue;
            }
            let (a, b, c, d) = (
                self.jpts[t.v[0] as usize],
                self.jpts[t.v[1] as usize],
                self.jpts[t.v[2] as usize],
                self.jpts[t.v[3] as usize],
            );
            // `from_tets`/fanning keep every tet positively oriented in jittered
            // space, so `p` is inside iff replacing any one vertex with `p`
            // keeps a non-negative signed volume.
            if tet_vol6(p, b, c, d) >= 0.0
                && tet_vol6(a, p, c, d) >= 0.0
                && tet_vol6(a, b, p, d) >= 0.0
                && tet_vol6(a, b, c, p) >= 0.0
            {
                return Some(ti);
            }
        }
        None
    }

    /// Recover every skin edge missing from the boundary Delaunay (Étage 1).
    /// A skin face absent only because the flat quad it lies on took the other
    /// Delaunay diagonal is fixed by recovering that diagonal edge; once all
    /// skin edges are present, the flanking skin faces reappear. Any face still
    /// absent afterwards is caught by [`Cdt3::mark_and_verify`] (Schönhardt-type
    /// input needing a Steiner point), so the pipeline never emits a leaking
    /// fill. Predicates run in jittered space, where a boundary diagonal is a
    /// generic (non-coplanar) missing edge rather than a degenerate one.
    fn recover_boundary(&mut self, skin: &[[usize; 3]], cancel: &dyn Cancel) -> Result<()> {
        let mut skin_edges: HashSet<(u32, u32)> = HashSet::new();
        for f in skin {
            for &(u, v) in &[(f[0], f[1]), (f[1], f[2]), (f[2], f[0])] {
                let (u, v) = (u as u32, v as u32);
                skin_edges.insert((u.min(v), u.max(v)));
            }
        }
        let skin_edges: Vec<(u32, u32)> = skin_edges.into_iter().collect();
        // Fixpoint: recovering one edge can disturb another, so re-scan until no
        // skin edge is missing or a whole pass makes no progress.
        let cap = skin_edges.len() * 4 + 16;
        for _ in 0..cap {
            cancel.check()?;
            let missing: Vec<(u32, u32)> = skin_edges
                .iter()
                .copied()
                .filter(|&(a, b)| !self.edge_present(a, b))
                .collect();
            if missing.is_empty() {
                return Ok(());
            }
            let mut progress = false;
            for (a, b) in &missing {
                if self.edge_present(*a, *b) {
                    continue; // recovered as a side effect this pass
                }
                if self.recover_edge_by_cavity(*a, *b) {
                    self.rebuild_adjacency();
                    progress = true;
                }
            }
            if !progress {
                let (a, b) = missing[0];
                return Err(PyrucastError::Message(format!(
                    "volume: cannot conform the surface without adding a boundary node — \
                     edge ({}, {}) is unrecoverable (Schönhardt-type polyhedron, with no \
                     tetrahedralization without a Steiner point).",
                    a, b
                )));
            }
        }
        Ok(())
    }

    /// Is edge `(a, b)` an edge of some live tetrahedron?
    fn edge_present(&self, a: u32, b: u32) -> bool {
        self.tets
            .iter()
            .any(|t| !t.dead && t.v.contains(&a) && t.v.contains(&b))
    }

    /// Does the open segment `(a, b)` cross the interior of triangle `(x, y, z)`
    /// (all indices into `jpts`)? Strict everywhere, so a triangle touching `a`
    /// or `b` is not a crossing.
    fn seg_crosses_tri(&self, a: u32, b: u32, x: u32, y: u32, z: u32) -> bool {
        let (pa, pb) = (self.jpts[a as usize], self.jpts[b as usize]);
        let (px, py, pz) = (
            self.jpts[x as usize],
            self.jpts[y as usize],
            self.jpts[z as usize],
        );
        // `a` and `b` must straddle the plane of the triangle.
        let sa = tet_vol6(px, py, pz, pa);
        let sb = tet_vol6(px, py, pz, pb);
        if sa == 0.0 || sb == 0.0 || (sa > 0.0) == (sb > 0.0) {
            return false;
        }
        // The line `ab` must pass to the same side of all three triangle edges.
        let e1 = tet_vol6(pa, pb, px, py);
        let e2 = tet_vol6(pa, pb, py, pz);
        let e3 = tet_vol6(pa, pb, pz, px);
        (e1 > 0.0 && e2 > 0.0 && e3 > 0.0) || (e1 < 0.0 && e2 < 0.0 && e3 < 0.0)
    }

    /// Recover missing edge `(a, b)` by re-tetrahedralizing the corridor it
    /// crosses. The corridor (tets the open segment passes through) is small; it
    /// is removed and refilled as the *star of one endpoint* — every boundary
    /// face not incident to that endpoint is joined to it. This forces edge
    /// `(a, b)`, because the corridor's boundary faces incident to the *other*
    /// endpoint become tets that span from one endpoint to the other. Valid iff
    /// the corridor is star-shaped from an endpoint (checked by signed volume);
    /// returns false otherwise, leaving the mesh untouched for the caller to
    /// report as a genuine Schönhardt obstruction. This is the tool a reflex
    /// edge needs — flips alone stall there.
    /// March the connected tube of tets the open segment `(a, b)` passes
    /// through: start in the tet at `a` whose opposite face the segment exits,
    /// then hop across each exit face until reaching the tet at `b`. Returns the
    /// ordered corridor, or `None` if the walk hits the hull, a constrained tet,
    /// or a face it cannot cross cleanly (a grazing/degenerate configuration —
    /// left for the caller to report). All predicates run on `jpts`.
    fn walk_corridor(&self, a: u32, b: u32) -> Option<Vec<usize>> {
        // Start: a tet incident to `a` whose face opposite `a` the segment exits.
        let mut start: Option<(usize, usize)> = None;
        for (ti, t) in self.tets.iter().enumerate() {
            if t.dead || t.cons.iter().any(|&c| c) {
                continue;
            }
            if let Some(la) = t.v.iter().position(|&v| v == a) {
                let f = tet_face(t.v, la);
                if self.seg_crosses_tri(a, b, f[0], f[1], f[2]) {
                    start = Some((ti, la));
                    break;
                }
            }
        }
        let (start_ti, la) = start?;
        let mut corridor = vec![start_ti];
        let mut current = start_ti;
        let mut exit_local = la;
        let cap = self.tets.len() + 4;
        for _ in 0..cap {
            let nb = self.tets[current].nbr[exit_local];
            if nb < 0 {
                return None;
            }
            let nb = nb as usize;
            if self.tets[nb].dead || self.tets[nb].cons.iter().any(|&c| c) {
                return None;
            }
            corridor.push(nb);
            if self.tets[nb].v.contains(&b) {
                return Some(corridor);
            }
            let entry_key = sorted3(tet_face(self.tets[current].v, exit_local));
            let mut next_exit = None;
            for li in 0..4 {
                if sorted3(tet_face(self.tets[nb].v, li)) == entry_key {
                    continue; // the face we just entered through
                }
                let f = tet_face(self.tets[nb].v, li);
                if self.seg_crosses_tri(a, b, f[0], f[1], f[2]) {
                    next_exit = Some(li);
                    break;
                }
            }
            exit_local = next_exit?;
            current = nb;
        }
        None
    }

    fn recover_edge_by_cavity(&mut self, a: u32, b: u32) -> bool {
        // Corridor: the connected tube of tets the segment marches through, from
        // the tet at `a` to the tet at `b`. Marching (rather than "any tet with a
        // crossed face") guarantees the removed set is simply connected, so its
        // boundary is a closed surface the gift-wrap can refill.
        let corridor = match self.walk_corridor(a, b) {
            Some(c) => c,
            None => return false,
        };
        if corridor.is_empty() {
            return false;
        }
        let corset: HashSet<usize> = corridor.iter().copied().collect();

        // Outward boundary faces of the corridor, each oriented so the apex it
        // was cut from (inside the corridor) has positive signed volume.
        let mut bfaces: Vec<[u32; 3]> = Vec::new();
        for &ti in &corridor {
            let t = self.tets[ti];
            for li in 0..4 {
                let neighbour_in = t.nbr[li] >= 0 && corset.contains(&(t.nbr[li] as usize));
                if neighbour_in {
                    continue;
                }
                let f = tet_face(t.v, li);
                let apex = t.v[li];
                let mut g = [f[0], f[1], f[2]];
                if tet_vol6(
                    self.jpts[g[0] as usize],
                    self.jpts[g[1] as usize],
                    self.jpts[g[2] as usize],
                    self.jpts[apex as usize],
                ) < 0.0
                {
                    g.swap(0, 1);
                }
                bfaces.push(g);
            }
        }

        // Every vertex of the removed tets is an allowed apex (the boundary-face
        // set alone can miss a vertex the correct fill needs).
        let mut verts: Vec<u32> = Vec::new();
        for &ti in &corridor {
            for &v in &self.tets[ti].v {
                if !verts.contains(&v) {
                    verts.push(v);
                }
            }
        }
        let res = self.cavity_fill(a, b, &bfaces, &verts);
        match res {
            Some(fill) => {
                for &ti in &corridor {
                    self.tets[ti].dead = true;
                }
                self.tets.extend(fill);
                true
            }
            None => false,
        }
    }

    /// Tetrahedralize the corridor bounded by outward faces `bfaces` so that
    /// edge `(a, b)` is present, by gift-wrapping from the boundary inward. Each
    /// open face is closed by an apex chosen by the empty-circumsphere (Delaunay)
    /// rule, **except** that when the open face touches one endpoint of `(a, b)`
    /// and the other endpoint is a valid apex, that endpoint is chosen — which
    /// forces the edge to appear. Returns the tets, or `None` if the front
    /// stalls or the edge never materialises (left for the caller to report as a
    /// genuine Schönhardt obstruction). Runs on jittered coordinates.
    fn cavity_fill(&self, a: u32, b: u32, bfaces: &[[u32; 3]], verts: &[u32]) -> Option<Vec<Tet>> {
        // Open front: sorted key → the directed face whose cavity side (still to
        // be filled) is the positive side. Boundary faces start cavity-positive.
        let mut front: HashMap<[u32; 3], [u32; 3]> = HashMap::new();
        for g in bfaces {
            front.insert(sorted3(*g), *g);
        }

        let mut fill: Vec<Tet> = Vec::new();
        let cap = (bfaces.len() + verts.len()) * 50 + 200;
        let mut guard = 0usize;
        while let Some((&key, &dir)) = front.iter().next() {
            guard += 1;
            if guard > cap {
                return None;
            }
            front.remove(&key);
            let [p, q, r] = dir;
            let c = self.pick_apex([p, q, r], verts, a, b, &front)?;
            fill.push(Tet {
                v: [p, q, r, c],
                nbr: [-1; 4],
                cons: [false; 4],
                dead: false,
            });
            // Toggle the three new faces, each oriented so the *remaining* cavity
            // is on its positive side (the third base vertex on the negative side).
            for (x, y) in [(p, q), (q, r), (r, p)] {
                let opp = [p, q, r].into_iter().find(|&w| w != x && w != y).unwrap();
                let mut df = [x, y, c];
                if tet_vol6(
                    self.jpts[df[0] as usize],
                    self.jpts[df[1] as usize],
                    self.jpts[df[2] as usize],
                    self.jpts[opp as usize],
                ) > 0.0
                {
                    df.swap(0, 1); // put `opp` on the negative side
                }
                let k = sorted3(df);
                if front.remove(&k).is_none() {
                    front.insert(k, df);
                }
            }
        }
        // The fill must actually contain edge (a, b).
        if !fill.iter().any(|t| t.v.contains(&a) && t.v.contains(&b)) {
            return None;
        }
        Some(fill)
    }

    /// The apex closing open face `(p, q, r)` on its positive (cavity) side.
    /// Since the corridor is star-shaped from the segment `(a, b)`, prefer the
    /// endpoints as apex — building an `a`-star, a `b`-star and the `a`–`b`
    /// bridge tets that force the edge — then fall back to the Delaunay vertex.
    /// A candidate is only taken if its tet is a valid ear (encloses no other
    /// cavity vertex). `None` if the face cannot be closed.
    fn pick_apex(
        &self,
        face: [u32; 3],
        verts: &[u32],
        a: u32,
        b: u32,
        front: &HashMap<[u32; 3], [u32; 3]>,
    ) -> Option<u32> {
        let [p, q, r] = face;
        // A candidate must be on the face's positive (cavity) side AND visible
        // from it inside the cavity: the segment from the face centroid to the
        // apex must not cross any other open face. Visibility is what keeps the
        // advancing front inside a non-convex cavity (unconstrained Delaunay
        // would pick apexes whose tets poke through a reflex wall).
        let m = Point3::from(
            (self.jpts[p as usize].coords
                + self.jpts[q as usize].coords
                + self.jpts[r as usize].coords)
                / 3.0,
        );
        let this_key = sorted3(face);
        let visible = |c: u32| {
            let jc = self.jpts[c as usize];
            front.iter().all(|(k, g)| {
                *k == this_key
                    || !seg_crosses_tri_pts(
                        m,
                        jc,
                        self.jpts[g[0] as usize],
                        self.jpts[g[1] as usize],
                        self.jpts[g[2] as usize],
                    )
            })
        };
        let cand: Vec<u32> = verts
            .iter()
            .copied()
            .filter(|&c| {
                !face.contains(&c)
                    && tet_vol6(
                        self.jpts[p as usize],
                        self.jpts[q as usize],
                        self.jpts[r as usize],
                        self.jpts[c as usize],
                    ) > 0.0
                    && visible(c)
            })
            .collect();
        if cand.is_empty() {
            return None;
        }
        // A candidate is a valid ear if no other candidate lies strictly inside
        // the tet (p, q, r, c) — that would make the ear enclose a vertex.
        let ear_ok = |c: u32| {
            cand.iter().all(|&d| {
                d == c || {
                    let (jp, jq, jr, jc, jd) = (
                        self.jpts[p as usize],
                        self.jpts[q as usize],
                        self.jpts[r as usize],
                        self.jpts[c as usize],
                        self.jpts[d as usize],
                    );
                    // Tet (p,q,r,c) is positively oriented; `d` is inside iff
                    // every replace-a-vertex volume stays positive.
                    !(tet_vol6(jd, jq, jr, jc) > 0.0
                        && tet_vol6(jp, jd, jr, jc) > 0.0
                        && tet_vol6(jp, jq, jd, jc) > 0.0
                        && tet_vol6(jp, jq, jr, jd) > 0.0)
                }
            })
        };
        // Prefer the endpoints (forces edge (a, b)); then the empty-circumsphere
        // (Delaunay) vertex; then any valid ear.
        for pref in [a, b] {
            if cand.contains(&pref) && ear_ok(pref) {
                return Some(pref);
            }
        }
        for &c in &cand {
            let empty = cand.iter().all(|&d| {
                d == c
                    || in_sphere3(
                        self.jpts[p as usize],
                        self.jpts[q as usize],
                        self.jpts[r as usize],
                        self.jpts[c as usize],
                        self.jpts[d as usize],
                    ) <= 0.0
            });
            if empty && ear_ok(c) {
                return Some(c);
            }
        }
        cand.iter().copied().find(|&c| ear_ok(c))
    }
}

/// Does the open segment `(pa, pb)` cross the interior of triangle `(x, y, z)`
/// — the point-coordinate form of [`Cdt3::seg_crosses_tri`], for a visibility
/// ray whose endpoint is a computed point rather than a mesh vertex.
fn seg_crosses_tri_pts(pa: Point3, pb: Point3, x: Point3, y: Point3, z: Point3) -> bool {
    let sa = tet_vol6(x, y, z, pa);
    let sb = tet_vol6(x, y, z, pb);
    if sa == 0.0 || sb == 0.0 || (sa > 0.0) == (sb > 0.0) {
        return false;
    }
    let e1 = tet_vol6(pa, pb, x, y);
    let e2 = tet_vol6(pa, pb, y, z);
    let e3 = tet_vol6(pa, pb, z, x);
    (e1 > 0.0 && e2 > 0.0 && e3 > 0.0) || (e1 < 0.0 && e2 < 0.0 && e3 < 0.0)
}

/// In-sphere test: for a positively oriented tetrahedron `(a, b, c, d)`, returns
/// a positive value iff `e` lies strictly inside its circumsphere. The sign of
/// the standard 4×4 lifted determinant.
fn in_sphere3(a: Point3, b: Point3, c: Point3, d: Point3, e: Point3) -> f64 {
    use nalgebra::Matrix4;
    let row = |p: Point3| {
        let r = p - e;
        [r.x, r.y, r.z, r.norm_squared()]
    };
    let (ra, rb, rc, rd) = (row(a), row(b), row(c), row(d));
    let m = Matrix4::new(
        ra[0], ra[1], ra[2], ra[3], rb[0], rb[1], rb[2], rb[3], rc[0], rc[1], rc[2], rc[3], rd[0],
        rd[1], rd[2], rd[3],
    );
    m.determinant()
}

/// Small deterministic per-index unit-ish jitter vector (components in
/// `[-0.5, 0.5]`), used to break cospherical/coplanar degeneracies in the
/// Delaunay connectivity without disturbing the output coordinates.
fn jitter(i: usize) -> Vector3 {
    let h = |k: usize| -> f64 {
        let x = (i as u64).wrapping_mul(0x9E3779B97F4A7C15) ^ (k as u64).wrapping_mul(0x632BE5AB);
        ((x >> 11) as f64 / (1u64 << 53) as f64) - 0.5
    };
    Vector3::new(h(1), h(2), h(3))
}

/// Incremental Bowyer–Watson Delaunay tetrahedralization of `pts`. A large
/// super-tetrahedron (built around `center` with extent set by `diag`)
/// bootstraps the construction and is removed at the end, leaving only
/// tetrahedra whose four vertices are input points (indices `< pts.len()`).
fn bowyer_watson(
    pts: &[Point3],
    center: Point3,
    diag: f64,
    cancel: &dyn Cancel,
) -> Result<Vec<[usize; 4]>> {
    let n = pts.len();
    // Super-tetrahedron vertices (a regular tetra of circumradius `r`), far
    // enough out that every input point sits well inside.
    let r = diag * 1000.0 + 1.0;
    let dirs = [
        Vector3::new(0.0, 0.0, 1.0),
        Vector3::new(2.0 * 2.0_f64.sqrt() / 3.0, 0.0, -1.0 / 3.0),
        Vector3::new(-2.0_f64.sqrt() / 3.0, (2.0_f64 / 3.0).sqrt(), -1.0 / 3.0),
        Vector3::new(-2.0_f64.sqrt() / 3.0, -(2.0_f64 / 3.0).sqrt(), -1.0 / 3.0),
    ];
    let mut work: Vec<Point3> = pts.to_vec();
    for d in &dirs {
        work.push(center + d * r);
    }

    let mut tets: Vec<[usize; 4]> = vec![[n, n + 1, n + 2, n + 3]];

    for p in 0..n {
        cancel.check()?;
        let pp = work[p];

        // Tetrahedra whose circumsphere contains `pp` form the cavity.
        let mut bad: Vec<usize> = Vec::new();
        for (ti, t) in tets.iter().enumerate() {
            match circumsphere(work[t[0]], work[t[1]], work[t[2]], work[t[3]]) {
                Some((c, r2)) => {
                    if (pp - c).norm_squared() <= r2 * (1.0 + 1e-12) {
                        bad.push(ti);
                    }
                }
                None => bad.push(ti), // degenerate tet → drop it into the cavity
            }
        }

        // Cavity boundary: faces used by exactly one bad tetrahedron.
        let mut face_count: HashMap<[usize; 3], (u32, [usize; 3])> = HashMap::new();
        for &ti in &bad {
            for f in tet_faces(tets[ti]) {
                let mut key = f;
                key.sort_unstable();
                let e = face_count.entry(key).or_insert((0, f));
                e.0 += 1;
                e.1 = f;
            }
        }

        // Remove the cavity tetrahedra, then fan the boundary faces to `pp`.
        let badset: std::collections::HashSet<usize> = bad.into_iter().collect();
        let mut next: Vec<[usize; 4]> = tets
            .iter()
            .enumerate()
            .filter(|(i, _)| !badset.contains(i))
            .map(|(_, t)| *t)
            .collect();
        for (_, (count, f)) in face_count {
            if count != 1 {
                continue;
            }
            let mut nt = [f[0], f[1], f[2], p];
            if tet_vol6(work[nt[0]], work[nt[1]], work[nt[2]], work[nt[3]]) < 0.0 {
                nt.swap(1, 2);
            }
            next.push(nt);
        }
        tets = next;
    }

    // Drop every tetrahedron still touching a super-vertex.
    tets.retain(|t| t.iter().all(|&v| v < n));
    Ok(tets)
}

/// The four triangular faces of a tetrahedron (each omitting one vertex).
#[inline]
fn tet_faces(t: [usize; 4]) -> [[usize; 3]; 4] {
    [
        [t[1], t[2], t[3]],
        [t[0], t[2], t[3]],
        [t[0], t[1], t[3]],
        [t[0], t[1], t[2]],
    ]
}

/// Circumsphere of tetrahedron `(a, b, c, d)`: `(center, radius²)`, or `None`
/// when the four points are (near) coplanar. Solves the linear system for the
/// centre equidistant from all four vertices.
fn circumsphere(a: Point3, b: Point3, c: Point3, d: Point3) -> Option<(Point3, f64)> {
    use nalgebra::Matrix3;
    let ba = b - a;
    let ca = c - a;
    let da = d - a;
    let mat = Matrix3::from_rows(&[ba.transpose(), ca.transpose(), da.transpose()]);
    let rhs = Vector3::new(ba.norm_squared(), ca.norm_squared(), da.norm_squared()) * 0.5;
    let sol = mat.lu().solve(&rhs)?;
    Some((a + sol, sol.norm_squared()))
}

/// True if `p` lies inside the closed, consistently oriented triangular
/// surface `boundary` (indices into `points`), via the signed solid angle
/// (winding number). The total solid angle subtended by a closed surface is
/// `±4π` from an interior point and `0` from an exterior one — a robust test
/// with no ray/edge degeneracies. A point coincident with a surface vertex is
/// treated as inside.
fn point_inside_envelope(p: Point3, boundary: &[[usize; 3]], points: &[Point3], eps: f64) -> bool {
    let mut total = 0.0;
    for f in boundary {
        let a: Vector3 = points[f[0]] - p;
        let b: Vector3 = points[f[1]] - p;
        let c: Vector3 = points[f[2]] - p;
        let la = a.norm();
        let lb = b.norm();
        let lc = c.norm();
        if la < eps || lb < eps || lc < eps {
            return true; // on a boundary vertex
        }
        // Van Oosterom–Strackee: signed solid angle of triangle (a, b, c).
        let num = a.dot(&b.cross(&c));
        let den = la * lb * lc + a.dot(&b) * lc + a.dot(&c) * lb + b.dot(&c) * la;
        total += 2.0 * num.atan2(den);
    }
    total.abs() > 2.0 * std::f64::consts::PI
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::containers::mesh::Coords;
    use crate::store::{insert, Handle};

    /// Build a closed TRI3 surface from explicit points and triangles,
    /// auto-orienting every triangle so its normal points **away** from
    /// `inside` (consistent outward winding for a convex body).
    fn build_surface(
        coords: Handle<Coords>,
        pts: &[(f64, f64, f64)],
        tris: &[[usize; 3]],
        inside: (f64, f64, f64),
    ) -> Mesh {
        let nodes: Vec<NodeId> = pts
            .iter()
            .map(|&(x, y, z)| Node::create_in(coords.clone(), &[x, y, z]).unwrap().id())
            .collect();
        let p: Vec<Point3> = pts.iter().map(|&(x, y, z)| Point3::new(x, y, z)).collect();
        let ctr = Point3::new(inside.0, inside.1, inside.2);
        let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::TRI3));
        for t in tris {
            let a = p[t[0]];
            let b = p[t[1]];
            let c = p[t[2]];
            let nrm = (b - a).cross(&(c - a));
            // Normal should point away from the interior point.
            let t = if nrm.dot(&(ctr - a)) > 0.0 {
                [t[0], t[2], t[1]]
            } else {
                *t
            };
            mesh.add_cell(&[nodes[t[0]], nodes[t[1]], nodes[t[2]]])
                .unwrap();
        }
        mesh
    }

    /// Sum of the (positive) volumes of every TET4 cell, asserting each cell
    /// has strictly positive signed volume (correct TET4 orientation).
    fn total_tet_volume(mesh: &Mesh) -> f64 {
        let types = mesh.element_types().unwrap();
        assert_eq!(types, vec![ElementType::TET4]);
        let n = mesh.cell_count().unwrap();
        let mut total = 0.0;
        for ci in 0..n {
            let p: Vec<Vec<f64>> = (0..4)
                .map(|ni| mesh.node(0, ci, ni).unwrap().coord().unwrap())
                .collect();
            let a = Point3::new(p[0][0], p[0][1], p[0][2]);
            let b = Point3::new(p[1][0], p[1][1], p[1][2]);
            let c = Point3::new(p[2][0], p[2][1], p[2][2]);
            let d = Point3::new(p[3][0], p[3][1], p[3][2]);
            let v6 = tet_vol6(a, b, c, d);
            assert!(
                v6 > 0.0,
                "cell {} has non-positive signed volume {}",
                ci,
                v6
            );
            total += v6 / 6.0;
        }
        total
    }

    /// Volume enclosed by a closed TRI3 envelope, via the divergence theorem
    /// (sum of `p0·(p1×p2)/6` over its triangles). Sign depends on the
    /// envelope orientation, so the magnitude is the geometric volume — the
    /// reference a correct fill must reproduce.
    fn enclosed_volume(mesh: &Mesh) -> f64 {
        let counts = mesh.cell_counts().unwrap();
        let mut v6 = 0.0;
        for (si, &cnt) in counts.iter().enumerate() {
            for ci in 0..cnt {
                let p: Vec<Vec<f64>> = (0..3)
                    .map(|ni| mesh.node(si, ci, ni).unwrap().coord().unwrap())
                    .collect();
                let a = Point3::new(p[0][0], p[0][1], p[0][2]);
                let b = Point3::new(p[1][0], p[1][1], p[1][2]);
                let c = Point3::new(p[2][0], p[2][1], p[2][2]);
                v6 += a.coords.dot(&b.coords.cross(&c.coords));
            }
        }
        (v6 / 6.0).abs()
    }

    fn tetrahedron(coords: Handle<Coords>) -> Mesh {
        let pts = [
            (0.0, 0.0, 0.0),
            (1.0, 0.0, 0.0),
            (0.0, 1.0, 0.0),
            (0.0, 0.0, 1.0),
        ];
        let tris = [[0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3]];
        build_surface(coords, &pts, &tris, (0.25, 0.25, 0.25))
    }

    /// Triangular prism: the triangle `(0,0)-(1,0)-(0,1)` extruded to
    /// `z = height` (volume `0.5·height`). A convex, non-cospherical solid.
    fn prism(coords: Handle<Coords>, height: f64) -> Mesh {
        let pts = [
            (0.0, 0.0, 0.0),
            (1.0, 0.0, 0.0),
            (0.0, 1.0, 0.0),
            (0.0, 0.0, height),
            (1.0, 0.0, height),
            (0.0, 1.0, height),
        ];
        let tris = [
            [0, 1, 2], // bottom cap
            [3, 4, 5], // top cap
            [0, 1, 4],
            [0, 4, 3], // side 0-1
            [1, 2, 5],
            [1, 5, 4], // side 1-2
            [2, 0, 3],
            [2, 3, 5], // side 2-0
        ];
        build_surface(coords, &pts, &tris, (0.25, 0.25, height / 2.0))
    }

    /// Unit cube `[0, s]³` surface, each square face split into two triangles.
    /// The eight corners are cospherical — a Delaunay-degenerate input that
    /// the jittered connectivity must still handle.
    fn cube(coords: Handle<Coords>, s: f64) -> Mesh {
        let pts = [
            (0.0, 0.0, 0.0),
            (s, 0.0, 0.0),
            (s, s, 0.0),
            (0.0, s, 0.0),
            (0.0, 0.0, s),
            (s, 0.0, s),
            (s, s, s),
            (0.0, s, s),
        ];
        let quads = [
            [0, 1, 2, 3],
            [4, 5, 6, 7],
            [0, 1, 5, 4],
            [1, 2, 6, 5],
            [2, 3, 7, 6],
            [3, 0, 4, 7],
        ];
        let mut tris: Vec<[usize; 3]> = Vec::new();
        for q in &quads {
            tris.push([q[0], q[1], q[2]]);
            tris.push([q[0], q[2], q[3]]);
        }
        build_surface(coords, &pts, &tris, (s / 2.0, s / 2.0, s / 2.0))
    }

    /// Regular octahedron with vertices at `±1` on each axis (volume 4/3).
    fn octahedron(coords: Handle<Coords>) -> Mesh {
        let pts = [
            (1.0, 0.0, 0.0),
            (-1.0, 0.0, 0.0),
            (0.0, 1.0, 0.0),
            (0.0, -1.0, 0.0),
            (0.0, 0.0, 1.0),
            (0.0, 0.0, -1.0),
        ];
        let mut tris: Vec<[usize; 3]> = Vec::new();
        for &xi in &[0usize, 1] {
            for &yi in &[2usize, 3] {
                for &zi in &[4usize, 5] {
                    tris.push([xi, yi, zi]);
                }
            }
        }
        build_surface(coords, &pts, &tris, (0.0, 0.0, 0.0))
    }

    /// L-shaped prism (a non-convex, star-shaped solid): the L polygon
    /// `(0,0)-(2,0)-(2,1)-(1,1)-(1,2)-(0,2)` extruded to `z = h` (volume `3·h`).
    fn l_prism(coords: Handle<Coords>, h: f64) -> Mesh {
        let poly = [
            (0.0, 0.0),
            (2.0, 0.0),
            (2.0, 1.0),
            (1.0, 1.0),
            (1.0, 2.0),
            (0.0, 2.0),
        ];
        let mut pts: Vec<(f64, f64, f64)> = Vec::new();
        for &(x, y) in &poly {
            pts.push((x, y, 0.0));
        }
        for &(x, y) in &poly {
            pts.push((x, y, h));
        }
        let mut tris: Vec<[usize; 3]> = Vec::new();
        // Caps (fan from vertex 0 / 6 — valid for this L).
        for k in 1..5 {
            tris.push([0, k, k + 1]);
            tris.push([6, 6 + k, 6 + k + 1]);
        }
        // Sides.
        for k in 0..6 {
            let bi = k;
            let bj = (k + 1) % 6;
            let ti = k + 6;
            let tj = (k + 1) % 6 + 6;
            tris.push([bi, bj, tj]);
            tris.push([bi, tj, ti]);
        }
        build_surface(coords, &pts, &tris, (0.5, 0.5, h / 2.0))
    }

    /// Boundary faces of a TET4 mesh: sorted node-index triples on exactly one
    /// tetrahedron.
    fn boundary_faces(mesh: &Mesh) -> HashSet<[usize; 3]> {
        let n = mesh.cell_count().unwrap();
        let mut count: HashMap<[usize; 3], u32> = HashMap::new();
        for ci in 0..n {
            let ids: Vec<usize> = (0..4)
                .map(|ni| mesh.node(0, ci, ni).unwrap().id().0 as usize)
                .collect();
            for f in [
                [ids[1], ids[2], ids[3]],
                [ids[0], ids[2], ids[3]],
                [ids[0], ids[1], ids[3]],
                [ids[0], ids[1], ids[2]],
            ] {
                let mut key = f;
                key.sort_unstable();
                *count.entry(key).or_insert(0) += 1;
            }
        }
        count
            .into_iter()
            .filter(|&(_, c)| c == 1)
            .map(|(k, _)| k)
            .collect()
    }

    /// Skin faces of the input envelope as sorted node-index triples.
    fn skin_faces(env: &Mesh) -> HashSet<[usize; 3]> {
        let mut out = HashSet::new();
        let counts = env.cell_counts().unwrap();
        for (si, &cnt) in counts.iter().enumerate() {
            for ci in 0..cnt {
                let mut f: [usize; 3] = [
                    env.node(si, ci, 0).unwrap().id().0 as usize,
                    env.node(si, ci, 1).unwrap().id().0 as usize,
                    env.node(si, ci, 2).unwrap().id().0 as usize,
                ];
                f.sort_unstable();
                out.insert(f);
            }
        }
        out
    }

    // Non-convex reflex geometry: the gift-wrap cavity re-tet recovers most
    // reflex edges but still overlaps on a few residual non-convex corridors —
    // full exact-predicate boundary recovery is future work. Kept as a target.
    #[test]
    #[ignore = "needs complete exact-predicate 3-D boundary recovery (reflex corridors)"]
    fn volume_l_prism_conserves_volume_and_boundary() {
        let coords = insert(Coords::new(3).unwrap());
        let env = l_prism(coords.clone(), 1.0);
        let want = enclosed_volume(&env); // 3·1 = 3
        let skin = skin_faces(&env);
        let mesh = volume(&env, Some(10.0)).unwrap();
        assert!(
            (total_tet_volume(&mesh) - want).abs() < 1e-9,
            "L-prism volume drift: got {}, want {}",
            total_tet_volume(&mesh),
            want
        );
        assert_eq!(
            boundary_faces(&mesh),
            skin,
            "L-prism boundary must equal the input skin exactly"
        );
    }

    #[test]
    fn volume_tetrahedron_is_one_tet() {
        let coords = insert(Coords::new(3).unwrap());
        let mesh = volume(&tetrahedron(coords.clone()), Some(10.0)).unwrap();
        assert_eq!(mesh.cell_count().unwrap(), 1);
        assert!((total_tet_volume(&mesh) - 1.0 / 6.0).abs() < 1e-12);
    }

    #[test]
    fn volume_coarse_prism_conserves_volume() {
        let coords = insert(Coords::new(3).unwrap());
        // Size larger than the prism: no interior nodes, fills from corners.
        let env = prism(coords.clone(), 1.0);
        let want = enclosed_volume(&env);
        let mesh = volume(&env, Some(10.0)).unwrap();
        assert!(
            (total_tet_volume(&mesh) - want).abs() < 1e-9,
            "prism volume drift: got {}, want {}",
            total_tet_volume(&mesh),
            want
        );
    }

    #[test]
    fn volume_refined_prism_creates_interior_and_conserves_volume() {
        let coords = insert(Coords::new(3).unwrap());
        let env = prism(coords.clone(), 1.0);
        let want = enclosed_volume(&env);
        let before = read(&coords).unwrap().node_count();
        let mesh = volume(&env, Some(0.3)).unwrap();
        let after = read(&coords).unwrap().node_count();
        assert!(after > before, "expected interior nodes to be created");
        assert!(mesh.cell_count().unwrap() > 3, "expected a refined fill");
        assert!(
            (total_tet_volume(&mesh) - want).abs() < 1e-9,
            "prism volume drift: got {}, want {}",
            total_tet_volume(&mesh),
            want
        );
    }

    #[test]
    fn volume_cube_conserves_volume() {
        // Despite cospherical corners, the jittered Delaunay fills the cube.
        let coords = insert(Coords::new(3).unwrap());
        let env = cube(coords.clone(), 1.0);
        let want = enclosed_volume(&env);
        let mesh = volume(&env, Some(10.0)).unwrap();
        assert!(
            (total_tet_volume(&mesh) - want).abs() < 1e-9,
            "cube volume drift: got {}, want {}",
            total_tet_volume(&mesh),
            want
        );
    }

    #[test]
    fn volume_refined_cube_conserves_volume() {
        let coords = insert(Coords::new(3).unwrap());
        let env = cube(coords.clone(), 1.0);
        let want = enclosed_volume(&env);
        let mesh = volume(&env, Some(0.34)).unwrap();
        assert!(mesh.cell_count().unwrap() > 6, "expected a refined fill");
        assert!(
            (total_tet_volume(&mesh) - want).abs() < 1e-9,
            "cube volume drift: got {}, want {}",
            total_tet_volume(&mesh),
            want
        );
    }

    #[test]
    fn volume_octahedron_conserves_volume() {
        let coords = insert(Coords::new(3).unwrap());
        let env = octahedron(coords.clone());
        let want = enclosed_volume(&env); // 4/3
        let mesh = volume(&env, Some(0.5)).unwrap();
        assert!(
            (total_tet_volume(&mesh) - want).abs() < 1e-9,
            "octahedron volume drift: got {}, want {}",
            total_tet_volume(&mesh),
            want
        );
    }

    #[test]
    fn volume_reuses_boundary_nodes() {
        let coords = insert(Coords::new(3).unwrap());
        let mesh = volume(&tetrahedron(coords.clone()), Some(10.0)).unwrap();
        // The single tet reuses the 4 boundary nodes — no node created.
        assert_eq!(read(&coords).unwrap().node_count(), 4);
        assert_eq!(mesh.cell_count().unwrap(), 1);
    }

    #[test]
    fn volume_rejects_open_surface() {
        // Seven of the octahedron's eight faces: a closed surface with a hole
        // (≥ 4 faces, so it reaches — and trips — the manifold/closure check).
        let coords = insert(Coords::new(3).unwrap());
        let pts = [
            (1.0, 0.0, 0.0),
            (-1.0, 0.0, 0.0),
            (0.0, 1.0, 0.0),
            (0.0, -1.0, 0.0),
            (0.0, 0.0, 1.0),
            (0.0, 0.0, -1.0),
        ];
        let mut tris: Vec<[usize; 3]> = Vec::new();
        for &xi in &[0usize, 1] {
            for &yi in &[2usize, 3] {
                for &zi in &[4usize, 5] {
                    tris.push([xi, yi, zi]);
                }
            }
        }
        tris.pop(); // drop one face → open surface
        let open = build_surface(coords.clone(), &pts, &tris, (0.0, 0.0, 0.0));
        let err = volume(&open, Some(10.0)).unwrap_err();
        let msg = format!("{}", err);
        assert!(
            msg.contains("open or inconsistently oriented") || msg.contains("non-manifold"),
            "unexpected error: {}",
            msg
        );
    }

    #[test]
    fn volume_rejects_non_tri3() {
        let coords = insert(Coords::new(3).unwrap());
        let ns: Vec<NodeId> = [
            (0.0, 0.0, 0.0),
            (1.0, 0.0, 0.0),
            (1.0, 1.0, 0.0),
            (0.0, 1.0, 0.0),
        ]
        .iter()
        .map(|&(x, y, z)| Node::create_in(coords.clone(), &[x, y, z]).unwrap().id())
        .collect();
        let mut quad = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::QUA4));
        quad.add_cell(&ns).unwrap();
        assert!(volume(&quad, None).is_err());
    }

    #[test]
    fn volume_rejects_2d_coords() {
        let coords = insert(Coords::new(2).unwrap());
        let ns: Vec<NodeId> = [(0.0, 0.0), (1.0, 0.0), (0.0, 1.0)]
            .iter()
            .map(|&(x, y)| Node::create_in(coords.clone(), &[x, y]).unwrap().id())
            .collect();
        let mut tri = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::TRI3));
        tri.add_cell(&ns).unwrap();
        let err = volume(&tri, None).unwrap_err();
        assert!(format!("{}", err).contains("3-D"));
    }

    #[test]
    fn volume_cancellable_stops_on_preset_flag() {
        use std::sync::atomic::AtomicBool;
        let coords = insert(Coords::new(3).unwrap());
        let flag = AtomicBool::new(true);
        let err = volume_cancellable(&octahedron(coords.clone()), Some(0.3), &flag).unwrap_err();
        assert!(matches!(err, PyrucastError::Interrupted));
    }

    #[test]
    fn volume_cancellable_completes_when_not_cancelled() {
        use std::sync::atomic::AtomicBool;
        let coords = insert(Coords::new(3).unwrap());
        let flag = AtomicBool::new(false);
        let mesh = volume_cancellable(&octahedron(coords.clone()), Some(0.5), &flag).unwrap();
        assert!(mesh.cell_count().unwrap() > 0);
    }
}