File size: 1,384 Bytes
f877698
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import numpy as np
from scipy.spatial.transform import Rotation, Slerp


def interpolate_poses(context_times, context_c2w, target_times) -> np.ndarray:
    t = np.asarray(context_times, dtype=np.float64)
    poses = np.asarray(context_c2w, dtype=np.float64)
    targets = np.atleast_1d(np.asarray(target_times, dtype=np.float64))
    if t.ndim != 1 or len(t) < 2 or poses.shape != (len(t), 4, 4) or np.any(np.diff(t) <= 0):
        raise ValueError('Need >= 2 context poses with strictly increasing times')
    rotations = Rotation.from_matrix(poses[:, :3, :3])
    centres = poses[:, :3, 3]
    out = np.tile(np.eye(4), (len(targets), 1, 1))
    inside = (targets >= t[0]) & (targets <= t[-1])
    if inside.any():
        out[inside, :3, :3] = Slerp(t, rotations)(targets[inside]).as_matrix()
        out[inside, :3, 3] = np.stack([np.interp(targets[inside], t, centres[:, k]) for k in range(3)], -1)
    for mask, a, b in ((targets > t[-1], -2, -1), (targets < t[0], 0, 1)):
        if mask.any():
            alpha = (targets[mask] - t[a]) / (t[b] - t[a])
            relative = (rotations[a].inv() * rotations[b]).as_rotvec()
            out[mask, :3, :3] = (rotations[a] * Rotation.from_rotvec(alpha[:, None] * relative[None])).as_matrix()
            out[mask, :3, 3] = centres[a] + alpha[:, None] * (centres[b] - centres[a])
    return out