2012-05-30 21:57:56 -07:00
|
|
|
from __future__ import division
|
|
|
|
|
2012-06-22 21:10:15 -07:00
|
|
|
from itertools import combinations
|
|
|
|
|
2012-05-30 21:57:56 -07:00
|
|
|
from surf.geometry import PolygonMesh, Vertex
|
|
|
|
|
|
|
|
|
|
|
|
def _get_third_point(mesh, eid, fid):
|
|
|
|
evids = set(mesh.edges[eid])
|
|
|
|
fvids = set(mesh.faces[fid])
|
|
|
|
other_vid = fvids - evids
|
|
|
|
assert len(other_vid) == 1
|
|
|
|
other_vid = other_vid.pop()
|
|
|
|
return other_vid
|
|
|
|
|
|
|
|
|
|
|
|
def _make_edge_point(mesh, eid):
|
|
|
|
final_vert = Vertex()
|
|
|
|
|
|
|
|
a = 1 / 2
|
|
|
|
b = 1 / 8
|
|
|
|
c = -1 / 16
|
|
|
|
|
|
|
|
a_verts = [mesh.vertices[v] for v in mesh.edges[eid]]
|
|
|
|
for v in a_verts:
|
|
|
|
final_vert += a * v
|
|
|
|
|
|
|
|
for fid in mesh.faces_for_edge[eid]:
|
|
|
|
other_vid = _get_third_point(mesh, eid, fid)
|
|
|
|
final_vert += b * mesh.vertices[other_vid]
|
|
|
|
|
|
|
|
other_edges = [e for e in mesh.edges_for_face[fid] if e != eid]
|
|
|
|
for other_edge in other_edges:
|
|
|
|
wing_face = [f for f in mesh.faces_for_edge[other_edge]
|
|
|
|
if f != fid]
|
|
|
|
assert len(wing_face) == 1
|
|
|
|
vid = _get_third_point(mesh, other_edge, wing_face[0])
|
|
|
|
final_vert += c * mesh.vertices[vid]
|
|
|
|
|
|
|
|
return final_vert
|
|
|
|
|
|
|
|
|
2012-05-29 16:33:15 -07:00
|
|
|
def refine(mesh):
|
2012-05-30 21:57:56 -07:00
|
|
|
new_verts = list(mesh.vertices)
|
2012-06-22 20:52:02 -07:00
|
|
|
nv_offset = len(new_verts)
|
2012-05-30 21:57:56 -07:00
|
|
|
new_edges = []
|
2012-06-23 10:50:17 -07:00
|
|
|
new_faces = []
|
2012-05-30 21:57:56 -07:00
|
|
|
|
|
|
|
for eid, verts_for_edge in enumerate(mesh.edges):
|
|
|
|
new_vert = _make_edge_point(mesh, eid)
|
|
|
|
new_verts.append(new_vert)
|
|
|
|
|
2012-06-22 20:52:02 -07:00
|
|
|
# populate *some* of the new edges (missing all edges on a face ...)
|
|
|
|
new_vert_id = eid + nv_offset
|
|
|
|
for evid in mesh.edges[eid]:
|
|
|
|
new_edge = [evid, new_vert_id]
|
|
|
|
new_edges.append(new_edge)
|
|
|
|
|
2012-06-22 21:10:15 -07:00
|
|
|
for fid in range(len(mesh.faces)):
|
|
|
|
# join all new edge points on a face:
|
2012-06-23 10:50:17 -07:00
|
|
|
new_center_face = [i + nv_offset for i in mesh.edges_for_face[fid]]
|
|
|
|
inner_edges = list(combinations(new_center_face, 2))
|
|
|
|
new_edges.extend(inner_edges)
|
|
|
|
|
|
|
|
# make new faces: center face
|
|
|
|
new_faces.append(new_center_face)
|
|
|
|
|
|
|
|
# three new faces, one per old vertex:
|
|
|
|
for fvid in mesh.faces[fid]:
|
|
|
|
possible_edges = mesh.edges_for_vert[fvid]
|
|
|
|
face_edges = mesh.edges_for_face[fid]
|
|
|
|
edges_for_fvid = list(set(possible_edges) & set(face_edges))
|
|
|
|
assert len(edges_for_fvid) == 2
|
|
|
|
new_faces.append([
|
|
|
|
edges_for_fvid[0] + nv_offset,
|
|
|
|
fvid,
|
|
|
|
edges_for_fvid[1] + nv_offset
|
|
|
|
])
|
2012-06-22 21:10:15 -07:00
|
|
|
|
2012-05-30 21:57:56 -07:00
|
|
|
return PolygonMesh(new_verts, new_faces, new_edges)
|