animsmith-core 0.5.0

Engine-agnostic data model, sampling, measurements, and checks for the animsmith animation-clip linter
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//! Rig profiles: checks never reference bone names, they reference
//! *roles*. A profile maps roles to name matchers; built-ins cover the
//! common rigs and auto-detection scores every built-in by resolved-role
//! coverage. A check whose required roles do not resolve reports a typed
//! coverage gap — never a false failure.

use crate::config::RigConfig;
use crate::model::{BoneId, Skeleton};
use serde::Deserialize;
use std::collections::{BTreeMap, BTreeSet};

/// Semantic bone roles used by checks and measurements.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Role {
    /// Scene or locomotion root.
    Root,
    /// Pelvis/hips control, used as the primary body reference.
    Hips,
    /// Spine control.
    Spine,
    /// Head control.
    Head,
    /// Left foot control.
    LeftFoot,
    /// Right foot control.
    RightFoot,
    /// Left toe control.
    LeftToe,
    /// Right toe control.
    RightToe,
    /// Left hand control.
    LeftHand,
    /// Right hand control.
    RightHand,
}

impl Role {
    /// Stable snake-case role name used in config and result messages.
    pub fn as_str(self) -> &'static str {
        match self {
            Role::Root => "root",
            Role::Hips => "hips",
            Role::Spine => "spine",
            Role::Head => "head",
            Role::LeftFoot => "left_foot",
            Role::RightFoot => "right_foot",
            Role::LeftToe => "left_toe",
            Role::RightToe => "right_toe",
            Role::LeftHand => "left_hand",
            Role::RightHand => "right_hand",
        }
    }
}

/// Stable explanation of how one resolved role matched its delivered bone
/// name.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RoleResolutionPolicy {
    /// A built-in binding matched exactly (including its established
    /// namespace-stripped form).
    Exact,
    /// A built-in binding had no exact candidate and matched one unique
    /// ASCII-case-insensitive candidate.
    AsciiCaseInsensitive,
    /// An exact user-supplied `[rig.roles]` binding supplied the name.
    Explicit,
}

impl RoleResolutionPolicy {
    /// Stable output-contract spelling.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Exact => "exact",
            Self::AsciiCaseInsensitive => "ascii-case-insensitive",
            Self::Explicit => "explicit",
        }
    }
}

/// Typed overall result of resolving a configured rig.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ResolutionOutcome {
    /// Every binding considered by the selected profile resolved.
    Resolved,
    /// A usable, injective role map was produced but one or more bindings did
    /// not have a candidate.
    Coverage,
    /// One exact binding had more than one candidate.
    AmbiguousExactMatch,
    /// One case-insensitive binding had more than one candidate.
    AmbiguousFoldedMatch,
    /// Two roles would have selected the same bone.
    RoleCollision,
    /// More than one built-in profile had the same best automatic score.
    AmbiguousProfile,
}

impl ResolutionOutcome {
    /// Stable output-contract spelling.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Resolved => "resolved",
            Self::Coverage => "coverage",
            Self::AmbiguousExactMatch => "ambiguous_exact_match",
            Self::AmbiguousFoldedMatch => "ambiguous_folded_match",
            Self::RoleCollision => "role_collision",
            Self::AmbiguousProfile => "ambiguous_profile",
        }
    }

    const fn is_ambiguous(self) -> bool {
        !matches!(self, Self::Resolved | Self::Coverage)
    }
}

/// How a role's bone is found by name. Matching also tries a
/// namespace-stripped variant of each bone name (`"ns:Hips"` → `"Hips"`).
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum NameMatcher {
    /// Exact bone-name match, with namespace-stripped fallback.
    Exact(&'static str),
}

impl NameMatcher {
    fn exact_matches(&self, bone_name: &str) -> bool {
        let NameMatcher::Exact(wanted) = self;
        bone_name == *wanted
            || bone_name
                .rsplit_once(':')
                .is_some_and(|(_, stripped)| stripped == *wanted)
    }

    fn ascii_case_insensitive_matches(&self, bone_name: &str) -> bool {
        let NameMatcher::Exact(wanted) = self;
        bone_name.eq_ignore_ascii_case(wanted)
            || bone_name
                .rsplit_once(':')
                .is_some_and(|(_, stripped)| stripped.eq_ignore_ascii_case(wanted))
    }
}

/// A named set of role-to-bone-name matchers.
#[derive(Debug, Clone)]
pub struct RigProfile {
    /// Profile name used in configuration and result messages.
    pub name: &'static str,
    /// Role matchers tried against a skeleton.
    pub bindings: Vec<(Role, NameMatcher)>,
}

/// Role → bone resolution for one skeleton.
#[derive(Debug, Clone)]
pub struct ResolvedRoles {
    /// Name of the profile that produced this resolution ("custom" for
    /// inline role maps).
    pub profile: String,
    map: BTreeMap<Role, ResolvedBone>,
    outcome: ResolutionOutcome,
}

#[derive(Debug, Clone)]
struct ResolvedBone {
    id: BoneId,
    name: String,
    policy: RoleResolutionPolicy,
}

impl ResolvedRoles {
    /// Bone id for a role, when resolved.
    pub fn get(&self, role: Role) -> Option<BoneId> {
        self.map.get(&role).map(|bone| bone.id)
    }

    /// Resolution policy for a role, when it resolved.
    pub fn policy(&self, role: Role) -> Option<RoleResolutionPolicy> {
        self.map.get(&role).map(|bone| bone.policy)
    }

    /// Typed result of the overall profile/configuration resolution.
    pub const fn outcome(&self) -> ResolutionOutcome {
        self.outcome
    }

    /// Bone id and captured name for internal boundaries that must reject a
    /// role map reused with a different skeleton.
    pub(crate) fn get_with_name(&self, role: Role) -> Option<(BoneId, &str)> {
        self.map
            .get(&role)
            .map(|bone| (bone.id, bone.name.as_str()))
    }

    /// Number of resolved roles.
    pub fn len(&self) -> usize {
        self.map.len()
    }

    /// Whether no roles resolved.
    pub fn is_empty(&self) -> bool {
        self.map.is_empty()
    }

    /// Iterate resolved `(role, bone_id)` pairs in role order.
    pub fn iter(&self) -> impl Iterator<Item = (Role, BoneId)> + '_ {
        self.map.iter().map(|(&role, bone)| (role, bone.id))
    }

    pub(crate) fn iter_with_details(
        &self,
    ) -> impl Iterator<Item = (Role, BoneId, &str, RoleResolutionPolicy)> + '_ {
        self.map
            .iter()
            .map(|(&role, bone)| (role, bone.id, bone.name.as_str(), bone.policy))
    }

    /// Build from explicit role → bone-name pairs (for example a config
    /// inline map). Pairs whose bone name is absent are not bound, but make
    /// the result coverage-incomplete; when a role appears more than once,
    /// the last resolved pair wins. A final map that would bind two roles to
    /// one bone is refused as a typed collision.
    pub fn from_names(
        skeleton: &Skeleton,
        names: impl IntoIterator<Item = (Role, String)>,
    ) -> Self {
        let mut map = BTreeMap::new();
        let mut coverage_gap = false;
        for (role, name) in names {
            if let Some(id) = skeleton.bones.iter().position(|b| b.name == name) {
                map.insert(
                    role,
                    ResolvedBone {
                        id,
                        name: skeleton.bones[id].name.clone(),
                        policy: RoleResolutionPolicy::Explicit,
                    },
                );
            } else {
                coverage_gap = true;
            }
        }
        let outcome = injective_outcome(&map).unwrap_or(if coverage_gap || map.is_empty() {
            ResolutionOutcome::Coverage
        } else {
            ResolutionOutcome::Resolved
        });
        Self {
            profile: "custom".into(),
            map: if outcome.is_ambiguous() {
                BTreeMap::new()
            } else {
                map
            },
            outcome,
        }
    }
}

impl Default for ResolvedRoles {
    fn default() -> Self {
        unresolved("unknown", ResolutionOutcome::Coverage)
    }
}

impl RigProfile {
    /// Resolve this profile against `skeleton` using established exact matching
    /// semantics. Built-in alias fallback is deliberately unavailable through
    /// this public custom-profile API.
    pub fn resolve(&self, skeleton: &Skeleton) -> ResolvedRoles {
        self.resolve_with_options(skeleton, &BTreeSet::new(), false)
    }

    fn resolve_builtin_excluding(
        &self,
        skeleton: &Skeleton,
        excluded_roles: &BTreeSet<Role>,
    ) -> ResolvedRoles {
        self.resolve_with_options(skeleton, excluded_roles, true)
    }

    fn resolve_with_options(
        &self,
        skeleton: &Skeleton,
        excluded_roles: &BTreeSet<Role>,
        allow_ascii_case_insensitive_fallback: bool,
    ) -> ResolvedRoles {
        let mut map = BTreeMap::new();
        let mut coverage_gap = false;
        for (role, matcher) in &self.bindings {
            if excluded_roles.contains(role) {
                continue;
            }
            let exact: Vec<_> = skeleton
                .bones
                .iter()
                .enumerate()
                .filter_map(|(id, bone)| matcher.exact_matches(&bone.name).then_some(id))
                .collect();
            let (id, policy) = match exact.as_slice() {
                [id] => (*id, RoleResolutionPolicy::Exact),
                [] if allow_ascii_case_insensitive_fallback => {
                    let folded: Vec<_> = skeleton
                        .bones
                        .iter()
                        .enumerate()
                        .filter_map(|(id, bone)| {
                            matcher
                                .ascii_case_insensitive_matches(&bone.name)
                                .then_some(id)
                        })
                        .collect();
                    match folded.as_slice() {
                        [id] => (*id, RoleResolutionPolicy::AsciiCaseInsensitive),
                        [] => {
                            coverage_gap = true;
                            continue;
                        }
                        _ => return unresolved(self.name, ResolutionOutcome::AmbiguousFoldedMatch),
                    }
                }
                [] => {
                    coverage_gap = true;
                    continue;
                }
                _ => return unresolved(self.name, ResolutionOutcome::AmbiguousExactMatch),
            };
            map.insert(
                *role,
                ResolvedBone {
                    id,
                    name: skeleton.bones[id].name.clone(),
                    policy,
                },
            );
        }
        if injective_outcome(&map).is_some() {
            return unresolved(self.name, ResolutionOutcome::RoleCollision);
        }
        ResolvedRoles {
            profile: self.name.into(),
            map,
            outcome: if coverage_gap {
                ResolutionOutcome::Coverage
            } else {
                ResolutionOutcome::Resolved
            },
        }
    }
}

fn unresolved(profile: &str, outcome: ResolutionOutcome) -> ResolvedRoles {
    ResolvedRoles {
        profile: profile.into(),
        map: BTreeMap::new(),
        outcome,
    }
}

fn injective_outcome(map: &BTreeMap<Role, ResolvedBone>) -> Option<ResolutionOutcome> {
    let mut ids = BTreeSet::new();
    map.values()
        .any(|bone| !ids.insert(bone.id))
        .then_some(ResolutionOutcome::RoleCollision)
}

/// The built-in profiles.
pub fn builtin_profiles() -> Vec<RigProfile> {
    use NameMatcher::Exact;
    use Role::*;
    vec![
        RigProfile {
            name: "mixamo",
            bindings: vec![
                (Hips, Exact("mixamorig:Hips")),
                (Spine, Exact("mixamorig:Spine")),
                (Head, Exact("mixamorig:Head")),
                (LeftFoot, Exact("mixamorig:LeftFoot")),
                (RightFoot, Exact("mixamorig:RightFoot")),
                (LeftToe, Exact("mixamorig:LeftToeBase")),
                (RightToe, Exact("mixamorig:RightToeBase")),
                (LeftHand, Exact("mixamorig:LeftHand")),
                (RightHand, Exact("mixamorig:RightHand")),
            ],
        },
        RigProfile {
            name: "ue-mannequin",
            bindings: vec![
                (Root, Exact("root")),
                (Hips, Exact("pelvis")),
                (Spine, Exact("spine_01")),
                (Head, Exact("head")),
                (LeftFoot, Exact("foot_l")),
                (RightFoot, Exact("foot_r")),
                (LeftToe, Exact("ball_l")),
                (RightToe, Exact("ball_r")),
                (LeftHand, Exact("hand_l")),
                (RightHand, Exact("hand_r")),
            ],
        },
        RigProfile {
            name: "humanoid",
            bindings: vec![
                (Root, Exact("root")),
                (Hips, Exact("humanoid_ Pelvis")),
                (Spine, Exact("humanoid_ Spine")),
                (Head, Exact("humanoid_ Head")),
                (LeftFoot, Exact("humanoid_ L Foot")),
                (RightFoot, Exact("humanoid_ R Foot")),
                (LeftToe, Exact("humanoid_ L Toe0")),
                (RightToe, Exact("humanoid_ R Toe0")),
                (LeftHand, Exact("humanoid_ L Hand")),
                (RightHand, Exact("humanoid_ R Hand")),
            ],
        },
    ]
}

/// Auto-detect with a typed result. A profile must resolve at least two roles;
/// equal best candidates are refused rather than chosen by declaration order.
pub fn detect_profile_detailed(skeleton: &Skeleton) -> ResolvedRoles {
    detect_profile_excluding(skeleton, &BTreeSet::new(), 0)
}

fn detect_profile_excluding(
    skeleton: &Skeleton,
    excluded_roles: &BTreeSet<Role>,
    score_offset: usize,
) -> ResolvedRoles {
    let resolved: Vec<_> = builtin_profiles()
        .iter()
        .map(|profile| profile.resolve_builtin_excluding(skeleton, excluded_roles))
        .collect();
    let ambiguities: Vec<_> = resolved
        .iter()
        .filter_map(|roles| roles.outcome.is_ambiguous().then_some(roles.outcome))
        .collect();
    if ambiguities.len() == 1 {
        return unresolved("unknown", ambiguities[0]);
    }
    if ambiguities.len() > 1 {
        return unresolved("unknown", ResolutionOutcome::AmbiguousProfile);
    }
    let candidates: Vec<_> = resolved
        .iter()
        .filter(|roles| {
            !roles.outcome.is_ambiguous() && !roles.is_empty() && roles.len() + score_offset >= 2
        })
        .cloned()
        .collect();
    let Some(best_score) = candidates.iter().map(ResolvedRoles::len).max() else {
        return ResolvedRoles::default();
    };
    let mut best = candidates
        .into_iter()
        .filter(|roles| roles.len() == best_score);
    let selected = best.next().expect("a best score has a candidate");
    if best.next().is_some() {
        unresolved("unknown", ResolutionOutcome::AmbiguousProfile)
    } else {
        selected
    }
}

/// Auto-detect a built-in profile, preserving the historical optional API.
/// Use [`detect_profile_detailed`] when a caller needs the typed outcome.
pub fn detect_profile(skeleton: &Skeleton) -> Option<ResolvedRoles> {
    let resolved = detect_profile_detailed(skeleton);
    (!resolved.is_empty() && !resolved.outcome.is_ambiguous()).then_some(resolved)
}

/// Resolve a profile by name, or auto-detect for `"auto"`, retaining a typed
/// ambiguity or coverage result for callers that publish it.
pub fn resolve_named_detailed(skeleton: &Skeleton, profile: &str) -> ResolvedRoles {
    if profile == "auto" {
        return detect_profile_detailed(skeleton);
    }
    builtin_profiles()
        .iter()
        .find(|candidate| candidate.name == profile)
        .map(|candidate| candidate.resolve_builtin_excluding(skeleton, &BTreeSet::new()))
        .unwrap_or_default()
}

/// Resolve a profile by name, or auto-detect for `"auto"`.
pub fn resolve_named(skeleton: &Skeleton, profile: &str) -> Option<ResolvedRoles> {
    if profile != "auto"
        && !builtin_profiles()
            .iter()
            .any(|candidate| candidate.name == profile)
    {
        return None;
    }
    let resolved = resolve_named_detailed(skeleton, profile);
    (!resolved.outcome.is_ambiguous()).then_some(resolved)
}

/// Resolve a configured rig profile and apply inline role overrides.
///
/// Inline role bindings win over bindings from the named or auto-detected
/// profile. They remain exact, while built-in bindings use only the documented
/// ASCII case-tolerant fallback. A collision is refused instead of selecting a
/// role by configuration or profile declaration order.
pub fn resolve_configured_roles(skeleton: &Skeleton, rig: &RigConfig) -> ResolvedRoles {
    let mut explicit = BTreeMap::new();
    let mut explicit_coverage_gap = false;
    let mut inline_contributed = false;
    for (&role, name) in &rig.roles {
        if let Some(id) = skeleton.bones.iter().position(|bone| bone.name == *name) {
            inline_contributed = true;
            explicit.insert(
                role,
                ResolvedBone {
                    id,
                    name: skeleton.bones[id].name.clone(),
                    policy: RoleResolutionPolicy::Explicit,
                },
            );
        } else {
            explicit_coverage_gap = true;
        }
    }
    if injective_outcome(&explicit).is_some() {
        return unresolved("unknown", ResolutionOutcome::RoleCollision);
    }
    let overridden_roles: BTreeSet<_> = rig.roles.keys().copied().collect();
    let base = if rig.profile == "auto" {
        detect_profile_excluding(skeleton, &overridden_roles, explicit.len())
    } else {
        builtin_profiles()
            .iter()
            .find(|profile| profile.name == rig.profile)
            .map(|profile| profile.resolve_builtin_excluding(skeleton, &overridden_roles))
            .unwrap_or_default()
    };
    if base.outcome.is_ambiguous() {
        return unresolved("unknown", base.outcome);
    }
    let base_contributed = !base.is_empty();
    let mut map = base.map;
    map.extend(explicit);
    if injective_outcome(&map).is_some() {
        return unresolved("unknown", ResolutionOutcome::RoleCollision);
    }
    let profile = match (base_contributed, inline_contributed) {
        (false, false) => "unknown".into(),
        (false, true) => "custom".into(),
        (true, false) => base.profile,
        (true, true) => format!("{}+custom", base.profile),
    };
    let outcome = if explicit_coverage_gap
        || (base.outcome == ResolutionOutcome::Coverage
            && (base_contributed || rig.profile != "auto"))
        || map.is_empty()
    {
        ResolutionOutcome::Coverage
    } else {
        ResolutionOutcome::Resolved
    };
    ResolvedRoles {
        profile,
        map,
        outcome,
    }
}