// Block geometry, wood material and voxel grid. // Axes: x = length (grain, L), y = depth (front face at y=0, T), z = height (up, R). export const DEFAULT_BLOCK = { L: 200, W: 70, H: 60, // outer size, mm wall: 8, top: 7, // wall and top-plate thickness, mm slotH: 6, inset: 15, // slot height and end inset, mm rho: 705, EL: 12.6, // density kg/m^3, longitudinal modulus GPa }; export const AIR = { rho: 1.2, c: 343 }; // Hard-maple-like orthotropic ratios (USDA Wood Handbook style), scaled by E_L. export function woodStiffness(EL /* Pa */, scale = 1) { const E = EL * scale; const Ex = E, Ey = 0.065 * E, Ez = 0.132 * E; // L, T, R const nuLT = 0.476, nuLR = 0.424, nuRT = 0.774; const S = [ [1 / Ex, -nuLT / Ex, -nuLR / Ex], [-nuLT / Ex, 1 / Ey, -nuRT / Ez], [-nuLR / Ex, -nuRT / Ez, 1 / Ez], ]; const C = inv3(S); return { c11: C[0][0], c22: C[1][1], c33: C[2][2], c12: C[0][1], c13: C[0][2], c23: C[1][2], gxy: 0.063 * E, gxz: 0.111 * E, gyz: 0.02 * E, }; } function inv3(m) { const [a, b, c] = m[0], [d, e, f] = m[1], [g, h, i] = m[2]; const A = e * i - f * h, B = -(d * i - f * g), Cc = d * h - e * g; const det = a * A + b * B + c * Cc; return [ [A / det, -(b * i - c * h) / det, (b * f - c * e) / det], [B / det, (a * i - c * g) / det, -(a * f - c * d) / det], [Cc / det, -(a * h - b * g) / det, (a * e - b * d) / det], ]; } // Is the point (mm, block-local) inside wood? export function isWood(b, x, y, z) { if (x < 0 || x > b.L || y < 0 || y > b.W || z < 0 || z > b.H) return false; const zTopIn = b.H - b.top; if (x > b.wall && x < b.L - b.wall && y > b.wall && y < b.W - b.wall && z > b.wall && z < zTopIn) return false; if (x > b.inset && x < b.L - b.inset && y <= b.wall && z > zTopIn - b.slotH && z < zTopIn) return false; return true; } // Boxes [x0,x1,y0,y1,z0,z1] (mm) that make up the block, for rendering. export function blockBoxes(b) { const zt = b.H - b.top, zs = zt - b.slotH; const boxes = [ [0, b.L, 0, b.W, 0, b.wall], [0, b.L, 0, b.W, zt, b.H], [0, b.L, b.W - b.wall, b.W, b.wall, zt], [0, b.wall, 0, b.W - b.wall, b.wall, zt], [b.L - b.wall, b.L, 0, b.W - b.wall, b.wall, zt], [b.wall, b.L - b.wall, 0, b.wall, b.wall, zs], ]; if (b.inset > b.wall) { boxes.push([b.wall, b.inset, 0, b.wall, zs, zt]); boxes.push([b.L - b.inset, b.L - b.wall, 0, b.wall, zs, zt]); } return boxes.filter((q) => q[1] > q[0] && q[3] > q[2] && q[5] > q[4]); } export function validateBlock(b) { const e = []; if (b.wall * 2 >= Math.min(b.L, b.W)) e.push('walls too thick'); if (b.top + b.slotH + b.wall >= b.H) e.push('top + slot + wall must be < height'); if (b.inset < b.wall) e.push('slot inset must be ≥ wall'); if (2 * b.inset >= b.L) e.push('slot inset too large'); return e; } export const MIC_OFFSET = 12; // mm in front of the slot export function micPos(b) { return [b.L / 2, -MIC_OFFSET, b.H - b.top - b.slotH / 2]; } // Voxelize block + surrounding air. dx in mm. export function buildGrid(b, dx, margin = 20, ns = 10) { const mc = Math.ceil(margin / dx); // air margin (mm -> cells); ns = sponge cells const pad = ns + mc; const nbx = Math.ceil(b.L / dx), nby = Math.ceil(b.W / dx), nbz = Math.ceil(b.H / dx); const nx = nbx + 2 * pad, ny = nby + 2 * pad, nz = nbz + 2 * pad; const o = pad * dx; // block origin offset (mm) const N = nx * ny * nz; const mat = new Uint32Array(N); for (let k = 0; k < nz; k++) { const z = (k + 0.5) * dx - o; for (let j = 0; j < ny; j++) { const y = (j + 0.5) * dx - o; for (let i = 0; i < nx; i++) { const x = (i + 0.5) * dx - o; if (isWood(b, x, y, z)) mat[i + nx * (j + ny * k)] = 1; } } } const id = (i, j, k) => i + nx * (j + ny * k); // Top-surface vz nodes: one per block column (node above topmost wood cell). const surf = []; // node indices const surfIJ = []; // [ib, jb] block-column indices const kTop = new Int32Array(nbx * nby).fill(-1); for (let jb = 0; jb < nby; jb++) { for (let ib = 0; ib < nbx; ib++) { const i = ib + pad, j = jb + pad; for (let k = nz - 2; k > 0; k--) { if (mat[id(i, j, k)] === 1) { kTop[ib + nbx * jb] = k; break; } } } } const surfIndex = new Int32Array(nbx * nby).fill(-1); for (let jb = 0; jb < nby; jb++) { for (let ib = 0; ib < nbx; ib++) { const k = kTop[ib + nbx * jb]; if (k < 0) continue; surfIndex[ib + nbx * jb] = surf.length; surf.push(id(ib + pad, jb + pad, k)); surfIJ.push([ib, jb]); } } const mp = micPos(b); const mi = Math.floor((mp[0] + o) / dx), mj = Math.floor((mp[1] + o) / dx), mk = Math.floor((mp[2] + o) / dx); const micIdx = id(mi, mj, mk); let nWood = 0; for (let n = 0; n < N; n++) nWood += mat[n]; return { nx, ny, nz, N, dx, ns, pad, o, nbx, nby, mat, surf: Uint32Array.from(surf), surfIJ, surfIndex, kTop, micIdx, nWood, id }; } // Gaussian force patch around strike point (mm) on the top surface. export function strikePatch(grid, sx, sy) { const ib0 = Math.round(sx / grid.dx - 0.5), jb0 = Math.round(sy / grid.dx - 0.5); const idx = [], w = []; for (let dj = -2; dj <= 2; dj++) { for (let di = -2; di <= 2; di++) { const ib = ib0 + di, jb = jb0 + dj; if (ib < 0 || jb < 0 || ib >= grid.nbx || jb >= grid.nby) continue; const s = grid.surfIndex[ib + grid.nbx * jb]; if (s < 0) continue; idx.push(grid.surf[s]); w.push(Math.exp(-(di * di + dj * dj) / 2)); } } const sum = w.reduce((a, c) => a + c, 0); return { idx: Uint32Array.from(idx), w: Float32Array.from(w.map((v) => v / sum)) }; }