"""Small, dependency-light geometry helpers shared by the rules and the engine.""" from __future__ import annotations from typing import Iterable, Sequence import numpy as np BBox = tuple[int, int, int, int] Point = tuple[float, float] def bbox_area(b: Sequence[float]) -> float: return float(max(0.0, b[2] - b[0]) * max(0.0, b[3] - b[1])) def bbox_intersection(a: Sequence[float], b: Sequence[float]) -> float: x1, y1 = max(a[0], b[0]), max(a[1], b[1]) x2, y2 = min(a[2], b[2]), min(a[3], b[3]) return float(max(0.0, x2 - x1) * max(0.0, y2 - y1)) def bbox_iou(a: Sequence[float], b: Sequence[float]) -> float: inter = bbox_intersection(a, b) union = bbox_area(a) + bbox_area(b) - inter return inter / union if union > 0 else 0.0 def containment(inner: Sequence[float], outer: Sequence[float]) -> float: """Fraction of ``inner`` covered by ``outer``.""" area = bbox_area(inner) return bbox_intersection(inner, outer) / area if area > 0 else 0.0 def bbox_center(b: Sequence[float]) -> Point: return ((b[0] + b[2]) / 2.0, (b[1] + b[3]) / 2.0) def bbox_bottom_center(b: Sequence[float]) -> Point: return ((b[0] + b[2]) / 2.0, float(b[3])) def bbox_diag(b: Sequence[float]) -> float: return float(np.hypot(b[2] - b[0], b[3] - b[1])) def expand_bbox(b: Sequence[float], left: float, top: float, right: float, bottom: float, shape: tuple[int, ...] | None = None) -> BBox: """Expand by fractions of box width/height; optionally clip to an image shape.""" w, h = b[2] - b[0], b[3] - b[1] x1, y1, x2, y2 = b[0] - w * left, b[1] - h * top, b[2] + w * right, b[3] + h * bottom if shape is not None: H, W = shape[:2] x1, y1, x2, y2 = max(0, x1), max(0, y1), min(W - 1, x2), min(H - 1, y2) return int(round(x1)), int(round(y1)), int(round(x2)), int(round(y2)) def clip_bbox(b: Sequence[float], shape: tuple[int, ...]) -> BBox: H, W = shape[:2] x1 = int(max(0, min(round(b[0]), W - 1))) y1 = int(max(0, min(round(b[1]), H - 1))) x2 = int(max(0, min(round(b[2]), W - 1))) y2 = int(max(0, min(round(b[3]), H - 1))) return x1, y1, x2, y2 def to_pixels(points: Iterable[Sequence[float]], shape: tuple[int, ...]) -> np.ndarray: """Points may be normalised [0, 1] or absolute pixels; returns float32 pixel array.""" arr = np.asarray(list(points), dtype=np.float32).reshape(-1, 2) if arr.size and np.all(arr >= 0.0) and np.all(arr <= 1.0): H, W = shape[:2] arr = arr * np.array([W, H], dtype=np.float32) return arr def point_in_polygon(p: Point, polygon: np.ndarray) -> bool: """Ray casting; works on float polygons without OpenCV.""" x, y = p inside = False n = len(polygon) j = n - 1 for i in range(n): xi, yi = polygon[i] xj, yj = polygon[j] if (yi > y) != (yj > y): x_cross = (xj - xi) * (y - yi) / ((yj - yi) or 1e-12) + xi if x < x_cross: inside = not inside j = i return inside def signed_side(p: Point, a: Point, b: Point) -> float: """>0 on the left of a->b, <0 on the right (image coordinates), 0 on the line.""" return float((b[0] - a[0]) * (p[1] - a[1]) - (b[1] - a[1]) * (p[0] - a[0])) def distance_to_line(p: Point, a: Point, b: Point) -> float: length = float(np.hypot(b[0] - a[0], b[1] - a[1])) or 1e-9 return abs(signed_side(p, a, b)) / length def projection_t(p: Point, a: Point, b: Point) -> float: """Parameter t of the projection of p onto segment a->b (0..1 inside the segment).""" ab = np.array([b[0] - a[0], b[1] - a[1]], dtype=np.float64) denom = float(ab @ ab) or 1e-9 return float(np.array([p[0] - a[0], p[1] - a[1]]) @ ab / denom) def unit(v: Sequence[float]) -> np.ndarray: arr = np.asarray(v, dtype=np.float64) n = float(np.linalg.norm(arr)) return arr / n if n > 0 else arr