Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123import { describe, expect, it } from "vitest";import { cuboid, facingChain, inset, near, paintOrder, prism, project, rect, sketch, span} from "#lib/components/landing/selfhost/electronics/iso.ts";
const named = (id: string, min: [number, number, number], max: [number, number, number]) => ({ id, ...cuboid(min, max)});
describe("project", () => { it("sends x down to the right, y down to the left and z straight up", () => { const [ux, uy] = project(1, 0, 0); const [vx, vy] = project(0, 1, 0); expect(ux).toBeGreaterThan(0); expect(uy).toBeGreaterThan(0); expect(vx).toBeLessThan(0); expect(vy).toBeGreaterThan(0); expect(project(0, 0, 1)).toEqual([0, -1]); });});
describe("rects and boxes", () => { it("insets every side", () => { expect(inset(span(0, 0, 10, 6), 2)).toEqual(span(2, 2, 8, 4)); });
it("keeps a box's heights when insetting its footprint", () => { expect(inset(cuboid([0, 0, 1], [4, 4, 3]), 1)).toEqual(cuboid([1, 1, 1], [3, 3, 3])); });
it("finds the depth of each visible face", () => { const box = cuboid([0, 0, 0], [1, 2, 3]); expect([near(box, "side"), near(box, "front"), near(box, "top")]).toEqual([1, 2, 3]); });});
describe("facingChain", () => { it("walks the two walls a viewer can see", () => { expect(facingChain(rect(span(0, 0, 1, 1)))).toEqual([ [1, 0], [1, 1], [0, 1] ]); });});
describe("prism", () => { it("splits its fill into the top face and the band of walls under it", () => { const place = (u: number, v: number, d: number) => `${u},${v},${d}`; const { top, band, fill } = prism(rect(span(0, 0, 1, 1)), 0, 2, place); expect(top).toBe("M0,0,2 L1,0,2 L1,1,2 L0,1,2 Z"); expect(band).toBe("M1,0,2 L1,1,2 L0,1,2 L0,1,0 L1,1,0 L1,0,0 Z"); expect(fill).toBe(`${top} ${band}`); });});
describe("paintOrder", () => { it("paints nearer boxes after the ones they cover", () => { const back = named("back", [0, 0, 0], [4, 4, 4]); const right = named("right", [5, 0, 0], [9, 4, 4]); const above = named("above", [0, 0, 5], [4, 4, 9]); const ids = paintOrder([above, right, back]).map((p) => p.id); expect(ids.indexOf("back")).toBeLessThan(ids.indexOf("right")); expect(ids.indexOf("back")).toBeLessThan(ids.indexOf("above")); });});
describe("sketch", () => { const { at, face, mark, polygon } = sketch(100, 100, 2);
it("centres and scales the projection", () => { expect(at(0, 0, 0)).toEqual([100, 100]); expect(at(0, 0, 1)).toEqual([100, 98]); });
it("lays an outline on each face at its depth", () => { const square = rect(span(0, 0, 1, 1)); expect(face("top", square, 5)).toBe( polygon([ [0, 0, 5], [1, 0, 5], [1, 1, 5], [0, 1, 5] ]) ); expect(face("front", square, 5)).toBe( polygon([ [0, 5, 0], [1, 5, 0], [1, 5, 1], [0, 5, 1] ]) ); expect(face("side", square, 5)).toBe( polygon([ [5, 0, 0], [5, 1, 0], [5, 1, 1], [5, 0, 1] ]) ); });
it("centres a mark on its point", () => { const [, , , , ex, ey] = mark("top", [0, 0, 0], 32) .match(/-?[\d.]+/g)! .map(Number); const [cx, cy] = at(-16, -16, 0); expect(ex).toBeCloseTo(cx); expect(ey).toBeCloseTo(cy); });});