import type { RenderScene2d } from './types'; import type { PackedTilePolyline } from './worker-protocol'; /** Removes tile polylines from a scene and packs them into transferable buffers. */ export function packTilePolylines(scene: RenderScene2d): PackedTilePolyline[] | undefined { const polylines = scene.layers.filter((layer) => layer.type === 'polyline'); if (polylines.length === 0) return undefined; const packed = polylines.map(({ vertices, breaks, ...layer }) => { const coordinates = new Float64Array(vertices.length * 2); for (let index = 0; index < vertices.length; index += 1) { coordinates[index * 2] = vertices[index][0]; coordinates[index * 2 + 1] = vertices[index][1]; } return { coordinates, breaks: Uint32Array.from(breaks), layer }; }); scene.layers = scene.layers.filter((layer) => layer.type !== 'polyline'); return packed; } export function packedPolylineBytes(polylines: PackedTilePolyline[] | undefined): number { return (polylines ?? []).reduce( (bytes, polyline) => bytes + polyline.coordinates.byteLength + polyline.breaks.byteLength, 0 ); }