Code/HumanoidRetargeter/Core/Solve/TargetRigMappingExtensions.cs

Extension method for TargetRig that converts the rig's role annotations into a MappingResult. It creates a MappingResult named after the rig, sets Confidence to 1, and fills RoleToBone entries for any annotated bones by iterating the rig's skeleton indices.

Reflection
#nullable enable annotations

using HumanoidRetargeter.Core.Mapping;
using HumanoidRetargeter.Core.Target;

namespace HumanoidRetargeter.Core.Solve;

/// <summary>
/// Bridges a <see cref="TargetRig"/>'s role annotations into the <see cref="MappingResult"/>
/// shape shared with source mappings, so target-side machinery (rest normalization, canonical
/// frames) can run on the exact same code paths as the source side.
/// </summary>
public static class TargetRigMappingExtensions
{
    /// <summary>Role → target bone index mapping of the rig's annotated animated bones.</summary>
    public static MappingResult ToMappingResult(this TargetRig rig)
    {
        ArgumentNullException.ThrowIfNull(rig);

        var map = new MappingResult(rig.Name, MappingSource.Preset) { Confidence = 1f };
        for (var i = 0; i < rig.Skeleton.Count; i++)
        {
            if (rig.RoleOf(i) is BoneRole role)
                map.RoleToBone[role] = i;
        }
        return map;
    }
}