-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene_reconstruction.py
180 lines (139 loc) · 5.05 KB
/
scene_reconstruction.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
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
import numpy as np
from vedo import Plotter, Grid, Cone, Box, Plane, Sphere
class RenderableObject(object):
element_type = 'generic'
def __init__(self, name, color=None):
self.name = name
self.color = color
self._elems = []
def render(self):
raise NotImplementedError
def __repr__(self):
return f'{self.element_type:10} {self.name:12}'
class RenderableCamera(RenderableObject):
element_type = 'camera'
def __init__(self, geom, *args, **kwargs):
self.geom = geom
super().__init__(*args, **kwargs)
def render(self):
if not self._elems:
#TODO Render all camera origins with cones
camera_pos = (0, 0, -10)
self._elems.append(Box(pos=camera_pos, length=80, width=30, height=20, c="black"))
cone = Cone(axis=(0, 0, -1), height=1000, r=1200, res=100, pos=(0, 0, +500))
cone.color("lightgray").alpha(0.1)
self._elems.append(cone)
return self._elems
class RenderablePlane(RenderableObject):
element_type = 'plane'
def __init__(self, centroid, normal, *args, **kwargs):
self._centroid = centroid
self._normal = normal
super().__init__(*args, **kwargs)
@property
def centroid(self):
return self._centroid
@centroid.setter
def centroid(self, value):
if not np.array_equal(value, self._centroid):
self._elems = []
self._centroid = value
@property
def normal(self):
return self._normal
@normal.setter
def normal(self, value):
if not np.array_equal(value, self._normal):
self._elems = []
self._normal = value
def render(self):
if not self._elems:
self._elems.append(Plane(pos=self.centroid, normal=self.normal, s=(1000,1000),
c=self.color, alpha=0.4))
return self._elems
def __repr__(self):
return super().__repr__() + f' centroid {self.centroid}; normal {self.normal}'
class RenderableMarker(RenderableObject):
element_type = 'marker'
def __init__(self, tvec, size=50, *args, **kwargs):
self._tvec = None
self.tvec = tvec
self.size = size
super().__init__(*args, **kwargs)
@property
def tvec(self):
return self._tvec
@tvec.setter
def tvec(self, value):
if isinstance(value, dict):
value = np.array([value['x'], value['y'], value['z']])
if self._tvec is None or not np.array_equal(value, self._tvec):
self._elems = []
self._tvec = value
def render(self):
if not self._elems:
pos = self.tvec.flatten()
print(pos)
self._elems.append(Sphere(pos=pos, r=self.size/2, c=self.color))
return self._elems
def __repr__(self):
return super().__repr__() + f' tvec {self.tvec.tolist()}'
class SceneReconstruction(object):
def __init__(self):
self.elements = {}
self._target : Plotter = None
def _clear_elements(self, name):
to_remove = []
for e in self.elements.keys():
if e.startswith(name):
to_remove.append(e)
for e in to_remove:
del self.elements[e]
def upsert_camera(self, geom):
if 'camera' in self.elements.keys():
# We assume the coordinate systems are fixed.
pass
else:
self.elements['camera'] = RenderableCamera(name='camera', geom=geom)
def clear_plane(self, name):
self._clear_elements(name)
def upsert_plane(self, name, centroid, normal, color=None):
if centroid is None or normal is None:
return
if name in self.elements.keys():
self.elements[name].centroid = centroid
self.elements[name].normal = normal
else:
self.elements[name] = RenderablePlane(centroid=centroid, normal=normal, name=name, color=color)
def clear_markers(self, name):
self._clear_elements(name)
def upsert_marker(self, name, tvec, color=None):
if name in self.elements.keys():
self.elements[name].tvec = tvec
else:
self.elements[name] = RenderableMarker(tvec=tvec, name=name, color=color)
def remove_element(self, name):
if name in self.elements:
del self.elements[name]
@property
def target(self):
return self._target
@target.setter
def target(self, target):
self._target = target
def update_scene(self):
self.target.clear(at=0)
elements_to_show = False
for name, elem_spec in self.elements.items():
elem_spec : RenderableObject
elems = elem_spec.render()
for elem in elems:
self.target.at(0).show(elem)
elements_to_show = True
if not elements_to_show:
empty_grid = Grid()
self.target.at(0).show(empty_grid)
self.target.axes = 1
print(self.target)
def close(self):
pass