[rotate / affine] check args for validity
This commit is contained in:
parent
05f9186268
commit
1d2b1ca8d3
2 changed files with 48 additions and 0 deletions
|
|
@ -1,6 +1,8 @@
|
||||||
import numpy
|
import numpy
|
||||||
|
import pytest
|
||||||
from numpy.testing import assert_allclose
|
from numpy.testing import assert_allclose
|
||||||
|
|
||||||
|
import adze
|
||||||
import adze.transform
|
import adze.transform
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -29,3 +31,40 @@ def test_rotate_z_quarter_turn() -> None:
|
||||||
}
|
}
|
||||||
rotated = adze.transform.rotate(poly, numpy.pi / 2, axis=2)
|
rotated = adze.transform.rotate(poly, numpy.pi / 2, axis=2)
|
||||||
assert_allclose(rotated['vertices'], [[0.0, 1.0, 0.0]], atol=1e-12)
|
assert_allclose(rotated['vertices'], [[0.0, 1.0, 0.0]], atol=1e-12)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('axis', [-1, 3, 4])
|
||||||
|
def test_rotate_rejects_invalid_axis(axis: int) -> None:
|
||||||
|
poly = {
|
||||||
|
'vertices': numpy.array([[1.0, 2.0, 3.0]]),
|
||||||
|
'neighbors': numpy.empty((0, 3), dtype=int),
|
||||||
|
}
|
||||||
|
|
||||||
|
with pytest.raises(adze.AdzeError, match='Axis must be 0, 1, or 2'):
|
||||||
|
adze.transform.rotate(poly, numpy.pi / 2, axis=axis)
|
||||||
|
|
||||||
|
|
||||||
|
def test_affine_requires_4x4_matrix() -> None:
|
||||||
|
poly = {
|
||||||
|
'vertices': numpy.array([[1.0, 2.0, 3.0]]),
|
||||||
|
'neighbors': numpy.empty((0, 3), dtype=int),
|
||||||
|
}
|
||||||
|
|
||||||
|
with pytest.raises(adze.AdzeError, match='shape \\(4, 4\\)'):
|
||||||
|
adze.transform.affine(poly, numpy.eye(3))
|
||||||
|
|
||||||
|
|
||||||
|
def test_affine_rejects_zero_homogeneous_coordinate() -> None:
|
||||||
|
poly = {
|
||||||
|
'vertices': numpy.array([[1.0, 2.0, 3.0]]),
|
||||||
|
'neighbors': numpy.empty((0, 3), dtype=int),
|
||||||
|
}
|
||||||
|
xform = numpy.array([
|
||||||
|
[1.0, 0.0, 0.0, 0.0],
|
||||||
|
[0.0, 1.0, 0.0, 0.0],
|
||||||
|
[0.0, 0.0, 1.0, 0.0],
|
||||||
|
[0.0, 0.0, 0.0, 0.0],
|
||||||
|
])
|
||||||
|
|
||||||
|
with pytest.raises(adze.AdzeError, match='zero homogeneous coordinate'):
|
||||||
|
adze.transform.affine(poly, xform)
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ from numpy import sin, cos
|
||||||
from numpy.typing import ArrayLike
|
from numpy.typing import ArrayLike
|
||||||
|
|
||||||
from . import poly_t
|
from . import poly_t
|
||||||
|
from .util import AdzeError
|
||||||
|
|
||||||
|
|
||||||
def rotate(poly: poly_t, theta: float, axis: int) -> poly_t:
|
def rotate(poly: poly_t, theta: float, axis: int) -> poly_t:
|
||||||
|
|
@ -18,6 +19,9 @@ def rotate(poly: poly_t, theta: float, axis: int) -> poly_t:
|
||||||
Rotated polyhedron.
|
Rotated polyhedron.
|
||||||
Data is altered in-place but also returned for convenience.
|
Data is altered in-place but also returned for convenience.
|
||||||
"""
|
"""
|
||||||
|
if axis not in (0, 1, 2):
|
||||||
|
raise AdzeError(f'Axis must be 0, 1, or 2. Got {axis}')
|
||||||
|
|
||||||
s, c = sin(theta), cos(theta)
|
s, c = sin(theta), cos(theta)
|
||||||
v0 = poly['vertices'][:, axis - 2].copy()
|
v0 = poly['vertices'][:, axis - 2].copy()
|
||||||
v1 = poly['vertices'][:, axis - 1]
|
v1 = poly['vertices'][:, axis - 1]
|
||||||
|
|
@ -72,8 +76,13 @@ def affine(poly: poly_t, xform: ArrayLike) -> poly_t:
|
||||||
Data is altered in-place but also returned for convenience.
|
Data is altered in-place but also returned for convenience.
|
||||||
"""
|
"""
|
||||||
xform = numpy.array(xform, copy=False)
|
xform = numpy.array(xform, copy=False)
|
||||||
|
if xform.shape != (4, 4):
|
||||||
|
raise AdzeError(f'Affine transform matrix must have shape (4, 4). Got {xform.shape}')
|
||||||
|
|
||||||
mat4x3 = xform[:, :3]
|
mat4x3 = xform[:, :3]
|
||||||
mat4x1 = xform[:, 3]
|
mat4x1 = xform[:, 3]
|
||||||
vh = mat4x3 @ poly['vertices'].T + mat4x1
|
vh = mat4x3 @ poly['vertices'].T + mat4x1
|
||||||
|
if numpy.any(vh[3, :] == 0):
|
||||||
|
raise AdzeError('Affine transform produced points with zero homogeneous coordinate')
|
||||||
poly['vertices'][:] = (vh[:3, :] / vh[3, :]).T
|
poly['vertices'][:] = (vh[:3, :] / vh[3, :]).T
|
||||||
return poly
|
return poly
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue