def __init__(self, vertices, center=None): Surface.__init__(self) c = center or _center_of_point_cloud(vertices) self.center = c num_vertices = len(vertices) if len(vertices) < 2: raise Exception('insufficient vertices') normal = self._compute_normal(c, vertices) v1 = vertices[0] i = Vector(v1.x - c.x, v1.y - c.y, v1.z - c.z) j = i.cross(normal) vertices = vertices[:] vertices.append(vertices[0]) s = Surface() for index in xrange(num_vertices): a = vertices[index] b = vertices[index+1] a1 = Vector(a.x - c.x, a.y - c.y, a.z - c.z) a.tcoord = (a1.dot(i), a1.dot(j)) face = Face(Edge(c, a), Edge(a, b), Edge(b, c)) s.add(face) self.add(s)
def mesh(self): vertices = self.vertices _vertices = tuple([v._obj for v in vertices]) indices = self._obj.face_indices(_vertices) tcoords = [self.tcoords.get(v.id) for v in vertices] no_normal_verts = [v for v in vertices if v.id not in self.vnormals] _vnormals = {} for v in no_normal_verts: fnormals = [f.normal() for f in v._obj.faces()] fnormals = [Vector(*fn).normalize() for fn in fnormals] vnormal = Vector() for fn in fnormals: vnormal = vnormal + fn vnormal = vnormal.normalize() vnormal = (vnormal.x, vnormal.y, vnormal.z) _vnormals[v.id] = vnormal normals = [] for index, v in enumerate(vertices): n = self.vnormals.get(v.id) or _vnormals[v.id] normals.append(n) vertices[index] = v.position return vertices, indices, normals, tcoords
def _compute_normal(self, center, vertices): normals = [] c = center v1 = vertices[0] v1 = Vector(v1.x - c.x, v1.y - c.y, v1.z - c.z) for v in vertices[1:2]: v = Vector(v.x - c.x, v.y - c.y, v.z - c.z) n = v1.cross(v) normals.append(n.normalize()) normal = Vector() for _n in normals: normal = normal + _n return normal.normalize()