Body plan: airway openings as data (Mouth and Nose on the humanoid head), validated to reach outside their part
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
ae49a7de8c
commit
bbeed80af6
3 changed files with 160 additions and 3 deletions
|
|
@ -30,7 +30,13 @@ namespace Lodur
|
|||
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.
|
||||
/// Where the airway opens to the outside: a sphere of <see cref="Radius"/> m centred at <see cref="Offset"/> in the part's frame; liquid inside it is liquid
|
||||
/// at the airway. The sphere must reach outside the part's shape, since liquid never enters a body.
|
||||
/// </summary>
|
||||
public readonly record struct AirwayOpeningDef(string Label, Side Side, int Part, (float X, float Y, float Z) Offset, float Radius);
|
||||
|
||||
/// <summary>
|
||||
/// A body as data: parts, the joints that are also its tree, sense points and airway openings, 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
|
||||
|
|
@ -39,18 +45,22 @@ namespace Lodur
|
|||
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>();
|
||||
private readonly Dictionary<(string, Side), int> _airway = new Dictionary<(string, Side), int>();
|
||||
|
||||
public BodyPlan(IReadOnlyList<PartDef> parts, IReadOnlyList<JointDef> joints, IReadOnlyList<SensePointDef> sensePoints)
|
||||
public BodyPlan(IReadOnlyList<PartDef> parts, IReadOnlyList<JointDef> joints, IReadOnlyList<SensePointDef> sensePoints,
|
||||
IReadOnlyList<AirwayOpeningDef>? airwayOpenings = null)
|
||||
{
|
||||
Parts = new List<PartDef>(parts).AsReadOnly();
|
||||
Joints = new List<JointDef>(joints).AsReadOnly();
|
||||
SensePoints = new List<SensePointDef>(sensePoints).AsReadOnly();
|
||||
AirwayOpenings = new List<AirwayOpeningDef>(airwayOpenings ?? Array.Empty<AirwayOpeningDef>()).AsReadOnly();
|
||||
_parentOf = Validate();
|
||||
}
|
||||
|
||||
public IReadOnlyList<PartDef> Parts { get; }
|
||||
public IReadOnlyList<JointDef> Joints { get; }
|
||||
public IReadOnlyList<SensePointDef> SensePoints { get; }
|
||||
public IReadOnlyList<AirwayOpeningDef> AirwayOpenings { 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];
|
||||
|
|
@ -58,6 +68,7 @@ namespace Lodur
|
|||
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);
|
||||
public int AirwayIndex(string label, Side side = Side.Center) => Find(_airway, "airway opening", 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}.");
|
||||
|
|
@ -71,6 +82,7 @@ namespace Lodur
|
|||
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");
|
||||
Unique(AirwayOpenings.Count, i => (AirwayOpenings[i].Label, AirwayOpenings[i].Side), _airway, "airway opening");
|
||||
|
||||
// 2. Joint ends in range and ordered, so the tree is acyclic and parts are created parent first.
|
||||
foreach (var d in Joints)
|
||||
|
|
@ -111,6 +123,11 @@ namespace Lodur
|
|||
if (s.Part < 0 || s.Part >= Parts.Count) throw new ArgumentException($"sense point '{s.Label}' {s.Side}: part {s.Part} is not a part.");
|
||||
}
|
||||
|
||||
foreach (var a in AirwayOpenings)
|
||||
{
|
||||
if (a.Part < 0 || a.Part >= Parts.Count) throw new ArgumentException($"airway opening '{a.Label}' {a.Side}: part {a.Part} is not a part.");
|
||||
}
|
||||
|
||||
// 5. Numbers a physics world can take.
|
||||
foreach (var p in Parts)
|
||||
{
|
||||
|
|
@ -151,9 +168,50 @@ namespace Lodur
|
|||
if (!Finite(s.Offset)) throw new ArgumentException($"sense point '{s.Label}' {s.Side} has a non-finite number.");
|
||||
}
|
||||
|
||||
foreach (var a in AirwayOpenings)
|
||||
{
|
||||
if (!Finite(a.Offset) || !IsFinite(a.Radius)) throw new ArgumentException($"airway opening '{a.Label}' {a.Side} has a non-finite number.");
|
||||
if (!(a.Radius > 0f)) throw new ArgumentException($"airway opening '{a.Label}' {a.Side}: radius is not positive.");
|
||||
}
|
||||
|
||||
// 6. An opening's sphere reaches outside its part: liquid never enters a body, so a sphere wholly inside reads nothing.
|
||||
foreach (var a in AirwayOpenings)
|
||||
{
|
||||
if (SignedDistance(Parts[a.Part], a.Offset) <= -a.Radius)
|
||||
{
|
||||
throw new ArgumentException($"airway opening '{a.Label}' {a.Side} lies inside its part.");
|
||||
}
|
||||
}
|
||||
|
||||
return parentOf;
|
||||
}
|
||||
|
||||
/// <summary>Signed distance from a point in the part's frame to the part's shape: negative inside.</summary>
|
||||
private static float SignedDistance(PartDef p, (float X, float Y, float Z) o)
|
||||
{
|
||||
var sz = p.Size;
|
||||
switch (p.Shape)
|
||||
{
|
||||
case PartShape.Sphere:
|
||||
return MathF.Sqrt(o.X * o.X + o.Y * o.Y + o.Z * o.Z) - sz.X;
|
||||
case PartShape.Capsule:
|
||||
{
|
||||
var dy = o.Y - Math.Clamp(o.Y, -sz.Y, sz.Y);
|
||||
return MathF.Sqrt(o.X * o.X + dy * dy + o.Z * o.Z) - sz.X;
|
||||
}
|
||||
default:
|
||||
{
|
||||
var qx = Math.Abs(o.X) - sz.X;
|
||||
var qy = Math.Abs(o.Y) - sz.Y;
|
||||
var qz = Math.Abs(o.Z) - sz.Z;
|
||||
var ox = Math.Max(qx, 0f);
|
||||
var oy = Math.Max(qy, 0f);
|
||||
var oz = Math.Max(qz, 0f);
|
||||
return MathF.Sqrt(ox * ox + oy * oy + oz * oz) + Math.Min(Math.Max(qx, Math.Max(qy, qz)), 0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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++)
|
||||
|
|
|
|||
|
|
@ -99,7 +99,16 @@ namespace Lodur
|
|||
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);
|
||||
|
||||
// The airway opens at the face, facing +z, each centred on the head sphere's surface (|offset| 0.110, head r 0.11): fluid never enters
|
||||
// a body, so a sensor wholly inside the head reads 0 (probe, 512-drop pour: inside 0.0 mL; centred on the surface at the nose up to
|
||||
// 46.9 mL). Radius 0.025 holds one drop centre (r 1.25 cm) outside the solid; centres 5.05 cm apart, so the spheres do not overlap.
|
||||
var airway = new[]
|
||||
{
|
||||
new AirwayOpeningDef("Mouth", Side.Center, head, (0f, -0.04f, 0.1025f), 0.025f),
|
||||
new AirwayOpeningDef("Nose", Side.Center, head, (0f, 0.01f, 0.1095f), 0.025f),
|
||||
};
|
||||
return new BodyPlan(parts, joints, senses, airway);
|
||||
}
|
||||
|
||||
private static int Add(List<PartDef> parts, PartDef part)
|
||||
|
|
|
|||
|
|
@ -191,6 +191,83 @@ public sealed class BodyPlanTests
|
|||
Assert.Equal(2, plan.Parts.Count);
|
||||
}
|
||||
|
||||
// Airway openings: rules 1, 4, 5, 6.
|
||||
private static BodyPlan WithAirway(PartDef part, params AirwayOpeningDef[] openings) =>
|
||||
new(new[] { part }, Array.Empty<JointDef>(), Array.Empty<SensePointDef>(), openings);
|
||||
|
||||
private static PartDef Shaped(PartShape shape, (float, float, float) size) =>
|
||||
new("P", Side.Center, shape, size, (0f, 0f, 0f), Quat.Identity, 1000f, 0.8f, 0f);
|
||||
|
||||
private static AirwayOpeningDef Opening((float, float, float) offset, float radius = 0.025f, string label = "O") =>
|
||||
new(label, Side.Center, 0, offset, radius);
|
||||
|
||||
[Fact]
|
||||
public void Rule1_a_repeated_airway_label_and_side_is_refused_and_another_side_is_not()
|
||||
{
|
||||
var part = Shaped(PartShape.Sphere, (0.11f, 0f, 0f));
|
||||
Refuses(() => WithAirway(part, Opening((0f, 0f, 0.11f)), Opening((0f, 0.05f, 0.1f))), "two airway openings are labelled 'O'");
|
||||
var plan = WithAirway(part, Opening((0f, 0f, 0.11f)), Opening((0f, 0.05f, 0.1f), label: "O") with { Side = Side.Left });
|
||||
Assert.Equal(1, plan.AirwayIndex("O", Side.Left));
|
||||
Refuses(() => plan.AirwayIndex("O", Side.Right), "no airway opening labelled 'O' on side Right");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rule4_an_airway_opening_on_no_part_is_refused()
|
||||
{
|
||||
var part = Shaped(PartShape.Sphere, (0.11f, 0f, 0f));
|
||||
Refuses(() => WithAirway(part, Opening((0f, 0f, 0.11f)) with { Part = 1 }), "part 1 is not a part");
|
||||
Refuses(() => WithAirway(part, Opening((0f, 0f, 0.11f)) with { Part = -1 }), "part -1 is not a part");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rule5_a_non_finite_offset_or_radius_and_a_radius_not_above_zero_are_refused()
|
||||
{
|
||||
var part = Shaped(PartShape.Sphere, (0.11f, 0f, 0f));
|
||||
Refuses(() => WithAirway(part, Opening((0f, float.NaN, 0.11f))), "non-finite");
|
||||
Refuses(() => WithAirway(part, Opening((float.PositiveInfinity, 0f, 0.11f))), "non-finite");
|
||||
Refuses(() => WithAirway(part, Opening((0f, 0f, 0.11f), float.NaN)), "non-finite");
|
||||
Refuses(() => WithAirway(part, Opening((0f, 0f, 0.11f), 0f)), "radius is not positive");
|
||||
Refuses(() => WithAirway(part, Opening((0f, 0f, 0.11f), -0.01f)), "radius is not positive");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rule6_an_opening_wholly_inside_its_part_is_refused_and_one_straddling_the_surface_is_accepted_for_each_shape()
|
||||
{
|
||||
// sphere r 0.11: centre at 0.05 with radius 0.025 is wholly inside (distance -0.06); at the surface it straddles.
|
||||
var sphere = Shaped(PartShape.Sphere, (0.11f, 0f, 0f));
|
||||
Refuses(() => WithAirway(sphere, Opening((0f, 0f, 0.05f))), "lies inside its part");
|
||||
WithAirway(sphere, Opening((0f, 0f, 0.11f)));
|
||||
WithAirway(sphere, Opening((0f, 0f, 0.12f)));
|
||||
|
||||
// capsule r 0.05, half length 0.2 along y: past the cap is measured from the end of the segment, beside the side from the axis.
|
||||
var capsule = Shaped(PartShape.Capsule, (0.05f, 0.2f, 0f));
|
||||
Refuses(() => WithAirway(capsule, Opening((0f, 0.1f, 0.01f))), "lies inside its part");
|
||||
Refuses(() => WithAirway(capsule, Opening((0f, 0.2f, 0f))), "lies inside its part");
|
||||
Refuses(() => WithAirway(capsule, Opening((0f, 0.22f, 0f))), "lies inside its part"); // 0.02 past the segment end, r 0.05: inside by 0.03, more than the radius 0.025
|
||||
WithAirway(capsule, Opening((0.05f, 0.1f, 0f)));
|
||||
WithAirway(capsule, Opening((0f, 0.25f, 0f)));
|
||||
WithAirway(capsule, Opening((0f, 0.3f, 0f)));
|
||||
|
||||
// box half extents (0.1, 0.05, 0.2): the signed distance is the box's, so a point deep in one axis is not rescued by another.
|
||||
var box = Shaped(PartShape.Box, (0.1f, 0.05f, 0.2f));
|
||||
Refuses(() => WithAirway(box, Opening((0f, 0f, 0f))), "lies inside its part");
|
||||
Refuses(() => WithAirway(box, Opening((0.05f, 0.01f, 0.1f))), "lies inside its part");
|
||||
WithAirway(box, Opening((0.1f, 0f, 0f)));
|
||||
WithAirway(box, Opening((0f, 0.05f, 0f)));
|
||||
WithAirway(box, Opening((0.11f, 0.06f, 0.21f)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_airway_opening_list_is_copied_and_null_means_none()
|
||||
{
|
||||
var part = Shaped(PartShape.Sphere, (0.11f, 0f, 0f));
|
||||
var list = new List<AirwayOpeningDef> { Opening((0f, 0f, 0.11f)) };
|
||||
var plan = new BodyPlan(new[] { part }, Array.Empty<JointDef>(), Array.Empty<SensePointDef>(), list);
|
||||
list.Add(Opening((0f, 0f, 0.12f), label: "P"));
|
||||
Assert.Single(plan.AirwayOpenings);
|
||||
Assert.Empty(Valid().AirwayOpenings);
|
||||
}
|
||||
|
||||
// The humanoid.
|
||||
[Fact]
|
||||
public void The_humanoid_has_19_parts_18_joints_and_3_sense_points_labelled_in_build_order()
|
||||
|
|
@ -215,6 +292,19 @@ public sealed class BodyPlanTests
|
|||
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_has_a_mouth_and_a_nose_on_the_head_each_on_its_surface()
|
||||
{
|
||||
var plan = BodyPlans.Humanoid();
|
||||
Assert.Equal(new[] { "Mouth", "Nose" }, plan.AirwayOpenings.Select(a => a.Label).ToArray());
|
||||
foreach (var a in plan.AirwayOpenings)
|
||||
{
|
||||
Assert.Equal(plan.PartIndex("Head"), a.Part);
|
||||
var o = a.Offset;
|
||||
Assert.InRange(MathF.Sqrt(o.X * o.X + o.Y * o.Y + o.Z * o.Z), 0.109f, 0.111f);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_humanoid_parent_tree_is_the_old_one()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue