| 172 | |
| 173 | |
| 174 | def get_rays_np(H, W, focal, c2w): |
| 175 | i, j = np.meshgrid(np.arange(W, dtype=np.float32), np.arange(H, dtype=np.float32), indexing='xy') |
| 176 | dirs = np.stack([(i-W*.5)/focal, -(j-H*.5)/focal, -np.ones_like(i)], -1) |
| 177 | # Rotate ray directions from camera frame to the world frame |
| 178 | rays_d = np.sum(dirs[..., np.newaxis, :] * c2w[:3,:3], -1) # dot product, equals to: [c2w.dot(dir) for dir in dirs] |
| 179 | # Translate camera frame's origin to the world frame. It is the origin of all rays. |
| 180 | rays_o = np.broadcast_to(c2w[:3,-1], np.shape(rays_d)) |
| 181 | return rays_o, rays_d |
| 182 | |
| 183 | |
| 184 | def ndc_rays(H, W, focal, near, rays_o, rays_d): |