7b06bdfWoodblock strike simulator: modal model, FDTD mode analysis, SoundFont exportJeremy Magland 1// Block geometry, wood material and voxel grid.
2// Axes: x = length (grain, L), y = depth (front face at y=0, T), z = height (up, R).
4export const DEFAULT_BLOCK = {
5 L: 200, W: 70, H: 60, // outer size, mm
6 wall: 8, top: 7, // wall and top-plate thickness, mm
7 slotH: 6, inset: 15, // slot height and end inset, mm
8 rho: 705, EL: 12.6, // density kg/m^3, longitudinal modulus GPa
9};
11export const AIR = { rho: 1.2, c: 343 };
13// Hard-maple-like orthotropic ratios (USDA Wood Handbook style), scaled by E_L.
14export function woodStiffness(EL /* Pa */, scale = 1) {
15 const E = EL * scale;
16 const Ex = E, Ey = 0.065 * E, Ez = 0.132 * E; // L, T, R
17 const nuLT = 0.476, nuLR = 0.424, nuRT = 0.774;
18 const S = [
19 [1 / Ex, -nuLT / Ex, -nuLR / Ex],
20 [-nuLT / Ex, 1 / Ey, -nuRT / Ez],
21 [-nuLR / Ex, -nuRT / Ez, 1 / Ez],
22 ];
23 const C = inv3(S);
24 return {
25 c11: C[0][0], c22: C[1][1], c33: C[2][2],
26 c12: C[0][1], c13: C[0][2], c23: C[1][2],
27 gxy: 0.063 * E, gxz: 0.111 * E, gyz: 0.02 * E,
28 };
29}
31function inv3(m) {
32 const [a, b, c] = m[0], [d, e, f] = m[1], [g, h, i] = m[2];
33 const A = e * i - f * h, B = -(d * i - f * g), Cc = d * h - e * g;
34 const det = a * A + b * B + c * Cc;
35 return [
36 [A / det, -(b * i - c * h) / det, (b * f - c * e) / det],
37 [B / det, (a * i - c * g) / det, -(a * f - c * d) / det],
38 [Cc / det, -(a * h - b * g) / det, (a * e - b * d) / det],
39 ];
40}
42// Is the point (mm, block-local) inside wood?
43export function isWood(b, x, y, z) {
44 if (x < 0 || x > b.L || y < 0 || y > b.W || z < 0 || z > b.H) return false;
45 const zTopIn = b.H - b.top;
46 if (x > b.wall && x < b.L - b.wall && y > b.wall && y < b.W - b.wall && z > b.wall && z < zTopIn) return false;
47 if (x > b.inset && x < b.L - b.inset && y <= b.wall && z > zTopIn - b.slotH && z < zTopIn) return false;
48 return true;
49}
51// Boxes [x0,x1,y0,y1,z0,z1] (mm) that make up the block, for rendering.
52export function blockBoxes(b) {
53 const zt = b.H - b.top, zs = zt - b.slotH;
54 const boxes = [
55 [0, b.L, 0, b.W, 0, b.wall],
56 [0, b.L, 0, b.W, zt, b.H],
57 [0, b.L, b.W - b.wall, b.W, b.wall, zt],
58 [0, b.wall, 0, b.W - b.wall, b.wall, zt],
59 [b.L - b.wall, b.L, 0, b.W - b.wall, b.wall, zt],
60 [b.wall, b.L - b.wall, 0, b.wall, b.wall, zs],
61 ];
62 if (b.inset > b.wall) {
63 boxes.push([b.wall, b.inset, 0, b.wall, zs, zt]);
64 boxes.push([b.L - b.inset, b.L - b.wall, 0, b.wall, zs, zt]);
65 }
66 return boxes.filter((q) => q[1] > q[0] && q[3] > q[2] && q[5] > q[4]);
67}
69export function validateBlock(b) {
70 const e = [];
71 if (b.wall * 2 >= Math.min(b.L, b.W)) e.push('walls too thick');
72 if (b.top + b.slotH + b.wall >= b.H) e.push('top + slot + wall must be < height');
73 if (b.inset < b.wall) e.push('slot inset must be ≥ wall');
74 if (2 * b.inset >= b.L) e.push('slot inset too large');
75 return e;
76}
78export const MIC_OFFSET = 12; // mm in front of the slot
80export function micPos(b) {
81 return [b.L / 2, -MIC_OFFSET, b.H - b.top - b.slotH / 2];
82}
84// Voxelize block + surrounding air. dx in mm.
85export function buildGrid(b, dx, margin = 20, ns = 10) {
86 const mc = Math.ceil(margin / dx); // air margin (mm -> cells); ns = sponge cells
87 const pad = ns + mc;
88 const nbx = Math.ceil(b.L / dx), nby = Math.ceil(b.W / dx), nbz = Math.ceil(b.H / dx);
89 const nx = nbx + 2 * pad, ny = nby + 2 * pad, nz = nbz + 2 * pad;
90 const o = pad * dx; // block origin offset (mm)
91 const N = nx * ny * nz;
92 const mat = new Uint32Array(N);
93 for (let k = 0; k < nz; k++) {
94 const z = (k + 0.5) * dx - o;
95 for (let j = 0; j < ny; j++) {
96 const y = (j + 0.5) * dx - o;
97 for (let i = 0; i < nx; i++) {
98 const x = (i + 0.5) * dx - o;
99 if (isWood(b, x, y, z)) mat[i + nx * (j + ny * k)] = 1;
100 }
101 }
102 }
103 const id = (i, j, k) => i + nx * (j + ny * k);
104 // Top-surface vz nodes: one per block column (node above topmost wood cell).
105 const surf = []; // node indices
106 const surfIJ = []; // [ib, jb] block-column indices
107 const kTop = new Int32Array(nbx * nby).fill(-1);
108 for (let jb = 0; jb < nby; jb++) {
109 for (let ib = 0; ib < nbx; ib++) {
110 const i = ib + pad, j = jb + pad;
111 for (let k = nz - 2; k > 0; k--) {
112 if (mat[id(i, j, k)] === 1) { kTop[ib + nbx * jb] = k; break; }
113 }
114 }
115 }
116 const surfIndex = new Int32Array(nbx * nby).fill(-1);
117 for (let jb = 0; jb < nby; jb++) {
118 for (let ib = 0; ib < nbx; ib++) {
119 const k = kTop[ib + nbx * jb];
120 if (k < 0) continue;
121 surfIndex[ib + nbx * jb] = surf.length;
122 surf.push(id(ib + pad, jb + pad, k));
123 surfIJ.push([ib, jb]);
124 }
125 }
126 const mp = micPos(b);
127 const mi = Math.floor((mp[0] + o) / dx), mj = Math.floor((mp[1] + o) / dx), mk = Math.floor((mp[2] + o) / dx);
128 const micIdx = id(mi, mj, mk);
129 let nWood = 0;
130 for (let n = 0; n < N; n++) nWood += mat[n];
131 return { nx, ny, nz, N, dx, ns, pad, o, nbx, nby, mat, surf: Uint32Array.from(surf), surfIJ, surfIndex, kTop, micIdx, nWood, id };
132}
134// Gaussian force patch around strike point (mm) on the top surface.
135export function strikePatch(grid, sx, sy) {
136 const ib0 = Math.round(sx / grid.dx - 0.5), jb0 = Math.round(sy / grid.dx - 0.5);
137 const idx = [], w = [];
138 for (let dj = -2; dj <= 2; dj++) {
139 for (let di = -2; di <= 2; di++) {
140 const ib = ib0 + di, jb = jb0 + dj;
141 if (ib < 0 || jb < 0 || ib >= grid.nbx || jb >= grid.nby) continue;
142 const s = grid.surfIndex[ib + grid.nbx * jb];
143 if (s < 0) continue;
144 idx.push(grid.surf[s]);
145 w.push(Math.exp(-(di * di + dj * dj) / 2));
146 }
147 }
148 const sum = w.reduce((a, c) => a + c, 0);
149 return { idx: Uint32Array.from(idx), w: Float32Array.from(w.map((v) => v / sum)) };
150}