This commit is contained in:
Andras Schmelczer 2026-09-11 16:09:28 +01:00
commit d8f2407ddf

60
src/lib/cut-preview.ts Normal file
View file

@ -0,0 +1,60 @@
import type { FlooringPiece, Point, SkirtingPiece } from './types';
export interface PreviewBox {
x: number;
y: number;
width: number;
height: number;
}
export const exactMm = (value: number) =>
new Intl.NumberFormat('en-GB', { maximumFractionDigits: 3 }).format(value);
export const viewBox = (box: PreviewBox) => `${box.x} ${box.y} ${box.width} ${box.height}`;
export function flooringTemplate(piece: FlooringPiece): Point[] {
if (piece.template.length) return piece.template;
return piece.shaped
? []
: [
{ x: 0, y: 0 },
{ x: piece.length, y: 0 },
{ x: piece.length, y: piece.width },
{ x: 0, y: piece.width },
];
}
export function skirtingTemplate(piece: SkirtingPiece, thickness: number): Point[] {
if (piece.template?.length === 4) return piece.template;
if (piece.leftMitre !== 0 || piece.rightMitre !== 0) return [];
return [
{ x: 0, y: 0 },
{ x: piece.length, y: 0 },
{ x: piece.length, y: thickness },
{ x: 0, y: thickness },
];
}
/** Padding changes the viewport only; all vertices stay in their original millimetres. */
export function fullPreviewBox(length: number, width: number): PreviewBox {
const padding = Math.max(length, width, 1) * 0.06;
return { x: -padding, y: -padding, width: length + 2 * padding, height: width + 2 * padding };
}
/** Equal square windows enlarge both axes equally; a 45° mitre remains 45°. */
export function skirtingEndBoxes(
template: Point[],
thickness: number,
): { left: PreviewBox; right: PreviewBox } {
const [backLeft, backRight, frontRight, frontLeft] = template;
const side = Math.max(
1,
thickness * 3,
Math.abs(backLeft.x - frontLeft.x) / 0.76,
Math.abs(backRight.x - frontRight.x) / 0.76,
);
const padding = side * 0.12;
const common = { y: (thickness - side) / 2, width: side, height: side };
return {
left: { ...common, x: Math.min(backLeft.x, frontLeft.x) - padding },
right: { ...common, x: Math.max(backRight.x, frontRight.x) - side + padding },
};
}