import type { Annotation } from '@faunosteo/core-wasm/types' import { defaultRadius, round2, type Frame } from './frame' const pointRadii = ( annotations: readonly Annotation[], viewId: string, ): number[] => annotations.flatMap((a) => a.kind === 'point' && a.view_id === viewId && a.geometry?.type === 'circle' ? [a.geometry.r] : [], ) /** The slider's stops: the distinct radii of the view's points, ascending (links excluded). */ export function sizeStops( annotations: readonly Annotation[], viewId: string, ): number[] { return [...new Set(pointRadii(annotations, viewId))].sort((a, b) => a - b) } /** A5: the radius used most in the view, the smaller on a tie; null without any point. */ export function mostUsedSize( annotations: readonly Annotation[], viewId: string, ): number | null { const counts = new Map() for (const r of pointRadii(annotations, viewId)) counts.set(r, (counts.get(r) ?? 0) + 1) let best: [number, number] | null = null for (const [r, n] of counts) { if (!best || n > best[1] || (n === best[1] && r < best[0])) best = [r, n] } return best?.[0] ?? null } /** The nearest stop within `reach` drawing units, else the value on the 0.01 grid. */ export function snapSize( value: number, stops: readonly number[], reach: number, ): number { let nearest: number | null = null for (const stop of stops) { const closer = nearest === null || Math.abs(stop - value) < Math.abs(nearest - value) if (Math.abs(stop - value) <= reach && closer) nearest = stop } return nearest ?? round2(value) } /** * A5: the size of a new point on every path (click, C26, keyboard): the strip's choice, * else the view's most used size, else F's fallback. */ export function newPointSize( annotations: readonly Annotation[], viewId: string, frame: Frame, chosen: number | null, ): number { return chosen ?? mostUsedSize(annotations, viewId) ?? defaultRadius(frame) } export interface SizeRange { low: number high: number } /** R-10: around the stops when there are some, around the fallback radius otherwise. */ export function sizeRange( stops: readonly number[], minimum: number | null, fallback: number, ): SizeRange { const smallest = stops.length > 0 ? stops[0] : fallback const largest = stops.length > 0 ? stops[stops.length - 1] : fallback const base = minimum === null ? smallest : Math.min(smallest, minimum) return { low: round2(Math.max(0.01, base / 2)), high: round2(2 * largest) } }