Transform 6D rays (ray origins and ray directions) using a 4x4 transformation matrix. Args: - ray_origins: HxWx3 or BxHxWx3 torch tensor - ray_directions: HxWx3 or BxHxWx3 torch tensor - transformation: 4x4 or Bx4x4 torch tensor - normalize_to_unit_sphere: b
(ray_origins, ray_directions, transformation)
| 448 | |
| 449 | |
| 450 | def transform_rays(ray_origins, ray_directions, transformation): |
| 451 | """ |
| 452 | Transform 6D rays (ray origins and ray directions) using a 4x4 transformation matrix. |
| 453 | |
| 454 | Args: |
| 455 | - ray_origins: HxWx3 or BxHxWx3 torch tensor |
| 456 | - ray_directions: HxWx3 or BxHxWx3 torch tensor |
| 457 | - transformation: 4x4 or Bx4x4 torch tensor |
| 458 | - normalize_to_unit_sphere: bool, whether to normalize the transformed ray directions to unit length |
| 459 | |
| 460 | Returns: |
| 461 | transformed ray_origins (HxWx3 or BxHxWx3 tensor) and ray_directions (HxWx3 or BxHxWx3 tensor) |
| 462 | """ |
| 463 | # Add batch dimension if not present |
| 464 | if ray_origins.dim() == 3: |
| 465 | ray_origins = ray_origins.unsqueeze(0) |
| 466 | ray_directions = ray_directions.unsqueeze(0) |
| 467 | transformation = transformation.unsqueeze(0) |
| 468 | squeeze_batch_dim = True |
| 469 | else: |
| 470 | squeeze_batch_dim = False |
| 471 | |
| 472 | # Transform ray origins and directions |
| 473 | ray_origins_homo = torch.cat( |
| 474 | [ray_origins, torch.ones_like(ray_origins[..., :1])], dim=-1 |
| 475 | ) |
| 476 | ray_directions_homo = torch.cat( |
| 477 | [ray_directions, torch.zeros_like(ray_directions[..., :1])], dim=-1 |
| 478 | ) |
| 479 | transformed_ray_origins = ein.einsum( |
| 480 | transformation, ray_origins_homo, "b i k, b h w k -> b h w i" |
| 481 | ) |
| 482 | transformed_ray_directions = ein.einsum( |
| 483 | transformation, ray_directions_homo, "b i k, b h w k -> b h w i" |
| 484 | ) |
| 485 | transformed_ray_origins = transformed_ray_origins[..., :3] |
| 486 | transformed_ray_directions = transformed_ray_directions[..., :3] |
| 487 | |
| 488 | # Remove batch dimension if it was added |
| 489 | if squeeze_batch_dim: |
| 490 | transformed_ray_origins = transformed_ray_origins.squeeze(0) |
| 491 | transformed_ray_directions = transformed_ray_directions.squeeze(0) |
| 492 | |
| 493 | return transformed_ray_origins, transformed_ray_directions |
| 494 | |
| 495 | |
| 496 | def convert_z_depth_to_depth_along_ray(z_depth, intrinsics): |
nothing calls this directly
no outgoing calls
no test coverage detected