[clip / _split] always return a new poly
This commit is contained in:
parent
f834598e0f
commit
05f9186268
2 changed files with 50 additions and 24 deletions
|
|
@ -1,8 +1,41 @@
|
||||||
from typing import overload, Literal
|
from typing import overload, Literal
|
||||||
|
import math
|
||||||
import numpy
|
import numpy
|
||||||
|
|
||||||
from . import poly_t, plane_t
|
from . import poly_t, plane_t
|
||||||
from .util import build_empty, _get_map
|
from .util import build_empty, _get_map, AdzeError
|
||||||
|
|
||||||
|
|
||||||
|
def _copy_poly(poly: poly_t) -> poly_t:
|
||||||
|
return {
|
||||||
|
'vertices': poly['vertices'].copy(),
|
||||||
|
'neighbors': poly['neighbors'].copy(),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _normalize_plane(plane: plane_t) -> plane_t:
|
||||||
|
normal = plane[0]
|
||||||
|
if not isinstance(normal, numpy.ndarray) or normal.shape != (3,):
|
||||||
|
normal = numpy.asarray(normal, dtype=float)
|
||||||
|
if normal.shape != (3,):
|
||||||
|
raise AdzeError(f'Plane normal must have shape (3,). Got {normal.shape}')
|
||||||
|
|
||||||
|
nx = float(normal[0])
|
||||||
|
ny = float(normal[1])
|
||||||
|
nz = float(normal[2])
|
||||||
|
if not (math.isfinite(nx) and math.isfinite(ny) and math.isfinite(nz)):
|
||||||
|
raise AdzeError('Plane normal must contain only finite values')
|
||||||
|
if nx == 0.0 and ny == 0.0 and nz == 0.0:
|
||||||
|
raise AdzeError('Plane normal must be non-zero')
|
||||||
|
|
||||||
|
try:
|
||||||
|
distance = float(plane[1])
|
||||||
|
except (TypeError, ValueError) as exc:
|
||||||
|
distance_shape = numpy.asarray(plane[1]).shape
|
||||||
|
raise AdzeError(f'Plane distance must be a scalar. Got {distance_shape}') from exc
|
||||||
|
if not math.isfinite(distance):
|
||||||
|
raise AdzeError('Plane distance must be finite')
|
||||||
|
return normal, distance
|
||||||
|
|
||||||
|
|
||||||
def clip(poly: poly_t, plane: plane_t, atol: float = 1e-12) -> poly_t:
|
def clip(poly: poly_t, plane: plane_t, atol: float = 1e-12) -> poly_t:
|
||||||
|
|
@ -74,6 +107,7 @@ def _split(poly: poly_t, plane: plane_t, same_only: bool = False, atol: float =
|
||||||
(same_side_poly, other_side_poly) if same_only is False (default), otherwise
|
(same_side_poly, other_side_poly) if same_only is False (default), otherwise
|
||||||
only returns same_side_poly.
|
only returns same_side_poly.
|
||||||
"""
|
"""
|
||||||
|
plane = _normalize_plane(plane)
|
||||||
vertices = poly['vertices']
|
vertices = poly['vertices']
|
||||||
neighbors = poly['neighbors']
|
neighbors = poly['neighbors']
|
||||||
num_start = neighbors.shape[0]
|
num_start = neighbors.shape[0]
|
||||||
|
|
@ -84,30 +118,20 @@ def _split(poly: poly_t, plane: plane_t, same_only: bool = False, atol: float =
|
||||||
|
|
||||||
dists = plane[1] + (vertices @ plane[0])
|
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():
|
if (dists >= -atol).all():
|
||||||
# Entirely on "left" or touching plane within tolerance
|
ret = _copy_poly(poly), build_empty()
|
||||||
ret = poly, build_empty()
|
|
||||||
return ret[0] if same_only else ret
|
return ret[0] if same_only else ret
|
||||||
|
|
||||||
if (dists <= atol).all():
|
if (dists <= atol).all():
|
||||||
# Entirely on "right" or touching plane within tolerance
|
ret = build_empty(), _copy_poly(poly)
|
||||||
ret = build_empty(), poly
|
|
||||||
return ret[0] if same_only else ret
|
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
|
left = dists > 0
|
||||||
right = numpy.logical_not(left)
|
right = numpy.logical_not(left)
|
||||||
|
|
||||||
num_left = numpy.count_nonzero(left)
|
num_left = numpy.count_nonzero(left)
|
||||||
num_right = num_start - num_left
|
num_right = num_start - num_left
|
||||||
|
|
||||||
# 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]
|
right_inds = numpy.where(right)[0]
|
||||||
next_vertex = neighbors[right_inds, :].ravel()
|
next_vertex = neighbors[right_inds, :].ravel()
|
||||||
|
|
||||||
|
|
@ -118,8 +142,8 @@ def _split(poly: poly_t, plane: plane_t, same_only: bool = False, atol: float =
|
||||||
#
|
#
|
||||||
# No edges cross the plane, but components exist on either side
|
# No edges cross the plane, but components exist on either side
|
||||||
#
|
#
|
||||||
# r2l_edges = numpy.empty((0, 3), dtype=int)
|
rl_edge_inds = numpy.empty(0, dtype=int)
|
||||||
target_edge_inds = numpy.empty((0, 1), dtype=int)
|
target_edge_inds = numpy.empty(0, dtype=int)
|
||||||
new_vertices = numpy.empty((0, 3), dtype=float)
|
new_vertices = numpy.empty((0, 3), dtype=float)
|
||||||
src_pns = target_edge_inds.copy()
|
src_pns = target_edge_inds.copy()
|
||||||
num_new_vertices = 0
|
num_new_vertices = 0
|
||||||
|
|
@ -181,26 +205,22 @@ def _split(poly: poly_t, plane: plane_t, same_only: bool = False, atol: float =
|
||||||
b1d = numpy.ravel_multi_index(tuple(edge_walk_results), dims)
|
b1d = numpy.ravel_multi_index(tuple(edge_walk_results), dims)
|
||||||
target_edge_inds = _get_map(a1d, b1d)
|
target_edge_inds = _get_map(a1d, b1d)
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# map from old vertex ind to new
|
# map from old vertex ind to new
|
||||||
#
|
#
|
||||||
ind_map_left = numpy.zeros(num_start, dtype=int)
|
ind_map_left = numpy.zeros(num_start, dtype=int)
|
||||||
# current side vertices get new (equal or lower) indices from delete operation, remain in same order
|
|
||||||
ind_map_left[left] = numpy.arange(num_left)
|
ind_map_left[left] = numpy.arange(num_left)
|
||||||
# Hops across the boundary depend on (origin, destination) pair since that determines final vertex
|
|
||||||
|
|
||||||
# use argsort to generate link from target edge to current edge
|
|
||||||
target_order = numpy.argsort(target_edge_inds)
|
target_order = numpy.argsort(target_edge_inds)
|
||||||
new_neighbors_left = numpy.column_stack((
|
new_neighbors_left = numpy.column_stack((
|
||||||
ind_map_left[lverts], # update left vertices based on new indexing
|
ind_map_left[lverts],
|
||||||
target_order + num_left, # new vertex indices are same as edge indices + offset
|
target_order + num_left,
|
||||||
target_edge_inds + num_left))
|
target_edge_inds + num_left))
|
||||||
|
|
||||||
old_vertices_left = vertices[left]
|
old_vertices_left = vertices[left]
|
||||||
|
|
||||||
old_neighbors_left = neighbors.copy()
|
old_neighbors_left = neighbors.copy()
|
||||||
old_neighbors_left[left] = ind_map_left[old_neighbors_left[left]] # map vertices on left side, wrongly mapping any right-side vertices to 0
|
old_neighbors_left[left] = ind_map_left[old_neighbors_left[left]]
|
||||||
old_neighbors_left[(lverts, src_pns)] = num_left + numpy.arange(num_new_vertices)
|
old_neighbors_left[(lverts, src_pns)] = num_left + numpy.arange(num_new_vertices)
|
||||||
old_neighbors_left = old_neighbors_left[left]
|
old_neighbors_left = old_neighbors_left[left]
|
||||||
|
|
||||||
|
|
@ -216,8 +236,8 @@ def _split(poly: poly_t, plane: plane_t, same_only: bool = False, atol: float =
|
||||||
ind_map_right[right] = numpy.arange(num_right)
|
ind_map_right[right] = numpy.arange(num_right)
|
||||||
|
|
||||||
new_neighbors_right = numpy.column_stack((
|
new_neighbors_right = numpy.column_stack((
|
||||||
ind_map_right[rverts], # update right vertices based on new indexing
|
ind_map_right[rverts],
|
||||||
target_edge_inds + num_right, # new vertex indices are same as edge indices + offset
|
target_edge_inds + num_right,
|
||||||
target_order + num_right))
|
target_order + num_right))
|
||||||
|
|
||||||
old_vertices_right = vertices[right]
|
old_vertices_right = vertices[right]
|
||||||
|
|
|
||||||
|
|
@ -237,6 +237,7 @@ def test_voxelize_requires_strictly_increasing_axes() -> None:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
<<<<<<< HEAD
|
||||||
def test_clip_rejects_zero_normal_plane() -> None:
|
def test_clip_rejects_zero_normal_plane() -> None:
|
||||||
poly = adze.build_box([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]])
|
poly = adze.build_box([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]])
|
||||||
|
|
||||||
|
|
@ -279,6 +280,8 @@ def test_boundary_from_poly_rejects_invalid_graph() -> None:
|
||||||
adze.boundary_from_poly(poly)
|
adze.boundary_from_poly(poly)
|
||||||
|
|
||||||
|
|
||||||
|
=======
|
||||||
|
>>>>>>> 34c60fa ([clip / _split] always return a new poly)
|
||||||
def test_clip_noop_returns_independent_poly() -> None:
|
def test_clip_noop_returns_independent_poly() -> None:
|
||||||
poly = adze.build_box([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]])
|
poly = adze.build_box([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]])
|
||||||
|
|
||||||
|
|
@ -300,6 +303,7 @@ def test_split_noop_returns_independent_poly() -> None:
|
||||||
assert kept_poly['vertices'] is not poly['vertices']
|
assert kept_poly['vertices'] is not poly['vertices']
|
||||||
kept_poly['vertices'][0, 0] = 999.0
|
kept_poly['vertices'][0, 0] = 999.0
|
||||||
assert poly['vertices'][0, 0] == 0.0
|
assert poly['vertices'][0, 0] == 0.0
|
||||||
|
<<<<<<< HEAD
|
||||||
|
|
||||||
|
|
||||||
def test_split_handles_disconnected_components_without_cut_edges() -> None:
|
def test_split_handles_disconnected_components_without_cut_edges() -> None:
|
||||||
|
|
@ -339,3 +343,5 @@ def test_tetrahedron_helpers_reject_negative_winding(tetra_fun) -> None:
|
||||||
[1.0, 0.0, 0.0],
|
[1.0, 0.0, 0.0],
|
||||||
[0.0, 0.0, 1.0],
|
[0.0, 0.0, 1.0],
|
||||||
])
|
])
|
||||||
|
=======
|
||||||
|
>>>>>>> 34c60fa ([clip / _split] always return a new poly)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue