File size: 4,226 Bytes
917fe92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import os, sys
import numpy as np
import math


def spherical_to_cartesian(sph):

    theta, azimuth, radius = sph

    return np.array([
        radius * np.sin(theta) * np.cos(azimuth),
        radius * np.sin(theta) * np.sin(azimuth),
        radius * np.cos(theta),
    ])


def cartesian_to_spherical(xyz):

    xy = xyz[0]**2 + xyz[1]**2
    radius = np.sqrt(xy + xyz[2]**2)
    theta = np.arctan2(np.sqrt(xy), xyz[2])
    azimuth = np.arctan2(xyz[1], xyz[0])

    return np.array([theta, azimuth, radius])


def relative_spherical(xyz_target, xyz_cond):

    sp_target = cartesian_to_spherical(xyz_target)
    sp_cond = cartesian_to_spherical(xyz_cond)

    theta_cond, azimuth_cond, z_cond = sp_cond
    theta_target, azimuth_target, z_target = sp_target

    d_theta = theta_target - theta_cond
    d_azimuth = (azimuth_target - azimuth_cond) % (2 * math.pi)
    d_z = z_target - z_cond

    return np.array([d_theta, d_azimuth, d_z])


def elu_to_c2w(eye, lookat, up):

    if isinstance(eye, list):
        eye = np.array(eye)
    if isinstance(lookat, list):
        lookat = np.array(lookat)
    if isinstance(up, list):
        up = np.array(up)

    l = eye - lookat
    l = l / np.linalg.norm(l)
    s = np.cross(l, up)
    s = s / np.linalg.norm(s)
    uu = np.cross(s, l)

    rot = np.eye(3)
    rot[0, :] = -s
    rot[1, :] = uu
    rot[2, :] = l
    
    c2w = np.eye(4)
    c2w[:3, :3] = rot.T
    c2w[:3, 3] = eye

    return c2w


def c2w_to_elu(c2w):

    w2c = np.linalg.inv(c2w)
    eye = c2w[:3, 3]
    lookat_dir = -w2c[2, :3]
    lookat = eye + lookat_dir
    up = w2c[1, :3]

    return eye, lookat, up


    '''save pose output'''
    jdata = {
        'anchor_vid': anchor_vid,
        'obs': {}
    }

    jdata['obs'][anchor_vid] = {
        'img_path': f'{anchor_vid:03d}.png'
    }

    for key in aux_data:
        jdata['obs'][anchor_vid][key] = aux_data[key][0]

    anchor_sph = None

    anchor_rt = None

    if os.path.exists(os.path.join(obj_root, 'poses', f'{anchor_vid:03d}.npy')):
        anchor_rt = np.load(os.path.join(obj_root, 'poses', f'{anchor_vid:03d}.npy'))
    elif os.path.exists(os.path.join(obj_root, 'poses', f'{anchor_vid:03d}.txt')):
        anchor_rt = np.loadtxt(os.path.join(obj_root, 'poses', f'{anchor_vid:03d}.txt'))

    if anchor_rt is not None:

        anchor_xyz = anchor_rt[:3, -1]

        anchor_sph = cartesian_to_spherical(anchor_xyz)
        jdata['obs'][anchor_vid]['sph'] = anchor_sph.tolist()

        if export_xyz:        
            jdata['obs'][anchor_vid]['xyz'] = {
                'x': anchor_xyz[0],
                'y': anchor_xyz[1],
                'z': anchor_xyz[2]
            }


    for i in range(0, len(target_vids)):
        
        target_vid = target_vids[i]

        rel_sph = np.array(pred_sphs[i])

        opack = {
            'img_path': f'{target_vid:03d}.png',
            'rel_sph': rel_sph.tolist()
        }

        for key in aux_data:
            opack[key] = aux_data[key][i+1]

        if anchor_sph is not None:

            target_sph = anchor_sph + rel_sph

            if export_xyz:
                target_xyz = spherical_to_cartesian(target_sph)

                opack['xyz'] = {
                    'x': target_xyz[0],
                    'y': target_xyz[1],
                    'z': target_xyz[2]
                }

            target_rt = None

            if os.path.exists(os.path.join(obj_root, 'poses', f'{target_vid:03d}.npy')):
                target_rt = np.load(os.path.join(obj_root, 'poses', f'{target_vid:03d}.npy'))
            elif os.path.exists(os.path.join(obj_root, 'poses', f'{target_vid:03d}.txt')):
                target_rt = np.loadtxt(os.path.join(obj_root, 'poses', f'{target_vid:03d}.txt'))

            if target_rt is not None:

                if export_xyz:
                    opack['gt_xyz'] = {
                        'x': target_rt[0, -1],
                        'y': target_rt[1, -1],
                        'z': target_rt[2, -1]
                    }

                gt_rel_sph = relative_spherical(target_rt[:3, -1], anchor_rt[:3, -1])

                opack['gt_rel_sph'] = gt_rel_sph.tolist()

        jdata['obs'][target_vid] = opack

    return jdata