generalize component-finding code, and rewrite boundary representation to use find_components and find_faces

This commit is contained in:
jan 2018-03-10 23:54:19 -08:00
commit cc64f55c90

124
pyr3d.py
View file

@ -380,20 +380,7 @@ def is_good(poly) -> bool:
#// handle multiply-connected polyhedra by testing each component separately
#// Flood-fill starting from each vertex to give each connected region a unique ID.
rcur = 1
regions = numpy.zeros(num_vertices, dtype=int)
stack = []
for v in range(num_vertices):
if regions[v]:
continue
nums = numpy.array([v], dtype=int)
while nums.size > 0:
regions[nums] = rcur
new_nums = numpy.unique(neighbors[nums])
nums = new_nums[regions[new_nums] == 0]
rcur += 1
regions = find_components(neighbors)
# loop over unique pairs of verts
vct = numpy.empty_like(regions, dtype=bool)
@ -763,80 +750,57 @@ def poly_faces_from_verts(vertices, faces):
@profile
def init_brep(poly):
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 into_boundary(poly):
neighbors = poly['neighbors']
vertices = poly['vertices']
owner_components = find_components(neighbors)
faces = find_faces(neighbors)
num_components = owner_components.max() + 1
components = [[] for _ in range(num_components)]
for face in faces:
component_ind = owner_components[face[0]]
components[component_ind].append(face)
unique_verts, unique_vertex_map = numpy.unique(vertices, axis=0, return_inverse=True)
umap = numpy.empty(unique_verts.shape[0], dtype=int)
boundary_map = numpy.empty(vertices.shape[0], dtype=int)
boundaries = []
for i, component in enumerate(components):
mask = owner_components == i
umask = unique_vertex_map[mask]
used_uverts = numpy.unique(umask)
verts_in_component = numpy.empty(unique_verts.shape[0], dtype=bool)
component_map = numpy.empty_like(verts_in_component, dtype=int)
vert_visited = numpy.zeros(vertices.shape[0], dtype=numpy.int8)
edge_visited = numpy.zeros_like(vertices, dtype=bool)
umap[used_uverts] = numpy.arange(used_uverts.size) # map to account for deletion of unique verts not used in this component
boundary_map[mask] = umap[umask] # map from non-unique verts in face to unique, in-use verts
vstart = 0
components = []
while True:
verts_in_component.fill(0)
pedge = 0
faces = []
while True:
vcur = vstart
uvcur = unique_vertex_map[vcur]
face = []
while True:
vert_visited[vcur] += 1
edge_visited[vcur, pedge] = True
verts_in_component[uvcur] = True
vnext = neighbors[vcur, pedge]
uvnext = unique_vertex_map[vnext]
# don't append duplicates
if uvcur != uvnext:
face.append(uvcur)
if vnext == vstart:
break
# Figure out index of back-reference
# and go ccw to next edge (clockwise face traversal)
np = (neighbors[vnext] == vcur).argmax()
pedge = (np + 1) % 3
vcur = vnext
uvcur = uvnext
faces.append(face)
# Continue using a vertex touching a connected, unvisited face
# Any vertex visited only 1 or 2 times must be touching such a face
nv = numpy.where(numpy.logical_and(0 < vert_visited, vert_visited < 3))[0]
if len(nv) == 0:
# No more connected, unvisited faces!
break
vstart = nv[0]
# Pick an unvisited edge; argmin is equivalent to looking for the first 0
pedge = edge_visited[vstart].argmin()
component_map[verts_in_component] = numpy.arange(numpy.count_nonzero(verts_in_component))
component = {
'vertices': unique_verts[verts_in_component],
'faces': [component_map[face] for face in faces],
boundary = {
'vertices': unique_verts[used_uverts],
'faces': [boundary_map[face] for face in component],
}
components.append(component)
boundaries.append(boundary)
# Continue from an unvisited vertex
np = numpy.where(vert_visited == 0)[0]
if len(np) == 0:
# Visited all vertices!
break
vstart = np[0]
return components
return boundaries
def voxelize(poly, ibox, d, poly_order=0):