-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathview_control.py
95 lines (82 loc) · 2.83 KB
/
view_control.py
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
from collections import defaultdict
from dataclasses import dataclass
import moderngl
import numpy as np
from moderngl_window.timers.clock import Timer
from PIL import Image
import depth_utils
import point_cloud_rendering_utils as pcru
from point_viewer import PointCloudViewer
from pointcloud import SDPointCloud
@dataclass
class ScreenCapture:
color_image: Image
depth_image: Image
width: int
height: int
ids: np.ndarray
class ViewControl:
"""Controls a moderngl_window viewer"""
def __init__(
self,
sd_pcd: SDPointCloud,
width: int,
height: int,
retexture_callback: callable,
retexture_width: int,
retexture_height: int,
debug: bool = False,
):
self.sd_pcd = sd_pcd
self.debug = debug
callbacks = defaultdict(lambda: lambda: print("Action not defined"))
callbacks["retexture"] = lambda ctx, mvp: retexture_callback(
self.create_screen_capture(
ctx, mvp, retexture_width, retexture_height, debug
)
)
callbacks["load"] = lambda: self.sd_pcd.load("retexture")
callbacks["save"] = lambda: self.sd_pcd.save("retexture")
if debug:
callbacks["reset"] = lambda: self.sd_pcd.reset()
callbacks["retexture_only"] = lambda: self.sd_pcd.filter(
self.sd_pcd.retextured_point_ids
)
callbacks["blend"] = lambda: self.sd_pcd.flag(
self.sd_pcd.retextured_point_ids
)
callbacks["flag"] = lambda ids: self.sd_pcd.flag(ids)
callbacks["filter"] = lambda ids: self.sd_pcd.filter(ids)
self.viewer = PointCloudViewer(
self.sd_pcd.pcd,
callbacks,
title="StableScan",
size=(width, height),
debug=debug,
)
def run(self):
"""Runs a custom render loop"""
timer = Timer()
timer.start()
while not self.viewer.wnd.is_closing:
self.viewer.step(timer)
self.viewer.wnd.destroy()
def create_screen_capture(
self,
ctx: moderngl.Framebuffer,
mvp: np.ndarray,
width: int,
height: int,
debug: bool = False,
) -> ScreenCapture:
params = (ctx, self.sd_pcd.pcd, mvp, width, height)
screen_image = pcru.render_pointcloud(*params, debug=debug)
depth_image = pcru.create_depth_image(*params, filter=False, debug=debug)
depth_image_filtered = pcru.create_depth_image(
*params, filter=True, debug=debug
)
raw_ids = pcru.obtain_point_ids(*params, debug=debug)
ids, _ = depth_utils.filter_ids(
raw_ids, depth_image_filtered, depth_image, debug=debug
)
return ScreenCapture(screen_image, depth_image_filtered, width, height, ids)