79 lines
2.3 KiB
Python
79 lines
2.3 KiB
Python
"""Tests für die Rotationsfunktionen."""
|
|
|
|
import math
|
|
|
|
import pytest
|
|
|
|
from matrixmania.compute import rot_2d, rot_3d
|
|
|
|
|
|
def _flatten(matrix: list[list[float]]) -> list[float]:
|
|
"""Flacht eine Matrix zu einer flachen Liste ab.
|
|
|
|
Args:
|
|
matrix (list[list[float]]): Zu flachende Matrix.
|
|
|
|
Returns:
|
|
list[float]: Alle Matrixelemente in Zeilen-Reihenfolge.
|
|
"""
|
|
return [value for row in matrix for value in row]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("angle", "expected"),
|
|
[
|
|
(0.0, [1.0, 0.0, 0.0, 1.0]),
|
|
(math.pi / 2, [0.0, -1.0, 1.0, 0.0]),
|
|
(math.pi, [-1.0, 0.0, 0.0, -1.0]),
|
|
(2 * math.pi, [1.0, 0.0, 0.0, 1.0]),
|
|
],
|
|
)
|
|
def test_rot_2d(angle: float, expected: list[float]) -> None:
|
|
"""Prüft die 2D-Rotationsmatrix für einen Winkel.
|
|
|
|
Args:
|
|
angle (float): Drehwinkel im Bogenmaß.
|
|
expected (list[float]): Erwartete Matrixwerte in Zeilen-Reihenfolge.
|
|
|
|
Raises:
|
|
AssertionError: Wenn die Rotationsmatrix nicht erwartet wird.
|
|
"""
|
|
# Arrange: Winkel und Erwartungswerte stammen aus der Parametrisierung.
|
|
|
|
# Act
|
|
result = _flatten(rot_2d(angle))
|
|
|
|
# Assert
|
|
assert result == pytest.approx(expected)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("angle", "axis", "expected"),
|
|
[
|
|
(0.0, "x", [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]),
|
|
(0.0, "y", [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]),
|
|
(0.0, "z", [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]),
|
|
(math.pi / 2, "x", [1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0]),
|
|
(math.pi / 2, "y", [0.0, 0.0, 1.0, 0.0, 1.0, 0.0, -1.0, 0.0, 0.0]),
|
|
(math.pi / 2, "z", [0.0, -1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0]),
|
|
],
|
|
)
|
|
def test_rot_3d(angle: float, axis: str, expected: list[float]) -> None:
|
|
"""Prüft die 3D-Rotationsmatrix für einen Winkel und eine Achse.
|
|
|
|
Args:
|
|
angle (float): Drehwinkel im Bogenmaß.
|
|
axis (str): Rotationsachse ("x", "y" oder "z").
|
|
expected (list[float]): Erwartete Matrixwerte in Zeilen-Reihenfolge.
|
|
|
|
Raises:
|
|
AssertionError: Wenn die Rotationsmatrix nicht erwartet wird.
|
|
"""
|
|
# Arrange: Winkel, Achse und Erwartungswerte stammen aus der Parametrisierung.
|
|
|
|
# Act
|
|
result = _flatten(rot_3d(angle, axis))
|
|
|
|
# Assert
|
|
assert result == pytest.approx(expected)
|