disjoint meshes set theory
This file contains 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
verts = [[1.101621389389038, 0.541892945766449, 0.0], [0.8510395884513855, -0.006982157006859779, 0.0], [0.7924670577049255, -0.006982157006859779, 0.0], [0.5302482843399048, 0.541892945766449, 0.0], [0.677261471748352, 0.541892945766449, 0.0], [0.8203957080841064, 0.23273855447769165, 0.0], [0.9561598300933838, 0.541892945766449, 0.0], [0.37742435932159424, 0.541892945766449, 0.0], [0.6179208755493164, 0.0, 0.0], [0.4728471636772156, 0.0, 0.0], [0.42823895812034607, 0.10705973953008652, 0.0], [0.18580295145511627, 0.10705973953008652, 0.0], [0.14546160399913788, 0.0, 0.0], [0.0023273855913430452, 0.0, 0.0], [0.22071373462677002, 0.541892945766449, 0.0], [0.38828548789024353, 0.20170675218105316, 0.0], [0.30100852251052856, 0.4123351573944092, 0.0], [0.2214895337820053, 0.20170675218105316, 0.0]] | |
faces = [[2, 4, 3], [2, 5, 4], [5, 0, 6], [5, 1, 0], [2, 1, 5], [13, 7, 14], [13, 16, 7], [16, 8, 7], [13, 17, 16], [15, 8, 16], [13, 11, 17], [11, 15, 17], [11, 10, 15], [10, 8, 15], [13, 12, 11], [9, 8, 10]] | |
import itertools | |
from collections import defaultdict | |
def process_to_boundaries(vex, pox): | |
edges = defaultdict(int) | |
for poly in pox: | |
for idx in range(len(poly)-1): | |
edge = tuple(sorted([poly[idx], poly[idx+1]])) | |
edges[edge] += 1 | |
edge = tuple(sorted([poly[-1], poly[0]])) | |
edges[edge] += 1 | |
return [k for k, v in edges.items() if v == 1] | |
def process_to_islands(vex, pox): | |
islands = defaultdict(set) | |
for poly in pox: | |
first_idx = list(sorted(poly))[0] | |
islands[f"{first_idx}"].update(poly) | |
interim_islands = list(islands.values()) | |
def merge_islands(islands): | |
found_island_part = False | |
num_islands = len(islands) | |
new_island_dict = defaultdict(set) | |
combo_matrix = [] | |
permut = itertools.permutations(list(range(num_islands)), 2) | |
combos = set([tuple(sorted(e)) for e in permut]) | |
for c in combos: | |
intersect = islands[c[0]] & islands[c[1]] | |
if intersect: | |
found_island_part = True | |
combo_matrix.append(c) | |
if not found_island_part: | |
return islands | |
else: | |
lusax = [set(i) for i in combo_matrix] | |
for c in combo_matrix: | |
for idx, subset in enumerate(lusax): | |
if set(c) & subset: | |
# if subset already taken in the past skip the != | |
lusax[idx] |= set(c) | |
continue | |
new_proto_islands = set(frozenset(i) for i in lusax) | |
new_islands = list() | |
for merge_indices in new_proto_islands: | |
disjoint = [islands[i] for i in merge_indices] | |
disjoint = set(itertools.chain(*disjoint)) | |
new_islands.append(disjoint) | |
return merge_islands(new_islands) | |
return merge_islands(interim_islands) | |
boundaries = process_to_boundaries(verts, faces) | |
print(boundaries) | |
# | |
islands = process_to_islands(verts, faces) | |
print(islands) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment