import math import pytest from matrixmania import project_perspective def approx_equal(a: float, b: float, rel_tol: float = 1e-9) -> bool: """Utility to compare two floats with a relative tolerance.""" return math.isclose(a, b, rel_tol=rel_tol) @pytest.mark.parametrize( "point,focal_length,expected", [ # Standardpunkt mit f = 1.0 ( (1.0, 2.0, 3.0), 1.0, (1.0 / (4.0 + 3.0), 2.0 / (4.0 + 3.0)), ), # Anderer Punkt und andere Brennweite ( (2.0, -4.0, 1.0), 2.0, (2.0 * 2.0 / (4.0 + 1.0), 2.0 * -4.0 / (4.0 + 1.0)), ), ], ) def test_project_perspective_points(point, focal_length, expected): """Testet die Perspektivenprojektion für verschiedene Punkte und Brennweiten.""" result = project_perspective(point, focal_length=focal_length, scale=1.0) assert isinstance(result, tuple), "Result should be a tuple" assert len(result) == 2, "Result should contain two coordinates" assert approx_equal(result[0], expected[0]), f"X coordinate mismatch: {result[0]} != {expected[0]}" assert approx_equal(result[1], expected[1]), f"Y coordinate mismatch: {result[1]} != {expected[1]}" @pytest.mark.parametrize( "scale,expected", [ # Standard Skalierung (1.0) ( 1.0, (2.0 * 1.0 / (4.0 + 3.0), 2.0 * 2.0 / (4.0 + 3.0)), ), # Hohe Skalierung (100.0) ( 100.0, (2.0 * 100.0 * 1.0 / (4.0 + 3.0), 2.0 * 100.0 * 2.0 / (4.0 + 3.0)), ), # Kleine Skalierung (0.5) ( 0.5, (2.0 * 0.5 * 1.0 / (4.0 + 3.0), 2.0 * 0.5 * 2.0 / (4.0 + 3.0)), ), ], ) def test_project_perspective_scale(scale, expected): """Testet gezielt die Auswirkung verschiedener Skalierungsfaktoren (scale).""" point = (1.0, 2.0, 3.0) focal_length = 2.0 result = project_perspective(point, focal_length=focal_length, scale=scale) assert isinstance(result, tuple), "Result should be a tuple" assert len(result) == 2, "Result should contain two coordinates" assert approx_equal(result[0], expected[0]), f"X coordinate mismatch: {result[0]} != {expected[0]}" assert approx_equal(result[1], expected[1]), f"Y coordinate mismatch: {result[1]} != {expected[1]}"