Elffuss BraidHub

kikocis/SpatialLM public

5ec8f267b45b · 13.1 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
78 def normalize_and_discretize(self, num_bins):
79 height_min, height_max = NORMALIZATION_PRESET["height"]
80 world_min, world_max = NORMALIZATION_PRESET["world"]
81
82 self.height = (self.height - height_min) / (height_max - height_min) * num_bins
83 self.thickness = (
84 (self.thickness - height_min) / (height_max - height_min) * num_bins
85 )
86 self.ax = (self.ax - world_min) / (world_max - world_min) * num_bins
87 self.ay = (self.ay - world_min) / (world_max - world_min) * num_bins
88 self.az = (self.az - world_min) / (world_max - world_min) * num_bins
89 self.bx = (self.bx - world_min) / (world_max - world_min) * num_bins
90 self.by = (self.by - world_min) / (world_max - world_min) * num_bins
91 self.bz = (self.bz - world_min) / (world_max - world_min) * num_bins
92
93 self.height = np.clip(self.height, 0, num_bins - 1)
94 self.thickness = np.clip(self.thickness, 0, num_bins - 1)
95 self.ax = np.clip(self.ax, 0, num_bins - 1)
96 self.ay = np.clip(self.ay, 0, num_bins - 1)
97 self.az = np.clip(self.az, 0, num_bins - 1)
98 self.bx = np.clip(self.bx, 0, num_bins - 1)
99 self.by = np.clip(self.by, 0, num_bins - 1)
100 self.bz = np.clip(self.bz, 0, num_bins - 1)
101
102 def undiscretize_and_unnormalize(self, num_bins):
103 height_min, height_max = NORMALIZATION_PRESET["height"]
104 world_min, world_max = NORMALIZATION_PRESET["world"]
105
106 # undiscretize
107 self.height = self.height / num_bins
108 self.thickness = self.thickness / num_bins
109 self.ax = self.ax / num_bins
110 self.ay = self.ay / num_bins
111 self.az = self.az / num_bins
112 self.bx = self.bx / num_bins
113 self.by = self.by / num_bins
114 self.bz = self.bz / num_bins
115
116 # unnormalize
117 self.height = self.height * (height_max - height_min) + height_min
118 self.thickness = self.thickness * (height_max - height_min) + height_min
119 self.ax = self.ax * (world_max - world_min) + world_min
120 self.ay = self.ay * (world_max - world_min) + world_min
121 self.az = self.az * (world_max - world_min) + world_min
122 self.bx = self.bx * (world_max - world_min) + world_min
123 self.by = self.by * (world_max - world_min) + world_min
124 self.bz = self.bz * (world_max - world_min) + world_min
125
126 def to_language_string(self):
127 capitalized_label = self.entity_label.capitalize()
128 # wall_0=Wall(a_x,a_y,a_z,b_x,b_y,b_z,height,thickness)
129 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})"
130 return language_string
131
132
133@dataclass
134class Door:
135 id: int
136 wall_id: int
137 position_x: float
138 position_y: float
139 position_z: float
140 width: float
141 height: float
142 entity_label: str = "door"
143
144 def __post_init__(self):
145 self.id = int(self.id)
146 self.wall_id = int(self.wall_id)
147 self.position_x = float(self.position_x)
148 self.position_y = float(self.position_y)
149 self.position_z = float(self.position_z)
150 self.width = float(self.width)
151 self.height = float(self.height)
152
153 def rotate(self, angle: float):
154 center = np.array([self.position_x, self.position_y, self.position_z])
155 rotmat = R.from_rotvec([0, 0, angle]).as_matrix()
156 new_center = rotmat @ center
157
158 self.position_x = new_center[0]
159 self.position_y = new_center[1]
160 self.position_z = new_center[2]
161
162 def translate(self, translation: np.ndarray):
163 self.position_x += translation[0]
164 self.position_y += translation[1]
165 self.position_z += translation[2]
166
167 def scale(self, scaling: float):
168 self.width *= scaling
169 self.height *= scaling
170 self.position_x *= scaling
171 self.position_y *= scaling
172 self.position_z *= scaling
173
174 def normalize_and_discretize(self, num_bins):
175 width_min, width_max = NORMALIZATION_PRESET["width"]
176 height_min, height_max = NORMALIZATION_PRESET["height"]
177 world_min, world_max = NORMALIZATION_PRESET["world"]
178
179 self.width = (self.width - width_min) / (width_max - width_min) * num_bins
180 self.height = (self.height - height_min) / (height_max - height_min) * num_bins
181 self.position_x = (
182 (self.position_x - world_min) / (world_max - world_min) * num_bins
183 )
184 self.position_y = (
185 (self.position_y - world_min) / (world_max - world_min) * num_bins
186 )
187 self.position_z = (
188 (self.position_z - world_min) / (world_max - world_min) * num_bins
189 )
190
191 self.width = np.clip(self.width, 0, num_bins - 1)
192 self.height = np.clip(self.height, 0, num_bins - 1)
193 self.position_x = np.clip(self.position_x, 0, num_bins - 1)
194 self.position_y = np.clip(self.position_y, 0, num_bins - 1)
195 self.position_z = np.clip(self.position_z, 0, num_bins - 1)
196
197 def undiscretize_and_unnormalize(self, num_bins):
198 width_min, width_max = NORMALIZATION_PRESET["width"]
199 height_min, height_max = NORMALIZATION_PRESET["height"]
200 world_min, world_max = NORMALIZATION_PRESET["world"]
201
202 # undiscretize
203 self.width = self.width / num_bins
204 self.height = self.height / num_bins
205 self.position_x = self.position_x / num_bins
206 self.position_y = self.position_y / num_bins
207 self.position_z = self.position_z / num_bins
208
209 # unnormalize
210 self.width = self.width * (width_max - width_min) + width_min
211 self.height = self.height * (height_max - height_min) + height_min
212 self.position_x = self.position_x * (world_max - world_min) + world_min
213 self.position_y = self.position_y * (world_max - world_min) + world_min
214 self.position_z = self.position_z * (world_max - world_min) + world_min
215
216 def to_language_string(self):
217 capitalized_label = self.entity_label.capitalize()
218 self.id = self.id % 1000
219 # door_0=Door(wall_id,position_x,position_y,position_z,width,height)
220 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})"
221 return language_string
222
223
224@dataclass
225class Window(Door):
226 entity_label: str = "window"
227
228
229@dataclass
230class Bbox:
231 id: int
232 class_name: str
233 position_x: float
234 position_y: float
235 position_z: float
236 angle_z: float
237 scale_x: float
238 scale_y: float
239 scale_z: float
240 entity_label: str = "bbox"
241
242 def __post_init__(self):
243 self.id = int(self.id)
244 self.class_name = str(self.class_name)
245 self.position_x = float(self.position_x)
246 self.position_y = float(self.position_y)
247 self.position_z = float(self.position_z)
248 self.angle_z = float(self.angle_z)
249 self.scale_x = abs(float(self.scale_x))
250 self.scale_y = abs(float(self.scale_y))
251 self.scale_z = abs(float(self.scale_z))
252
253 def rotate(self, angle: float):
254 augment_rot_mat = R.from_rotvec([0, 0, angle]).as_matrix()
255 bbox_rot_mat = R.from_rotvec([0, 0, self.angle_z]).as_matrix()
256 new_bbox_rot_mat = augment_rot_mat @ bbox_rot_mat
257 new_angle_z = R.from_matrix(new_bbox_rot_mat).as_euler("ZYX")[0]
258 new_angle_z = (new_angle_z + np.pi) % (2 * np.pi) - np.pi # Range: [-pi, pi)
259
260 # Bbox is symmetric
261 symmetry = np.pi
262 if np.isclose(self.scale_x, self.scale_y, atol=1e-3):
263 symmetry = np.pi / 2
264 new_angle_z = (new_angle_z + np.pi) % symmetry - np.pi
265 self.angle_z = new_angle_z
266
267 bbox_center = np.array([self.position_x, self.position_y, self.position_z])
268 bbox_center = augment_rot_mat @ bbox_center
269 self.position_x = bbox_center[0]
270 self.position_y = bbox_center[1]
271 self.position_z = bbox_center[2]
272
273 def translate(self, translation: np.ndarray):
274 self.position_x += translation[0]
275 self.position_y += translation[1]
276 self.position_z += translation[2]
277
278 def scale(self, scaling: float):
279 self.scale_x *= scaling
280 self.scale_y *= scaling
281 self.scale_z *= scaling
282 self.position_x *= scaling
283 self.position_y *= scaling
284 self.position_z *= scaling
285
286 def normalize_and_discretize(self, num_bins):
287 world_min, world_max = NORMALIZATION_PRESET["world"]
288 scale_min, scale_max = NORMALIZATION_PRESET["scale"]
289 angle_min, angle_max = NORMALIZATION_PRESET["angle"]
290
291 self.position_x = (
292 (self.position_x - world_min) / (world_max - world_min) * num_bins
293 )
294 self.position_y = (
295 (self.position_y - world_min) / (world_max - world_min) * num_bins
296 )
297 self.position_z = (
298 (self.position_z - world_min) / (world_max - world_min) * num_bins
299 )
300 self.angle_z = (self.angle_z - angle_min) / (angle_max - angle_min) * num_bins
301 self.scale_x = (self.scale_x - scale_min) / (scale_max - scale_min) * num_bins
302 self.scale_y = (self.scale_y - scale_min) / (scale_max - scale_min) * num_bins
303 self.scale_z = (self.scale_z - scale_min) / (scale_max - scale_min) * num_bins
304
305 self.position_x = np.clip(self.position_x, 0, num_bins - 1)
306 self.position_y = np.clip(self.position_y, 0, num_bins - 1)
307 self.position_z = np.clip(self.position_z, 0, num_bins - 1)
308 self.angle_z = np.clip(self.angle_z, 0, num_bins - 1)
309 self.scale_x = np.clip(self.scale_x, 0, num_bins - 1)
310 self.scale_y = np.clip(self.scale_y, 0, num_bins - 1)
311 self.scale_z = np.clip(self.scale_z, 0, num_bins - 1)
312
313 def undiscretize_and_unnormalize(self, num_bins):
314 world_min, world_max = NORMALIZATION_PRESET["world"]
315 scale_min, scale_max = NORMALIZATION_PRESET["scale"]
316 angle_min, angle_max = NORMALIZATION_PRESET["angle"]
317
318 self.position_x = self.position_x / num_bins
319 self.position_y = self.position_y / num_bins
320 self.position_z = self.position_z / num_bins
321 self.angle_z = self.angle_z / num_bins
322 self.scale_x = self.scale_x / num_bins
323 self.scale_y = self.scale_y / num_bins
324 self.scale_z = self.scale_z / num_bins
325
326 self.position_x = self.position_x * (world_max - world_min) + world_min
327 self.position_y = self.position_y * (world_max - world_min) + world_min
328 self.position_z = self.position_z * (world_max - world_min) + world_min
329 self.angle_z = self.angle_z * (angle_max - angle_min) + angle_min
330 self.scale_x = self.scale_x * (scale_max - scale_min) + scale_min
331 self.scale_y = self.scale_y * (scale_max - scale_min) + scale_min
332 self.scale_z = self.scale_z * (scale_max - scale_min) + scale_min
333
334 def to_language_string(self):
335 capitalized_label = self.entity_label.capitalize()
336 self.id = self.id % 1000
337 # bbox_0=Bbox(class_name,position_x,position_y,position_z,angle_z,scale_x,scale_y,scale_z)
338 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})"
339 return language_string