File size: 1,937 Bytes
e340a84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
from typing import List

import numpy as np

BRANCH_OPTIONS = [
    "Point Head + Pose",
    "Depth Projection + Pose",
]
BRANCH_TO_KEY = {
    "Point Head + Pose": "point_head",
    "Depth Projection + Pose": "depth_projection",
}
DISPLAY_MODE_OPTIONS = [
    "Current Frame",
    "Accumulate to Frame",
    "All Frames",
]


def branch_key(label: str) -> str:
    return BRANCH_TO_KEY.get(label, "point_head")


def session_file(session_dir: str, name: str) -> str:
    return os.path.join(session_dir, name)


def load_metadata(session_dir: str) -> dict:
    with open(session_file(session_dir, "metadata.json"), "r") as f:
        return json.load(f)


def selected_frame_indices(
    num_frames: int, frame_index: int, display_mode: str
) -> List[int]:
    if num_frames <= 0:
        return []
    frame_index = int(np.clip(frame_index, 0, num_frames - 1))
    if display_mode == "Current Frame":
        return [frame_index]
    if display_mode == "Accumulate to Frame":
        return list(range(frame_index + 1))
    return list(range(num_frames))


def as_4x4(w2c):
    w2c = np.asarray(w2c, dtype=np.float64)
    if w2c.shape == (4, 4):
        return w2c
    out = np.eye(4, dtype=np.float64)
    out[:3, :4] = w2c
    return out


_VIEW_ROT = np.array(
    [
        [1.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        [0.0, -1.0, 0.0],
    ],
    dtype=np.float64,
)


def world_to_view(points):
    points = np.asarray(points, dtype=np.float64)
    return points @ _VIEW_ROT.T


def camera_center_from_w2c(w2c):
    c2w = np.linalg.inv(as_4x4(w2c))
    return c2w[:3, 3]


def c2w_in_view_space(w2c, origin_shift=None):
    c2w = np.linalg.inv(as_4x4(w2c))
    out = np.eye(4, dtype=np.float64)
    out[:3, :3] = _VIEW_ROT @ c2w[:3, :3]
    out[:3, 3] = world_to_view(c2w[:3, 3][None])[0]
    if origin_shift is not None:
        out[:3, 3] -= np.asarray(origin_shift, dtype=np.float64)
    return out