[build_polyhedron] perform more checks for polyhedron validity
This commit is contained in:
parent
665e1e4161
commit
f17667c93c
2 changed files with 82 additions and 5 deletions
|
|
@ -5,7 +5,7 @@ from numpy.linalg import norm
|
||||||
from numpy.typing import ArrayLike, NDArray
|
from numpy.typing import ArrayLike, NDArray
|
||||||
|
|
||||||
from . import poly_t, plane_t
|
from . import poly_t, plane_t
|
||||||
from .util import _get_maps, find_components, find_faces, AdzeError
|
from .util import _get_maps, find_components, find_faces, check_poly, AdzeError
|
||||||
|
|
||||||
|
|
||||||
def planes_from_boundary(
|
def planes_from_boundary(
|
||||||
|
|
@ -66,9 +66,28 @@ def build_polyhedron(vertices: ArrayLike, faces: Iterable[ArrayLike]) -> poly_t:
|
||||||
Schelgel-representation polyhedron
|
Schelgel-representation polyhedron
|
||||||
"""
|
"""
|
||||||
vertices = numpy.array(vertices, dtype=float)
|
vertices = numpy.array(vertices, dtype=float)
|
||||||
afaces = [numpy.array(face, dtype=int) for face in faces]
|
if vertices.ndim != 2 or vertices.shape[1] != 3:
|
||||||
|
raise AdzeError(f'Vertices must have shape (N, 3). Got {vertices.shape}')
|
||||||
|
|
||||||
edges_per_vertex = numpy.bincount(numpy.hstack(afaces))
|
afaces = [numpy.array(face, dtype=int) for face in faces]
|
||||||
|
if not afaces:
|
||||||
|
raise AdzeError('Need at least one face')
|
||||||
|
|
||||||
|
num_vertices = vertices.shape[0]
|
||||||
|
for face in afaces:
|
||||||
|
if face.ndim != 1:
|
||||||
|
raise AdzeError(f'Faces must be 1-dimensional. Got face with shape {face.shape}')
|
||||||
|
if face.size < 3:
|
||||||
|
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')
|
||||||
|
|
||||||
|
edges_per_vertex = numpy.bincount(numpy.hstack(afaces), minlength=num_vertices)
|
||||||
|
unused_vertices = numpy.where(edges_per_vertex == 0)[0]
|
||||||
|
if unused_vertices.size:
|
||||||
|
raise AdzeError(f'Vertices {unused_vertices} are unused by any face')
|
||||||
|
|
||||||
if edges_per_vertex.min() < 3:
|
if edges_per_vertex.min() < 3:
|
||||||
raise AdzeError(f'Vertex {edges_per_vertex.argmin()} has < 3 edges')
|
raise AdzeError(f'Vertex {edges_per_vertex.argmin()} has < 3 edges')
|
||||||
|
|
@ -102,7 +121,9 @@ def build_polyhedron(vertices: ArrayLike, faces: Iterable[ArrayLike]) -> poly_t:
|
||||||
neighbors[face[has_prev], 2] = next_vert[has_prev]
|
neighbors[face[has_prev], 2] = next_vert[has_prev]
|
||||||
neighbors[face[has_next], 2] = prev_vert[has_next]
|
neighbors[face[has_next], 2] = prev_vert[has_next]
|
||||||
|
|
||||||
return {'vertices': vertices, 'neighbors': neighbors}
|
poly = {'vertices': vertices, 'neighbors': neighbors}
|
||||||
|
check_poly(poly, check_connectivity=False)
|
||||||
|
return poly
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Some vertices link more than three edges.
|
Some vertices link more than three edges.
|
||||||
|
|
@ -172,7 +193,6 @@ def build_polyhedron(vertices: ArrayLike, faces: Iterable[ArrayLike]) -> poly_t:
|
||||||
start[0] = 0
|
start[0] = 0
|
||||||
|
|
||||||
# number of input vertices (v) and duplicates (d)
|
# number of input vertices (v) and duplicates (d)
|
||||||
num_vertices = vertices.shape[0]
|
|
||||||
num_duplicates = edges_per_vertex.sum()
|
num_duplicates = edges_per_vertex.sum()
|
||||||
|
|
||||||
neighbors = numpy.empty((num_duplicates, 3), dtype=int)
|
neighbors = numpy.empty((num_duplicates, 3), dtype=int)
|
||||||
|
|
@ -271,7 +291,9 @@ def build_polyhedron(vertices: ArrayLike, faces: Iterable[ArrayLike]) -> poly_t:
|
||||||
|
|
||||||
final_vertices = numpy.repeat(vertices, edges_per_vertex - 2, axis=0)
|
final_vertices = numpy.repeat(vertices, edges_per_vertex - 2, axis=0)
|
||||||
|
|
||||||
return {'vertices': final_vertices, 'neighbors': neighbors}
|
poly = {'vertices': final_vertices, 'neighbors': neighbors}
|
||||||
|
check_poly(poly, check_connectivity=False)
|
||||||
|
return poly
|
||||||
|
|
||||||
|
|
||||||
def boundary_from_poly(poly: poly_t) -> list[dict[str, Any]]:
|
def boundary_from_poly(poly: poly_t) -> list[dict[str, Any]]:
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,61 @@ def test_planes_from_boundary_rejects_zero_area_face() -> None:
|
||||||
planes_from_boundary(vertices, [[0, 1, 2]])
|
planes_from_boundary(vertices, [[0, 1, 2]])
|
||||||
|
|
||||||
|
|
||||||
|
def test_build_polyhedron_rejects_unused_vertices() -> None:
|
||||||
|
vertices = numpy.array([
|
||||||
|
[0.0, 0.0, 0.0],
|
||||||
|
[1.0, 0.0, 0.0],
|
||||||
|
[0.0, 1.0, 0.0],
|
||||||
|
[0.0, 0.0, 1.0],
|
||||||
|
[9.0, 9.0, 9.0],
|
||||||
|
])
|
||||||
|
faces = [
|
||||||
|
[0, 1, 2],
|
||||||
|
[0, 3, 1],
|
||||||
|
[0, 2, 3],
|
||||||
|
[1, 3, 2],
|
||||||
|
]
|
||||||
|
|
||||||
|
with pytest.raises(adze.AdzeError, match='unused by any face'):
|
||||||
|
adze.build_polyhedron(vertices, faces)
|
||||||
|
|
||||||
|
|
||||||
|
def test_build_polyhedron_rejects_invalid_face_index() -> None:
|
||||||
|
vertices = numpy.array([
|
||||||
|
[0.0, 0.0, 0.0],
|
||||||
|
[1.0, 0.0, 0.0],
|
||||||
|
[0.0, 1.0, 0.0],
|
||||||
|
[0.0, 0.0, 1.0],
|
||||||
|
])
|
||||||
|
faces = [
|
||||||
|
[0, 1, 2],
|
||||||
|
[0, 3, 1],
|
||||||
|
[0, 2, 3],
|
||||||
|
[1, 3, 5],
|
||||||
|
]
|
||||||
|
|
||||||
|
with pytest.raises(adze.AdzeError, match='outside the provided vertex list'):
|
||||||
|
adze.build_polyhedron(vertices, faces)
|
||||||
|
|
||||||
|
|
||||||
|
def test_build_polyhedron_rejects_inconsistent_face_winding() -> None:
|
||||||
|
vertices = numpy.array([
|
||||||
|
[0.0, 0.0, 0.0],
|
||||||
|
[1.0, 0.0, 0.0],
|
||||||
|
[0.0, 1.0, 0.0],
|
||||||
|
[0.0, 0.0, 1.0],
|
||||||
|
])
|
||||||
|
faces = [
|
||||||
|
[0, 1, 2],
|
||||||
|
[0, 1, 3],
|
||||||
|
[0, 3, 2],
|
||||||
|
[1, 2, 3],
|
||||||
|
]
|
||||||
|
|
||||||
|
with pytest.raises(adze.AdzeError, match='duplicate neighbors'):
|
||||||
|
adze.build_polyhedron(vertices, faces)
|
||||||
|
|
||||||
|
|
||||||
def test_check_poly_rejects_row_count_mismatch() -> None:
|
def test_check_poly_rejects_row_count_mismatch() -> None:
|
||||||
poly = {
|
poly = {
|
||||||
'vertices': numpy.array([
|
'vertices': numpy.array([
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue