thdmaker 0.0.4

A comprehensive 3D file format library supporting AMF, STL, 3MF and other 3D manufacturing formats
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
//! Basic types used throughout the 3MF library.

use std::fmt;
use std::str::FromStr;
use super::error::{Error, Result};
use super::trianglesets::TriangleSets;
use super::beamlattice::BeamLattice;

/// Triangle mesh.
#[derive(Debug, Clone, Default)]
pub struct Mesh {
    /// Mesh vertices.
    pub vertices: Vertices,
    /// Mesh triangles.
    pub triangles: Triangles,
    /// Optional triangle sets extension structure.
    pub triangle_sets: TriangleSets,
    /// Optional beam lattice extension structure.
    pub beam_lattice: Option<BeamLattice>,
    /// Optional volumetric extension volume id, reference to volume data.
    pub volume_id: Option<u32>
}

impl Mesh {
    /// Create a new empty mesh.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a mesh with given vertices and triangles.
    pub fn with_data(vertices: Vec<Vertex>, triangles: Vec<Triangle>) -> Self {
        Self {
            vertices: Vertices { vertices },
            triangles: Triangles { triangles },
            triangle_sets: TriangleSets::default(),
            beam_lattice: None,
            volume_id: None,
        }
    }

    /// Add a vertex and return its index.
    pub fn add_vertex(&mut self, vertex: Vertex) -> u32 {
        let index = self.vertices.vertices.len() as u32;
        self.vertices.vertices.push(vertex);
        index
    }

    /// Add a vertex by coordinates.
    pub fn add_vertex_coords(&mut self, x: f64, y: f64, z: f64) -> u32 {
        self.add_vertex(Vertex::new(x, y, z))
    }

    /// Add a triangle and return its index.
    pub fn add_triangle(&mut self, triangle: Triangle) -> u32 {
        let index = self.triangles.triangles.len() as u32;
        self.triangles.triangles.push(triangle);
        index
    }

    /// Add a triangle by vertex indices.
    pub fn add_triangle_indices(&mut self, v1: u32, v2: u32, v3: u32) -> u32 {
        self.add_triangle(Triangle::new(v1, v2, v3))
    }

    /// Get the number of vertices.
    pub fn vertex_count(&self) -> usize {
        self.vertices.vertices.len()
    }

    /// Get the number of triangles.
    pub fn triangle_count(&self) -> usize {
        self.triangles.triangles.len()
    }

    /// Calculate the bounding box of the mesh.
    pub fn bounding_box(&self) -> Option<BoundingBox> {
        if self.vertices.vertices.is_empty() {
            return None;
        }

        let first = &self.vertices.vertices[0];
        let mut min = Vertex::new(first.x, first.y, first.z);
        let mut max = Vertex::new(first.x, first.y, first.z);

        for v in &self.vertices.vertices {
            min.x = min.x.min(v.x);
            min.y = min.y.min(v.y);
            min.z = min.z.min(v.z);
            max.x = max.x.max(v.x);
            max.y = max.y.max(v.y);
            max.z = max.z.max(v.z);
        }

        Some(BoundingBox { min, max })
    }

    /// Calculate the normal of a triangle.
    pub fn triangle_normal(&self, triangle: &Triangle) -> Option<Vertex> {
        let v1 = self.vertices.vertices.get(triangle.v1 as usize)?;
        let v2 = self.vertices.vertices.get(triangle.v2 as usize)?;
        let v3 = self.vertices.vertices.get(triangle.v3 as usize)?;

        let edge1 = v2.sub(v1);
        let edge2 = v3.sub(v1);
        let normal = edge1.cross(&edge2);

        Some(normal.normalize())
    }

    /// Create a simple cube mesh.
    pub fn cube(size: f64) -> Self {
        let half = size / 2.0;
        let mut mesh = Mesh::new();

        // Add 8 vertices
        let v0 = mesh.add_vertex_coords(-half, -half, -half);
        let v1 = mesh.add_vertex_coords(half, -half, -half);
        let v2 = mesh.add_vertex_coords(half, half, -half);
        let v3 = mesh.add_vertex_coords(-half, half, -half);
        let v4 = mesh.add_vertex_coords(-half, -half, half);
        let v5 = mesh.add_vertex_coords(half, -half, half);
        let v6 = mesh.add_vertex_coords(half, half, half);
        let v7 = mesh.add_vertex_coords(-half, half, half);

        // Bottom face
        mesh.add_triangle_indices(v0, v2, v1);
        mesh.add_triangle_indices(v0, v3, v2);
        // Top face
        mesh.add_triangle_indices(v4, v5, v6);
        mesh.add_triangle_indices(v4, v6, v7);
        // Front face
        mesh.add_triangle_indices(v0, v1, v5);
        mesh.add_triangle_indices(v0, v5, v4);
        // Back face
        mesh.add_triangle_indices(v2, v3, v7);
        mesh.add_triangle_indices(v2, v7, v6);
        // Left face
        mesh.add_triangle_indices(v0, v4, v7);
        mesh.add_triangle_indices(v0, v7, v3);
        // Right face
        mesh.add_triangle_indices(v1, v2, v6);
        mesh.add_triangle_indices(v1, v6, v5);

        mesh
    }

    /// Create a simple tetrahedron mesh.
    pub fn tetrahedron(size: f64) -> Self {
        let mut mesh = Mesh::new();
        let s = size / 2.0;

        // Vertices of a regular tetrahedron
        let v0 = mesh.add_vertex_coords(s, s, s);
        let v1 = mesh.add_vertex_coords(s, -s, -s);
        let v2 = mesh.add_vertex_coords(-s, s, -s);
        let v3 = mesh.add_vertex_coords(-s, -s, s);

        // 4 faces
        mesh.add_triangle_indices(v0, v1, v2);
        mesh.add_triangle_indices(v0, v2, v3);
        mesh.add_triangle_indices(v0, v3, v1);
        mesh.add_triangle_indices(v1, v3, v2);

        mesh
    }

    /// Create a rectangular box (cuboid) mesh.
    pub fn box_shape(width: f64, height: f64, depth: f64) -> Self {
        let hw = width / 2.0;
        let hh = height / 2.0;
        let hd = depth / 2.0;

        let mut mesh = Mesh::new();

        // Add 8 vertices
        let v0 = mesh.add_vertex_coords(-hw, -hh, -hd);
        let v1 = mesh.add_vertex_coords(hw, -hh, -hd);
        let v2 = mesh.add_vertex_coords(hw, hh, -hd);
        let v3 = mesh.add_vertex_coords(-hw, hh, -hd);
        let v4 = mesh.add_vertex_coords(-hw, -hh, hd);
        let v5 = mesh.add_vertex_coords(hw, -hh, hd);
        let v6 = mesh.add_vertex_coords(hw, hh, hd);
        let v7 = mesh.add_vertex_coords(-hw, hh, hd);

        // Bottom face (z = -hd)
        mesh.add_triangle_indices(v0, v2, v1);
        mesh.add_triangle_indices(v0, v3, v2);
        // Top face (z = hd)
        mesh.add_triangle_indices(v4, v5, v6);
        mesh.add_triangle_indices(v4, v6, v7);
        // Front face (y = -hh)
        mesh.add_triangle_indices(v0, v1, v5);
        mesh.add_triangle_indices(v0, v5, v4);
        // Back face (y = hh)
        mesh.add_triangle_indices(v2, v3, v7);
        mesh.add_triangle_indices(v2, v7, v6);
        // Left face (x = -hw)
        mesh.add_triangle_indices(v0, v4, v7);
        mesh.add_triangle_indices(v0, v7, v3);
        // Right face (x = hw)
        mesh.add_triangle_indices(v1, v2, v6);
        mesh.add_triangle_indices(v1, v6, v5);

        mesh
    }

    /// Create a cylinder mesh.
    pub fn cylinder(radius: f64, height: f64, segments: usize) -> Self {
        let segments = segments.max(3);
        let half_height = height / 2.0;
        let mut mesh = Mesh::new();

        // Create vertices for top and bottom circles
        let mut bottom_vertices = Vec::with_capacity(segments);
        let mut top_vertices = Vec::with_capacity(segments);

        for i in 0..segments {
            let angle = 2.0 * std::f64::consts::PI * (i as f64) / (segments as f64);
            let x = radius * angle.cos();
            let y = radius * angle.sin();

            bottom_vertices.push(mesh.add_vertex_coords(x, y, -half_height));
            top_vertices.push(mesh.add_vertex_coords(x, y, half_height));
        }

        // Center vertices for caps
        let bottom_center = mesh.add_vertex_coords(0.0, 0.0, -half_height);
        let top_center = mesh.add_vertex_coords(0.0, 0.0, half_height);

        // Create triangles
        for i in 0..segments {
            let next = (i + 1) % segments;

            // Bottom cap (reversed winding for outward normals)
            mesh.add_triangle_indices(
                bottom_center,
                bottom_vertices[next],
                bottom_vertices[i],
            );

            // Top cap
            mesh.add_triangle_indices(top_center, top_vertices[i], top_vertices[next]);

            // Side triangles
            mesh.add_triangle_indices(
                bottom_vertices[i],
                bottom_vertices[next],
                top_vertices[next],
            );
            mesh.add_triangle_indices(bottom_vertices[i], top_vertices[next], top_vertices[i]);
        }

        mesh
    }

    /// Create a sphere mesh using UV sphere algorithm.
    pub fn sphere(radius: f64, rings: usize, segments: usize) -> Self {
        let rings = rings.max(2);
        let segments = segments.max(3);
        let mut mesh = Mesh::new();

        // Add vertices
        // Top pole
        let top_pole = mesh.add_vertex_coords(0.0, 0.0, radius);

        // Ring vertices
        let mut ring_vertices: Vec<Vec<u32>> = Vec::with_capacity(rings - 1);
        for r in 1..rings {
            let phi = std::f64::consts::PI * (r as f64) / (rings as f64);
            let z = radius * phi.cos();
            let ring_radius = radius * phi.sin();

            let mut ring = Vec::with_capacity(segments);
            for s in 0..segments {
                let theta = 2.0 * std::f64::consts::PI * (s as f64) / (segments as f64);
                let x = ring_radius * theta.cos();
                let y = ring_radius * theta.sin();
                ring.push(mesh.add_vertex_coords(x, y, z));
            }
            ring_vertices.push(ring);
        }

        // Bottom pole
        let bottom_pole = mesh.add_vertex_coords(0.0, 0.0, -radius);

        // Create triangles
        // Top cap
        for s in 0..segments {
            let next = (s + 1) % segments;
            mesh.add_triangle_indices(top_pole, ring_vertices[0][s], ring_vertices[0][next]);
        }

        // Middle rings
        for r in 0..(rings - 2) {
            for s in 0..segments {
                let next = (s + 1) % segments;
                mesh.add_triangle_indices(
                    ring_vertices[r][s],
                    ring_vertices[r + 1][s],
                    ring_vertices[r + 1][next],
                );
                mesh.add_triangle_indices(
                    ring_vertices[r][s],
                    ring_vertices[r + 1][next],
                    ring_vertices[r][next],
                );
            }
        }

        // Bottom cap
        let last_ring = rings - 2;
        for s in 0..segments {
            let next = (s + 1) % segments;
            mesh.add_triangle_indices(
                ring_vertices[last_ring][s],
                bottom_pole,
                ring_vertices[last_ring][next],
            );
        }

        mesh
    }

    /// Set the beam lattice.
    pub fn set_beam_lattice(&mut self, beam_lattice: BeamLattice) {
        self.beam_lattice = Some(beam_lattice);
    }

    /// Get the beam lattice.
    pub fn get_beam_lattice(&self) -> Option<&BeamLattice> {
        self.beam_lattice.as_ref()
    }

    /// Get the number of beams if beam lattice exists.
    pub fn beam_count(&self) -> usize {
        self.beam_lattice.as_ref().map_or(0, |l| l.beams.beams.len())
    }

    /// Get the number of balls if beam lattice exists.
    pub fn ball_count(&self) -> usize {
        self.beam_lattice.as_ref().map_or(0, |l| l.balls.balls.len())
    }

    /// Get vertex positions as f32 array.
    pub fn get_positions(&self) -> Vec<[f32; 3]> {
        self.vertices.vertices.iter().map(|v| {
            // Convert Z-up coordinates to Y-up coordinates
            // Z-up: (x, y, z) -> Y-up: (x, z, -y)
            [v.x as f32, v.z as f32, -v.y as f32]
        }).collect()
    }

    ///  Get vertex normals from triangles as f32 array.
    pub fn get_normals(&self) -> Vec<[f32; 3]> {
        let vertices = &self.vertices.vertices;
        let triangles = &self.triangles.triangles;
        let mut normals = vec![[0.0f32; 3]; vertices.len()];
        let mut counts = vec![0usize; vertices.len()];

        for triangle in triangles {
            if let (Some(v1), Some(v2), Some(v3)) = (
                vertices.get(triangle.v1 as usize),
                vertices.get(triangle.v2 as usize),
                vertices.get(triangle.v3 as usize),
            ) {
                // Calculate triangle normal using cross product
                let edge1 = (
                    v2.x - v1.x,
                    v2.y - v1.y,
                    v2.z - v1.z,
                );
                let edge2 = (
                    v3.x - v1.x,
                    v3.y - v1.y,
                    v3.z - v1.z,
                );

                let normal = (
                    edge1.1 * edge2.2 - edge1.2 * edge2.1,
                    edge1.2 * edge2.0 - edge1.0 * edge2.2,
                    edge1.0 * edge2.1 - edge1.1 * edge2.0,
                );

                // Normalize
                let len = (normal.0 * normal.0 + normal.1 * normal.1 + normal.2 * normal.2).sqrt();
                if len > 0.0 {
                    let normal = (
                        normal.0 / len,
                        normal.1 / len,
                        normal.2 / len,
                    );

                    // Add to each vertex
                    for idx in [triangle.v1, triangle.v2, triangle.v3] {
                        let idx = idx as usize;
                        normals[idx][0] += normal.0 as f32;
                        normals[idx][1] += normal.1 as f32;
                        normals[idx][2] += normal.2 as f32;
                        counts[idx] += 1;
                    }
                }
            }
        }

        // Average normals
        for i in 0..normals.len() {
            if counts[i] > 0 {
                normals[i][0] /= counts[i] as f32;
                normals[i][1] /= counts[i] as f32;
                normals[i][2] /= counts[i] as f32;

                // Normalize again
                let len = (normals[i][0] * normals[i][0] + normals[i][1] * normals[i][1] + normals[i][2] * normals[i][2]).sqrt();
                if len > 0.0 {
                    normals[i][0] /= len;
                    normals[i][1] /= len;
                    normals[i][2] /= len;
                }

                // Convert Z-up coordinates to Y-up coordinates
                // Z-up: (x, y, z) -> Y-up: (x, z, -y)
                let [x, y, z] = normals[i];
                normals[i][0] = x;
                normals[i][1] = z;
                normals[i][2] = -y;
            }
        }

        normals
    }

    /// Get triangle indices.
    pub fn get_indices(&self) -> Vec<u32> {
        self.triangles.triangles.iter().flat_map(|t| [t.v1, t.v2, t.v3]).collect()
    }
}

/// Bounding box defined by min and max corners.
#[derive(Debug, Clone, Copy)]
pub struct BoundingBox {
    pub min: Vertex,
    pub max: Vertex,
}

impl BoundingBox {
    /// Get the size of the bounding box.
    pub fn size(&self) -> (f64, f64, f64) {
        (
            self.max.x - self.min.x,
            self.max.y - self.min.y,
            self.max.z - self.min.z,
        )
    }

    /// Get the center of the bounding box.
    pub fn center(&self) -> Vertex {
        Vertex::new(
            (self.min.x + self.max.x) / 2.0,
            (self.min.y + self.max.y) / 2.0,
            (self.min.z + self.max.z) / 2.0,
        )
    }
}

/// Unit of measurement for the model.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Unit {
    /// Micrometers (μm)
    Micron,
    /// Millimeters (mm) - default
    #[default]
    Millimeter,
    /// Centimeters (cm)
    Centimeter,
    /// Inches
    Inch,
    /// Feet
    Foot,
    /// Meters
    Meter,
}

impl Unit {
    /// Convert a value from this unit to millimeters.
    pub fn millimeter_into(&self, value: f32) -> f32 {
        match self {
            Unit::Micron => value * 0.001,
            Unit::Millimeter => value,
            Unit::Centimeter => value * 10.0,
            Unit::Inch => value * 25.4,
            Unit::Foot => value * 304.8,
            Unit::Meter => value * 1000.0,
        }
    }

    /// Convert a value from millimeters to this unit.
    pub fn millimeter_from(&self, value: f32) -> f32 {
        match self {
            Unit::Micron => value * 1000.0,
            Unit::Millimeter => value,
            Unit::Centimeter => value * 0.1,
            Unit::Inch => value / 25.4,
            Unit::Foot => value / 304.8,
            Unit::Meter => value * 0.001,
        }
    }
}

impl fmt::Display for Unit {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Unit::Micron => write!(f, "micron"),
            Unit::Millimeter => write!(f, "millimeter"),
            Unit::Centimeter => write!(f, "centimeter"),
            Unit::Inch => write!(f, "inch"),
            Unit::Foot => write!(f, "foot"),
            Unit::Meter => write!(f, "meter"),
        }
    }
}

impl FromStr for Unit {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        match s.to_lowercase().as_str() {
            "micron" => Ok(Unit::Micron),
            "millimeter" => Ok(Unit::Millimeter),
            "centimeter" => Ok(Unit::Centimeter),
            "inch" => Ok(Unit::Inch),
            "foot" => Ok(Unit::Foot),
            "meter" => Ok(Unit::Meter),
            _ => Err(Error::InvalidAttribute {
                name: "unit".to_string(),
                message: format!("unknown unit: {}", s),
            }),
        }
    }
}

/// sRGB color with optional alpha channel.
/// 
/// Format: #RRGGBB or #RRGGBBAA
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Color {
    /// Red component (0-255)
    pub r: u8,
    /// Green component (0-255)
    pub g: u8,
    /// Blue component (0-255)
    pub b: u8,
    /// Alpha component (0-255, 255 = fully opaque)
    pub a: u8,
}

impl Color {
    /// Create a new opaque color.
    pub fn rgb(r: u8, g: u8, b: u8) -> Self {
        Self { r, g, b, a: 255 }
    }

    /// Create a new color with alpha.
    pub fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self { r, g, b, a }
    }

    /// Common colors.
    pub const WHITE: Color = Color { r: 255, g: 255, b: 255, a: 255 };
    pub const BLACK: Color = Color { r: 0, g: 0, b: 0, a: 255 };
    pub const RED: Color = Color { r: 255, g: 0, b: 0, a: 255 };
    pub const GREEN: Color = Color { r: 0, g: 255, b: 0, a: 255 };
    pub const BLUE: Color = Color { r: 0, g: 0, b: 255, a: 255 };
}

impl Default for Color {
    fn default() -> Self {
        Color::WHITE
    }
}

impl fmt::Display for Color {
    /// Convert to hex string (#RRGGBB or #RRGGBBAA).
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.a == 255 {
            write!(f, "#{:02X}{:02X}{:02X}", self.r, self.g, self.b)
        } else {
            write!(f, "#{:02X}{:02X}{:02X}{:02X}", self.r, self.g, self.b, self.a)
        }
    }
}

impl FromStr for Color {
    type Err = Error;

    /// Create a color from a hex string (#RRGGBB or #RRGGBBAA).
    fn from_str(s: &str) -> Result<Self> {
        let s = s.trim();
        if !s.starts_with('#') {
            return Err(Error::InvalidColor(format!(
                "color must start with '#': {}",
                s
            )));
        }
        let hex = &s[1..];
        match hex.len() {
            6 => {
                let r = u8::from_str_radix(&hex[0..2], 16)
                    .map_err(|_| Error::InvalidColor(format!("red: {}", s)))?;
                let g = u8::from_str_radix(&hex[2..4], 16)
                    .map_err(|_| Error::InvalidColor(format!("green: {}", s)))?;
                let b = u8::from_str_radix(&hex[4..6], 16)
                    .map_err(|_| Error::InvalidColor(format!("blue: {}", s)))?;
                Ok(Color::rgb(r, g, b))
            }
            8 => {
                let r = u8::from_str_radix(&hex[0..2], 16)
                    .map_err(|_| Error::InvalidColor(format!("red: {}", s)))?;
                let g = u8::from_str_radix(&hex[2..4], 16)
                    .map_err(|_| Error::InvalidColor(format!("green: {}", s)))?;
                let b = u8::from_str_radix(&hex[4..6], 16)
                    .map_err(|_| Error::InvalidColor(format!("blue: {}", s)))?;
                let a = u8::from_str_radix(&hex[6..8], 16)
                    .map_err(|_| Error::InvalidColor(format!("alpha: {}", s)))?;
                Ok(Color::rgba(r, g, b, a))
            }
            _ => Err(Error::InvalidColor(format!(
                "color must be 6 or 8 hex digits: {}",
                s
            ))),
        }
    }
}

/// 4x4 affine transformation matrix stored in row-major order.
/// 
/// The matrix format is:
/// ```text
/// | m00 m01 m02 0 |
/// | m10 m11 m12 0 |
/// | m20 m21 m22 0 |
/// | m30 m31 m32 1 |
/// ```
/// 
/// In 3MF, only 12 values are stored: m00 m01 m02 m10 m11 m12 m20 m21 m22 m30 m31 m32
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Matrix3D {
    /// First row: m00, m01, m02
    pub m0: [f64; 3],
    /// Second row: m10, m11, m12
    pub m1: [f64; 3],
    /// Third row: m20, m21, m22
    pub m2: [f64; 3],
    /// Fourth row (translation): m30, m31, m32
    pub m3: [f64; 3],
}

impl Matrix3D {
    /// Create a new identity matrix.
    pub fn identity() -> Self {
        Self {
            m0: [1.0, 0.0, 0.0],
            m1: [0.0, 1.0, 0.0],
            m2: [0.0, 0.0, 1.0],
            m3: [0.0, 0.0, 0.0],
        }
    }

    /// Create a translation matrix.
    pub fn translation(x: f64, y: f64, z: f64) -> Self {
        Self {
            m0: [1.0, 0.0, 0.0],
            m1: [0.0, 1.0, 0.0],
            m2: [0.0, 0.0, 1.0],
            m3: [x, y, z],
        }
    }

    /// Create a scaling matrix.
    pub fn scale(sx: f64, sy: f64, sz: f64) -> Self {
        Self {
            m0: [sx, 0.0, 0.0],
            m1: [0.0, sy, 0.0],
            m2: [0.0, 0.0, sz],
            m3: [0.0, 0.0, 0.0],
        }
    }

    /// Create a uniform scaling matrix.
    pub fn uniform_scale(s: f64) -> Self {
        Self::scale(s, s, s)
    }

    /// Create a rotation matrix around the X axis.
    pub fn rotation_x(angle_rad: f64) -> Self {
        let c = angle_rad.cos();
        let s = angle_rad.sin();
        Self {
            m0: [1.0, 0.0, 0.0],
            m1: [0.0, c, s],
            m2: [0.0, -s, c],
            m3: [0.0, 0.0, 0.0],
        }
    }

    /// Create a rotation matrix around the Y axis.
    pub fn rotation_y(angle_rad: f64) -> Self {
        let c = angle_rad.cos();
        let s = angle_rad.sin();
        Self {
            m0: [c, 0.0, -s],
            m1: [0.0, 1.0, 0.0],
            m2: [s, 0.0, c],
            m3: [0.0, 0.0, 0.0],
        }
    }

    /// Create a rotation matrix around the Z axis.
    pub fn rotation_z(angle_rad: f64) -> Self {
        let c = angle_rad.cos();
        let s = angle_rad.sin();
        Self {
            m0: [c, s, 0.0],
            m1: [-s, c, 0.0],
            m2: [0.0, 0.0, 1.0],
            m3: [0.0, 0.0, 0.0],
        }
    }

    /// Calculate the determinant of the 3x3 rotation/scale part.
    pub fn determinant_3x3(&self) -> f64 {
        self.m0[0] * (self.m1[1] * self.m2[2] - self.m1[2] * self.m2[1])
            - self.m0[1] * (self.m1[0] * self.m2[2] - self.m1[2] * self.m2[0])
            + self.m0[2] * (self.m1[0] * self.m2[1] - self.m1[1] * self.m2[0])
    }

    /// Converts the Z-up transform to a Y-up 4x4 column-major matrix.
    pub fn matrix_array_4x4(&self) -> [f32; 16] {
        [
            self.m0[0] as f32, self.m0[1] as f32, self.m0[2] as f32, 0.0,
            self.m1[0] as f32, self.m1[1] as f32, self.m1[2] as f32, 0.0,
            self.m2[0] as f32, self.m2[1] as f32, self.m2[2] as f32, 0.0,
            // Convert Z-up coordinates to Y-up coordinates
            // Z-up: (x, y, z) -> Y-up: (x, z, -y)
            self.m3[0] as f32, self.m3[2] as f32, -self.m3[1] as f32, 1.0,
        ]
    }

    /// Transform a point using this matrix.
    pub fn transform_point(&self, x: f64, y: f64, z: f64) -> (f64, f64, f64) {
        let nx = self.m0[0] * x + self.m1[0] * y + self.m2[0] * z + self.m3[0];
        let ny = self.m0[1] * x + self.m1[1] * y + self.m2[1] * z + self.m3[1];
        let nz = self.m0[2] * x + self.m1[2] * y + self.m2[2] * z + self.m3[2];
        (nx, ny, nz)
    }

    /// Multiply two matrices.
    pub fn multiply(&self, other: &Matrix3D) -> Matrix3D {
        Matrix3D {
            m0: [
                self.m0[0] * other.m0[0] + self.m0[1] * other.m1[0] + self.m0[2] * other.m2[0],
                self.m0[0] * other.m0[1] + self.m0[1] * other.m1[1] + self.m0[2] * other.m2[1],
                self.m0[0] * other.m0[2] + self.m0[1] * other.m1[2] + self.m0[2] * other.m2[2],
            ],
            m1: [
                self.m1[0] * other.m0[0] + self.m1[1] * other.m1[0] + self.m1[2] * other.m2[0],
                self.m1[0] * other.m0[1] + self.m1[1] * other.m1[1] + self.m1[2] * other.m2[1],
                self.m1[0] * other.m0[2] + self.m1[1] * other.m1[2] + self.m1[2] * other.m2[2],
            ],
            m2: [
                self.m2[0] * other.m0[0] + self.m2[1] * other.m1[0] + self.m2[2] * other.m2[0],
                self.m2[0] * other.m0[1] + self.m2[1] * other.m1[1] + self.m2[2] * other.m2[1],
                self.m2[0] * other.m0[2] + self.m2[1] * other.m1[2] + self.m2[2] * other.m2[2],
            ],
            m3: [
                self.m3[0] * other.m0[0] + self.m3[1] * other.m1[0] + self.m3[2] * other.m2[0] + other.m3[0],
                self.m3[0] * other.m0[1] + self.m3[1] * other.m1[1] + self.m3[2] * other.m2[1] + other.m3[1],
                self.m3[0] * other.m0[2] + self.m3[1] * other.m1[2] + self.m3[2] * other.m2[2] + other.m3[2],
            ],
        }
    }
}

impl Default for Matrix3D {
    fn default() -> Self {
        Self::identity()
    }
}

impl FromStr for Matrix3D {
    type Err = Error;

    /// Parse from 3MF format string "m00 m01 m02 m10 m11 m12 m20 m21 m22 m30 m31 m32".
    fn from_str(s: &str) -> Result<Self> {
        let values: Vec<f64> = s
            .split_whitespace()
            .map(|v| v.parse::<f64>())
            .collect::<std::result::Result<Vec<_>, _>>()
            .map_err(|_| Error::InvalidMatrix(format!("cannot parse matrix: {}", s)))?;

        if values.len() != 12 {
            return Err(Error::InvalidMatrix(format!(
                "matrix must have 12 values, got {}",
                values.len()
            )));
        }

        Ok(Self {
            m0: [values[0], values[1], values[2]],
            m1: [values[3], values[4], values[5]],
            m2: [values[6], values[7], values[8]],
            m3: [values[9], values[10], values[11]],
        })
    }
}

impl fmt::Display for Matrix3D {
    /// Convert to 3MF format string.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f, "{} {} {} {} {} {} {} {} {} {} {} {}",
            self.m0[0], self.m0[1], self.m0[2],
            self.m1[0], self.m1[1], self.m1[2],
            self.m2[0], self.m2[1], self.m2[2],
            self.m3[0], self.m3[1], self.m3[2]
        )
    }
}

/// A collection of vertices.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Vertices {
    pub vertices: Vec<Vertex>,
}

/// A 3D vertex (point).
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Vertex {
    pub x: f64,
    pub y: f64,
    pub z: f64,
}

impl Vertex {
    /// Create a new vertex.
    pub fn new(x: f64, y: f64, z: f64) -> Self {
        Self { x, y, z }
    }

    /// Calculate distance to another vertex.
    pub fn distance(&self, other: &Vertex) -> f64 {
        let dx = self.x - other.x;
        let dy = self.y - other.y;
        let dz = self.z - other.z;
        (dx * dx + dy * dy + dz * dz).sqrt()
    }

    /// Calculate the length from origin.
    pub fn length(&self) -> f64 {
        (self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
    }

    /// Normalize to unit length.
    pub fn normalize(&self) -> Self {
        let len = self.length();
        if len > 0.0 {
            Self {
                x: self.x / len,
                y: self.y / len,
                z: self.z / len,
            }
        } else {
            *self
        }
    }

    /// Cross product.
    pub fn cross(&self, other: &Vertex) -> Self {
        Self {
            x: self.y * other.z - self.z * other.y,
            y: self.z * other.x - self.x * other.z,
            z: self.x * other.y - self.y * other.x,
        }
    }

    /// Dot product.
    pub fn dot(&self, other: &Vertex) -> f64 {
        self.x * other.x + self.y * other.y + self.z * other.z
    }

    /// Subtract two vertices.
    pub fn sub(&self, other: &Vertex) -> Self {
        Self {
            x: self.x - other.x,
            y: self.y - other.y,
            z: self.z - other.z,
        }
    }
}

impl From<(f64, f64, f64)> for Vertex {
    fn from((x, y, z): (f64, f64, f64)) -> Self {
        Self { x, y, z }
    }
}

impl From<[f64; 3]> for Vertex {
    fn from([x, y, z]: [f64; 3]) -> Self {
        Self { x, y, z }
    }
}

/// A collection of triangles.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Triangles {
    pub triangles: Vec<Triangle>,
}

/// A triangle face referencing three vertices by index.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Triangle {
    /// Index of first vertex.
    pub v1: u32,
    /// Index of second vertex.
    pub v2: u32,
    /// Index of third vertex.
    pub v3: u32,
    /// Optional property index for first vertex.
    pub p1: Option<u32>,
    /// Optional property index for second vertex.
    pub p2: Option<u32>,
    /// Optional property index for third vertex.
    pub p3: Option<u32>,
    /// Optional property group ID.
    pub pid: Option<u32>,
}

impl Triangle {
    /// Create a new triangle.
    pub fn new(v1: u32, v2: u32, v3: u32) -> Self {
        Self {
            v1,
            v2,
            v3,
            p1: None,
            p2: None,
            p3: None,
            pid: None,
        }
    }

    /// Create a new triangle with property.
    pub fn with_property(v1: u32, v2: u32, v3: u32, pid: u32, pindex: u32) -> Self {
        Self {
            v1,
            v2,
            v3,
            p1: Some(pindex),
            p2: None,
            p3: None,
            pid: Some(pid),
        }
    }

    /// Get vertex indices as a tuple.
    pub fn vertices(&self) -> (u32, u32, u32) {
        (self.v1, self.v2, self.v3)
    }
}