[split] add a tolerance for splitting to avoid making tiny slivers

improve numerical stability
This commit is contained in:
jan 2026-03-07 07:07:43 -08:00
commit d71cb6a535
2 changed files with 60 additions and 21 deletions

View file

@ -5,7 +5,7 @@ from . import poly_t, plane_t
from .util import build_empty, _get_map
def clip(poly: poly_t, plane: plane_t) -> poly_t:
def clip(poly: poly_t, plane: plane_t, atol: float = 1e-12) -> poly_t:
"""
Clip a polyhedron with a plane, keeping only the part which is on
the same side as the plane's normal vector.
@ -15,15 +15,16 @@ def clip(poly: poly_t, plane: plane_t) -> poly_t:
Args:
poly: Polyhedron to clip, in chiral Schlegel representation.
plane: Plane to clip against.
atol: Absolute tolerance for "on-plane" checks. Default 1e-12.
Returns:
Clipped polyhedron.
"""
left_poly = _split(poly, plane, same_only=True)
left_poly = _split(poly, plane, same_only=True, atol=atol)
return left_poly
def split(poly: poly_t, plane: plane_t) -> tuple[poly_t, poly_t]:
def split(poly: poly_t, plane: plane_t, atol: float = 1e-12) -> tuple[poly_t, poly_t]:
"""
Clip a polyhedron with a plane, keeping both parts.
@ -32,29 +33,30 @@ def split(poly: poly_t, plane: plane_t) -> tuple[poly_t, poly_t]:
Args:
poly: Polyhedron to clip, in chiral Schlegel representation.
plane: Plane to clip against.
atol: Absolute tolerance for "on-plane" checks. Default 1e-12.
Returns:
same_side_poly, other_side_poly
The first polyhedron is on from the same side as the plane's normal vector.
"""
return _split(poly, plane, same_only=False)
return _split(poly, plane, same_only=False, atol=atol)
@overload
def _split(poly: poly_t, plane: plane_t, same_only: Literal[True]) -> poly_t:
def _split(poly: poly_t, plane: plane_t, same_only: Literal[True], atol: float = 1e-12) -> poly_t:
...
@overload
def _split(poly: poly_t, plane: plane_t, same_only: Literal[False]) -> tuple[poly_t, poly_t]:
def _split(poly: poly_t, plane: plane_t, same_only: Literal[False], atol: float = 1e-12) -> tuple[poly_t, poly_t]:
...
@overload
def _split(poly: poly_t, plane: plane_t, same_only: bool) -> tuple[poly_t, poly_t] | poly_t:
def _split(poly: poly_t, plane: plane_t, same_only: bool, atol: float = 1e-12) -> tuple[poly_t, poly_t] | poly_t:
...
def _split(poly: poly_t, plane: plane_t, same_only: bool = False) -> tuple[poly_t, poly_t] | poly_t:
def _split(poly: poly_t, plane: plane_t, same_only: bool = False, atol: float = 1e-12) -> tuple[poly_t, poly_t] | poly_t:
"""
Clip a polyhedron with a plane.
@ -67,6 +69,7 @@ def _split(poly: poly_t, plane: plane_t, same_only: bool = False) -> tuple[poly_
plane: Plane to clip against.
same_only: If True, only returns the clipped poly from the same side as the
plane's normal vector. Default False.
atol: Absolute tolerance for "on-plane" checks. Default 1e-12.
Returns:
(same_side_poly, other_side_poly) if same_only is False (default), otherwise
only returns same_side_poly.
@ -75,25 +78,35 @@ def _split(poly: poly_t, plane: plane_t, same_only: bool = False) -> tuple[poly_
neighbors = poly['neighbors']
num_start = neighbors.shape[0]
if num_start == 0:
ret = build_empty(), build_empty()
return ret[0] if same_only else ret
dists = plane[1] + (vertices @ plane[0])
# 1. First, check if the polyhedron is entirely on one side (within tolerance)
# If all distances are > -atol, the whole thing is effectively on the "left"
# If all distances are < atol, the whole thing is effectively on the "right"
if (dists >= -atol).all():
# Entirely on "left" or touching plane within tolerance
ret = poly, build_empty()
return ret[0] if same_only else ret
if (dists <= atol).all():
# Entirely on "right" or touching plane within tolerance
ret = build_empty(), poly
return ret[0] if same_only else ret
# 2. If it's truly crossing, perform a binary partition for the splitting algorithm.
# We use > 0 for binary robustness of the graph surgery logic.
left = dists > 0
right = numpy.logical_not(left)
num_left = numpy.count_nonzero(left)
num_right = num_start - num_left
#
# if the poly lies entirely on one side of the plane, just return it
#
if num_left == 0:
# if all on right or touching plane
ret = build_empty(), poly
return ret[0] if same_only else ret
if dists.min() >= 0:
# if all on left or touching plane
# Can't reuse 'left' because <= vs <
ret = poly, build_empty()
return ret[0] if same_only else ret
# Note: Because of the atol checks above, we know num_left and num_right are both > 0
# and at least one point is > atol and at least one is < -atol.
right_inds = numpy.where(right)[0]
next_vertex = neighbors[right_inds, :].ravel()
@ -220,4 +233,3 @@ def _split(poly: poly_t, plane: plane_t, same_only: bool = False) -> tuple[poly_
}
return left_poly, right_poly

View file

@ -151,6 +151,33 @@ def test_manual_nonconvex(poly_order: int, n_zigs: int = 3, z_offset: float = 0.
split_and_check(poly, (numpy.array((0, 1, 0)), -0.5 * (1.0 + z_offset)), poly_order)
def test_clip_with_atol() -> None:
"""
Verify that atol correctly handles vertices very close to the plane.
"""
# Unit tetrahedron
tet = adze.build_tetrahedron([
[0, 0, 0],
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])
# Plane x = 0 (keeping x > 0)
plane = (numpy.array([1, 0, 0]), 0)
# Move one vertex slightly to x = -1e-13
tet['vertices'][0, 0] = -1e-13
# Without atol (default 1e-12), it should still keep the whole thing
# because -1e-13 > -1e-12.
clipped = adze.clip(tet, plane, atol=1e-12)
assert_allclose(adze.get_volume(tet), adze.get_volume(clipped))
# With a very small atol, it might clip it
clipped_small = adze.clip(tet, plane, atol=1e-15)
assert adze.get_volume(clipped_small) < adze.get_volume(tet)
def test_torus(poly_order: int) -> None:
"""
Recursively split a torus with random degenerate cut planes until the pieces