# (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. """Validated camera geometry and pairwise displacement in metric units.""" from __future__ import annotations import torch def camera_validity(poses: torch.Tensor, intrinsics: torch.Tensor) -> torch.Tensor: if poses.ndim != 5 or poses.shape[-2:] != (4, 4): raise ValueError("poses must have shape (B, V, T, 4, 4)") if intrinsics.shape[:3] != poses.shape[:3] or intrinsics.shape[-2:] not in ( (3, 3), (4, 4), ): raise ValueError("intrinsics must match camera (B, V, T) dimensions") with torch.no_grad(), torch.autocast(poses.device.type, enabled=False): pose_finite = torch.isfinite(poses).all(dim=(-2, -1)) k_finite = torch.isfinite(intrinsics).all(dim=(-2, -1)) rotation = torch.where( pose_finite[..., None, None], poses[..., :3, :3].float(), torch.eye(3, device=poses.device), ) calibration = torch.where( k_finite[..., None, None], intrinsics[..., :3, :3].float(), torch.eye(3, device=intrinsics.device), ) (inverse_rotation, rotation_info) = torch.linalg.inv_ex(rotation) (inverse_k, k_info) = torch.linalg.inv_ex(calibration) return ( pose_finite & k_finite & (poses[..., 3, :3].abs() < 1e-06).all(dim=-1) & ((poses[..., 3, 3] - 1).abs() < 1e-06) & (rotation_info == 0) & (k_info == 0) & torch.isfinite(inverse_rotation).all(dim=(-2, -1)) & torch.isfinite(inverse_k).all(dim=(-2, -1)) ).all(dim=(1, 2)) def sanitize_camera_inputs( poses: torch.Tensor, intrinsics: torch.Tensor, aspect_ratio: torch.Tensor | None, condition_mask: torch.Tensor | None, ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor | None]: batch = poses.shape[0] if condition_mask is not None and bool( ( ~torch.isfinite(condition_mask) | (condition_mask != 0) & (condition_mask != 1) ).any() ): raise ValueError("camera condition mask must contain zero or one") keep = ( torch.ones(batch, device=poses.device, dtype=torch.bool) if condition_mask is None else condition_mask.to(device=poses.device, dtype=torch.bool) ) if keep.shape != (batch,): raise ValueError("camera condition mask must have shape (B,)") mask = keep[:, None, None, None, None] with torch.autocast(poses.device.type, enabled=False): safe_poses = torch.where( mask, poses, torch.eye(4, device=poses.device, dtype=poses.dtype) ) safe_k = torch.where( mask, intrinsics.float(), torch.eye(intrinsics.shape[-1], device=intrinsics.device), ) if not bool(camera_validity(safe_poses, safe_k).all()): raise ValueError( "camera-ON samples require finite poses and invertible intrinsics" ) if aspect_ratio is not None: valid = torch.isfinite(aspect_ratio) & (aspect_ratio > 0) active = keep.reshape((batch,) + (1,) * (aspect_ratio.ndim - 1)) if bool((active & ~valid).any()): raise ValueError("camera-ON aspect ratios must be finite and positive") aspect_ratio = torch.where(valid, aspect_ratio, torch.ones_like(aspect_ratio)) return (safe_poses, safe_k, aspect_ratio) def camera_diameter(poses: torch.Tensor) -> torch.Tensor: with torch.autocast(poses.device.type, enabled=False): centres = poses[..., :3, 3].flatten(1, 2) centres = (centres - centres[:, :1]).float() distances = torch.cdist( centres, centres, compute_mode="donot_use_mm_for_euclid_dist" ) return distances.amax(dim=(1, 2)) def center_camera_translations(poses: torch.Tensor) -> torch.Tensor: with torch.autocast(poses.device.type, enabled=False): result = poses.clone() result[..., :3, 3] -= result[:, :1, :1, :3, 3].clone() return result.float() def center_world_to_ray_transforms(rays: torch.Tensor) -> torch.Tensor: with torch.autocast(rays.device.type, enabled=False): result = rays.float().clone() rotation = result[..., :3, :3] translation = result[..., :3, 3] centres = -torch.matmul( rotation.transpose(-1, -2), translation.unsqueeze(-1) ).squeeze(-1) origin = centres.flatten(1, 2).mean(dim=1) result[..., :3, 3] = translation + torch.matmul( rotation, origin[:, None, None, :, None] ).squeeze(-1) return result