def _load_transformed_wall_maps(self, scene_map_info, episode): seen_maps = [] wall_maps = [] start_position = episode.start_position # (X, Y, Z) start_rotation = quaternion_xyzw_to_wxyz(episode.start_rotation) start_heading = compute_heading_from_quaternion(start_rotation) for floor_data in scene_map_info: seen_map = np.load(floor_data["seen_map_path"]) wall_map = np.load(floor_data["wall_map_path"]) # ===== Transform the maps relative to the episode start pose ===== map_view_position = floor_data["world_position"] map_view_heading = floor_data["world_heading"] # Originally, Z is downward and X is rightward. # Convert it to X upward and Y rightward x_map, y_map = -map_view_position[2], map_view_position[0] theta_map = map_view_heading x_start, y_start = -start_position[2], start_position[0] theta_start = start_heading # Compute relative coordinates r_rel = math.sqrt((x_start - x_map) ** 2 + (y_start - y_map) ** 2) phi_rel = math.atan2(y_start - y_map, x_start - x_map) - theta_map x_rel = r_rel * math.cos(phi_rel) / self.config.MAP_SCALE y_rel = r_rel * math.sin(phi_rel) / self.config.MAP_SCALE theta_rel = theta_start - theta_map # Convert these to image coordinates with X being rightward and Y # being downward x_img_rel = y_rel y_img_rel = -x_rel theta_img_rel = theta_rel x_trans = torch.Tensor([[x_img_rel, y_img_rel, theta_img_rel]]) # Perform the transformations p_seen_map = rearrange(torch.Tensor(seen_map), "h w c -> () c h w") p_wall_map = rearrange(torch.Tensor(wall_map), "h w c -> () c h w") p_seen_map_trans = spatial_transform_map(p_seen_map, x_trans) p_wall_map_trans = spatial_transform_map(p_wall_map, x_trans) seen_map_trans = asnumpy(p_seen_map_trans) seen_map_trans = rearrange(seen_map_trans, "() c h w -> h w c") wall_map_trans = asnumpy(p_wall_map_trans) wall_map_trans = rearrange(wall_map_trans, "() c h w -> h w c") seen_maps.append(seen_map_trans) wall_maps.append(wall_map_trans) return seen_maps, wall_maps
def _get_mesh_occupancy(self): agent_position = self.current_episode.start_position agent_rotation = quaternion_xyzw_to_wxyz(self.current_episode.start_rotation) a_x, a_y = maps.to_grid( agent_position[0], agent_position[2], self._coordinate_min, self._coordinate_max, self._map_resolution, ) # The map size here represents size around the agent, not infront. mrange = int(self.map_size * 1.5 / 2.0) # Add extra padding if map range is coordinates go out of bounds y_start = a_y - mrange y_end = a_y + mrange x_start = a_x - mrange x_end = a_x + mrange x_l_pad, y_l_pad, x_r_pad, y_r_pad = 0, 0, 0, 0 H, W = self._top_down_map.shape if x_start < 0: x_l_pad = int(-x_start) x_start += x_l_pad x_end += x_l_pad if x_end >= W: x_r_pad = int(x_end - W + 1) if y_start < 0: y_l_pad = int(-y_start) y_start += y_l_pad y_end += y_l_pad if y_end >= H: y_r_pad = int(y_end - H + 1) ego_map = np.pad(self._top_down_map, ((y_l_pad, y_r_pad), (x_l_pad, x_r_pad))) ego_map = ego_map[y_start : (y_end + 1), x_start : (x_end + 1)] if ego_map.shape[0] == 0 or ego_map.shape[1] == 0: ego_map = np.zeros((2 * mrange + 1, 2 * mrange + 1), dtype=np.uint8) # Rotate to get egocentric map # Negative since the value returned is clockwise rotation about Y, # but we need anti-clockwise rotation agent_heading = -compute_heading_from_quaternion(agent_rotation) agent_heading = math.degrees(agent_heading) half_size = ego_map.shape[0] // 2 center = (half_size, half_size) M = cv2.getRotationMatrix2D(center, agent_heading, scale=1.0) ego_map = cv2.warpAffine( ego_map, M, (ego_map.shape[1], ego_map.shape[0]), flags=cv2.INTER_NEAREST, borderMode=cv2.BORDER_CONSTANT, borderValue=(1,), ) ego_map = ego_map.astype(np.float32) mrange = int(self.map_size / 2.0) start_coor = half_size - mrange end_coor = int(start_coor + self.map_size - 1) ego_map = ego_map[start_coor : (end_coor + 1), start_coor : (end_coor + 1)] # This map is currently 0 if occupied and 1 if unoccupied. Flip it. ego_map = 1.0 - ego_map # Flip the x axis because to_grid() flips the conventions ego_map = np.flip(ego_map, axis=1) # Append explored status in the 2nd channel ego_map = np.stack([ego_map, np.ones_like(ego_map)], axis=2) return ego_map
def _get_wall_occupancy(self, episode, agent_state): episode_id = (episode.episode_id, episode.scene_id) # Load the episode specific maps only if the episode has changed if self.current_wall_episode_id != episode_id: self.current_wall_episode_id = episode_id if self.config.GT_TYPE == "wall_occupancy": scene_id = episode.scene_id.split("/")[-1] self._scene_maps_info = self._all_maps_info[scene_id] # Load the maps per floor seen_maps, wall_maps = self._load_transformed_wall_maps( self._scene_maps_info, episode, ) self._scene_maps = {} self._scene_maps["seen_maps"] = seen_maps self._scene_maps["wall_maps"] = wall_maps agent_state = self._sim.get_agent_state() current_height = agent_state.position[1] best_floor_idx = None best_floor_dist = math.inf for floor_idx, floor_data in enumerate(self._scene_maps_info): floor_height = floor_data["floor_height"] if abs(current_height - floor_height) < best_floor_dist: best_floor_idx = floor_idx best_floor_dist = abs(current_height - floor_height) assert best_floor_idx is not None current_wall_map = self._scene_maps["wall_maps"][best_floor_idx] # Take only channel 0 as both channels have save values current_wall_map = current_wall_map[..., 0] # ========= Get local egocentric crop of the current wall map ========= # Compute relative pose of agent from start location start_position = episode.start_position # (X, Y, Z) start_rotation = quaternion_xyzw_to_wxyz(episode.start_rotation) start_heading = compute_heading_from_quaternion(start_rotation) start_pose = torch.Tensor( [[-start_position[2], start_position[0], start_heading]] ) agent_position = agent_state.position agent_heading = compute_heading_from_quaternion(agent_state.rotation) agent_pose = torch.Tensor( [[-agent_position[2], agent_position[0], agent_heading]] ) rel_pose = subtract_pose(start_pose, agent_pose)[0] # (3,) # Compute agent position on the map image map_scale = self.config.MAP_SCALE H, W = current_wall_map.shape[:2] Hby2, Wby2 = (H + 1) // 2, (W + 1) // 2 agent_map_x = int(rel_pose[1].item() / map_scale + Wby2) agent_map_y = int(-rel_pose[0].item() / map_scale + Hby2) # Crop the region around the agent. mrange = int(1.5 * self.map_size) # Add extra padding if map range is coordinates go out of bounds y_start = agent_map_y - mrange y_end = agent_map_y + mrange x_start = agent_map_x - mrange x_end = agent_map_x + mrange x_l_pad, y_l_pad, x_r_pad, y_r_pad = 0, 0, 0, 0 H, W = current_wall_map.shape if x_start < 0: x_l_pad = int(-x_start) x_start += x_l_pad x_end += x_l_pad if x_end >= W: x_r_pad = int(x_end - W + 1) if y_start < 0: y_l_pad = int(-y_start) y_start += y_l_pad y_end += y_l_pad if y_end >= H: y_r_pad = int(y_end - H + 1) ego_map = np.pad(current_wall_map, ((y_l_pad, y_r_pad), (x_l_pad, x_r_pad))) ego_map = ego_map[y_start: (y_end + 1), x_start: (x_end + 1)] agent_heading = rel_pose[2].item() agent_heading = math.degrees(agent_heading) half_size = ego_map.shape[0] // 2 center = (half_size, half_size) M = cv2.getRotationMatrix2D(center, agent_heading, scale=1.0) ego_map = cv2.warpAffine( ego_map, M, (ego_map.shape[1], ego_map.shape[0]), flags=cv2.INTER_NEAREST, borderMode=cv2.BORDER_CONSTANT, borderValue=(1,), ) ego_map = ego_map.astype(np.float32) mrange = int(self.map_size) ego_map = ego_map[ (half_size - mrange): (half_size + mrange), (half_size - mrange): (half_size + mrange), ] # Get forward region infront of the agent half_size = ego_map.shape[0] // 2 quarter_size = ego_map.shape[0] // 4 center = (half_size, half_size) ego_map = ego_map[0:half_size, quarter_size: (quarter_size + half_size)] # Append explored status in the 2nd channel ego_map = np.stack([ego_map, ego_map], axis=2) return ego_map