Initial buildout

This commit is contained in:
Jan Petykiewicz 2026-03-07 08:26:29 -08:00
commit f600b52f32

View file

@ -0,0 +1,80 @@
from __future__ import annotations
from typing import TYPE_CHECKING
import numpy as np
if TYPE_CHECKING:
from shapely.geometry import Polygon
class DangerMap:
"""A pre-computed grid for heuristic proximity costs."""
def __init__(
self,
bounds: tuple[float, float, float, float],
resolution: float = 1.0,
safety_threshold: float = 10.0,
k: float = 1.0,
) -> None:
# bounds: (minx, miny, maxx, maxy)
self.minx, self.miny, self.maxx, self.maxy = bounds
self.resolution = resolution
self.safety_threshold = safety_threshold
self.k = k
# Grid dimensions
self.width_cells = int(np.ceil((self.maxx - self.minx) / self.resolution))
self.height_cells = int(np.ceil((self.maxy - self.miny) / self.resolution))
# Use uint8 for memory efficiency if normalized, or float16/float32.
# Let's use float32 for simplicity and precision in the prototype.
# For a 1000x1000 grid, this is only 4MB.
# For 20000x20000, it's 1.6GB.
self.grid = np.zeros((self.width_cells, self.height_cells), dtype=np.float32)
def precompute(self, obstacles: list[Polygon]) -> None:
"""Pre-compute the proximity costs for the entire grid."""
# For each cell, find distance to nearest obstacle.
# This is a distance transform problem.
# For the prototype, we can use a simpler approach or scipy.ndimage.distance_transform_edt.
from scipy.ndimage import distance_transform_edt
# Create a binary mask of obstacles
mask = np.ones((self.width_cells, self.height_cells), dtype=bool)
# Rasterize obstacles (simplified: mark cells whose center is inside an obstacle)
# This is slow for many obstacles; in a real engine, we'd use a faster rasterizer.
from shapely.geometry import Point
for poly in obstacles:
# Get bounding box in grid coordinates
p_minx, p_miny, p_maxx, p_maxy = poly.bounds
x_start = max(0, int((p_minx - self.minx) / self.resolution))
x_end = min(self.width_cells, int((p_maxx - self.minx) / self.resolution) + 1)
y_start = max(0, int((p_miny - self.miny) / self.resolution))
y_end = min(self.height_cells, int((p_maxy - self.miny) / self.resolution) + 1)
for ix in range(x_start, x_end):
cx = self.minx + (ix + 0.5) * self.resolution
for iy in range(y_start, y_end):
cy = self.miny + (iy + 0.5) * self.resolution
if poly.contains(Point(cx, cy)):
mask[ix, iy] = False
# Distance transform (mask=True for empty space)
distances = distance_transform_edt(mask) * self.resolution
# Proximity cost: k / d^2 if d < threshold, else 0
# To avoid division by zero, we cap distances at a small epsilon (e.g. 0.1um)
safe_distances = np.maximum(distances, 0.1)
self.grid = np.where(distances < self.safety_threshold, self.k / (safe_distances**2), 0.0).astype(np.float32)
def get_cost(self, x: float, y: float) -> float:
"""Get the proximity cost at a specific coordinate."""
ix = int((x - self.minx) / self.resolution)
iy = int((y - self.miny) / self.resolution)
if 0 <= ix < self.width_cells and 0 <= iy < self.height_cells:
return float(self.grid[ix, iy])
return 1e6 # Outside bounds is expensive