mostly comments, but broke something

This commit is contained in:
jan 2018-03-28 14:58:38 -07:00
commit 3f8b9bef06

View file

@ -1,6 +1,8 @@
from typing import List, Dict
import numpy
from numpy.linalg import norm
from . import poly_t, plane_t
from .util import _get_map, find_components, find_faces
try:
@ -11,7 +13,18 @@ except NameError:
return f
def planes_from_boundary(vertices, faces):
def planes_from_boundary(vertices: numpy.ndarray,
faces: List[numpy.ndarray]
) -> List[plane_t]:
'''
Generate a list of planes (normal, signed distance) from the faces of a boundary-
representation polyhedron.
:param vertices: Nx3 ndarray specifying the polyhedron's vertex positions.
:param faces: List of 1d ndarrays. Each ndarray is a list of vertex indices,
specifying a face.
:return: List of planes, represented by (normal vector, signed distance to origin) tuples.
'''
planes = []
for face in faces:
face_verts = vertices[face]
@ -26,8 +39,17 @@ def planes_from_boundary(vertices, faces):
@profile
def build_polyhedron(in_vertices, faces):
in_vertices = numpy.array(in_vertices, dtype=float)
def build_polyhedron(vertices: numpy.ndarray, faces: numpy.ndarray) -> poly_t:
'''
Generate a 3-verted-connected Schlegel diagram corresponding to the
provided boundary-representation polyhedron.
:param vertices: Nx3 ndarray specifying the polyhedron's vertex positions.
:param faces: List of 1d ndarrays. Each ndarray is a list of vertex indices,
specifying a face.
:return: Schelgel-representation polyhedron
'''
vertices = numpy.array(vertices, dtype=float)
faces = [numpy.array(face, dtype=int) for face in faces]
edges_per_vertex = numpy.bincount(numpy.hstack(faces))
@ -36,10 +58,9 @@ def build_polyhedron(in_vertices, faces):
raise Exception(f'Vertex {edges_per_vertex.argmin()} has < 3 edges')
if edges_per_vertex.max() == 3:
# Don't need to duplicate any vertices to make 3-connected graph
vertices = in_vertices
# Link up the vertices
'''
In this case, we don't need to duplicate any vertices to make a 3-vertex-connected graph
'''
neighbors = numpy.empty_like(vertices, dtype=int).fill(vertices.size)
for face in faces:
# Check if each vertex in this face is already linked to its next or previous face-neighbor
@ -61,7 +82,7 @@ def build_polyhedron(in_vertices, faces):
# If the vertex has already been linked to something (by a different face),
# fill in the third neighbor
# Note that this causes excess writes; it might improve performace to check
# if the third neighbor is already filled
# if the third neighbor is already filled (maybe? TODO)
neighbors[face[has_prev], 2] = next_vert[has_prev]
neighbors[face[has_next], 2] = prev_vert[has_next]
@ -130,33 +151,32 @@ def build_polyhedron(in_vertices, faces):
the links to reflect their removal.
'''
# starting row for each original vertex
vstart = numpy.roll(numpy.cumsum(edges_per_vertex), 1)
vstart[0] = 0
start = numpy.roll(numpy.cumsum(edges_per_vertex), 1)
start[0] = 0
# number of input vertices (v) and duplicates (d)
num_v = in_vertices.shape[0]
num_vertices = vertices.shape[0]
num_duplicates = edges_per_vertex.sum()
# Linkage and aux array
neighbors = numpy.empty((num_duplicates, 3), dtype=int)
util = numpy.zeros(num_duplicates, dtype=int)
'''
Fill columns (1, 2) with original vertex inds (f#_next, f#_prev)
'''
num_filled_rows = numpy.zeros(num_duplicates, dtype=int)
for face in faces:
# assume a face can't use the same vertex twice
v_inds = vstart[face] + util[face]
v_inds = start[face] + num_filled_rows[face]
neighbors[v_inds, 1] = numpy.roll(face, -1)
neighbors[v_inds, 2] = numpy.roll(face, 1)
util[face] += 1
num_filled_rows[face] += 1
'''
Fill columns (0, 2) with new vertex inds (next_row, prev_row)
to complete doubly-linked rings
'''
for v in range(num_v):
v0 = vstart[v]
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])
neighbors[s, 0] = n + v0
@ -177,8 +197,8 @@ def build_polyhedron(in_vertices, faces):
src_mask = numpy.empty(num_duplicates, dtype=bool)
dst_mask = numpy.empty(num_duplicates, dtype=bool)
for v in range(num_v):
s = vstart[v]
for v in range(num_vertices):
s = start[v]
e = s + edges_per_vertex[v]
src_mask.fill(False)
@ -199,20 +219,20 @@ def build_polyhedron(in_vertices, faces):
'''
Remove first two duplicates of each vertex (they aren't needed)
'''
nxt, third, prv = neighbors[vstart, :].T
nxt, third, prv = neighbors[start, :].T
nxt_nxt, nxt_third, _nxt_prv = neighbors[nxt, :].T
# Which vertex to link to
dest_map = numpy.arange(num_duplicates, dtype=int) # default maps to itself
dest_map[vstart] = prv # first maps to previous
dest_map[start] = prv # first maps to previous
dest_map[nxt] = nxt_nxt # second maps to next
# Which backreference to modify
back_map = numpy.full_like(dest_map, 1) # default is 'third'
back_map[vstart] = 0 # if we mapped from a first vertex to its previous, set to 'next'
back_map[start] = 0 # if we mapped from a first vertex to its previous, set to 'next'
back_map[nxt] = 2 # if we mapped from a second vertex to its next, set to 'previous'
# Map 'third' to where it should point to
# Map 'third' to where it should point to. 'dm'~'dest_map'
dm_third = dest_map[third]
dm_nxt_third = dest_map[nxt_third]
@ -223,31 +243,41 @@ def build_polyhedron(in_vertices, faces):
neighbors[dm_nxt_third, back_map[nxt_third]] = nxt_nxt # next-third's backreference points to next-next
# Mark for removal
util.fill(0)
util[vstart] = 1
util[nxt] = 1
keep_map = num_filled_rows # reuse to avoid reallocating
keep_map.fill(1)
keep_map[start] = 0
keep_map[nxt] = 0
neighbors = neighbors[util == 0]
util[util == 0] = numpy.arange(neighbors.shape[0])
neighbors[:] = util[neighbors]
neighbors = neighbors[keep_map != 0]
keep_map[keep_map != 0] = numpy.arange(neighbors.shape[0])
neighbors[:] = keep_map[neighbors]
vertices = numpy.repeat(in_vertices, edges_per_vertex - 2, axis=0)
final_vertices = numpy.repeat(vertices, edges_per_vertex - 2, axis=0)
return {'vertices': vertices, 'neighbors': neighbors}
return {'vertices': final_vertices, 'neighbors': neighbors}
@profile
def boundary_from_poly(poly):
def boundary_from_poly(poly: poly_t) -> Dict:
'''
Generate a boundary represeentation corresponding to the provided
3-vertex-connected Schlegel diagram.
:param poly: Polyhedron to convert, in Schlegel representation.
:return: Dict containing 'vertices' (Nx3 ndarray of vertex locations)
and 'faces' (list of ndarrays specifying the indices of vertices
used for constructing each face).
'''
neighbors = poly['neighbors']
vertices = poly['vertices']
owner_components = find_components(neighbors)
owning_component = find_components(neighbors)
faces = find_faces(neighbors)
num_components = owner_components.max() + 1
num_components = owning_component.max() + 1
components = [[] for _ in range(num_components)]
for face in faces:
component_ind = owner_components[face[0]]
component_ind = owning_component[face[0]]
components[component_ind].append(face)
unique_verts, unique_vertex_map = numpy.unique(vertices, axis=0, return_inverse=True)
@ -255,7 +285,7 @@ def boundary_from_poly(poly):
boundary_map = numpy.empty(vertices.shape[0], dtype=int)
boundaries = []
for i, component in enumerate(components):
mask = owner_components == i
mask = owning_component == i
umask = unique_vertex_map[mask]
used_uverts = numpy.unique(umask)