repl.it
@realazthat/

Triplaner-mesh-split

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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