diff --git a/chop/boundary.py b/chop/boundary.py index 5d65f28..e9e283f 100644 --- a/chop/boundary.py +++ b/chop/boundary.py @@ -3,7 +3,7 @@ import numpy from numpy.linalg import norm from . import poly_t, plane_t -from .util import _get_map, find_components, find_faces +from .util import _get_maps, find_components, find_faces try: # Check if running with line_profiler @@ -178,7 +178,7 @@ def build_polyhedron(vertices: numpy.ndarray, faces: numpy.ndarray) -> poly_t: for v in range(num_vertices): v0 = start[v] s = slice(v0, v0 + edges_per_vertex[v]) - m, n = _get_map(neighbors[s, 2], neighbors[s, 1]) + m, n = _get_maps(neighbors[s, 2], neighbors[s, 1]) neighbors[s, 0] = n + v0 neighbors[s, 2] = m + v0 @@ -212,7 +212,7 @@ def build_polyhedron(vertices: numpy.ndarray, faces: numpy.ndarray) -> poly_t: dst_mask[s:e] = target_vert[s:e] > v # mask out any references to verts >= current dst_vert = target_vert[dst_mask] - ma, mb = _get_map(dst_vert, src_vert) + ma, mb = _get_maps(dst_vert, src_vert) neighbors[dst_mask, 1] = numpy.where(src_mask)[0][mb] neighbors[src_mask, 1] = numpy.where(dst_mask)[0][ma] diff --git a/chop/split.py b/chop/split.py index c732082..c158f85 100644 --- a/chop/split.py +++ b/chop/split.py @@ -56,15 +56,14 @@ def split(poly, plane, same_only: bool = False): ''' 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 + 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() - if ret is not None: return ret[0] if same_only else ret right_inds = numpy.where(right)[0] @@ -132,7 +131,7 @@ def split(poly, plane, same_only: bool = False): # 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, _ = _get_map_rows(r2l_edges[:, :2], edge_walk_results) + target_edge_inds = _get_map_rows(r2l_edges[:, :2], edge_walk_results) re = r2l_edges[:, 0] le = r2l_edges[:, 1] diff --git a/chop/util.py b/chop/util.py index 437b0f8..8b40377 100644 --- a/chop/util.py +++ b/chop/util.py @@ -135,21 +135,59 @@ def find_faces(neighbors: numpy.ndarray) -> List[numpy.ndarray]: return cleaned_faces(remove, k) +@profile def _get_map(a, b): ''' - Returns m, n such that a[m] == b, b[n] == a + Returns m such that a[m] == b Assumes that all elements appear exactly once in each array ''' - assert(a.size == b.size) if a.size == 0: return numpy.array(()), numpy.array(()) sa = a.argsort() sb = b.argsort() - ra = numpy.empty_like(sa) - rb = numpy.empty_like(sa) - ra[sa] = numpy.arange(sa.size) - rb[sb] = numpy.arange(sa.size) - return sa[rb], sb[ra] + r = numpy.empty_like(sa) + r[sb] = numpy.arange(sa.size) + return sa[r] + + +@profile +def _get_map_rows(a, b): + ''' + Row-wise _get_map + ''' + dims = numpy.maximum(a.max(0), b.max(0)) + 1 + a1d = numpy.ravel_multi_index(a.T, dims) + b1d = numpy.ravel_multi_index(b.T, dims) + return _get_map(a1d, b1d) + + +@profile +def _get_maps(a, b): + ''' + Returns m, n such that a[m] == b, b[n] == a + Assumes that all elements appear exactly once in each array + ''' + if a.size == 0: + return numpy.array(()), numpy.array(()) + sa = a.argsort() + sb = b.argsort() + r = numpy.empty_like(sa) + r[sb] = numpy.arange(sa.size) + m = sa[r] + r[sa] = numpy.arange(sa.size) + n = sb[r] + return m, n + + +@profile +def _get_maps_rows(a, b): + ''' + Row-wise _get_map_2way + ''' + dims = numpy.maximum(a.max(0), b.max(0)) + 1 + a1d = numpy.ravel_multi_index(a.T, dims) + b1d = numpy.ravel_multi_index(b.T, dims) + return _get_map(a1d, b1d) def _argsort_rows(a): @@ -161,16 +199,6 @@ def _argsort_rows(a): return numpy.argsort(a1d) -def _get_map_rows(a, b): - ''' - Row-wise _get_map - ''' - dims = numpy.maximum(a.max(0), b.max(0)) + 1 - a1d = numpy.ravel_multi_index(a.T, dims) - b1d = numpy.ravel_multi_index(b.T, dims) - return _get_map(a1d, b1d) - - def _searchsorted_rows(a, b): ''' Row-wise searchsorted diff --git a/tpy_r3d.py b/tpy_r3d.py index 232383f..e1f2ff5 100644 --- a/tpy_r3d.py +++ b/tpy_r3d.py @@ -31,7 +31,7 @@ TOL_FAIL = 1e-4 MIN_VOL = 1e-8 # order of polynomial integration for all tests -POLY_ORDER = 2 +POLY_ORDER = 0 def test_split_tet_manual():