import { describe, expect, it } from "vitest"; import { polygon2dUnion, polygon2dDifference, polygon2dIntersection, polygon2dXor, cadBezierArcLength, sampleCadBezier, cadBezierNormal, cadBezierTangent, cadBezierOffset, cadBezierIntersections, applyHorizontalConstraint, applyVerticalConstraint, applyDistanceConstraint, applyParallelConstraint, applyPerpendicularConstraint, applyTangentConstraint, applyConcentricConstraint, type CadBezierCurve, type Point2D, } from "@/lib/cad2dEngine"; describe("2D CAD Engine - Polygon Booleans via polygon-clipping", () => { const squareA: Point2D[] = [ { x: 0, y: 0 }, { x: 20, y: 0 }, { x: 20, y: 20 }, { x: 0, y: 20 }, ]; const squareB: Point2D[] = [ { x: 10, y: 10 }, { x: 30, y: 10 }, { x: 30, y: 30 }, { x: 10, y: 30 }, ]; it("calculates 2D boolean union of two overlapping squares", () => { const unionResult = polygon2dUnion(squareA, squareB); expect(unionResult.length).toBe(1); expect(unionResult[0].outer.length).toBeGreaterThanOrEqual(6); // L-like merged contour }); it("calculates 2D boolean difference (A - B)", () => { const diffResult = polygon2dDifference(squareA, squareB); expect(diffResult.length).toBe(1); // Difference cuts out the overlapping [10,10]->[20,20] area expect(diffResult[0].outer.length).toBeGreaterThanOrEqual(6); }); it("calculates 2D boolean difference creating an inner hole when B is inside A", () => { const innerSquare: Point2D[] = [ { x: 5, y: 5 }, { x: 15, y: 5 }, { x: 15, y: 15 }, { x: 5, y: 15 }, ]; const holeResult = polygon2dDifference(squareA, innerSquare); expect(holeResult.length).toBe(1); expect(holeResult[0].holes.length).toBe(1); expect(holeResult[0].holes[0].length).toBe(4); }); it("calculates 2D boolean intersection (A ∩ B)", () => { const intersectResult = polygon2dIntersection(squareA, squareB); expect(intersectResult.length).toBe(1); expect(intersectResult[0].outer.length).toBe(4); // 10x10 square }); it("calculates 2D boolean XOR", () => { const xorResult = polygon2dXor(squareA, squareB); expect(xorResult.length).toBeGreaterThanOrEqual(1); }); }); describe("2D CAD Engine - Parametric Bezier Engine via bezier-js", () => { const curve: CadBezierCurve = { p0: { x: 0, y: 0 }, p1: { x: 10, y: 30 }, p2: { x: 30, y: 30 }, p3: { x: 40, y: 0 }, }; it("computes exact arc length of cubic Bezier curve", () => { const length = cadBezierArcLength(curve); expect(length).toBeGreaterThan(40); expect(length).toBeLessThan(100); }); it("samples discrete polyline points along the curve", () => { const samples = sampleCadBezier(curve, 10); expect(samples.length).toBe(11); expect(samples[0]).toEqual({ x: 0, y: 0 }); expect(samples[samples.length - 1]).toEqual({ x: 40, y: 0 }); }); it("calculates tangent and normal direction unit vectors", () => { const tangent = cadBezierTangent(curve, 0.5); const normal = cadBezierNormal(curve, 0.5); expect(Math.hypot(tangent.x, tangent.y)).toBeCloseTo(1, 2); expect(Math.hypot(normal.x, normal.y)).toBeCloseTo(1, 2); // Tangent and normal must be orthogonal (dot product = 0) expect(tangent.x * normal.x + tangent.y * normal.y).toBeCloseTo(0, 2); }); it("calculates parallel offset curves for CAD wall/et kalınlığı", () => { const offsets = cadBezierOffset(curve, 5); expect(offsets.length).toBeGreaterThan(0); expect(offsets[0].length).toBeGreaterThan(0); }); it("calculates intersection points between two Bezier curves", () => { const crossingCurve: CadBezierCurve = { p0: { x: 20, y: -10 }, p1: { x: 20, y: 15 }, p2: { x: 20, y: 25 }, p3: { x: 20, y: 40 }, }; const intersections = cadBezierIntersections(curve, crossingCurve); expect(intersections.length).toBeGreaterThanOrEqual(1); expect(intersections[0].x).toBeCloseTo(20, 1); }); }); describe("2D CAD Engine - Geometric Constraints & Snapping", () => { it("enforces horizontal constraint (Y alignment)", () => { const p1 = { x: 10, y: 25 }; const p2 = { x: 50, y: 80 }; const adjusted = applyHorizontalConstraint(p1, p2); expect(adjusted).toEqual({ x: 50, y: 25 }); }); it("enforces vertical constraint (X alignment)", () => { const p1 = { x: 10, y: 25 }; const p2 = { x: 50, y: 80 }; const adjusted = applyVerticalConstraint(p1, p2); expect(adjusted).toEqual({ x: 10, y: 80 }); }); it("enforces fixed distance constraint along direction vector", () => { const p1 = { x: 0, y: 0 }; const p2 = { x: 30, y: 40 }; // Current distance is 50 const adjusted = applyDistanceConstraint(p1, p2, 100); expect(Math.hypot(adjusted.x - p1.x, adjusted.y - p1.y)).toBeCloseTo(100, 3); expect(adjusted.x).toBeCloseTo(60, 3); expect(adjusted.y).toBeCloseTo(80, 3); }); it("enforces parallel constraint between two segments", () => { const fixed: [Point2D, Point2D] = [{ x: 0, y: 0 }, { x: 10, y: 0 }]; // Horizontal line const moving: [Point2D, Point2D] = [{ x: 5, y: 5 }, { x: 15, y: 25 }]; // Slanted line of length ~22.36 const [m1, m2] = applyParallelConstraint(fixed, moving); expect(m1).toEqual({ x: 5, y: 5 }); expect(m2.y).toBeCloseTo(5, 3); // Must be strictly horizontal now }); it("enforces perpendicular constraint (90 degrees)", () => { const fixed: [Point2D, Point2D] = [{ x: 0, y: 0 }, { x: 10, y: 0 }]; // Horizontal line const moving: [Point2D, Point2D] = [{ x: 5, y: 5 }, { x: 20, y: 10 }]; // Slanted const [m1, m2] = applyPerpendicularConstraint(fixed, moving); expect(m1).toEqual({ x: 5, y: 5 }); expect(m2.x).toBeCloseTo(5, 3); // Must be strictly vertical }); it("enforces tangent constraint between circle and line", () => { const center = { x: 50, y: 50 }; const radius = 20; const lineStart = { x: 0, y: 50 }; const [start, tangent] = applyTangentConstraint(center, radius, lineStart); expect(start).toEqual(lineStart); // Distance from circle center to tangent point must equal radius const distToCenter = Math.hypot(tangent.x - center.x, tangent.y - center.y); expect(distToCenter).toBeCloseTo(radius, 2); }); it("enforces concentric constraint", () => { const fixedCenter = { x: 100, y: 200 }; const concentric = applyConcentricConstraint(fixedCenter, 35); expect(concentric.center).toEqual(fixedCenter); expect(concentric.radius).toBe(35); }); });