| 160 | |
| 161 | # Ray helpers |
| 162 | def get_rays(H, W, focal, c2w): |
| 163 | i, j = torch.meshgrid(torch.linspace(0, W-1, W), torch.linspace(0, H-1, H)) # pytorch's meshgrid has indexing='ij' |
| 164 | i = i.t() |
| 165 | j = j.t() |
| 166 | dirs = torch.stack([(i-W*.5)/focal, -(j-H*.5)/focal, -torch.ones_like(i)], -1) |
| 167 | # Rotate ray directions from camera frame to the world frame |
| 168 | rays_d = torch.sum(dirs[..., np.newaxis, :] * c2w[:3,:3], -1) # dot product, equals to: [c2w.dot(dir) for dir in dirs] |
| 169 | # Translate camera frame's origin to the world frame. It is the origin of all rays. |
| 170 | rays_o = c2w[:3,-1].expand(rays_d.shape) |
| 171 | return rays_o, rays_d |
| 172 | |
| 173 | |
| 174 | def get_rays_np(H, W, focal, c2w): |