{"version":3,"file":"floorPlan.hit.mjs","names":[],"sources":["../../../../package/common/floorPlan/floorPlan.hit.ts"],"sourcesContent":["/**\n * Hit testing against a derived plan.\n *\n * Tolerances arrive in WORLD units and are expected to have been converted from screen\n * pixels by the caller (`HIT_PX / view.zoom`), so a handle stays the same size on screen\n * at any zoom level.\n */\n\nimport { dist, pointToSegment } from './floorPlan.geometry';\nimport { openingGeometry } from './floorPlan.path';\nimport { roomAtPoint } from './floorPlan.rooms';\nimport type { DerivedRoom, PlanFloor, PlanPoint, PlanSelection } from './floorPlan.types';\n\nexport interface HitTolerances {\n  node: number;\n  opening: number;\n  wall: number;\n}\n\n/** Screen-pixel hit radii, divided by `view.zoom` to get world tolerances. */\nexport const HIT_PX: HitTolerances = { node: 10, opening: 9, wall: 8 };\n/** Wider targets for touch, where there is no pixel-accurate pointer. */\nexport const HIT_PX_COARSE: HitTolerances = { node: 18, opening: 16, wall: 14 };\n\nexport function worldTolerances(px: HitTolerances, zoom: number): HitTolerances {\n  const safeZoom = zoom > 0 ? zoom : 1;\n  return { node: px.node / safeZoom, opening: px.opening / safeZoom, wall: px.wall / safeZoom };\n}\n\nexport interface HitTestOptions {\n  /** Restricts what can be hit. Defaults to everything. */\n  accept?: Array<PlanSelection['type']>;\n  excludeIds?: ReadonlySet<string>;\n}\n\n/**\n * Finds what is under `point`, in priority order: node handle, opening, wall, then room\n * fill.\n *\n * The order is what makes editing feel right. A node sits on top of the walls that meet\n * there, and an opening sits on top of the wall it cuts, so the smaller, more specific\n * target always wins over the larger one underneath it.\n */\nexport function hitTest(\n  floor: PlanFloor,\n  rooms: DerivedRoom[],\n  point: PlanPoint,\n  tolerances: HitTolerances,\n  options: HitTestOptions = {},\n): PlanSelection | null {\n  const accepts = (type: PlanSelection['type']) => !options.accept || options.accept.includes(type);\n  const excluded = (id: string) => options.excludeIds?.has(id) ?? false;\n\n  if (accepts('node')) {\n    let best: { id: string; d: number } | null = null;\n    for (const node of Object.values(floor.nodes)) {\n      if (excluded(node.id)) continue;\n      const d = dist(point, node);\n      if (d <= tolerances.node && (!best || d < best.d)) best = { id: node.id, d };\n    }\n    if (best) return { type: 'node', id: best.id };\n  }\n\n  if (accepts('opening')) {\n    let best: { id: string; d: number } | null = null;\n    for (const opening of Object.values(floor.openings)) {\n      if (excluded(opening.id)) continue;\n      const geo = openingGeometry(floor, opening);\n      if (!geo) continue;\n      const d = pointToSegment(point, geo.p1, geo.p2).dist;\n      if (d <= tolerances.opening && (!best || d < best.d)) best = { id: opening.id, d };\n    }\n    if (best) return { type: 'opening', id: best.id };\n  }\n\n  if (accepts('wall')) {\n    let best: { id: string; d: number } | null = null;\n    for (const wall of Object.values(floor.walls)) {\n      if (excluded(wall.id)) continue;\n      const a = floor.nodes[wall.a];\n      const b = floor.nodes[wall.b];\n      if (!a || !b) continue;\n      // Half the wall's own thickness, so a thick wall is grabbable across its body\n      // rather than only along its centreline.\n      const reach = Math.max(tolerances.wall, wall.thickness / 2);\n      const d = pointToSegment(point, a, b).dist;\n      if (d <= reach && (!best || d < best.d)) best = { id: wall.id, d };\n    }\n    if (best) return { type: 'wall', id: best.id };\n  }\n\n  if (accepts('room')) {\n    const room = roomAtPoint(rooms, point);\n    if (room?.roomId && !excluded(room.roomId)) return { type: 'room', id: room.roomId };\n  }\n\n  return null;\n}\n\nexport function selectionsEqual(a: PlanSelection | null, b: PlanSelection | null): boolean {\n  if (a === b) return true;\n  if (!a || !b) return false;\n  return a.type === b.type && a.id === b.id;\n}\n"],"mappings":"+KAoBA,IAAa,EAAwB,CAAE,KAAM,GAAI,QAAS,EAAG,KAAM,CAAE,EAExD,EAA+B,CAAE,KAAM,GAAI,QAAS,GAAI,KAAM,EAAG,EAE9E,SAAgB,EAAgB,EAAmB,EAA6B,CAC9E,IAAM,EAAW,EAAO,EAAI,EAAO,EACnC,MAAO,CAAE,KAAM,EAAG,KAAO,EAAU,QAAS,EAAG,QAAU,EAAU,KAAM,EAAG,KAAO,CAAS,CAC9F,CAgBA,SAAgB,E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