From 544a57062912025af6cb15a25ae677927439b266 Mon Sep 17 00:00:00 2001 From: "Stephen M. McQuay" Date: Thu, 10 May 2012 08:51:52 -0600 Subject: [PATCH] Changed the Vertex class to inherit from list - allows for simpler jsonification --- surf/geometry.py | 52 ++++++++++++++++++++++++++++++++---------------- surf/subd/cc.py | 7 ++++--- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/surf/geometry.py b/surf/geometry.py index fbd7e83..5eeb07c 100644 --- a/surf/geometry.py +++ b/surf/geometry.py @@ -1,5 +1,5 @@ from __future__ import division -import pprint +import json ''' http://en.wikipedia.org/wiki/Polygon_mesh @@ -39,7 +39,7 @@ def centroid(verts): return Vertex(x, y, z) -class Vertex(object): +class Vertex(list): ''' A vertex is a position along with other information such as color, normal 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) 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 - self.y = y - self.z = z + + 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] def __eq__(self, other): 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( 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): return self.__mul__(other) @@ -182,7 +200,7 @@ class PolygonMesh(object): 'edges': self.edges, 'faces': self.faces, } - return pprint.pformat(d) + return json.dumps(d) __str__ = __unicode__ __repr__ = __unicode__ diff --git a/surf/subd/cc.py b/surf/subd/cc.py index 6df68e3..a3fdaaf 100644 --- a/surf/subd/cc.py +++ b/surf/subd/cc.py @@ -88,10 +88,11 @@ def refine(mesh): if __name__ == '__main__': + import sys import json from surf.subd.cc import refine - - cube = json.load(open('blender/samples/cube.json', 'r')) + input_file_name = sys.argv[1] + cube = json.load(open(input_file_name, 'r')) p = PolygonMesh(**cube) q = refine(p) - print json.dumps({'vertices': [list(v) for v in q.vertices]}) + print q