split up split and moments
This commit is contained in:
parent
fb0715e618
commit
acb86b8013
1 changed files with 166 additions and 154 deletions
170
chop/split.py
Normal file
170
chop/split.py
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
from typing import Dict, Tuple
|
||||
import numpy
|
||||
|
||||
from .util import _searchsorted_rows, _argsort_rows
|
||||
|
||||
try:
|
||||
profile
|
||||
except NameError:
|
||||
def profile(f):
|
||||
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, plane):
|
||||
left_poly = split(poly, plane, left_only=True)
|
||||
return left_poly
|
||||
|
||||
|
||||
@profile
|
||||
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 num_left == 0:
|
||||
# if all on right or touching plane
|
||||
ret = build_empty_poly(), poly
|
||||
elif dists.min() >= 0:
|
||||
# if all on left or touching plane
|
||||
# Can't reuse 'left' because <= vs <
|
||||
ret = poly, build_empty_poly()
|
||||
if ret is not None:
|
||||
return ret[0] if left_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, but components 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
|
||||
re = numpy.empty(0, dtype=int)
|
||||
le = re.copy()
|
||||
else:
|
||||
right_inds3 = numpy.repeat(right_inds, 3)
|
||||
pnbr_inds = numpy.arange(3 * num_right) % 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
|
||||
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
|
||||
|
||||
# sort now so we can use searchsorted later
|
||||
r2l_edge_order = _argsort_rows(r2l_edges[:, :2])
|
||||
r2l_edges = r2l_edges[r2l_edge_order]
|
||||
|
||||
num_new_vertices = r2l_edges.shape[0]
|
||||
|
||||
# walk counter-clockwise to next crossing edge
|
||||
prev_verts = r2l_edges[:, 0]
|
||||
cur_verts = r2l_edges[:, 1]
|
||||
edge_walk_inds = numpy.arange(num_new_vertices)
|
||||
edge_walk_results = numpy.empty((r2l_edges.shape[0], 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 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]
|
||||
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...
|
||||
target_edge_inds = _searchsorted_rows(r2l_edges[:, :2], edge_walk_results)
|
||||
|
||||
re = r2l_edges[:, 0]
|
||||
le = r2l_edges[:, 1]
|
||||
d_right = dists[re][:, None]
|
||||
d_left = dists[le][:, None]
|
||||
new_vertices = (d_right * vertices[le] - d_left * vertices[re]) / (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[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
|
||||
old_neighbors_left[(le, 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 left_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[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]]
|
||||
old_neighbors_right[(re, r2l_edges[:, 2])] = 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
|
||||
|
||||
Loading…
Reference in a new issue