2012-03-22 09:09:14 -07:00
|
|
|
from __future__ import division
|
2012-05-10 07:51:52 -07:00
|
|
|
import json
|
2012-03-19 19:38:40 -07:00
|
|
|
|
|
|
|
'''
|
|
|
|
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-05-29 16:33:15 -07:00
|
|
|
__all__ = [
|
|
|
|
'Vertex',
|
|
|
|
'PolygonMesh',
|
|
|
|
]
|
|
|
|
|
2012-05-07 22:21:31 -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-05-09 08:24:26 -07:00
|
|
|
|
2012-05-08 23:32:34 -07:00
|
|
|
def centroid(verts):
|
|
|
|
xs = [v.x for v in verts]
|
|
|
|
ys = [v.y for v in verts]
|
|
|
|
zs = [v.z for v in verts]
|
2012-05-07 22:21:31 -07:00
|
|
|
|
|
|
|
# average each vertex component
|
|
|
|
x = sum(xs) / len(xs)
|
|
|
|
y = sum(ys) / len(ys)
|
|
|
|
z = sum(zs) / len(zs)
|
|
|
|
|
|
|
|
return Vertex(x, y, z)
|
|
|
|
|
2012-05-09 08:24:26 -07:00
|
|
|
|
2012-05-10 07:51:52 -07:00
|
|
|
class Vertex(list):
|
2012-03-19 19:38:40 -07:00
|
|
|
'''
|
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-05-07 22:21:31 -07:00
|
|
|
|
|
|
|
For the sake of our algorithms, we will only worry about the (x, y, z)
|
|
|
|
float positions. Eventually we will also keep track of weights.
|
2012-03-19 19:38:40 -07:00
|
|
|
'''
|
2012-05-10 07:51:52 -07:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
""" The constructor supports the following formats:
|
|
|
|
>>> Vertex([3, 1, 4])
|
|
|
|
V[3, 1, 4]
|
|
|
|
>>> Vertex(2, 7, 2)
|
|
|
|
V[2, 7, 2]
|
|
|
|
>>> Vertex()
|
|
|
|
V[0, 0, 0]
|
|
|
|
"""
|
|
|
|
if len(args) == 0:
|
|
|
|
a = [[0, 0, 0]]
|
|
|
|
a.extend(args)
|
|
|
|
super(Vertex, self).__init__(*a, **kwargs)
|
|
|
|
elif len(args) == 1:
|
|
|
|
if len(args[0]) != 3:
|
|
|
|
raise TypeError("Only support 3D at the moment")
|
|
|
|
super(Vertex, self).__init__(*args, **kwargs)
|
|
|
|
elif len(args) == 3:
|
|
|
|
super(Vertex, self).__init__(args, **kwargs)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def x(self):
|
|
|
|
return self[0]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def y(self):
|
|
|
|
return self[1]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def z(self):
|
|
|
|
return self[2]
|
2012-05-07 22:21:31 -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-05-07 22:21:31 -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-05-07 22:21:31 -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-05-07 22:21:31 -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-05-07 22:21:31 -07:00
|
|
|
|
2012-03-19 19:38:40 -07:00
|
|
|
def __rmul__(self, other):
|
|
|
|
return self.__mul__(other)
|
2012-05-07 22:21:31 -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-05-07 22:21:31 -07:00
|
|
|
|
2012-03-20 06:16:15 -07:00
|
|
|
def __neg__(self):
|
|
|
|
return Vertex(-self.x, -self.y, -self.z)
|
2012-05-07 22:21:31 -07:00
|
|
|
|
2012-04-16 00:11:27 -07:00
|
|
|
def __unicode__(self):
|
2012-05-08 23:32:34 -07:00
|
|
|
return u'V{0}'.format([self.x, self.y, self.z])
|
2012-05-07 22:21:31 -07:00
|
|
|
|
2012-04-16 00:11:27 -07:00
|
|
|
__str__ = __unicode__
|
|
|
|
__repr__ = __unicode__
|
|
|
|
|
2012-03-20 06:16:15 -07:00
|
|
|
|
2012-05-07 22:21:31 -07:00
|
|
|
class PolygonMesh(object):
|
2012-03-19 19:38:40 -07:00
|
|
|
'''
|
2012-05-07 22:21:31 -07:00
|
|
|
A polygon object is a collection of the following lists:
|
|
|
|
|
|
|
|
- a list containing the 3-space vertex information
|
|
|
|
- a list containing the edge indices ([0, 1] means first and second
|
|
|
|
elements of the vertices list)
|
|
|
|
- a list of the faces (indices of the vertices in a given face)
|
|
|
|
|
|
|
|
- connectivity information (eventually will be calculated given the verts,
|
|
|
|
edges, and faces)
|
2012-03-19 19:38:40 -07:00
|
|
|
'''
|
2012-05-07 22:21:31 -07:00
|
|
|
|
2012-05-15 16:35:00 -07:00
|
|
|
def __init__(self, vertices, faces, edges=None, **kwargs):
|
|
|
|
self.vertices = [Vertex(*v) for v in vertices]
|
|
|
|
self.faces = faces
|
|
|
|
self.edges = edges
|
2012-05-07 22:21:31 -07:00
|
|
|
|
|
|
|
# the strategy for the following members involves lazy-instantiating
|
|
|
|
# them if they weren't passing them in:
|
|
|
|
self._faces_for_edge = kwargs.get('faces for edge', None)
|
|
|
|
self._edges_for_face = kwargs.get('edges for face', None)
|
|
|
|
self._edges_for_vert = kwargs.get('edges for vert', None)
|
|
|
|
self._faces_for_vert = kwargs.get('faces for vert', None)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def faces_for_edge(self):
|
|
|
|
"""returns a list of face indices for a given edge index.
|
|
|
|
|
|
|
|
Intended to be used in the following way:
|
|
|
|
|
|
|
|
>>> # vs, es, fs, ffe, eff, efv, ffv from blender or similar
|
|
|
|
>>> mesh = Polygon(vs, es, fs, ffe, eff, efv, ffv)
|
|
|
|
>>> mesh.faces_for_edge[0]
|
|
|
|
[0, 1]
|
|
|
|
>>> [self.face[i] for i in mesh.faces_for_edge[1]]
|
2012-05-08 23:32:34 -07:00
|
|
|
[[...], [...], ...]
|
2012-05-07 22:21:31 -07:00
|
|
|
|
|
|
|
where 0 and 1 are indices into the face list
|
|
|
|
"""
|
|
|
|
if self._faces_for_edge is None:
|
|
|
|
# TODO: eventually support generating this ourselves ...
|
2012-05-29 12:09:51 -07:00
|
|
|
raise NotImplementedError
|
2012-05-07 22:21:31 -07:00
|
|
|
return self._faces_for_edge
|
|
|
|
|
|
|
|
@property
|
|
|
|
def edges_for_face(self):
|
|
|
|
"""returns a list of edge indices for a given face index."""
|
|
|
|
if self._edges_for_face is None:
|
|
|
|
# TODO: eventually support generating this ourselves ...
|
2012-05-29 12:09:51 -07:00
|
|
|
raise NotImplementedError
|
2012-05-07 22:21:31 -07:00
|
|
|
return self._edges_for_face
|
|
|
|
|
|
|
|
@property
|
|
|
|
def edges_for_vert(self):
|
|
|
|
"""returns a list of edge indices for a given vertex index."""
|
|
|
|
if self._edges_for_vert is None:
|
|
|
|
# TODO: eventually support generating this ourselves ...
|
2012-05-29 12:09:51 -07:00
|
|
|
raise NotImplementedError
|
2012-05-07 22:21:31 -07:00
|
|
|
return self._edges_for_vert
|
|
|
|
|
|
|
|
@property
|
|
|
|
def faces_for_vert(self):
|
|
|
|
"""returns a list of face indices for a given vert index."""
|
|
|
|
if self._faces_for_vert is None:
|
|
|
|
# TODO: eventually support generating this ourselves ...
|
2012-05-29 12:09:51 -07:00
|
|
|
raise NotImplementedError
|
2012-05-07 22:21:31 -07:00
|
|
|
return self._faces_for_vert
|
|
|
|
|
2012-03-19 19:38:40 -07:00
|
|
|
def __unicode__(self):
|
2012-05-07 22:21:31 -07:00
|
|
|
# TODO: perhaps also add connectivity here?
|
2012-03-19 19:38:40 -07:00
|
|
|
d = {
|
|
|
|
'vertices': self.vertices,
|
|
|
|
'edges': self.edges,
|
|
|
|
'faces': self.faces,
|
|
|
|
}
|
2012-05-10 07:51:52 -07:00
|
|
|
return json.dumps(d)
|
2012-05-07 22:21:31 -07:00
|
|
|
|
2012-03-19 19:38:40 -07:00
|
|
|
__str__ = __unicode__
|
2012-04-16 00:11:27 -07:00
|
|
|
__repr__ = __unicode__
|
2012-05-08 23:32:34 -07:00
|
|
|
|
|
|
|
def centroid(self, vert_ids):
|
|
|
|
verts = [self.vertices[vid] for vid in vert_ids]
|
|
|
|
return centroid(verts)
|