Created
May 9, 2025 07:11
-
-
Save zeffii/6f522f87c568113c6926aa1c6be1013a to your computer and use it in GitHub Desktop.
decompose_no_classify_yet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
in verts v | |
in faces s | |
out v_out v | |
out e_out s | |
''' | |
import numpy as np | |
def find_cylinder_endpoints(vertices, faces): | |
# Convert vertices to NumPy for efficiency | |
v = np.array(vertices) | |
# Extract boundary edges (those shared by only one face) | |
edge_count = {} | |
for face in faces: | |
for i in range(len(face)): | |
edge = tuple(sorted((face[i], face[(i + 1) % len(face)]))) | |
edge_count[edge] = edge_count.get(edge, 0) + 1 | |
boundary_edges = [e for e, count in edge_count.items() if count == 1] | |
# Group boundary edges into loops | |
loops = find_edge_loops(boundary_edges) | |
# Output vertex centers (approximate endpoints) | |
v_out = [np.mean(v[loop], axis=0) for loop in loops] | |
e_out = [(i, i + 1) for i in range(0, len(v_out), 2)] | |
return v_out, e_out | |
def find_edge_loops(edges): | |
# Find all edge-connected loops | |
edge_map = {} | |
for e1, e2 in edges: | |
edge_map.setdefault(e1, []).append(e2) | |
edge_map.setdefault(e2, []).append(e1) | |
loops = [] | |
visited = set() | |
for start in edge_map: | |
if start in visited: | |
continue | |
loop = [] | |
current = start | |
while current not in visited: | |
visited.add(current) | |
loop.append(current) | |
next_candidates = [n for n in edge_map[current] if n not in visited] | |
if not next_candidates: | |
break | |
current = next_candidates[0] | |
loops.append(loop) | |
return loops | |
for vlist, flist in zip(verts, faces): | |
pts = find_cylinder_endpoints(vlist, flist) | |
nverts, nedges = pts | |
nnverts = [n.tolist() for n in nverts] | |
v_out.append(nnverts) | |
e_out.append(nedges) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
alvarez