import numpy as np import torch def fibonacci_sphere(num_points: int, radius: float = 1.0): ga = (3 - np.sqrt(5)) * np.pi # golden angle in radians theta = ga * np.arange(num_points) z = np.linspace(1/num_points - 1, 1 - 1/num_points, num_points) r = np.sqrt(1 - z * z) x = r * np.cos(theta) y = r * np.sin(theta) # Combine into a (num_points, 3) array and scale by radius points = np.vstack((x, y, z)).T * radius return points def ray_sample(cam2world_matrix, intrinsics, grid): """ Create batches of rays and return origins and directions. cam2world_matrix: (N, 4, 4) intrinsics: (N, 3, 3) resolution: int ray_origins: (N, M, 3) ray_dirs: (N, M, 3) """ cam_locs_world = cam2world_matrix[:, :3, 3] fx = intrinsics[:, 0, 0] fy = intrinsics[:, 1, 1] cx = intrinsics[:, 0, 2] cy = intrinsics[:, 1, 2] sk = intrinsics[:, 0, 1] #move grid from (B*num_views, d1, d2, 2) --> (B*num_views, d1*d2, 2) B_and_num_views, d1, d2, _ = grid.shape N, M = cam2world_matrix.shape[0], d1**2 grid = grid.view(B_and_num_views, d1*d2, 2) #move grid from [-1, 1] to [0, 1] uv = (grid + 1)/2 # uv = torch.stack( # torch.meshgrid( # torch.arange( # resolution, dtype=torch.float32, device=cam2world_matrix.device # ), # torch.arange( # resolution, dtype=torch.float32, device=cam2world_matrix.device # ), # indexing="ij", # ) # ) * (1.0 / resolution) + (0.5 / resolution) # uv = repeat(uv, "c h w -> b (h w) c", b=N) x_cam = uv[:, :, 0].view(N, -1) y_cam = uv[:, :, 1].view(N, -1) z_cam = torch.ones((N, M), device=cam2world_matrix.device) x_lift = ( ( x_cam - cx.unsqueeze(-1) + cy.unsqueeze(-1) * sk.unsqueeze(-1) / fy.unsqueeze(-1) - sk.unsqueeze(-1) * y_cam / fy.unsqueeze(-1) ) / fx.unsqueeze(-1) * z_cam ) y_lift = (y_cam - cy.unsqueeze(-1)) / fy.unsqueeze(-1) * z_cam cam_rel_points = torch.stack( (x_lift, y_lift, z_cam, torch.ones_like(z_cam)), dim=-1 ) world_rel_points = torch.bmm( cam2world_matrix, cam_rel_points.permute(0, 2, 1) ).permute(0, 2, 1)[:, :, :3] ray_dirs = world_rel_points - cam_locs_world[:, None, :] ray_dirs = torch.nn.functional.normalize(ray_dirs, dim=2) ray_origins = cam_locs_world.unsqueeze(1).repeat(1, ray_dirs.shape[1], 1) return ray_origins, ray_dirs def intersect_ray_sphere_batch(ray_origins, ray_dirs, sphere_center, sphere_radius): """ ray_origins: [B, N, 3] ray_dirs: [B, N, 3] sphere_center: [3] oppure [1, 1, 3] sphere_radius: float o tensor scalare """ # Assicura shape broadcastabili if sphere_center.ndim == 1: sphere_center = sphere_center.view(1, 1, 3) if isinstance(sphere_radius, float): sphere_radius = torch.tensor(sphere_radius, dtype=ray_origins.dtype, device=ray_origins.device) # OC: vettore dall'origine del raggio al centro della sfera oc = ray_origins - sphere_center # [B, N, 3] a = torch.sum(ray_dirs ** 2, dim=-1) # [B, N] b = 2.0 * torch.sum(oc * ray_dirs, dim=-1) # [B, N] c = torch.sum(oc ** 2, dim=-1) - sphere_radius**2 # [B, N] discriminant = b**2 - 4*a*c # [B, N] valid = discriminant >= 0 # [B, N] # Prepara t come zeri t = torch.zeros_like(a) # [B, N] sqrt_disc = torch.sqrt(discriminant[valid]) t_valid = (-b[valid] - sqrt_disc) / (2.0 * a[valid]) t[valid] = t_valid # Calcola il punto di intersezione intersection = ray_origins + t.unsqueeze(-1) * ray_dirs # [B, N, 3] return intersection, valid