From 2cc609957038a33f87fe668f7d3acbd36734ac2a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 26 Nov 2026 03:40:49 +0100 Subject: [PATCH] Airway drain follows the drop of each opening's rim below the deepest airway sump; the facing cosine and DrainFacingStart are deleted ReadAirway writes per opening how far its lowest rim point lies below the deepest BodyPlan.AirwaySumps point along gravity; AirwaySample.Drop and AirwayDef.DrainDrop replace Facing and DrainFacingStart. A side-lying body now drains and an upright one does not; the humanoid pose path is pinned by creation-pose, rotated-head, side-lying fall, welded-stool and seated-fall tests. Co-Authored-By: Claude Sonnet 5.5 --- README.md | 2 +- src/Lodur.Body.Ymir/Ragdoll.cs | 41 +++- src/Lodur.Core/BodyPlan.cs | 24 ++- src/Lodur.Core/BodyPlans.cs | 10 +- src/Lodur.Core/Physiology.cs | 22 +- src/Lodur.Core/PhysiologyPlan.cs | 8 +- src/Lodur.Core/PhysiologyPlans.cs | 4 +- .../Lodur.Core.Tests/AirwayPhysiologyTests.cs | 55 ++--- tests/Lodur.Core.Tests/BodyPlanTests.cs | 22 +- tests/Lodur.Core.Tests/OrientTests.cs | 2 +- tests/Lodur.Core.Tests/PhysiologyFixtures.cs | 2 +- tests/Lodur.Tests/AirwayTests.cs | 200 +++++++++++++++--- 12 files changed, 303 insertions(+), 89 deletions(-) diff --git a/README.md b/README.md index a34a5e4..6639f7f 100644 --- a/README.md +++ b/README.md @@ -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 (live and pooled volume) and each opening's facing against the caller's down), +`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 drop below the airway sumps 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 diff --git a/src/Lodur.Body.Ymir/Ragdoll.cs b/src/Lodur.Body.Ymir/Ragdoll.cs index ff934df..75a6e48 100644 --- a/src/Lodur.Body.Ymir/Ragdoll.cs +++ b/src/Lodur.Body.Ymir/Ragdoll.cs @@ -174,18 +174,31 @@ namespace Lodur.Body.Ymir /// /// 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 (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 . Allocates nothing. + /// (zeros in a world with no particle system), and the opening's drop in m: with d the normalised , how far the opening's lowest rim point + /// lies below the deepest plan airway sump along d (positive below, negative above). The rim is the opening's circle of Radius in the plane across its outward normal + /// (its part's rotation applied to the normalised offset), so a mouth turned to the side still has its half-width of drop. Drop comes from the body's pose and is written in + /// every world. Refuses spans not one per opening and a zero . Allocates nothing. /// - public void ReadAirway(Span live, Span pooled, Span facing, (float X, float Y, float Z) down) + public void ReadAirway(Span live, Span pooled, Span drop, (float X, float Y, float Z) down) { var n = _sensorLane.Length; - if (live.Length != n || pooled.Length != n || facing.Length != n) throw new ArgumentException($"live, pooled and facing need {n} entries, one per airway opening."); + if (live.Length != n || pooled.Length != n || drop.Length != n) throw new ArgumentException($"live, pooled and drop 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 dx = down.X / dl; + var dy = down.Y / dl; + var dz = down.Z / dl; var v = _world.View; + var deepest = float.NegativeInfinity; + for (var m = 0; m < Plan.AirwaySumps.Count; m++) + { + var s = Plan.AirwaySumps[m]; + var i = _bodyLane[s.Part]; + new Quat(v.RotationX[i], v.RotationY[i], v.RotationZ[i], v.RotationW[i]).Rotate(s.Offset.X, s.Offset.Y, s.Offset.Z, out var rx, out var ry, out var rz); + deepest = MathF.Max(deepest, (v.PositionX[i] + rx) * dx + (v.PositionY[i] + ry) * dy + (v.PositionZ[i] + rz) * dz); + } + for (var k = 0; k < n; k++) { var lane = _sensorLane[k]; @@ -195,10 +208,22 @@ namespace Lodur.Body.Ymir var a = Plan.AirwayOpenings[k]; var i = _bodyLane[a.Part]; var o = a.Offset; + var q = new Quat(v.RotationX[i], v.RotationY[i], v.RotationZ[i], v.RotationW[i]); + q.Rotate(o.X, o.Y, o.Z, out var cx, out var cy, out var cz); + var centre = (v.PositionX[i] + cx) * dx + (v.PositionY[i] + cy) * dy + (v.PositionZ[i] + cz) * dz; 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; + var rim = a.Radius; + if (ol > 0f) + { + q.Rotate(o.X / ol, o.Y / ol, o.Z / ol, out var nx, out var ny, out var nz); + var along = dx * nx + dy * ny + dz * nz; + var px = dx - along * nx; + var py = dy - along * ny; + var pz = dz - along * nz; + rim *= MathF.Sqrt(px * px + py * py + pz * pz); + } + + drop[k] = centre + rim - deepest; } } } diff --git a/src/Lodur.Core/BodyPlan.cs b/src/Lodur.Core/BodyPlan.cs index f05701f..37a8d90 100644 --- a/src/Lodur.Core/BodyPlan.cs +++ b/src/Lodur.Core/BodyPlan.cs @@ -31,12 +31,15 @@ namespace Lodur /// /// Where the airway opens to the outside: a sphere of m centred at 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. + /// at the airway. The sphere must reach outside the part's shape, since liquid never enters a body. Its Radius is also its rim: the opening's half-width. /// public readonly record struct AirwayOpeningDef(string Label, Side Side, int Part, (float X, float Y, float Z) Offset, float Radius); + /// A point inside the airway, at in the part's frame, where liquid pools behind the openings (the oropharynx, the glottis). Nothing reads one by label. + public readonly record struct AirwaySumpDef(string Label, int Part, (float X, float Y, float Z) Offset); + /// - /// A body as data: parts, the joints that are also its tree, sense points and airway openings, each named by label and side. + /// A body as data: parts, the joints that are also its tree, sense points, airway openings and the sumps behind them, each named by label and side. /// Engine-free; a physics binding builds it into a world. Validated at construction. /// public sealed class BodyPlan @@ -48,12 +51,13 @@ namespace Lodur private readonly Dictionary<(string, Side), int> _airway = new Dictionary<(string, Side), int>(); public BodyPlan(IReadOnlyList parts, IReadOnlyList joints, IReadOnlyList sensePoints, - IReadOnlyList? airwayOpenings = null) + IReadOnlyList? airwayOpenings = null, IReadOnlyList? airwaySumps = null) { Parts = new List(parts).AsReadOnly(); Joints = new List(joints).AsReadOnly(); SensePoints = new List(sensePoints).AsReadOnly(); AirwayOpenings = new List(airwayOpenings ?? Array.Empty()).AsReadOnly(); + AirwaySumps = new List(airwaySumps ?? Array.Empty()).AsReadOnly(); _parentOf = Validate(); } @@ -61,6 +65,7 @@ namespace Lodur public IReadOnlyList Joints { get; } public IReadOnlyList SensePoints { get; } public IReadOnlyList AirwayOpenings { get; } + public IReadOnlyList AirwaySumps { get; } /// The part ordinal a part hangs from (the parent of the one joint that attaches it), -1 for the root. public int ParentOf(int part) => _parentOf[part]; @@ -128,6 +133,14 @@ namespace Lodur if (a.Part < 0 || a.Part >= Parts.Count) throw new ArgumentException($"airway opening '{a.Label}' {a.Side}: part {a.Part} is not a part."); } + foreach (var a in AirwaySumps) + { + if (a.Part < 0 || a.Part >= Parts.Count) throw new ArgumentException($"airway sump '{a.Label}': part {a.Part} is not a part."); + } + + // An opening's drop is measured below the deepest sump: openings with none would have nothing to fall below. + if (AirwayOpenings.Count > 0 && AirwaySumps.Count == 0) throw new ArgumentException("airway openings need at least one airway sump to drain below."); + // 5. Numbers a physics world can take. foreach (var p in Parts) { @@ -174,6 +187,11 @@ namespace Lodur if (!(a.Radius > 0f)) throw new ArgumentException($"airway opening '{a.Label}' {a.Side}: radius is not positive."); } + foreach (var a in AirwaySumps) + { + if (!Finite(a.Offset)) throw new ArgumentException($"airway sump '{a.Label}' has a non-finite number."); + } + // 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) { diff --git a/src/Lodur.Core/BodyPlans.cs b/src/Lodur.Core/BodyPlans.cs index 8356275..f2f803f 100644 --- a/src/Lodur.Core/BodyPlans.cs +++ b/src/Lodur.Core/BodyPlans.cs @@ -108,7 +108,15 @@ namespace Lodur 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); + // Liquid pools behind the openings at the airway's deepest points: the oropharynx 8 cm behind the lips lying on the back, the glottis 7 cm below the mouth upright (placeholders). + // An opening drains by gravity when its lowest rim point lies below the deepest of them: the principle of the recovery position, the mouth dependent so fluid runs out + // (ERC 2021 BLS, Resuscitation 161:98-114), and of postural drainage, the outlet below the reservoir. + var sumps = new[] + { + new AirwaySumpDef("Pharynx", head, (0f, -0.06f, 0.02f)), + new AirwaySumpDef("Glottis", head, (0f, -0.11f, 0.04f)), + }; + return new BodyPlan(parts, joints, senses, airway, sumps); } private static int Add(List parts, PartDef part) diff --git a/src/Lodur.Core/Physiology.cs b/src/Lodur.Core/Physiology.cs index b559e19..f78b8b1 100644 --- a/src/Lodur.Core/Physiology.cs +++ b/src/Lodur.Core/Physiology.cs @@ -8,18 +8,18 @@ namespace Lodur /// /// The liquid at a body's airway openings this step, as the caller measures it: the live liquid and the pooled liquid, each a non-negative amount - /// summed over the openings, and Facing, the greatest cosine between an opening's outward direction and down, in [-1, 1] (1: an opening faces straight down). + /// summed over the openings, and Drop in m, at least 0: how far the lowest opening's rim lies below the deepest point liquid pools at inside the airway, 0 when none lies below. /// The default is nothing at the airway. /// - public readonly record struct AirwaySample(double Live, double Pooled, double Facing) + public readonly record struct AirwaySample(double Live, double Pooled, double Drop) { - /// One entry per opening: Live and Pooled summed, Facing the largest (-1 with no opening). - public static AirwaySample Of(ReadOnlySpan live, ReadOnlySpan pooled, ReadOnlySpan facing) + /// One entry per opening: Live and Pooled summed, Drop the largest of the openings' signed drops, floored at 0 (0 with no opening). + public static AirwaySample Of(ReadOnlySpan live, ReadOnlySpan pooled, ReadOnlySpan drop) { - double l = 0.0, p = 0.0, f = -1.0; + double l = 0.0, p = 0.0, f = 0.0; for (var i = 0; i < live.Length; i++) l += live[i]; for (var i = 0; i < pooled.Length; i++) p += pooled[i]; - for (var i = 0; i < facing.Length; i++) f = Math.Max(f, facing[i]); + for (var i = 0; i < drop.Length; i++) f = Math.Max(f, drop[i]); return new AirwaySample(l, p, f); } } @@ -201,7 +201,7 @@ namespace Lodur /// The liquid in m^3 the last step coughed out of the airway. public double Coughed => _coughed; - /// The liquid in m^3 the last step drained out of the airway (facing down). + /// The liquid in m^3 the last step drained out of the airway (by gravity through the mouth). public double Drained => _drained; /// The tissue's remaining fraction, 0..1, as of the last step. @@ -282,9 +282,9 @@ namespace Lodur } var air = input.Airway; - if (!(air.Live >= 0.0 && air.Live < double.PositiveInfinity && air.Pooled >= 0.0 && air.Pooled < double.PositiveInfinity && air.Facing >= -1.0 && air.Facing <= 1.0)) + if (!(air.Live >= 0.0 && air.Live < double.PositiveInfinity && air.Pooled >= 0.0 && air.Pooled < double.PositiveInfinity && air.Drop >= 0.0 && air.Drop < double.PositiveInfinity)) { - throw new ArgumentException("the airway sample's liquid must be finite and not negative and its facing in [-1, 1]."); + throw new ArgumentException("the airway sample's liquid must be finite and not negative and its drop finite and not negative."); } if (Dead) return; @@ -318,14 +318,14 @@ namespace Lodur _breathing = Capacity(EffectTarget.Breathing); // 3b. The airway, a quantity (not particles): liquid at the openings is drawn in by a free breath, never by a held one; a cough clears it by this step's - // consciousness, and it drains by facing. What a step removes is reported as Coughed and Drained; occlusion takes its share of the ventilation below. + // consciousness, and it drains by gravity through the mouth. What a step removes is reported as Coughed and Drained; occlusion takes its share of the ventilation below. var held = input.BreathHeld; var aw = _airway; var at = aw.LiveWeight * air.Live + aw.PooledWeight * air.Pooled; _atAirway = at > 0.0 && at >= aw.HoldAt; var inflow = held ? 0.0 : aw.DrawPerSecond * _breathing * at; var cough = aw.CoughPerSecond * Effect.EvalCurve(aw.CoughByConsciousness, _consciousness); - var drain = aw.DrainPerSecond * Clamp01((air.Facing - aw.DrainFacingStart) / (1.0 - aw.DrainFacingStart)); + var drain = aw.DrainPerSecond * Clamp01(air.Drop / aw.DrainDrop); var total = cough + drain; var avail = _airwayLiquid + dt * inflow; var want = dt * total * _airwayLiquid; diff --git a/src/Lodur.Core/PhysiologyPlan.cs b/src/Lodur.Core/PhysiologyPlan.cs index a38ce34..3940131 100644 --- a/src/Lodur.Core/PhysiologyPlan.cs +++ b/src/Lodur.Core/PhysiologyPlan.cs @@ -67,12 +67,12 @@ namespace Lodur /// x the live liquid plus x the pooled; at or more it is "at the airway" /// and the Actor holds the breath. A free breath draws x Breathing x (liquid at the airway) per second into it; a cough /// removes x (consciousness) x contents per second; and it drains at - /// x clamp01((facing - ) / (1 - DrainFacingStart)) x contents, facing being the cosine of an - /// opening's outward direction with down, -1..1. + /// x clamp01(drop / ) x contents, drop being how far (m) the lowest opening's rim lies below the deepest airway sump along + /// gravity: m of drop drains at the full rate. /// public readonly record struct AirwayDef( double Capacity, double DrawPerSecond, double LiveWeight, double PooledWeight, double HoldAt, double CoughPerSecond, - IReadOnlyList<(double X, double Y)> CoughByConsciousness, double DrainPerSecond, double DrainFacingStart); + IReadOnlyList<(double X, double Y)> CoughByConsciousness, double DrainPerSecond, double DrainDrop); /// /// A piecewise-linear curve in its driver, clamped at the ends. An equal-x pair is a step: at that x the later value holds @@ -298,7 +298,7 @@ namespace Lodur throw new ArgumentException("airway: a number is not finite and at least 0."); } - if (!(w.DrainFacingStart >= -1.0 && w.DrainFacingStart < 1.0)) throw new ArgumentException("airway: DrainFacingStart is not in [-1, 1)."); + if (!(w.DrainDrop > 0.0 && w.DrainDrop < double.PositiveInfinity)) throw new ArgumentException("airway: DrainDrop is not finite and above 0."); ValidateCurve(w.CoughByConsciousness, "airway: the cough curve"); if (AfflictionSlots < 1) throw new ArgumentException("the affliction table needs at least one slot."); } diff --git a/src/Lodur.Core/PhysiologyPlans.cs b/src/Lodur.Core/PhysiologyPlans.cs index b09be8d..4580744 100644 --- a/src/Lodur.Core/PhysiologyPlans.cs +++ b/src/Lodur.Core/PhysiologyPlans.cs @@ -93,8 +93,8 @@ namespace Lodur new[] { (0.5, 0.0), (1.3, 1.0) }, new[] { (0.5, 0.0), (1.0, 0.6) }), // The airway (placeholders). Capacity 150 mL is 2 mL/kg at 75 kg (an upper-airway and bronchial volume, the lung's own is far larger: the airway fills before the lung); it holds the breath from 15.6 mL // of liquid at the openings (a mouthful, a tenth of the capacity). A free breath draws the liquid at the airway in at 1 per second, an awake cough clears 2 per second of what is there and fails below 0.3 - // consciousness, and the airway drains at 0.1 per second once the face points down (facing from 0: on his side halves it). Liquid and pooled liquid count alike. - new AirwayDef(150e-6, 1.0, 1.0, 1.0, 15.6e-6, 2.0, new[] { (0.3, 0.0), (0.6, 1.0) }, 0.1, 0.0), 64); + // consciousness, and the airway drains at 0.1 per second once the lowest rim of an opening lies 5 cm below the deepest sump (a true side pose, drop 0.025 m the mouth's half-width, halves it; side-lying falls measure a median 0.043 m, upright -0.047 m, so he drains nothing standing). Liquid and pooled liquid count alike. + new AirwayDef(150e-6, 1.0, 1.0, 1.0, 15.6e-6, 2.0, new[] { (0.3, 0.0), (0.6, 1.0) }, 0.1, 0.05), 64); } } } diff --git a/tests/Lodur.Core.Tests/AirwayPhysiologyTests.cs b/tests/Lodur.Core.Tests/AirwayPhysiologyTests.cs index 33adbd3..98b9eb4 100644 --- a/tests/Lodur.Core.Tests/AirwayPhysiologyTests.cs +++ b/tests/Lodur.Core.Tests/AirwayPhysiologyTests.cs @@ -109,7 +109,7 @@ public sealed class AirwayPhysiologyTests private readonly record struct Hold(double FirstLiquid, double MaxBeforeGasp, double Gasp, double WillAtGasp, double After, double Window); - /// A real Actor, calm and awake, with liquid at the airway openings from the first tick on, facing down. + /// A real Actor, calm and awake, with liquid at the airway openings from the first tick on, no opening below the sump. private static Hold HoldUnderLiquid(double slowScale, WithdrawalParameters? breath = null, double attention = 1.0) { var p = Breather(slowScale); @@ -117,7 +117,7 @@ public sealed class AirwayPhysiologyTests a.SetAttention(attention); var s = new float[2]; var q = new Quat[2]; - var liquid = new AirwaySample(2.0 * HoldAt, 0.0, -1.0); + var liquid = new AirwaySample(2.0 * HoldAt, 0.0, 0.0); double first = -1, max = 0, gasp = -1, will = -1, after = -1, window = -1; for (long i = 0; i < (long)(200.0 / Dt); i++) { @@ -188,7 +188,7 @@ public sealed class AirwayPhysiologyTests a.SetAttention(0.0); var s = new float[2]; var q = new Quat[2]; - var liquid = new AirwaySample(2.0 * HoldAt, 0.0, -1.0); + var liquid = new AirwaySample(2.0 * HoldAt, 0.0, 0.0); for (long i = 0; i < (long)(5.0 / Dt); i++) { Assert.Equal(TickEvents.None, a.Tick(NoPoint, NoPoint, NoPoint, default, s, q, default, liquid) & TickEvents.Gasp); @@ -204,30 +204,30 @@ public sealed class AirwayPhysiologyTests private static Physiology Drowned() { var p = Breather(20.0); - var flood = new PhysiologyInput(0.0, 0.0, false, new AirwaySample(100.0 * HoldAt, 0.0, -1.0)); + var flood = new PhysiologyInput(0.0, 0.0, false, new AirwaySample(100.0 * HoldAt, 0.0, 0.0)); for (long i = 0; i < (long)(600.0 / Dt) && p.Consciousness > 0.1; i++) p.Step(Dt, flood); Assert.True(p.Consciousness <= 0.1 && !p.Dead, $"consciousness {p.Consciousness}"); Assert.Equal(Capacity, p.AirwayLiquid); return p; } - /// The unconscious Fella, liquid no longer at the airway, with his face this way; steps for the given seconds. - private static Physiology Rolled(double facing, double seconds) + /// The unconscious Fella, liquid no longer at the airway, with his lowest opening rim this far (m) below the deepest sump; steps for the given seconds. + private static Physiology Rolled(double drop, double seconds) { var p = Drowned(); - var rolled = new PhysiologyInput(0.0, 0.0, false, new AirwaySample(0.0, 0.0, facing)); + var rolled = new PhysiologyInput(0.0, 0.0, false, new AirwaySample(0.0, 0.0, drop)); for (long i = 0; i < (long)(seconds / Dt) && !p.Dead; i++) p.Step(Dt, rolled); return p; } [Theory] - [InlineData(1.0)] - [InlineData(0.5)] - public void Face_down_or_on_his_side_the_airway_drains_and_he_lives(double facing) + [InlineData(0.05)] + [InlineData(0.025)] + public void Face_down_or_on_his_side_the_airway_drains_and_he_lives(double drop) { // Placeholders: Drain decides nothing here but whether it is above zero (a tiny drain restarts ventilation and the awake cough then clears the airway: probed at 0.1, 0.01, 0.001); Cough decides the recovery once he wakes. - // Out at 0.1 with the airway full, then drained at 0.1 (facing 1) or 0.05 (facing 0.5) per second: back above 0.5 in 7.0 and 8.5 s (the bound is the 200 s simulated; a drain that waits on consciousness never comes). Face up it never drains and he dies (below). - var p = Rolled(facing, 200.0); + // Out at 0.1 with the airway full, then drained at 0.1 (drop 0.05, face down) or 0.05 (drop 0.025, on his side) per second: back above 0.5 in 7.0 and 8.5 s (the bound is the 200 s simulated; a drain that waits on consciousness never comes). Face up it never drains and he dies (below). + var p = Rolled(drop, 200.0); Assert.False(p.Dead); Assert.True(p.Consciousness > 0.5, $"consciousness {p.Consciousness}"); Assert.True(p.AirwayLiquid < 1e-6, $"airway liquid {p.AirwayLiquid}"); @@ -236,8 +236,8 @@ public sealed class AirwayPhysiologyTests [Fact] public void Face_up_the_airway_stays_full_and_he_dies_through_consciousness() { - // Decided by the Cough curve (zero below 0.3 consciousness) and by facing; Drain and Draw decide nothing here (nothing drains face up, nothing is drawn, none at the openings). - var p = Rolled(-1.0, 600.0); + // Decided by the Cough curve (zero below 0.3 consciousness) and by drop (0 face up: no opening rim lies below the sump); Drain and Draw decide nothing here (nothing drains face up, nothing is drawn, none at the openings). + var p = Rolled(0.0, 600.0); Assert.True(p.Dead); Assert.Equal(DeathCause.Consciousness, p.Cause); Assert.Equal(Capacity, p.AirwayLiquid); @@ -248,7 +248,7 @@ public sealed class AirwayPhysiologyTests { var p = Breather(20.0); for (var d = 0; d < dims; d++) Assert.True(p.Afflict(Af("Dim"), 0, BrainLayer, 1.0)); - var flood = new PhysiologyInput(0.0, 0.0, false, new AirwaySample(100.0 * HoldAt, 0.0, -1.0)); + var flood = new PhysiologyInput(0.0, 0.0, false, new AirwaySample(100.0 * HoldAt, 0.0, 0.0)); for (long i = 0; i < (long)(2.0 / Dt); i++) p.Step(Dt, flood); Assert.Equal(Capacity, p.AirwayLiquid); return p; @@ -257,12 +257,12 @@ public sealed class AirwayPhysiologyTests [Fact] public void An_awake_Fella_coughs_his_airway_clear() { - // Placeholders deciding this: Cough (2.2 s to clear half, and every bound below); Draw only fills the half airway first and decides no outcome; Drain is 0 facing up and decides nothing. + // Placeholders deciding this: Cough (2.2 s to clear half, and every bound below); Draw only fills the half airway first and decides no outcome; Drain is 0 face up and decides nothing. // Half the airway, awake, face up, nothing at the openings: gone (below 1e-6, a 75-fold fall) in 10 s; shipped it takes 2.2 s. Without the cough (or with one gated by something else) it never falls. var p = Breather(20.0); - var flood = new PhysiologyInput(0.0, 0.0, false, new AirwaySample(100.0 * HoldAt, 0.0, -1.0)); + var flood = new PhysiologyInput(0.0, 0.0, false, new AirwaySample(100.0 * HoldAt, 0.0, 0.0)); while (p.AirwayLiquid < 0.5 * Capacity) p.Step(Dt, flood); - var clear = new PhysiologyInput(0.0, 0.0, false, new AirwaySample(0.0, 0.0, -1.0)); + var clear = new PhysiologyInput(0.0, 0.0, false, new AirwaySample(0.0, 0.0, 0.0)); double coughed = 0.0; for (long i = 0; i < (long)(10.0 / Dt); i++) { @@ -278,10 +278,10 @@ public sealed class AirwayPhysiologyTests [Fact] public void A_stuporous_Fella_cannot_cough() { - // Consciousness held at 0.2 by eight Dim afflictions, below the cough's 0.3: the Cough curve's knee (a placeholder) decides this; Drain decides nothing (facing up). + // Consciousness held at 0.2 by eight Dim afflictions, below the cough's 0.3: the Cough curve's knee (a placeholder) decides this; Drain decides nothing (face up). var p = Flooded(8); Assert.Equal(0.2, p.Consciousness, 9); - var clear = new PhysiologyInput(0.0, 0.0, false, new AirwaySample(0.0, 0.0, -1.0)); + var clear = new PhysiologyInput(0.0, 0.0, false, new AirwaySample(0.0, 0.0, 0.0)); for (long i = 0; i < (long)(10.0 / Dt); i++) { p.Step(Dt, clear); @@ -333,7 +333,7 @@ public sealed class AirwayPhysiologyTests var a = Real(p, StruggleBrain()); var s = new float[2]; var q = new Quat[2]; - var low = new AirwaySample(0.5 * HoldAt, 0.0, -1.0); + var low = new AirwaySample(0.5 * HoldAt, 0.0, 0.0); for (long i = 0; i < (long)(5.0 / Dt); i++) { Assert.Equal(TickEvents.None, a.Tick(NoPoint, NoPoint, NoPoint, default, s, q, default, low) & TickEvents.Gasp); @@ -350,7 +350,7 @@ public sealed class AirwayPhysiologyTests { var p = Breather(20.0); if (lungLost) Assert.True(p.Afflict(Af("Lesion"), 0, 2, 5.0)); - p.Step(Dt, new PhysiologyInput(0.0, 0.0, false, new AirwaySample(0.5 * HoldAt, 0.0, -1.0))); + p.Step(Dt, new PhysiologyInput(0.0, 0.0, false, new AirwaySample(0.5 * HoldAt, 0.0, 0.0))); Assert.Equal(lungLost ? 0.5 : 1.0, p.Breathing); return p.AirwayLiquid; } @@ -376,25 +376,26 @@ public sealed class AirwayPhysiologyTests } [Fact] - public void An_airway_sample_is_the_sum_of_what_is_live_and_pooled_and_the_greatest_facing() + public void An_airway_sample_is_the_sum_of_what_is_live_and_pooled_and_the_greatest_drop() { var s = AirwaySample.Of(new[] { 1f, 2f }, new[] { 0.5f }, new[] { -0.5f, 0.25f, -1f }); Assert.Equal(new AirwaySample(3.0, 0.5, 0.25), s); - Assert.Equal(new AirwaySample(0.0, 0.0, -1.0), AirwaySample.Of(default, default, default)); + Assert.Equal(new AirwaySample(1.0, 1.0, 0.0), AirwaySample.Of(new[] { 1f }, new[] { 1f }, new[] { -0.5f, -1f })); + Assert.Equal(new AirwaySample(0.0, 0.0, 0.0), AirwaySample.Of(default, default, default)); } [Fact] public void A_sample_outside_its_range_is_refused_before_anything_changes() { var p = Breather(20.0); - foreach (var bad in new[] { new AirwaySample(-1e-9, 0, 0), new AirwaySample(0, double.NaN, 0), new AirwaySample(double.PositiveInfinity, 0, 0), new AirwaySample(0, 0, 1.01), new AirwaySample(0, 0, -1.01), new AirwaySample(0, 0, double.NaN) }) + foreach (var bad in new[] { new AirwaySample(-1e-9, 0, 0), new AirwaySample(0, double.NaN, 0), new AirwaySample(double.PositiveInfinity, 0, 0), new AirwaySample(0, 0, -1e-9), new AirwaySample(0, 0, double.NaN), new AirwaySample(0, 0, double.PositiveInfinity) }) { Assert.Throws(() => p.Step(Dt, new PhysiologyInput(0.0, 0.0, false, bad))); } Assert.Equal(1.0, p.BloodOxygen); Assert.Equal(0.0, p.AirwayLiquid); - p.Step(Dt, new PhysiologyInput(0.0, 0.0, false, new AirwaySample(1.0, 1.0, 1.0))); + p.Step(Dt, new PhysiologyInput(0.0, 0.0, false, new AirwaySample(1.0, 1.0, 0.05))); } [Fact] @@ -409,7 +410,7 @@ public sealed class AirwayPhysiologyTests foreach (var bad in new[] { ok with { Capacity = 0.0 }, ok with { Capacity = double.NaN }, ok with { DrawPerSecond = -1.0 }, ok with { HoldAt = double.PositiveInfinity }, - ok with { CoughPerSecond = -0.1 }, ok with { DrainPerSecond = double.NaN }, ok with { DrainFacingStart = 1.0 }, ok with { DrainFacingStart = -1.5 }, + ok with { CoughPerSecond = -0.1 }, ok with { DrainPerSecond = double.NaN }, ok with { DrainDrop = 0.0 }, ok with { DrainDrop = -0.01 }, ok with { DrainDrop = double.NaN }, ok with { CoughByConsciousness = Array.Empty<(double X, double Y)>() }, ok with { CoughByConsciousness = new[] { (1.0, 0.0), (0.0, 1.0) } }, ok with { LiveWeight = -1.0 }, ok with { PooledWeight = double.NaN }, }) diff --git a/tests/Lodur.Core.Tests/BodyPlanTests.cs b/tests/Lodur.Core.Tests/BodyPlanTests.cs index c9c05c6..1b854bb 100644 --- a/tests/Lodur.Core.Tests/BodyPlanTests.cs +++ b/tests/Lodur.Core.Tests/BodyPlanTests.cs @@ -193,7 +193,10 @@ public sealed class BodyPlanTests // Airway openings: rules 1, 4, 5, 6. private static BodyPlan WithAirway(PartDef part, params AirwayOpeningDef[] openings) => - new(new[] { part }, Array.Empty(), Array.Empty(), openings); + new(new[] { part }, Array.Empty(), Array.Empty(), openings, new[] { new AirwaySumpDef("S", 0, (0f, 0f, 0f)) }); + + private static BodyPlan WithSumps(PartDef part, params AirwaySumpDef[] sumps) => + new(new[] { part }, Array.Empty(), Array.Empty(), new[] { Opening((0f, 0f, 0.11f)) }, sumps); 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); @@ -257,12 +260,25 @@ public sealed class BodyPlanTests WithAirway(box, Opening((0.11f, 0.06f, 0.21f))); } + [Fact] + public void Airway_openings_need_a_sump_on_a_part_with_a_finite_offset() + { + var part = Shaped(PartShape.Sphere, (0.11f, 0f, 0f)); + Refuses(() => WithSumps(part), "need at least one airway sump"); + Refuses(() => WithSumps(part, new AirwaySumpDef("S", 1, (0f, 0f, 0f))), "part 1 is not a part"); + Refuses(() => WithSumps(part, new AirwaySumpDef("S", -1, (0f, 0f, 0f))), "part -1 is not a part"); + Refuses(() => WithSumps(part, new AirwaySumpDef("S", 0, (0f, float.NaN, 0f))), "non-finite"); + Refuses(() => WithSumps(part, new AirwaySumpDef("S", 0, (float.PositiveInfinity, 0f, 0f))), "non-finite"); + Assert.Single(WithSumps(part, new AirwaySumpDef("S", 0, (0f, 0f, 0f))).AirwaySumps); + Assert.Empty(Valid().AirwaySumps); + } + [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 { Opening((0f, 0f, 0.11f)) }; - var plan = new BodyPlan(new[] { part }, Array.Empty(), Array.Empty(), list); + var plan = new BodyPlan(new[] { part }, Array.Empty(), Array.Empty(), list, new[] { new AirwaySumpDef("S", 0, (0f, 0f, 0f)) }); list.Add(Opening((0f, 0f, 0.12f), label: "P")); Assert.Single(plan.AirwayOpenings); Assert.Empty(Valid().AirwayOpenings); @@ -297,6 +313,8 @@ public sealed class BodyPlanTests { var plan = BodyPlans.Humanoid(); Assert.Equal(new[] { "Mouth", "Nose" }, plan.AirwayOpenings.Select(a => a.Label).ToArray()); + Assert.Equal(new[] { "Pharynx", "Glottis" }, plan.AirwaySumps.Select(a => a.Label).ToArray()); + Assert.All(plan.AirwaySumps, s => Assert.Equal(plan.PartIndex("Head"), s.Part)); foreach (var a in plan.AirwayOpenings) { Assert.Equal(plan.PartIndex("Head"), a.Part); diff --git a/tests/Lodur.Core.Tests/OrientTests.cs b/tests/Lodur.Core.Tests/OrientTests.cs index 6518579..63f6598 100644 --- a/tests/Lodur.Core.Tests/OrientTests.cs +++ b/tests/Lodur.Core.Tests/OrientTests.cs @@ -580,7 +580,7 @@ public sealed class OrientTests for (var j = 0; j < Joints; j++) joints[j] = Plan.Joints[j]; var i = Plan.JointIndex(label, side); joints[i] = change(joints[i]); - return new BodyPlan(Plan.Parts, joints, Plan.SensePoints, Plan.AirwayOpenings); + return new BodyPlan(Plan.Parts, joints, Plan.SensePoints, Plan.AirwayOpenings, Plan.AirwaySumps); } [Fact] diff --git a/tests/Lodur.Core.Tests/PhysiologyFixtures.cs b/tests/Lodur.Core.Tests/PhysiologyFixtures.cs index f0c3fdf..d0731a1 100644 --- a/tests/Lodur.Core.Tests/PhysiologyFixtures.cs +++ b/tests/Lodur.Core.Tests/PhysiologyFixtures.cs @@ -51,7 +51,7 @@ internal static class PhysioFixtures public static RespirationDef DefaultRespiration => PhysiologyPlans.Humanoid().Respiration; /// No liquid ever counts as at the airway: the airway stays empty and the hold is never wanted. - public static AirwayDef InertAirway => new(150e-6, 1.0, 1.0, 1.0, 1e9, 2.0, new[] { (0.3, 0.0), (0.6, 1.0) }, 0.1, 0.0); + public static AirwayDef InertAirway => new(150e-6, 1.0, 1.0, 1.0, 1e9, 2.0, new[] { (0.3, 0.0), (0.6, 1.0) }, 0.1, 0.05); /// The humanoid's hypoxia curve: brain oxygen to consciousness, flat 1 above 0.70. public static Effect Hypoxia => new(EffectTarget.Consciousness, EffectOp.Factor, EffectDriver.BrainOxygen, new[] { (0.10, 0.0), (0.35, 0.1), (0.70, 1.0) }); diff --git a/tests/Lodur.Tests/AirwayTests.cs b/tests/Lodur.Tests/AirwayTests.cs index 32bf66e..9c48afe 100644 --- a/tests/Lodur.Tests/AirwayTests.cs +++ b/tests/Lodur.Tests/AirwayTests.cs @@ -12,6 +12,17 @@ public sealed class AirwayTests private const float DropVolume = (2f * R) * (2f * R) * (2f * R); private static readonly uint FluidPhase = Box3DSoaPhase.Of(0, fluid: true); + // Bounds, from deterministic-bound-margin (readings are identical run to run). Side-lying falls (12 seeds): shipped worst drop 0.0132 m, least mutant (a head read from the pelvis's lane) + // reads at or below 0 on 5 of 12, counted as one unit of 1 mm by the zero-reading convention: bound sqrt(0.0132 x 0.001) = 0.0036 m, separation 13. + private const float SideDropBound = 0.0036f; + + // Upright (creation pose -0.0467, welded stool at full strength -0.0461 m): least mutant, the shallowest sump, reads +0.005. A bound across zero has no geometric midpoint: the + // arithmetic one, -0.0206 m, sits 0.0255 m from each side. + private const float UprightBound = -0.0206f; + + // Limp seated falls (40 seeds): shipped 31 drain, the pelvis-lane mutant 0 (counted as 1): bound sqrt(31 x 1) = 5.6, so at least 6 drain. + private const int SeatedDraining = 6; + private static readonly BodyPlan Plan = BodyPlans.Humanoid(); private static readonly int Mouth = Plan.AirwayIndex("Mouth"); private static readonly int Nose = Plan.AirwayIndex("Nose"); @@ -46,11 +57,11 @@ public sealed class AirwayTests return (live, pooled); } - private static float[] Facing(Ragdoll doll, (float X, float Y, float Z) down) + private static float[] DropOf(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; + var drop = new float[Plan.AirwayOpenings.Count]; + doll.ReadAirway(new float[drop.Length], new float[drop.Length], drop, down); + return drop; } [Fact] @@ -124,40 +135,173 @@ public sealed class AirwayTests world.Step(Dt, 4); var live = new[] { 7f, 7f }; var pooled = new[] { 7f, 7f }; - var facing = new[] { 7f, 7f }; - doll.ReadAirway(live, pooled, facing, (0f, 0f, 1f)); + var drop = new[] { 7f, 7f }; + doll.ReadAirway(live, pooled, drop, (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); + Assert.Equal(0.0716f, drop[Mouth], 0.002f); + Assert.Equal(0.0718f, drop[Nose], 0.002f); + } + + /// The drop at the creation pose for each down, metres: Mouth, Nose. + [Theory] + [InlineData(0f, -1f, 0f, -0.0467f, -0.0951f)] // upright: the glottis is the deepest sump, both rims above it + [InlineData(0f, 0f, -1f, -0.0734f, -0.0872f)] // on his back: the oropharynx is the deepest, the lips above it + [InlineData(0f, 0f, 1f, 0.0716f, 0.0718f)] // face down + [InlineData(1f, 0f, 0f, 0.0250f, 0.0250f)] // on his side: the rim, the half-width, lies below the sumps on the midline + [InlineData(-1f, 0f, 0f, 0.0250f, 0.0250f)] + [InlineData(0f, 1f, 0f, 0.0433f, 0.0949f)] // inverted + public void Drop_at_the_creation_pose_follows_down(float x, float y, float z, float mouth, float nose) + { + // Each within 0.002 m. Kills: the offset ignored (nose and mouth swap), the rim ignored (the side poses read 0), the rim taken as a sphere (face down reads 0.094), + // the shallowest sump (upright reads +0.005), and a drop cosine restored (the side poses read 0). + using var world = ParticleWorld(Ragdoll.Needs(Plan).Sensors); + var doll = Ragdoll.Build(world, Plan, (0f, 0f), firstStableId: 100); + var drop = DropOf(doll, (x, y, z)); + Assert.Equal(mouth, drop[Mouth], 0.002f); + Assert.Equal(nose, drop[Nose], 0.002f); } [Fact] - public void Facing_at_the_creation_pose_follows_down() + public void The_length_of_down_does_not_matter() { 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); + var unit = DropOf(doll, (0f, -1f, 0f)); + var weight = DropOf(doll, (0f, -9.81f, 0f)); + var side = DropOf(doll, (1f, 0f, 0f)); + var sideLong = DropOf(doll, (3f, 0f, 0f)); + for (var k = 0; k < unit.Length; k++) + { + Assert.Equal(unit[k], weight[k], 1e-6f); + Assert.Equal(side[k], sideLong[k], 1e-6f); + } + } + + /// The humanoid on a ground slab, limp, shoved by a seeded torque on pelvis and torso for six steps, then six seconds: the largest drop over its openings and the torso's face direction y. + private static (float Drop, float TorsoFaceY) Fall(ulong seed) + { + var (bodies, joints, _) = Ragdoll.Needs(Plan); + using var world = new Box3DSoaWorld(capacity: bodies + 1, jointCapacity: joints); + world.AddStaticBox(1, (0f, -0.5f, 0f), (20f, 0.5f, 20f), friction: 0.8f); + var doll = Ragdoll.Build(world, Plan, (0f, 0f), firstStableId: 100); + var strength = new float[Plan.Joints.Count]; + var target = new Quat[Plan.Joints.Count]; + Array.Fill(target, Quat.Identity); + var v = world.View; + var rng = new SplitMix64(seed); + for (var s = 0; s < 120 * 6; s++) + { + if (s < 6) + { + foreach (var part in new[] { "Pelvis", "Torso" }) + { + var i = doll.BodyLane(Plan.PartIndex(part)); + v.TorqueX[i] = 200f * rng.NextSigned(); + v.TorqueY[i] = 200f * rng.NextSigned(); + v.TorqueZ[i] = 200f * rng.NextSigned(); + } + } + + doll.Drive(strength, target); + world.Step(Dt, 4); + } + + var t = doll.BodyLane(Plan.PartIndex("Torso")); + new Quat(v.RotationX[t], v.RotationY[t], v.RotationZ[t], v.RotationW[t]).Rotate(0f, 0f, 1f, out _, out var faceY, out _); + var drop = DropOf(doll, (0f, -1f, 0f)); + return (MathF.Max(drop[Mouth], drop[Nose]), faceY); + } + + /// The humanoid welded to a stool (the SeatedOrientTests rig): held at the given strength for three seconds, or limp (strength 0) after a seeded torso shove for six: the largest drop over its openings. + private static float Seated(float strength, ulong seed) + { + var (bodies, joints, _) = Ragdoll.Needs(Plan); + using var world = new Box3DSoaWorld(capacity: bodies + 2, jointCapacity: joints + 1); + world.AddStaticBox(1, (0f, -0.5f, 0f), (20f, 0.5f, 20f), friction: 0.8f); + var stool = world.AddStaticBox(2, (0f, 1.11f, -0.115f), (0.05f, 0.05f, 0.025f), friction: 0.8f); + var doll = Ragdoll.Build(world, Plan, (0f, 0f), firstStableId: 100); + world.AddJoint(Box3DSoaJointKind.Weld, stool, doll.BodyLane(Plan.PartIndex("Pelvis")), (0f, 1.11f, -0.09f), (0f, 1f, 0f)); + var held = new float[Plan.Joints.Count]; + Array.Fill(held, strength); + var target = new Quat[Plan.Joints.Count]; + Array.Fill(target, Quat.Identity); + var v = world.View; + var rng = new SplitMix64(seed); + var steps = 120 * (strength > 0f ? 3 : 6); + for (var s = 0; s < steps; s++) + { + if (strength == 0f && s < 6) + { + var i = doll.BodyLane(Plan.PartIndex("Torso")); + v.TorqueX[i] = 60f * rng.NextSigned(); + v.TorqueY[i] = 60f * rng.NextSigned(); + v.TorqueZ[i] = 60f * rng.NextSigned(); + } + + doll.Drive(held, target); + world.Step(Dt, 4); + } + + var drop = DropOf(doll, (0f, -1f, 0f)); + return MathF.Max(drop[Mouth], drop[Nose]); } [Fact] - public void Facing_follows_the_head() + public void Drop_follows_the_heads_rotation() { - // 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. + // One box head created turned 90 degrees about x (its +z face along -y), a sump at its centre: the opening's centre and normal come from the lane's rotation, not the plan's offsets. + // Down (0,-1,0): the mouth's centre lies 0.1025 m below the sump and its rim adds 0.0091 (0.0233 x the sine of the angle + // between down and its normal), the nose 0.1095 and 0.0023; unrotated the mouth would read 0.04 + 0.0233 less the sump. Down (0,0,1): the mouth's centre 0.04 above the sump and its rim 0.0233 + // below it, -0.0167. Kills the rotation ignored (the part's identity pose). 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 turned = new BodyPlan([head], [], [], [.. Plan.AirwayOpenings.Select(o => o with { Part = 0 })], [new AirwaySumpDef("Sump", 0, (0f, 0f, 0f))]); 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); + var drop = new float[2]; + doll.ReadAirway(new float[2], new float[2], drop, (0f, -1f, 0f)); + Assert.Equal(0.1116f, drop[Mouth], 0.002f); + Assert.Equal(0.1118f, drop[Nose], 0.002f); + doll.ReadAirway(new float[2], new float[2], drop, (0f, 0f, 1f)); + Assert.Equal(-0.0167f, drop[Mouth], 0.002f); + } + + [Fact] + public void A_side_lying_limp_humanoid_drains() + { + // The twelve falls of the 240-seed sweep that end on the side (torso face y within 0.3 of level) each read a drop above the bound. Kills a head read from the pelvis's lane + // (5 of the 12 read at or below 0: the pelvis stays upright). + foreach (var seed in new ulong[] { 6, 10, 11, 18, 39, 66, 92, 120, 133, 151, 171, 208 }) + { + var (drop, faceY) = Fall(seed); + Assert.InRange(faceY, -0.3f, 0.3f); + Assert.True(drop > SideDropBound, $"seed {seed}: drop {drop} (face y {faceY})"); + } + } + + [Fact] + public void Upright_drains_nothing() + { + // The creation pose and the humanoid welded to a stool at full strength: both rims stay above the glottis (readings -0.047 and -0.046). + using var world = ParticleWorld(Ragdoll.Needs(Plan).Sensors); + var doll = Ragdoll.Build(world, Plan, (0f, 0f), firstStableId: 100); + var create = DropOf(doll, (0f, -1f, 0f)); + var seated = Seated(1f, 1); + Assert.True(MathF.Max(create[Mouth], create[Nose]) < UprightBound, $"creation {create[Mouth]} {create[Nose]}"); + Assert.True(seated < UprightBound, $"seated {seated}"); + } + + [Fact] + public void A_limp_seated_humanoid_whose_head_hangs_drains() + { + var draining = 0; + for (ulong seed = 1; seed <= 40; seed++) + { + if (Seated(0f, seed) > 0f) draining++; + } + + Assert.True(draining >= SeatedDraining, $"{draining} of 40 drain"); } [Fact] @@ -175,15 +319,15 @@ public sealed class AirwayTests world.Step(Dt, 4); var live = new float[2]; var pooled = new float[2]; - var facing = new float[2]; + var drop = new float[2]; var down = (0f, -1f, 0f); - doll.ReadAirway(live, pooled, facing, down); + doll.ReadAirway(live, pooled, drop, down); var before = GC.GetAllocatedBytesForCurrentThread(); - for (var i = 0; i < 1000; i++) doll.ReadAirway(live, pooled, facing, down); + for (var i = 0; i < 1000; i++) doll.ReadAirway(live, pooled, drop, down); Assert.Equal(0L, GC.GetAllocatedBytesForCurrentThread() - before); - Assert.Throws(() => doll.ReadAirway(new float[1], pooled, facing, down)); - Assert.Throws(() => doll.ReadAirway(live, new float[3], facing, down)); + Assert.Throws(() => doll.ReadAirway(new float[1], pooled, drop, down)); + Assert.Throws(() => doll.ReadAirway(live, new float[3], drop, down)); Assert.Throws(() => doll.ReadAirway(live, pooled, new float[1], down)); - Assert.Throws(() => doll.ReadAirway(live, pooled, facing, (0f, 0f, 0f))); + Assert.Throws(() => doll.ReadAirway(live, pooled, drop, (0f, 0f, 0f))); } }