mostly comments, but broke something
This commit is contained in:
parent
fc82bc255c
commit
3f8b9bef06
1 changed files with 366 additions and 127 deletions
|
|
@ -1,7 +1,7 @@
|
|||
from typing import Dict, Tuple
|
||||
import numpy
|
||||
|
||||
from .util import _searchsorted_rows, _argsort_rows
|
||||
from . import poly_t, plane_t
|
||||
from .util import _searchsorted_rows, _argsort_rows, build_empty
|
||||
|
||||
try:
|
||||
# Check if running with line_profiler
|
||||
|
|
@ -11,20 +11,37 @@ except NameError:
|
|||
return f
|
||||
|
||||
|
||||
def build_empty_poly() -> Dict:
|
||||
return {
|
||||
'vertices': numpy.empty((0, 3), dtype=float),
|
||||
'neighbors': numpy.empty((0, 3), dtype=int),
|
||||
}
|
||||
def clip(poly: poly_t, plane: plane_t) -> poly_t:
|
||||
'''
|
||||
Clip a polyhedron with a plane, keeping only the part which is on
|
||||
the same side as the plane's normal vector.
|
||||
|
||||
This function is equivalent to calling split(poly, plane, same_only=True).
|
||||
|
||||
def clip(poly, plane):
|
||||
left_poly = split(poly, plane, left_only=True)
|
||||
:param poly: Polyhedron to clip, in chiral Schlegel representation.
|
||||
:param plane: Plane to clip against.
|
||||
:return: Clipped polyhedron.
|
||||
'''
|
||||
left_poly = split(poly, plane, same_only=True)
|
||||
return left_poly
|
||||
|
||||
|
||||
@profile
|
||||
def split(poly, plane, left_only: bool = False):
|
||||
def split(poly, plane, same_only: bool = False):
|
||||
'''
|
||||
Clip a polyhedron with a plane.
|
||||
|
||||
If same_only is False (default), returns the tuple (same_side_poly, other_side_poly),
|
||||
where same_side_poly is on the same side as the plane's normal vector. Otherwise,
|
||||
returns only same_side_poly (somewhat faster).
|
||||
|
||||
:param poly: Polyhedron to clip, in chiral Schlegel representation.
|
||||
:param plane: Plane to clip against.
|
||||
:param same_only: If True, only returns the clipped poly from the same side as the
|
||||
plane's normal vector. Default False.
|
||||
:return: (same_side_poly, other_side_poly) if same_only is False (default), otherwise
|
||||
only returns same_side_poly.
|
||||
'''
|
||||
vertices = poly['vertices']
|
||||
neighbors = poly['neighbors']
|
||||
num_start = poly['vertices'].shape[0]
|
||||
|
|
@ -36,17 +53,19 @@ def split(poly, plane, left_only: bool = False):
|
|||
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 the poly lies entirely on one side of the plane, just return it
|
||||
'''
|
||||
ret = None
|
||||
if num_left == 0:
|
||||
# if all on right or touching plane
|
||||
ret = build_empty_poly(), poly
|
||||
ret = build_empty(), poly
|
||||
elif dists.min() >= 0:
|
||||
# if all on left or touching plane
|
||||
# Can't reuse 'left' because <= vs <
|
||||
ret = poly, build_empty_poly()
|
||||
ret = poly, build_empty()
|
||||
if ret is not None:
|
||||
return ret[0] if left_only else ret
|
||||
return ret[0] if same_only else ret
|
||||
|
||||
right_inds = numpy.where(right)[0]
|
||||
next_vertex = neighbors[right_inds, :].ravel()
|
||||
|
|
@ -55,7 +74,9 @@ def split(poly, plane, left_only: bool = False):
|
|||
far_vertex_left = left[next_vertex]
|
||||
|
||||
if not far_vertex_left.any():
|
||||
# No edges cross, but components on either side
|
||||
'''
|
||||
No edges cross the plane, but components exist on either side
|
||||
'''
|
||||
r2l_edges = numpy.empty((0, 3), dtype=int)
|
||||
target_edge_inds = numpy.empty((0, 1), dtype=int)
|
||||
new_vertices = numpy.empty((0, 3), dtype=float)
|
||||
|
|
@ -64,6 +85,9 @@ def split(poly, plane, left_only: bool = False):
|
|||
re = numpy.empty(0, dtype=int)
|
||||
le = re.copy()
|
||||
else:
|
||||
'''
|
||||
Some edges cross the plane
|
||||
'''
|
||||
right_inds3 = numpy.repeat(right_inds, 3)
|
||||
pnbr_inds = numpy.arange(3 * num_right) % 3
|
||||
|
||||
|
|
@ -78,7 +102,9 @@ def split(poly, plane, left_only: bool = False):
|
|||
|
||||
num_new_vertices = r2l_edges.shape[0]
|
||||
|
||||
# walk counter-clockwise to next crossing edge
|
||||
'''
|
||||
walk to next crossing edge
|
||||
'''
|
||||
prev_verts = r2l_edges[:, 0]
|
||||
cur_verts = r2l_edges[:, 1]
|
||||
edge_walk_inds = numpy.arange(num_new_vertices)
|
||||
|
|
@ -96,12 +122,13 @@ def split(poly, plane, left_only: bool = False):
|
|||
|
||||
next_verts = cur_links[(row, new_pn)]
|
||||
|
||||
# Save the current vertices as previous, and our chosen neighbors as current
|
||||
# Save edges that cross back to right, and remove them from the list of verts
|
||||
done = right[next_verts]
|
||||
not_done = numpy.logical_not(done)
|
||||
edge_walk_results[edge_walk_inds[done], 1] = cur_verts[done]
|
||||
edge_walk_results[edge_walk_inds[done], 0] = next_verts[done]
|
||||
|
||||
# Save the current vertices as previous, and our chosen neighbors as current
|
||||
not_done = numpy.logical_not(done)
|
||||
prev_verts = cur_verts[not_done]
|
||||
cur_verts = next_verts[not_done]
|
||||
edge_walk_inds = edge_walk_inds[not_done]
|
||||
|
|
@ -144,7 +171,7 @@ def split(poly, plane, left_only: bool = False):
|
|||
'neighbors': numpy.vstack((old_neighbors_left, new_neighbors_left)),
|
||||
}
|
||||
|
||||
if left_only:
|
||||
if same_only:
|
||||
return left_poly
|
||||
|
||||
ind_map_right = numpy.zeros(num_start, dtype=int)
|
||||
|
|
|
|||
Loading…
Reference in a new issue