-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathgui_utils.py
More file actions
190 lines (159 loc) · 5.98 KB
/
gui_utils.py
File metadata and controls
190 lines (159 loc) · 5.98 KB
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
# Code here is modified based on the GUI code from the MonoGS project.
# Copyright 2024 The MonoGS Authors.
# Licensed under the License issued by the MonoGS Authors
# available here: https://github.com/muskie82/MonoGS/blob/main/LICENSE.md
#
# Modifications made by Jianhao Zheng / Stanford University, 2025.
import queue
import cv2
import numpy as np
import open3d as o3d
import torch
from thirdparty.gaussian_splatting.utils.general_utils import (
build_scaling_rotation,
strip_symmetric,
)
cv_gl = np.array([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
class Frustum:
def __init__(self, line_set, view_dir=None, view_dir_behind=None, size=None):
self.line_set = line_set
self.view_dir = view_dir
self.view_dir_behind = view_dir_behind
self.size = size
def update_pose(self, pose):
points = np.asarray(self.line_set.points)
points_hmg = np.hstack([points, np.ones((points.shape[0], 1))])
points = (pose @ points_hmg.transpose())[0:3, :].transpose()
base = np.array([[0.0, 0.0, 0.0]]) * self.size
base_hmg = np.hstack([base, np.ones((base.shape[0], 1))])
cameraeye = pose @ base_hmg.transpose()
cameraeye = cameraeye[0:3, :].transpose()
eye = cameraeye[0, :]
# base_behind = np.array([[0.0, -2.5, -30.0]]) * self.size
base_behind = np.array([[0.0, -1.0, -10.0]]) * self.size
base_behind_hmg = np.hstack([base_behind, np.ones((base_behind.shape[0], 1))])
cameraeye_behind = pose @ base_behind_hmg.transpose()
cameraeye_behind = cameraeye_behind[0:3, :].transpose()
eye_behind = cameraeye_behind[0, :]
center = np.mean(points[1:, :], axis=0)
up = points[2] - points[4]
self.view_dir = (center, eye, up, pose)
self.view_dir_behind = (center, eye_behind, up, pose)
self.center = center
self.eye = eye
self.up = up
def create_frustum(pose, frusutum_color=[0, 1, 0], size=0.02):
points = (
np.array(
[
[0.0, 0.0, 0],
[1.0, -0.5, 2],
[-1.0, -0.5, 2],
[1.0, 0.5, 2],
[-1.0, 0.5, 2],
]
)
* size
)
lines = [[0, 1], [0, 2], [0, 3], [0, 4], [1, 2], [1, 3], [2, 4], [3, 4]]
colors = [frusutum_color for i in range(len(lines))]
canonical_line_set = o3d.geometry.LineSet()
canonical_line_set.points = o3d.utility.Vector3dVector(points)
canonical_line_set.lines = o3d.utility.Vector2iVector(lines)
canonical_line_set.colors = o3d.utility.Vector3dVector(colors)
frustum = Frustum(canonical_line_set, size=size)
frustum.update_pose(pose)
return frustum
class GaussianPacket:
def __init__(
self,
gaussians=None,
keyframe=None,
current_frame=None,
gtcolor=None,
gtdepth=None,
uncertainty=None,
keyframes=None,
finish=False,
kf_window=None,
keyframe_colors=None,
full_traj=None,
full_traj_gt=None
):
self.has_gaussians = False
if gaussians is not None:
self.has_gaussians = True
self.get_xyz = gaussians.get_xyz.detach().clone()
self.active_sh_degree = gaussians.active_sh_degree
self.get_opacity = gaussians.get_opacity.detach().clone()
self.get_scaling = gaussians.get_scaling.detach().clone()
self.get_rotation = gaussians.get_rotation.detach().clone()
self.max_sh_degree = gaussians.max_sh_degree
self.get_features = gaussians.get_features.detach().clone()
self._rotation = gaussians._rotation.detach().clone()
self.rotation_activation = torch.nn.functional.normalize
self.unique_kfIDs = gaussians.unique_kfIDs.clone()
self.n_obs = gaussians.n_obs.clone()
self.keyframe = keyframe
self.current_frame = current_frame
self.gtcolor = self.resize_img(gtcolor, 320)
self.gtdepth = self.resize_img(gtdepth, 320)
self.uncertainty = self.resize_img(uncertainty, 320)
self.keyframes = keyframes
self.finish = finish
self.kf_window = kf_window
self.keyframe_colors = keyframe_colors
self.full_traj=full_traj
self.full_traj_gt=full_traj_gt
def resize_img(self, img, width):
if img is None:
return None
# check if img is numpy
if isinstance(img, np.ndarray):
height = int(width * img.shape[0] / img.shape[1])
return cv2.resize(img, (width, height))
height = int(width * img.shape[1] / img.shape[2])
# img is 3xHxW
img = torch.nn.functional.interpolate(
img.unsqueeze(0), size=(height, width), mode="bilinear", align_corners=False
)
return img.squeeze(0)
def get_covariance(self, scaling_modifier=1):
return self.build_covariance_from_scaling_rotation(
self.get_scaling, scaling_modifier, self._rotation
)
def build_covariance_from_scaling_rotation(
self, scaling, scaling_modifier, rotation
):
L = build_scaling_rotation(scaling_modifier * scaling, rotation)
actual_covariance = L @ L.transpose(1, 2)
symm = strip_symmetric(actual_covariance)
return symm
def get_latest_queue(q):
message = None
while True:
try:
message_latest = q.get_nowait()
if message is not None:
del message
message = message_latest
except queue.Empty:
if q.qsize() < 1:
break
return message
class Packet_vis2main:
flag_pause = None
class ParamsGUI:
def __init__(
self,
pipe=None,
background=None,
gaussians=None,
q_main2vis=None,
q_vis2main=None,
):
self.pipe = pipe
self.background = background
self.gaussians = gaussians
self.q_main2vis = q_main2vis
self.q_vis2main = q_vis2main