lib3mf-converters 0.4.0

Convert between 3MF, STL, and OBJ formats for 3D printing workflows
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
//! STL format import and export (binary and ASCII).
//!
//! This module provides conversion between STL files and 3MF [`Model`] structures.
//! Both binary and ASCII STL formats are supported.
//!
//! ## STL Formats
//!
//! ### Binary STL
//!
//! The binary STL format consists of:
//! - 80-byte header (typically unused, set to zeros)
//! - 4-byte little-endian unsigned integer triangle count
//! - For each triangle:
//!   - 12 bytes: normal vector (3 × f32, often ignored by importers)
//!   - 12 bytes: vertex 1 (x, y, z as f32)
//!   - 12 bytes: vertex 2 (x, y, z as f32)
//!   - 12 bytes: vertex 3 (x, y, z as f32)
//!   - 2 bytes: attribute byte count (typically 0)
//!
//! ### ASCII STL
//!
//! The ASCII STL format is a text-based format with keyword-delimited geometry:
//! - Keywords are case-insensitive (real-world files use both uppercase and lowercase)
//! - Multiple solids per file are supported (each becomes a separate object)
//! - Solid names with spaces are supported
//!
//! ## Auto-Detection
//!
//! [`StlImporter::read()`] automatically detects the format using the file size formula.
//! Use [`StlImporter::read_binary()`] or [`StlImporter::read_ascii()`] for explicit format selection.
//!
//! ## Examples
//!
//! ### Importing STL (auto-detect)
//!
//! ```no_run
//! use lib3mf_converters::stl::StlImporter;
//! use std::fs::File;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let file = File::open("model.stl")?;
//! let model = StlImporter::read(file)?;
//! println!("Imported {} objects", model.build.items.len());
//! # Ok(())
//! # }
//! ```
//!
//! ### Exporting Binary STL
//!
//! ```no_run
//! use lib3mf_converters::stl::BinaryStlExporter;
//! use lib3mf_core::model::Model;
//! use std::fs::File;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let model = Model::default();
//! let file = File::create("output.stl")?;
//! BinaryStlExporter::write(&model, file)?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Exporting ASCII STL
//!
//! ```no_run
//! use lib3mf_converters::stl::AsciiStlExporter;
//! use lib3mf_core::model::Model;
//! use std::fs::File;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let model = Model::default();
//! let file = File::create("output.stl")?;
//! AsciiStlExporter::write(&model, file)?;
//! # Ok(())
//! # }
//! ```
//!
//! [`Model`]: lib3mf_core::model::Model

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use lib3mf_core::error::{Lib3mfError, Result};
use lib3mf_core::model::resources::ResourceId;
use lib3mf_core::model::{BuildItem, Mesh, Model, Triangle, Vertex};
use std::collections::HashMap;
use std::io::{BufRead, BufReader, Read, Seek, SeekFrom, Write};

/// Detected STL file format.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StlFormat {
    /// Binary STL — compact 80-byte header followed by packed triangle records.
    Binary,
    /// ASCII STL — human-readable `solid`/`facet normal`/`vertex` text format.
    Ascii,
}

/// Detects whether an STL file is binary or ASCII.
///
/// Uses the reliable size-formula check to disambiguate binary files whose headers
/// begin with the ASCII text "solid" (a common case with many CAD tools).
///
/// # Arguments
///
/// * `reader` - Any type implementing [`Read`] + [`Seek`]
///
/// # Returns
///
/// - [`StlFormat::Binary`] if the file matches the binary STL size formula, or if the
///   first 5 bytes are not `solid` (case-insensitive).
/// - [`StlFormat::Ascii`] otherwise.
///
/// After return, the reader position is reset to 0.
pub fn detect_stl_format<R: Read + Seek>(reader: &mut R) -> Result<StlFormat> {
    let mut buf = [0u8; 84];
    let n = reader.read(&mut buf).map_err(Lib3mfError::Io)?;
    reader.seek(SeekFrom::Start(0)).map_err(Lib3mfError::Io)?;

    if n < 5 {
        // Very small file — treat as ASCII
        return Ok(StlFormat::Ascii);
    }

    if !buf[..5].eq_ignore_ascii_case(b"solid") {
        return Ok(StlFormat::Binary);
    }

    // First 5 bytes are "solid" — could be ASCII or binary with "solid" in header.
    // Use size formula: binary STL = 84 + triangle_count * 50 bytes.
    if n >= 84 {
        let tri_count = u32::from_le_bytes([buf[80], buf[81], buf[82], buf[83]]);
        let expected_binary_size = 84u64 + tri_count as u64 * 50;
        let file_size = reader.seek(SeekFrom::End(0)).map_err(Lib3mfError::Io)?;
        reader.seek(SeekFrom::Start(0)).map_err(Lib3mfError::Io)?;
        if file_size == expected_binary_size {
            return Ok(StlFormat::Binary);
        }
    }

    Ok(StlFormat::Ascii)
}

/// Imports STL files (binary or ASCII) into 3MF [`Model`] structures.
///
/// The importer supports both binary and ASCII STL formats:
///
/// - [`read()`]: Auto-detects the format using the file size formula, then dispatches
///   to the appropriate parser. Requires `Read + Seek`.
/// - [`read_binary()`]: Explicit binary-format parser. Requires only `Read`.
/// - [`read_ascii()`]: Explicit ASCII-format parser. Requires only `Read`.
///
/// Vertices are deduplicated using bitwise float comparison during import.
///
/// [`read()`]: StlImporter::read
/// [`read_binary()`]: StlImporter::read_binary
/// [`read_ascii()`]: StlImporter::read_ascii
/// [`Model`]: lib3mf_core::model::Model
pub struct StlImporter;

impl Default for StlImporter {
    fn default() -> Self {
        Self::new()
    }
}

impl StlImporter {
    /// Creates a new STL importer instance.
    pub fn new() -> Self {
        Self
    }

    /// Reads an STL file, auto-detecting binary vs ASCII format.
    ///
    /// Uses the file size formula to distinguish binary files (even those whose headers
    /// begin with "solid") from ASCII files.
    ///
    /// # Arguments
    ///
    /// * `reader` - Any type implementing [`Read`] + [`Seek`] containing STL data
    ///
    /// # Returns
    ///
    /// A [`Model`] containing the parsed geometry. Binary STL produces a single object;
    /// ASCII STL produces one object per solid.
    ///
    /// # Errors
    ///
    /// Returns [`Lib3mfError::Io`] if reading or seeking fails.
    /// Returns [`Lib3mfError::Validation`] or [`Lib3mfError::InvalidStructure`] if parsing fails.
    ///
    /// [`Model`]: lib3mf_core::model::Model
    /// [`Lib3mfError::Io`]: lib3mf_core::error::Lib3mfError::Io
    pub fn read<R: Read + Seek>(mut reader: R) -> Result<Model> {
        let format = detect_stl_format(&mut reader)?;
        match format {
            StlFormat::Binary => Self::read_binary(reader),
            StlFormat::Ascii => Self::read_ascii(reader),
        }
    }

    /// Reads a binary STL file and converts it to a 3MF [`Model`].
    ///
    /// # Arguments
    ///
    /// * `reader` - Any type implementing [`Read`] containing binary STL data
    ///
    /// # Returns
    ///
    /// A [`Model`] containing:
    /// - Single mesh object with ResourceId(1) named "STL Import"
    /// - All triangles from the STL file
    /// - Deduplicated vertices (using bitwise float comparison)
    /// - Single build item referencing the mesh object
    ///
    /// # Errors
    ///
    /// Returns [`Lib3mfError::Io`] if:
    /// - Cannot read 80-byte header
    /// - Cannot read triangle count
    /// - Cannot read triangle data (normals, vertices, attribute bytes)
    ///
    /// Returns [`Lib3mfError::Validation`] if triangle count field cannot be parsed.
    ///
    /// # Format Details
    ///
    /// - **Vertex deduplication**: Uses HashMap with bitwise float comparison `[x.to_bits(), y.to_bits(), z.to_bits()]`
    ///   as key. Only exactly identical vertices (bitwise) are merged.
    /// - **Normal vectors**: Read from STL but ignored (not stored in Model).
    /// - **Attribute bytes**: Read but ignored (2-byte field after each triangle).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use lib3mf_converters::stl::StlImporter;
    /// use std::fs::File;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let file = File::open("cube.stl")?;
    /// let model = StlImporter::read_binary(file)?;
    ///
    /// // Access the imported mesh
    /// let obj = model.resources.get_object(lib3mf_core::model::resources::ResourceId(1))
    ///     .expect("STL import creates object with ID 1");
    /// if let lib3mf_core::model::Geometry::Mesh(mesh) = &obj.geometry {
    ///     println!("Imported {} vertices, {} triangles",
    ///         mesh.vertices.len(), mesh.triangles.len());
    /// }
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// [`Model`]: lib3mf_core::model::Model
    /// [`Lib3mfError::Io`]: lib3mf_core::error::Lib3mfError::Io
    /// [`Lib3mfError::Validation`]: lib3mf_core::error::Lib3mfError::Validation
    pub fn read_binary<R: Read>(mut reader: R) -> Result<Model> {
        // STL Format:
        // 80 bytes header
        // 4 bytes triangle info (u32)
        // Triangles...

        let mut header = [0u8; 80];
        reader.read_exact(&mut header).map_err(Lib3mfError::Io)?;

        let triangle_count = reader.read_u32::<LittleEndian>().map_err(|_| {
            Lib3mfError::Validation("Failed to read STL triangle count".to_string())
        })?;

        let mut mesh = Mesh::default();
        let mut vert_map: HashMap<[u32; 3], u32> = HashMap::new();

        for _ in 0..triangle_count {
            // Normal (3 floats) - Ignored
            let _nx = reader.read_f32::<LittleEndian>().map_err(Lib3mfError::Io)?;
            let _ny = reader.read_f32::<LittleEndian>().map_err(Lib3mfError::Io)?;
            let _nz = reader.read_f32::<LittleEndian>().map_err(Lib3mfError::Io)?;

            let mut indices = [0u32; 3];

            for index in &mut indices {
                let x = reader.read_f32::<LittleEndian>().map_err(Lib3mfError::Io)?;
                let y = reader.read_f32::<LittleEndian>().map_err(Lib3mfError::Io)?;
                let z = reader.read_f32::<LittleEndian>().map_err(Lib3mfError::Io)?;

                let key = [x.to_bits(), y.to_bits(), z.to_bits()];

                let idx = *vert_map.entry(key).or_insert_with(|| {
                    let new_idx = mesh.vertices.len() as u32;
                    mesh.vertices.push(Vertex { x, y, z });
                    new_idx
                });
                *index = idx;
            }

            let _attr_byte_count = reader.read_u16::<LittleEndian>().map_err(Lib3mfError::Io)?;

            mesh.triangles.push(Triangle {
                v1: indices[0],
                v2: indices[1],
                v3: indices[2],
                ..Default::default()
            });
        }

        let mut model = Model::default();
        let resource_id = ResourceId(1); // Default ID

        let object = lib3mf_core::model::Object {
            id: resource_id,
            object_type: lib3mf_core::model::ObjectType::Model,
            name: Some("STL Import".to_string()),
            part_number: None,
            uuid: None,
            pid: None,
            pindex: None,
            thumbnail: None,
            geometry: lib3mf_core::model::Geometry::Mesh(mesh),
        };

        let _ = model.resources.add_object(object);

        model.build.items.push(BuildItem {
            object_id: resource_id,
            transform: glam::Mat4::IDENTITY,
            part_number: None,
            uuid: None,
            path: None,
            printable: None,
        });

        Ok(model)
    }

    /// Reads an ASCII STL file and converts it to a 3MF [`Model`].
    ///
    /// Parses one or more `solid ... endsolid` blocks. Each solid becomes a separate
    /// [`Object`] with its own [`ResourceId`] and [`BuildItem`].
    ///
    /// # Arguments
    ///
    /// * `reader` - Any type implementing [`Read`] containing ASCII STL text
    ///
    /// # Returns
    ///
    /// A [`Model`] containing:
    /// - One mesh object per solid, with ResourceIds starting at 1
    /// - Object names taken from the solid name (if any)
    /// - Deduplicated vertices per solid (using bitwise float comparison)
    /// - One build item per solid with identity transform
    ///
    /// # Errors
    ///
    /// Returns [`Lib3mfError::Io`] if reading fails.
    /// Returns [`Lib3mfError::InvalidStructure`] if vertex coordinates cannot be parsed.
    ///
    /// # Behavior
    ///
    /// - Keywords are matched case-insensitively (`SOLID`, `Facet`, `VERTEX`, etc.)
    /// - Solid names with spaces are supported (`solid My Cool Part`)
    /// - `endsolid` name is not validated (may differ from `solid` name or be absent)
    /// - Files that end without `endsolid` are handled leniently
    /// - Normal vectors from `facet normal` lines are read and discarded
    ///
    /// [`Model`]: lib3mf_core::model::Model
    /// [`Object`]: lib3mf_core::model::Object
    /// [`ResourceId`]: lib3mf_core::model::resources::ResourceId
    /// [`BuildItem`]: lib3mf_core::model::BuildItem
    pub fn read_ascii<R: Read>(reader: R) -> Result<Model> {
        let buf_reader = BufReader::new(reader);
        let mut model = Model::default();
        let mut next_id = 1u32;

        let mut current_mesh: Option<(Mesh, String)> = None;
        let mut vert_map: HashMap<[u32; 3], u32> = HashMap::new();
        // Buffer for the 3 vertices of the current facet
        let mut facet_verts: Vec<(f32, f32, f32)> = Vec::with_capacity(3);

        for line_res in buf_reader.lines() {
            let line = line_res.map_err(Lib3mfError::Io)?;
            let trimmed = line.trim();
            if trimmed.is_empty() {
                continue;
            }

            let parts: Vec<&str> = trimmed.split_whitespace().collect();
            if parts.is_empty() {
                continue;
            }

            match parts[0].to_ascii_lowercase().as_str() {
                "solid" => {
                    // Name is everything after "solid" (joined with spaces)
                    let name = if parts.len() > 1 {
                        parts[1..].join(" ")
                    } else {
                        String::new()
                    };
                    current_mesh = Some((Mesh::default(), name));
                    vert_map.clear();
                    facet_verts.clear();
                }
                "facet" => {
                    // facet normal nx ny nz — read and discard normals
                    facet_verts.clear();
                }
                "vertex" => {
                    if parts.len() >= 4 {
                        let x = parts[1].parse::<f32>().map_err(|_| {
                            Lib3mfError::InvalidStructure("Invalid STL vertex x coordinate".into())
                        })?;
                        let y = parts[2].parse::<f32>().map_err(|_| {
                            Lib3mfError::InvalidStructure("Invalid STL vertex y coordinate".into())
                        })?;
                        let z = parts[3].parse::<f32>().map_err(|_| {
                            Lib3mfError::InvalidStructure("Invalid STL vertex z coordinate".into())
                        })?;
                        facet_verts.push((x, y, z));
                    }
                }
                "endfacet" => {
                    if facet_verts.len() != 3 {
                        return Err(Lib3mfError::InvalidStructure(format!(
                            "STL facet must have exactly 3 vertices, found {}",
                            facet_verts.len()
                        )));
                    }
                    if let Some((ref mut mesh, _)) = current_mesh {
                        let mut indices = [0u32; 3];
                        for (i, &(x, y, z)) in facet_verts.iter().enumerate() {
                            let key = [x.to_bits(), y.to_bits(), z.to_bits()];
                            let idx = *vert_map.entry(key).or_insert_with(|| {
                                let new_idx = mesh.vertices.len() as u32;
                                mesh.vertices.push(Vertex { x, y, z });
                                new_idx
                            });
                            indices[i] = idx;
                        }
                        mesh.triangles.push(Triangle {
                            v1: indices[0],
                            v2: indices[1],
                            v3: indices[2],
                            ..Default::default()
                        });
                    }
                    facet_verts.clear();
                }
                "endsolid" => {
                    if let Some((mesh, name)) = current_mesh.take() {
                        finalize_solid(&mut model, mesh, name, next_id);
                        next_id += 1;
                    }
                }
                // Skip: "outer", "loop", "endloop", and any other unrecognized lines
                _ => {}
            }
        }

        // Handle file that ended without endsolid (lenient parsing)
        if let Some((mesh, name)) = current_mesh.take() {
            finalize_solid(&mut model, mesh, name, next_id);
        }

        Ok(model)
    }
}

/// Finalizes a parsed solid into a Model object and build item.
fn finalize_solid(model: &mut Model, mesh: Mesh, name: String, id: u32) {
    let resource_id = ResourceId(id);
    let object_name = if name.is_empty() { None } else { Some(name) };

    let object = lib3mf_core::model::Object {
        id: resource_id,
        object_type: lib3mf_core::model::ObjectType::Model,
        name: object_name,
        part_number: None,
        uuid: None,
        pid: None,
        pindex: None,
        thumbnail: None,
        geometry: lib3mf_core::model::Geometry::Mesh(mesh),
    };

    let _ = model.resources.add_object(object);

    model.build.items.push(BuildItem {
        object_id: resource_id,
        transform: glam::Mat4::IDENTITY,
        part_number: None,
        uuid: None,
        path: None,
        printable: None,
    });
}

/// Computes a face normal from three vertices using the cross product.
///
/// Returns a zero vector for degenerate (zero-area) triangles.
fn compute_face_normal(v1: glam::Vec3, v2: glam::Vec3, v3: glam::Vec3) -> glam::Vec3 {
    let edge1 = v2 - v1;
    let edge2 = v3 - v1;
    edge1.cross(edge2).normalize_or_zero()
}

/// Exports 3MF [`Model`] structures to binary STL files.
///
/// The exporter flattens all mesh objects referenced in build items into a single STL file,
/// applying build item transformations to vertex coordinates.
///
/// This was previously named `StlExporter`. If you were using `StlExporter`, update your
/// code to use `BinaryStlExporter`.
///
/// [`Model`]: lib3mf_core::model::Model
pub struct BinaryStlExporter;

impl BinaryStlExporter {
    /// Writes a 3MF [`Model`] to binary STL format.
    ///
    /// # Arguments
    ///
    /// * `model` - The 3MF model to export
    /// * `writer` - Any type implementing [`Write`] to receive STL data
    ///
    /// # Returns
    ///
    /// `Ok(())` on successful export.
    ///
    /// # Errors
    ///
    /// Returns [`Lib3mfError::Io`] if any write operation fails.
    ///
    /// # Format Details
    ///
    /// - **Header**: 80 zero bytes (standard for most STL files)
    /// - **Normals**: Written as (0, 0, 0) - viewers must compute face normals
    /// - **Transformations**: Build item transforms are applied to vertex coordinates
    /// - **Attribute bytes**: Written as 0 (no extended attributes)
    ///
    /// # Behavior
    ///
    /// - Only mesh objects from `model.build.items` are exported
    /// - Non-mesh geometries (Components, BooleanShape, etc.) are skipped
    /// - Each build item's transformation matrix is applied to its mesh vertices
    /// - All triangles from all build items are combined into a single STL file
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use lib3mf_converters::stl::BinaryStlExporter;
    /// use lib3mf_core::model::Model;
    /// use std::fs::File;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let model = Model::default();
    /// let output = File::create("exported.stl")?;
    /// BinaryStlExporter::write(&model, output)?;
    /// println!("Model exported successfully");
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// [`Model`]: lib3mf_core::model::Model
    /// [`Lib3mfError::Io`]: lib3mf_core::error::Lib3mfError::Io
    pub fn write<W: Write>(model: &Model, mut writer: W) -> Result<()> {
        // 1. Collect all triangles from all build items
        let mut triangles: Vec<(glam::Vec3, glam::Vec3, glam::Vec3)> = Vec::new(); // v1, v2, v3

        for item in &model.build.items {
            #[allow(clippy::collapsible_if)]
            if let Some(object) = model.resources.get_object(item.object_id) {
                if let lib3mf_core::model::Geometry::Mesh(mesh) = &object.geometry {
                    let transform = item.transform;

                    for tri in &mesh.triangles {
                        let v1_local = mesh.vertices[tri.v1 as usize];
                        let v2_local = mesh.vertices[tri.v2 as usize];
                        let v3_local = mesh.vertices[tri.v3 as usize];

                        let v1 = transform
                            .transform_point3(glam::Vec3::new(v1_local.x, v1_local.y, v1_local.z));
                        let v2 = transform
                            .transform_point3(glam::Vec3::new(v2_local.x, v2_local.y, v2_local.z));
                        let v3 = transform
                            .transform_point3(glam::Vec3::new(v3_local.x, v3_local.y, v3_local.z));

                        triangles.push((v1, v2, v3));
                    }
                }
            }
        }

        // 2. Write Header (80 bytes)
        let header = [0u8; 80];
        writer.write_all(&header).map_err(Lib3mfError::Io)?;

        // 3. Write Count
        writer
            .write_u32::<LittleEndian>(triangles.len() as u32)
            .map_err(Lib3mfError::Io)?;

        // 4. Write Triangles
        for (v1, v2, v3) in triangles {
            // Normal (0,0,0) - let viewer calculate
            writer
                .write_f32::<LittleEndian>(0.0)
                .map_err(Lib3mfError::Io)?;
            writer
                .write_f32::<LittleEndian>(0.0)
                .map_err(Lib3mfError::Io)?;
            writer
                .write_f32::<LittleEndian>(0.0)
                .map_err(Lib3mfError::Io)?;

            // v1
            writer
                .write_f32::<LittleEndian>(v1.x)
                .map_err(Lib3mfError::Io)?;
            writer
                .write_f32::<LittleEndian>(v1.y)
                .map_err(Lib3mfError::Io)?;
            writer
                .write_f32::<LittleEndian>(v1.z)
                .map_err(Lib3mfError::Io)?;

            // v2
            writer
                .write_f32::<LittleEndian>(v2.x)
                .map_err(Lib3mfError::Io)?;
            writer
                .write_f32::<LittleEndian>(v2.y)
                .map_err(Lib3mfError::Io)?;
            writer
                .write_f32::<LittleEndian>(v2.z)
                .map_err(Lib3mfError::Io)?;

            // v3
            writer
                .write_f32::<LittleEndian>(v3.x)
                .map_err(Lib3mfError::Io)?;
            writer
                .write_f32::<LittleEndian>(v3.y)
                .map_err(Lib3mfError::Io)?;
            writer
                .write_f32::<LittleEndian>(v3.z)
                .map_err(Lib3mfError::Io)?;

            // Attribute byte count (0)
            writer
                .write_u16::<LittleEndian>(0)
                .map_err(Lib3mfError::Io)?;
        }

        Ok(())
    }

    /// Writes a 3MF [`Model`] to binary STL format with support for multi-part 3MF files.
    ///
    /// This method extends [`write`] by recursively resolving component references and external
    /// model parts using a [`PartResolver`]. This is necessary for 3MF files with the Production
    /// Extension that reference objects from external model parts.
    ///
    /// # Arguments
    ///
    /// * `model` - The root 3MF model to export
    /// * `resolver` - A [`PartResolver`] for loading external model parts from the 3MF archive
    /// * `writer` - Any type implementing [`Write`] to receive STL data
    ///
    /// # Returns
    ///
    /// `Ok(())` on successful export.
    ///
    /// # Errors
    ///
    /// Returns [`Lib3mfError::Io`] if any write operation fails.
    ///
    /// Returns errors from the resolver if external parts cannot be loaded.
    ///
    /// # Behavior
    ///
    /// - Recursively resolves component hierarchies using the PartResolver
    /// - Follows external references via component `path` attributes
    /// - Applies accumulated transformations through the component tree
    /// - Flattens all resolved meshes into a single STL file
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use lib3mf_converters::stl::BinaryStlExporter;
    /// use lib3mf_core::archive::ZipArchiver;
    /// use lib3mf_core::model::resolver::PartResolver;
    /// use std::fs::File;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let model = lib3mf_core::model::Model::default();
    /// let archive_file = File::open("multipart.3mf")?;
    /// let mut archiver = ZipArchiver::new(archive_file)?;
    /// let resolver = PartResolver::new(&mut archiver, model.clone());
    ///
    /// let output = File::create("output.stl")?;
    /// BinaryStlExporter::write_with_resolver(&model, resolver, output)?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// [`write`]: BinaryStlExporter::write
    /// [`Model`]: lib3mf_core::model::Model
    /// [`PartResolver`]: lib3mf_core::model::resolver::PartResolver
    /// [`Lib3mfError::Io`]: lib3mf_core::error::Lib3mfError::Io
    pub fn write_with_resolver<W: Write, A: lib3mf_core::archive::ArchiveReader>(
        model: &Model,
        mut resolver: lib3mf_core::model::resolver::PartResolver<A>,
        mut writer: W,
    ) -> Result<()> {
        // 1. Collect all triangles from all build items (recursively)
        let mut triangles: Vec<(glam::Vec3, glam::Vec3, glam::Vec3)> = Vec::new();

        for item in &model.build.items {
            collect_triangles(
                &mut resolver,
                item.object_id,
                item.transform,
                None, // Start with root path (None)
                &mut triangles,
            )?;
        }

        // 2. Write Header (80 bytes)
        let header = [0u8; 80];
        writer.write_all(&header).map_err(Lib3mfError::Io)?;

        // 3. Write Count
        writer
            .write_u32::<LittleEndian>(triangles.len() as u32)
            .map_err(Lib3mfError::Io)?;

        // 4. Write Triangles
        for (v1, v2, v3) in triangles {
            // Normal (0,0,0) - let viewer calculate
            writer
                .write_f32::<LittleEndian>(0.0)
                .map_err(Lib3mfError::Io)?;
            writer
                .write_f32::<LittleEndian>(0.0)
                .map_err(Lib3mfError::Io)?;
            writer
                .write_f32::<LittleEndian>(0.0)
                .map_err(Lib3mfError::Io)?;

            // v1
            writer
                .write_f32::<LittleEndian>(v1.x)
                .map_err(Lib3mfError::Io)?;
            writer
                .write_f32::<LittleEndian>(v1.y)
                .map_err(Lib3mfError::Io)?;
            writer
                .write_f32::<LittleEndian>(v1.z)
                .map_err(Lib3mfError::Io)?;

            // v2
            writer
                .write_f32::<LittleEndian>(v2.x)
                .map_err(Lib3mfError::Io)?;
            writer
                .write_f32::<LittleEndian>(v2.y)
                .map_err(Lib3mfError::Io)?;
            writer
                .write_f32::<LittleEndian>(v2.z)
                .map_err(Lib3mfError::Io)?;

            // v3
            writer
                .write_f32::<LittleEndian>(v3.x)
                .map_err(Lib3mfError::Io)?;
            writer
                .write_f32::<LittleEndian>(v3.y)
                .map_err(Lib3mfError::Io)?;
            writer
                .write_f32::<LittleEndian>(v3.z)
                .map_err(Lib3mfError::Io)?;

            // Attribute byte count (0)
            writer
                .write_u16::<LittleEndian>(0)
                .map_err(Lib3mfError::Io)?;
        }

        Ok(())
    }
}

/// Exports 3MF [`Model`] structures to ASCII STL files.
///
/// Each mesh object in the model's build items is written as a separate ASCII STL solid.
/// Face normals are computed from the triangle edges using the cross product.
///
/// [`Model`]: lib3mf_core::model::Model
pub struct AsciiStlExporter;

impl AsciiStlExporter {
    /// Writes a 3MF [`Model`] to ASCII STL format.
    ///
    /// Each mesh object referenced in the model's build items is written as a separate
    /// `solid ... endsolid` block. The solid name is taken from the object's name field,
    /// or left empty if the object has no name.
    ///
    /// # Arguments
    ///
    /// * `model` - The 3MF model to export
    /// * `writer` - Any type implementing [`Write`] to receive ASCII STL text
    ///
    /// # Returns
    ///
    /// `Ok(())` on successful export.
    ///
    /// # Errors
    ///
    /// Returns [`Lib3mfError::Io`] if any write operation fails.
    ///
    /// # Format Details
    ///
    /// - **Normals**: Computed via cross product of triangle edges (`normalize_or_zero()`)
    /// - **Degenerate triangles**: Emit zero normal `(0 0 0)`, triangle is not skipped
    /// - **Normal format**: Scientific notation with 6 decimal places (`{:.6e}`)
    /// - **Vertex format**: Fixed-point with 6 decimal places (`{:.6}`)
    /// - **Transformations**: Build item transforms are applied to vertex coordinates
    /// - **Solid names**: Taken from `object.name`, empty string if `None`
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use lib3mf_converters::stl::AsciiStlExporter;
    /// use lib3mf_core::model::Model;
    /// use std::fs::File;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let model = Model::default();
    /// let output = File::create("exported.stl")?;
    /// AsciiStlExporter::write(&model, output)?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// [`Model`]: lib3mf_core::model::Model
    /// [`Lib3mfError::Io`]: lib3mf_core::error::Lib3mfError::Io
    pub fn write<W: Write>(model: &Model, mut writer: W) -> Result<()> {
        for item in &model.build.items {
            #[allow(clippy::collapsible_if)]
            if let Some(object) = model.resources.get_object(item.object_id) {
                if let lib3mf_core::model::Geometry::Mesh(mesh) = &object.geometry {
                    let name = object.name.as_deref().unwrap_or("");
                    let transform = item.transform;

                    writeln!(writer, "solid {name}").map_err(Lib3mfError::Io)?;

                    for tri in &mesh.triangles {
                        let v1_local = mesh.vertices[tri.v1 as usize];
                        let v2_local = mesh.vertices[tri.v2 as usize];
                        let v3_local = mesh.vertices[tri.v3 as usize];

                        let v1 = transform
                            .transform_point3(glam::Vec3::new(v1_local.x, v1_local.y, v1_local.z));
                        let v2 = transform
                            .transform_point3(glam::Vec3::new(v2_local.x, v2_local.y, v2_local.z));
                        let v3 = transform
                            .transform_point3(glam::Vec3::new(v3_local.x, v3_local.y, v3_local.z));

                        let normal = compute_face_normal(v1, v2, v3);

                        writeln!(
                            writer,
                            "  facet normal {:.6e} {:.6e} {:.6e}",
                            normal.x, normal.y, normal.z
                        )
                        .map_err(Lib3mfError::Io)?;
                        writeln!(writer, "    outer loop").map_err(Lib3mfError::Io)?;
                        writeln!(writer, "      vertex {:.6} {:.6} {:.6}", v1.x, v1.y, v1.z)
                            .map_err(Lib3mfError::Io)?;
                        writeln!(writer, "      vertex {:.6} {:.6} {:.6}", v2.x, v2.y, v2.z)
                            .map_err(Lib3mfError::Io)?;
                        writeln!(writer, "      vertex {:.6} {:.6} {:.6}", v3.x, v3.y, v3.z)
                            .map_err(Lib3mfError::Io)?;
                        writeln!(writer, "    endloop").map_err(Lib3mfError::Io)?;
                        writeln!(writer, "  endfacet").map_err(Lib3mfError::Io)?;
                    }

                    writeln!(writer, "endsolid {name}").map_err(Lib3mfError::Io)?;
                }
            }
        }
        Ok(())
    }

    /// Writes a 3MF [`Model`] to ASCII STL format with support for multi-part 3MF files.
    ///
    /// Resolves component references and external model parts using a [`PartResolver`],
    /// then writes all collected triangles as a single ASCII STL solid.
    ///
    /// # Arguments
    ///
    /// * `model` - The root 3MF model to export
    /// * `resolver` - A [`PartResolver`] for loading external model parts from the 3MF archive
    /// * `writer` - Any type implementing [`Write`] to receive ASCII STL text
    ///
    /// # Returns
    ///
    /// `Ok(())` on successful export.
    ///
    /// # Errors
    ///
    /// Returns [`Lib3mfError::Io`] if any write operation fails.
    /// Returns errors from the resolver if external parts cannot be loaded.
    ///
    /// [`Model`]: lib3mf_core::model::Model
    /// [`PartResolver`]: lib3mf_core::model::resolver::PartResolver
    pub fn write_with_resolver<W: Write, A: lib3mf_core::archive::ArchiveReader>(
        model: &Model,
        mut resolver: lib3mf_core::model::resolver::PartResolver<A>,
        mut writer: W,
    ) -> Result<()> {
        // Collect all triangles from all build items (recursively)
        let mut triangles: Vec<(glam::Vec3, glam::Vec3, glam::Vec3)> = Vec::new();

        for item in &model.build.items {
            collect_triangles(
                &mut resolver,
                item.object_id,
                item.transform,
                None,
                &mut triangles,
            )?;
        }

        // Write as a single solid (resolver flattens all objects)
        writeln!(writer, "solid ").map_err(Lib3mfError::Io)?;

        for (v1, v2, v3) in triangles {
            let normal = compute_face_normal(v1, v2, v3);

            writeln!(
                writer,
                "  facet normal {:.6e} {:.6e} {:.6e}",
                normal.x, normal.y, normal.z
            )
            .map_err(Lib3mfError::Io)?;
            writeln!(writer, "    outer loop").map_err(Lib3mfError::Io)?;
            writeln!(writer, "      vertex {:.6} {:.6} {:.6}", v1.x, v1.y, v1.z)
                .map_err(Lib3mfError::Io)?;
            writeln!(writer, "      vertex {:.6} {:.6} {:.6}", v2.x, v2.y, v2.z)
                .map_err(Lib3mfError::Io)?;
            writeln!(writer, "      vertex {:.6} {:.6} {:.6}", v3.x, v3.y, v3.z)
                .map_err(Lib3mfError::Io)?;
            writeln!(writer, "    endloop").map_err(Lib3mfError::Io)?;
            writeln!(writer, "  endfacet").map_err(Lib3mfError::Io)?;
        }

        writeln!(writer, "endsolid ").map_err(Lib3mfError::Io)?;

        Ok(())
    }
}

fn collect_triangles<A: lib3mf_core::archive::ArchiveReader>(
    resolver: &mut lib3mf_core::model::resolver::PartResolver<A>,
    object_id: ResourceId,
    transform: glam::Mat4,
    path: Option<&str>,
    triangles: &mut Vec<(glam::Vec3, glam::Vec3, glam::Vec3)>,
) -> Result<()> {
    // Resolve geometry
    // Note: We need to clone the geometry or handle the borrow of resolver carefully.
    // resolving returns a reference to Model and Object, which borrows from resolver.
    // We can't keep that borrow while recursing (mutably borrowing resolver).
    // So we need to clone the relevant data (Geometry) or restructure.
    // Cloning Geometry (which contains Mesh) might be expensive but safe.
    // OR: resolve_object returns reference, we inspect it, then drop reference before recursing.

    let geometry = {
        let res = resolver.resolve_object(object_id, path)?;
        if let Some((_, obj)) = res {
            Some(obj.geometry.clone()) // Cloning geometry to release borrow
        } else {
            None
        }
    };

    if let Some(geo) = geometry {
        match geo {
            lib3mf_core::model::Geometry::Mesh(mesh) => {
                for tri in &mesh.triangles {
                    let v1_local = mesh.vertices[tri.v1 as usize];
                    let v2_local = mesh.vertices[tri.v2 as usize];
                    let v3_local = mesh.vertices[tri.v3 as usize];

                    let v1 = transform
                        .transform_point3(glam::Vec3::new(v1_local.x, v1_local.y, v1_local.z));
                    let v2 = transform
                        .transform_point3(glam::Vec3::new(v2_local.x, v2_local.y, v2_local.z));
                    let v3 = transform
                        .transform_point3(glam::Vec3::new(v3_local.x, v3_local.y, v3_local.z));

                    triangles.push((v1, v2, v3));
                }
            }
            lib3mf_core::model::Geometry::Components(comps) => {
                for comp in comps.components {
                    let new_transform = transform * comp.transform;
                    let next_path_store = comp.path.clone();
                    let next_path = next_path_store.as_deref().or(path);

                    collect_triangles(
                        resolver,
                        comp.object_id,
                        new_transform,
                        next_path,
                        triangles,
                    )?;
                }
            }
            _ => {}
        }
    }

    Ok(())
}

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

    // ===== Helper functions =====

    /// Build a minimal binary STL with the given triangles.
    fn make_binary_stl(
        header: &[u8; 80],
        triangles: &[(f32, f32, f32, f32, f32, f32, f32, f32, f32)],
    ) -> Vec<u8> {
        // Each element: (v1x, v1y, v1z, v2x, v2y, v2z, v3x, v3y, v3z)
        use byteorder::{LittleEndian, WriteBytesExt};
        let mut buf = Vec::new();
        buf.extend_from_slice(header);
        buf.write_u32::<LittleEndian>(triangles.len() as u32)
            .unwrap();
        for &(v1x, v1y, v1z, v2x, v2y, v2z, v3x, v3y, v3z) in triangles {
            // normal (0,0,0)
            buf.write_f32::<LittleEndian>(0.0).unwrap();
            buf.write_f32::<LittleEndian>(0.0).unwrap();
            buf.write_f32::<LittleEndian>(0.0).unwrap();
            // v1
            buf.write_f32::<LittleEndian>(v1x).unwrap();
            buf.write_f32::<LittleEndian>(v1y).unwrap();
            buf.write_f32::<LittleEndian>(v1z).unwrap();
            // v2
            buf.write_f32::<LittleEndian>(v2x).unwrap();
            buf.write_f32::<LittleEndian>(v2y).unwrap();
            buf.write_f32::<LittleEndian>(v2z).unwrap();
            // v3
            buf.write_f32::<LittleEndian>(v3x).unwrap();
            buf.write_f32::<LittleEndian>(v3y).unwrap();
            buf.write_f32::<LittleEndian>(v3z).unwrap();
            // attribute byte count
            buf.write_u16::<LittleEndian>(0).unwrap();
        }
        buf
    }

    /// Build a simple model with one mesh object containing the given vertices and triangles.
    fn make_simple_model(
        vertices: Vec<(f32, f32, f32)>,
        triangles: Vec<(u32, u32, u32)>,
        name: Option<&str>,
    ) -> Model {
        use lib3mf_core::model::{Geometry, Object, ObjectType};

        let mut mesh = Mesh::default();
        for (x, y, z) in vertices {
            mesh.vertices.push(Vertex { x, y, z });
        }
        for (v1, v2, v3) in triangles {
            mesh.triangles.push(Triangle {
                v1,
                v2,
                v3,
                ..Default::default()
            });
        }

        let resource_id = ResourceId(1);
        let object = Object {
            id: resource_id,
            object_type: ObjectType::Model,
            name: name.map(|s| s.to_string()),
            part_number: None,
            uuid: None,
            pid: None,
            pindex: None,
            thumbnail: None,
            geometry: Geometry::Mesh(mesh),
        };

        let mut model = Model::default();
        let _ = model.resources.add_object(object);
        model.build.items.push(BuildItem {
            object_id: resource_id,
            transform: glam::Mat4::IDENTITY,
            part_number: None,
            uuid: None,
            path: None,
            printable: None,
        });
        model
    }

    // ===== Test 1: detect_stl_format returns Binary for binary STL =====

    #[test]
    fn test_detect_binary_format() {
        let header = [0u8; 80];
        let tris = vec![(0.0f32, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0)];
        let data = make_binary_stl(&header, &tris);
        let mut cursor = Cursor::new(data);
        let fmt = detect_stl_format(&mut cursor).expect("detect should succeed");
        assert_eq!(fmt, StlFormat::Binary);
    }

    // ===== Test 2: detect_stl_format returns Ascii for ASCII STL =====

    #[test]
    fn test_detect_ascii_format() {
        let ascii = "solid test\nendsolid test\n";
        let mut cursor = Cursor::new(ascii.as_bytes().to_vec());
        let fmt = detect_stl_format(&mut cursor).expect("detect should succeed");
        assert_eq!(fmt, StlFormat::Ascii);
    }

    // ===== Test 3: detect returns Binary even when header starts with "solid" =====

    #[test]
    fn test_detect_binary_with_solid_header() {
        // Build binary STL whose 80-byte header starts with "solid"
        let mut header = [0u8; 80];
        let solid_bytes = b"solid binary_test_header";
        header[..solid_bytes.len()].copy_from_slice(solid_bytes);

        let tris = vec![(0.0f32, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0)];
        let data = make_binary_stl(&header, &tris);

        // Verify the size formula: 84 + 1 * 50 = 134
        assert_eq!(data.len(), 134);

        let mut cursor = Cursor::new(data);
        let fmt = detect_stl_format(&mut cursor).expect("detect should succeed");
        assert_eq!(
            fmt,
            StlFormat::Binary,
            "Binary STL with 'solid' in header must be detected as Binary"
        );
    }

    // ===== Test 4: read_ascii parses a simple single-triangle STL =====

    #[test]
    fn test_read_ascii_simple_triangle() {
        let ascii = "\
solid triangle
  facet normal 0 0 1
    outer loop
      vertex 0 0 0
      vertex 1 0 0
      vertex 0 1 0
    endloop
  endfacet
endsolid triangle
";
        let model = StlImporter::read_ascii(Cursor::new(ascii)).expect("parse should succeed");

        assert_eq!(model.build.items.len(), 1);
        let obj = model
            .resources
            .get_object(ResourceId(1))
            .expect("object 1 should exist");
        if let lib3mf_core::model::Geometry::Mesh(mesh) = &obj.geometry {
            assert_eq!(mesh.vertices.len(), 3, "should have 3 unique vertices");
            assert_eq!(mesh.triangles.len(), 1, "should have 1 triangle");
            // Check vertex coordinates
            assert!((mesh.vertices[0].x - 0.0).abs() < 1e-6);
            assert!((mesh.vertices[1].x - 1.0).abs() < 1e-6);
            assert!((mesh.vertices[2].y - 1.0).abs() < 1e-6);
        } else {
            panic!("expected Mesh geometry");
        }
    }

    // ===== Test 5: case-insensitive keywords =====

    #[test]
    fn test_read_ascii_case_insensitive() {
        let ascii = "\
SOLID uppercase
  FACET NORMAL 0 0 1
    OUTER LOOP
      VERTEX 0 0 0
      VERTEX 1 0 0
      VERTEX 0 1 0
    ENDLOOP
  ENDFACET
ENDSOLID uppercase
";
        let model = StlImporter::read_ascii(Cursor::new(ascii)).expect("parse should succeed");
        assert_eq!(model.build.items.len(), 1);
        let obj = model.resources.get_object(ResourceId(1)).unwrap();
        if let lib3mf_core::model::Geometry::Mesh(mesh) = &obj.geometry {
            assert_eq!(mesh.triangles.len(), 1);
        } else {
            panic!("expected Mesh geometry");
        }
    }

    // ===== Test 6: multi-solid creates multiple objects =====

    #[test]
    fn test_read_ascii_multi_solid() {
        let ascii = "\
solid part_a
  facet normal 0 0 1
    outer loop
      vertex 0 0 0
      vertex 1 0 0
      vertex 0 1 0
    endloop
  endfacet
endsolid part_a
solid part_b
  facet normal 0 0 -1
    outer loop
      vertex 0 0 1
      vertex 1 0 1
      vertex 0 1 1
    endloop
  endfacet
endsolid part_b
";
        let model = StlImporter::read_ascii(Cursor::new(ascii)).expect("parse should succeed");
        assert_eq!(model.build.items.len(), 2, "should have 2 build items");

        let obj1 = model.resources.get_object(ResourceId(1)).expect("object 1");
        let obj2 = model.resources.get_object(ResourceId(2)).expect("object 2");

        if let lib3mf_core::model::Geometry::Mesh(m) = &obj1.geometry {
            assert_eq!(m.triangles.len(), 1);
        }
        if let lib3mf_core::model::Geometry::Mesh(m) = &obj2.geometry {
            assert_eq!(m.triangles.len(), 1);
        }
    }

    // ===== Test 7: solid name with spaces =====

    #[test]
    fn test_read_ascii_solid_name_with_spaces() {
        let ascii = "\
solid My Cool Part
  facet normal 0 0 1
    outer loop
      vertex 0 0 0
      vertex 1 0 0
      vertex 0 1 0
    endloop
  endfacet
endsolid My Cool Part
";
        let model = StlImporter::read_ascii(Cursor::new(ascii)).expect("parse should succeed");
        let obj = model.resources.get_object(ResourceId(1)).expect("object 1");
        assert_eq!(obj.name, Some("My Cool Part".to_string()));
    }

    // ===== Test 8: vertex deduplication =====

    #[test]
    fn test_read_ascii_vertex_dedup() {
        // Two triangles sharing two vertices
        let ascii = "\
solid dedup
  facet normal 0 0 1
    outer loop
      vertex 0 0 0
      vertex 1 0 0
      vertex 0 1 0
    endloop
  endfacet
  facet normal 0 0 1
    outer loop
      vertex 1 0 0
      vertex 1 1 0
      vertex 0 1 0
    endloop
  endfacet
endsolid dedup
";
        let model = StlImporter::read_ascii(Cursor::new(ascii)).expect("parse should succeed");
        let obj = model.resources.get_object(ResourceId(1)).expect("object 1");
        if let lib3mf_core::model::Geometry::Mesh(mesh) = &obj.geometry {
            assert_eq!(mesh.triangles.len(), 2);
            // 4 unique vertices: (0,0,0), (1,0,0), (0,1,0), (1,1,0)
            assert_eq!(
                mesh.vertices.len(),
                4,
                "shared vertices should be deduplicated"
            );
        } else {
            panic!("expected Mesh");
        }
    }

    // ===== Test 9: mismatched endsolid name is accepted =====

    #[test]
    fn test_read_ascii_mismatched_endsolid() {
        let ascii = "\
solid foo
  facet normal 0 0 1
    outer loop
      vertex 0 0 0
      vertex 1 0 0
      vertex 0 1 0
    endloop
  endfacet
endsolid bar
";
        let model = StlImporter::read_ascii(Cursor::new(ascii)).expect("parse should succeed");
        assert_eq!(
            model.build.items.len(),
            1,
            "mismatched endsolid name should not cause error"
        );
    }

    // ===== Test 10: file ends without endsolid =====

    #[test]
    fn test_read_ascii_no_endsolid() {
        let ascii = "\
solid truncated
  facet normal 0 0 1
    outer loop
      vertex 0 0 0
      vertex 1 0 0
      vertex 0 1 0
    endloop
  endfacet
";
        let model = StlImporter::read_ascii(Cursor::new(ascii)).expect("parse should succeed");
        assert_eq!(
            model.build.items.len(),
            1,
            "truncated file should still produce one object"
        );
        let obj = model.resources.get_object(ResourceId(1)).expect("object 1");
        if let lib3mf_core::model::Geometry::Mesh(mesh) = &obj.geometry {
            assert_eq!(mesh.triangles.len(), 1);
        }
    }

    // ===== Test 11: write_ascii_simple produces expected keywords =====

    #[test]
    fn test_write_ascii_simple() {
        // A single triangle in the XY plane with normal pointing +Z
        let model = make_simple_model(
            vec![(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)],
            vec![(0, 1, 2)],
            Some("test"),
        );

        let mut output = Vec::new();
        AsciiStlExporter::write(&model, &mut output).expect("write should succeed");
        let text = String::from_utf8(output).expect("valid UTF-8");

        assert!(
            text.contains("solid test"),
            "should contain solid keyword with name"
        );
        assert!(text.contains("facet normal"), "should contain facet normal");
        assert!(text.contains("outer loop"), "should contain outer loop");
        assert!(text.contains("vertex"), "should contain vertex lines");
        assert!(text.contains("endloop"), "should contain endloop");
        assert!(text.contains("endfacet"), "should contain endfacet");
        assert!(
            text.contains("endsolid test"),
            "should contain endsolid keyword with name"
        );

        // Normal should be non-zero for a valid triangle
        // The normal should be (0, 0, 1) for the XY-plane triangle
        let has_nonzero_normal = text
            .lines()
            .filter(|l| l.trim().starts_with("facet normal"))
            .any(|l| {
                let parts: Vec<&str> = l.split_whitespace().collect();
                if parts.len() >= 5 {
                    let nz: f64 = parts[4].parse().unwrap_or(0.0);
                    nz.abs() > 0.5
                } else {
                    false
                }
            });
        assert!(
            has_nonzero_normal,
            "normal should be non-zero for valid triangle"
        );
    }

    // ===== Test 12: degenerate triangle emits zero normal =====

    #[test]
    fn test_write_ascii_degenerate_normal() {
        // All three vertices are collinear — degenerate triangle
        let model = make_simple_model(
            vec![(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (2.0, 0.0, 0.0)],
            vec![(0, 1, 2)],
            None,
        );

        let mut output = Vec::new();
        AsciiStlExporter::write(&model, &mut output).expect("write should succeed");
        let text = String::from_utf8(output).expect("valid UTF-8");

        let normal_line = text
            .lines()
            .find(|l| l.trim().starts_with("facet normal"))
            .expect("should have facet normal line");

        let parts: Vec<&str> = normal_line.split_whitespace().collect();
        assert!(parts.len() >= 5, "facet normal line should have 5 parts");
        let nx: f64 = parts[2].parse().unwrap_or(f64::NAN);
        let ny: f64 = parts[3].parse().unwrap_or(f64::NAN);
        let nz: f64 = parts[4].parse().unwrap_or(f64::NAN);
        assert!(
            nx.abs() < 1e-6 && ny.abs() < 1e-6 && nz.abs() < 1e-6,
            "degenerate triangle normal should be (0,0,0), got ({nx}, {ny}, {nz})"
        );
    }

    // ===== Test 13: object name in solid/endsolid =====

    #[test]
    fn test_write_ascii_object_name() {
        let model = make_simple_model(
            vec![(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)],
            vec![(0, 1, 2)],
            Some("MyPart"),
        );

        let mut output = Vec::new();
        AsciiStlExporter::write(&model, &mut output).expect("write should succeed");
        let text = String::from_utf8(output).expect("valid UTF-8");

        let first_line = text.lines().next().expect("should have lines");
        assert_eq!(
            first_line, "solid MyPart",
            "first line should be 'solid MyPart'"
        );

        let last_line = text
            .lines()
            .filter(|l| !l.is_empty())
            .last()
            .expect("should have lines");
        assert_eq!(
            last_line, "endsolid MyPart",
            "last line should be 'endsolid MyPart'"
        );
    }

    // ===== Test 14: roundtrip ASCII STL -> Model -> ASCII STL -> Model =====

    #[test]
    fn test_roundtrip_ascii() {
        // Create a simple model: 4 vertices, 2 triangles (a flat square)
        let model = make_simple_model(
            vec![
                (0.0, 0.0, 0.0),
                (1.0, 0.0, 0.0),
                (1.0, 1.0, 0.0),
                (0.0, 1.0, 0.0),
            ],
            vec![(0, 1, 2), (0, 2, 3)],
            Some("RoundtripTest"),
        );

        // Write to ASCII
        let mut buf1 = Vec::new();
        AsciiStlExporter::write(&model, &mut buf1).expect("first write should succeed");

        // Parse back
        let model2 =
            StlImporter::read_ascii(Cursor::new(&buf1)).expect("first re-read should succeed");

        // Write again
        let mut buf2 = Vec::new();
        AsciiStlExporter::write(&model2, &mut buf2).expect("second write should succeed");

        // Parse again
        let model3 =
            StlImporter::read_ascii(Cursor::new(&buf2)).expect("second re-read should succeed");

        // Compare: same vertex count, triangle count, and positions
        let get_mesh_info = |m: &Model| -> (usize, usize, Vec<(f32, f32, f32)>) {
            let obj = m.resources.get_object(ResourceId(1)).expect("object 1");
            if let lib3mf_core::model::Geometry::Mesh(mesh) = &obj.geometry {
                let verts: Vec<(f32, f32, f32)> =
                    mesh.vertices.iter().map(|v| (v.x, v.y, v.z)).collect();
                (mesh.vertices.len(), mesh.triangles.len(), verts)
            } else {
                panic!("expected Mesh");
            }
        };

        let (v_count2, t_count2, verts2) = get_mesh_info(&model2);
        let (v_count3, t_count3, verts3) = get_mesh_info(&model3);

        assert_eq!(
            v_count2, v_count3,
            "vertex count must be stable across roundtrips"
        );
        assert_eq!(
            t_count2, t_count3,
            "triangle count must be stable across roundtrips"
        );

        // Vertex positions should match within f32 formatting tolerance (1e-5)
        for (i, (&(x2, y2, z2), &(x3, y3, z3))) in verts2.iter().zip(verts3.iter()).enumerate() {
            assert!(
                (x2 - x3).abs() < 1e-5 && (y2 - y3).abs() < 1e-5 && (z2 - z3).abs() < 1e-5,
                "vertex {i} position mismatch: ({x2},{y2},{z2}) vs ({x3},{y3},{z3})"
            );
        }

        // Original model should also match second model structurally
        assert_eq!(v_count2, 4, "should have 4 vertices");
        assert_eq!(t_count2, 2, "should have 2 triangles");
    }

    // ===== Test 15: BinaryStlExporter::write produces correct binary output =====

    #[test]
    fn test_write_binary_simple() {
        use byteorder::{LittleEndian, ReadBytesExt};

        // One triangle: vertices at (0,0,0), (1,0,0), (0,1,0)
        let model = make_simple_model(
            vec![(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)],
            vec![(0, 1, 2)],
            None,
        );

        let mut buf = Vec::new();
        BinaryStlExporter::write(&model, Cursor::new(&mut buf)).expect("write should succeed");

        // Total: 80-byte header + 4-byte count + 1 * 50-byte triangle = 134
        assert_eq!(
            buf.len(),
            134,
            "binary STL size should be 134 bytes for 1 triangle"
        );

        // Triangle count at bytes 80..84
        let mut count_bytes = Cursor::new(&buf[80..84]);
        let tri_count = count_bytes.read_u32::<LittleEndian>().unwrap();
        assert_eq!(tri_count, 1, "triangle count should be 1");

        // Triangle data layout (per triangle):
        //   bytes 84..96:  normal (3 x f32 = 12 bytes)
        //   bytes 96..108: vertex 1 (3 x f32 = 12 bytes)
        //   bytes 108..120: vertex 2 (3 x f32 = 12 bytes)
        //   bytes 120..132: vertex 3 (3 x f32 = 12 bytes)
        //   bytes 132..134: attribute byte count (u16 = 2 bytes)
        let mut tri_cursor = Cursor::new(&buf[96..]);

        // Vertex 1: (0,0,0)
        let v1x = tri_cursor.read_f32::<LittleEndian>().unwrap();
        let v1y = tri_cursor.read_f32::<LittleEndian>().unwrap();
        let v1z = tri_cursor.read_f32::<LittleEndian>().unwrap();
        assert!((v1x - 0.0).abs() < 1e-6, "v1.x should be 0.0, got {v1x}");
        assert!((v1y - 0.0).abs() < 1e-6, "v1.y should be 0.0, got {v1y}");
        assert!((v1z - 0.0).abs() < 1e-6, "v1.z should be 0.0, got {v1z}");

        // Vertex 2: (1,0,0)
        let v2x = tri_cursor.read_f32::<LittleEndian>().unwrap();
        let v2y = tri_cursor.read_f32::<LittleEndian>().unwrap();
        let v2z = tri_cursor.read_f32::<LittleEndian>().unwrap();
        assert!((v2x - 1.0).abs() < 1e-6, "v2.x should be 1.0, got {v2x}");
        assert!((v2y - 0.0).abs() < 1e-6, "v2.y should be 0.0, got {v2y}");
        assert!((v2z - 0.0).abs() < 1e-6, "v2.z should be 0.0, got {v2z}");

        // Vertex 3: (0,1,0)
        let v3x = tri_cursor.read_f32::<LittleEndian>().unwrap();
        let v3y = tri_cursor.read_f32::<LittleEndian>().unwrap();
        let v3z = tri_cursor.read_f32::<LittleEndian>().unwrap();
        assert!((v3x - 0.0).abs() < 1e-6, "v3.x should be 0.0, got {v3x}");
        assert!((v3y - 1.0).abs() < 1e-6, "v3.y should be 1.0, got {v3y}");
        assert!((v3z - 0.0).abs() < 1e-6, "v3.z should be 0.0, got {v3z}");
    }

    // ===== Test 16: BinaryStlExporter::write roundtrip preserves triangle count =====

    #[test]
    fn test_roundtrip_binary() {
        // Create a flat square with 4 vertices and 2 triangles
        let model = make_simple_model(
            vec![
                (0.0, 0.0, 0.0),
                (1.0, 0.0, 0.0),
                (1.0, 1.0, 0.0),
                (0.0, 1.0, 0.0),
            ],
            vec![(0, 1, 2), (0, 2, 3)],
            None,
        );

        // Write to binary STL
        let mut buf = Vec::new();
        BinaryStlExporter::write(&model, Cursor::new(&mut buf)).expect("write should succeed");

        // Read back using StlImporter
        let model2 =
            StlImporter::read(Cursor::new(buf)).expect("binary roundtrip read should succeed");

        // Binary STL import deduplicates vertices, so only check triangle count
        let obj2 = model2
            .resources
            .get_object(ResourceId(1))
            .expect("object 1");
        if let lib3mf_core::model::Geometry::Mesh(mesh2) = &obj2.geometry {
            assert_eq!(
                mesh2.triangles.len(),
                2,
                "read-back should have 2 triangles"
            );
        } else {
            panic!("expected Mesh geometry");
        }
    }

    // ===== Test 17: BinaryStlExporter::write combines triangles from multiple build items =====

    #[test]
    fn test_write_binary_multi_object() {
        use byteorder::{LittleEndian, ReadBytesExt};
        use lib3mf_core::model::{Geometry, Object, ObjectType};

        // Object 1: 1 triangle
        let mut mesh1 = Mesh::default();
        mesh1.vertices.push(Vertex {
            x: 0.0,
            y: 0.0,
            z: 0.0,
        });
        mesh1.vertices.push(Vertex {
            x: 1.0,
            y: 0.0,
            z: 0.0,
        });
        mesh1.vertices.push(Vertex {
            x: 0.0,
            y: 1.0,
            z: 0.0,
        });
        mesh1.triangles.push(Triangle {
            v1: 0,
            v2: 1,
            v3: 2,
            ..Default::default()
        });

        let obj1 = Object {
            id: ResourceId(1),
            object_type: ObjectType::Model,
            name: None,
            part_number: None,
            uuid: None,
            pid: None,
            pindex: None,
            thumbnail: None,
            geometry: Geometry::Mesh(mesh1),
        };

        // Object 2: 2 triangles
        let mut mesh2 = Mesh::default();
        mesh2.vertices.push(Vertex {
            x: 0.0,
            y: 0.0,
            z: 1.0,
        });
        mesh2.vertices.push(Vertex {
            x: 1.0,
            y: 0.0,
            z: 1.0,
        });
        mesh2.vertices.push(Vertex {
            x: 1.0,
            y: 1.0,
            z: 1.0,
        });
        mesh2.vertices.push(Vertex {
            x: 0.0,
            y: 1.0,
            z: 1.0,
        });
        mesh2.triangles.push(Triangle {
            v1: 0,
            v2: 1,
            v3: 2,
            ..Default::default()
        });
        mesh2.triangles.push(Triangle {
            v1: 0,
            v2: 2,
            v3: 3,
            ..Default::default()
        });

        let obj2 = Object {
            id: ResourceId(2),
            object_type: ObjectType::Model,
            name: None,
            part_number: None,
            uuid: None,
            pid: None,
            pindex: None,
            thumbnail: None,
            geometry: Geometry::Mesh(mesh2),
        };

        let mut model = Model::default();
        let _ = model.resources.add_object(obj1);
        let _ = model.resources.add_object(obj2);
        model.build.items.push(BuildItem {
            object_id: ResourceId(1),
            transform: glam::Mat4::IDENTITY,
            part_number: None,
            uuid: None,
            path: None,
            printable: None,
        });
        model.build.items.push(BuildItem {
            object_id: ResourceId(2),
            transform: glam::Mat4::IDENTITY,
            part_number: None,
            uuid: None,
            path: None,
            printable: None,
        });

        let mut buf = Vec::new();
        BinaryStlExporter::write(&model, Cursor::new(&mut buf)).expect("write should succeed");

        // Total: 80 + 4 + 3 * 50 = 234 bytes
        assert_eq!(
            buf.len(),
            234,
            "binary STL size should be 234 bytes for 3 triangles"
        );

        // Triangle count at bytes 80..84 should be 3
        let mut count_cursor = Cursor::new(&buf[80..84]);
        let tri_count = count_cursor.read_u32::<LittleEndian>().unwrap();
        assert_eq!(tri_count, 3, "combined triangle count should be 3 (1 + 2)");
    }

    // ===== Test 18: auto-detect dispatches to binary parser =====

    #[test]
    fn test_auto_detect_read_binary() {
        let header = [0u8; 80];
        let tris = vec![(0.0f32, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0)];
        let data = make_binary_stl(&header, &tris);
        let cursor = Cursor::new(data);
        let model = StlImporter::read(cursor).expect("auto-detect binary should succeed");

        assert_eq!(model.build.items.len(), 1);
        let obj = model.resources.get_object(ResourceId(1)).expect("object 1");
        if let lib3mf_core::model::Geometry::Mesh(mesh) = &obj.geometry {
            assert_eq!(mesh.triangles.len(), 1, "should have 1 triangle");
        } else {
            panic!("expected Mesh");
        }
    }

    // ===== Test 19: auto-detect dispatches to ASCII parser =====

    #[test]
    fn test_auto_detect_read_ascii() {
        let ascii = "\
solid autotest
  facet normal 0 0 1
    outer loop
      vertex 0 0 0
      vertex 1 0 0
      vertex 0 1 0
    endloop
  endfacet
endsolid autotest
";
        let cursor = Cursor::new(ascii.as_bytes().to_vec());
        let model = StlImporter::read(cursor).expect("auto-detect ASCII should succeed");

        assert_eq!(model.build.items.len(), 1);
        let obj = model.resources.get_object(ResourceId(1)).expect("object 1");
        assert_eq!(obj.name, Some("autotest".to_string()));
        if let lib3mf_core::model::Geometry::Mesh(mesh) = &obj.geometry {
            assert_eq!(mesh.triangles.len(), 1, "should have 1 triangle");
        } else {
            panic!("expected Mesh");
        }
    }
}