[moments] Error if order is negative or non-integer

This commit is contained in:
Jan Petykiewicz 2026-04-01 19:49:56 -07:00
commit f834598e0f
2 changed files with 235 additions and 0 deletions

View file

@ -1,10 +1,24 @@
from operator import index
import numpy import numpy
from numpy.typing import NDArray from numpy.typing import NDArray
from . import poly_t from . import poly_t
from .util import AdzeError
from .util import find_faces from .util import find_faces
def _normalize_order(order: int) -> int:
try:
normalized = index(order)
except TypeError as exc:
raise AdzeError('Moment order must be an integer') from exc
if normalized < 0:
raise AdzeError(f'Moment order must be non-negative. Got {normalized}')
return normalized
def number_of_moments(order: int) -> int: def number_of_moments(order: int) -> int:
""" """
Returns the number of moments with a given order or below. Returns the number of moments with a given order or below.
@ -15,6 +29,7 @@ def number_of_moments(order: int) -> int:
Returns: Returns:
Number of moments with that order or fewer. Number of moments with that order or fewer.
""" """
order = _normalize_order(order)
return ((order + 1) * (order + 2) * (order + 3)) // 6 return ((order + 1) * (order + 2) * (order + 3)) // 6
@ -42,6 +57,7 @@ def get_moments(poly: poly_t, order: int) -> NDArray[numpy.float64]:
Returns: Moments, increasing polynomial order: Returns: Moments, increasing polynomial order:
[1, x, y, z, xx, xy, xz, yy, yz, zz, xxx, xxy, xxz, ...] [1, x, y, z, xx, xy, xz, yy, yz, zz, xxx, xxy, xxz, ...]
""" """
order = _normalize_order(order)
vertices = poly['vertices'] vertices = poly['vertices']
neighbors = poly['neighbors'] neighbors = poly['neighbors']
num_verts = vertices.shape[0] num_verts = vertices.shape[0]

View file

@ -18,6 +18,61 @@ def test_empty_poly_moments_shape(order: int) -> None:
assert_allclose(moments, numpy.zeros_like(moments)) assert_allclose(moments, numpy.zeros_like(moments))
@pytest.mark.parametrize('order', [-3, -2, -1])
def test_negative_moment_order_rejected(order: int) -> None:
poly = adze.build_box([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]])
edges = [numpy.array([0.0, 1.0]) for _ in range(3)]
with pytest.raises(adze.AdzeError, match='Moment order must be non-negative'):
adze.number_of_moments(order)
with pytest.raises(adze.AdzeError, match='Moment order must be non-negative'):
adze.get_moments(poly, order)
with pytest.raises(adze.AdzeError, match='Moment order must be non-negative'):
adze.voxelize(poly, edges, order=order)
def test_planes_from_boundary_tetrahedron() -> 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, 2],
]
planes = planes_from_boundary(vertices, faces)
assert len(planes) == 4
for normal, distance in planes:
assert normal.shape == (3,)
assert numpy.isscalar(distance)
assert_allclose(numpy.linalg.norm(normal), 1.0)
def test_planes_from_boundary_nontriangular_face() -> None:
vertices = numpy.array([
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
])
faces = [
[0, 3, 2, 1],
[0, 1, 4],
[1, 2, 4],
[2, 3, 4],
[3, 0, 4],
]
normal, distance = planes_from_boundary(vertices, faces)[0]
assert_allclose(normal, [0.0, 0.0, -1.0])
assert_allclose(distance, 0.0)
def test_planes_from_boundary_rejects_zero_area_face() -> None: def test_planes_from_boundary_rejects_zero_area_face() -> None:
vertices = numpy.array([ vertices = numpy.array([
@ -30,6 +85,19 @@ def test_planes_from_boundary_rejects_zero_area_face() -> None:
planes_from_boundary(vertices, [[0, 1, 2]]) planes_from_boundary(vertices, [[0, 1, 2]])
@pytest.mark.parametrize('face', [[-1, 1, 2], [0, 1, 5]])
def test_planes_from_boundary_rejects_invalid_face_index(face: list[int]) -> 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],
])
with pytest.raises(adze.AdzeError, match='outside the provided vertex list'):
planes_from_boundary(vertices, [face])
def test_build_polyhedron_rejects_unused_vertices() -> None: def test_build_polyhedron_rejects_unused_vertices() -> None:
vertices = numpy.array([ vertices = numpy.array([
[0.0, 0.0, 0.0], [0.0, 0.0, 0.0],
@ -49,6 +117,33 @@ def test_build_polyhedron_rejects_unused_vertices() -> None:
adze.build_polyhedron(vertices, faces) adze.build_polyhedron(vertices, faces)
def test_build_polyhedron_accepts_box_with_quadrilateral_faces() -> None:
vertices = numpy.array([
[0.0, 0.0, 0.0],
[0.0, 0.0, 1.0],
[0.0, 1.0, 0.0],
[0.0, 1.0, 1.0],
[1.0, 0.0, 0.0],
[1.0, 0.0, 1.0],
[1.0, 1.0, 0.0],
[1.0, 1.0, 1.0],
])
faces = [
[0, 4, 6, 2],
[0, 1, 5, 4],
[0, 2, 3, 1],
[4, 5, 7, 6],
[6, 7, 3, 2],
[1, 3, 7, 5],
]
poly = adze.build_polyhedron(vertices, faces)
adze.util.check_poly(poly)
assert_allclose(adze.get_volume(poly), 1.0)
assert len(adze.boundary_from_poly(poly)) == 1
def test_build_polyhedron_rejects_invalid_face_index() -> None: def test_build_polyhedron_rejects_invalid_face_index() -> None:
vertices = numpy.array([ vertices = numpy.array([
[0.0, 0.0, 0.0], [0.0, 0.0, 0.0],
@ -85,6 +180,26 @@ def test_build_polyhedron_rejects_inconsistent_face_winding() -> None:
adze.build_polyhedron(vertices, faces) adze.build_polyhedron(vertices, faces)
def test_build_polyhedron_rejects_invalid_high_degree_boundary() -> None:
vertices = numpy.array([
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[1.0, 1.0, 0.0],
[0.5, 0.5, 1.0],
])
faces = [
[0, 1, 2, 3],
[0, 1, 4],
[1, 3, 4],
[3, 2, 4],
[2, 0, 4],
]
with pytest.raises(adze.AdzeError, match='valid closed polyhedron boundary'):
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([
@ -120,3 +235,107 @@ def test_voxelize_requires_strictly_increasing_axes() -> None:
[numpy.array([1.0, 0.0]), numpy.array([0.0, 1.0]), numpy.array([0.0, 1.0])], [numpy.array([1.0, 0.0]), numpy.array([0.0, 1.0]), numpy.array([0.0, 1.0])],
order=0, order=0,
) )
def test_clip_rejects_zero_normal_plane() -> None:
poly = adze.build_box([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]])
with pytest.raises(adze.AdzeError, match='Plane normal must be non-zero'):
adze.clip(poly, (numpy.array([0.0, 0.0, 0.0]), 0.0))
@pytest.mark.parametrize('normal', [numpy.array([1.0, 0.0]), numpy.array([1.0, 0.0, 0.0, 0.0])])
def test_split_rejects_wrong_plane_normal_shape(normal: numpy.ndarray) -> None:
poly = adze.build_box([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]])
with pytest.raises(adze.AdzeError, match='Plane normal must have shape'):
adze.split(poly, (normal, 0.0))
def test_split_rejects_non_scalar_plane_distance() -> None:
poly = adze.build_box([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]])
with pytest.raises(adze.AdzeError, match='Plane distance must be a scalar'):
adze.split(poly, (numpy.array([1.0, 0.0, 0.0]), numpy.array([0.0, 1.0])))
def test_boundary_from_poly_rejects_invalid_graph() -> None:
poly = {
'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],
]),
'neighbors': numpy.array([
[1, 2, 3],
[2, 3, 0],
[3, 0, 1],
[0, 1, 2],
], dtype=int),
}
with pytest.raises(adze.AdzeError, match='invalid neighbor ordering'):
adze.boundary_from_poly(poly)
def test_clip_noop_returns_independent_poly() -> None:
poly = adze.build_box([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]])
clipped = adze.clip(poly, (numpy.array([1.0, 0.0, 0.0]), 0.0))
assert clipped is not poly
assert clipped['vertices'] is not poly['vertices']
clipped['vertices'][0, 0] = 999.0
assert poly['vertices'][0, 0] == 0.0
def test_split_noop_returns_independent_poly() -> None:
poly = adze.build_box([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]])
empty_poly, kept_poly = adze.split(poly, (numpy.array([1.0, 0.0, 0.0]), -2.0))
assert empty_poly['vertices'].shape == (0, 3)
assert kept_poly is not poly
assert kept_poly['vertices'] is not poly['vertices']
kept_poly['vertices'][0, 0] = 999.0
assert poly['vertices'][0, 0] == 0.0
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_right = adze.build_box([[3.0, 0.0, 0.0], [4.0, 1.0, 1.0]])
poly = {
'vertices': numpy.vstack((poly_left['vertices'], poly_right['vertices'])),
'neighbors': numpy.vstack((poly_left['neighbors'], poly_right['neighbors'] + poly_left['vertices'].shape[0])),
}
split_left, split_right = adze.split(poly, (numpy.array([1.0, 0.0, 0.0]), -2.0))
adze.util.check_poly(split_left)
adze.util.check_poly(split_right)
assert_allclose(adze.get_volume(split_left), 1.0)
assert_allclose(adze.get_volume(split_right), 1.0)
@pytest.mark.parametrize('box_fun', [adze.build_box, adze.planes_from_box])
def test_box_helpers_reject_inverted_corners(box_fun) -> None:
with pytest.raises(adze.AdzeError, match='box_corners\\[0\\] < box_corners\\[1\\]'):
box_fun([[1.0, 1.0, 1.0], [0.0, 0.0, 0.0]])
@pytest.mark.parametrize('tetra_fun', [adze.build_tetrahedron, adze.planes_from_tetrahedron])
def test_tetrahedron_helpers_reject_wrong_shape(tetra_fun) -> None:
with pytest.raises(adze.AdzeError, match='shape \\(4, 3\\)'):
tetra_fun([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]])
@pytest.mark.parametrize('tetra_fun', [adze.build_tetrahedron, adze.planes_from_tetrahedron])
def test_tetrahedron_helpers_reject_negative_winding(tetra_fun) -> None:
with pytest.raises(adze.AdzeError, match='positive winding'):
tetra_fun([
[0.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 0.0, 1.0],
])