MCPcopy Create free account
hub / github.com/apple/ml-pointersect / Test_Generate_Cam_Rays

Class Test_Generate_Cam_Rays

tests/plib/test_utils.py:118–172  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

116
117
118class Test_Generate_Cam_Rays(unittest.TestCase):
119
120 def test_1(self):
121 m = 1
122 width_px, height_px = 100, 50
123 f = 40.
124 cam_poses = rigid_motion.generate_random_camera_poses(
125 n=m,
126 max_angle=180.,
127 min_r=0.5,
128 max_r=1.5,
129 )
130 cam_poses = utils.to_tensor(cam_poses, dtype=torch.float)
131 cam_poses = torch.stack(cam_poses, dim=0) # (m, 4, 4)
132
133 intrinsics = torch.tensor([
134 [f, 0., width_px * 0.5],
135 [0., f, height_px * 0.5],
136 [0, 0, 1],
137 ]).expand(m ,3, 3) # (m, 3, 3)
138
139 ray_origins_w, ray_directions_w = utils.generate_camera_rays(
140 cam_poses=cam_poses,
141 intrinsics=intrinsics,
142 width_px=width_px,
143 height_px=height_px,
144 subsample=1,
145 )
146
147 # o3d ray
148 H_w2c = torch.linalg.inv(cam_poses) # (m, 4, 4)
149 o3d_ray_origins = []
150 o3d_ray_directions = []
151 for i in range(m):
152 rays = o3d.t.geometry.RaycastingScene.create_rays_pinhole(
153 intrinsic_matrix=intrinsics[i].numpy(),
154 extrinsic_matrix=H_w2c[i].numpy(),
155 width_px=width_px,
156 height_px=height_px,
157 ) # (target_height_px, target_width_px, 6) [ox, oy, oz, dx, dy, dz] origin is the pinhole
158 rays = torch.from_numpy(rays.numpy()) # (h, w, 6)
159 ray_o = rays[:, :, :3]
160 ray_d = rays[:, :, 3:]
161
162 o3d_ray_origins.append(ray_o)
163 o3d_ray_directions.append(ray_d)
164 o3d_ray_origins = torch.stack(o3d_ray_origins, dim=0) # (m, h, w, 3)
165 o3d_ray_directions = torch.stack(o3d_ray_directions, dim=0) # (m, h, w, 3)
166 o3d_ray_directions = o3d_ray_directions / torch.linalg.vector_norm(o3d_ray_directions, dim=-1, keepdims=True)
167
168
169 assert torch.allclose(o3d_ray_origins, ray_origins_w)
170 assert torch.allclose(o3d_ray_directions, ray_directions_w, rtol=1e-4, atol=1e-4)
171
172 diff = torch.linalg.vector_norm(o3d_ray_directions - ray_directions_w, dim=-1)
173
174
175

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected