BodyPlan: parts, joints that are the tree, sense points by label and side; Humanoid() carries the old numbers; HumanoidRagdoll.cs renamed Ragdoll.cs (content unchanged)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
d7a626b20a
commit
f04ad22371
4 changed files with 484 additions and 0 deletions
157
src/Lodur.Core/BodyPlan.cs
Normal file
157
src/Lodur.Core/BodyPlan.cs
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Lodur
|
||||
{
|
||||
/// <summary>Which side of the body a part, joint or sense point is on; Center for the midline.</summary>
|
||||
public enum Side { Center, Left, Right }
|
||||
|
||||
/// <summary>A part's collision shape.</summary>
|
||||
public enum PartShape { Box, Capsule, Sphere }
|
||||
|
||||
/// <summary>A joint's degrees of freedom: a ball (swing cone and twist) or a hinge about its axis.</summary>
|
||||
public enum JointKind { Spherical, Revolute }
|
||||
|
||||
/// <summary>
|
||||
/// One rigid part. <see cref="Size"/> is (x, y, z): half extents for a box; radius, half length, 0 for a capsule;
|
||||
/// radius, 0, 0 for a sphere. <see cref="Position"/> is the creation pose relative to the build origin.
|
||||
/// The part tree is not a field here: it is the joint list.
|
||||
/// </summary>
|
||||
public readonly record struct PartDef(
|
||||
string Label, Side Side, PartShape Shape, (float X, float Y, float Z) Size, (float X, float Y, float Z) Position,
|
||||
Quat Rotation, float Density, float Friction, float RollingResistance);
|
||||
|
||||
/// <summary>A joint from <see cref="Parent"/> to <see cref="Child"/> (part ordinals); the child's only joint is the one that attaches it.</summary>
|
||||
public readonly record struct JointDef(
|
||||
string Label, Side Side, JointKind Kind, int Parent, int Child, (float X, float Y, float Z) Anchor,
|
||||
(float X, float Y, float Z) Axis, float Lower, float Upper, float Cone, MuscleGains Gains);
|
||||
|
||||
/// <summary>A point on a part that perception senses at: the part's position plus its rotation applied to <see cref="Offset"/>.</summary>
|
||||
public readonly record struct SensePointDef(string Label, Side Side, int Part, (float X, float Y, float Z) Offset);
|
||||
|
||||
/// <summary>
|
||||
/// A body as data: parts, the joints that are also its tree, and sense points, each named by label and side.
|
||||
/// Engine-free; a binding (Lodur.Body.Ymir) builds it into a physics world. Validated at construction.
|
||||
/// </summary>
|
||||
public sealed class BodyPlan
|
||||
{
|
||||
private readonly int[] _parentOf;
|
||||
private readonly Dictionary<(string, Side), int> _parts = new Dictionary<(string, Side), int>();
|
||||
private readonly Dictionary<(string, Side), int> _joints = new Dictionary<(string, Side), int>();
|
||||
private readonly Dictionary<(string, Side), int> _senses = new Dictionary<(string, Side), int>();
|
||||
|
||||
public BodyPlan(IReadOnlyList<PartDef> parts, IReadOnlyList<JointDef> joints, IReadOnlyList<SensePointDef> sensePoints)
|
||||
{
|
||||
Parts = new List<PartDef>(parts).AsReadOnly();
|
||||
Joints = new List<JointDef>(joints).AsReadOnly();
|
||||
SensePoints = new List<SensePointDef>(sensePoints).AsReadOnly();
|
||||
_parentOf = Validate();
|
||||
}
|
||||
|
||||
public IReadOnlyList<PartDef> Parts { get; }
|
||||
public IReadOnlyList<JointDef> Joints { get; }
|
||||
public IReadOnlyList<SensePointDef> SensePoints { get; }
|
||||
|
||||
/// <summary>The part ordinal a part hangs from (the parent of the one joint that attaches it), -1 for the root.</summary>
|
||||
public int ParentOf(int part) => _parentOf[part];
|
||||
|
||||
public int PartIndex(string label, Side side = Side.Center) => Find(_parts, "part", label, side);
|
||||
public int JointIndex(string label, Side side = Side.Center) => Find(_joints, "joint", label, side);
|
||||
public int SenseIndex(string label, Side side = Side.Center) => Find(_senses, "sense point", label, side);
|
||||
|
||||
private static int Find(Dictionary<(string, Side), int> index, string what, string label, Side side) =>
|
||||
index.TryGetValue((label, side), out var i) ? i : throw new ArgumentException($"no {what} labelled '{label}' on side {side}.");
|
||||
|
||||
private static bool IsFinite(float f) => !float.IsNaN(f) && !float.IsInfinity(f);
|
||||
private static bool Finite((float X, float Y, float Z) v) => IsFinite(v.X) && IsFinite(v.Y) && IsFinite(v.Z);
|
||||
|
||||
private int[] Validate()
|
||||
{
|
||||
// 1. (Label, Side) unique per list, labels non-empty.
|
||||
Unique(Parts.Count, i => (Parts[i].Label, Parts[i].Side), _parts, "part");
|
||||
Unique(Joints.Count, i => (Joints[i].Label, Joints[i].Side), _joints, "joint");
|
||||
Unique(SensePoints.Count, i => (SensePoints[i].Label, SensePoints[i].Side), _senses, "sense point");
|
||||
|
||||
// 2. Joint ends in range and ordered, so the tree is acyclic and parts are created parent first.
|
||||
foreach (var d in Joints)
|
||||
{
|
||||
if (d.Parent < 0 || d.Parent >= Parts.Count || d.Child < 0 || d.Child >= Parts.Count)
|
||||
{
|
||||
throw new ArgumentException($"joint '{d.Label}' {d.Side}: parent {d.Parent} or child {d.Child} is not a part.");
|
||||
}
|
||||
|
||||
if (d.Parent >= d.Child)
|
||||
{
|
||||
throw new ArgumentException($"joint '{d.Label}' {d.Side}: parent {d.Parent} must precede child {d.Child}.");
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Part 0 is the root and the child of no joint; every other part is the child of exactly one joint.
|
||||
if (Parts.Count == 0) throw new ArgumentException("a body plan needs a root part.");
|
||||
var parentOf = new int[Parts.Count];
|
||||
var attached = new int[Parts.Count];
|
||||
for (var p = 0; p < parentOf.Length; p++) parentOf[p] = -1;
|
||||
foreach (var d in Joints)
|
||||
{
|
||||
parentOf[d.Child] = d.Parent;
|
||||
attached[d.Child]++;
|
||||
}
|
||||
|
||||
for (var p = 0; p < attached.Length; p++)
|
||||
{
|
||||
var want = p == 0 ? 0 : 1;
|
||||
if (attached[p] != want)
|
||||
{
|
||||
throw new ArgumentException($"part '{Parts[p].Label}' {Parts[p].Side} is the child of {attached[p]} joints, needs {want}.");
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Sense points sit on parts.
|
||||
foreach (var s in SensePoints)
|
||||
{
|
||||
if (s.Part < 0 || s.Part >= Parts.Count) throw new ArgumentException($"sense point '{s.Label}' {s.Side}: part {s.Part} is not a part.");
|
||||
}
|
||||
|
||||
// 5. Numbers a physics world can take.
|
||||
foreach (var p in Parts)
|
||||
{
|
||||
var r = p.Rotation;
|
||||
var finite = Finite(p.Size) && Finite(p.Position) && IsFinite(r.X) && IsFinite(r.Y) && IsFinite(r.Z) && IsFinite(r.W)
|
||||
&& IsFinite(p.Density) && IsFinite(p.Friction) && IsFinite(p.RollingResistance);
|
||||
if (!finite) throw new ArgumentException($"part '{p.Label}' {p.Side} has a non-finite number.");
|
||||
var sz = p.Size;
|
||||
var sized = p.Shape == PartShape.Box ? sz.X > 0f && sz.Y > 0f && sz.Z > 0f
|
||||
: p.Shape == PartShape.Capsule ? sz.X > 0f && sz.Y > 0f : sz.X > 0f;
|
||||
if (!sized) throw new ArgumentException($"part '{p.Label}' {p.Side}: a size its shape uses is not positive.");
|
||||
if (!(p.Density > 0f)) throw new ArgumentException($"part '{p.Label}' {p.Side}: density is not positive.");
|
||||
}
|
||||
|
||||
foreach (var d in Joints)
|
||||
{
|
||||
var g = d.Gains;
|
||||
var finite = Finite(d.Anchor) && Finite(d.Axis) && IsFinite(d.Lower) && IsFinite(d.Upper) && IsFinite(d.Cone)
|
||||
&& IsFinite(g.SwingStiffness) && IsFinite(g.SwingDamping) && IsFinite(g.TwistStiffness) && IsFinite(g.TwistDamping) && IsFinite(g.Passive);
|
||||
if (!finite) throw new ArgumentException($"joint '{d.Label}' {d.Side} has a non-finite number.");
|
||||
if (d.Lower > d.Upper) throw new ArgumentException($"joint '{d.Label}' {d.Side}: lower limit exceeds upper.");
|
||||
if (d.Cone < 0f) throw new ArgumentException($"joint '{d.Label}' {d.Side}: cone is negative.");
|
||||
}
|
||||
|
||||
foreach (var s in SensePoints)
|
||||
{
|
||||
if (!Finite(s.Offset)) throw new ArgumentException($"sense point '{s.Label}' {s.Side} has a non-finite number.");
|
||||
}
|
||||
|
||||
return parentOf;
|
||||
}
|
||||
|
||||
private static void Unique(int count, Func<int, (string, Side)> key, Dictionary<(string, Side), int> index, string what)
|
||||
{
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var (label, side) = key(i);
|
||||
if (string.IsNullOrEmpty(label)) throw new ArgumentException($"a {what} has an empty label.");
|
||||
if (!index.TryAdd((label, side), i)) throw new ArgumentException($"two {what}s are labelled '{label}' on side {side}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
111
src/Lodur.Core/BodyPlans.cs
Normal file
111
src/Lodur.Core/BodyPlans.cs
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Lodur
|
||||
{
|
||||
/// <summary>Body plans in code, until they live in a store (follow-up lodur-body-plan-cultcache).</summary>
|
||||
public static class BodyPlans
|
||||
{
|
||||
private const float Density = 1000f;
|
||||
private const float Friction = 0.8f;
|
||||
/// <summary>The fingers' creation curl at the knuckle, radians: the relaxed resting hand (question hand-rest-curl).</summary>
|
||||
private const float RestCurl = 0.35f;
|
||||
private const float Thumb30 = 30f * MathF.PI / 180f; // the thumb's metacarpal, out of the palm plane
|
||||
private const float HeadRolling = 0.1f; // a bare sphere on the ground rolls on, so the fallen head never settles
|
||||
|
||||
/// <summary>
|
||||
/// A 19-part humanoid: capsule limbs, box pelvis, torso, feet and palms, a sphere head, a thumb capsule and a fingers box per hand,
|
||||
/// 18 joints: spherical at spine, neck, shoulders, hips, wrists and thumbs, hinges at elbows, knees, ankles and knuckles.
|
||||
/// Facing +z, left is +x, standing on y = 0 at the creation pose, which is also every joint's zero. Parts and joints are in
|
||||
/// build order: the trunk, then per side (left, right) the limbs, then per side the hand.
|
||||
/// </summary>
|
||||
public static BodyPlan Humanoid()
|
||||
{
|
||||
var parts = new List<PartDef>();
|
||||
var joints = new List<JointDef>();
|
||||
|
||||
int Box(string label, Side side, float x, float y, float z, float hx, float hy, float hz) =>
|
||||
Add(parts, new PartDef(label, side, PartShape.Box, (hx, hy, hz), (x, y, z), Quat.Identity, Density, Friction, 0f));
|
||||
int Capsule(string label, Side side, float x, float y, float radius, float half) =>
|
||||
Add(parts, new PartDef(label, side, PartShape.Capsule, (radius, half, 0f), (x, y, 0f), Quat.Identity, Density, Friction, 0f));
|
||||
|
||||
var pelvis = Box("Pelvis", Side.Center, 0f, 1.11f, 0f, 0.15f, 0.08f, 0.09f);
|
||||
var torso = Box("Torso", Side.Center, 0f, 1.41f, 0f, 0.15f, 0.22f, 0.09f);
|
||||
var head = Add(parts, new PartDef("Head", Side.Center, PartShape.Sphere, (0.11f, 0f, 0f), (0f, 1.74f, 0f), Quat.Identity, Density, Friction, HeadRolling));
|
||||
var sides = new[] { (Side.Left, 1f), (Side.Right, -1f) };
|
||||
var upperArm = new int[2]; var lowerArm = new int[2]; var thigh = new int[2]; var shin = new int[2]; var foot = new int[2];
|
||||
for (var k = 0; k < 2; k++)
|
||||
{
|
||||
var (side, sx) = sides[k];
|
||||
upperArm[k] = Capsule("UpperArm", side, sx * 0.21f, 1.395f, 0.045f, 0.11f);
|
||||
lowerArm[k] = Capsule("LowerArm", side, sx * 0.21f, 1.10f, 0.04f, 0.10f);
|
||||
thigh[k] = Capsule("Thigh", side, sx * 0.09f, 0.79f, 0.07f, 0.18f);
|
||||
shin[k] = Capsule("Shin", side, sx * 0.09f, 0.31f, 0.06f, 0.17f);
|
||||
foot[k] = Box("Foot", side, sx * 0.09f, 0.04f, 0.05f, 0.05f, 0.04f, 0.12f);
|
||||
}
|
||||
|
||||
// Hands, appended after the base parts. The palm faces -sx with the thumb forward (+z); the thumb capsule
|
||||
// lies along its metacarpal axis, 30 degrees out of the palm plane; the fingers are created at the rest curl.
|
||||
var cmcAxis = (0f, -MathF.Cos(Thumb30), MathF.Sin(Thumb30));
|
||||
var palm = new int[2]; var thumb = new int[2]; var fingers = new int[2];
|
||||
for (var k = 0; k < 2; k++)
|
||||
{
|
||||
var (side, sx) = sides[k];
|
||||
palm[k] = Box("Palm", side, sx * 0.21f, 0.930f, 0f, 0.014f, 0.045f, 0.040f);
|
||||
var thumbSpin = Quat.FromAxisAngle(1f, 0f, 0f, -Thumb30);
|
||||
thumb[k] = Add(parts, new PartDef("Thumb", side, PartShape.Capsule, (0.011f, 0.03f, 0f),
|
||||
(sx * 0.206f + 0.05f * cmcAxis.Item1, 0.955f + 0.05f * cmcAxis.Item2, 0.03f + 0.05f * cmcAxis.Item3),
|
||||
thumbSpin, Density, Friction, 0f));
|
||||
var curl = Quat.FromAxisAngle(0f, 0f, sx, -RestCurl);
|
||||
fingers[k] = Add(parts, new PartDef("Fingers", side, PartShape.Box, (0.011f, 0.0425f, 0.038f),
|
||||
(sx * 0.21f - sx * MathF.Sin(RestCurl) * 0.0425f, 0.885f - MathF.Cos(RestCurl) * 0.0425f, 0f),
|
||||
curl, Density, Friction, 0f));
|
||||
}
|
||||
|
||||
void Joint(string label, Side side, JointKind kind, int parent, int child, float x, float y, float z,
|
||||
(float, float, float) axis, MuscleGains gains, float lower = 0f, float upper = 0f, float cone = 0f) =>
|
||||
joints.Add(new JointDef(label, side, kind, parent, child, (x, y, z), axis, lower, upper, cone, gains));
|
||||
|
||||
var up = (0f, 1f, 0f);
|
||||
var across = (1f, 0f, 0f);
|
||||
// Gains are tuned against measured stand/fall runs at 120 Hz (see the tests), not derived: explicit PD on
|
||||
// light limbs is stable only while damping * dt stays under the inertia it acts on.
|
||||
Joint("Spine", Side.Center, JointKind.Spherical, pelvis, torso, 0f, 1.19f, 0f, up, new MuscleGains(500f, 4f, 200f, 8f), -0.4f, 0.4f, 0.5f);
|
||||
Joint("Neck", Side.Center, JointKind.Spherical, torso, head, 0f, 1.63f, 0f, up, new MuscleGains(30f, 0.8f, 20f, 0.6f), -0.6f, 0.6f, 0.6f);
|
||||
for (var k = 0; k < 2; k++)
|
||||
{
|
||||
var (side, sx) = sides[k];
|
||||
Joint("Shoulder", side, JointKind.Spherical, torso, upperArm[k], sx * 0.21f, 1.55f, 0f, up, new MuscleGains(20f, 1.5f, 1f, 0.02f), -1f, 1f, 1.5f); // damping carries the hand's mass (hands-gains: 1 and 0.6 before)
|
||||
Joint("Elbow", side, JointKind.Revolute, upperArm[k], lowerArm[k], sx * 0.21f, 1.24f, 0f, across, new MuscleGains(0f, 0f, 12f, 0.9f), -2.4f, 0f);
|
||||
Joint("Hip", side, JointKind.Spherical, pelvis, thigh[k], sx * 0.09f, 1.04f, 0f, up, new MuscleGains(600f, 3f, 10f, 0.15f), -0.5f, 0.5f, 1.2f);
|
||||
Joint("Knee", side, JointKind.Revolute, thigh[k], shin[k], sx * 0.09f, 0.54f, 0f, across, new MuscleGains(0f, 0f, 300f, 8f), 0f, 2.4f);
|
||||
Joint("Ankle", side, JointKind.Revolute, shin[k], foot[k], sx * 0.09f, 0.08f, 0f, across, new MuscleGains(0f, 0f, 100f, 0.5f), -0.6f, 0.6f);
|
||||
}
|
||||
|
||||
// Hand gains are bounded by each body's own inertia, not the arm subtree's (explicit PD is stable only while
|
||||
// damping * dt stays under the inertia it acts on; a wrist kd sized for the arm limit-cycles at once), and
|
||||
// every hand joint keeps passive damping when limp so loose hands settle instead of ringing.
|
||||
for (var k = 0; k < 2; k++)
|
||||
{
|
||||
var (side, sx) = sides[k];
|
||||
Joint("Wrist", side, JointKind.Spherical, lowerArm[k], palm[k], sx * 0.21f, 0.975f, 0f, up, new MuscleGains(1f, 0.012f, 0.3f, 0.005f, 1f), -1.2f, 1.2f, 1.0f);
|
||||
Joint("Thumb", side, JointKind.Spherical, palm[k], thumb[k], sx * 0.206f, 0.955f, 0.03f, cmcAxis, new MuscleGains(0.06f, 0.001f, 0f, 0f, 1f), 0f, 0f, 0.7f); // twist locked: a free thumb spins
|
||||
Joint("Fingers", side, JointKind.Revolute, palm[k], fingers[k], sx * 0.21f, 0.885f, 0f, (0f, 0f, sx), new MuscleGains(0f, 0f, 0.3f, 0.004f, 1f), -1.6f + RestCurl, 0.35f + RestCurl);
|
||||
}
|
||||
|
||||
var senses = new[]
|
||||
{
|
||||
new SensePointDef("Hand", Side.Left, palm[0], (0f, 0f, 0f)),
|
||||
new SensePointDef("Hand", Side.Right, palm[1], (0f, 0f, 0f)),
|
||||
new SensePointDef("Head", Side.Center, head, (0f, 0f, 0f)),
|
||||
};
|
||||
return new BodyPlan(parts, joints, senses);
|
||||
}
|
||||
|
||||
private static int Add(List<PartDef> parts, PartDef part)
|
||||
{
|
||||
parts.Add(part);
|
||||
return parts.Count - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
216
tests/Lodur.Core.Tests/BodyPlanTests.cs
Normal file
216
tests/Lodur.Core.Tests/BodyPlanTests.cs
Normal file
|
|
@ -0,0 +1,216 @@
|
|||
using Lodur;
|
||||
|
||||
namespace Lodur.Core.Tests;
|
||||
|
||||
public sealed class BodyPlanTests
|
||||
{
|
||||
private static PartDef Box(string label, Side side = Side.Center) =>
|
||||
new(label, side, PartShape.Box, (0.1f, 0.1f, 0.1f), (0f, 0f, 0f), Quat.Identity, 1000f, 0.8f, 0f);
|
||||
|
||||
private static JointDef Joint(string label, int parent, int child, Side side = Side.Center) =>
|
||||
new(label, side, JointKind.Spherical, parent, child, (0f, 0f, 0f), (0f, 1f, 0f), -1f, 1f, 0.5f, new MuscleGains(1f, 1f, 1f, 1f));
|
||||
|
||||
private static BodyPlan Plan(PartDef[] parts, JointDef[] joints, SensePointDef[]? senses = null) =>
|
||||
new(parts, joints, senses ?? Array.Empty<SensePointDef>());
|
||||
|
||||
private static BodyPlan Valid() => Plan(new[] { Box("A"), Box("B") }, new[] { Joint("J", 0, 1) });
|
||||
|
||||
private static void Refuses(Action build, string saying)
|
||||
{
|
||||
var e = Assert.Throws<ArgumentException>(build);
|
||||
Assert.Contains(saying, e.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_valid_plan_resolves_labels_and_derives_the_tree_from_its_joints()
|
||||
{
|
||||
var plan = Valid();
|
||||
Assert.Equal(1, plan.PartIndex("B"));
|
||||
Assert.Equal(0, plan.JointIndex("J"));
|
||||
Assert.Equal(new[] { -1, 0 }, new[] { plan.ParentOf(0), plan.ParentOf(1) });
|
||||
}
|
||||
|
||||
// Rule 1: (label, side) unique per list, labels non-empty.
|
||||
[Fact]
|
||||
public void Rule1_a_repeated_label_and_side_is_refused_in_each_list_and_another_side_is_not()
|
||||
{
|
||||
Refuses(() => Plan(new[] { Box("A"), Box("A") }, new[] { Joint("J", 0, 1) }), "two parts are labelled 'A'");
|
||||
Refuses(() => Plan(new[] { Box("A"), Box("B"), Box("C") }, new[] { Joint("J", 0, 1), Joint("J", 0, 2) }), "two joints are labelled 'J'");
|
||||
Refuses(() => Plan(new[] { Box("A"), Box("B") }, new[] { Joint("J", 0, 1) },
|
||||
new[] { new SensePointDef("S", Side.Left, 0, default), new SensePointDef("S", Side.Left, 1, default) }), "two sense points are labelled 'S'");
|
||||
var plan = Plan(new[] { Box("A"), Box("L", Side.Left), Box("L", Side.Right) }, new[] { Joint("J", 0, 1, Side.Left), Joint("J", 0, 2, Side.Right) });
|
||||
Assert.Equal(2, plan.PartIndex("L", Side.Right));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rule1_an_empty_label_is_refused()
|
||||
{
|
||||
Refuses(() => Plan(new[] { Box("A"), Box("") }, new[] { Joint("J", 0, 1) }), "empty label");
|
||||
}
|
||||
|
||||
// Rule 2: joint ends in range, parent before child.
|
||||
[Theory]
|
||||
[InlineData(0, 2)]
|
||||
[InlineData(-1, 1)]
|
||||
[InlineData(5, 1)]
|
||||
public void Rule2_a_joint_end_that_is_not_a_part_is_refused(int parent, int child)
|
||||
{
|
||||
Refuses(() => Plan(new[] { Box("A"), Box("B") }, new[] { Joint("J", parent, child) }), "is not a part");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rule2_a_parent_that_does_not_precede_its_child_is_refused()
|
||||
{
|
||||
Refuses(() => Plan(new[] { Box("A"), Box("B") }, new[] { Joint("J", 1, 0) }), "must precede");
|
||||
Refuses(() => Plan(new[] { Box("A"), Box("B") }, new[] { Joint("J", 1, 1) }), "must precede");
|
||||
}
|
||||
|
||||
// Rule 3: part 0 the root, every other part the child of exactly one joint.
|
||||
[Fact]
|
||||
public void Rule3_a_part_with_no_joint_is_refused()
|
||||
{
|
||||
Refuses(() => Plan(new[] { Box("A"), Box("B"), Box("C") }, new[] { Joint("J", 0, 1) }), "child of 0 joints");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rule3_a_part_with_two_joints_is_refused()
|
||||
{
|
||||
Refuses(() => Plan(new[] { Box("A"), Box("B"), Box("C") }, new[] { Joint("J", 0, 1), Joint("K", 0, 2), Joint("M", 1, 2) }), "child of 2 joints");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rule3_a_plan_without_parts_is_refused()
|
||||
{
|
||||
Refuses(() => Plan(Array.Empty<PartDef>(), Array.Empty<JointDef>()), "needs a root");
|
||||
}
|
||||
|
||||
// Rule 4: sense points name parts.
|
||||
[Theory]
|
||||
[InlineData(2)]
|
||||
[InlineData(-1)]
|
||||
public void Rule4_a_sense_point_on_no_part_is_refused(int part)
|
||||
{
|
||||
Refuses(() => Plan(new[] { Box("A"), Box("B") }, new[] { Joint("J", 0, 1) }, new[] { new SensePointDef("S", Side.Center, part, default) }), "is not a part");
|
||||
}
|
||||
|
||||
// Rule 5: numbers a physics world can take.
|
||||
[Fact]
|
||||
public void Rule5_a_non_finite_part_number_is_refused()
|
||||
{
|
||||
var nan = Box("B") with { Position = (float.NaN, 0f, 0f) };
|
||||
Refuses(() => Plan(new[] { Box("A"), nan }, new[] { Joint("J", 0, 1) }), "non-finite");
|
||||
var inf = Box("B") with { Friction = float.PositiveInfinity };
|
||||
Refuses(() => Plan(new[] { Box("A"), inf }, new[] { Joint("J", 0, 1) }), "non-finite");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rule5_a_size_the_shape_uses_must_be_positive_and_one_it_does_not_use_may_be_zero()
|
||||
{
|
||||
Refuses(() => Plan(new[] { Box("A"), Box("B") with { Size = (0.1f, 0f, 0.1f) } }, new[] { Joint("J", 0, 1) }), "size its shape uses");
|
||||
Refuses(() => Plan(new[] { Box("A"), Box("B") with { Shape = PartShape.Capsule, Size = (0.1f, 0f, 0f) } }, new[] { Joint("J", 0, 1) }), "size its shape uses");
|
||||
Refuses(() => Plan(new[] { Box("A"), Box("B") with { Shape = PartShape.Sphere, Size = (-0.1f, 0f, 0f) } }, new[] { Joint("J", 0, 1) }), "size its shape uses");
|
||||
var capsule = Plan(new[] { Box("A"), Box("B") with { Shape = PartShape.Capsule, Size = (0.1f, 0.2f, 0f) } }, new[] { Joint("J", 0, 1) });
|
||||
Assert.Equal(2, capsule.Parts.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rule5_a_density_that_is_not_positive_is_refused()
|
||||
{
|
||||
Refuses(() => Plan(new[] { Box("A"), Box("B") with { Density = 0f } }, new[] { Joint("J", 0, 1) }), "density");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rule5_a_joint_with_a_non_finite_number_is_refused()
|
||||
{
|
||||
Refuses(() => Plan(new[] { Box("A"), Box("B") }, new[] { Joint("J", 0, 1) with { Axis = (0f, float.NaN, 0f) } }), "non-finite");
|
||||
Refuses(() => Plan(new[] { Box("A"), Box("B") }, new[] { Joint("J", 0, 1) with { Gains = new MuscleGains(float.PositiveInfinity, 1f, 1f, 1f) } }), "non-finite");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rule5_a_lower_limit_above_the_upper_is_refused()
|
||||
{
|
||||
Refuses(() => Plan(new[] { Box("A"), Box("B") }, new[] { Joint("J", 0, 1) with { Lower = 0.5f, Upper = 0.4f } }), "lower limit");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rule5_a_negative_cone_is_refused()
|
||||
{
|
||||
Refuses(() => Plan(new[] { Box("A"), Box("B") }, new[] { Joint("J", 0, 1) with { Cone = -0.1f } }), "cone");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rule5_a_non_finite_sense_offset_is_refused()
|
||||
{
|
||||
Refuses(() => Plan(new[] { Box("A"), Box("B") }, new[] { Joint("J", 0, 1) }, new[] { new SensePointDef("S", Side.Center, 0, (0f, float.NaN, 0f)) }), "non-finite");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_plan_copies_its_lists()
|
||||
{
|
||||
var parts = new List<PartDef> { Box("A"), Box("B") };
|
||||
var plan = Plan(parts.ToArray(), new[] { Joint("J", 0, 1) });
|
||||
var viaList = new BodyPlan(parts, new[] { Joint("J", 0, 1) }, Array.Empty<SensePointDef>());
|
||||
parts.Add(Box("C"));
|
||||
Assert.Equal(2, viaList.Parts.Count);
|
||||
Assert.Equal(2, plan.Parts.Count);
|
||||
}
|
||||
|
||||
// The humanoid.
|
||||
[Fact]
|
||||
public void The_humanoid_has_19_parts_18_joints_and_3_sense_points_labelled_in_build_order()
|
||||
{
|
||||
var plan = BodyPlans.Humanoid();
|
||||
string Name(string label, Side side) => side == Side.Center ? label : label + side;
|
||||
Assert.Equal(new[]
|
||||
{
|
||||
"Pelvis", "Torso", "Head",
|
||||
"UpperArmLeft", "LowerArmLeft", "ThighLeft", "ShinLeft", "FootLeft",
|
||||
"UpperArmRight", "LowerArmRight", "ThighRight", "ShinRight", "FootRight",
|
||||
"PalmLeft", "ThumbLeft", "FingersLeft", "PalmRight", "ThumbRight", "FingersRight",
|
||||
}, plan.Parts.Select(p => Name(p.Label, p.Side)).ToArray());
|
||||
Assert.Equal(new[]
|
||||
{
|
||||
"Spine", "Neck",
|
||||
"ShoulderLeft", "ElbowLeft", "HipLeft", "KneeLeft", "AnkleLeft",
|
||||
"ShoulderRight", "ElbowRight", "HipRight", "KneeRight", "AnkleRight",
|
||||
"WristLeft", "ThumbLeft", "FingersLeft", "WristRight", "ThumbRight", "FingersRight",
|
||||
}, plan.Joints.Select(j => Name(j.Label, j.Side)).ToArray());
|
||||
Assert.Equal(new[] { "HandLeft", "HandRight", "Head" }, plan.SensePoints.Select(s => Name(s.Label, s.Side)).ToArray());
|
||||
Assert.Equal(new[] { plan.PartIndex("Palm", Side.Left), plan.PartIndex("Palm", Side.Right), plan.PartIndex("Head") }, plan.SensePoints.Select(s => s.Part).ToArray());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_humanoid_parent_tree_is_the_old_one()
|
||||
{
|
||||
var plan = BodyPlans.Humanoid();
|
||||
// pelvis torso head uaL laL thL shL ftL uaR laR thR shR ftR plL thL fiL plR thR fiR
|
||||
var old = new[] { -1, 0, 1, 1, 3, 0, 5, 6, 1, 8, 0, 10, 11, 4, 13, 13, 9, 16, 16 };
|
||||
Assert.Equal(old, Enumerable.Range(0, plan.Parts.Count).Select(plan.ParentOf).ToArray());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_humanoid_hand_joints_hang_palm_thumb_fingers_off_the_lower_arm_per_side()
|
||||
{
|
||||
var plan = BodyPlans.Humanoid();
|
||||
foreach (var side in new[] { Side.Left, Side.Right })
|
||||
{
|
||||
Assert.Equal((plan.PartIndex("LowerArm", side), plan.PartIndex("Palm", side)),
|
||||
(plan.Joints[plan.JointIndex("Wrist", side)].Parent, plan.Joints[plan.JointIndex("Wrist", side)].Child));
|
||||
Assert.Equal((plan.PartIndex("Palm", side), plan.PartIndex("Thumb", side)),
|
||||
(plan.Joints[plan.JointIndex("Thumb", side)].Parent, plan.Joints[plan.JointIndex("Thumb", side)].Child));
|
||||
Assert.Equal((plan.PartIndex("Palm", side), plan.PartIndex("Fingers", side)),
|
||||
(plan.Joints[plan.JointIndex("Fingers", side)].Parent, plan.Joints[plan.JointIndex("Fingers", side)].Child));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_lookup_with_the_wrong_side_refuses_and_names_label_and_side()
|
||||
{
|
||||
var plan = BodyPlans.Humanoid();
|
||||
Refuses(() => plan.PartIndex("Palm"), "'Palm' on side Center");
|
||||
Refuses(() => plan.PartIndex("Pelvis", Side.Left), "'Pelvis' on side Left");
|
||||
Refuses(() => plan.JointIndex("Knee"), "'Knee' on side Center");
|
||||
Refuses(() => plan.SenseIndex("Head", Side.Right), "'Head' on side Right");
|
||||
Refuses(() => plan.PartIndex("Tail"), "'Tail'");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue