Attach is a batch command ending in one derivation; ONE_WAY leaves its body unpushed (ABI 17)
ymir_particles_attach takes n descs: validate all, FULL when fewer than n lanes are free, lowest lanes by cursor into lane_list, one TOPO_FULL, every lane backed out on a refusal. ymir_box3d_soa_attach checks each desc's removed body. YMIR_ATTACH_FLAG_ONE_WAY (4): end_w is 0 and end_push books nothing, refused on STATIC (the known-bit mask extended, no bump for the bit). C#: Box3DSoaAttachmentDesc public with a constructor, Attach(span, handles), ExpectedAbi 17. Follow-up ymir-flesh-contact-s1-lows: the walk_half source-text pin becomes a behavioural scene (0.400 shipped, 24.4 with the skip removed); the retired-shape table write asserts its room in Debug; the full Debug Soa suite ran to completion (220/220). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
e51cf76202
commit
e3c01bed9e
10 changed files with 490 additions and 70 deletions
|
|
@ -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 16) so the Aetheria session's contract is untouched.
|
||||
separate native object (`ymir_box3d_soa.c`, ABI 17) so the Aetheria session's contract is untouched.
|
||||
|
||||
## Contract
|
||||
|
||||
- Native (ABI 16): `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 / remove / set_material / free_slots /
|
||||
- Native (ABI 17): `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*) / 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.
|
||||
|
|
@ -132,14 +132,14 @@ separate native object (`ymir_box3d_soa.c`, ABI 16) so the Aetheria session's co
|
|||
component`. The phase word: bits 0-19 are the group, bit 20 `SELF_COLLIDE`, bit 21 `SELF_FILTER`, bit 22 `FLUID`, bit 23 `NO_PARTICLE_CONTACT` (refused together with `FLUID`); any other bit is refused.
|
||||
Distance lanes: two particles, rest length, compliance (m/N), `tension` (sensor). Bend lanes: two distances that share one
|
||||
particle (and so the three particles a, b, c), rest and limit offsets, compliance. Attachment lanes: kind (body, static,
|
||||
removed), flags (`TETHER`; `LIMB`, Body only: the particle is flesh of the body's limb tree, so contact skips every body whose limb-tree root, the `parent` lane followed to its end, is the attached body's), particle, body, point, compliance, `force` (sensor). Held lanes (derived, command time): per particle the list of bodies its live Body attachments name, which contact skips, and `limb_body`, the body of its lowest live `LIMB` attachment or -1. Limit: when a particle's `LIMB` attachments name bodies of two limb trees only the lowest one's tree is skipped; the other tree's bones collide, except the bodies the particle is attached to. Tether lanes (derived): attachment,
|
||||
removed), flags (`TETHER`; `ONE_WAY`, Body only: the body side weighs nothing, so the attachment holds its particle to the body's point and never moves or loads the body, for flesh whose mass the bone already carries; `LIMB`, Body only: the particle is flesh of the body's limb tree, so contact skips every body whose limb-tree root, the `parent` lane followed to its end, is the attached body's), particle, body, point, compliance, `force` (sensor). Held lanes (derived, command time): per particle the list of bodies its live Body attachments name, which contact skips, and `limb_body`, the body of its lowest live `LIMB` attachment or -1. Limit: when a particle's `LIMB` attachments name bodies of two limb trees only the lowest one's tree is skipped; the other tree's bones collide, except the bodies the particle is attached to. Tether lanes (derived): attachment,
|
||||
particle, bound, parent tether, edge, other attachment. Material table: friction, XSPH viscosity, wet friction and the caller's material id. Handles are
|
||||
(lane, generation).
|
||||
- Commands, each refused whole before anything changes: `create_particles(n, desc{position, velocity, rest, mass, radius,
|
||||
phase, material})` takes the lowest free lanes; `destroy_particles(n, handles)` removes every constraint that touches them;
|
||||
`create_distances(n, desc{a, b, rest (< 0 = rest-pose length), compliance})`; `create_bends(n, desc{e1, e2, compliance
|
||||
(< 0 = soft pass off), max_angle (< 0 = no limit)})`, refused unless the two distances share exactly one particle and the
|
||||
bend does something; `attach(desc{kind body|static, flags, particle, body, point, compliance})`; `remove(type distance|bend|
|
||||
bend does something; `attach(n, desc{kind body|static, flags, particle, body, point, compliance}*)` (a batch like `create_distances`: every desc validated, the lowest free lanes by cursor, one derivation, backed out whole on a refusal); `remove(type distance|bend|
|
||||
attachment, n, handles)`; `set_material(index, desc{friction, viscosity (>= 0), material_id})`; `free_slots(lane set)`. A distance takes
|
||||
the bends over it with it. Ymir never removes a constraint or releases an attachment on a load: tear, cut, gib and release
|
||||
are the caller's say-so.
|
||||
|
|
@ -303,7 +303,7 @@ no allocation across 100 steps with fact reads. `Box3DSoaImpulseTests`: `contact
|
|||
|
||||
`Box3DSoaRemoveTests`: removing a body retires its joints and zeroes its lane (old handles stale, others untouched, facts one step), is refused while an attachment or a liquid sensor names it, each alone and for an anchor too (nothing changed; after the caller removes them it succeeds; attach, sensors, add_joint and a parent then refuse the lane), refuses a current-generation handle to a tombstone (destroy_joint and remove_body) and lets an add reuse stable id 0, skips a removed lane in the particle substeps and the write-lane check (its write lanes are still cleared), names every removed toucher over 200 single-removal steps in a two-lane world (the retired table is cleared each step), survives 12 seeds of random add/remove churn on a floor (run in Debug too: Box3D validates its broad-phase there), takes its wet patches with it (count and summed volume on the fact, another body's patches untouched), ends a grab when the anchor goes, ends a touch by name, leaves a drop unable to settle, keeps the other lanes' state, reuses a lane one step later with a new generation, repeats bit for bit with removals and thresholds over 300 steps and allocates nothing managed. `Box3DSoaJointTests`: the constraint lanes read m g, an overload is reported once its load reaches the threshold and the joint stays, thresholds are refused when NaN or negative. `Box3DSoaGrabTests`: every joint kind destroys through one path. `Box3DSoaParticleTests`: the ABI 8 scene's six checkpoint hashes with no particle capacity (pinned from 5104b17); a 48-particle rope (a builder in the tests: distances and soft bends) hanging from a tethered static point under 1% stretch for 4 s; a 70 kg sphere held by the same rope (from 1 s to 5 s speed under 0.001 m/s, stretch under 0.1%, the impulse per tick within 0.5% of m g dt); an anchor dragging a 5 kg box along the floor (`particle_impulse` reads it); a sphere, capsule, box and static box each pushed into a lying rope's end (no particle over 10% of its radius inside); a 16 x 16 sheet from the same two commands hung by two corners (after 5 s the corners carry the weight within 1% and no edge is past 5%), and by two corners of two pins each (after 8 s, same bounds: it still rings at 5 s); edge tension, the edge at 0, 10 and 46 and both attachment forces read the load at 1, 5, 20 and 70 kg; a cut (remove an edge of a hung 40-rope: the bends over it die, tethers 39 -> 19, the component splits, the head holds and the tail falls); overload (300 kg) removes nothing; destroying a mid-rope particle removes its constraints and releases a held body; a 24-particle cantilever at 32 substeps under a 30 and a 10 degree hard limit and a stiff and a floppy soft pass, and a limit-only bend bit-identical to no bend inside its cone; momentum of a rope, a sheet and a blob thrown into each other with no gravity within 5e-4 and energy never rising; phase rules (groups, `SELF_COLLIDE`, `SELF_FILTER`, a folded rope); an off-centre attachment spinning a free box by I^-1 (r x J) within 2%; lanes reused after a step lowest first, stale generations refused, a derivation that does not fit refused with no lane changed, bad input refused; a pile of four ropes and a sheet on two bodies byte-equal across two runs at six checkpoints (pinned), a one-ulp difference diverging, and eight concurrent worlds giving the serial bytes; no allocation across 100 steps and across a cut, a destroy and a create, and none for a 300-distance batch created and removed; gravity along each axis; a spinning box carrying its attachment point and two ends on one light box settling (the proxy's rotation); one group's `SELF_COLLIDE` needing both particles and `SELF_FILTER` either; a slack pair tether leaving its body alone; a refused `attach` leaving no lane and no tether; each particle on the nearest point of its two nearest islands (a 5-rope held at both ends, a star whose centre keeps the two nearer arms, a 9-rope with two touching points counting as one island) and no pair between two static points; every tether's parent present after each of 20 attaches on a 32 x 32 sheet (float ties at rank 2 gated); a particle held by two bodies touching neither. `Box3DSoaParticleSourceTests` reads the C source: no Box3D call in `ymir_particles.*`, no allocation outside create and free, no threads or atomics, no rope, sheet, cloth or chain identifier in the solver or the binding, the tether and component lanes written only by the derivation, every topology command ending in it, `begin` and `end` called once (in the step), `substep` once (in the callback), the callback the only user of the fork's two accessors, no topology change in the phases, and the retired names absent.
|
||||
|
||||
`Box3DSoaCouplingTests`: a sphere thrown at 5 m/s with a 48 x 20 g rope from rest keeps x momentum within 0.1% and gains no energy, at 5 and 70 kg; thrown up it reaches the apex momentum allows; 5 kg snapping a taut rope at 6 m/s keeps at most 5% of its energy and 70 kg is still within a tick; a 45 degree pendulum for 12 s gains no energy and loses at most 1.25 times a heavy-particle control, and a hung body keeps its spin about the vertical; removing the body attachment or destroying the rope's particles frees a held 70 kg body in the step after, with no upward impulse; `particle_impulse` equals M dv minus M g dt every tick, hung and thrown; material friction 0.6 holds a rope on a 20 degree slope and friction 0 does not; no particle is farther from a tethered point than its rest path; an attached end stays on its body point; a world whose only body is static still steps the particles (a dropped rope falls g t^2 / 2 within 2%) and rests; and a cost line with 16 ropes x 48 and one 16 x 16 sheet at 4 substeps, one body and forty (printed, not asserted).
|
||||
`Box3DSoaCouplingTests` (also the batch file): a 256-attachment sheet attached one by one and as one batch ends with equal lanes, generations and state after 120 steps; a batch with one bad desc (a fluid particle, a removed body, `STATIC` with `ONE_WAY` or `LIMB`), more descs than free lanes, or a derivation past the tether capacity changes no lane; `ONE_WAY` leaves its box at speed 0 with a 0 impulse lane while the particle holds its point; spawn cost lines for 256 attachments and for one and four Fellas. A sphere thrown at 5 m/s with a 48 x 20 g rope from rest keeps x momentum within 0.1% and gains no energy, at 5 and 70 kg; thrown up it reaches the apex momentum allows; 5 kg snapping a taut rope at 6 m/s keeps at most 5% of its energy and 70 kg is still within a tick; a 45 degree pendulum for 12 s gains no energy and loses at most 1.25 times a heavy-particle control, and a hung body keeps its spin about the vertical; removing the body attachment or destroying the rope's particles frees a held 70 kg body in the step after, with no upward impulse; `particle_impulse` equals M dv minus M g dt every tick, hung and thrown; material friction 0.6 holds a rope on a 20 degree slope and friction 0 does not; no particle is farther from a tethered point than its rest path; an attached end stays on its body point; a world whose only body is static still steps the particles (a dropped rope falls g t^2 / 2 within 2%) and rests; and a cost line with 16 ropes x 48 and one 16 x 16 sheet at 4 substeps, one body and forty (printed, not asserted).
|
||||
|
||||
## Not done
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
#include "ymir_particles.h"
|
||||
#include "ymir_world_lock.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
|
|
@ -61,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 16u
|
||||
#define YMIR_SOA_ABI_VERSION 17u
|
||||
|
||||
// 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
|
||||
|
|
@ -1211,6 +1212,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_remove_body(ymir_box3d_soa_world* w
|
|||
b3ShapeId shape;
|
||||
if (!ymir_soa_is_anchor(world->kind[lane]) && b3Body_GetShapes(world->bodies[lane], &shape, 1) == 1)
|
||||
{
|
||||
assert(world->retired_count < (uint32_t)world->capacity); // Debug: one step's removals fit; the step clears the table
|
||||
world->retired_shapes[world->retired_count++] = (ymir_soa_retired_shape){shape, lane, world->stable_id[lane]};
|
||||
}
|
||||
world->body_removed_pending[world->body_removed_pending_count++] = (ymir_box3d_soa_body_removed_fact){
|
||||
|
|
@ -1739,18 +1741,21 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_create_bends(
|
|||
}
|
||||
|
||||
YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_attach(
|
||||
ymir_box3d_soa_world* world, const ymir_attachment_desc* d, ymir_handle* out)
|
||||
ymir_box3d_soa_world* world, int32_t n, const ymir_attachment_desc* d, ymir_handle* out)
|
||||
{
|
||||
if (world == NULL || world->particles == NULL)
|
||||
if (world == NULL || world->particles == NULL || n < 0 || (n > 0 && d == NULL))
|
||||
{
|
||||
return YMIR_SOA_INVALID_ARGUMENT;
|
||||
}
|
||||
if (d != NULL && d->kind == YMIR_ATTACH_BODY && d->body >= 0 && d->body < world->count &&
|
||||
world->kind[d->body] == YMIR_SOA_KIND_REMOVED)
|
||||
for (int32_t k = 0; k < n; ++k)
|
||||
{
|
||||
return YMIR_SOA_INVALID_ARGUMENT;
|
||||
if (d[k].kind == YMIR_ATTACH_BODY && d[k].body >= 0 && d[k].body < world->count &&
|
||||
world->kind[d[k].body] == YMIR_SOA_KIND_REMOVED)
|
||||
{
|
||||
return YMIR_SOA_INVALID_ARGUMENT;
|
||||
}
|
||||
}
|
||||
return ymir_particles_attach(world->particles, d, world->count, world->step_count, out);
|
||||
return ymir_particles_attach(world->particles, n, d, world->count, world->step_count, out);
|
||||
}
|
||||
|
||||
YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_remove(ymir_box3d_soa_world* world, uint32_t type, int32_t n, const ymir_handle* h)
|
||||
|
|
|
|||
|
|
@ -137,7 +137,7 @@ static size_t layout(ymir_particles* p, unsigned char* base)
|
|||
CARVE(dp_x, float, np); CARVE(dp_y, float, np); CARVE(dp_z, float, np);
|
||||
CARVE(dist_acc, float, nd); CARVE(lambda, float, np);
|
||||
CARVE(tacc_x, float, nt); CARVE(tacc_y, float, nt); CARVE(tacc_z, float, nt);
|
||||
CARVE(lane_list, int32_t, nd); CARVE(cell_x, int32_t, np); CARVE(cell_y, int32_t, np); CARVE(cell_z, int32_t, np);
|
||||
CARVE(lane_list, int32_t, nd > na ? nd : na); CARVE(cell_x, int32_t, np); CARVE(cell_y, int32_t, np); CARVE(cell_z, int32_t, np);
|
||||
CARVE(cell_start, int32_t, table + 1); CARVE(cell_fill, int32_t, table); CARVE(sorted, int32_t, np);
|
||||
CARVE(contacts, int32_t, np);
|
||||
CARVE(sx, float, np); CARVE(sy, float, np); CARVE(sz, float, np); CARVE(sw, float, np); CARVE(sphase, uint32_t, np);
|
||||
|
|
@ -1160,51 +1160,67 @@ int32_t ymir_particles_create_bends(ymir_particles* p, int32_t n, const ymir_ben
|
|||
return rederive_topology(p, TOPO_FULL, NULL, 0);
|
||||
}
|
||||
|
||||
int32_t ymir_particles_attach(ymir_particles* p, const ymir_attachment_desc* d, int32_t body_count, uint64_t step_count, ymir_handle* out)
|
||||
int32_t ymir_particles_attach(ymir_particles* p, int32_t n, const ymir_attachment_desc* d, int32_t body_count, uint64_t step_count, ymir_handle* out)
|
||||
{
|
||||
if (p == NULL || d == NULL || (d->kind != YMIR_ATTACH_BODY && d->kind != YMIR_ATTACH_STATIC) ||
|
||||
(d->flags & ~(uint32_t)(YMIR_ATTACH_FLAG_TETHER | YMIR_ATTACH_FLAG_LIMB)) != 0 ||
|
||||
(d->kind == YMIR_ATTACH_STATIC && (d->flags & YMIR_ATTACH_FLAG_LIMB) != 0) || !particle_ok(p, d->particle.lane, d->particle.generation) ||
|
||||
!finite3(d->point) || !isfinite(d->compliance) || d->compliance < 0.0f ||
|
||||
(p->phase[d->particle.lane] & YMIR_PHASE_FLUID) != 0) // fluid holds no constraint
|
||||
if (p == NULL || n < 0 || (n > 0 && d == NULL))
|
||||
{
|
||||
return YMIR_PARTICLES_INVALID_ARGUMENT;
|
||||
}
|
||||
if (d->kind == YMIR_ATTACH_BODY && (d->body < 0 || d->body >= body_count))
|
||||
for (int32_t k = 0; k < n; ++k)
|
||||
{
|
||||
return YMIR_PARTICLES_INVALID_ARGUMENT;
|
||||
if ((d[k].kind != YMIR_ATTACH_BODY && d[k].kind != YMIR_ATTACH_STATIC) ||
|
||||
(d[k].flags & ~(uint32_t)(YMIR_ATTACH_FLAG_TETHER | YMIR_ATTACH_FLAG_LIMB | YMIR_ATTACH_FLAG_ONE_WAY)) != 0 ||
|
||||
(d[k].kind == YMIR_ATTACH_STATIC && (d[k].flags & (YMIR_ATTACH_FLAG_LIMB | YMIR_ATTACH_FLAG_ONE_WAY)) != 0) ||
|
||||
!particle_ok(p, d[k].particle.lane, d[k].particle.generation) || !finite3(d[k].point) || !isfinite(d[k].compliance) ||
|
||||
d[k].compliance < 0.0f || (p->phase[d[k].particle.lane] & YMIR_PHASE_FLUID) != 0 || // fluid holds no constraint
|
||||
(d[k].kind == YMIR_ATTACH_BODY && (d[k].body < 0 || d[k].body >= body_count)))
|
||||
{
|
||||
return YMIR_PARTICLES_INVALID_ARGUMENT;
|
||||
}
|
||||
}
|
||||
int32_t a = next_lane(p->att_kind, YMIR_ATTACH_REMOVED, p->att_removed_at, p->attachment_count, p->attachment_capacity, 0, step_count);
|
||||
if (a < 0)
|
||||
if (free_count(p->att_kind, YMIR_ATTACH_REMOVED, p->att_removed_at, p->attachment_count, p->attachment_capacity, step_count) < n)
|
||||
{
|
||||
return YMIR_PARTICLES_FULL;
|
||||
}
|
||||
int32_t old_count = p->attachment_count;
|
||||
p->attachment_count = a + 1 > p->attachment_count ? a + 1 : p->attachment_count;
|
||||
p->att_kind[a] = d->kind;
|
||||
p->att_flags[a] = d->flags;
|
||||
p->att_particle[a] = d->particle.lane;
|
||||
p->att_body[a] = d->kind == YMIR_ATTACH_BODY ? d->body : -1;
|
||||
p->att_point_x[a] = d->point[0];
|
||||
p->att_point_y[a] = d->point[1];
|
||||
p->att_point_z[a] = d->point[2];
|
||||
p->att_compliance[a] = d->compliance;
|
||||
p->att_force_x[a] = p->att_force_y[a] = p->att_force_z[a] = 0.0f;
|
||||
int32_t old_count = p->attachment_count, cursor = 0;
|
||||
for (int32_t k = 0; k < n; ++k)
|
||||
{
|
||||
int32_t a = next_lane(p->att_kind, YMIR_ATTACH_REMOVED, p->att_removed_at, p->attachment_count, p->attachment_capacity, cursor, step_count);
|
||||
cursor = a + 1;
|
||||
p->lane_list[k] = a;
|
||||
p->attachment_count = a + 1 > p->attachment_count ? a + 1 : p->attachment_count;
|
||||
p->att_kind[a] = d[k].kind;
|
||||
p->att_flags[a] = d[k].flags;
|
||||
p->att_particle[a] = d[k].particle.lane;
|
||||
p->att_body[a] = d[k].kind == YMIR_ATTACH_BODY ? d[k].body : -1;
|
||||
p->att_point_x[a] = d[k].point[0];
|
||||
p->att_point_y[a] = d[k].point[1];
|
||||
p->att_point_z[a] = d[k].point[2];
|
||||
p->att_compliance[a] = d[k].compliance;
|
||||
p->att_force_x[a] = p->att_force_y[a] = p->att_force_z[a] = 0.0f;
|
||||
}
|
||||
int32_t status = rederive_topology(p, TOPO_FULL, NULL, 0);
|
||||
if (status != YMIR_PARTICLES_OK)
|
||||
{
|
||||
p->att_kind[a] = a < old_count ? YMIR_ATTACH_REMOVED : 0;
|
||||
p->att_flags[a] = 0;
|
||||
p->att_particle[a] = a < old_count ? -1 : 0;
|
||||
p->att_body[a] = a < old_count ? -1 : 0;
|
||||
p->att_point_x[a] = p->att_point_y[a] = p->att_point_z[a] = 0.0f;
|
||||
p->att_compliance[a] = 0.0f;
|
||||
for (int32_t k = 0; k < n; ++k) // back out: the lanes read as they did before the command
|
||||
{
|
||||
int32_t a = p->lane_list[k];
|
||||
p->att_kind[a] = a < old_count ? YMIR_ATTACH_REMOVED : 0;
|
||||
p->att_flags[a] = 0;
|
||||
p->att_particle[a] = a < old_count ? -1 : 0;
|
||||
p->att_body[a] = a < old_count ? -1 : 0;
|
||||
p->att_point_x[a] = p->att_point_y[a] = p->att_point_z[a] = 0.0f;
|
||||
p->att_compliance[a] = 0.0f;
|
||||
}
|
||||
p->attachment_count = old_count;
|
||||
return status;
|
||||
}
|
||||
if (out != NULL)
|
||||
{
|
||||
*out = (ymir_handle){a, p->att_generation[a]};
|
||||
for (int32_t k = 0; k < n; ++k)
|
||||
{
|
||||
out[k] = (ymir_handle){p->lane_list[k], p->att_generation[p->lane_list[k]]};
|
||||
}
|
||||
}
|
||||
return YMIR_PARTICLES_OK;
|
||||
}
|
||||
|
|
@ -1405,13 +1421,13 @@ static v3 end_point(const ymir_particles* p, int32_t a)
|
|||
|
||||
static float end_w(const ymir_particles* p, int32_t a, v3 at, v3 n)
|
||||
{
|
||||
return p->att_kind[a] == YMIR_ATTACH_BODY ? proxy_w(p->proxies + p->att_body[a], at, n) : 0.0f;
|
||||
return p->att_kind[a] == YMIR_ATTACH_BODY && !(p->att_flags[a] & YMIR_ATTACH_FLAG_ONE_WAY) ? proxy_w(p->proxies + p->att_body[a], at, n) : 0.0f;
|
||||
}
|
||||
|
||||
static void end_push(const phase* f, int32_t a, v3 at, v3 imp)
|
||||
{
|
||||
ymir_particles* p = f->p;
|
||||
if (p->att_kind[a] == YMIR_ATTACH_BODY)
|
||||
if (p->att_kind[a] == YMIR_ATTACH_BODY && !(p->att_flags[a] & YMIR_ATTACH_FLAG_ONE_WAY))
|
||||
{
|
||||
proxy_push(f, p->proxies + p->att_body[a], at, imp);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -68,7 +68,8 @@ enum
|
|||
enum
|
||||
{
|
||||
YMIR_ATTACH_FLAG_TETHER = 1u, // bounds each particle by the rest path to the nearest point of its two nearest islands of tether attachments
|
||||
YMIR_ATTACH_FLAG_LIMB = 2u // the particle is flesh of its body's limb tree: contact skips every body whose limb-tree root is the attached body's root; refused on a STATIC attachment
|
||||
YMIR_ATTACH_FLAG_LIMB = 2u, // the particle is flesh of its body's limb tree: contact skips every body whose limb-tree root is the attached body's root; refused on a STATIC attachment
|
||||
YMIR_ATTACH_FLAG_ONE_WAY = 4u // the body side weighs nothing: the attachment holds its particle to the body's point and never moves or loads the body (the bone's own mass already carries the flesh); refused on a STATIC attachment
|
||||
};
|
||||
|
||||
enum
|
||||
|
|
@ -279,7 +280,8 @@ int32_t ymir_particles_create_particles(ymir_particles* p, int32_t n, const ymir
|
|||
int32_t ymir_particles_destroy_particles(ymir_particles* p, int32_t n, const ymir_handle* h, uint64_t step_count);
|
||||
int32_t ymir_particles_create_distances(ymir_particles* p, int32_t n, const ymir_distance_desc* d, uint64_t step_count, ymir_handle* out);
|
||||
int32_t ymir_particles_create_bends(ymir_particles* p, int32_t n, const ymir_bend_desc* d, uint64_t step_count, ymir_handle* out);
|
||||
int32_t ymir_particles_attach(ymir_particles* p, const ymir_attachment_desc* d, int32_t body_count, uint64_t step_count, ymir_handle* out);
|
||||
// Attaches n particles in the lowest free lanes, by cursor, as a batch: every desc is validated, FULL when fewer than n lanes are free, one derivation, and a derivation that does not fit backs every lane out.
|
||||
int32_t ymir_particles_attach(ymir_particles* p, int32_t n, const ymir_attachment_desc* d, int32_t body_count, uint64_t step_count, ymir_handle* out);
|
||||
int32_t ymir_particles_remove(ymir_particles* p, uint32_t type, int32_t n, const ymir_handle* h, uint64_t step_count);
|
||||
// Settles the named fluid particles into wet patches and destroys them. Each reads its support from the last step (support_body, else
|
||||
// support_particle) and its contact point from the proxies the caller has just filled; a drop's volume is (2 r)^3. Refuses whole
|
||||
|
|
|
|||
|
|
@ -173,9 +173,12 @@ public struct Box3DSoaBendDesc
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>Native layout of <c>ymir_attachment_desc</c>.</summary>
|
||||
/// <summary>
|
||||
/// Native layout of <c>ymir_attachment_desc</c>; see <see cref="Box3DSoaWorld.Attach(Box3DSoaAttachKind, Box3DSoaParticleHandle, int, ValueTuple{float, float, float}, float, bool, bool, bool)"/>
|
||||
/// for the fields.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct Box3DSoaAttachmentDesc
|
||||
public struct Box3DSoaAttachmentDesc
|
||||
{
|
||||
public uint Kind;
|
||||
public uint Flags;
|
||||
|
|
@ -184,6 +187,19 @@ internal struct Box3DSoaAttachmentDesc
|
|||
public float PointX, PointY, PointZ;
|
||||
public float Compliance;
|
||||
public uint Reserved;
|
||||
|
||||
public Box3DSoaAttachmentDesc(
|
||||
Box3DSoaAttachKind kind, Box3DSoaParticleHandle particle, int target = -1, (float X, float Y, float Z) point = default,
|
||||
float compliance = 0f, bool tether = false, bool limb = false, bool oneWay = false)
|
||||
{
|
||||
Kind = (uint)kind;
|
||||
Flags = (tether ? 1u : 0u) | (limb ? 2u : 0u) | (oneWay ? 4u : 0u);
|
||||
Particle = particle;
|
||||
Body = target;
|
||||
PointX = point.X; PointY = point.Y; PointZ = point.Z;
|
||||
Compliance = compliance;
|
||||
Reserved = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Native layout of <c>ymir_sensor_desc</c>: a sphere of <c>Radius</c> m (> 0) centred on <c>Point</c> in the frame of body lane <c>Body</c>.</summary>
|
||||
|
|
@ -649,7 +665,7 @@ internal static unsafe partial class Box3DSoaNative
|
|||
|
||||
[LibraryImport(Library)]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
|
||||
internal static partial int ymir_box3d_soa_attach(nint world, in Box3DSoaAttachmentDesc desc, out Box3DSoaParticleHandle handle);
|
||||
internal static partial int ymir_box3d_soa_attach(nint world, int n, Box3DSoaAttachmentDesc* desc, Box3DSoaParticleHandle* output);
|
||||
|
||||
[LibraryImport(Library)]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
|
||||
|
|
@ -940,7 +956,7 @@ public readonly unsafe struct Box3DSoaView
|
|||
/// </summary>
|
||||
public sealed class Box3DSoaWorld : IDisposable
|
||||
{
|
||||
private const uint ExpectedAbi = 16;
|
||||
private const uint ExpectedAbi = 17;
|
||||
private nint _handle;
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -1277,25 +1293,47 @@ public sealed class Box3DSoaWorld : IDisposable
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ties each particle of <paramref name="attachments"/> to a body point or a world point, as one command ending in one derivation:
|
||||
/// lanes are taken lowest first in order, and a refusal (a bad desc, a removed body, no free lanes, tethers past capacity) throws
|
||||
/// with nothing changed. <paramref name="handles"/> may be empty.
|
||||
/// </summary>
|
||||
public unsafe void Attach(ReadOnlySpan<Box3DSoaAttachmentDesc> attachments, Span<Box3DSoaConstraintHandle> handles = default)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_handle == 0, this);
|
||||
if (!handles.IsEmpty && handles.Length < attachments.Length) throw new ArgumentException("One handle per attachment.", nameof(handles));
|
||||
Box3DSoaParticleHandle[]? rented = null;
|
||||
Span<Box3DSoaParticleHandle> native = attachments.Length <= 256 ? stackalloc Box3DSoaParticleHandle[attachments.Length] : (rented = ArrayPool<Box3DSoaParticleHandle>.Shared.Rent(attachments.Length)).AsSpan(0, attachments.Length);
|
||||
try
|
||||
{
|
||||
fixed (Box3DSoaAttachmentDesc* d = attachments)
|
||||
fixed (Box3DSoaParticleHandle* h = native)
|
||||
Check(Box3DSoaNative.ymir_box3d_soa_attach(_handle, attachments.Length, d, h));
|
||||
for (var i = 0; i < handles.Length && i < native.Length; i++)
|
||||
handles[i] = new Box3DSoaConstraintHandle(Box3DSoaConstraintType.Attachment, native[i].Lane, native[i].Generation);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (rented is not null) ArrayPool<Box3DSoaParticleHandle>.Shared.Return(rented);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ties <paramref name="particle"/> to a body point (kind Body: <paramref name="target"/> is the body lane, <paramref name="point"/> in
|
||||
/// its frame) or a world point (Static). With <paramref name="tether"/> the point also bounds every particle of the component by
|
||||
/// the rest path. With <paramref name="limb"/> (Body only) the particle is flesh of the body's limb tree: contact skips every body
|
||||
/// that shares the attached body's limb-tree root, not just the attached body. <paramref name="compliance"/> is m/N, 0 for rigid.
|
||||
/// Attach where the particle already is.
|
||||
/// that shares the attached body's limb-tree root, not just the attached body. With <paramref name="oneWay"/> (Body only) the body
|
||||
/// side weighs nothing: the particle is held to the body's point and the body is never moved or loaded by it.
|
||||
/// <paramref name="compliance"/> is m/N, 0 for rigid. Attach where the particle already is.
|
||||
/// </summary>
|
||||
public Box3DSoaConstraintHandle Attach(
|
||||
Box3DSoaAttachKind kind, Box3DSoaParticleHandle particle, int target = -1, (float X, float Y, float Z) point = default,
|
||||
float compliance = 0f, bool tether = false, bool limb = false)
|
||||
float compliance = 0f, bool tether = false, bool limb = false, bool oneWay = false)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_handle == 0, this);
|
||||
var desc = new Box3DSoaAttachmentDesc
|
||||
{
|
||||
Kind = (uint)kind, Flags = (tether ? 1u : 0u) | (limb ? 2u : 0u), Particle = particle, Body = target,
|
||||
PointX = point.X, PointY = point.Y, PointZ = point.Z, Compliance = compliance,
|
||||
};
|
||||
Check(Box3DSoaNative.ymir_box3d_soa_attach(_handle, desc, out var handle));
|
||||
return new Box3DSoaConstraintHandle(Box3DSoaConstraintType.Attachment, handle.Lane, handle.Generation);
|
||||
var desc = new Box3DSoaAttachmentDesc(kind, particle, target, point, compliance, tether, limb, oneWay);
|
||||
Span<Box3DSoaConstraintHandle> handle = stackalloc Box3DSoaConstraintHandle[1];
|
||||
Attach(new ReadOnlySpan<Box3DSoaAttachmentDesc>(in desc), handle);
|
||||
return handle[0];
|
||||
}
|
||||
|
||||
/// <summary>Removes constraints of one type (a distance takes the bends over it with it). A stale handle is refused.</summary>
|
||||
|
|
|
|||
333
tests/Ymir.Box3D.Soa.Tests/Box3DSoaCouplingBatchTests.cs
Normal file
333
tests/Ymir.Box3D.Soa.Tests/Box3DSoaCouplingBatchTests.cs
Normal file
|
|
@ -0,0 +1,333 @@
|
|||
using System.Runtime.InteropServices;
|
||||
using Ymir.Box3D;
|
||||
|
||||
namespace Ymir.Box3D.Soa.Tests;
|
||||
|
||||
using Pos = (float X, float Y, float Z);
|
||||
|
||||
/// <summary>Attach as a batch ending in one derivation, and the ONE_WAY flag (docs/soa-world.md, attachment lanes).</summary>
|
||||
public sealed partial class Box3DSoaCouplingTests
|
||||
{
|
||||
private static ulong FnvOf(ulong h, ReadOnlySpan<byte> bytes)
|
||||
{
|
||||
foreach (var b in bytes) { h ^= b; h *= 1099511628211ul; }
|
||||
return h;
|
||||
}
|
||||
|
||||
/// <summary>Every attachment lane, the tether count and the particle position and velocity lanes, as one hash.</summary>
|
||||
private static ulong StateHash(Box3DSoaView v)
|
||||
{
|
||||
var h = 1469598103934665603ul;
|
||||
h = FnvOf(h, MemoryMarshal.AsBytes(v.AttachmentKind));
|
||||
h = FnvOf(h, MemoryMarshal.AsBytes(v.AttachmentFlags));
|
||||
h = FnvOf(h, MemoryMarshal.AsBytes(v.AttachmentGeneration));
|
||||
h = FnvOf(h, MemoryMarshal.AsBytes(v.AttachmentParticle));
|
||||
h = FnvOf(h, MemoryMarshal.AsBytes(v.AttachmentBody));
|
||||
h = FnvOf(h, MemoryMarshal.AsBytes(v.AttachmentPointX));
|
||||
h = FnvOf(h, MemoryMarshal.AsBytes(v.AttachmentPointY));
|
||||
h = FnvOf(h, MemoryMarshal.AsBytes(v.AttachmentPointZ));
|
||||
h = FnvOf(h, MemoryMarshal.AsBytes(v.AttachmentCompliance));
|
||||
h = FnvOf(h, BitConverter.GetBytes(v.AttachmentCount));
|
||||
h = FnvOf(h, BitConverter.GetBytes(v.TetherCount));
|
||||
h = FnvOf(h, MemoryMarshal.AsBytes(v.ParticleX)); h = FnvOf(h, MemoryMarshal.AsBytes(v.ParticleY)); h = FnvOf(h, MemoryMarshal.AsBytes(v.ParticleZ));
|
||||
h = FnvOf(h, MemoryMarshal.AsBytes(v.ParticleVx)); h = FnvOf(h, MemoryMarshal.AsBytes(v.ParticleVy)); h = FnvOf(h, MemoryMarshal.AsBytes(v.ParticleVz));
|
||||
return h;
|
||||
}
|
||||
|
||||
private static Box3DSoaConstraintHandle AttachOne(Box3DSoaWorld world, in Box3DSoaAttachmentDesc d) =>
|
||||
world.Attach((Box3DSoaAttachKind)d.Kind, d.Particle, d.Body, (d.PointX, d.PointY, d.PointZ), d.Compliance,
|
||||
tether: (d.Flags & 1u) != 0, limb: (d.Flags & 2u) != 0, oneWay: (d.Flags & 4u) != 0);
|
||||
|
||||
/// <summary>
|
||||
/// A 16 x 16 sheet (256 particles, distances and bends) and four capsules; attachment k ties particle k to the capsule of its quadrant at the
|
||||
/// particle's own place. Every 7th is ONE_WAY, every 11th LIMB, so the batch carries each flag.
|
||||
/// </summary>
|
||||
private static (Box3DSoaWorld World, Box3DSoaAttachmentDesc[] Descs) SheetOnCapsules()
|
||||
{
|
||||
var world = Box3DSoaParticleTests.World(bodies: 8, particles: 264, distances: 600, bends: 600, attachments: 256, tethers: 8, gravityY: -G);
|
||||
Pos origin = (-0.225f, 0.5f, -0.225f);
|
||||
var sheet = Graph.Sheet(world, origin, (Spacing, 0f, 0f), (0f, 0f, Spacing), 16, 16, ParticleMass, ParticleRadius);
|
||||
var bodies = new Pos[] { (-0.11f, 0.3f, -0.11f), (0.11f, 0.3f, -0.11f), (-0.11f, 0.3f, 0.11f), (0.11f, 0.3f, 0.11f) };
|
||||
var lanes = new int[4];
|
||||
for (var q = 0; q < 4; q++) lanes[q] = world.AddCapsule((ulong)(q + 1), bodies[q], 0.05f, 0.1f, 1000f);
|
||||
var descs = new Box3DSoaAttachmentDesc[256];
|
||||
for (var k = 0; k < 256; k++)
|
||||
{
|
||||
int i = k % 16, j = k / 16, q = (i / 8) + 2 * (j / 8);
|
||||
Pos at = (origin.X + Spacing * i, origin.Y, origin.Z + Spacing * j);
|
||||
descs[k] = new Box3DSoaAttachmentDesc(Box3DSoaAttachKind.Body, sheet.Particles[k], lanes[q], (at.X - bodies[q].X, at.Y - bodies[q].Y, at.Z - bodies[q].Z),
|
||||
compliance: 1e-5f, limb: k % 11 == 0, oneWay: k % 7 == 0);
|
||||
}
|
||||
return (world, descs);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Attach_batch_equals_the_same_attaches_one_by_one()
|
||||
{
|
||||
var (singles, descs) = SheetOnCapsules();
|
||||
var (batch, _) = SheetOnCapsules();
|
||||
var one = new Box3DSoaConstraintHandle[256];
|
||||
var many = new Box3DSoaConstraintHandle[256];
|
||||
using (singles)
|
||||
using (batch)
|
||||
{
|
||||
for (var k = 0; k < 256; k++) one[k] = AttachOne(singles, descs[k]);
|
||||
batch.Attach(descs, many);
|
||||
Assert.Equal(one, many);
|
||||
Assert.Equal(256, batch.View.AttachmentCount);
|
||||
Assert.Equal(StateHash(singles.View), StateHash(batch.View));
|
||||
Box3DSoaParticleTests.Run(singles, 120);
|
||||
Box3DSoaParticleTests.Run(batch, 120);
|
||||
Assert.Equal(StateHash(singles.View), StateHash(batch.View));
|
||||
var line = $"ATTACH_BATCH 256 attachments, 120 steps: lanes and generations equal, state hash {StateHash(batch.View):x16}";
|
||||
Console.Error.WriteLine(line);
|
||||
Reading(line);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Attach_batch_reuses_lanes_lowest_first_as_singles_would()
|
||||
{
|
||||
var (singles, descs) = SheetOnCapsules();
|
||||
var (batch, _) = SheetOnCapsules();
|
||||
using (singles)
|
||||
using (batch)
|
||||
{
|
||||
var first = new Box3DSoaConstraintHandle[256];
|
||||
batch.Attach(descs, first);
|
||||
foreach (var d in descs) AttachOne(singles, d);
|
||||
// remove every third lane in both worlds, step, then attach 40 again: the same lowest free lanes with new generations
|
||||
var gone = first.Where((_, k) => k % 3 == 0).ToArray();
|
||||
singles.Remove(gone);
|
||||
batch.Remove(gone);
|
||||
Box3DSoaParticleTests.Run(singles, 1);
|
||||
Box3DSoaParticleTests.Run(batch, 1);
|
||||
var again = descs.Where((_, k) => k % 3 == 0).Take(40).ToArray();
|
||||
var viaSingles = again.Select(d => AttachOne(singles, d)).ToArray();
|
||||
var viaBatch = new Box3DSoaConstraintHandle[40];
|
||||
batch.Attach(again, viaBatch);
|
||||
Assert.Equal(viaSingles, viaBatch);
|
||||
Assert.True(viaBatch.Zip(gone, (n, o) => n.Lane == o.Lane && n.Generation == o.Generation + 1).Take(40).All(x => x));
|
||||
Assert.Equal(StateHash(singles.View), StateHash(batch.View));
|
||||
}
|
||||
}
|
||||
|
||||
private static string Snapshot(Box3DSoaWorld world)
|
||||
{
|
||||
var v = world.View;
|
||||
return $"{v.AttachmentCount}|{v.TetherCount}|{StateHash(v):x16}|{string.Join(",", v.AttachmentKind.ToArray())}|{string.Join(",", v.AttachmentGeneration.ToArray())}";
|
||||
}
|
||||
|
||||
private static void AssertRefusedWhole(Box3DSoaWorld world, Box3DSoaAttachmentDesc[] batch, string status, string what)
|
||||
{
|
||||
var before = Snapshot(world);
|
||||
var e = Assert.Throws<InvalidOperationException>(() => world.Attach(batch));
|
||||
Assert.Contains($"status {status}", e.Message);
|
||||
Assert.True(before == Snapshot(world), $"{what}: the refusal changed a lane");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_batch_with_one_bad_desc_changes_nothing()
|
||||
{
|
||||
using var world = Box3DSoaParticleTests.World(bodies: 4, particles: 64, distances: 64, bends: 8, attachments: 16, tethers: 600, gravityY: 0f, fluid: 4);
|
||||
var ps = new Box3DSoaParticleHandle[16];
|
||||
world.CreateParticles(Graph.Line((0f, 1f, 0f), (Spacing, 0f, 0f), 16).Select(q => new Box3DSoaParticleDesc(q, ParticleMass, ParticleRadius)).ToArray(), ps);
|
||||
world.CreateDistances(Enumerable.Range(0, 15).Select(i => new Box3DSoaDistanceDesc(ps[i], ps[i + 1])).ToArray());
|
||||
var fluid = new Box3DSoaParticleHandle[1];
|
||||
world.CreateParticles([new Box3DSoaParticleDesc((0f, 3f, 0f), ParticleMass, ParticleRadius, Box3DSoaPhase.Of(2, fluid: true))], fluid);
|
||||
var live = world.AddCapsule(1, (0f, 0f, 0f), 0.05f, 0.1f, 1000f);
|
||||
var dead = world.AddCapsule(2, (1f, 0f, 0f), 0.05f, 0.1f, 1000f);
|
||||
world.RemoveBody(dead);
|
||||
world.Attach(Box3DSoaAttachKind.Static, ps[0], point: (0f, 1f, 0f), tether: true); // a held tether lane to keep unchanged
|
||||
world.Attach(Box3DSoaAttachKind.Body, ps[15], live, (0f, 0f, 0f));
|
||||
Assert.True(world.View.TetherCount > 0);
|
||||
|
||||
Box3DSoaAttachmentDesc[] Batch(Box3DSoaAttachmentDesc bad)
|
||||
{
|
||||
var b = new Box3DSoaAttachmentDesc[8];
|
||||
for (var k = 0; k < 8; k++) b[k] = new Box3DSoaAttachmentDesc(Box3DSoaAttachKind.Body, ps[1 + k], live, (0.01f * k, 0.05f, 0f), tether: k % 2 == 0);
|
||||
b[5] = bad;
|
||||
return b;
|
||||
}
|
||||
AssertRefusedWhole(world, Batch(new Box3DSoaAttachmentDesc(Box3DSoaAttachKind.Body, fluid[0], live)), "1", "a fluid particle");
|
||||
AssertRefusedWhole(world, Batch(new Box3DSoaAttachmentDesc(Box3DSoaAttachKind.Body, ps[9], dead.Lane)), "1", "a removed body");
|
||||
AssertRefusedWhole(world, Batch(new Box3DSoaAttachmentDesc(Box3DSoaAttachKind.Static, ps[9], point: (0f, 1f, 0f), oneWay: true)), "1", "STATIC with ONE_WAY");
|
||||
AssertRefusedWhole(world, Batch(new Box3DSoaAttachmentDesc(Box3DSoaAttachKind.Static, ps[9], point: (0f, 1f, 0f), limb: true)), "1", "STATIC with LIMB");
|
||||
AssertRefusedWhole(world, Batch(new Box3DSoaAttachmentDesc(Box3DSoaAttachKind.Body, ps[9], live, compliance: -1f)), "1", "a negative compliance");
|
||||
// a batch that fits is taken whole afterwards
|
||||
world.Attach(Batch(new Box3DSoaAttachmentDesc(Box3DSoaAttachKind.Body, ps[9], live, (0f, 0.05f, 0f))));
|
||||
Assert.Equal(10, world.View.AttachmentCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_batch_past_the_free_lanes_or_the_tether_capacity_changes_nothing()
|
||||
{
|
||||
using var world = Box3DSoaParticleTests.World(bodies: 4, particles: 32, distances: 32, bends: 8, attachments: 8, tethers: 5, gravityY: 0f);
|
||||
var ps = new Box3DSoaParticleHandle[10];
|
||||
world.CreateParticles(Graph.Line((0f, 1f, 0f), (Spacing, 0f, 0f), 10).Select(q => new Box3DSoaParticleDesc(q, ParticleMass, ParticleRadius)).ToArray(), ps);
|
||||
world.CreateDistances(Enumerable.Range(0, 9).Select(i => new Box3DSoaDistanceDesc(ps[i], ps[i + 1])).ToArray()); // no tether attachment yet: no tethers
|
||||
var body = world.AddCapsule(1, (0f, 0f, 0f), 0.05f, 0.1f, 1000f);
|
||||
world.Attach(Box3DSoaAttachKind.Body, ps[9], body, (0f, 0f, 0f));
|
||||
var spare = new Box3DSoaConstraintHandle[2]; // two tombstones below the count, which the tether batch below would reuse
|
||||
world.Attach([new Box3DSoaAttachmentDesc(Box3DSoaAttachKind.Body, ps[7], body), new Box3DSoaAttachmentDesc(Box3DSoaAttachKind.Body, ps[8], body)], spare);
|
||||
world.Remove(spare);
|
||||
Box3DSoaParticleTests.Run(world, 1);
|
||||
var free = new Box3DSoaAttachmentDesc[8]; // one lane is taken: 8 do not fit in 7
|
||||
for (var k = 0; k < 8; k++) free[k] = new Box3DSoaAttachmentDesc(Box3DSoaAttachKind.Body, ps[k], body);
|
||||
AssertRefusedWhole(world, free, "3", "more descs than free lanes");
|
||||
// two tether attachments over the 10-rope need more tethers than the capacity of 5: the derivation refuses and every lane of the batch backs out
|
||||
var tether = new[]
|
||||
{
|
||||
new Box3DSoaAttachmentDesc(Box3DSoaAttachKind.Static, ps[0], point: (0f, 1f, 0f), tether: true),
|
||||
new Box3DSoaAttachmentDesc(Box3DSoaAttachKind.Static, ps[8], point: (8 * Spacing, 1f, 0f), tether: true),
|
||||
};
|
||||
var before = Snapshot(world);
|
||||
Assert.Throws<InvalidOperationException>(() => world.Attach(tether));
|
||||
Assert.True(before == Snapshot(world), "a derivation that does not fit left a lane of the batch");
|
||||
Assert.Equal(3, world.View.AttachmentCount);
|
||||
Assert.Equal(0, world.View.TetherCount);
|
||||
Assert.Equal(2u, world.View.AttachmentKind[1]); // the tombstones read as they did
|
||||
Assert.Equal(2u, world.View.AttachmentKind[2]);
|
||||
}
|
||||
|
||||
private static (Box3DSoaWorld World, int Box, Box3DSoaParticleHandle Particle) OneWayScene(bool oneWay)
|
||||
{
|
||||
var world = Box3DSoaParticleTests.World(bodies: 4, particles: 8, distances: 1, bends: 1, attachments: 4, tethers: 1, gravityY: 0f);
|
||||
var box = world.AddBox(1, (0f, 0f, 0f), (0.1f, 0.1f, 0.1f), 125f); // 1 kg
|
||||
var p = new Box3DSoaParticleHandle[1];
|
||||
world.CreateParticles([new Box3DSoaParticleDesc((0.2f, 0f, 0f), 0.5f, ParticleRadius, velocity: (0f, 1f, 0f))], p);
|
||||
world.Attach(Box3DSoaAttachKind.Body, p[0], box, (0.2f, 0f, 0f), oneWay: oneWay);
|
||||
return (world, box, p[0]);
|
||||
}
|
||||
|
||||
/// <summary>The box's top speed and largest impulse lane magnitude, and the particle's worst distance from its point, over 60 ticks.</summary>
|
||||
private static (float Speed, float Impulse, float Deviation) OneWayRun(bool oneWay)
|
||||
{
|
||||
var (world, box, particle) = OneWayScene(oneWay);
|
||||
using (world)
|
||||
{
|
||||
var speed = 0f; var impulse = 0f; var deviation = 0f;
|
||||
for (var i = 0; i < 60; i++)
|
||||
{
|
||||
world.Step(Dt, Sub);
|
||||
var v = world.View;
|
||||
speed = MathF.Max(speed, MathF.Sqrt(Sq(v.VelocityX[box]) + Sq(v.VelocityY[box]) + Sq(v.VelocityZ[box])));
|
||||
impulse = MathF.Max(impulse, MathF.Sqrt(Sq(v.ParticleImpulseX[box]) + Sq(v.ParticleImpulseY[box]) + Sq(v.ParticleImpulseZ[box])));
|
||||
// the point is the box's local (0.2, 0, 0); a one-way box does not move or turn, and the plain run is not judged on this
|
||||
deviation = MathF.Max(deviation, MathF.Sqrt(Sq(v.ParticleX[particle.Lane] - (v.PositionX[box] + 0.2f)) + Sq(v.ParticleY[particle.Lane] - v.PositionY[box]) + Sq(v.ParticleZ[particle.Lane] - v.PositionZ[box])));
|
||||
}
|
||||
return (speed, impulse, deviation);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_one_way_attachment_holds_its_particle_and_leaves_its_body_alone()
|
||||
{
|
||||
var plain = OneWayRun(false);
|
||||
var one = OneWayRun(true);
|
||||
var line = $"ONE_WAY box speed / impulse lane / particle deviation: plain {F(plain.Speed)} / {F(plain.Impulse)} / {F(plain.Deviation)}; one-way {F(one.Speed)} / {F(one.Impulse)} / {F(one.Deviation)}";
|
||||
Console.Error.WriteLine(line);
|
||||
Reading(line);
|
||||
Assert.True(plain.Impulse > 0f && plain.Speed > 0.05f, "without the flag the particle did not move the box: " + line);
|
||||
Assert.Equal(0f, one.Speed);
|
||||
Assert.Equal(0f, one.Impulse);
|
||||
Assert.True(one.Deviation < OneWayDeviation, "the one-way particle left its point: " + line);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_one_way_attachment_is_refused_on_a_world_point()
|
||||
{
|
||||
using var world = Box3DSoaParticleTests.World(gravityY: 0f);
|
||||
var p = new Box3DSoaParticleHandle[1];
|
||||
world.CreateParticles([new Box3DSoaParticleDesc((0f, 1f, 0f), ParticleMass, ParticleRadius)], p);
|
||||
Assert.Throws<InvalidOperationException>(() => world.Attach(Box3DSoaAttachKind.Static, p[0], point: (0f, 1f, 0f), oneWay: true));
|
||||
Assert.Equal(0, world.View.AttachmentCount);
|
||||
Assert.Throws<InvalidOperationException>(() => world.Attach([new Box3DSoaAttachmentDesc { Kind = 0, Flags = 8u, Particle = p[0], Body = 0 }])); // an unknown bit
|
||||
}
|
||||
|
||||
/// <summary>One Fella's flesh in the probe's counts: 661 particles (a 22 x 30 sheet and one more), a distance per sheet link, every particle attached to one of 19 bones.</summary>
|
||||
private static double Spawn(int fellas, bool batch)
|
||||
{
|
||||
const int per = 661, sheet = 660;
|
||||
using var world = Box3DSoaParticleTests.World(bodies: 19 * fellas + 1, particles: per * fellas + 8, distances: 1300 * fellas, bends: 8,
|
||||
attachments: per * fellas, tethers: 8, gravityY: 0f);
|
||||
var bones = new int[19 * fellas];
|
||||
for (var b = 0; b < bones.Length; b++) bones[b] = world.AddCapsule((ulong)(b + 1), (0.12f * (b % 19), 0f, 0.5f * (b / 19)), 0.05f, 0.1f, 100f);
|
||||
var all = new List<Box3DSoaAttachmentDesc>();
|
||||
var watch = System.Diagnostics.Stopwatch.StartNew();
|
||||
for (var f = 0; f < fellas; f++)
|
||||
{
|
||||
var desc = new Box3DSoaParticleDesc[per];
|
||||
for (var k = 0; k < per; k++)
|
||||
desc[k] = new Box3DSoaParticleDesc((0.04f * (k % 22), 0.04f * (k / 22), 0.5f * f), ParticleMass, ParticleRadius, Box3DSoaPhase.Of((uint)(f + 1), noParticleContact: true));
|
||||
var p = new Box3DSoaParticleHandle[per];
|
||||
world.CreateParticles(desc, p);
|
||||
var d = new List<Box3DSoaDistanceDesc>();
|
||||
for (var k = 0; k < sheet; k++)
|
||||
{
|
||||
if (k % 22 != 21 && k + 1 < sheet) d.Add(new(p[k], p[k + 1]));
|
||||
if (k + 22 < sheet) d.Add(new(p[k], p[k + 22]));
|
||||
}
|
||||
world.CreateDistances(d.ToArray());
|
||||
for (var k = 0; k < per; k++)
|
||||
{
|
||||
var bone = bones[19 * f + k % 19];
|
||||
all.Add(new Box3DSoaAttachmentDesc(Box3DSoaAttachKind.Body, p[k], bone, (desc[k].X - 0.12f * (k % 19), desc[k].Y, desc[k].Z - 0.5f * f), compliance: 1e-4f, limb: true, oneWay: k % 2 == 0));
|
||||
}
|
||||
}
|
||||
if (batch) world.Attach(all.ToArray());
|
||||
else foreach (var a in all) AttachOne(world, a);
|
||||
return watch.Elapsed.TotalMilliseconds;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Attach_spawn_cost_line()
|
||||
{
|
||||
// The 256-attachment sheet as 256 single attaches and as one batch, alternately 5 times; then Fella-scale spawns (661 attachments each, one and
|
||||
// four) the same way, 3 times, the whole spawn timed. Printed, no bound: a batch ending in one derivation against one per attachment.
|
||||
static double Median(double[] x) { Array.Sort(x); return x[x.Length / 2]; }
|
||||
var single = new double[5]; var batch = new double[5];
|
||||
for (var r = 0; r < 5; r++)
|
||||
{
|
||||
foreach (var asBatch in r % 2 == 0 ? new[] { false, true } : new[] { true, false })
|
||||
{
|
||||
var (world, descs) = SheetOnCapsules();
|
||||
using (world)
|
||||
{
|
||||
var watch = System.Diagnostics.Stopwatch.StartNew();
|
||||
if (asBatch) world.Attach(descs);
|
||||
else foreach (var d in descs) AttachOne(world, d);
|
||||
(asBatch ? batch : single)[r] = watch.Elapsed.TotalMilliseconds;
|
||||
}
|
||||
}
|
||||
}
|
||||
var line = $"ATTACH_SPAWN 256 attachments: singles median {F(Median(single))} ms, one batch {F(Median(batch))} ms";
|
||||
Console.Error.WriteLine(line);
|
||||
Reading(line);
|
||||
foreach (var fellas in new[] { 1, 4 })
|
||||
{
|
||||
var s = new double[3]; var b = new double[3];
|
||||
for (var r = 0; r < 3; r++)
|
||||
{
|
||||
s[r] = Spawn(fellas, false);
|
||||
b[r] = Spawn(fellas, true);
|
||||
}
|
||||
var fella = $"FELLA_SPAWN {fellas} Fella(s), {661 * fellas} attachments, whole spawn: singles median {F(Median(s))} ms, batched attach median {F(Median(b))} ms";
|
||||
Console.Error.WriteLine(fella);
|
||||
Reading(fella);
|
||||
}
|
||||
Assert.True(Median(batch) > 0);
|
||||
}
|
||||
|
||||
// Bounds by lodur:ruling:deterministic-bound-margin and deterministic-bound-zero-reading (the unit is 1 um; a shipped zero counts as half a unit).
|
||||
// The mutants, each a one-line change to ymir_particles.c or ymir_box3d_soa.c, and what separates them:
|
||||
// Mo end_push books on a ONE_WAY body the box's speed reads 400 m/s: Assert.Equal(0f, speed) and the impulse lane 310
|
||||
// Mw end_w weighs a ONE_WAY body particle deviation below
|
||||
// Mb the batch's back-out skips the lanes A_batch_past_the_free_lanes_or_the_tether_capacity fails (a reused tombstone reads live)
|
||||
// Mv only the first desc validated A_batch_with_one_bad_desc fails; Mq ONE_WAY accepted on STATIC: both refusal tests fail
|
||||
// Ms the removed-body check on the first desc only: A_batch_with_one_bad_desc fails
|
||||
// Mc the cursor not advanced: reads identical (lane k is live once written, so the scan skips it): a cost-only equivalent mutant, unclaimed.
|
||||
// Mr a rederive per desc: cost only (the cost line), unclaimed.
|
||||
// OneWayDeviation: shipped worst 0 (-> 5e-7), least claimed mutant Mw 2.17828e-3, separation 4357; bound sqrt(5e-7 x 2.17828e-3).
|
||||
private const float OneWayDeviation = 3.3e-5f;
|
||||
}
|
||||
|
|
@ -10,7 +10,7 @@ using Pos = (float X, float Y, float Z);
|
|||
/// momentum and energy are kept through a throw and a snap, and a release takes effect in its step. The ropes are built
|
||||
/// by a test-local builder. Every reading the tests judge is also appended to the file named by YMIR_READINGS when set.
|
||||
/// </summary>
|
||||
public sealed class Box3DSoaCouplingTests
|
||||
public sealed partial class Box3DSoaCouplingTests
|
||||
{
|
||||
private const float Dt = Box3DSoaParticleTests.Dt;
|
||||
private const int Sub = Box3DSoaParticleTests.Sub;
|
||||
|
|
|
|||
|
|
@ -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(16u, Box3DSoaNative.ymir_box3d_soa_get_abi_version());
|
||||
Box3DSoaWorld.RequireNativeAbi(16);
|
||||
Assert.Throws<InvalidOperationException>(() => Box3DSoaWorld.RequireNativeAbi(15)); // an older native under this managed build
|
||||
Assert.Throws<InvalidOperationException>(() => Box3DSoaWorld.RequireNativeAbi(17)); // a newer one
|
||||
Assert.Equal(17u, Box3DSoaNative.ymir_box3d_soa_get_abi_version());
|
||||
Box3DSoaWorld.RequireNativeAbi(17);
|
||||
Assert.Throws<InvalidOperationException>(() => Box3DSoaWorld.RequireNativeAbi(16)); // an older native under this managed build
|
||||
Assert.Throws<InvalidOperationException>(() => Box3DSoaWorld.RequireNativeAbi(18)); // a newer one
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
|
|||
|
|
@ -1065,8 +1065,8 @@ public sealed class Box3DSoaParticleTests
|
|||
// grid_mask: the walk misses most pairs) 0.045: bound sqrt(0.759 x 0.045) = 0.185, separation 16.9. With the bit: shipped 0.126, least claimed mutant
|
||||
// Mn (the bit ignored: grid_sort, build_grid and walk_half all treat the lane as solid) 0.759: bound sqrt(0.126 x 0.759) = 0.309, separation 6.0.
|
||||
// Mw (walk_half's skip alone removed) reads 0.126 where the lane's stale cell lands on nothing, and 0.063 with its cells written (it then walks only the
|
||||
// forward half of its pairs): it does not separate, so it is unclaimed here; the skip is what keeps a lane's stale cell out of the walk, and the
|
||||
// source test pins it. A resting rope read 0.759, not 0.95: soft contact under the rope's weight, not this cut.
|
||||
// forward half of its pairs): it does not separate here, so it is unclaimed in this test; the skip is what keeps a lane's stale cell out of the walk, and
|
||||
// Flesh_lane_with_a_stale_cell_pushes_nothing claims it. A resting rope read 0.759, not 0.95: soft contact under the rope's weight, not this cut.
|
||||
var contact = RopesClosest(false);
|
||||
var through = RopesClosest(true);
|
||||
var line = $"FLESH_PASS closest cross pair in ri + rj: without the bit {contact:F3}, with it on the dropped rope {through:F3}";
|
||||
|
|
@ -1076,6 +1076,33 @@ public sealed class Box3DSoaParticleTests
|
|||
Assert.True(through < 0.309f, "a rope with the bit did not pass through: " + line);
|
||||
}
|
||||
|
||||
/// <summary>Two particles of different groups 0.010 m apart (0.4 of ri + rj) in cell (0, 0, 0), no gravity, one carrying the bit. The pair's separation after 10 ticks, in ri + rj.</summary>
|
||||
private static float StaleCellSeparation(bool fleshFirst)
|
||||
{
|
||||
using var world = World(gravityY: 0f);
|
||||
var flesh = Box3DSoaPhase.Of(1, noParticleContact: true);
|
||||
var plain = Box3DSoaPhase.Of(2);
|
||||
var p = new Box3DSoaParticleHandle[2];
|
||||
world.CreateParticles([new((0.001f, 0.001f, 0.001f), ParticleMass, ParticleRadius, fleshFirst ? flesh : plain), new((0.011f, 0.001f, 0.001f), ParticleMass, ParticleRadius, fleshFirst ? plain : flesh)], p);
|
||||
Run(world, 10);
|
||||
return Graph.Dist(world.View, p[0].Lane, p[1].Lane) / (2f * ParticleRadius);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_flesh_lane_is_not_walked_by_the_particle_contact_pass()
|
||||
{
|
||||
// A lane with the bit is in no grid cell, so its cell lanes keep their creation value (0, 0, 0). If the walk visited it, it would find the other
|
||||
// particle in that cell and push it. Readings: shipped (the walk skips it) 0.400 (the start, untouched); walk_half's skip removed (the lane walks
|
||||
// its stale cell) 24.400: bound sqrt(0.400 x 24.4) = 3.12, separation 61. Lane order matters: the walk pairs a lane with higher lanes of its cell,
|
||||
// so the flesh lane is the lower one here.
|
||||
var line = $"FLESH_WALK separation after 10 ticks in ri + rj: flesh lane first {StaleCellSeparation(true):F3}";
|
||||
Console.Error.WriteLine(line);
|
||||
Readings.Add(line);
|
||||
Assert.True(StaleCellSeparation(true) < StaleCellBound, "the flesh lane pushed a particle of another group: " + line);
|
||||
}
|
||||
|
||||
private const float StaleCellBound = 3.12f;
|
||||
|
||||
/// <summary>A 12 x 12 sheet of touching particles (pitch 2 r) carrying the flag, held at its four corners by static pins, at y 0.5.</summary>
|
||||
private static (Box3DSoaWorld World, Made Sheet) FleshSheet(int fluid)
|
||||
{
|
||||
|
|
@ -1311,7 +1338,6 @@ public sealed class Box3DSoaParticleSourceTests
|
|||
var c = StripComments(File.ReadAllText(Path.Combine(Native(), "ymir_particles.c")));
|
||||
Assert.Equal(0, Count(Body(c, "static void grid_sort("), @"table_mask"));
|
||||
Assert.Equal(0, Count(Body(c, "YMIR_INLINE void walk_half("), @"table_mask"));
|
||||
Assert.Contains("YMIR_PHASE_NO_PARTICLE_CONTACT", Body(c, "YMIR_INLINE void walk_half(")); // the walk skips exactly the lanes grid_sort left out
|
||||
Assert.Equal(1, Count(c, @"grid_mask\s*="));
|
||||
Assert.Contains("grid_mask =", Body(c, "static void build_grid("));
|
||||
Assert.Contains("grid_sort(p, inv_cell, 0, p->table_mask)", Body(c, "static float fluid_grid("));
|
||||
|
|
|
|||
|
|
@ -193,7 +193,7 @@ public sealed class Box3DSoaRemoveTests
|
|||
}
|
||||
|
||||
/// <summary>The retired shape table holds one step's removals (capacity: the body count); a removal every step for many steps
|
||||
/// overruns it unless each step clears it, and every removed toucher's End still names it.</summary>
|
||||
/// overruns it unless each step clears it, and every removed toucher's End still names it. In a Debug build the write asserts the table has room, so the never-cleared mutant (the clear deleted) aborts the run there: a Debug-only observable, as the Release build has no bounds check.</summary>
|
||||
[Fact]
|
||||
public void The_retired_table_holds_one_steps_removals()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue