diff --git a/adze/boundary.py b/adze/boundary.py index a2edb7b..d4f3736 100644 --- a/adze/boundary.py +++ b/adze/boundary.py @@ -25,34 +25,28 @@ def planes_from_boundary( List of planes, represented by (normal vector, signed distance to origin) tuples. """ 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] planes = [] 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] - - # Newell's method for robust normal calculation - # n_x = sum (y_i - y_{i+1}) * (z_i + z_{i+1}) - # n_y = sum (z_i - z_{i+1}) * (x_i + x_{i+1}) - # n_z = sum (x_i - x_{i+1}) * (y_i + y_{i+1}) - - # Shift vertices to wrap around - v_curr = face_verts - v_next = numpy.roll(face_verts, -1, axis=0) - - # 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) - + if face_verts.shape[0] < 3: + raise AdzeError('Faces must have at least three vertices') + rolled_verts = numpy.roll(face_verts, -1, axis=0) + n = numpy.array(( + 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])), + numpy.sum((face_verts[:, 0] - rolled_verts[:, 0]) * (face_verts[:, 1] + rolled_verts[:, 1])), + ), dtype=float) + tolerance = numpy.finfo(float).eps * 64 * max(1.0, float(numpy.abs(face_verts).max())) ** 2 n_norm = norm(n) - if n_norm < 1e-15: - # Face is degenerate (zero area) - continue - + if n_norm <= tolerance: + raise AdzeError('Faces must have non-zero area') n /= n_norm - centroid = numpy.mean(face_verts, axis=0) + centroid = numpy.sum(face_verts, axis=0) / len(face) d = -n @ centroid planes.append((n, d)) return planes diff --git a/adze/test/test_edge_cases.py b/adze/test/test_edge_cases.py index 5028036..dfc6401 100644 --- a/adze/test/test_edge_cases.py +++ b/adze/test/test_edge_cases.py @@ -4,6 +4,8 @@ from numpy.testing import assert_allclose import adze import adze.util +from adze.boundary import planes_from_boundary + def test_boundary_from_empty_poly() -> None: 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: poly = { 'vertices': numpy.array([