def rotate(self, angle, vector1, vector2, rot_point): tnd = self.to_num_dimension num = self.num_dimension + 1 rot_pos = tnd(rot_point).reshape(num - 1, 1) matrix_translate = np.eye(num) matrix_translate[0:num - 1, num - 1:num] = rot_pos * -1. matrix_rot = rotation_from_angle_and_plane(angle, tnd(vector1, up=1), tnd(vector2, up=1)) matrix_from_origin = matrix_rot.copy() matrix_rot = np.dot(matrix_rot, matrix_translate) matrix_translate[0:num - 1, num - 1:num] = rot_pos matrix_rot = np.dot(matrix_translate, matrix_rot) # print(matrix_rot) for obj in self.objects: obj.rotate(matrix_rot, matrix_from_origin)
def test_rotation_multiple_90(): for dim in range(2, 6): image_1 = np.ones((6, ) * dim) image_2 = np.zeros((6, ) * dim) image_2[(slice(None, 3), ) * dim] = 1 center_1 = None center_2 = (1, ) * dim for image, center in zip((image_1, image_2), (center_1, center_2)): v1 = np.zeros(dim) v2 = np.zeros(dim) v1[-2] = 1 v2[-1] = 1 for angle in (np.pi / 2 * x for x in range(1, 4)): rotation = mgen.rotation_from_angle_and_plane(angle, v1, v2) output = transform(image, rotation, (0, ) * dim, origin=center) # need some atol here since we are comparing exactly to 0 np.testing.assert_allclose(output, image, atol=1e-10)
def test_rotation_nd(self): for rand1 in np.random.uniform(-np.pi, np.pi, 100): # Test 2D case is_close(rotation_from_angle(rand1), rotation_from_angle_and_plane(rand1, (1,0), (0,1))) # Test 3D case is_close(rotation_around_z(rand1), rotation_from_angle_and_plane(rand1, (1,0,0), (0,1,0))) # Test normalisation of first parameter is_close(rotation_around_z(rand1), rotation_from_angle_and_plane(rand1, (2,0,0), (0,1,0))) # Test normalisation of second parameter is_close(rotation_around_z(rand1), rotation_from_angle_and_plane(rand1, (1,0,0), (0,2,0))) # Test normalisation of both parameters is_close(rotation_around_z(rand1), rotation_from_angle_and_plane(rand1, (2,0,0), (0,2,0))) is_close(rotation_around_x(rand1), rotation_from_angle_and_plane(rand1, (0,1,0), (0,0,1))) is_close(rotation_around_y(rand1), rotation_from_angle_and_plane(rand1, (0,0,1), (1,0,0))) # Test generic properties of higher dimensional rotations # create two perpendicular random vectors dimension = random.randint(4,100) v1 = np.random.uniform(-1., 1., dimension) v1 = v1/np.linalg.norm(v1) v2 = np.random.uniform(-1., 1., dimension) v2 = v2/np.linalg.norm(v2) v2 = v2 - v2.dot(v1)*v1 m = rotation_from_angle_and_plane(rand1, v1, v2) m_inv = rotation_from_angle_and_plane(-rand1, v1, v2) is_close(m.dot(m.T), np.eye(dimension)) self.assertAlmostEqual(np.linalg.det(m), 1.0) is_close(m, m_inv.T) with self.assertRaises(ValueError): rotation_from_angle_and_plane(0., (1,0,0), (0,1,0,0)) with self.assertRaises(ValueError): rotation_from_angle_and_plane(0., (1,0,0,0), (0,1,0)) with self.assertRaises(ValueError): rotation_from_angle_and_plane(0., (1,0,0), (1,0,0)) with self.assertRaises(ValueError): rotation_from_angle_and_plane(0., (0,0), (1,0)) with self.assertRaises(ValueError): rotation_from_angle_and_plane(0., (1,0), (0,0))