| 2408 | |
| 2409 | |
| 2410 | def generate_camera_grids( |
| 2411 | num_x: int, |
| 2412 | num_y: int, |
| 2413 | cam_position_center, # (1, 3) |
| 2414 | delta: float = 0.5, |
| 2415 | ) -> T.Union[torch.Tensor, np.ndarray]: |
| 2416 | if isinstance(cam_position_center, np.ndarray): |
| 2417 | cam_position_center = torch.from_numpy(cam_position_center).float() |
| 2418 | elif isinstance(cam_position_center, (list, tuple)): |
| 2419 | cam_position_center = torch.tensor(cam_position_center).float() |
| 2420 | |
| 2421 | cam_position_center = cam_position_center.float() |
| 2422 | |
| 2423 | ys = torch.zeros_like(cam_position_center) |
| 2424 | ys[..., 2] = 1 |
| 2425 | Rs_c2w_grid = rigid_motion.construct_coord_frame( |
| 2426 | z=-1 * cam_position_center, # (n, 3) |
| 2427 | y=ys, # (n, 3, 3) |
| 2428 | ) |
| 2429 | |
| 2430 | x_sample = torch.arange(num_x) - (num_x - 1) / 2 |
| 2431 | y_sample = torch.arange(num_y) - (num_y - 1) / 2 |
| 2432 | grid_x, grid_y = torch.meshgrid(x_sample, y_sample) |
| 2433 | |
| 2434 | grid_id = torch.stack([grid_x.reshape(-1), grid_y.reshape(-1)], dim=-1) # (num_x*num_y,2) |
| 2435 | cam_positions_w = grid_id @ Rs_c2w_grid[..., 0:2].t() * delta + cam_position_center.unsqueeze(0) |
| 2436 | |
| 2437 | ys = torch.zeros_like(cam_positions_w) |
| 2438 | ys[..., 2] = 1 |
| 2439 | Rs_c2w = rigid_motion.construct_coord_frame( |
| 2440 | z=-1 * cam_positions_w, # (n, 3) |
| 2441 | y=ys, # (n, 3, 3) |
| 2442 | ) |
| 2443 | |
| 2444 | *b_shape, a, b = Rs_c2w.shape |
| 2445 | Hs_c2w = torch.zeros(*b_shape, 4, 4) |
| 2446 | Hs_c2w[..., :3, :3] = Rs_c2w |
| 2447 | Hs_c2w[..., :3, 3] = cam_positions_w |
| 2448 | Hs_c2w[..., 3, 3] = 1 |
| 2449 | |
| 2450 | return Hs_c2w # (n, 4, 4) |
| 2451 | |
| 2452 | |
| 2453 | def generate_camera_polar_grids( |