| 1 | import numpy as np |
| 2 | from scipy.spatial.transform import Rotation as R |
| 3 | from spatiallm.layout.entity import Wall, Door, Window, Bbox, NORMALIZATION_PRESET |
| 4 | |
| 5 | |
| 6 | class Layout: |
| 7 | def __init__(self, str: str = None): |
| 8 | self.walls = [] |
| 9 | self.doors = [] |
| 10 | self.windows = [] |
| 11 | self.bboxes = [] |
| 12 | |
| 13 | if str: |
| 14 | self.from_str(str) |
| 15 | |
| 16 | @staticmethod |
| 17 | def get_grid_size(): |
| 18 | world_min, world_max = NORMALIZATION_PRESET["world"] |
| 19 | return (world_max - world_min) / NORMALIZATION_PRESET["num_bins"] |
| 20 | |
| 21 | @staticmethod |
| 22 | def get_num_bins(): |
| 23 | return NORMALIZATION_PRESET["num_bins"] |
| 24 | |
| 25 | def from_str(self, s: str): |
| 26 | s = s.lstrip("\n") |
| 27 | lines = s.split("\n") |
| 28 | # wall lookup table |
| 29 | existing_walls = [] |
| 30 | for line in lines: |
| 31 | try: |
| 32 | label = line.split("=")[0] |
| 33 | entity_id = int(label.split("_")[1]) |
| 34 | entity_label = label.split("_")[0] |
| 35 | |
| 36 | # extract params |
| 37 | start_pos = line.find("(") |
| 38 | end_pos = line.find(")") |
| 39 | params = line[start_pos + 1 : end_pos].split(",") |
| 40 | |
| 41 | if entity_label == Wall.entity_label: |
| 42 | wall_args = [ |
| 43 | "ax", |
| 44 | "ay", |
| 45 | "az", |
| 46 | "bx", |
| 47 | "by", |
| 48 | "bz", |
| 49 | "height", |
| 50 | "thickness", |
| 51 | ] |
| 52 | wall_params = dict(zip(wall_args, params[0:8])) |
| 53 | entity = Wall(id=entity_id, **wall_params) |
| 54 | existing_walls.append(entity_id) |
| 55 | self.walls.append(entity) |
| 56 | elif entity_label == Door.entity_label: |
| 57 | wall_id = int(params[0].split("_")[1]) |
| 58 | if wall_id not in existing_walls: |
| 59 | continue |
| 60 | |
| 61 | door_args = [ |
| 62 | "position_x", |
| 63 | "position_y", |
| 64 | "position_z", |
| 65 | "width", |
| 66 | "height", |
| 67 | ] |
| 68 | door_params = dict(zip(door_args, params[1:6])) |
| 69 | entity = Door( |
| 70 | id=entity_id, |
| 71 | wall_id=wall_id, |
| 72 | **door_params, |
| 73 | ) |
| 74 | self.doors.append(entity) |
| 75 | elif entity_label == Window.entity_label: |
| 76 | wall_id = int(params[0].split("_")[1]) |
| 77 | if wall_id not in existing_walls: |
| 78 | continue |
| 79 | |
| 80 | window_args = [ |
| 81 | "position_x", |
| 82 | "position_y", |
| 83 | "position_z", |
| 84 | "width", |
| 85 | "height", |
| 86 | ] |
| 87 | window_params = dict(zip(window_args, params[1:6])) |
| 88 | entity = Window( |
| 89 | id=entity_id, |
| 90 | wall_id=wall_id, |
| 91 | **window_params, |
| 92 | ) |
| 93 | self.windows.append(entity) |
| 94 | elif entity_label == Bbox.entity_label: |
| 95 | class_name = params[0] |
| 96 | bbox_args = [ |
| 97 | "position_x", |
| 98 | "position_y", |
| 99 | "position_z", |
| 100 | "angle_z", |
| 101 | "scale_x", |
| 102 | "scale_y", |
| 103 | "scale_z", |
| 104 | ] |
| 105 | bbox_params = dict(zip(bbox_args, params[1:8])) |
| 106 | entity = Bbox( |
| 107 | id=entity_id, |
| 108 | class_name=class_name, |
| 109 | **bbox_params, |
| 110 | ) |
| 111 | self.bboxes.append(entity) |
| 112 | except Exception as e: |
| 113 | continue |
| 114 | |
| 115 | def to_boxes(self): |
| 116 | boxes = [] |
| 117 | lookup = {} |
| 118 | for wall in self.walls: |
| 119 | # assume the walls has a thickness of 0.0 for now |
| 120 | thickness = 0.0 |
| 121 | corner_a = np.array([wall.ax, wall.ay, wall.az]) |
| 122 | corner_b = np.array([wall.bx, wall.by, wall.bz]) |
| 123 | length = np.linalg.norm(corner_a - corner_b) |
| 124 | direction = corner_b - corner_a |
| 125 | angle = np.arctan2(direction[1], direction[0]) |
| 126 | lookup[wall.id] = {"wall": wall, "angle": angle} |
| 127 | |
| 128 | center = (corner_a + corner_b) * 0.5 + np.array([0, 0, 0.5 * wall.height]) |
| 129 | scale = np.array([length, thickness, wall.height]) |
| 130 | rotation = R.from_rotvec([0, 0, angle]).as_matrix() |
| 131 | box = { |
| 132 | "id": wall.id, |
| 133 | "class": Wall.entity_label, |
| 134 | "label": Wall.entity_label, |
| 135 | "center": center, |
| 136 | "rotation": rotation, |
| 137 | "scale": scale, |
| 138 | } |
| 139 | boxes.append(box) |
| 140 | |
| 141 | for fixture in self.doors + self.windows: |
| 142 | wall_id = fixture.wall_id |
| 143 | wall_info = lookup.get(wall_id, None) |
| 144 | if wall_info is None: |
| 145 | continue |
| 146 | |
| 147 | wall = wall_info["wall"] |
| 148 | angle = wall_info["angle"] |
| 149 | thickness = wall.thickness |
| 150 | |
| 151 | center = np.array( |
| 152 | [fixture.position_x, fixture.position_y, fixture.position_z] |
| 153 | ) |
| 154 | scale = np.array([fixture.width, thickness, fixture.height]) |
| 155 | rotation = R.from_rotvec([0, 0, angle]).as_matrix() |
| 156 | class_prefix = 1000 if fixture.entity_label == Door.entity_label else 2000 |
| 157 | box = { |
| 158 | "id": fixture.id + class_prefix, |
| 159 | "class": fixture.entity_label, |
| 160 | "label": fixture.entity_label, |
| 161 | "center": center, |
| 162 | "rotation": rotation, |
| 163 | "scale": scale, |
| 164 | } |
| 165 | boxes.append(box) |
| 166 | |
| 167 | for bbox in self.bboxes: |
| 168 | center = np.array([bbox.position_x, bbox.position_y, bbox.position_z]) |
| 169 | scale = np.array([bbox.scale_x, bbox.scale_y, bbox.scale_z]) |
| 170 | rotation = R.from_rotvec([0, 0, bbox.angle_z]).as_matrix() |
| 171 | class_name = bbox.class_name |
| 172 | box = { |
| 173 | "id": bbox.id + 3000, |
| 174 | "class": Bbox.entity_label, |
| 175 | "label": class_name, |
| 176 | "center": center, |
| 177 | "rotation": rotation, |
| 178 | "scale": scale, |
| 179 | } |
| 180 | boxes.append(box) |
| 181 | |
| 182 | return boxes |
| 183 | |
| 184 | def get_entities(self): |
| 185 | return self.walls + self.doors + self.windows + self.bboxes |
| 186 | |
| 187 | def normalize_and_discretize(self): |
| 188 | for entity in self.get_entities(): |
| 189 | entity.normalize_and_discretize() |
| 190 | |
| 191 | def undiscretize_and_unnormalize(self): |
| 192 | for entity in self.get_entities(): |
| 193 | entity.undiscretize_and_unnormalize() |
| 194 | |
| 195 | def translate(self, translation: np.ndarray): |
| 196 | for entity in self.get_entities(): |
| 197 | entity.translate(translation) |
| 198 | |
| 199 | def rotate(self, angle: float): |
| 200 | for entity in self.get_entities(): |
| 201 | entity.rotate(angle) |
| 202 | |
| 203 | def scale(self, scale: float): |
| 204 | for entity in self.get_entities(): |
| 205 | entity.scale(scale) |
| 206 | |
| 207 | def to_language_string(self): |
| 208 | entity_strings = [] |
| 209 | for entity in self.get_entities(): |
| 210 | entity_strings.append(entity.to_language_string()) |
| 211 | return "\n".join(entity_strings) |