Changed the Vertex class to inherit from list
- allows for simpler jsonification
This commit is contained in:
parent
a00f1cb59a
commit
544a570629
@ -1,5 +1,5 @@
|
|||||||
from __future__ import division
|
from __future__ import division
|
||||||
import pprint
|
import json
|
||||||
|
|
||||||
'''
|
'''
|
||||||
http://en.wikipedia.org/wiki/Polygon_mesh
|
http://en.wikipedia.org/wiki/Polygon_mesh
|
||||||
@ -39,7 +39,7 @@ def centroid(verts):
|
|||||||
return Vertex(x, y, z)
|
return Vertex(x, y, z)
|
||||||
|
|
||||||
|
|
||||||
class Vertex(object):
|
class Vertex(list):
|
||||||
'''
|
'''
|
||||||
A vertex is a position along with other information such as color, normal
|
A vertex is a position along with other information such as color, normal
|
||||||
vector and texture coordinates.
|
vector and texture coordinates.
|
||||||
@ -47,10 +47,38 @@ class Vertex(object):
|
|||||||
For the sake of our algorithms, we will only worry about the (x, y, z)
|
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.
|
float positions. Eventually we will also keep track of weights.
|
||||||
'''
|
'''
|
||||||
def __init__(self, x=0.0, y=0.0, z=0.0):
|
|
||||||
self.x = x
|
def __init__(self, *args, **kwargs):
|
||||||
self.y = y
|
""" The constructor supports the following formats:
|
||||||
self.z = z
|
>>> 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]
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if(self.x == other.x and self.y == other.y and self.z == other.z):
|
if(self.x == other.x and self.y == other.y and self.z == other.z):
|
||||||
@ -74,16 +102,6 @@ class Vertex(object):
|
|||||||
raise TypeError("{0} has an unexpected type: {1}".format(
|
raise TypeError("{0} has an unexpected type: {1}".format(
|
||||||
other, type(other)))
|
other, type(other)))
|
||||||
|
|
||||||
def __iter__(self):
|
|
||||||
"""Added to be able to conveniently convert a Vertex into a list
|
|
||||||
|
|
||||||
>>> v = Vertex(0, 1, 2)
|
|
||||||
>>> assert list(v) == [0, 1, 2]
|
|
||||||
"""
|
|
||||||
yield self.x
|
|
||||||
yield self.y
|
|
||||||
yield self.z
|
|
||||||
|
|
||||||
def __rmul__(self, other):
|
def __rmul__(self, other):
|
||||||
return self.__mul__(other)
|
return self.__mul__(other)
|
||||||
|
|
||||||
@ -182,7 +200,7 @@ class PolygonMesh(object):
|
|||||||
'edges': self.edges,
|
'edges': self.edges,
|
||||||
'faces': self.faces,
|
'faces': self.faces,
|
||||||
}
|
}
|
||||||
return pprint.pformat(d)
|
return json.dumps(d)
|
||||||
|
|
||||||
__str__ = __unicode__
|
__str__ = __unicode__
|
||||||
__repr__ = __unicode__
|
__repr__ = __unicode__
|
||||||
|
@ -88,10 +88,11 @@ def refine(mesh):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
import sys
|
||||||
import json
|
import json
|
||||||
from surf.subd.cc import refine
|
from surf.subd.cc import refine
|
||||||
|
input_file_name = sys.argv[1]
|
||||||
cube = json.load(open('blender/samples/cube.json', 'r'))
|
cube = json.load(open(input_file_name, 'r'))
|
||||||
p = PolygonMesh(**cube)
|
p = PolygonMesh(**cube)
|
||||||
q = refine(p)
|
q = refine(p)
|
||||||
print json.dumps({'vertices': [list(v) for v in q.vertices]})
|
print q
|
||||||
|
Loading…
Reference in New Issue
Block a user