// Mode shapes of the FE body for the 3D view: for each FE (coupled) mode, the normal displacement // of the top and back plate nodes, the cavity pressure on the mid-depth layer of air cells and the // displacement of the f-hole air plugs, all per unit modal coordinate (mass-normalised, like // BodyMode.by). They line up with the first nModes entries of BodyModel.modes. // // Binary form (for shipping the default body's shapes): a 4-byte header length, a JSON header, // padding to 4 bytes, then the arrays. The plate and air fields are quantised to int8 per mode // (one scale for both plates, one for the air), which is plenty for a picture. export interface FEShapes { feKey: string; nModes: number; /** the plate grid: node k = i * ns + j at x = x0 + i h, s = s0 + j h */ grid: { nx: number; ns: number; h: number; x0: number; s0: number }; topNodes: Int32Array; backNodes: Int32Array; /** air cells: horizontal cell (i * nsA + j) -> column or -1; column height (top surface z) */ cav: { nxA: number; nsA: number; x0: number; s0: number; ha: number; col: Int32Array; colH: Float32Array }; plugs: { xc: number; sc: number; tilt: number; length: number; width: number }[]; /** nModes x nTop, nModes x nBack (m per unit eta, +z up), nModes x nCols (Pa per unit eta), nModes x nPlugs (m, outward) */ top: Float32Array; back: Float32Array; air: Float32Array; plug: Float32Array; } export function encodeShapes(s: FEShapes): Uint8Array { const nTop = s.topNodes.length, nBack = s.backNodes.length, nCols = s.cav.colH.length, np = s.plugs.length, n = s.nModes; const header = JSON.stringify({ v: 1, feKey: s.feKey, nModes: n, grid: s.grid, cav: { nxA: s.cav.nxA, nsA: s.cav.nsA, x0: s.cav.x0, s0: s.cav.s0, ha: s.cav.ha }, plugs: s.plugs, nTop, nBack, nCols }); const hb = new TextEncoder().encode(header); const hLen = 4 + Math.ceil(hb.length / 4) * 4; const f32 = s.topNodes.length + s.backNodes.length + s.cav.col.length + nCols + 2 * n + n * np; const total = hLen + 4 * f32 + n * (nTop + nBack + nCols); const out = new Uint8Array(total); const dv = new DataView(out.buffer); dv.setUint32(0, hb.length, true); out.set(hb, 4); let o = hLen; const putI = (a: ArrayLike) => { for (let i = 0; i < a.length; i++, o += 4) dv.setInt32(o, a[i], true); }; const putF = (a: ArrayLike) => { for (let i = 0; i < a.length; i++, o += 4) dv.setFloat32(o, a[i], true); }; putI(s.topNodes); putI(s.backNodes); putI(s.cav.col); putF(s.cav.colH); const scP = new Float32Array(n), scA = new Float32Array(n); for (let m = 0; m < n; m++) { let mp = 0, ma = 0; for (let a = 0; a < nTop; a++) mp = Math.max(mp, Math.abs(s.top[m * nTop + a])); for (let a = 0; a < nBack; a++) mp = Math.max(mp, Math.abs(s.back[m * nBack + a])); for (let a = 0; a < nCols; a++) ma = Math.max(ma, Math.abs(s.air[m * nCols + a])); scP[m] = mp / 127; scA[m] = ma / 127; } putF(scP); putF(scA); putF(s.plug); const q = (v: number, sc: number) => (sc > 0 ? Math.max(-127, Math.min(127, Math.round(v / sc))) : 0); for (let m = 0; m < n; m++) for (let a = 0; a < nTop; a++) out[o++] = q(s.top[m * nTop + a], scP[m]) & 255; for (let m = 0; m < n; m++) for (let a = 0; a < nBack; a++) out[o++] = q(s.back[m * nBack + a], scP[m]) & 255; for (let m = 0; m < n; m++) for (let a = 0; a < nCols; a++) out[o++] = q(s.air[m * nCols + a], scA[m]) & 255; return out; } export function decodeShapes(buf: ArrayBuffer): FEShapes { const dv = new DataView(buf); const hl = dv.getUint32(0, true); const h = JSON.parse(new TextDecoder().decode(new Uint8Array(buf, 4, hl))); let o = 4 + Math.ceil(hl / 4) * 4; const n = h.nModes as number, nTop = h.nTop as number, nBack = h.nBack as number, nCols = h.nCols as number, np = (h.plugs as unknown[]).length; const getI = (len: number) => { const a = new Int32Array(len); for (let i = 0; i < len; i++, o += 4) a[i] = dv.getInt32(o, true); return a; }; const getF = (len: number) => { const a = new Float32Array(len); for (let i = 0; i < len; i++, o += 4) a[i] = dv.getFloat32(o, true); return a; }; const topNodes = getI(nTop), backNodes = getI(nBack), col = getI(h.cav.nxA * h.cav.nsA), colH = getF(nCols); const scP = getF(n), scA = getF(n), plug = getF(n * np); const i8 = new Int8Array(buf); const deq = (len: number, sc: Float32Array) => { const a = new Float32Array(n * len); for (let m = 0; m < n; m++) for (let j = 0; j < len; j++) a[m * len + j] = i8[o++] * sc[m]; return a; }; const top = deq(nTop, scP), back = deq(nBack, scP), air = deq(nCols, scA); return { feKey: h.feKey, nModes: n, grid: h.grid, topNodes, backNodes, cav: { ...h.cav, col, colH }, plugs: h.plugs, top, back, air, plug }; }