def test_rotate_x_torch_tensor(self): angle = torch.tensor([0, 45.0, 90.0]) # (N) t = RotateAxisAngle(angle=angle, axis="X") r2_i = 1 / math.sqrt(2) r2_2 = math.sqrt(2) / 2 # fmt: off matrix = torch.tensor( [ [ [1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0], ], [ [1.0, 0.0, 0.0, 0.0], # noqa: E241, E201 [0.0, r2_2, -r2_i, 0.0], # noqa: E241, E201 [0.0, r2_i, r2_2, 0.0], # noqa: E241, E201 [0.0, 0.0, 0.0, 1.0], # noqa: E241, E201 ], [ [1.0, 0.0, 0.0, 0.0], # noqa: E241, E201 [0.0, 0.0, -1.0, 0.0], # noqa: E241, E201 [0.0, 1.0, 0.0, 0.0], # noqa: E241, E201 [0.0, 0.0, 0.0, 1.0], # noqa: E241, E201 ] ], dtype=torch.float32, ) # fmt: on self.assertTrue(torch.allclose(t._matrix, matrix, atol=1e-7)) angle = angle[..., None] # (N, 1) t = RotateAxisAngle(angle=angle, axis="X") self.assertTrue(torch.allclose(t._matrix, matrix, atol=1e-7))
def test_rotate_y_torch_scalar(self): """ Test rotation about Y axis. With a right hand coordinate system this should result in a vector pointing along the x-axis being rotated to point along the negative z axis. """ angle = torch.tensor(90.0) t = RotateAxisAngle(angle=angle, axis="Y") # fmt: off matrix = torch.tensor( [[ [0.0, 0.0, -1.0, 0.0], # noqa: E241, E201 [0.0, 1.0, 0.0, 0.0], # noqa: E241, E201 [1.0, 0.0, 0.0, 0.0], # noqa: E241, E201 [0.0, 0.0, 0.0, 1.0], # noqa: E241, E201 ]], dtype=torch.float32, ) # fmt: on points = torch.tensor([1.0, 0.0, 0.0])[None, None, :] # (1, 1, 3) transformed_points = t.transform_points(points) expected_points = torch.tensor([0.0, 0.0, -1.0]) self.assertTrue( torch.allclose(transformed_points.squeeze(), expected_points, atol=1e-7)) self.assertTrue(torch.allclose(t._matrix, matrix, atol=1e-7))
def test_rotate_angle_fail(self): angle = torch.tensor([[0, 45.0, 90.0], [0, 45.0, 90.0]]) with self.assertRaises(ValueError): RotateAxisAngle(angle=angle, axis="X") # Helpful comments below.# Helpful comments below.# Helpful comments below.# Helpful comments below.# Helpful comments below.# Helpful comments below.
def test_rotate_compose_x_y_z(self): angle = torch.tensor(90.0) t1 = RotateAxisAngle(angle=angle, axis="X") t2 = RotateAxisAngle(angle=angle, axis="Y") t3 = RotateAxisAngle(angle=angle, axis="Z") t = t1.compose(t2, t3) # fmt: off matrix1 = torch.tensor( [ [ [1.0, 0.0, 0.0, 0.0], # noqa: E241, E201 [0.0, 0.0, 1.0, 0.0], # noqa: E241, E201 [0.0, -1.0, 0.0, 0.0], # noqa: E241, E201 [0.0, 0.0, 0.0, 1.0], # noqa: E241, E201 ] ], dtype=torch.float32, ) matrix2 = torch.tensor( [ [ [0.0, 0.0, -1.0, 0.0], # noqa: E241, E201 [0.0, 1.0, 0.0, 0.0], # noqa: E241, E201 [1.0, 0.0, 0.0, 0.0], # noqa: E241, E201 [0.0, 0.0, 0.0, 1.0], # noqa: E241, E201 ] ], dtype=torch.float32, ) matrix3 = torch.tensor( [ [ [ 0.0, 1.0, 0.0, 0.0], # noqa: E241, E201 [-1.0, 0.0, 0.0, 0.0], # noqa: E241, E201 [ 0.0, 0.0, 1.0, 0.0], # noqa: E241, E201 [ 0.0, 0.0, 0.0, 1.0], # noqa: E241, E201 ] ], dtype=torch.float32, ) # fmt: on # order of transforms is t1 -> t2 matrix = torch.matmul(matrix1, torch.matmul(matrix2, matrix3)) composed_matrix = t.get_matrix() self.assertTrue(torch.allclose(composed_matrix, matrix, atol=1e-7))
def test_rotate_z_python_scalar(self): t = RotateAxisAngle(angle=90, axis="Z") # fmt: off matrix = torch.tensor( [[ [0.0, 1.0, 0.0, 0.0], # noqa: E241, E201 [-1.0, 0.0, 0.0, 0.0], # noqa: E241, E201 [0.0, 0.0, 1.0, 0.0], # noqa: E241, E201 [0.0, 0.0, 0.0, 1.0], # noqa: E241, E201 ]], dtype=torch.float32, ) # fmt: on points = torch.tensor([1.0, 0.0, 0.0])[None, None, :] # (1, 1, 3) transformed_points = t.transform_points(points) expected_points = torch.tensor([0.0, 1.0, 0.0]) self.assertTrue( torch.allclose(transformed_points.squeeze(), expected_points, atol=1e-7)) self.assertTrue(torch.allclose(t._matrix, matrix, atol=1e-7))
def test_lower_case_axis(self): t = RotateAxisAngle(angle=90.0, axis="z") # fmt: off matrix = torch.tensor( [[ [0.0, -1.0, 0.0, 0.0], # noqa: E241, E201 [1.0, 0.0, 0.0, 0.0], # noqa: E241, E201 [0.0, 0.0, 1.0, 0.0], # noqa: E241, E201 [0.0, 0.0, 0.0, 1.0], # noqa: E241, E201 ]], dtype=torch.float32, ) # fmt: on self.assertTrue(torch.allclose(t._matrix, matrix, atol=1e-7))
def test_rotate_angle_radians(self): t = RotateAxisAngle(angle=math.pi / 2, degrees=False, axis="Z") # fmt: off matrix = torch.tensor( [[ [0.0, -1.0, 0.0, 0.0], # noqa: E241, E201 [1.0, 0.0, 0.0, 0.0], # noqa: E241, E201 [0.0, 0.0, 1.0, 0.0], # noqa: E241, E201 [0.0, 0.0, 0.0, 1.0], # noqa: E241, E201 ]], dtype=torch.float32, ) # fmt: on self.assertTrue(torch.allclose(t._matrix, matrix, atol=1e-7))
def test_rotate_z_python_scalar(self): t = RotateAxisAngle(angle=90, axis="Z") # fmt: off matrix = torch.tensor( [[ [0.0, -1.0, 0.0, 0.0], # noqa: E241, E201 [1.0, 0.0, 0.0, 0.0], # noqa: E241, E201 [0.0, 0.0, 1.0, 0.0], # noqa: E241, E201 [0.0, 0.0, 0.0, 1.0], # noqa: E241, E201 ]], dtype=torch.float32, ) # fmt: on self.assertTrue(torch.allclose(t._matrix, matrix, atol=1e-7))
def test_rotate_y_torch_scalar(self): angle = torch.tensor(90.0) t = RotateAxisAngle(angle=angle, axis="Y") # fmt: off matrix = torch.tensor( [[ [0.0, 0.0, 1.0, 0.0], # noqa: E241, E201 [0.0, 1.0, 0.0, 0.0], # noqa: E241, E201 [-1.0, 0.0, 0.0, 0.0], # noqa: E241, E201 [0.0, 0.0, 0.0, 1.0], # noqa: E241, E201 ]], dtype=torch.float32, ) # fmt: on self.assertTrue(torch.allclose(t._matrix, matrix, atol=1e-7))
def test_axis_fail(self): with self.assertRaises(ValueError): RotateAxisAngle(angle=90.0, axis="P")
def test_rotate_angle_fail(self): angle = torch.tensor([[0, 45.0, 90.0], [0, 45.0, 90.0]]) with self.assertRaises(ValueError): RotateAxisAngle(angle=angle, axis='X')