[planes_from_boundary] reject zero-area faces and incorrect dims

This commit is contained in:
Jan Petykiewicz 2026-04-01 19:16:46 -07:00
commit 665e1e4161
2 changed files with 29 additions and 22 deletions

View file

@ -25,34 +25,28 @@ def planes_from_boundary(
List of planes, represented by (normal vector, signed distance to origin) tuples. List of planes, represented by (normal vector, signed distance to origin) tuples.
""" """
vertices = numpy.asarray(vertices, dtype=float) vertices = numpy.asarray(vertices, dtype=float)
if vertices.ndim != 2 or vertices.shape[1] != 3:
raise AdzeError(f'Vertices must have shape (N, 3). Got {vertices.shape}')
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:
raise AdzeError(f'Faces must be 1-dimensional. Got face with shape {face.shape}')
face_verts = vertices[face] face_verts = vertices[face]
if face_verts.shape[0] < 3:
# Newell's method for robust normal calculation raise AdzeError('Faces must have at least three vertices')
# n_x = sum (y_i - y_{i+1}) * (z_i + z_{i+1}) rolled_verts = numpy.roll(face_verts, -1, axis=0)
# n_y = sum (z_i - z_{i+1}) * (x_i + x_{i+1}) n = numpy.array((
# n_z = sum (x_i - x_{i+1}) * (y_i + y_{i+1}) numpy.sum((face_verts[:, 1] - rolled_verts[:, 1]) * (face_verts[:, 2] + rolled_verts[:, 2])),
numpy.sum((face_verts[:, 2] - rolled_verts[:, 2]) * (face_verts[:, 0] + rolled_verts[:, 0])),
# Shift vertices to wrap around numpy.sum((face_verts[:, 0] - rolled_verts[:, 0]) * (face_verts[:, 1] + rolled_verts[:, 1])),
v_curr = face_verts ), dtype=float)
v_next = numpy.roll(face_verts, -1, axis=0) tolerance = numpy.finfo(float).eps * 64 * max(1.0, float(numpy.abs(face_verts).max())) ** 2
# Ensure n is float64 to avoid casting issues with integer inputs
n = numpy.array([
numpy.sum((v_curr[:, 1] - v_next[:, 1]) * (v_curr[:, 2] + v_next[:, 2])),
numpy.sum((v_curr[:, 2] - v_next[:, 2]) * (v_curr[:, 0] + v_next[:, 0])),
numpy.sum((v_curr[:, 0] - v_next[:, 0]) * (v_curr[:, 1] + v_next[:, 1]))
], dtype=float)
n_norm = norm(n) n_norm = norm(n)
if n_norm < 1e-15: if n_norm <= tolerance:
# Face is degenerate (zero area) raise AdzeError('Faces must have non-zero area')
continue
n /= n_norm n /= n_norm
centroid = numpy.mean(face_verts, axis=0) centroid = numpy.sum(face_verts, axis=0) / len(face)
d = -n @ centroid d = -n @ centroid
planes.append((n, d)) planes.append((n, d))
return planes return planes

View file

@ -4,6 +4,8 @@ from numpy.testing import assert_allclose
import adze import adze
import adze.util import adze.util
from adze.boundary import planes_from_boundary
def test_boundary_from_empty_poly() -> None: def test_boundary_from_empty_poly() -> None:
assert adze.boundary_from_poly(adze.util.build_empty()) == [] assert adze.boundary_from_poly(adze.util.build_empty()) == []
@ -17,6 +19,17 @@ def test_empty_poly_moments_shape(order: int) -> None:
def test_planes_from_boundary_rejects_zero_area_face() -> None:
vertices = numpy.array([
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[2.0, 0.0, 0.0],
])
with pytest.raises(adze.AdzeError, match='non-zero area'):
planes_from_boundary(vertices, [[0, 1, 2]])
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([