from collections import defaultdict from surf.geometry import PolygonMesh, centroid def refine(mesh): new_vertices = list(mesh.vertices) f_vert_offset = len(new_vertices) edge_vids_for = defaultdict(list) face_ids_for_v = defaultdict(list) # TODO: not completely populated yet: new_edges = list(mesh.edges) # TODO: must be populated: new_faces = [] # For each face, add a face point for cur_face_id, cur_face in enumerate(mesh.faces): # Set each face point to be the centroid of all original points for the # respective face. # here we have already created a list that is a copy of the original # verts. we append the new face points to that same list, and simply # keep track of offsets into the new vert list ... see e_vert_offset # below new_face_point = mesh.centroid(cur_face) new_vertices.append(new_face_point) for i in cur_face: face_ids_for_v[i].append(cur_face_id) e_vert_offset = len(new_vertices) # For each edge, add an edge point. for cur_edge_id, cur_edge in enumerate(mesh.edges): # make mapping from edge -> new_face_vert for later face_ids_for_edge = mesh.faces_for_edge[cur_edge_id] for fid in face_ids_for_edge: edge_vids_for[fid].append(cur_edge_id) tmp_verts = [] # Set each edge point to be the average of the two neighbouring, (very # recently calculated) face points ... tmp_verts.extend([new_vertices[f + f_vert_offset] for f in face_ids_for_edge]) # ... and its two original endpoints. tmp_verts.extend([mesh.vertices[vid] for vid in cur_edge]) # centroid == average new_vertices.append(centroid(tmp_verts)) # For each face point, add an edge for every edge of the face, connecting # the face point to each edge point for the face. for trunc_vid in xrange(len(new_vertices[f_vert_offset:e_vert_offset])): overall_vid = f_vert_offset + trunc_vid for edge_vid in edge_vids_for[trunc_vid]: new_edges.append([edge_vid, overall_vid]) # v_vert_offset = len(new_vertices) # For each original point P for new_vid in xrange(f_vert_offset): # take the average F of all n face points for faces touching P ... new_face_ids = face_ids_for_v[new_vid] F_verts = [] for fid in new_face_ids: F_verts.append(new_vertices[f_vert_offset + fid]) F = centroid(F_verts) # and take the average R of all n edge midpoints for edges touching P # wiki is wrong ... it should be the edge points, not midpoints ... edges = [mesh.edges[eid] for eid in mesh.edges_for_vert[new_vid]] e_verts = [] for edge in edges: e_verts.extend([mesh.vertices[vid] for vid in edge]) R = centroid(e_verts) # where each edge midpoint is the average of its two endpoint vertices. # Move each original point to the point (or add it to new_verts) new_vertex_point = (F + 2 * R + (len(edges) - 3) * new_vertices[new_vid]) / len(edges) new_vertices.append(new_vertex_point) return PolygonMesh(vertices=new_vertices, edges=new_edges, faces=new_faces) if __name__ == '__main__': import json from surf.subd.cc import refine cube = json.load(open('blender/samples/cube.json', 'r')) p = PolygonMesh(**cube) q = refine(p) print q