import { CAP_SEGMENTS, capDimensions, capOutline, capValues, validateLegCap } from "../furniture-leg-cap"; import type { CadKernel, CadParameters } from "./kernel"; /** A closed sole and an open, measured taper; all caps share one print layout. */ export function buildLegCaps(c: CadKernel, input: CadParameters) { validateLegCap(input); const p = capValues(input), d = capDimensions(p), n = CAP_SEGMENTS; const vertices: number[] = [], faces: number[] = []; for (const [x, y] of d.centers) { const start = vertices.length / 3; const rings = [capOutline(p, "foot", p.clearance / 2 + p.wall), capOutline(p, "foot", p.clearance / 2 + p.wall), capOutline(p, "lip", p.clearance / 2 + p.wall), capOutline(p, "lip", p.clearance / 2), capOutline(p, "foot", p.clearance / 2)]; const heights = [0, p.floor, p.floor + p.depth, p.floor + p.depth, p.floor]; rings.forEach((ring, r) => ring.forEach(v => vertices.push(v[0] + x, v[1] + y, heights[r]))); const bottom = vertices.length / 3; vertices.push(x, y, 0); const floor = vertices.length / 3; vertices.push(x, y, p.floor); for (let i = 0; i < n; i++) { const j = (i + 1) % n; faces.push(bottom, start + j, start + i, floor, start + 4 * n + i, start + 4 * n + j); for (let r = 0; r < 4; r++) { const a = start + r * n + i, b = start + r * n + j, u = start + (r + 1) * n + i, v = start + (r + 1) * n + j; faces.push(a, b, v, a, v, u); } } } return c.keep(new c.api.Manifold(new c.api.Mesh({ numProp: 3, vertProperties: new Float32Array(vertices), triVerts: new Uint32Array(faces) }))); }