from __future__ import division import pprint ''' http://en.wikipedia.org/wiki/Polygon_mesh Polygon meshes may be represented in a variety of ways, using different methods to store the vertex, edge and face data. These include: - Face-vertex - Winged-edge - Half-edge - Quad-edge - Corner-tables - Vertex-vertex - Face-vertex We have chosen to use a winged-edge style mesh for our purpopses. ''' def cross(a, b): i = a.y * b.z - a.z * b.y j = a.z * b.x - a.x * b.z k = a.x * b.y - a.y * b.x return Vertex(i, j, k) class Vertex(object): ''' A vertex is a position along with other information such as color, normal vector and texture coordinates. ''' def __init__(self, polygon, x=0, y=0, z=0): self.polygon = polygon self.x = x self.y = y self.z = z self.edges = [] def __eq__(self, other): if(self.x == other.x and self.y == other.y and self.z == other.z): return True else: return False def __repr__(self): return "[%.2f, %.2f, %.2f]" % (self.x, self.y, self.z) def __add__(self, other): # for now just assume type(other) = Vertex... bad, I know return Vertex(self.x + other.x, self.y + other.y, self.z + other.z) def __radd__(self, other): return other + self # return self.__add__(other) def __mul__(self, other): if isinstance(other, Vertex): return cross(self, other) elif isinstance(other, (float, int)): return Vertex(self.x * other, self.y * other, self.z * other) else: raise TypeError("{0} has an unexpected type: {1}".format( other, type(other))) def __rmul__(self, other): return self.__mul__(other) def __div__(self, other): # same assumption as __mult__ return Vertex(self.x / other, self.y / other, self.z / other) __truediv__ = __div__ def __neg__(self): return Vertex(-self.x, -self.y, -self.z) class Edge(object): ''' ''' def __init__(self, polygon): self.polygon = polygon self.vertices = [] self.faces = [] self.edges = [] def neighborFace(self, neighborFace): '''Get neighboring face id ''' if neighborFace == self.faces[0]: return self.faces[1] else: return self.faces[0] class Face(object): ''' A face is a closed set of edges, in which a triangle face has three edges, and a quad face has four edges. ''' def __init__(self, polygon): self.polygon = polygon self.edges = [] class Polygon(object): ''' ''' def __init__(self, vs=None, es=None, fs=None): self.vertices = vs or [] self.edges = es or [] self.faces = fs or [] def __unicode__(self): d = { 'vertices': self.vertices, 'edges': self.edges, 'faces': self.faces, } return pprint.pformat(d) __str__ = __unicode__ __repr__ = __unicode__