refactor: app state stores PolyWarp
This commit is contained in:
parent
dc59f5ed63
commit
0130188416
2 changed files with 16 additions and 13 deletions
|
|
@ -1,8 +1,11 @@
|
||||||
import { describe, it, expect } from 'vitest';
|
import { describe, it, expect } from 'vitest';
|
||||||
import { createState } from './state';
|
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', () => {
|
describe('app state', () => {
|
||||||
it('starts with no segments and a zero alignment', () => {
|
it('starts with no segments and a zero alignment', () => {
|
||||||
|
|
@ -20,12 +23,12 @@ describe('app state', () => {
|
||||||
|
|
||||||
it('projected() returns image-space segments when calibrated', () => {
|
it('projected() returns image-space segments when calibrated', () => {
|
||||||
const s = createState();
|
const s = createState();
|
||||||
s.setHomography(IDENT);
|
s.setWarp(IDENT);
|
||||||
s.loadGcode('G21 G90\nG1 X10 Y0');
|
s.loadGcode('G21 G90\nG1 X10 Y0');
|
||||||
s.alignment.tx = 2;
|
s.alignment.tx = 2;
|
||||||
const proj = s.projected();
|
const proj = s.projected();
|
||||||
expect(proj[0]!.points[0]).toEqual([2, 0]);
|
expect(proj[0]!.points[0]![0]).toBeCloseTo(2, 4);
|
||||||
expect(proj[0]!.points[1]).toEqual([12, 0]);
|
expect(proj[0]!.points[1]![0]).toBeCloseTo(12, 4);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('projected() returns [] when not calibrated', () => {
|
it('projected() returns [] when not calibrated', () => {
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
import type { Segment, Alignment, Mat3 } from '../types';
|
import type { Segment, Alignment, PolyWarp } from '../types';
|
||||||
import { parseGcode } from '../gcode/parser';
|
import { parseGcode } from '../gcode/parser';
|
||||||
import { projectSegments } from '../geometry/transform';
|
import { projectSegments } from '../geometry/transform';
|
||||||
|
|
||||||
export interface AppState {
|
export interface AppState {
|
||||||
segments: Segment[];
|
segments: Segment[];
|
||||||
alignment: Alignment;
|
alignment: Alignment;
|
||||||
homography: Mat3 | null;
|
warp: PolyWarp | null;
|
||||||
loadGcode(text: string): string[];
|
loadGcode(text: string): string[];
|
||||||
setHomography(H: Mat3 | null): void;
|
setWarp(w: PolyWarp | null): void;
|
||||||
projected(): Segment[];
|
projected(): Segment[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -15,18 +15,18 @@ export function createState(): AppState {
|
||||||
const state: AppState = {
|
const state: AppState = {
|
||||||
segments: [],
|
segments: [],
|
||||||
alignment: { tx: 0, ty: 0, rot: 0 },
|
alignment: { tx: 0, ty: 0, rot: 0 },
|
||||||
homography: null,
|
warp: null,
|
||||||
loadGcode(text: string): string[] {
|
loadGcode(text: string): string[] {
|
||||||
const { segments, warnings } = parseGcode(text);
|
const { segments, warnings } = parseGcode(text);
|
||||||
state.segments = segments;
|
state.segments = segments;
|
||||||
return warnings;
|
return warnings;
|
||||||
},
|
},
|
||||||
setHomography(H: Mat3 | null): void {
|
setWarp(w: PolyWarp | null): void {
|
||||||
state.homography = H;
|
state.warp = w;
|
||||||
},
|
},
|
||||||
projected(): Segment[] {
|
projected(): Segment[] {
|
||||||
if (!state.homography) return [];
|
if (!state.warp) return [];
|
||||||
return projectSegments(state.segments, state.alignment, state.homography);
|
return projectSegments(state.segments, state.alignment, state.warp);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
return state;
|
return state;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue