Add perspective projection and tests
This commit is contained in:
1 parent
4931d5e7dc
commit
72d00b4c01
2 files changed
+84
-4
No files matched your search
+38
-2
@@ -1,5 +1,6 @@
|
|||||||
from typing import TypeAlias
|
|
||||||
from math import cos, sin
|
from math import cos, sin
|
||||||
|
from typing import TypeAlias
|
||||||
|
|
||||||
|
|
||||||
Number: TypeAlias = int | float
|
Number: TypeAlias = int | float
|
||||||
Matrix: TypeAlias = list[list[Number]]
|
Matrix: TypeAlias = list[list[Number]]
|
||||||
@@ -192,6 +193,38 @@ def project_ortho(
|
|||||||
|
|
||||||
return (x * scale, y * scale)
|
return (x * scale, y * scale)
|
||||||
|
|
||||||
|
def project_perspective(
|
||||||
|
point: tuple[float, float, float],
|
||||||
|
focal_length: float = 1.0,
|
||||||
|
scale: float = 1.0,
|
||||||
|
camera_distance: float = 4.0,
|
||||||
|
) -> tuple[float, float]:
|
||||||
|
"""Project a 3D point onto a 2D plane using perspective.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
point: The 3D point containing x, y, and z coordinates.
|
||||||
|
focal_length: Controls the strength of the perspective.
|
||||||
|
scale: Scaling factor for the projected coordinates.
|
||||||
|
camera_distance: Distance offset of the camera.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The projected 2D coordinates.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: If the projection denominator is zero.
|
||||||
|
"""
|
||||||
|
x, y, z = point
|
||||||
|
|
||||||
|
depth = z + camera_distance
|
||||||
|
|
||||||
|
if depth == 0:
|
||||||
|
raise ValueError("Projection depth must not be zero.")
|
||||||
|
|
||||||
|
projected_x = x * focal_length / depth * scale
|
||||||
|
projected_y = y * focal_length / depth * scale
|
||||||
|
|
||||||
|
return (projected_x, projected_y)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# Beispiel Multiplikation
|
# Beispiel Multiplikation
|
||||||
@@ -215,4 +248,7 @@ if __name__ == "__main__":
|
|||||||
expected = [[1, 4], [2, 5], [3, 6]]
|
expected = [[1, 4], [2, 5], [3, 6]]
|
||||||
|
|
||||||
assert transpose(matrix) == expected
|
assert transpose(matrix) == expected
|
||||||
print("Transponierung erfolgreich:", transpose(matrix))
|
print("Transponierung erfolgreich:", transpose(matrix))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
from compute import project_ortho
|
import pytest
|
||||||
|
|
||||||
|
from compute import project_ortho, project_perspective
|
||||||
|
|
||||||
|
|
||||||
def test_project_ortho_without_scaling() -> None:
|
def test_project_ortho_without_scaling() -> None:
|
||||||
@@ -25,4 +27,46 @@ def test_project_ortho_with_scaling() -> None:
|
|||||||
result = project_ortho(point, scale)
|
result = project_ortho(point, scale)
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
||||||
|
def test_project_perspective_without_scaling() -> None:
|
||||||
|
"""Test perspective projection with default parameters."""
|
||||||
|
# Arrange
|
||||||
|
point = (1.0, 2.0, 3.0)
|
||||||
|
expected = (1 / 7, 2 / 7)
|
||||||
|
|
||||||
|
# Act
|
||||||
|
result = project_perspective(point)
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
assert result == pytest.approx(expected)
|
||||||
|
|
||||||
|
|
||||||
|
def test_project_perspective_with_scaling() -> None:
|
||||||
|
"""Test perspective projection with scaling and focal length."""
|
||||||
|
# Arrange
|
||||||
|
point = (1.0, 2.0, 3.0)
|
||||||
|
focal_length = 2.0
|
||||||
|
scale = 100.0
|
||||||
|
expected = (200 / 7, 400 / 7)
|
||||||
|
|
||||||
|
# Act
|
||||||
|
result = project_perspective(
|
||||||
|
point,
|
||||||
|
focal_length=focal_length,
|
||||||
|
scale=scale,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
assert result == pytest.approx(expected)
|
||||||
|
|
||||||
|
def test_project_perspective_zero_depth() -> None:
|
||||||
|
"""Test that zero projection depth raises an error."""
|
||||||
|
# Arrange
|
||||||
|
point = (1.0, 2.0, -4.0)
|
||||||
|
|
||||||
|
# Act & Assert
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
project_perspective(point)
|
||||||
|
|
||||||
|
# python -m pytest test_matrixmania/test_projection.py -v
|
||||||
Reference in new issue
Block a user