import numpy from . import poly_t, plane_t from .util import build_empty, _get_map_rows try: # Check if running with line_profiler profile except NameError: def profile(f): return f 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). :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, 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] 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 ''' if num_left == 0: # if all on right or touching plane ret = build_empty(), poly return ret[0] if same_only else ret elif 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 right_inds = numpy.where(right)[0] next_vertex = neighbors[right_inds, :].ravel() # remove edges which connect two same-side vertices far_vertex_left = left[next_vertex] if not far_vertex_left.any(): ''' 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) src_pns = target_edge_inds.copy() num_new_vertices = 0 rverts = numpy.empty(0, dtype=int) lverts = rverts.copy() else: ''' Some edges cross the plane ''' right_inds3 = numpy.repeat(right_inds, 3) # remove edges which don't also touch the left half-plane rverts = right_inds3[far_vertex_left] lverts = next_vertex[far_vertex_left] if not same_only: pinds = (numpy.arange(3 * num_right) % 3)[far_vertex_left] num_new_vertices = rverts.size ''' walk to next crossing edge ''' prev_verts = rverts cur_verts = lverts edge_walk_inds = numpy.arange(num_new_vertices) edge_walk_results = numpy.empty((num_new_vertices, 2), dtype=int) src_pns = None 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] row, src_pn = numpy.where(cur_links == prev_verts[:, None]) new_pn = (src_pn + 1) % 3 if src_pns is None: src_pns = src_pn next_verts = cur_links[(row, new_pn)] # Save edges that cross back to right, and remove them from the list of verts done = right[next_verts] 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] # now edge_walk_results is list of [[target clipped adjacent vertex, target connected vertex], ...] # figure out the index of each target edge... arr = numpy.column_stack((rverts, lverts)) target_edge_inds = _get_map_rows(arr, edge_walk_results) d_right = dists[rverts][:, None] d_left = dists[lverts][:, None] new_vertices = (d_right * vertices[lverts] - d_left * vertices[rverts]) / (d_right - d_left) ''' map from old vertex ind to new ''' 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) # 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) new_neighbors_left = numpy.column_stack(( ind_map_left[lverts], # update left vertices based on new indexing target_order + num_left, # new vertex indices are same as edge indices + offset target_edge_inds + num_left)) old_vertices_left = poly['vertices'][left] old_neighbors_left = poly['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[(lverts, src_pns)] = num_left + numpy.arange(num_new_vertices) old_neighbors_left = old_neighbors_left[left] left_poly = { 'vertices': numpy.vstack((old_vertices_left, new_vertices)), 'neighbors': numpy.vstack((old_neighbors_left, new_neighbors_left)), } if same_only: return left_poly ind_map_right = numpy.zeros(num_start, dtype=int) ind_map_right[right] = numpy.arange(num_right) new_neighbors_right = numpy.column_stack(( ind_map_right[rverts], # update right vertices based on new indexing target_edge_inds + num_right, # new vertex indices are same as edge indices + offset target_order + num_right)) old_vertices_right = poly['vertices'][right] old_neighbors_right = poly['neighbors'].copy() old_neighbors_right[right] = ind_map_right[old_neighbors_right[right]] old_neighbors_right[(rverts, pinds)] = num_right + numpy.arange(num_new_vertices) old_neighbors_right = old_neighbors_right[right] right_poly = { 'vertices': numpy.vstack((old_vertices_right, new_vertices)), 'neighbors': numpy.vstack((old_neighbors_right, new_neighbors_right)), } return left_poly, right_poly