pyrucast 0.13.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
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
//! Python wrappers for the mesher operations in [`crate::ops::mesh`].
//!
//! Free functions that build or transform a [`PyMesh`]. Kept here —
//! mirroring `src/ops/mesher/` — rather than on the `Mesh` class, per the
//! `py/ops/` convention (operations live with operations).

use crate::atoms::ElementType;
use crate::containers::mesh::Mesh;
use crate::ops::mesh::{
    CellBlock, CellLayout, CellValues, FrontRelax, GaussRule, NodeOrder, NodeValues,
};
use crate::py::coords::PyCoords;
use crate::py::element_field::{PyElementField, PySubElementField};
use crate::py::mesh::PyMesh;
use crate::py::node::PyNode;
use crate::py::node_field::{PyNodeField, PySubNodeField};
use crate::py::signals::PySignals;
use pyo3::exceptions::PyTypeError;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::PyDict;
use pyrucast_macros::py_op;

/// A POI1 mesh holding every live node of `coords`.
///
/// # Parameters
///
/// - `coords`: the coordinates whose nodes to take.
///
/// # Returns
///
/// A POI1 `Mesh` of one zone.
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn from_live_nodes(coords: PyRef<PyCoords>) -> PyResult<PyMesh> {
    let mesh = crate::ops::mesh::from_live_nodes(coords.handle.clone())?;
    Ok(PyMesh { inner: mesh })
}

/// A POI1 mesh with one point per node of `nodes`, in their order.
///
/// The `Coords` comes from the nodes themselves — every `Node` carries its own —,
/// so no `Coords` argument is needed.
///
/// # Parameters
///
/// - `nodes`: the nodes, all of one `Coords`.
///
/// # Returns
///
/// A POI1 `Mesh` of one zone.
///
/// # Raises
///
/// `RuntimeError` if `nodes` is empty.
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn poi1_from_nodes(nodes: Vec<PyRef<PyNode>>) -> PyResult<PyMesh> {
    let ns: Vec<crate::atoms::Node> = nodes.iter().map(|n| n.as_node().clone()).collect();
    let mesh = crate::ops::mesh::poi1_from_nodes(&ns)?;
    Ok(PyMesh { inner: mesh })
}

/// The distinct nodes of `mesh`, as a POI1 mesh, submesh by submesh.
///
/// # Parameters
///
/// - `mesh`: the mesh whose nodes to take.
///
/// # Returns
///
/// A POI1 `Mesh` with one zone per submesh of `mesh`, holding its distinct nodes
/// in the order they first appear.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn to_poi1(mesh: PyRef<PyMesh>) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::to_poi1(&mesh.inner)?;
    Ok(PyMesh { inner: result })
}

/// A POI1 mesh of one fresh node at the centroid of each cell of `mesh`.
///
/// A POI1 input is therefore copied to fresh nodes at the same places — handy to
/// create the support nodes of Lagrange multipliers from a set of constrained
/// points.
///
/// # Parameters
///
/// - `mesh`: the mesh whose cells to take.
///
/// # Returns
///
/// A POI1 `Mesh` with one zone per submesh of `mesh`, one fresh node per cell.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn barycenter(mesh: PyRef<PyMesh>) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::barycenter(&mesh.inner)?;
    Ok(PyMesh { inner: result })
}

/// Keep the cells of `mesh` resting on the nodes of `points`.
///
/// Only the set of nodes `points` references matters, typically a POI1 mesh.
///
/// # Parameters
///
/// - `mesh`: the cells to filter.
/// - `points`: the nodes, on the same `Coords` as `mesh`.
/// - `strict`: `True` keeps a cell when **all** its nodes are in the set;
///   `False` when **at least one** is.
///
/// # Returns
///
/// A `Mesh` mirroring `mesh` submesh by submesh — same types, possibly empty
/// zones.
///
/// # Raises
///
/// `RuntimeError` if `mesh` and `points` are not attached to the same `Coords`.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, points, strict=true))]
pub fn elements_on(mesh: PyRef<PyMesh>, points: PyRef<PyMesh>, strict: bool) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::elements_on(&mesh.inner, &points.inner, strict)?;
    Ok(PyMesh { inner: result })
}

// --- Node selection by geometric region (Cast3m `POIN … SPHE / CYLI / …`) ---
//
// One operator per (shape, side). They all return a POI1 mesh mirroring
// `mesh` submesh by submesh — one zone per input zone, possibly empty — and
// all take the same `tol`, the geometric precision of the test as a distance
// to the region's surface; `tol=None` means `1e-6 ×` the bounding-box
// diagonal. The odd one out is the *nearest* node, which returns a single
// node and lives on the class: `mesh.nearest_node(point)`.

/// Nodes inside the sphere of centre `center` and radius `radius`.
///
/// Keeps the nodes at a distance `≤ radius + tol` from `center`. In 2-D the
/// sphere is a disc.
///
/// # Parameters
///
/// - `mesh`: the mesh whose nodes to test.
/// - `center`: the centre, with the dimension of the mesh.
/// - `radius`: the radius, `≥ 0`.
/// - `tol`: the precision of the test, a distance to the surface of the region;
///   `None` takes `1e-6 ×` the diagonal of the mesh's bounding box.
///
/// # Returns
///
/// A POI1 `Mesh` with one zone per zone of `mesh`, possibly empty, holding the
/// nodes kept.
///
/// # Raises
///
/// `RuntimeError` if a point does not have the dimension of the mesh, if `radius` is negative, or if `tol` is negative.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, center, radius, tol=None))]
pub fn points_in_sphere(
    mesh: PyRef<PyMesh>,
    center: Vec<f64>,
    radius: f64,
    tol: Option<f64>,
) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::points_in_sphere(&mesh.inner, &center, radius, tol)?;
    Ok(PyMesh { inner: result })
}

/// Nodes on the sphere of centre `center` and radius `radius`.
///
/// Keeps the nodes whose distance to `center` is within `tol` of `radius`, on
/// either side. In 2-D this selects a circle.
///
/// # Parameters
///
/// - `mesh`: the mesh whose nodes to test.
/// - `center`: the centre, with the dimension of the mesh.
/// - `radius`: the radius, `≥ 0`.
/// - `tol`: the precision of the test, a distance to the surface of the region;
///   `None` takes `1e-6 ×` the diagonal of the mesh's bounding box.
///
/// # Returns
///
/// A POI1 `Mesh` with one zone per zone of `mesh`, possibly empty, holding the
/// nodes kept.
///
/// # Raises
///
/// `RuntimeError` if a point does not have the dimension of the mesh, if `radius` is negative, or if `tol` is negative.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, center, radius, tol=None))]
pub fn points_on_sphere(
    mesh: PyRef<PyMesh>,
    center: Vec<f64>,
    radius: f64,
    tol: Option<f64>,
) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::points_on_sphere(&mesh.inner, &center, radius, tol)?;
    Ok(PyMesh { inner: result })
}

/// Nodes on the plane through `origin` with normal `normal`.
///
/// Keeps the nodes within `tol` of the plane — the usual way to grab a boundary
/// face of a box mesh. In 2-D the plane is a line.
///
/// # Parameters
///
/// - `mesh`: the mesh whose nodes to test.
/// - `origin`: a point of the plane.
/// - `normal`: the normal of the plane, not necessarily of unit length.
/// - `tol`: the precision of the test, a distance to the surface of the region;
///   `None` takes `1e-6 ×` the diagonal of the mesh's bounding box.
///
/// # Returns
///
/// A POI1 `Mesh` with one zone per zone of `mesh`, possibly empty, holding the
/// nodes kept.
///
/// # Raises
///
/// `RuntimeError` if a point does not have the dimension of the mesh, if `normal` is the zero vector, or if `tol` is negative.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, origin, normal, tol=None))]
pub fn points_on_plane(
    mesh: PyRef<PyMesh>,
    origin: Vec<f64>,
    normal: Vec<f64>,
    tol: Option<f64>,
) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::points_on_plane(&mesh.inner, &origin, &normal, tol)?;
    Ok(PyMesh { inner: result })
}

/// Nodes below the plane through `origin`: the side `normal` points away from.
///
/// The half-space includes the plane. There is no `points_above_plane`: flip the
/// normal and this is the other half-space.
///
/// # Parameters
///
/// - `mesh`: the mesh whose nodes to test.
/// - `origin`: a point of the plane.
/// - `normal`: the normal of the plane, pointing to the side left out.
/// - `tol`: the precision of the test, a distance to the surface of the region;
///   `None` takes `1e-6 ×` the diagonal of the mesh's bounding box.
///
/// # Returns
///
/// A POI1 `Mesh` with one zone per zone of `mesh`, possibly empty, holding the
/// nodes kept.
///
/// # Raises
///
/// `RuntimeError` if a point does not have the dimension of the mesh, if `normal` is the zero vector, or if `tol` is negative.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, origin, normal, tol=None))]
pub fn points_below_plane(
    mesh: PyRef<PyMesh>,
    origin: Vec<f64>,
    normal: Vec<f64>,
    tol: Option<f64>,
) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::points_below_plane(&mesh.inner, &origin, &normal, tol)?;
    Ok(PyMesh { inner: result })
}

/// Nodes on the infinite line through `a` and `b`.
///
/// Keeps the nodes at a distance `≤ tol` from the line, with no bound along it —
/// `pyrucast.mesh.points_in_cylinder` with a small radius gives a selection
/// clipped to the segment `a → b`.
///
/// # Parameters
///
/// - `mesh`: the mesh whose nodes to test.
/// - `a`, `b`: two distinct points of the line.
/// - `tol`: the precision of the test, a distance to the surface of the region;
///   `None` takes `1e-6 ×` the diagonal of the mesh's bounding box.
///
/// # Returns
///
/// A POI1 `Mesh` with one zone per zone of `mesh`, possibly empty, holding the
/// nodes kept.
///
/// # Raises
///
/// `RuntimeError` if a point does not have the dimension of the mesh, if `a` and `b` coincide, or if `tol` is negative.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, a, b, tol=None))]
pub fn points_on_line(
    mesh: PyRef<PyMesh>,
    a: Vec<f64>,
    b: Vec<f64>,
    tol: Option<f64>,
) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::points_on_line(&mesh.inner, &a, &b, tol)?;
    Ok(PyMesh { inner: result })
}

/// Nodes inside the finite cylinder of axis `base → top` and radius `radius`.
///
/// The cylinder is capped: a node is kept when it is within `radius + tol` of the
/// axis **and** its axial coordinate falls between the two end sections.
///
/// # Parameters
///
/// - `mesh`: the mesh whose nodes to test.
/// - `base`, `top`: the centres of the two end sections, distinct.
/// - `radius`: the radius, `≥ 0`.
/// - `tol`: the precision of the test, a distance to the surface of the region;
///   `None` takes `1e-6 ×` the diagonal of the mesh's bounding box.
///
/// # Returns
///
/// A POI1 `Mesh` with one zone per zone of `mesh`, possibly empty, holding the
/// nodes kept.
///
/// # Raises
///
/// `RuntimeError` if a point does not have the dimension of the mesh, if `base` and `top` coincide, if `radius` is negative, or if `tol` is negative.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, base, top, radius, tol=None))]
pub fn points_in_cylinder(
    mesh: PyRef<PyMesh>,
    base: Vec<f64>,
    top: Vec<f64>,
    radius: f64,
    tol: Option<f64>,
) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::points_in_cylinder(&mesh.inner, &base, &top, radius, tol)?;
    Ok(PyMesh { inner: result })
}

/// Nodes on the lateral surface of the cylinder of axis `base → top`.
///
/// The end discs are **not** part of the selection — they are flat faces, which
/// `pyrucast.mesh.points_on_plane` cuts. This is how you grab the bore of a tube.
///
/// # Parameters
///
/// - `mesh`: the mesh whose nodes to test.
/// - `base`, `top`: the centres of the two end sections, distinct.
/// - `radius`: the radius, `≥ 0`.
/// - `tol`: the precision of the test, a distance to the surface of the region;
///   `None` takes `1e-6 ×` the diagonal of the mesh's bounding box.
///
/// # Returns
///
/// A POI1 `Mesh` with one zone per zone of `mesh`, possibly empty, holding the
/// nodes kept.
///
/// # Raises
///
/// `RuntimeError` if a point does not have the dimension of the mesh, if `base` and `top` coincide, if `radius` is negative, or if `tol` is negative.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, base, top, radius, tol=None))]
pub fn points_on_cylinder(
    mesh: PyRef<PyMesh>,
    base: Vec<f64>,
    top: Vec<f64>,
    radius: f64,
    tol: Option<f64>,
) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::points_on_cylinder(&mesh.inner, &base, &top, radius, tol)?;
    Ok(PyMesh { inner: result })
}

/// Nodes inside the truncated cone of axis `base → top`.
///
/// Its radius is `base_radius` at `base` and `top_radius` at `top`: the default
/// `top_radius=0` gives a true cone whose apex is `top`, `top_radius=base_radius`
/// a cylinder. Capped like `pyrucast.mesh.points_in_cylinder`.
///
/// # Parameters
///
/// - `mesh`: the mesh whose nodes to test.
/// - `base`, `top`: the centres of the two end sections, distinct.
/// - `base_radius`: the radius at `base`, `≥ 0`.
/// - `top_radius`: the radius at `top`, `≥ 0`.
/// - `tol`: the precision of the test, a distance to the surface of the region;
///   `None` takes `1e-6 ×` the diagonal of the mesh's bounding box.
///
/// # Returns
///
/// A POI1 `Mesh` with one zone per zone of `mesh`, possibly empty, holding the
/// nodes kept.
///
/// # Raises
///
/// `RuntimeError` if a point does not have the dimension of the mesh, if `base` and `top` coincide, if a radius is negative, or if `tol` is negative.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, base, top, base_radius, top_radius=0.0, tol=None))]
pub fn points_in_cone(
    mesh: PyRef<PyMesh>,
    base: Vec<f64>,
    top: Vec<f64>,
    base_radius: f64,
    top_radius: f64,
    tol: Option<f64>,
) -> PyResult<PyMesh> {
    let result =
        crate::ops::mesh::points_in_cone(&mesh.inner, &base, &top, base_radius, top_radius, tol)?;
    Ok(PyMesh { inner: result })
}

/// Nodes on the lateral surface of the truncated cone of axis `base → top`.
///
/// The distance to the slanted surface is the perpendicular one, so the band
/// stays `tol` wide however steep the cone is. As for
/// `pyrucast.mesh.points_on_cylinder`, the end discs are not part of the
/// selection.
///
/// # Parameters
///
/// - `mesh`: the mesh whose nodes to test.
/// - `base`, `top`: the centres of the two end sections, distinct.
/// - `base_radius`: the radius at `base`, `≥ 0`.
/// - `top_radius`: the radius at `top`, `≥ 0`; `0` gives a true cone.
/// - `tol`: the precision of the test, a distance to the surface of the region;
///   `None` takes `1e-6 ×` the diagonal of the mesh's bounding box.
///
/// # Returns
///
/// A POI1 `Mesh` with one zone per zone of `mesh`, possibly empty, holding the
/// nodes kept.
///
/// # Raises
///
/// `RuntimeError` if a point does not have the dimension of the mesh, if `base` and `top` coincide, if a radius is negative, or if `tol` is negative.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, base, top, base_radius, top_radius=0.0, tol=None))]
pub fn points_on_cone(
    mesh: PyRef<PyMesh>,
    base: Vec<f64>,
    top: Vec<f64>,
    base_radius: f64,
    top_radius: f64,
    tol: Option<f64>,
) -> PyResult<PyMesh> {
    let result =
        crate::ops::mesh::points_on_cone(&mesh.inner, &base, &top, base_radius, top_radius, tol)?;
    Ok(PyMesh { inner: result })
}

/// Nodes inside the torus of centre `center` and axis `axis`, in 3-D.
///
/// The section is circular: the torus is the set of points at a distance
/// `≤ minor_radius` from the circle of radius `major_radius` drawn around
/// `center` in the plane normal to `axis`.
///
/// # Parameters
///
/// - `mesh`: the mesh whose nodes to test.
/// - `center`: the centre of the torus.
/// - `axis`: the axis of revolution.
/// - `major_radius`: the radius of the circle the tube follows.
/// - `minor_radius`: the radius of the tube.
/// - `tol`: the precision of the test, a distance to the surface of the region;
///   `None` takes `1e-6 ×` the diagonal of the mesh's bounding box.
///
/// # Returns
///
/// A POI1 `Mesh` with one zone per zone of `mesh`, possibly empty, holding the
/// nodes kept.
///
/// # Raises
///
/// `RuntimeError` if the mesh is not 3-D, if a point does not have the dimension of the mesh, if `axis` is the zero vector, if a radius is negative, or if `tol` is negative.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, center, axis, major_radius, minor_radius, tol=None))]
pub fn points_in_torus(
    mesh: PyRef<PyMesh>,
    center: Vec<f64>,
    axis: Vec<f64>,
    major_radius: f64,
    minor_radius: f64,
    tol: Option<f64>,
) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::points_in_torus(
        &mesh.inner,
        &center,
        &axis,
        major_radius,
        minor_radius,
        tol,
    )?;
    Ok(PyMesh { inner: result })
}

/// Nodes on the torus of centre `center` and axis `axis`, in 3-D.
///
/// Keeps the nodes within `tol` of the tube's surface. The torus being closed,
/// there is no cap to worry about.
///
/// # Parameters
///
/// - `mesh`: the mesh whose nodes to test.
/// - `center`: the centre of the torus.
/// - `axis`: the axis of revolution.
/// - `major_radius`: the radius of the circle the tube follows.
/// - `minor_radius`: the radius of the tube.
/// - `tol`: the precision of the test, a distance to the surface of the region;
///   `None` takes `1e-6 ×` the diagonal of the mesh's bounding box.
///
/// # Returns
///
/// A POI1 `Mesh` with one zone per zone of `mesh`, possibly empty, holding the
/// nodes kept.
///
/// # Raises
///
/// `RuntimeError` if the mesh is not 3-D, if a point does not have the dimension of the mesh, if `axis` is the zero vector, if a radius is negative, or if `tol` is negative.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, center, axis, major_radius, minor_radius, tol=None))]
pub fn points_on_torus(
    mesh: PyRef<PyMesh>,
    center: Vec<f64>,
    axis: Vec<f64>,
    major_radius: f64,
    minor_radius: f64,
    tol: Option<f64>,
) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::points_on_torus(
        &mesh.inner,
        &center,
        &axis,
        major_radius,
        minor_radius,
        tol,
    )?;
    Ok(PyMesh { inner: result })
}

/// Fuse the submeshes of `mesh` of one element type, dropping duplicate cells.
///
/// A duplicate cell has the same node sequence as another. Types appear in the
/// order they are first seen; the face colour of the first submesh of each type is
/// kept.
///
/// # Parameters
///
/// - `mesh`: the mesh to consolidate, left untouched.
///
/// # Returns
///
/// A new `Mesh`, one submesh per element type.
///
/// # Raises
///
/// `RuntimeError` if `mesh` has no submesh (no `Coords` to attach to).
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(name = "consolidate_mesh")]
pub fn consolidate(mesh: PyRef<PyMesh>) -> PyResult<PyMesh> {
    Ok(PyMesh {
        inner: crate::ops::mesh::consolidate(&mesh.inner)?,
    })
}

/// Weld together the nodes of `mesh` closer than `tol`.
///
/// The connectivity is redirected to one representative per cluster — the
/// smallest-id node of the cluster, which keeps its own coordinates (no
/// averaging). A cluster is a connected component of the relation "closer than
/// `tol`": the weld propagates from node to node, so on a chain a—b—c where a and
/// c are further apart than `tol`, all three still end up as one — which is why a
/// `tol` of the order of the element size collapses a whole region rather than a
/// seam. Every call prints a one-line tally on stdout once the weld is done —
/// nodes welded, cells dropped, tolerance used.
///
/// # Parameters
///
/// - `mesh`: the mesh to weld.
/// - `tol`: the distance, `≥ 0`; `0` welds only coincident nodes.
/// - `in_place`: `False` builds a new mesh, where cells that collapse onto a
///   repeated node (a degenerate segment, triangle, …) are dropped. `True`
///   rewrites the connectivity of `mesh`'s **own** submeshes instead and returns
///   `mesh` itself. Since the union `mesh_a | mesh_b` shares its submeshes, welding
///   that union in place welds `mesh_a` and `mesh_b` themselves: afterwards they
///   share their interface nodes. The structure is kept (same submeshes, same
///   cells in the same order), so a cell that would collapse is an error, as is a
///   submesh already sealed by a finite-element space, a field or a matrix; both
///   are checked before anything is written.
///
/// # Returns
///
/// A new `Mesh` mirroring `mesh` (same submeshes, types and colours), or `mesh`
/// itself with `in_place=True`.
///
/// # Raises
///
/// `RuntimeError` if `tol` is negative, or, with `in_place=True`, for a cell that
/// would collapse or a sealed submesh.
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, tol, in_place=false))]
pub fn merge_nodes(
    py: Python<'_>,
    mesh: Py<PyMesh>,
    tol: f64,
    in_place: bool,
) -> PyResult<Py<PyMesh>> {
    let result = crate::ops::mesh::merge_nodes(&mesh.borrow(py).inner, tol, in_place)?;
    if in_place {
        // The operator welded `mesh`'s own submeshes and handed the same mesh
        // back; return the caller's object itself, so `out is mesh` holds.
        return Ok(mesh);
    }
    Py::new(py, PyMesh { inner: result })
}

/// The boundary of a surface mesh, as SEG2 loops.
///
/// An edge used by exactly one cell is a boundary edge; the boundary edges, pooled
/// across all surface submeshes, are chained into closed loops. The loops keep the
/// counterclockwise boundary orientation (outer loop counterclockwise, holes
/// clockwise), so the result feeds straight back into
/// `pyrucast.mesh.triangulate_surface`.
///
/// # Parameters
///
/// - `mesh`: a surface mesh, TRI3, QUA4, TRI6, QUA8 or QUA9.
/// - `angle_deg`: when given, each loop is split into open **sides** at its
///   corners — where the boundary turns by more than `angle_deg` degrees —, as
///   `pyrucast.mesh.skin` splits a volume's skin into flat faces; a loop with no
///   such corner stays whole. `None` keeps every loop whole.
///
/// # Returns
///
/// A `Mesh` with one SEG2 submesh per loop — or per side —, on the nodes of
/// `mesh`: a single loop for a simply connected domain, several with holes or
/// disjoint pieces.
///
/// # Raises
///
/// `RuntimeError` if `mesh` is not a surface mesh.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, angle_deg=None))]
pub fn border(mesh: PyRef<PyMesh>, angle_deg: Option<f64>) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::border(&mesh.inner, angle_deg)?;
    Ok(PyMesh { inner: result })
}

/// The boundary surface of a volume mesh, one submesh per flat face.
///
/// A facet of a volume cell used by exactly one cell lies on the boundary;
/// sharing is decided on the facet's corners, so cells of different degrees still
/// cancel. The boundary facets, pooled across all volume submeshes, are grouped
/// into flat faces by flooding across shared edges as long as neighbouring facets
/// stay coplanar. **A facet keeps its own type**: a HEX8 yields QUA4, a TET10
/// TRI6, a HEX27 QUA9 — so the skin of a quadratic mesh is quadratic.
///
/// # Parameters
///
/// - `mesh`: a volume mesh — TET4, PYRA5, PENTA6, HEX8 or their quadratic
///   counterparts TET10, PENTA15, HEX20, HEX27.
/// - `angle_deg`: the largest angle, in degrees, between the normals of two
///   neighbouring facets of one flat face; `None` takes 1°.
///
/// # Returns
///
/// A `Mesh` with one submesh per flat face and per facet type — 6 for a cube, 5
/// for a prism, 5 for a pyramid. The facets keep their outward orientation and
/// the nodes of `mesh`.
///
/// # Raises
///
/// `RuntimeError` if `mesh` is not a 3-D volume mesh.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, angle_deg=None))]
pub fn skin(mesh: PyRef<PyMesh>, angle_deg: Option<f64>) -> PyResult<PyMesh> {
    // Python's optional kwarg is flattened **here**, at the boundary: past
    // this point the operator receives an angle, not an absence to test for.
    let angle_deg = angle_deg.unwrap_or(crate::ops::mesh::skin::DEFAULT_ANGLE_DEG);
    let result = crate::ops::mesh::skin(&mesh.inner, angle_deg)?;
    Ok(PyMesh { inner: result })
}

/// Make the orientation of the cells of `mesh` consistent.
///
/// Cells sharing a facet are oriented alike — all normals of a surface point the
/// same way, all segments of a curve run head to tail, all volume cells share one
/// handedness —, in any dimension, linear or quadratic. Each connected component
/// is seeded by its lowest-indexed cell, which keeps its orientation; the
/// absolute sense is not chosen (`pyrucast.mesh.invert` flips a whole mesh, a
/// hole's boundary for instance).
///
/// # Parameters
///
/// - `mesh`: the mesh to orient.
///
/// # Returns
///
/// A fresh `Mesh` sharing the nodes of `mesh`, which is left untouched.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn orient(mesh: PyRef<PyMesh>) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::orient(&mesh.inner)?;
    Ok(PyMesh { inner: result })
}

/// Reorder the cells of a line mesh into a continuous chain.
///
/// The complement of `pyrucast.mesh.orient`: where `orient` fixes the
/// *direction* of the cells, `chain` fixes their *order* — each submesh is sorted
/// so that consecutive cells share a node, flipped where needed, so reading the
/// connectivity walks the curve from one end to the other.
///
/// # Parameters
///
/// - `mesh`: a line mesh, SEG2 or SEG3; each submesh is chained on its own and
///   must be one continuous chain, open or closed.
///
/// # Returns
///
/// A fresh `Mesh` sharing the nodes of `mesh`.
///
/// # Raises
///
/// `RuntimeError` if `mesh` is not a line mesh, for a node carrying three
/// segments (a branch), or for disjoint pieces in one submesh.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn chain(mesh: PyRef<PyMesh>) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::chain(&mesh.inner)?;
    Ok(PyMesh { inner: result })
}

/// Reverse the orientation of every cell of `mesh`.
///
/// Flips each cell's winding, direction or handedness, in any dimension (POI1
/// cells are unchanged). With `pyrucast.mesh.orient`, it chooses the inside or
/// outside sense of a closed contour or surface.
///
/// # Parameters
///
/// - `mesh`: the mesh to flip.
///
/// # Returns
///
/// A fresh `Mesh` sharing the nodes of `mesh`, which is left untouched.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn invert(mesh: PyRef<PyMesh>) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::invert(&mesh.inner)?;
    Ok(PyMesh { inner: result })
}

/// A straight line of `n_elems` cells from node `a` to node `b`.
///
/// # Parameters
///
/// - `a`, `b`: the end nodes, of one `Coords`.
/// - `n_elems`: the number of cells, `≥ 1`.
/// - `element_type`: `"SEG2"` (the default) or `"SEG3"`.
///
/// # Returns
///
/// A `Mesh` of one zone, with fresh nodes between `a` and `b`.
///
/// # Raises
///
/// - `ValueError` for an unknown `element_type`.
/// - `RuntimeError` if `n_elems` is zero, or if `a` and `b` belong to different
///   `Coords`.
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (a, b, n_elems, element_type=None))]
pub fn line(
    a: PyRef<PyNode>,
    b: PyRef<PyNode>,
    n_elems: usize,
    element_type: Option<ElementType>,
) -> PyResult<PyMesh> {
    let element_type = element_type.unwrap_or(ElementType::SEG2);
    let mesh = crate::ops::mesh::line(a.as_node(), b.as_node(), n_elems, element_type)?;
    Ok(PyMesh { inner: mesh })
}

/// A closed circle of `n_elems` cells, centred on `center`.
///
/// # Parameters
///
/// - `center`: the centre node.
/// - `normal`: the normal of the circle's plane, three components even in 2-D.
/// - `radius`: the radius.
/// - `n_elems`: the number of cells.
/// - `element_type`: `"SEG2"` (the default) or `"SEG3"`.
///
/// # Returns
///
/// A `Mesh` of one zone, on fresh nodes.
///
/// # Raises
///
/// - `ValueError` for an unknown `element_type`.
/// - `RuntimeError` if `normal` does not have three components.
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (center, normal, radius, n_elems, element_type=None))]
pub fn circle(
    center: PyRef<PyNode>,
    normal: Vec<f64>,
    radius: f64,
    n_elems: usize,
    element_type: Option<ElementType>,
) -> PyResult<PyMesh> {
    let element_type = element_type.unwrap_or(ElementType::SEG2);
    let mesh = crate::ops::mesh::circle(center.as_node(), &normal, radius, n_elems, element_type)?;
    Ok(PyMesh { inner: mesh })
}

/// An open arc of `n_elems` cells from `a` to `b`, centred on `center`.
///
/// The circle passes through both ends; the shorter arc is built.
///
/// # Parameters
///
/// - `a`, `b`: the end nodes.
/// - `center`: the centre node of the circle.
/// - `n_elems`: the number of cells, `≥ 1`.
/// - `element_type`: `"SEG2"` (the default) or `"SEG3"`.
///
/// # Returns
///
/// A `Mesh` of one zone, with fresh nodes between `a` and `b`.
///
/// # Raises
///
/// - `ValueError` for an unknown `element_type`.
/// - `RuntimeError` if `a`, `center` and `b` are aligned (the plane of the arc is
///   undefined).
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (a, center, b, n_elems, element_type=None))]
pub fn arc(
    a: PyRef<PyNode>,
    center: PyRef<PyNode>,
    b: PyRef<PyNode>,
    n_elems: usize,
    element_type: Option<ElementType>,
) -> PyResult<PyMesh> {
    let element_type = element_type.unwrap_or(ElementType::SEG2);
    let mesh = crate::ops::mesh::arc(
        a.as_node(),
        center.as_node(),
        b.as_node(),
        n_elems,
        element_type,
    )?;
    Ok(PyMesh { inner: mesh })
}

/// Fill the space between two line meshes with `n_layers` layers of cells.
///
/// Two **closed loops** (each ending on its own first node) give a closed ring,
/// the seam shared between its first and last cells.
///
/// # Parameters
///
/// - `mesh_a`, `mesh_b`: the two lines, with the same number of cells.
/// - `n_layers`: the number of layers, `≥ 1`.
/// - `element_type`: between SEG2 lines, `"QUA4"` (the default), `"TRI3"`,
///   `"QUA8"`, `"QUA9"` or `"TRI6"` — a QUA4 mesh is built first, then converted
///   (diagonal split for the triangles, promoted to quadratic for the others).
///   Between SEG3 lines, `"QUA8"` (the default) or `"QUA9"`, built directly: the
///   lines' own mid-nodes are reused, so a strip between two arcs follows them
///   exactly.
///
/// # Returns
///
/// A new `Mesh` sharing the nodes of `mesh_a` and `mesh_b`.
///
/// # Raises
///
/// `RuntimeError` if the two lines differ in cell count, for an unsupported
/// `element_type`, or for a closed loop swept onto an open line.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh_a, mesh_b, n_layers, element_type=None))]
pub fn sweep(
    mesh_a: PyRef<PyMesh>,
    mesh_b: PyRef<PyMesh>,
    n_layers: usize,
    element_type: Option<ElementType>,
) -> PyResult<PyMesh> {
    // `None`: the surface the lines' degree calls for.
    let element_type = match element_type {
        Some(et) => et,
        None if mesh_a.inner.element_types()? == [ElementType::SEG3] => ElementType::QUA8,
        None => ElementType::QUA4,
    };
    let mesh = crate::ops::mesh::sweep(&mesh_a.inner, &mesh_b.inner, n_layers, element_type)?;
    Ok(PyMesh { inner: mesh })
}

/// A structured surface bounded by four SEG2 sides, by transfinite interpolation.
///
/// The generalization of `pyrucast.mesh.sweep` from two lines to four (a Coons
/// patch).
///
/// # Parameters
///
/// - `side1`, `side2`, `side3`, `side4`: the four sides, forming a closed
///   contour `side1 → side2 → side3 → side4 → side1`, each ending on the start
///   node of the next. `side1`/`side3` and `side2`/`side4` are the two pairs of
///   **opposite** sides, each with the same cell count.
/// - `element_type`: `"QUA4"` (the default), `"TRI3"`, `"QUA8"`, `"QUA9"` or
///   `"TRI6"` — the same conversion path as `pyrucast.mesh.sweep`.
///
/// # Returns
///
/// A new `Mesh` on the nodes of the sides plus fresh interior nodes.
///
/// # Raises
///
/// `RuntimeError` if the sides do not close, if opposite sides differ in cell
/// count, or for an unsupported `element_type`.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (side1, side2, side3, side4, element_type=None))]
pub fn transfinite(
    side1: PyRef<PyMesh>,
    side2: PyRef<PyMesh>,
    side3: PyRef<PyMesh>,
    side4: PyRef<PyMesh>,
    element_type: Option<ElementType>,
) -> PyResult<PyMesh> {
    let element_type = element_type.unwrap_or(ElementType::QUA4);
    let mesh = crate::ops::mesh::transfinite(
        &side1.inner,
        &side2.inner,
        &side3.inner,
        &side4.inner,
        element_type,
    )?;
    Ok(PyMesh { inner: mesh })
}

/// Extrude `mesh` along `direction` in `n_layers` layers.
///
/// SEG2 → QUA4, TRI3 → PENTA6, QUA4 → HEX8.
///
/// # Parameters
///
/// - `mesh`: the mesh to extrude.
/// - `direction`: the total displacement vector, with the dimension of the mesh.
/// - `n_layers`: the number of layers, `≥ 1`.
///
/// # Returns
///
/// A new `Mesh`, sharing the nodes of `mesh` on its first layer.
///
/// # Raises
///
/// `RuntimeError` if `n_layers` is zero, or if the produced cells need 3-D
/// coordinates the mesh does not have (a surface extruded in 2-D).
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn extrude(mesh: PyRef<PyMesh>, direction: Vec<f64>, n_layers: usize) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::extrude(&mesh.inner, &direction, n_layers)?;
    Ok(PyMesh { inner: result })
}

/// Revolve `mesh` about an axis in `n_layers` layers, over a total `angle`.
///
/// The rotational companion of `pyrucast.mesh.extrude`: SEG2 → QUA4,
/// TRI3 → PENTA6, QUA4 → HEX8, and in quadratic SEG3 → QUA8, TRI6 → PENTA15,
/// QUA8 → HEX20 (the mid-node of an edge along the sweep sits on the arc). A full
/// turn closes the ring: the last layer of nodes is the first one again.
///
/// A **line** may touch the axis by one corner: that node stays put, and the cells
/// touching it close into triangles — SEG2 → TRI3, SEG3 → TRI6 —, a submesh of
/// their own after the quadrangles. A line from the axis to the rim thus revolves
/// into a disc sector: a fan of triangles at the centre, a ring of quadrangles
/// around it, one node at the centre.
///
/// # Parameters
///
/// - `mesh`: the mesh to revolve. A surface cell may not touch the axis (its prism
///   would collapse), nor may a line lie along it.
/// - `angle`: the total angle, in radians, `|angle| ≤ 2π`; counterclockwise for a
///   positive angle in 2-D.
/// - `n_layers`: the number of layers, `≥ 1`.
/// - `center`: in 2-D, the centre of the revolution; in 3-D, a point of its axis.
/// - `axis`: in 3-D, the direction of the axis (right-handed), required; ignored
///   in 2-D.
///
/// # Returns
///
/// A new `Mesh`, sharing the nodes of `mesh` on its first layer.
///
/// # Raises
///
/// `RuntimeError` if `|angle|` exceeds a full turn, if the produced cells need 3-D
/// coordinates the mesh does not have, or for a cell the axis would collapse.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, angle, n_layers, center, axis=None))]
pub fn revolve(
    mesh: PyRef<PyMesh>,
    angle: f64,
    n_layers: usize,
    center: Vec<f64>,
    axis: Option<Vec<f64>>,
) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::revolve(&mesh.inner, angle, n_layers, &center, axis.as_deref())?;
    Ok(PyMesh { inner: result })
}

/// Fill the space between two matching surface meshes with `n_layers` layers.
///
/// The 3-D companion of `pyrucast.mesh.sweep`: TRI3 faces → PENTA6 prisms,
/// QUA4 faces → HEX8 hexahedra.
///
/// # Parameters
///
/// - `mesh_a`, `mesh_b`: the two surface meshes, TRI3 or QUA4, cell `i` of one
///   facing cell `i` of the other.
/// - `n_layers`: the number of layers, `≥ 1`.
///
/// # Returns
///
/// A new `Mesh` sharing the nodes of `mesh_a` and `mesh_b`.
///
/// # Raises
///
/// `RuntimeError` for a surface type other than TRI3 or QUA4, or if the two
/// meshes do not match.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn sweep_solid(
    mesh_a: PyRef<PyMesh>,
    mesh_b: PyRef<PyMesh>,
    n_layers: usize,
) -> PyResult<PyMesh> {
    let mesh = crate::ops::mesh::sweep_solid(&mesh_a.inner, &mesh_b.inner, n_layers)?;
    Ok(PyMesh { inner: mesh })
}

/// The quadratic (Lagrange-2) copy of the linear mesh `mesh`.
///
/// Each element type is bumped to its quadratic sibling (TRI3 → TRI6,
/// HEX8 → HEX20, …). Corner nodes are reused; one node is created at the middle of
/// each edge and shared between the cells that use it.
///
/// # Parameters
///
/// - `mesh`: the linear mesh, left untouched.
///
/// # Returns
///
/// A new `Mesh`.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn to_quadratic(mesh: PyRef<PyMesh>) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::to_quadratic(&mesh.inner)?;
    Ok(PyMesh { inner: result })
}

/// Split every cell of `mesh` into cells of `element_type`, adding no node.
///
/// Supported: the identity (already the target type, copied as it is),
/// `"QUA4"` → `"TRI3"` (two triangles per quadrangle, diagonal `(0, 2)`), and
/// `"HEX8"` → `"TET4"` (six tetrahedra per hexahedron, a conforming split).
/// Corner nodes are reused; face colours are kept. To promote to a quadratic type,
/// which creates mid-edge nodes, use `pyrucast.mesh.to_quadratic`.
///
/// # Parameters
///
/// - `mesh`: the mesh to convert, left untouched.
/// - `element_type`: the target element type.
///
/// # Returns
///
/// A new `Mesh` on the nodes of `mesh`.
///
/// # Raises
///
/// - `ValueError` for an unknown `element_type`.
/// - `RuntimeError` for a conversion that is not supported.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn convert(mesh: PyRef<PyMesh>, element_type: ElementType) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::convert(&mesh.inner, element_type)?;
    Ok(PyMesh { inner: result })
}

/// Copy `mesh`, onto fresh nodes or onto the same nodes.
///
/// Both modes keep the submesh order, element types, cell order and face colours,
/// and hand back a mesh that is **never sealed** — editable even when the source
/// has been frozen by a finite-element space, a field or a matrix.
///
/// # Parameters
///
/// - `mesh`: the mesh to copy, left untouched.
/// - `new_nodes`: `True` gives each distinct node of `mesh` one fresh node at the
///   same position (nodes shared between cells stay shared), so the two meshes
///   no longer move together. `False` copies the connectivity only: the two
///   meshes share their nodes, and moving one moves it in both.
///
/// # Returns
///
/// A new `Mesh`.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, new_nodes=true))]
pub fn copy(mesh: PyRef<PyMesh>, new_nodes: bool) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::copy(&mesh.inner, new_nodes)?;
    Ok(PyMesh { inner: result })
}

/// Push every node of `mesh` radially onto the sphere of centre `center`.
///
/// Corners and mid-nodes alike move along the ray from `center` through them, to
/// the distance `radius`. In a plane through `center` the sphere is a circle: a
/// contour of that plane lands on it node for node, each at its own angle — the
/// inner loop of a ring, ready for `pyrucast.mesh.sweep`.
///
/// # Parameters
///
/// - `mesh`: the mesh to project, left untouched.
/// - `center`: the centre of the sphere.
/// - `radius`: the radius of the sphere.
///
/// # Returns
///
/// A fresh `Mesh` with its own nodes, one per distinct node (a closed loop stays
/// closed), in the same zones, types and colours.
///
/// # Raises
///
/// `RuntimeError` for a node on `center`, which has no direction.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn project_on_sphere(mesh: PyRef<PyMesh>, center: Vec<f64>, radius: f64) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::project_on_sphere(&mesh.inner, &center, radius)?;
    Ok(PyMesh { inner: result })
}

/// Translate `mesh` by `vector`.
///
/// # Parameters
///
/// - `mesh`: the mesh to move.
/// - `vector`: the translation, with the dimension of the mesh.
///
/// # Returns
///
/// A fresh `Mesh` with its own nodes; `mesh` is left untouched.
///
/// # Raises
///
/// `RuntimeError` if `vector` does not have the dimension of the mesh.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn translate(mesh: PyRef<PyMesh>, vector: Vec<f64>) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::translate(&mesh.inner, &vector)?;
    Ok(PyMesh { inner: result })
}

/// Rotate `mesh` by `angle` about `center`.
///
/// # Parameters
///
/// - `mesh`: the mesh to turn.
/// - `angle`: the angle, in radians.
/// - `center`: in 2-D, the centre of the rotation; in 3-D, a point of its axis.
/// - `axis`: in 3-D, the direction of the axis (right-handed), required; ignored
///   in 2-D.
///
/// # Returns
///
/// A fresh `Mesh` with its own nodes; `mesh` is left untouched.
///
/// # Raises
///
/// `RuntimeError` if a point does not have the dimension of the mesh, or in 3-D
/// without `axis`.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, angle, center, axis=None))]
pub fn rotate(
    mesh: PyRef<PyMesh>,
    angle: f64,
    center: Vec<f64>,
    axis: Option<Vec<f64>>,
) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::rotate(&mesh.inner, angle, &center, axis.as_deref())?;
    Ok(PyMesh { inner: result })
}

/// Mirror `mesh` through the point `center`: every node goes to `2·center − x`.
///
/// In 3-D the map reverses orientation, so the cells are reordered (as
/// `pyrucast.mesh.invert` does) to keep the copy's Jacobians positive; in 2-D it
/// is a plain half-turn and nothing is reordered.
///
/// # Parameters
///
/// - `mesh`: the mesh to mirror.
/// - `center`: the centre of symmetry, with the dimension of the mesh.
///
/// # Returns
///
/// A fresh `Mesh` with its own nodes; `mesh` is left untouched.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn symmetry_point(mesh: PyRef<PyMesh>, center: Vec<f64>) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::symmetry_point(&mesh.inner, &center)?;
    Ok(PyMesh { inner: result })
}

/// Mirror `mesh` through the infinite line through `a` and `b`.
///
/// In 2-D this is the mirror image about the line; in 3-D it is the half-turn
/// about it (the mirror image through a plane is
/// `pyrucast.mesh.symmetry_plane`). It reverses orientation in 2-D only, where
/// the cells are reordered (as `pyrucast.mesh.invert` does) to keep the copy's
/// Jacobians positive.
///
/// # Parameters
///
/// - `mesh`: the mesh to mirror.
/// - `a`, `b`: two distinct points of the line.
///
/// # Returns
///
/// A fresh `Mesh` with its own nodes; `mesh` is left untouched.
///
/// # Raises
///
/// `RuntimeError` if `a` and `b` coincide.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn symmetry_line(mesh: PyRef<PyMesh>, a: Vec<f64>, b: Vec<f64>) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::symmetry_line(&mesh.inner, &a, &b)?;
    Ok(PyMesh { inner: result })
}

/// Mirror `mesh` through the plane through the points `a`, `b` and `c`, in 3-D.
///
/// Only the plane the three points span matters, not their order. The map always
/// reverses orientation, so the cells are reordered (as `pyrucast.mesh.invert`
/// does) to keep the copy's Jacobians positive. In 2-D the mirror about a line is
/// `pyrucast.mesh.symmetry_line`.
///
/// # Parameters
///
/// - `mesh`: the mesh to mirror, in 3-D.
/// - `a`, `b`, `c`: three points of the plane, not aligned.
///
/// # Returns
///
/// A fresh `Mesh` with its own nodes; `mesh` is left untouched.
///
/// # Raises
///
/// `RuntimeError` if the mesh is not 3-D, or if the three points are aligned.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn symmetry_plane(
    mesh: PyRef<PyMesh>,
    a: Vec<f64>,
    b: Vec<f64>,
    c: Vec<f64>,
) -> PyResult<PyMesh> {
    let result = crate::ops::mesh::symmetry_plane(&mesh.inner, &a, &b, &c)?;
    Ok(PyMesh { inner: result })
}

/// Mesh the inside of the closed SEG2 loops of `contour`, Delaunay and Ruppert.
///
/// A constrained Delaunay triangulation refined by Ruppert's algorithm. The
/// contour may be 2-D, or a planar loop in 3-D, meshed in its best-fit plane and
/// lifted back.
///
/// # Parameters
///
/// - `contour`: **one or more** closed SEG2 loops, each oriented by the caller: a
///   **counterclockwise** loop is a domain's outer boundary, a **clockwise** loop
///   a hole inside an outer loop. Several disjoint counterclockwise loops mesh
///   several independent domains at once.
/// - `element_type`: `"TRI3"`, or `"QUA4"`, quadrangle-dominant: the result may
///   also carry a few triangles at the boundary.
/// - `size`: the target edge length; `None` takes the mean boundary edge length,
///   domain by domain.
///
/// # Returns
///
/// A new `Mesh` on the contour's nodes plus fresh interior nodes.
///
/// # Raises
///
/// `RuntimeError` for a loop of fewer than three segments or not closed, or for
/// an `element_type` other than TRI3 or QUA4.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (contour, element_type, size=None))]
pub fn triangulate_surface(
    py: Python<'_>,
    contour: PyRef<PyMesh>,
    element_type: ElementType,
    size: Option<f64>,
) -> PyResult<PyMesh> {
    // Poll Python signals while meshing so a long run stays Ctrl+C-able.
    let mesh = crate::ops::mesh::triangulate_surface_cancellable(
        &contour.inner,
        element_type,
        size,
        &PySignals(py),
    )?;
    Ok(PyMesh { inner: mesh })
}

/// Pave the inside of closed loops with quadrangles, in rows walking inward.
///
/// The quadrangle companion of `pyrucast.mesh.triangulate_surface`, and the one
/// to reach for when the mesh is going to be computed on: it lays quadrangles
/// down directly, in rows that follow the contour, instead of triangulating and
/// pairing triangles up afterwards. The contour is untouchable: its nodes come
/// back at their own positions and no node is ever added on a boundary edge. A
/// contour the paver cannot work with is therefore reported rather than worked
/// around.
///
/// # Parameters
///
/// - `contour`: **one or more** closed SEG2 loops, oriented as for
///   `pyrucast.mesh.triangulate_surface`: a **counterclockwise** loop is a
///   domain's outer boundary, a **clockwise** loop a hole. Several disjoint
///   counterclockwise loops mesh several independent domains at once. It may be
///   2-D, or a planar loop in 3-D, meshed in its best-fit plane and lifted back.
/// - `element_type`: `"QUA4"`, `"QUA8"` or `"QUA9"`.
/// - `size`: the target edge length; `None` takes the mean boundary edge length,
///   domain by domain.
/// - `prefer_quads`: `False` lets an odd loop cost one triangle, returned in a
///   separate TRI3 submesh along with the few cells a distorted leftover polygon
///   could not make square. `True` removes the first kind, not the second, and
///   raises on a loop with an odd number of segments: a polygon with an odd
///   number of sides has no filling by quadrangles alone, and evening the count
///   would mean adding a boundary node.
/// - `relax`: what the front may do to itself between two rows. After each row
///   the fresh chain is smoothed, which keeps the front from kinking — and, being
///   a Laplacian, rounds its corners off. A front sheds nodes only at its
///   **corners**, so once they are rounded away it keeps every node it has while
///   its perimeter shrinks, and the middle comes out finer than asked: a plain
///   20 × 20 square at size 1 gives 600 cells instead of 400.
///   - `"free"` (`None`, the default): a node moves wherever the smoothing
///     points. The only mode that never lets the front kink.
///   - `"along"`: the same step, kept only **along** the front: the spacing is
///     evened out, the shape is not. The square then comes out as the 400 exact
///     squares anyone would draw.
///   - `"none"`: the front stays exactly where the row put it.
///
///   `"along"` and `"none"` win on anything with corners to keep and can kink on
///   a curve, where there is nothing to preserve and everything to straighten.
///
/// # Returns
///
/// A new `Mesh` on the contour's nodes plus fresh interior nodes.
///
/// # Raises
///
/// - `ValueError` for an unknown `relax`.
/// - `RuntimeError` for an `element_type` other than QUA4, QUA8 or QUA9, with
///   `prefer_quads=True` on an odd loop, or for a contour too coarse for the size
///   asked, where the front folds onto itself.
/// - `RuntimeError` also for a run that fails to converge.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (contour, element_type, size=None, prefer_quads=false, relax=None))]
pub fn pave_surface(
    py: Python<'_>,
    contour: PyRef<PyMesh>,
    element_type: ElementType,
    size: Option<f64>,
    prefer_quads: bool,
    relax: Option<FrontRelax>,
) -> PyResult<PyMesh> {
    // Poll Python signals while paving so a long run stays Ctrl+C-able.
    let mesh = crate::ops::mesh::pave_surface_cancellable(
        &contour.inner,
        element_type,
        size,
        prefer_quads,
        relax.unwrap_or_default(),
        &PySignals(py),
    )?;
    Ok(PyMesh { inner: mesh })
}

/// Move the interior nodes of a 2-D surface mesh to improve its cells.
///
/// The connectivity and the boundary stay exactly as they are. Two guarantees
/// hold: **no node on the boundary ever moves**, and a position is taken only
/// when every incident cell stays valid and the worst incident quality does not
/// get worse. Smoothing cannot fix a node with the wrong number of cells around
/// it — that is `pyrucast.mesh.cleanup`'s job, and running it first is usually
/// what unlocks the smoothing.
///
/// # Parameters
///
/// - `mesh`: a 2-D mesh; its TRI3 and QUA4 cells are smoothed, its POI1 and SEG2
///   submeshes ignored.
/// - `sweeps`: the number of passes.
/// - `angular`: `True` uses angle-based smoothing, which aims at the right angles
///   a quadrangle wants; `False` the plain Laplacian, which aims at the
///   barycentre of the neighbours and knows nothing about angles.
/// - `in_place`: `True` writes the new positions onto your own nodes and returns
///   `mesh` itself; `False` duplicates the moved nodes into a fresh mesh, the
///   boundary's nodes being shared since they never move.
///
/// # Returns
///
/// A new `Mesh`, or `mesh` itself with `in_place=True`.
///
/// # Raises
///
/// `RuntimeError` if `mesh` is not 2-D, or has no surface cells (TRI3/QUA4).
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, sweeps=20, angular=true, in_place=false))]
pub fn regularize(
    mesh: PyRef<PyMesh>,
    sweeps: usize,
    angular: bool,
    in_place: bool,
) -> PyResult<PyMesh> {
    let out = crate::ops::mesh::regularize(&mesh.inner, sweeps, angular, in_place)?;
    Ok(PyMesh { inner: out })
}

/// Fix the connectivity of a 2-D surface mesh: doublets and wrong valences.
///
/// It removes the doublets, gives up the nodes that have only three cells around
/// them, and switches the diagonals that lower the valence error. **No node of
/// the contour ever moves**, and none is ever given up: the mesh keeps exactly
/// the boundary it came with. Smoothing cannot fix these: the angles around a
/// node sum to 2π whatever the positions. Turning a lone triangle into a
/// quadrangle is `pyrucast.mesh.merge_triangles`'s job.
///
/// # Parameters
///
/// - `mesh`: a 2-D surface mesh, TRI3 and QUA4, never modified.
///
/// # Returns
///
/// A new `Mesh`, wound the way `mesh` was, clockwise or not. Your own nodes come
/// back untouched, apart from those on a ring a collapse relaxed, which are
/// duplicated.
///
/// # Raises
///
/// `RuntimeError` if `mesh` is not 2-D, or has no surface cells (TRI3/QUA4).
///
/// # Notes
///
/// A **doublet** is an interior node with only two quadrangles around it, which
/// therefore share two edges. A node of the **wrong valence** wants four cells
/// and has three or five. Three moves answer that:
///
/// - the **diagonal switch**, for a node of valence five or more: two quadrangles
///   sharing an edge form a hexagon, which splits across any of its three
///   diagonals. It is applied only when it strictly lowers the valence error and
///   leaves both cells convex.
/// - the **collapse**, for an interior node with only three cells: round a node
///   carrying `q` quadrangles and `t` triangles the star is bounded by a polygon
///   of `n = 2q + t` sides, which decomposes with no interior node into `q'`
///   quadrangles and `t'` triangles such that `2q' + t' = n − 2` — always a cell
///   fewer. So the node is given up along with that cell. Removing a node leaves
///   the ring round it stretched, so the move is judged after relaxing that ring;
///   a move that does not improve the neighbourhood's worst cell is undone whole.
/// - the **pair collapse**, for two interior nodes sharing an edge with at least
///   one short of a cell: together they carry `val(a) + val(b) − 2` cells and
///   both re-cut with two cells fewer.
///
/// Triangles only ever leave two at a time: their number has the parity of the
/// boundary's edge count, which nothing here may change.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn cleanup(mesh: PyRef<PyMesh>) -> PyResult<PyMesh> {
    let out = crate::ops::mesh::cleanup(&mesh.inner)?;
    Ok(PyMesh { inner: out })
}

/// Remove the triangles from a quadrangle-dominant mesh `mesh`, in pairs.
///
/// # Parameters
///
/// - `mesh`: a 2-D surface mesh, TRI3 and QUA4.
///
/// # Returns
///
/// A new `Mesh`.
///
/// # Raises
///
/// `RuntimeError` if `mesh` is not 2-D, or has no surface cells (TRI3/QUA4).
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn merge_triangles(mesh: PyRef<PyMesh>) -> PyResult<PyMesh> {
    let out = crate::ops::mesh::merge_triangles(&mesh.inner)?;
    Ok(PyMesh { inner: out })
}

/// Mesh the inside of closed loops with a structured grid core and a frontal band.
///
/// The regular companion of `pyrucast.mesh.pave_surface`, with the same input,
/// output and untouchable contour. `pave_surface` walks a front inward until two
/// of its rows meet, and that meeting line carries the valence defects, the
/// leftover triangles and the flattest cells — even a plain rectangle comes out
/// as an onion with four diagonal seams. `grid_surface` lays a tensor grid
/// instead, and leaves the front only what the grid could not reach.
///
/// The grid's lines are taken from the contour: every axis-aligned edge long
/// enough to be a feature pins a line, and the gaps are subdivided at about
/// `size`. A grid node landing on a contour node **is** that node. On a
/// rectilinear domain laid out for the grid there is no band at all: every cell
/// a rectangle, no triangle. That is the one thing asked of the caller: **break
/// every side at the shape's own corners and let each piece take a whole number
/// of cells**. A contour that does not just gets more band and less grid, down to
/// the quality of `pave_surface` in the worst case.
///
/// # Parameters
///
/// - `contour`: **one or more** closed SEG2 loops, oriented as for
///   `pyrucast.mesh.triangulate_surface`: a **counterclockwise** loop is a
///   domain's outer boundary, a **clockwise** loop a hole. Several disjoint
///   counterclockwise loops mesh several independent domains at once. It may be
///   2-D, or a planar loop in 3-D, meshed in its best-fit plane and lifted back.
/// - `element_type`: `"QUA4"`, `"QUA8"` or `"QUA9"`.
/// - `size`: the target edge length; `None` takes the mean boundary edge length,
///   domain by domain.
/// - `band`: extra clearance, in cells, between the core and the contour. Zero is
///   the useful value; raise it only for a contour the grid cannot meet, such as
///   a curve, where giving the front a couple of cells to work in beats letting
///   it fight for a sliver.
/// - `prefer_quads`: `False` lets an odd loop cost one triangle, returned in a
///   separate TRI3 submesh along with the few cells a distorted leftover polygon
///   could not make square. `True` removes the first kind, not the second, and
///   raises on a loop with an odd number of segments: a polygon with an odd
///   number of sides has no filling by quadrangles alone, and evening the count
///   would mean adding a boundary node.
/// - `relax`: what the front may do to itself between two rows — `"free"`
///   (`None`, the default), `"along"` or `"none"`; see
///   `pyrucast.mesh.pave_surface`.
///
/// # Returns
///
/// A new `Mesh` on the contour's nodes plus fresh interior nodes.
///
/// # Raises
///
/// - `ValueError` for an unknown `relax`.
/// - `RuntimeError` for an `element_type` other than QUA4, QUA8 or QUA9, with
///   `prefer_quads=True` on an odd loop, or for a contour too coarse for the size
///   asked, where the front folds onto itself.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (contour, element_type, size=None, band=0, prefer_quads=false, relax=None))]
pub fn grid_surface(
    py: Python<'_>,
    contour: PyRef<PyMesh>,
    element_type: ElementType,
    size: Option<f64>,
    band: usize,
    prefer_quads: bool,
    relax: Option<FrontRelax>,
) -> PyResult<PyMesh> {
    let mesh = crate::ops::mesh::grid_surface_cancellable(
        &contour.inner,
        element_type,
        size,
        band,
        prefer_quads,
        relax.unwrap_or_default(),
        &PySignals(py),
    )?;
    Ok(PyMesh { inner: mesh })
}

/// Mesh the inside of closed loops with a grid core, a line per contour node.
///
/// The sibling of `pyrucast.mesh.grid_surface`, which it does not replace: same
/// input, output and untouchable contour, and neither wins everywhere.
/// `grid_surface` pins a line on the coordinate each aligned side lies on; every
/// line is straight. `grid_surface2` gives every node of the contour the line
/// that crosses it, collapses the bands too thin to be cells, and lets a grid
/// node within a quarter cell of a contour node move onto it. A row is therefore
/// a polyline, so one row can meet two facing walls at two different heights: a
/// wall cut into ten can face a wall cut into eleven.
///
/// **Reach for it on a rectilinear shape**, all the more so when its sides were
/// not cut at the corners facing them; **reach for `grid_surface` on anything
/// curved**. Measured worst cell, `grid_surface` then `grid_surface2`: plate with
/// a step off the grid 0.405 / 0.963, L with arbitrary dimensions 0.437 / 0.979,
/// crenellated profile 0.323 / 0.651, house with a pitched roof 0.420 / 0.548. On
/// a **circle** the order reverses hard — 0.288 against 0.005 —: nothing dictates
/// a grid line over most of a curve.
///
/// # Parameters
///
/// - `contour`: **one or more** closed SEG2 loops, oriented as for
///   `pyrucast.mesh.triangulate_surface`: a **counterclockwise** loop is a
///   domain's outer boundary, a **clockwise** loop a hole. Several disjoint
///   counterclockwise loops mesh several independent domains at once. It may be
///   2-D, or a planar loop in 3-D, meshed in its best-fit plane and lifted back.
/// - `element_type`: `"QUA4"`, `"QUA8"` or `"QUA9"`.
/// - `size`: the target edge length; `None` takes the mean boundary edge length,
///   domain by domain.
/// - `band`: as for `pyrucast.mesh.grid_surface`.
/// - `prefer_quads`: `False` lets an odd loop cost one triangle, returned in a
///   separate TRI3 submesh along with the few cells a distorted leftover polygon
///   could not make square. `True` removes the first kind, not the second, and
///   raises on a loop with an odd number of segments: a polygon with an odd
///   number of sides has no filling by quadrangles alone, and evening the count
///   would mean adding a boundary node.
/// - `relax`: what the front may do to itself between two rows — `"free"`
///   (`None`, the default), `"along"` or `"none"`; see
///   `pyrucast.mesh.pave_surface`.
///
/// # Returns
///
/// A new `Mesh` on the contour's nodes plus fresh interior nodes.
///
/// # Raises
///
/// - `ValueError` for an unknown `relax`.
/// - `RuntimeError` for an `element_type` other than QUA4, QUA8 or QUA9, with
///   `prefer_quads=True` on an odd loop, or for a contour too coarse for the size
///   asked, where the front folds onto itself.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (contour, element_type, size=None, band=0, prefer_quads=false, relax=None))]
pub fn grid_surface2(
    py: Python<'_>,
    contour: PyRef<PyMesh>,
    element_type: ElementType,
    size: Option<f64>,
    band: usize,
    prefer_quads: bool,
    relax: Option<FrontRelax>,
) -> PyResult<PyMesh> {
    let mesh = crate::ops::mesh::grid_surface2_cancellable(
        &contour.inner,
        element_type,
        size,
        band,
        prefer_quads,
        relax.unwrap_or_default(),
        &PySignals(py),
    )?;
    Ok(PyMesh { inner: mesh })
}

/// Mesh a closed envelope: a hexahedral boundary layer over a tetrahedral core.
///
/// The 3-D companion of `pyrucast.mesh.pave_surface`. Hexahedra go where they
/// matter, in the layer against the boundary where gradients are steepest; the
/// smooth interior is left to tetrahedra. Pyramids join the two: the layer's
/// inner faces are squares and a tetrahedron has none, so without them the mesh
/// could not be conforming.
///
/// # Parameters
///
/// - `envelope`: a closed surface of QUA4 and/or TRI3 facets, its normals pointing
///   **out of the material**; its nodes are reused as they are.
/// - `layers`: the number of boundary layers, grown inward.
/// - `thickness`: the depth of each layer; `None` takes the envelope's mean edge
///   length, which gives roughly cube-shaped cells.
/// - `size`: the target cell size of the tetrahedral core.
///
/// # Returns
///
/// A new `Mesh` with a HEX8 submesh (born of the QUA4 facets), a PENTA6 one (born
/// of the TRI3 facets), a PYRA5 one and a TET4 one, each present only if not
/// empty.
///
/// # Raises
///
/// `RuntimeError` if `envelope` is not closed.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (envelope, layers=1, thickness=None, size=None))]
pub fn pave_volume(
    py: Python<'_>,
    envelope: PyRef<PyMesh>,
    layers: usize,
    thickness: Option<f64>,
    size: Option<f64>,
) -> PyResult<PyMesh> {
    // Poll Python signals while meshing so a long run stays Ctrl+C-able.
    let mesh = crate::ops::mesh::pave_volume_cancellable(
        &envelope.inner,
        layers,
        thickness,
        size,
        &PySignals(py),
    )?;
    Ok(PyMesh { inner: mesh })
}

/// Fill the inside of a closed TRI3 `envelope` with TET4 cells.
///
/// The 3-D companion of `pyrucast.mesh.triangulate_surface`. A concave shape is
/// fine, and an internal cavity is simply another closed surface whose normals
/// point into the hole. The envelope's nodes are reused as they are, and nodes
/// are added inside the solid so the cells come out well shaped.
///
/// # Parameters
///
/// - `envelope`: the closed TRI3 surface, its normals pointing **out of the
///   material**.
/// - `size`: the target edge length; `None` takes the mean edge length of the
///   envelope.
/// - `allow_surface_nodes`: lets the mesher cut the envelope finer where it
///   cannot otherwise fit it or make it usable. The shape is kept — every added
///   node lies on the edge or facet it divides —, but the skin of the result no
///   longer matches the surface handed in, and a warning on stderr says how many
///   nodes were added. Without it, such a surface is refused rather than meshed
///   badly.
///
/// # Returns
///
/// A new `Mesh` of TET4 cells.
///
/// # Raises
///
/// `RuntimeError` if `envelope` is not a closed surface, or, without
/// `allow_surface_nodes`, for a surface the mesher cannot fit.
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (envelope, size=None, allow_surface_nodes=false))]
pub fn triangulate_volume(
    py: Python<'_>,
    envelope: PyRef<PyMesh>,
    size: Option<f64>,
    allow_surface_nodes: bool,
) -> PyResult<PyMesh> {
    // Poll Python signals while meshing so a long run stays Ctrl+C-able.
    let mesh = crate::ops::mesh::triangulate_volume_cancellable(
        &envelope.inner,
        size,
        allow_surface_nodes,
        &PySignals(py),
    )?;
    Ok(PyMesh { inner: mesh })
}

/// Turn an ordered list of `(group name, Mesh)` pairs into a `dict` that
/// keeps the file order (Python dicts are insertion-ordered).
fn groups_to_dict<'py>(
    py: Python<'py>,
    groups: Vec<(String, Mesh)>,
) -> PyResult<Bound<'py, PyDict>> {
    let dict = PyDict::new(py);
    for (name, mesh) in groups {
        dict.set_item(name, Py::new(py, PyMesh { inner: mesh })?)?;
    }
    Ok(dict)
}

/// The pyrucast element type of a gmsh element-type code.
///
/// The translation `pyrucast.mesh.from_gmsh` applies to every block before
/// handing it to `pyrucast.mesh.from_arrays`.
///
/// # Parameters
///
/// - `code`: the gmsh code, `2` for a 3-node triangle.
///
/// # Returns
///
/// The name of the pyrucast element type, `"TRI3"` for `2`.
///
/// # Raises
///
/// `RuntimeError` for a gmsh type pyrucast has no counterpart for (third order and
/// above, …).
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn element_type_from_gmsh(code: u32) -> PyResult<&'static str> {
    Ok(crate::ops::mesh::gmsh::element_type_from_gmsh(code)?.name())
}

/// Pyrucast's Gauss rule for `element_type`, carried into an external element.
///
/// The points are mapped into the external reference element and the weights
/// scaled by the ratio of the reference measures.
///
/// # Parameters
///
/// - `element_type`: the element type.
/// - `ref_nodes`: the nodes of the external reference element, `dim` coordinates
///   each, numbered in `order`.
/// - `order`: the numbering of `ref_nodes`: `"pyrucast"`, `"gmsh"` or `"med"`.
///
/// # Returns
///
/// `(xi, weights)`: the points, in pyrucast's order, and their weights.
///
/// # Raises
///
/// - `ValueError` for an unknown `element_type` or `order`.
/// - `RuntimeError` if the external element is not an affine image of pyrucast's
///   under that numbering.
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn gauss_to_external(
    element_type: ElementType,
    ref_nodes: Vec<f64>,
    order: &str,
) -> PyResult<(Vec<f64>, Vec<f64>)> {
    let order = crate::py::arrays::parse_order(order)?;
    Ok(crate::ops::mesh::gauss_map::gauss_to_external(
        element_type,
        order,
        &ref_nodes,
    )?)
}

/// Where each Gauss point of `element_type` sits in an external rule.
///
/// # Parameters
///
/// - `element_type`: the element type.
/// - `ref_nodes`: the nodes of the external reference element, numbered in
///   `order`.
/// - `xi`, `weights`: the external rule, declared in that element.
/// - `order`: the numbering of `ref_nodes`: `"pyrucast"`, `"gmsh"` or `"med"`.
///
/// # Returns
///
/// For each Gauss point of pyrucast, the index of the same point in `xi`.
///
/// # Raises
///
/// - `ValueError` for an unknown `element_type` or `order`.
/// - `RuntimeError` if the two rules are not the same quadrature.
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn match_gauss(
    element_type: ElementType,
    ref_nodes: Vec<f64>,
    xi: Vec<f64>,
    weights: Vec<f64>,
    order: &str,
) -> PyResult<Vec<usize>> {
    let order = crate::py::arrays::parse_order(order)?;
    Ok(crate::ops::mesh::gauss_map::match_gauss(
        element_type,
        order,
        &ref_nodes,
        &xi,
        &weights,
    )?)
}

/// Build meshes and fields from flat arrays, the shape `export.to_arrays` returns.
///
/// The one import every exchange format goes through
/// (`pyrucast.mesh.from_gmsh` and `pyrucast.mesh.from_medcoupling` are built on
/// it). Only the nodes a cell references are created. numpy arrays are read
/// **without being copied**, through the buffer protocol; plain Python lists work
/// too, at the cost of a conversion.
///
/// # Parameters
///
/// - `coords`: where the nodes are added; its dimension decides how many
///   coordinates are kept.
/// - `node_tags`: one integer tag per node (`uint64` or `int64`).
/// - `node_coords`: 1 to 3 coordinates per tag, in the same order, padded with
///   zeros.
/// - `blocks`: a sequence of `(element_type, node_tags, cell_tags, groups)`, one
///   per block of cells sharing a type and a combination of groups: the pyrucast
///   type name (`"TET4"`), the flat connectivity in node tags, one tag per cell
///   (or an empty sequence when no cell field names the block), and the names of
///   the groups the cells belong to. A 3-tuple without `cell_tags` is accepted
///   too.
/// - `node_fields`: a sequence of `(components, node_tags, values)`,
///   `len(node_tags) * len(components)` values, node-major. A node field keeps
///   the nodes the mesh created.
/// - `cell_fields`: a sequence of `(components, cell_tags, values, layout)`.
///   `layout` is `"cell"` (one value per cell and component) or a sequence of
///   Gauss rules `(element_type, ref_nodes, xi, weights)` in the format's own
///   reference element — one value per point, component and cell, the points in
///   the order of the rule, which must be pyrucast's. A cell field gets a zone on
///   every group all of whose cells it defines.
/// - `order`: the node numbering inside a cell: `"pyrucast"` (the same as VTK),
///   `"gmsh"` or `"med"`.
///
/// # Returns
///
/// `(meshes, node_fields, element_fields)`: a `dict` from group name to `Mesh`
/// (all on `coords`, in order of first appearance), then the fields in the order
/// they were given.
///
/// # Raises
///
/// - `ValueError` for an unknown `order` or element type.
/// - `RuntimeError` for a cell referencing a node tag that is not given.
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (coords, node_tags, node_coords, blocks, *, node_fields = Vec::new(), cell_fields = Vec::new(), order = "pyrucast"))]
#[allow(clippy::too_many_arguments)]
pub fn from_arrays<'py>(
    py: Python<'py>,
    coords: PyRef<PyCoords>,
    node_tags: &Bound<'py, PyAny>,
    node_coords: &Bound<'py, PyAny>,
    blocks: &Bound<'py, PyAny>,
    node_fields: Vec<Py<PyAny>>,
    cell_fields: Vec<Py<PyAny>>,
    order: &str,
) -> PyResult<(Bound<'py, PyDict>, Vec<PyNodeField>, Vec<PyElementField>)> {
    let order = crate::py::arrays::parse_order(order)?;
    // The tag type follows the node table: medcoupling hands `int64`, gmsh
    // `uint64`. Every other tag array is borrowed as the same type (copied
    // only if its dtype differs).
    let signed = pyo3::buffer::PyBuffer::<i64>::get(node_tags).is_ok();
    let node_fields: Vec<Bound<'py, PyAny>> =
        node_fields.into_iter().map(|f| f.into_bound(py)).collect();
    let cell_fields: Vec<Bound<'py, PyAny>> =
        cell_fields.into_iter().map(|f| f.into_bound(py)).collect();
    let inputs = ExchangeInputs::unpack(blocks, &node_fields, &cell_fields)?;
    let out = if signed {
        inputs.import::<i64>(coords.handle.clone(), node_tags, node_coords, order)?
    } else {
        inputs.import::<u64>(coords.handle.clone(), node_tags, node_coords, order)?
    };
    Ok((
        groups_to_dict(py, out.groups)?,
        out.node_fields
            .into_iter()
            .map(|inner| PyNodeField { inner })
            .collect(),
        out.element_fields
            .into_iter()
            .map(|inner| PyElementField { inner })
            .collect(),
    ))
}

/// The Python objects behind `from_arrays`' arguments, unpacked **first** so
/// they outlive the slices borrowed from them.
struct ExchangeInputs<'py> {
    blocks: Vec<BlockObjects<'py>>,
    node_fields: Vec<(Vec<String>, Bound<'py, PyAny>, Bound<'py, PyAny>)>,
    cell_fields: Vec<CellFieldObjects<'py>>,
}

/// One block as given: `(element_type, node_tags, cell_tags, groups)`.
type BlockObjects<'py> = (
    ElementType,
    Bound<'py, PyAny>,
    Option<Bound<'py, PyAny>>,
    Vec<String>,
);

/// One cell field as given: `(components, cell_tags, values, rules)`, the
/// rules absent for the `"cell"` layout.
type CellFieldObjects<'py> = (
    Vec<String>,
    Bound<'py, PyAny>,
    Bound<'py, PyAny>,
    Option<Vec<RuleObjects<'py>>>,
);

/// One Gauss rule as given: `(element_type, ref_nodes, xi, weights)`.
type RuleObjects<'py> = (
    ElementType,
    Bound<'py, PyAny>,
    Bound<'py, PyAny>,
    Bound<'py, PyAny>,
);

fn nth<'py>(item: &Bound<'py, PyAny>, k: usize, what: &str) -> PyResult<Bound<'py, PyAny>> {
    item.get_item(k)
        .map_err(|_| PyValueError::new_err(format!("{what}: missing entry {k}")))
}

impl<'py> ExchangeInputs<'py> {
    fn unpack(
        blocks: &Bound<'py, PyAny>,
        node_fields: &[Bound<'py, PyAny>],
        cell_fields: &[Bound<'py, PyAny>],
    ) -> PyResult<Self> {
        let mut bs = Vec::new();
        for (i, item) in blocks.try_iter()?.enumerate() {
            let item = item?;
            let what = format!("block {i}: expected (element_type, node_tags, cell_tags, groups)");
            let (cells, groups) = match item.len()? {
                3 => (None, nth(&item, 2, &what)?),
                4 => (Some(nth(&item, 2, &what)?), nth(&item, 3, &what)?),
                _ => return Err(PyValueError::new_err(what)),
            };
            bs.push((
                nth(&item, 0, &what)?.extract()?,
                nth(&item, 1, &what)?,
                cells,
                groups.extract()?,
            ));
        }
        let mut nfs = Vec::new();
        for (i, item) in node_fields.iter().enumerate() {
            let what = format!("node field {i}: expected (components, node_tags, values)");
            nfs.push((
                nth(item, 0, &what)?.extract()?,
                nth(item, 1, &what)?,
                nth(item, 2, &what)?,
            ));
        }
        let mut cfs = Vec::new();
        for (i, item) in cell_fields.iter().enumerate() {
            let what = format!("cell field {i}: expected (components, cell_tags, values, layout)");
            let layout = nth(item, 3, &what)?;
            let rules = if let Ok(s) = layout.extract::<String>() {
                if !s.trim().eq_ignore_ascii_case("cell") {
                    return Err(PyValueError::new_err(format!(
                        "cell field {i}: layout '{s}' is neither 'cell' nor a list of Gauss rules"
                    )));
                }
                None
            } else {
                let mut rules = Vec::new();
                for r in layout.try_iter()? {
                    let r = r?;
                    let what = format!("cell field {i}: expected Gauss rules (element_type, ref_nodes, xi, weights)");
                    rules.push((
                        nth(&r, 0, &what)?.extract()?,
                        nth(&r, 1, &what)?,
                        nth(&r, 2, &what)?,
                        nth(&r, 3, &what)?,
                    ));
                }
                Some(rules)
            };
            cfs.push((
                nth(item, 0, &what)?.extract()?,
                nth(item, 1, &what)?,
                nth(item, 2, &what)?,
                rules,
            ));
        }
        Ok(Self {
            blocks: bs,
            node_fields: nfs,
            cell_fields: cfs,
        })
    }

    fn import<T>(
        &self,
        coords: crate::handle::Handle<crate::coords::Coords>,
        node_tags: &Bound<'py, PyAny>,
        node_coords: &Bound<'py, PyAny>,
        order: NodeOrder,
    ) -> PyResult<crate::ops::mesh::Imported>
    where
        T: crate::ops::mesh::Tag + pyo3::buffer::Element + for<'b, 'p> FromPyObject<'b, 'p>,
    {
        use crate::py::arrays::borrow;
        let tags = borrow::<T>(node_tags)?;
        let xyz = borrow::<f64>(node_coords)?;
        let conns = self
            .blocks
            .iter()
            .map(|b| borrow::<T>(&b.1))
            .collect::<PyResult<Vec<_>>>()?;
        let cells = self
            .blocks
            .iter()
            .map(|b| b.2.as_ref().map(borrow::<T>).transpose())
            .collect::<PyResult<Vec<_>>>()?;
        let blocks: Vec<CellBlock<'_, T>> = self
            .blocks
            .iter()
            .zip(&conns)
            .zip(&cells)
            .map(|((b, conn), cells)| CellBlock {
                element_type: b.0,
                node_tags: conn.as_slice(),
                cell_tags: cells.as_ref().map_or(&[], |c| c.as_slice()),
                groups: &b.3,
            })
            .collect();

        let nf_arrays = self
            .node_fields
            .iter()
            .map(|f| Ok((borrow::<T>(&f.1)?, borrow::<f64>(&f.2)?)))
            .collect::<PyResult<Vec<_>>>()?;
        let node_values: Vec<NodeValues<'_, T>> = self
            .node_fields
            .iter()
            .zip(&nf_arrays)
            .map(|(f, (t, v))| NodeValues {
                components: &f.0,
                node_tags: t.as_slice(),
                values: v.as_slice(),
            })
            .collect();

        let cf_arrays = self
            .cell_fields
            .iter()
            .map(|f| Ok((borrow::<T>(&f.1)?, borrow::<f64>(&f.2)?)))
            .collect::<PyResult<Vec<_>>>()?;
        let rule_arrays = self
            .cell_fields
            .iter()
            .map(|f| {
                f.3.iter()
                    .flatten()
                    .map(|r| {
                        Ok((
                            borrow::<f64>(&r.1)?,
                            borrow::<f64>(&r.2)?,
                            borrow::<f64>(&r.3)?,
                        ))
                    })
                    .collect::<PyResult<Vec<_>>>()
            })
            .collect::<PyResult<Vec<_>>>()?;
        let rules: Vec<Vec<GaussRule<'_>>> = self
            .cell_fields
            .iter()
            .zip(&rule_arrays)
            .map(|(f, arrays)| {
                f.3.iter()
                    .flatten()
                    .zip(arrays)
                    .map(|(r, (refs, xi, w))| GaussRule {
                        element_type: r.0,
                        ref_nodes: refs.as_slice(),
                        xi: xi.as_slice(),
                        weights: w.as_slice(),
                    })
                    .collect()
            })
            .collect();
        let cell_values: Vec<CellValues<'_, T>> = self
            .cell_fields
            .iter()
            .zip(&cf_arrays)
            .zip(&rules)
            .map(|((f, (t, v)), rules)| CellValues {
                components: &f.0,
                cell_tags: t.as_slice(),
                values: v.as_slice(),
                layout: if f.3.is_some() {
                    CellLayout::Gauss(rules)
                } else {
                    CellLayout::Cell
                },
            })
            .collect();

        Ok(crate::ops::mesh::from_arrays(
            coords,
            tags.as_slice(),
            xyz.as_slice(),
            &blocks,
            &node_values,
            &cell_values,
            order,
        )?)
    }
}

/// Read a gmsh `.msh` file, one `Mesh` per physical group.
///
/// ASCII or binary, MSH 2.2 or 4.1. All returned meshes share one `Coords`, so a
/// node on the boundary between two named groups is the same node on both sides.
/// Inside a group's mesh there is one submesh per element type; elements with no
/// physical group land under the key `"<ungrouped>"`. Every element type pyrucast
/// knows is read: POI1, SEG2, TRI3, QUA4, TET4, PYRA5, PENTA6, HEX8 and the
/// quadratic SEG3, TRI6, QUA8, QUA9, TET10, PENTA15, HEX20, HEX27.
///
/// # Parameters
///
/// - `coords`: where the nodes are added; its dimension decides how many of
///   gmsh's three coordinates are kept (a 2-D `Coords` keeps `x` and `y`).
/// - `path`: the file to read.
///
/// # Returns
///
/// A `dict` from physical group name to `Mesh`, in the order of the file.
///
/// # Raises
///
/// `RuntimeError` if the file cannot be read or parsed, or for a gmsh element
/// type pyrucast does not know.
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn read_gmsh<'py>(
    py: Python<'py>,
    coords: PyRef<PyCoords>,
    path: &str,
) -> PyResult<Bound<'py, PyDict>> {
    let groups = crate::ops::mesh::read_gmsh(coords.handle.clone(), std::path::Path::new(path))?;
    groups_to_dict(py, groups)
}

/// Like `pyrucast.mesh.read_gmsh`, from the `.msh` text held in a string.
///
/// # Parameters
///
/// - `coords`: where the nodes are added.
/// - `text`: the content of a `.msh` file.
///
/// # Returns
///
/// A `dict` from physical group name to `Mesh`, in the order of the text.
///
/// # Raises
///
/// `RuntimeError` if the text cannot be parsed, or for a gmsh element type
/// pyrucast does not know.
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn read_gmsh_str<'py>(
    py: Python<'py>,
    coords: PyRef<PyCoords>,
    text: &str,
) -> PyResult<Bound<'py, PyDict>> {
    let groups = crate::ops::mesh::read_gmsh_str(coords.handle.clone(), text)?;
    groups_to_dict(py, groups)
}

/// The part of the support of `field` whose values lie in a band, as a `Mesh`.
///
/// Zone by zone: a field at the nodes gives POI1 submeshes of the passing
/// **nodes**; a field at the Gauss points gives submeshes of the passing
/// **cells**, each of its zone's element type, a cell passing only when *all* its
/// Gauss points do. When several components are tested they combine with **AND**:
/// a node or a cell is kept only when *every* tested component is in the band. Its
/// sibling `pyrucast.node_field.mask` rewrites the values instead, keeping the
/// structure.
///
/// # Parameters
///
/// - `field`: a `NodeField`, `SubNodeField`, `ElementField` or `SubElementField`.
/// - `ge`, `gt`: the lower bound, `v ≥ ge` or `v > gt` — at most one of the two.
/// - `le`, `lt`: the upper bound, `v ≤ le` or `v < lt` — at most one of the two.
///   A bound left out leaves that side open; at least one bound is given.
/// - `components`: the components to test, only on the zones carrying **all** of
///   them — a zone missing one is skipped. `None` tests every component of each
///   zone.
///
/// # Returns
///
/// A `Mesh` with one submesh per zone processed.
///
/// # Raises
///
/// - `TypeError` if `field` is none of the four field types.
/// - `RuntimeError` if no bound is given, if both bounds of one side are, or if
///   the lower bound exceeds the upper one.
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (field, ge=None, gt=None, le=None, lt=None, components=None))]
#[allow(clippy::too_many_arguments)]
pub fn select(
    field: &Bound<'_, PyAny>,
    ge: Option<f64>,
    gt: Option<f64>,
    le: Option<f64>,
    lt: Option<f64>,
    components: Option<Vec<String>>,
) -> PyResult<PyMesh> {
    if let Ok(f) = field.extract::<PyRef<PyNodeField>>() {
        select_nodes(f, ge, gt, le, lt, components)
    } else if let Ok(f) = field.extract::<PyRef<PyElementField>>() {
        select_cells(f, ge, gt, le, lt, components)
    } else if let Ok(f) = field.extract::<PyRef<PySubNodeField>>() {
        select_sub_nodes(f, ge, gt, le, lt, components)
    } else if let Ok(f) = field.extract::<PyRef<PySubElementField>>() {
        select_sub_cells(f, ge, gt, le, lt, components)
    } else {
        Err(PyTypeError::new_err(
            "expected a NodeField, SubNodeField, ElementField or SubElementField",
        ))
    }
}

// The four flavours of `select`, each documented for its sole receiver. Not
// registered in the module — the free function above remains the polymorphic
// entry point — they give `#[py_op]` the method of each flavour.
// `#[pyfunction]` serves only to make valid the pyo3 signature that `#[py_op]`
// copies.

/// Select the **nodes** of this field passing a value band, zone by zone —
/// a value-range filter returning a `Mesh` with one POI1 submesh per
/// processed zone.
///
/// The band is set by the four comparison bounds — `ge` (`≥`), `gt` (`>`),
/// `le` (`≤`), `lt` (`<`); give at most one lower (`ge`/`gt`) and one upper
/// (`le`/`lt`), at least one overall. With several components in play they
/// are combined with **AND**: a node is kept only when *every* tested
/// component is in band.
///
/// `components=None` tests every component of each zone. A `components`
/// list tests **only** those components, and only on the zones carrying
/// **all** of them — a zone missing any listed component is skipped (no
/// submesh). Errors if no bound is given, or the lower one exceeds the upper.
#[py_op(method_on = PyNodeField, name = "select")]
#[pyfunction]
#[pyo3(signature = (field, ge=None, gt=None, le=None, lt=None, components=None))]
pub fn select_nodes(
    field: PyRef<PyNodeField>,
    ge: Option<f64>,
    gt: Option<f64>,
    le: Option<f64>,
    lt: Option<f64>,
    components: Option<Vec<String>>,
) -> PyResult<PyMesh> {
    let band = crate::atoms::Band::new(ge, gt, le, lt)?;
    Ok(PyMesh {
        inner: crate::ops::mesh::select_nodes(&field.inner, &band, components)?,
    })
}

/// Select the **cells** of this field passing a value band, zone by zone —
/// a value-range filter returning a `Mesh` with one submesh per processed
/// zone, each of its zone's element type. A cell passes only when *all* its
/// Gauss points do.
///
/// The band is set by the four comparison bounds — `ge` (`≥`), `gt` (`>`),
/// `le` (`≤`), `lt` (`<`); give at most one lower (`ge`/`gt`) and one upper
/// (`le`/`lt`), at least one overall. With several components in play they
/// are combined with **AND**: a cell is kept only when *every* tested
/// component is in band.
///
/// `components=None` tests every component of each zone. A `components`
/// list tests **only** those components, and only on the zones carrying
/// **all** of them — a zone missing any listed component is skipped (no
/// submesh). Errors if no bound is given, or the lower one exceeds the upper.
#[py_op(method_on = PyElementField, name = "select")]
#[pyfunction]
#[pyo3(signature = (field, ge=None, gt=None, le=None, lt=None, components=None))]
pub fn select_cells(
    field: PyRef<PyElementField>,
    ge: Option<f64>,
    gt: Option<f64>,
    le: Option<f64>,
    lt: Option<f64>,
    components: Option<Vec<String>>,
) -> PyResult<PyMesh> {
    let band = crate::atoms::Band::new(ge, gt, le, lt)?;
    Ok(PyMesh {
        inner: crate::ops::mesh::select_cells(&field.inner, &band, components)?,
    })
}

/// Select the **nodes** of this sub-field passing a value band — a
/// value-range filter returning a `Mesh` with a single POI1 submesh.
///
/// The band is set by the four comparison bounds — `ge` (`≥`), `gt` (`>`),
/// `le` (`≤`), `lt` (`<`); give at most one lower (`ge`/`gt`) and one upper
/// (`le`/`lt`), at least one overall. With several components in play they
/// are combined with **AND**: a node is kept only when *every* tested
/// component is in band.
///
/// `components=None` tests every component. A `components` list tests
/// **only** those components; if the sub-field lacks any of them, nothing is
/// tested and the result is an **empty** `Mesh`. Errors if no bound is given,
/// or the lower one exceeds the upper.
#[py_op(method_on = PySubNodeField, name = "select")]
#[pyfunction]
#[pyo3(signature = (field, ge=None, gt=None, le=None, lt=None, components=None))]
pub fn select_sub_nodes(
    field: PyRef<PySubNodeField>,
    ge: Option<f64>,
    gt: Option<f64>,
    le: Option<f64>,
    lt: Option<f64>,
    components: Option<Vec<String>>,
) -> PyResult<PyMesh> {
    let band = crate::atoms::Band::new(ge, gt, le, lt)?;
    Ok(PyMesh {
        inner: crate::ops::mesh::select_sub_nodes(&field.handle.read(), &band, components)?,
    })
}

/// Select the **cells** of this sub-field passing a value band — a
/// value-range filter returning a `Mesh` with a single submesh of the
/// sub-field's element type. A cell passes only when *all* its Gauss points
/// do.
///
/// The band is set by the four comparison bounds — `ge` (`≥`), `gt` (`>`),
/// `le` (`≤`), `lt` (`<`); give at most one lower (`ge`/`gt`) and one upper
/// (`le`/`lt`), at least one overall. With several components in play they
/// are combined with **AND**: a cell is kept only when *every* tested
/// component is in band.
///
/// `components=None` tests every component. A `components` list tests
/// **only** those components; if the sub-field lacks any of them, nothing is
/// tested and the result is an **empty** `Mesh`. Errors if no bound is given,
/// or the lower one exceeds the upper.
#[py_op(method_on = PySubElementField, name = "select")]
#[pyfunction]
#[pyo3(signature = (field, ge=None, gt=None, le=None, lt=None, components=None))]
pub fn select_sub_cells(
    field: PyRef<PySubElementField>,
    ge: Option<f64>,
    gt: Option<f64>,
    le: Option<f64>,
    lt: Option<f64>,
    components: Option<Vec<String>>,
) -> PyResult<PyMesh> {
    let band = crate::atoms::Band::new(ge, gt, le, lt)?;
    Ok(PyMesh {
        inner: crate::ops::mesh::select_sub_cells(&field.handle.read(), &band, components)?,
    })
}

// ─── Delegation methods ────────────────────────────────────────────────────
//
// The "subject" face of the operators above (`CONVENTIONS.md`, § "Le verbe
// exposé aussi en méthode"). They now come from `#[py_op]`, placed on the free
// function: the method is derived from its signature, and its documentation
// **copied** rather than pointed at — which is what makes it appear in full
// in `help()` as in the stub the IDEs read.
//
// This block therefore keeps only what breaks the mould. `merge_nodes` does:
// its receiver is a `Py<Self>` and its return a `Py<PyMesh>` — the object
// itself, not a borrowed view. `#[py_op]` emits only a `PyRef` receiver, and
// bending it for a single case would cost more than this very method.
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pymethods)]
#[pymethods]
impl PyMesh {
    /// Weld together the nodes of `mesh` closer than `tol`.
    ///
    /// The connectivity is redirected to one representative per cluster — the
    /// smallest-id node of the cluster, which keeps its own coordinates (no
    /// averaging). A cluster is a connected component of the relation "closer than
    /// `tol`": the weld propagates from node to node, so on a chain a—b—c where a and
    /// c are further apart than `tol`, all three still end up as one — which is why a
    /// `tol` of the order of the element size collapses a whole region rather than a
    /// seam. Every call prints a one-line tally on stdout once the weld is done —
    /// nodes welded, cells dropped, tolerance used.
    ///
    /// # Parameters
    ///
    /// - `mesh`: the mesh to weld.
    /// - `tol`: the distance, `≥ 0`; `0` welds only coincident nodes.
    /// - `in_place`: `False` builds a new mesh, where cells that collapse onto a
    ///   repeated node (a degenerate segment, triangle, …) are dropped. `True`
    ///   rewrites the connectivity of `mesh`'s **own** submeshes instead and returns
    ///   `mesh` itself. Since the union `mesh_a | mesh_b` shares its submeshes, welding
    ///   that union in place welds `mesh_a` and `mesh_b` themselves: afterwards they
    ///   share their interface nodes. The structure is kept (same submeshes, same
    ///   cells in the same order), so a cell that would collapse is an error, as is a
    ///   submesh already sealed by a finite-element space, a field or a matrix; both
    ///   are checked before anything is written.
    ///
    /// # Returns
    ///
    /// A new `Mesh` mirroring `mesh` (same submeshes, types and colours), or `mesh`
    /// itself with `in_place=True`.
    ///
    /// # Raises
    ///
    /// `RuntimeError` if `tol` is negative, or, with `in_place=True`, for a cell that
    /// would collapse or a sealed submesh.
    #[pyo3(signature = (tol, in_place=false))]
    fn merge_nodes(
        slf: Py<Self>,
        py: Python<'_>,
        tol: f64,
        in_place: bool,
    ) -> PyResult<Py<PyMesh>> {
        super::mesh::merge_nodes(py, slf, tol, in_place)
    }
}