26 lines
785 B
Python
26 lines
785 B
Python
# sites/ai_mouse/ai_mouse/_utils.py
|
|
"""Shared utility functions used by both _generator.py and _trainer.py."""
|
|
from __future__ import annotations
|
|
|
|
import numpy as np
|
|
|
|
|
|
def resample_arc(xy: np.ndarray, n_points: int) -> np.ndarray:
|
|
"""Resample a 2-D polyline to exactly n_points via arc-length interpolation.
|
|
|
|
Args:
|
|
xy: (M, 2) array of (x, y) coordinates.
|
|
n_points: desired number of output points.
|
|
|
|
Returns:
|
|
(n_points, 2) array uniformly spaced along cumulative arc length.
|
|
"""
|
|
arc = np.concatenate(
|
|
[[0], np.cumsum(np.linalg.norm(np.diff(xy, axis=0), axis=1))]
|
|
)
|
|
s_new = np.linspace(0, arc[-1], n_points)
|
|
return np.stack(
|
|
[np.interp(s_new, arc, xy[:, 0]), np.interp(s_new, arc, xy[:, 1])],
|
|
axis=1,
|
|
)
|