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