Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Triplaner-mesh-split created by realazthat -
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):
# 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.
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
# 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)
# 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
You can’t perform that action at this time.