File size: 3,964 Bytes
91093de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""Read-only metric helpers for the nine Dev15 handover validators."""
from __future__ import annotations

import math

from common import body_speed, final_window, in_aabb, norm, roll_pitch


def planar_travel(frames, body_name: str) -> float:
    first = frames[0]["bodies"][body_name]["position"]
    last = frames[-1]["bodies"][body_name]["position"]
    return math.hypot(last[0] - first[0], last[1] - first[1])


def signed_travel(frames, body_name: str, axis: int) -> float:
    first = frames[0]["bodies"][body_name]["position"][axis]
    last = frames[-1]["bodies"][body_name]["position"][axis]
    return last - first


def lateral_span(frames, body_name: str, travel_axis: int) -> float:
    other = 1 - travel_axis
    values = [frame["bodies"][body_name]["position"][other] for frame in frames]
    return max(values) - min(values)


def height_span(frames, body_name: str) -> float:
    values = [frame["bodies"][body_name]["position"][2] for frame in frames]
    return max(values) - min(values)


def maximum_tilt(frames, body_name: str) -> float:
    return max(
        max(abs(value) for value in roll_pitch(frame["bodies"][body_name]["quaternion"]))
        for frame in frames
    )


def terminal_stopped(frames, body_name: str, hold_s: float, threshold: float) -> bool:
    window = final_window(frames, hold_s)
    return bool(window) and all(body_speed(frame, body_name) <= threshold for frame in window)


def terminal_in_region(frames, body_name: str, hold_s: float, region: dict) -> bool:
    window = final_window(frames, hold_s)
    return bool(window) and all(
        in_aabb(frame["bodies"][body_name]["position"], region) for frame in window
    )


def first_region_time(frames, body_name: str, region: dict):
    for frame in frames:
        if in_aabb(frame["bodies"][body_name]["position"], region):
            return frame["t"]
    return None


def first_axis_progress_time(frames, body_name: str, axis: int, threshold: float, sign: float):
    start = frames[0]["bodies"][body_name]["position"][axis]
    for frame in frames:
        progress = sign * (frame["bodies"][body_name]["position"][axis] - start)
        if progress >= threshold:
            return frame["t"]
    return None


def interpolate_points(points: list[list[float]], phase: float) -> list[float]:
    phase = max(0.0, min(1.0, phase)) * (len(points) - 1)
    left = int(phase)
    right = min(len(points) - 1, left + 1)
    blend = phase - left
    return [
        (1.0 - blend) * points[left][index] + blend * points[right][index]
        for index in range(3)
    ]


def relative_position_in_body_frame(frame, body_name: str, target_name: str) -> list[float]:
    body = frame["bodies"][body_name]
    target = frame["bodies"][target_name]
    vx, vy, vz = [target["position"][i] - body["position"][i] for i in range(3)]
    w, x, y, z = body["quaternion"]
    return [
        (1 - 2 * (y * y + z * z)) * vx + 2 * (x * y + z * w) * vy + 2 * (x * z - y * w) * vz,
        2 * (x * y - z * w) * vx + (1 - 2 * (x * x + z * z)) * vy + 2 * (y * z + x * w) * vz,
        2 * (x * z + y * w) * vx + 2 * (y * z - x * w) * vy + (1 - 2 * (x * x + y * y)) * vz,
    ]


def relative_orientation_in_body_frame(frame, body_name: str, target_name: str) -> list[float]:
    aw, ax, ay, az = frame["bodies"][body_name]["quaternion"]
    bw, bx, by, bz = frame["bodies"][target_name]["quaternion"]
    relative = [
        aw * bw + ax * bx + ay * by + az * bz,
        aw * bx - ax * bw - ay * bz + az * by,
        aw * by + ax * bz - ay * bw - az * bx,
        aw * bz - ax * by + ay * bx - az * bw,
    ]
    magnitude = norm(relative)
    return [value / magnitude for value in relative]


def quaternion_distance(a, b) -> float:
    dot = abs(sum(float(a[index]) * float(b[index]) for index in range(4)))
    return 2.0 * math.acos(max(-1.0, min(1.0, dot)))


def point_distance(a, b) -> float:
    return norm([a[index] - b[index] for index in range(3)])