def test_create(self): v = Vector3() self.assertTrue(np.array_equal(v, [0.,0.,0.])) self.assertEqual(v.shape, self._shape) v = Vector3([1.,2.,3.]) self.assertTrue(np.array_equal(v, [1.,2.,3.])) self.assertEqual(v.shape, self._shape) v = Vector3(Vector3()) self.assertTrue(np.array_equal(v, [0.,0.,0.])) self.assertEqual(v.shape, self._shape) v4 = [1., 2., 3., 4.] result = vector3.create_from_vector4(v4) v, w = result np.testing.assert_almost_equal(v, [1.,2.,3.], decimal=5) np.testing.assert_almost_equal(w, 4., decimal=5) v4 = Vector4([1., 2., 3., 4.]) result = vector3.create_from_vector4(v4) v, w = result np.testing.assert_almost_equal(v, [1.,2.,3.], decimal=5) np.testing.assert_almost_equal(w, 4., decimal=5) m = Matrix44.from_translation([1.,2.,3.]) v = Vector3.from_matrix44_translation(m) self.assertTrue(np.array_equal(v, [1.,2.,3.])) m = Matrix44.from_translation([1.,2.,3.]) v = Vector3(m) self.assertTrue(np.array_equal(v, [1.,2.,3.]))
def apply_to_vector( quat, vec ): """Rotates a vector by a quaternion. :param numpy.array quat: The quaternion. :param numpy.array vec: The vector. :rtype: numpy.array :return: The vector rotated by the quaternion. .. seealso:: http://content.gpwiki.org/index.php/OpenGL:Tutorials:Using_Quaternions_to_represent_rotation """ def apply( quat, vec3 ): """ v = numpy.array( vec ) return v + 2.0 * vector.cross( quat[:-1], vector.cross( quat[:-1], v ) + (quat[-1] * v) ) """ length = vector.length( vec3 ) vec3 = vector.normalise( vec3 ) # use the vector to create a new quaternion # this is basically the vector3 to vector4 conversion with W = 0 vec_quat = numpy.array( [ vec3[ 0 ], vec3[ 1 ], vec3[ 2 ], 0.0 ] ) # quat * vec * quat^-1 result = cross( quat, cross( vec_quat, conjugate( quat ) ) ) return result[ :-1 ] * length if vec.size == 3: # convert to vector4 # ignore w component return apply( quat, vec ) elif vec.size == 4: vec3 = vector3.create_from_vector4( vec ) vec3 = apply( quat, vec3 ) return vector4.create_from_vector3( vec3 ) else: raise ValueError( "Vector size unsupported" )
def test_create_from_vector4(self): v4 = [1., 2., 3., 4.] result = vector3.create_from_vector4(v4) v, w = result np.testing.assert_almost_equal(v, [1., 2., 3.], decimal=5) np.testing.assert_almost_equal(w, 4., decimal=5)
def test_create_from_vector4(self): v4 = [1., 2., 3., 4.] result = vector3.create_from_vector4(v4) v, w = result np.testing.assert_almost_equal(v, [1.,2.,3.], decimal=5) np.testing.assert_almost_equal(w, 4., decimal=5)