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:
2026-11-19 12:38:53 +01:00
parent ef2c9f9f45
commit ee0e1ff575
9 changed files with 430 additions and 3 deletions
+225
View File
@@ -0,0 +1,225 @@
import { useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import {
CROSSING_MIN_LENGTH,
type CrossingDirection,
type CrossingLine,
type FramePoint,
} from "@parking/shared";
import { useLiveFrames } from "./CameraDock.js";
// Draws a camera's crossing line over its live picture — our own version of a Hikvision
// Line Crossing rule, for cameras that have none: a segment, sides A and B, and which
// crossing counts (A→B, B→A, A↔B). Points are normalised to the frame, so the line holds
// at any display size. Side A is the screen-left of p1→p2 (see crossingSide in shared).
type Drag = { end: "p1" | "p2" } | null;
const HANDLE_PX = 14; // grab radius around an end point
export function CrossingLineEditor({
deviceId,
value,
onChange,
}: {
deviceId: string;
value: CrossingLine | null;
onChange: (next: CrossingLine | null) => void;
}) {
const { t } = useTranslation();
const live = useLiveFrames(deviceId, true);
const surface = useRef<SVGSVGElement | null>(null);
const [size, setSize] = useState({ w: 0, h: 0 });
const [drag, setDrag] = useState<Drag>(null);
// While drawing a new line it lives here, so a click-length line never reaches the form.
const [draft, setDraft] = useState<CrossingLine | null>(null);
const line = draft ?? value;
// The overlay sits exactly on the picture (the wrapper is sized by the <img>), so its
// rectangle IS the frame: no letterbox offset to correct for.
useEffect(() => {
const el = surface.current;
if (!el) return;
const ro = new ResizeObserver(([e]) => e && setSize({ w: e.contentRect.width, h: e.contentRect.height }));
ro.observe(el);
return () => ro.disconnect();
}, [live.hasFrame]);
const toFrame = (ev: React.PointerEvent): FramePoint => {
const r = surface.current!.getBoundingClientRect();
const clamp = (v: number) => Math.min(1, Math.max(0, v));
return { x: clamp((ev.clientX - r.left) / r.width), y: clamp((ev.clientY - r.top) / r.height) };
};
const px = (p: FramePoint) => ({ x: p.x * size.w, y: p.y * size.h });
function onDown(ev: React.PointerEvent<SVGSVGElement>) {
if (!live.hasFrame) return;
ev.currentTarget.setPointerCapture(ev.pointerId);
const at = toFrame(ev);
if (value) {
for (const end of ["p1", "p2"] as const) {
const e = px(value[end]);
const a = px(at);
if (Math.hypot(e.x - a.x, e.y - a.y) <= HANDLE_PX) {
setDrag({ end });
return;
}
}
}
// Anywhere else: draw a new line from here, keeping the chosen direction.
setDraft({ p1: at, p2: at, direction: value?.direction ?? "a-to-b" });
setDrag({ end: "p2" });
}
function onMove(ev: React.PointerEvent<SVGSVGElement>) {
if (!drag) return;
const at = toFrame(ev);
if (draft) setDraft({ ...draft, [drag.end]: at });
else if (value) onChange({ ...value, [drag.end]: at });
}
function onUp() {
if (draft && Math.hypot(draft.p2.x - draft.p1.x, draft.p2.y - draft.p1.y) >= CROSSING_MIN_LENGTH) {
onChange(draft);
}
setDraft(null);
setDrag(null);
}
const directions: [CrossingDirection, string][] = [
["a-to-b", "A → B"],
["b-to-a", "B → A"],
["both", "A ↔ B"],
];
return (
<div className="my-2 rounded-term border border-term-border bg-term-bg p-2 text-[0.75rem]">
<div className="font-semibold text-term-text">{t("setup.crossingTitle")}</div>
<p className="hint mt-0.5">{t("setup.crossingHint")}</p>
<div className="relative mt-2 inline-block w-full select-none">
<img
ref={live.imgRefs.editor}
alt=""
draggable={false}
className={live.hasFrame ? "block h-auto w-full" : "hidden"}
/>
{!live.hasFrame && (
<div className="flex aspect-video w-full items-center justify-center rounded bg-term-panel text-term-muted">
{live.status.state === "unavailable" || live.status.state === "retrying"
? live.status.reason
: t("setup.crossingWaiting")}
</div>
)}
{live.hasFrame && (
<svg
ref={surface}
className="absolute inset-0 h-full w-full cursor-crosshair touch-none"
onPointerDown={onDown}
onPointerMove={onMove}
onPointerUp={onUp}
onPointerCancel={onUp}
>
{line && size.w > 0 && <LineOverlay line={line} p1={px(line.p1)} p2={px(line.p2)} />}
</svg>
)}
</div>
<div className="mt-2 flex flex-wrap items-center gap-2">
<div className="flex gap-1" role="radiogroup" aria-label={t("setup.crossingDirection")}>
{directions.map(([d, label]) => (
<button
key={d}
type="button"
role="radio"
aria-checked={value?.direction === d}
disabled={!value}
className={`btn btn-sm ${value?.direction === d ? "btn-primary" : ""}`}
onClick={() => value && onChange({ ...value, direction: d })}
>
{label}
</button>
))}
</div>
<button
type="button"
className="btn btn-sm"
disabled={!value}
onClick={() => value && onChange({ ...value, p1: value.p2, p2: value.p1 })}
>
{t("setup.crossingSwap")}
</button>
<button type="button" className="btn btn-sm" disabled={!value} onClick={() => onChange(null)}>
{t("setup.crossingClear")}
</button>
</div>
{!value && <p className="hint mt-1">{t("setup.crossingNone")}</p>}
</div>
);
}
/** The segment, its end handles, the A/B labels either side, and an arrow across the
* middle pointing the way that counts (double-headed for A↔B). Pixel space. */
function LineOverlay({ line, p1, p2 }: { line: CrossingLine; p1: FramePoint; p2: FramePoint }) {
const dx = p2.x - p1.x;
const dy = p2.y - p1.y;
const len = Math.hypot(dx, dy) || 1;
// Unit normal toward side A: the screen-left of p1→p2 (y down) is (dy, -dx).
const nA = { x: dy / len, y: -dx / len };
const mid = { x: (p1.x + p2.x) / 2, y: (p1.y + p2.y) / 2 };
const at = (k: number) => ({ x: mid.x + nA.x * k, y: mid.y + nA.y * k });
const aSide = at(22);
const bSide = at(-22);
const labelA = at(40);
const labelB = at(-40);
// Arrow runs from the side crossings start on to the side they end on.
const [from, to] = line.direction === "b-to-a" ? [bSide, aSide] : [aSide, bSide];
const both = line.direction === "both";
const stroke = "rgb(250 204 21)"; // amber, readable over daylight and night pictures
return (
<g>
<defs>
<marker id="crossing-head" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="5" markerHeight="5" orient="auto-start-reverse">
<path d="M0,0 L10,5 L0,10 z" fill={stroke} />
</marker>
</defs>
<line x1={p1.x} y1={p1.y} x2={p2.x} y2={p2.y} stroke="black" strokeOpacity={0.5} strokeWidth={5} />
<line x1={p1.x} y1={p1.y} x2={p2.x} y2={p2.y} stroke={stroke} strokeWidth={3} />
<line
x1={from.x}
y1={from.y}
x2={to.x}
y2={to.y}
stroke={stroke}
strokeWidth={3}
markerEnd="url(#crossing-head)"
markerStart={both ? "url(#crossing-head)" : undefined}
/>
{[p1, p2].map((p, i) => (
<circle key={i} cx={p.x} cy={p.y} r={6} fill={stroke} stroke="black" strokeWidth={1.5} />
))}
{(
[
["A", labelA],
["B", labelB],
] as const
).map(([txt, p]) => (
<text
key={txt}
x={p.x}
y={p.y}
textAnchor="middle"
dominantBaseline="central"
fontSize={16}
fontWeight={700}
fill={stroke}
stroke="black"
strokeWidth={3}
paintOrder="stroke"
>
{txt}
</text>
))}
</g>
);
}