modulize
This commit is contained in:
parent
cc64f55c90
commit
3d7cf7b503
1 changed files with 934 additions and 736 deletions
151
util.py
Normal file
151
util.py
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
import numpy
|
||||
|
||||
try:
|
||||
profile
|
||||
except NameError:
|
||||
def profile(f):
|
||||
return f
|
||||
|
||||
|
||||
def number_of_moments(order):
|
||||
return ((order + 1) * (order + 2) * (order + 3)) // 6
|
||||
|
||||
|
||||
@profile
|
||||
def find_components(neighbors):
|
||||
num_vertices = neighbors.shape[0]
|
||||
current_component = 1
|
||||
component = numpy.zeros(num_vertices, dtype=int)
|
||||
for v_start in range(num_vertices):
|
||||
if component[v_start] != 0:
|
||||
continue
|
||||
|
||||
verts = numpy.array([v_start], dtype=int)
|
||||
while verts.size > 0:
|
||||
component[verts] = current_component
|
||||
next_verts = numpy.unique(neighbors[verts])
|
||||
verts = next_verts[component[next_verts] == 0]
|
||||
|
||||
current_component += 1
|
||||
return component - 1
|
||||
|
||||
|
||||
@profile
|
||||
def find_faces(neighbors):
|
||||
num_verts = neighbors.shape[0]
|
||||
|
||||
edge_id = numpy.arange(3 * num_verts)
|
||||
nxt_edge = numpy.tile(numpy.arange(3), num_verts)
|
||||
vstart = edge_id // 3
|
||||
vcur = vstart.copy()
|
||||
vnext = neighbors[vcur, nxt_edge]
|
||||
|
||||
faces = [[v] for v in vcur]
|
||||
mask = vstart # dummy for loop init, could use numpy.array([True]) instead
|
||||
remove = numpy.zeros_like(edge_id, dtype=bool)
|
||||
|
||||
while mask.any():
|
||||
for i, vn in zip(edge_id, vnext):
|
||||
faces[i].append(vn)
|
||||
|
||||
prv_edge = (neighbors[vnext, :] == vcur[:, None]).argmax(axis=1)
|
||||
nxt_edge = (prv_edge + 1) % 3
|
||||
vcur, vnext = vnext, neighbors[vnext, nxt_edge]
|
||||
|
||||
remove_mask = vcur < vstart
|
||||
remove[edge_id[remove_mask]] = True
|
||||
|
||||
mask = numpy.logical_and(vnext != vstart, ~remove_mask)
|
||||
edge_id = edge_id[mask]
|
||||
vcur = vcur[mask]
|
||||
vnext = vnext[mask]
|
||||
vstart = vstart[mask]
|
||||
|
||||
faces_set = []
|
||||
for i, do_remove in enumerate(remove):
|
||||
if not do_remove:
|
||||
faces_set.append(faces[i])
|
||||
|
||||
return faces_set
|
||||
|
||||
|
||||
def _get_map(a, b):
|
||||
# Assume all elements appear exactly once in each array
|
||||
# Returns m, n such that a[m] == b, b[n] == a
|
||||
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]
|
||||
|
||||
|
||||
def _argsort_rows(a):
|
||||
dims = a.max(0) + 1
|
||||
a1d = numpy.ravel_multi_index(a.T, dims)
|
||||
return numpy.argsort(a1d)
|
||||
|
||||
|
||||
def _searchsorted_rows(a, b):
|
||||
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 numpy.searchsorted(a1d, b1d)
|
||||
|
||||
|
||||
def check_poly(poly, check_connectivity=True) -> bool:
|
||||
neighbors = poly['neighbors']
|
||||
vertices = poly['vertices']
|
||||
num_vertices = vertices.shape[0]
|
||||
|
||||
out_of_bounds = numpy.logical_or(neighbors >= vertices.shape[0], neighbors < 0)
|
||||
if numpy.any(out_of_bounds):
|
||||
which_vertices = numpy.where(out_of_bounds)[0]
|
||||
raise Exception(f'Vertices {which_vertices} have out-of-bounds neighbors')
|
||||
|
||||
repeated_neighbors = neighbors == numpy.roll(neighbors, 1, axis=1)
|
||||
if numpy.any(repeated_neighbors):
|
||||
which_vertices = numpy.where(repeated_neighbors)[0]
|
||||
raise Exception(f'Vertices {which_vertices} have duplicate neighbors')
|
||||
|
||||
vertex_uses = numpy.bincount(neighbors.ravel())
|
||||
if numpy.any(vertex_uses != 3):
|
||||
which_vertices = numpy.where(vertex_uses != 3)[0]
|
||||
raise Exception(f'Vertices {which_vertices} are referenced {vertex_uses[which_vertices]} times,'
|
||||
' should be exactly 3')
|
||||
|
||||
# check for 3-vertex-connectedness (~n^3 because need bfs for each removed vertex pair)
|
||||
if check_connectivity:
|
||||
components = find_components(neighbors)
|
||||
|
||||
vert_ids = numpy.arange(num_vertices, dtype=int)
|
||||
visited = numpy.empty(num_vertices, dtype=bool)
|
||||
for part_id in numpy.unique(components):
|
||||
part_mask = components == part_id
|
||||
part_verts = vert_ids[part_mask]
|
||||
|
||||
vcs = set(part_verts[:3])
|
||||
for ia in range(part_verts.size):
|
||||
va = part_verts[ia]
|
||||
for vb in part_verts[ia + 1:]:
|
||||
# pick vc != {va, vb}
|
||||
vc = (vcs - set((va, vb))).pop()
|
||||
|
||||
# flood-fill from vc without passing through va, vb
|
||||
visited.fill(False)
|
||||
visited[[va, vb]] = True
|
||||
|
||||
nums = numpy.array([vc], dtype=int)
|
||||
while nums.size > 0:
|
||||
visited[nums] = True
|
||||
new_nums = numpy.unique(neighbors[nums])
|
||||
nums = new_nums[~visited[new_nums]]
|
||||
|
||||
# Make sure we touched all vertices in the component
|
||||
if not visited[components == part_id].all():
|
||||
raise Exception('Vertex graph is not 3-vertex-connected')
|
||||
return True
|
||||
Loading…
Reference in a new issue