diff --git a/chop/pyr3d.py b/chop/pyr3d.py index c6f6fb8..3833ad5 100644 --- a/chop/pyr3d.py +++ b/chop/pyr3d.py @@ -26,17 +26,21 @@ def clip(poly, plane): def split(poly, plane, left_only: bool = False): vertices = poly['vertices'] neighbors = poly['neighbors'] + num_start = poly['vertices'].shape[0] dists = plane[1] + (vertices @ plane[0]) 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 ret = None - if not numpy.any(left): + if num_left == 0: # if all on right or touching plane ret = build_empty_poly(), poly - elif numpy.min(dists) >= 0: + elif dists.min() >= 0: # if all on left or touching plane # Can't reuse 'left' because <= vs < ret = poly, build_empty_poly() @@ -45,7 +49,7 @@ def split(poly, plane, left_only: bool = False): right_inds = numpy.where(right)[0] right_inds3 = numpy.repeat(right_inds, 3) - pnbr_inds = numpy.tile(numpy.arange(3), right_inds.size) + pnbr_inds = numpy.tile(numpy.arange(3), num_right) # array of [[clipped vertex (cv0), connected vertex pv00], [cv0, pv01], [cv0, pv02], [cv1, pv10], ...] # which is effectively a list of all edges which touch the right half-plane @@ -70,14 +74,12 @@ def split(poly, plane, left_only: bool = False): num_new_vertices = r2l_edges.shape[0] # walk counter-clockwise to next crossing edge - edge_walk = r2l_edges[:, :2].copy() + prev_verts = r2l_edges[:, 0].copy() + cur_verts = r2l_edges[:, 1].copy() edge_walk_inds = numpy.arange(num_new_vertices) - edge_walk_results = numpy.empty_like(edge_walk) + edge_walk_results = numpy.empty((r2l_edges.shape[0], 2), dtype=int) src_pns = None - while edge_walk.size > 0: - prev_verts = edge_walk[:, 0] - cur_verts = edge_walk[:, 1] - + while cur_verts.size > 0: # Get the list of our current neighbors, find which one points to the # vertex we came from, and choose the next (ccw) one cur_links = neighbors[cur_verts] @@ -90,15 +92,16 @@ 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 edge_walk + # 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], 0] = cur_verts[done] edge_walk_results[edge_walk_inds[done], 1] = next_verts[done] - edge_walk = numpy.column_stack((cur_verts[not_done], next_verts[not_done])) + prev_verts = cur_verts[not_done] + cur_verts = next_verts[not_done] edge_walk_inds = edge_walk_inds[not_done] - # now edge_walk is list of [[target connected vertex, target clipped adjacent vertex], ...] + # now edge_walk_results is list of [[target connected vertex, target clipped adjacent vertex], ...] # figure out the index of each target edge... target_edge_inds = _searchsorted_rows(r2l_edges[:, :2], edge_walk_results[:, ::-1]) @@ -110,9 +113,6 @@ def split(poly, plane, left_only: bool = False): new_vertices = (d_right * vertices[v_left] - d_left * vertices[v_right]) / (d_right - d_left) # vertex counts - num_start = poly['vertices'].shape[0] - num_left = numpy.count_nonzero(left) - num_right = num_start - num_left ''' map from old vertex ind to new