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 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-24 21:42:47 +01:00
commit ca237bb0af
3 changed files with 247 additions and 54 deletions

View file

@ -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))

View file

@ -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);