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_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])