From ca237bb0af74e994e2e1f0dd2b5717f17ade3524 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 24 Nov 2026 21:42:47 +0100 Subject: [PATCH] particles: tether to the nearest point of each particle's two nearest islands (Kim 2012), no static-static pair tethers Replaces the multi-source one-tether-per-particle derivation. A tether island is a maximal set of tether particles joined by live distances; each non-tether particle is held to the nearest point of its two nearest islands by rest path, written per island in lane order behind a parent gate (float ties at rank 2). A pair tether is derived only when a body is among its two attachments (end_w is 0 at a static end). Tests: two-island derivation (rope, star, 9-rope) and the two-corner-group sheet (read at 8 s: the sheet rings at 5 s). Pile hashes re-pinned: the pile's tether set changes with the rule; two runs agree. Co-Authored-By: Claude Opus 5.5 --- native/Ymir.Box3D/ymir_particles.c | 174 ++++++++++++++---- native/Ymir.Box3D/ymir_particles.h | 14 +- .../Box3DSoaParticleTests.cs | 115 ++++++++++-- 3 files changed, 248 insertions(+), 55 deletions(-) diff --git a/native/Ymir.Box3D/ymir_particles.c b/native/Ymir.Box3D/ymir_particles.c index a30013c..e95a181 100644 --- a/native/Ymir.Box3D/ymir_particles.c +++ b/native/Ymir.Box3D/ymir_particles.c @@ -62,6 +62,10 @@ static void put(float* x, float* y, float* z, int i, v3 v) } +// A particle is held to the nearest point of each of its two nearest tether islands (Kim, Chentanez, Mueller 2012 sec. 3.4); +// Kim's typical four were measured on the 20-pin 32x32 sheet: 1.66% worst edge against 1.72%, for twice the lanes. +#define YMIR_TETHER_ISLANDS 2 + // ---- memory: one block, laid out once at create ------------------------------------------------------------- static size_t layout(ymir_particles* p, unsigned char* base) @@ -112,6 +116,7 @@ static size_t layout(ymir_particles* p, unsigned char* base) CARVE(heap_key, float, heap); CARVE(heap_node, int32_t, heap); CARVE(heap_edge, int32_t, heap); CARVE(path, float, np); CARVE(via, int32_t, np); CARVE(owner, int32_t, np); CARVE(tether_of, int32_t, np); CARVE(queue, int32_t, np); CARVE(comp_size, int32_t, np); CARVE(done, uint8_t, np); + CARVE(isle, int32_t, np); CARVE(near_isle, int32_t, YMIR_TETHER_ISLANDS * np); CARVE(near_path, float, YMIR_TETHER_ISLANDS * np); #undef CARVE return off + 16; } @@ -342,20 +347,23 @@ static int is_tether(const ymir_particles* p, int32_t a) return p->att_kind[a] != YMIR_ATTACH_REMOVED && (p->att_flags[a] & YMIR_ATTACH_FLAG_TETHER); } -// 1 when attachments a < b are both tethers on different particles of one component (`label` names components), -// so the rest path between their particles bounds the distance between their points. +// 1 when attachments a < b are both tethers on different particles of one component (`label` names components) and at +// least one is on a body, so the rest path between their particles bounds the distance between their points. Two world +// points never move: their pair has no end to pull (end_w is 0 at both). static int pairs_with(const ymir_particles* p, int32_t a, int32_t b, const int32_t* label) { int32_t pa = p->att_particle[a], pb = p->att_particle[b]; - return is_tether(p, b) && pa != pb && label[pa] == label[pb]; + return is_tether(p, b) && pa != pb && label[pa] == label[pb] && + (p->att_kind[a] == YMIR_ATTACH_BODY || p->att_kind[b] == YMIR_ATTACH_BODY); } -// Dijkstra (binary heap, ties by node then edge) over the live distances' rest lengths, from the particle of every -// TETHER attachment (`only` < 0) or from one attachment's particle (`only` >= 0). Writes for each settled particle -// its rest path `path`, the edge it was reached by `via` (-1 at a source), the attachment it is nearest `owner` -// (the lowest attachment on a particle that holds several), and its place in settle order `queue`: a parent settles -// before its children. Returns the settled count. Writes no lane. -static int32_t settle(ymir_particles* p, int32_t only) +// Dijkstra (binary heap, ties by node then edge) over the live distances' rest lengths, from one attachment's particle +// (`only` >= 0) or from every particle of the tether island named `island` (>= 0; `isle` labels them). Writes for each +// settled particle its rest path `path`, the edge it was reached by `via` (-1 at a source), the attachment it is nearest +// `owner` (the lowest TETHER attachment on a source particle), and its place in settle order `queue`: a parent settles +// before its children. In island mode a tether particle of another island is settled but not expanded: the path through +// it is implied by its own tethers and the pair tethers. Returns the settled count. Writes no lane. +static int32_t settle(ymir_particles* p, int32_t only, int32_t island) { int32_t size = 0, n = 0; for (int32_t i = 0; i < p->particle_count; ++i) @@ -368,7 +376,7 @@ static int32_t settle(ymir_particles* p, int32_t only) for (int32_t a = 0; a < p->attachment_count; ++a) { int32_t s = p->att_particle[a]; - if (only >= 0 ? a != only : !is_tether(p, a)) + if (only >= 0 ? a != only : !is_tether(p, a) || p->isle[s] != island) { continue; } @@ -390,6 +398,10 @@ static int32_t settle(ymir_particles* p, int32_t only) } p->done[u] = 1; p->queue[n++] = u; + if (only < 0 && p->isle[u] >= 0 && p->via[u] >= 0) + { + continue; // another island's tether particle: settled, not expanded + } for (int32_t k = p->adj_start[u]; k < p->adj_start[u + 1]; ++k) { int32_t e = p->adj_edge[k], v = p->dist_a[e] == u ? p->dist_b[e] : p->dist_a[e]; @@ -488,13 +500,91 @@ static int32_t rederive_topology(ymir_particles* p) } p->comp_size[i] = tail; } - // One tether per particle that is not a source, to the attachment it is nearest (Kim 2012), plus for each pair of - // tether attachments one pair tether behind a route of booking lanes along their rest path. Counted before written. - int64_t need = 0; - int32_t n = settle(p, -1); - for (int32_t k = 0; k < n; ++k) + // Islands (Kim 2012 sec. 3.4): a particle holding a live TETHER attachment is a tether particle; an island is a maximal + // set of them joined by live distances, named by its lowest TETHER attachment. isle labels tether particles, -1 elsewhere + // (-2 while a tether particle awaits its island). + for (int32_t i = 0; i < np; ++i) { - need += p->via[p->queue[k]] >= 0; + p->isle[i] = -1; + } + for (int32_t a = 0; a < p->attachment_count; ++a) + { + if (is_tether(p, a)) + { + p->isle[p->att_particle[a]] = -2; + } + } + for (int32_t a = 0; a < p->attachment_count; ++a) + { + int32_t s = p->att_particle[a]; + if (!is_tether(p, a) || p->isle[s] != -2) + { + continue; + } + int32_t head = 0, tail = 0; + p->isle[s] = a; + p->queue[tail++] = s; + while (head < tail) + { + int32_t u = p->queue[head++]; + for (int32_t k = p->adj_start[u]; k < p->adj_start[u + 1]; ++k) + { + int32_t e = p->adj_edge[k], v = p->dist_a[e] == u ? p->dist_b[e] : p->dist_a[e]; + if (p->isle[v] == -2) + { + p->isle[v] = a; + p->queue[tail++] = v; + } + } + } + } + // Each particle that is not a tether particle is held to the nearest point of each of its YMIR_TETHER_ISLANDS nearest + // islands: pass 1 keeps, per particle, the nearest islands by rest path (ties to the lower island); pass 2 writes them. + // Plus for each pair of tether attachments with a body among them one pair tether behind a route of booking lanes along + // their rest path. Counted before written. + int64_t need = 0; + for (int32_t i = 0; i < YMIR_TETHER_ISLANDS * np; ++i) + { + p->near_isle[i] = -1; + } + for (int32_t a = 0; a < p->attachment_count; ++a) + { + if (!is_tether(p, a) || p->isle[p->att_particle[a]] != a) + { + continue; + } + int32_t n = settle(p, -1, a); + for (int32_t k = 0; k < n; ++k) + { + int32_t u = p->queue[k]; + if (p->via[u] < 0 || p->isle[u] >= 0) + { + continue; + } + int32_t* isl = p->near_isle + YMIR_TETHER_ISLANDS * u; + float* dist = p->near_path + YMIR_TETHER_ISLANDS * u; + float d = p->path[u]; + int32_t m = 0; + while (m < YMIR_TETHER_ISLANDS && isl[m] >= 0 && !(d < dist[m])) // an equal path keeps the earlier island + { + ++m; + } + if (m == YMIR_TETHER_ISLANDS) + { + continue; + } + for (int32_t j = YMIR_TETHER_ISLANDS - 1; j > m; --j) + { + isl[j] = isl[j - 1]; + dist[j] = dist[j - 1]; + } + isl[m] = a; + dist[m] = d; + } + } + for (int32_t i = 0; i < YMIR_TETHER_ISLANDS * np; ++i) + { + need += p->near_isle[i] >= 0; } for (int32_t a = 0; a < p->attachment_count; ++a) { @@ -502,7 +592,7 @@ static int32_t rederive_topology(ymir_particles* p) { continue; } - settle(p, a); + settle(p, a, -1); for (int32_t b = a + 1; b < p->attachment_count; ++b) { if (pairs_with(p, a, b, p->tether_of)) @@ -520,26 +610,42 @@ static int32_t rederive_topology(ymir_particles* p) p->component[i] = p->tether_of[i] >= 0 ? p->tether_of[i] : 0; } int32_t nt = 0; - n = settle(p, -1); - for (int32_t i = 0; i < np; ++i) + for (int32_t a = 0; a < p->attachment_count; ++a) { - p->tether_of[i] = -1; - } - for (int32_t k = 0; k < n; ++k) - { - int32_t u = p->queue[k], e = p->via[u]; - if (e < 0) + if (!is_tether(p, a) || p->isle[p->att_particle[a]] != a) { continue; } - int32_t t = nt++, other = p->dist_a[e] == u ? p->dist_b[e] : p->dist_a[e]; - p->tether_att[t] = p->owner[u]; - p->tether_particle[t] = u; - p->tether_bound[t] = p->path[u]; - p->tether_edge[t] = e; - p->tether_other[t] = -1; - p->tether_parent[t] = p->tether_of[other]; - p->tether_of[u] = t; + int32_t n = settle(p, -1, a); + for (int32_t i = 0; i < np; ++i) + { + p->tether_of[i] = -1; + } + for (int32_t k = 0; k < n; ++k) + { + int32_t u = p->queue[k], e = p->via[u], held = 0; + if (e < 0 || p->isle[u] >= 0) + { + continue; + } + for (int32_t m = 0; m < YMIR_TETHER_ISLANDS; ++m) + { + held |= p->near_isle[YMIR_TETHER_ISLANDS * u + m] == a; + } + int32_t other = p->dist_a[e] == u ? p->dist_b[e] : p->dist_a[e]; + if (!held || (p->via[other] >= 0 && p->tether_of[other] < 0)) + { + continue; // not one of u's nearest islands; or its parent is not held to this island (a float tie at rank 2) + } + int32_t t = nt++; + p->tether_att[t] = p->owner[u]; + p->tether_particle[t] = u; + p->tether_bound[t] = p->path[u]; + p->tether_edge[t] = e; + p->tether_other[t] = -1; + p->tether_parent[t] = p->tether_of[other]; + p->tether_of[u] = t; + } } for (int32_t a = 0; a < p->attachment_count; ++a) { @@ -547,7 +653,7 @@ static int32_t rederive_topology(ymir_particles* p) { continue; } - settle(p, a); + settle(p, a, -1); for (int32_t b = a + 1; b < p->attachment_count; ++b) { if (!pairs_with(p, a, b, p->component)) diff --git a/native/Ymir.Box3D/ymir_particles.h b/native/Ymir.Box3D/ymir_particles.h index e925370..ce6e7ac 100644 --- a/native/Ymir.Box3D/ymir_particles.h +++ b/native/Ymir.Box3D/ymir_particles.h @@ -59,7 +59,7 @@ enum enum { - YMIR_ATTACH_FLAG_TETHER = 1u // the attachment bounds every particle its component reaches by the rest path + YMIR_ATTACH_FLAG_TETHER = 1u // bounds each particle by the rest path to the nearest point of its two nearest islands of tether attachments }; enum @@ -187,8 +187,10 @@ typedef struct ymir_particles float *att_force_x, *att_force_y, *att_force_z; // N, the force the attachment exerts on its particles (mean): a sensor uint64_t* att_removed_at; - // tether lanes: derived: one per particle that is not a source, to the attachment it is nearest, in settle order, then per - // attachment pair a route of booking lanes (attachment -1, edge only) along their rest path and the pair tether; parents come first; + // tether lanes: derived: grouped by island in lane order (an island: tether particles joined by live distances, named by its + // lowest tether attachment), settle order within; per island one for each particle that is not a tether particle and holds the island + // among its two nearest, to the attachment point it is nearest; then per attachment pair with a body among them a route of + // booking lanes (attachment -1, edge only) along their rest path and the pair tether; parents come first; // other >= 0 makes the tether a bound between two attachment points (edge -1) instead of a particle and a point int32_t *tether_att, *tether_particle, *tether_parent, *tether_edge, *tether_other; float* tether_bound; @@ -200,8 +202,8 @@ typedef struct ymir_particles // solver scratch, allocated once at create float *pp_x, *pp_y, *pp_z, *dp_x, *dp_y, *dp_z, *dist_acc, *tacc_x, *tacc_y, *tacc_z; int32_t *lane_list, *cell_x, *cell_y, *cell_z, *cell_start, *cell_fill, *sorted, *contacts; - int32_t *adj_start, *adj_edge, *heap_node, *heap_edge, *via, *owner, *tether_of, *queue, *comp_size; - float *heap_key, *path; + int32_t *adj_start, *adj_edge, *heap_node, *heap_edge, *via, *owner, *tether_of, *queue, *comp_size, *isle, *near_isle; + float *heap_key, *path, *near_path; uint8_t* done; uint32_t table_mask; void* arena; @@ -213,7 +215,7 @@ void ymir_particles_free(ymir_particles* p); // Commands. `step_count` is the world's completed steps: a removed lane is reused once a step has passed, lowest // lane first. Every refusal happens before anything changes; a command that would need more tethers than the -// capacity is refused whole. Every topology command ends in the one rederive_topology. +// capacity is refused whole (tether capacity: up to two per particle reached, plus the pair routes). Every topology command ends in the one rederive_topology. int32_t ymir_particles_create_particles(ymir_particles* p, int32_t n, const ymir_particle_desc* d, uint64_t step_count, ymir_handle* out); 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); diff --git a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaParticleTests.cs b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaParticleTests.cs index 7295934..0919c89 100644 --- a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaParticleTests.cs +++ b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaParticleTests.cs @@ -541,22 +541,107 @@ public sealed class Box3DSoaParticleTests } [Fact] - public void A_particle_between_two_tether_points_takes_the_nearer_and_a_tie_the_lower_lane() + public void Each_particle_takes_the_nearest_point_of_its_two_nearest_islands() { - // A 5-particle rope held at both ends: particle 1 is nearest the first point, particle 3 the second, and particle 2 is two - // edges from each. A tie goes to the path through the lower particle lane, so to the first attachment. - using var world = World(); - var rope = Graph.Rope(world, Line((0f, 0f, 0f), (0.03f, 0f, 0f), 5), ParticleMass, ParticleRadius, bend: -1f); - var first = world.Attach(Box3DSoaAttachKind.Static, rope.Particles[0], point: (0f, 0f, 0f), tether: true); - var second = world.Attach(Box3DSoaAttachKind.Static, rope.Particles[4], point: (0.12f, 0f, 0f), tether: true); - var view = world.View; - var owner = new Dictionary(); + // (a) A 5-rope held at both ends by static tether points: each middle particle has one tether per end, bounded by the + // rest path to it, and the two world points get no pair tether between them (neither can move). + using (var world = World()) + { + var rope = Graph.Rope(world, Line((0f, 0f, 0f), (0.03f, 0f, 0f), 5), ParticleMass, ParticleRadius, bend: -1f); + var first = world.Attach(Box3DSoaAttachKind.Static, rope.Particles[0], point: (0f, 0f, 0f), tether: true); + var second = world.Attach(Box3DSoaAttachKind.Static, rope.Particles[4], point: (0.12f, 0f, 0f), tether: true); + var view = world.View; + Assert.Equal(6, view.TetherCount); + Assert.DoesNotContain(view.TetherOther.ToArray(), o => o >= 0); + for (var k = 1; k <= 3; k++) + { + Assert.Equal(0.03f * k, Bound(view, first.Lane, rope.Particles[k].Lane), 1e-5f); + Assert.Equal(0.03f * (4 - k), Bound(view, second.Lane, rope.Particles[k].Lane), 1e-5f); + } + } + // (b) A star: the centre is joined by arms of one, two and three edges to three static tether points, and is held to + // the one- and two-edge points only: the third island is not among its two nearest. + using (var world = World()) + { + var star = new List { new((0f, 0f, 0f), ParticleMass, ParticleRadius, 0) }; + Pos[] dir = [(1f, 0f, 0f), (-0.5f, 0.866f, 0f), (-0.5f, -0.866f, 0f)]; + var ends = new int[3]; + var arm = new List<(int From, int To)>(); + for (var a = 0; a < 3; a++) + { + var from = 0; + for (var s2 = 1; s2 <= a + 1; s2++) + { + star.Add(new Box3DSoaParticleDesc((dir[a].X * 0.03f * s2, dir[a].Y * 0.03f * s2, 0f), ParticleMass, ParticleRadius, 0)); + arm.Add((from, star.Count - 1)); + from = star.Count - 1; + } + ends[a] = from; + } + var particles = new Box3DSoaParticleHandle[star.Count]; + world.CreateParticles(star.ToArray(), particles); + var edges = new Box3DSoaConstraintHandle[arm.Count]; + world.CreateDistances(arm.Select(x => new Box3DSoaDistanceDesc(particles[x.From], particles[x.To], 0f)).ToArray(), edges); + var points = new Box3DSoaConstraintHandle[3]; + for (var a = 0; a < 3; a++) + points[a] = world.Attach(Box3DSoaAttachKind.Static, particles[ends[a]], point: (dir[a].X * 0.03f * (a + 1), dir[a].Y * 0.03f * (a + 1), 0f), tether: true); + var view = world.View; + Assert.Equal(0.03f, Bound(view, points[0].Lane, particles[0].Lane), 1e-5f); + Assert.Equal(0.06f, Bound(view, points[1].Lane, particles[0].Lane), 1e-5f); + Assert.Equal(-1f, Bound(view, points[2].Lane, particles[0].Lane, absent: -1f)); + } + // (c) A 9-rope with tether points on particles 0 and 1 (one island: they touch) and on 8: particle 4 is held to the + // nearer of the island's two points, the one on particle 1, and to the point on 8, never to the one on 0. + using (var world = World()) + { + var line = Line((0f, 0f, 0f), (0.03f, 0f, 0f), 9); + var rope = Graph.Rope(world, line, ParticleMass, ParticleRadius, bend: -1f); + var p0 = world.Attach(Box3DSoaAttachKind.Static, rope.Particles[0], point: line[0], tether: true); + var p1 = world.Attach(Box3DSoaAttachKind.Static, rope.Particles[1], point: line[1], tether: true); + var p8 = world.Attach(Box3DSoaAttachKind.Static, rope.Particles[8], point: line[8], tether: true); + var view = world.View; + var q = rope.Particles[4].Lane; + Assert.Equal(0.09f, Bound(view, p1.Lane, q), 1e-5f); + Assert.Equal(0.12f, Bound(view, p8.Lane, q), 1e-5f); + Assert.Equal(-1f, Bound(view, p0.Lane, q, absent: -1f)); + Assert.Equal(12, view.TetherCount); // particles 2..7, two each + } + } + + // The bound of the one tether that holds `particle` to attachment `att` (a particle tether, not a pair route). + private static float Bound(Box3DSoaView view, int att, int particle, float absent = float.NaN) + { + var found = new List(); for (var t = 0; t < view.TetherCount; t++) - if (view.TetherAttachment[t] >= 0 && view.TetherOther[t] < 0) owner[view.TetherParticle[t]] = view.TetherAttachment[t]; - Assert.Equal(3, owner.Count); // the three particles that are not themselves a tether point: one tether each, not two - Assert.Equal(first.Lane, owner[rope.Particles[1].Lane]); - Assert.Equal(first.Lane, owner[rope.Particles[2].Lane]); - Assert.Equal(second.Lane, owner[rope.Particles[3].Lane]); + if (view.TetherAttachment[t] == att && view.TetherParticle[t] == particle && view.TetherOther[t] < 0) found.Add(view.TetherBound[t]); + if (found.Count == 0 && !float.IsNaN(absent)) return absent; + Assert.Single(found); + return found[0]; + } + + [Fact] + public void Sheet_hung_by_two_corner_groups() + { + // 16 x 16 hung by pins on top-row particles 0, 1, 14 and 15: two corners of two adjacent pins each. Ranking pins instead + // of islands spends both of a particle's tethers on one corner's two pins and the sheet stretches by 11.6% at 5 s. + // Read at 8 s: the sheet still rings at 5 s (Fy 50.636 N of 51.200, 1.10% under) and carries its weight within 0.2% from + // 6 s (51.286, 51.227, 51.132 N at 6, 7, 8 s); the two-corner single-pin sheet settles by about 9 s the same way. + using var world = World(particles: 256, distances: 512, bends: 512, attachments: 8, tethers: 1024); + const float sp = 0.04f; + var sheet = Graph.Sheet(world, (0f, 2f, 0f), (sp, 0f, 0f), (0f, -sp, 0f), 16, 16, ParticleMass, 0.02f, bend: 1e-4f); + foreach (var i in new[] { 0, 15, 1, 14 }) + world.Attach(Box3DSoaAttachKind.Static, sheet.Particles[i], point: (i * sp, 2f, 0f), tether: true); + var view = world.View; + Run(world, 8 * 120); + double fy = 0; + for (var a = 0; a < 4; a++) fy += view.AttachmentForceY[a]; + var weight = 256 * ParticleMass * 10.0; + Readings.Add($"CORNER_GROUPS attach Fy sum {fy:F3} N, weight {weight:F3} N"); + Assert.True(Math.Abs(fy - weight) < 0.01 * weight, $"the pins carry {fy} N of {weight} N"); + var maxStretch = 0f; + foreach (var e in sheet.Edges) maxStretch = MathF.Max(maxStretch, MathF.Abs(Graph.Dist(view, view.DistanceA[e.Lane], view.DistanceB[e.Lane]) / view.DistanceRest[e.Lane] - 1f)); + Readings.Add($"CORNER_GROUPS max edge stretch {maxStretch:P3}"); + Assert.True(maxStretch < 0.05f, $"an edge stretched by {maxStretch}"); } [Fact] @@ -805,7 +890,7 @@ public sealed class Box3DSoaParticleTests // Recorded on this commit's build (gcc 13.2, -ffp-contract=off): the pile of four ropes and a sheet on two bodies. Re-pinned when each particle took // one tether to its nearest tether point (the hash covers the tether lanes) and pair contact summed in a new order; behaviour is held by the tests around it. - private static readonly ulong[] PileHashes = [15006925195759244470ul, 16721272710792469888ul, 16453595234508470899ul, 14765572690055433670ul, 9516360654899358608ul, 11390100683950314529ul]; + private static readonly ulong[] PileHashes = [12993313564863515222ul, 2307790533006845688ul, 13436656559607346955ul, 6468437088969606470ul, 10562521318003626344ul, 7711170181992570593ul]; [Fact] public void Particle_world_is_byte_deterministic()