182eaafAdd a Physics 3D tab: slow-motion 3D replay of the simulationJeremy Magland 1// Mode shapes of the FE body for the 3D view: for each FE (coupled) mode, the normal displacement
2// of the top and back plate nodes, the cavity pressure on the mid-depth layer of air cells and the
3// displacement of the f-hole air plugs, all per unit modal coordinate (mass-normalised, like
4// BodyMode.by). They line up with the first nModes entries of BodyModel.modes.
5//
6// Binary form (for shipping the default body's shapes): a 4-byte header length, a JSON header,
7// padding to 4 bytes, then the arrays. The plate and air fields are quantised to int8 per mode
8// (one scale for both plates, one for the air), which is plenty for a picture.
10export interface FEShapes {
11 feKey: string;
12 nModes: number;
13 /** the plate grid: node k = i * ns + j at x = x0 + i h, s = s0 + j h */
14 grid: { nx: number; ns: number; h: number; x0: number; s0: number };
15 topNodes: Int32Array;
16 backNodes: Int32Array;
17 /** air cells: horizontal cell (i * nsA + j) -> column or -1; column height (top surface z) */
18 cav: { nxA: number; nsA: number; x0: number; s0: number; ha: number; col: Int32Array; colH: Float32Array };
19 plugs: { xc: number; sc: number; tilt: number; length: number; width: number }[];
20 /** nModes x nTop, nModes x nBack (m per unit eta, +z up), nModes x nCols (Pa per unit eta), nModes x nPlugs (m, outward) */
21 top: Float32Array;
22 back: Float32Array;
23 air: Float32Array;
24 plug: Float32Array;
25}
27export function encodeShapes(s: FEShapes): Uint8Array {
28 const nTop = s.topNodes.length, nBack = s.backNodes.length, nCols = s.cav.colH.length, np = s.plugs.length, n = s.nModes;
29 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 });
30 const hb = new TextEncoder().encode(header);
31 const hLen = 4 + Math.ceil(hb.length / 4) * 4;
32 const f32 = s.topNodes.length + s.backNodes.length + s.cav.col.length + nCols + 2 * n + n * np;
33 const total = hLen + 4 * f32 + n * (nTop + nBack + nCols);
34 const out = new Uint8Array(total);
35 const dv = new DataView(out.buffer);
36 dv.setUint32(0, hb.length, true);
37 out.set(hb, 4);
38 let o = hLen;
39 const putI = (a: ArrayLike<number>) => { for (let i = 0; i < a.length; i++, o += 4) dv.setInt32(o, a[i], true); };
40 const putF = (a: ArrayLike<number>) => { for (let i = 0; i < a.length; i++, o += 4) dv.setFloat32(o, a[i], true); };
41 putI(s.topNodes); putI(s.backNodes); putI(s.cav.col); putF(s.cav.colH);
42 const scP = new Float32Array(n), scA = new Float32Array(n);
43 for (let m = 0; m < n; m++) {
44 let mp = 0, ma = 0;
45 for (let a = 0; a < nTop; a++) mp = Math.max(mp, Math.abs(s.top[m * nTop + a]));
46 for (let a = 0; a < nBack; a++) mp = Math.max(mp, Math.abs(s.back[m * nBack + a]));
47 for (let a = 0; a < nCols; a++) ma = Math.max(ma, Math.abs(s.air[m * nCols + a]));
48 scP[m] = mp / 127; scA[m] = ma / 127;
49 }
50 putF(scP); putF(scA); putF(s.plug);
51 const q = (v: number, sc: number) => (sc > 0 ? Math.max(-127, Math.min(127, Math.round(v / sc))) : 0);
52 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;
53 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;
54 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;
55 return out;
56}
58export function decodeShapes(buf: ArrayBuffer): FEShapes {
59 const dv = new DataView(buf);
60 const hl = dv.getUint32(0, true);
61 const h = JSON.parse(new TextDecoder().decode(new Uint8Array(buf, 4, hl)));
62 let o = 4 + Math.ceil(hl / 4) * 4;
63 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;
64 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; };
65 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; };
66 const topNodes = getI(nTop), backNodes = getI(nBack), col = getI(h.cav.nxA * h.cav.nsA), colH = getF(nCols);
67 const scP = getF(n), scA = getF(n), plug = getF(n * np);
68 const i8 = new Int8Array(buf);
69 const deq = (len: number, sc: Float32Array) => {
70 const a = new Float32Array(n * len);
71 for (let m = 0; m < n; m++) for (let j = 0; j < len; j++) a[m * len + j] = i8[o++] * sc[m];
72 return a;
73 };
74 const top = deq(nTop, scP), back = deq(nBack, scP), air = deq(nCols, scA);
75 return { feKey: h.feKey, nModes: n, grid: h.grid, topNodes, backNodes, cav: { ...h.cav, col, colH }, plugs: h.plugs, top, back, air, plug };
76}