diff --git a/apps/app/src/anatomy/accessLabels.ts b/apps/app/src/anatomy/accessLabels.ts new file mode 100644 index 00000000..a2c58624 --- /dev/null +++ b/apps/app/src/anatomy/accessLabels.ts @@ -0,0 +1,244 @@ +/** D25 (board `docs/design/v1-2-6-u2-detail-views`, revision 4, validated 2026-11-22): where + * each access label goes on a canvas, in CSS px of the canvas box. The board's + * `placeTabsStrict`, ported, plus the accesses cut by the visible edge (panel 17) and the room + * of « Autres vues (N) ». Pure: the page measures, this decides, the same way on the editor, + * the catalogue and the bilan. */ + +export interface Box { x: number; y: number; w: number; h: number } +/** A point marker to keep clear of (its disc and hit ring), or a handle. */ +export interface Disc { cx: number; cy: number; r: number } +export interface Segment { x1: number; y1: number; x2: number; y2: number } +export interface LabelRequest { + /** The access annotation id; requests come in the document order of the accesses. */ + id: string + /** The access shape's box on screen. */ + access: Box + /** The label's measured width, text and padding. */ + w: number +} +export interface LabelLayoutInput { + area: Box + labels: readonly LabelRequest[] + obstacles: readonly Disc[] + /** Boxes already used over the canvas, such as the catalogue's zoom buttons (A8). */ + reserved?: readonly Box[] + /** The measured size of the « Autres vues (N) » button. */ + button: { w: number; h: number } +} +export interface PlacedLabel { id: string; box: Box; leader: Segment | null } +export interface LabelLayout { + /** Document order. */ + placed: PlacedLabel[] + /** Ids of the accesses listed in « Autres vues (N) », document order. */ + overflow: string[] + /** Where « Autres vues (N) » goes; null when nothing overflows. */ + button: Box | null +} + +export const LABEL_H = 20 +export const LABEL_MAX_W = 180 +const MARGIN = 4 +const GAP = 3 +const INSET = 4 +const STEP = 2 +const BUTTON_INSET = 6 + +interface XY { x: number; y: number } +interface Wanted { id: string; a: Box; w: number; cut: boolean; order: number } +interface Item { id: string; box: Box; leader: Segment | null; order: number } +interface Room { area: Box; obstacles: readonly Disc[]; taken: Box[] } + +const overlaps = (a: Box, b: Box): boolean => + a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h +const clamp = (v: number, lo: number, hi: number): number => Math.min(Math.max(v, lo), Math.max(lo, hi)) +const centreOf = (a: Box): XY => ({ x: a.x + a.w / 2, y: a.y + a.h / 2 }) + +function hitsDisc(d: Disc, r: Box): boolean { + const dx = Math.max(r.x - d.cx, 0, d.cx - r.x - r.w) + const dy = Math.max(r.y - d.cy, 0, d.cy - r.y - r.h) + return dx * dx + dy * dy < d.r * d.r +} + +function visiblePart(access: Box, area: Box): Box | null { + const x = Math.max(access.x, area.x) + const y = Math.max(access.y, area.y) + const r = Math.min(access.x + access.w, area.x + area.w) + const b = Math.min(access.y + access.h, area.y + area.h) + return r > x && b > y ? { x, y, w: r - x, h: b - y } : null +} + +/** Whether the visible edge cuts the access (zoomed in, panned). */ +function isCut(access: Box, area: Box): boolean { + return access.x < area.x || access.y < area.y || + access.x + access.w > area.x + area.w || access.y + access.h > area.y + area.h +} + +function inside(r: Box, area: Box): boolean { + return r.x >= area.x + MARGIN && r.y >= area.y + MARGIN && + r.x + r.w <= area.x + area.w - MARGIN && r.y + r.h <= area.y + area.h - MARGIN +} + +function free(r: Box, room: Room): boolean { + return inside(r, room.area) && !room.taken.some((t) => overlaps(r, t)) && + !room.obstacles.some((d) => hitsDisc(d, r)) +} + +/** The five places in order: above top-left, above top-right, below bottom-left, below + * bottom-right, inside top-left. */ +function places(a: Box, w: number): Box[] { + const top = a.y - GAP - LABEL_H + const below = a.y + a.h + GAP + return [ + { x: a.x, y: top, w, h: LABEL_H }, + { x: a.x + a.w - w, y: top, w, h: LABEL_H }, + { x: a.x, y: below, w, h: LABEL_H }, + { x: a.x + a.w - w, y: below, w, h: LABEL_H }, + { x: a.x + INSET, y: a.y + INSET, w, h: LABEL_H }, + ] +} + +function fitInto(r: Box, area: Box): Box { + const x = clamp(r.x, area.x + MARGIN, area.x + area.w - MARGIN - r.w) + const y = clamp(r.y, area.y + MARGIN, area.y + area.h - MARGIN - r.h) + return { x, y, w: r.w, h: r.h } +} + +/** Along the access's top edge, left to right. */ +function slid(a: Box, w: number, room: Room): Box | null { + const y = a.y - GAP - LABEL_H + for (let x = a.x; x <= a.x + a.w - w; x += STEP) { + const r = { x, y, w, h: LABEL_H } + if (free(r, room)) return r + } + return null +} + +/** Higher and higher above the access's left edge. */ +function stacked(a: Box, w: number, room: Room): Box | null { + for (let y = a.y - GAP - LABEL_H - STEP; y > room.area.y; y -= STEP) { + const r = { x: a.x, y, w, h: LABEL_H } + if (free(r, room)) return r + } + return null +} + +/** A thin line from the label's nearest point to `anchor`; none when the anchor is under it. */ +function leaderTo(anchor: XY, r: Box): Segment | null { + const sx = clamp(anchor.x, r.x, r.x + r.w) + const sy = clamp(anchor.y, r.y, r.y + r.h) + return sx !== anchor.x || sy !== anchor.y ? { x1: sx, y1: sy, x2: anchor.x, y2: anchor.y } : null +} + +function cornerOf(a: Box, area: Box): XY { + return { + x: clamp(a.x, area.x + MARGIN, area.x + area.w - MARGIN), + y: clamp(a.y, area.y + MARGIN, area.y + area.h - MARGIN), + } +} + +function turn(p: XY, q: XY, r: XY): number { + return (q.x - p.x) * (r.y - p.y) - (q.y - p.y) * (r.x - p.x) +} + +function segmentsCross(s: Segment, t: Segment): boolean { + const p = { x: s.x1, y: s.y1 } + const q = { x: s.x2, y: s.y2 } + const r = { x: t.x1, y: t.y1 } + const u = { x: t.x2, y: t.y2 } + return turn(p, q, r) * turn(p, q, u) < 0 && turn(r, u, p) * turn(r, u, q) < 0 +} + +function segmentHitsBox(s: Segment, b: Box): boolean { + const within = (x: number, y: number) => x > b.x && x < b.x + b.w && y > b.y && y < b.y + b.h + if (within(s.x1, s.y1) || within(s.x2, s.y2)) return true + const c = [{ x: b.x, y: b.y }, { x: b.x + b.w, y: b.y }, { x: b.x + b.w, y: b.y + b.h }, { x: b.x, y: b.y + b.h }] + return c.some((v, i) => segmentsCross(s, { x1: v.x, y1: v.y, x2: c[(i + 1) % 4].x, y2: c[(i + 1) % 4].y })) +} + +/** The visible accesses, document order; an access entirely outside the area gets no label. */ +function wantedOf(labels: readonly LabelRequest[], area: Box): Wanted[] { + const wanted: Wanted[] = [] + labels.forEach((label, order) => { + const seen = visiblePart(label.access, area) + if (seen === null) return + const cut = isCut(label.access, area) + wanted.push({ id: label.id, a: cut ? seen : label.access, w: Math.min(label.w, LABEL_MAX_W), cut, order }) + }) + return wanted +} + +/** The five places, then the slide; a cut access first tries its label held against the + * visible edge, with a leader to the visible part. */ +function firstPlace(it: Wanted, room: Room): Item | null { + const held = it.cut ? [fitInto(places(it.a, it.w)[0], room.area)] : [] + const box = [...held, ...places(it.a, it.w)].find((r) => free(r, room)) ?? slid(it.a, it.w, room) + if (box === null) return null + return { id: it.id, box, leader: it.cut ? leaderTo(centreOf(it.a), box) : null, order: it.order } +} + +/** A label whose leader crosses another leader or another label goes to the list. */ +function dropCrossings(items: Item[], overflow: Wanted[], wanted: readonly Wanted[]): void { + for (let bad = true; bad; ) { + bad = false + for (const it of items) { + const leader = it.leader + if (leader === null) continue + const crosses = items.some((o) => o !== it && + ((o.leader !== null && segmentsCross(leader, o.leader)) || segmentHitsBox(leader, o.box))) + if (!crosses) continue + items.splice(items.indexOf(it), 1) + overflow.push(wanted.find((w) => w.id === it.id) as Wanted) + bad = true + break + } + } +} + +function layout(input: LabelLayoutInput, reserved: Box | null): { placed: PlacedLabel[]; overflow: string[] } { + const taken = [...(input.reserved ?? []), ...(reserved ? [reserved] : [])] + const room: Room = { area: input.area, obstacles: input.obstacles, taken } + const wanted = wantedOf(input.labels, input.area) + const items: Item[] = [] + const needy: Wanted[] = [] + for (const it of wanted) { + const item = firstPlace(it, room) + if (item === null) needy.push(it) + else { room.taken.push(item.box); items.push(item) } + } + const overflow: Wanted[] = [] + needy.sort((p, q) => p.a.x - q.a.x || p.order - q.order) + for (const it of needy) { + const box = stacked(it.a, it.w, room) + if (box === null) { overflow.push(it); continue } + room.taken.push(box) + items.push({ id: it.id, box, leader: leaderTo(cornerOf(it.a, input.area), box), order: it.order }) + } + dropCrossings(items, overflow, wanted) + items.sort((p, q) => p.order - q.order) + overflow.sort((p, q) => p.order - q.order) + return { placed: items.map(({ id, box, leader }) => ({ id, box, leader })), overflow: overflow.map((w) => w.id) } +} + +/** « Autres vues (N) » in the top-right corner, under any reserved box already there (A8). */ +function buttonBox(input: LabelLayoutInput): Box { + const box: Box = { + x: input.area.x + input.area.w - BUTTON_INSET - input.button.w, + y: input.area.y + BUTTON_INSET, + w: input.button.w, + h: input.button.h, + } + for (const r of [...(input.reserved ?? [])].sort((p, q) => p.y - q.y)) { + if (overlaps(box, r)) box.y = r.y + r.h + BUTTON_INSET + } + return box +} + +/** D25: every label placed or listed, never lost. When some must go to the list, the corner + * of « Autres vues (N) » is reserved and the placement runs once more around it. */ +export function placeAccessLabels(input: LabelLayoutInput): LabelLayout { + const first = layout(input, null) + if (first.overflow.length === 0) return { ...first, button: null } + const button = buttonBox(input) + const second = layout(input, button) + return { ...second, button: second.overflow.length > 0 ? button : null } +} diff --git a/apps/app/tests/fixtures/d25/panel-15.json b/apps/app/tests/fixtures/d25/panel-15.json new file mode 100644 index 00000000..7c0c6b94 --- /dev/null +++ b/apps/app/tests/fixtures/d25/panel-15.json @@ -0,0 +1,32 @@ +{ + "area": {"x": 0, "y": 0, "w": 343, "h": 257}, + "labels": [ + {"id": "Tête (vue latérale gauche)", "access": {"x": 33.852, "y": 23.659, "w": 45.741, "h": 50.157}, "w": 165.093}, + {"id": "Épaule", "access": {"x": 107.983, "y": 119.399, "w": 47.318, "h": 47.318}, "w": 58.904}, + {"id": "Coude", "access": {"x": 115.712, "y": 178.704, "w": 18.927, "h": 18.927}, "w": 57.248}, + {"id": "Genou antérieur", "access": {"x": 118.393, "y": 192.427, "w": 14.195, "h": 12.618}, "w": 113.997}, + {"id": "Boulet antérieur", "access": {"x": 109.718, "y": 193.058, "w": 28.391, "h": 43.533}, "w": 113.073} + ], + "obstacles": [ + {"cx": 248.203, "cy": 220.344, "r": 15}, + {"cx": 251.199, "cy": 182.805, "r": 15}, + {"cx": 238.897, "cy": 144.793, "r": 15}, + {"cx": 257.982, "cy": 96.529, "r": 15}, + {"cx": 236.688, "cy": 91.166, "r": 15}, + {"cx": 131.642, "cy": 143.058, "r": 15}, + {"cx": 125.183, "cy": 188.168, "r": 15}, + {"cx": 125.491, "cy": 197.474, "r": 15}, + {"cx": 124.072, "cy": 217.505, "r": 15}, + {"cx": 131.642, "cy": 216.243, "r": 15}, + {"cx": 232.588, "cy": 81.229, "r": 15}, + {"cx": 184.323, "cy": 83.28, "r": 15}, + {"cx": 144.418, "cy": 83.28, "r": 15} + ], + "expected": { + "placed": [ + {"id": "Tête (vue latérale gauche)", "box": {"x": 37.852, "y": 27.659, "w": 165.093, "h": 20}, "leader": null}, + {"id": "Boulet antérieur", "box": {"x": 109.718, "y": 108.058, "w": 113.073, "h": 20}, "leader": {"x1": 109.718, "y1": 128.058, "x2": 109.718, "y2": 193.058}} + ], + "overflow": ["Épaule", "Coude", "Genou antérieur"] + } +} diff --git a/apps/app/tests/unit/anatomy.accessLabels.test.ts b/apps/app/tests/unit/anatomy.accessLabels.test.ts new file mode 100644 index 00000000..0e632282 --- /dev/null +++ b/apps/app/tests/unit/anatomy.accessLabels.test.ts @@ -0,0 +1,214 @@ +import { describe, expect, it } from 'vitest' +import panel15 from '../fixtures/d25/panel-15.json' +import { + LABEL_MAX_W, + placeAccessLabels, + type Box, + type Disc, + type LabelLayout, + type LabelRequest, + type Segment, +} from '@/anatomy/accessLabels' + +const AREA: Box = { x: 0, y: 0, w: 400, h: 300 } +const BUTTON = { w: 110, h: 28 } + +function run(labels: LabelRequest[], obstacles: Disc[] = [], reserved?: Box[], area = AREA) { + return placeAccessLabels({ area, labels, obstacles, reserved, button: BUTTON }) +} + +const one = (access: Box, w: number, id = 'a'): LabelRequest[] => [{ id, access, w }] +const box = (x: number, y: number, w: number, h = 20): Box => ({ x, y, w, h }) + +describe('placeAccessLabels', () => { + it('places a label free above the access', () => { + const out = run(one({ x: 100, y: 100, w: 60, h: 40 }, 50)) + expect(out.placed).toEqual([{ id: 'a', box: box(100, 77, 50), leader: null }]) + expect(out.overflow).toEqual([]) + expect(out.button).toBeNull() + }) + + it('takes above right when a point blocks above left', () => { + const out = run(one({ x: 100, y: 100, w: 60, h: 40 }, 50), [{ cx: 98, cy: 87, r: 6 }]) + expect(out.placed[0].box).toEqual(box(110, 77, 50)) + }) + + it('goes below left when there is no room above', () => { + const out = run(one({ x: 100, y: 10, w: 60, h: 40 }, 50)) + expect(out.placed[0].box).toEqual(box(100, 53, 50)) + }) + + it('goes inside a large access', () => { + const out = run(one({ x: 100, y: 10, w: 120, h: 270 }, 50)) + expect(out.placed[0].box).toEqual(box(104, 14, 50)) + expect(out.placed[0].leader).toBeNull() + }) + + it('slides along the top edge', () => { + const discs: Disc[] = [ + { cx: 102, cy: 87, r: 6 }, { cx: 255, cy: 87, r: 6 }, + { cx: 100, cy: 152, r: 6 }, { cx: 258, cy: 152, r: 6 }, { cx: 110, cy: 112, r: 6 }, + ] + const out = run(one({ x: 100, y: 100, w: 160, h: 40 }, 50), discs) + expect(out.placed).toEqual([{ id: 'a', box: box(108, 77, 50), leader: null }]) + }) + + const STACK_ACCESS: Box = { x: 200, y: 200, w: 40, h: 30 } + const STACK_DISCS: Disc[] = [ + { cx: 210, cy: 185, r: 12 }, { cx: 235, cy: 185, r: 12 }, + { cx: 210, cy: 245, r: 12 }, { cx: 230, cy: 214, r: 6 }, + ] + + it('stacks higher with a leader', () => { + const out = run(one(STACK_ACCESS, 60), STACK_DISCS) + expect(out.placed).toEqual([ + { id: 'a', box: box(200, 153, 60), leader: { x1: 200, y1: 173, x2: 200, y2: 200 } }, + ]) + }) + + it('lists the access when no place exists', () => { + const out = run(one(STACK_ACCESS, 60), [{ cx: 210, cy: 185, r: 200 }]) + expect(out.placed).toEqual([]) + expect(out.overflow).toEqual(['a']) + expect(out.button).toEqual({ x: 284, y: 6, w: 110, h: 28 }) + }) + + it('holds a label cut by the edge against it (panel 17)', () => { + const out = run(one({ x: -20, y: -10, w: 80, h: 60 }, 90)) + expect(out.placed).toEqual([ + { id: 'a', box: box(4, 4, 90), leader: { x1: 30, y1: 24, x2: 30, y2: 25 } }, + ]) + }) + + it('ignores an access entirely off screen', () => { + const out = run(one({ x: 500, y: 100, w: 60, h: 40 }, 50)) + expect(out.placed).toEqual([]) + expect(out.overflow).toEqual([]) + }) + + it('caps a long name at the maximum width', () => { + const out = run(one({ x: 100, y: 100, w: 60, h: 40 }, 260)) + expect(out.placed[0].box.w).toBe(LABEL_MAX_W) + }) + + it('moves the button under a reserved corner (A8)', () => { + const labels: LabelRequest[] = [ + { id: 'a', access: STACK_ACCESS, w: 60 }, + { id: 'b', access: { x: 300, y: 30, w: 60, h: 40 }, w: 50 }, + ] + const out = run(labels, [{ cx: 210, cy: 185, r: 200 }], [{ x: 252, y: 8, w: 140, h: 44 }]) + expect(out.overflow).toEqual(['a', 'b']) + expect(out.button).toEqual({ x: 284, y: 58, w: 110, h: 28 }) + }) + + it('reproduces the board on panel 15', () => { + const { expected, ...input } = panel15 + const out = placeAccessLabels({ ...input, button: BUTTON }) + expect(out.overflow).toEqual(expected.overflow) + expect(out.button).toEqual({ x: 227, y: 6, w: 110, h: 28 }) + expect(out.placed).toHaveLength(expected.placed.length) + out.placed.forEach((p, i) => { + const e = expected.placed[i] + expect(p.id).toBe(e.id) + expectClose(p.box, e.box) + if (e.leader === null) expect(p.leader).toBeNull() + else expectClose(p.leader!, e.leader) + }) + }) +}) + +function expectClose(actual: Box | Segment, wanted: Box | Segment) { + const w = wanted as unknown as Record + const a = actual as unknown as Record + for (const key of Object.keys(w)) expect(Math.abs(a[key] - w[key])).toBeLessThan(0.002) +} + +// Seeded generator (mulberry32) so a failure replays. +function rng(seed: number): () => number { + let s = seed >>> 0 + return () => { + s = (s + 0x6d2b79f5) >>> 0 + let t = s + t = Math.imul(t ^ (t >>> 15), t | 1) + t ^= t + Math.imul(t ^ (t >>> 7), t | 61) + return ((t ^ (t >>> 14)) >>> 0) / 4294967296 + } +} + +const between = (r: () => number, lo: number, hi: number) => lo + r() * (hi - lo) + +function randomInput(r: () => number) { + const area: Box = { x: 0, y: 0, w: between(r, 200, 1100), h: between(r, 150, 750) } + const labels: LabelRequest[] = [] + const count = 1 + Math.floor(r() * 8) + for (let i = 0; i < count; i++) { + const w = between(r, 10, 160) + const h = between(r, 10, 140) + labels.push({ + id: `l${i}`, + access: { x: between(r, -60, area.w + 20), y: between(r, -60, area.h + 20), w, h }, + w: between(r, 40, 200), + }) + } + const obstacles: Disc[] = [] + const discs = Math.floor(r() * 16) + for (let i = 0; i < discs; i++) { + obstacles.push({ cx: between(r, 0, area.w), cy: between(r, 0, area.h), r: between(r, 4, 20) }) + } + return { area, labels, obstacles } +} + +const overlap = (a: Box, b: Box) => + a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h +const visible = (a: Box, area: Box) => + Math.min(a.x + a.w, area.x + area.w) > Math.max(a.x, area.x) && + Math.min(a.y + a.h, area.y + area.h) > Math.max(a.y, area.y) +const discHits = (d: Disc, r: Box) => { + const dx = Math.max(r.x - d.cx, 0, d.cx - r.x - r.w) + const dy = Math.max(r.y - d.cy, 0, d.cy - r.y - r.h) + return dx * dx + dy * dy < d.r * d.r +} +const turn = (p: number[], q: number[], r: number[]) => + (q[0] - p[0]) * (r[1] - p[1]) - (q[1] - p[1]) * (r[0] - p[0]) +const cross = (s: Segment, t: Segment) => { + const [p, q, a, b] = [[s.x1, s.y1], [s.x2, s.y2], [t.x1, t.y1], [t.x2, t.y2]] + return turn(p, q, a) * turn(p, q, b) < 0 && turn(a, b, p) * turn(a, b, q) < 0 +} +function leaderHitsBox(s: Segment, b: Box): boolean { + const c = [[b.x, b.y], [b.x + b.w, b.y], [b.x + b.w, b.y + b.h], [b.x, b.y + b.h]] + const edges = c.map((v, i) => ({ x1: v[0], y1: v[1], x2: c[(i + 1) % 4][0], y2: c[(i + 1) % 4][1] })) + return edges.some((e) => cross(s, e)) +} + +function checkInvariants(input: ReturnType, out: LabelLayout) { + const { area, labels, obstacles } = input + const visibleIds = labels.filter((l) => visible(l.access, area)).map((l) => l.id) + const listed = [...out.placed.map((p) => p.id), ...out.overflow].sort() + expect(listed).toEqual([...visibleIds].sort()) + const boxes = out.placed.map((p) => p.box) + if (out.button) boxes.push(out.button) + for (const p of out.placed) { + expect(p.box.x).toBeGreaterThanOrEqual(area.x + 4 - 1e-9) + expect(p.box.y).toBeGreaterThanOrEqual(area.y + 4 - 1e-9) + expect(p.box.x + p.box.w).toBeLessThanOrEqual(area.x + area.w - 4 + 1e-9) + expect(p.box.y + p.box.h).toBeLessThanOrEqual(area.y + area.h - 4 + 1e-9) + expect(obstacles.some((d) => discHits(d, p.box))).toBe(false) + expect(boxes.filter((b) => b !== p.box && overlap(b, p.box))).toEqual([]) + if (p.leader === null) continue + for (const o of out.placed) { + if (o === p) continue + expect(leaderHitsBox(p.leader, o.box)).toBe(false) + if (o.leader) expect(cross(p.leader, o.leader)).toBe(false) + } + } +} + +describe('placeAccessLabels invariants', () => { + it('holds on 2000 seeded random layouts', () => { + const r = rng(20261122) + for (let i = 0; i < 2000; i++) { + const input = randomInput(r) + checkInvariants(input, placeAccessLabels({ ...input, button: BUTTON })) + } + }) +})