generalize component-finding code, and rewrite boundary representation to use find_components and find_faces
This commit is contained in:
parent
df913ee4ac
commit
cc64f55c90
2 changed files with 47 additions and 83 deletions
124
pyr3d.py
124
pyr3d.py
|
|
@ -380,20 +380,7 @@ def is_good(poly) -> bool:
|
||||||
|
|
||||||
#// handle multiply-connected polyhedra by testing each component separately
|
#// handle multiply-connected polyhedra by testing each component separately
|
||||||
#// Flood-fill starting from each vertex to give each connected region a unique ID.
|
#// Flood-fill starting from each vertex to give each connected region a unique ID.
|
||||||
rcur = 1
|
regions = find_components(neighbors)
|
||||||
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
|
|
||||||
|
|
||||||
# loop over unique pairs of verts
|
# loop over unique pairs of verts
|
||||||
vct = numpy.empty_like(regions, dtype=bool)
|
vct = numpy.empty_like(regions, dtype=bool)
|
||||||
|
|
@ -763,80 +750,57 @@ def poly_faces_from_verts(vertices, faces):
|
||||||
|
|
||||||
|
|
||||||
@profile
|
@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']
|
neighbors = poly['neighbors']
|
||||||
vertices = poly['vertices']
|
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)
|
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)
|
umap[used_uverts] = numpy.arange(used_uverts.size) # map to account for deletion of unique verts not used in this component
|
||||||
component_map = numpy.empty_like(verts_in_component, dtype=int)
|
boundary_map[mask] = umap[umask] # map from non-unique verts in face to unique, in-use verts
|
||||||
vert_visited = numpy.zeros(vertices.shape[0], dtype=numpy.int8)
|
|
||||||
edge_visited = numpy.zeros_like(vertices, dtype=bool)
|
|
||||||
|
|
||||||
vstart = 0
|
boundary = {
|
||||||
components = []
|
'vertices': unique_verts[used_uverts],
|
||||||
while True:
|
'faces': [boundary_map[face] for face in component],
|
||||||
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],
|
|
||||||
}
|
}
|
||||||
components.append(component)
|
boundaries.append(boundary)
|
||||||
|
|
||||||
# Continue from an unvisited vertex
|
return boundaries
|
||||||
np = numpy.where(vert_visited == 0)[0]
|
|
||||||
if len(np) == 0:
|
|
||||||
# Visited all vertices!
|
|
||||||
break
|
|
||||||
vstart = np[0]
|
|
||||||
|
|
||||||
return components
|
|
||||||
|
|
||||||
|
|
||||||
def voxelize(poly, ibox, d, poly_order=0):
|
def voxelize(poly, ibox, d, poly_order=0):
|
||||||
|
|
|
||||||
|
|
@ -498,7 +498,7 @@ def test_poly_pyramid():
|
||||||
pyr3d.is_good(opoly)
|
pyr3d.is_good(opoly)
|
||||||
stack_split(opoly)
|
stack_split(opoly)
|
||||||
|
|
||||||
brep = pyr3d.init_brep(opoly)
|
brep = pyr3d.into_boundary(opoly)
|
||||||
logger.debug(f'Round-trip converted pyramid: {brep}')
|
logger.debug(f'Round-trip converted pyramid: {brep}')
|
||||||
#plot_brep(brep)
|
#plot_brep(brep)
|
||||||
|
|
||||||
|
|
@ -586,7 +586,7 @@ def test_torus_load_and_chop():
|
||||||
# pyr3d.is_good(opoly)
|
# pyr3d.is_good(opoly)
|
||||||
stack_split(opoly)
|
stack_split(opoly)
|
||||||
|
|
||||||
brep = pyr3d.init_brep(opoly)
|
brep = pyr3d.into_boundary(opoly)
|
||||||
logger.debug(f'Round-trip converted torus: {brep}')
|
logger.debug(f'Round-trip converted torus: {brep}')
|
||||||
#plot_brep(brep)
|
#plot_brep(brep)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue