more input validation
This commit is contained in:
parent
4da3376e63
commit
4f1920c2bb
3 changed files with 44 additions and 6 deletions
|
|
@ -27,14 +27,20 @@ def planes_from_boundary(
|
||||||
vertices = numpy.asarray(vertices, dtype=float)
|
vertices = numpy.asarray(vertices, dtype=float)
|
||||||
if vertices.ndim != 2 or vertices.shape[1] != 3:
|
if vertices.ndim != 2 or vertices.shape[1] != 3:
|
||||||
raise AdzeError(f'Vertices must have shape (N, 3). Got {vertices.shape}')
|
raise AdzeError(f'Vertices must have shape (N, 3). Got {vertices.shape}')
|
||||||
|
num_vertices = vertices.shape[0]
|
||||||
afaces = [numpy.asarray(ff) for ff in faces]
|
afaces = [numpy.asarray(ff) for ff in faces]
|
||||||
planes = []
|
planes = []
|
||||||
for face in afaces:
|
for face in afaces:
|
||||||
if face.ndim != 1:
|
if face.ndim != 1:
|
||||||
raise AdzeError(f'Faces must be 1-dimensional. Got face with shape {face.shape}')
|
raise AdzeError(f'Faces must be 1-dimensional. Got face with shape {face.shape}')
|
||||||
face_verts = vertices[face]
|
if face.size < 3:
|
||||||
if face_verts.shape[0] < 3:
|
|
||||||
raise AdzeError('Faces must have at least three vertices')
|
raise AdzeError('Faces must have at least three vertices')
|
||||||
|
if numpy.unique(face).size != face.size:
|
||||||
|
raise AdzeError('Faces may not repeat a vertex index')
|
||||||
|
if numpy.any(face < 0) or numpy.any(face >= num_vertices):
|
||||||
|
raise AdzeError('Faces reference vertices outside the provided vertex list')
|
||||||
|
|
||||||
|
face_verts = vertices[face]
|
||||||
rolled_verts = numpy.roll(face_verts, -1, axis=0)
|
rolled_verts = numpy.roll(face_verts, -1, axis=0)
|
||||||
n = numpy.array((
|
n = numpy.array((
|
||||||
numpy.sum((face_verts[:, 1] - rolled_verts[:, 1]) * (face_verts[:, 2] + rolled_verts[:, 2])),
|
numpy.sum((face_verts[:, 1] - rolled_verts[:, 1]) * (face_verts[:, 2] + rolled_verts[:, 2])),
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,34 @@ from numpy.linalg import norm
|
||||||
from numpy.typing import ArrayLike
|
from numpy.typing import ArrayLike
|
||||||
|
|
||||||
from . import poly_t, plane_t
|
from . import poly_t, plane_t
|
||||||
|
from .util import AdzeError
|
||||||
|
|
||||||
|
|
||||||
|
def _normalize_tetrahedron_vertices(vertices: ArrayLike) -> numpy.ndarray:
|
||||||
|
verts = numpy.asarray(vertices, dtype=float)
|
||||||
|
if verts.shape != (4, 3):
|
||||||
|
raise AdzeError(f'Tetrahedron vertices must have shape (4, 3). Got {verts.shape}')
|
||||||
|
if not numpy.isfinite(verts).all():
|
||||||
|
raise AdzeError('Tetrahedron vertices must contain only finite values')
|
||||||
|
|
||||||
|
signed_six_volume = -numpy.linalg.det(verts[:3] - verts[3])
|
||||||
|
tolerance = numpy.finfo(float).eps * 64 * max(1.0, float(numpy.abs(verts).max())) ** 3
|
||||||
|
if abs(signed_six_volume) <= tolerance:
|
||||||
|
raise AdzeError('Tetrahedron vertices must span a non-zero volume')
|
||||||
|
if signed_six_volume < 0:
|
||||||
|
raise AdzeError("Tetrahedron vertices must use the helper's positive winding")
|
||||||
|
return verts
|
||||||
|
|
||||||
|
|
||||||
|
def _normalize_box_corners(box_corners: ArrayLike) -> numpy.ndarray:
|
||||||
|
corners = numpy.asarray(box_corners, dtype=float)
|
||||||
|
if corners.shape != (2, 3):
|
||||||
|
raise AdzeError(f'Box corners must have shape (2, 3). Got {corners.shape}')
|
||||||
|
if not numpy.isfinite(corners).all():
|
||||||
|
raise AdzeError('Box corners must contain only finite values')
|
||||||
|
if numpy.any(corners[0] >= corners[1]):
|
||||||
|
raise AdzeError('Box corners must satisfy box_corners[0] < box_corners[1] on every axis')
|
||||||
|
return corners
|
||||||
|
|
||||||
|
|
||||||
def build_tetrahedron(vertices: ArrayLike) -> poly_t:
|
def build_tetrahedron(vertices: ArrayLike) -> poly_t:
|
||||||
|
|
@ -16,8 +44,9 @@ def build_tetrahedron(vertices: ArrayLike) -> poly_t:
|
||||||
Returns:
|
Returns:
|
||||||
Tetrahedron in Schelgel representation.
|
Tetrahedron in Schelgel representation.
|
||||||
"""
|
"""
|
||||||
|
vertices = _normalize_tetrahedron_vertices(vertices)
|
||||||
poly = {
|
poly = {
|
||||||
'vertices': numpy.array(vertices, dtype=float),
|
'vertices': vertices,
|
||||||
'neighbors': numpy.array((
|
'neighbors': numpy.array((
|
||||||
(1, 3, 2),
|
(1, 3, 2),
|
||||||
(2, 3, 0),
|
(2, 3, 0),
|
||||||
|
|
@ -39,6 +68,7 @@ def build_box(box_corners: ArrayLike) -> poly_t:
|
||||||
Returns:
|
Returns:
|
||||||
Box in Schelgel representation.
|
Box in Schelgel representation.
|
||||||
"""
|
"""
|
||||||
|
box_corners = _normalize_box_corners(box_corners)
|
||||||
neighbors = [
|
neighbors = [
|
||||||
[1, 4, 3],
|
[1, 4, 3],
|
||||||
[2, 5, 0],
|
[2, 5, 0],
|
||||||
|
|
@ -50,7 +80,6 @@ def build_box(box_corners: ArrayLike) -> poly_t:
|
||||||
[6, 3, 4],
|
[6, 3, 4],
|
||||||
]
|
]
|
||||||
|
|
||||||
box_corners = numpy.asarray(box_corners, dtype=float)
|
|
||||||
vertices = [
|
vertices = [
|
||||||
[box_corners[0, 0], box_corners[0, 1], box_corners[0, 2]],
|
[box_corners[0, 0], box_corners[0, 1], box_corners[0, 2]],
|
||||||
[box_corners[1, 0], box_corners[0, 1], box_corners[0, 2]],
|
[box_corners[1, 0], box_corners[0, 1], box_corners[0, 2]],
|
||||||
|
|
@ -80,7 +109,7 @@ def planes_from_box(box_corners: ArrayLike) -> list[plane_t]:
|
||||||
Returns:
|
Returns:
|
||||||
List of planes, facing inwards.
|
List of planes, facing inwards.
|
||||||
"""
|
"""
|
||||||
box_corners = numpy.asarray(box_corners, dtype=float)
|
box_corners = _normalize_box_corners(box_corners)
|
||||||
faces = [
|
faces = [
|
||||||
(( 0, 0, 1), -box_corners[0, 2]),
|
(( 0, 0, 1), -box_corners[0, 2]),
|
||||||
(( 0, 0, -1), box_corners[1, 2]),
|
(( 0, 0, -1), box_corners[1, 2]),
|
||||||
|
|
@ -104,7 +133,7 @@ def planes_from_tetrahedron(vertices: ArrayLike) -> list[plane_t]:
|
||||||
Returns:
|
Returns:
|
||||||
List of planes, facing inwards.
|
List of planes, facing inwards.
|
||||||
"""
|
"""
|
||||||
verts = numpy.asarray(vertices, dtype=float)
|
verts = _normalize_tetrahedron_vertices(vertices)
|
||||||
vset_inds = numpy.array((
|
vset_inds = numpy.array((
|
||||||
[3, 1, 2, 1],
|
[3, 1, 2, 1],
|
||||||
[2, 0, 3, 2],
|
[2, 0, 3, 2],
|
||||||
|
|
|
||||||
|
|
@ -302,6 +302,7 @@ def test_split_noop_returns_independent_poly() -> None:
|
||||||
assert poly['vertices'][0, 0] == 0.0
|
assert poly['vertices'][0, 0] == 0.0
|
||||||
|
|
||||||
|
|
||||||
|
<<<<<<< HEAD
|
||||||
def test_split_handles_disconnected_components_without_cut_edges() -> None:
|
def test_split_handles_disconnected_components_without_cut_edges() -> None:
|
||||||
poly_left = adze.build_box([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]])
|
poly_left = adze.build_box([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]])
|
||||||
poly_right = adze.build_box([[3.0, 0.0, 0.0], [4.0, 1.0, 1.0]])
|
poly_right = adze.build_box([[3.0, 0.0, 0.0], [4.0, 1.0, 1.0]])
|
||||||
|
|
@ -318,6 +319,8 @@ def test_split_handles_disconnected_components_without_cut_edges() -> None:
|
||||||
assert_allclose(adze.get_volume(split_right), 1.0)
|
assert_allclose(adze.get_volume(split_right), 1.0)
|
||||||
|
|
||||||
|
|
||||||
|
=======
|
||||||
|
>>>>>>> 549a417 (more input validation)
|
||||||
@pytest.mark.parametrize('box_fun', [adze.build_box, adze.planes_from_box])
|
@pytest.mark.parametrize('box_fun', [adze.build_box, adze.planes_from_box])
|
||||||
def test_box_helpers_reject_inverted_corners(box_fun) -> None:
|
def test_box_helpers_reject_inverted_corners(box_fun) -> None:
|
||||||
with pytest.raises(adze.AdzeError, match='box_corners\\[0\\] < box_corners\\[1\\]'):
|
with pytest.raises(adze.AdzeError, match='box_corners\\[0\\] < box_corners\\[1\\]'):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue