import { describe, expect, it } from "vitest"; import * as THREE from "three"; import { PlanarIntersectionGenerator, ProjectionGenerator } from "@/lib/projection"; import { computeModelProjectionAsync, computeModelProjectionSync, generateSectionHatchSegments, mapProjectionSegmentsToSheet, } from "@/lib/technicalProjectionBridge"; import type { WorkplaneShape } from "@/types/sketchforge"; function makeBox(id: string, x: number, y: number, z: number, w: number, h: number, d: number, hole = false): WorkplaneShape { return { id, name: id, kind: "box", color: hole ? "#ef4444" : "#0ea5e9", hole, x, z, elevation: y, size: Math.max(w, d), width: w, depth: d, height: h, rotation: 0, }; } function makeCylinder(id: string, x: number, y: number, z: number, dia: number, h: number, hole = false): WorkplaneShape { return { id, name: id, kind: "cylinder", color: hole ? "#ef4444" : "#0ea5e9", hole, x, z, elevation: y, size: dia, width: dia, depth: dia, height: h, rotation: 0, sides: 28, }; } describe("Stage C — three-edge-projection HLR & Section Cut", () => { it("separates visible front faces from occluded rear edges with ProjectionGenerator", () => { const group = new THREE.Group(); const frontMat = new THREE.MeshBasicMaterial(); const upperBox = new THREE.Mesh(new THREE.BoxGeometry(20, 4, 30), frontMat); upperBox.position.set(0, 10, 0); const lowerWideBox = new THREE.Mesh(new THREE.BoxGeometry(60, 4, 20), frontMat); lowerWideBox.position.set(0, 0, 0); group.add(upperBox, lowerWideBox); group.updateMatrixWorld(true); const projector = new ProjectionGenerator(); projector.angleThreshold = 28; const result = projector.generateSync(group); const visibleSegs = result.visibleEdges.getAllSegments(); const hiddenSegs = result.hiddenEdges.getAllSegments(); expect(visibleSegs.length).toBeGreaterThan(4); // Parts of the wider lower box underneath x in [-10, 10] must be classified as hidden expect(hiddenSegs.length).toBeGreaterThan(0); upperBox.geometry.dispose(); lowerWideBox.geometry.dispose(); frontMat.dispose(); }); it("extracts closed cross-section edges with PlanarIntersectionGenerator", () => { const box = new THREE.BoxGeometry(40, 20, 30); const slicer = new PlanarIntersectionGenerator(); slicer.plane.set(new THREE.Vector3(0, 1, 0), 0); const cutLines = slicer.generate(box); const pos = cutLines.getAttribute("position"); expect(pos.count).toBeGreaterThanOrEqual(8); cutLines.dispose(); box.dispose(); }); it("projects stepped flange with through-hole into ISO views and A-A hatched section", () => { const flangeAssembly: WorkplaneShape[] = [ makeCylinder("base-flange", 0, 0, 0, 100, 12, false), makeCylinder("boss-neck", 0, 12, 0, 46, 26, false), makeCylinder("center-bore", 0, 0, 0, 24, 38, true), ]; const sectionProj = computeModelProjectionSync(flangeAssembly, { sectionAAEnabled: true, radialSegments: 24 }); expect(sectionProj.csgApplied).toBe(true); expect(sectionProj.triangleCount).toBeGreaterThan(20); expect(sectionProj.views.front.visible.length).toBeGreaterThan(4); expect(sectionProj.views.front.sectionCut.length).toBeGreaterThan(6); expect(sectionProj.views.top.visible.length).toBeGreaterThan(20); expect(sectionProj.views.side.visible.length).toBeGreaterThan(4); expect(sectionProj.views.isometric.visible.length).toBeGreaterThan(10); // Check A-A section hatch lines stay outside the central Ø24 bore (x in [-12, 12]) const sheetSectionPrims = mapProjectionSegmentsToSheet( sectionProj.views.front, { x: 0, y: 0 }, 1, { includeHidden: false, includeSectionHatch: true }, ); const hatchPrims = sheetSectionPrims.filter((p) => p.layer === "HATCH"); expect(hatchPrims.length).toBeGreaterThan(4); // Every hatch segment must sit cleanly in the solid steel walls (|x| >= 11.5) and never span across x=0 bore for (const h of hatchPrims) { const crossesZero = (h.x < -2 && (h.x2 ?? h.x) > 2) || ((h.x2 ?? h.x) < -2 && h.x > 2); expect(crossesZero).toBe(false); } // Without section A-A, the internal Ø24 bore appears in front view hidden edges const unsectionedProj = computeModelProjectionSync(flangeAssembly, { sectionAAEnabled: false, radialSegments: 24 }); expect(unsectionedProj.views.front.hidden.length).toBeGreaterThan(0); }); it("supports async HLR projection and L-bracket with mounting holes", async () => { const lBracket: WorkplaneShape[] = [ makeBox("base", 0, 0, 0, 100, 12, 60, false), makeBox("wall", -42, 12, 0, 16, 40, 60, false), makeCylinder("hole1", 25, 0, 15, 12, 12, true), ]; const proj = await computeModelProjectionAsync(lBracket, { sectionAAEnabled: true, radialSegments: 20 }); expect(proj.views.top.visible.length).toBeGreaterThan(8); expect(proj.views.front.sectionCut.length).toBeGreaterThan(3); const directHatch = generateSectionHatchSegments(proj.views.front.sectionCut, 6); expect(directHatch.length).toBeGreaterThan(2); }); });