2012-03-22 09:09:14 -07:00
|
|
|
from __future__ import division
|
2012-03-19 19:38:40 -07:00
|
|
|
import pprint
|
|
|
|
|
|
|
|
'''
|
|
|
|
http://en.wikipedia.org/wiki/Polygon_mesh
|
|
|
|
|
2012-03-19 21:56:12 -07:00
|
|
|
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.
|
|
|
|
|
2012-03-19 19:38:40 -07:00
|
|
|
'''
|
|
|
|
|
2012-03-20 06:16:15 -07:00
|
|
|
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)
|
|
|
|
|
2012-04-16 00:11:27 -07:00
|
|
|
|
2012-03-19 19:38:40 -07:00
|
|
|
class Vertex(object):
|
|
|
|
'''
|
2012-03-19 21:56:12 -07:00
|
|
|
A vertex is a position along with other information such as color, normal
|
|
|
|
vector and texture coordinates.
|
2012-03-19 19:38:40 -07:00
|
|
|
'''
|
2012-05-07 20:06:39 -07:00
|
|
|
def __init__(self, x=0.0, y=0.0, z=0.0):
|
2012-04-16 00:11:27 -07:00
|
|
|
'''
|
|
|
|
'''
|
2012-03-19 19:38:40 -07:00
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
self.z = z
|
2012-04-16 00:11:27 -07:00
|
|
|
|
2012-03-19 21:18:02 -07:00
|
|
|
def __eq__(self, other):
|
|
|
|
if(self.x == other.x and self.y == other.y and self.z == other.z):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2012-04-16 00:11:27 -07:00
|
|
|
|
2012-03-19 19:38:40 -07:00
|
|
|
def __add__(self, other):
|
|
|
|
# for now just assume type(other) = Vertex... bad, I know
|
2012-03-19 21:56:12 -07:00
|
|
|
return Vertex(self.x + other.x, self.y + other.y, self.z + other.z)
|
2012-04-16 00:11:27 -07:00
|
|
|
|
2012-03-19 19:38:40 -07:00
|
|
|
def __radd__(self, other):
|
2012-03-20 06:16:15 -07:00
|
|
|
return other + self
|
2012-04-16 00:11:27 -07:00
|
|
|
|
2012-03-19 21:56:12 -07:00
|
|
|
def __mul__(self, other):
|
2012-03-20 06:16:15 -07:00
|
|
|
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)))
|
2012-04-16 00:11:27 -07:00
|
|
|
|
2012-03-19 19:38:40 -07:00
|
|
|
def __rmul__(self, other):
|
|
|
|
return self.__mul__(other)
|
2012-04-16 00:11:27 -07:00
|
|
|
|
2012-03-19 19:38:40 -07:00
|
|
|
def __div__(self, other):
|
|
|
|
# same assumption as __mult__
|
|
|
|
return Vertex(self.x / other, self.y / other, self.z / other)
|
2012-03-22 09:09:14 -07:00
|
|
|
__truediv__ = __div__
|
2012-04-16 00:11:27 -07:00
|
|
|
|
2012-03-20 06:16:15 -07:00
|
|
|
def __neg__(self):
|
|
|
|
return Vertex(-self.x, -self.y, -self.z)
|
2012-04-16 00:11:27 -07:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
d = {
|
|
|
|
'id': self.id,
|
|
|
|
'parent_id': self.parent_id,
|
|
|
|
'coords': [self.x, self.y, self.z]
|
|
|
|
}
|
|
|
|
return pprint.pformat(d)
|
|
|
|
|
|
|
|
__str__ = __unicode__
|
|
|
|
__repr__ = __unicode__
|
|
|
|
|
2012-03-20 06:16:15 -07:00
|
|
|
|
2012-03-19 19:38:40 -07:00
|
|
|
class Edge(object):
|
|
|
|
'''
|
|
|
|
'''
|
2012-04-14 19:35:05 -07:00
|
|
|
|
2012-04-16 00:11:27 -07:00
|
|
|
next_id = 0
|
|
|
|
def __init__(self, polygon, parent_id=None, vs=None, es=None, fs=None):
|
|
|
|
'''
|
|
|
|
'''
|
2012-04-14 19:35:05 -07:00
|
|
|
self.polygon = polygon
|
2012-04-16 00:11:27 -07:00
|
|
|
self.parent_id = parent_id
|
|
|
|
self.vertex_ids = vs or []
|
|
|
|
self.edge_ids = es or []
|
|
|
|
self.face_ids = fs or []
|
|
|
|
self.id = Edge.next_id
|
|
|
|
Edge.next_id += 1
|
2012-04-14 19:35:05 -07:00
|
|
|
|
2012-04-16 00:11:27 -07:00
|
|
|
def neighbor_face_id(self, neighbor_face_id):
|
2012-04-14 19:35:05 -07:00
|
|
|
'''Get neighboring face id
|
|
|
|
'''
|
2012-04-16 00:11:27 -07:00
|
|
|
if neighbor_face_id == self.face_ids[0]:
|
|
|
|
return self.face_ids[1]
|
2012-03-19 19:38:40 -07:00
|
|
|
else:
|
2012-04-16 00:11:27 -07:00
|
|
|
return self.face_ids[0]
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
d = {
|
|
|
|
'id': self.id,
|
|
|
|
'vertex_ids': self.vertex_ids,
|
|
|
|
'edge_ids': self.edge_ids,
|
|
|
|
'face_ids': self.face_ids,
|
|
|
|
}
|
|
|
|
return pprint.pformat(d)
|
|
|
|
|
|
|
|
__str__ = __unicode__
|
|
|
|
__repr__ = __unicode__
|
|
|
|
|
|
|
|
@property
|
|
|
|
def vertices(self):
|
|
|
|
return [self.polygon.vertex(v_id) for v_id in self.vertex_ids]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def faces(self):
|
|
|
|
return [self.polygon.face(f_id) for f_id in self.face_ids]
|
|
|
|
|
|
|
|
def winged_edges_at_vertex(self, index):
|
|
|
|
edge_ids = []
|
|
|
|
for edge in self.edges:
|
|
|
|
if self.vertex_ids[index] in edge.vertex_ids:
|
|
|
|
edge_ids.append(edge.id)
|
|
|
|
return edge_ids
|
2012-03-19 21:56:12 -07:00
|
|
|
|
2012-03-19 19:38:40 -07:00
|
|
|
class Face(object):
|
|
|
|
'''
|
2012-04-16 00:11:27 -07:00
|
|
|
A face is a closed set of edges,
|
|
|
|
in which a triangle face has three edges,
|
2012-03-19 21:56:12 -07:00
|
|
|
and a quad face has four edges.
|
2012-03-19 19:38:40 -07:00
|
|
|
'''
|
2012-04-16 00:11:27 -07:00
|
|
|
|
|
|
|
next_id = 0
|
|
|
|
def __init__(self, polygon, parent_id=None, es=None):
|
|
|
|
'''
|
|
|
|
'''
|
2012-04-14 19:35:05 -07:00
|
|
|
self.polygon = polygon
|
2012-04-16 00:11:27 -07:00
|
|
|
self.parent_id = parent_id
|
|
|
|
self.edge_ids = es or []
|
|
|
|
self.id = Face.next_id
|
|
|
|
Face.next_id += 1
|
2012-04-14 19:35:05 -07:00
|
|
|
|
2012-04-16 00:11:27 -07:00
|
|
|
def __unicode__(self):
|
|
|
|
d = {'id': self.id, 'edge_ids': self.edge_ids}
|
|
|
|
return pprint.pformat(d)
|
|
|
|
|
|
|
|
__str__ = __unicode__
|
|
|
|
__repr__ = __unicode__
|
|
|
|
|
|
|
|
def edge(self, edge_id):
|
|
|
|
return self.polygon.edge(edge_id)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def edges(self):
|
|
|
|
return [self.polygon.edge(e_id) for e_id in self.edge_ids]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def vertices(self):
|
|
|
|
vertices = []
|
|
|
|
for edge_id in self.edge_ids:
|
|
|
|
edge = self.polygon.edge(edge_id)
|
|
|
|
vertices.extend(edge.vertices)
|
|
|
|
|
|
|
|
return list(set(vertices))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def centroid(self):
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
# gather all face vertex coords
|
|
|
|
face_vertices = self.vertices
|
|
|
|
|
|
|
|
xs = [vertex.x for vertex in face_vertices]
|
|
|
|
ys = [vertex.y for vertex in face_vertices]
|
|
|
|
zs = [vertex.z for vertex in face_vertices]
|
|
|
|
|
|
|
|
# average each vertex component
|
|
|
|
x = sum(xs) / len(xs)
|
|
|
|
y = sum(ys) / len(ys)
|
|
|
|
z = sum(zs) / len(zs)
|
|
|
|
|
|
|
|
return [x, y, z]
|
|
|
|
|
|
|
|
|
2012-03-19 19:38:40 -07:00
|
|
|
class Polygon(object):
|
|
|
|
'''
|
|
|
|
'''
|
2012-04-16 00:11:27 -07:00
|
|
|
|
2012-04-14 19:35:05 -07:00
|
|
|
def __init__(self, vs=None, es=None, fs=None):
|
|
|
|
self.vertices = vs or []
|
|
|
|
self.edges = es or []
|
|
|
|
self.faces = fs or []
|
|
|
|
|
2012-04-16 00:11:27 -07:00
|
|
|
def face(self, face_id):
|
|
|
|
for face in self.faces:
|
|
|
|
if face.id == face_id:
|
|
|
|
return face
|
|
|
|
return None
|
|
|
|
|
|
|
|
def edge(self, edge_id):
|
|
|
|
for edge in self.edges:
|
|
|
|
if edge.id == edge_id:
|
|
|
|
return edge
|
|
|
|
return None
|
|
|
|
|
|
|
|
def vertex(self, vertex_id):
|
|
|
|
for vertex in self.vertices:
|
|
|
|
if vertex.id == vertex_id:
|
|
|
|
return vertex
|
|
|
|
return None
|
|
|
|
|
|
|
|
def edge_ids_with_parent(self, parent_edge_id):
|
|
|
|
child_edge_ids = []
|
|
|
|
for edge in self.edges:
|
|
|
|
if edge.parent_id == parent_edge_id:
|
|
|
|
child_edge_ids.append(edge.id)
|
|
|
|
return child_edge_ids
|
|
|
|
|
2012-03-19 19:38:40 -07:00
|
|
|
def __unicode__(self):
|
|
|
|
d = {
|
|
|
|
'vertices': self.vertices,
|
|
|
|
'edges': self.edges,
|
|
|
|
'faces': self.faces,
|
|
|
|
}
|
|
|
|
return pprint.pformat(d)
|
2012-04-14 19:35:05 -07:00
|
|
|
|
2012-03-19 19:38:40 -07:00
|
|
|
__str__ = __unicode__
|
2012-04-16 00:11:27 -07:00
|
|
|
__repr__ = __unicode__
|
|
|
|
|