csgrs 0.20.1

Constructive solid geometry (CSG) on meshes using BSP trees in Rust
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
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
use crate::errors::ValidationError;
use crate::float_types::{EPSILON, FRAC_PI_2, PI, Real};
use crate::mesh::Mesh;
use crate::mesh::bsp::Node;
use crate::mesh::plane::Plane;
use crate::mesh::polygon::Polygon;
use crate::mesh::vertex::{Vertex, VertexCluster};
use crate::sketch::Sketch;
use crate::traits::CSG;
use geo::{Area, Geometry, HasDimensions};
use hashbrown::HashMap;
use nalgebra::{Point3, Vector3};

// --------------------------------------------------------
//   Helpers
// --------------------------------------------------------

/// Returns the approximate bounding box `[min_x, min_y, min_z, max_x, max_y, max_z]`
/// for a set of polygons.
fn bounding_box(polygons: &[Polygon<()>]) -> [Real; 6] {
    let mut min_x = Real::MAX;
    let mut min_y = Real::MAX;
    let mut min_z = Real::MAX;
    let mut max_x = Real::MIN;
    let mut max_y = Real::MIN;
    let mut max_z = Real::MIN;

    for poly in polygons {
        for v in &poly.vertices {
            let p = v.pos;
            if p.x < min_x {
                min_x = p.x;
            }
            if p.y < min_y {
                min_y = p.y;
            }
            if p.z < min_z {
                min_z = p.z;
            }
            if p.x > max_x {
                max_x = p.x;
            }
            if p.y > max_y {
                max_y = p.y;
            }
            if p.z > max_z {
                max_z = p.z;
            }
        }
    }

    [min_x, min_y, min_z, max_x, max_y, max_z]
}

/// Quick helper to compare floating-point results with an acceptable tolerance.
fn approx_eq(a: Real, b: Real, eps: Real) -> bool {
    (a - b).abs() < eps
}

// --------------------------------------------------------
//   Vertex Tests
// --------------------------------------------------------

#[test]
fn test_vertex_flip() {
    let mut v = Vertex::new(Point3::new(1.0, 2.0, 3.0), Vector3::x());
    v.flip();
    // Position remains the same
    assert_eq!(v.pos, Point3::new(1.0, 2.0, 3.0));
    // Normal should be negated
    assert_eq!(v.normal, -Vector3::x());
}

// --------------------------------------------------------
//   Polygon Tests
// --------------------------------------------------------

#[test]
fn test_polygon_construction() {
    let v1 = Vertex::new(Point3::origin(), Vector3::y());
    let v2 = Vertex::new(Point3::new(1.0, 0.0, 1.0), Vector3::y());
    let v3 = Vertex::new(Point3::new(1.0, 0.0, -1.0), Vector3::y());

    let poly: Polygon<()> = Polygon::new(vec![v1.clone(), v2.clone(), v3.clone()], None);
    assert_eq!(poly.vertices.len(), 3);
    // Plane should be defined by these three points. We expect a normal near ±Y.
    assert!(
        approx_eq(poly.plane.normal().dot(&Vector3::y()).abs(), 1.0, 1e-8),
        "Expected plane normal to match ±Y"
    );
}

// --------------------------------------------------------
//   CSG: STL Export
// --------------------------------------------------------

#[test]
#[cfg(feature = "stl-io")]
fn test_to_stl_ascii() {
    let cube: Mesh<()> = Mesh::cube(2.0, None);
    let stl_str = cube.to_stl_ascii("test_cube");
    // Basic checks
    assert!(stl_str.contains("solid test_cube"));
    assert!(stl_str.contains("endsolid test_cube"));

    // Should contain some facet normals
    assert!(stl_str.contains("facet normal"));
    // Should contain some vertex lines
    assert!(stl_str.contains("vertex"));
}

// --------------------------------------------------------
//   Node & Clipping Tests
//   (Optional: these get more into internal details)
// --------------------------------------------------------

#[test]
fn test_degenerate_polygon_after_clipping() {
    let vertices = vec![
        Vertex::new(Point3::origin(), Vector3::y()),
        Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::y()),
        Vertex::new(Point3::new(0.5, 1.0, 0.0), Vector3::y()),
    ];

    let polygon: Polygon<()> = Polygon::new(vertices.clone(), None);
    let plane = Plane::from_normal(Vector3::new(0.0, 0.0, 1.0), 0.0);

    eprintln!("Original polygon: {:?}", polygon);
    eprintln!("Clipping plane: {:?}", plane);

    let (_coplanar_front, _coplanar_back, front, back) = plane.split_polygon(&polygon);

    eprintln!("Front polygons: {:?}", front);
    eprintln!("Back polygons: {:?}", back);

    assert!(front.is_empty(), "Front should be empty for this test");
    assert!(back.is_empty(), "Back should be empty for this test");
}

#[test]
fn test_valid_polygon_clipping() {
    let vertices = vec![
        Vertex::new(Point3::origin(), Vector3::y()),
        Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::y()),
        Vertex::new(Point3::new(0.5, 1.0, 0.0), Vector3::y()),
    ];

    let polygon: Polygon<()> = Polygon::new(vertices, None);

    let plane = Plane::from_normal(-Vector3::y(), -0.5);

    eprintln!("Polygon before clipping: {:?}", polygon);
    eprintln!("Clipping plane: {:?}", plane);

    let (_coplanar_front, _coplanar_back, front, back) = plane.split_polygon(&polygon);

    eprintln!("Front polygons: {:?}", front);
    eprintln!("Back polygons: {:?}", back);

    assert!(!front.is_empty(), "Front should not be empty");
    assert!(!back.is_empty(), "Back should not be empty");
}

// ------------------------------------------------------------
// Vertex tests
// ------------------------------------------------------------
#[test]
fn test_vertex_new() {
    let pos = Point3::new(1.0, 2.0, 3.0);
    let normal = Vector3::new(0.0, 1.0, 0.0);
    let v = Vertex::new(pos, normal);
    assert_eq!(v.pos, pos);
    assert_eq!(v.normal, normal);
}

#[test]
fn test_vertex_interpolate() {
    let v1 = Vertex::new(Point3::origin(), Vector3::x());
    let v2 = Vertex::new(Point3::new(2.0, 2.0, 2.0), Vector3::y());
    let v_mid = v1.interpolate(&v2, 0.5);
    assert!(approx_eq(v_mid.pos.x, 1.0, EPSILON));
    assert!(approx_eq(v_mid.pos.y, 1.0, EPSILON));
    assert!(approx_eq(v_mid.pos.z, 1.0, EPSILON));
    assert!(approx_eq(v_mid.normal.x, 0.5, EPSILON));
    assert!(approx_eq(v_mid.normal.y, 0.5, EPSILON));
    assert!(approx_eq(v_mid.normal.z, 0.0, EPSILON));
}

// ------------------------------------------------------------
// Plane tests
// ------------------------------------------------------------
#[test]
fn test_plane_flip() {
    let mut plane = Plane::from_normal(Vector3::y(), 2.0);
    plane.flip();
    assert_eq!(plane.normal(), Vector3::new(0.0, -1.0, 0.0));
    assert_eq!(plane.offset(), -2.0);
}

#[test]
fn test_plane_split_polygon() {
    // Define a plane that splits the XY plane at y=0
    let plane = Plane::from_normal(Vector3::new(0.0, 1.0, 0.0), 0.0);

    // A polygon that crosses y=0 line: a square from ( -1, -1 ) to (1, 1 )
    let poly: Polygon<()> = Polygon::new(
        vec![
            Vertex::new(Point3::new(-1.0, -1.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(1.0, -1.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(1.0, 1.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(-1.0, 1.0, 0.0), Vector3::z()),
        ],
        None,
    );

    let (cf, cb, f, b) = plane.split_polygon(&poly);
    // This polygon is spanning across y=0 plane => we expect no coplanar, but front/back polygons.
    assert_eq!(cf.len(), 0);
    assert_eq!(cb.len(), 0);
    assert_eq!(f.len(), 1);
    assert_eq!(b.len(), 1);

    // Check that each part has at least 3 vertices and is "above" or "below" the plane
    // in rough terms
    let front_poly = &f[0];
    let back_poly = &b[0];
    assert!(front_poly.vertices.len() >= 3);
    assert!(back_poly.vertices.len() >= 3);

    // Quick check: all front vertices should have y >= 0 (within an epsilon).
    for v in &front_poly.vertices {
        assert!(v.pos.y >= -EPSILON);
    }
    // All back vertices should have y <= 0 (within an epsilon).
    for v in &back_poly.vertices {
        assert!(v.pos.y <= EPSILON);
    }
}

// ------------------------------------------------------------
// Polygon tests
// ------------------------------------------------------------
#[test]
fn test_polygon_new() {
    let vertices = vec![
        Vertex::new(Point3::origin(), Vector3::z()),
        Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::z()),
        Vertex::new(Point3::new(0.0, 1.0, 0.0), Vector3::z()),
    ];
    let poly: Polygon<()> = Polygon::new(vertices.clone(), None);
    assert_eq!(poly.vertices.len(), 3);
    assert_eq!(poly.metadata, None);
    // Plane normal should be +Z for the above points
    assert!(approx_eq(poly.plane.normal().x, 0.0, EPSILON));
    assert!(approx_eq(poly.plane.normal().y, 0.0, EPSILON));
    assert!(approx_eq(poly.plane.normal().z, 1.0, EPSILON));
}

#[test]
fn test_polygon_flip() {
    let mut poly: Polygon<()> = Polygon::new(
        vec![
            Vertex::new(Point3::origin(), Vector3::z()),
            Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(0.0, 1.0, 0.0), Vector3::z()),
        ],
        None,
    );
    let plane_normal_before = poly.plane.normal();
    poly.flip();
    // The vertices should be reversed, and normal flipped
    assert_eq!(poly.vertices.len(), 3);
    assert!(approx_eq(
        poly.plane.normal().x,
        -plane_normal_before.x,
        EPSILON
    ));
    assert!(approx_eq(
        poly.plane.normal().y,
        -plane_normal_before.y,
        EPSILON
    ));
    assert!(approx_eq(
        poly.plane.normal().z,
        -plane_normal_before.z,
        EPSILON
    ));
}

#[test]
fn test_polygon_triangulate() {
    // A quad:
    let poly: Polygon<()> = Polygon::new(
        vec![
            Vertex::new(Point3::origin(), Vector3::z()),
            Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(1.0, 1.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(0.0, 1.0, 0.0), Vector3::z()),
        ],
        None,
    );
    let triangles = poly.triangulate();
    // We expect 2 triangles from a quad
    assert_eq!(
        triangles.len(),
        2,
        "A quad should triangulate into 2 triangles"
    );
}

#[test]
fn test_polygon_subdivide_triangles() {
    // A single triangle (level=1 should produce 4 sub-triangles)
    let poly: Polygon<()> = Polygon::new(
        vec![
            Vertex::new(Point3::origin(), Vector3::z()),
            Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(0.0, 1.0, 0.0), Vector3::z()),
        ],
        None,
    );
    let subs = poly.subdivide_triangles(1.try_into().expect("not 0"));
    // One triangle subdivided once => 4 smaller triangles
    assert_eq!(subs.len(), 4);

    // If we subdivide the same single tri 2 levels, we expect 16 sub-triangles.
    let subs2 = poly.subdivide_triangles(2.try_into().expect("not 0"));
    assert_eq!(subs2.len(), 16);
}

#[test]
fn test_polygon_recalc_plane_and_normals() {
    let mut poly: Polygon<()> = Polygon::new(
        vec![
            Vertex::new(Point3::origin(), Vector3::zeros()),
            Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::zeros()),
            Vertex::new(Point3::new(0.0, 1.0, 0.0), Vector3::zeros()),
        ],
        None,
    );
    poly.set_new_normal();
    assert!(approx_eq(poly.plane.normal().z, 1.0, EPSILON));
    for v in &poly.vertices {
        assert!(approx_eq(v.normal.x, 0.0, EPSILON));
        assert!(approx_eq(v.normal.y, 0.0, EPSILON));
        assert!(approx_eq(v.normal.z, 1.0, EPSILON));
    }
}

// ------------------------------------------------------------
// Node tests
// ------------------------------------------------------------
#[test]
fn test_node_new_and_build() {
    // A simple triangle:
    let p: Polygon<()> = Polygon::new(
        vec![
            Vertex::new(Point3::origin(), Vector3::z()),
            Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(0.0, 1.0, 0.0), Vector3::z()),
        ],
        None,
    );
    let node: Node<()> = Node::from_polygons(&[p.clone()]);
    // The node should have built a tree with plane = p.plane, polygons = [p], no front/back children
    assert!(node.plane.is_some());
    assert_eq!(node.polygons.len(), 1);
    assert!(node.front.is_none());
    assert!(node.back.is_none());
}

#[test]
fn test_node_invert() {
    let p: Polygon<()> = Polygon::new(
        vec![
            Vertex::new(Point3::origin(), Vector3::z()),
            Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(0.0, 1.0, 0.0), Vector3::z()),
        ],
        None,
    );
    let mut node: Node<()> = Node::from_polygons(&[p.clone()]);
    let original_count = node.polygons.len();
    let original_normal = node.plane.as_ref().unwrap().normal();
    node.invert();
    // The plane normal should be flipped, polygons should be flipped, and front/back swapped (they were None).
    let flipped_normal = node.plane.as_ref().unwrap().normal();
    assert!(approx_eq(flipped_normal.x, -original_normal.x, EPSILON));
    assert!(approx_eq(flipped_normal.y, -original_normal.y, EPSILON));
    assert!(approx_eq(flipped_normal.z, -original_normal.z, EPSILON));
    // We shouldn't lose polygons by inverting
    assert_eq!(node.polygons.len(), original_count);
    // If we invert back, we should get the same geometry
    node.invert();
    assert_eq!(node.polygons.len(), original_count);
}

#[test]
fn test_node_clip_polygons2() {
    // A node with a single plane normal to +Z, passing through z=0
    let plane = Plane::from_normal(Vector3::z(), 0.0);
    let mut node: Node<()> = Node {
        plane: Some(plane),
        front: None,
        back: None,
        polygons: Vec::new(),
    };
    // Build the node with some polygons
    // We'll put a polygon in the plane exactly (z=0) and one above, one below
    let poly_in_plane: Polygon<()> = Polygon::new(
        vec![
            Vertex::new(Point3::origin(), Vector3::z()),
            Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(0.0, 1.0, 0.0), Vector3::z()),
        ],
        None,
    );
    let poly_above: Polygon<()> = Polygon::new(
        vec![
            Vertex::new(Point3::new(0.0, 0.0, 1.0), Vector3::z()),
            Vertex::new(Point3::new(1.0, 0.0, 1.0), Vector3::z()),
            Vertex::new(Point3::new(0.0, 1.0, 1.0), Vector3::z()),
        ],
        None,
    );
    let poly_below: Polygon<()> = Polygon::new(
        vec![
            Vertex::new(Point3::new(0.0, 0.0, -1.0), Vector3::z()),
            Vertex::new(Point3::new(1.0, 0.0, -1.0), Vector3::z()),
            Vertex::new(Point3::new(0.0, 1.0, -1.0), Vector3::z()),
        ],
        None,
    );

    node.build(&[
        poly_in_plane.clone(),
        poly_above.clone(),
        poly_below.clone(),
    ]);
    // Now node has polygons: [poly_in_plane], front child with poly_above, back child with poly_below

    // Clip a polygon that crosses from z=-0.5 to z=0.5
    let crossing_poly: Polygon<()> = Polygon::new(
        vec![
            Vertex::new(Point3::new(-1.0, -1.0, -0.5), Vector3::z()),
            Vertex::new(Point3::new(2.0, -1.0, 0.5), Vector3::z()),
            Vertex::new(Point3::new(-1.0, 2.0, 0.5), Vector3::z()),
        ],
        None,
    );
    let clipped = node.clip_polygons(&[crossing_poly.clone()]);
    // The crossing polygon should be clipped against z=0 plane and any sub-planes from front/back nodes
    // For a single-plane node, we expect either one or two polygons left (front part & back part).
    // But we built subtrees, so let's just check if clipped is not empty.
    assert!(!clipped.is_empty());
}

#[test]
fn test_node_clip_to() {
    // Basic test: if we clip a node to another that encloses it fully, we keep everything
    let poly: Polygon<()> = Polygon::new(
        vec![
            Vertex::new(Point3::new(-0.5, -0.5, 0.0), Vector3::z()),
            Vertex::new(Point3::new(0.5, -0.5, 0.0), Vector3::z()),
            Vertex::new(Point3::new(0.0, 0.5, 0.0), Vector3::z()),
        ],
        None,
    );
    let mut node_a: Node<()> = Node::from_polygons(&[poly]);
    // Another polygon that fully encloses the above
    let big_poly: Polygon<()> = Polygon::new(
        vec![
            Vertex::new(Point3::new(-1.0, -1.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(1.0, -1.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(1.0, 1.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(-1.0, 1.0, 0.0), Vector3::z()),
        ],
        None,
    );
    let node_b: Node<()> = Node::from_polygons(&[big_poly]);
    node_a.clip_to(&node_b);
    // We expect nodeA's polygon to be present
    let all_a = node_a.all_polygons();
    assert_eq!(all_a.len(), 1);
}

#[test]
fn test_node_all_polygons() {
    // Build a node with multiple polygons
    let poly1: Polygon<()> = Polygon::new(
        vec![
            Vertex::new(Point3::origin(), Vector3::z()),
            Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(0.0, 1.0, 0.0), Vector3::z()),
        ],
        None,
    );
    let poly2: Polygon<()> = Polygon::new(
        vec![
            Vertex::new(Point3::new(0.0, 0.0, 1.0), Vector3::z()),
            Vertex::new(Point3::new(1.0, 0.0, 1.0), Vector3::z()),
            Vertex::new(Point3::new(0.0, 1.0, 1.0), Vector3::z()),
        ],
        None,
    );

    let node: Node<()> = Node::from_polygons(&[poly1.clone(), poly2.clone()]);
    let all_polys = node.all_polygons();
    // We expect to retrieve both polygons
    assert_eq!(all_polys.len(), 2);
}

// ------------------------------------------------------------
// CSG tests
// ------------------------------------------------------------
#[test]
fn test_csg_from_polygons_and_to_polygons() {
    let poly: Polygon<()> = Polygon::new(
        vec![
            Vertex::new(Point3::origin(), Vector3::z()),
            Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(0.0, 1.0, 0.0), Vector3::z()),
        ],
        None,
    );
    let csg: Mesh<()> = Mesh::from_polygons(&[poly.clone()], None);
    assert_eq!(csg.polygons.len(), 1);
    assert_eq!(csg.polygons[0].vertices.len(), 3);
}

#[test]
fn test_csg_union() {
    let cube1: Mesh<()> = Mesh::cube(2.0, None).translate(-1.0, -1.0, -1.0); // from -1 to +1 in all coords
    let cube2: Mesh<()> = Mesh::cube(1.0, None).translate(0.5, 0.5, 0.5);

    let union_csg = cube1.union(&cube2);
    assert!(
        !union_csg.polygons.is_empty(),
        "Union of two cubes should produce polygons"
    );

    // Check bounding box => should now at least range from -1 to (0.5+1) = 1.5
    let bb = bounding_box(&union_csg.polygons);
    assert!(approx_eq(bb[0], -1.0, 1e-8));
    assert!(approx_eq(bb[1], -1.0, 1e-8));
    assert!(approx_eq(bb[2], -1.0, 1e-8));
    assert!(approx_eq(bb[3], 1.5, 1e-8));
    assert!(approx_eq(bb[4], 1.5, 1e-8));
    assert!(approx_eq(bb[5], 1.5, 1e-8));
}

#[test]
fn test_csg_difference() {
    // Subtract a smaller cube from a bigger one
    let big_cube: Mesh<()> = Mesh::cube(4.0, None).translate(-2.0, -2.0, -2.0); // radius=2 => spans [-2,2]
    let small_cube: Mesh<()> = Mesh::cube(2.0, None).translate(-1.0, -1.0, -1.0); // radius=1 => spans [-1,1]

    let result = big_cube.difference(&small_cube);
    assert!(
        !result.polygons.is_empty(),
        "Subtracting a smaller cube should leave polygons"
    );

    // Check bounding box => should still be [-2,-2,-2, 2,2,2], but with a chunk removed
    let bb = bounding_box(&result.polygons);
    // At least the bounding box remains the same
    assert!(approx_eq(bb[0], -2.0, 1e-8));
    assert!(approx_eq(bb[3], 2.0, 1e-8));
}

#[test]
fn test_csg_union2() {
    let c1: Mesh<()> = Mesh::cube(2.0, None); // cube from (-1..+1) if that's how you set radius=1 by default
    let c2: Mesh<()> = Mesh::sphere(1.0, 16, 8, None); // default sphere radius=1
    let unioned = c1.union(&c2);
    // We can check bounding box is bigger or at least not smaller than either shape's box
    let bb_union = unioned.bounding_box();
    let bb_cube = c1.bounding_box();
    let bb_sphere = c2.bounding_box();
    assert!(bb_union.mins.x <= bb_cube.mins.x.min(bb_sphere.mins.x));
    assert!(bb_union.maxs.x >= bb_cube.maxs.x.max(bb_sphere.maxs.x));
}

#[test]
fn test_csg_intersect() {
    let c1: Mesh<()> = Mesh::cube(2.0, None);
    let c2: Mesh<()> = Mesh::sphere(1.0, 16, 8, None);
    let isect = c1.intersection(&c2);
    let bb_isect = isect.bounding_box();
    // The intersection bounding box should be smaller than or equal to each
    let bb_cube = c1.bounding_box();
    let bb_sphere = c2.bounding_box();
    assert!(bb_isect.mins.x >= bb_cube.mins.x - EPSILON);
    assert!(bb_isect.mins.x >= bb_sphere.mins.x - EPSILON);
    assert!(bb_isect.maxs.x <= bb_cube.maxs.x + EPSILON);
    assert!(bb_isect.maxs.x <= bb_sphere.maxs.x + EPSILON);
}

#[test]
fn test_csg_intersect2() {
    let sphere: Mesh<()> = Mesh::sphere(1.0, 16, 8, None);
    let cube: Mesh<()> = Mesh::cube(2.0, None);

    let intersection = sphere.intersection(&cube);
    assert!(
        !intersection.polygons.is_empty(),
        "Sphere ∩ Cube should produce the portion of the sphere inside the cube"
    );

    // Check bounding box => intersection is roughly a sphere clipped to [-1,1]^3
    let bb = bounding_box(&intersection.polygons);
    // Should be a region inside the [-1,1] box
    for &val in &bb[..3] {
        assert!(val >= -1.0 - 1e-1);
    }
    for &val in &bb[3..] {
        assert!(val <= 1.0 + 1e-1);
    }
}

#[test]
fn test_csg_inverse() {
    let c1: Mesh<()> = Mesh::cube(2.0, None);
    let inv = c1.inverse();
    // The polygons are flipped
    // We can check just that the polygon planes are reversed, etc.
    // A full check might compare the polygons, but let's do a quick check on one polygon.
    let orig_poly = &c1.polygons[0];
    let inv_poly = &inv.polygons[0];
    assert!(approx_eq(
        orig_poly.plane.normal().x,
        -inv_poly.plane.normal().x,
        EPSILON
    ));
    assert!(approx_eq(
        orig_poly.plane.normal().y,
        -inv_poly.plane.normal().y,
        EPSILON
    ));
    assert!(approx_eq(
        orig_poly.plane.normal().z,
        -inv_poly.plane.normal().z,
        EPSILON
    ));
    assert_eq!(
        c1.polygons.len(),
        inv.polygons.len(),
        "Inverse should keep the same polygon count, but flip them"
    );
}

#[test]
fn test_csg_cube() {
    let c: Mesh<()> = Mesh::cube(2.0, None);
    // By default, corner at (0,0,0)
    // We expect 6 faces, each 4 vertices = 6 polygons
    assert_eq!(c.polygons.len(), 6);
    // Check bounding box
    let bb = c.bounding_box();
    assert!(approx_eq(bb.mins.x, 0.0, EPSILON));
    assert!(approx_eq(bb.maxs.x, 2.0, EPSILON));
}

// --------------------------------------------------------
//   CSG: Basic Shape Generation
// --------------------------------------------------------

#[test]
fn test_csg_sphere() {
    // Default sphere => radius=1, slices=16, stacks=8
    let sphere: Mesh<()> = Mesh::sphere(1.0, 16, 8, None);
    assert!(!sphere.polygons.is_empty(), "Sphere should generate polygons");

    let bb = bounding_box(&sphere.polygons);
    // Should roughly be [-1, -1, -1, 1, 1, 1]
    assert!(approx_eq(bb[0], -1.0, 1e-1));
    assert!(approx_eq(bb[1], -1.0, 1e-1));
    assert!(approx_eq(bb[2], -1.0, 1e-1));
    assert!(approx_eq(bb[3], 1.0, 1e-1));
    assert!(approx_eq(bb[4], 1.0, 1e-1));
    assert!(approx_eq(bb[5], 1.0, 1e-1));

    // We expect 16 * 8 polygons = 128 polygons
    // each stack band is 16 polys, times 8 => 128.
    assert_eq!(sphere.polygons.len(), 16 * 8);
}

#[test]
fn test_csg_cylinder() {
    // Default cylinder => from (0,0,0) to (0,2,0) with radius=1
    let cylinder: Mesh<()> = Mesh::cylinder(1.0, 2.0, 16, None);
    assert!(
        !cylinder.polygons.is_empty(),
        "Cylinder should generate polygons"
    );

    let bb = bounding_box(&cylinder.polygons);
    // Expect x in [-1,1], y in [-1,1], z in [-1,1].
    assert!(approx_eq(bb[0], -1.0, 1e-8), "min X");
    assert!(approx_eq(bb[1], -1.0, 1e-8), "min Y");
    assert!(approx_eq(bb[2], 0.0, 1e-8), "min Z");
    assert!(approx_eq(bb[3], 1.0, 1e-8), "max X");
    assert!(approx_eq(bb[4], 1.0, 1e-8), "max Y");
    assert!(approx_eq(bb[5], 2.0, 1e-8), "max Z");

    // We have slices = 16, plus 16*2 polygons for the end caps
    assert_eq!(cylinder.polygons.len(), 48);
}

#[test]
fn test_csg_polyhedron() {
    // A simple tetrahedron
    let pts = &[
        [0.0, 0.0, 0.0], // 0
        [1.0, 0.0, 0.0], // 1
        [0.0, 1.0, 0.0], // 2
        [0.0, 0.0, 1.0], // 3
    ];
    let faces: [&[usize]; 4] = [&[0, 1, 2], &[0, 1, 3], &[1, 2, 3], &[2, 0, 3]];
    let csg_tetra: Mesh<()> = Mesh::polyhedron(pts, &faces, None).unwrap();
    // We should have exactly 4 triangular faces
    assert_eq!(csg_tetra.polygons.len(), 4);
}

#[test]
fn test_csg_transform_translate_rotate_scale() {
    let c: Mesh<()> = Mesh::cube(2.0, None).center();
    let translated = c.translate(1.0, 2.0, 3.0);
    let rotated = c.rotate(90.0, 0.0, 0.0); // 90 deg about X
    let scaled = c.scale(2.0, 1.0, 1.0);

    // Quick bounding box checks
    let bb_t = translated.bounding_box();
    assert!(approx_eq(bb_t.mins.x, -1.0 + 1.0, EPSILON));
    assert!(approx_eq(bb_t.mins.y, -1.0 + 2.0, EPSILON));
    assert!(approx_eq(bb_t.mins.z, -1.0 + 3.0, EPSILON));

    let bb_s = scaled.bounding_box();
    assert!(approx_eq(bb_s.mins.x, -2.0, EPSILON)); // scaled by 2 in X
    assert!(approx_eq(bb_s.maxs.x, 2.0, EPSILON));
    assert!(approx_eq(bb_s.mins.y, -1.0, EPSILON));
    assert!(approx_eq(bb_s.maxs.y, 1.0, EPSILON));

    // For rotated, let's just check one polygon's vertices to see if z got mapped to y, etc.
    // (A thorough check would be more geometry-based.)
    let poly0 = &rotated.polygons[0];
    for v in &poly0.vertices {
        // After a 90° rotation around X, the old Y should become old Z,
        // and the old Z should become -old Y.
        // We can't trivially guess each vertex's new coordinate but can do a sanity check:
        // The bounding box in Y might be [-1..1], but let's check we have differences in Y from original.
        assert_ne!(v.pos.y, 0.0); // Expect something was changed if originally it was ±1 in Z
    }
}

#[test]
fn test_csg_mirror() {
    let c: Mesh<()> = Mesh::cube(2.0, None);
    let plane_x = Plane::from_normal(Vector3::x(), 0.0); // x=0 plane
    let mirror_x = c.mirror(plane_x);
    let bb_mx = mirror_x.bounding_box();
    // The original cube was from x=0..2, so mirrored across X=0 should be -2..0
    assert!(approx_eq(bb_mx.mins.x, -2.0, EPSILON));
    assert!(approx_eq(bb_mx.maxs.x, 0.0, EPSILON));
}

#[test]
#[cfg(feature = "chull-io")]
fn test_csg_convex_hull() {
    // If we take a shape with some random points, the hull should just enclose them
    let c1: Mesh<()> = Mesh::sphere(1.0, 16, 8, None);
    // The convex_hull of a sphere's sampling is basically that same shape, but let's see if it runs.
    let hull = c1.convex_hull();
    // The hull should have some polygons
    assert!(!hull.polygons.is_empty());
}

#[test]
#[cfg(feature = "chull-io")]
fn test_csg_minkowski_sum() {
    // Minkowski sum of two cubes => bigger cube offset by edges
    let c1: Mesh<()> = Mesh::cube(2.0, None).center();
    let c2: Mesh<()> = Mesh::cube(1.0, None).center();
    let sum = c1.minkowski_sum(&c2);
    let bb_sum = sum.bounding_box();
    // Expect bounding box from -1.5..+1.5 in each axis if both cubes were centered at (0,0,0).
    assert!(approx_eq(bb_sum.mins.x, -1.5, 0.01));
    assert!(approx_eq(bb_sum.maxs.x, 1.5, 0.01));
}

#[test]
fn test_csg_subdivide_triangles() {
    let cube: Mesh<()> = Mesh::cube(2.0, None);
    // subdivide_triangles(1) => each polygon (quad) is triangulated => 2 triangles => each tri subdivides => 4
    // So each face with 4 vertices => 2 triangles => each becomes 4 => total 8 per face => 6 faces => 48
    let subdiv = cube.subdivide_triangles(1.try_into().expect("not 0"));
    assert_eq!(subdiv.polygons.len(), 6 * 8);
}

#[test]
fn test_csg_renormalize() {
    let mut cube: Mesh<()> = Mesh::cube(2.0, None);
    // After we do some transforms, normals might be changed. We can artificially change them:
    for poly in &mut cube.polygons {
        for v in &mut poly.vertices {
            v.normal = Vector3::x(); // just set to something
        }
    }
    cube.renormalize();
    // Now each polygon's vertices should match the plane's normal
    for poly in &cube.polygons {
        for v in &poly.vertices {
            assert!(approx_eq(v.normal.x, poly.plane.normal().x, EPSILON));
            assert!(approx_eq(v.normal.y, poly.plane.normal().y, EPSILON));
            assert!(approx_eq(v.normal.z, poly.plane.normal().z, EPSILON));
        }
    }
}

#[test]
fn test_csg_ray_intersections() {
    let cube: Mesh<()> = Mesh::cube(2.0, None).center();
    // Ray from (-2,0,0) toward +X
    let origin = Point3::new(-2.0, 0.0, 0.0);
    let direction = Vector3::new(1.0, 0.0, 0.0);
    let hits = cube.ray_intersections(&origin, &direction);
    // Expect 2 intersections with the cube's side at x=-1 and x=1
    assert_eq!(hits.len(), 2);
    // The distances should be 1 unit from -2.0 -> -1 => t=1, and from -2.0 -> +1 => t=3
    assert!(approx_eq(hits[0].1, 1.0, EPSILON));
    assert!(approx_eq(hits[1].1, 3.0, EPSILON));
}

#[test]
fn test_csg_square() {
    let sq: Sketch<()> = Sketch::square(2.0, None);
    let mesh_2d: Mesh<()> = sq.into();
    // Single polygon, 4 vertices
    assert_eq!(mesh_2d.polygons.len(), 1);
    let poly = &mesh_2d.polygons[0];
    assert!(
        matches!(poly.vertices.len(), 4 | 5),
        "Expected 4 or 5 vertices, got {}",
        poly.vertices.len()
    );
}

#[test]
fn test_csg_circle() {
    let circle: Sketch<()> = Sketch::circle(2.0, 32, None);
    let mesh_2d: Mesh<()> = circle.into();
    // Single polygon with 32 segments => 32 or 33 vertices if closed
    assert_eq!(mesh_2d.polygons.len(), 1);
    let poly = &mesh_2d.polygons[0];
    assert!(
        matches!(poly.vertices.len(), 32 | 33),
        "Expected 32 or 33 vertices, got {}",
        poly.vertices.len()
    );
}

#[test]
fn test_csg_extrude() {
    let sq: Sketch<()> = Sketch::square(2.0, None);
    let extruded = sq.extrude(5.0);
    // We expect:
    //   bottom polygon: 2 (square triangulated)
    //   top polygon 2 (square triangulated)
    //   side polygons: 4 for a square (one per edge)
    // => total 8 polygons
    assert_eq!(extruded.polygons.len(), 8);
    // Check bounding box
    let bb = extruded.bounding_box();
    assert!(approx_eq(bb.mins.z, 0.0, EPSILON));
    assert!(approx_eq(bb.maxs.z, 5.0, EPSILON));
}

#[test]
fn test_csg_revolve() {
    // Default square is from (0,0) to (1,1) in XY.
    // Shift it so it's from (1,0) to (2,1) — i.e. at least 1.0 unit away from the Z-axis.
    // and rotate it 90 degrees so that it can be swept around Z
    let square: Sketch<()> = Sketch::square(2.0, None)
        .translate(1.0, 0.0, 0.0)
        .rotate(90.0, 0.0, 0.0);

    // Now revolve this translated square around the Z-axis, 360° in 16 segments.
    let revolve = square.revolve(360.0, 16).unwrap();

    // We expect a ring-like “tube” instead of a degenerate shape.
    assert!(!revolve.polygons.is_empty());
}

#[test]
fn test_csg_bounding_box() {
    let sphere: Mesh<()> = Mesh::sphere(1.0, 16, 8, None);
    let bb = sphere.bounding_box();
    // center=(2,-1,3), radius=2 => bounding box min=(0,-3,1), max=(4,1,5)
    assert!(approx_eq(bb.mins.x, -1.0, 0.1));
    assert!(approx_eq(bb.mins.y, -1.0, 0.1));
    assert!(approx_eq(bb.mins.z, -1.0, 0.1));
    assert!(approx_eq(bb.maxs.x, 1.0, 0.1));
    assert!(approx_eq(bb.maxs.y, 1.0, 0.1));
    assert!(approx_eq(bb.maxs.z, 1.0, 0.1));
}

#[test]
fn test_csg_vertices() {
    let cube: Mesh<()> = Mesh::cube(2.0, None);
    let verts = cube.vertices();
    // 6 faces x 4 vertices each = 24
    assert_eq!(verts.len(), 24);
}

#[test]
#[cfg(feature = "offset")]
fn test_csg_offset_2d() {
    let square: Sketch<()> = Sketch::square(2.0, None);
    let grown = square.offset(0.5);
    let shrunk = square.offset(-0.5);
    let bb_square = square.bounding_box();
    let bb_grown = grown.bounding_box();
    let bb_shrunk = shrunk.bounding_box();

    println!("Square bb: {:#?}", bb_square);
    println!("Grown bb: {:#?}", bb_grown);
    println!("Shrunk bb: {:#?}", bb_shrunk);

    // Should be bigger
    assert!(bb_grown.maxs.x > bb_square.maxs.x + 0.4);

    // Should be smaller
    assert!(bb_shrunk.maxs.x < bb_square.maxs.x + 0.1);
}

#[cfg(feature = "truetype-text")]
#[test]
fn test_csg_text() {
    // We can't easily test visually, but we can at least test that it doesn't panic
    // and returns some polygons for normal ASCII letters.
    let font_data = include_bytes!("../asar.ttf");
    let text_csg: Sketch<()> = Sketch::text("ABC", font_data, 10.0, None);
    assert!(!text_csg.geometry.is_empty());
}

#[test]
fn test_csg_to_trimesh() {
    let cube: Mesh<()> = Mesh::cube(2.0, None);
    let shape = cube.to_trimesh();
    // Should be a TriMesh with 12 triangles
    if let Some(trimesh) = shape {
        assert_eq!(trimesh.indices().len(), 12); // 6 faces => 2 triangles each => 12
    } else {
        panic!("Expected a TriMesh");
    }
}

#[test]
fn test_csg_mass_properties() {
    let cube: Mesh<()> = Mesh::cube(2.0, None).center(); // side=2 => volume=8. If density=1 => mass=8
    let (mass, com, _frame) = cube.mass_properties(1.0);
    println!("{:#?}", mass);
    // For a centered cube with side 2, volume=8 => mass=8 => COM=(0,0,0)
    assert!(approx_eq(mass, 8.0, 0.1));
    assert!(approx_eq(com.x, 0.0, 0.001));
    assert!(approx_eq(com.y, 0.0, 0.001));
    assert!(approx_eq(com.z, 0.0, 0.001));
}

#[test]
fn test_csg_to_rigid_body() {
    use crate::float_types::rapier3d::prelude::*;
    let cube: Mesh<()> = Mesh::cube(2.0, None);
    let mut rb_set = RigidBodySet::new();
    let mut co_set = ColliderSet::new();
    let handle = cube.to_rigid_body(
        &mut rb_set,
        &mut co_set,
        Vector3::new(10.0, 0.0, 0.0),
        Vector3::new(0.0, 0.0, FRAC_PI_2), // 90 deg around Z
        1.0,
    );
    let rb = rb_set.get(handle).unwrap();
    let pos = rb.translation();
    assert!(approx_eq(pos.x, 10.0, EPSILON));
}

#[test]
#[cfg(feature = "stl-io")]
fn test_csg_to_stl_and_from_stl_file() -> Result<(), Box<dyn std::error::Error>> {
    // We'll create a small shape, write to an STL, read it back.
    // You can redirect to a temp file or do an in-memory test.
    let tmp_path = "test_csg_output.stl";

    let cube: Mesh<()> = Mesh::cube(2.0, None);
    let res = cube.to_stl_binary("A cube");
    let _ = std::fs::write(tmp_path, res.as_ref().unwrap());
    assert!(res.is_ok());

    let stl_data: Vec<u8> = std::fs::read(tmp_path)?;
    let csg_in: Mesh<()> = Mesh::from_stl(&stl_data, None)?;
    // We expect to read the same number of triangular faces as the cube originally had
    // (though the orientation/normals might differ).
    // The default cube -> 6 polygons x 1 polygon each with 4 vertices => 12 triangles in STL.
    // So from_stl_file => we get 12 triangles as 12 polygons (each is a tri).
    assert_eq!(csg_in.polygons.len(), 12);

    // Cleanup the temp file if desired
    let _ = std::fs::remove_file(tmp_path);
    Ok(())
}

/// A small, custom metadata type to demonstrate usage.
/// We derive PartialEq so we can assert equality in tests.
#[derive(Debug, Clone, PartialEq)]
struct MyMetaData {
    id: u32,
    label: String,
}

#[test]
fn test_polygon_metadata_string() {
    // Create a simple triangle polygon with shared data = Some("triangle".to_string()).
    let verts = vec![
        Vertex::new(Point3::origin(), Vector3::z()),
        Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::z()),
        Vertex::new(Point3::new(0.0, 1.0, 0.0), Vector3::z()),
    ];
    let mut poly = Polygon::new(verts, Some("triangle".to_string()));

    // Check getter
    assert_eq!(poly.metadata(), Some(&"triangle".to_string()));

    // Check setter
    poly.set_metadata("updated".to_string());
    assert_eq!(poly.metadata(), Some(&"updated".to_string()));

    // Check mutable getter
    if let Some(data) = poly.metadata_mut() {
        data.push_str("_appended");
    }
    assert_eq!(poly.metadata(), Some(&"updated_appended".to_string()));
}

#[test]
fn test_polygon_metadata_integer() {
    // Create a polygon with integer shared data
    let verts = vec![
        Vertex::new(Point3::origin(), Vector3::z()),
        Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::z()),
        Vertex::new(Point3::new(0.0, 1.0, 0.0), Vector3::z()),
    ];
    let poly = Polygon::new(verts, Some(42u32));

    // Confirm data
    assert_eq!(poly.metadata(), Some(&42));
}

#[test]
fn test_polygon_metadata_custom_struct() {
    // Create a polygon with our custom struct
    let my_data = MyMetaData {
        id: 999,
        label: "MyLabel".into(),
    };
    let verts = vec![
        Vertex::new(Point3::origin(), Vector3::z()),
        Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::z()),
        Vertex::new(Point3::new(0.0, 1.0, 0.0), Vector3::z()),
    ];
    let poly = Polygon::new(verts, Some(my_data.clone()));

    assert_eq!(poly.metadata(), Some(&my_data));
}

#[test]
fn test_csg_construction_with_metadata() {
    // Build a Mesh of two polygons, each with distinct shared data.
    let poly_a = Polygon::new(
        vec![
            Vertex::new(Point3::origin(), Vector3::z()),
            Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(1.0, 1.0, 0.0), Vector3::z()),
        ],
        Some("PolyA".to_string()),
    );
    let poly_b = Polygon::new(
        vec![
            Vertex::new(Point3::new(2.0, 0.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(3.0, 0.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(3.0, 1.0, 0.0), Vector3::z()),
        ],
        Some("PolyB".to_string()),
    );
    let csg = Mesh::from_polygons(&[poly_a.clone(), poly_b.clone()], None);

    // We expect two polygons with the same shared data as the originals.
    assert_eq!(csg.polygons.len(), 2);
    assert_eq!(csg.polygons[0].metadata(), Some(&"PolyA".to_string()));
    assert_eq!(csg.polygons[1].metadata(), Some(&"PolyB".to_string()));
}

#[test]
fn test_union_metadata() {
    // Let's union two squares in the XY plane, each with different shared data.
    // So after union, we typically get polygons from each original shape.
    // If there's any overlap, new polygons might be formed, but in Sketch
    // each new polygon inherits the shared data from whichever polygon it came from.

    // Cube1 from (0,0) to (1,1) => label "Cube1"
    let cube1 = Mesh::cube(1.0, None); // bottom-left at (0,0), top-right at (1,1)
    let mut cube1 = cube1; // now let us set shared data for each polygon
    for p in &mut cube1.polygons {
        p.set_metadata("Cube1".to_string());
    }

    // Translate Cube2 so it partially overlaps. => label "Cube2"
    let cube2 = Mesh::cube(1.0, None).translate(0.5, 0.0, 0.0);
    let mut cube2 = cube2;
    for p in &mut cube2.polygons {
        p.set_metadata("Cube2".to_string());
    }

    // Union
    let union_csg = cube1.union(&cube2);

    // Depending on the library's polygon splitting, we often end up with multiple polygons.
    // We can at least confirm that each polygon's shared data is EITHER "Square1" or "Square2",
    // and never mixed or lost.
    for poly in &union_csg.polygons {
        let data = poly.metadata().unwrap();
        assert!(
            data == "Cube1" || data == "Cube2",
            "Union polygon has unexpected shared data = {:?}",
            data
        );
    }
}

#[test]
fn test_difference_metadata() {
    // Difference two cubes, each with different shared data. The resulting polygons
    // come from the *minuend* (the first shape) with *some* portion clipped out.
    // So the differenced portion from the second shape won't appear in the final.

    let mut cube1 = Mesh::cube(2.0, Some("Cube1".to_string()));
    for p in &mut cube1.polygons {
        p.set_metadata("Cube1".to_string());
    }

    let mut cube2 = Mesh::cube(2.0, Some("Cube2".to_string())).translate(0.5, 0.5, 0.5);
    for p in &mut cube2.polygons {
        p.set_metadata("Cube2".to_string());
    }

    let result = cube1.difference(&cube2);

    println!("{:#?}", cube1);
    println!("{:#?}", cube2);
    println!("{:#?}", result);

    // All polygons in the result should come from "Cube1" only.
    for poly in &result.polygons {
        assert_eq!(poly.metadata(), Some(&"Cube1".to_string()));
    }
}

#[test]
fn test_intersect_metadata() {
    // Intersection: the resulting polygons should come from polygons that are inside both.
    // Typically, the library picks polygons from the first shape, then clips them
    // against the second. Depending on exact implementation, the polygons that remain
    // carry the first shape's shared data. In many CSG implementations, the final polygons
    // keep the "side" from whichever shape is relevant. That might be shape A or B or both.
    // We'll check that we only see "Cube1" or "Cube2" but not random data.

    let mut cube1 = Mesh::cube(2.0, None);
    for p in &mut cube1.polygons {
        p.set_metadata("Cube1".to_string());
    }

    let mut cube2 = Mesh::cube(2.0, None).translate(0.5, 0.5, 0.5);
    for p in &mut cube2.polygons {
        p.set_metadata("Cube2".to_string());
    }

    let result = cube1.intersection(&cube2);

    // Depending on the implementation, it's common that intersection polygons are
    // actually from both shapes or from shape A. Let's check that if they do have shared data,
    // it must be from either "Cube1" or "Cube2".
    for poly in &result.polygons {
        let data = poly.metadata().unwrap();
        assert!(
            data == "Cube1" || data == "Cube2",
            "Intersection polygon has unexpected shared data = {:?}",
            data
        );
    }
}

#[test]
fn test_flip_invert_metadata() {
    // Flipping or inverting a shape should NOT change the shared data;
    // it only flips normals/polygons.

    let mut csg = Mesh::cube(2.0, None);
    for p in &mut csg.polygons {
        p.set_metadata("MyCube".to_string());
    }

    // Invert
    let inverted = csg.inverse();
    for poly in &inverted.polygons {
        assert_eq!(poly.metadata(), Some(&"MyCube".to_string()));
    }
}

#[test]
fn test_subdivide_metadata() {
    // Subdivide a polygon with shared data, ensure all new subdivided polygons
    // preserve that data.

    let poly = Polygon::new(
        vec![
            Vertex::new(Point3::origin(), Vector3::z()),
            Vertex::new(Point3::new(2.0, 0.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(2.0, 2.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(0.0, 2.0, 0.0), Vector3::z()),
        ],
        Some("LargeQuad".to_string()),
    );
    let csg = Mesh::from_polygons(&[poly], None);
    let subdivided = csg.subdivide_triangles(1.try_into().expect("not 0")); // one level of subdivision

    // Now it's split into multiple triangles. Each should keep "LargeQuad" as metadata.
    assert!(subdivided.polygons.len() > 1);
    for spoly in &subdivided.polygons {
        assert_eq!(spoly.metadata(), Some(&"LargeQuad".to_string()));
    }
}

#[test]
fn test_transform_metadata() {
    // Make sure that transform does not lose or change shared data.
    let poly = Polygon::new(
        vec![
            Vertex::new(Point3::origin(), Vector3::z()),
            Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::z()),
            Vertex::new(Point3::new(0.0, 1.0, 0.0), Vector3::z()),
        ],
        Some("Tri".to_string()),
    );
    let csg = Mesh::from_polygons(&[poly], None);
    let csg_trans = csg.translate(10.0, 5.0, 0.0);
    let csg_scale = csg_trans.scale(2.0, 2.0, 1.0);
    let csg_rot = csg_scale.rotate(0.0, 0.0, 45.0);

    for poly in &csg_rot.polygons {
        assert_eq!(poly.metadata(), Some(&"Tri".to_string()));
    }
}

#[test]
fn test_complex_metadata_struct_in_boolean_ops() {
    // We'll do an operation using a custom struct to verify it remains intact.
    // We'll do a union for instance.

    #[derive(Debug, Clone, PartialEq)]
    struct Color(u8, u8, u8);

    let mut csg1 = Mesh::cube(2.0, None);
    for p in &mut csg1.polygons {
        p.set_metadata(Color(255, 0, 0));
    }
    let mut csg2 = Mesh::cube(2.0, None).translate(0.5, 0.5, 0.5);
    for p in &mut csg2.polygons {
        p.set_metadata(Color(0, 255, 0));
    }

    let unioned = csg1.union(&csg2);
    // Now polygons are either from csg1 (red) or csg2 (green).
    for poly in &unioned.polygons {
        let col = poly.metadata().unwrap();
        assert!(
            *col == Color(255, 0, 0) || *col == Color(0, 255, 0),
            "Unexpected color in union: {:?}",
            col
        );
    }
}

#[test]
fn test_square_ccw_ordering() {
    let square = Sketch::<()>::square(2.0, None);
    let mp = square.to_multipolygon();
    assert_eq!(mp.0.len(), 1);
    let poly = &mp.0[0];
    let area = poly.signed_area();
    assert!(area > 0.0, "Square vertices are not CCW ordered");
}

#[test]
#[cfg(feature = "offset")]
fn test_offset_2d_positive_distance_grows() {
    let square = Sketch::<()>::square(2.0, None); // Centered square with size 2x2
    let offset = square.offset(0.5); // Positive offset should grow the square

    // The original square has area 4.0
    // The offset square should have area greater than 4.0
    let mp = offset.to_multipolygon();
    assert_eq!(mp.0.len(), 1);
    let poly = &mp.0[0];
    let area = poly.signed_area();
    assert!(
        area > 4.0,
        "Offset with positive distance did not grow the square"
    );
}

#[test]
#[cfg(feature = "offset")]
fn test_offset_2d_negative_distance_shrinks() {
    let square = Sketch::<()>::square(2.0, None); // Centered square with size 2x2
    let offset = square.offset(-0.5); // Negative offset should shrink the square

    // The original square has area 4.0
    // The offset square should have area less than 4.0
    let mp = offset.to_multipolygon();
    assert_eq!(mp.0.len(), 1);
    let poly = &mp.0[0];
    let area = poly.signed_area();
    assert!(
        area < 4.0,
        "Offset with negative distance did not shrink the square"
    );
}

#[test]
fn test_polygon_2d_enforce_ccw_ordering() {
    // Define a triangle in CW order
    let points_cw = vec![[0.0, 0.0], [1.0, 0.0], [0.5, 1.0]];
    let csg_cw = Sketch::<()>::polygon(&points_cw, None);
    // Enforce CCW ordering
    csg_cw.renormalize();
    let poly = &csg_cw.geometry.0[0];
    let area = poly.signed_area();
    assert!(area > 0.0, "Polygon ordering was not corrected to CCW");
}

#[test]
#[cfg(feature = "offset")]
fn test_circle_offset_2d() {
    let circle = Sketch::<()>::circle(1.0, 32, None);
    let offset_grow = circle.offset(0.2); // Should grow the circle
    let offset_shrink = circle.offset(-0.2); // Should shrink the circle

    let grow = offset_grow.to_multipolygon();
    let shrink = offset_shrink.to_multipolygon();
    let grow_area = grow.0[0].signed_area();
    let shrink_area = shrink.0[0].signed_area();

    // Original circle has area ~3.1416
    let original_area = 3.141592653589793;

    assert!(
        grow_area > original_area,
        "Offset with positive distance did not grow the circle"
    );
    assert!(
        shrink_area < original_area,
        "Offset with negative distance did not shrink the circle"
    );
}

/// Helper to make a simple Polygon in 3D with given vertices.
fn make_polygon_3d(points: &[[Real; 3]]) -> Polygon<()> {
    let mut verts = Vec::new();
    for p in points {
        let pos = Point3::new(p[0], p[1], p[2]);
        // For simplicity, just store an arbitrary normal; Polygon::new re-computes the plane anyway.
        let normal = Vector3::z();
        verts.push(Vertex::new(pos, normal));
    }
    Polygon::new(verts, None)
}

#[test]
fn test_same_number_of_vertices() {
    // "Bottom" is a triangle in 3D
    let bottom = make_polygon_3d(&[[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.5, 0.5, 0.0]]);
    // "Top" is the same triangle, shifted up in Z
    let top = make_polygon_3d(&[[0.0, 0.0, 1.0], [1.0, 0.0, 1.0], [0.5, 0.5, 1.0]]);

    // This should succeed with no panic:
    let csg = Sketch::loft(&bottom, &top, true).unwrap();

    // Expect:
    //  - bottom polygon
    //  - top polygon
    //  - 3 side polygons (one for each edge of the triangle)
    assert_eq!(
        csg.polygons.len(),
        1 /*bottom*/ + 1 /*top*/ + 3 /*sides*/
    );
}

#[test]
fn test_different_number_of_vertices_panics() {
    // Bottom has 3 vertices
    let bottom = make_polygon_3d(&[[0.0, 0.0, 0.0], [2.0, 0.0, 0.0], [1.0, 1.0, 0.0]]);
    // Top has 4 vertices
    let top = make_polygon_3d(&[
        [0.0, 0.0, 2.0],
        [2.0, 0.0, 2.0],
        [2.0, 2.0, 2.0],
        [0.0, 2.0, 2.0],
    ]);

    // Call the API and assert the specific error variant is returned
    let result = Sketch::loft(&bottom, &top, true);
    assert!(matches!(result, Err(ValidationError::MismatchedVertices)));
}

#[test]
fn test_consistent_winding() {
    // Make a square in the XY plane (bottom)
    let bottom = make_polygon_3d(&[
        [0.0, 0.0, 0.0],
        [1.0, 0.0, 0.0],
        [1.0, 1.0, 0.0],
        [0.0, 1.0, 0.0],
    ]);
    // Make the same square, shifted up in Z, with the same winding direction
    let top = make_polygon_3d(&[
        [0.0, 0.0, 1.0],
        [1.0, 0.0, 1.0],
        [1.0, 1.0, 1.0],
        [0.0, 1.0, 1.0],
    ]);

    let csg = Sketch::loft(&bottom, &top, false).unwrap();

    // Expect 1 bottom + 1 top + 4 side faces = 6 polygons
    assert_eq!(csg.polygons.len(), 6);

    // Optionally check that each polygon has at least 3 vertices
    for poly in &csg.polygons {
        assert!(poly.vertices.len() >= 3);
    }
}

#[test]
fn test_inverted_orientation() {
    // Bottom square
    let bottom = make_polygon_3d(&[
        [0.0, 0.0, 0.0],
        [1.0, 0.0, 0.0],
        [1.0, 1.0, 0.0],
        [0.0, 1.0, 0.0],
    ]);
    // Top square, but with vertices in opposite order => "flipped" winding
    let mut top = make_polygon_3d(&[
        [0.0, 1.0, 1.0],
        [1.0, 1.0, 1.0],
        [1.0, 0.0, 1.0],
        [0.0, 0.0, 1.0],
    ]);

    // We can fix by flipping `top`:
    top.flip();

    let csg = Sketch::loft(&bottom, &top, false).unwrap();

    // Expect 1 bottom + 1 top + 4 sides = 6 polygons
    assert_eq!(csg.polygons.len(), 6);

    // Check bounding box for sanity
    let bbox = csg.bounding_box();
    assert!(
        bbox.mins.z < bbox.maxs.z,
        "Should have a non-zero height in the Z dimension"
    );
}

#[test]
fn test_union_of_extruded_shapes() {
    // We'll extrude two shapes that partially overlap, then union them.

    // First shape: triangle
    let bottom1 = make_polygon_3d(&[[0.0, 0.0, 0.0], [2.0, 0.0, 0.0], [1.0, 1.0, 0.0]]);
    let top1 = make_polygon_3d(&[[0.0, 0.0, 1.0], [2.0, 0.0, 1.0], [1.0, 1.0, 1.0]]);
    let csg1 = Sketch::loft(&bottom1, &top1, true).unwrap();

    // Second shape: small shifted square
    let bottom2 = make_polygon_3d(&[
        [1.0, -0.2, 0.5],
        [2.0, -0.2, 0.5],
        [2.0, 0.8, 0.5],
        [1.0, 0.8, 0.5],
    ]);
    let top2 = make_polygon_3d(&[
        [1.0, -0.2, 1.5],
        [2.0, -0.2, 1.5],
        [2.0, 0.8, 1.5],
        [1.0, 0.8, 1.5],
    ]);
    let csg2 = Sketch::loft(&bottom2, &top2, true).unwrap();

    // Union them
    let unioned = csg1.union(&csg2);

    // Sanity check: union shouldn't be empty
    assert!(!unioned.polygons.is_empty());

    // Its bounding box should span at least from z=0 to z=1.5
    let bbox = unioned.bounding_box();
    assert!(bbox.mins.z <= 0.0 + EPSILON);
    assert!(bbox.maxs.z >= 1.5 - EPSILON);
}

#[test]
fn test_flatten_cube() {
    // 1) Create a cube from (-1,-1,-1) to (+1,+1,+1)
    let cube = Mesh::<()>::cube(2.0, None);
    // 2) Flatten into the XY plane
    let flattened = cube.flatten();

    // The flattened cube should have 1 polygon1, now in z=0
    assert_eq!(
        flattened.geometry.len(),
        1,
        "Flattened cube should have 1 face in z=0"
    );

    // Optional: we can check the bounding box in z-dimension is effectively zero
    let bbox = flattened.bounding_box();
    let thickness = bbox.maxs.z - bbox.mins.z;
    assert!(
        thickness.abs() < EPSILON,
        "Flattened shape should have negligible thickness in z"
    );
}

#[test]
fn test_slice_cylinder() {
    // 1) Create a cylinder (start=-1, end=+1) with radius=1, 32 slices
    let cyl = Mesh::<()>::cylinder(1.0, 2.0, 32, None).center();
    // 2) Slice at z=0
    let cross_section = cyl.slice(Plane::from_normal(Vector3::z(), 0.0));

    // For a simple cylinder, the cross-section is typically 1 circle polygon
    // (unless the top or bottom also exactly intersect z=0, which they do not in this scenario).
    // So we expect exactly 1 polygon.
    assert_eq!(
        cross_section.geometry.len(),
        1,
        "Slicing a cylinder at z=0 should yield exactly 1 cross-section polygon"
    );

    let poly_geom = &cross_section.geometry.0[0];
    // Geometry → Polygon
    let poly = match poly_geom {
        Geometry::Polygon(p) => p,
        _ => panic!("Cross-section geometry is not a polygon"),
    };

    // `geo::Polygon` stores a closed ring – skip the last (repeat) vertex.
    let vcount = poly.exterior().0.len() - 1;

    // We used 32 slices for the cylinder, so we expect up to 32 edges
    // in the cross-section circle. Some slight differences might occur
    // if the slicing logic merges or sorts vertices.
    // Typically, you might see vcount = 32 or vcount = 34, etc.
    // Let's just check it's > 3 and in a plausible range:
    assert!(
        vcount >= 3 && vcount <= 40,
        "Expected cross-section circle to have a number of edges ~32, got {}",
        vcount
    );
}

/// Helper to create a `Polygon` in the XY plane from an array of (x,y) points,
/// with z=0 and normal=+Z.
fn polygon_from_xy_points(xy_points: &[[Real; 2]]) -> Polygon<()> {
    assert!(xy_points.len() >= 3, "Need at least 3 points for a polygon.");

    let normal = Vector3::z();
    let vertices: Vec<Vertex> = xy_points
        .iter()
        .map(|&[x, y]| Vertex::new(Point3::new(x, y, 0.0), normal))
        .collect();

    Polygon::new(vertices, None)
}

/// Test a simple case of `flatten_and_union` with a single square in the XY plane.
/// We expect the same shape back.
#[test]
fn test_flatten_and_union_single_polygon() {
    // Create a Mesh with one polygon (a unit square).
    let square_poly =
        polygon_from_xy_points(&[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]]);
    let csg = Mesh::from_polygons(&[square_poly], None);

    // Flatten & union it
    let flat_csg = csg.flatten();

    // Expect the same bounding box
    assert!(
        !flat_csg.geometry.0[0].is_empty(),
        "Result should not be empty"
    );
    let bb = flat_csg.bounding_box();
    assert_eq!(bb.mins.x, 0.0);
    assert_eq!(bb.mins.y, 0.0);
    assert_eq!(bb.maxs.x, 1.0);
    assert_eq!(bb.maxs.y, 1.0);
}

/// Test `flatten_and_union` with two overlapping squares.
/// The result should be a single unioned polygon covering [0..2, 0..1].
#[test]
fn test_flatten_and_union_two_overlapping_squares() {
    // First square from (0,0) to (1,1)
    let square1 = polygon_from_xy_points(&[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]]);
    // Second square from (1,0) to (2,1)
    let square2 = polygon_from_xy_points(&[[1.0, 0.0], [2.0, 0.0], [2.0, 1.0], [1.0, 1.0]]);
    let csg = Mesh::from_polygons(&[square1, square2], None);

    let flat_csg = csg.flatten();
    assert!(
        !flat_csg.geometry.0[0].is_empty(),
        "Union should not be empty"
    );

    // The bounding box should now span x=0..2, y=0..1
    let bb = flat_csg.bounding_box();
    assert_eq!(bb.mins.x, 0.0);
    assert_eq!(bb.maxs.x, 2.0);
    assert_eq!(bb.mins.y, 0.0);
    assert_eq!(bb.maxs.y, 1.0);
}

/// Test `flatten_and_union` with two disjoint squares.
/// The result should have two separate polygons.
#[test]
fn test_flatten_and_union_two_disjoint_squares() {
    // Square A at (0..1, 0..1)
    let square_a = polygon_from_xy_points(&[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]]);
    // Square B at (2..3, 2..3)
    let square_b = polygon_from_xy_points(&[[2.0, 2.0], [3.0, 2.0], [3.0, 3.0], [2.0, 3.0]]);
    let csg = Mesh::from_polygons(&[square_a, square_b], None);

    let flat_csg = csg.flatten();
    assert!(!flat_csg.geometry.0[0].is_empty());
}

/// Test `flatten_and_union` when polygons are not perfectly in the XY plane,
/// but very close to z=0. This checks sensitivity to floating errors.
#[test]
fn test_flatten_and_union_near_xy_plane() {
    let normal = Vector3::z();
    // Slightly "tilted" or with z=1e-6
    let poly1 = Polygon::<()>::new(
        vec![
            Vertex::new(Point3::new(0.0, 0.0, 1e-6), normal),
            Vertex::new(Point3::new(1.0, 0.0, 1e-6), normal),
            Vertex::new(Point3::new(1.0, 1.0, 1e-6), normal),
            Vertex::new(Point3::new(0.0, 1.0, 1e-6), normal),
        ],
        None,
    );

    let csg = Mesh::from_polygons(&[poly1], None);
    let flat_csg = csg.flatten();

    assert!(
        !flat_csg.geometry.0[0].is_empty(),
        "Should flatten to a valid polygon"
    );
    let bb = flat_csg.bounding_box();
    assert_eq!(bb.mins.x, 0.0);
    assert_eq!(bb.maxs.x, 1.0);
    assert_eq!(bb.mins.y, 0.0);
    assert_eq!(bb.maxs.y, 1.0);
}

/// Test with multiple polygons that share edges or have nearly collinear edges
/// to ensure numeric tolerances (`eps_area`, `eps_pos`) don't remove them incorrectly.
#[test]
fn test_flatten_and_union_collinear_edges() {
    // Two rectangles sharing a long horizontal edge
    let rect1 = polygon_from_xy_points(&[[0.0, 0.0], [2.0, 0.0], [2.0, 1.0], [0.0, 1.0]]);
    let rect2 = polygon_from_xy_points(&[
        [2.0, 0.0],
        [4.0, 0.0],
        [4.0, 1.001], // slightly off
        [2.0, 1.0],
    ]);

    let csg = Mesh::<()>::from_polygons(&[rect1, rect2], None);
    let flat_csg = csg.flatten();

    // Expect 1 polygon from x=0..4, y=0..~1.0ish
    assert!(!flat_csg.geometry.0[0].is_empty());
    let bb = flat_csg.bounding_box();
    assert!((bb.maxs.x - 4.0).abs() < 1e-5, "Should span up to x=4.0");
    // Also check the y-range is ~1.001
    assert!((bb.maxs.y - 1.001).abs() < 1e-3);
}

/// If you suspect `flatten_and_union` is returning no polygons, this test
/// ensures we get at least one polygon for a simple shape. If it fails,
/// you can println! debug info in `flatten_and_union`.
#[test]
fn test_flatten_and_union_debug() {
    let cube = Mesh::<()>::cube(2.0, None);
    let flattened = cube.flatten();
    assert!(
        !flattened.geometry.0[0].is_empty(),
        "Flattened cube should not be empty"
    );
    let area = flattened.geometry.0[0].signed_area();
    assert!(area > 3.9, "Flattened cube too small");
}

#[test]
fn test_contains_vertex() {
    let csg_cube = Mesh::<()>::cube(6.0, None);

    assert!(csg_cube.contains_vertex(&Point3::new(3.0, 3.0, 3.0)));
    assert!(csg_cube.contains_vertex(&Point3::new(1.0, 2.0, 5.9)));
    assert!(!csg_cube.contains_vertex(&Point3::new(3.0, 3.0, 6.0)));
    assert!(!csg_cube.contains_vertex(&Point3::new(3.0, 3.0, -6.0)));
    assert!(!csg_cube.contains_vertex(&Point3::new(3.0, 3.0, 0.0)));
    assert!(csg_cube.contains_vertex(&Point3::new(3.0, 3.0, 0.01)));

    #[cfg(feature = "f64")]
    {
        assert!(csg_cube.contains_vertex(&Point3::new(3.0, 3.0, 5.99999999)));
        assert!(csg_cube.contains_vertex(&Point3::new(3.0, 3.0, 6.0 - 1e-11)));
        assert!(csg_cube.contains_vertex(&Point3::new(3.0, 3.0, 6.0 - 1e-14)));
        assert!(csg_cube.contains_vertex(&Point3::new(3.0, 3.0, 5.9 + 9e-9)));
    }

    #[cfg(feature = "f32")]
    {
        assert!(csg_cube.contains_vertex(&Point3::new(3.0, 3.0, 5.999999)));
        assert!(csg_cube.contains_vertex(&Point3::new(3.0, 3.0, 6.0 - 1e-6)));
        assert!(csg_cube.contains_vertex(&Point3::new(3.0, 3.0, 5.9 + 9e-9)));
    }

    assert!(csg_cube.contains_vertex(&Point3::new(3.0, -3.0, 3.0)));
    assert!(!csg_cube.contains_vertex(&Point3::new(3.0, -3.01, 3.0)));
    assert!(csg_cube.contains_vertex(&Point3::new(0.01, 4.0, 3.0)));
    assert!(!csg_cube.contains_vertex(&Point3::new(-0.01, 4.0, 3.0)));

    let csg_cube_hole = Mesh::<()>::cube(4.0, None);
    let cube_with_hole = csg_cube.difference(&csg_cube_hole);

    assert!(!cube_with_hole.contains_vertex(&Point3::new(0.01, 4.0, 3.0)));
    assert!(cube_with_hole.contains_vertex(&Point3::new(0.01, 4.01, 3.0)));
    assert!(!cube_with_hole.contains_vertex(&Point3::new(-0.01, 4.0, 3.0)));
    assert!(cube_with_hole.contains_vertex(&Point3::new(1.0, 2.0, 5.9)));
    assert!(!cube_with_hole.contains_vertex(&Point3::new(3.0, 3.0, 6.0)));

    let csg_sphere = Mesh::<()>::sphere(6.0, 14, 14, None);

    assert!(csg_sphere.contains_vertex(&Point3::new(3.0, 3.0, 3.0)));
    assert!(csg_sphere.contains_vertex(&Point3::new(-3.0, -3.0, -3.0)));
    assert!(!csg_sphere.contains_vertex(&Point3::new(1.0, 2.0, 5.9)));

    assert!(!csg_sphere.contains_vertex(&Point3::new(1.0, 1.0, 5.8)));
    assert!(!csg_sphere.contains_vertex(&Point3::new(0.0, 3.0, 5.8)));
    assert!(csg_sphere.contains_vertex(&Point3::new(0.0, 0.0, 5.8)));

    assert!(!csg_sphere.contains_vertex(&Point3::new(3.0, 3.0, 6.0)));
    assert!(!csg_sphere.contains_vertex(&Point3::new(3.0, 3.0, -6.0)));
    assert!(csg_sphere.contains_vertex(&Point3::new(3.0, 3.0, 0.0)));

    assert!(csg_sphere.contains_vertex(&Point3::new(0.0, 0.0, -5.8)));
    assert!(!csg_sphere.contains_vertex(&Point3::new(3.0, 3.0, -5.8)));
    assert!(!csg_sphere.contains_vertex(&Point3::new(3.0, 3.0, -6.01)));
    assert!(csg_sphere.contains_vertex(&Point3::new(3.0, 3.0, 0.01)));
}

#[test]
fn test_union_crash() {
    let items: [Mesh<()>; 2] = [
        Mesh::from_polygons(
            &[
                Polygon::new(
                    vec![
                        Vertex {
                            pos: Point3::new(640.0, 0.0, 640.0),
                            normal: Vector3::new(0.0, -1.0, 0.0),
                        },
                        Vertex {
                            pos: Point3::new(768.0, 0.0, 128.0),
                            normal: Vector3::new(0.0, -1.0, 0.0),
                        },
                        Vertex {
                            pos: Point3::new(1280.0, 0.0, 256.0),
                            normal: Vector3::new(0.0, -1.0, 0.0),
                        },
                        Vertex {
                            pos: Point3::new(1024.0, 0.0, 640.0),
                            normal: Vector3::new(0.0, -1.0, 0.0),
                        },
                    ],
                    None,
                ),
                Polygon::new(
                    vec![
                        Vertex {
                            pos: Point3::new(1024.0, 256.0, 640.0),
                            normal: Vector3::new(0.0, 1.0, 0.0),
                        },
                        Vertex {
                            pos: Point3::new(1280.0, 256.0, 256.0),
                            normal: Vector3::new(0.0, 1.0, 0.0),
                        },
                        Vertex {
                            pos: Point3::new(768.0, 256.0, 128.0),
                            normal: Vector3::new(0.0, 1.0, 0.0),
                        },
                        Vertex {
                            pos: Point3::new(640.0, 256.0, 640.0),
                            normal: Vector3::new(0.0, 1.0, 0.0),
                        },
                    ],
                    None,
                ),
                Polygon::new(
                    vec![
                        Vertex {
                            pos: Point3::new(640.0, 0.0, 640.0),
                            normal: Vector3::new(
                                0.9701425433158875,
                                -0.0,
                                0.24253563582897186,
                            ),
                        },
                        Vertex {
                            pos: Point3::new(640.0, 256.0, 640.0),
                            normal: Vector3::new(
                                0.9701425433158875,
                                -0.0,
                                0.24253563582897186,
                            ),
                        },
                        Vertex {
                            pos: Point3::new(768.0, 256.0, 128.0),
                            normal: Vector3::new(
                                0.9701425433158875,
                                -0.0,
                                0.24253563582897186,
                            ),
                        },
                        Vertex {
                            pos: Point3::new(768.0, 0.0, 128.0),
                            normal: Vector3::new(
                                0.9701425433158875,
                                -0.0,
                                0.24253563582897186,
                            ),
                        },
                    ],
                    None,
                ),
                Polygon::new(
                    vec![
                        Vertex {
                            pos: Point3::new(768.0, 0.0, 128.0),
                            normal: Vector3::new(
                                -0.24253563582897186,
                                0.0,
                                0.9701425433158875,
                            ),
                        },
                        Vertex {
                            pos: Point3::new(768.0, 256.0, 128.0),
                            normal: Vector3::new(
                                -0.24253563582897186,
                                0.0,
                                0.9701425433158875,
                            ),
                        },
                        Vertex {
                            pos: Point3::new(1280.0, 256.0, 256.0),
                            normal: Vector3::new(
                                -0.24253563582897186,
                                0.0,
                                0.9701425433158875,
                            ),
                        },
                        Vertex {
                            pos: Point3::new(1280.0, 0.0, 256.0),
                            normal: Vector3::new(
                                -0.24253563582897186,
                                0.0,
                                0.9701425433158875,
                            ),
                        },
                    ],
                    None,
                ),
                Polygon::new(
                    vec![
                        Vertex {
                            pos: Point3::new(1280.0, 0.0, 256.0),
                            normal: Vector3::new(
                                -0.8320503234863281,
                                0.0,
                                -0.5547001957893372,
                            ),
                        },
                        Vertex {
                            pos: Point3::new(1280.0, 256.0, 256.0),
                            normal: Vector3::new(
                                -0.8320503234863281,
                                0.0,
                                -0.5547001957893372,
                            ),
                        },
                        Vertex {
                            pos: Point3::new(1024.0, 256.0, 640.0),
                            normal: Vector3::new(
                                -0.8320503234863281,
                                0.0,
                                -0.5547001957893372,
                            ),
                        },
                        Vertex {
                            pos: Point3::new(1024.0, 0.0, 640.0),
                            normal: Vector3::new(
                                -0.8320503234863281,
                                0.0,
                                -0.5547001957893372,
                            ),
                        },
                    ],
                    None,
                ),
                Polygon::new(
                    vec![
                        Vertex {
                            pos: Point3::new(1024.0, 0.0, 640.0),
                            normal: Vector3::new(0.0, 0.0, -1.0),
                        },
                        Vertex {
                            pos: Point3::new(1024.0, 256.0, 640.0),
                            normal: Vector3::new(0.0, 0.0, -1.0),
                        },
                        Vertex {
                            pos: Point3::new(640.0, 256.0, 640.0),
                            normal: Vector3::new(0.0, 0.0, -1.0),
                        },
                        Vertex {
                            pos: Point3::new(640.0, 0.0, 640.0),
                            normal: Vector3::new(0.0, 0.0, -1.0),
                        },
                    ],
                    None,
                ),
            ],
            None,
        ),
        Mesh::from_polygons(
            &[
                Polygon::new(
                    vec![
                        Vertex {
                            pos: Point3::new(896.0, 0.0, 768.0),
                            normal: Vector3::new(0.0, -1.0, 0.0),
                        },
                        Vertex {
                            pos: Point3::new(768.0, 0.0, 512.0),
                            normal: Vector3::new(0.0, -1.0, 0.0),
                        },
                        Vertex {
                            pos: Point3::new(1280.0, 0.0, 384.0),
                            normal: Vector3::new(0.0, -1.0, 0.0),
                        },
                        Vertex {
                            pos: Point3::new(1280.0, 0.0, 640.0),
                            normal: Vector3::new(0.0, -1.0, 0.0),
                        },
                    ],
                    None,
                ),
                Polygon::new(
                    vec![
                        Vertex {
                            pos: Point3::new(1280.0, 256.0, 640.0),
                            normal: Vector3::new(0.0, 1.0, 0.0),
                        },
                        Vertex {
                            pos: Point3::new(1280.0, 256.0, 384.0),
                            normal: Vector3::new(0.0, 1.0, 0.0),
                        },
                        Vertex {
                            pos: Point3::new(768.0, 256.0, 512.0),
                            normal: Vector3::new(0.0, 1.0, 0.0),
                        },
                        Vertex {
                            pos: Point3::new(896.0, 256.0, 768.0),
                            normal: Vector3::new(0.0, 1.0, 0.0),
                        },
                    ],
                    None,
                ),
                Polygon::new(
                    vec![
                        Vertex {
                            pos: Point3::new(896.0, 0.0, 768.0),
                            normal: Vector3::new(0.8944271802902222, 0.0, -0.4472135901451111),
                        },
                        Vertex {
                            pos: Point3::new(896.0, 256.0, 768.0),
                            normal: Vector3::new(0.8944271802902222, 0.0, -0.4472135901451111),
                        },
                        Vertex {
                            pos: Point3::new(768.0, 256.0, 512.0),
                            normal: Vector3::new(0.8944271802902222, 0.0, -0.4472135901451111),
                        },
                        Vertex {
                            pos: Point3::new(768.0, 0.0, 512.0),
                            normal: Vector3::new(0.8944271802902222, 0.0, -0.4472135901451111),
                        },
                    ],
                    None,
                ),
                Polygon::new(
                    vec![
                        Vertex {
                            pos: Point3::new(768.0, 0.0, 512.0),
                            normal: Vector3::new(
                                0.24253563582897186,
                                -0.0,
                                0.9701425433158875,
                            ),
                        },
                        Vertex {
                            pos: Point3::new(768.0, 256.0, 512.0),
                            normal: Vector3::new(
                                0.24253563582897186,
                                -0.0,
                                0.9701425433158875,
                            ),
                        },
                        Vertex {
                            pos: Point3::new(1280.0, 256.0, 384.0),
                            normal: Vector3::new(
                                0.24253563582897186,
                                -0.0,
                                0.9701425433158875,
                            ),
                        },
                        Vertex {
                            pos: Point3::new(1280.0, 0.0, 384.0),
                            normal: Vector3::new(
                                0.24253563582897186,
                                -0.0,
                                0.9701425433158875,
                            ),
                        },
                    ],
                    None,
                ),
                Polygon::new(
                    vec![
                        Vertex {
                            pos: Point3::new(1280.0, 0.0, 384.0),
                            normal: Vector3::new(-1.0, 0.0, 0.0),
                        },
                        Vertex {
                            pos: Point3::new(1280.0, 256.0, 384.0),
                            normal: Vector3::new(-1.0, 0.0, 0.0),
                        },
                        Vertex {
                            pos: Point3::new(1280.0, 256.0, 640.0),
                            normal: Vector3::new(-1.0, 0.0, 0.0),
                        },
                        Vertex {
                            pos: Point3::new(1280.0, 0.0, 640.0),
                            normal: Vector3::new(-1.0, 0.0, 0.0),
                        },
                    ],
                    None,
                ),
                Polygon::new(
                    vec![
                        Vertex {
                            pos: Point3::new(1280.0, 0.0, 640.0),
                            normal: Vector3::new(
                                -0.3162277638912201,
                                0.0,
                                -0.9486832618713379,
                            ),
                        },
                        Vertex {
                            pos: Point3::new(1280.0, 256.0, 640.0),
                            normal: Vector3::new(
                                -0.3162277638912201,
                                0.0,
                                -0.9486832618713379,
                            ),
                        },
                        Vertex {
                            pos: Point3::new(896.0, 256.0, 768.0),
                            normal: Vector3::new(
                                -0.3162277638912201,
                                0.0,
                                -0.9486832618713379,
                            ),
                        },
                        Vertex {
                            pos: Point3::new(896.0, 0.0, 768.0),
                            normal: Vector3::new(
                                -0.3162277638912201,
                                0.0,
                                -0.9486832618713379,
                            ),
                        },
                    ],
                    None,
                ),
            ],
            None,
        ),
    ];

    let combined = items[0].union(&items[1]);
    println!("{:?}", combined);
}

#[test]
fn test_mesh_quality_analysis() {
    let cube: Mesh<()> = Mesh::cube(2.0, None);

    // Analyze triangle quality
    let qualities = cube.analyze_triangle_quality();
    assert!(
        !qualities.is_empty(),
        "Should have quality metrics for triangles"
    );

    // All cube triangles should have reasonable quality
    for quality in &qualities {
        assert!(
            quality.quality_score >= 0.0,
            "Quality score should be non-negative"
        );
        assert!(
            quality.quality_score <= 1.0,
            "Quality score should be at most 1.0"
        );
        assert!(quality.area > 0.0, "Triangle area should be positive");
        assert!(quality.min_angle > 0.0, "Minimum angle should be positive");
        assert!(quality.max_angle < PI, "Maximum angle should be less than π");
    }

    // Compute overall mesh quality metrics
    let mesh_metrics = cube.compute_mesh_quality();
    assert!(
        mesh_metrics.avg_quality >= 0.0,
        "Average quality should be non-negative"
    );
    assert!(
        mesh_metrics.min_quality >= 0.0,
        "Minimum quality should be non-negative"
    );
    assert!(
        mesh_metrics.high_quality_ratio >= 0.0,
        "High quality ratio should be non-negative"
    );
    assert!(
        mesh_metrics.high_quality_ratio <= 1.0,
        "High quality ratio should be at most 1.0"
    );
    assert!(
        mesh_metrics.avg_edge_length > 0.0,
        "Average edge length should be positive"
    );
}

#[test]
fn test_adaptive_mesh_refinement() {
    let cube: Mesh<()> = Mesh::cube(2.0, None);
    let original_polygon_count = cube.polygons.len();

    // Refine mesh adaptively based on quality and edge length
    let refined = cube.adaptive_refine(0.3, 2.0, 15.0); // Refine triangles with quality < 0.3, edge length > 2.0, or curvature > 15 deg

    // Refined mesh should generally have more polygons (unless already very high quality)
    // Note: Since cube has high-quality faces, refinement may not increase polygon count significantly
    assert!(
        refined.polygons.len() >= original_polygon_count,
        "Adaptive refinement should maintain or increase polygon count"
    );

    // Test with more aggressive settings
    let aggressive_refined = cube.adaptive_refine(0.8, 1.0, 5.0);
    assert!(
        aggressive_refined.polygons.len() >= refined.polygons.len(),
        "More aggressive refinement should result in equal or more polygons"
    );
}

#[test]
fn test_laplacian_mesh_smoothing() {
    let sphere: Mesh<()> = Mesh::sphere(1.0, 16, 16, None);
    let original_positions: Vec<_> = sphere
        .polygons
        .iter()
        .flat_map(|poly| poly.vertices.iter().map(|v| v.pos))
        .collect();

    // Apply mild Laplacian smoothing
    let smoothed = sphere.laplacian_smooth(0.1, 2, false);

    // Mesh should have same number of polygons
    assert_eq!(
        smoothed.polygons.len(),
        sphere.polygons.len(),
        "Smoothing should preserve polygon count"
    );

    // Vertices should have moved (unless already perfectly smooth)
    let smoothed_positions: Vec<_> = smoothed
        .polygons
        .iter()
        .flat_map(|poly| poly.vertices.iter().map(|v| v.pos))
        .collect();

    assert_eq!(
        original_positions.len(),
        smoothed_positions.len(),
        "Should preserve vertex count"
    );

    // At least some vertices should have moved (unless mesh was already perfect)
    let mut moved_count = 0;
    for (orig, smooth) in original_positions.iter().zip(smoothed_positions.iter()) {
        if (orig - smooth).norm() > 1e-10 {
            moved_count += 1;
        }
    }

    // With sphere geometry and smoothing, we expect some movement
    println!("Moved vertices: {}/{}", moved_count, original_positions.len());
}

#[test]
fn test_remove_poor_triangles() {
    // Create a degenerate case by making a very thin triangle
    let vertices = vec![
        Vertex::new(Point3::new(0.0, 0.0, 0.0), Vector3::z()),
        Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::z()),
        Vertex::new(Point3::new(0.5, 1e-8, 0.0), Vector3::z()), // Very thin triangle
    ];
    let bad_polygon: Polygon<()> = Polygon::new(vertices, None);
    let csg_with_bad = Mesh::from_polygons(&[bad_polygon], None);

    // Remove poor quality triangles
    let filtered = csg_with_bad.remove_poor_triangles(0.1);

    // Should remove the poor quality triangle
    assert!(
        filtered.polygons.len() <= csg_with_bad.polygons.len(),
        "Should remove or maintain triangle count"
    );
}

#[test]
fn test_vertex_distance_operations() {
    let v1 = Vertex::new(Point3::new(0.0, 0.0, 0.0), Vector3::x());
    let v2 = Vertex::new(Point3::new(3.0, 4.0, 0.0), Vector3::y());

    // Test distance calculation
    let distance = v1.distance_to(&v2);
    assert!(
        (distance - 5.0).abs() < 1e-10,
        "Distance should be 5.0 (3-4-5 triangle)"
    );

    // Test squared distance (should be 25.0)
    let distance_sq = v1.distance_squared_to(&v2);
    assert!(
        (distance_sq - 25.0).abs() < 1e-10,
        "Squared distance should be 25.0"
    );

    // Test normal angle
    let angle = v1.normal_angle_to(&v2);
    assert!(
        (angle - PI / 2.0).abs() < 1e-10,
        "Angle between x and y normals should be π/2"
    );
}

#[test]
fn test_vertex_interpolation_methods() {
    let v1 = Vertex::new(Point3::new(0.0, 0.0, 0.0), Vector3::x());
    let v2 = Vertex::new(Point3::new(2.0, 2.0, 2.0), Vector3::y());

    // Test linear interpolation
    let mid_linear = v1.interpolate(&v2, 0.5);
    assert!(
        (mid_linear.pos - Point3::new(1.0, 1.0, 1.0)).norm() < 1e-10,
        "Linear interpolation midpoint should be (1,1,1)"
    );

    // Test spherical interpolation
    let mid_slerp = v1.slerp_interpolate(&v2, 0.5);
    assert!(
        (mid_slerp.pos - Point3::new(1.0, 1.0, 1.0)).norm() < 1e-10,
        "SLERP position should match linear for positions"
    );

    // Normal should be normalized and between the two normals
    assert!(
        (mid_slerp.normal.norm() - 1.0).abs() < 1e-10,
        "SLERP normal should be unit length"
    );
}

#[test]
fn test_barycentric_interpolation() {
    let v1 = Vertex::new(Point3::new(0.0, 0.0, 0.0), Vector3::x());
    let v2 = Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::y());
    let v3 = Vertex::new(Point3::new(0.0, 1.0, 0.0), Vector3::z());

    // Test centroid (equal weights)
    let centroid =
        Vertex::barycentric_interpolate(&v1, &v2, &v3, 1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0);
    let expected_pos = Point3::new(1.0 / 3.0, 1.0 / 3.0, 0.0);
    assert!(
        (centroid.pos - expected_pos).norm() < 1e-10,
        "Barycentric centroid should be at (1/3, 1/3, 0)"
    );

    // Test vertex recovery (weight=1 for one vertex)
    let recovered_v1 = Vertex::barycentric_interpolate(&v1, &v2, &v3, 1.0, 0.0, 0.0);
    assert!(
        (recovered_v1.pos - v1.pos).norm() < 1e-10,
        "Barycentric should recover original vertex"
    );
}

#[test]
fn test_vertex_clustering() {
    let vertices = vec![
        Vertex::new(Point3::new(0.0, 0.0, 0.0), Vector3::x()),
        Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::x()),
        Vertex::new(Point3::new(0.5, 0.5, 0.0), Vector3::y()),
    ];

    let cluster = VertexCluster::from_vertices(&vertices).expect("Should create cluster");

    // Check cluster properties
    assert_eq!(cluster.count, 3, "Cluster should contain 3 vertices");
    assert!(cluster.radius > 0.0, "Cluster should have positive radius");

    // Centroid should be reasonable
    let expected_centroid = Point3::new(0.5, 1.0 / 6.0, 0.0);
    assert!(
        (cluster.position - expected_centroid).norm() < 1e-10,
        "Cluster centroid should be average of vertex positions"
    );

    // Convert back to vertex
    let representative = cluster.to_vertex();
    assert_eq!(
        representative.pos, cluster.position,
        "Representative should have cluster position"
    );
    assert_eq!(
        representative.normal, cluster.normal,
        "Representative should have cluster normal"
    );
}

#[test]
fn test_mesh_connectivity_adjacency_usage() {
    // Create a simple cube to test mesh connectivity
    let cube: Mesh<()> = Mesh::cube(2.0, None);

    // Build the mesh connectivity graph
    let (vertex_map, adjacency_map) = cube.build_connectivity();

    // Verify that the adjacency map is properly built and used
    println!("Mesh connectivity analysis:");
    println!("  Total unique vertices: {}", vertex_map.vertex_count());
    println!("  Adjacency map entries: {}", adjacency_map.len());

    // Verify that vertices have neighbors
    assert!(
        vertex_map.vertex_count() > 0,
        "Vertex map should not be empty"
    );
    assert!(adjacency_map.len() > 0, "Adjacency map should not be empty");

    // Test that each vertex has some neighbors
    let mut total_neighbors = 0;
    for (vertex_idx, neighbors) in &adjacency_map {
        total_neighbors += neighbors.len();
        println!("  Vertex {} has {} neighbors", vertex_idx, neighbors.len());
        assert!(
            neighbors.len() > 0,
            "Each vertex should have at least one neighbor"
        );
    }

    println!("  Total neighbor relationships: {}", total_neighbors);

    // Test the actual mesh connectivity in Laplacian smoothing
    let smoothed_cube = cube.laplacian_smooth(0.1, 1, false);

    // Verify the smoothed mesh has the same number of polygons
    assert_eq!(
        cube.polygons.len(),
        smoothed_cube.polygons.len(),
        "Smoothing should preserve polygon count"
    );

    // Verify that smoothing actually changes vertex positions
    let original_pos = cube.polygons[0].vertices[0].pos;
    let smoothed_pos = smoothed_cube.polygons[0].vertices[0].pos;
    let position_change = (original_pos - smoothed_pos).norm();

    println!("  Position change from smoothing: {:.6}", position_change);
    assert!(
        position_change > 1e-10,
        "Smoothing should change vertex positions"
    );
}

#[test]
fn test_vertex_connectivity_analysis() {
    // Create a more complex mesh to test vertex connectivity
    let sphere: Mesh<()> = Mesh::sphere(1.0, 16, 8, None);
    let (vertex_map, adjacency_map) = sphere.build_connectivity();

    // Build vertex positions map for analysis
    let mut vertex_positions = HashMap::new();
    for (pos, idx) in vertex_map.get_vertex_positions() {
        vertex_positions.insert(*idx, *pos);
    }

    // Test vertex connectivity analysis for a few vertices
    let mut total_regularity = 0.0;
    let mut vertex_count = 0;

    for &vertex_idx in adjacency_map.keys().take(5) {
        let (valence, regularity) =
            crate::mesh::vertex::Vertex::analyze_connectivity_with_index(
                vertex_idx,
                &adjacency_map,
            );

        println!(
            "Vertex {}: valence={}, regularity={:.3}",
            vertex_idx, valence, regularity
        );

        assert!(valence > 0, "Vertex should have positive valence");
        assert!(
            regularity >= 0.0 && regularity <= 1.0,
            "Regularity should be in [0,1]"
        );

        total_regularity += regularity;
        vertex_count += 1;
    }

    let avg_regularity = total_regularity / vertex_count as Real;
    println!("Average regularity: {:.3}", avg_regularity);

    // Sphere vertices should have reasonable regularity
    assert!(
        avg_regularity > 0.1,
        "Sphere vertices should have decent regularity"
    );
}

#[test]
fn test_mesh_quality_with_adjacency() {
    // Create a triangulated cube
    let cube: Mesh<()> = Mesh::cube(2.0, None).triangulate();

    // Test triangle quality analysis
    let qualities = cube.analyze_triangle_quality();
    println!("Triangle quality analysis:");
    println!("  Number of triangles: {}", qualities.len());

    if !qualities.is_empty() {
        let avg_quality: Real =
            qualities.iter().map(|q| q.quality_score).sum::<Real>() / qualities.len() as Real;
        println!("  Average quality score: {:.3}", avg_quality);

        let min_quality = qualities
            .iter()
            .map(|q| q.quality_score)
            .fold(Real::INFINITY, |a, b| a.min(b));
        println!("  Minimum quality score: {:.3}", min_quality);

        // Cube triangles should have reasonable quality
        assert!(avg_quality > 0.1, "Cube triangles should have decent quality");
        assert!(min_quality >= 0.0, "Quality scores should be non-negative");
    }

    // Test mesh quality metrics
    let metrics = cube.compute_mesh_quality();
    println!("Mesh quality metrics:");
    println!("  Average quality: {:.3}", metrics.avg_quality);
    println!("  Minimum quality: {:.3}", metrics.min_quality);
    println!("  High quality ratio: {:.3}", metrics.high_quality_ratio);
    println!("  Sliver count: {}", metrics.sliver_count);
    println!("  Average edge length: {:.3}", metrics.avg_edge_length);
    println!("  Edge length std: {:.3}", metrics.edge_length_std);

    assert!(
        metrics.avg_quality >= 0.0,
        "Average quality should be non-negative"
    );
    assert!(
        metrics.min_quality >= 0.0,
        "Minimum quality should be non-negative"
    );
    assert!(
        metrics.high_quality_ratio >= 0.0 && metrics.high_quality_ratio <= 1.0,
        "High quality ratio should be in [0,1]"
    );
}

#[test]
fn test_adjacency_map_actually_used() {
    // This test specifically verifies that the adjacency map is actually used
    // by comparing results with and without proper connectivity

    let cube: Mesh<()> = Mesh::cube(2.0, None);

    // Build connectivity
    let (vertex_map, adjacency_map) = cube.build_connectivity();

    // Verify the adjacency map is not empty and has meaningful data
    assert!(!adjacency_map.is_empty(), "Adjacency map should not be empty");

    // Verify that vertices have multiple neighbors (not just self-references)
    let mut has_multiple_neighbors = false;
    for neighbors in adjacency_map.values() {
        if neighbors.len() > 2 {
            has_multiple_neighbors = true;
            break;
        }
    }
    assert!(
        has_multiple_neighbors,
        "Some vertices should have multiple neighbors"
    );

    // Test that the adjacency map affects smoothing
    let smoothed_0_iterations = cube.laplacian_smooth(0.0, 1, false);
    let smoothed_1_iterations = cube.laplacian_smooth(0.1, 1, false);

    // With lambda=0, no smoothing should occur
    let original_first_vertex = cube.polygons[0].vertices[0].pos;
    let zero_smoothed_first_vertex = smoothed_0_iterations.polygons[0].vertices[0].pos;
    let smoothed_first_vertex = smoothed_1_iterations.polygons[0].vertices[0].pos;

    // With lambda=0, position should be unchanged
    let zero_diff = (original_first_vertex - zero_smoothed_first_vertex).norm();
    assert!(
        zero_diff < 1e-10,
        "Zero smoothing should not change positions"
    );

    // With lambda=0.1, position should change
    let smooth_diff = (original_first_vertex - smoothed_first_vertex).norm();
    assert!(
        smooth_diff > 1e-10,
        "Smoothing should change vertex positions"
    );

    println!("Adjacency map usage verified:");
    println!("  Vertex count: {}", vertex_map.vertex_count());
    println!("  Adjacency entries: {}", adjacency_map.len());
    println!("  Smoothing effect: {:.6}", smooth_diff);
}

#[test]
fn test_cube_basics() {
    let cube: Mesh<()> = Mesh::cube(2.0, None);

    // A cube should have 6 faces
    assert_eq!(cube.polygons.len(), 6);

    // Each face should have 4 vertices
    for poly in &cube.polygons {
        assert_eq!(poly.vertices.len(), 4);
    }

    // Check bounding box dimensions (cube should be 2x2x2)
    let bbox = cube.bounding_box();
    let width = bbox.maxs.x - bbox.mins.x;
    let height = bbox.maxs.y - bbox.mins.y;
    let depth = bbox.maxs.z - bbox.mins.z;

    assert!((width - 2.0).abs() < 1e-10, "Width should be 2.0");
    assert!((height - 2.0).abs() < 1e-10, "Height should be 2.0");
    assert!((depth - 2.0).abs() < 1e-10, "Depth should be 2.0");
}

#[test]
fn test_cube_intersection() {
    let cube1: Mesh<()> = Mesh::cube(2.0, None);
    let cube2: Mesh<()> = Mesh::cube(2.0, None).translate(1.0, 0.0, 0.0);

    let intersection = cube1.intersection(&cube2);

    // The intersection should have some polygons
    assert!(
        intersection.polygons.len() > 0,
        "Intersection should produce some polygons"
    );

    // Check that intersection bounding box is reasonable
    let bbox = intersection.bounding_box();
    let width = bbox.maxs.x - bbox.mins.x;
    assert!(
        width > 0.0 && width < 2.0,
        "Intersection width should be between 0 and 2"
    );
}

#[test]
fn test_taubin_smoothing() {
    let sphere: Mesh<()> = Mesh::sphere(1.0, 16, 16, None);
    let original_positions: Vec<_> = sphere
        .polygons
        .iter()
        .flat_map(|poly| poly.vertices.iter().map(|v| v.pos))
        .collect();

    // Apply Taubin smoothing
    let smoothed = sphere.taubin_smooth(0.1, -0.105, 2, false);

    // Mesh should have same number of polygons
    assert_eq!(
        smoothed.polygons.len(),
        sphere.polygons.len(),
        "Smoothing should preserve polygon count"
    );

    // At least some vertices should have moved
    let smoothed_positions: Vec<_> = smoothed
        .polygons
        .iter()
        .flat_map(|poly| poly.vertices.iter().map(|v| v.pos))
        .collect();

    let mut moved_count = 0;
    for (orig, smooth) in original_positions.iter().zip(smoothed_positions.iter()) {
        if (orig - smooth).norm() > 1e-10 {
            moved_count += 1;
        }
    }
    assert!(
        moved_count > 0,
        "Taubin smoothing should change vertex positions"
    );
}