diff --git a/adze/util.py b/adze/util.py index 5df0ad5..d7af74f 100644 --- a/adze/util.py +++ b/adze/util.py @@ -29,6 +29,37 @@ def build_empty() -> poly_t: } +def merge_polyhedra(polys: Sequence[poly_t]) -> poly_t: + """ + Merge multiple polyhedra into a single representation. + + Args: + polys: Sequence of polyhedra to merge. + + Returns: + A single polyhedron containing all input polyhedra as separate components. + """ + if not polys: + return build_empty() + + valid_polys = [p for p in polys if p['vertices'].shape[0] > 0] + if not valid_polys: + return build_empty() + + all_vertices = numpy.vstack([p['vertices'] for p in valid_polys]) + + all_neighbors = [] + offset = 0 + for p in valid_polys: + all_neighbors.append(p['neighbors'] + offset) + offset += p['vertices'].shape[0] + + return { + 'vertices': all_vertices, + 'neighbors': numpy.vstack(all_neighbors), + } + + def find_components(neighbors: NDArray[numpy.integer]) -> NDArray[numpy.integer]: """ Given an adjacency list, flood-fill to find and label connected components.