Skip to content
0.2.0-dev Download ZIP
119 lines ·4.4 KB Raw
1using System.Numerics;
2
3namespace EvilEngine.Formats.Humanoids;
4
5public sealed record HumanoidBetween(string From, string To);
6
7public sealed record HumanoidTwist(string Follows, float Share);
8
9public sealed record HumanoidBinding(string Layout, IReadOnlyDictionary<string, int> Bones)
10{
11 public int? Root { get; init; }
12
13 public IReadOnlyDictionary<int, HumanoidBetween> Between { get; init; } =
14 new SortedDictionary<int, HumanoidBetween>();
15
16 public IReadOnlyDictionary<int, HumanoidTwist> Twist { get; init; } = new SortedDictionary<int, HumanoidTwist>();
17
18 public IReadOnlyDictionary<string, Quaternion>? Reference { get; init; }
19
20 public bool Equals(HumanoidBinding? other)
21 {
22 return other is not null && Layout == other.Layout && Root == other.Root && Same(Bones, other.Bones) &&
23 Same(Between, other.Between) && Same(Twist, other.Twist) &&
24 (Reference is null
25 ? other.Reference is null
26 : other.Reference is not null && Same(Reference, other.Reference));
27 }
28
29 public bool Has(string bone)
30 {
31 return Bones.ContainsKey(bone);
32 }
33
34 public override int GetHashCode()
35 {
36 return HashCode.Combine(Layout, Root, Bones.Count, Between.Count, Twist.Count);
37 }
38
39 public string? Problem(int nodes)
40 {
41 if (!IsId(Layout))
42 return $"the layout '{Layout}' isn't a namespace:object id";
43 if (Bones.Count == 0)
44 return "it maps no bones";
45 var used = new Dictionary<int, string>();
46 foreach (var (bone, node) in Bones)
47 {
48 if (!HumanoidProfile.Has(bone))
49 return $"'{bone}' isn't a bone of the humanoid profile";
50 if (bone == HumanoidProfile.Root)
51 return "Root is given by root, not in bones";
52 if (node < 0 || node >= nodes)
53 return $"{bone} names node {node}, and there are {nodes}";
54 if (!used.TryAdd(node, bone))
55 return $"{bone} and {used[node]} are both node {node}";
56 }
57
58 if (Root is { } root)
59 {
60 if (root < 0 || root >= nodes)
61 return $"root names node {root}, and there are {nodes}";
62 if (!used.TryAdd(root, HumanoidProfile.Root))
63 return $"root and {used[root]} are both node {root}";
64 }
65
66 foreach (var (node, between) in Between)
67 {
68 if (node < 0 || node >= nodes)
69 return $"between names node {node}, and there are {nodes}";
70 if (!used.TryAdd(node, "between"))
71 return $"between names node {node}, which {used[node]} already is";
72 if (!Bones.ContainsKey(between.From) || !Bones.ContainsKey(between.To))
73 return $"node {node} is between {between.From} and {between.To}, which must both be mapped";
74 }
75
76 foreach (var (node, twist) in Twist)
77 {
78 if (node < 0 || node >= nodes)
79 return $"twist names node {node}, and there are {nodes}";
80 if (!used.TryAdd(node, "twist"))
81 return $"twist names node {node}, which {used[node]} already is";
82 if (!Bones.ContainsKey(twist.Follows))
83 return $"node {node} twists with {twist.Follows}, which isn't mapped";
84 if (!(twist.Share > 0 && twist.Share <= 1))
85 return $"node {node}'s twist share must be above 0 and at most 1";
86 }
87
88 if (Reference is not null)
89 foreach (var (bone, rotation) in Reference)
90 {
91 if (!Bones.ContainsKey(bone))
92 return $"the reference turns {bone}, which isn't mapped";
93 if (MathF.Abs(rotation.Length() - 1) > 1e-3f)
94 return $"the reference rotation of {bone} isn't a unit quaternion";
95 }
96
97 return null;
98 }
99
100 private static bool IsId(string text)
101 {
102 try
103 {
104 return ContentId.Parse(text).ToString() == text;
105 }
106 catch (Exception error) when (error is FormatException or ArgumentException)
107 {
108 return false;
109 }
110 }
111
112 private static bool Same<TKey, TValue>(IReadOnlyDictionary<TKey, TValue> a, IReadOnlyDictionary<TKey, TValue> b)
113 {
114 return a.Count == b.Count &&
115 a.All(pair =>
116 b.TryGetValue(pair.Key, out var value) &&
117 EqualityComparer<TValue>.Default.Equals(pair.Value, value));
118 }
119}