def test_operators_matrix33(self): m1 = Matrix33.identity() m2 = Matrix33.from_x_rotation(0.5) # add self.assertTrue(np.array_equal(m1 + m2, matrix33.create_identity() + matrix33.create_from_x_rotation(0.5))) # subtract self.assertTrue(np.array_equal(m1 - m2, matrix33.create_identity() - matrix33.create_from_x_rotation(0.5))) # multiply self.assertTrue(np.array_equal(m1 * m2, matrix33.multiply(matrix33.create_from_x_rotation(0.5), matrix33.create_identity()))) # divide self.assertRaises(ValueError, lambda: m1 / m2) # inverse self.assertTrue(np.array_equal(~m2, matrix33.inverse(matrix33.create_from_x_rotation(0.5)))) # == self.assertTrue(Matrix33() == Matrix33()) self.assertFalse(Matrix33() == Matrix33([1. for n in range(9)])) # != self.assertTrue(Matrix33() != Matrix33([1. for n in range(9)])) self.assertFalse(Matrix33() != Matrix33())
def test_operators_matrix44(self): m1 = Matrix33.identity() m2 = Matrix44.from_x_rotation(0.5) # add self.assertTrue( np.array_equal( m1 + m2, matrix33.create_identity() + matrix33.create_from_x_rotation(0.5))) # subtract self.assertTrue( np.array_equal( m1 - m2, matrix33.create_identity() - matrix33.create_from_x_rotation(0.5))) # multiply self.assertTrue( np.array_equal( m1 * m2, matrix33.multiply(matrix33.create_identity(), matrix33.create_from_x_rotation(0.5)))) # divide self.assertRaises(ValueError, lambda: m1 / m2)
def test_operators_matrix44(self): m1 = Matrix33.identity() m2 = Matrix44.from_x_rotation(0.5) # add self.assertTrue(np.array_equal(m1 + m2, matrix33.create_identity() + matrix33.create_from_x_rotation(0.5))) # subtract self.assertTrue(np.array_equal(m1 - m2, matrix33.create_identity() - matrix33.create_from_x_rotation(0.5))) # multiply self.assertTrue(np.array_equal(m1 * m2, matrix33.multiply(matrix33.create_identity(), matrix33.create_from_x_rotation(0.5)))) # divide self.assertRaises(ValueError, lambda: m1 / m2)
def test_euler_equivalence(self): eulers = euler.create_from_x_rotation(np.pi / 2.) m = matrix33.create_from_x_rotation(np.pi / 2.) q = quaternion.create_from_x_rotation(np.pi / 2.) qm = matrix33.create_from_quaternion(q) em = matrix33.create_from_eulers(eulers) self.assertTrue(np.allclose(qm, m)) self.assertTrue(np.allclose(qm, em)) self.assertTrue(np.allclose(m, em))
def rotated_x(): quat = quaternion.create_from_x_rotation( math.pi ) result = matrix33.create_from_quaternion( quat ) expected = matrix33.create_from_x_rotation( math.pi ) self.assertTrue( numpy.allclose( result, expected ), "Matrix33 from quaternion incorrect with PI rotation about X" )
def rotated_x(): mat = matrix33.create_from_x_rotation( math.pi ) vec = vector3.unit.y result = matrix33.apply_to_vector( mat, vec ) expected = -vec self.assertTrue( numpy.allclose( result, expected ), "Matrix33 apply_to_vector incorrect with rotation about X" )
def create_from_x_rotation( theta ): """Creates a matrix with the specified rotation about the X axis. :param float theta: The rotation, in radians, about the X-axis. :rtype: numpy.array :return: A matrix with the shape (4,4) with the specified rotation about the X-axis. .. seealso:: http://en.wikipedia.org/wiki/Rotation_matrix#In_three_dimensions """ mat = create_identity() mat[ 0:3, 0:3 ] = matrix33.create_from_x_rotation( theta ) return mat
def test_operators_matrix33(self): q = Quaternion() m = Matrix33.from_x_rotation(0.5) # add self.assertRaises(ValueError, lambda: q + m) # subtract self.assertRaises(ValueError, lambda: q - m) # multiply self.assertTrue( np.array_equal( q * m, quaternion.cross( quaternion.create(), quaternion.create_from_matrix( matrix33.create_from_x_rotation(0.5))))) # divide self.assertRaises(ValueError, lambda: q / m)
def test_create_from_inverse_of_quaternion(self): q = quaternion.create_from_x_rotation(np.pi / 2.0) result = matrix33.create_from_inverse_of_quaternion(q) self.assertTrue( np.allclose(result, matrix33.create_from_x_rotation(-np.pi / 2.0)))
def test_create_from_quaternion_equivalent(self): result = matrix33.create_from_quaternion( quaternion.create_from_x_rotation(0.5)) expected = matrix33.create_from_x_rotation(0.5) np.testing.assert_almost_equal(result, expected, decimal=5) self.assertTrue(result.dtype == np.float)
def test_multiply_rotation(self): m1 = matrix33.create_from_x_rotation(np.pi) m2 = matrix33.create_from_y_rotation(np.pi / 2.0) result = matrix33.multiply(m1, m2) self.assertTrue(np.allclose(result, np.dot(m1, m2)))
def test_apply_to_vector_rotated_x(self): mat = matrix33.create_from_x_rotation(np.pi) vec = vector3.unit.y result = matrix33.apply_to_vector(mat, vec) expected = -vec self.assertTrue(np.allclose(result, expected))
def test_create_from_quaternion_rotated_x(self): quat = quaternion.create_from_x_rotation(np.pi) result = matrix33.create_from_quaternion(quat) expected = matrix33.create_from_x_rotation(np.pi) self.assertTrue(np.allclose(result, expected))
def test_operators_matrix33(self): q = Quaternion() m = Matrix33.from_x_rotation(0.5) # add self.assertRaises(ValueError, lambda: q + m) # subtract self.assertRaises(ValueError, lambda: q - m) # multiply self.assertTrue(np.array_equal(q * m, quaternion.cross(quaternion.create(), quaternion.create_from_matrix(matrix33.create_from_x_rotation(0.5))))) # divide self.assertRaises(ValueError, lambda: q / m)
def test_create_from_x_rotation(self): mat = matrix33.create_from_x_rotation(np.pi / 2.) self.assertTrue(np.allclose(np.dot([1.,0.,0.], mat), [1.,0.,0.])) self.assertTrue(np.allclose(np.dot([0.,1.,0.], mat), [0.,0.,-1.])) self.assertTrue(np.allclose(np.dot([0.,0.,1.], mat), [0.,1.,0.]))
def test_create_from_inverse_of_quaternion(self): q = quaternion.create_from_x_rotation(np.pi / 2.0) result = matrix33.create_from_inverse_of_quaternion(q) self.assertTrue(np.allclose(result, matrix33.create_from_x_rotation(-np.pi / 2.0)))
def test_create_from_quaternion_equivalent(self): result = matrix33.create_from_quaternion(quaternion.create_from_x_rotation(0.5)) expected = matrix33.create_from_x_rotation(0.5) np.testing.assert_almost_equal(result, expected, decimal=5) self.assertTrue(result.dtype == np.float)
def test_multiply_rotation(self): m1 = matrix33.create_from_x_rotation(np.pi) m2 = matrix33.create_from_y_rotation(np.pi / 2.0) result = matrix33.multiply(m1, m2) self.assertTrue(np.allclose(result, np.dot(m1,m2)))
def test_create_from_x_rotation(self): mat = matrix33.create_from_x_rotation(np.pi / 2.) self.assertTrue(np.allclose(np.dot([1., 0., 0.], mat), [1., 0., 0.])) self.assertTrue(np.allclose(np.dot([0., 1., 0.], mat), [0., 0., -1.])) self.assertTrue(np.allclose(np.dot([0., 0., 1.], mat), [0., 1., 0.]))