File size: 11,789 Bytes
a064299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import os
import json
import numpy as np
import torch
from scene.cameras import Camera as GSCamera
from utils.graphics_utils import focal2fov


def generate_camera_rotation_matrix(camera_to_object, object_vertical_downward):
    camera_to_object = camera_to_object / np.linalg.norm(
        camera_to_object
    )  # last column
    # the second column of rotation matrix is pointing toward the downward vertical direction
    camera_y = (
        object_vertical_downward
        - np.dot(object_vertical_downward, camera_to_object) * camera_to_object
    )
    camera_y = camera_y / np.linalg.norm(camera_y)  # second column
    first_column = np.cross(camera_y, camera_to_object)
    R = np.column_stack((first_column, camera_y, camera_to_object))
    return R


# supply vertical vector in world space
def generate_local_coord(vertical_vector):
    vertical_vector = vertical_vector / np.linalg.norm(vertical_vector)
    horizontal_1 = np.array([1, 1, 1])
    if np.abs(np.dot(horizontal_1, vertical_vector)) < 0.01:
        horizontal_1 = np.array([0.72, 0.37, -0.67])
    # gram schimit
    horizontal_1 = (
        horizontal_1 - np.dot(horizontal_1, vertical_vector) * vertical_vector
    )
    horizontal_1 = horizontal_1 / np.linalg.norm(horizontal_1)
    horizontal_2 = np.cross(horizontal_1, vertical_vector)

    return vertical_vector, horizontal_1, horizontal_2


# scalar (in degrees), scalar (in degrees), scalar, vec3, mat33 = [horizontal_1; horizontal_2; vertical];  -> vec3
def get_point_on_sphere(azimuth, elevation, radius, center, observant_coordinates):
    canonical_coordinates = (
        np.array(
            [
                np.cos(azimuth / 180.0 * np.pi) * np.cos(elevation / 180.0 * np.pi),
                np.sin(azimuth / 180.0 * np.pi) * np.cos(elevation / 180.0 * np.pi),
                np.sin(elevation / 180.0 * np.pi),
            ]
        )
        * radius
    )

    return center + observant_coordinates @ canonical_coordinates


def get_camera_position_and_rotation(
    azimuth, elevation, radius, view_center, observant_coordinates
):
    # get camera position
    position = get_point_on_sphere(
        azimuth, elevation, radius, view_center, observant_coordinates
    )
    # get rotation matrix
    R = generate_camera_rotation_matrix(
        view_center - position, -observant_coordinates[:, 2]
    )
    return position, R


def get_current_radius_azimuth_and_elevation(
    camera_position, view_center, observesant_coordinates
):
    center2camera = -view_center + camera_position
    radius = np.linalg.norm(center2camera)
    dot_product = np.dot(center2camera, observesant_coordinates[:, 2])
    cosine = dot_product / (
        np.linalg.norm(center2camera) * np.linalg.norm(observesant_coordinates[:, 2])
    )
    elevation = np.rad2deg(np.pi / 2.0 - np.arccos(cosine))
    proj_onto_hori = center2camera - dot_product * observesant_coordinates[:, 2]
    dot_product2 = np.dot(proj_onto_hori, observesant_coordinates[:, 0])
    cosine2 = dot_product2 / (
        np.linalg.norm(proj_onto_hori) * np.linalg.norm(observesant_coordinates[:, 0])
    )

    if np.dot(proj_onto_hori, observesant_coordinates[:, 1]) > 0:
        azimuth = np.rad2deg(np.arccos(cosine2))
    else:
        azimuth = -np.rad2deg(np.arccos(cosine2))
    return radius, azimuth, elevation


def get_camera_view(
    model_path,
    default_camera_index=0,
    center_view_world_space=None,
    observant_coordinates=None,
    show_hint=False,
    init_azimuthm=None,
    init_elevation=None,
    init_radius=None,
    move_camera=False,
    current_frame=0,
    delta_a=0,
    delta_e=0,
    delta_r=0,
    downsample=1.0,
):
    """Load one of the default cameras for the scene."""
    cam_path = os.path.join(model_path, "cameras.json")
    with open(cam_path) as f:
        data = json.load(f)

        camera_view_info = {}
        camera_view_info["init_azimuthm"] = init_azimuthm
        camera_view_info["init_elevation"] = init_elevation
        camera_view_info["init_radius"] = init_radius

        if show_hint:
            if default_camera_index < 0:
                default_camera_index = 0
            r, a, e = get_current_radius_azimuth_and_elevation(
                data[default_camera_index]["position"],
                center_view_world_space,
                observant_coordinates,
            )
            print("Default camera ", default_camera_index, " has")
            print("azimuth:    ", a)
            print("elevation:  ", e)
            print("radius:     ", r)
            print("Now exit program and set your own input!")
            exit()

        if default_camera_index > -1:
            raw_camera = data[default_camera_index]
            # print(raw_camera)

            r, a, e = get_current_radius_azimuth_and_elevation(
                raw_camera["position"],
                center_view_world_space,
                observant_coordinates,
            )
            # if raw_camera["width"] < raw_camera["height"]:
            #     a = -a
            camera_view_info["init_azimuthm"] = a
            camera_view_info["init_elevation"] = e
            camera_view_info["init_radius"] = r

            init_azimuthm = a
            init_elevation = e
            init_radius = r

            if move_camera:
                assert delta_a is not None
                assert delta_e is not None
                assert delta_r is not None
                position, R = get_camera_position_and_rotation(
                    init_azimuthm + current_frame * delta_a,
                    init_elevation + current_frame * delta_e,
                    init_radius + current_frame * delta_r,
                    center_view_world_space,
                    observant_coordinates,
                )
                # print("position", position)
                raw_camera["rotation"] = R.tolist()
                raw_camera["position"] = position.tolist()

            # print("Default camera ", default_camera_index, " has")
            # print("azimuth:    ", a)
            # print("elevation:  ", e)
            # print("radius:     ", r)
        else:
            raw_camera = data[0]

            r, a, e = get_current_radius_azimuth_and_elevation(
                data[default_camera_index]["position"],
                center_view_world_space,
                observant_coordinates,
            )

            if move_camera:
                assert delta_a is not None
                assert delta_e is not None
                assert delta_r is not None
                position, R = get_camera_position_and_rotation(
                    init_azimuthm + current_frame * delta_a,
                    init_elevation + current_frame * delta_e,
                    init_radius + current_frame * delta_r,
                    center_view_world_space,
                    observant_coordinates,
                )
            else:
                position, R = get_camera_position_and_rotation(
                    init_azimuthm,
                    init_elevation,
                    init_radius,
                    center_view_world_space,
                    observant_coordinates,
                )
            raw_camera["rotation"] = R.tolist()
            raw_camera["position"] = position.tolist()


        tmp = np.zeros((4, 4))
        tmp[:3, :3] = raw_camera["rotation"]
        tmp[:3, 3] = raw_camera["position"]
        tmp[3, 3] = 1
        C2W = np.linalg.inv(tmp)
        R = C2W[:3, :3].transpose()
        T = C2W[:3, 3]
        # if 'pac_nerf/toothpaste' in model_path:
        #     T = C2W[:3, 3] * 1.5

        width = min(raw_camera["width"], 1920)
        height = min(raw_camera["height"], 1920)
        # fovx = focal2fov(raw_camera["fx"], width)
        # fovy = focal2fov(raw_camera["fy"], height)
        width = int(width * downsample)
        height = int(height * downsample)

        fovx = focal2fov(raw_camera["fx"]*downsample, width)
        fovy = focal2fov(raw_camera["fy"]*downsample, height)


        return GSCamera(
            colmap_id=0,
            R=R,
            T=T,
            FoVx=fovx,
            FoVy=fovy,
            image_width=width,
            image_height=height,
            image=torch.zeros((3, height, width)),  # fake
            gt_alpha_mask=None,
            image_name="fake",
            image_path="fake",
            uid=0,
            preload_img=False
        ), camera_view_info

def get_camera_view_endonerf():
    # endonerf camera para
    R_for_GSCamera = np.array([[1., -0., -0.],
                        [0., -1., -0.],
                        [0., -0., -1.]], dtype=float)
    T_for_GSCamera = np.zeros(3, dtype=float)
    # T_for_GSCamera[2] +=10 # 临时生成器械效果图
    # T_for_GSCamera[1] +=5 # 临时生成器械效果图

    FoVx = 1.0239093368021417
    FoVy = 0.8449463442193673
    width = int(640)
    height = int(512)
    image_tensor = torch.zeros((3, height, width))  # fake
    current_camera =  GSCamera(
        colmap_id=0,
        R=R_for_GSCamera,
        T=T_for_GSCamera,
        FoVx=FoVx,
        FoVy=FoVy,
        image_width=width,
        image_height=height,
        image=image_tensor,
        gt_alpha_mask=None,
        image_name="fake",
        image_path="fake",
        uid=0,
        preload_img=False
    )
    return current_camera

def get_camera_view_stereomis():
    # endonerf camera para
    R_for_GSCamera = np.array([[1., -0., -0.],
                        [0., -1., -0.],
                        [0., -0., -1.]], dtype=float)
    T_for_GSCamera = np.zeros(3, dtype=float)
    FoVx = 1.1036297361167346
    FoVy = 0.9152335928441784
    width = int(640)
    height = int(512)
    image_tensor = torch.zeros((3, height, width))  # fake
    current_camera =  GSCamera(
        colmap_id=0,
        R=R_for_GSCamera,
        T=T_for_GSCamera,
        FoVx=FoVx,
        FoVy=FoVy,
        image_width=width,
        image_height=height,
        image=image_tensor,
        gt_alpha_mask=None,
        image_name="fake",
        image_path="fake",
        uid=0,
        preload_img=False
    )
    return current_camera



def get_camera_view_cholecseg_sub():
    # endonerf camera para
    R_for_GSCamera = np.array([[1., -0., -0.],
                        [0., 1., -0.],
                        [0., -0., 1.]], dtype=float)
    T_for_GSCamera = np.zeros(3, dtype=float)
    FoVx = 0.7483519078147721
    FoVy = 0.4344497977059072
    width = int(854)
    height = int(480)
    image_tensor = torch.zeros((3, height, width))  # fake
    current_camera =  GSCamera(
        colmap_id=0,
        R=R_for_GSCamera,
        T=T_for_GSCamera,
        FoVx=FoVx,
        FoVy=FoVy,
        image_width=width,
        image_height=height,
        image=image_tensor,
        gt_alpha_mask=None,
        image_name="fake",
        image_path="fake",
        uid=0,
        preload_img=False
    )
    return current_camera


def get_camera_view_porcine_endo():
    # endonerf camera para
    R_for_GSCamera = np.array([[1., -0., -0.],
                        [0., 1., -0.],
                        [0., -0., 1.]], dtype=float)
    T_for_GSCamera = np.zeros(3, dtype=float)
    # T_for_GSCamera[2] +=10 # 临时生成器械效果图
    FoVx = 0.8283241463683268
    FoVy = 0.6349340651524208
    width = int(640)
    height = int(480)
    image_tensor = torch.zeros((3, height, width))  # fake
    current_camera =  GSCamera(
        colmap_id=0,
        R=R_for_GSCamera,
        T=T_for_GSCamera,
        FoVx=FoVx,
        FoVy=FoVy,
        image_width=width,
        image_height=height,
        image=image_tensor,
        gt_alpha_mask=None,
        image_name="fake",
        image_path="fake",
        uid=0,
        preload_img=False
    )
    return current_camera