polysmelt 0.17.7

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
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
//! The accessors, buffer views and animations readers build, held as plain values until the
//! document is written or its JSON is asked for. A JSON object for each would be built,
//! copied, read back by repair and serialized: for a model with thousands of animation
//! channels, most of the time a load takes.
//!
//! What's written is what the JSON objects would have written, byte for byte: keys in the
//! order a [`Map`] keeps them (sorted), numbers as `serde_json` formats them.

use std::io::Write as _;

use serde_json::{Map, Value, json};

use crate::{Context as _, ErrorKind, Result};

/// An accessor's `min` or `max`.
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum Limits {
    /// Floats: the `f32`s the data holds, widened, as JSON numbers hold them.
    Float(Vec<f64>),
    /// Integers, for integer components.
    Int(Vec<i64>),
}

impl Limits {
    pub(crate) fn floats(v: &[f32]) -> Self {
        Limits::Float(v.iter().map(|&x| f64::from(x)).collect())
    }

    pub(crate) fn len(&self) -> usize {
        match self {
            Limits::Float(v) => v.len(),
            Limits::Int(v) => v.len(),
        }
    }

    /// Component `i` as JSON reads it: `None` for a float that isn't finite (written as
    /// `null`).
    #[allow(clippy::cast_precision_loss, reason = "as `Value::as_f64` reads an integer")]
    pub(crate) fn get(&self, i: usize) -> Option<f64> {
        match self {
            Limits::Float(v) => v.get(i).copied().filter(|x| x.is_finite()),
            Limits::Int(v) => v.get(i).map(|&x| x as f64),
        }
    }

    fn to_value(&self) -> Value {
        match self {
            Limits::Float(v) => v.iter().map(|&x| Value::from(x)).collect(),
            Limits::Int(v) => v.iter().map(|&x| Value::from(x)).collect(),
        }
    }

    fn write(&self, w: &mut Vec<u8>) {
        w.push(b'[');
        match self {
            Limits::Float(v) => {
                for (i, &x) in v.iter().enumerate() {
                    if i > 0 {
                        w.push(b',');
                    }
                    float(w, x);
                }
            }
            Limits::Int(v) => {
                for (i, &x) in v.iter().enumerate() {
                    if i > 0 {
                        w.push(b',');
                    }
                    int(w, x);
                }
            }
        }
        w.push(b']');
    }
}

/// An accessor: its JSON's members.
#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct Accessor {
    pub buffer_view: Option<usize>,
    pub byte_offset: Option<u64>,
    pub component_type: u32,
    pub count: usize,
    /// `type`: `SCALAR`, `VEC3`, ...
    pub kind: &'static str,
    pub min: Option<Limits>,
    pub max: Option<Limits>,
    /// Written only when set.
    pub normalized: bool,
    pub sparse: Option<Sparse>,
}

/// A sparse accessor's replacements: `count` elements at the indices in view `indices` (of
/// component type `index_type`), their values in view `values`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Sparse {
    pub count: usize,
    pub indices: usize,
    pub index_type: u32,
    pub values: usize,
}

/// A buffer view.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct View {
    pub buffer: usize,
    pub byte_offset: Option<u64>,
    pub byte_length: u64,
    pub target: Option<u32>,
}

/// An animation.
#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct Animation {
    pub channels: Vec<Channel>,
    pub samplers: Vec<Sampler>,
    pub name: Option<String>,
    pub extras: Option<Value>,
}

/// An animation channel: sampler `sampler` drives `path` of node `node`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Channel {
    pub sampler: usize,
    pub node: usize,
    pub path: &'static str,
}

/// An animation sampler.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Sampler {
    pub input: usize,
    pub output: usize,
    pub interpolation: &'static str,
}

/// A document's accessors, buffer views and animations, when a reader built them: the JSON
/// holds none of these lists then.
#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct Tables {
    pub accessors: Vec<Accessor>,
    pub views: Vec<View>,
    pub animations: Vec<Animation>,
}

impl Accessor {
    pub(crate) fn to_value(&self) -> Value {
        let mut m = Map::new();
        if let Some(v) = self.buffer_view {
            m.insert("bufferView".into(), json!(v));
        }
        if let Some(o) = self.byte_offset {
            m.insert("byteOffset".into(), json!(o));
        }
        m.insert("componentType".into(), json!(self.component_type));
        m.insert("count".into(), json!(self.count));
        if let Some(x) = &self.max {
            m.insert("max".into(), x.to_value());
        }
        if let Some(x) = &self.min {
            m.insert("min".into(), x.to_value());
        }
        if self.normalized {
            m.insert("normalized".into(), json!(true));
        }
        if let Some(s) = &self.sparse {
            m.insert(
                "sparse".into(),
                json!({
                    "count": s.count,
                    "indices": { "bufferView": s.indices, "componentType": s.index_type },
                    "values": { "bufferView": s.values },
                }),
            );
        }
        m.insert("type".into(), json!(self.kind));
        Value::Object(m)
    }

    fn write(&self, w: &mut Vec<u8>) {
        let mut o = Object::start(w);
        if let Some(v) = self.buffer_view {
            o.key("bufferView");
            uint(o.w, v as u64);
        }
        if let Some(x) = self.byte_offset {
            o.key("byteOffset");
            uint(o.w, x);
        }
        o.key("componentType");
        uint(o.w, u64::from(self.component_type));
        o.key("count");
        uint(o.w, self.count as u64);
        if let Some(x) = &self.max {
            o.key("max");
            x.write(o.w);
        }
        if let Some(x) = &self.min {
            o.key("min");
            x.write(o.w);
        }
        if self.normalized {
            o.key("normalized");
            o.w.extend_from_slice(b"true");
        }
        if let Some(s) = &self.sparse {
            o.key("sparse");
            let mut so = Object::start(o.w);
            so.key("count");
            uint(so.w, s.count as u64);
            so.key("indices");
            let mut io = Object::start(so.w);
            io.key("bufferView");
            uint(io.w, s.indices as u64);
            io.key("componentType");
            uint(io.w, u64::from(s.index_type));
            io.end();
            so.key("values");
            let mut vo = Object::start(so.w);
            vo.key("bufferView");
            uint(vo.w, s.values as u64);
            vo.end();
            so.end();
        }
        o.key("type");
        literal(o.w, self.kind);
        o.end();
    }
}

impl View {
    pub(crate) fn to_value(self) -> Value {
        let mut m = Map::new();
        m.insert("buffer".into(), json!(self.buffer));
        m.insert("byteLength".into(), json!(self.byte_length));
        if let Some(o) = self.byte_offset {
            m.insert("byteOffset".into(), json!(o));
        }
        if let Some(t) = self.target {
            m.insert("target".into(), json!(t));
        }
        Value::Object(m)
    }

    fn write(&self, w: &mut Vec<u8>) {
        let mut o = Object::start(w);
        o.key("buffer");
        uint(o.w, self.buffer as u64);
        o.key("byteLength");
        uint(o.w, self.byte_length);
        if let Some(x) = self.byte_offset {
            o.key("byteOffset");
            uint(o.w, x);
        }
        if let Some(t) = self.target {
            o.key("target");
            uint(o.w, u64::from(t));
        }
        o.end();
    }

    /// Moves the view to its buffer's place in a GLB, `home[buffer]` (buffer, offset), as
    /// [`rehome`](crate::gltf::rehome) moves a JSON one.
    pub(crate) fn rehome(&mut self, home: &[(usize, usize)]) -> Result<()> {
        let &(nb, base) = home
            .get(self.buffer)
            .with_context(ErrorKind::Corrupt, || format!("missing buffer {}", self.buffer))?;
        let off = self.byte_offset.unwrap_or(0);
        let at = off
            .checked_add(base as u64)
            .context(ErrorKind::Truncated, "byteOffset is too large")?;
        self.buffer = nb;
        if nb == 0 || off != 0 {
            self.byte_offset = Some(at);
        }
        Ok(())
    }
}

impl Animation {
    pub(crate) fn to_value(&self) -> Value {
        let channels: Vec<Value> = self
            .channels
            .iter()
            .map(|c| json!({ "sampler": c.sampler, "target": { "node": c.node, "path": c.path } }))
            .collect();
        let samplers: Vec<Value> = self
            .samplers
            .iter()
            .map(|s| json!({ "input": s.input, "interpolation": s.interpolation, "output": s.output }))
            .collect();
        let mut m = Map::new();
        m.insert("channels".into(), Value::Array(channels));
        if let Some(e) = &self.extras {
            m.insert("extras".into(), e.clone());
        }
        if let Some(n) = &self.name {
            m.insert("name".into(), json!(n));
        }
        m.insert("samplers".into(), Value::Array(samplers));
        Value::Object(m)
    }

    fn write(&self, w: &mut Vec<u8>) -> Result<()> {
        let mut o = Object::start(w);
        o.key("channels");
        list(o.w, &self.channels, |w, c| {
            let mut co = Object::start(w);
            co.key("sampler");
            uint(co.w, c.sampler as u64);
            co.key("target");
            let mut t = Object::start(co.w);
            t.key("node");
            uint(t.w, c.node as u64);
            t.key("path");
            literal(t.w, c.path);
            t.end();
            co.end();
        });
        if let Some(e) = &self.extras {
            o.key("extras");
            value(o.w, e)?;
        }
        if let Some(n) = &self.name {
            o.key("name");
            string(o.w, n)?;
        }
        o.key("samplers");
        list(o.w, &self.samplers, |w, s| {
            let mut so = Object::start(w);
            so.key("input");
            uint(so.w, s.input as u64);
            so.key("interpolation");
            literal(so.w, s.interpolation);
            so.key("output");
            uint(so.w, s.output as u64);
            so.end();
        });
        o.end();
        Ok(())
    }
}

impl Tables {
    /// Adds the lists to `json` as JSON. `pruned` leaves out empty ones, as repair does;
    /// otherwise the accessors and views are there whenever there are views, as a reader
    /// writes them.
    pub(crate) fn insert_into(&self, json: &mut Map<String, Value>, pruned: bool) {
        let views = !self.views.is_empty();
        if !self.accessors.is_empty() || (views && !pruned) {
            json.insert(
                "accessors".into(),
                self.accessors.iter().map(Accessor::to_value).collect(),
            );
        }
        if views {
            json.insert("bufferViews".into(), self.views.iter().map(|&v| v.to_value()).collect());
        }
        if !self.animations.is_empty() {
            json.insert(
                "animations".into(),
                self.animations.iter().map(Animation::to_value).collect(),
            );
        }
    }
}

/// Writes `json` with `tables`' lists (those not empty) among its members, in key order: what
/// writing `json` with [`Tables::insert_into`] (pruned) done to it would write.
pub(crate) fn write_json(w: &mut Vec<u8>, json: &Map<String, Value>, tables: Option<&Tables>) -> Result<()> {
    let mut typed: Vec<&str> = Vec::new();
    if let Some(t) = tables {
        // In key order.
        for (key, has) in [
            ("accessors", !t.accessors.is_empty()),
            ("animations", !t.animations.is_empty()),
            ("bufferViews", !t.views.is_empty()),
        ] {
            if has {
                typed.push(key);
            }
        }
    }
    let mut pending = typed.into_iter().peekable();
    let write_typed = |w: &mut Vec<u8>, key: &str| -> Result<()> {
        let Some(t) = tables else { return Ok(()) };
        match key {
            "accessors" => list(w, &t.accessors, |w, a| a.write(w)),
            "bufferViews" => list(w, &t.views, |w, v| v.write(w)),
            _ => {
                w.push(b'[');
                for (i, a) in t.animations.iter().enumerate() {
                    if i > 0 {
                        w.push(b',');
                    }
                    a.write(w)?;
                }
                w.push(b']');
            }
        }
        Ok(())
    };
    let mut o = Object::start(w);
    for (k, v) in json {
        while let Some(&key) = pending.peek() {
            if key >= k.as_str() {
                break;
            }
            o.key(key);
            write_typed(o.w, key)?;
            pending.next();
        }
        if pending.peek() == Some(&k.as_str()) {
            // The table's list stands for it.
            continue;
        }
        o.comma();
        string(o.w, k)?;
        o.w.push(b':');
        value(o.w, v)?;
    }
    for key in pending {
        o.key(key);
        write_typed(o.w, key)?;
    }
    o.end();
    Ok(())
}

/// A JSON object being written: commas between its members.
struct Object<'w> {
    w: &'w mut Vec<u8>,
    first: bool,
}

impl<'w> Object<'w> {
    fn start(w: &'w mut Vec<u8>) -> Self {
        w.push(b'{');
        Object { w, first: true }
    }

    fn comma(&mut self) {
        if !self.first {
            self.w.push(b',');
        }
        self.first = false;
    }

    /// A member's key, which needs no escaping.
    fn key(&mut self, k: &str) {
        self.comma();
        literal(self.w, k);
        self.w.push(b':');
    }

    fn end(self) {
        self.w.push(b'}');
    }
}

fn list<T>(w: &mut Vec<u8>, items: &[T], mut each: impl FnMut(&mut Vec<u8>, &T)) {
    w.push(b'[');
    for (i, x) in items.iter().enumerate() {
        if i > 0 {
            w.push(b',');
        }
        each(w, x);
    }
    w.push(b']');
}

/// A string that needs no escaping: a key or a name glTF defines.
fn literal(w: &mut Vec<u8>, s: &str) {
    debug_assert!(s.bytes().all(|c| c.is_ascii_graphic() && c != b'"' && c != b'\\'));
    w.push(b'"');
    w.extend_from_slice(s.as_bytes());
    w.push(b'"');
}

fn value(w: &mut Vec<u8>, v: &Value) -> Result<()> {
    serde_json::to_writer(w, v).context(ErrorKind::Invalid, "writing the glTF JSON")
}

fn string(w: &mut Vec<u8>, s: &str) -> Result<()> {
    serde_json::to_writer(w, s).context(ErrorKind::Invalid, "writing the glTF JSON")
}

fn uint(w: &mut Vec<u8>, n: u64) {
    // Writing to a `Vec` can't fail.
    let _ = write!(w, "{n}");
}

fn int(w: &mut Vec<u8>, n: i64) {
    let _ = write!(w, "{n}");
}

/// A float as `serde_json` writes one: `null` when it isn't finite.
fn float(w: &mut Vec<u8>, x: f64) {
    let _ = serde_json::to_writer(w, &x);
}