File size: 2,692 Bytes
326d3cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
"""Time-aware penalties and segment-frame measurements with explicit geometry."""
import numpy as np
from scipy.spatial.transform import Rotation


def contiguous_segments(valid, times, max_gap=None):
    valid, times = np.asarray(valid, bool), np.asarray(times, float)
    result, start = [], None
    for i in range(len(times)):
        supported = valid[i] and np.isfinite(times[i])
        connected = i == 0 or (times[i] > times[i-1] and (max_gap is None or times[i]-times[i-1] <= max_gap))
        if start is not None and (not supported or not connected):
            result.append((start, i)); start = None
        if supported and start is None:
            start = i
    if start is not None:
        result.append((start, len(times)))
    return result


def angular_velocity(rotations, times, valid=None):
    """World angular-velocity vectors from rotation logarithms, never Euler derivatives."""
    rotations, times = np.asarray(rotations), np.asarray(times, float)
    valid = np.isfinite(rotations).all(axis=(1, 2)) if valid is None else np.asarray(valid, bool)
    velocity = np.full((len(times), 3), np.nan)
    for a, b in contiguous_segments(valid, times):
        if b-a < 3:
            continue
        intervals = Rotation.from_matrix(rotations[a+1:b] @ rotations[a:b-1].transpose(0, 2, 1)).as_rotvec()
        rates = intervals / np.diff(times[a:b])[:, None]
        velocity[a], velocity[b-1] = rates[0], rates[-1]
        left, right = np.diff(times[a:b])[:-1], np.diff(times[a:b])[1:]
        velocity[a+1:b-1] = (rates[:-1]*right[:, None] + rates[1:]*left[:, None])/(left+right)[:, None]
    return velocity


def heading(rotations, forward_axis=2):
    vector = np.asarray(rotations)[..., :, forward_axis]
    result = np.arctan2(vector[..., 2], vector[..., 0])
    return np.where(np.linalg.norm(vector[..., [0, 2]], axis=-1) > 1e-8, result, np.nan)


def wrapped_difference(a, b):
    return np.arctan2(np.sin(np.asarray(a)-b), np.cos(np.asarray(a)-b))


def sparse_temporal_pattern(frame_count, coordinate_count, marker_count):
    from scipy.sparse import lil_matrix
    rows = frame_count*marker_count*3 + max(frame_count-2, 0)*coordinate_count
    pattern = lil_matrix((rows, frame_count*coordinate_count), dtype=int)
    for frame in range(frame_count):
        pattern[frame*marker_count*3:(frame+1)*marker_count*3,
                frame*coordinate_count:(frame+1)*coordinate_count] = 1
    offset = frame_count*marker_count*3
    for frame in range(max(frame_count-2, 0)):
        pattern[offset+frame*coordinate_count:offset+(frame+1)*coordinate_count,
                frame*coordinate_count:(frame+3)*coordinate_count] = 1
    return pattern.tocsr()