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
use crate::config::{NORMAL_ATTRIB_NAME, TANGENT_ATTRIB_NAME};
use crate::mesh::Mesh;
use crate::AttribConfig;
use gltf::json;
use indexmap::map::IndexMap;
use meshx::mesh::TriMesh;
use meshx::topology::{FaceIndex, VertexIndex};
use serde::{Deserialize, Serialize};

type MaterialMap = IndexMap<meshx::io::obj::Material, Vec<usize>>;
type MaterialIdMap = IndexMap<u32, Vec<usize>>;

#[derive(Debug)]
pub enum AttribError {
    InvalidTexCoordAttribType(ComponentType),
    InvalidVector3AttribType(Type),
    Mesh(meshx::attrib::Error),
}

impl std::error::Error for AttribError {}

impl std::fmt::Display for AttribError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AttribError::InvalidTexCoordAttribType(t) => write!(
                f,
                "Invalid texture coordinate attribute type detected: {:?}. Skipping...",
                t
            ),
            AttribError::InvalidVector3AttribType(t) => write!(
                f,
                "Invalid 3D vector attribute type detected: {:?}. Skipping...",
                t
            ),
            AttribError::Mesh(e) => write!(f, "Mesh: {}", e),
        }
    }
}

impl From<meshx::attrib::Error> for AttribError {
    fn from(e: meshx::attrib::Error) -> AttribError {
        AttribError::Mesh(e)
    }
}

pub type VertexAttribute = meshx::attrib::Attribute<VertexIndex>;

#[derive(Clone, Debug, PartialEq)]
pub enum MaterialIds {
    /// Local material IDs inferred if materials are explicitly specified in the input mesh.
    Local {
        /// A map of materials with an associated list of face indices which use this material.
        map: MaterialMap,
    },
    /// Material IDs explicitly specified in the input mesh, which are interpreted as indexing some global material array.
    Global { map: MaterialIdMap },
}

#[derive(Clone, Debug)]
pub struct AttribTransfer {
    pub attribs_to_keep: Vec<Attribute>,
    pub color_attribs_to_keep: Vec<Attribute>,
    pub tex_attribs_to_keep: Vec<TextureAttribute>,
    pub material_ids: Option<MaterialIds>,
    pub normal_attrib: Vec<[f32; 3]>,
    pub tangent_attrib: Vec<[f32; 3]>,
}

/// Find per face material IDs in the given mesh by probing a given integer type `I`.
fn find_material_ids<I: Clone + num_traits::ToPrimitive + 'static>(
    mesh: &Mesh,
    attrib_name: &str,
) -> Option<Vec<u32>> {
    use meshx::attrib::Attrib;
    match mesh {
        Mesh::TriMesh(mesh) => mesh
            .attrib_iter::<I, FaceIndex>(attrib_name)
            .ok()
            .map(|iter| {
                iter.map(|x| {
                    x.to_u32()
                        .expect("Material ID does not fit into an unsigned 32 bit integer.")
                })
                .collect()
            }),
        Mesh::PointCloud(ptcloud) => {
            ptcloud
                .attrib_iter::<I, VertexIndex>(attrib_name)
                .ok()
                .map(|iter| {
                    iter.map(|x| {
                        x.to_u32()
                            .expect("Material ID does not fit into an unsigned 32 bit integer.")
                    })
                    .collect()
                })
        }
    }
}

/// Cleanup unwanted attributes from a given `Mesh`.
pub fn clean_mesh(
    mesh: &mut Mesh,
    config: AttribConfig<'_>,
    mut process_attrib_error: impl FnMut(AttribError),
) -> AttribTransfer {
    // First we promote any face-vertex attributes to vertex attributes and split the mesh
    // when needed when the face-vertex attribute values on neighbouring faces are different.
    if let Mesh::TriMesh(mesh) = mesh {
        config.texcoords.0.iter().for_each(|attrib| {
            if let Err(e) = promote_texture_coordinate_attribute_to_vertices(mesh, attrib) {
                process_attrib_error(e);
            }
        });
    }

    // In some cases normals and tangents are given per face-vertex instead of each vertex.
    // This allows, for instance, to specify sharp cusps on meshes that are otherwise smooth.
    // To preserve this effect we split the mesh when face-vertex normals do not coincide.
    if let Mesh::TriMesh(mesh) = mesh {
        config.attributes.0.iter().for_each(|attrib| {
            if attrib.0 == NORMAL_ATTRIB_NAME || attrib.0 == TANGENT_ATTRIB_NAME {
                if let Err(e) = promote_vector3_float_attribute_to_vertices(mesh, attrib) {
                    process_attrib_error(e);
                }
            }
        });
    }

    // Next we remove all attributes we want to keep.

    let tex_attribs_to_keep: Vec<_> = if let Mesh::TriMesh(mesh) = mesh {
        config
            .texcoords
            .0
            .iter()
            .enumerate()
            .filter_map(|(id, attrib)| {
                match remove_texture_coordinate_attribute(mesh, attrib, id) {
                    Err(e) => {
                        process_attrib_error(e);
                        None
                    }
                    Ok(r) => Some(r),
                }
            })
            .collect()
    } else {
        Vec::new()
    };

    // Remove normal and tangent attributes first.
    let mut normal_attrib = Vec::new();
    let mut tangent_attrib = Vec::new();
    config.attributes.0.iter().for_each(|attrib| {
        if attrib.0 == NORMAL_ATTRIB_NAME && normal_attrib.is_empty() {
            normal_attrib = remove_attribute(mesh, attrib)
                .and_then(|a| a.attribute.direct_clone_into_vec::<[f32; 3]>().ok())
                .unwrap_or_default();
        } else if attrib.0 == TANGENT_ATTRIB_NAME && tangent_attrib.is_empty() {
            tangent_attrib = remove_attribute(mesh, attrib)
                .and_then(|a| a.attribute.direct_clone_into_vec::<[f32; 3]>().ok())
                .unwrap_or_default();
        }
    });

    // It is important that these follow the tex attrib function since that can change mesh
    // topology.
    let attribs_to_keep: Vec<_> = config
        .attributes
        .0
        .iter()
        .filter_map(|attrib| remove_attribute(mesh, attrib))
        .collect();
    let color_attribs_to_keep: Vec<_> = config
        .colors
        .0
        .iter()
        .filter_map(|attrib| remove_attribute(mesh, attrib))
        .collect();

    // Find material indices in this mesh.
    // Try a bunch of different integer types or look for a material attribute found in wavefront-obj imports.
    let material_ids = find_material_ids::<u32>(mesh, config.material_attribute)
        .or_else(|| find_material_ids::<i32>(mesh, config.material_attribute))
        .or_else(|| find_material_ids::<i64>(mesh, config.material_attribute))
        .or_else(|| find_material_ids::<u64>(mesh, config.material_attribute))
        .or_else(|| find_material_ids::<i16>(mesh, config.material_attribute))
        .or_else(|| find_material_ids::<u16>(mesh, config.material_attribute))
        .or_else(|| find_material_ids::<i8>(mesh, config.material_attribute))
        .or_else(|| find_material_ids::<u8>(mesh, config.material_attribute))
        .map_or_else(
            || {
                // Find the "mtl" attribute loaded by meshx for obj files.
                //
                // This allows gltfgen to automatically determine materials from
                // .mtl files without having to specify materials in the
                // configuration.
                let map = extract_mtls(mesh);
                if map.is_empty() {
                    None
                } else {
                    Some(MaterialIds::Local { map })
                }
            },
            |ids| {
                Some(MaterialIds::Global {
                    map: group_mtls(ids.as_slice()),
                })
            },
        );

    // Remove all attributes from the mesh.
    // It is important to delete these attributes, because they could cause a huge memory overhead.
    match mesh {
        Mesh::TriMesh(mesh) => {
            mesh.vertex_attributes.clear();
            mesh.face_attributes.clear();
            mesh.face_vertex_attributes.clear();
            mesh.face_edge_attributes.clear();
        }
        Mesh::PointCloud(ptcloud) => {
            ptcloud.vertex_attributes.clear();
        }
    }

    // Instead of reinserting back into the mesh, we keep this outside the mesh so we can
    // determine the type of the attribute.
    AttribTransfer {
        attribs_to_keep,
        color_attribs_to_keep,
        tex_attribs_to_keep,
        material_ids,
        normal_attrib,
        tangent_attrib,
    }
}

/// Find and extract the "mtl" attribute loaded by `meshx` for obj files. This allows `gltfgen` to automatically determine
/// what textures to load.
fn extract_mtls(mesh: &mut Mesh) -> MaterialMap {
    use meshx::attrib::Attrib;
    let mut mtls = IndexMap::new();
    if let Ok(attrib) = match mesh {
        Mesh::TriMesh(mesh) => mesh.remove_attrib::<FaceIndex>("mtl"),
        Mesh::PointCloud(_) => return mtls, // Automatic materials not supported on pointclouds
    } {
        for (face_idx, mtl) in attrib
            .indirect_iter::<meshx::io::obj::Material>()
            .unwrap()
            .enumerate()
        {
            let face_indices: &mut Vec<usize> = mtls.entry(mtl.clone()).or_insert_with(Vec::new);
            face_indices.push(face_idx);
        }
    }
    mtls
}

/// Group the given list of material ids into groups of indices corresponding to the same id.
fn group_mtls(ids: &[u32]) -> MaterialIdMap {
    let mut map = IndexMap::new();
    for (face_idx, &mtl_id) in ids.iter().enumerate() {
        let face_indices: &mut Vec<usize> = map.entry(mtl_id).or_insert_with(Vec::new);
        face_indices.push(face_idx);
    }
    map
}

/// Remove the given attribute from the mesh and return it along with its name.
fn remove_attribute(mesh: &mut Mesh, attrib: (&String, &Type)) -> Option<Attribute> {
    use meshx::attrib::Attrib;
    match mesh {
        Mesh::TriMesh(mesh) => mesh.remove_attrib::<VertexIndex>(attrib.0),
        Mesh::PointCloud(mesh) => mesh.remove_attrib::<VertexIndex>(attrib.0),
    }
    .ok()
    .map(|a| Attribute {
        name: attrib.0.clone(),
        type_: *attrib.1,
        attribute: a,
    })
}

/// Try to promote the texture coordinate attribute from `FaceVertex` attribute to `Vertex`
/// attribute.
fn try_tex_coord_promote<T>(name: &str, mesh: &mut TriMesh<f32>) -> Result<(), AttribError>
where
    T: PartialEq + Clone + std::fmt::Debug + 'static,
{
    use meshx::attrib::AttribPromote;
    let err = "Texture coordinate type collisions detected. Could not promote attribute to vertex. Please report this issue.";
    Ok(mesh
        .attrib_promote::<[T; 2], _>(name, |a, b| assert_eq!(&*a, b, "{}", err))
        .map(|_| ())
        .or_else(|_| {
            mesh.attrib_promote::<[T; 3], _>(name, |a, b| assert_eq!(&*a, b, "{}", err))
                .map(|_| ())
        })?)
}

/// Try to promote a 3D vector attribute from `FaceVertex` attribute to `Vertex`
/// attribute.
fn try_vec3_promote<T>(name: &str, mesh: &mut TriMesh<f32>) -> Result<(), AttribError>
where
    T: PartialEq + Clone + std::fmt::Debug + 'static,
{
    use meshx::attrib::AttribPromote;
    let err = "Attribute type collisions detected. Could not promote attribute to vertex. Please report this issue.";
    Ok(mesh
        .attrib_promote::<[T; 3], _>(name, |a, b| assert_eq!(&*a, b, "{}", err))
        .map(|_| ())?)
}

/// Promote the given attribute to from a face-vertex to a vertex attribute.
///
/// This is done by splitting the vertex positions for unique values of the
/// given face-vertex attribute.
///
/// If the given attribute is already a vertex attribute, skip it.
fn promote_texture_coordinate_attribute_to_vertices(
    mesh: &mut TriMesh<f32>,
    attrib: (&String, &ComponentType),
) -> Result<(), AttribError> {
    use meshx::attrib::Attrib;
    use meshx::topology::FaceVertexIndex;

    if mesh.attrib_exists::<FaceVertexIndex>(attrib.0) {
        // Split the mesh according to texture attributes such that every unique
        // texture attribute value will have its own unique vertex. This is
        // required since gltf doesn't support multiple topologies.

        mesh.split_vertices_by_face_vertex_attrib(attrib.0);

        match *attrib.1 {
            ComponentType::U8 => try_tex_coord_promote::<u8>(attrib.0, mesh),
            ComponentType::U16 => try_tex_coord_promote::<u16>(attrib.0, mesh),
            ComponentType::F32 => try_tex_coord_promote::<f32>(attrib.0, mesh),
            t => Err(AttribError::InvalidTexCoordAttribType(t)),
        }
    } else {
        Ok(())
    }
}

/// Promote the given attribute to from a face-vertex to a vertex attribute.
///
/// This is done by splitting the vertex positions for unique values of the
/// given face-vertex attribute.
///
/// If the given attribute is already a vertex attribute, skip it.
fn promote_vector3_float_attribute_to_vertices(
    mesh: &mut TriMesh<f32>,
    attrib: (&String, &Type),
) -> Result<(), AttribError> {
    use meshx::attrib::Attrib;
    use meshx::topology::FaceVertexIndex;

    if mesh.attrib_exists::<FaceVertexIndex>(attrib.0) {
        mesh.split_vertices_by_face_vertex_attrib(attrib.0);

        match *attrib.1 {
            Type::Vec3(ComponentType::F32) => try_vec3_promote::<f32>(attrib.0, mesh),
            t => Err(AttribError::InvalidVector3AttribType(t)),
        }
    } else {
        Ok(())
    }
}

/// Remove the given vertex attribute.
fn remove_texture_coordinate_attribute(
    mesh: &mut TriMesh<f32>,
    attrib: (&String, &ComponentType),
    id: usize,
) -> Result<TextureAttribute, AttribError> {
    use meshx::attrib::Attrib;

    // The attribute has been promoted, remove it from the mesh for later use.
    Ok(mesh
        .remove_attrib::<VertexIndex>(attrib.0)
        .map(|a| TextureAttribute {
            id: id as u32,
            name: attrib.0.clone(),
            component_type: *attrib.1,
            attribute: a,
        })?)
}

#[derive(Clone, Debug, PartialEq)]
pub struct TextureAttribute {
    pub id: u32,
    pub name: String,
    pub component_type: ComponentType,
    pub attribute: VertexAttribute,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Attribute {
    pub name: String,
    pub type_: Type,
    pub attribute: VertexAttribute,
}

#[macro_export]
macro_rules! call_typed_fn {
    ($type:expr => $prefix:ident :: $fn:ident :: <_$(,$params:ident)*> $args:tt ) => {
        {
            match $type {
                Type::Scalar(ComponentType::I8)  | Type::I8 =>  $prefix :: $fn::<i8 $(,$params)*> $args,
                Type::Scalar(ComponentType::U8)  | Type::U8 =>  $prefix :: $fn::<u8 $(,$params)*> $args,
                Type::Scalar(ComponentType::I16) | Type::I16 => $prefix :: $fn::<i16 $(,$params)*>$args,
                Type::Scalar(ComponentType::U16) | Type::U16 => $prefix :: $fn::<u16 $(,$params)*>$args,
                Type::Scalar(ComponentType::U32) | Type::U32 => $prefix :: $fn::<u32 $(,$params)*>$args,
                Type::Scalar(ComponentType::F32) | Type::F32 => $prefix :: $fn::<f32 $(,$params)*>$args,

                Type::Vec2(ComponentType::I8 ) => $prefix :: $fn::<[i8 ; 2] $(,$params)*>$args,
                Type::Vec2(ComponentType::U8 ) => $prefix :: $fn::<[u8 ; 2] $(,$params)*>$args,
                Type::Vec2(ComponentType::I16) => $prefix :: $fn::<[i16; 2] $(,$params)*>$args,
                Type::Vec2(ComponentType::U16) => $prefix :: $fn::<[u16; 2] $(,$params)*>$args,
                Type::Vec2(ComponentType::U32) => $prefix :: $fn::<[u32; 2] $(,$params)*>$args,
                Type::Vec2(ComponentType::F32) => $prefix :: $fn::<[f32; 2] $(,$params)*>$args,

                Type::Vec3(ComponentType::I8 ) => $prefix :: $fn::<[i8 ; 3] $(,$params)*>$args,
                Type::Vec3(ComponentType::U8 ) => $prefix :: $fn::<[u8 ; 3] $(,$params)*>$args,
                Type::Vec3(ComponentType::I16) => $prefix :: $fn::<[i16; 3] $(,$params)*>$args,
                Type::Vec3(ComponentType::U16) => $prefix :: $fn::<[u16; 3] $(,$params)*>$args,
                Type::Vec3(ComponentType::U32) => $prefix :: $fn::<[u32; 3] $(,$params)*>$args,
                Type::Vec3(ComponentType::F32) => $prefix :: $fn::<[f32; 3] $(,$params)*>$args,

                Type::Vec4(ComponentType::I8 ) => $prefix :: $fn::<[i8 ; 4] $(,$params)*>$args,
                Type::Vec4(ComponentType::U8 ) => $prefix :: $fn::<[u8 ; 4] $(,$params)*>$args,
                Type::Vec4(ComponentType::I16) => $prefix :: $fn::<[i16; 4] $(,$params)*>$args,
                Type::Vec4(ComponentType::U16) => $prefix :: $fn::<[u16; 4] $(,$params)*>$args,
                Type::Vec4(ComponentType::U32) => $prefix :: $fn::<[u32; 4] $(,$params)*>$args,
                Type::Vec4(ComponentType::F32) => $prefix :: $fn::<[f32; 4] $(,$params)*>$args,

                Type::Mat2(ComponentType::I8 ) =>  $prefix :: $fn::<[[i8 ; 2]; 2] $(,$params)*>$args,
                Type::Mat2(ComponentType::U8 ) =>  $prefix :: $fn::<[[u8 ; 2]; 2] $(,$params)*>$args,
                Type::Mat2(ComponentType::I16) => $prefix :: $fn::<[[i16; 2]; 2] $(,$params)*>$args,
                Type::Mat2(ComponentType::U16) => $prefix :: $fn::<[[u16; 2]; 2] $(,$params)*>$args,
                Type::Mat2(ComponentType::U32) => $prefix :: $fn::<[[u32; 2]; 2] $(,$params)*>$args,
                Type::Mat2(ComponentType::F32) => $prefix :: $fn::<[[f32; 2]; 2] $(,$params)*>$args,

                Type::Mat3(ComponentType::I8 ) => $prefix :: $fn::<[[i8 ; 3]; 3] $(,$params)*>$args,
                Type::Mat3(ComponentType::U8 ) => $prefix :: $fn::<[[u8 ; 3]; 3] $(,$params)*>$args,
                Type::Mat3(ComponentType::I16) => $prefix :: $fn::<[[i16; 3]; 3] $(,$params)*>$args,
                Type::Mat3(ComponentType::U16) => $prefix :: $fn::<[[u16; 3]; 3] $(,$params)*>$args,
                Type::Mat3(ComponentType::U32) => $prefix :: $fn::<[[u32; 3]; 3] $(,$params)*>$args,
                Type::Mat3(ComponentType::F32) => $prefix :: $fn::<[[f32; 3]; 3] $(,$params)*>$args,

                Type::Mat4(ComponentType::I8 ) => $prefix :: $fn::<[[i8 ; 4]; 4] $(,$params)*>$args,
                Type::Mat4(ComponentType::U8 ) => $prefix :: $fn::<[[u8 ; 4]; 4] $(,$params)*>$args,
                Type::Mat4(ComponentType::I16) => $prefix :: $fn::<[[i16; 4]; 4] $(,$params)*>$args,
                Type::Mat4(ComponentType::U16) => $prefix :: $fn::<[[u16; 4]; 4] $(,$params)*>$args,
                Type::Mat4(ComponentType::U32) => $prefix :: $fn::<[[u32; 4]; 4] $(,$params)*>$args,
                Type::Mat4(ComponentType::F32) => $prefix :: $fn::<[[f32; 4]; 4] $(,$params)*>$args,
            }
        }
    };
}

/*
 * Parsing attributes from command line
 */

#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum ComponentType {
    /// Signed 8-bit integer. Corresponds to `GL_BYTE`.
    #[serde(alias = "i8")]
    I8,
    /// Unsigned 8-bit integer. Corresponds to `GL_UNSIGNED_BYTE`.
    #[serde(alias = "u8")]
    U8,
    /// Signed 16-bit integer. Corresponds to `GL_SHORT`.
    #[serde(alias = "i16")]
    I16,
    /// Unsigned 16-bit integer. Corresponds to `GL_UNSIGNED_SHORT`.
    #[serde(alias = "u16")]
    U16,
    /// Unsigned 32-bit integer. Corresponds to `GL_UNSIGNED_INT`.
    #[serde(alias = "u32")]
    U32,
    /// Single precision (32-bit) floating point number. Corresponds to `GL_FLOAT`.
    #[serde(alias = "f32")]
    F32,
}

impl std::str::FromStr for ComponentType {
    type Err = ron::de::Error;
    fn from_str(input: &str) -> Result<Self, Self::Err> {
        ron::de::from_str(input).map_err(Self::Err::from)
    }
}

impl From<ComponentType> for json::accessor::ComponentType {
    fn from(t: ComponentType) -> json::accessor::ComponentType {
        match t {
            ComponentType::I8 => json::accessor::ComponentType::I8,
            ComponentType::U8 => json::accessor::ComponentType::U8,
            ComponentType::I16 => json::accessor::ComponentType::I16,
            ComponentType::U16 => json::accessor::ComponentType::U16,
            ComponentType::U32 => json::accessor::ComponentType::U32,
            ComponentType::F32 => json::accessor::ComponentType::F32,
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Type {
    /// Signed 8-bit integer. Corresponds to `GL_BYTE`.
    #[serde(alias = "i8")]
    I8,
    /// Unsigned 8-bit integer. Corresponds to `GL_UNSIGNED_BYTE`.
    #[serde(alias = "u8")]
    U8,
    /// Signed 16-bit integer. Corresponds to `GL_SHORT`.
    #[serde(alias = "i16")]
    I16,
    /// Unsigned 16-bit integer. Corresponds to `GL_UNSIGNED_SHORT`.
    #[serde(alias = "u16")]
    U16,
    /// Unsigned 32-bit integer. Corresponds to `GL_UNSIGNED_INT`.
    #[serde(alias = "u32")]
    U32,
    /// Single precision (32-bit) floating point number. Corresponds to `GL_FLOAT`.
    #[serde(alias = "f32")]
    F32,
    /// Scalar quantity.
    #[serde(alias = "scalar")]
    Scalar(ComponentType),
    /// 2D vector.
    #[serde(alias = "vec2")]
    Vec2(ComponentType),
    /// 3D vector.
    #[serde(alias = "vec3")]
    Vec3(ComponentType),
    /// 4D vector.
    #[serde(alias = "vec4")]
    Vec4(ComponentType),
    /// 2x2 matrix.
    #[serde(alias = "mat2")]
    Mat2(ComponentType),
    /// 3x3 matrix.
    #[serde(alias = "mat3")]
    Mat3(ComponentType),
    /// 4x4 matrix.
    #[serde(alias = "mat4")]
    Mat4(ComponentType),
}

impl From<Type> for (json::accessor::Type, json::accessor::ComponentType) {
    fn from(t: Type) -> (json::accessor::Type, json::accessor::ComponentType) {
        let type_ = match t {
            Type::I8
            | Type::U8
            | Type::I16
            | Type::U16
            | Type::U32
            | Type::F32
            | Type::Scalar(_) => json::accessor::Type::Scalar,
            Type::Vec2(_) => json::accessor::Type::Vec2,
            Type::Vec3(_) => json::accessor::Type::Vec3,
            Type::Vec4(_) => json::accessor::Type::Vec4,
            Type::Mat2(_) => json::accessor::Type::Mat2,
            Type::Mat3(_) => json::accessor::Type::Mat3,
            Type::Mat4(_) => json::accessor::Type::Mat4,
        };

        let component_type = match t {
            Type::I8 => json::accessor::ComponentType::I8,
            Type::U8 => json::accessor::ComponentType::U8,
            Type::I16 => json::accessor::ComponentType::I16,
            Type::U16 => json::accessor::ComponentType::U16,
            Type::U32 => json::accessor::ComponentType::U32,
            Type::F32 => json::accessor::ComponentType::F32,
            Type::Scalar(c)
            | Type::Vec2(c)
            | Type::Vec3(c)
            | Type::Vec4(c)
            | Type::Mat2(c)
            | Type::Mat3(c)
            | Type::Mat4(c) => c.into(),
        };

        (type_, component_type)
    }
}

// Note that indexmap is essential here since we want to preserve the order of the texture
// coordinate attributes since we are using it explicitly in the gltf output.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct TextureAttributeInfo(pub IndexMap<String, ComponentType>);

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

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct AttributeInfo(pub IndexMap<String, Type>);

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

impl std::str::FromStr for AttributeInfo {
    type Err = ron::de::Error;
    fn from_str(input: &str) -> Result<AttributeInfo, Self::Err> {
        let idx_map: Result<IndexMap<String, Type>, Self::Err> =
            ron::de::from_str(input).map_err(Self::Err::from);
        idx_map.map(AttributeInfo)
    }
}

impl std::str::FromStr for TextureAttributeInfo {
    type Err = ron::de::Error;
    fn from_str(input: &str) -> Result<TextureAttributeInfo, Self::Err> {
        let idx_map: Result<IndexMap<String, ComponentType>, Self::Err> =
            ron::de::from_str(input).map_err(Self::Err::from);
        idx_map.map(TextureAttributeInfo)
    }
}