[check_poly] reject malformed neighbor arrays

This commit is contained in:
Jan Petykiewicz 2026-03-31 23:27:17 -07:00
commit f4762733a6
2 changed files with 25 additions and 1 deletions

View file

@ -3,6 +3,7 @@ import pytest
from numpy.testing import assert_allclose
import adze
import adze.util
def test_boundary_from_empty_poly() -> None:
assert adze.boundary_from_poly(adze.util.build_empty()) == []
@ -14,3 +15,22 @@ def test_empty_poly_moments_shape(order: int) -> None:
assert moments.shape == (adze.number_of_moments(order),)
assert_allclose(moments, numpy.zeros_like(moments))
def test_check_poly_rejects_row_count_mismatch() -> None:
poly = {
'vertices': numpy.array([
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[9.0, 9.0, 9.0],
]),
'neighbors': numpy.array([
[1, 2, 0],
[2, 0, 1],
[0, 1, 2],
], dtype=int),
}
with pytest.raises(adze.AdzeError, match='Neighbors must have shape'):
adze.util.check_poly(poly, check_connectivity=False)

View file

@ -261,6 +261,10 @@ def check_poly(poly: poly_t, check_connectivity: bool = True) -> None:
neighbors = poly['neighbors']
vertices = poly['vertices']
num_vertices = vertices.shape[0]
if vertices.ndim != 2 or vertices.shape[1] != 3:
raise AdzeError(f'Vertices must have shape (N, 3). Got {vertices.shape}')
if neighbors.shape != (num_vertices, 3):
raise AdzeError(f'Neighbors must have shape ({num_vertices}, 3). Got {neighbors.shape}')
out_of_bounds = numpy.logical_or(neighbors >= vertices.shape[0], neighbors < 0)
if numpy.any(out_of_bounds):
@ -272,7 +276,7 @@ def check_poly(poly: poly_t, check_connectivity: bool = True) -> None:
which_vertices = numpy.where(repeated_neighbors)[0]
raise AdzeError(f'Vertices {which_vertices} have duplicate neighbors')
vertex_uses = numpy.bincount(neighbors.ravel())
vertex_uses = numpy.bincount(neighbors.ravel(), minlength=num_vertices)
if numpy.any(vertex_uses != 3):
which_vertices = numpy.where(vertex_uses != 3)[0]
raise AdzeError(f'Vertices {which_vertices} are referenced {vertex_uses[which_vertices]} times,'