-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubdivision.py
181 lines (147 loc) · 6.05 KB
/
subdivision.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import taichi as ti
import halfedge as he
from obj_loader import load_obj
class Mesh:
def __init__(self, file_path):
vertex_positions, face_indices = load_obj(file_path)
self.vertices, self.faces, self.edges = he.build_half_edges(vertex_positions, face_indices)
def get_both_vertices(self, e):
return self.edges[e].origin, self.edges[self.edges[e].next].origin
def get_both_faces(self, e):
return self.edges[e].face, self.edges[self.edges[e].twin].face
def all_edges_around_face(self, f):
e = self.faces[f].edge
start_e = e
while True:
e = self.edges[e].next
yield e
if e == start_e or e == -1:
break
def all_edges_around_vertex(self, v):
e = self.vertices[v].edge
start_e = e
while True:
twin = self.edges[e].twin
e = self.edges[twin].next
yield e
if e == start_e or e == -1:
break
def all_unique_edges(self):
unique_edges = []
for e in range(len(self.edges)):
if not self.edges[e].twin in unique_edges:
unique_edges.append(e)
yield e
def compute_face_points(mesh):
face_points = ti.Vector.field(3, dtype=float, shape=len(mesh.faces))
for f in range(len(mesh.faces)):
sum_vertex = ti.Vector([0.0, 0.0, 0.0])
count = 0
for e in mesh.all_edges_around_face(f):
v = mesh.edges[e].origin
sum_vertex += mesh.vertices[v].position
count += 1
face_points[f] = sum_vertex / count
return face_points
def compute_edge_points(mesh, face_points):
edge_points = ti.Vector.field(3, dtype=float, shape=len(mesh.edges) // 2)
for i, e1 in enumerate(mesh.all_unique_edges()):
v1, v2 = mesh.get_both_vertices(e1)
f1, f2 = mesh.get_both_faces(e1)
v1_pos = mesh.vertices[v1].position
v2_pos = mesh.vertices[v2].position
position = (v1_pos + v2_pos + face_points[f1] + face_points[f2]) / 4.0
edge_points[i] = position
return edge_points
def move_vertices(mesh, face_points):
vertices = mesh.vertices
edges = mesh.edges
moved_positions = []
for v in range(len(vertices)):
sum_face_point = ti.Vector([0.0, 0.0, 0.0])
sum_edge_midpoint = ti.Vector([0.0, 0.0, 0.0])
count = 0
for e in mesh.all_edges_around_vertex(v):
# add face point
f = edges[e].face
sum_face_point += face_points[f]
# add edge midpoint
v1, v2 = mesh.get_both_vertices(e)
v1_pos = vertices[v1].position
v2_pos = vertices[v2].position
sum_edge_midpoint += (v1_pos + v2_pos) / 2.0
count += 1
fp = sum_face_point / count
ep = sum_edge_midpoint / count
vp = vertices[v].position
moved_positions.append((fp + 2.0 * ep + (count - 3.0) * vp) / count)
for v in range(len(vertices)):
vertices[v].position = moved_positions[v]
def subdivide(mesh):
face_points = compute_face_points(mesh)
edge_points = compute_edge_points(mesh, face_points)
move_vertices(mesh, face_points)
vertex_positions = []
# add original vertices
for v in range(len(mesh.vertices)):
vertex_positions.append(mesh.vertices[v].position)
# add edge points
edge_to_edge_points = {}
for i, e in enumerate(mesh.all_unique_edges()):
vertex_positions.append(edge_points[i])
twin = mesh.edges[e].twin
edge_to_edge_points[e] = len(vertex_positions) - 1
edge_to_edge_points[twin] = len(vertex_positions) - 1
# add face points
face_to_face_points = {}
for f in range(len(mesh.faces)):
vertex_positions.append(face_points[f])
face_to_face_points[f] = len(vertex_positions) - 1
face_indices = []
for f in range(len(mesh.faces)):
es = [e for e in mesh.all_edges_around_face(f)]
vs = [mesh.edges[e].origin for e in es]
for i in range(len(vs)):
indices = [vs[i],
edge_to_edge_points[es[i]],
face_to_face_points[f],
edge_to_edge_points[es[i-1]]]
face_indices.append(indices)
mesh.vertices, mesh.faces, mesh.edges = he.build_half_edges(vertex_positions, face_indices)
return face_points, edge_points
if __name__ == '__main__':
ti.init(arch=ti.vulkan)
window = ti.ui.Window("Subdivision", (1024, 1024), vsync=True)
gui = window.get_gui()
canvas = window.get_canvas()
canvas.set_background_color((1, 1, 1))
scene = ti.ui.Scene()
camera = ti.ui.Camera()
camera.position(0.0, 0.0, 4.0)
camera.lookat(0.0, 0.0, 0.0)
mesh = Mesh("data/cube.obj")
vertex_field = he.convert_to_vertex_field(mesh.vertices)
index_field = he.convert_to_line_index_field(mesh.edges)
face_points, edge_points = subdivide(mesh)
new_vertex_field = he.convert_to_vertex_field(mesh.vertices)
new_index_field = he.convert_to_line_index_field(mesh.edges)
while window.running:
camera.track_user_inputs(window, movement_speed=0.03, hold_key=ti.ui.RMB)
scene.set_camera(camera)
scene.point_light(pos=(0, 1, 2), color=(1, 1, 1))
scene.ambient_light((0.5, 0.5, 0.5))
if gui.button("Subdivide"):
face_points, edge_points = subdivide(mesh)
new_vertex_field = he.convert_to_vertex_field(mesh.vertices)
new_index_field = he.convert_to_line_index_field(mesh.edges)
# original mesh
scene.particles(vertex_field, radius=0.02)
scene.lines(vertex_field, width=2, indices=index_field)
# new mesh
scene.particles(new_vertex_field, radius=0.02, color=(0.0, 0.0, 1.0))
scene.lines(new_vertex_field, width=2, indices=new_index_field, color=(0.0, 0.0, 1.0))
# face points / edge points
scene.particles(face_points, radius=0.02, color=(1.0, 0.0, 0.0))
scene.particles(edge_points, radius=0.02, color=(0.0, 1.0, 0.0))
canvas.scene(scene)
window.show()