2012-03-19 21:18:02 -07:00
|
|
|
from surf.geometry import Vertex, Polygon
|
2012-03-19 19:38:40 -07:00
|
|
|
|
2012-03-19 21:56:12 -07:00
|
|
|
|
2012-03-19 19:38:40 -07:00
|
|
|
def refine(poly):
|
|
|
|
'''
|
|
|
|
For each face, add a face vertex
|
2012-03-19 21:56:12 -07:00
|
|
|
Set each face vertex to be the centroid of all original vertices for
|
|
|
|
the respective face.
|
2012-03-19 19:38:40 -07:00
|
|
|
For each edge, add an edge vertex.
|
2012-03-19 21:56:12 -07:00
|
|
|
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
|
2012-03-19 19:38:40 -07:00
|
|
|
'''
|
2012-03-19 21:56:12 -07:00
|
|
|
|
|
|
|
# each face knows how to subdivide and create a set of subfaces, including
|
|
|
|
# interior edges and setup their references correctly... <- not completely
|
|
|
|
# finished...
|
2012-03-19 19:38:40 -07:00
|
|
|
p = Polygon()
|
|
|
|
edges = []
|
|
|
|
vertices = []
|
|
|
|
faces = []
|
2012-03-19 21:56:12 -07:00
|
|
|
|
2012-03-19 19:38:40 -07:00
|
|
|
for face in poly.faces:
|
2012-03-19 21:56:12 -07:00
|
|
|
for subFace in face.sub_faces:
|
2012-03-19 19:38:40 -07:00
|
|
|
faces.append(subFace)
|
|
|
|
for edge in subFace.edges:
|
|
|
|
edges.append(edge)
|
|
|
|
for vertex in edge.vertices:
|
|
|
|
vertices.append(vertex)
|
2012-03-19 21:56:12 -07:00
|
|
|
|
2012-03-19 19:38:40 -07:00
|
|
|
newVertices = []
|
|
|
|
for vertex in poly.vertices:
|
2012-03-19 21:56:12 -07:00
|
|
|
face_vertices = []
|
|
|
|
edge_mid_points = []
|
2012-03-19 19:38:40 -07:00
|
|
|
for edge in vertex.edges:
|
2012-03-19 21:56:12 -07:00
|
|
|
edge_mid_points.append(edge.mid_point)
|
2012-03-19 19:38:40 -07:00
|
|
|
for face in edge.faces:
|
2012-03-19 21:56:12 -07:00
|
|
|
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)))
|
2012-03-19 19:38:40 -07:00
|
|
|
p = vertex
|
|
|
|
n = len(vertex.edges)
|
|
|
|
v = (f + 2.0 * r + (n - 3.0) * p) / n
|
|
|
|
newVertices.append(v)
|
2012-03-19 21:56:12 -07:00
|
|
|
|
2012-03-19 19:38:40 -07:00
|
|
|
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
|
2012-03-19 21:56:12 -07:00
|
|
|
#
|
2012-03-19 19:38:40 -07:00
|
|
|
# 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
|
2012-03-19 21:56:12 -07:00
|
|
|
|
|
|
|
# plotting these in excel seems to show the correct values (at first
|
|
|
|
# glace...)
|
|
|
|
|
|
|
|
# so now what.........
|
2012-03-19 19:38:40 -07:00
|
|
|
# (F + 2R + (n-3) P) / n
|
2012-03-19 21:56:12 -07:00
|
|
|
#
|
2012-03-19 19:38:40 -07:00
|
|
|
# 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)
|