polysmelt 0.17.6

Pure-Rust 3D asset interchange: glTF/GLB reading and writing, meshopt compression, and readers for common mesh formats, SketchUp included
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
//! X3D's XML encoding: a node is an element, its fields attributes, and a child node goes in
//! the field its `containerField` names, or the one its kind goes in by default. A
//! `ProtoInstance` is a node of the kind its `name` gives, its `fieldValue`s its fields.

use std::collections::{HashMap, HashSet};

use quick_xml::events::{BytesStart, Event};

use super::{Graph, MAX_DEPTH, Proto, Val};
use crate::formats::stream::f64_from;

/// What an open element is.
#[derive(Clone, Copy)]
enum Frame {
    Node(usize),
    /// `X3D`, `Scene` and `head`: their nodes are the scene's roots.
    Top,
    /// Not read, nor anything in it.
    Skip,
    /// `ProtoDeclare`, for the prototype it declares.
    Proto(usize),
    /// `ProtoInterface`, of a prototype.
    Interface(usize),
    /// A `field` of a prototype's interface: the prototype, and the field's place in it.
    Field(usize, usize),
    /// `ProtoBody`, of a prototype.
    Body(usize),
    /// `IS`, in a node.
    Is(usize),
    /// A `fieldValue` of a prototype instance: the node, and the field's place in it.
    FieldValue(usize, usize),
}

/// An XML file's scene graph, as much of it as reads.
pub(super) fn parse(d: &[u8]) -> Graph {
    let mut reader = quick_xml::Reader::from_reader(d);
    reader.config_mut().check_end_names = false;
    let mut p = Parser {
        g: Graph::default(),
        defs: HashMap::new(),
        saved: Vec::new(),
        strings: HashSet::new(),
        stack: Vec::new(),
    };
    loop {
        match reader.read_event() {
            Ok(Event::Start(e)) => {
                let f = p.open(&e);
                p.stack.push(f);
            }
            Ok(Event::Empty(e)) => {
                let f = p.open(&e);
                p.close(f);
            }
            Ok(Event::End(_)) => {
                if let Some(f) = p.stack.pop() {
                    p.close(f);
                }
            }
            Ok(Event::Eof) | Err(_) => break,
            Ok(_) => {}
        }
    }
    p.g
}

struct Parser {
    g: Graph,
    defs: HashMap<Vec<u8>, usize>,
    /// The file's names while a prototype's own are in `defs`.
    saved: Vec<HashMap<Vec<u8>, usize>>,
    /// The prototype fields whose type is a string, by prototype and field.
    strings: HashSet<(String, String)>,
    stack: Vec<Frame>,
}

impl Parser {
    /// Ends element `f`.
    fn close(&mut self, f: Frame) {
        if let Frame::Proto(_) = f
            && let Some(defs) = self.saved.pop()
        {
            self.defs = defs;
        }
    }

    /// Reads element `e` into the graph: what it is, for the elements inside it.
    fn open(&mut self, e: &BytesStart<'_>) -> Frame {
        let parent = self.stack.last().copied();
        if matches!(parent, Some(Frame::Skip)) || self.stack.len() >= MAX_DEPTH {
            return Frame::Skip;
        }
        let local = e.local_name();
        let kind = local.as_ref();
        let text = |name: &[u8]| attr(e, name).map(|v| String::from_utf8_lossy(v.trim_ascii()).into_owned());
        match (parent, kind) {
            (Some(Frame::Proto(i)), b"ProtoInterface") => return Frame::Interface(i),
            (Some(Frame::Proto(i)), b"ProtoBody") => return Frame::Body(i),
            (Some(Frame::Interface(i)), b"field") => {
                let Some(name) = text(b"name") else {
                    return Frame::Skip;
                };
                if matches!(text(b"accessType").as_deref(), Some("inputOnly" | "outputOnly")) {
                    return Frame::Skip;
                }
                let proto = self.g.protos[i].name.clone();
                let typ = text(b"type").unwrap_or_default();
                let v = attr(e, b"value").map(|raw| match typ.as_str() {
                    "MFString" => Val::Strs(strings(unescape(&raw).trim_ascii())),
                    "SFString" => Val::Strs(vec![String::from_utf8_lossy(&unescape(&raw)).into_owned()]),
                    _ => value(name.as_bytes(), &raw),
                });
                if matches!(typ.as_str(), "SFString" | "MFString") {
                    self.strings.insert((proto, name.clone()));
                }
                let fields = &mut self.g.protos[i].fields;
                fields.push((name, v));
                return Frame::Field(i, fields.len() - 1);
            }
            (Some(Frame::Node(n)), b"IS") => return Frame::Is(n),
            (Some(Frame::Is(n)), b"connect") => {
                if let (Some(field), Some(from)) = (text(b"nodeField"), text(b"protoField")) {
                    self.g.set(n, &field, Val::Is(from));
                }
                return Frame::Skip;
            }
            (Some(Frame::Node(n)), b"fieldValue") => {
                let Some(name) = text(b"name") else {
                    return Frame::Skip;
                };
                let stringy = self.strings.contains(&(self.g.nodes[n].kind.clone(), name.clone()));
                let v = match attr(e, b"value") {
                    Some(raw) if stringy => Val::Strs(strings(unescape(&raw).trim_ascii())),
                    Some(raw) => value(name.as_bytes(), &raw),
                    None => Val::Nodes(Vec::new()),
                };
                self.g.set(n, &name, v);
                let slot = self.g.nodes[n].fields.iter().position(|(k, _)| *k == name).unwrap_or(0);
                return Frame::FieldValue(n, slot);
            }
            (Some(Frame::Proto(_) | Frame::Interface(_) | Frame::Is(_)), _) => return Frame::Skip,
            _ => {}
        }
        match kind {
            b"X3D" => {
                if let Some(v) = attr(e, b"version") {
                    self.g.version = String::from_utf8_lossy(&v).into_owned();
                }
                return Frame::Top;
            }
            b"Scene" | b"head" => return Frame::Top,
            b"unit" => {
                if attr(e, b"category").as_deref() == Some(b"length")
                    && let Some(f) = attr(e, b"conversionFactor").and_then(|f| f64_from(f.trim_ascii()))
                {
                    self.g.unit = Some(f);
                    self.g.unit_name = text(b"name");
                }
                return Frame::Skip;
            }
            b"meta" => {
                if matches!(parent, Some(Frame::Top))
                    && let (Some(name), Some(content)) = (text(b"name"), text(b"content"))
                {
                    self.g.meta.push((name, content));
                }
                return Frame::Skip;
            }
            b"component" | b"IS" | b"field" | b"fieldValue" | b"connect" => return Frame::Skip,
            b"ProtoDeclare" => {
                let name = text(b"name").unwrap_or_default();
                self.g.protos.push(Proto {
                    name,
                    ..Proto::default()
                });
                // Its names are its own, apart from the file's.
                self.saved.push(std::mem::take(&mut self.defs));
                return Frame::Proto(self.g.protos.len() - 1);
            }
            b"ExternProtoDeclare" => {
                if let Some(name) = text(b"name") {
                    let urls = attr(e, b"url").map(|u| strings(unescape(&u).trim_ascii()));
                    self.g.externs.push((name, urls.unwrap_or_default()));
                }
                return Frame::Skip;
            }
            b"ROUTE" | b"Script" | b"IMPORT" | b"EXPORT" => {
                self.g.skip(&String::from_utf8_lossy(kind));
                return Frame::Skip;
            }
            _ => {}
        }
        let instance = kind == b"ProtoInstance";
        let kind = if instance {
            let Some(name) = text(b"name") else {
                self.g.skip("ProtoInstance");
                return Frame::Skip;
            };
            name
        } else {
            String::from_utf8_lossy(kind).into_owned()
        };
        let mut container: Option<String> = None;
        let mut def: Option<Vec<u8>> = None;
        let mut fields: Vec<(String, Val)> = Vec::new();
        for a in e.attributes().with_checks(false).flatten() {
            let key = a.key.local_name();
            let key = key.as_ref();
            match key {
                b"DEF" => def = Some(unescape(&a.value).into_owned()),
                b"USE" => {
                    // An instance: the node named, with nothing of its own.
                    let name = unescape(&a.value);
                    if let Some(&id) = self.defs.get(name.trim_ascii()) {
                        self.place(parent, &kind, container.as_deref(), id, e);
                    }
                    return Frame::Skip;
                }
                b"containerField" => container = Some(String::from_utf8_lossy(&unescape(&a.value)).into_owned()),
                b"class" | b"id" | b"style" | b"xmlns" => {}
                b"name" if instance => {}
                _ => fields.push((String::from_utf8_lossy(key).into_owned(), value(key, &a.value))),
            }
        }
        let name = def
            .as_ref()
            .map(|d| String::from_utf8_lossy(d.trim_ascii()).into_owned());
        let id = self.g.add(&kind, name);
        self.g.nodes[id].fields = fields;
        if let Some(d) = def {
            self.defs.insert(d.trim_ascii().to_vec(), id);
        }
        self.place(parent, &kind, container.as_deref(), id, e);
        Frame::Node(id)
    }

    /// Puts node `id` in its parent's field, or among the roots.
    fn place(&mut self, parent: Option<Frame>, kind: &str, container: Option<&str>, id: usize, e: &BytesStart<'_>) {
        // `containerField` may come after `USE`.
        let container = container
            .map(str::to_owned)
            .or_else(|| attr(e, b"containerField").map(|c| String::from_utf8_lossy(c.trim_ascii()).into_owned()));
        match parent {
            Some(Frame::Node(p)) => {
                let field = container.as_deref().unwrap_or_else(|| default_container(kind));
                self.g.push_child(p, field, id);
            }
            Some(Frame::Body(i)) => self.g.protos[i].body.push(id),
            Some(Frame::Field(i, k)) => match &mut self.g.protos[i].fields[k].1 {
                Some(Val::Nodes(list)) => list.push(id),
                v => *v = Some(Val::Nodes(vec![id])),
            },
            Some(Frame::FieldValue(n, slot)) => {
                if let Some((_, v)) = self.g.nodes[n].fields.get_mut(slot) {
                    match v {
                        Val::Nodes(list) => list.push(id),
                        v => *v = Val::Nodes(vec![id]),
                    }
                }
            }
            Some(Frame::Top) | None => self.g.roots.push(id),
            Some(Frame::Skip | Frame::Proto(_) | Frame::Interface(_) | Frame::Is(_)) => {}
        }
    }
}

/// An attribute's value, its entities undone.
fn attr(e: &BytesStart<'_>, name: &[u8]) -> Option<Vec<u8>> {
    e.attributes()
        .with_checks(false)
        .flatten()
        .find(|a| a.key.local_name().as_ref() == name)
        .map(|a| unescape(&a.value).into_owned())
}

/// The field a child node goes in when it doesn't say.
fn default_container(kind: &str) -> &'static str {
    match kind {
        "Appearance" => "appearance",
        "Material" | "PhysicalMaterial" | "UnlitMaterial" | "TwoSidedMaterial" => "material",
        "ImageTexture" | "PixelTexture" | "MovieTexture" | "MultiTexture" => "texture",
        "TextureTransform" | "MultiTextureTransform" => "textureTransform",
        "TextureProperties" => "textureProperties",
        "Coordinate" | "CoordinateDouble" => "coord",
        "Normal" => "normal",
        "Color" | "ColorRGBA" => "color",
        "TextureCoordinate" | "MultiTextureCoordinate" | "TextureCoordinateGenerator" => "texCoord",
        "Box"
        | "Cone"
        | "Cylinder"
        | "Sphere"
        | "IndexedFaceSet"
        | "IndexedTriangleSet"
        | "IndexedTriangleStripSet"
        | "IndexedTriangleFanSet"
        | "IndexedQuadSet"
        | "TriangleSet"
        | "TriangleStripSet"
        | "TriangleFanSet"
        | "QuadSet"
        | "ElevationGrid"
        | "Extrusion"
        | "Text"
        | "PointSet"
        | "IndexedLineSet"
        | "LineSet"
        | "Arc2D"
        | "ArcClose2D"
        | "Circle2D"
        | "Disk2D"
        | "Polyline2D"
        | "Polypoint2D"
        | "Rectangle2D"
        | "TriangleSet2D" => "geometry",
        k if k.starts_with("Metadata") => "metadata",
        _ => "children",
    }
}

/// Text with XML's entities undone (borrowed when it has none).
fn unescape(s: &[u8]) -> std::borrow::Cow<'_, [u8]> {
    if !s.contains(&b'&') {
        return s.into();
    }
    let mut out = Vec::with_capacity(s.len());
    let mut i = 0;
    while i < s.len() {
        if s[i] == b'&'
            && let Some(len) = s[i..].iter().take(12).position(|&c| c == b';')
        {
            let name = &s[i + 1..i + len];
            let ch = match name {
                b"amp" => Some('&'),
                b"lt" => Some('<'),
                b"gt" => Some('>'),
                b"quot" => Some('"'),
                b"apos" => Some('\''),
                [b'#', b'x' | b'X', hex @ ..] => std::str::from_utf8(hex)
                    .ok()
                    .and_then(|h| u32::from_str_radix(h, 16).ok())
                    .and_then(char::from_u32),
                [b'#', dec @ ..] => std::str::from_utf8(dec)
                    .ok()
                    .and_then(|d| d.parse().ok())
                    .and_then(char::from_u32),
                _ => None,
            };
            if let Some(c) = ch {
                let mut buf = [0; 4];
                out.extend_from_slice(c.encode_utf8(&mut buf).as_bytes());
                i += len + 1;
                continue;
            }
        }
        out.push(s[i]);
        i += 1;
    }
    out.into()
}

/// Whether `b` separates the values in an attribute.
fn gap(b: u8) -> bool {
    b <= b' ' || b == b','
}

/// An attribute's value, its type told from how it's written: quoted strings for `url` and
/// any value starting with a quote, numbers, `true` and `false`, or else text.
fn value(key: &[u8], raw: &[u8]) -> Val {
    let text = unescape(raw);
    let t = text.trim_ascii();
    if key == b"url" || t.first() == Some(&b'"') {
        return Val::Strs(strings(t));
    }
    if matches!(key, b"title" | b"description" | b"name" | b"value") {
        return Val::Strs(vec![String::from_utf8_lossy(t).into_owned()]);
    }
    let mut nums = Vec::new();
    let mut numeric = true;
    for w in t.split(|&b| gap(b)).filter(|w| !w.is_empty()) {
        let hex = || {
            let h = w.strip_prefix(b"0x").or_else(|| w.strip_prefix(b"0X"))?;
            u32::from_str_radix(std::str::from_utf8(h).ok()?, 16)
                .ok()
                .map(f64::from)
        };
        if let Some(v) = f64_from(w).or_else(hex) {
            nums.push(v);
        } else {
            numeric = false;
            break;
        }
    }
    if numeric {
        return Val::Nums(nums);
    }
    let bools: Option<Vec<bool>> = t
        .split(|&b| gap(b))
        .filter(|w| !w.is_empty())
        .map(|w| {
            if w.eq_ignore_ascii_case(b"true") {
                Some(true)
            } else if w.eq_ignore_ascii_case(b"false") {
                Some(false)
            } else {
                None
            }
        })
        .collect();
    match bools {
        Some(b) => Val::Bools(b),
        None => Val::Strs(vec![String::from_utf8_lossy(t).into_owned()]),
    }
}

/// An `MFString`'s strings: each in quotes, `\"` and `\\` escaped; text with no quotes is one
/// string.
fn strings(t: &[u8]) -> Vec<String> {
    if !t.contains(&b'"') {
        return if t.is_empty() {
            Vec::new()
        } else {
            vec![String::from_utf8_lossy(t).into_owned()]
        };
    }
    let mut out = Vec::new();
    let mut i = 0;
    while let Some(open) = t[i..].iter().position(|&c| c == b'"') {
        let mut j = i + open + 1;
        let mut s = Vec::new();
        while j < t.len() && t[j] != b'"' {
            if t[j] == b'\\' && j + 1 < t.len() {
                j += 1;
            }
            s.push(t[j]);
            j += 1;
        }
        out.push(String::from_utf8_lossy(&s).into_owned());
        i = (j + 1).min(t.len());
    }
    out
}