File size: 2,211 Bytes
ae2c68f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""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