polysmelt 0.15.2

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
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
//! HTR (Hierarchical Translation-Rotation) motion capture as a glTF document: the segments as nodes, the motion as
//! one animation.
//!
//! The file is in `[`sections`]`, `#` starting a comment. `[Header]` holds `name value` lines: `NumSegments`,
//! `NumFrames`, `DataFrameRate` (frames per second), `EulerRotationOrder` (`ZYX` is Rz·Ry·Rx on column vectors: the
//! last letter's rotation applies first), `CalibrationUnits` (`mm`, `cm`, `m`, `in`, or a fraction of one such as
//! `1/10.000 m`), `RotationUnits` (`Degrees` or `Radians`), `GlobalAxisofGravity` (the up axis), `BoneLengthAxis`
//! and `ScaleFactor`. `[SegmentNames&Hierarchy]` lists each segment and its parent, `GLOBAL` for a root.
//! `[BasePosition]` gives each segment's base pose: a translation, three angles and its bone length. Then each
//! segment has a section of its own, named after it, with a row per frame: the frame number, a translation, three
//! angles and a scale factor. `[EndOfFile]` ends the file.
//!
//! Each segment becomes a node, in the order the hierarchy lists them, its base pose the rest pose: its translation
//! is the base translation and its rotation the base angles'. A segment without children ends in a node named
//! `<segment>_end`, its bone length along the bone length axis. A frame's row is relative to the base pose: the
//! segment's translation is the base's plus the row's, and its rotation is the base rotation followed by the row's
//! (`T(base + row)·R(base)·R(row)` on column vectors). A file without a hierarchy (the global form) has each segment
//! named in `[BasePosition]` as a root. A parent that isn't a segment makes a root too.
//!
//! The rows become one linear animation, a translation and a rotation channel per segment with rows, frame `n` at
//! `(n − first) / DataFrameRate` seconds, `first` being the lowest frame number in the file. A row with too few
//! numbers, or one that isn't a finite number, or whose frame number doesn't follow the row before it, is left out
//! and counted; so are rows in a section that names no segment. The scale factor column stretches the bone, which
//! glTF has no way to say, so rows whose factor isn't 1 are counted, and the factor is dropped. The header's
//! `ScaleFactor` is recorded and not applied. A missing or bad `DataFrameRate` is taken as 30, and noted.
//!
//! Lengths become meters, and a Z (or X) gravity axis is turned to glTF's Y up. Nothing is allocated from a count
//! the file gives: the segments are capped, and rows take room as they're read.

use std::collections::HashMap;

use serde_json::{Map, json};

use super::Input;
use super::scene::{self, Animation, Channel, Interp, Node, Path, Scene};
use crate::Result;
use crate::document::{Document, Keep};
use crate::num::f32_of;

/// Segments a file may have: far past any capture rig.
const MAX_SEGMENTS: usize = 32_768;
/// Frames per second when the file doesn't say.
const DEFAULT_RATE: f64 = 30.0;

/// A segment and its rows.
struct Segment {
    name: String,
    parent: Option<String>,
    /// Translation, then angles about X, Y and Z.
    base: [f64; 6],
    bone_length: f64,
    /// The kept rows' frame numbers, and each row's translation and angles.
    frames: Vec<f64>,
    rows: Vec<[f64; 6]>,
}

/// The section being read.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Section {
    Header,
    Hierarchy,
    Base,
    /// A segment's rows; `None` for a section naming no segment.
    Rows(Option<usize>),
    End,
}

/// Up to 40 characters of `s`, for an error message.
fn show(s: &str) -> String {
    s.chars().take(40).collect()
}

/// The length of a calibration unit in meters: `mm`, `cm`, `m`, `in`, `ft`, or a fraction of one (`1/10.000 m`).
fn unit_length(s: &str) -> Option<f64> {
    let s = s.trim();
    let (amount, unit) = if let Some((a, u)) = s.rsplit_once(char::is_whitespace) {
        (a.trim(), u)
    } else {
        let split = s.find(|c: char| c.is_ascii_alphabetic()).unwrap_or(s.len());
        (s[..split].trim(), &s[split..])
    };
    let base = match unit.to_ascii_lowercase().as_str() {
        "mm" | "millimeter" | "millimeters" | "millimetre" | "millimetres" => 0.001,
        "cm" | "centimeter" | "centimeters" | "centimetre" | "centimetres" => 0.01,
        "dm" | "decimeter" | "decimeters" => 0.1,
        "m" | "meter" | "meters" | "metre" | "metres" => 1.0,
        "in" | "inch" | "inches" => 0.0254,
        "ft" | "foot" | "feet" => 0.3048,
        _ => return None,
    };
    let factor = if amount.is_empty() {
        1.0
    } else if let Some((n, d)) = amount.split_once('/') {
        n.trim().parse::<f64>().ok()? / d.trim().parse::<f64>().ok()?
    } else {
        amount.parse::<f64>().ok()?
    };
    let m = factor * base;
    (m.is_finite() && m > 0.0).then_some(m)
}

/// The rotation that takes up axis `axis` (`Y`, `Z`, `-X`, …) to +Y; `None` if it isn't one.
fn up_rotation(axis: &str) -> Option<[f64; 4]> {
    use std::f64::consts::{FRAC_PI_2, PI};
    let a = axis.trim().to_ascii_uppercase();
    let q = match a.trim_start_matches('+') {
        "Y" => [0.0, 0.0, 0.0, 1.0],
        "-Y" => scene::about(0, PI),
        "Z" => scene::about(0, -FRAC_PI_2),
        "-Z" => scene::about(0, FRAC_PI_2),
        "X" => scene::about(2, FRAC_PI_2),
        "-X" => scene::about(2, -FRAC_PI_2),
        _ => return None,
    };
    Some(q)
}

/// The rotation of angles `[x, y, z]` (radians) in Euler order `order`: the first axis's rotation outermost.
fn euler(order: &[usize; 3], angles: [f64; 3]) -> [f64; 4] {
    let mut q = [0.0, 0.0, 0.0, 1.0];
    for &a in order {
        q = scene::quat_mul(q, scene::about(a, angles[a]));
    }
    q
}

/// Up to `n` finite numbers from `tokens`; fewer if one isn't.
fn numbers<'a>(tokens: impl Iterator<Item = &'a str>, n: usize) -> Vec<f64> {
    let mut out = Vec::with_capacity(n);
    for t in tokens.take(n) {
        match t.parse::<f64>() {
            Ok(v) if v.is_finite() => out.push(v),
            _ => break,
        }
    }
    out
}

/// Reads an HTR file.
pub(crate) fn load(input: Input<'_>, keep: Keep) -> Result<Document<'static>> {
    let bytes = input.read()?;
    let text = scene::text(&bytes);
    let text = text.strip_prefix('\u{feff}').unwrap_or(&text);
    let mut header: HashMap<String, String> = HashMap::new();
    let mut segments: Vec<Segment> = Vec::new();
    let mut index: HashMap<String, usize> = HashMap::new();
    let mut section: Option<Section> = None;
    let mut has_hierarchy = false;
    let (mut dropped, mut unknown_rows, mut scaled_rows) = (0usize, 0usize, 0usize);
    let mut add = |segments: &mut Vec<Segment>, name: &str, parent: Option<String>| -> Result<usize> {
        if let Some(&i) = index.get(name) {
            return Ok(i);
        }
        ensure!(
            segments.len() < MAX_SEGMENTS,
            "HTR has more than {MAX_SEGMENTS} segments"
        );
        index.insert(name.to_owned(), segments.len());
        segments.push(Segment {
            name: name.to_owned(),
            parent,
            base: [0.0; 6],
            bone_length: 0.0,
            frames: Vec::new(),
            rows: Vec::new(),
        });
        Ok(segments.len() - 1)
    };
    let mut lookup: HashMap<String, usize> = HashMap::new();
    for (k, raw) in text.lines().enumerate() {
        let no = k + 1;
        let line = raw.split('#').next().unwrap_or("").trim();
        if line.is_empty() {
            continue;
        }
        if let Some(name) = line.strip_prefix('[').and_then(|l| l.strip_suffix(']')) {
            let name = name.trim();
            let next = match name.to_ascii_lowercase().as_str() {
                "header" => Section::Header,
                "segmentnames&hierarchy" => {
                    has_hierarchy = true;
                    Section::Hierarchy
                }
                "baseposition" => Section::Base,
                "endoffile" => Section::End,
                _ => {
                    ensure!(
                        section.is_some(),
                        "not an HTR file: it starts with `[{}]`, not [Header]",
                        show(name)
                    );
                    if lookup.len() != segments.len() {
                        lookup = segments.iter().enumerate().map(|(i, s)| (s.name.clone(), i)).collect();
                    }
                    Section::Rows(lookup.get(name).copied())
                }
            };
            section = Some(next);
            if next == Section::End {
                break;
            }
            continue;
        }
        let Some(at) = section else {
            bail!("not an HTR file: it starts with `{}`, not [Header]", show(line));
        };
        let mut tokens = line.split_whitespace();
        let first = tokens.next().unwrap_or("");
        match at {
            Section::Header => {
                let value = line[first.len()..].trim();
                header.insert(first.to_ascii_lowercase(), value.to_owned());
            }
            Section::Hierarchy => {
                let parent = tokens
                    .next()
                    .filter(|p| !p.eq_ignore_ascii_case("GLOBAL") && *p != first);
                add(&mut segments, first, parent.map(str::to_owned))?;
            }
            Section::Base => {
                let v = numbers(tokens, 7);
                ensure!(
                    v.len() == 7,
                    "HTR line {no}: segment `{}`'s base position needs seven numbers",
                    show(first)
                );
                let i = add(&mut segments, first, None)?;
                segments[i].base.copy_from_slice(&v[..6]);
                segments[i].bone_length = v[6];
            }
            Section::Rows(None) => unknown_rows += 1,
            Section::Rows(Some(i)) => {
                let v = numbers(line.split_whitespace(), 8);
                let s = &mut segments[i];
                if v.len() < 7 || s.frames.last().is_some_and(|&f| v[0] <= f) {
                    dropped += 1;
                    continue;
                }
                if v.len() > 7 && (v[7] - 1.0).abs() > 1e-9 {
                    scaled_rows += 1;
                }
                s.frames.push(v[0]);
                s.rows.push([v[1], v[2], v[3], v[4], v[5], v[6]]);
            }
            Section::End => {}
        }
    }
    let Some(_) = section else {
        bail!("not an HTR file: it's empty");
    };
    ensure!(!segments.is_empty(), "HTR has no segments");

    let get = |k: &str| header.get(&k.to_ascii_lowercase()).map(String::as_str);
    let order_text = get("EulerRotationOrder").unwrap_or("ZYX");
    let order: Vec<usize> = order_text
        .bytes()
        .filter_map(|c| match c.to_ascii_uppercase() {
            b'X' => Some(0),
            b'Y' => Some(1),
            b'Z' => Some(2),
            _ => None,
        })
        .collect();
    let Ok(order) = <[usize; 3]>::try_from(order) else {
        bail!(
            "HTR EulerRotationOrder `{}` isn't three of X, Y and Z",
            show(order_text)
        );
    };
    ensure!(
        order_text.trim().len() == 3,
        "HTR EulerRotationOrder `{}` isn't three of X, Y and Z",
        show(order_text)
    );
    let radians = match get("RotationUnits").map(str::to_ascii_lowercase).as_deref() {
        None => false,
        Some(u) if u.starts_with("deg") => false,
        Some(u) if u.starts_with("rad") => true,
        Some(u) => bail!("HTR RotationUnits `{}` isn't Degrees or Radians", show(u)),
    };
    let angle = if radians { 1.0 } else { std::f64::consts::PI / 180.0 };
    let rate = get("DataFrameRate")
        .and_then(|r| r.parse::<f64>().ok())
        .filter(|r| r.is_finite() && *r > 0.0);
    let frame_time = 1.0 / rate.unwrap_or(DEFAULT_RATE);
    let units = get("CalibrationUnits");
    let meters = units.and_then(unit_length);
    let gravity = get("GlobalAxisofGravity").unwrap_or("Y");
    let up = up_rotation(gravity);
    let length_axis_text = get("BoneLengthAxis").unwrap_or("Y");
    let length_axis = up_rotation(length_axis_text).map(|_| {
        let a = length_axis_text.trim().to_ascii_uppercase();
        let sign = if a.starts_with('-') { -1.0 } else { 1.0 };
        let mut v = [0.0; 3];
        let k = match a.trim_start_matches(['+', '-']) {
            "X" => 0,
            "Z" => 2,
            _ => 1,
        };
        v[k] = sign;
        v
    });

    let mut scene = Scene::default();
    let base_rotation = |s: &Segment| euler(&order, [s.base[3] * angle, s.base[4] * angle, s.base[5] * angle]);
    let mut has_children = vec![false; segments.len()];
    let parents: Vec<Option<usize>> = segments
        .iter()
        .map(|s| s.parent.as_ref().and_then(|p| index.get(p).copied()))
        .collect();
    for p in parents.iter().flatten() {
        has_children[*p] = true;
    }
    let unknown_parents = segments
        .iter()
        .zip(&parents)
        .filter(|(s, p)| s.parent.is_some() && p.is_none())
        .count();
    for (s, &parent) in segments.iter().zip(&parents) {
        let mut node = Node::named(s.name.clone(), parent);
        node.translation = [s.base[0], s.base[1], s.base[2]].map(f32_of);
        node.rotation = scene::normalize(base_rotation(s));
        scene.add(node);
    }
    for (i, s) in segments.iter().enumerate() {
        if !has_children[i] {
            let mut end = Node::named(format!("{}_end", s.name), Some(i));
            let axis = length_axis.unwrap_or([0.0, 1.0, 0.0]);
            end.translation = axis.map(|c| f32_of(c * s.bone_length));
            scene.add(end);
        }
    }
    let first = segments
        .iter()
        .filter_map(|s| s.frames.first().copied())
        .fold(f64::INFINITY, f64::min);
    let mut channels = Vec::new();
    for (i, s) in segments.iter().enumerate() {
        if s.frames.is_empty() {
            continue;
        }
        let times: Vec<f32> = s.frames.iter().map(|&f| f32_of((f - first) * frame_time)).collect();
        let qb = base_rotation(s);
        let mut translations = Vec::with_capacity(s.rows.len() * 3);
        let mut rotations = Vec::with_capacity(s.rows.len() * 4);
        for r in &s.rows {
            translations.extend([s.base[0] + r[0], s.base[1] + r[1], s.base[2] + r[2]].map(f32_of));
            let qf = euler(&order, [r[3] * angle, r[4] * angle, r[5] * angle]);
            rotations.extend(scene::normalize(scene::quat_mul(qb, qf)));
        }
        channels.push(Channel {
            node: i,
            path: Path::Translation,
            times: times.clone(),
            values: translations,
            interp: Interp::Linear,
        });
        channels.push(Channel {
            node: i,
            path: Path::Rotation,
            times,
            values: rotations,
            interp: Interp::Linear,
        });
    }
    let frames = segments.iter().map(|s| s.frames.len()).max().unwrap_or(0);
    if keep.animations && !channels.is_empty() {
        scene.animations.push(Animation {
            name: "motion".into(),
            channels,
            extras: None,
        });
    }

    let mut source = Map::new();
    source.insert("format".into(), json!("HTR"));
    source.insert("upAxis".into(), json!(gravity));
    for (key, name) in [
        ("FileVersion", "fileVersion"),
        ("DataType", "dataType"),
        ("CalibrationUnits", "units"),
        ("RotationUnits", "rotationUnits"),
        ("EulerRotationOrder", "eulerRotationOrder"),
        ("BoneLengthAxis", "boneLengthAxis"),
    ] {
        if let Some(v) = get(key) {
            source.insert(name.into(), json!(v));
        }
    }
    for (key, name) in [("ScaleFactor", "scaleFactor"), ("NumFrames", "frames")] {
        if let Some(v) = get(key).and_then(|v| v.parse::<f64>().ok()).filter(|v| v.is_finite()) {
            source.insert(name.into(), json!(v));
        }
    }
    source.insert("frameTime".into(), json!(frame_time));
    let facts = &mut scene.facts;
    facts.insert("segments".into(), json!(segments.len()));
    facts.insert("frames".into(), json!(frames));
    facts.insert("frame_time".into(), json!(frame_time));
    if rate.is_none() {
        facts.insert("frame_time_guessed".into(), json!(true));
    }
    if dropped > 0 {
        facts.insert("dropped_rows".into(), json!(dropped));
    }
    if unknown_rows > 0 {
        facts.insert("unknown_segment_rows".into(), json!(unknown_rows));
    }
    if scaled_rows > 0 {
        facts.insert("scale_factors_dropped".into(), json!(scaled_rows));
    }
    if unknown_parents > 0 {
        facts.insert("unknown_parents".into(), json!(unknown_parents));
    }
    if meters.is_none() {
        facts.insert("units_unknown".into(), json!(true));
    }
    if up.is_none() {
        facts.insert("up_axis_unknown".into(), json!(true));
    }
    if length_axis.is_none() {
        facts.insert("bone_length_axis_unknown".into(), json!(true));
    }
    if !has_hierarchy {
        facts.insert("global".into(), json!(true));
    }
    scene.source = source;
    scene.scale(f32_of(meters.unwrap_or(1.0)));
    // Y up needs no turn; every other axis's quaternion has w below 1.
    if let Some(q) = up.filter(|q| q[3] < 1.0) {
        scene.rotate_roots(scene::normalize(q));
    }
    scene.finish(keep)
}

#[cfg(test)]
mod tests {
    use super::*;

    const HTR: &str = "# a two-segment test
[Header]
FileType htr
DataType HTRS
FileVersion 1
NumSegments 2
NumFrames 2
DataFrameRate 10
EulerRotationOrder ZYX
CalibrationUnits mm
RotationUnits Degrees
GlobalAxisofGravity Y
BoneLengthAxis Y
ScaleFactor 1.0
[SegmentNames&Hierarchy]
hip GLOBAL
knee hip
[BasePosition]
hip 0 1000 0 0 0 0 400
knee 0 -400 0 0 0 90 300
[hip]
1 0 0 0 0 0 0 1
2 100 0 0 0 90 0 1
[knee]
1 0 0 0 0 0 0 1
2 0 0 0 90 0 0 1.5
[EndOfFile]
";

    fn read(text: &str) -> Result<Document<'static>> {
        load(Input::memory(text.as_bytes(), "t.htr"), Keep::ALL)
    }

    fn error(text: &str) -> String {
        match read(text) {
            Ok(_) => panic!("read without error"),
            Err(e) => e.to_string(),
        }
    }

    fn near(a: &[f32], b: &[f32]) -> bool {
        a.len() == b.len() && a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1e-5)
    }

    #[test]
    fn two_segments() {
        let doc = read(HTR).unwrap();
        let nodes = doc.json["nodes"].as_array().unwrap();
        let names: Vec<_> = nodes.iter().map(|n| n["name"].as_str().unwrap()).collect();
        assert_eq!(names, ["hip", "knee", "knee_end"]);
        assert_eq!(doc.facts["segments"], json!(2));
        assert_eq!(doc.facts["frames"], json!(2));
        assert_eq!(doc.facts["scale_factors_dropped"], json!(1));
        let rig = doc.rig().unwrap();
        assert!(near(&rig.nodes[0].rest.translation, &[0.0, 1.0, 0.0]));
        let s = std::f32::consts::FRAC_1_SQRT_2;
        assert!(near(&rig.nodes[1].rest.rotation, &[0.0, 0.0, s, s]));
        assert!(near(&rig.nodes[2].rest.translation, &[0.0, 0.3, 0.0]));
        let pose = rig.pose(Some(0), 0.1);
        assert!(near(&pose[0].translation, &[0.1, 1.0, 0.0]));
        assert!(near(&pose[0].rotation, &[0.0, s, 0.0, s]));
        // knee: the base's 90° about Z, then the row's 90° about X.
        let want = scene::normalize(scene::quat_mul(
            scene::about(2, std::f64::consts::FRAC_PI_2),
            scene::about(0, std::f64::consts::FRAC_PI_2),
        ));
        assert!(near(&pose[1].rotation, &want), "{:?}", pose[1].rotation);
        assert_eq!(rig.animations[0].channels[0].times, [0.0, 0.1]);
    }

    #[test]
    fn euler_order() {
        // ZYX: Rz·Ry·Rx, so X turns first. Rx(90) then Rz(90) takes Y to Z, then Z stays.
        let q = euler(
            &[2, 1, 0],
            [std::f64::consts::FRAC_PI_2, 0.0, std::f64::consts::FRAC_PI_2],
        );
        let v = scene::rotate(q, [0.0, 1.0, 0.0]);
        assert!(near(&v.map(f32_of), &[0.0, 0.0, 1.0]));
        let v = scene::rotate(q, [1.0, 0.0, 0.0]);
        assert!(near(&v.map(f32_of), &[0.0, 1.0, 0.0]));
    }

    #[test]
    fn z_up_and_units() {
        let text = HTR
            .replace("GlobalAxisofGravity Y", "GlobalAxisofGravity Z")
            .replace("CalibrationUnits mm", "CalibrationUnits 1/10.000 m");
        let rig = read(&text).unwrap().rig().unwrap();
        // 1000 tenths of a meter up Z is 100 m up Y.
        assert!(near(&rig.nodes[0].rest.translation, &[0.0, 0.0, -100.0]));
        assert_eq!(unit_length("mm"), Some(0.001));
        assert_eq!(unit_length("1/10.000 m"), Some(0.1));
        assert!((unit_length("2.54cm").unwrap() - 0.0254).abs() < 1e-12);
        assert_eq!(unit_length("furlongs"), None);
        assert_eq!(unit_length("1/0 m"), None);
    }

    #[test]
    fn bad_rows_are_dropped() {
        let text = HTR
            .replace(
                "2 0 0 0 90 0 0 1.5",
                "2 0 0 x\n2 0 0 0 0 0 0\n1 0 0 0 0 0 0\n3 0 0 0 0 0 0",
            )
            .replace("[EndOfFile]", "[nobody]\n1 2 3\n[EndOfFile]\njunk");
        let doc = read(&text).unwrap();
        assert_eq!(doc.facts["dropped_rows"], json!(2));
        assert_eq!(doc.facts["unknown_segment_rows"], json!(1));
        let rig = doc.rig().unwrap();
        assert_eq!(rig.animations[0].channels[2].times, [0.0, 0.1, 0.2]);
    }

    #[test]
    fn global_form() {
        let text =
            "[Header]\nDataFrameRate 0\n[BasePosition]\na 0 0 0 0 0 0 1\nb 1 0 0 0 0 0 1\n[a]\n0 0 0 0 0 0 0 1\n";
        let doc = read(text).unwrap();
        assert_eq!(doc.facts["global"], json!(true));
        assert_eq!(doc.facts["frame_time_guessed"], json!(true));
        assert_eq!(doc.json["scenes"][0]["nodes"], json!([0, 1]));
    }

    #[test]
    fn bad_files_are_errors() {
        let cases = [
            ("", "empty"),
            ("hello\n", "not an HTR file"),
            ("[hip]\n", "not an HTR file"),
            ("[Header]\n", "no segments"),
            ("[Header]\n[BasePosition]\na 0 0\n", "seven numbers"),
            (
                "[Header]\nEulerRotationOrder ZY\n[BasePosition]\na 0 0 0 0 0 0 1\n",
                "EulerRotationOrder",
            ),
            (
                "[Header]\nEulerRotationOrder ZYXQ\n[BasePosition]\na 0 0 0 0 0 0 1\n",
                "EulerRotationOrder",
            ),
            (
                "[Header]\nRotationUnits grads\n[BasePosition]\na 0 0 0 0 0 0 1\n",
                "RotationUnits",
            ),
        ];
        for (text, want) in cases {
            let e = error(text);
            assert!(e.contains(want), "{text:?}: {e}");
        }
        let mut many = String::new();
        for i in 0..=MAX_SEGMENTS {
            many.push('s');
            many.push_str(&i.to_string());
            many.push_str(" GLOBAL\n");
        }
        assert!(error(&format!("[Header]\n[SegmentNames&Hierarchy]\n{many}")).contains("more than"));
    }

    #[test]
    fn loops_and_unknown_parents() {
        let text = HTR
            .replace("hip GLOBAL", "hip knee")
            .replace("knee hip", "knee hip\nfoot ankle");
        let doc = read(&text).unwrap();
        assert_eq!(doc.facts["unknown_parents"], json!(1));
        assert!(doc.rig().is_ok());
    }

    #[test]
    fn truncated_files_never_panic() {
        for cut in 0..HTR.len() {
            let _ = read(&HTR[..cut]);
        }
    }
}