diff --git a/docs/soa-world.md b/docs/soa-world.md
index 8686d7a..60c8742 100644
--- a/docs/soa-world.md
+++ b/docs/soa-world.md
@@ -8,12 +8,12 @@ Box3D decides every physical outcome; Ymir creates bodies, steps the world and e
`ymir_box3d_session_*` (`ymir_box3d.c`) is the planar Aetheria session: gravity is zero, Y is locked,
only yaw rotates (`motionLocks.linearY/angularX/angularZ`), and state leaves by `copy_bodies` into an
array of structs. A body cannot fall or tumble there, and every read is a copy. The SoA world is a
-separate native object (`ymir_box3d_soa.c`, ABI 20) so the Aetheria session's contract is untouched.
+separate native object (`ymir_box3d_soa.c`, ABI 21) so the Aetheria session's contract is untouched.
## Contract
-- Native (ABI 20): `ymir_box3d_soa_create(capacity, joint_capacity, gravity, fact_capacity, hit_threshold, particle_capacities*) /
- create_particles / destroy_particles / create_distances / create_bends / create_sensors / attach(n, desc*) / set_pinned(n, handles, pinned) / remove / set_material / free_slots /
+- Native (ABI 21): `ymir_box3d_soa_create(capacity, joint_capacity, gravity, fact_capacity, hit_threshold, particle_capacities*) /
+ create_particles / destroy_particles / create_distances / create_bends / create_sensors / take_wet(n, sensors, volumes, taken) / attach(n, desc*) / set_pinned(n, handles, pinned) / remove / set_material / free_slots /
add_body / add_joint / step / get_view / get_facts / set_anchor_target / destroy_joint / set_joint_thresholds / remove_body / free_joint_slots / free_body_slots / apply_impulse / cast_ray_closest / destroy`. `add_body` takes one descriptor (sphere, capsule, box, static box, anchor; position,
rotation, velocity, size, density, friction, restitution, rolling resistance, material id, parent, limb). Capacities are fixed at creation,
so lane pointers never move.
@@ -243,7 +243,7 @@ separate native object (`ymir_box3d_soa.c`, ABI 20) so the Aetheria session's co
disc holds its contact point (volumes add, the centre is the volume-weighted mean, the radius is recomputed); else it takes the lowest free lane; else it joins the nearest patch of the same support
and material; else (no patch on its support) it adds its volume to the nearest live patch of its material in the world, by world distance from the drop to the patch's centre (the patch's body pose applied; ties go to the lowest lane), and that centre stays; else (no live patch of its material at all) the batch is refused `FULL`, whole, and every lane is as it was (an undo log sized by `fluid_capacity`). A dead or non-fluid particle, a stale handle, a particle named twice
or one with no support is refused whole too. `wet_volume` per body (the view's `WetVolume`, `body_wet_volume` natively) is the sum of the live patches on the body in wet lane order, written by
- `rederive_wet` alone, which `settle`, `remove(WET)` and `destroy_particles` (when it kills patches on the solid particles it destroys) call; the step never writes a wet lane or the volume. `remove(WET)` kills patches and ends in `rederive_wet`,
+ `rederive_wet` alone, which `settle`, `take_wet`, `remove(WET)` and `destroy_particles` (when it kills patches on the solid particles it destroys) call; the step never writes a wet lane or the volume. `remove(WET)` kills patches and ends in `rederive_wet`,
never in `rederive_topology`; `destroy_particles` kills the patches on the solid particles it destroys. `settle` and `remove` return the patch handles to read. Material `wet_friction` (>= 0) is stored
and viewed (`MaterialWetFriction`), and lowers friction where the liquid lies (below). Which particles settle, and when, is the caller's policy
(speed, age, film depth: `lodur:follow_up:bds-blood-settle-policy`); Ymir holds the data and the command.
@@ -258,6 +258,14 @@ separate native object (`ymir_box3d_soa.c`, ABI 20) so the Aetheria session's co
(`lodur:ruling:liquid-sensor-reads-pooled-liquid`). Both are end-of-step readings: a `settle`, `destroy_particles` or `remove` between steps shows at the next step, which is the contract. The pass runs before `ymir_particles_end`'s early return for no live particle, so a sensor never keeps a reading once the liquid is gone. The readings are sensors: the step
writes them and no pass reads them back, so a world with sensors steps byte for byte as one without, and a world with none steps as ABI 14. `remove(SENSOR)` ends in no topology derivation. Ymir names no organ
(a head's airway, a lung): the consumer places a sensor and maps the volume (`lodur:follow_up:lodur-airway-liquid`). Gives up: a sphere only, not a shape-exact volume, and no per-liquid split.
+ - Take (ABI 21). `take_wet(n, sensors, volumes, taken)` (`Box3DSoaWorld.TakeWet`) removes up to `volumes[k]` m^3 of pooled liquid from the wet patches sensor k reads, so liquid a body breathes in leaves the world.
+ It refuses the whole call, changing nothing (INVALID_ARGUMENT), for n < 0, a NULL array with n > 0, a stale or dead sensor, a sensor named twice, and a volume that is not finite or is < 0 (managed: an
+ `ArgumentException` for a handle that is not a sensor, unequal lengths, or a non-empty `taken` shorter than the sensors). Per sensor in call order: W is the pooled sum above, `f = W > 0 ? min(1, v / W) : 0`, and a second walk of
+ the same sphere test (`pooled_walk`, shared with the reading, so the take removes from exactly the patches the reading summed, patches on solid particles included) scales every visited patch's volume by `1 - f`
+ and its radius by `wet_disc_radius`; a patch at f = 1 is removed as `remove(WET)`. Proportional by design: the film on a face and the pool beneath it shrink together and no lane order decides whose liquid is taken. A later
+ sensor in the call sees an earlier one's take (the index is rederived between sensors once a take has run, because a removed lane's centre is reset); `taken[k]` (m^3, W f) is what left; `rederive_wet` runs once more at
+ the end, and not at all when nothing changed. The proxies are refreshed first, as `settle` does. `sensor_wet_volume` is written by the step's end alone: the reading changes at the next step. Ymir names no organ (a lung, an airway):
+ the consumer decides when liquid is breathed. Live drops are not taken (the caller destroys whole drops).
- Wet friction. Settled liquid lowers friction where it lands, per contact, never per shape: `wet_mu(body, point, mu)` is `min(mu, wet_friction)` of the lowest live patch on that body whose
disc holds the point (the point in the body's frame, as `wet_x wet_y wet_z` are), and `mu` at once when the body's `wet_volume` is 0. It is the one patch test. A particle that touches a body
uses it at the nearest surface point for a solid particle (also `min` with `particle_wet_mu[i]`, the wet friction of the lowest live patch on that solid particle, +inf when dry, derived by
diff --git a/native/Ymir.Box3D/ymir_box3d_soa.c b/native/Ymir.Box3D/ymir_box3d_soa.c
index 2b4bc4e..535a815 100644
--- a/native/Ymir.Box3D/ymir_box3d_soa.c
+++ b/native/Ymir.Box3D/ymir_box3d_soa.c
@@ -62,7 +62,7 @@ enum
YMIR_SOA_JOINT_REMOVED = 4 // a retired joint's lane (destroy_joint): never accepted by add_joint
};
-#define YMIR_SOA_ABI_VERSION 20u
+#define YMIR_SOA_ABI_VERSION 21u
// A body to create. `size` by kind: sphere {radius}, capsule {radius, half_length} (segment along
// local Y), box and static box {half_x, half_y, half_z}. `parent` is the lane index of an earlier body
@@ -1828,6 +1828,17 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_settle(ymir_box3d_soa_world* world,
return ymir_particles_settle(world->particles, n, h, world->step_count, out);
}
+// Takes pooled liquid out of the wet patches liquid sensors read (see ymir_particles_take_wet): the proxies are refreshed first, so the spheres are where the bodies are now.
+YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_take_wet(ymir_box3d_soa_world* world, int32_t n, const ymir_handle* sensors, const float* volumes, float* taken)
+{
+ if (world == NULL || world->particles == NULL)
+ {
+ return YMIR_SOA_INVALID_ARGUMENT;
+ }
+ ymir_soa_gather_proxies(world);
+ return ymir_particles_take_wet(world->particles, n, sensors, volumes, world->step_count, taken);
+}
+
// Pins or wakes particles (see ymir_particles_set_pinned): the proxies are refreshed first, so a wake lands on the anchor's point at the bodies' poses now.
YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_set_pinned(ymir_box3d_soa_world* world, int32_t n, const ymir_handle* h, uint32_t pinned)
{
diff --git a/native/Ymir.Box3D/ymir_particles.c b/native/Ymir.Box3D/ymir_particles.c
index 047dca1..9028755 100644
--- a/native/Ymir.Box3D/ymir_particles.c
+++ b/native/Ymir.Box3D/ymir_particles.c
@@ -2601,10 +2601,28 @@ void ymir_particles_substep(ymir_particles* p, float h, float gx, float gy, floa
}
}
+static float wet_disc_radius(float volume);
+
+// A patch keeps the fraction keep of its volume: its radius follows, and it dies at a kept volume of 0.
+static void take_from_wet(ymir_particles* p, int32_t w, float keep, uint64_t step_count)
+{
+ float kept = p->wet_volume[w] * keep;
+ if (!(kept > 0.0f))
+ {
+ kill_wet(p, w, step_count);
+ return;
+ }
+ p->wet_volume[w] = kept;
+ p->wet_radius[w] = wet_disc_radius(kept);
+}
+
// The volume of the live wet patches whose centre lies within r of the world point c. A body's patches go through the wet cut's index: the sphere is taken into the
// body's frame (where its patches' centres are), culled against the box of the body's discs, and only centres within r of its x are walked, in the index's order
// (bodies ascending, then centre x). Patches on solid particles have no index and are summed after, in wet lane order, at their particle's position.
-static float pooled_volume(const ymir_particles* p, v3 c, float r)
+// The one sphere test of the wet patches: the sensor's reading walks it with keep 1 (read only); take_wet walks it again with keep < 1, which also scales
+// every patch it visits by keep (recomputing its radius) and kills one whose kept volume is 0. Returns the volume inside before any change.
+// A kill resets the lane, so the index is stale after a walk that killed: the caller rederives before the next walk.
+static float pooled_walk(ymir_particles* p, v3 c, float r, float keep, uint64_t step_count)
{
float total = 0.0f;
float r2 = r * r;
@@ -2642,6 +2660,10 @@ static float pooled_volume(const ymir_particles* p, v3 c, float r)
if (vdot(d, d) <= r2)
{
total += p->wet_volume[w];
+ if (keep < 1.0f)
+ {
+ take_from_wet(p, w, keep, step_count);
+ }
}
}
}
@@ -2656,12 +2678,23 @@ static float pooled_volume(const ymir_particles* p, v3 c, float r)
if (vdot(d, d) <= r2)
{
total += p->wet_volume[w];
+ if (keep < 1.0f)
+ {
+ take_from_wet(p, w, keep, step_count);
+ }
}
}
}
return total;
}
+// A sensor's centre in the world: its point on its body at the pose the last substep (or the caller's gather) left the proxy in.
+static v3 sensor_centre(const ymir_particles* p, int32_t s)
+{
+ const ymir_particle_proxy* b = p->proxies + p->sensor_body[s];
+ return vadd((v3){b->position[0], b->position[1], b->position[2]}, qrot(b->rotation, (v3){p->sensor_x[s], p->sensor_y[s], p->sensor_z[s]}));
+}
+
// Each live sensor reads the liquid inside it: the sum, in lane order, of (2 r)^3 over the live fluid particles whose centre lies within its sphere, against the
// pose the last substep left each proxy in. It runs whether or not any particle is live, so a sensor never keeps a reading once the liquid is gone.
static void read_sensors(ymir_particles* p)
@@ -2676,10 +2709,9 @@ static void read_sensors(ymir_particles* p)
float pooled = 0.0f;
if (p->sensor_body[s] < p->body_count)
{
- const ymir_particle_proxy* b = p->proxies + p->sensor_body[s];
- v3 c = vadd((v3){b->position[0], b->position[1], b->position[2]}, qrot(b->rotation, (v3){p->sensor_x[s], p->sensor_y[s], p->sensor_z[s]}));
+ v3 c = sensor_centre(p, s);
float r2 = p->sensor_radius[s] * p->sensor_radius[s];
- pooled = pooled_volume(p, c, p->sensor_radius[s]);
+ pooled = pooled_walk(p, c, p->sensor_radius[s], 1.0f, 0);
for (int32_t i = 0; p->live_fluid > 0 && i < p->particle_count; ++i)
{
if (!p->particle_live[i] || !(p->phase[i] & YMIR_PHASE_FLUID))
@@ -2972,6 +3004,66 @@ int32_t ymir_particles_settle(ymir_particles* p, int32_t n, const ymir_handle* h
return ymir_particles_destroy_particles(p, n, h, step_count);
}
+int32_t ymir_particles_take_wet(ymir_particles* p, int32_t n, const ymir_handle* sensors, const float* volumes, uint64_t step_count, float* taken)
+{
+ if (p == NULL || n < 0 || (n > 0 && (sensors == NULL || volumes == NULL)))
+ {
+ return YMIR_PARTICLES_INVALID_ARGUMENT;
+ }
+ for (int32_t k = 0; k < n; ++k)
+ {
+ if (!sensor_ok(p, sensors[k].lane, sensors[k].generation) || !isfinite(volumes[k]) || volumes[k] < 0.0f)
+ {
+ return YMIR_PARTICLES_INVALID_ARGUMENT;
+ }
+ for (int32_t j = 0; j < k; ++j)
+ {
+ if (sensors[j].lane == sensors[k].lane)
+ {
+ return YMIR_PARTICLES_INVALID_ARGUMENT;
+ }
+ }
+ }
+ int stale = 0; // a take may have killed a lane the index still lists (its centre reset): the next sensor rederives before it walks
+ for (int32_t k = 0; k < n; ++k)
+ {
+ int32_t s = sensors[k].lane;
+ if (taken != NULL)
+ {
+ taken[k] = 0.0f;
+ }
+ if (p->sensor_body[s] >= p->body_count || volumes[k] == 0.0f)
+ {
+ continue;
+ }
+ if (stale)
+ {
+ rederive_wet(p);
+ stale = 0;
+ }
+ v3 c = sensor_centre(p, s);
+ float r = p->sensor_radius[s];
+ float w = pooled_walk(p, c, r, 1.0f, step_count);
+ float f = w > 0.0f ? fminf(1.0f, volumes[k] / w) : 0.0f;
+ float keep = 1.0f - f;
+ if (!(keep < 1.0f)) // nothing to take, or a fraction below float's reach: the walk would change nothing
+ {
+ continue;
+ }
+ pooled_walk(p, c, r, keep, step_count);
+ stale = 1;
+ if (taken != NULL)
+ {
+ taken[k] = w * f;
+ }
+ }
+ if (stale)
+ {
+ rederive_wet(p);
+ }
+ return YMIR_PARTICLES_OK;
+}
+
int32_t ymir_particles_create_sensors(ymir_particles* p, int32_t n, const ymir_sensor_desc* d, int32_t body_count, uint64_t step_count, ymir_handle* out)
{
if (p == NULL || n < 0 || (n > 0 && d == NULL))
diff --git a/native/Ymir.Box3D/ymir_particles.h b/native/Ymir.Box3D/ymir_particles.h
index ce6e958..cb6e285 100644
--- a/native/Ymir.Box3D/ymir_particles.h
+++ b/native/Ymir.Box3D/ymir_particles.h
@@ -208,7 +208,7 @@ typedef struct ymir_particles
float* density; // sensor: a fluid particle's number density over its rest density n_i / n0 in the last substep it ran, else 0
// wet lanes: a patch of settled liquid on one support, a body (wet_body, its point in the body frame) or a solid particle (wet_particle, point 0);
- // one of the two is -1, and a free lane holds -1 in both. Written by settle, remove and destroy_particles only; the step never touches them.
+ // one of the two is -1, and a free lane holds -1 in both. Written by settle, take_wet, remove and destroy_particles only; the step never touches them.
int32_t *wet_body, *wet_particle;
float *wet_x, *wet_y, *wet_z, *wet_radius, *wet_volume; // wet_volume m^3; wet_radius = sqrt(volume / (pi YMIR_WET_FILM))
uint32_t *wet_material, *wet_live, *wet_generation;
@@ -304,6 +304,12 @@ int32_t ymir_particles_set_pinned(ymir_particles* p, int32_t n, const ymir_handl
// (INVALID_ARGUMENT) a dead or non-fluid particle or one with no support, FULL when a drop finds no lane and no live patch of its material anywhere to merge into, before any
// change. A drop that finds no lane and no patch on its support adds its volume to the nearest patch of its material in the world (the patch's centre stays). `out` (NULL or n handles) receives the patch each drop landed in.
int32_t ymir_particles_settle(ymir_particles* p, int32_t n, const ymir_handle* h, uint64_t step_count, ymir_handle* out);
+// Takes pooled liquid out of the wet patches a liquid sensor reads. Refuses whole (INVALID_ARGUMENT, nothing changed) n < 0, a NULL array with n > 0, a stale or dead
+// sensor, a sensor named twice, and a volume that is not finite or is < 0. Then per sensor in call order: W is the pooled volume of the patches whose centres lie in the
+// sensor's sphere (the walk the reading sums, at the proxies' pose now), f = W > 0 ? min(1, volume / W) : 0, and every patch in the sphere keeps (1 - f) of its volume
+// (radius recomputed; a patch at f == 1 is removed as remove(WET)): proportional, so no lane order decides which patch gives. A later sensor sees an earlier one's take.
+// `taken` (NULL or n floats) receives per sensor the volume that left (m^3), W * f. Readings change at the next step's end; none is written here. One rederive_wet when anything changed.
+int32_t ymir_particles_take_wet(ymir_particles* p, int32_t n, const ymir_handle* sensors, const float* volumes, uint64_t step_count, float* taken);
// Creates liquid sensors in the lowest free lanes. Refuses whole (INVALID_ARGUMENT) a body outside [0, body_count), a radius that is not finite and > 0, or a
// non-finite point; FULL when there are not n free lanes; nothing changes. A sensor only reads: its volume moves nothing.
int32_t ymir_particles_create_sensors(ymir_particles* p, int32_t n, const ymir_sensor_desc* d, int32_t body_count, uint64_t step_count, ymir_handle* out);
diff --git a/src/Ymir.Box3D/Box3DSoaWorld.cs b/src/Ymir.Box3D/Box3DSoaWorld.cs
index 5175a1a..019b568 100644
--- a/src/Ymir.Box3D/Box3DSoaWorld.cs
+++ b/src/Ymir.Box3D/Box3DSoaWorld.cs
@@ -687,6 +687,10 @@ internal static unsafe partial class Box3DSoaNative
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
internal static partial int ymir_box3d_soa_settle(nint world, int n, Box3DSoaParticleHandle* handles, Box3DSoaParticleHandle* output);
+ [LibraryImport(Library)]
+ [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
+ internal static partial int ymir_box3d_soa_take_wet(nint world, int n, Box3DSoaParticleHandle* sensors, float* volumes, float* taken);
+
[LibraryImport(Library)]
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
internal static partial int ymir_box3d_soa_remove(nint world, uint type, int n, Box3DSoaParticleHandle* handles);
@@ -974,7 +978,7 @@ public readonly unsafe struct Box3DSoaView
///
public sealed class Box3DSoaWorld : IDisposable
{
- private const uint ExpectedAbi = 20;
+ private const uint ExpectedAbi = 21;
private nint _handle;
/// The gravity (m/s^2) this world was created with; native has no setter, so this copy cannot disagree.
@@ -1415,6 +1419,30 @@ public sealed class Box3DSoaWorld : IDisposable
patches[i] = new Box3DSoaConstraintHandle(Box3DSoaConstraintType.Wet, native[i].Lane, native[i].Generation);
}
+ ///
+ /// Takes up to [k] m^3 of pooled liquid from the wet patches sensor k reads (the patches whose centres lie in its sphere, at the bodies' poses now), every patch the
+ /// same fraction, so no lane order decides which gives; a patch taken to nothing is removed. Sensors apply in call order, a later one seeing an earlier one's take.
+ /// (or empty) receives the volume that left per sensor. Refuses the whole batch, changing nothing, for a stale or removed sensor, a sensor named twice,
+ /// or a volume that is not finite or is negative; throws for a handle that is not a sensor, unequal lengths, or a non-empty shorter than the sensors.
+ /// Readings change at the next step's end. Which liquid is breathed, and when, is the caller's policy.
+ ///
+ public unsafe void TakeWet(ReadOnlySpan sensors, ReadOnlySpan volumes, Span taken = default)
+ {
+ ObjectDisposedException.ThrowIf(_handle == 0, this);
+ if (sensors.Length != volumes.Length) throw new ArgumentException("One volume per sensor.", nameof(volumes));
+ if (!taken.IsEmpty && taken.Length < sensors.Length) throw new ArgumentException("One taken per sensor.", nameof(taken));
+ var native = new Box3DSoaParticleHandle[sensors.Length];
+ for (var i = 0; i < sensors.Length; i++)
+ {
+ if (sensors[i].Type != Box3DSoaConstraintType.Sensor) throw new ArgumentException("Sensors only.", nameof(sensors));
+ native[i] = new Box3DSoaParticleHandle(sensors[i].Lane, sensors[i].Generation);
+ }
+ fixed (Box3DSoaParticleHandle* h = native)
+ fixed (float* v = volumes)
+ fixed (float* t = taken)
+ Check(Box3DSoaNative.ymir_box3d_soa_take_wet(_handle, native.Length, h, v, taken.IsEmpty ? null : t));
+ }
+
///
/// Creates liquid sensors in the lowest free lanes: each is a sphere of radius m (> 0) centred on point in the frame of body lane body,
/// and reads at the end of every step. Refuses the whole batch, changing nothing, for a body that does not exist,
diff --git a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaGrabTests.cs b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaGrabTests.cs
index 9e8fc9a..721cea9 100644
--- a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaGrabTests.cs
+++ b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaGrabTests.cs
@@ -167,10 +167,10 @@ public sealed class Box3DSoaGrabTests
[Fact]
public void The_managed_side_refuses_a_native_library_of_another_abi_in_either_direction()
{
- Assert.Equal(20u, Box3DSoaNative.ymir_box3d_soa_get_abi_version());
- Box3DSoaWorld.RequireNativeAbi(20);
- Assert.Throws(() => Box3DSoaWorld.RequireNativeAbi(19)); // an older native under this managed build
- Assert.Throws(() => Box3DSoaWorld.RequireNativeAbi(21)); // a newer one
+ Assert.Equal(21u, Box3DSoaNative.ymir_box3d_soa_get_abi_version());
+ Box3DSoaWorld.RequireNativeAbi(21);
+ Assert.Throws(() => Box3DSoaWorld.RequireNativeAbi(20)); // an older native under this managed build
+ Assert.Throws(() => Box3DSoaWorld.RequireNativeAbi(22)); // a newer one
}
[Fact]
diff --git a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaTakeWetTests.cs b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaTakeWetTests.cs
new file mode 100644
index 0000000..d63771e
--- /dev/null
+++ b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaTakeWetTests.cs
@@ -0,0 +1,221 @@
+using Ymir.Box3D;
+
+namespace Ymir.Box3D.Soa.Tests;
+
+using Pos = (float X, float Y, float Z);
+
+/// take_wet (ABI 21): pooled liquid leaves the wet patches a liquid sensor reads, every patch the same fraction (docs/soa-world.md, Take).
+public sealed partial class Box3DSoaFluidTests
+{
+ // A floor (body 0, its frame origin y -0.5) with three patches: A (x -0.3, two drops, 2 D), B (x -0.1, D) and C (x 0.4, D), and bare floor between.
+ // Sensor centres are given by x on the floor's top (body-frame y 0.5).
+ private static (Box3DSoaWorld World, Box3DSoaConstraintHandle[] Sensors) TakeFloor(params (float X, float Radius)[] sensors)
+ {
+ var (world, drops) = DropsOnFloor([(-0.3f, 0f, 0f), (-0.285f, 0f, 0f), (-0.1f, 0f, 0f), (0.4f, 0f, 0f)], 8, 4);
+ var h = new Box3DSoaConstraintHandle[sensors.Length];
+ world.CreateSensors(sensors.Select(s => (0, (s.X, 0.5f, 0f), s.Radius)).ToArray(), h);
+ world.Settle(drops);
+ world.Step(Dt, Sub);
+ return (world, h);
+ }
+
+ private static float WetRadiusOf(float volume) => MathF.Sqrt(volume / (3.14159265358979f * 1.0e-3f));
+
+ [Fact]
+ public void TakeWet_a_partial_take_leaves_every_patch_in_the_sphere_the_same_fraction()
+ {
+ var (world, s) = TakeFloor((-0.2f, 0.15f));
+ using var _ = world;
+ var before = world.View;
+ var vol = before.WetPatchVolume.ToArray();
+ var rad = before.WetRadius.ToArray();
+ var x = before.WetX.ToArray();
+ var live = before.WetLive.ToArray();
+ var inside = Enumerable.Range(0, before.WetCount).Where(w => live[w] != 0 && x[w] < 0f).ToArray();
+ var outside = Enumerable.Range(0, before.WetCount).Where(w => live[w] != 0 && x[w] > 0f).ToArray();
+ Assert.Equal(2, inside.Length);
+ Assert.Single(outside);
+ var pooled = before.SensorWetVolume[s[0]];
+ Assert.Equal(inside.Sum(w => vol[w]), pooled, pooled * 1e-5f);
+ Assert.NotEqual(vol[inside[0]], vol[inside[1]]); // 2 D and D: an equal share would differ from a proportional one
+ var bodyBefore = before.WetVolume[0];
+ var ask = pooled / 3f;
+ var taken = new float[1];
+ world.TakeWet(s, [ask], taken);
+ var v = world.View;
+ Assert.Equal(ask, taken[0], ask * 1e-5f);
+ var keep = 1f - ask / pooled;
+ foreach (var w in inside)
+ {
+ Assert.Equal(vol[w] * keep, v.WetPatchVolume[w], vol[w] * 1e-5f);
+ Assert.Equal(WetRadiusOf(v.WetPatchVolume[w]), v.WetRadius[w], 1e-6f);
+ Assert.Equal(1u, v.WetLive[w]);
+ }
+ foreach (var w in outside)
+ {
+ Assert.Equal(vol[w], v.WetPatchVolume[w]);
+ Assert.Equal(rad[w], v.WetRadius[w]);
+ }
+ Assert.Equal(bodyBefore - taken[0], v.WetVolume[0], bodyBefore * 1e-5f);
+ Assert.Equal(pooled, v.SensorWetVolume[s[0]]); // a reading: it changes at the next step
+ world.Step(Dt, Sub);
+ v = world.View;
+ Assert.Equal(pooled - ask, v.SensorWetVolume[s[0]], pooled * 1e-5f);
+ Assert.Equal(PooledInside(v, (-0.2f, 0f, 0f), 0.15f).Volume, v.SensorWetVolume[s[0]]);
+ Readings.Add($"TAKE partial: pooled {pooled:E4} asked {ask:E4} taken {taken[0]:E4}, keep {keep:F4} in both patches");
+ }
+
+ [Fact]
+ public void TakeWet_taking_at_least_what_is_there_empties_the_sphere()
+ {
+ var (world, s) = TakeFloor((-0.2f, 0.15f));
+ using var _ = world;
+ var before = world.View;
+ var vol = before.WetPatchVolume.ToArray();
+ var x = before.WetX.ToArray();
+ var live = before.WetLive.ToArray();
+ var gen = before.WetGeneration.ToArray();
+ var inside = Enumerable.Range(0, before.WetCount).Where(w => live[w] != 0 && x[w] < 0f).ToArray();
+ var outside = Enumerable.Range(0, before.WetCount).Single(w => live[w] != 0 && x[w] > 0f);
+ var pooled = before.SensorWetVolume[s[0]];
+ var bodyBefore = before.WetVolume[0];
+ var taken = new float[1];
+ world.TakeWet(s, [10f], taken);
+ var v = world.View;
+ Assert.Equal(pooled, taken[0]);
+ foreach (var w in inside)
+ {
+ Assert.Equal(0u, v.WetLive[w]);
+ Assert.Equal(gen[w] + 1, v.WetGeneration[w]);
+ }
+ Assert.Equal(vol[outside], v.WetPatchVolume[outside]);
+ Assert.Equal(1u, v.WetLive[outside]);
+ Assert.Equal(bodyBefore - pooled, v.WetVolume[0], bodyBefore * 1e-5f);
+ world.Step(Dt, Sub);
+ Assert.Equal(0f, world.View.SensorWetVolume[s[0]]);
+ }
+
+ [Fact]
+ public void TakeWet_nothing_to_take_changes_nothing()
+ {
+ var (world, s) = TakeFloor((-0.2f, 0.15f), (0.1f, 0.05f)); // the second is over bare floor
+ using var _ = world;
+ var wet = WetBytes(world.View);
+ var body = world.View.WetVolume[0];
+ var taken = new float[] { 9f, 9f };
+ world.TakeWet(s, [0f, 1f], taken);
+ Assert.Equal([0f, 0f], taken);
+ Assert.True(wet.AsSpan().SequenceEqual(WetBytes(world.View)), "a wet lane changed");
+ Assert.Equal(body, world.View.WetVolume[0]);
+ world.TakeWet([], [], default);
+ Assert.True(wet.AsSpan().SequenceEqual(WetBytes(world.View)));
+ }
+
+ [Fact]
+ public void TakeWet_a_patch_on_a_solid_particle_is_taken_as_one_on_a_body()
+ {
+ using var world = new Box3DSoaWorld(4, gravityY: -9.81f, particles: new Box3DSoaParticleCapacities(256, 160, 140, 16, 1024, 2, 16, 4, 2));
+ world.AddStaticBox(1, (0f, -50f, 0f), (1f, 1f, 1f));
+ var sheet = Graph.Sheet(world, (0f, 0.6f, 0f), (0.04f, 0f, 0f), (0f, 0f, 0.04f), 8, 8, 0.02f, 0.02f, Box3DSoaPhase.Of(4, true, true));
+ for (var j = 0; j < 8; j++)
+ for (var i = 0; i < 8; i += 7)
+ world.Attach(Box3DSoaAttachKind.Static, sheet.Particles[j * 8 + i], point: (0.04f * i, 0.6f, 0.04f * j));
+ var drops = new Box3DSoaParticleHandle[1];
+ world.CreateParticles([Fluid((0.12f, 0.64f, 0.12f))], drops);
+ for (var tick = 0; tick < 20; tick++) world.Step(1f / 60f, Sub);
+ var under = world.View.SupportParticle[drops[0].Lane];
+ Assert.True(under >= 0);
+ world.Settle(drops);
+ var v0 = world.View;
+ var h = new Box3DSoaConstraintHandle[1];
+ world.CreateSensors([(0, (v0.ParticleX[under], v0.ParticleY[under] + 50f, v0.ParticleZ[under]), 0.02f)], h);
+ world.Step(Dt, Sub);
+ var pooled = world.View.SensorWetVolume[h[0]];
+ var vol = world.View.WetPatchVolume[0];
+ Assert.True(pooled > 0f, "the patch on the solid particle is inside the sphere");
+ Assert.Equal(pooled, vol);
+ var taken = new float[1];
+ world.TakeWet(h, [pooled / 4f], taken);
+ Assert.Equal(vol * 0.75f, world.View.WetPatchVolume[0], vol * 1e-5f);
+ Assert.Equal(WetRadiusOf(world.View.WetPatchVolume[0]), world.View.WetRadius[0], 1e-6f);
+ Assert.Equal(pooled / 4f, taken[0], pooled * 1e-5f);
+ world.TakeWet(h, [1f], taken);
+ Assert.Equal(0u, world.View.WetLive[0]);
+ Assert.Equal(1u, world.View.WetGeneration[0]);
+ Assert.Equal(-1, world.View.WetParticle[0]);
+ }
+
+ [Fact]
+ public void TakeWet_two_sensors_in_one_call()
+ {
+ // disjoint: the first over A and B, the second over C.
+ var (world, s) = TakeFloor((-0.2f, 0.15f), (0.4f, 0.1f));
+ using (world)
+ {
+ var (pa, pc) = (world.View.SensorWetVolume[s[0]], world.View.SensorWetVolume[s[1]]);
+ Assert.True(pa > 0f && pc > 0f);
+ var taken = new float[2];
+ world.TakeWet(s, [pa / 2f, pc / 2f], taken);
+ Assert.Equal(pa / 2f, taken[0], pa * 1e-5f);
+ Assert.Equal(pc / 2f, taken[1], pc * 1e-5f);
+ world.Step(Dt, Sub);
+ Assert.Equal(pa / 2f, world.View.SensorWetVolume[s[0]], pa * 1e-5f);
+ Assert.Equal(pc / 2f, world.View.SensorWetVolume[s[1]], pc * 1e-5f);
+ }
+ // overlapping: the same sphere twice: the second sees what the first left, asks for more than is there, and no patch goes below 0.
+ var (world2, t) = TakeFloor((-0.2f, 0.15f), (-0.2f, 0.15f));
+ using (world2)
+ {
+ var before = world2.View.WetPatchVolume.ToArray();
+ var x = world2.View.WetX.ToArray();
+ var pooled = world2.View.SensorWetVolume[t[0]];
+ var taken = new float[2];
+ var first = pooled / 3f;
+ world2.TakeWet(t, [first, 10f], taken);
+ Assert.Equal(first, taken[0], first * 1e-5f);
+ Assert.Equal(pooled - first, taken[1], pooled * 1e-5f);
+ var v = world2.View;
+ for (var w = 0; w < v.WetCount; w++) Assert.True(v.WetPatchVolume[w] >= 0f && v.WetPatchVolume[w] <= before[w]);
+ world2.Step(Dt, Sub);
+ Assert.Equal(0f, world2.View.SensorWetVolume[t[0]]);
+ Assert.Equal(0f, world2.View.SensorWetVolume[t[1]]);
+ for (var w = 0; w < v.WetCount; w++) if (x[w] > 0f) Assert.Equal(before[w], world2.View.WetPatchVolume[w]); // C untouched
+ }
+ }
+
+ [Fact]
+ public void TakeWet_a_later_sensor_finds_its_patch_after_an_earlier_take_removed_one()
+ {
+ // A lies first in the body's index by centre x: removing it resets its centre to 0, which would put it between B and C and hide B from the second sensor's search.
+ var (world, s) = TakeFloor((-0.3f, 0.05f), (-0.1f, 0.05f));
+ using var _ = world;
+ var pb = world.View.SensorWetVolume[s[1]];
+ Assert.True(pb > 0f && world.View.SensorWetVolume[s[0]] > 0f);
+ var taken = new float[2];
+ world.TakeWet(s, [10f, 10f], taken);
+ Assert.Equal(pb, taken[1]);
+ Assert.Equal(1, Enumerable.Range(0, world.View.WetCount).Count(w => world.View.WetLive[w] != 0)); // only C is left
+ }
+
+ [Fact]
+ public void TakeWet_refusals_change_nothing()
+ {
+ var (world, s) = TakeFloor((-0.2f, 0.15f), (0.4f, 0.1f));
+ using var _ = world;
+ var wet = WetBytes(world.View);
+ var good = world.View.SensorWetVolume[s[0]] * 0.5f;
+ void Unchanged() => Assert.True(wet.AsSpan().SequenceEqual(WetBytes(world.View)), "a wet lane changed on a refusal");
+ // the first sensor is valid and would take; each refusal is in the second place, so the first must not have run
+ Assert.ThrowsAny(() => world.TakeWet(s, [good, -1f])); Unchanged();
+ Assert.ThrowsAny(() => world.TakeWet(s, [good, float.NaN])); Unchanged();
+ Assert.ThrowsAny(() => world.TakeWet(s, [good, float.PositiveInfinity])); Unchanged();
+ Assert.ThrowsAny(() => world.TakeWet([s[0], s[0]], [good, good])); Unchanged();
+ Assert.ThrowsAny(() => world.TakeWet([s[0], s[1] with { Generation = s[1].Generation + 1 }], [good, good])); Unchanged();
+ var gone = s[1];
+ world.Remove(gone);
+ Assert.ThrowsAny(() => world.TakeWet([s[0], gone], [good, good])); Unchanged();
+ Assert.Throws(() => world.TakeWet([new Box3DSoaConstraintHandle(Box3DSoaConstraintType.Wet, 0, 0)], [good])); Unchanged();
+ Assert.Throws(() => world.TakeWet([s[0]], [good, good])); Unchanged();
+ Assert.Throws(() => world.TakeWet([s[0], s[0]], [good, good], new float[1])); Unchanged();
+ }
+}
diff --git a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWetTests.cs b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWetTests.cs
index df7252e..bf6673e 100644
--- a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWetTests.cs
+++ b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWetTests.cs
@@ -453,7 +453,7 @@ public sealed partial class Box3DSoaFluidTests
Assert.Contains("rederive_wet(p);", branch);
Assert.DoesNotContain("rederive_topology", branch);
Assert.DoesNotContain("TOPO_", branch);
- Assert.Equal(4, System.Text.RegularExpressions.Regex.Matches(source, @"[^_]rederive_wet\(p\);").Count); // settle, remove(WET), release_body (a removed body takes its patches), and destroy when a destroyed solid takes its patches (particle_wet_mu goes dry again)
+ Assert.Equal(6, System.Text.RegularExpressions.Regex.Matches(source, @"[^_]rederive_wet\(p\);").Count); // settle, take_wet (twice: before a later sensor walks over a lane a take killed, and at the end), remove(WET), release_body (a removed body takes its patches), and destroy when a destroyed solid takes its patches (particle_wet_mu goes dry again)
}
// ---- determinism ----------------------------------------------------------------------------------------