2018-03-09 16:45:38 -08:00
|
|
|
import numpy
|
|
|
|
|
|
2018-03-28 14:58:38 -07:00
|
|
|
from . import poly_t, plane_t
|
2018-03-28 18:49:08 -07:00
|
|
|
from .util import build_empty, _get_map_rows
|
2018-03-09 16:45:38 -08:00
|
|
|
|
|
|
|
|
try:
|
2018-03-28 10:54:15 -07:00
|
|
|
# Check if running with line_profiler
|
2018-03-09 16:45:38 -08:00
|
|
|
profile
|
2018-03-20 15:10:45 -07:00
|
|
|
except NameError:
|
2018-03-09 16:45:38 -08:00
|
|
|
def profile(f):
|
|
|
|
|
return f
|
|
|
|
|
|
|
|
|
|
|
2018-03-28 14:58:38 -07:00
|
|
|
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.
|
2018-03-09 16:45:38 -08:00
|
|
|
|
2018-03-28 14:58:38 -07:00
|
|
|
This function is equivalent to calling split(poly, plane, same_only=True).
|
2018-03-09 16:45:38 -08:00
|
|
|
|
2018-03-28 14:58:38 -07:00
|
|
|
: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)
|
2018-03-20 15:10:45 -07:00
|
|
|
return left_poly
|
|
|
|
|
|
2018-03-09 16:45:38 -08:00
|
|
|
|
|
|
|
|
@profile
|
2018-03-28 14:58:38 -07:00
|
|
|
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.
|
|
|
|
|
'''
|
2018-03-09 16:45:38 -08:00
|
|
|
vertices = poly['vertices']
|
|
|
|
|
neighbors = poly['neighbors']
|
2018-03-21 11:39:31 -07:00
|
|
|
num_start = poly['vertices'].shape[0]
|
2018-03-09 16:45:38 -08:00
|
|
|
|
|
|
|
|
dists = plane[1] + (vertices @ plane[0])
|
|
|
|
|
left = dists > 0
|
|
|
|
|
right = numpy.logical_not(left)
|
|
|
|
|
|
2018-03-21 11:39:31 -07:00
|
|
|
num_left = numpy.count_nonzero(left)
|
|
|
|
|
num_right = num_start - num_left
|
|
|
|
|
|
2018-03-28 14:58:38 -07:00
|
|
|
'''
|
|
|
|
|
if the poly lies entirely on one side of the plane, just return it
|
|
|
|
|
'''
|
2018-03-21 11:39:31 -07:00
|
|
|
if num_left == 0:
|
2018-03-09 16:45:38 -08:00
|
|
|
# if all on right or touching plane
|
2018-03-28 14:58:38 -07:00
|
|
|
ret = build_empty(), poly
|
2018-03-28 21:33:16 -07:00
|
|
|
return ret[0] if same_only else ret
|
2018-03-21 11:39:31 -07:00
|
|
|
elif dists.min() >= 0:
|
2018-03-09 16:45:38 -08:00
|
|
|
# if all on left or touching plane
|
|
|
|
|
# Can't reuse 'left' because <= vs <
|
2018-03-28 14:58:38 -07:00
|
|
|
ret = poly, build_empty()
|
|
|
|
|
return ret[0] if same_only else ret
|
2018-03-09 16:45:38 -08:00
|
|
|
|
|
|
|
|
right_inds = numpy.where(right)[0]
|
2018-03-21 13:37:59 -07:00
|
|
|
next_vertex = neighbors[right_inds, :].ravel()
|
2018-03-09 16:45:38 -08:00
|
|
|
|
|
|
|
|
# remove edges which connect two same-side vertices
|
2018-03-21 13:37:59 -07:00
|
|
|
far_vertex_left = left[next_vertex]
|
2018-03-09 16:45:38 -08:00
|
|
|
|
2018-03-21 13:37:59 -07:00
|
|
|
if not far_vertex_left.any():
|
2018-03-28 14:58:38 -07:00
|
|
|
'''
|
|
|
|
|
No edges cross the plane, but components exist on either side
|
|
|
|
|
'''
|
2018-03-20 15:10:45 -07:00
|
|
|
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)
|
2018-03-21 13:37:59 -07:00
|
|
|
src_pns = target_edge_inds.copy()
|
2018-03-20 15:10:45 -07:00
|
|
|
num_new_vertices = 0
|
2018-03-21 13:37:59 -07:00
|
|
|
re = numpy.empty(0, dtype=int)
|
|
|
|
|
le = re.copy()
|
2018-03-20 15:10:45 -07:00
|
|
|
else:
|
2018-03-28 14:58:38 -07:00
|
|
|
'''
|
|
|
|
|
Some edges cross the plane
|
|
|
|
|
'''
|
2018-03-21 13:37:59 -07:00
|
|
|
right_inds3 = numpy.repeat(right_inds, 3)
|
|
|
|
|
|
|
|
|
|
# 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
|
2018-03-28 22:57:38 -07:00
|
|
|
# remove edges which don't also touch the left half-plane
|
|
|
|
|
rverts = right_inds3[far_vertex_left]
|
|
|
|
|
lverts = next_vertex[far_vertex_left]
|
|
|
|
|
pinds = (numpy.arange(3 * num_right) % 3)[far_vertex_left]
|
|
|
|
|
# r2l_edges = numpy.column_stack((right_inds3, next_vertex, pnbr_inds))
|
|
|
|
|
# r2l_edges = r2l_edges[far_vertex_left, :] # remove edges which don't also touch the left half-plane
|
2018-03-21 13:37:59 -07:00
|
|
|
|
2018-03-28 22:57:38 -07:00
|
|
|
num_new_vertices = rverts.size
|
2018-03-09 16:45:38 -08:00
|
|
|
|
2018-03-28 14:58:38 -07:00
|
|
|
'''
|
|
|
|
|
walk to next crossing edge
|
|
|
|
|
'''
|
2018-03-28 22:57:38 -07:00
|
|
|
prev_verts = rverts
|
|
|
|
|
cur_verts = lverts
|
2018-03-20 15:10:45 -07:00
|
|
|
edge_walk_inds = numpy.arange(num_new_vertices)
|
2018-03-28 22:57:38 -07:00
|
|
|
edge_walk_results = numpy.empty((rverts.size, 2), dtype=int)
|
2018-03-20 15:10:45 -07:00
|
|
|
src_pns = None
|
2018-03-21 11:39:31 -07:00
|
|
|
while cur_verts.size > 0:
|
2018-03-20 15:10:45 -07:00
|
|
|
# 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
|
2018-03-09 16:45:38 -08:00
|
|
|
|
2018-03-20 15:10:45 -07:00
|
|
|
if src_pns is None:
|
|
|
|
|
src_pns = src_pn
|
2018-03-09 16:45:38 -08:00
|
|
|
|
2018-03-20 15:10:45 -07:00
|
|
|
next_verts = cur_links[(row, new_pn)]
|
2018-03-09 16:45:38 -08:00
|
|
|
|
2018-03-21 11:39:31 -07:00
|
|
|
# Save edges that cross back to right, and remove them from the list of verts
|
2018-03-20 15:10:45 -07:00
|
|
|
done = right[next_verts]
|
2018-03-21 14:14:46 -07:00
|
|
|
edge_walk_results[edge_walk_inds[done], 1] = cur_verts[done]
|
|
|
|
|
edge_walk_results[edge_walk_inds[done], 0] = next_verts[done]
|
2018-03-28 14:58:38 -07:00
|
|
|
|
|
|
|
|
# Save the current vertices as previous, and our chosen neighbors as current
|
|
|
|
|
not_done = numpy.logical_not(done)
|
2018-03-21 11:39:31 -07:00
|
|
|
prev_verts = cur_verts[not_done]
|
|
|
|
|
cur_verts = next_verts[not_done]
|
2018-03-20 15:10:45 -07:00
|
|
|
edge_walk_inds = edge_walk_inds[not_done]
|
2018-03-09 16:45:38 -08:00
|
|
|
|
2018-03-21 14:14:46 -07:00
|
|
|
# now edge_walk_results is list of [[target clipped adjacent vertex, target connected vertex], ...]
|
2018-03-09 16:45:38 -08:00
|
|
|
|
2018-03-20 15:10:45 -07:00
|
|
|
# figure out the index of each target edge...
|
2018-03-28 22:57:38 -07:00
|
|
|
arr = numpy.column_stack((rverts, lverts))
|
|
|
|
|
target_edge_inds = _get_map_rows(arr, edge_walk_results)
|
2018-03-09 16:45:38 -08:00
|
|
|
|
2018-03-28 22:57:38 -07:00
|
|
|
re, le = rverts, lverts
|
2018-03-21 13:37:59 -07:00
|
|
|
d_right = dists[re][:, None]
|
|
|
|
|
d_left = dists[le][:, None]
|
|
|
|
|
new_vertices = (d_right * vertices[le] - d_left * vertices[re]) / (d_right - d_left)
|
2018-03-09 16:45:38 -08:00
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
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[le], # 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
|
2018-03-20 15:10:45 -07:00
|
|
|
old_neighbors_left[(le, src_pns)] = num_left + numpy.arange(num_new_vertices)
|
2018-03-09 16:45:38 -08:00
|
|
|
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)),
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-28 14:58:38 -07:00
|
|
|
if same_only:
|
2018-03-09 16:45:38 -08:00
|
|
|
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[re], # 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]]
|
2018-03-28 22:57:38 -07:00
|
|
|
old_neighbors_right[(re, pinds)] = num_right + numpy.arange(num_new_vertices)
|
2018-03-09 16:45:38 -08:00
|
|
|
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
|
|
|
|
|
|