diff --git a/src/app/state.test.ts b/src/app/state.test.ts index 73bb312..4dbacd3 100644 --- a/src/app/state.test.ts +++ b/src/app/state.test.ts @@ -1,8 +1,11 @@ import { describe, it, expect } from 'vitest'; import { createState } from './state'; -import type { Mat3 } from '../types'; +import { estimatePolyWarp } from '../geometry/polywarp'; +import type { Vec2 } from '../types'; -const IDENT: Mat3 = [1, 0, 0, 0, 1, 0, 0, 0, 1]; +// Identity warp: image == machine. +const square: Vec2[] = [[0, 0], [10, 0], [10, 10], [0, 10]]; +const IDENT = estimatePolyWarp(square, square, 1); describe('app state', () => { it('starts with no segments and a zero alignment', () => { @@ -20,12 +23,12 @@ describe('app state', () => { it('projected() returns image-space segments when calibrated', () => { const s = createState(); - s.setHomography(IDENT); + s.setWarp(IDENT); s.loadGcode('G21 G90\nG1 X10 Y0'); s.alignment.tx = 2; const proj = s.projected(); - expect(proj[0]!.points[0]).toEqual([2, 0]); - expect(proj[0]!.points[1]).toEqual([12, 0]); + expect(proj[0]!.points[0]![0]).toBeCloseTo(2, 4); + expect(proj[0]!.points[1]![0]).toBeCloseTo(12, 4); }); it('projected() returns [] when not calibrated', () => { diff --git a/src/app/state.ts b/src/app/state.ts index b08f67c..c57f65a 100644 --- a/src/app/state.ts +++ b/src/app/state.ts @@ -1,13 +1,13 @@ -import type { Segment, Alignment, Mat3 } from '../types'; +import type { Segment, Alignment, PolyWarp } from '../types'; import { parseGcode } from '../gcode/parser'; import { projectSegments } from '../geometry/transform'; export interface AppState { segments: Segment[]; alignment: Alignment; - homography: Mat3 | null; + warp: PolyWarp | null; loadGcode(text: string): string[]; - setHomography(H: Mat3 | null): void; + setWarp(w: PolyWarp | null): void; projected(): Segment[]; } @@ -15,18 +15,18 @@ export function createState(): AppState { const state: AppState = { segments: [], alignment: { tx: 0, ty: 0, rot: 0 }, - homography: null, + warp: null, loadGcode(text: string): string[] { const { segments, warnings } = parseGcode(text); state.segments = segments; return warnings; }, - setHomography(H: Mat3 | null): void { - state.homography = H; + setWarp(w: PolyWarp | null): void { + state.warp = w; }, projected(): Segment[] { - if (!state.homography) return []; - return projectSegments(state.segments, state.alignment, state.homography); + if (!state.warp) return []; + return projectSegments(state.segments, state.alignment, state.warp); }, }; return state;