Example #1
0
def test_rotate_vector():
    v = Vec3(1, 1, 0)

    q1 = Quat.from_rotation_on_axis(0, np.pi / 3)
    q2 = Quat.from_rotation_on_axis(1, np.pi / 3)
    q3 = Quat.from_rotation_on_axis(2, np.pi / 3)

    t1 = Transform(q1, Vec3.zero())
    t2 = Transform(q2, Vec3.zero())
    t3 = Transform(q3, Vec3.zero())

    # forward rotation
    v1 = t1.rotate(v)
    v2 = t2.rotate(v)
    v3 = t3.rotate(v)

    theta1 = (60) * np.pi / 180.0
    v1_true = Vec3(1, math.cos(theta1), math.sin(theta1))
    v2_true = Vec3(math.cos(theta1), 1, -math.sin(theta1))

    theta2 = (45 + 60) * np.pi / 180.0
    v3_true = math.sqrt(2) * Vec3(math.cos(theta2), math.sin(theta2), 0)

    assert v1 == v1_true
    assert v2 == v2_true
    assert v3 == v3_true

    # inverse rotate
    v1_rev = t1.inverse_rotate(v1_true)
    v2_rev = t2.inverse_rotate(v2_true)
    v3_rev = t3.inverse_rotate(v3_true)

    assert v == v1_rev
    assert v == v2_rev
    assert v == v3_rev