baseball-engine / baseball_batting /coach_geometry.py
github-actions[bot]
Automated Deploy: 83f1f6fa59ef9d22788ee608e3ba12967f91dd5c
ae2c68f
Raw History Blame Contribute Delete
2.21 kB
"""Contract 1.1 geometry shared by video export and reference-data calibration.
Inputs are anatomical joint centres in any fixed orthonormal frame, in metres.
These are kinematic descriptions, not measurements of force or energy transfer.
"""
import numpy as np
EXTENDED_METRICS = (
'torso_inclination_at_stride_start',
'hip_shoulder_separation_at_stride_start',
'pelvis_rotation_to_contact',
'lead_knee_extension_to_contact',
'torso_inclination_at_contact',
'shoulder_rotation_after_contact',
)
FOLLOW_THROUGH_SECONDS = .1
def inclination(hips, shoulders, up):
torso = np.asarray(shoulders) - np.asarray(hips)
vertical = torso @ np.asarray(up)
horizontal = torso - vertical[..., None] * np.asarray(up)
angle = np.degrees(np.arctan2(np.linalg.norm(horizontal, axis=-1), vertical))
return np.where(vertical > .05, angle, np.nan)
def knee_angle(hip, knee, ankle):
a, b = np.asarray(hip) - knee, np.asarray(ankle) - knee
denominator = np.linalg.norm(a, axis=-1) * np.linalg.norm(b, axis=-1)
cosine = np.divide(np.sum(a * b, axis=-1), denominator,
out=np.full_like(denominator, np.nan), where=denominator > 1e-8)
return np.degrees(np.arccos(np.clip(cosine, -1, 1)))
def rotation_amount(headings, start, end):
"""Magnitude of net unwrapped horizontal rotation; never a peak-speed proxy."""
if start is None or end is None or start >= end:
return np.nan
values = np.asarray(headings)[start:end + 1]
if not np.isfinite(values).all():
return np.nan
return float(abs(np.degrees(np.unwrap(np.radians(values))[-1] - np.radians(values[0]))))
def follow_through_frame(times, contact, end):
"""Nearest original sample to contact + 100 ms; a complete interval is required."""
if contact is None:
return None
times = np.asarray(times, float)
if not np.isfinite(times[contact:end + 1]).all() or np.any(np.diff(times[contact:end + 1]) <= 0):
return None
target = times[contact] + FOLLOW_THROUGH_SECONDS
if times[end] < target:
return None
i = int(np.argmin(np.abs(times[contact:end + 1] - target))) + contact
return i if i > contact else None