33 lines
1.3 KiB
TypeScript
33 lines
1.3 KiB
TypeScript
import { beforeAll, describe, expect, it } from 'vitest';
|
|
import { bounds } from '../src/lib/geometry';
|
|
import { shapeFromPolygons } from '../src/lib/margin';
|
|
import { loadOpenCV, type CV } from '../src/lib/opencv-loader';
|
|
|
|
let cv: CV;
|
|
beforeAll(async () => {
|
|
({ cv } = await loadOpenCV());
|
|
});
|
|
|
|
const outer = [{ x: 40, y: 30 }, { x: 160, y: 30 }, { x: 160, y: 130 }, { x: 40, y: 130 }];
|
|
const hole = [{ x: 90, y: 70 }, { x: 110, y: 70 }, { x: 110, y: 90 }, { x: 90, y: 90 }];
|
|
const near = (actual: number, expected: number) => expect(Math.abs(actual - expected)).toBeLessThanOrEqual(2);
|
|
|
|
describe('shapeFromPolygons', () => {
|
|
it('traces the drawn outline and hole', () => {
|
|
const shape = shapeFromPolygons(cv, 200, 160, { outer, holes: [hole] }, 0)!;
|
|
const b = bounds(shape.outer);
|
|
near(b.minX, 40); near(b.maxX, 160); near(b.minY, 30); near(b.maxY, 130);
|
|
expect(shape.holes).toHaveLength(1);
|
|
});
|
|
|
|
it('applies the margin to outline and hole', () => {
|
|
const shape = shapeFromPolygons(cv, 200, 160, { outer, holes: [hole] }, -5)!;
|
|
near(bounds(shape.outer).minX, 45);
|
|
near(bounds(shape.holes[0]).minX, 84);
|
|
});
|
|
|
|
it('returns null for an outline with fewer than 3 points', () => {
|
|
expect(shapeFromPolygons(cv, 200, 160, { outer: outer.slice(0, 2), holes: [] }, 0)).toBeNull();
|
|
});
|
|
});
|