def test_orthographic_kwargs(self): cameras = FoVOrthographicCameras(znear=5.0, zfar=100.0) far = 10.0 P = cameras.get_projection_transform(znear=1.0, zfar=far) vertices = torch.tensor([1, 2, far], dtype=torch.float32) projected_verts = torch.tensor([1, 2, 1], dtype=torch.float32) vertices = vertices[None, None, :] v1 = P.transform_points(vertices) self.assertClose(v1.squeeze(), projected_verts)
def test_orthographic_scaled(self): vertices = torch.tensor([1, 2, 0.5], dtype=torch.float32) vertices = vertices[None, None, :] scale = torch.tensor([[2.0, 0.5, 20]]) # applying the scale puts the z coordinate at the far clipping plane # so the z is mapped to 1.0 projected_verts = torch.tensor([2, 1, 1], dtype=torch.float32) cameras = FoVOrthographicCameras(znear=1.0, zfar=10.0, scale_xyz=scale) P = cameras.get_projection_transform() v1 = P.transform_points(vertices) v2 = orthographic_project_naive(vertices, scale) self.assertClose(v1[..., :2], v2[..., :2]) self.assertClose(v1, projected_verts[None, None])
def test_orthographic_mixed_inputs_broadcast(self): far = torch.tensor([10.0, 20.0]) near = 1.0 cameras = FoVOrthographicCameras(znear=near, zfar=far) P = cameras.get_projection_transform() vertices = torch.tensor([1.0, 2.0, 10.0], dtype=torch.float32) z2 = 1.0 / (20.0 - 1.0) * 10.0 + -1.0 / (20.0 - 1.0) projected_verts = torch.tensor([[1.0, 2.0, 1.0], [1.0, 2.0, z2]], dtype=torch.float32) vertices = vertices[None, None, :] v1 = P.transform_points(vertices) v2 = orthographic_project_naive(vertices) self.assertClose(v1[..., :2], torch.cat([v2, v2])[..., :2]) self.assertClose(v1.squeeze(), projected_verts)
def test_orthographic_mixed_inputs_grad(self): far = torch.tensor([10.0]) near = 1.0 scale = torch.tensor([[1.0, 1.0, 1.0]], requires_grad=True) cameras = FoVOrthographicCameras(znear=near, zfar=far, scale_xyz=scale) P = cameras.get_projection_transform() vertices = torch.tensor([1.0, 2.0, 10.0], dtype=torch.float32) vertices_batch = vertices[None, None, :] v1 = P.transform_points(vertices_batch) v1.sum().backward() self.assertTrue(hasattr(scale, "grad")) scale_grad = scale.grad.clone() grad_scale = torch.tensor([[ vertices[0] * P._matrix[:, 0, 0], vertices[1] * P._matrix[:, 1, 1], vertices[2] * P._matrix[:, 2, 2], ]]) self.assertClose(scale_grad, grad_scale)
def test_orthographic(self): far = 10.0 near = 1.0 cameras = FoVOrthographicCameras(znear=near, zfar=far) P = cameras.get_projection_transform() vertices = torch.tensor([1, 2, far], dtype=torch.float32) projected_verts = torch.tensor([1, 2, 1], dtype=torch.float32) vertices = vertices[None, None, :] v1 = P.transform_points(vertices) v2 = orthographic_project_naive(vertices) self.assertClose(v1[..., :2], v2[..., :2]) self.assertClose(v1.squeeze(), projected_verts) vertices[..., 2] = near projected_verts[2] = 0.0 v1 = P.transform_points(vertices) v2 = orthographic_project_naive(vertices) self.assertClose(v1[..., :2], v2[..., :2]) self.assertClose(v1.squeeze(), projected_verts)