@flare-engine/simulation 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -21
- package/NOTICE +2 -0
- package/README.md +2 -2
- package/dist/index.d.mts +137 -132
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +137 -132
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +602 -637
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +595 -604
- package/dist/index.mjs.map +1 -1
- package/package.json +16 -8
package/dist/index.mjs
CHANGED
|
@@ -1,614 +1,605 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const axOld = new Float32Array(cap);
|
|
13
|
-
const ayOld = new Float32Array(cap);
|
|
14
|
-
let count = 0;
|
|
15
|
-
let primed = false;
|
|
16
|
-
const computeAccel = (g, soft, gr, rs) => {
|
|
17
|
-
const soft2 = soft * soft;
|
|
18
|
-
for (let i = 0; i < count; i++) {
|
|
19
|
-
ax[i] = 0;
|
|
20
|
-
ay[i] = 0;
|
|
21
|
-
}
|
|
22
|
-
for (let i = 0; i < count; i++) {
|
|
23
|
-
const xi = px[i] ?? 0;
|
|
24
|
-
const yi = py[i] ?? 0;
|
|
25
|
-
const mi = mass[i] ?? 0;
|
|
26
|
-
for (let j = i + 1; j < count; j++) {
|
|
27
|
-
const dx = (px[j] ?? 0) - xi;
|
|
28
|
-
const dy = (py[j] ?? 0) - yi;
|
|
29
|
-
const d2 = dx * dx + dy * dy + soft2;
|
|
30
|
-
const invD = 1 / Math.sqrt(d2);
|
|
31
|
-
const invD3 = g * invD * invD * invD;
|
|
32
|
-
const mj = mass[j] ?? 0;
|
|
33
|
-
ax[i] = (ax[i] ?? 0) + dx * invD3 * mj;
|
|
34
|
-
ay[i] = (ay[i] ?? 0) + dy * invD3 * mj;
|
|
35
|
-
ax[j] = (ax[j] ?? 0) - dx * invD3 * mi;
|
|
36
|
-
ay[j] = (ay[j] ?? 0) - dy * invD3 * mi;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
if (gr > 0 && count > 1) {
|
|
40
|
-
const cxp = px[0] ?? 0;
|
|
41
|
-
const cyp = py[0] ?? 0;
|
|
42
|
-
const beta = gr * g * (mass[0] ?? 0) * rs * rs;
|
|
43
|
-
for (let i = 1; i < count; i++) {
|
|
44
|
-
const dx = (px[i] ?? 0) - cxp;
|
|
45
|
-
const dy = (py[i] ?? 0) - cyp;
|
|
46
|
-
const r2 = dx * dx + dy * dy + soft2;
|
|
47
|
-
const invr2 = 1 / r2;
|
|
48
|
-
const k = beta * invr2 * invr2 * Math.sqrt(invr2);
|
|
49
|
-
ax[i] = (ax[i] ?? 0) - dx * k;
|
|
50
|
-
ay[i] = (ay[i] ?? 0) - dy * k;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
return {
|
|
55
|
-
capacity: cap,
|
|
56
|
-
get count() {
|
|
57
|
-
return count;
|
|
58
|
-
},
|
|
59
|
-
px,
|
|
60
|
-
py,
|
|
61
|
-
vx,
|
|
62
|
-
vy,
|
|
63
|
-
mass,
|
|
64
|
-
tag,
|
|
65
|
-
maxMass() {
|
|
66
|
-
let m = 0;
|
|
67
|
-
for (let i = 0; i < count; i++) {
|
|
68
|
-
const v = mass[i] ?? 0;
|
|
69
|
-
if (v > m) m = v;
|
|
70
|
-
}
|
|
71
|
-
return m;
|
|
72
|
-
},
|
|
73
|
-
add(x, y, vx0, vy0, m, t = 0) {
|
|
74
|
-
if (count >= cap) return -1;
|
|
75
|
-
const i = count++;
|
|
76
|
-
px[i] = x;
|
|
77
|
-
py[i] = y;
|
|
78
|
-
vx[i] = vx0;
|
|
79
|
-
vy[i] = vy0;
|
|
80
|
-
mass[i] = m;
|
|
81
|
-
tag[i] = t;
|
|
82
|
-
primed = false;
|
|
83
|
-
return i;
|
|
84
|
-
},
|
|
85
|
-
step(dt, g, soft, gr = 0, rs = 0) {
|
|
86
|
-
if (count === 0 || dt <= 0) return;
|
|
87
|
-
if (!primed) {
|
|
88
|
-
computeAccel(g, soft, gr, rs);
|
|
89
|
-
primed = true;
|
|
90
|
-
}
|
|
91
|
-
for (let i = 0; i < count; i++) {
|
|
92
|
-
const axi = ax[i] ?? 0;
|
|
93
|
-
const ayi = ay[i] ?? 0;
|
|
94
|
-
px[i] = (px[i] ?? 0) + (vx[i] ?? 0) * dt + 0.5 * axi * dt * dt;
|
|
95
|
-
py[i] = (py[i] ?? 0) + (vy[i] ?? 0) * dt + 0.5 * ayi * dt * dt;
|
|
96
|
-
axOld[i] = axi;
|
|
97
|
-
ayOld[i] = ayi;
|
|
98
|
-
}
|
|
99
|
-
computeAccel(g, soft, gr, rs);
|
|
100
|
-
for (let i = 0; i < count; i++) {
|
|
101
|
-
vx[i] = (vx[i] ?? 0) + 0.5 * ((axOld[i] ?? 0) + (ax[i] ?? 0)) * dt;
|
|
102
|
-
vy[i] = (vy[i] ?? 0) + 0.5 * ((ayOld[i] ?? 0) + (ay[i] ?? 0)) * dt;
|
|
103
|
-
}
|
|
104
|
-
},
|
|
105
|
-
totalEnergy(g, soft, gr = 0, rs = 0) {
|
|
106
|
-
const soft2 = soft * soft;
|
|
107
|
-
let ke = 0;
|
|
108
|
-
for (let i = 0; i < count; i++) {
|
|
109
|
-
const m = mass[i] ?? 0;
|
|
110
|
-
const vxi = vx[i] ?? 0;
|
|
111
|
-
const vyi = vy[i] ?? 0;
|
|
112
|
-
ke += 0.5 * m * (vxi * vxi + vyi * vyi);
|
|
113
|
-
}
|
|
114
|
-
let pe = 0;
|
|
115
|
-
for (let i = 0; i < count; i++) {
|
|
116
|
-
for (let j = i + 1; j < count; j++) {
|
|
117
|
-
const dx = (px[j] ?? 0) - (px[i] ?? 0);
|
|
118
|
-
const dy = (py[j] ?? 0) - (py[i] ?? 0);
|
|
119
|
-
const d = Math.sqrt(dx * dx + dy * dy + soft2);
|
|
120
|
-
pe -= g * (mass[i] ?? 0) * (mass[j] ?? 0) / d;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
if (gr > 0 && count > 1) {
|
|
124
|
-
const cxp = px[0] ?? 0;
|
|
125
|
-
const cyp = py[0] ?? 0;
|
|
126
|
-
const beta = gr * g * (mass[0] ?? 0) * rs * rs;
|
|
127
|
-
for (let i = 1; i < count; i++) {
|
|
128
|
-
const dx = (px[i] ?? 0) - cxp;
|
|
129
|
-
const dy = (py[i] ?? 0) - cyp;
|
|
130
|
-
const u = dx * dx + dy * dy + soft2;
|
|
131
|
-
pe -= beta * (mass[i] ?? 0) / (3 * u * Math.sqrt(u));
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
return ke + pe;
|
|
135
|
-
},
|
|
136
|
-
centerOfMass(out) {
|
|
137
|
-
let mx = 0;
|
|
138
|
-
let my = 0;
|
|
139
|
-
let mt = 0;
|
|
140
|
-
for (let i = 0; i < count; i++) {
|
|
141
|
-
const m = mass[i] ?? 0;
|
|
142
|
-
mx += (px[i] ?? 0) * m;
|
|
143
|
-
my += (py[i] ?? 0) * m;
|
|
144
|
-
mt += m;
|
|
145
|
-
}
|
|
146
|
-
out.x = mt > 0 ? mx / mt : 0;
|
|
147
|
-
out.y = mt > 0 ? my / mt : 0;
|
|
148
|
-
},
|
|
149
|
-
translate(dx, dy) {
|
|
150
|
-
for (let i = 0; i < count; i++) {
|
|
151
|
-
px[i] = (px[i] ?? 0) + dx;
|
|
152
|
-
py[i] = (py[i] ?? 0) + dy;
|
|
153
|
-
}
|
|
154
|
-
},
|
|
155
|
-
absorb(into, victim) {
|
|
156
|
-
if (into === victim || into < 0 || victim < 0 || into >= count || victim >= count) return;
|
|
157
|
-
const mi = mass[into] ?? 0;
|
|
158
|
-
const mj = mass[victim] ?? 0;
|
|
159
|
-
const M = mi + mj;
|
|
160
|
-
if (M <= 0) return;
|
|
161
|
-
px[into] = ((px[into] ?? 0) * mi + (px[victim] ?? 0) * mj) / M;
|
|
162
|
-
py[into] = ((py[into] ?? 0) * mi + (py[victim] ?? 0) * mj) / M;
|
|
163
|
-
vx[into] = ((vx[into] ?? 0) * mi + (vx[victim] ?? 0) * mj) / M;
|
|
164
|
-
vy[into] = ((vy[into] ?? 0) * mi + (vy[victim] ?? 0) * mj) / M;
|
|
165
|
-
mass[into] = M;
|
|
166
|
-
const last = count - 1;
|
|
167
|
-
if (victim !== last) {
|
|
168
|
-
px[victim] = px[last] ?? 0;
|
|
169
|
-
py[victim] = py[last] ?? 0;
|
|
170
|
-
vx[victim] = vx[last] ?? 0;
|
|
171
|
-
vy[victim] = vy[last] ?? 0;
|
|
172
|
-
mass[victim] = mass[last] ?? 0;
|
|
173
|
-
tag[victim] = tag[last] ?? 0;
|
|
174
|
-
}
|
|
175
|
-
count--;
|
|
176
|
-
primed = false;
|
|
177
|
-
},
|
|
178
|
-
closestPair(out) {
|
|
179
|
-
out.i = -1;
|
|
180
|
-
out.j = -1;
|
|
181
|
-
out.sep = Number.POSITIVE_INFINITY;
|
|
182
|
-
for (let i = 0; i < count; i++) {
|
|
183
|
-
for (let j = i + 1; j < count; j++) {
|
|
184
|
-
const dx = (px[j] ?? 0) - (px[i] ?? 0);
|
|
185
|
-
const dy = (py[j] ?? 0) - (py[i] ?? 0);
|
|
186
|
-
const d = Math.sqrt(dx * dx + dy * dy);
|
|
187
|
-
if (d < out.sep) {
|
|
188
|
-
out.sep = d;
|
|
189
|
-
out.i = i;
|
|
190
|
-
out.j = j;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// src/integrate/verlet.ts
|
|
199
|
-
function leapfrogInto(count, px, py, vx, vy, dt, accel, ax, ay) {
|
|
200
|
-
accel(count, px, py, vx, vy, ax, ay);
|
|
201
|
-
for (let i = 0; i < count; i++) {
|
|
202
|
-
vx[i] = (vx[i] ?? 0) + 0.5 * (ax[i] ?? 0) * dt;
|
|
203
|
-
vy[i] = (vy[i] ?? 0) + 0.5 * (ay[i] ?? 0) * dt;
|
|
204
|
-
}
|
|
205
|
-
for (let i = 0; i < count; i++) {
|
|
206
|
-
px[i] = (px[i] ?? 0) + (vx[i] ?? 0) * dt;
|
|
207
|
-
py[i] = (py[i] ?? 0) + (vy[i] ?? 0) * dt;
|
|
208
|
-
}
|
|
209
|
-
accel(count, px, py, vx, vy, ax, ay);
|
|
210
|
-
for (let i = 0; i < count; i++) {
|
|
211
|
-
vx[i] = (vx[i] ?? 0) + 0.5 * (ax[i] ?? 0) * dt;
|
|
212
|
-
vy[i] = (vy[i] ?? 0) + 0.5 * (ay[i] ?? 0) * dt;
|
|
213
|
-
}
|
|
1
|
+
//#region src/agent/stigmergy.ts
|
|
2
|
+
const TWO_PI$1 = Math.PI * 2;
|
|
3
|
+
const SENSE_DEFAULTS = {
|
|
4
|
+
sensorAngle: .5,
|
|
5
|
+
sensorDist: 9,
|
|
6
|
+
rotateAngle: .45
|
|
7
|
+
};
|
|
8
|
+
/** Read the field at one sensor: `offset` rad off the agent heading, `dist` px ahead. */
|
|
9
|
+
function senseAt(field, x, y, heading, offset, dist) {
|
|
10
|
+
const a = heading + offset;
|
|
11
|
+
return field.sample(x + Math.cos(a) * dist, y + Math.sin(a) * dist);
|
|
214
12
|
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
accel(count, px, py, vx, vy, ax, ay);
|
|
223
|
-
for (let i = 0; i < count; i++) {
|
|
224
|
-
vx[i] = (vx[i] ?? 0) + 0.5 * ((axPrev[i] ?? 0) + (ax[i] ?? 0)) * dt;
|
|
225
|
-
vy[i] = (vy[i] ?? 0) + 0.5 * ((ayPrev[i] ?? 0) + (ay[i] ?? 0)) * dt;
|
|
226
|
-
}
|
|
13
|
+
/** Jones turn decision from three sensor readings: −1 turn left, 0 straight, +1 turn right.
|
|
14
|
+
* `rand` ∈ [0,1) breaks the symmetric "both sides brighter" case. */
|
|
15
|
+
function decideTurn(left, center, right, rand) {
|
|
16
|
+
if (center > left && center > right) return 0;
|
|
17
|
+
if (center < left && center < right) return rand < .5 ? -1 : 1;
|
|
18
|
+
if (left < right) return 1;
|
|
19
|
+
return -1;
|
|
227
20
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
}
|
|
295
|
-
return h;
|
|
296
|
-
},
|
|
297
|
-
prune(now) {
|
|
298
|
-
for (let i = 0; i < cap; i++) {
|
|
299
|
-
if (active[i] === 0) continue;
|
|
300
|
-
if (now - (t0[i] ?? 0) > (life[i] ?? 1) * LIFE_CUTOFF) {
|
|
301
|
-
active[i] = 0;
|
|
302
|
-
live--;
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
},
|
|
306
|
-
clear() {
|
|
307
|
-
active.fill(0);
|
|
308
|
-
live = 0;
|
|
309
|
-
}
|
|
310
|
-
};
|
|
21
|
+
function createStigmergy(field, config, rng) {
|
|
22
|
+
const { count } = config;
|
|
23
|
+
const px = new Float32Array(count);
|
|
24
|
+
const py = new Float32Array(count);
|
|
25
|
+
const heading = new Float32Array(count);
|
|
26
|
+
const fw = field.w;
|
|
27
|
+
const fh = field.h;
|
|
28
|
+
for (let i = 0; i < count; i++) {
|
|
29
|
+
px[i] = rng.float(0, fw);
|
|
30
|
+
py[i] = rng.float(0, fh);
|
|
31
|
+
heading[i] = rng.float(0, TWO_PI$1);
|
|
32
|
+
}
|
|
33
|
+
function wrapX(x, w) {
|
|
34
|
+
if (field.edge === "toroidal") return (x % w + w) % w;
|
|
35
|
+
return x < 0 ? 0 : x >= w ? w - 1 : x;
|
|
36
|
+
}
|
|
37
|
+
function wrapY(y, h) {
|
|
38
|
+
if (field.edge === "toroidal") return (y % h + h) % h;
|
|
39
|
+
return y < 0 ? 0 : y >= h ? h - 1 : y;
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
count,
|
|
43
|
+
px,
|
|
44
|
+
py,
|
|
45
|
+
heading,
|
|
46
|
+
update() {
|
|
47
|
+
const w = field.w;
|
|
48
|
+
const h = field.h;
|
|
49
|
+
const sa = config.sensorAngle;
|
|
50
|
+
const sd = config.sensorDist;
|
|
51
|
+
const turnRate = config.rotateAngle;
|
|
52
|
+
const chaos = config.chaos;
|
|
53
|
+
const stepSize = config.stepSize;
|
|
54
|
+
const deposit = config.depositAmount;
|
|
55
|
+
for (let i = 0; i < count; i++) {
|
|
56
|
+
const x = px[i] ?? 0;
|
|
57
|
+
const y = py[i] ?? 0;
|
|
58
|
+
let hd = heading[i] ?? 0;
|
|
59
|
+
const cF = senseAt(field, x, y, hd, 0, sd);
|
|
60
|
+
const cL = senseAt(field, x, y, hd, -sa, sd);
|
|
61
|
+
const cR = senseAt(field, x, y, hd, sa, sd);
|
|
62
|
+
hd += decideTurn(cL, cF, cR, rng.float(0, 1)) * turnRate;
|
|
63
|
+
hd += (rng.float(0, 1) * 2 - 1) * chaos;
|
|
64
|
+
if (config.onSense) hd += config.onSense(i, x, y, hd);
|
|
65
|
+
const nx = wrapX(x + Math.cos(hd) * stepSize, w);
|
|
66
|
+
const ny = wrapY(y + Math.sin(hd) * stepSize, h);
|
|
67
|
+
px[i] = nx;
|
|
68
|
+
py[i] = ny;
|
|
69
|
+
heading[i] = hd;
|
|
70
|
+
field.deposit(nx, ny, deposit);
|
|
71
|
+
if (config.onMove) config.onMove(i, nx, ny, hd);
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
forEachAgent(cb, stride = 1) {
|
|
75
|
+
const s = Math.max(1, Math.floor(stride));
|
|
76
|
+
for (let i = 0; i < count; i += s) cb(px[i] ?? 0, py[i] ?? 0);
|
|
77
|
+
},
|
|
78
|
+
resize(width, height) {
|
|
79
|
+
const newW = Math.max(1, width);
|
|
80
|
+
const newH = Math.max(1, height);
|
|
81
|
+
for (let i = 0; i < count; i++) {
|
|
82
|
+
px[i] = wrapX(px[i] ?? 0, newW);
|
|
83
|
+
py[i] = wrapY(py[i] ?? 0, newH);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
311
87
|
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/field/scalar-field.ts
|
|
90
|
+
const clamp01 = (v) => v < 0 ? 0 : v > 1 ? 1 : v;
|
|
91
|
+
const smooth = (t) => {
|
|
92
|
+
const c = clamp01(t);
|
|
93
|
+
return c * c * (3 - 2 * c);
|
|
318
94
|
};
|
|
319
95
|
function createScalarField(width, height, cellSize, edgeMode = "toroidal") {
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
rows = Math.max(1, Math.ceil(h / cell));
|
|
508
|
-
a = new Float32Array(cols * rows);
|
|
509
|
-
b = new Float32Array(cols * rows);
|
|
510
|
-
rebuildEdgeFns();
|
|
511
|
-
}
|
|
512
|
-
};
|
|
513
|
-
return field;
|
|
96
|
+
const cell = Math.max(1, cellSize);
|
|
97
|
+
let w = Math.max(1, width);
|
|
98
|
+
let h = Math.max(1, height);
|
|
99
|
+
let cols = Math.max(1, Math.ceil(w / cell));
|
|
100
|
+
let rows = Math.max(1, Math.ceil(h / cell));
|
|
101
|
+
let a = new Float32Array(cols * rows);
|
|
102
|
+
let b = new Float32Array(cols * rows);
|
|
103
|
+
let wrapCol;
|
|
104
|
+
let wrapRow;
|
|
105
|
+
function rebuildEdgeFns() {
|
|
106
|
+
if (edgeMode === "toroidal") {
|
|
107
|
+
wrapCol = (c) => (c % cols + cols) % cols;
|
|
108
|
+
wrapRow = (r) => (r % rows + rows) % rows;
|
|
109
|
+
} else {
|
|
110
|
+
wrapCol = (c) => c < 0 ? 0 : c >= cols ? cols - 1 : c;
|
|
111
|
+
wrapRow = (r) => r < 0 ? 0 : r >= rows ? rows - 1 : r;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
rebuildEdgeFns();
|
|
115
|
+
const field = {
|
|
116
|
+
get w() {
|
|
117
|
+
return w;
|
|
118
|
+
},
|
|
119
|
+
get h() {
|
|
120
|
+
return h;
|
|
121
|
+
},
|
|
122
|
+
get cols() {
|
|
123
|
+
return cols;
|
|
124
|
+
},
|
|
125
|
+
get rows() {
|
|
126
|
+
return rows;
|
|
127
|
+
},
|
|
128
|
+
get cell() {
|
|
129
|
+
return cell;
|
|
130
|
+
},
|
|
131
|
+
get edge() {
|
|
132
|
+
return edgeMode;
|
|
133
|
+
},
|
|
134
|
+
sample(x, y) {
|
|
135
|
+
const gx = x / cell - .5;
|
|
136
|
+
const gy = y / cell - .5;
|
|
137
|
+
const x0 = Math.floor(gx);
|
|
138
|
+
const y0 = Math.floor(gy);
|
|
139
|
+
const fx = gx - x0;
|
|
140
|
+
const fy = gy - y0;
|
|
141
|
+
const xa = wrapCol(x0);
|
|
142
|
+
const xb = wrapCol(x0 + 1);
|
|
143
|
+
const ya = wrapRow(y0);
|
|
144
|
+
const yb = wrapRow(y0 + 1);
|
|
145
|
+
const a00 = a[ya * cols + xa] ?? 0;
|
|
146
|
+
const a10 = a[ya * cols + xb] ?? 0;
|
|
147
|
+
const a01 = a[yb * cols + xa] ?? 0;
|
|
148
|
+
const a11 = a[yb * cols + xb] ?? 0;
|
|
149
|
+
const top = a00 + (a10 - a00) * fx;
|
|
150
|
+
return top + (a01 + (a11 - a01) * fx - top) * fy;
|
|
151
|
+
},
|
|
152
|
+
gradient(x, y, out) {
|
|
153
|
+
out.x = field.sample(x + cell, y) - field.sample(x - cell, y);
|
|
154
|
+
out.y = field.sample(x, y + cell) - field.sample(x, y - cell);
|
|
155
|
+
},
|
|
156
|
+
deposit(x, y, amount) {
|
|
157
|
+
const cx = wrapCol(Math.floor(x / cell));
|
|
158
|
+
const i = wrapRow(Math.floor(y / cell)) * cols + cx;
|
|
159
|
+
a[i] = (a[i] ?? 0) + amount;
|
|
160
|
+
},
|
|
161
|
+
erode(x, y, radiusPx, mul) {
|
|
162
|
+
const cr = Math.max(1, Math.ceil(radiusPx / cell));
|
|
163
|
+
const ccx = Math.floor(x / cell);
|
|
164
|
+
const ccy = Math.floor(y / cell);
|
|
165
|
+
const r2 = cr * cr;
|
|
166
|
+
for (let dy = -cr; dy <= cr; dy++) for (let dx = -cr; dx <= cr; dx++) {
|
|
167
|
+
if (dx * dx + dy * dy > r2) continue;
|
|
168
|
+
const i = wrapRow(ccy + dy) * cols + wrapCol(ccx + dx);
|
|
169
|
+
a[i] = (a[i] ?? 0) * mul;
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
step(diffuseRate, decayRate, maxVal) {
|
|
173
|
+
for (let r = 0; r < rows; r++) {
|
|
174
|
+
const base = r * cols;
|
|
175
|
+
for (let c = 0; c < cols; c++) {
|
|
176
|
+
const cl = edgeMode === "toroidal" ? c === 0 ? cols - 1 : c - 1 : c === 0 ? 0 : c - 1;
|
|
177
|
+
const cr2 = edgeMode === "toroidal" ? c === cols - 1 ? 0 : c + 1 : c === cols - 1 ? cols - 1 : c + 1;
|
|
178
|
+
b[base + c] = ((a[base + cl] ?? 0) + (a[base + c] ?? 0) + (a[base + cr2] ?? 0)) / 3;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
for (let r = 0; r < rows; r++) {
|
|
182
|
+
const ru = edgeMode === "toroidal" ? r === 0 ? rows - 1 : r - 1 : r === 0 ? 0 : r - 1;
|
|
183
|
+
const rd = edgeMode === "toroidal" ? r === rows - 1 ? 0 : r + 1 : r === rows - 1 ? rows - 1 : r + 1;
|
|
184
|
+
const base = r * cols;
|
|
185
|
+
const upBase = ru * cols;
|
|
186
|
+
const dnBase = rd * cols;
|
|
187
|
+
for (let c = 0; c < cols; c++) {
|
|
188
|
+
const blurred = ((b[upBase + c] ?? 0) + (b[base + c] ?? 0) + (b[dnBase + c] ?? 0)) / 3;
|
|
189
|
+
const orig = a[base + c] ?? 0;
|
|
190
|
+
const next = (orig + (blurred - orig) * diffuseRate) * decayRate;
|
|
191
|
+
a[base + c] = next > maxVal ? maxVal : next;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
meanValue(stride) {
|
|
196
|
+
const s = Math.max(1, Math.floor(stride));
|
|
197
|
+
let sum = 0;
|
|
198
|
+
let n = 0;
|
|
199
|
+
for (let i = 0; i < a.length; i += s) {
|
|
200
|
+
sum += a[i] ?? 0;
|
|
201
|
+
n++;
|
|
202
|
+
}
|
|
203
|
+
return n > 0 ? sum / n : 0;
|
|
204
|
+
},
|
|
205
|
+
maxValue(stride) {
|
|
206
|
+
const s = Math.max(1, Math.floor(stride));
|
|
207
|
+
if (Math.ceil(a.length / s) === 0) return 0;
|
|
208
|
+
let m = Number.NEGATIVE_INFINITY;
|
|
209
|
+
for (let i = 0; i < a.length; i += s) {
|
|
210
|
+
const v = a[i] ?? 0;
|
|
211
|
+
if (v > m) m = v;
|
|
212
|
+
}
|
|
213
|
+
return m;
|
|
214
|
+
},
|
|
215
|
+
localMean(x, y, radiusPx) {
|
|
216
|
+
const cr = Math.max(1, Math.ceil(radiusPx / cell));
|
|
217
|
+
const ccx = Math.floor(x / cell);
|
|
218
|
+
const ccy = Math.floor(y / cell);
|
|
219
|
+
let sum = 0;
|
|
220
|
+
let n = 0;
|
|
221
|
+
for (let dy = -cr; dy <= cr; dy++) for (let dx = -cr; dx <= cr; dx++) {
|
|
222
|
+
sum += a[wrapRow(ccy + dy) * cols + wrapCol(ccx + dx)] ?? 0;
|
|
223
|
+
n++;
|
|
224
|
+
}
|
|
225
|
+
return n > 0 ? sum / n : 0;
|
|
226
|
+
},
|
|
227
|
+
toRGBA(out, norm, low, mid, hi, knot) {
|
|
228
|
+
const inv = norm > 1e-6 ? 1 / norm : 0;
|
|
229
|
+
const lr = low[0] ?? 0;
|
|
230
|
+
const lg = low[1] ?? 0;
|
|
231
|
+
const lb = low[2] ?? 0;
|
|
232
|
+
const mr = mid[0] ?? 0;
|
|
233
|
+
const mg = mid[1] ?? 0;
|
|
234
|
+
const mb = mid[2] ?? 0;
|
|
235
|
+
const hr = hi[0] ?? 0;
|
|
236
|
+
const hg = hi[1] ?? 0;
|
|
237
|
+
const hb = hi[2] ?? 0;
|
|
238
|
+
const kr = knot[0] ?? 0;
|
|
239
|
+
const kg = knot[1] ?? 0;
|
|
240
|
+
const kb = knot[2] ?? 0;
|
|
241
|
+
for (let i = 0; i < a.length; i++) {
|
|
242
|
+
const v = smooth((a[i] ?? 0) * inv);
|
|
243
|
+
let r;
|
|
244
|
+
let g;
|
|
245
|
+
let bl;
|
|
246
|
+
if (v < .5) {
|
|
247
|
+
const t = v * 2;
|
|
248
|
+
r = lr + (mr - lr) * t;
|
|
249
|
+
g = lg + (mg - lg) * t;
|
|
250
|
+
bl = lb + (mb - lb) * t;
|
|
251
|
+
} else if (v < .85) {
|
|
252
|
+
const t = (v - .5) / .35;
|
|
253
|
+
r = mr + (hr - mr) * t;
|
|
254
|
+
g = mg + (hg - mg) * t;
|
|
255
|
+
bl = mb + (hb - mb) * t;
|
|
256
|
+
} else {
|
|
257
|
+
const t = (v - .85) / .15;
|
|
258
|
+
r = hr + (kr - hr) * t;
|
|
259
|
+
g = hg + (kg - hg) * t;
|
|
260
|
+
bl = hb + (kb - hb) * t;
|
|
261
|
+
}
|
|
262
|
+
const o = i << 2;
|
|
263
|
+
const rv = r * v * 255;
|
|
264
|
+
const gv = g * v * 255;
|
|
265
|
+
const bv = bl * v * 255;
|
|
266
|
+
out[o] = rv < 0 ? 0 : rv > 255 ? 255 : rv | 0;
|
|
267
|
+
out[o + 1] = gv < 0 ? 0 : gv > 255 ? 255 : gv | 0;
|
|
268
|
+
out[o + 2] = bv < 0 ? 0 : bv > 255 ? 255 : bv | 0;
|
|
269
|
+
out[o + 3] = 255;
|
|
270
|
+
}
|
|
271
|
+
},
|
|
272
|
+
resize(width2, height2) {
|
|
273
|
+
w = Math.max(1, width2);
|
|
274
|
+
h = Math.max(1, height2);
|
|
275
|
+
cols = Math.max(1, Math.ceil(w / cell));
|
|
276
|
+
rows = Math.max(1, Math.ceil(h / cell));
|
|
277
|
+
a = new Float32Array(cols * rows);
|
|
278
|
+
b = new Float32Array(cols * rows);
|
|
279
|
+
rebuildEdgeFns();
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
return field;
|
|
514
283
|
}
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
284
|
+
//#endregion
|
|
285
|
+
//#region src/integrate/nbody.ts
|
|
286
|
+
function createNBodySystem(capacity) {
|
|
287
|
+
const cap = Math.max(1, Math.floor(capacity));
|
|
288
|
+
const px = new Float32Array(cap);
|
|
289
|
+
const py = new Float32Array(cap);
|
|
290
|
+
const vx = new Float32Array(cap);
|
|
291
|
+
const vy = new Float32Array(cap);
|
|
292
|
+
const mass = new Float32Array(cap);
|
|
293
|
+
const tag = new Float32Array(cap);
|
|
294
|
+
const ax = new Float32Array(cap);
|
|
295
|
+
const ay = new Float32Array(cap);
|
|
296
|
+
const axOld = new Float32Array(cap);
|
|
297
|
+
const ayOld = new Float32Array(cap);
|
|
298
|
+
let count = 0;
|
|
299
|
+
let primed = false;
|
|
300
|
+
const computeAccel = (g, soft, gr, rs) => {
|
|
301
|
+
const soft2 = soft * soft;
|
|
302
|
+
for (let i = 0; i < count; i++) {
|
|
303
|
+
ax[i] = 0;
|
|
304
|
+
ay[i] = 0;
|
|
305
|
+
}
|
|
306
|
+
for (let i = 0; i < count; i++) {
|
|
307
|
+
const xi = px[i] ?? 0;
|
|
308
|
+
const yi = py[i] ?? 0;
|
|
309
|
+
const mi = mass[i] ?? 0;
|
|
310
|
+
for (let j = i + 1; j < count; j++) {
|
|
311
|
+
const dx = (px[j] ?? 0) - xi;
|
|
312
|
+
const dy = (py[j] ?? 0) - yi;
|
|
313
|
+
const d2 = dx * dx + dy * dy + soft2;
|
|
314
|
+
const invD = 1 / Math.sqrt(d2);
|
|
315
|
+
const invD3 = g * invD * invD * invD;
|
|
316
|
+
const mj = mass[j] ?? 0;
|
|
317
|
+
ax[i] = (ax[i] ?? 0) + dx * invD3 * mj;
|
|
318
|
+
ay[i] = (ay[i] ?? 0) + dy * invD3 * mj;
|
|
319
|
+
ax[j] = (ax[j] ?? 0) - dx * invD3 * mi;
|
|
320
|
+
ay[j] = (ay[j] ?? 0) - dy * invD3 * mi;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if (gr > 0 && count > 1) {
|
|
324
|
+
const cxp = px[0] ?? 0;
|
|
325
|
+
const cyp = py[0] ?? 0;
|
|
326
|
+
const beta = gr * g * (mass[0] ?? 0) * rs * rs;
|
|
327
|
+
for (let i = 1; i < count; i++) {
|
|
328
|
+
const dx = (px[i] ?? 0) - cxp;
|
|
329
|
+
const dy = (py[i] ?? 0) - cyp;
|
|
330
|
+
const invr2 = 1 / (dx * dx + dy * dy + soft2);
|
|
331
|
+
const k = beta * invr2 * invr2 * Math.sqrt(invr2);
|
|
332
|
+
ax[i] = (ax[i] ?? 0) - dx * k;
|
|
333
|
+
ay[i] = (ay[i] ?? 0) - dy * k;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
return {
|
|
338
|
+
capacity: cap,
|
|
339
|
+
get count() {
|
|
340
|
+
return count;
|
|
341
|
+
},
|
|
342
|
+
px,
|
|
343
|
+
py,
|
|
344
|
+
vx,
|
|
345
|
+
vy,
|
|
346
|
+
mass,
|
|
347
|
+
tag,
|
|
348
|
+
maxMass() {
|
|
349
|
+
let m = 0;
|
|
350
|
+
for (let i = 0; i < count; i++) {
|
|
351
|
+
const v = mass[i] ?? 0;
|
|
352
|
+
if (v > m) m = v;
|
|
353
|
+
}
|
|
354
|
+
return m;
|
|
355
|
+
},
|
|
356
|
+
add(x, y, vx0, vy0, m, t = 0) {
|
|
357
|
+
if (count >= cap) return -1;
|
|
358
|
+
const i = count++;
|
|
359
|
+
px[i] = x;
|
|
360
|
+
py[i] = y;
|
|
361
|
+
vx[i] = vx0;
|
|
362
|
+
vy[i] = vy0;
|
|
363
|
+
mass[i] = m;
|
|
364
|
+
tag[i] = t;
|
|
365
|
+
primed = false;
|
|
366
|
+
return i;
|
|
367
|
+
},
|
|
368
|
+
step(dt, g, soft, gr = 0, rs = 0) {
|
|
369
|
+
if (count === 0 || dt <= 0) return;
|
|
370
|
+
if (!primed) {
|
|
371
|
+
computeAccel(g, soft, gr, rs);
|
|
372
|
+
primed = true;
|
|
373
|
+
}
|
|
374
|
+
for (let i = 0; i < count; i++) {
|
|
375
|
+
const axi = ax[i] ?? 0;
|
|
376
|
+
const ayi = ay[i] ?? 0;
|
|
377
|
+
px[i] = (px[i] ?? 0) + (vx[i] ?? 0) * dt + .5 * axi * dt * dt;
|
|
378
|
+
py[i] = (py[i] ?? 0) + (vy[i] ?? 0) * dt + .5 * ayi * dt * dt;
|
|
379
|
+
axOld[i] = axi;
|
|
380
|
+
ayOld[i] = ayi;
|
|
381
|
+
}
|
|
382
|
+
computeAccel(g, soft, gr, rs);
|
|
383
|
+
for (let i = 0; i < count; i++) {
|
|
384
|
+
vx[i] = (vx[i] ?? 0) + .5 * ((axOld[i] ?? 0) + (ax[i] ?? 0)) * dt;
|
|
385
|
+
vy[i] = (vy[i] ?? 0) + .5 * ((ayOld[i] ?? 0) + (ay[i] ?? 0)) * dt;
|
|
386
|
+
}
|
|
387
|
+
},
|
|
388
|
+
totalEnergy(g, soft, gr = 0, rs = 0) {
|
|
389
|
+
const soft2 = soft * soft;
|
|
390
|
+
let ke = 0;
|
|
391
|
+
for (let i = 0; i < count; i++) {
|
|
392
|
+
const m = mass[i] ?? 0;
|
|
393
|
+
const vxi = vx[i] ?? 0;
|
|
394
|
+
const vyi = vy[i] ?? 0;
|
|
395
|
+
ke += .5 * m * (vxi * vxi + vyi * vyi);
|
|
396
|
+
}
|
|
397
|
+
let pe = 0;
|
|
398
|
+
for (let i = 0; i < count; i++) for (let j = i + 1; j < count; j++) {
|
|
399
|
+
const dx = (px[j] ?? 0) - (px[i] ?? 0);
|
|
400
|
+
const dy = (py[j] ?? 0) - (py[i] ?? 0);
|
|
401
|
+
const d = Math.sqrt(dx * dx + dy * dy + soft2);
|
|
402
|
+
pe -= g * (mass[i] ?? 0) * (mass[j] ?? 0) / d;
|
|
403
|
+
}
|
|
404
|
+
if (gr > 0 && count > 1) {
|
|
405
|
+
const cxp = px[0] ?? 0;
|
|
406
|
+
const cyp = py[0] ?? 0;
|
|
407
|
+
const beta = gr * g * (mass[0] ?? 0) * rs * rs;
|
|
408
|
+
for (let i = 1; i < count; i++) {
|
|
409
|
+
const dx = (px[i] ?? 0) - cxp;
|
|
410
|
+
const dy = (py[i] ?? 0) - cyp;
|
|
411
|
+
const u = dx * dx + dy * dy + soft2;
|
|
412
|
+
pe -= beta * (mass[i] ?? 0) / (3 * u * Math.sqrt(u));
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return ke + pe;
|
|
416
|
+
},
|
|
417
|
+
centerOfMass(out) {
|
|
418
|
+
let mx = 0;
|
|
419
|
+
let my = 0;
|
|
420
|
+
let mt = 0;
|
|
421
|
+
for (let i = 0; i < count; i++) {
|
|
422
|
+
const m = mass[i] ?? 0;
|
|
423
|
+
mx += (px[i] ?? 0) * m;
|
|
424
|
+
my += (py[i] ?? 0) * m;
|
|
425
|
+
mt += m;
|
|
426
|
+
}
|
|
427
|
+
out.x = mt > 0 ? mx / mt : 0;
|
|
428
|
+
out.y = mt > 0 ? my / mt : 0;
|
|
429
|
+
},
|
|
430
|
+
translate(dx, dy) {
|
|
431
|
+
for (let i = 0; i < count; i++) {
|
|
432
|
+
px[i] = (px[i] ?? 0) + dx;
|
|
433
|
+
py[i] = (py[i] ?? 0) + dy;
|
|
434
|
+
}
|
|
435
|
+
},
|
|
436
|
+
absorb(into, victim) {
|
|
437
|
+
if (into === victim || into < 0 || victim < 0 || into >= count || victim >= count) return;
|
|
438
|
+
const mi = mass[into] ?? 0;
|
|
439
|
+
const mj = mass[victim] ?? 0;
|
|
440
|
+
const M = mi + mj;
|
|
441
|
+
if (M <= 0) return;
|
|
442
|
+
px[into] = ((px[into] ?? 0) * mi + (px[victim] ?? 0) * mj) / M;
|
|
443
|
+
py[into] = ((py[into] ?? 0) * mi + (py[victim] ?? 0) * mj) / M;
|
|
444
|
+
vx[into] = ((vx[into] ?? 0) * mi + (vx[victim] ?? 0) * mj) / M;
|
|
445
|
+
vy[into] = ((vy[into] ?? 0) * mi + (vy[victim] ?? 0) * mj) / M;
|
|
446
|
+
mass[into] = M;
|
|
447
|
+
const last = count - 1;
|
|
448
|
+
if (victim !== last) {
|
|
449
|
+
px[victim] = px[last] ?? 0;
|
|
450
|
+
py[victim] = py[last] ?? 0;
|
|
451
|
+
vx[victim] = vx[last] ?? 0;
|
|
452
|
+
vy[victim] = vy[last] ?? 0;
|
|
453
|
+
mass[victim] = mass[last] ?? 0;
|
|
454
|
+
tag[victim] = tag[last] ?? 0;
|
|
455
|
+
}
|
|
456
|
+
count--;
|
|
457
|
+
primed = false;
|
|
458
|
+
},
|
|
459
|
+
closestPair(out) {
|
|
460
|
+
out.i = -1;
|
|
461
|
+
out.j = -1;
|
|
462
|
+
out.sep = Number.POSITIVE_INFINITY;
|
|
463
|
+
for (let i = 0; i < count; i++) for (let j = i + 1; j < count; j++) {
|
|
464
|
+
const dx = (px[j] ?? 0) - (px[i] ?? 0);
|
|
465
|
+
const dy = (py[j] ?? 0) - (py[i] ?? 0);
|
|
466
|
+
const d = Math.sqrt(dx * dx + dy * dy);
|
|
467
|
+
if (d < out.sep) {
|
|
468
|
+
out.sep = d;
|
|
469
|
+
out.i = i;
|
|
470
|
+
out.j = j;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
};
|
|
526
475
|
}
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
476
|
+
//#endregion
|
|
477
|
+
//#region src/integrate/verlet.ts
|
|
478
|
+
/**
|
|
479
|
+
* Self-contained kick-drift-kick leapfrog (velocity-Verlet-equivalent). Advances pos/vel in place
|
|
480
|
+
* under accel; symplectic => bounded energy for conservative forces. ax/ay are caller scratch
|
|
481
|
+
* (length >= count). Evals accel TWICE per step. Zero-alloc.
|
|
482
|
+
*/
|
|
483
|
+
function leapfrogInto(count, px, py, vx, vy, dt, accel, ax, ay) {
|
|
484
|
+
accel(count, px, py, vx, vy, ax, ay);
|
|
485
|
+
for (let i = 0; i < count; i++) {
|
|
486
|
+
vx[i] = (vx[i] ?? 0) + .5 * (ax[i] ?? 0) * dt;
|
|
487
|
+
vy[i] = (vy[i] ?? 0) + .5 * (ay[i] ?? 0) * dt;
|
|
488
|
+
}
|
|
489
|
+
for (let i = 0; i < count; i++) {
|
|
490
|
+
px[i] = (px[i] ?? 0) + (vx[i] ?? 0) * dt;
|
|
491
|
+
py[i] = (py[i] ?? 0) + (vy[i] ?? 0) * dt;
|
|
492
|
+
}
|
|
493
|
+
accel(count, px, py, vx, vy, ax, ay);
|
|
494
|
+
for (let i = 0; i < count; i++) {
|
|
495
|
+
vx[i] = (vx[i] ?? 0) + .5 * (ax[i] ?? 0) * dt;
|
|
496
|
+
vy[i] = (vy[i] ?? 0) + .5 * (ay[i] ?? 0) * dt;
|
|
497
|
+
}
|
|
532
498
|
}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
}
|
|
553
|
-
const af = {
|
|
554
|
-
count,
|
|
555
|
-
px,
|
|
556
|
-
py,
|
|
557
|
-
heading,
|
|
558
|
-
update() {
|
|
559
|
-
const w = field.w;
|
|
560
|
-
const h = field.h;
|
|
561
|
-
const sa = config.sensorAngle;
|
|
562
|
-
const sd = config.sensorDist;
|
|
563
|
-
const turnRate = config.rotateAngle;
|
|
564
|
-
const chaos = config.chaos;
|
|
565
|
-
const stepSize = config.stepSize;
|
|
566
|
-
const deposit = config.depositAmount;
|
|
567
|
-
for (let i = 0; i < count; i++) {
|
|
568
|
-
const x = px[i] ?? 0;
|
|
569
|
-
const y = py[i] ?? 0;
|
|
570
|
-
let hd = heading[i] ?? 0;
|
|
571
|
-
const cF = senseAt(field, x, y, hd, 0, sd);
|
|
572
|
-
const cL = senseAt(field, x, y, hd, -sa, sd);
|
|
573
|
-
const cR = senseAt(field, x, y, hd, sa, sd);
|
|
574
|
-
hd += decideTurn(cL, cF, cR, rng.float(0, 1)) * turnRate;
|
|
575
|
-
hd += (rng.float(0, 1) * 2 - 1) * chaos;
|
|
576
|
-
if (config.onSense) hd += config.onSense(i, x, y, hd);
|
|
577
|
-
const nx = wrapX(x + Math.cos(hd) * stepSize, w);
|
|
578
|
-
const ny = wrapY(y + Math.sin(hd) * stepSize, h);
|
|
579
|
-
px[i] = nx;
|
|
580
|
-
py[i] = ny;
|
|
581
|
-
heading[i] = hd;
|
|
582
|
-
field.deposit(nx, ny, deposit);
|
|
583
|
-
if (config.onMove) config.onMove(i, nx, ny, hd);
|
|
584
|
-
}
|
|
585
|
-
},
|
|
586
|
-
forEachAgent(cb, stride = 1) {
|
|
587
|
-
const s = Math.max(1, Math.floor(stride));
|
|
588
|
-
for (let i = 0; i < count; i += s) {
|
|
589
|
-
cb(px[i] ?? 0, py[i] ?? 0);
|
|
590
|
-
}
|
|
591
|
-
},
|
|
592
|
-
resize(width, height) {
|
|
593
|
-
const newW = Math.max(1, width);
|
|
594
|
-
const newH = Math.max(1, height);
|
|
595
|
-
for (let i = 0; i < count; i++) {
|
|
596
|
-
px[i] = wrapX(px[i] ?? 0, newW);
|
|
597
|
-
py[i] = wrapY(py[i] ?? 0, newH);
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
};
|
|
601
|
-
return af;
|
|
499
|
+
/**
|
|
500
|
+
* Velocity-Verlet step reusing a cached a(t) — the nbody scheme, ONE accel eval/step. On ENTRY
|
|
501
|
+
* ax/ay must hold a(t) for the current positions; on RETURN they hold a(t+dt) (feed into the next
|
|
502
|
+
* step). axPrev/ayPrev are caller scratch (length >= count). Zero-alloc.
|
|
503
|
+
*
|
|
504
|
+
* Symplectic => bounded energy for conservative forces.
|
|
505
|
+
*/
|
|
506
|
+
function verletStep(count, px, py, vx, vy, dt, accel, ax, ay, axPrev, ayPrev) {
|
|
507
|
+
for (let i = 0; i < count; i++) {
|
|
508
|
+
px[i] = (px[i] ?? 0) + (vx[i] ?? 0) * dt + .5 * (ax[i] ?? 0) * dt * dt;
|
|
509
|
+
py[i] = (py[i] ?? 0) + (vy[i] ?? 0) * dt + .5 * (ay[i] ?? 0) * dt * dt;
|
|
510
|
+
axPrev[i] = ax[i] ?? 0;
|
|
511
|
+
ayPrev[i] = ay[i] ?? 0;
|
|
512
|
+
}
|
|
513
|
+
accel(count, px, py, vx, vy, ax, ay);
|
|
514
|
+
for (let i = 0; i < count; i++) {
|
|
515
|
+
vx[i] = (vx[i] ?? 0) + .5 * ((axPrev[i] ?? 0) + (ax[i] ?? 0)) * dt;
|
|
516
|
+
vy[i] = (vy[i] ?? 0) + .5 * ((ayPrev[i] ?? 0) + (ay[i] ?? 0)) * dt;
|
|
517
|
+
}
|
|
602
518
|
}
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
519
|
+
//#endregion
|
|
520
|
+
//#region src/wave/wave-packet-pool.ts
|
|
521
|
+
const TWO_PI = Math.PI * 2;
|
|
522
|
+
const PACKET_WIDTH = 1.3;
|
|
523
|
+
const ATTEN = 320;
|
|
524
|
+
const LIFE_CUTOFF = 5;
|
|
525
|
+
function createRipplePool(capacity) {
|
|
526
|
+
const cap = Math.max(1, Math.floor(capacity));
|
|
527
|
+
const ox = new Float32Array(cap);
|
|
528
|
+
const oy = new Float32Array(cap);
|
|
529
|
+
const t0 = new Float32Array(cap);
|
|
530
|
+
const amp = new Float32Array(cap);
|
|
531
|
+
const speed = new Float32Array(cap);
|
|
532
|
+
const wavelength = new Float32Array(cap);
|
|
533
|
+
const life = new Float32Array(cap);
|
|
534
|
+
const active = new Uint8Array(cap);
|
|
535
|
+
let live = 0;
|
|
536
|
+
const freeSlot = () => {
|
|
537
|
+
for (let i = 0; i < cap; i++) if (active[i] === 0) return i;
|
|
538
|
+
let oldest = 0;
|
|
539
|
+
let best = Number.POSITIVE_INFINITY;
|
|
540
|
+
for (let i = 0; i < cap; i++) {
|
|
541
|
+
const t = t0[i] ?? 0;
|
|
542
|
+
if (t < best) {
|
|
543
|
+
best = t;
|
|
544
|
+
oldest = i;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
return oldest;
|
|
548
|
+
};
|
|
549
|
+
return {
|
|
550
|
+
capacity: cap,
|
|
551
|
+
get count() {
|
|
552
|
+
return live;
|
|
553
|
+
},
|
|
554
|
+
emit(x, y, now, a, sp, wl, lf) {
|
|
555
|
+
const i = freeSlot();
|
|
556
|
+
if (active[i] === 0) live++;
|
|
557
|
+
ox[i] = x;
|
|
558
|
+
oy[i] = y;
|
|
559
|
+
t0[i] = now;
|
|
560
|
+
amp[i] = a;
|
|
561
|
+
speed[i] = sp;
|
|
562
|
+
wavelength[i] = Math.max(1, wl);
|
|
563
|
+
life[i] = Math.max(.01, lf);
|
|
564
|
+
active[i] = 1;
|
|
565
|
+
},
|
|
566
|
+
heightAt(x, y, now) {
|
|
567
|
+
let h = 0;
|
|
568
|
+
for (let i = 0; i < cap; i++) {
|
|
569
|
+
if (active[i] === 0) continue;
|
|
570
|
+
const age = now - (t0[i] ?? 0);
|
|
571
|
+
if (age < 0) continue;
|
|
572
|
+
const wl = wavelength[i] ?? 1;
|
|
573
|
+
const R = (speed[i] ?? 0) * age;
|
|
574
|
+
const dx = x - (ox[i] ?? 0);
|
|
575
|
+
const dy = y - (oy[i] ?? 0);
|
|
576
|
+
const front = Math.sqrt(dx * dx + dy * dy) - R;
|
|
577
|
+
const width = wl * PACKET_WIDTH;
|
|
578
|
+
const env = Math.exp(-(front * front) / (2 * width * width));
|
|
579
|
+
if (env < .001) continue;
|
|
580
|
+
const tdecay = Math.exp(-age / (life[i] ?? 1));
|
|
581
|
+
const sdecay = 1 / Math.sqrt(1 + R / ATTEN);
|
|
582
|
+
const k = TWO_PI / wl;
|
|
583
|
+
h += (amp[i] ?? 0) * env * tdecay * sdecay * Math.cos(k * front);
|
|
584
|
+
}
|
|
585
|
+
return h;
|
|
586
|
+
},
|
|
587
|
+
prune(now) {
|
|
588
|
+
for (let i = 0; i < cap; i++) {
|
|
589
|
+
if (active[i] === 0) continue;
|
|
590
|
+
if (now - (t0[i] ?? 0) > (life[i] ?? 1) * LIFE_CUTOFF) {
|
|
591
|
+
active[i] = 0;
|
|
592
|
+
live--;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
},
|
|
596
|
+
clear() {
|
|
597
|
+
active.fill(0);
|
|
598
|
+
live = 0;
|
|
599
|
+
}
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
//#endregion
|
|
603
|
+
export { SENSE_DEFAULTS, createNBodySystem, createRipplePool, createScalarField, createStigmergy, decideTurn, leapfrogInto, senseAt, verletStep };
|
|
604
|
+
|
|
614
605
|
//# sourceMappingURL=index.mjs.map
|