def test_create_from_y_rotation(self): # 180 degree turn around Y axis q = quaternion.create_from_y_rotation(np.pi) self.assertTrue(np.allclose(q, [0., 1., 0., 0.])) # 90 degree rotation around Y axis q = quaternion.create_from_y_rotation(np.pi / 2.) self.assertTrue(np.allclose(q, [0., np.sqrt(0.5), 0., np.sqrt(0.5)])) # -90 degree rotation around Y axis q = quaternion.create_from_y_rotation(-np.pi / 2.)
def test_procedural_examples(self): from pyrr import quaternion, matrix44, vector3 import numpy as np point = vector3.create(1.,2.,3.) orientation = quaternion.create() translation = vector3.create() scale = vector3.create(1,1,1) # translate along X by 1 translation += [1.0, 0.0, 0.0] # rotate about Y by pi/2 rotation = quaternion.create_from_y_rotation(np.pi / 2.0) orientation = quaternion.cross(rotation, orientation) # create a matrix # start our matrix off using the scale matrix = matrix44.create_from_scale(scale) # apply our orientation orientation = matrix44.create_from_quaternion(orientation) matrix = matrix44.multiply(matrix, orientation) # apply our translation translation_matrix = matrix44.create_from_translation(translation) matrix = matrix44.multiply(matrix, translation_matrix) # transform our point by the matrix point = matrix44.apply_to_vector(matrix, point)
def test_decompose(self): # define expectations expected_scale = vector3.create(*[1, 1, 2], dtype='f4') expected_rotation = quaternion.create_from_y_rotation(np.pi, dtype='f4') expected_translation = vector3.create(*[10, 0, -5], dtype='f4') expected_model = np.array([ [-1, 0, 0, 0], [0, 1, 0, 0], [0, 0, -2, 0], [10, 0, -5, 1], ], dtype='f4') # compose matrix using Pyrr s = matrix44.create_from_scale(expected_scale, dtype='f4') r = matrix44.create_from_quaternion(expected_rotation, dtype='f4') t = matrix44.create_from_translation(expected_translation, dtype='f4') model = s.dot(r).dot(t) np.testing.assert_almost_equal(model, expected_model) self.assertTrue(model.dtype == expected_model.dtype) # decompose matrix scale, rotation, translation = matrix44.decompose(model) np.testing.assert_almost_equal(scale, expected_scale) self.assertTrue(scale.dtype == expected_scale.dtype) np.testing.assert_almost_equal(rotation, expected_rotation) self.assertTrue(rotation.dtype == expected_rotation.dtype) np.testing.assert_almost_equal(translation, expected_translation) self.assertTrue(translation.dtype == expected_translation.dtype)
def test_apply_to_vector_y(self): # 180 degree turn around Y axis q = quaternion.create_from_y_rotation(np.pi) self.assertTrue(np.allclose(quaternion.apply_to_vector(q, [1., 0., 0.]), [-1., 0., 0.])) self.assertTrue(np.allclose(quaternion.apply_to_vector(q, [0., 1., 0.]), [0., 1., 0.])) self.assertTrue(np.allclose(quaternion.apply_to_vector(q, [0., 0., 1.]), [0., 0.,-1.])) # 90 degree rotation around Y axis q = quaternion.create_from_y_rotation(np.pi / 2.) self.assertTrue(np.allclose(quaternion.apply_to_vector(q, [1., 0., 0.]), [0., 0.,-1.])) self.assertTrue(np.allclose(quaternion.apply_to_vector(q, [0., 1., 0.]), [0., 1., 0.])) self.assertTrue(np.allclose(quaternion.apply_to_vector(q, [0., 0., 1.]), [1., 0., 0.])) # -90 degree rotation around Y axis q = quaternion.create_from_y_rotation(-np.pi / 2.) self.assertTrue(np.allclose(quaternion.apply_to_vector(q, [1., 0., 0.]), [0., 0., 1.])) self.assertTrue(np.allclose(quaternion.apply_to_vector(q, [0., 1., 0.]), [0., 1., 0.])) self.assertTrue(np.allclose(quaternion.apply_to_vector(q, [0., 0., 1.]), [-1., 0., 0.]))
def rotated_y(): quat = quaternion.create_from_y_rotation( math.pi ) result = matrix33.create_from_quaternion( quat ) expected = matrix33.create_from_y_rotation( math.pi ) self.assertTrue( numpy.allclose( result, expected ), "Matrix33 from quaternion incorrect with PI rotation about Y" )
def test_apply_to_vector_y(self): quat = quaternion.create_from_y_rotation(np.pi / 2.) self.assertTrue( np.allclose(quaternion.apply_to_vector(quat, [1., 0., 0.]), [0., 0., 1.])) self.assertTrue( np.allclose(quaternion.apply_to_vector(quat, [0., 1., 0.]), [0., 1., 0.])) self.assertTrue( np.allclose(quaternion.apply_to_vector(quat, [0., 0., 1.]), [-1., 0., 0.]))
def rotate_y(self, radians): """Yaw the transform about it's Y axis. .. note:: Amount > 0 == yaw right. Amount < 0 == yaw left. """ if radians == 0.0: return quat = quaternion.create_from_y_rotation(radians) self.rotate_quaternion(quat)
def rotate_y( self, radians ): """Yaw the transform about it's Y axis. .. note:: Amount > 0 == yaw right. Amount < 0 == yaw left. """ if radians == 0.0: return quat = quaternion.create_from_y_rotation( radians ) self.rotate_quaternion( quat )
def test_create_from_quaternion_rotated_y(self): quat = quaternion.create_from_y_rotation(np.pi) result = matrix33.create_from_quaternion(quat) expected = matrix33.create_from_y_rotation(np.pi) self.assertTrue(np.allclose(result, expected))
def test_create_from_y_rotation(self): result = quaternion.create_from_y_rotation(np.pi) np.testing.assert_almost_equal(result, [0.,1.,0.,0.], decimal=3) self.assertTrue(result.dtype == np.float)
def test_apply_to_vector_y(self): quat = quaternion.create_from_y_rotation(np.pi / 2.) self.assertTrue(np.allclose(quaternion.apply_to_vector(quat,[1.,0.,0.]), [0.,0.,1.])) self.assertTrue(np.allclose(quaternion.apply_to_vector(quat,[0.,1.,0.]), [0.,1.,0.])) self.assertTrue(np.allclose(quaternion.apply_to_vector(quat,[0.,0.,1.]), [-1.,0.,0.]))
def test_create_from_y_rotation(self): result = quaternion.create_from_y_rotation(np.pi) np.testing.assert_almost_equal(result, [0., 1., 0., 0.], decimal=3) self.assertTrue(result.dtype == np.float)