refactor: app state stores PolyWarp

This commit is contained in:
sjat 2026-06-11 09:23:42 +02:00
parent dc59f5ed63
commit 0130188416
2 changed files with 16 additions and 13 deletions

View file

@ -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', () => {

View file

@ -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;