surfaces/surf/subd/cc.py

81 lines
2.7 KiB
Python
Raw Normal View History

from surf.geometry import Vertex, Polygon
def refine(poly):
'''
For each face, add a face vertex
Set each face vertex to be the centroid of all original vertices for
the respective face.
For each edge, add an edge vertex.
Set each edge vertex to be the average of the two neighbouring face
vertices and its two original endvertices.
For each face vertex, add an edge for every edge of the face, connecting
the face vertex to each edge vertex for the face.
For each original vertex P, take the average F of all n face vertices for
faces touching P, and take the average R of all n edge midvertices for
edges touching P, where each edge midvertex is the average of its two
endvertex vertices. Move each original vertex to the vertex
'''
# each face knows how to subdivide and create a set of subfaces, including
# interior edges and setup their references correctly... <- not completely
# finished...
p = Polygon()
edges = []
vertices = []
faces = []
for face in poly.faces:
for subFace in face.sub_faces:
faces.append(subFace)
for edge in subFace.edges:
edges.append(edge)
for vertex in edge.vertices:
vertices.append(vertex)
newVertices = []
for vertex in poly.vertices:
face_vertices = []
edge_mid_points = []
for edge in vertex.edges:
edge_mid_points.append(edge.mid_point)
for face in edge.faces:
face_vertices.append(face.centroid)
f = sum(list(
set(face_vertices)), Vertex()) / len(list(set(face_vertices)))
r = sum(list(
set(edge_mid_points)), Vertex()) / len(list(set(edge_mid_points)))
p = vertex
n = len(vertex.edges)
v = (f + 2.0 * r + (n - 3.0) * p) / n
newVertices.append(v)
for vertex, newVertex in zip(poly.vertices, newVertices):
vertex.x = newVertex.x
vertex.y = newVertex.y
vertex.z = newVertex.z
# so now what.........
# (F + 2R + (n-3) P) / n
#
# F = average of all face vertices touching P
# R = average of all edge vertices touching P
# P original point
# n = number of edges connecting to P
p.faces = faces
p.vertices = vertices
p.edges = edges
# plotting these in excel seems to show the correct values (at first
# glace...)
# so now what.........
# (F + 2R + (n-3) P) / n
#
# F = average of all face vertices touching P
# R = average of all edge vertices touching P
# P original point
# n = face vertices or edge vertices (should be the same number)
return Polygon(vertices, edges, faces)