Paketstruktur, rot_3D, Projektionen und pytest-Tests
- compute.py nach src/matrixmania verschoben, __init__.py exportiert die Funktionen - rot_3D, project_ortho und project_perspective ergänzt - Tests in test/test_matrixmania (test_rot.py, test_projection.py) - pyproject.toml: dev-Abhängigkeiten und pytest-pythonpath Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
892ec5e2c9
commit
7b8c76c554
9 files changed
+450
-3
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,78 @@
|
||||
"""Tests für project_ortho und project_perspective."""
|
||||
|
||||
import pytest
|
||||
|
||||
from matrixmania import project_ortho, project_perspective
|
||||
|
||||
|
||||
def test_project_ortho_without_scale() -> None:
|
||||
"""Ohne Skalierung wird nur die z-Koordinate weggelassen."""
|
||||
# Arrange
|
||||
point = (1.0, 2.0, 3.0)
|
||||
|
||||
# Act
|
||||
result = project_ortho(point)
|
||||
|
||||
# Assert
|
||||
assert result == (1.0, 2.0)
|
||||
|
||||
|
||||
def test_project_ortho_with_scale_100() -> None:
|
||||
"""Mit scale=100 werden x und y verhundertfacht."""
|
||||
# Arrange
|
||||
point = (1.0, 2.0, 3.0)
|
||||
|
||||
# Act
|
||||
result = project_ortho(point, scale=100)
|
||||
|
||||
# Assert
|
||||
assert result == (100.0, 200.0)
|
||||
|
||||
|
||||
def test_project_perspective_without_scale() -> None:
|
||||
"""Punkt (1, 2, 3), focal_length=1, scale=1: Teilen durch 3 + 4 = 7."""
|
||||
# Arrange
|
||||
point = (1.0, 2.0, 3.0)
|
||||
|
||||
# Act
|
||||
result = project_perspective(point, focal_length=1, scale=1)
|
||||
|
||||
# Assert
|
||||
assert result == pytest.approx((1 / 7, 2 / 7))
|
||||
|
||||
|
||||
def test_project_perspective_with_scale_and_focal_length() -> None:
|
||||
"""Punkt (1, 2, 3), focal_length=2, scale=100: Faktor 2 / 7 * 100."""
|
||||
# Arrange
|
||||
point = (1.0, 2.0, 3.0)
|
||||
|
||||
# Act
|
||||
result = project_perspective(point, focal_length=2, scale=100)
|
||||
|
||||
# Assert
|
||||
assert result == pytest.approx((200 / 7, 400 / 7))
|
||||
|
||||
|
||||
def test_project_perspective_far_point_appears_smaller() -> None:
|
||||
"""Ein weiter entfernter Punkt landet näher am Ursprung."""
|
||||
# Arrange
|
||||
near_point = (1.0, 1.0, 0.0)
|
||||
far_point = (1.0, 1.0, 10.0)
|
||||
|
||||
# Act
|
||||
near = project_perspective(near_point)
|
||||
far = project_perspective(far_point)
|
||||
|
||||
# Assert
|
||||
assert abs(far[0]) < abs(near[0])
|
||||
assert abs(far[1]) < abs(near[1])
|
||||
|
||||
|
||||
def test_project_perspective_point_in_camera_raises_value_error() -> None:
|
||||
"""Liegt der Punkt genau in der Kamera (Tiefe 0), kommt ein ValueError."""
|
||||
# Arrange
|
||||
point = (1.0, 1.0, -4.0)
|
||||
|
||||
# Act / Assert
|
||||
with pytest.raises(ValueError):
|
||||
project_perspective(point, camera_distance=4.0)
|
||||
@@ -0,0 +1,91 @@
|
||||
"""Tests für rot_2D und rot_3D."""
|
||||
|
||||
import math
|
||||
|
||||
import pytest
|
||||
|
||||
from matrixmania import matmul, rot_2D, rot_3D
|
||||
|
||||
|
||||
def test_rot_2D_zero_angle_is_identity() -> None:
|
||||
"""Bei 0 Grad ist die Rotationsmatrix die Einheitsmatrix."""
|
||||
# Arrange
|
||||
angle = 0.0
|
||||
|
||||
# Act
|
||||
result = rot_2D(angle)
|
||||
|
||||
# Assert
|
||||
assert result == [[1, 0], [0, 1]]
|
||||
|
||||
|
||||
def test_rot_2D_quarter_turn_moves_x_axis_to_y_axis() -> None:
|
||||
"""Eine Drehung um 90 Grad bringt (1, 0) nach (0, 1)."""
|
||||
# Arrange
|
||||
point = [[1], [0]]
|
||||
|
||||
# Act
|
||||
rotated = matmul(rot_2D(math.pi / 2), point)
|
||||
|
||||
# Assert
|
||||
assert rotated[0][0] == pytest.approx(0, abs=1e-9)
|
||||
assert rotated[1][0] == pytest.approx(1)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("axis", ["x", "y", "z"])
|
||||
def test_rot_3D_zero_angle_is_identity(axis: str) -> None:
|
||||
"""Bei 0 Grad ist die Rotationsmatrix für jede Achse die Einheitsmatrix."""
|
||||
# Arrange
|
||||
identity = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
|
||||
|
||||
# Act
|
||||
result = rot_3D(0.0, axis)
|
||||
|
||||
# Assert
|
||||
assert result == identity
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("axis", "point", "expected"),
|
||||
[
|
||||
("x", [[0], [1], [0]], [[0], [0], [1]]), # y-Achse -> z-Achse
|
||||
("y", [[0], [0], [1]], [[1], [0], [0]]), # z-Achse -> x-Achse
|
||||
("z", [[1], [0], [0]], [[0], [1], [0]]), # x-Achse -> y-Achse
|
||||
],
|
||||
)
|
||||
def test_rot_3D_quarter_turn(
|
||||
axis: str, point: list[list[float]], expected: list[list[float]]
|
||||
) -> None:
|
||||
"""Eine Drehung um 90 Grad dreht die Achsen wie erwartet weiter."""
|
||||
# Arrange
|
||||
rotation = rot_3D(math.pi / 2, axis)
|
||||
|
||||
# Act
|
||||
result = matmul(rotation, point)
|
||||
|
||||
# Assert
|
||||
for row, expected_row in zip(result, expected, strict=True):
|
||||
assert row[0] == pytest.approx(expected_row[0], abs=1e-9)
|
||||
|
||||
|
||||
def test_rot_3D_point_on_axis_stays_unchanged() -> None:
|
||||
"""Ein Punkt auf der Drehachse bleibt bei der Drehung liegen."""
|
||||
# Arrange
|
||||
point_on_x_axis = [[2], [0], [0]]
|
||||
|
||||
# Act
|
||||
result = matmul(rot_3D(1.2, "x"), point_on_x_axis)
|
||||
|
||||
# Assert
|
||||
assert result == [[2], [0], [0]]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("axis", ["a", "X", "", "xy"])
|
||||
def test_rot_3D_invalid_axis_raises_value_error(axis: str) -> None:
|
||||
"""Ungültige Achsen führen zu einem ValueError."""
|
||||
# Arrange
|
||||
angle = 1.0
|
||||
|
||||
# Act / Assert
|
||||
with pytest.raises(ValueError):
|
||||
rot_3D(angle, axis)
|
||||
Reference in new issue
Block a user