feat(cameras): crossing line editor in Setup (A→B / B→A / A↔B)
Our own line-crossing rule for cameras that have none: draw a segment over the camera's live picture, sides A and B, and pick which crossing counts. Stored as camera config.crossing (CrossingLine in @parking/shared, points normalised to the frame, side A on the screen-left of p1→p2), validated on save; null clears it. Nothing consumes it yet (passage confirmation). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RZgP5oMncSaVmKRKk1UvN4
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { crossingSide, parseCrossingLine, type CrossingLine } from "./index.js";
|
||||
|
||||
// A horizontal line drawn left→right across the middle of the frame.
|
||||
const line: CrossingLine = { p1: { x: 0.1, y: 0.5 }, p2: { x: 0.9, y: 0.5 }, direction: "a-to-b" };
|
||||
|
||||
describe("crossingSide", () => {
|
||||
it("puts the screen-left of p1→p2 on side A (above a left→right line)", () => {
|
||||
expect(crossingSide(line, { x: 0.5, y: 0.2 })).toBe("a");
|
||||
expect(crossingSide(line, { x: 0.5, y: 0.8 })).toBe("b");
|
||||
});
|
||||
it("swapping the points swaps the sides", () => {
|
||||
const swapped = { ...line, p1: line.p2, p2: line.p1 };
|
||||
expect(crossingSide(swapped, { x: 0.5, y: 0.2 })).toBe("b");
|
||||
});
|
||||
it("is null exactly on the line", () => {
|
||||
expect(crossingSide(line, { x: 0.3, y: 0.5 })).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseCrossingLine", () => {
|
||||
it("accepts a valid line", () => {
|
||||
expect(parseCrossingLine(line)).toEqual(line);
|
||||
});
|
||||
it("rejects out-of-frame points, unknown directions and a click-length line", () => {
|
||||
expect(parseCrossingLine({ ...line, p1: { x: -0.1, y: 0.5 } })).toBeNull();
|
||||
expect(parseCrossingLine({ ...line, direction: "up" })).toBeNull();
|
||||
expect(parseCrossingLine({ ...line, p2: { x: 0.11, y: 0.5 } })).toBeNull();
|
||||
expect(parseCrossingLine(null)).toBeNull();
|
||||
expect(parseCrossingLine({ p1: { x: "0.1", y: 0.5 }, p2: line.p2, direction: "both" })).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -2211,3 +2211,57 @@ export function effectiveModules(entitled: readonly ModuleId[], activated: reado
|
||||
}
|
||||
return MODULE_IDS.filter((id) => on.has(id));
|
||||
}
|
||||
|
||||
// --- Passage confirmation: a crossing line drawn over a camera's view ---------
|
||||
// The fleet cameras have no line-crossing rule of their own, so Setup draws one (like a
|
||||
// Hikvision Line Crossing rule: a segment, sides A and B, and the direction that counts),
|
||||
// and the booth's own tracker judges the crossing. See wiki/concepts/passage-confirmation.md.
|
||||
|
||||
/** Which crossing counts as a passage: A→B, B→A, or either (A↔B). */
|
||||
export const CROSSING_DIRECTIONS = ["a-to-b", "b-to-a", "both"] as const;
|
||||
export type CrossingDirection = (typeof CROSSING_DIRECTIONS)[number];
|
||||
|
||||
/** A point normalised to the camera frame: x right, y down, both 0..1. */
|
||||
export interface FramePoint {
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
||||
/** Stored as camera `config.crossing`. Side A is on the LEFT of p1→p2 as seen on screen;
|
||||
* swapping the sides is swapping the two points. */
|
||||
export interface CrossingLine {
|
||||
p1: FramePoint;
|
||||
p2: FramePoint;
|
||||
direction: CrossingDirection;
|
||||
}
|
||||
|
||||
/** Shortest line accepted (normalised length) — a click, not a drawn line. */
|
||||
export const CROSSING_MIN_LENGTH = 0.03;
|
||||
|
||||
/** A valid `CrossingLine` from untrusted JSON, or null. */
|
||||
export function parseCrossingLine(v: unknown): CrossingLine | null {
|
||||
if (!v || typeof v !== "object") return null;
|
||||
const o = v as Record<string, unknown>;
|
||||
const pt = (p: unknown): FramePoint | null => {
|
||||
if (!p || typeof p !== "object") return null;
|
||||
const { x, y } = p as Record<string, unknown>;
|
||||
if (typeof x !== "number" || typeof y !== "number") return null;
|
||||
if (!(x >= 0 && x <= 1 && y >= 0 && y <= 1)) return null;
|
||||
return { x, y };
|
||||
};
|
||||
const p1 = pt(o.p1);
|
||||
const p2 = pt(o.p2);
|
||||
const direction = o.direction;
|
||||
if (!p1 || !p2) return null;
|
||||
if (!CROSSING_DIRECTIONS.includes(direction as CrossingDirection)) return null;
|
||||
if (Math.hypot(p2.x - p1.x, p2.y - p1.y) < CROSSING_MIN_LENGTH) return null;
|
||||
return { p1, p2, direction: direction as CrossingDirection };
|
||||
}
|
||||
|
||||
/** Which side of the (infinite) line a point lies on, or null exactly on it. Note the
|
||||
* frame's y axis points down: the left of p1→p2 on screen has a negative cross product. */
|
||||
export function crossingSide(line: CrossingLine, pt: FramePoint): "a" | "b" | null {
|
||||
const { p1, p2 } = line;
|
||||
const cross = (p2.x - p1.x) * (pt.y - p1.y) - (p2.y - p1.y) * (pt.x - p1.x);
|
||||
return cross < 0 ? "a" : cross > 0 ? "b" : null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user