diff --git a/src/Lodur.Core/Physiology.cs b/src/Lodur.Core/Physiology.cs
index 0d9af91..c66756f 100644
--- a/src/Lodur.Core/Physiology.cs
+++ b/src/Lodur.Core/Physiology.cs
@@ -56,29 +56,13 @@ namespace Lodur
_organism = new Effect[plan.OrganismEffects.Count];
for (var i = 0; i < _organism.Length; i++) _organism[i] = plan.OrganismEffects[i];
- // Resolve: every body part has exactly one stack and every stack names a body part, by (label, side).
var parts = body.Parts.Count;
- var stackOf = new int[parts];
- for (var p = 0; p < parts; p++) stackOf[p] = -1;
- for (var s = 0; s < plan.Parts.Count; s++)
- {
- var def = plan.Parts[s];
- var part = -1;
- for (var p = 0; p < parts; p++)
- {
- if (body.Parts[p].Label == def.PartLabel && body.Parts[p].Side == def.Side) part = p;
- }
-
- if (part < 0) throw new ArgumentException($"a tissue stack names no body part: '{def.PartLabel}' {def.Side}.");
- if (stackOf[part] >= 0) throw new ArgumentException($"body part '{def.PartLabel}' {def.Side} has two tissue stacks.");
- stackOf[part] = s;
- }
+ var stackOf = plan.Resolve(body);
_partStart = new int[parts + 1];
var total = 0;
for (var p = 0; p < parts; p++)
{
- if (stackOf[p] < 0) throw new ArgumentException($"body part '{body.Parts[p].Label}' {body.Parts[p].Side} has no tissue stack.");
_partStart[p] = total;
total += plan.Parts[stackOf[p]].Tissues.Count;
}
diff --git a/src/Lodur.Core/PhysiologyPlan.cs b/src/Lodur.Core/PhysiologyPlan.cs
index 1c3a0e3..4cb7df4 100644
--- a/src/Lodur.Core/PhysiologyPlan.cs
+++ b/src/Lodur.Core/PhysiologyPlan.cs
@@ -117,6 +117,43 @@ namespace Lodur
public int AfflictionIndex(string label) =>
_afflictionIndex.TryGetValue(label, out var i) ? i : throw new ArgumentException($"no affliction labelled '{label}'.");
+ ///
+ /// The one (label, side) join of stacks to a body: element p is the index in of body part p's stack. Refuses a
+ /// stack naming no part, a part with two stacks, a part with no stack, and shells that do not fit (a stack's summed Thickness must be
+ /// below the part's inscribed half extent, compared at the shape's float precision). Physiology and the sheet builder call it; it allocates, at construction only.
+ ///
+ public int[] Resolve(BodyPlan body)
+ {
+ var parts = body.Parts.Count;
+ var stackOf = new int[parts];
+ for (var p = 0; p < parts; p++) stackOf[p] = -1;
+ for (var s = 0; s < Parts.Count; s++)
+ {
+ var def = Parts[s];
+ var part = -1;
+ for (var p = 0; p < parts; p++)
+ {
+ if (body.Parts[p].Label == def.PartLabel && body.Parts[p].Side == def.Side) part = p;
+ }
+
+ if (part < 0) throw new ArgumentException($"a tissue stack names no body part: '{def.PartLabel}' {def.Side}.");
+ if (stackOf[part] >= 0) throw new ArgumentException($"body part '{def.PartLabel}' {def.Side} has two tissue stacks.");
+ stackOf[part] = s;
+ }
+
+ for (var p = 0; p < parts; p++)
+ {
+ var d = body.Parts[p];
+ if (stackOf[p] < 0) throw new ArgumentException($"body part '{d.Label}' {d.Side} has no tissue stack.");
+ var shells = 0.0;
+ foreach (var t in Parts[stackOf[p]].Tissues) shells += t.Thickness;
+ var half = d.Shape == PartShape.Box ? Math.Min(d.Size.X, Math.Min(d.Size.Y, d.Size.Z)) : d.Size.X;
+ if (!((float)shells < half)) throw new ArgumentException($"body part '{d.Label}' {d.Side}: the shells ({shells} m) do not fit inside its inscribed half extent ({half} m).");
+ }
+
+ return stackOf;
+ }
+
private static IReadOnlyList Own(IReadOnlyList effects)
{
var list = new List(effects.Count);
diff --git a/tests/Lodur.Core.Tests/PhysiologyTests.cs b/tests/Lodur.Core.Tests/PhysiologyTests.cs
index 52f4ba2..f299332 100644
--- a/tests/Lodur.Core.Tests/PhysiologyTests.cs
+++ b/tests/Lodur.Core.Tests/PhysiologyTests.cs
@@ -45,6 +45,36 @@ public sealed class PhysiologyTests
Over(Stack("Root", BrainT), Stack("Kid", MuscleT) with { Side = Side.Left }), 20)); // the side is part of the name
}
+ [Fact]
+ public void Resolve_maps_each_body_part_to_its_stack()
+ {
+ Assert.Equal(new[] { 1, 0 }, Over(Stack("Kid", MuscleT), Stack("Root", BrainT)).Resolve(Tiny()));
+ }
+
+ [Fact]
+ public void Resolve_refuses_the_join_rules_itself()
+ {
+ var body = Tiny();
+ Assert.Throws(() => Over(Stack("Root", BrainT)).Resolve(body));
+ Assert.Throws(() => Over(Stack("Root", BrainT), Stack("Kid", MuscleT), Stack("Ghost", MuscleT)).Resolve(body));
+ Assert.Throws(() => Over(Stack("Root", BrainT), Stack("Kid", MuscleT), Stack("Kid", MuscleT)).Resolve(body));
+ }
+
+ [Fact]
+ public void Shells_must_fit_inside_the_part_by_its_smallest_half_extent()
+ {
+ PhysiologyPlan Shelled(double t) => Over(Stack("Root", BrainT with { Thickness = t }), Stack("Kid", MuscleT));
+ Assert.Throws(() => Shelled(0.1).Resolve(Tiny()));
+ Assert.Throws(() => Shelled(0.2).Resolve(Tiny()));
+ Shelled(0.0999).Resolve(Tiny());
+
+ PartDef Slab(string label) => new(label, Side.Center, PartShape.Box, (0.1f, 0.02f, 0.1f), (0f, 0f, 0f), Quat.Identity, 1000f, 0.8f, 0f);
+ var joint = new JointDef("J", Side.Center, JointKind.Spherical, 0, 1, (0f, 0f, 0f), (0f, 1f, 0f), 0f, 0f, 0f, new MuscleGains(1f, 1f, 1f, 1f));
+ var slab = new BodyPlan(new[] { Slab("Root"), Slab("Kid") }, new[] { joint }, Array.Empty());
+ Assert.Throws(() => Shelled(0.03).Resolve(slab));
+ Shelled(0.019).Resolve(slab);
+ }
+
[Fact]
public void A_physiology_refuses_a_body_with_no_consciousness_tissue_or_a_joint_child_with_no_motor_tissue()
{