Elffuss BraidHub

kikocis/SpatialLM public

main · 14.0 KiBRawPermalink
1# Copyright (c) Manycore Tech Inc. and affiliates.
2# All rights reserved.
3
4"""
5This code is derived from the SceneScript language sequence and entity parameters.
6
7Reference: https://github.com/facebookresearch/scenescript/blob/main/src/data/language_sequence.py
8"""
9
10from dataclasses import dataclass
11
12import numpy as np
13from scipy.spatial.transform import Rotation as R
14
15NORMALIZATION_PRESET = {
16 "world": (0.0, 32.0),
17 "height": (0.0, 25.6),
18 "width": (0.0, 25.6),
19 "scale": (0.0, 20.0),
20 "angle": (-6.2832, 6.2832),
21}
22
23
24@dataclass
25class Wall:
26 id: int
27 ax: float
28 ay: float
29 az: float
30 bx: float
31 by: float
32 bz: float
33 height: float
34 thickness: float
35 entity_label: str = "wall"
36
37 def __post_init__(self):
38 self.id = int(self.id)
39 self.ax = float(self.ax)
40 self.ay = float(self.ay)
41 self.az = float(self.az)
42 self.bx = float(self.bx)
43 self.by = float(self.by)
44 self.bz = float(self.bz)
45 self.height = float(self.height)
46 self.thickness = float(self.thickness)
47
48 def rotate(self, angle: float):
49 wall_start = np.array([self.ax, self.ay, self.az])
50 wall_end = np.array([self.bx, self.by, self.bz])
51 rotmat = R.from_rotvec([0, 0, angle]).as_matrix()
52 wall_start = rotmat @ wall_start
53 wall_end = rotmat @ wall_end
54
55 self.ax = wall_start[0]
56 self.ay = wall_start[1]
57 self.az = wall_start[2]
58 self.bx = wall_end[0]
59 self.by = wall_end[1]
60 self.bz = wall_end[2]
61
62 def translate(self, translation: np.ndarray):
63 self.ax += translation[0]
64 self.ay += translation[1]
65 self.az += translation[2]
66 self.bx += translation[0]
67 self.by += translation[1]
68 self.bz += translation[2]
69
70 def scale(self, scaling: float):
71 self.height *= scaling
72 self.thickness *= scaling
73 self.ax *= scaling
74 self.ay *= scaling
75 self.az *= scaling
76 self.bx *= scaling
77 self.by *= scaling
78 self.bz *= scaling
79
80 def normalize_and_discretize(self, num_bins):
81 height_min, height_max = NORMALIZATION_PRESET["height"]
82 world_min, world_max = NORMALIZATION_PRESET["world"]
83
84 self.height = (self.height - height_min) / (height_max - height_min) * num_bins
85 self.thickness = (
86 (self.thickness - height_min) / (height_max - height_min) * num_bins
87 )
88 self.ax = (self.ax - world_min) / (world_max - world_min) * num_bins
89 self.ay = (self.ay - world_min) / (world_max - world_min) * num_bins
90 self.az = (self.az - world_min) / (world_max - world_min) * num_bins
91 self.bx = (self.bx - world_min) / (world_max - world_min) * num_bins
92 self.by = (self.by - world_min) / (world_max - world_min) * num_bins
93 self.bz = (self.bz - world_min) / (world_max - world_min) * num_bins
94
95 self.height = np.clip(int(self.height), 0, num_bins - 1)
96 self.thickness = np.clip(int(self.thickness), 0, num_bins - 1)
97 self.ax = np.clip(int(self.ax), 0, num_bins - 1)
98 self.ay = np.clip(int(self.ay), 0, num_bins - 1)
99 self.az = np.clip(int(self.az), 0, num_bins - 1)
100 self.bx = np.clip(int(self.bx), 0, num_bins - 1)
101 self.by = np.clip(int(self.by), 0, num_bins - 1)
102 self.bz = np.clip(int(self.bz), 0, num_bins - 1)
103
104 def undiscretize_and_unnormalize(self, num_bins):
105 height_min, height_max = NORMALIZATION_PRESET["height"]
106 world_min, world_max = NORMALIZATION_PRESET["world"]
107
108 # undiscretize
109 self.height = self.height / num_bins
110 self.thickness = self.thickness / num_bins
111 self.ax = self.ax / num_bins
112 self.ay = self.ay / num_bins
113 self.az = self.az / num_bins
114 self.bx = self.bx / num_bins
115 self.by = self.by / num_bins
116 self.bz = self.bz / num_bins
117
118 # unnormalize
119 self.height = self.height * (height_max - height_min) + height_min
120 self.thickness = self.thickness * (height_max - height_min) + height_min
121 self.ax = self.ax * (world_max - world_min) + world_min
122 self.ay = self.ay * (world_max - world_min) + world_min
123 self.az = self.az * (world_max - world_min) + world_min
124 self.bx = self.bx * (world_max - world_min) + world_min
125 self.by = self.by * (world_max - world_min) + world_min
126 self.bz = self.bz * (world_max - world_min) + world_min
127
128 def to_language_string(self):
129 capitalized_label = self.entity_label.capitalize()
130 # wall_0=Wall(a_x,a_y,a_z,b_x,b_y,b_z,height,thickness)
131 language_string = f"{self.entity_label}_{self.id}={capitalized_label}({self.ax},{self.ay},{self.az},{self.bx},{self.by},{self.bz},{self.height},{self.thickness})"
132 return language_string
133
134 def sort_key(self):
135 # Lex-sort corners
136 wall_start = np.array([self.ax, self.ay, self.az])
137 wall_end = np.array([self.bx, self.by, self.bz])
138 corners = np.stack([wall_start, wall_end]) # [2, 3]
139
140 idx = np.lexsort(corners.T) # [2]. Sorts by z, y, x.
141 corner_1_ordered, corner_2_ordered = corners[idx]
142
143 # Sort wall-corners
144 self.ax, self.ay, self.az = corner_1_ordered
145 self.bx, self.by, self.bz = corner_2_ordered
146
147 return np.concatenate([corner_2_ordered, corner_1_ordered])
148
149
150@dataclass
151class Door:
152 id: int
153 wall_id: int
154 position_x: float
155 position_y: float
156 position_z: float
157 width: float
158 height: float
159 entity_label: str = "door"
160
161 def __post_init__(self):
162 self.id = int(self.id)
163 self.wall_id = int(self.wall_id)
164 self.position_x = float(self.position_x)
165 self.position_y = float(self.position_y)
166 self.position_z = float(self.position_z)
167 self.width = float(self.width)
168 self.height = float(self.height)
169
170 def rotate(self, angle: float):
171 center = np.array([self.position_x, self.position_y, self.position_z])
172 rotmat = R.from_rotvec([0, 0, angle]).as_matrix()
173 new_center = rotmat @ center
174
175 self.position_x = new_center[0]
176 self.position_y = new_center[1]
177 self.position_z = new_center[2]
178
179 def translate(self, translation: np.ndarray):
180 self.position_x += translation[0]
181 self.position_y += translation[1]
182 self.position_z += translation[2]
183
184 def scale(self, scaling: float):
185 self.width *= scaling
186 self.height *= scaling
187 self.position_x *= scaling
188 self.position_y *= scaling
189 self.position_z *= scaling
190
191 def normalize_and_discretize(self, num_bins):
192 width_min, width_max = NORMALIZATION_PRESET["width"]
193 height_min, height_max = NORMALIZATION_PRESET["height"]
194 world_min, world_max = NORMALIZATION_PRESET["world"]
195
196 self.width = (self.width - width_min) / (width_max - width_min) * num_bins
197 self.height = (self.height - height_min) / (height_max - height_min) * num_bins
198 self.position_x = (
199 (self.position_x - world_min) / (world_max - world_min) * num_bins
200 )
201 self.position_y = (
202 (self.position_y - world_min) / (world_max - world_min) * num_bins
203 )
204 self.position_z = (
205 (self.position_z - world_min) / (world_max - world_min) * num_bins
206 )
207
208 self.width = np.clip(int(self.width), 0, num_bins - 1)
209 self.height = np.clip(int(self.height), 0, num_bins - 1)
210 self.position_x = np.clip(int(self.position_x), 0, num_bins - 1)
211 self.position_y = np.clip(int(self.position_y), 0, num_bins - 1)
212 self.position_z = np.clip(int(self.position_z), 0, num_bins - 1)
213
214 def undiscretize_and_unnormalize(self, num_bins):
215 width_min, width_max = NORMALIZATION_PRESET["width"]
216 height_min, height_max = NORMALIZATION_PRESET["height"]
217 world_min, world_max = NORMALIZATION_PRESET["world"]
218
219 # undiscretize
220 self.width = self.width / num_bins
221 self.height = self.height / num_bins
222 self.position_x = self.position_x / num_bins
223 self.position_y = self.position_y / num_bins
224 self.position_z = self.position_z / num_bins
225
226 # unnormalize
227 self.width = self.width * (width_max - width_min) + width_min
228 self.height = self.height * (height_max - height_min) + height_min
229 self.position_x = self.position_x * (world_max - world_min) + world_min
230 self.position_y = self.position_y * (world_max - world_min) + world_min
231 self.position_z = self.position_z * (world_max - world_min) + world_min
232
233 def to_language_string(self):
234 capitalized_label = self.entity_label.capitalize()
235 self.id = self.id % 1000
236 # door_0=Door(wall_id,position_x,position_y,position_z,width,height)
237 language_string = f"{self.entity_label}_{self.id}={capitalized_label}(wall_{self.wall_id},{self.position_x},{self.position_y},{self.position_z},{self.width},{self.height})"
238 return language_string
239
240 def sort_key(self):
241 return np.array([self.position_x, self.position_y])
242
243
244@dataclass
245class Window(Door):
246 entity_label: str = "window"
247
248
249@dataclass
250class Bbox:
251 id: int
252 class_name: str
253 position_x: float
254 position_y: float
255 position_z: float
256 angle_z: float
257 scale_x: float
258 scale_y: float
259 scale_z: float
260 entity_label: str = "bbox"
261
262 def __post_init__(self):
263 self.id = int(self.id)
264 self.class_name = str(self.class_name)
265 self.position_x = float(self.position_x)
266 self.position_y = float(self.position_y)
267 self.position_z = float(self.position_z)
268 self.angle_z = float(self.angle_z)
269 self.scale_x = abs(float(self.scale_x))
270 self.scale_y = abs(float(self.scale_y))
271 self.scale_z = abs(float(self.scale_z))
272
273 def rotate(self, angle: float):
274 augment_rot_mat = R.from_rotvec([0, 0, angle]).as_matrix()
275 bbox_rot_mat = R.from_rotvec([0, 0, self.angle_z]).as_matrix()
276 new_bbox_rot_mat = augment_rot_mat @ bbox_rot_mat
277 new_angle_z = R.from_matrix(new_bbox_rot_mat).as_euler("ZYX")[0]
278 new_angle_z = (new_angle_z + np.pi) % (2 * np.pi) - np.pi # Range: [-pi, pi)
279
280 # Bbox is symmetric
281 symmetry = np.pi
282 if np.isclose(self.scale_x, self.scale_y, atol=1e-3):
283 symmetry = np.pi / 2
284 new_angle_z = (new_angle_z + np.pi) % symmetry - np.pi
285 self.angle_z = new_angle_z
286
287 bbox_center = np.array([self.position_x, self.position_y, self.position_z])
288 bbox_center = augment_rot_mat @ bbox_center
289 self.position_x = bbox_center[0]
290 self.position_y = bbox_center[1]
291 self.position_z = bbox_center[2]
292
293 def translate(self, translation: np.ndarray):
294 self.position_x += translation[0]
295 self.position_y += translation[1]
296 self.position_z += translation[2]
297
298 def scale(self, scaling: float):
299 self.scale_x *= scaling
300 self.scale_y *= scaling
301 self.scale_z *= scaling
302 self.position_x *= scaling
303 self.position_y *= scaling
304 self.position_z *= scaling
305
306 def normalize_and_discretize(self, num_bins):
307 world_min, world_max = NORMALIZATION_PRESET["world"]
308 scale_min, scale_max = NORMALIZATION_PRESET["scale"]
309 angle_min, angle_max = NORMALIZATION_PRESET["angle"]
310
311 self.position_x = (
312 (self.position_x - world_min) / (world_max - world_min) * num_bins
313 )
314 self.position_y = (
315 (self.position_y - world_min) / (world_max - world_min) * num_bins
316 )
317 self.position_z = (
318 (self.position_z - world_min) / (world_max - world_min) * num_bins
319 )
320 self.angle_z = (self.angle_z - angle_min) / (angle_max - angle_min) * num_bins
321 self.scale_x = (self.scale_x - scale_min) / (scale_max - scale_min) * num_bins
322 self.scale_y = (self.scale_y - scale_min) / (scale_max - scale_min) * num_bins
323 self.scale_z = (self.scale_z - scale_min) / (scale_max - scale_min) * num_bins
324
325 self.position_x = np.clip(int(self.position_x), 0, num_bins - 1)
326 self.position_y = np.clip(int(self.position_y), 0, num_bins - 1)
327 self.position_z = np.clip(int(self.position_z), 0, num_bins - 1)
328 self.angle_z = np.clip(int(self.angle_z), 0, num_bins - 1)
329 self.scale_x = np.clip(int(self.scale_x), 0, num_bins - 1)
330 self.scale_y = np.clip(int(self.scale_y), 0, num_bins - 1)
331 self.scale_z = np.clip(int(self.scale_z), 0, num_bins - 1)
332
333 def undiscretize_and_unnormalize(self, num_bins):
334 world_min, world_max = NORMALIZATION_PRESET["world"]
335 scale_min, scale_max = NORMALIZATION_PRESET["scale"]
336 angle_min, angle_max = NORMALIZATION_PRESET["angle"]
337
338 self.position_x = self.position_x / num_bins
339 self.position_y = self.position_y / num_bins
340 self.position_z = self.position_z / num_bins
341 self.angle_z = self.angle_z / num_bins
342 self.scale_x = self.scale_x / num_bins
343 self.scale_y = self.scale_y / num_bins
344 self.scale_z = self.scale_z / num_bins
345
346 self.position_x = self.position_x * (world_max - world_min) + world_min
347 self.position_y = self.position_y * (world_max - world_min) + world_min
348 self.position_z = self.position_z * (world_max - world_min) + world_min
349 self.angle_z = self.angle_z * (angle_max - angle_min) + angle_min
350 self.scale_x = self.scale_x * (scale_max - scale_min) + scale_min
351 self.scale_y = self.scale_y * (scale_max - scale_min) + scale_min
352 self.scale_z = self.scale_z * (scale_max - scale_min) + scale_min
353
354 def to_language_string(self):
355 capitalized_label = self.entity_label.capitalize()
356 self.id = self.id % 1000
357 # bbox_0=Bbox(class_name,position_x,position_y,position_z,angle_z,scale_x,scale_y,scale_z)
358 language_string = f"{self.entity_label}_{self.id}={capitalized_label}({self.class_name},{self.position_x},{self.position_y},{self.position_z},{self.angle_z},{self.scale_x},{self.scale_y},{self.scale_z})"
359 return language_string
360
361 def sort_key(self):
362 return np.array([self.position_x, self.position_y])