def test_slerp(): a = Quaternion(x, y, z, w) b = Quaternion(-x, -y, -z, -w) c = a.clone().slerp(b, 0) d = a.clone().slerp(b, 1) assert a.equals(c), "Passed" assert b.equals(d), "Passed" sqrt1_2 = sqrt(1 / 2) e = Quaternion(1, 0, 0, 0) f = Quaternion(0, 0, 1, 0) expected = Quaternion(sqrt1_2, 0, sqrt1_2, 0) result = e.clone().slerp(f, 0.5) assert abs(result.x - expected.x) <= eps, "Check x" assert abs(result.y - expected.y) <= eps, "Check y" assert abs(result.z - expected.z) <= eps, "Check z" assert abs(result.w - expected.w) <= eps, "Check w" g = Quaternion(0, sqrt1_2, 0, sqrt1_2) h = Quaternion(0, -sqrt1_2, 0, sqrt1_2) expected = Quaternion(0, 0, 0, 1) result = g.clone().slerp(h, 0.5) assert abs(result.x - expected.x) <= eps, "Check x" assert abs(result.y - expected.y) <= eps, "Check y" assert abs(result.z - expected.z) <= eps, "Check z" assert abs(result.w - expected.w) <= eps, "Check w"
def test_equals(): a = Quaternion(x, y, z, w) b = Quaternion(-x, -y, -z, -w) assert a.x != b.x assert a.y != b.y assert not a.equals(b) assert not b.equals(a) a.copy(b) assert a.x == b.x assert a.y == b.y assert a.equals(b) assert b.equals(a)
def test_rotate_towards(): a = Quaternion() b = Quaternion().set_from_euler(Euler(0, pi, 0)) c = Quaternion() half_pi = pi * 0.5 a.rotate_towards(b, 0) assert a.equals(a) is True a.rotate_towards(b, pi * 2) # test overshoot assert a.equals(b) is True a.set(0, 0, 0, 1) a.rotate_towards(b, half_pi) assert a.angle_to(c) - half_pi <= eps
def test_set_from_axis_angle(): # TODO: find cases to validate. # assert True zero = Quaternion() a = Quaternion().set_from_axis_angle(Vector3(1, 0, 0), 0) assert a.equals(zero) a = Quaternion().set_from_axis_angle(Vector3(0, 1, 0), 0) assert a.equals(zero) a = Quaternion().set_from_axis_angle(Vector3(0, 0, 1), 0) assert a.equals(zero) b1 = Quaternion().set_from_axis_angle(Vector3(1, 0, 0), pi) assert not a.equals(b1) b2 = Quaternion().set_from_axis_angle(Vector3(1, 0, 0), -pi) assert not a.equals(b2) b1.multiply(b2) assert a.equals(b1)