Skip to main content

animsmith_core/
model.rs

1//! The loader-facing layer: clips, tracks, and the skeleton before metric
2//! resampling or repair. The glTF loader preserves authored animation
3//! values; the FBX loader normalizes scene coordinates and bakes takes to
4//! linear TRS tracks. Mechanical checks (NaN, quaternion flips, key
5//! density, …) read this layer; semantic checks read the sampled layer
6//! built from it (see [`crate::sample`]).
7
8use glam::{Mat3, Mat4, Quat, Vec3};
9use serde::{Deserialize, Serialize};
10use std::collections::{BTreeMap, BTreeSet};
11
12/// Stable machine-readable reason a positive-uniform affine linear part failed
13/// classification.
14///
15/// Core consumers map these typed facts into their own domain-specific
16/// diagnostics rather than sharing a broad operation error enum.
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18#[non_exhaustive]
19pub enum AffineDomainViolation {
20    /// The linear part's axis lengths are not equal (non-uniform scale).
21    NonUniformScale,
22    /// The linear part's axes are not mutually orthogonal (shear).
23    Sheared,
24    /// The linear part has a negative determinant (reflection).
25    Reflected,
26    /// The linear part is singular or near-singular.
27    Singular,
28    /// The linear part contains a non-finite component.
29    NonFinite,
30}
31
32/// Tolerances supplied by one caller of
33/// [`classify_positive_uniform_affine`].
34///
35/// The classifier deliberately owns no global policy: Appendix D scale
36/// planning uses its versioned `f64` policy, while skinned bind-pose
37/// canonicalization retains its established `1e-4` acceptance band.
38#[derive(Debug, Clone, Copy, PartialEq)]
39pub(crate) struct PositiveUniformAffineTolerance {
40    pub(crate) equal_axis: f64,
41    pub(crate) relative_orthogonality: f64,
42    pub(crate) singular_determinant_relative: f64,
43}
44
45/// Policy-neutral geometric facts for one affine linear part.
46///
47/// Every derived operation widens the source `f32` columns to `f64` first.
48/// Callers deliberately apply their own tolerance and precedence policies to
49/// this one fact record: strict positive-uniform operations reject a domain
50/// violation, while measurement retains finite descriptive evidence.
51#[derive(Debug, Clone, Copy, PartialEq)]
52pub(crate) struct AffineGeometryFacts {
53    pub(crate) axis_lengths: [f64; 3],
54    pub(crate) mean_axis_length: f64,
55    pub(crate) determinant: f64,
56    pub(crate) axis_length_product: f64,
57    /// Widened dot products in XY, XZ, YZ order.
58    pub(crate) cross_axis_dots: [f64; 3],
59}
60
61impl AffineGeometryFacts {
62    /// Derive the complete finite fact record, or reject it atomically.
63    ///
64    /// The scalar triple product's operation order is part of the established
65    /// Appendix D classifier behavior and must not be replaced with an f32
66    /// determinant or a differently associated f64 expansion.
67    pub(crate) fn from_linear(linear: Mat3) -> Result<Self, AffineDomainViolation> {
68        if !linear.is_finite() {
69            return Err(AffineDomainViolation::NonFinite);
70        }
71        let columns = [
72            linear.x_axis.as_dvec3(),
73            linear.y_axis.as_dvec3(),
74            linear.z_axis.as_dvec3(),
75        ];
76        let axis_lengths = affine_axis_lengths(linear);
77        let mean_axis_length = average_affine_axis_length(axis_lengths);
78        let determinant = columns[2].dot(columns[0].cross(columns[1]));
79        let axis_length_product = axis_lengths[0] * axis_lengths[1] * axis_lengths[2];
80        let cross_axis_dots = [
81            columns[0].dot(columns[1]),
82            columns[0].dot(columns[2]),
83            columns[1].dot(columns[2]),
84        ];
85        if axis_lengths.iter().any(|value| !value.is_finite())
86            || !mean_axis_length.is_finite()
87            || !determinant.is_finite()
88            || !axis_length_product.is_finite()
89            || cross_axis_dots.iter().any(|value| !value.is_finite())
90        {
91            return Err(AffineDomainViolation::NonFinite);
92        }
93        Ok(Self {
94            axis_lengths,
95            mean_axis_length,
96            determinant,
97            axis_length_product,
98            cross_axis_dots,
99        })
100    }
101
102    /// Whether every axis lies within a symmetric mean-relative band.
103    ///
104    /// Comparing every value to the mean removes the privileged-X behavior
105    /// of pairwise-from-X tests. The longer-operand base keeps the predicate
106    /// symmetric around the mean, and `<=` makes the boundary inclusive.
107    pub(crate) fn has_equal_axis_lengths(self, relative_tolerance: f64) -> bool {
108        values_equal_to_mean(
109            &self.axis_lengths,
110            self.mean_axis_length,
111            relative_tolerance,
112        )
113    }
114}
115
116/// Whether every finite value lies within a symmetric relative band around
117/// `mean`, using the longer operand as the relative base.
118pub(crate) fn values_equal_to_mean(values: &[f64], mean: f64, relative_tolerance: f64) -> bool {
119    values
120        .iter()
121        .all(|&value| (value - mean).abs() <= relative_tolerance * mean.abs().max(value.abs()))
122}
123
124/// Classify an affine linear part as an orientation-preserving positive
125/// uniform scale and return its common factor.
126///
127/// Inputs widen to `f64` before every derived calculation. This preserves the
128/// Appendix D scale classifier's boundary behaviour; callers choose the
129/// tolerance policy appropriate to their separate contract.
130pub(crate) fn classify_positive_uniform_affine(
131    linear: Mat3,
132    tolerance: PositiveUniformAffineTolerance,
133) -> Result<f64, AffineDomainViolation> {
134    let facts = AffineGeometryFacts::from_linear(linear)?;
135    if facts.mean_axis_length <= 0.0 {
136        return Err(AffineDomainViolation::Singular);
137    }
138
139    // Check singularity before the shape facts. A degenerate basis that is
140    // also non-uniform or sheared is still singular, which keeps the
141    // independently named rejection classes deterministic.
142    // Expand the scalar triple product from the widened columns. Calling
143    // `Mat3::determinant` here would perform the derived arithmetic in f32
144    // before widening and moves the singular boundary.
145    if facts.determinant.abs()
146        <= tolerance.singular_determinant_relative * facts.axis_length_product
147    {
148        return Err(AffineDomainViolation::Singular);
149    }
150    // This is a relative band with no unit floor: a floor would become an
151    // absolute tolerance for sub-unit transforms. The longer-operand base
152    // keeps the comparison symmetric, and `>` makes the boundary inclusive.
153    if !facts.has_equal_axis_lengths(tolerance.equal_axis) {
154        return Err(AffineDomainViolation::NonUniformScale);
155    }
156
157    // Scale the dot-product band by the square of the common factor. As with
158    // the axis band, equality is accepted and only a value beyond it rejects.
159    let orthogonality_tolerance =
160        tolerance.relative_orthogonality * facts.mean_axis_length * facts.mean_axis_length;
161    if facts
162        .cross_axis_dots
163        .iter()
164        .any(|dot| dot.abs() > orthogonality_tolerance)
165    {
166        return Err(AffineDomainViolation::Sheared);
167    }
168    if facts.determinant < 0.0 {
169        return Err(AffineDomainViolation::Reflected);
170    }
171    Ok(facts.mean_axis_length)
172}
173
174/// The three column lengths of a linear part, widened to `f64` first.
175///
176/// The positive-uniform classifier and the scale proof's observed-factor
177/// witness both use this helper so that their factor is one shared quantity.
178pub(crate) fn affine_axis_lengths(linear: Mat3) -> [f64; 3] {
179    [
180        linear.x_axis.as_dvec3().length(),
181        linear.y_axis.as_dvec3().length(),
182        linear.z_axis.as_dvec3().length(),
183    ]
184}
185
186/// The arithmetic-mean common factor represented by three affine axis
187/// lengths.
188///
189/// The finite widened inputs are summed in ascending order so this shared
190/// factor does not depend on an affine matrix's authored column order.
191pub(crate) fn average_affine_axis_length(lengths: [f64; 3]) -> f64 {
192    let mut ascending = lengths;
193    ascending.sort_by(f64::total_cmp);
194    (ascending[0] + ascending[1] + ascending[2]) / 3.0
195}
196
197#[cfg(test)]
198pub(crate) mod affine_test_fixtures {
199    use super::{Mat3, Vec3};
200
201    /// A finite diagonal basis intentionally between the two callers' equal
202    /// axis bands: Appendix D rejects it, while skinned canonicalization's
203    /// established `1e-4` policy accepts it.
204    pub(crate) fn tolerance_divergence_basis() -> Mat3 {
205        Mat3::from_diagonal(Vec3::new(1.0, 1.000_05, 1.0))
206    }
207
208    /// A nearly orthogonal basis whose only non-zero cross-axis dot product
209    /// lies between the two callers' orthogonality bands.
210    pub(crate) fn orthogonality_tolerance_divergence_basis() -> Mat3 {
211        Mat3::from_cols(Vec3::X, Vec3::new(5.0e-5, 1.0, 0.0), Vec3::Z)
212    }
213
214    /// All signed column orders of the exact Appendix D v6 mean fixture.
215    ///
216    /// Odd permutations negate their first column, preserving orientation
217    /// without changing any axis length.
218    pub(crate) fn appendix_d_v6_mean_permutations() -> [Mat3; 6] {
219        let columns = [
220            Vec3::new(
221                f32::from_bits(0x3f0e_8cbb),
222                f32::from_bits(0x3f26_fbbe),
223                f32::from_bits(0x3f21_9bc7),
224            ),
225            Vec3::new(
226                f32::from_bits(0x3d9c_b415),
227                f32::from_bits(0x3e92_d82b),
228                f32::from_bits(0x3f82_e85d),
229            ),
230            Vec3::new(
231                f32::from_bits(0x3f14_5226),
232                f32::from_bits(0x3e9e_e50d),
233                f32::from_bits(0x3f56_817c),
234            ),
235        ];
236        [
237            Mat3::from_cols(columns[0], columns[1], columns[2]),
238            Mat3::from_cols(-columns[0], columns[2], columns[1]),
239            Mat3::from_cols(-columns[1], columns[0], columns[2]),
240            Mat3::from_cols(columns[1], columns[2], columns[0]),
241            Mat3::from_cols(columns[2], columns[0], columns[1]),
242            Mat3::from_cols(-columns[2], columns[1], columns[0]),
243        ]
244    }
245}
246
247/// Index into [`Skeleton::bones`].
248pub type BoneId = usize;
249
250/// Node-local TRS transform.
251#[derive(Debug, Clone, Copy, PartialEq)]
252pub struct Transform {
253    /// Translation in scene units.
254    pub translation: Vec3,
255    /// Orientation relative to the parent node.
256    pub rotation: Quat,
257    /// Non-uniform local scale.
258    pub scale: Vec3,
259}
260
261impl Transform {
262    /// The identity transform: zero translation, identity rotation, and
263    /// unit scale.
264    pub const IDENTITY: Self = Self {
265        translation: Vec3::ZERO,
266        rotation: Quat::IDENTITY,
267        scale: Vec3::ONE,
268    };
269
270    /// Convert this TRS transform to a matrix using glam's
271    /// scale-rotation-translation order.
272    pub fn to_mat4(&self) -> Mat4 {
273        Mat4::from_scale_rotation_translation(self.scale, self.rotation, self.translation)
274    }
275}
276
277impl Default for Transform {
278    fn default() -> Self {
279        Self::IDENTITY
280    }
281}
282
283/// One skeleton node/bone in parent-before-child order.
284#[derive(Debug, Clone)]
285pub struct Bone {
286    /// Bone/node name as authored or normalized by the loader.
287    pub name: String,
288    /// Parent bone index; `None` means this is a root bone.
289    pub parent: Option<BoneId>,
290    /// Rest pose, node-local. Whether this or the inverse-bind-derived
291    /// rest is authoritative is a `bind-pose` check concern.
292    pub rest: Transform,
293    /// Inverse bind matrix from a skin, when one references this bone.
294    pub inverse_bind: Option<Mat4>,
295}
296
297/// Bones in topological order: a bone's parent always precedes it.
298/// Loaders are responsible for establishing this invariant.
299#[derive(Debug, Clone, Default)]
300pub struct Skeleton {
301    /// Bones in topological order.
302    pub bones: Vec<Bone>,
303}
304
305impl Skeleton {
306    /// Name of the bone at `id`.
307    ///
308    /// # Panics
309    ///
310    /// Panics if `id` is not a valid index into [`Skeleton::bones`].
311    pub fn bone_name(&self, id: BoneId) -> &str {
312        &self.bones[id].name
313    }
314}
315
316/// Structural failure composing [`Skeleton`] rest-local transforms into
317/// world matrices, returned by [`world_rest_matrices`].
318///
319/// This is deliberately generic over the eventual caller-facing error: every
320/// caller of [`world_rest_matrices`] maps one of these two structural facts
321/// into its own typed error variant rather than sharing an error enum across
322/// module boundaries.
323#[derive(Debug, Clone, Copy, PartialEq, Eq)]
324pub(crate) enum WorldMatrixError {
325    /// The node's local or accumulated world transform has a non-finite
326    /// component.
327    NonFiniteTransform {
328        /// The node with the non-finite transform.
329        node: BoneId,
330    },
331    /// The node's parent is not earlier in [`Skeleton::bones`], so the
332    /// skeleton is not in the required parent-before-child order.
333    InvalidParent {
334        /// The node with an invalid parent.
335        node: BoneId,
336        /// The invalid parent index.
337        parent: BoneId,
338    },
339}
340
341/// Compose every [`Bone::rest`] local transform in `skeleton` into a
342/// parent-before-child world matrix, shared by every module that needs plain
343/// rest-world FK (skin bind-pose canonicalization, static mesh baking, and
344/// scale planning/proof).
345///
346/// `skeleton.bones` order is trusted as parent-before-child, matching
347/// [`Skeleton`]'s documented invariant; a parent index that is not strictly
348/// less than its child's is reported as [`WorldMatrixError::InvalidParent`]
349/// rather than assumed.
350pub(crate) fn world_rest_matrices(skeleton: &Skeleton) -> Result<Vec<Mat4>, WorldMatrixError> {
351    let mut worlds = Vec::with_capacity(skeleton.bones.len());
352    for (node, bone) in skeleton.bones.iter().enumerate() {
353        let local = bone.rest.to_mat4();
354        if !mat4_is_finite(local) {
355            return Err(WorldMatrixError::NonFiniteTransform { node });
356        }
357        let world = match bone.parent {
358            Some(parent) if parent < node => worlds[parent] * local,
359            Some(parent) => return Err(WorldMatrixError::InvalidParent { node, parent }),
360            None => local,
361        };
362        if !mat4_is_finite(world) {
363            return Err(WorldMatrixError::NonFiniteTransform { node });
364        }
365        worlds.push(world);
366    }
367    Ok(worlds)
368}
369
370/// Compose rest-world matrices for a partial-evidence consumer.
371///
372/// Unlike [`world_rest_matrices`], this deliberately preserves a slot for
373/// every bone and makes only the malformed chain unavailable. Measurement
374/// uses that behaviour to retain finite evidence from unrelated roots.
375pub(crate) fn tolerant_world_rest_matrices(skeleton: &Skeleton) -> Vec<Option<Mat4>> {
376    let mut worlds = Vec::with_capacity(skeleton.bones.len());
377    for bone in &skeleton.bones {
378        let local = bone.rest.to_mat4();
379        let world = match bone.parent {
380            Some(parent) => worlds
381                .get(parent)
382                .copied()
383                .flatten()
384                .map(|parent_world| parent_world * local),
385            None => Some(local),
386        }
387        .filter(|matrix| mat4_is_finite(*matrix));
388        worlds.push(world);
389    }
390    worlds
391}
392
393pub(crate) fn mat4_is_finite(matrix: Mat4) -> bool {
394    matrix.to_cols_array().into_iter().all(f32::is_finite)
395}
396
397/// Animated property targeted by a [`Track`].
398#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
399#[serde(rename_all = "snake_case")]
400pub enum Property {
401    /// Local translation channel.
402    Translation,
403    /// Local rotation channel.
404    Rotation,
405    /// Local scale channel.
406    Scale,
407}
408
409impl Property {
410    /// Stable snake-case name used in diagnostics and serialized
411    /// metadata.
412    pub fn as_str(self) -> &'static str {
413        match self {
414            Property::Translation => "translation",
415            Property::Rotation => "rotation",
416            Property::Scale => "scale",
417        }
418    }
419}
420
421/// Interpolation mode for a [`Track`].
422#[derive(Debug, Clone, Copy, PartialEq, Eq)]
423pub enum Interpolation {
424    /// Linear interpolation between key values.
425    Linear,
426    /// Hold the previous key until the next key.
427    Step,
428    /// glTF cubic spline: `values` holds `[in-tangent, value, out-tangent]`
429    /// triplets per keyframe. Use [`Track::value_index`] to address the
430    /// value elements.
431    CubicSpline,
432}
433
434/// Storage for a track's key values.
435#[derive(Debug, Clone)]
436pub enum TrackValues {
437    /// Translation or scale values.
438    Vec3s(Vec<Vec3>),
439    /// Rotation values.
440    Quats(Vec<Quat>),
441}
442
443impl TrackValues {
444    /// Number of stored values, including tangents for cubic-spline
445    /// tracks.
446    pub fn len(&self) -> usize {
447        match self {
448            TrackValues::Vec3s(v) => v.len(),
449            TrackValues::Quats(v) => v.len(),
450        }
451    }
452
453    /// Whether there are no stored values.
454    pub fn is_empty(&self) -> bool {
455        self.len() == 0
456    }
457}
458
459/// One animated property of one bone.
460#[derive(Debug, Clone)]
461pub struct Track {
462    /// Bone index targeted by this track.
463    pub bone: BoneId,
464    /// Property animated on the target bone.
465    pub property: Property,
466    /// Interpolation mode used between keys.
467    pub interpolation: Interpolation,
468    /// Keyframe times in seconds. Same length as the keyframe count
469    /// (tangent elements in cubic tracks do not add times).
470    pub times: Vec<f32>,
471    /// Key values, with cubic-spline tracks storing tangent triplets.
472    pub values: TrackValues,
473}
474
475impl Track {
476    /// Number of keyframes.
477    pub fn key_count(&self) -> usize {
478        self.times.len()
479    }
480
481    /// Index into `values` of keyframe `k`'s value element (skips
482    /// tangents for cubic tracks).
483    pub fn value_index(&self, k: usize) -> usize {
484        match self.interpolation {
485            Interpolation::CubicSpline => 3 * k + 1,
486            _ => k,
487        }
488    }
489
490    /// Keyframe `k`'s value, for Vec3 tracks.
491    pub fn key_vec3(&self, k: usize) -> Option<Vec3> {
492        match &self.values {
493            TrackValues::Vec3s(v) => v.get(self.value_index(k)).copied(),
494            TrackValues::Quats(_) => None,
495        }
496    }
497
498    /// Keyframe `k`'s value, for rotation tracks.
499    pub fn key_quat(&self, k: usize) -> Option<Quat> {
500        match &self.values {
501            TrackValues::Quats(v) => v.get(self.value_index(k)).copied(),
502            TrackValues::Vec3s(_) => None,
503        }
504    }
505
506    /// First key time, or `0.0` for an empty track.
507    pub fn start_time(&self) -> f32 {
508        self.times.first().copied().unwrap_or(0.0)
509    }
510
511    /// Last key time, or `0.0` for an empty track.
512    pub fn end_time(&self) -> f32 {
513        self.times.last().copied().unwrap_or(0.0)
514    }
515}
516
517/// One animation clip targeting the document skeleton.
518#[derive(Debug, Clone)]
519pub struct Clip {
520    /// Clip name, used as the key in measurement maps and config
521    /// expectations.
522    pub name: String,
523    /// Clip length in seconds (max sampler end time across tracks).
524    pub duration_s: f64,
525    /// Animated tracks belonging to this clip.
526    pub tracks: Vec<Track>,
527}
528
529/// Loader-provided provenance for a [`Document`].
530#[derive(Debug, Clone, Default)]
531pub struct SourceInfo {
532    /// Source path, when the loader was given one.
533    pub path: Option<String>,
534    /// Source format label such as `"glb"` or `"fbx"`.
535    pub format: Option<String>,
536}
537
538/// A loaded file: one skeleton, any number of clips targeting it, and
539/// the scene assets (meshes, materials, and textures) that rode in alongside
540/// them.
541/// `assets` is default-empty: the check catalog judges animation and
542/// ignores it, but the load/write round-trip carries it so `transform`
543/// and `convert` preserve geometry instead of silently dropping it.
544#[derive(Debug, Clone, Default)]
545pub struct Document {
546    /// Skeleton shared by every clip.
547    pub skeleton: Skeleton,
548    /// Animation clips targeting [`Document::skeleton`].
549    pub clips: Vec<Clip>,
550    /// Meshes, materials, and textures carried by the loaded scene.
551    pub assets: SceneAssets,
552    /// Optional source provenance.
553    pub source: SourceInfo,
554}
555
556/// A structural invariant violated by [`validate_document_shape`].
557///
558/// Validation is a snapshot, not a durable guarantee: [`Document`] and its
559/// nested fields are publicly mutable. Strict operations that rely on this
560/// full shape must validate again at each public boundary.
561#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
562#[non_exhaustive]
563pub enum DocumentShapeError {
564    /// A bone's local rest transform or its composed rest-world transform is
565    /// non-finite.
566    #[error("node {node} has a non-finite rest transform")]
567    NonFiniteSkeletonRest {
568        /// The affected skeleton node.
569        node: BoneId,
570    },
571    /// A bone's parent does not precede it in parent-before-child order.
572    #[error("node {node} has invalid parent {parent}")]
573    InvalidSkeletonParent {
574        /// The affected skeleton node.
575        node: BoneId,
576        /// The invalid parent index.
577        parent: BoneId,
578    },
579    /// The source-node projection declares one source node identity twice.
580    #[error("source skeleton declares duplicate source node index {source_node_index}")]
581    DuplicateSourceNodeIndex {
582        /// The duplicated source-node index.
583        source_node_index: usize,
584    },
585    /// The source-skin projection declares one source skin identity twice.
586    #[error("source skeleton declares duplicate source skin index {source_skin_index}")]
587    DuplicateSourceSkinIndex {
588        /// The duplicated source-skin index.
589        source_skin_index: usize,
590    },
591    /// A complete source-node projection contradicts the normalized skeleton.
592    #[error(
593        "source node {source_node_index} contradicts the document skeleton's parent chain ({violation})"
594    )]
595    SourceProjection {
596        /// The source node whose projection failed.
597        source_node_index: usize,
598        /// The typed projection failure.
599        violation: SourceProjectionViolation,
600    },
601    /// A clip declares the same target `(node, property)` more than once.
602    #[error("clip {clip_index} declares duplicate {property:?} tracks for node {node}")]
603    DuplicateClipTrack {
604        /// Index into [`Document::clips`].
605        clip_index: usize,
606        /// The duplicated target node.
607        node: BoneId,
608        /// The duplicated animated property.
609        property: Property,
610    },
611    /// A track is malformed for its target, interpolation, or value storage.
612    #[error("clip {clip_index} track for node {node} has an invalid shape ({violation})")]
613    TrackShape {
614        /// Index into [`Document::clips`].
615        clip_index: usize,
616        /// The track's target node.
617        node: BoneId,
618        /// The typed track-shape failure.
619        violation: TrackShapeViolation,
620    },
621    /// A mesh instance has an invalid reference or inverse-bind payload.
622    #[error("mesh instance {instance_index} is invalid ({violation})")]
623    MeshInstanceShape {
624        /// Index into [`SceneAssets::instances`].
625        instance_index: usize,
626        /// The typed mesh-instance failure.
627        violation: MeshInstanceShapeViolation,
628    },
629    /// A bone-level inverse-bind matrix is non-finite.
630    #[error("node {node} has a non-finite inverse-bind matrix")]
631    NonFiniteBoneInverseBind {
632        /// The affected skeleton node.
633        node: BoneId,
634    },
635}
636
637/// The way a complete source-node projection contradicts the skeleton.
638#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
639#[non_exhaustive]
640pub enum SourceProjectionViolation {
641    /// A projected bone index is outside [`Skeleton::bones`].
642    #[error("projected_bone_out_of_range")]
643    ProjectedBoneOutOfRange,
644    /// Two source-node rows project to the same normalized bone.
645    #[error("two_source_nodes_project_to_one_bone")]
646    TwoSourceNodesProjectToOneBone,
647    /// An ancestor walk names a source node absent from the projection table.
648    #[error("parent_source_node_is_missing")]
649    ParentSourceNodeMissing,
650    /// An ancestor walk through unprojected rows does not terminate.
651    #[error("cyclic_unprojected_source_parent_chain")]
652    CyclicUnprojectedSourceParentChain,
653    /// The nearest projected source ancestor differs from the bone parent.
654    #[error("projection_and_skeleton_parents_differ")]
655    NearestProjectedParentMismatch,
656    /// A projected bone has an unprojected direct skeleton child.
657    #[error("projected_bone_has_an_unprojected_skeleton_child")]
658    ProjectedBoneHasUnprojectedSkeletonChild,
659}
660
661/// The way a clip track is malformed.
662#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
663#[non_exhaustive]
664pub enum TrackShapeViolation {
665    /// The target bone index is outside [`Skeleton::bones`].
666    #[error("bone_index_out_of_range")]
667    BoneIndexOutOfRange,
668    /// The track has no keyframe times.
669    #[error("empty_times")]
670    EmptyTimes,
671    /// At least one keyframe time is not finite.
672    #[error("non_finite_time")]
673    NonFiniteTime,
674    /// Keyframe times are not strictly increasing.
675    #[error("times_not_strictly_increasing")]
676    TimesNotStrictlyIncreasing,
677    /// Stored value count disagrees with the interpolation's key count.
678    #[error("value_count_mismatch")]
679    ValueCountMismatch,
680    /// The value storage does not match the targeted property.
681    #[error("value_type_mismatches_property")]
682    ValueTypeMismatchesProperty,
683    /// At least one stored value is not finite.
684    #[error("non_finite_value")]
685    NonFiniteValue,
686}
687
688/// The way a mesh instance is malformed.
689#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
690#[non_exhaustive]
691pub enum MeshInstanceShapeViolation {
692    /// The instance node index is outside [`Skeleton::bones`].
693    #[error("node_index_out_of_range")]
694    NodeIndexOutOfRange,
695    /// The mesh index is outside [`SceneAssets::meshes`].
696    #[error("mesh_index_out_of_range")]
697    MeshIndexOutOfRange,
698    /// A skin joint index is outside [`Skeleton::bones`].
699    #[error("skin_joint_out_of_range")]
700    SkinJointOutOfRange,
701    /// A non-empty inverse-bind array has a different length from skin joints.
702    #[error("skin_ibm_count_mismatch")]
703    SkinInverseBindCountMismatch,
704    /// An instance inverse-bind matrix is not finite.
705    #[error("non_finite_inverse_bind")]
706    NonFiniteSkinInverseBind,
707}
708
709// --- Scene assets (meshes/materials) -----------------------------------
710//
711// The geometry half of a [`Document`]. Populated by both format loaders and
712// emitted by the writer, so a full conversion preserves geometry. Primitives
713// may be indexed already; [`Primitive::weld`] can index unindexed exact
714// duplicates without collapsing authored seams.
715
716/// One triangle-list primitive sharing a material. Attribute arrays may be
717/// indexed already; [`Primitive::weld`] dedupes an unindexed primitive into
718/// indexed form.
719///
720/// Additional glTF skin-influence attribute sets are intentionally metadata
721/// only. The primary four influences remain in [`Self::joints`] and
722/// [`Self::weights`]; consumers can use this metadata to apply their own
723/// policy without the core assuming how additional influences are evaluated.
724#[derive(Debug, Clone, Default)]
725pub struct Primitive {
726    /// Index into [`SceneAssets::materials`].
727    pub material: Option<usize>,
728    /// Triangle indices into the attribute arrays; empty = unindexed.
729    pub indices: Vec<u32>,
730    /// Vertex positions in scene units.
731    pub positions: Vec<Vec3>,
732    /// Same length as `positions`, or empty.
733    pub normals: Vec<Vec3>,
734    /// Same length as `positions`, or empty.
735    pub uvs: Vec<[f32; 2]>,
736    /// Indices into an owning instance's skin-joint list; empty if unskinned.
737    pub joints: Vec<[u16; 4]>,
738    /// Skinning weights parallel to [`Primitive::joints`].
739    pub weights: Vec<[f32; 4]>,
740    /// Declared non-primary skin-influence attribute sets.
741    ///
742    /// Each entry records whether the glTF primitive had `JOINTS_n` and/or
743    /// `WEIGHTS_n` for `n >= 1`. Entries are sorted by
744    /// [`AdditionalInfluenceSet::set_index`].
745    pub additional_influence_sets: Vec<AdditionalInfluenceSet>,
746}
747
748/// Presence metadata for one non-primary glTF skin-influence attribute set.
749///
750/// A set may contain only one side because source assets can declare
751/// `JOINTS_n` and `WEIGHTS_n` independently. This type deliberately does not
752/// retain the corresponding per-vertex values: the core model's skinning
753/// semantics remain the primary `JOINTS_0` / `WEIGHTS_0` set.
754#[derive(Debug, Clone, Copy, PartialEq, Eq)]
755pub struct AdditionalInfluenceSet {
756    /// glTF attribute-set number (`n >= 1`).
757    pub set_index: u32,
758    /// Whether `JOINTS_n` was declared.
759    pub joints_present: bool,
760    /// Whether `WEIGHTS_n` was declared.
761    pub weights_present: bool,
762}
763
764/// One source mesh definition, independent of any node that instances it.
765#[derive(Debug, Clone, Default)]
766pub struct MeshAsset {
767    /// Mesh name.
768    pub name: String,
769    /// Stable index of this definition in the source format.
770    ///
771    /// glTF permits several nodes to instance one mesh definition. Loaders
772    /// preserve that distinction through [`SceneAssets::instances`].
773    pub source_mesh_index: usize,
774    /// Triangle-list primitives belonging to this mesh.
775    pub primitives: Vec<Primitive>,
776}
777
778/// One node instance of a source [`MeshAsset`] definition.
779#[derive(Debug, Clone, Default)]
780pub struct MeshInstance {
781    /// Index of the source-format node that owns this mesh instance.
782    pub source_node_index: usize,
783    /// The node this mesh hangs off in the core skeleton.
784    pub node: BoneId,
785    /// Index into [`SceneAssets::meshes`] of the instanced definition.
786    pub mesh: usize,
787    /// Skin joints in cluster order. Empty = unskinned.
788    pub skin_joints: Vec<BoneId>,
789    /// Per-joint inverse bind matrices, parallel to `skin_joints`
790    /// (glTF convention: joint-bind-world⁻¹ × geometry-to-world, all
791    /// in the converted scene space). Falls back to the bones'
792    /// `inverse_bind` when empty.
793    pub skin_ibms: Vec<Mat4>,
794}
795
796/// One declared source scene and its root nodes.
797#[derive(Debug, Clone, Default)]
798pub struct SceneAsset {
799    /// Index of this scene in the source-format scene array.
800    pub source_scene_index: usize,
801    /// Authored scene name, when the source format provides one.
802    pub name: Option<String>,
803    /// Root nodes belonging to this scene, represented as core bone ids.
804    pub roots: Vec<BoneId>,
805}
806
807/// Whether a loader supplied source-node and source-skin identity evidence.
808///
809/// The skeleton used by sampling is deliberately format-neutral and is ordered
810/// for parent-before-child FK. Source formats can use a different stable node
811/// order, so this coverage flag keeps an empty source table from being
812/// mistaken for a source file with no nodes or skins.
813#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
814#[serde(rename_all = "snake_case")]
815pub enum SourceSkeletonCoverage {
816    /// The loader cannot provide source-node and source-skin identity facts.
817    #[default]
818    Unavailable,
819    /// The source-node and source-skin tables describe the loaded input.
820    /// Source nodes with [`SourceNodeAsset::bone`] form the downward-closed
821    /// projection consumed by strict operations.
822    Complete,
823}
824
825/// The source-projected local-rest representation of one source node.
826///
827/// glTF permits either decomposed TRS properties or a matrix. Keeping this
828/// representation separate from [`Bone::rest`] avoids presenting a lossy
829/// matrix decomposition as though it were authored TRS evidence. For glTF the
830/// value is the authored node member. A loader whose format semantics require
831/// coordinate, helper-node, or inheritance normalization may instead project
832/// that documented source-side result; its format-specific capability
833/// inventory must make the distinction explicit. In particular, the FBX
834/// loader records ufbx's adjusted/compensated TRS here and never claims it is
835/// the raw FBX transform stack.
836#[derive(Debug, Clone)]
837pub enum SourceNodeLocalRest {
838    /// Source-declared or format-normalized translation, rotation, and scale.
839    Trs {
840        /// Local translation in scene units.
841        translation: Vec3,
842        /// Local orientation relative to the parent node.
843        rotation: Quat,
844        /// Local non-uniform scale.
845        scale: Vec3,
846    },
847    /// Source-declared or format-normalized column-major 4×4 local transform.
848    Matrix(Mat4),
849}
850
851/// One source-side node with stable loader identity facts.
852///
853/// For a direct format projection this is an authored node. A loader that
854/// normalizes helper or inheritance semantics may also include generated
855/// source-side nodes, provided its capability inventory records that boundary.
856///
857/// Marked `#[non_exhaustive]` because this projection grows as loaders learn
858/// to carry more source-native identity (`bone` was the most recent
859/// addition): out-of-crate embedders construct it through
860/// [`SourceNodeAsset::new`] and assign the optional facts they have, so a
861/// later field cannot break their build. The sibling source-asset structs in
862/// this module are not yet marked; they are stable in a way this one has
863/// already demonstrated it is not.
864#[derive(Debug, Clone)]
865#[non_exhaustive]
866pub struct SourceNodeAsset {
867    /// Stable index in the loader's complete source-side node table.
868    pub source_node_index: usize,
869    /// Source-projected node name, when present.
870    pub name: Option<String>,
871    /// Source-side node-table index of the projected parent, when any.
872    pub parent_source_node_index: Option<usize>,
873    /// Source scenes that name this projected node as a root, in source-scene order.
874    pub scene_root_indices: Vec<usize>,
875    /// Source-projected local-rest representation.
876    pub local_rest: SourceNodeLocalRest,
877    /// The core [`BoneId`] this source node normalized to, when the loader
878    /// retained it as an independent normalized node.
879    ///
880    /// `None` means this source row has no independent [`Skeleton`] bone. A
881    /// loader may have dropped an unreachable node, or it may have folded a
882    /// static connector's authored local rest into the next projected node.
883    /// The row remains authoritative source identity and local-rest evidence
884    /// under [`SourceSkeletonCoverage::Complete`]. Format-neutral consumers
885    /// that need to resolve a raw source-node selector (for example
886    /// [`crate::scale::ScaleOperation::RestBindUniformScale`]'s
887    /// `source_root_node_index`/skin joints) into the normalized
888    /// [`Skeleton`] must use this field rather than assuming source-node
889    /// order equals bone order.
890    ///
891    /// With [`SourceSkeletonCoverage::Complete`] coverage, the `Some` rows
892    /// must form a downward-closed, nearest-projected-parent-preserving
893    /// projection into the normalized skeleton. Unprojected source rows may
894    /// occur between projected ancestors; [`validate_document_shape`]
895    /// verifies that relation.
896    pub bone: Option<BoneId>,
897}
898
899impl SourceNodeAsset {
900    /// One source node identified by its stable source-array index and its
901    /// source-projected local rest — the two facts every loader necessarily has.
902    ///
903    /// Every remaining fact ([`Self::name`], [`Self::parent_source_node_index`],
904    /// [`Self::scene_root_indices`], [`Self::bone`]) starts absent and is
905    /// assigned through the public fields. This is the only way to build the
906    /// value outside `animsmith-core`, since the type is `#[non_exhaustive]`.
907    pub fn new(source_node_index: usize, local_rest: SourceNodeLocalRest) -> Self {
908        Self {
909            source_node_index,
910            name: None,
911            parent_source_node_index: None,
912            scene_root_indices: Vec::new(),
913            local_rest,
914            bone: None,
915        }
916    }
917}
918
919/// Read status for a source skin's inverse-bind declaration.
920///
921/// glTF supplies this through an accessor. Other formats may supply an
922/// equivalent ordered declaration (for example, FBX cluster bind matrices)
923/// that the loader projects into target coordinates. Format-specific
924/// capability evidence must distinguish projected values from exact source
925/// payload preservation.
926#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
927#[serde(rename_all = "snake_case")]
928pub enum SourceInverseBindAccessorStatus {
929    /// The skin did not declare inverse-bind matrices.
930    #[default]
931    Absent,
932    /// The declaration was readable and had at least one matrix per declared joint.
933    Available,
934    /// The source declared a count-zero inverse-bind payload.
935    EmptyAccessor,
936    /// The declaration was readable but has fewer matrices than declared joints.
937    CountMismatch,
938    /// The source declared bind matrices that the loader could not read.
939    Unreadable,
940}
941
942/// Read-side evidence for one source skin inverse-bind declaration.
943#[derive(Debug, Clone, Default)]
944pub struct SourceInverseBindAccessor {
945    /// Whether the source bind declaration was absent, complete, or malformed.
946    pub status: SourceInverseBindAccessorStatus,
947    /// Declared source matrix count, or `None` when no matrices were declared.
948    pub declared_count: Option<usize>,
949    /// Matrices in declared joint order when they were readable.
950    ///
951    /// glTF retains raw accessor values. A format loader may instead retain a
952    /// documented coordinate-normalized projection of the source bind
953    /// declaration. This may contain non-finite values from a parseable binary
954    /// accessor or equivalent source structure.
955    /// Measurement serialization must classify those values rather than emit
956    /// non-finite JSON numbers.
957    pub matrices: Vec<Mat4>,
958}
959
960/// One source node that declares use of a source skin.
961#[derive(Debug, Clone)]
962pub struct SourceSkinAttachment {
963    /// Stable node-array index of the attachment node.
964    pub source_node_index: usize,
965    /// Stable source mesh-definition index, when the node declares a mesh.
966    ///
967    /// This remains present even when the current core mesh importer skips the
968    /// definition (for example, because it has no triangle-list primitive).
969    pub source_mesh_index: Option<usize>,
970}
971
972/// One source skin definition, kept separate from bone-level convenience data.
973#[derive(Debug, Clone, Default)]
974pub struct SourceSkinAsset {
975    /// Stable skin-array index in the source format.
976    pub source_skin_index: usize,
977    /// Authored skin name, when present.
978    pub name: Option<String>,
979    /// Explicitly declared skeleton root, when present; never inferred.
980    pub skeleton_root_source_node_index: Option<usize>,
981    /// Source joints in declared skin-slot order.
982    pub joint_source_node_indices: Vec<usize>,
983    /// Source inverse-bind declaration evidence for this skin.
984    ///
985    /// glTF retains exact accessor values. Other loaders may retain a
986    /// documented coordinate-normalized projection, as described by
987    /// [`SourceInverseBindAccessor`].
988    pub inverse_bind_accessor: SourceInverseBindAccessor,
989    /// Source nodes that reference this skin, in source-node order.
990    pub attachments: Vec<SourceSkinAttachment>,
991}
992
993/// Source-node and source-skin evidence carried beside normalized scene assets.
994#[derive(Debug, Clone, Default)]
995pub struct SourceSkeletonAssets {
996    /// Whether these source tables are complete for the loaded input.
997    pub coverage: SourceSkeletonCoverage,
998    /// Source nodes in stable source-node order.
999    pub nodes: Vec<SourceNodeAsset>,
1000    /// Source skins in stable source-skin order.
1001    pub skins: Vec<SourceSkinAsset>,
1002}
1003
1004/// An embedded texture: raw encoded image bytes (glTF embeds the file
1005/// as-is, no decoding).
1006#[derive(Debug, Clone)]
1007pub struct TextureAsset {
1008    /// Encoded image bytes.
1009    pub bytes: Vec<u8>,
1010    /// "image/png" or "image/jpeg".
1011    pub mime: String,
1012}
1013
1014/// A normal-map texture and the scalar applied to its X/Y components.
1015///
1016/// Keeping the scale beside the texture makes the glTF normal-texture state
1017/// atomic: a scale cannot accidentally survive after its texture is removed.
1018#[derive(Debug, Clone)]
1019pub struct NormalTextureAsset {
1020    /// Embedded encoded normal-map image.
1021    pub texture: TextureAsset,
1022    /// Scalar multiplier for the decoded tangent-space X/Y components.
1023    pub scale: f32,
1024}
1025
1026/// An occlusion texture and the scalar applied to its sampled value.
1027///
1028/// Keeping the strength beside the texture makes the glTF occlusion-texture
1029/// state atomic: a strength cannot accidentally survive after its texture is
1030/// removed.
1031#[derive(Debug, Clone)]
1032pub struct OcclusionTextureAsset {
1033    /// Embedded encoded occlusion texture.
1034    pub texture: TextureAsset,
1035    /// Scalar multiplier for the sampled occlusion value.
1036    pub strength: f32,
1037}
1038
1039/// PBR material factors plus optional embedded glTF texture slots.
1040#[derive(Debug, Clone)]
1041pub struct MaterialAsset {
1042    /// Material name.
1043    pub name: String,
1044    /// Multiplied with the texture when one is present (set to white
1045    /// by the FBX loader in that case, matching exporter convention).
1046    pub base_color: [f32; 4],
1047    /// Metallic factor.
1048    pub metallic: f32,
1049    /// Roughness factor.
1050    pub roughness: f32,
1051    /// Embedded base-color texture, if one was loaded.
1052    pub base_color_texture: Option<TextureAsset>,
1053    /// Embedded tangent-space normal texture, if one was loaded.
1054    pub normal_texture: Option<NormalTextureAsset>,
1055    /// Embedded metallic-roughness texture, if one was loaded.
1056    ///
1057    /// glTF stores roughness in green and metallic in blue.
1058    pub metallic_roughness_texture: Option<TextureAsset>,
1059    /// Embedded occlusion texture, if one was loaded.
1060    pub occlusion_texture: Option<OcclusionTextureAsset>,
1061}
1062
1063/// Whether source material-resource inspection covers the whole input.
1064///
1065/// This sidecar is deliberately separate from writer-facing [`MaterialAsset`]
1066/// values. A loader may preserve materials for writing while declining to
1067/// inspect resource provenance or decode image metadata.
1068#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
1069#[serde(rename_all = "snake_case")]
1070pub enum MaterialResourceCoverage {
1071    /// The loader inspected its complete documented source material-resource
1072    /// domain. Format-specific documentation defines which binding slots that
1073    /// domain includes.
1074    Complete,
1075    /// The loader cannot provide source resource evidence.
1076    #[default]
1077    Unavailable,
1078}
1079
1080/// A material texture slot with stable source-format meaning.
1081///
1082/// Declaration order is the stable wire order used by material-resource
1083/// measurements.
1084#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
1085#[serde(rename_all = "snake_case")]
1086pub enum MaterialTextureSlot {
1087    /// Base-color texture.
1088    BaseColor,
1089    /// Tangent-space normal texture.
1090    Normal,
1091    /// Combined metallic-roughness texture.
1092    MetallicRoughness,
1093    /// Occlusion texture.
1094    Occlusion,
1095    /// Emissive texture.
1096    Emissive,
1097}
1098
1099/// One source material-to-texture binding.
1100#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1101pub struct SourceMaterialTextureBinding {
1102    /// Material slot in stable semantic order.
1103    pub slot: MaterialTextureSlot,
1104    /// Stable source texture index.
1105    pub texture_index: usize,
1106}
1107
1108/// One source material definition, independent of writer-facing material data.
1109#[derive(Debug, Clone, Default)]
1110pub struct SourceMaterialAsset {
1111    /// Stable source material index.
1112    pub material_index: usize,
1113    /// Authored name, when present.
1114    pub name: Option<String>,
1115    /// Source texture bindings, sorted by [`SourceMaterialTextureBinding::slot`].
1116    pub texture_bindings: Vec<SourceMaterialTextureBinding>,
1117}
1118
1119/// One source texture definition.
1120#[derive(Debug, Clone, Default)]
1121pub struct SourceTextureAsset {
1122    /// Stable source texture index.
1123    pub texture_index: usize,
1124    /// Authored name, when present.
1125    pub name: Option<String>,
1126    /// Stable source image index referenced by this texture.
1127    pub image_index: usize,
1128}
1129
1130/// How an image payload was declared by its source format.
1131#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
1132#[serde(rename_all = "snake_case")]
1133pub enum ImageSourceKind {
1134    /// Bytes embedded directly in a container record.
1135    Embedded,
1136    /// Bytes encoded in a data URI.
1137    DataUri,
1138    /// A relative or otherwise external resource reference.
1139    External,
1140}
1141
1142/// Image container format recognized by inspection.
1143#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
1144#[serde(rename_all = "snake_case")]
1145pub enum ImageContainerFormat {
1146    /// PNG image data.
1147    Png,
1148    /// JPEG image data.
1149    Jpeg,
1150}
1151
1152/// Decoded image color representation reported by inspection.
1153#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
1154#[serde(rename_all = "snake_case")]
1155pub enum DecodedImageColorType {
1156    /// Single-channel, 8-bit luminance.
1157    L8,
1158    /// Luminance plus alpha, 8-bit channels.
1159    La8,
1160    /// RGB, 8-bit channels.
1161    Rgb8,
1162    /// RGBA, 8-bit channels.
1163    Rgba8,
1164    /// Single-channel, 16-bit luminance.
1165    L16,
1166    /// Luminance plus alpha, 16-bit channels.
1167    La16,
1168    /// RGB, 16-bit channels.
1169    Rgb16,
1170    /// RGBA, 16-bit channels.
1171    Rgba16,
1172}
1173
1174/// Why source-image inspection could not produce decoded metadata.
1175#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
1176#[serde(rename_all = "snake_case")]
1177pub enum ImageUnavailableReason {
1178    /// The source does not make the image payload available to the loader.
1179    SourceUnavailable,
1180    /// A data URI could not be parsed or decoded.
1181    InvalidDataUri,
1182    /// The image container is not supported for inspection.
1183    UnsupportedContainer,
1184    /// Supported image bytes could not be decoded.
1185    DecodeFailed,
1186    /// Inspection declined the resource because it exceeded a resource limit.
1187    ResourceLimit,
1188}
1189
1190/// Result of bounded source-image inspection.
1191#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1192pub enum SourceImageInspection {
1193    /// Decoded metadata was available without retaining decoded pixels.
1194    Available {
1195        /// Pixel width.
1196        width: u32,
1197        /// Pixel height.
1198        height: u32,
1199        /// Number of decoded channels.
1200        channel_count: u8,
1201        /// Decoded color representation.
1202        color_type: DecodedImageColorType,
1203    },
1204    /// Inspection could not provide decoded metadata.
1205    Unavailable {
1206        /// Stable unavailability reason.
1207        reason: ImageUnavailableReason,
1208    },
1209}
1210
1211/// One source image definition and bounded inspection result.
1212#[derive(Debug, Clone)]
1213pub struct SourceImageAsset {
1214    /// Stable source image index.
1215    pub image_index: usize,
1216    /// Authored name, when present.
1217    pub name: Option<String>,
1218    /// Source declaration kind.
1219    pub source_kind: ImageSourceKind,
1220    /// MIME type declared by the source, when present.
1221    pub declared_mime_type: Option<String>,
1222    /// Detected container format, when recognisable.
1223    pub detected_container: Option<ImageContainerFormat>,
1224    /// Bounded image-inspection result.
1225    pub inspection: SourceImageInspection,
1226}
1227
1228/// Read-only source material-resource evidence carried beside scene assets.
1229#[derive(Debug, Clone, Default)]
1230pub struct MaterialResourceAssets {
1231    /// Whether the source resource lists are complete.
1232    pub coverage: MaterialResourceCoverage,
1233    /// Source materials in source order.
1234    pub materials: Vec<SourceMaterialAsset>,
1235    /// Source textures in source order.
1236    pub textures: Vec<SourceTextureAsset>,
1237    /// Source images in source order.
1238    pub images: Vec<SourceImageAsset>,
1239}
1240
1241impl Primitive {
1242    /// Dedupe identical corners into indexed triangles. Exact
1243    /// bit-equality only — no tolerance welding, so seams authored via
1244    /// split normals/UVs are preserved.
1245    pub fn weld(&mut self) {
1246        if !self.indices.is_empty() || self.positions.is_empty() {
1247            return;
1248        }
1249        let corner_key = |i: usize| -> Vec<u8> {
1250            let mut key = Vec::with_capacity(64);
1251            let mut push_f32s = |vals: &[f32]| {
1252                for v in vals {
1253                    key.extend_from_slice(&v.to_le_bytes());
1254                }
1255            };
1256            push_f32s(&self.positions[i].to_array());
1257            if let Some(n) = self.normals.get(i) {
1258                push_f32s(&n.to_array());
1259            }
1260            if let Some(uv) = self.uvs.get(i) {
1261                push_f32s(uv);
1262            }
1263            if let Some(w) = self.weights.get(i) {
1264                push_f32s(w);
1265            }
1266            if let Some(j) = self.joints.get(i) {
1267                for v in j {
1268                    key.extend_from_slice(&v.to_le_bytes());
1269                }
1270            }
1271            key
1272        };
1273        let mut seen: std::collections::HashMap<Vec<u8>, u32> = std::collections::HashMap::new();
1274        let mut indices = Vec::with_capacity(self.positions.len());
1275        let mut positions = Vec::new();
1276        let mut normals = Vec::new();
1277        let mut uvs = Vec::new();
1278        let mut joints = Vec::new();
1279        let mut weights = Vec::new();
1280        for i in 0..self.positions.len() {
1281            let index = *seen.entry(corner_key(i)).or_insert_with(|| {
1282                positions.push(self.positions[i]);
1283                if let Some(n) = self.normals.get(i) {
1284                    normals.push(*n);
1285                }
1286                if let Some(uv) = self.uvs.get(i) {
1287                    uvs.push(*uv);
1288                }
1289                if let Some(j) = self.joints.get(i) {
1290                    joints.push(*j);
1291                }
1292                if let Some(w) = self.weights.get(i) {
1293                    weights.push(*w);
1294                }
1295                (positions.len() - 1) as u32
1296            });
1297            indices.push(index);
1298        }
1299        self.indices = indices;
1300        self.positions = positions;
1301        self.normals = normals;
1302        self.uvs = uvs;
1303        self.joints = joints;
1304        self.weights = weights;
1305    }
1306}
1307
1308/// Mesh definitions, their node instances, scenes, and materials carried
1309/// alongside animation data.
1310#[derive(Debug, Clone, Default)]
1311pub struct SceneAssets {
1312    /// Mesh definitions in source order, including definitions without a node
1313    /// instance.
1314    pub meshes: Vec<MeshAsset>,
1315    /// Node instances of the mesh definitions, in source node order.
1316    pub instances: Vec<MeshInstance>,
1317    /// Materials referenced by mesh primitives.
1318    pub materials: Vec<MaterialAsset>,
1319    /// Read-only source material, texture, and image evidence for measurement.
1320    /// Writer-facing material slots remain in [`Self::materials`].
1321    pub material_resources: MaterialResourceAssets,
1322    /// Declared source scenes in source order.
1323    pub scenes: Vec<SceneAsset>,
1324    /// Source scene index selected by default, when one was declared.
1325    pub default_scene: Option<usize>,
1326    /// Source-node and source-skin identity evidence for skeleton measurements.
1327    ///
1328    /// This is intentionally separate from the normalized [`Skeleton`] and
1329    /// from [`MeshInstance::skin_ibms`]: a source node order need not match
1330    /// FK order, and one joint can have different inverse binds in different
1331    /// source skins.
1332    pub source_skeleton: SourceSkeletonAssets,
1333}
1334
1335/// Validate the enumerated structural snapshot strict document operations
1336/// rely on.
1337///
1338/// This is a snapshot only: [`Document`] is publicly mutable, so a successful
1339/// call does not certify a document against later mutation. Any strict
1340/// operation that relies on this full shape must rerun validation at its own
1341/// public boundary.
1342/// Tolerant analysis APIs may intentionally accept documents this rejects and
1343/// preserve the valid evidence they can read. This function does not validate
1344/// operation-specific capability, affine, closure, proof, or payload
1345/// invariants such as primitive skinning shape and base positions.
1346///
1347/// # Errors
1348///
1349/// Returns a typed [`DocumentShapeError`] for the first violation in stable
1350/// validation order: skeleton rest/topology, source identity/projection,
1351/// tracks, instances, then bone-level inverse binds.
1352pub fn validate_document_shape(document: &Document) -> Result<(), DocumentShapeError> {
1353    validate_skeleton_rest(&document.skeleton)?;
1354    validate_source_skeleton_identity(&document.assets.source_skeleton)?;
1355    validate_source_projection(document)?;
1356    validate_clip_tracks(document)?;
1357    validate_mesh_instances(document)?;
1358    validate_bone_inverse_binds(&document.skeleton)
1359}
1360
1361fn validate_skeleton_rest(skeleton: &Skeleton) -> Result<(), DocumentShapeError> {
1362    world_rest_matrices(skeleton)
1363        .map(|_| ())
1364        .map_err(|error| match error {
1365            WorldMatrixError::NonFiniteTransform { node } => {
1366                DocumentShapeError::NonFiniteSkeletonRest { node }
1367            }
1368            WorldMatrixError::InvalidParent { node, parent } => {
1369                DocumentShapeError::InvalidSkeletonParent { node, parent }
1370            }
1371        })
1372}
1373
1374fn validate_source_skeleton_identity(
1375    source_skeleton: &SourceSkeletonAssets,
1376) -> Result<(), DocumentShapeError> {
1377    let mut seen_nodes = BTreeSet::new();
1378    for node in &source_skeleton.nodes {
1379        if !seen_nodes.insert(node.source_node_index) {
1380            return Err(DocumentShapeError::DuplicateSourceNodeIndex {
1381                source_node_index: node.source_node_index,
1382            });
1383        }
1384    }
1385    let mut seen_skins = BTreeSet::new();
1386    for skin in &source_skeleton.skins {
1387        if !seen_skins.insert(skin.source_skin_index) {
1388            return Err(DocumentShapeError::DuplicateSourceSkinIndex {
1389                source_skin_index: skin.source_skin_index,
1390            });
1391        }
1392    }
1393    Ok(())
1394}
1395
1396/// Validate the identity relation a `Complete` source projection claims.
1397///
1398/// Projected rows must be injective, preserve each bone's nearest projected
1399/// ancestor, and be downward-closed in the normalized skeleton. Unprojected
1400/// source rows may remain between projected ancestors, and unrelated
1401/// unprojected roots remain legal; totality is not required. Non-`Complete`
1402/// rows are not identity evidence and are deliberately ignored.
1403///
1404/// The rule is load-bearing for consumers such as scale that select a rewrite
1405/// domain through source-node ancestry but apply and prove it through
1406/// [`Skeleton::bones`]. Without agreement, a normalized child can sit outside
1407/// the selected source closure while its parent moves, leaving its displaced
1408/// world rest outside every declared proof walk.
1409fn validate_source_projection(document: &Document) -> Result<(), DocumentShapeError> {
1410    let source_skeleton = &document.assets.source_skeleton;
1411    if source_skeleton.coverage != SourceSkeletonCoverage::Complete {
1412        return Ok(());
1413    }
1414
1415    let bones = &document.skeleton.bones;
1416    let mut bone_of_source = BTreeMap::new();
1417    let mut source_of_bone = BTreeMap::new();
1418    let mut skeleton_parents = Vec::with_capacity(source_skeleton.nodes.len());
1419    for node in &source_skeleton.nodes {
1420        let Some(bone) = node.bone else {
1421            continue;
1422        };
1423        let skeleton_parent = bones
1424            .get(bone)
1425            .ok_or(DocumentShapeError::SourceProjection {
1426                source_node_index: node.source_node_index,
1427                violation: SourceProjectionViolation::ProjectedBoneOutOfRange,
1428            })?
1429            .parent;
1430        if source_of_bone
1431            .insert(bone, node.source_node_index)
1432            .is_some()
1433        {
1434            return Err(DocumentShapeError::SourceProjection {
1435                source_node_index: node.source_node_index,
1436                violation: SourceProjectionViolation::TwoSourceNodesProjectToOneBone,
1437            });
1438        }
1439        bone_of_source.insert(node.source_node_index, bone);
1440        skeleton_parents.push((node, skeleton_parent));
1441    }
1442
1443    let by_source_index: BTreeMap<_, _> = source_skeleton
1444        .nodes
1445        .iter()
1446        .map(|node| (node.source_node_index, node))
1447        .collect();
1448    let unprojected_rows = source_skeleton.nodes.len() - bone_of_source.len();
1449    // Cache only successful suffix resolutions. A malformed suffix still
1450    // fails on the first projected row that reaches it, preserving that row
1451    // as the error owner; a later row is never visited after the error.
1452    let mut resolved_unprojected = BTreeMap::<usize, Option<BoneId>>::new();
1453    for (node, skeleton_parent) in skeleton_parents {
1454        let mut cursor = node.parent_source_node_index;
1455        let mut unresolved_suffix = Vec::new();
1456        let projected_parent = loop {
1457            let Some(parent_source_node_index) = cursor else {
1458                break None;
1459            };
1460            if let Some(&bone) = bone_of_source.get(&parent_source_node_index) {
1461                break Some(bone);
1462            }
1463            if let Some(&projected_parent) = resolved_unprojected.get(&parent_source_node_index) {
1464                break projected_parent;
1465            }
1466            let parent = by_source_index.get(&parent_source_node_index).ok_or(
1467                DocumentShapeError::SourceProjection {
1468                    source_node_index: node.source_node_index,
1469                    violation: SourceProjectionViolation::ParentSourceNodeMissing,
1470                },
1471            )?;
1472            unresolved_suffix.push(parent_source_node_index);
1473            if unresolved_suffix.len() > unprojected_rows {
1474                return Err(DocumentShapeError::SourceProjection {
1475                    source_node_index: node.source_node_index,
1476                    violation: SourceProjectionViolation::CyclicUnprojectedSourceParentChain,
1477                });
1478            }
1479            cursor = parent.parent_source_node_index;
1480        };
1481        for source_node_index in unresolved_suffix {
1482            resolved_unprojected.insert(source_node_index, projected_parent);
1483        }
1484        if projected_parent != skeleton_parent {
1485            return Err(DocumentShapeError::SourceProjection {
1486                source_node_index: node.source_node_index,
1487                violation: SourceProjectionViolation::NearestProjectedParentMismatch,
1488            });
1489        }
1490    }
1491
1492    for (bone, child) in bones.iter().enumerate() {
1493        if source_of_bone.contains_key(&bone) {
1494            continue;
1495        }
1496        if let Some(parent) = child.parent
1497            && let Some(&source_node_index) = source_of_bone.get(&parent)
1498        {
1499            return Err(DocumentShapeError::SourceProjection {
1500                source_node_index,
1501                violation: SourceProjectionViolation::ProjectedBoneHasUnprojectedSkeletonChild,
1502            });
1503        }
1504    }
1505    Ok(())
1506}
1507
1508fn validate_clip_tracks(document: &Document) -> Result<(), DocumentShapeError> {
1509    let bone_count = document.skeleton.bones.len();
1510    for (clip_index, clip) in document.clips.iter().enumerate() {
1511        let mut seen = Vec::with_capacity(clip.tracks.len());
1512        for track in &clip.tracks {
1513            if track.bone >= bone_count {
1514                return Err(DocumentShapeError::TrackShape {
1515                    clip_index,
1516                    node: track.bone,
1517                    violation: TrackShapeViolation::BoneIndexOutOfRange,
1518                });
1519            }
1520            if seen.contains(&(track.bone, track.property)) {
1521                return Err(DocumentShapeError::DuplicateClipTrack {
1522                    clip_index,
1523                    node: track.bone,
1524                    property: track.property,
1525                });
1526            }
1527            seen.push((track.bone, track.property));
1528            validate_track_shape(clip_index, track)?;
1529        }
1530    }
1531    Ok(())
1532}
1533
1534pub(crate) fn validate_track_shape(
1535    clip_index: usize,
1536    track: &Track,
1537) -> Result<(), DocumentShapeError> {
1538    let violation = if track.times.is_empty() {
1539        Some(TrackShapeViolation::EmptyTimes)
1540    } else if track.times.iter().any(|time| !time.is_finite()) {
1541        Some(TrackShapeViolation::NonFiniteTime)
1542    } else if track.times.windows(2).any(|times| times[0] >= times[1]) {
1543        Some(TrackShapeViolation::TimesNotStrictlyIncreasing)
1544    } else {
1545        let expected_values = match track.interpolation {
1546            Interpolation::CubicSpline => track.times.len().checked_mul(3),
1547            Interpolation::Linear | Interpolation::Step => Some(track.times.len()),
1548        };
1549        if expected_values != Some(track.values.len()) {
1550            Some(TrackShapeViolation::ValueCountMismatch)
1551        } else if !matches!(
1552            (&track.values, track.property),
1553            (
1554                TrackValues::Vec3s(_),
1555                Property::Translation | Property::Scale
1556            ) | (TrackValues::Quats(_), Property::Rotation)
1557        ) {
1558            Some(TrackShapeViolation::ValueTypeMismatchesProperty)
1559        } else if match &track.values {
1560            TrackValues::Vec3s(values) => values.iter().any(|value| !value.is_finite()),
1561            TrackValues::Quats(values) => values.iter().any(|value| !value.is_finite()),
1562        } {
1563            Some(TrackShapeViolation::NonFiniteValue)
1564        } else {
1565            None
1566        }
1567    };
1568    violation.map_or(Ok(()), |violation| {
1569        Err(DocumentShapeError::TrackShape {
1570            clip_index,
1571            node: track.bone,
1572            violation,
1573        })
1574    })
1575}
1576
1577fn validate_mesh_instances(document: &Document) -> Result<(), DocumentShapeError> {
1578    let bone_count = document.skeleton.bones.len();
1579    let mesh_count = document.assets.meshes.len();
1580    for (instance_index, instance) in document.assets.instances.iter().enumerate() {
1581        let violation = if instance.node >= bone_count {
1582            Some(MeshInstanceShapeViolation::NodeIndexOutOfRange)
1583        } else if instance.mesh >= mesh_count {
1584            Some(MeshInstanceShapeViolation::MeshIndexOutOfRange)
1585        } else if instance
1586            .skin_joints
1587            .iter()
1588            .any(|&joint| joint >= bone_count)
1589        {
1590            Some(MeshInstanceShapeViolation::SkinJointOutOfRange)
1591        } else if !instance.skin_ibms.is_empty()
1592            && instance.skin_ibms.len() != instance.skin_joints.len()
1593        {
1594            Some(MeshInstanceShapeViolation::SkinInverseBindCountMismatch)
1595        } else if instance.skin_ibms.iter().any(|ibm| !mat4_is_finite(*ibm)) {
1596            Some(MeshInstanceShapeViolation::NonFiniteSkinInverseBind)
1597        } else {
1598            None
1599        };
1600        if let Some(violation) = violation {
1601            return Err(DocumentShapeError::MeshInstanceShape {
1602                instance_index,
1603                violation,
1604            });
1605        }
1606    }
1607    Ok(())
1608}
1609
1610fn validate_bone_inverse_binds(skeleton: &Skeleton) -> Result<(), DocumentShapeError> {
1611    for (node, bone) in skeleton.bones.iter().enumerate() {
1612        if let Some(inverse_bind) = bone.inverse_bind
1613            && !mat4_is_finite(inverse_bind)
1614        {
1615            return Err(DocumentShapeError::NonFiniteBoneInverseBind { node });
1616        }
1617    }
1618    Ok(())
1619}
1620
1621#[cfg(test)]
1622mod tests {
1623    use super::*;
1624
1625    #[test]
1626    fn property_serde_uses_the_stable_trs_vocabulary() {
1627        assert_eq!(
1628            serde_json::to_value([Property::Translation, Property::Rotation, Property::Scale,])
1629                .expect("properties serialize"),
1630            serde_json::json!(["translation", "rotation", "scale"])
1631        );
1632        assert_eq!(
1633            serde_json::from_value::<Vec<Property>>(serde_json::json!([
1634                "translation",
1635                "rotation",
1636                "scale"
1637            ]))
1638            .expect("properties deserialize"),
1639            [Property::Translation, Property::Rotation, Property::Scale,]
1640        );
1641    }
1642
1643    fn bone(parent: Option<BoneId>) -> Bone {
1644        Bone {
1645            name: "bone".into(),
1646            parent,
1647            rest: Transform::IDENTITY,
1648            inverse_bind: None,
1649        }
1650    }
1651
1652    fn one_bone_document() -> Document {
1653        Document {
1654            skeleton: Skeleton {
1655                bones: vec![bone(None)],
1656            },
1657            ..Document::default()
1658        }
1659    }
1660
1661    fn source_node(
1662        source_node_index: usize,
1663        parent_source_node_index: Option<usize>,
1664        bone: Option<BoneId>,
1665    ) -> SourceNodeAsset {
1666        SourceNodeAsset {
1667            source_node_index,
1668            name: None,
1669            parent_source_node_index,
1670            scene_root_indices: Vec::new(),
1671            local_rest: SourceNodeLocalRest::Trs {
1672                translation: Vec3::ZERO,
1673                rotation: Quat::IDENTITY,
1674                scale: Vec3::ONE,
1675            },
1676            bone,
1677        }
1678    }
1679
1680    fn valid_track() -> Track {
1681        Track {
1682            bone: 0,
1683            property: Property::Translation,
1684            interpolation: Interpolation::Linear,
1685            times: vec![0.0],
1686            values: TrackValues::Vec3s(vec![Vec3::ZERO]),
1687        }
1688    }
1689
1690    fn track_document(track: Track) -> Document {
1691        let mut document = one_bone_document();
1692        document.clips.push(Clip {
1693            name: "clip".into(),
1694            duration_s: 0.0,
1695            tracks: vec![track],
1696        });
1697        document
1698    }
1699
1700    fn instance_document() -> Document {
1701        let mut document = one_bone_document();
1702        document.assets.meshes.push(MeshAsset::default());
1703        document.assets.instances.push(MeshInstance {
1704            node: 0,
1705            mesh: 0,
1706            ..MeshInstance::default()
1707        });
1708        document
1709    }
1710
1711    #[test]
1712    fn document_shape_validation_accepts_a_complete_projection_with_an_unprojected_intermediate() {
1713        let mut document = Document {
1714            skeleton: Skeleton {
1715                bones: vec![bone(None), bone(Some(0))],
1716            },
1717            assets: SceneAssets {
1718                source_skeleton: SourceSkeletonAssets {
1719                    coverage: SourceSkeletonCoverage::Complete,
1720                    nodes: vec![
1721                        source_node(10, None, Some(0)),
1722                        source_node(11, Some(10), None),
1723                        source_node(12, Some(11), Some(1)),
1724                    ],
1725                    ..SourceSkeletonAssets::default()
1726                },
1727                meshes: vec![MeshAsset::default()],
1728                instances: vec![MeshInstance {
1729                    node: 1,
1730                    mesh: 0,
1731                    skin_joints: vec![0, 1],
1732                    skin_ibms: vec![Mat4::IDENTITY, Mat4::IDENTITY],
1733                    ..MeshInstance::default()
1734                }],
1735                ..SceneAssets::default()
1736            },
1737            ..Document::default()
1738        };
1739        document.clips.push(Clip {
1740            name: "clip".into(),
1741            duration_s: 0.0,
1742            tracks: vec![valid_track()],
1743        });
1744
1745        assert_eq!(validate_document_shape(&document), Ok(()));
1746    }
1747
1748    #[test]
1749    fn shared_unprojected_parent_suffix_preserves_each_projected_parent() {
1750        const CONNECTORS: usize = 64;
1751        const PROJECTED_CHILDREN: usize = 64;
1752
1753        let mut nodes = Vec::with_capacity(1 + CONNECTORS + PROJECTED_CHILDREN);
1754        nodes.push(source_node(0, None, Some(0)));
1755        for source_node_index in 1..=CONNECTORS {
1756            nodes.push(source_node(
1757                source_node_index,
1758                Some(source_node_index - 1),
1759                None,
1760            ));
1761        }
1762        for child in 0..PROJECTED_CHILDREN {
1763            nodes.push(source_node(
1764                1 + CONNECTORS + child,
1765                Some(CONNECTORS),
1766                Some(1 + child),
1767            ));
1768        }
1769        let document = Document {
1770            skeleton: Skeleton {
1771                bones: std::iter::once(bone(None))
1772                    .chain((0..PROJECTED_CHILDREN).map(|_| bone(Some(0))))
1773                    .collect(),
1774            },
1775            assets: SceneAssets {
1776                source_skeleton: SourceSkeletonAssets {
1777                    coverage: SourceSkeletonCoverage::Complete,
1778                    nodes,
1779                    ..SourceSkeletonAssets::default()
1780                },
1781                ..SceneAssets::default()
1782            },
1783            ..Document::default()
1784        };
1785
1786        assert_eq!(validate_document_shape(&document), Ok(()));
1787        let mut mismatched = document.clone();
1788        mismatched.skeleton.bones[PROJECTED_CHILDREN].parent = None;
1789        assert_eq!(
1790            validate_document_shape(&mismatched),
1791            Err(DocumentShapeError::SourceProjection {
1792                source_node_index: CONNECTORS + PROJECTED_CHILDREN,
1793                violation: SourceProjectionViolation::NearestProjectedParentMismatch,
1794            })
1795        );
1796    }
1797
1798    #[test]
1799    fn document_shape_validation_has_an_analytic_error_for_every_variant() {
1800        let projection_error =
1801            |source_node_index, violation| DocumentShapeError::SourceProjection {
1802                source_node_index,
1803                violation,
1804            };
1805        let track_error = |node, violation| DocumentShapeError::TrackShape {
1806            clip_index: 0,
1807            node,
1808            violation,
1809        };
1810        let instance_error = |violation| DocumentShapeError::MeshInstanceShape {
1811            instance_index: 0,
1812            violation,
1813        };
1814
1815        let mut non_finite_rest = one_bone_document();
1816        non_finite_rest.skeleton.bones[0].rest.translation.x = f32::NAN;
1817        let overflowed_rest_world = Document {
1818            skeleton: Skeleton {
1819                bones: vec![
1820                    Bone {
1821                        rest: Transform {
1822                            scale: Vec3::splat(f32::MAX),
1823                            ..Transform::IDENTITY
1824                        },
1825                        ..bone(None)
1826                    },
1827                    Bone {
1828                        rest: Transform {
1829                            translation: Vec3::splat(2.0),
1830                            ..Transform::IDENTITY
1831                        },
1832                        ..bone(Some(0))
1833                    },
1834                ],
1835            },
1836            ..Document::default()
1837        };
1838        let self_parent = Document {
1839            skeleton: Skeleton {
1840                bones: vec![bone(Some(0))],
1841            },
1842            ..Document::default()
1843        };
1844        let forward_parent = Document {
1845            skeleton: Skeleton {
1846                bones: vec![bone(Some(1)), bone(None)],
1847            },
1848            ..Document::default()
1849        };
1850        let far_parent = Document {
1851            skeleton: Skeleton {
1852                bones: vec![bone(Some(99))],
1853            },
1854            ..Document::default()
1855        };
1856        let duplicate_node = Document {
1857            assets: SceneAssets {
1858                source_skeleton: SourceSkeletonAssets {
1859                    nodes: vec![
1860                        source_node(9, None, None),
1861                        source_node(10, None, None),
1862                        source_node(9, None, None),
1863                    ],
1864                    ..SourceSkeletonAssets::default()
1865                },
1866                ..SceneAssets::default()
1867            },
1868            ..Document::default()
1869        };
1870        let duplicate_skin = Document {
1871            assets: SceneAssets {
1872                source_skeleton: SourceSkeletonAssets {
1873                    skins: vec![
1874                        SourceSkinAsset {
1875                            source_skin_index: 4,
1876                            ..SourceSkinAsset::default()
1877                        },
1878                        SourceSkinAsset {
1879                            source_skin_index: 5,
1880                            ..SourceSkinAsset::default()
1881                        },
1882                        SourceSkinAsset {
1883                            source_skin_index: 4,
1884                            ..SourceSkinAsset::default()
1885                        },
1886                    ],
1887                    ..SourceSkeletonAssets::default()
1888                },
1889                ..SceneAssets::default()
1890            },
1891            ..Document::default()
1892        };
1893        let complete_projection = |nodes| SceneAssets {
1894            source_skeleton: SourceSkeletonAssets {
1895                coverage: SourceSkeletonCoverage::Complete,
1896                nodes,
1897                ..SourceSkeletonAssets::default()
1898            },
1899            ..SceneAssets::default()
1900        };
1901        let out_of_range_projection = Document {
1902            skeleton: Skeleton {
1903                bones: vec![bone(None)],
1904            },
1905            assets: complete_projection(vec![source_node(10, None, Some(1))]),
1906            ..Document::default()
1907        };
1908        let non_injective_projection = Document {
1909            skeleton: Skeleton {
1910                bones: vec![bone(None)],
1911            },
1912            assets: complete_projection(vec![
1913                source_node(10, None, Some(0)),
1914                source_node(11, None, Some(0)),
1915            ]),
1916            ..Document::default()
1917        };
1918        let missing_projection_parent = Document {
1919            skeleton: Skeleton {
1920                bones: vec![bone(None), bone(Some(0))],
1921            },
1922            assets: complete_projection(vec![source_node(11, Some(99), Some(1))]),
1923            ..Document::default()
1924        };
1925        // Exactly one unprojected row lies between projected child 11 and the
1926        // genuinely missing parent 99. The strict `> unprojected_rows` guard
1927        // must preserve the missing-parent classification; `>=` reports a
1928        // cycle at this exact boundary instead.
1929        let missing_projection_parent_at_cycle_bound = Document {
1930            skeleton: Skeleton {
1931                bones: vec![bone(None), bone(Some(0))],
1932            },
1933            assets: complete_projection(vec![
1934                source_node(10, None, Some(0)),
1935                source_node(11, Some(12), Some(1)),
1936                source_node(12, Some(99), None),
1937            ]),
1938            ..Document::default()
1939        };
1940        let cyclic_unprojected_parent = Document {
1941            skeleton: Skeleton {
1942                bones: vec![bone(None), bone(Some(0))],
1943            },
1944            assets: complete_projection(vec![
1945                source_node(11, Some(12), Some(1)),
1946                source_node(12, Some(12), None),
1947            ]),
1948            ..Document::default()
1949        };
1950        let cyclic_unprojected_parent_pair = Document {
1951            skeleton: Skeleton {
1952                bones: vec![bone(None), bone(Some(0))],
1953            },
1954            assets: complete_projection(vec![
1955                source_node(11, Some(12), Some(1)),
1956                source_node(12, Some(13), None),
1957                source_node(13, Some(12), None),
1958            ]),
1959            ..Document::default()
1960        };
1961        let mismatched_nearest_parent = Document {
1962            skeleton: Skeleton {
1963                bones: vec![bone(None), bone(Some(0))],
1964            },
1965            assets: complete_projection(vec![
1966                source_node(10, None, Some(0)),
1967                source_node(11, None, Some(1)),
1968            ]),
1969            ..Document::default()
1970        };
1971        let unprojected_child = Document {
1972            skeleton: Skeleton {
1973                bones: vec![bone(None), bone(Some(0))],
1974            },
1975            assets: complete_projection(vec![source_node(10, None, Some(0))]),
1976            ..Document::default()
1977        };
1978
1979        let duplicate_track = {
1980            let track = valid_track();
1981            let mut document = track_document(track.clone());
1982            document.clips[0].tracks.push(Track {
1983                property: Property::Scale,
1984                ..valid_track()
1985            });
1986            document.clips[0].tracks.push(track);
1987            document
1988        };
1989        let mut boundary_out_of_range_track = valid_track();
1990        boundary_out_of_range_track.bone = 1;
1991        let mut far_out_of_range_track = valid_track();
1992        far_out_of_range_track.bone = 99;
1993        let empty_track = Track {
1994            times: Vec::new(),
1995            values: TrackValues::Vec3s(Vec::new()),
1996            ..valid_track()
1997        };
1998        let non_finite_later_time = Track {
1999            times: vec![0.0, f32::NAN],
2000            values: TrackValues::Vec3s(vec![Vec3::ZERO, Vec3::ZERO]),
2001            ..valid_track()
2002        };
2003        let unordered_times = Track {
2004            times: vec![1.0, 0.0],
2005            values: TrackValues::Vec3s(vec![Vec3::ZERO, Vec3::ZERO]),
2006            ..valid_track()
2007        };
2008        let equal_times = Track {
2009            times: vec![0.0, 0.0],
2010            values: TrackValues::Vec3s(vec![Vec3::ZERO, Vec3::ZERO]),
2011            ..valid_track()
2012        };
2013        let wrong_linear_value_count = Track {
2014            values: TrackValues::Vec3s(Vec::new()),
2015            ..valid_track()
2016        };
2017        let excess_linear_value_count = Track {
2018            values: TrackValues::Vec3s(vec![Vec3::ZERO, Vec3::ZERO]),
2019            ..valid_track()
2020        };
2021        let wrong_step_value_count = Track {
2022            interpolation: Interpolation::Step,
2023            times: vec![0.0, 1.0],
2024            values: TrackValues::Vec3s(vec![Vec3::ZERO]),
2025            ..valid_track()
2026        };
2027        let excess_step_value_count = Track {
2028            interpolation: Interpolation::Step,
2029            values: TrackValues::Vec3s(vec![Vec3::ZERO, Vec3::ZERO]),
2030            ..valid_track()
2031        };
2032        let wrong_cubic_value_count = Track {
2033            interpolation: Interpolation::CubicSpline,
2034            times: vec![0.0, 1.0],
2035            values: TrackValues::Vec3s(vec![Vec3::ZERO; 4]),
2036            ..valid_track()
2037        };
2038        let excess_cubic_value_count = Track {
2039            interpolation: Interpolation::CubicSpline,
2040            values: TrackValues::Vec3s(vec![Vec3::ZERO; 4]),
2041            ..valid_track()
2042        };
2043        let wrong_translation_value_type = Track {
2044            values: TrackValues::Quats(vec![Quat::IDENTITY]),
2045            ..valid_track()
2046        };
2047        let wrong_scale_value_type = Track {
2048            property: Property::Scale,
2049            values: TrackValues::Quats(vec![Quat::IDENTITY]),
2050            ..valid_track()
2051        };
2052        let wrong_rotation_value_type = Track {
2053            property: Property::Rotation,
2054            values: TrackValues::Vec3s(vec![Vec3::ZERO]),
2055            ..valid_track()
2056        };
2057        let non_finite_value = Track {
2058            values: TrackValues::Vec3s(vec![Vec3::splat(f32::NAN)]),
2059            ..valid_track()
2060        };
2061
2062        let mut bad_instance_node = instance_document();
2063        bad_instance_node.assets.instances[0].node = 1;
2064        let mut far_instance_node = instance_document();
2065        far_instance_node.assets.instances[0].node = 99;
2066        let mut bad_instance_mesh = instance_document();
2067        bad_instance_mesh.assets.instances[0].mesh = 1;
2068        let mut far_instance_mesh = instance_document();
2069        far_instance_mesh.assets.instances[0].mesh = 99;
2070        let mut bad_instance_joint = instance_document();
2071        bad_instance_joint.assets.instances[0].skin_joints = vec![1];
2072        let mut far_instance_joint = instance_document();
2073        far_instance_joint.assets.instances[0].skin_joints = vec![99];
2074        let mut bad_instance_count = instance_document();
2075        bad_instance_count.assets.instances[0].skin_joints = vec![0];
2076        bad_instance_count.assets.instances[0].skin_ibms = vec![Mat4::IDENTITY, Mat4::IDENTITY];
2077        let mut short_instance_count = instance_document();
2078        short_instance_count.skeleton.bones.push(bone(Some(0)));
2079        short_instance_count.assets.instances[0].skin_joints = vec![0, 1];
2080        short_instance_count.assets.instances[0].skin_ibms = vec![Mat4::IDENTITY];
2081        let mut bad_instance_ibm = instance_document();
2082        bad_instance_ibm.assets.instances[0].skin_joints = vec![0];
2083        bad_instance_ibm.assets.instances[0].skin_ibms =
2084            vec![Mat4::from_cols_array(&[f32::NAN; 16])];
2085        let mut bad_bone_ibm = one_bone_document();
2086        bad_bone_ibm.skeleton.bones[0].inverse_bind = Some(Mat4::from_cols_array(&[f32::NAN; 16]));
2087
2088        let cases = vec![
2089            (
2090                "non-finite rest",
2091                non_finite_rest,
2092                DocumentShapeError::NonFiniteSkeletonRest { node: 0 },
2093            ),
2094            (
2095                "non-finite composed rest world",
2096                overflowed_rest_world,
2097                DocumentShapeError::NonFiniteSkeletonRest { node: 1 },
2098            ),
2099            (
2100                "self parent",
2101                self_parent,
2102                DocumentShapeError::InvalidSkeletonParent { node: 0, parent: 0 },
2103            ),
2104            (
2105                "forward parent",
2106                forward_parent,
2107                DocumentShapeError::InvalidSkeletonParent { node: 0, parent: 1 },
2108            ),
2109            (
2110                "far parent",
2111                far_parent,
2112                DocumentShapeError::InvalidSkeletonParent {
2113                    node: 0,
2114                    parent: 99,
2115                },
2116            ),
2117            (
2118                "duplicate source node",
2119                duplicate_node,
2120                DocumentShapeError::DuplicateSourceNodeIndex {
2121                    source_node_index: 9,
2122                },
2123            ),
2124            (
2125                "duplicate source skin",
2126                duplicate_skin,
2127                DocumentShapeError::DuplicateSourceSkinIndex {
2128                    source_skin_index: 4,
2129                },
2130            ),
2131            (
2132                "projected bone range",
2133                out_of_range_projection,
2134                projection_error(10, SourceProjectionViolation::ProjectedBoneOutOfRange),
2135            ),
2136            (
2137                "projection injectivity",
2138                non_injective_projection,
2139                projection_error(
2140                    11,
2141                    SourceProjectionViolation::TwoSourceNodesProjectToOneBone,
2142                ),
2143            ),
2144            (
2145                "missing projection parent",
2146                missing_projection_parent,
2147                projection_error(11, SourceProjectionViolation::ParentSourceNodeMissing),
2148            ),
2149            (
2150                "missing projection parent at cycle bound",
2151                missing_projection_parent_at_cycle_bound,
2152                projection_error(11, SourceProjectionViolation::ParentSourceNodeMissing),
2153            ),
2154            (
2155                "cyclic projection parent",
2156                cyclic_unprojected_parent,
2157                projection_error(
2158                    11,
2159                    SourceProjectionViolation::CyclicUnprojectedSourceParentChain,
2160                ),
2161            ),
2162            (
2163                "cyclic projection parent pair",
2164                cyclic_unprojected_parent_pair,
2165                projection_error(
2166                    11,
2167                    SourceProjectionViolation::CyclicUnprojectedSourceParentChain,
2168                ),
2169            ),
2170            (
2171                "nearest projection parent",
2172                mismatched_nearest_parent,
2173                projection_error(
2174                    11,
2175                    SourceProjectionViolation::NearestProjectedParentMismatch,
2176                ),
2177            ),
2178            (
2179                "projection downward closure",
2180                unprojected_child,
2181                projection_error(
2182                    10,
2183                    SourceProjectionViolation::ProjectedBoneHasUnprojectedSkeletonChild,
2184                ),
2185            ),
2186            (
2187                "duplicate track",
2188                duplicate_track,
2189                DocumentShapeError::DuplicateClipTrack {
2190                    clip_index: 0,
2191                    node: 0,
2192                    property: Property::Translation,
2193                },
2194            ),
2195            (
2196                "track bone range boundary",
2197                track_document(boundary_out_of_range_track),
2198                track_error(1, TrackShapeViolation::BoneIndexOutOfRange),
2199            ),
2200            (
2201                "track bone range far",
2202                track_document(far_out_of_range_track),
2203                track_error(99, TrackShapeViolation::BoneIndexOutOfRange),
2204            ),
2205            (
2206                "empty track",
2207                track_document(empty_track),
2208                track_error(0, TrackShapeViolation::EmptyTimes),
2209            ),
2210            (
2211                "non-finite time",
2212                track_document(non_finite_later_time),
2213                track_error(0, TrackShapeViolation::NonFiniteTime),
2214            ),
2215            (
2216                "unordered times",
2217                track_document(unordered_times),
2218                track_error(0, TrackShapeViolation::TimesNotStrictlyIncreasing),
2219            ),
2220            (
2221                "equal times",
2222                track_document(equal_times),
2223                track_error(0, TrackShapeViolation::TimesNotStrictlyIncreasing),
2224            ),
2225            (
2226                "linear value count",
2227                track_document(wrong_linear_value_count),
2228                track_error(0, TrackShapeViolation::ValueCountMismatch),
2229            ),
2230            (
2231                "linear excess value count",
2232                track_document(excess_linear_value_count),
2233                track_error(0, TrackShapeViolation::ValueCountMismatch),
2234            ),
2235            (
2236                "step value count",
2237                track_document(wrong_step_value_count),
2238                track_error(0, TrackShapeViolation::ValueCountMismatch),
2239            ),
2240            (
2241                "step excess value count",
2242                track_document(excess_step_value_count),
2243                track_error(0, TrackShapeViolation::ValueCountMismatch),
2244            ),
2245            (
2246                "cubic value count",
2247                track_document(wrong_cubic_value_count),
2248                track_error(0, TrackShapeViolation::ValueCountMismatch),
2249            ),
2250            (
2251                "cubic excess value count",
2252                track_document(excess_cubic_value_count),
2253                track_error(0, TrackShapeViolation::ValueCountMismatch),
2254            ),
2255            (
2256                "translation value type",
2257                track_document(wrong_translation_value_type),
2258                track_error(0, TrackShapeViolation::ValueTypeMismatchesProperty),
2259            ),
2260            (
2261                "scale value type",
2262                track_document(wrong_scale_value_type),
2263                track_error(0, TrackShapeViolation::ValueTypeMismatchesProperty),
2264            ),
2265            (
2266                "rotation value type",
2267                track_document(wrong_rotation_value_type),
2268                track_error(0, TrackShapeViolation::ValueTypeMismatchesProperty),
2269            ),
2270            (
2271                "non-finite value",
2272                track_document(non_finite_value),
2273                track_error(0, TrackShapeViolation::NonFiniteValue),
2274            ),
2275            (
2276                "instance node boundary",
2277                bad_instance_node,
2278                instance_error(MeshInstanceShapeViolation::NodeIndexOutOfRange),
2279            ),
2280            (
2281                "instance node far",
2282                far_instance_node,
2283                instance_error(MeshInstanceShapeViolation::NodeIndexOutOfRange),
2284            ),
2285            (
2286                "instance mesh boundary",
2287                bad_instance_mesh,
2288                instance_error(MeshInstanceShapeViolation::MeshIndexOutOfRange),
2289            ),
2290            (
2291                "instance mesh far",
2292                far_instance_mesh,
2293                instance_error(MeshInstanceShapeViolation::MeshIndexOutOfRange),
2294            ),
2295            (
2296                "instance joint boundary",
2297                bad_instance_joint,
2298                instance_error(MeshInstanceShapeViolation::SkinJointOutOfRange),
2299            ),
2300            (
2301                "instance joint far",
2302                far_instance_joint,
2303                instance_error(MeshInstanceShapeViolation::SkinJointOutOfRange),
2304            ),
2305            (
2306                "instance ibm count excess",
2307                bad_instance_count,
2308                instance_error(MeshInstanceShapeViolation::SkinInverseBindCountMismatch),
2309            ),
2310            (
2311                "instance ibm count short",
2312                short_instance_count,
2313                instance_error(MeshInstanceShapeViolation::SkinInverseBindCountMismatch),
2314            ),
2315            (
2316                "instance ibm finite",
2317                bad_instance_ibm,
2318                instance_error(MeshInstanceShapeViolation::NonFiniteSkinInverseBind),
2319            ),
2320            (
2321                "bone ibm finite",
2322                bad_bone_ibm,
2323                DocumentShapeError::NonFiniteBoneInverseBind { node: 0 },
2324            ),
2325        ];
2326        for (name, document, expected) in cases {
2327            assert_eq!(validate_document_shape(&document), Err(expected), "{name}");
2328        }
2329    }
2330
2331    #[test]
2332    fn document_shape_finiteness_checks_every_stored_component() {
2333        for component in 0..3 {
2334            let mut translation = Vec3::ZERO.to_array();
2335            translation[component] = f32::NAN;
2336            let mut document = one_bone_document();
2337            document.skeleton.bones[0].rest.translation = Vec3::from_array(translation);
2338            assert_eq!(
2339                validate_document_shape(&document),
2340                Err(DocumentShapeError::NonFiniteSkeletonRest { node: 0 }),
2341                "rest translation component {component}"
2342            );
2343
2344            let mut scale = Vec3::ONE.to_array();
2345            scale[component] = f32::NAN;
2346            let mut document = one_bone_document();
2347            document.skeleton.bones[0].rest.scale = Vec3::from_array(scale);
2348            assert_eq!(
2349                validate_document_shape(&document),
2350                Err(DocumentShapeError::NonFiniteSkeletonRest { node: 0 }),
2351                "rest scale component {component}"
2352            );
2353
2354            let mut value = Vec3::ZERO.to_array();
2355            value[component] = f32::NAN;
2356            let document = track_document(Track {
2357                values: TrackValues::Vec3s(vec![Vec3::from_array(value)]),
2358                ..valid_track()
2359            });
2360            assert_eq!(
2361                validate_document_shape(&document),
2362                Err(DocumentShapeError::TrackShape {
2363                    clip_index: 0,
2364                    node: 0,
2365                    violation: TrackShapeViolation::NonFiniteValue,
2366                }),
2367                "track Vec3 component {component}"
2368            );
2369        }
2370
2371        for component in 0..4 {
2372            let mut rotation = Quat::IDENTITY.to_array();
2373            rotation[component] = f32::NAN;
2374            let mut document = one_bone_document();
2375            document.skeleton.bones[0].rest.rotation = Quat::from_array(rotation);
2376            assert_eq!(
2377                validate_document_shape(&document),
2378                Err(DocumentShapeError::NonFiniteSkeletonRest { node: 0 }),
2379                "rest rotation component {component}"
2380            );
2381
2382            let document = track_document(Track {
2383                property: Property::Rotation,
2384                values: TrackValues::Quats(vec![Quat::from_array(rotation)]),
2385                ..valid_track()
2386            });
2387            assert_eq!(
2388                validate_document_shape(&document),
2389                Err(DocumentShapeError::TrackShape {
2390                    clip_index: 0,
2391                    node: 0,
2392                    violation: TrackShapeViolation::NonFiniteValue,
2393                }),
2394                "track quaternion component {component}"
2395            );
2396        }
2397
2398        for key in 0..3 {
2399            let mut times = vec![0.0, 1.0, 2.0];
2400            times[key] = f32::NAN;
2401            let document = track_document(Track {
2402                times,
2403                values: TrackValues::Vec3s(vec![Vec3::ZERO; 3]),
2404                ..valid_track()
2405            });
2406            assert_eq!(
2407                validate_document_shape(&document),
2408                Err(DocumentShapeError::TrackShape {
2409                    clip_index: 0,
2410                    node: 0,
2411                    violation: TrackShapeViolation::NonFiniteTime,
2412                }),
2413                "track time {key}"
2414            );
2415        }
2416
2417        for component in 0..16 {
2418            let mut columns = Mat4::IDENTITY.to_cols_array();
2419            columns[component] = f32::NAN;
2420            let inverse_bind = Mat4::from_cols_array(&columns);
2421
2422            let mut instance_document = instance_document();
2423            instance_document.assets.instances[0].skin_joints = vec![0];
2424            instance_document.assets.instances[0].skin_ibms = vec![inverse_bind];
2425            assert_eq!(
2426                validate_document_shape(&instance_document),
2427                Err(DocumentShapeError::MeshInstanceShape {
2428                    instance_index: 0,
2429                    violation: MeshInstanceShapeViolation::NonFiniteSkinInverseBind,
2430                }),
2431                "instance inverse-bind component {component}"
2432            );
2433
2434            let mut bone_document = one_bone_document();
2435            bone_document.skeleton.bones[0].inverse_bind = Some(inverse_bind);
2436            assert_eq!(
2437                validate_document_shape(&bone_document),
2438                Err(DocumentShapeError::NonFiniteBoneInverseBind { node: 0 }),
2439                "bone inverse-bind component {component}"
2440            );
2441        }
2442    }
2443
2444    #[test]
2445    fn document_shape_rejects_duplicate_tracks_for_every_property() {
2446        let tracks = [
2447            (Property::Translation, TrackValues::Vec3s(vec![Vec3::ZERO])),
2448            (Property::Scale, TrackValues::Vec3s(vec![Vec3::ONE])),
2449            (Property::Rotation, TrackValues::Quats(vec![Quat::IDENTITY])),
2450        ];
2451
2452        for (property, values) in tracks {
2453            let track = Track {
2454                property,
2455                values,
2456                ..valid_track()
2457            };
2458            let mut document = track_document(track.clone());
2459            document.clips[0].tracks.push(track);
2460
2461            assert_eq!(
2462                validate_document_shape(&document),
2463                Err(DocumentShapeError::DuplicateClipTrack {
2464                    clip_index: 0,
2465                    node: 0,
2466                    property,
2467                }),
2468                "duplicate {property:?} track"
2469            );
2470        }
2471    }
2472
2473    #[test]
2474    fn document_shape_rejects_infinite_times_quaternions_and_inverse_binds() {
2475        for non_finite in [f32::INFINITY, f32::NEG_INFINITY] {
2476            let document = track_document(Track {
2477                times: vec![non_finite],
2478                values: TrackValues::Vec3s(vec![Vec3::ZERO]),
2479                ..valid_track()
2480            });
2481            assert_eq!(
2482                validate_document_shape(&document),
2483                Err(DocumentShapeError::TrackShape {
2484                    clip_index: 0,
2485                    node: 0,
2486                    violation: TrackShapeViolation::NonFiniteTime,
2487                }),
2488                "track time {non_finite}"
2489            );
2490
2491            let document = track_document(Track {
2492                property: Property::Rotation,
2493                values: TrackValues::Quats(vec![Quat::from_xyzw(non_finite, 0.0, 0.0, 1.0)]),
2494                ..valid_track()
2495            });
2496            assert_eq!(
2497                validate_document_shape(&document),
2498                Err(DocumentShapeError::TrackShape {
2499                    clip_index: 0,
2500                    node: 0,
2501                    violation: TrackShapeViolation::NonFiniteValue,
2502                }),
2503                "track quaternion {non_finite}"
2504            );
2505
2506            let mut columns = Mat4::IDENTITY.to_cols_array();
2507            columns[0] = non_finite;
2508            let inverse_bind = Mat4::from_cols_array(&columns);
2509            let mut document = instance_document();
2510            document.assets.instances[0].skin_joints = vec![0];
2511            document.assets.instances[0].skin_ibms = vec![inverse_bind];
2512            assert_eq!(
2513                validate_document_shape(&document),
2514                Err(DocumentShapeError::MeshInstanceShape {
2515                    instance_index: 0,
2516                    violation: MeshInstanceShapeViolation::NonFiniteSkinInverseBind,
2517                }),
2518                "instance inverse bind {non_finite}"
2519            );
2520
2521            let mut document = one_bone_document();
2522            document.skeleton.bones[0].inverse_bind = Some(inverse_bind);
2523            assert_eq!(
2524                validate_document_shape(&document),
2525                Err(DocumentShapeError::NonFiniteBoneInverseBind { node: 0 }),
2526                "bone inverse bind {non_finite}"
2527            );
2528        }
2529    }
2530
2531    #[test]
2532    fn document_shape_checks_mesh_and_joint_references_on_later_instances() {
2533        let later_instance = MeshInstance {
2534            node: 0,
2535            mesh: 0,
2536            ..MeshInstance::default()
2537        };
2538
2539        let mut document = instance_document();
2540        document.assets.instances.push(later_instance.clone());
2541        document.assets.instances[1].mesh = 1;
2542        assert_eq!(
2543            validate_document_shape(&document),
2544            Err(DocumentShapeError::MeshInstanceShape {
2545                instance_index: 1,
2546                violation: MeshInstanceShapeViolation::MeshIndexOutOfRange,
2547            })
2548        );
2549
2550        let mut document = instance_document();
2551        document.assets.instances.push(later_instance);
2552        document.assets.instances[1].skin_joints = vec![1];
2553        assert_eq!(
2554            validate_document_shape(&document),
2555            Err(DocumentShapeError::MeshInstanceShape {
2556                instance_index: 1,
2557                violation: MeshInstanceShapeViolation::SkinJointOutOfRange,
2558            })
2559        );
2560    }
2561
2562    #[test]
2563    fn document_shape_finds_duplicates_that_do_not_involve_the_first_item() {
2564        let mut document = Document::default();
2565        document.assets.source_skeleton.skins = [4, 5, 5]
2566            .into_iter()
2567            .map(|source_skin_index| SourceSkinAsset {
2568                source_skin_index,
2569                ..SourceSkinAsset::default()
2570            })
2571            .collect();
2572        assert_eq!(
2573            validate_document_shape(&document),
2574            Err(DocumentShapeError::DuplicateSourceSkinIndex {
2575                source_skin_index: 5,
2576            })
2577        );
2578
2579        let scale_track = Track {
2580            property: Property::Scale,
2581            values: TrackValues::Vec3s(vec![Vec3::ONE]),
2582            ..valid_track()
2583        };
2584        let mut document = track_document(valid_track());
2585        document.clips[0].tracks.push(scale_track.clone());
2586        document.clips[0].tracks.push(scale_track);
2587        assert_eq!(
2588            validate_document_shape(&document),
2589            Err(DocumentShapeError::DuplicateClipTrack {
2590                clip_index: 0,
2591                node: 0,
2592                property: Property::Scale,
2593            })
2594        );
2595    }
2596
2597    #[test]
2598    fn document_shape_checks_later_tracks_and_inverse_binds() {
2599        let mut document = track_document(valid_track());
2600        document.clips[0].tracks.push(Track {
2601            property: Property::Scale,
2602            times: Vec::new(),
2603            values: TrackValues::Vec3s(Vec::new()),
2604            ..valid_track()
2605        });
2606        assert_eq!(
2607            validate_document_shape(&document),
2608            Err(DocumentShapeError::TrackShape {
2609                clip_index: 0,
2610                node: 0,
2611                violation: TrackShapeViolation::EmptyTimes,
2612            })
2613        );
2614
2615        let scale_track = Track {
2616            property: Property::Scale,
2617            values: TrackValues::Vec3s(vec![Vec3::ONE]),
2618            ..valid_track()
2619        };
2620        let mut document = track_document(valid_track());
2621        document.clips.push(Clip {
2622            name: "later".into(),
2623            duration_s: 0.0,
2624            tracks: vec![scale_track.clone(), scale_track],
2625        });
2626        assert_eq!(
2627            validate_document_shape(&document),
2628            Err(DocumentShapeError::DuplicateClipTrack {
2629                clip_index: 1,
2630                node: 0,
2631                property: Property::Scale,
2632            })
2633        );
2634
2635        let mut document = track_document(valid_track());
2636        document.clips.push(Clip {
2637            name: "later".into(),
2638            duration_s: 0.0,
2639            tracks: vec![Track {
2640                property: Property::Scale,
2641                times: Vec::new(),
2642                values: TrackValues::Vec3s(Vec::new()),
2643                ..valid_track()
2644            }],
2645        });
2646        assert_eq!(
2647            validate_document_shape(&document),
2648            Err(DocumentShapeError::TrackShape {
2649                clip_index: 1,
2650                node: 0,
2651                violation: TrackShapeViolation::EmptyTimes,
2652            })
2653        );
2654
2655        let mut columns = Mat4::IDENTITY.to_cols_array();
2656        columns[15] = f32::NAN;
2657        let non_finite_inverse_bind = Mat4::from_cols_array(&columns);
2658
2659        let mut document = instance_document();
2660        document.skeleton.bones.push(bone(Some(0)));
2661        document.assets.instances[0].skin_joints = vec![0, 1];
2662        document.assets.instances[0].skin_ibms = vec![Mat4::IDENTITY, non_finite_inverse_bind];
2663        assert_eq!(
2664            validate_document_shape(&document),
2665            Err(DocumentShapeError::MeshInstanceShape {
2666                instance_index: 0,
2667                violation: MeshInstanceShapeViolation::NonFiniteSkinInverseBind,
2668            })
2669        );
2670
2671        let mut document = instance_document();
2672        document.assets.instances.push(MeshInstance {
2673            node: 0,
2674            mesh: 0,
2675            skin_joints: vec![0],
2676            skin_ibms: vec![non_finite_inverse_bind],
2677            ..MeshInstance::default()
2678        });
2679        assert_eq!(
2680            validate_document_shape(&document),
2681            Err(DocumentShapeError::MeshInstanceShape {
2682                instance_index: 1,
2683                violation: MeshInstanceShapeViolation::NonFiniteSkinInverseBind,
2684            })
2685        );
2686
2687        let mut document = instance_document();
2688        document.assets.instances.push(MeshInstance {
2689            node: 0,
2690            mesh: 0,
2691            skin_joints: vec![0],
2692            skin_ibms: vec![Mat4::IDENTITY, Mat4::IDENTITY],
2693            ..MeshInstance::default()
2694        });
2695        assert_eq!(
2696            validate_document_shape(&document),
2697            Err(DocumentShapeError::MeshInstanceShape {
2698                instance_index: 1,
2699                violation: MeshInstanceShapeViolation::SkinInverseBindCountMismatch,
2700            })
2701        );
2702
2703        let mut document = one_bone_document();
2704        document.skeleton.bones.push(Bone {
2705            inverse_bind: Some(non_finite_inverse_bind),
2706            ..bone(Some(0))
2707        });
2708        assert_eq!(
2709            validate_document_shape(&document),
2710            Err(DocumentShapeError::NonFiniteBoneInverseBind { node: 1 })
2711        );
2712    }
2713
2714    #[test]
2715    fn document_shape_violation_names_remain_machine_stable() {
2716        let source_projection = [
2717            (
2718                SourceProjectionViolation::ProjectedBoneOutOfRange,
2719                "projected_bone_out_of_range",
2720            ),
2721            (
2722                SourceProjectionViolation::TwoSourceNodesProjectToOneBone,
2723                "two_source_nodes_project_to_one_bone",
2724            ),
2725            (
2726                SourceProjectionViolation::ParentSourceNodeMissing,
2727                "parent_source_node_is_missing",
2728            ),
2729            (
2730                SourceProjectionViolation::CyclicUnprojectedSourceParentChain,
2731                "cyclic_unprojected_source_parent_chain",
2732            ),
2733            (
2734                SourceProjectionViolation::NearestProjectedParentMismatch,
2735                "projection_and_skeleton_parents_differ",
2736            ),
2737            (
2738                SourceProjectionViolation::ProjectedBoneHasUnprojectedSkeletonChild,
2739                "projected_bone_has_an_unprojected_skeleton_child",
2740            ),
2741        ];
2742        for (violation, expected) in source_projection {
2743            assert_eq!(violation.to_string(), expected);
2744        }
2745
2746        let track = [
2747            (
2748                TrackShapeViolation::BoneIndexOutOfRange,
2749                "bone_index_out_of_range",
2750            ),
2751            (TrackShapeViolation::EmptyTimes, "empty_times"),
2752            (TrackShapeViolation::NonFiniteTime, "non_finite_time"),
2753            (
2754                TrackShapeViolation::TimesNotStrictlyIncreasing,
2755                "times_not_strictly_increasing",
2756            ),
2757            (
2758                TrackShapeViolation::ValueCountMismatch,
2759                "value_count_mismatch",
2760            ),
2761            (
2762                TrackShapeViolation::ValueTypeMismatchesProperty,
2763                "value_type_mismatches_property",
2764            ),
2765            (TrackShapeViolation::NonFiniteValue, "non_finite_value"),
2766        ];
2767        for (violation, expected) in track {
2768            assert_eq!(violation.to_string(), expected);
2769        }
2770
2771        let instance = [
2772            (
2773                MeshInstanceShapeViolation::NodeIndexOutOfRange,
2774                "node_index_out_of_range",
2775            ),
2776            (
2777                MeshInstanceShapeViolation::MeshIndexOutOfRange,
2778                "mesh_index_out_of_range",
2779            ),
2780            (
2781                MeshInstanceShapeViolation::SkinJointOutOfRange,
2782                "skin_joint_out_of_range",
2783            ),
2784            (
2785                MeshInstanceShapeViolation::SkinInverseBindCountMismatch,
2786                "skin_ibm_count_mismatch",
2787            ),
2788            (
2789                MeshInstanceShapeViolation::NonFiniteSkinInverseBind,
2790                "non_finite_inverse_bind",
2791            ),
2792        ];
2793        for (violation, expected) in instance {
2794            assert_eq!(violation.to_string(), expected);
2795        }
2796    }
2797
2798    #[test]
2799    fn tolerant_world_rests_keep_unrelated_partial_evidence() {
2800        let skeleton = Skeleton {
2801            bones: vec![
2802                bone(None),
2803                bone(Some(99)),
2804                Bone {
2805                    rest: Transform {
2806                        translation: Vec3::X,
2807                        ..Transform::IDENTITY
2808                    },
2809                    ..bone(None)
2810                },
2811                Bone {
2812                    rest: Transform {
2813                        translation: Vec3::Y,
2814                        ..Transform::IDENTITY
2815                    },
2816                    ..bone(Some(2))
2817                },
2818                bone(Some(1)),
2819            ],
2820        };
2821
2822        let worlds = tolerant_world_rest_matrices(&skeleton);
2823        assert_eq!(worlds.len(), 5);
2824        assert_eq!(worlds[0], Some(Mat4::IDENTITY));
2825        assert_eq!(worlds[1], None, "the malformed parent is unavailable");
2826        assert_eq!(worlds[2], Some(Mat4::from_translation(Vec3::X)));
2827        assert_eq!(
2828            worlds[3],
2829            Some(Mat4::from_translation(Vec3::new(1.0, 1.0, 0.0))),
2830            "a finite independent chain remains measurable"
2831        );
2832        assert_eq!(
2833            worlds[4], None,
2834            "a child of unavailable evidence is unavailable"
2835        );
2836    }
2837
2838    #[test]
2839    fn shared_affine_classifier_respects_distinct_caller_tolerances() {
2840        let equal_axis_basis = affine_test_fixtures::tolerance_divergence_basis();
2841        let strict = PositiveUniformAffineTolerance {
2842            equal_axis: 1.0e-5,
2843            relative_orthogonality: 1.0e-5,
2844            singular_determinant_relative: 1.0e-6,
2845        };
2846        let loose = PositiveUniformAffineTolerance {
2847            equal_axis: 1.0e-4,
2848            relative_orthogonality: 1.0e-4,
2849            singular_determinant_relative: 0.0,
2850        };
2851
2852        assert_eq!(
2853            classify_positive_uniform_affine(equal_axis_basis, strict),
2854            Err(AffineDomainViolation::NonUniformScale),
2855            "the stricter caller rejects this equal-axis difference"
2856        );
2857        assert!(
2858            classify_positive_uniform_affine(equal_axis_basis, loose).is_ok(),
2859            "the looser caller accepts this equal-axis difference"
2860        );
2861
2862        let orthogonality_basis = affine_test_fixtures::orthogonality_tolerance_divergence_basis();
2863        assert_eq!(
2864            classify_positive_uniform_affine(orthogonality_basis, strict),
2865            Err(AffineDomainViolation::Sheared),
2866            "the stricter caller rejects this cross-axis dot product"
2867        );
2868        assert!(
2869            classify_positive_uniform_affine(orthogonality_basis, loose).is_ok(),
2870            "the looser caller accepts this cross-axis dot product"
2871        );
2872    }
2873
2874    #[test]
2875    fn shared_affine_classifier_pins_its_symmetric_f64_formula() {
2876        let policy = PositiveUniformAffineTolerance {
2877            equal_axis: 1.0e-5,
2878            relative_orthogonality: 1.0e-5,
2879            singular_determinant_relative: 1.0e-6,
2880        };
2881
2882        // Exact binary32 lengths whose mean is exactly 99_999 in binary64.
2883        // The longest-axis deviation is exactly 1: accepted only when the
2884        // relative base is max(mean, axis), then refused one binary32 ulp
2885        // farther out.
2886        let on_long_edge = Mat3::from_diagonal(Vec3::new(99_998.5, 99_998.5, 100_000.0));
2887        assert_eq!(
2888            classify_positive_uniform_affine(on_long_edge, policy),
2889            Ok(99_999.0)
2890        );
2891        let short = 99_998.5;
2892        let long = 100_000.0 + 0.007_812_5;
2893        for diagonal in [
2894            Vec3::new(long, short, short),
2895            Vec3::new(short, long, short),
2896            Vec3::new(short, short, long),
2897        ] {
2898            assert_eq!(
2899                classify_positive_uniform_affine(Mat3::from_diagonal(diagonal), policy),
2900                Err(AffineDomainViolation::NonUniformScale)
2901            );
2902        }
2903
2904        // A one-sided comparison would miss the uniquely short axis. At this
2905        // exact binary32 step the short-axis deviation is outside the band,
2906        // while each longer axis remains inside it.
2907        let short = 1.0 - 2.0_f32.powi(-16);
2908        for diagonal in [
2909            Vec3::new(short, 1.0, 1.0),
2910            Vec3::new(1.0, short, 1.0),
2911            Vec3::new(1.0, 1.0, short),
2912        ] {
2913            assert_eq!(
2914                classify_positive_uniform_affine(Mat3::from_diagonal(diagonal), policy),
2915                Err(AffineDomainViolation::NonUniformScale)
2916            );
2917        }
2918
2919        // Only binary64 dot-product arithmetic places this basis outside the
2920        // orthogonality band; binary32 rounds the deciding dot back inside.
2921        let c0 = Vec3::new(0.12792248, -0.99066633, -0.047073245);
2922        let c1 = Vec3::new(-0.34637994, -0.00016034879, -0.93809813);
2923        let c2 = Vec3::new(0.92933476, 0.13630849, -0.3431568);
2924        assert!((c1.dot(c2) as f64).abs() < 1.0e-5);
2925        assert!(c1.as_dvec3().dot(c2.as_dvec3()).abs() > 1.0e-5);
2926        assert_eq!(
2927            classify_positive_uniform_affine(Mat3::from_cols(c0, c1, c2), policy),
2928            Err(AffineDomainViolation::Sheared)
2929        );
2930
2931        // Orthogonality is sign-independent; dropping abs() accepts the
2932        // negative case while leaving the positive fixture green.
2933        for shear in [2.0_f32.powi(-15), -2.0_f32.powi(-15)] {
2934            let basis = Mat3::from_cols(Vec3::X, Vec3::new(shear, 1.0, 0.0), Vec3::Z);
2935            assert_eq!(
2936                classify_positive_uniform_affine(basis, policy),
2937                Err(AffineDomainViolation::Sheared)
2938            );
2939        }
2940    }
2941
2942    #[test]
2943    fn affine_axis_mean_is_ascending_and_column_order_invariant() {
2944        // This is the audited counterexample. These are the widened lengths
2945        // of three exact binary32 columns; their authored-order sum changes
2946        // by one binary64 ulp when the columns are cycled. The canonical
2947        // ascending association is the lower result.
2948        let lengths = [
2949            f64::from_bits(0x3ff1_09e7_e000_022c),
2950            f64::from_bits(0x3ff1_09ec_6000_0eb5),
2951            f64::from_bits(0x3ff1_09fa_e000_3cde),
2952        ];
2953        let expected = f64::from_bits(0x3ff1_09ef_b555_6f3f);
2954        let ascending = (lengths[0] + lengths[1] + lengths[2]) / 3.0;
2955        let descending = (lengths[2] + lengths[1] + lengths[0]) / 3.0;
2956        assert_eq!(expected.to_bits(), 0x3ff1_09ef_b555_6f3f);
2957        assert_eq!(ascending.to_bits(), expected.to_bits());
2958        assert_eq!(descending.to_bits(), 0x3ff1_09ef_b555_6f40);
2959        for order in [
2960            [0, 1, 2],
2961            [0, 2, 1],
2962            [1, 0, 2],
2963            [1, 2, 0],
2964            [2, 0, 1],
2965            [2, 1, 0],
2966        ] {
2967            assert_eq!(
2968                average_affine_axis_length(order.map(|index| lengths[index])),
2969                expected,
2970                "axis order {order:?}"
2971            );
2972        }
2973
2974        // The association is observable even for exactly representable
2975        // dyadic inputs: adding the two small terms first retains them,
2976        // while adding either to `2^53` loses both. This catches replacing
2977        // the canonical sort with a different fixed input order.
2978        let dyadic = [2.0_f64.powi(53), 1.0, 1.0];
2979        let ascending = (dyadic[1] + dyadic[2] + dyadic[0]) / 3.0;
2980        let descending = (dyadic[0] + dyadic[1] + dyadic[2]) / 3.0;
2981        assert_ne!(ascending, descending);
2982        assert_eq!(average_affine_axis_length(dyadic), ascending);
2983
2984        // The same exact binary32 columns exercise the classifier. Every
2985        // permutation below preserves orientation: odd column permutations
2986        // negate one column, which leaves lengths unchanged and restores the
2987        // determinant's sign. The equal-axis boundary must consequently
2988        // reject the same geometry in all six authored axis orders.
2989        let permutations = affine_test_fixtures::appendix_d_v6_mean_permutations();
2990        let expected_length_bits = lengths.map(f64::to_bits);
2991        assert_eq!(
2992            affine_axis_lengths(permutations[0]).map(f64::to_bits),
2993            expected_length_bits
2994        );
2995        let tolerance = PositiveUniformAffineTolerance {
2996            equal_axis: 1.0e-5,
2997            relative_orthogonality: 1.0e-5,
2998            singular_determinant_relative: 1.0e-6,
2999        };
3000        for (permutation, linear) in permutations.into_iter().enumerate() {
3001            assert!(
3002                linear
3003                    .x_axis
3004                    .as_dvec3()
3005                    .cross(linear.y_axis.as_dvec3())
3006                    .dot(linear.z_axis.as_dvec3())
3007                    > 0.0,
3008                "orientation for permutation {permutation}"
3009            );
3010            assert_eq!(
3011                average_affine_axis_length(affine_axis_lengths(linear)).to_bits(),
3012                expected.to_bits(),
3013                "mean for permutation {permutation}"
3014            );
3015            assert_eq!(
3016                classify_positive_uniform_affine(linear, tolerance),
3017                Err(AffineDomainViolation::NonUniformScale),
3018                "classification for permutation {permutation}"
3019            );
3020        }
3021    }
3022
3023    #[test]
3024    fn shared_affine_classifier_pins_f64_determinant_arithmetic() {
3025        // These exact binary32 columns make the f32 scalar triple product
3026        // land above the same threshold that the product of widened columns
3027        // lands below. The remaining bands are deliberately loose so only
3028        // singularity arithmetic decides the result.
3029        let linear = Mat3::from_cols(
3030            Vec3::new(
3031                f32::from_bits(0x3ff3_5574),
3032                f32::from_bits(0x3f0e_fa3c),
3033                0.0,
3034            ),
3035            Vec3::new(
3036                f32::from_bits(0x3ff5_5e17),
3037                f32::from_bits(0x3f10_2c31),
3038                0.0,
3039            ),
3040            Vec3::Z,
3041        );
3042        let columns = [
3043            linear.x_axis.as_dvec3(),
3044            linear.y_axis.as_dvec3(),
3045            linear.z_axis.as_dvec3(),
3046        ];
3047        let determinant_f64 = columns[2].dot(columns[0].cross(columns[1]));
3048        let determinant_f32 = f64::from(linear.determinant());
3049        let lengths = affine_axis_lengths(linear);
3050        let threshold = (determinant_f64 + determinant_f32) / 2.0;
3051        assert!(determinant_f64 < threshold);
3052        assert!(determinant_f32 > threshold);
3053
3054        assert_eq!(
3055            classify_positive_uniform_affine(
3056                linear,
3057                PositiveUniformAffineTolerance {
3058                    equal_axis: 10.0,
3059                    relative_orthogonality: 10.0,
3060                    singular_determinant_relative: threshold
3061                        / (lengths[0] * lengths[1] * lengths[2]),
3062                },
3063            ),
3064            Err(AffineDomainViolation::Singular)
3065        );
3066
3067        // Every derived determinant operand must stay widened as well. A
3068        // binary32 axis-product intermediate overflows on this otherwise
3069        // finite, positive, exactly uniform basis and falsely calls it
3070        // singular under Appendix D's non-zero relative threshold.
3071        let large_uniform = 2.0e19_f32;
3072        assert_eq!(
3073            classify_positive_uniform_affine(
3074                Mat3::from_diagonal(Vec3::splat(large_uniform)),
3075                PositiveUniformAffineTolerance {
3076                    equal_axis: 1.0e-5,
3077                    relative_orthogonality: 1.0e-5,
3078                    singular_determinant_relative: 1.0e-6,
3079                },
3080            ),
3081            Ok(f64::from(large_uniform))
3082        );
3083    }
3084
3085    #[test]
3086    fn affine_geometry_facts_pin_every_widened_field_and_slot() {
3087        let linear = Mat3::from_cols(
3088            Vec3::new(1.0, 2.0, 3.0),
3089            Vec3::new(4.0, 5.0, 6.0),
3090            Vec3::new(7.0, 8.0, 10.0),
3091        );
3092
3093        let facts = AffineGeometryFacts::from_linear(linear).expect("finite widened facts");
3094        assert_eq!(
3095            facts.axis_lengths.map(f64::to_bits),
3096            [
3097                0x400d_eeea_1168_3f49,
3098                0x4021_8cc8_21d6_d3e3,
3099                0x402d_3064_dcc8_ae67,
3100            ]
3101        );
3102        assert_eq!(facts.mean_axis_length.to_bits(), 0x4022_12f7_d653_30b4);
3103        assert_eq!(facts.determinant.to_bits(), 0xc008_0000_0000_0000);
3104        assert_eq!(facts.axis_length_product.to_bits(), 0x407d_f2e3_88f2_1b01);
3105        assert_eq!(
3106            facts.cross_axis_dots.map(f64::to_bits),
3107            [
3108                0x4040_0000_0000_0000,
3109                0x404a_8000_0000_0000,
3110                0x4060_0000_0000_0000,
3111            ],
3112            "cross-axis slots are XY, XZ, YZ"
3113        );
3114    }
3115
3116    #[test]
3117    fn affine_geometry_facts_widen_every_dot_product_before_multiplying() {
3118        let x = Vec3::new(
3119            f32::from_bits(0x3ff3_5574),
3120            f32::from_bits(0x3f0e_fa3c),
3121            0.0,
3122        );
3123        let y = Vec3::new(
3124            f32::from_bits(0x3ff5_5e17),
3125            f32::from_bits(0x3f10_2c31),
3126            0.0,
3127        );
3128        let widened_dot = x.as_dvec3().dot(y.as_dvec3());
3129        let f32_then_widened = f64::from(x.dot(y));
3130
3131        for (slot, linear) in [
3132            (0, Mat3::from_cols(x, y, Vec3::Z)),
3133            (1, Mat3::from_cols(x, Vec3::Z, y)),
3134            (2, Mat3::from_cols(Vec3::Z, x, y)),
3135        ] {
3136            let facts = AffineGeometryFacts::from_linear(linear).expect("finite widened facts");
3137            assert_eq!(facts.cross_axis_dots[slot], widened_dot);
3138            assert_ne!(
3139                facts.cross_axis_dots[slot], f32_then_widened,
3140                "dot slot {slot} must multiply and add in f64, not widen an f32 result"
3141            );
3142        }
3143    }
3144
3145    #[test]
3146    fn weld_preserves_uv_seams_at_shared_positions() {
3147        let mut primitive = Primitive {
3148            positions: vec![Vec3::ZERO, Vec3::ZERO, Vec3::ZERO],
3149            uvs: vec![[0.0, 0.0], [1.0, 0.0], [0.0, 0.0]],
3150            ..Primitive::default()
3151        };
3152
3153        primitive.weld();
3154
3155        assert_eq!(primitive.positions.len(), 2);
3156        let reconstructed_corners = primitive
3157            .indices
3158            .iter()
3159            .map(|&index| {
3160                let index = index as usize;
3161                (primitive.positions[index], primitive.uvs[index])
3162            })
3163            .collect::<Vec<_>>();
3164        assert_eq!(
3165            reconstructed_corners,
3166            vec![
3167                (Vec3::ZERO, [0.0, 0.0]),
3168                (Vec3::ZERO, [1.0, 0.0]),
3169                (Vec3::ZERO, [0.0, 0.0]),
3170            ]
3171        );
3172    }
3173}