# Vector class is based in part on matfunc.py, by Raymond Hettinger, # from http://users.rcn.com/python/download/python.htm from OpenGL.GL import * import operator, math, types class Vector(list): concat = list.__add__ # A substitute for the overridden __add__ method def __getslice__( self, i, j ): return Vector( list.__getslice__(self,i,j) ) def __init__( self, elems ): list.__init__( self, elems ) def __str__( self ): return ' '.join( map(str, self) ) def map( self, op, rhs=None ): if rhs is None: # Unary case return Vector( map(op, self) ) elif isinstance(rhs, types.ListType): # List / List op assert len(self) == len(rhs), 'Vector operation requires lengths to agree' return Vector( map(op, self, rhs) ) else: # List / Scalar op return Vector( [op(e,rhs) for e in self] ) def __mul__( self, rhs ): return self.map( operator.mul, rhs ) def __div__( self, rhs ): return self.map( operator.div, rhs ) def __sub__( self, rhs ): return self.map( operator.sub, rhs ) def __add__( self, rhs ): return self.map( operator.add, rhs ) def __rmul__( self, lhs ): return self.map( operator.mul, lhs ) def __rdiv__( self, lhs ): return self.map( lambda x,y: y / x, lhs ) def __rsub__( self, lhs ): return self.map( lambda x,y: y - x, lhs ) def __radd__( self, lhs ): return self.map( operator.mul, lhs ) def __abs__( self ): return self.map( abs ) def __neg__( self ): return self.map( operator.neg ) def __iadd__( self, other ): self = self + other return self def __imul__( self, other ): self = self * other return self def dot( self, otherVec ): return reduce(operator.add, map(operator.mul, self, otherVec), 0.0) def length( self ): return math.sqrt(self.dot(self)) def lengthSquared( self ): return self.dot(self) def normalize( self ): return self / self.norm() def cross( self, otherVec ): 'Compute a cross product with another vector' assert len(self) == len(otherVec) == 3, 'Cross product only defined for 3-D vectors' u, v = self, otherVec return Vector([ u[1]*v[2]-u[2]*v[1], u[2]*v[0]-u[0]*v[2], u[0]*v[1]-u[1]*v[0] ]) def distance( self, otherVec ): return (self-otherVec).length() def distanceSquared( self, otherVec ): return (self-otherVec).lengthSquared() def drawLine( self ): glBegin(GL_LINES) glVertex3f(0, 0, 0) if len(self) > 2: glVertex3fv(self) elif len(self) == 2: glVertex2fv(self) glEnd() X_Axis = Vector([1, 0, 0]) Y_Axis = Vector([0, 1, 0]) Z_Axis = Vector([0, 0, 1])