From: Bruce S. <Bru...@nc...> - 2009-08-01 22:10:22
|
It occurred to me to try a different approach to the issue of deleting a faces object and recreating it. I would guess that performance ought to be significantly better if you simply change the faces.pos array as in the following version. Also note the minor point that it isn't necessary to "try" whether norm(v) works and then making v = vector(0,0,0) if it doesn't work. As the documentation for vector operations in Visual says, if v is vector(0,0,0), as a convenience norm(v) returns vector(0,0,0), just as you wanted. Bruce Sherwood ------------------------- from __future__ import division from visual import * newframe = frame class Object(object): def __init__(self, parent=None, frame=None, pos=(0,0,0), axis=(1,0,0), color=color.green): self.frame = newframe(frame=frame, pos=pos, axis=axis) self.surface = faces(frame=self.frame) self.color = color self.clearSurface() def clearSurface(self): self.surface.pos = [] self.vertexList = [] self.normalList = [] self.colorList = [] def addTri(self, p1, p2, p3): """Add a triangle to the model""" v1 = vector(p1) v2 = vector(p2) v3 = vector(p3) normal = norm( cross(v2-v1, v3-v1) ) ## try is not necessary; norm(vector(0,0,0)) is vector(0,0,0) - see docs ## try: ## normal = norm( cross(v2-v1, v3-v1) ) ## except: ## normal = vector(0,0,0) self.vertexList = self.vertexList + [v1,v2,v3] self.normalList = self.normalList + [normal,normal,normal] self.colorList = self.colorList + [self.color,self.color,self.color] def computeSurface(self): """ Create faces out of vertex, normal and color info""" self.surface.pos = self.vertexList self.surface.normal = self.normalList self.surface.color = self.colorList ## self.surface = faces(frame=self.frame, pos=self.vertexList, ## normal=self.normalList, ## color=self.colorList) if __name__ == '__main__': # origin sphere for reference sphere(radius=0.1) p1 = (-2, -1, 0) p2 = (-1, 1, 0) p3 = (-2, 1, 0) p4 = (3, -1, 0) p5 = (5, -1, 0) p6 = (4, 2, 0) x=Object() x.addTri(p1, p2, p3) x.computeSurface() scene.mouse.getclick() print "new surface" x.clearSurface() x.addTri(p4, p5, p6) x.computeSurface() print x.vertexList |