ReadAirway also writes each opening's facing against the caller's down, from the body's pose; the two-span form is deleted

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-26 03:07:05 +01:00
commit 66d96ac3eb
3 changed files with 65 additions and 11 deletions

View file

@ -15,7 +15,7 @@ are not coded.
Entry points: `src/Lodur.Core/Actor.cs` (one character's nervous state, one tick per
physics step), `src/Lodur.Core/PdMuscle.cs` (engine-free PD law, strength per muscle),
`src/Lodur.Core/BodyPlan.cs` (a body as data: parts, joints that are the tree, sense points, airway openings),
`src/Lodur.Body.Ymir/Ragdoll.cs` (builds a `BodyPlan` into a Ymir world; `Drive`, `ReadSensePoints`, and `ReadAirway`, which reads the liquid sensors it places at the airway openings),
`src/Lodur.Body.Ymir/Ragdoll.cs` (builds a `BodyPlan` into a Ymir world; `Drive`, `ReadSensePoints`, and `ReadAirway`, which reads the liquid sensors it places at the airway openings (live and pooled volume) and each opening's facing against the caller's down),
`tests/Lodur.Core.Tests` (the layers and parity with the pain box; builds without Ymir),
`tests/Lodur.Tests` (stand, fall, rest, same-seed-same-hash, and the connected pain box:
`ConnectedPainBoxTests`). The map this was cut from is

View file

@ -173,13 +173,17 @@ namespace Lodur.Body.Ymir
}
/// <summary>
/// Writes, per plan airway opening, the end-of-step volume (m^3) of live fluid and of settled (wet patch) liquid inside its sphere.
/// Zeros in a world with no particle system. Allocates nothing.
/// Writes, per plan airway opening, the end-of-step volume (m^3) of live fluid and of settled (wet patch) liquid inside its sphere
/// (zeros in a world with no particle system), and the opening's facing: the cosine between its part's rotation applied to the
/// opening's normalised offset and the normalised <paramref name="down"/> (1 pointing straight down, 0 on the side, -1 up).
/// Facing comes from the body's pose and is written in every world. Refuses spans not one per opening and a zero <paramref name="down"/>. Allocates nothing.
/// </summary>
public void ReadAirway(Span<float> live, Span<float> pooled)
public void ReadAirway(Span<float> live, Span<float> pooled, Span<float> facing, (float X, float Y, float Z) down)
{
var n = _sensorLane.Length;
if (live.Length != n || pooled.Length != n) throw new ArgumentException($"live and pooled need {n} entries, one per airway opening.");
if (live.Length != n || pooled.Length != n || facing.Length != n) throw new ArgumentException($"live, pooled and facing need {n} entries, one per airway opening.");
var dl = MathF.Sqrt(down.X * down.X + down.Y * down.Y + down.Z * down.Z);
if (!(dl > 0f) || !float.IsFinite(dl)) throw new ArgumentException("down needs a nonzero finite direction.", nameof(down));
var v = _world.View;
for (var k = 0; k < n; k++)
@ -187,6 +191,14 @@ namespace Lodur.Body.Ymir
var lane = _sensorLane[k];
live[k] = lane < 0 ? 0f : v.SensorLiquidVolume[lane];
pooled[k] = lane < 0 ? 0f : v.SensorWetVolume[lane];
var a = Plan.AirwayOpenings[k];
var i = _bodyLane[a.Part];
var o = a.Offset;
var ol = MathF.Sqrt(o.X * o.X + o.Y * o.Y + o.Z * o.Z);
if (ol <= 0f) { facing[k] = 0f; continue; }
new Quat(v.RotationX[i], v.RotationY[i], v.RotationZ[i], v.RotationW[i]).Rotate(o.X / ol, o.Y / ol, o.Z / ol, out var rx, out var ry, out var rz);
facing[k] = (rx * down.X + ry * down.Y + rz * down.Z) / dl;
}
}
}

View file

@ -42,10 +42,17 @@ public sealed class AirwayTests
{
var live = new float[Plan.AirwayOpenings.Count];
var pooled = new float[Plan.AirwayOpenings.Count];
doll.ReadAirway(live, pooled);
doll.ReadAirway(live, pooled, new float[Plan.AirwayOpenings.Count], (0f, -1f, 0f));
return (live, pooled);
}
private static float[] Facing(Ragdoll doll, (float X, float Y, float Z) down)
{
var facing = new float[Plan.AirwayOpenings.Count];
doll.ReadAirway(new float[facing.Length], new float[facing.Length], facing, down);
return facing;
}
[Fact]
public void A_drop_held_at_the_mouth_reads_at_the_mouth_only()
{
@ -117,9 +124,40 @@ public sealed class AirwayTests
world.Step(Dt, 4);
var live = new[] { 7f, 7f };
var pooled = new[] { 7f, 7f };
doll.ReadAirway(live, pooled);
var facing = new[] { 7f, 7f };
doll.ReadAirway(live, pooled, facing, (0f, 0f, 1f));
Assert.Equal(new[] { 0f, 0f }, live);
Assert.Equal(new[] { 0f, 0f }, pooled);
Assert.True(facing[Nose] > 0.9f && facing[Mouth] > 0.8f);
}
[Fact]
public void Facing_at_the_creation_pose_follows_down()
{
using var world = ParticleWorld(Ragdoll.Needs(Plan).Sensors);
var doll = Ragdoll.Build(world, Plan, (0f, 0f), firstStableId: 100);
var toward = Facing(doll, (0f, 0f, 1f));
Assert.True(toward[Nose] > 0.9f && toward[Mouth] > 0.8f);
var away = Facing(doll, (0f, 0f, -1f));
Assert.True(away[Nose] < -0.8f && away[Mouth] < -0.8f);
var side = Facing(doll, (0f, -1f, 0f));
Assert.True(MathF.Abs(side[Nose]) < 0.2f);
}
[Fact]
public void Facing_follows_the_head()
{
// A head part created turned 90 degrees about x (its +z face along -y): the facing is read from the lane's rotation, not from the plan's offsets.
var head = new PartDef("Head", Side.Center, PartShape.Box, (0.1f, 0.1f, 0.1f), (0f, 1f, 0f), Quat.FromAxisAngle(1f, 0f, 0f, MathF.PI / 2f), 1000f, 0.5f, 0f);
var turned = new BodyPlan([head], [], [], [.. Plan.AirwayOpenings.Select(o => o with { Part = 0 })]);
var (bodies, joints, _) = Ragdoll.Needs(turned);
using var world = new Box3DSoaWorld(bodies, gravityY: 0f, jointCapacity: joints);
var doll = Ragdoll.Build(world, turned, (0f, 0f), firstStableId: 100);
var facing = new float[2];
doll.ReadAirway(new float[2], new float[2], facing, (0f, -1f, 0f));
Assert.True(facing[Nose] > 0.8f && facing[Mouth] > 0.8f, $"nose {facing[Nose]} mouth {facing[Mouth]}");
doll.ReadAirway(new float[2], new float[2], facing, (0f, 0f, 1f));
Assert.True(MathF.Abs(facing[Nose]) < 0.2f);
}
[Fact]
@ -137,11 +175,15 @@ public sealed class AirwayTests
world.Step(Dt, 4);
var live = new float[2];
var pooled = new float[2];
doll.ReadAirway(live, pooled);
var facing = new float[2];
var down = (0f, -1f, 0f);
doll.ReadAirway(live, pooled, facing, down);
var before = GC.GetAllocatedBytesForCurrentThread();
for (var i = 0; i < 1000; i++) doll.ReadAirway(live, pooled);
for (var i = 0; i < 1000; i++) doll.ReadAirway(live, pooled, facing, down);
Assert.Equal(0L, GC.GetAllocatedBytesForCurrentThread() - before);
Assert.Throws<ArgumentException>(() => doll.ReadAirway(new float[1], pooled));
Assert.Throws<ArgumentException>(() => doll.ReadAirway(live, new float[3]));
Assert.Throws<ArgumentException>(() => doll.ReadAirway(new float[1], pooled, facing, down));
Assert.Throws<ArgumentException>(() => doll.ReadAirway(live, new float[3], facing, down));
Assert.Throws<ArgumentException>(() => doll.ReadAirway(live, pooled, new float[1], down));
Assert.Throws<ArgumentException>(() => doll.ReadAirway(live, pooled, facing, (0f, 0f, 0f)));
}
}