def test_methods(): trans_idt = transform_identity() base = transform_identity() vec = Vector3(1, 2, 3) meths_specs = { "inverse": ([], trans_idt), "inverseTimes": ([trans_idt], trans_idt), "invXform": ([vec], vec), } for name, (args, expected_ret) in meths_specs.items(): v = base.copy() ret = getattr(v, name)(*args) assert_equal(ret, expected_ret, "%s(): %r != %r" % (name, ret, expected_ret)) # Operators trans1 = transform_identity() half_pi = math.pi / 2 rot = Quaternion() rot.setRotation(Vector3(0, 1, 0), math.pi / 2) trans2 = Transform() trans2.rotation = rot trans2.origin = Vector3(1, 0, 0) vec = Vector3(1, 0, 0) assert_equal(trans2(vec), Vector3(1, 0, -1)) assert_equal(trans2 * vec, Vector3(1, 0, -1)) rot2 = trans2 * rot assert_almost_equal(rot2.getAngle(), math.pi, places=5) comp_trans_ref = Transform(Matrix3x3(-1, 0, 0, 0, 1, 0, 0, 0, -1), Vector3(1, 0, -1)) assert_equal(trans2 * trans2, comp_trans_ref) trans2 *= trans2 assert_equal(trans2, comp_trans_ref)
def test_get_set(): trans_idt = transform_identity() # openGLMatrix property ref_basis = Matrix3x3( 1, 2, 3, 4, 5, 6, 7, 8, 9) ref_origin = Vector3(1, 2, 3) ref_trans = Transform(ref_basis, ref_origin) np_mat = np.array(( 1, 4, 7, 0, 2, 5, 8, 0, 3, 6, 9, 0, 0, 0, 0, 1)) trans = Transform() trans.openGLMatrix = np_mat assert (trans.getOpenGLMatrix() == np_mat).all() assert (ref_trans.openGLMatrix == np.array(( 1, 4, 7, 0, 2, 5, 8, 0, 3, 6, 9, 0, 1, 2, 3, 1))).all() # origin/basis properties trans = Transform() trans.origin = ref_origin trans.basis = ref_basis assert_equal(trans, ref_trans) trans = Transform() trans.setOrigin(ref_origin) trans.setBasis(ref_basis) assert_equal(trans.getOrigin(), ref_origin) assert_equal(trans.getBasis(), ref_basis) # rotation property trans = Transform() rot = Quaternion() rot.setRotation(Vector3(0, 1, 0), math.pi / 2) trans.rotation = rot trans.origin = ref_origin ref_rot_trans = Transform(Matrix3x3( 0, 0, 1, 0, 1, 0, -1, 0, 0), ref_origin) assert_equal(trans, ref_rot_trans) # setIdentity() trans = Transform() trans.setIdentity() assert_equal(trans.basis, matrix3x3_identity()) assert_equal(trans.origin, Vector3(0, 0, 0)) # mult() trans = Transform() trans.mult(trans_idt, trans_idt) assert_equal(trans, trans_idt)
def test_methods(): # Create some vectors base = Quaternion(0, 0, 2, 1) other = Quaternion(0, 2, 0, 1) other2 = Quaternion(2, 0, 0) other3 = Quaternion(1, 1, 0) # Generic methods tests meths_specs = { "dot": ([other], 1), "length": ([], 2.2360679774997898), "length2": ([], 5), "normalized": ([], Quaternion(0.0, 0.0, 0.89442718029, 0.447213590145)), "normalize": ([], Quaternion(0.0, 0.0, 0.89442718029, 0.447213590145)), "getAngle": ([], 0), "getAxis": ([], Vector3(1, 0, 0)), "inverse": ([], Quaternion(0, 0, -2, 1)), "farthest": ([other], Quaternion(0, -2, 0, -1)), "nearest": ([other], Quaternion(0, 2, 0, 1)), "slerp": ([other, 0.5], Quaternion(0.0, 1.29099440575, 1.29099440575, 1.29099440575)), "angle": ([other], 1.3694384098052979), } for name, (args, expected_ret) in meths_specs.items(): v = base.copy() ret = getattr(v, name)(*args) assert_equal(ret, expected_ret, "%s(): %r != %r" % (name, ret, expected_ret)) # Assignment methods assign_meth_specs = { "setRotation": ([Vector3(0, 1, 0), 0], Quaternion(0, 0, 0, 1)), "setEuler": ([0, 0, 0], Quaternion(0, 0, 0, 1)), } for name, (args, expected) in assign_meth_specs.items(): v = base.copy() getattr(v, name)(*args) assert_equal(v, expected, "%s(): %r != %r" % (name, v, expected)) # Overloaded operators assert_equal(base * 10, Quaternion(0, 0, 20, 10)) assert_equal(base / 10, Quaternion(0, 0, 0.2, 0.1)) assert_raises(TypeError, lambda: 10 / base) assert_equal(base + other, Quaternion(0, 2, 2, 2)) assert_equal(base - other, Quaternion(0, -2, 2, 0))
def test_get_set(): # Basic get/set mat = Matrix3x3(1, 2, 3, 4, 5, 6, 7, 8, 9) assert_equal([mat.getColumn(i) for i in range(3)], [Vector3(1, 4, 7), Vector3(2, 5, 8), Vector3(3, 6, 9)]) assert_equal([mat.getRow(i) for i in range(3)], [Vector3(1, 2, 3), Vector3(4, 5, 6), Vector3(7, 8, 9)]) assert_equal([mat[i] for i in range(3)], [Vector3(1, 2, 3), Vector3(4, 5, 6), Vector3(7, 8, 9)]) mat2 = Matrix3x3() np_mat = np.array(( 1, 4, 7, 0, 2, 5, 8, 0, 3, 6, 9, 0, 0, 0, 0, 1)) mat2.setFromOpenGLSubMatrix(np_mat) assert_equal(mat, mat2) assert (mat.getOpenGLSubMatrix() == np_mat).all() mat2.setValue(1, 2, 3, 4, 5, 6, 7, 8, 9) assert_equal(mat, mat2) # Rotations # quaternion qpi = math.pi / 4 rot_quat = Quaternion() rot_quat.setRotation(Vector3(0.25, 0.75, 0.5), qpi) mat.setRotation(rot_quat) mat_rot_quat = mat.getRotation() assert_almost_equal(rot_quat.x, mat_rot_quat.x) assert_almost_equal(rot_quat.y, mat_rot_quat.y) assert_almost_equal(rot_quat.z, mat_rot_quat.z) assert_almost_equal(rot_quat.w, mat_rot_quat.w) # Euler angles = (0.1, 0.2, 0.3) expected_res = ( [0.1, 0.2, 0.3], [0.3, 0.2, 0.1], ) for suffix, expected in zip(("YPR", "ZYX"), expected_res ): getattr(mat, "setEuler" + suffix)(*angles) res = getattr(mat, "getEuler" + suffix)() for i in range(3): assert_almost_equal(res[i], expected[i])
def test_basic(): # Basic creation and attributes tests quat = Quaternion() quat = Quaternion(1, 2, 3, 4) assert_equal(quat.x, 1) assert_equal(quat.y, 2) assert_equal(quat.z, 3) assert_equal(quat.w, 4) quat.x = 4 quat.y = 5 quat.z = 6 quat.w = 7 assert_equal(quat.x, 4) assert_equal(quat.y, 5) assert_equal(quat.z, 6) assert_equal(quat.w, 7) # Fancy constructors quat = Quaternion(0, 0, 0) assert_equal((quat.x, quat.y, quat.z, quat.w), (0, 0, 0, 1)) quat = Quaternion(Vector3(0, 1, 0), 0) assert_equal((quat.x, quat.y, quat.z, quat.w), (0, 0, 0, 1))