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
/// Double, base double, white space collapse, pattern: ((\-|\+)?(([0-9]+(\.[0-9]+)?)|(\.[0-9]+))((e|E)(\-|\+)?[0-9]+)?)
/// NegOneToOne, base Double, min: -1.0, max: 1.0
/// Degrees, base Double, min: -360, max: 360
/// TextureBase, base string, white space collapse, pattern: ([0-9]|[a-z]|[A-Z]|\+|\/|\=)+
/// ColorValue, base string

use std::fmt::{Display, Formatter, Result};

/// AMF Document
#[derive(Debug, Clone, Default)]
pub struct Document {
    pub unit: Option<Unit>,
    // Double
    pub version: Option<f64>,
    // xml:lang
    pub lang: Option<String>,
    // At least one object
    pub objects: Vec<Object>,
    // Can be empty
    pub materials: Vec<Material>,
    // Can be empty
    pub textures: Vec<Texture>,
    // Can be empty
    pub constellations: Vec<Constellation>,
    // Can be empty
    pub metadatas: Vec<Metadata>,
}

/// Unit Element
#[derive(Debug, Clone, Default, PartialEq)]
pub enum Unit {
    Inch,
    Millimeter,
    #[default]
    Meter,
    Feet,
    Micron,
}

/// Object Element
#[derive(Debug, Clone, Default)]
pub struct Object {
    pub id: u32,
    // Can be empty
    pub colors: Vec<Color>,
    // Must have only one mesh
    pub mesh: Mesh,
    // Can be empty
    pub metadatas: Vec<Metadata>,
}

/// Metadata Element
#[derive(Debug, Clone, Default)]
pub struct Metadata {
    pub r#type: String,
    // Element content text
    pub value: String,
}

/// Color Definition
#[derive(Debug, Clone, PartialEq)]
pub struct Color {
    // ColorValue
    pub r: String,
    // ColorValue
    pub g: String,
    // ColorValue
    pub b: String,
    // ColorValue
    pub a: Option<String>,
}

/// Mesh
#[derive(Debug, Clone, Default)]
pub struct Mesh {
    pub vertices: Vertices,
    pub volume: Volume,
}

/// Set of vertices
#[derive(Debug, Clone, Default)]
pub struct Vertices {
    // At least one vertex
    pub vertices: Vec<Vertex>,
    // Can be empty
    pub edges: Vec<Edge>,
}

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

    pub fn get_normals(&self) -> Vec<[f32; 3]> {
        self.vertices.iter().map(|v| {
            if let Some(normal) = v.normals.first() {
                // Convert Z-up coordinates to Y-up coordinates
                // Z-up: (x, y, z) -> Y-up: (x, z, -y)
                [normal.nx as f32, normal.nz as f32, -normal.ny as f32]
            } else {
                [0.0, 0.0, 0.0]
            }
        }).collect()
    }
}

/// Vertex
#[derive(Debug, Clone, Default)]
pub struct Vertex {
    // Can be empty
    pub metadatas: Vec<Metadata>,
    // Must have only one coordinates
    pub coordinates: Coordinates,
    // Can be empty
    pub colors: Vec<Color>,
    // Can be empty
    pub normals: Vec<Normal>,
}

/// Coordinate Points
#[derive(Debug, Clone, Default)]
pub struct Coordinates {
    // Double
    pub x: f64,
    // Double
    pub y: f64,
    // Double
    pub z: f64,
}

/// Normal Vector
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Normal {
    // NegOneToOne
    pub nx: f64,
    // NegOneToOne
    pub ny: f64,
    // NegOneToOne
    pub nz: f64,
}

/// Edge
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Edge {
    pub v1: u32,
    // NegOneToOne
    pub dx1: f64,
    // NegOneToOne
    pub dy1: f64,
    // NegOneToOne
    pub dz1: f64,
    pub v2: u32,
    // NegOneToOne
    pub dx2: f64,
    // NegOneToOne
    pub dy2: f64,
    // NegOneToOne
    pub dz2: f64,
}

/// Volume (including triangular data)
#[derive(Debug, Clone, Default)]
pub struct Volume {
    // Rename: materialid
    pub material_id: Option<u32>,
    pub r#type: Option<VolumeType>,
    // Can be empty
    pub metadatas: Vec<Metadata>,
    // At least four triangles
    pub triangles: Vec<Triangle>,
    // Can be empty
    pub colors: Vec<Color>,
}

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

/// Volume Type
#[derive(Debug, Clone, Default, PartialEq)]
pub enum VolumeType {
    #[default]
    Object,
    Support,
}

impl From<&str> for VolumeType {
    fn from(value: &str) -> Self {
        match value {
            "object" => Self::Object,
            "support" => Self::Support,
            _ => Self::Object,
        }
    }
}

impl Display for VolumeType {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        match self {
            Self::Object => write!(f, "object"),
            Self::Support => write!(f, "support"),
        }
    }
}

/// Triangle
#[derive(Debug, Clone, Default)]
pub struct Triangle {
    pub v1: u32,
    pub v2: u32,
    pub v3: u32,
    pub texmap: Option<TexMap>,
    pub colors: Vec<Color>,
}

/// Texture Mapping
#[derive(Debug, Clone, Default, PartialEq)]
pub struct TexMap {
    pub rtexid: Option<u32>,
    pub gtexid: Option<u32>,
    pub btexid: Option<u32>,
    pub atexid: Option<u32>,
    // utex and vtex 2D texture coordinates in the standard
    pub utex1: f64,
    pub utex2: f64,
    pub utex3: f64,
    pub vtex1: f64,
    pub vtex2: f64,
    pub vtex3: f64,
    // wtext 3D texture reservation
    pub wtex1: Option<f64>,
    pub wtex2: Option<f64>,
    pub wtex3: Option<f64>,
}

/// Material Definition
#[derive(Debug, Clone, Default)]
pub struct Material {
    pub id: u32,
    pub metadatas: Vec<Metadata>,
    pub composites: Vec<Composite>,
}

/// Composite Material
#[derive(Debug, Clone, Default)]
pub struct Composite {
    // Rename: materialid
    pub material_id: u32,
    // Element content text
    pub value: String,
}

/// Texture Definition
#[derive(Debug, Clone, Default)]
pub struct Texture {
    pub id: u32,
    // Pixel
    pub width: u32,
    // Pixel
    pub height: u32,
    // Pixel, useful for 3D texture, usually 1 for 2D texture
    pub depth: u32,
    pub tiled: bool,
    pub r#type: TextureType,
    // TextureBase, element content text,
    // The texture data is encoded in Base64 as binary data (such as PNG, JPEG images)
    pub value: String,
}

/// Texture Type
#[derive(Debug, Clone, Default, PartialEq)]
pub enum TextureType {
    #[default]
    Grayscale,
}

impl From<&str> for TextureType {
    fn from(value: &str) -> Self {
        match value {
            "grayscale" => Self::Grayscale,
            _ => Self::Grayscale,
        }
    }
}

impl Display for TextureType {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        match self {
            Self::Grayscale => write!(f, "grayscale"),
        }
    }
}

/// Constellation
#[derive(Debug, Clone, Default)]
pub struct Constellation {
    pub id: u32,
    // At least two instances
    pub instances: Vec<Instance>,
}

/// Instance, reference to a objec
#[derive(Debug, Clone, Default)]
pub struct Instance {
    // Rename: objectid
    pub object_id: u32,
    // Double
    pub deltax: Option<f64>,
    // Double
    pub deltay: Option<f64>,
    // Double
    pub deltaz: Option<f64>,
    // Degrees
    pub rx: Option<f64>,
    // Degrees
    pub ry: Option<f64>,
    // Degrees
    pub rz: Option<f64>,
}

impl Unit {
    // Scaling unit mapping
    pub const SCALES: &[(Self, f64)] = &[
        (Self::Millimeter, 1.0),
        (Self::Inch, 25.4),
        (Self::Feet, 304.8),
        (Self::Meter, 1000.0),
        (Self::Micron, 0.001),
    ];
}

impl From<&str> for Unit {
    fn from(s: &str) -> Self {
        match s.to_lowercase().as_str() {
            "inch" => Unit::Inch,
            "millimeter" | "mm" => Unit::Millimeter,
            "meter" | "m" => Unit::Meter,
            "feet" | "foot" | "ft" => Unit::Feet,
            "micron" => Unit::Micron,
            _ => Unit::Meter,
        }
    }
}

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

impl Color {
    /// Create a white color
    pub fn white() -> Self {
        Self {
            r: "1.0".to_string(),
            g: "1.0".to_string(),
            b: "1.0".to_string(),
            a: Some("1.0".to_string()),
        }
    }

    /// Create a black color
    pub fn black() -> Self {
        Self {
            r: "0.0".to_string(),
            g: "0.0".to_string(),
            b: "0.0".to_string(),
            a: Some("1.0".to_string()),
        }
    }

    /// Create a red color
    pub fn red() -> Self {
        Self {
            r: "1.0".to_string(),
            g: "0.0".to_string(),
            b: "0.0".to_string(),
            a: Some("1.0".to_string()),
        }
    }

    /// Create a green color
    pub fn green() -> Self {
        Self {
            r: "0.0".to_string(),
            g: "1.0".to_string(),
            b: "0.0".to_string(),
            a: Some("1.0".to_string()),
        }
    }

    /// Create a blue color
    pub fn blue() -> Self {
        Self {
            r: "0.0".to_string(),
            g: "0.0".to_string(),
            b: "1.0".to_string(),
            a: Some("1.0".to_string()),
        }
    }
}

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

impl Material {
    /// Create a new material
    pub fn new(id: u32) -> Self {
        Self {
            id,
            metadatas: Vec::new(),
            composites: Vec::new(),
        }
    }

    /// Set material name
    pub fn set_name(&mut self, value: &str) {
        // Remove the existing name metadata
        self.metadatas.retain(|m| m.r#type != "name");
        
        // Add new name metadata
        self.metadatas.push(Metadata {
            r#type: "name".to_string(),
            value: value.to_string(),
        });
    }

    /// Obtain the name of the material
    pub fn get_name(&self) -> Option<&str> {
        self.metadatas
            .iter()
            .find(|m| m.r#type == "name")
            .map(|m| m.value.as_str())
    }
}

impl Volume {
    /// Create a new volume
    pub fn new(material_id: Option<u32>) -> Self {
        Self {
            material_id,
            r#type: None,
            metadatas: Vec::new(),
            triangles: Vec::new(),
            colors: Vec::new(),
        }
    }

    /// Add a triangle to the volume
    pub fn add_triangle(&mut self, triangle: Triangle) {
        self.triangles.push(triangle);
    }

    /// Set the name of the volume
    pub fn set_name(&mut self, value: &str) {
        // Remove the existing name metadata
        self.metadatas.retain(|m| m.r#type != "name");
        
        // Add new name metadata
        self.metadatas.push(Metadata {
            r#type: "name".to_string(),
            value: value.to_string(),
        });
    }

    /// Obtain the volume name
    pub fn get_name(&self) -> Option<&str> {
        self.metadatas
            .iter()
            .find(|m| m.r#type == "name")
            .map(|m| m.value.as_str())
    }
}

impl Mesh {
    /// Create a new mesh
    pub fn new() -> Self {
        Self {
            vertices: Vertices::default(),
            volume: Volume::default(),
        }
    }

    /// Set the vertices to the mesh
    pub fn set_vertices(&mut self, vertices: Vec<Vertex>) {
        self.vertices.vertices = vertices;
    }

    /// Add vertices to the mesh
    pub fn add_vertices(&mut self, vertices: Vec<Vertex>) {
        self.vertices.vertices.extend(vertices);
    }

    /// Set the edges to the mesh
    pub fn set_edge(&mut self, edges: Vec<Edge>) {
        self.vertices.edges = edges;
    }

    /// Add edges to the mesh
    pub fn add_edge(&mut self, edges: Vec<Edge>) {
        self.vertices.edges.extend(edges);
    }

    /// Set the volume of the mesh
    pub fn set_volume(&mut self, volume: Volume) {
        self.volume = volume;
    }
}

impl Object {
    /// Create a new object
    pub fn new(id: u32) -> Self {
        Self {
            id,
            metadatas: Vec::new(),
            colors: Vec::new(),
            mesh: Mesh::new(),
        }
    }

    /// Set the name of the object
    pub fn set_name(&mut self, value: &str) {
        // Remove the existing name metadata
        self.metadatas.retain(|m| m.r#type != "name");
        
        // Add new name metadata
        self.metadatas.push(Metadata {
            r#type: "name".to_string(),
            value: value.to_string(),
        });
    }

    /// Obtain the name of the object
    pub fn get_name(&self) -> Option<&str> {
        self.metadatas
            .iter()
            .find(|m| m.r#type == "name")
            .map(|m| m.value.as_str())
    }

    /// Set the mesh of the object
    pub fn set_mesh(&mut self, mesh: Mesh) {
        self.mesh = mesh;
    }
}

// Helper functions for type conversions
impl Document {
    /// Create a new empty AMF document
    pub fn new() -> Self {
        Self {
            unit: None,
            version: None,
            lang: None,
            objects: Vec::new(),
            materials: Vec::new(),
            textures: Vec::new(),
            constellations: Vec::new(),
            metadatas: Vec::new(),
        }
    }

    /// Add an object to the AMF document
    pub fn add_object(&mut self, object: Object) {
        self.objects.push(object);
    }

    /// Get the object with the specified ID
    pub fn get_object(&self, id: u32) -> Option<&Object> {
        self.objects.iter().find(|o| o.id == id)
    }

    /// Get the object with the specified ID mutably
    pub fn get_object_mut(&mut self, id: u32) -> Option<&mut Object> {
        self.objects.iter_mut().find(|o| o.id == id)
    }

    /// Set the version of the AMF document
    pub fn set_version(&mut self, value: f64) {
        self.version = Some(value);
    }
}