Created
December 25, 2017 00:24
-
-
Save realazthat/3026d92f67440ced99fdab622a1bc26b to your computer and use it in GitHub Desktop.
Triplaner-mesh-split created by realazthat - https://repl.it/@realazthat/Triplaner-mesh-split
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
from collections import defaultdict | |
import copy | |
vertices = [] | |
triangles = [] | |
# map {(old vertex id, new material_id) => new vertex id} | |
new_vertex_mappings = {} | |
def has_duplicate_vertex_with_this_material(v_idx0, material_id): | |
return (v_idx0, material_id) in new_vertex_mappings | |
def has_conflicting_vertex(triangle): | |
i,j,k = triangle | |
u,v,w = vertices[i], vertices[j], vertices[k] | |
return v.material_id != u.material_id or w.material_id != u.material_id | |
def get_majority_material(triangle): | |
# dictionary that defaults to 0 | |
counts = defaultdict(lambda: 0) | |
i,j,k = triangle | |
u,v,w = vertices[i], vertices[j], vertices[k] | |
if u.material_id == v.material_id: | |
return u.material_id | |
if u.material_id == w.material_id: | |
return u.material_id | |
if v.material_id == w.material_id: | |
return v.material_id | |
# No majority ... :( | |
# We have a few choices of what to do here, but the simplest is just to | |
# find an ordering. | |
return min([u.material_id, v.material_id, w.material_id]) | |
for triangle in triangles: | |
if not has_conflicting_vertex(triangle): | |
continue | |
# majority or decide how to determine material for this special case here: | |
majority_material = get_majority_material(triangle) | |
for t_i in range(3): | |
v_idx0 = triangle[t_i] | |
v = vertices[v_idx0] | |
if v.material_id == majority_material: | |
# all good already. | |
continue | |
if has_duplicate_vertex_with_this_material(v_idx0, majority_material): | |
# Check if we already duplicated this vertex to have this material. | |
# Get the duplicate | |
duplicate_idx = new_vertex_mappings[ (v_idx0, majority_material) ] | |
triangle[t_i] = duplicate_idx | |
else: | |
# Make a copy of the vertex | |
duplicate_v = copy.deepcopy(v) | |
# Except the copy should have the changed material id. | |
duplicate_v.material_id = majority_material | |
# Put the copy in the vertices buffer. | |
duplicate_idx = len(vertices) | |
vertices.append(duplicate_v) | |
# Record the mapping from the old vertex to the new one. | |
new_vertex_mappings[ (v_idx0, majority_material) ] = duplicate_idx | |
triangle[t_i] = duplicate_idx | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment