(H, W, focal, c2w)
| 3 | |
| 4 | # Ray helpers |
| 5 | def get_rays(H, W, focal, c2w): |
| 6 | i, j = torch.meshgrid(torch.linspace(0, W-1, W), torch.linspace(0, H-1, H), indexing='ij') # pytorch's meshgrid has indexing='ij' |
| 7 | i = i.t() |
| 8 | j = j.t() |
| 9 | dirs = torch.stack([(i-W*.5)/focal, -(j-H*.5)/focal, -torch.ones_like(i)], -1) |
| 10 | |
| 11 | # Rotate ray directions from camera frame to the world frame |
| 12 | rays_d = torch.sum(dirs[..., np.newaxis, :] * c2w[:3,:3], -1) # dot product, equals to: [c2w.dot(dir) for dir in dirs] |
| 13 | # Translate camera frame's origin to the world frame. It is the origin of all rays. |
| 14 | rays_o = c2w[:3,-1].expand(rays_d.shape) |
| 15 | return rays_o, rays_d # rays_o (100,100,3), rays_d (100,100,3) |
| 16 | |
| 17 | |
| 18 | def get_rays_np(H, W, focal, c2w): |
no outgoing calls
no test coverage detected