mesh_loader/collada/
geometry.rs

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
use super::*;

/// The `<library_geometries>` element.
///
/// See the [specification][1.4] for details.
///
/// [1.4] https://www.khronos.org/files/collada_spec_1_4.pdf#page=99
#[derive(Default)]
pub(super) struct LibraryGeometries<'a> {
    // /// The unique identifier of this element.
    // pub(super) id: Option<&'a str>,
    // /// The name of this element.
    // pub(super) name: Option<&'a str>,
    pub(super) geometries: BTreeMap<&'a str, Geometry<'a>>,

    pub(super) accessors: HashMap<&'a str, Accessor<'a>>,
    pub(super) array_data: HashMap<&'a str, ArrayData<'a>>,
}

/// The `<geometry>` element.
///
/// See the [specification][1.4] for details.
///
/// [1.4]: https://www.khronos.org/files/collada_spec_1_4.pdf#page=68
pub(super) struct Geometry<'a> {
    /// The unique identifier of this element.
    pub(super) id: &'a str,
    // /// The name of this element.
    // pub(super) name: Option<&'a str>,
    pub(super) mesh: Mesh<'a>,
}

/// The `<mesh>` element.
///
/// See the [specification][1.4] for details.
///
/// [1.4]: https://www.khronos.org/files/collada_spec_1_4.pdf#page=112
pub(super) struct Mesh<'a> {
    pub(super) vertices: Vertices<'a>,
    pub(super) primitives: Vec<Primitive<'a>>,
}

pub(super) struct Vertices<'a> {
    /// The unique identifier of this element.
    pub(super) id: &'a str,
    // /// The name of this element.
    // pub(super) name: Option<&'a str>,
    pub(super) input: VerticesInputs<'a>,
}

pub(super) struct VerticesInputs<'a> {
    pub(super) position: UnsharedInput<'a>,
    pub(super) normal: Option<UnsharedInput<'a>>,
    pub(super) texcoord: Option<UnsharedInput<'a>>,
    pub(super) color: Option<UnsharedInput<'a>>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(super) enum PrimitiveType {
    /// The `<lines>` element.
    Lines,
    /// The `<linestrips>` element.
    LineStrips,
    /// The `<polygons>` element.
    Polygons,
    /// The `<polylist>` element.
    Polylist,
    /// The `<triangles>` element.
    Triangles,
    /// The `<trifans>` element.
    TriFans,
    /// The `<tristrips>` element.
    TriStrips,
}

impl PrimitiveType {
    pub(super) fn face_size(self) -> Option<u32> {
        match self {
            PrimitiveType::Lines | PrimitiveType::LineStrips => Some(2),
            PrimitiveType::Triangles | PrimitiveType::TriFans | PrimitiveType::TriStrips => Some(3),
            PrimitiveType::Polygons | PrimitiveType::Polylist => None,
        }
    }

    pub(super) fn min_face_size(self) -> u32 {
        self.face_size().unwrap_or(1)
    }
}

pub(super) struct PrimitiveInputs<'a> {
    pub(super) vertex: SharedInput<'a, Vertices<'a>>,
    pub(super) normal: Option<SharedInput<'a>>,
    pub(super) color: Option<SharedInput<'a>>,
    pub(super) texcoord: Vec<SharedInput<'a>>,
}

pub(super) struct Primitive<'a> {
    /// The type of this element.
    pub(super) ty: PrimitiveType,

    // /// The name of this element.
    // pub(super) name: Option<&'a str>,
    /// The number of primitives.
    pub(super) count: u32,
    /// A symbol for a material.
    pub(super) material: Option<&'a str>,

    /// Declares the input semantics of a data source and connects a consumer to that source.
    pub(super) input: Option<PrimitiveInputs<'a>>,
    /// The number of vertices for one polygon.
    ///
    /// Only [polylist] actually have a vcount element, but we use this field to
    /// represent the number of primitives other than [lines] and [triangles].
    ///
    /// The values included in this list are:
    ///
    /// - For [polylist] and [polygons]: `1 <= n`, and contains one polygon.
    /// - For [linestrips]: `2 <= n`, and contains `n - 1` lines.
    /// - For [tristrips] and [trifans]: `3 <= n`, and contains `n - 2` triangles.
    ///
    /// For [lines] and [triangles]: Since we know vcount of [lines] is always `vec![2; count]` and vcount of
    /// [triangles] is always `vec![3; count]`, this field is not used and is empty.
    ///
    /// [lines]: PrimitiveType::Lines
    /// [linestrips]: PrimitiveType::LineStrips
    /// [polylist]: PrimitiveType::Polylist
    /// [polygons]: PrimitiveType::Polygons
    /// [triangles]: PrimitiveType::Triangles
    /// [trifans]: PrimitiveType::TriFans
    /// [tristrips]: PrimitiveType::TriStrips
    pub(super) vcount: Vec<u32>,
    /// The vertex attributes (indices) for an individual primitive.
    pub(super) p: Vec<u32>,

    pub(super) stride: u32,
}

// -----------------------------------------------------------------------------
// Parsing

pub(super) fn parse_library_geometries<'a>(
    cx: &mut Context<'a>,
    node: xml::Node<'a, '_>,
) -> io::Result<()> {
    debug_assert_eq!(node.tag_name().name(), "library_geometries");
    // cx.library_geometries.id = node.attribute("id");
    // cx.library_geometries.name = node.attribute("name");

    for node in node.element_children() {
        match node.tag_name().name() {
            "geometry" => {
                if let Some(geometry) = parse_geometry(cx, node)? {
                    cx.library_geometries
                        .geometries
                        .insert(geometry.id, geometry);
                }
            }
            "asset" | "extra" => { /* skip */ }
            _ => return Err(error::unexpected_child_elem(node)),
        }
    }

    if cx.library_geometries.geometries.is_empty() {
        return Err(error::one_or_more_elems(node, "geometry"));
    }

    Ok(())
}

fn parse_geometry<'a>(
    cx: &mut Context<'a>,
    node: xml::Node<'a, '_>,
) -> io::Result<Option<Geometry<'a>>> {
    debug_assert_eq!(node.tag_name().name(), "geometry");
    // The specification say it is optional, but it is actually required.
    let id = node.required_attribute("id")?;
    let mut mesh = None;

    for node in node.element_children() {
        match node.tag_name().name() {
            "mesh" => {
                mesh = Some(parse_mesh(cx, node)?);
            }
            "convex_mesh" | "spline" | "brep" => {
                // warn!(
                //     "<{}> child element in <{}> element is unsupported ({})",
                //     child.tag_name().name(),
                //     child.parent_element().unwrap().tag_name().name(),
                //     child.node_location()
                // );
                return Ok(None);
            }
            "asset" | "extra" => { /* skip */ }
            _ => return Err(error::unexpected_child_elem(node)),
        }
    }

    let mesh = match mesh {
        Some(mesh) => mesh,
        None => return Err(error::one_or_more_elems(node, "mesh")),
    };

    Ok(Some(Geometry {
        id,
        // name: node.attribute("name"),
        mesh,
    }))
}

fn parse_mesh<'a>(cx: &mut Context<'a>, node: xml::Node<'a, '_>) -> io::Result<Mesh<'a>> {
    debug_assert_eq!(node.tag_name().name(), "mesh");
    let mut primitives = vec![];
    let mut has_source = false;
    let mut vertices = None;

    for node in node.element_children() {
        let name = node.tag_name().name();
        match name {
            "source" => {
                has_source = true;
                let s = Source::parse(node)?;
                if let Some(acc) = s.accessor {
                    cx.library_geometries.accessors.insert(s.id, acc);
                }
                if let Some(data) = s.array_element {
                    cx.library_geometries.array_data.insert(data.id, data.data);
                }
            }
            "vertices" => {
                vertices = Some(parse_vertices(node)?);
            }
            "lines" | "linestrips" | "polygons" | "polylist" | "triangles" | "trifans"
            | "tristrips" => {
                primitives.push(parse_primitive(node, name.parse().unwrap())?);
            }
            "extra" => { /* skip */ }
            _ => return Err(error::unexpected_child_elem(node)),
        }
    }

    if !has_source {
        return Err(error::one_or_more_elems(node, "source"));
    }
    let vertices = match vertices {
        Some(vertices) => vertices,
        None => return Err(error::exactly_one_elem(node, "vertices")),
    };

    Ok(Mesh {
        vertices,
        primitives,
    })
}

fn parse_vertices<'a>(node: xml::Node<'a, '_>) -> io::Result<Vertices<'a>> {
    debug_assert_eq!(node.tag_name().name(), "vertices");
    let id = node.required_attribute("id")?;

    let mut input_position = None;
    let mut input_normal = None;
    let mut input_texcoord = None;
    let mut input_color = None;

    for node in node.element_children() {
        match node.tag_name().name() {
            "input" => {
                let i = UnsharedInput::parse(node)?;
                match i.semantic {
                    InputSemantic::POSITION => input_position = Some(i),
                    InputSemantic::NORMAL => input_normal = Some(i),
                    InputSemantic::TEXCOORD => input_texcoord = Some(i),
                    InputSemantic::COLOR => input_color = Some(i),
                    _semantic => {
                        // warn!(
                        //     "unsupported semantic {:?} in <input> ({})",
                        //     semantic,
                        //     node.node_location(),
                        // );
                    }
                }
            }
            "extra" => { /* skip */ }
            _ => return Err(error::unexpected_child_elem(node)),
        }
    }

    // One input must specify semantic="POSITION".
    let input_position = match input_position {
        Some(input_position) => input_position,
        None => return Err(error::one_or_more_elems(node, "input")),
    };

    Ok(Vertices {
        id,
        // name: node.attribute("name"),
        input: VerticesInputs {
            position: input_position,
            normal: input_normal,
            texcoord: input_texcoord,
            color: input_color,
        },
    })
}

impl FromStr for PrimitiveType {
    type Err = io::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "lines" => Self::Lines,
            "linestrips" => Self::LineStrips,
            "polygons" => Self::Polygons,
            "polylist" => Self::Polylist,
            "triangles" => Self::Triangles,
            "trifans" => Self::TriFans,
            "tristrips" => Self::TriStrips,
            _ => bail!("unknown primitive type {:?}", s),
        })
    }
}

fn parse_primitive<'a>(node: xml::Node<'a, '_>, ty: PrimitiveType) -> io::Result<Primitive<'a>> {
    debug_assert_eq!(node.tag_name().name().parse::<PrimitiveType>().unwrap(), ty);
    let count: u32 = node.parse_required_attribute("count")?;
    let mut vcount = vec![];
    let mut p = vec![];
    let mut stride = 0;

    let mut input_vertex = None;
    let mut input_normal = None;
    let mut input_color = None;
    let mut input_texcoord = vec![];

    for node in node.element_children() {
        match node.tag_name().name() {
            "input" => {
                let i = SharedInput::parse(node)?;
                stride = cmp::max(stride, i.offset + 1);
                match i.semantic {
                    InputSemantic::VERTEX => {
                        // ignore all position streams except 0 - there can be only one position
                        if i.set == 0 {
                            input_vertex = Some(i);
                        }
                    }
                    InputSemantic::NORMAL => {
                        // ignore all position streams except 0 - there can be only one position
                        if i.set == 0 {
                            input_normal = Some(i);
                        }
                    }
                    InputSemantic::COLOR => input_color = Some(i),
                    InputSemantic::TEXCOORD => input_texcoord.push(i),
                    _semantic => {
                        // warn!(
                        //     "unsupported semantic {:?} in <input> ({})",
                        //     semantic,
                        //     node.node_location(),
                        // );
                    }
                }
            }
            "vcount" => {
                // Only <polylist> has <vcount>.
                if ty != PrimitiveType::Polylist {
                    return Err(error::unexpected_child_elem(node));
                }
                if !vcount.is_empty() {
                    return Err(error::multiple_elems(node));
                }
                // It is possible to not contain any indices.
                if count == 0 {
                    continue;
                }

                vcount.reserve(count as usize);

                // TODO: use parse_int_array_exact?
                let mut iter = xml::parse_int_array::<u32>(node.trimmed_text());
                for _ in 0..count {
                    let value = iter.next().ok_or_else(|| {
                        format_err!(
                            "expected more values while reading <{}> contents at {}",
                            node.tag_name().name(),
                            node.node_location()
                        )
                    })??;
                    if value >= 1 {
                        vcount.push(value);
                    } else {
                        bail!(
                            "incorrect number of indices in <p> element ({})",
                            node.node_location()
                        );
                    }
                }
            }
            "p" => {
                // It is possible to not contain any indices.
                if count == 0 {
                    continue;
                }

                if matches!(
                    ty,
                    PrimitiveType::Lines | PrimitiveType::Polylist | PrimitiveType::Triangles
                ) {
                    // For primitives with at most one <p> element,
                    // the length of indices can be pre-calculated.

                    if !p.is_empty() {
                        return Err(error::multiple_elems(node));
                    }

                    let mut expected_count = 0;
                    match ty {
                        PrimitiveType::Polylist => {
                            for &i in &vcount {
                                expected_count += i as usize;
                            }
                        }
                        PrimitiveType::Lines => {
                            expected_count = count as usize * 2;
                        }
                        PrimitiveType::Triangles => {
                            expected_count = count as usize * 3;
                        }
                        _ => unreachable!(),
                    }

                    p.reserve(expected_count * stride as usize);

                    // TODO: It seems some exporters put negative indices sometimes.
                    // TODO: use parse_int_array_exact?
                    for value in xml::parse_int_array(node.trimmed_text()) {
                        p.push(value.map_err(|e| {
                            format_err!(
                                "{e} in <{}> element ({})",
                                node.tag_name().name(),
                                node.text_location(),
                            )
                        })?);
                    }

                    if p.len() != expected_count * stride as usize {
                        // TODO: It seems SketchUp 15.3.331 writes the wrong 'count' for 'lines'.
                        bail!(
                            "incorrect index count in <p> element, expected {} but found {} ({})",
                            expected_count * stride as usize,
                            p.len(),
                            node.node_location()
                        );
                    }
                } else {
                    // For primitives that can have multiple <p> elements,
                    // One <p> element corresponds to one polygon.
                    // Therefore, we represent them in the same way as polylist.
                    // See the description of the `Primitive::vcount` field for more information.

                    if vcount.capacity() == 0 {
                        vcount.reserve(count as usize);
                    }

                    let prev_len = p.len();

                    // TODO: It seems some exporters put negative indices sometimes.
                    // TODO: use parse_int_array_exact?
                    for value in xml::parse_int_array(node.trimmed_text()) {
                        p.push(value.map_err(|e| {
                            format_err!(
                                "{e} in <{}> element ({})",
                                node.tag_name().name(),
                                node.text_location(),
                            )
                        })?);
                    }

                    #[allow(clippy::cast_possible_truncation)]
                    let added = (p.len() - prev_len) as u32;
                    if added % stride != 0 {
                        bail!(
                            "incorrect index count in <p> element, expected multiple of {}, but found {} ({})",
                            stride,
                            p.len(),
                            node.node_location()
                        );
                    }
                    let vc = added / stride;
                    if vc >= ty.min_face_size() {
                        vcount.push(vc);
                    } else {
                        bail!(
                            "incorrect number of indices in <p> element ({})",
                            node.node_location()
                        );
                    }
                }
            }
            "ph" => {
                // warn!(
                //     "<{}> child element in <{}> element is unsupported ({})",
                //     child.tag_name().name(),
                //     child.parent_element().unwrap().tag_name().name(),
                //     child.node_location()
                // );
            }
            "extra" => { /* skip */ }
            _ => return Err(error::unexpected_child_elem(node)),
        }
    }

    // When at least one input is present, one input must specify semantic="VERTEX".
    if input_vertex.is_none()
        && (input_normal.is_some() || input_color.is_some() || !input_texcoord.is_empty())
    {
        bail!(
            "one <input> in <{}> element must specify semantic=\"VERTEX\" ({})",
            node.tag_name().name(),
            node.node_location()
        );
    }
    // Attempt to respect the specified set.
    if !input_texcoord.is_empty() {
        input_texcoord.sort_by_key(|i| i.set);
    }

    Ok(Primitive {
        ty,
        // name: node.attribute("name"),
        count,
        material: node.attribute("material"),
        input: input_vertex.map(|vertex| PrimitiveInputs {
            vertex: vertex.cast(),
            normal: input_normal,
            color: input_color,
            texcoord: input_texcoord,
        }),
        vcount,
        p,
        stride,
    })
}