mostly comments, but broke something
This commit is contained in:
parent
fc82bc255c
commit
3f8b9bef06
1 changed files with 366 additions and 127 deletions
107
chop/util.py
107
chop/util.py
|
|
@ -1,3 +1,4 @@
|
|||
from typing import Dict, Tuple, List
|
||||
import numpy
|
||||
|
||||
try:
|
||||
|
|
@ -8,8 +9,33 @@ except NameError:
|
|||
return f
|
||||
|
||||
|
||||
plane_t = Tuple[numpy.ndarray, float]
|
||||
poly_t = Dict[str, numpy.ndarray]
|
||||
|
||||
|
||||
def build_empty() -> poly_t:
|
||||
'''
|
||||
Build an empty Schlegel representation (containing no vertices).
|
||||
|
||||
:return: Empty Schelegel representation.
|
||||
'''
|
||||
return {
|
||||
'vertices': numpy.empty((0, 3), dtype=float),
|
||||
'neighbors': numpy.empty((0, 3), dtype=int),
|
||||
}
|
||||
|
||||
|
||||
@profile
|
||||
def find_components(neighbors):
|
||||
def find_components(neighbors: numpy.ndarray) -> numpy.ndarray:
|
||||
'''
|
||||
Given an adjacency list, flood-fill to find and label connected components.
|
||||
|
||||
:param neighbors: NxM adjacency list. Each of the N vertices is assumed
|
||||
to have the same number of neighbors, which are specified by their
|
||||
indices.
|
||||
:return: 1D ndarray with N entries. Entries are integer labels denoting
|
||||
which connected component the vertex belongs to.
|
||||
'''
|
||||
num_vertices = neighbors.shape[0]
|
||||
current_component = 1
|
||||
component = numpy.zeros(num_vertices, dtype=int)
|
||||
|
|
@ -28,27 +54,39 @@ def find_components(neighbors):
|
|||
|
||||
|
||||
@profile
|
||||
def find_faces(neighbors: numpy.ndarray):
|
||||
def find_faces(neighbors: numpy.ndarray) -> List[numpy.ndarray]:
|
||||
'''
|
||||
Traverse the Schlegel diagram to find chordless cycles
|
||||
(i.e., faces of the polyhedron). Assumes 3-vertex-connected
|
||||
graph with exactly 3 neighbors per vertex. Neighbors are ordered
|
||||
counter-clockwise.
|
||||
|
||||
:param neighbors: Nx3 adjacency list. Neighbors are specified by their
|
||||
indices.
|
||||
:return: List of ndarrays, each of which contains the indices in a given
|
||||
face, starting with the lowest index in the face.
|
||||
'''
|
||||
num_verts = neighbors.shape[0]
|
||||
|
||||
edge_id = numpy.arange(3 * num_verts)
|
||||
nxt_edge = numpy.arange(num_verts * 3) % 3
|
||||
vstart = edge_id // 3
|
||||
vcur = vstart.copy()
|
||||
vnext = neighbors[vcur, nxt_edge]
|
||||
v_start, nxt_edge = numpy.divmod(edge_id, 3) # arange % 3 is faster than tile
|
||||
v_current = v_start.copy()
|
||||
v_next = neighbors[v_current, nxt_edge]
|
||||
|
||||
mask = vstart # dummy for loop init, could use numpy.array([True]) instead
|
||||
mask = v_start # dummy for loop init, could use numpy.array([True]) instead
|
||||
remove = numpy.zeros_like(edge_id, dtype=bool)
|
||||
|
||||
'''
|
||||
Choose a method for storing the face info based on expected
|
||||
runtime vs unnecessary memory allocation
|
||||
'''
|
||||
if num_verts < 1000:
|
||||
# ~3n^2 in memory, but much faster in cases where n isn't prohibitively large
|
||||
faces = numpy.full((3*num_verts, num_verts-1), -1, dtype=int)
|
||||
faces[:, 0] = vcur
|
||||
faces[:, 0] = v_current
|
||||
|
||||
def add_edges(edge_id, vnext, k):
|
||||
faces[edge_id, k] = vnext
|
||||
def add_edges(edge_id, v_nxt, k):
|
||||
faces[edge_id, k] = v_nxt
|
||||
|
||||
def cleaned_faces(remove, k):
|
||||
faces_set = []
|
||||
|
|
@ -56,11 +94,11 @@ def find_faces(neighbors: numpy.ndarray):
|
|||
faces_set.append(row[row >= 0])
|
||||
return faces_set
|
||||
else:
|
||||
# Alternative approach, less memory-intensive buy slower
|
||||
faces = [[v] for v in vcur]
|
||||
# Alternative approach, less memory-intensive but slower
|
||||
faces = [[v] for v in v_current]
|
||||
|
||||
def add_edges(edge_id, vnext, _k):
|
||||
for i, vn in zip(edge_id, vnext):
|
||||
def add_edges(edge_id, v_nxt, _k):
|
||||
for i, vn in zip(edge_id, v_nxt):
|
||||
faces[i].append(vn)
|
||||
|
||||
def cleaned_faces(remove, _k):
|
||||
|
|
@ -69,8 +107,17 @@ def find_faces(neighbors: numpy.ndarray):
|
|||
faces_set.append(faces[i])
|
||||
return faces_set
|
||||
|
||||
'''
|
||||
Starting at all vertices, take a step along all edges.
|
||||
If the vertex index decreases, stop traversal and mark the path for removal.
|
||||
Otherwise, continue stepping along the _next_ edge each time, until
|
||||
a) the vertex index decreases (stop and mark for removal)
|
||||
b) we reach the original vertex (stop, done with this path)
|
||||
'''
|
||||
k = 1
|
||||
v = numpy.vstack((edge_id, vstart, vcur, vnext))
|
||||
#init_mask = v_next > v_current
|
||||
#v = numpy.vstack((edge_id, v_start, v_current, v_next))[:, init_mask]
|
||||
v = numpy.vstack((edge_id, v_start, v_current, v_next))
|
||||
while mask.any():
|
||||
add_edges(v[0], v[3], k)
|
||||
|
||||
|
|
@ -89,8 +136,10 @@ def find_faces(neighbors: numpy.ndarray):
|
|||
|
||||
|
||||
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
|
||||
'''
|
||||
Returns m, n such that a[m] == b, b[n] == a
|
||||
Assumes that all elements appear exactly once in each array
|
||||
'''
|
||||
assert(a.size == b.size)
|
||||
if a.size == 0:
|
||||
return numpy.array(()), numpy.array(())
|
||||
|
|
@ -104,12 +153,18 @@ def _get_map(a, b):
|
|||
|
||||
|
||||
def _argsort_rows(a):
|
||||
'''
|
||||
Row-wise argsort
|
||||
'''
|
||||
dims = a.max(0) + 1
|
||||
a1d = numpy.ravel_multi_index(a.T, dims)
|
||||
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)
|
||||
|
|
@ -117,13 +172,28 @@ def _get_map_rows(a, b):
|
|||
|
||||
|
||||
def _searchsorted_rows(a, b):
|
||||
'''
|
||||
Row-wise searchsorted
|
||||
'''
|
||||
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:
|
||||
def check_poly(poly: poly_t, check_connectivity: bool = True):
|
||||
'''
|
||||
Verify that the polyhedron
|
||||
- has no out-of-bounds neighbors
|
||||
- has no duplicate neighbors
|
||||
- has exactly 3 edges per vertex
|
||||
- is 3-vertex-connected (if check_connectivity is True; slow!)
|
||||
|
||||
:param poly: Polyhedron to check.
|
||||
:param check_connectivity: Whether to check for 3-vertex-connectedness.
|
||||
This is slow! Default True.
|
||||
:raises: Exception if the polyhedron fails a check
|
||||
'''
|
||||
neighbors = poly['neighbors']
|
||||
vertices = poly['vertices']
|
||||
num_vertices = vertices.shape[0]
|
||||
|
|
@ -174,4 +244,3 @@ def check_poly(poly, check_connectivity=True) -> bool:
|
|||
# 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