def test_multiply(): a = Vector3(x, y, z) b = Vector3(2, 3, -5) a.multiply(b) assert a.x == x * 2, "Check x" assert a.y == y * 3, "Check y" assert a.z == z * -5, "Check z"
def test_add(): a = Vector3(x, y, z) b = Vector3(-x, -y, -z) a.add(b) assert a.x == 0 assert a.y == 0 assert a.z == 0
def test_clamp_scalar(): a = Vector3(-0.01, 0.5, 1.5) clamped = Vector3(0.1, 0.5, 1.0) a.clamp_scalar(0.1, 1.0) assert abs(a.x - clamped.x) <= 0.001, "Check x" assert abs(a.y - clamped.y) <= 0.001, "Check y" assert abs(a.z - clamped.z) <= 0.001, "Check z"
def test_multiply_vectors(): a = Vector3(x, y, z) b = Vector3(2, 3, -5) c = Vector3().multiply_vectors(a, b) assert c.x == x * 2, "Check x" assert c.y == y * 3, "Check y" assert c.z == z * -5, "Check z"
def test_sub(): a = Vector3(x, y, z) b = Vector3(-x, -y, -z) a.sub(b) assert a.x == 2 * x assert a.y == 2 * y assert a.z == 2 * z
def test_set_from_cylindrical(): a = Vector3() cyl = Cylindrical(10, pi * 0.125, 20) expected = Vector3(3.826834323650898, 20, 9.238795325112868) a.set_from_cylindrical(cyl) assert abs(a.x - expected.x) <= eps, "Check x" assert abs(a.y - expected.y) <= eps, "Check y" assert abs(a.z - expected.z) <= eps, "Check z"
def test_cross(): a = Vector3(x, y, z) b = Vector3(2 * x, -y, 0.5 * z) crossed = Vector3(18, 12, -18) a.cross(b) assert abs(a.x - crossed.x) <= eps, "Check x" assert abs(a.y - crossed.y) <= eps, "Check y" assert abs(a.z - crossed.z) <= eps, "Check z"
def test_add_scaled_vector(): a = Vector3(x, y, z) b = Vector3(2, 3, 4) s = 3 a.add_scaled_vector(b, s) assert a.x == x + b.x * s, "Check x" assert a.y == y + b.y * s, "Check y" assert a.z == z + b.z * s, "Check z"
def test_apply_axis_angle(): a = Vector3(x, y, z) axis = Vector3(0, 1, 0) angle = pi / 4.0 expected = Vector3(3 * sqrt(2), 3, sqrt(2)) a.apply_axis_angle(axis, angle) assert abs(a.x - expected.x) <= eps, "Check x" assert abs(a.y - expected.y) <= eps, "Check y" assert abs(a.z - expected.z) <= eps, "Check z"
def test_apply_euler(): a = Vector3(x, y, z) euler = Euler(90, -45, 0) expected = Vector3(-2.352970120501014, -4.7441750936226645, 0.9779234597246458) a.apply_euler(euler) assert abs(a.x - expected.x) <= eps, "Check x" assert abs(a.y - expected.y) <= eps, "Check y" assert abs(a.z - expected.z) <= eps, "Check z"
def test_cross_vectors(): a = Vector3(x, y, z) b = Vector3(x, -y, z) c = Vector3() crossed = Vector3(24, 0, -12) c.cross_vectors(a, b) assert abs(c.x - crossed.x) <= eps, "Check x" assert abs(c.y - crossed.y) <= eps, "Check y" assert abs(c.z - crossed.z) <= eps, "Check z"
def test_dot(): a = Vector3(x, y, z) b = Vector3(-x, -y, -z) c = Vector3() result = a.dot(b) assert result == (-x * x - y * y - z * z) result = a.dot(c) assert result == 0
def test_transform_direction(): a = Vector3(x, y, z) m = Matrix4() transformed = Vector3(0.3713906763541037, 0.5570860145311556, 0.7427813527082074) a.transform_direction(m) assert abs(a.x - transformed.x) <= eps, "Check x" assert abs(a.y - transformed.y) <= eps, "Check y" assert abs(a.z - transformed.z) <= eps, "Check z"
def test_make_basisextract_basis(): identity_basis = [Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)] a = Matrix4().make_basis(identity_basis[0], identity_basis[1], identity_basis[2]) identity = Matrix4() assert matrix_equals(a, identity) test_bases = [[Vector3(0, 1, 0), Vector3(-1, 0, 0), Vector3(0, 0, 1)]] for i in range(len(test_bases)): test_basis = test_bases[i] b = Matrix4().make_basis(test_basis[0], test_basis[1], test_basis[2]) out_basis = [Vector3(), Vector3(), Vector3()] b.extract_basis(out_basis[0], out_basis[1], out_basis[2]) # check what goes in, is what comes out. for j in range(len(out_basis)): assert out_basis[j].equals(test_basis[j]) # get the basis out the hard war for j in range(len(identity_basis)): out_basis[j].copy(identity_basis[j]) out_basis[j].apply_matrix4(b) # did the multiply method of basis extraction work? for j in range(len(out_basis)): assert out_basis[j].equals(test_basis[j])
def test_set_from_spherical(): a = Vector3() phi = acos(-0.5) theta = sqrt(pi) * phi sph = Spherical(10, phi, theta) expected = Vector3(-4.677914006701843, -5, -7.288149322420796) a.set_from_spherical(sph) assert abs(a.x - expected.x) <= eps, "Check x" assert abs(a.y - expected.y) <= eps, "Check y" assert abs(a.z - expected.z) <= eps, "Check z"
def test_set_from_matrix_scale(): a = Vector3() m = Matrix4().set(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53) expected = Vector3(25.573423705088842, 31.921779399024736, 35.70714214271425) a.set_from_matrix_scale(m) assert abs(a.x - expected.x) <= eps, "Check x" assert abs(a.y - expected.y) <= eps, "Check y" assert abs(a.z - expected.z) <= eps, "Check z"
def test_set_from_unit_vectors(): a = Quaternion() b = Vector3(1, 0, 0) c = Vector3(0, 1, 0) expected = Quaternion(0, 0, sqrt(2) / 2, sqrt(2) / 2) a.set_from_unit_vectors(b, c) assert abs(a.x - expected.x) <= eps, "Check x" assert abs(a.y - expected.y) <= eps, "Check y" assert abs(a.z - expected.z) <= eps, "Check z" assert abs(a.w - expected.w) <= eps, "Check w"
def test_instancing(): a = Vector3() assert a.x == 0 assert a.y == 0 assert a.z == 0 a = Vector3(x, y, z) assert a.x == x assert a.y == y assert a.z == z assert repr(a)
def test_add_scalar(): a = Vector3(x, y, z) s = 20 a.add_scalar(s) assert a.x == x + s assert a.y == y + s assert a.z == z + s c = Vector3().add_scalar(s) assert c.x == s assert c.y == s assert c.z == s
def test_set_length(): a = Vector3(x, 0, 0) assert a.length() == x a.set_length(y) assert a.length() == y a = Vector3(0, 0, 0) assert a.length() == 0 a.set_length(y) assert a.length() == 0 with pytest.raises(TypeError): a.set_length()
def test_manhattan_length(): a = Vector3(x, 0, 0) b = Vector3(0, -y, 0) c = Vector3(0, 0, z) d = Vector3() assert a.manhattan_length() == x, "Positive x" assert b.manhattan_length() == y, "Negative y" assert c.manhattan_length() == z, "Positive z" assert d.manhattan_length() == 0, "Empty initialization" a.set(x, y, z) assert a.manhattan_length() == abs(x) + abs(y) + abs(z), "All components"
def test_multiplydivide(): a = Vector3(x, y, z) b = Vector3(2 * x, 2 * y, 2 * z) c = Vector3(4 * x, 4 * y, 4 * z) a.multiply(b) assert a.x == x * b.x, "multiply: check x" assert a.y == y * b.y, "multiply: check y" assert a.z == z * b.z, "multiply: check z" b.divide(c) assert abs(b.x - 0.5) <= eps, "divide: check z" assert abs(b.y - 0.5) <= eps, "divide: check z" assert abs(b.z - 0.5) <= eps, "divide: check z"
def test_lerpclone(): a = Vector3(x, 0, z) b = Vector3(0, -y, 0) assert a.lerp(a, 0).equals(a.lerp(a, 0.5)) assert a.lerp(a, 0).equals(a.lerp(a, 1)) assert a.clone().lerp(b, 0).equals(a) assert a.clone().lerp(b, 0.5).x == x * 0.5 assert a.clone().lerp(b, 0.5).y == -y * 0.5 assert a.clone().lerp(b, 0.5).z == z * 0.5 assert a.clone().lerp(b, 1).equals(b)
def test_set_from_euler_set_from_quaternion(): angles = [Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)] # ensure euler conversion to/from Quaternion matches. for i in range(len(orders)): for j in range(len(angles)): eulers2 = Euler().set_from_quaternion( Quaternion().set_from_euler( Euler(angles[j].x, angles[j].y, angles[j].z, orders[i]) ), orders[i], ) new_angle = Vector3(eulers2.x, eulers2.y, eulers2.z) assert new_angle.distance_to(angles[j]) < 0.001
def test_copy(): a = Vector3(x, y, z) b = Vector3().copy(a) assert b.x == x assert b.y == y assert b.z == z # ensure that it is a True copy a.x = 0 a.y = -1 a.z = -2 assert b.x == x assert b.y == y assert b.z == z
def test_negate(): a = Vector3(x, y, z) a.negate() assert a.x == -x assert a.y == -y assert a.z == -z
def test_set_position(): a = Matrix4().set(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15) b = Vector3(-1, -2, -3) c = Matrix4().set(0, 1, 2, -1, 4, 5, 6, -2, 8, 9, 10, -3, 12, 13, 14, 15) a.set_position(b) assert matrix_equals(a, c)
def test_make_translation(): a = Matrix4() b = Vector3(2, 3, 4) c = Matrix4().set(1, 0, 0, 2, 0, 1, 0, 3, 0, 0, 1, 4, 0, 0, 0, 1) a.make_translation(b.x, b.y, b.z) assert matrix_equals(a, c)
def test_make_rotation_axis(): axis = Vector3(1.5, 0.0, 1.0).normalize() rads = radians(45) a = Matrix4().make_rotation_axis(axis, rads) expected = Matrix4().set( 0.9098790095958609, -0.39223227027636803, 0.13518148560620882, 0, 0.39223227027636803, 0.7071067811865476, -0.588348405414552, 0, 0.13518148560620882, 0.588348405414552, 0.7972277715906868, 0, 0, 0, 0, 1, ) assert matrix_equals(a, expected), "Check numeric result"
def test_normalize(): a = Vector3(x, 0, 0) b = Vector3(0, -y, 0) c = Vector3(0, 0, z) a.normalize() assert a.length() == 1 assert a.x == 1 b.normalize() assert b.length() == 1 assert b.y == -1 c.normalize() assert c.length() == 1 assert c.z == 1