def getVertsFromCenter(self, center): self.center = center halfSide = Numeric.multiply( Numeric.array((self.xside, self.yside, self.zside)), 0.5) pt1 = tuple(Numeric.add(center, halfSide)) pt0 = tuple(Numeric.subtract(center, halfSide)) return (pt0, pt1)
def __rmul__(self, other): if TensorModule.isTensor(other): product = other.dot(TensorModule.Tensor(self.array)) if product.rank == 1: return Vector(product.array) else: return product else: return Vector(Numeric.multiply(self.array, other))
def __mul__(self, other): if isVector(other): return Numeric.add.reduce(self.array*other.array) elif TensorModule.isTensor(other): product = TensorModule.Tensor(self.array).dot(other) if product.rank == 1: return Vector(product.array) else: return product elif hasattr(other, "_product_with_vector"): return other._product_with_vector(self) else: return Vector(Numeric.multiply(self.array, other))
def __mul__(self, other): if isVector(other): return Numeric.add.reduce(self.array * other.array) elif TensorModule.isTensor(other): product = TensorModule.Tensor(self.array).dot(other) if product.rank == 1: return Vector(product.array) else: return product elif hasattr(other, "_product_with_vector"): return other._product_with_vector(self) else: return Vector(Numeric.multiply(self.array, other))
def compute(self): spl=[] coordflag = 0 indexflag = 0 scaleflag = 0 coords = [] indices = [] scale=[] llc = [] lli = [] result = [] ind = [] length = 0 for i in range(len(self.data)): d = self.data[i] spl = string.split(d) for k in range(len(spl)): if spl[k] == 'coord' and spl[k+1] == 'Coordinate': coordflag = 1 if spl[k] == 'coordIndex': indexflag = 1 if spl[k] == 'scale': scaleflag = 1 if coordflag == 1: if spl[0]=='}': # we reach the end of 'coords' coordflag=0 continue if spl[0] == 'coord': #this is the first line which we dont continue #want to parse if spl[-1] == ']': #this is the last line where we want lc = spl[-4:-1] #to get rid of the ']' else: lc = spl[-3:] lc[-1] = lc[-1][:-1] llc=[] for n in range(len(lc)): llc.append(float(lc[n])) coords.append(llc) if indexflag == 1: testEnd = string.split(d) # if testEnd[0]=='texCoord': # indexflag=0 # continue if spl[-1] == ']': #this is the last line where we want li = spl[-9:-1] #to get rid of the ']' li[-1] = li[-1]+',' #and add a ',' to the last number indexflag=0 else: li = spl[-8:] ind.extend(li) lli = [] if scaleflag == 1: sc = string.split(d) scale.append( float(sc[1]) ) scale.append( float(sc[2]) ) scale.append( float(sc[3]) ) scaleflag = 0 # make index list. Note that we add -1 as many times as needed # to each index entry in order to make them all the same length lenght=0 # this is the variable that tells us how many -1 we will ad for n in range(len(ind)): if ind[n] != '-1,': lli.append(int(ind[n][:-1])) else: lli.reverse() # index list has to be inverted lli.append(-1) if len(lli) > length: length = len(lli) indices.append(lli) lli = [] # here we go again over the indices and add the -1 if necessary for m in range(len(indices)): if len(indices[m]) < length: for o in range(length-len(indices[m])): indices[m].append(-1) # apply scale to coords if len(scale): coords = Numeric.multiply(coords, scale) result = [coords]+[indices] return result
def __init__(self, name=None, check=1, **kw): self.maxCube = None self.minCube = None if not kw.get('origin') \ and not kw.get('center') \ and not kw.get('cornerPoints') \ and not kw.get('minCube'): kw['origin'] = (0, 0, 0) #set up some defaults: materials = kw.get('materials') #print 'in BOX materials kw=', materials if not materials: kw['materials'] = ((0,0,1),(0,1,0),(0,0,1),(0,1,0),(1,0,0),(1,0,0),) #print 'in BOX after test for materials, kwmaterials=', kw['materials'] vertices = kw.get('vertices') if vertices is not None: assert len(vertices)==8 pt1 = Numeric.array(vertices[2]) pt2 = Numeric.array(vertices[4]) self.center = tuple(Numeric.multiply(Numeric.add(pt1,pt2), 0.5)) self.xside, self.yside, self.zside = \ Numeric.subtract(pt2,pt1) else: #set up center center = kw.get('center') #if not center: center = (0.,0.,0.) self.center = center #set up sides side = kw.get('side') if side: self.xside = side self.yside = side self.zside = side else: xside = kw.get('xside') if not xside: xside = 1.0 self.xside = xside yside = kw.get('yside') if not yside: yside = 1.0 self.yside = yside zside = kw.get('zside') if not zside: zside = 1.0 self.zside = zside #NB faces shouldn't change self.faces=((0,3,2,1), (3,7,6,2), (7,4,5,6), (0,1,5,4), (1,2,6,5), (0,4,7,3)) self.funcs = {} fkeys = ['center', 'origin', 'centerOrigin', 'xside', \ 'yside', 'zside','maxCube', 'minCube'] fs = [self.getVertsFromCenter, self.getVertsFromOrigin, \ self.getVertsFromCenterOrigin, CallBackFunction(self.getVertsFromSide, 'xside'), CallBackFunction(self.getVertsFromSide, 'yside'), CallBackFunction(self.getVertsFromSide, 'zside'), self.setMaxCube, self.setMinCube] for i in range(len(fkeys)): self.funcs[fkeys[i]] = fs[i] self.pickableVertices = 1 kw['frontPolyMode'] = 'line' apply( IndexedPolygons.__init__, (self, name, check), kw ) self.inheritMaterial = 0 #print 'calling self.Set with ', kw.get('materials') #apply(self.Set,(), kw) # register functions to compute normals self.VertexNormalFunction(self.ComputeVertexNormals) self.vertexSet.normals.ComputeMode( viewerConst.AUTO ) self.FaceNormalFunction(self.ComputeFaceNormals) self.faceSet.normals.ComputeMode( viewerConst.AUTO ) self._PrimitiveType() self.GetNormals() self.oldFPM = GL.GL_LINE
def getVertsFromCenter(self, center): self.center = center halfSide = Numeric.multiply(Numeric.array((self.xside, self.yside, self.zside)), 0.5) pt1=tuple(Numeric.add(center,halfSide)) pt0=tuple(Numeric.subtract(center,halfSide)) return (pt0, pt1)
def getVertsFromOrigin(self, origin): #set new center here, also side = Numeric.array((self.xside, self.yside, self.zside)) self.center = tuple(Numeric.add(origin, Numeric.multiply(side, 0.5))) pt1=tuple(Numeric.add(origin, side)) return (origin, pt1)
def halfPt(self, pt): return Numeric.multiply(pt, 0.5)
def Set(self, check=1, redo=1, updateOwnGui=True, **kw): """set data for this object: Set polygon's vertices, faces, normals or materials check=1 : verify that all the keywords present can be handle by this func redo=1 : append self to viewer.objectsNeedingRedo updateOwnGui=True : allow to update owngui at the end this func """ redoFlags = 0 #newKeyList is list of keys specified in call to Set newKeyList = kw.keys() # Setting both center AND origin is a special case # which sets all side lengths centerOrigin = 0 if 'center' in newKeyList and 'origin' in newKeyList: centerOrigin = 1 side= kw.get( 'side') if side: kw['xside'] = side kw['yside'] = side kw['zside'] = side newKeyList.extend(['xside','yside', 'zside']) newKeyList = uniq(newKeyList) apply(self.updateVal, (['xside','yside','zside'],),kw) #these are either 1 or None self.maxCube = kw.get('maxCube') self.minCube = kw.get('minCube') if self.minCube and self.maxCube: self.center = [ (self.minCube[0] + self.maxCube[0]) * .5 , (self.minCube[1] + self.maxCube[1]) * .5 , (self.minCube[2] + self.maxCube[2]) * .5 ] # kl used to implement this precedence: # side < [x,y,z]side < center < origin < cornerPoints < vertices # vertices are dealt with last cornerPoints = None kl = ['xside', 'yside', 'zside','minCube', 'maxCube', 'center', 'origin'] for key in kl: if key in newKeyList: newVal = kw.get(key) if not newVal: continue if key in ['center','origin'] and centerOrigin: self.center = kw.get('center') newVal = kw.get('origin') newKeyList.remove('center') newKeyList.remove('origin') f = self.funcs['centerOrigin'] else: del kw[key] f = self.funcs[key] cornerPoints = apply(f, (newVal,),{}) #if cornerPoints are specified, they override other info newcornerPoints = kw.get('cornerPoints') if newcornerPoints: cornerPoints = newcornerPoints if cornerPoints: ptList = self.getVertsFromCornerPts(cornerPoints) else: ptList = None #vertices overrides everything: set center+sides newVertices = kw.get('vertices') if newVertices is not None: assert len(newVertices)==8 pt1 = Numeric.array(newVertices[2]) pt2 = Numeric.array(newVertices[4]) self.center = tuple(Numeric.multiply(Numeric.add(pt1,pt2), 0.5)) self.xside, self.yside, self.zside = \ Numeric.subtract(pt2,pt1) redoFlags |= self._redoFlags['redoDisplayListFlag'] elif ptList: assert len(ptList)==8 kw['vertices'] = ptList redoFlags |= self._redoFlags['redoDisplayListFlag'] if kw.get('faces') is None: kw['faces'] = self.faces redoFlags |= apply( IndexedPolygons.Set, (self, check, 0), kw ) return self.redoNow(redo, updateOwnGui, redoFlags)
def __init__(self, name=None, check=1, **kw): self.maxCube = None self.minCube = None if not kw.get('origin') \ and not kw.get('center') \ and not kw.get('cornerPoints') \ and not kw.get('minCube'): kw['origin'] = (0, 0, 0) #set up some defaults: materials = kw.get('materials') #print 'in BOX materials kw=', materials if not materials: kw['materials'] = ( (0, 0, 1), (0, 1, 0), (0, 0, 1), (0, 1, 0), (1, 0, 0), (1, 0, 0), ) #print 'in BOX after test for materials, kwmaterials=', kw['materials'] vertices = kw.get('vertices') if vertices is not None: assert len(vertices) == 8 pt1 = Numeric.array(vertices[2]) pt2 = Numeric.array(vertices[4]) self.center = tuple(Numeric.multiply(Numeric.add(pt1, pt2), 0.5)) self.xside, self.yside, self.zside = \ Numeric.subtract(pt2,pt1) else: #set up center center = kw.get('center') #if not center: center = (0.,0.,0.) self.center = center #set up sides side = kw.get('side') if side: self.xside = side self.yside = side self.zside = side else: xside = kw.get('xside') if not xside: xside = 1.0 self.xside = xside yside = kw.get('yside') if not yside: yside = 1.0 self.yside = yside zside = kw.get('zside') if not zside: zside = 1.0 self.zside = zside #NB faces shouldn't change self.faces = ((0, 3, 2, 1), (3, 7, 6, 2), (7, 4, 5, 6), (0, 1, 5, 4), (1, 2, 6, 5), (0, 4, 7, 3)) self.funcs = {} fkeys = ['center', 'origin', 'centerOrigin', 'xside', \ 'yside', 'zside','maxCube', 'minCube'] fs = [self.getVertsFromCenter, self.getVertsFromOrigin, \ self.getVertsFromCenterOrigin, CallBackFunction(self.getVertsFromSide, 'xside'), CallBackFunction(self.getVertsFromSide, 'yside'), CallBackFunction(self.getVertsFromSide, 'zside'), self.setMaxCube, self.setMinCube] for i in range(len(fkeys)): self.funcs[fkeys[i]] = fs[i] self.pickableVertices = 1 kw['frontPolyMode'] = 'line' apply(IndexedPolygons.__init__, (self, name, check), kw) self.inheritMaterial = 0 #print 'calling self.Set with ', kw.get('materials') #apply(self.Set,(), kw) # register functions to compute normals self.VertexNormalFunction(self.ComputeVertexNormals) self.vertexSet.normals.ComputeMode(viewerConst.AUTO) self.FaceNormalFunction(self.ComputeFaceNormals) self.faceSet.normals.ComputeMode(viewerConst.AUTO) self._PrimitiveType() self.GetNormals() self.oldFPM = GL.GL_LINE
def getVertsFromOrigin(self, origin): #set new center here, also side = Numeric.array((self.xside, self.yside, self.zside)) self.center = tuple(Numeric.add(origin, Numeric.multiply(side, 0.5))) pt1 = tuple(Numeric.add(origin, side)) return (origin, pt1)
def Set(self, check=1, redo=1, updateOwnGui=True, **kw): """set data for this object: Set polygon's vertices, faces, normals or materials check=1 : verify that all the keywords present can be handle by this func redo=1 : append self to viewer.objectsNeedingRedo updateOwnGui=True : allow to update owngui at the end this func """ redoFlags = 0 #newKeyList is list of keys specified in call to Set newKeyList = kw.keys() # Setting both center AND origin is a special case # which sets all side lengths centerOrigin = 0 if 'center' in newKeyList and 'origin' in newKeyList: centerOrigin = 1 side = kw.get('side') if side: kw['xside'] = side kw['yside'] = side kw['zside'] = side newKeyList.extend(['xside', 'yside', 'zside']) newKeyList = uniq(newKeyList) apply(self.updateVal, (['xside', 'yside', 'zside'], ), kw) #these are either 1 or None self.maxCube = kw.get('maxCube') self.minCube = kw.get('minCube') if self.minCube and self.maxCube: self.center = [(self.minCube[0] + self.maxCube[0]) * .5, (self.minCube[1] + self.maxCube[1]) * .5, (self.minCube[2] + self.maxCube[2]) * .5] # kl used to implement this precedence: # side < [x,y,z]side < center < origin < cornerPoints < vertices # vertices are dealt with last cornerPoints = None kl = [ 'xside', 'yside', 'zside', 'minCube', 'maxCube', 'center', 'origin' ] for key in kl: if key in newKeyList: newVal = kw.get(key) if not newVal: continue if key in ['center', 'origin'] and centerOrigin: self.center = kw.get('center') newVal = kw.get('origin') newKeyList.remove('center') newKeyList.remove('origin') f = self.funcs['centerOrigin'] else: del kw[key] f = self.funcs[key] cornerPoints = apply(f, (newVal, ), {}) #if cornerPoints are specified, they override other info newcornerPoints = kw.get('cornerPoints') if newcornerPoints: cornerPoints = newcornerPoints if cornerPoints: ptList = self.getVertsFromCornerPts(cornerPoints) else: ptList = None #vertices overrides everything: set center+sides newVertices = kw.get('vertices') if newVertices is not None: assert len(newVertices) == 8 pt1 = Numeric.array(newVertices[2]) pt2 = Numeric.array(newVertices[4]) self.center = tuple(Numeric.multiply(Numeric.add(pt1, pt2), 0.5)) self.xside, self.yside, self.zside = \ Numeric.subtract(pt2,pt1) redoFlags |= self._redoFlags['redoDisplayListFlag'] elif ptList: assert len(ptList) == 8 kw['vertices'] = ptList redoFlags |= self._redoFlags['redoDisplayListFlag'] if kw.get('faces') is None: kw['faces'] = self.faces redoFlags |= apply(IndexedPolygons.Set, (self, check, 0), kw) return self.redoNow(redo, updateOwnGui, redoFlags)
def compute(self): spl = [] coordflag = 0 indexflag = 0 scaleflag = 0 coords = [] indices = [] scale = [] llc = [] lli = [] result = [] ind = [] length = 0 for i in range(len(self.data)): d = self.data[i] spl = string.split(d) for k in range(len(spl)): if spl[k] == 'coord' and spl[k + 1] == 'Coordinate': coordflag = 1 if spl[k] == 'coordIndex': indexflag = 1 if spl[k] == 'scale': scaleflag = 1 if coordflag == 1: if spl[0] == '}': # we reach the end of 'coords' coordflag = 0 continue if spl[0] == 'coord': #this is the first line which we dont continue #want to parse if spl[-1] == ']': #this is the last line where we want lc = spl[-4:-1] #to get rid of the ']' else: lc = spl[-3:] lc[-1] = lc[-1][:-1] llc = [] for n in range(len(lc)): llc.append(float(lc[n])) coords.append(llc) if indexflag == 1: testEnd = string.split(d) # if testEnd[0]=='texCoord': # indexflag=0 # continue if spl[-1] == ']': #this is the last line where we want li = spl[-9:-1] #to get rid of the ']' li[-1] = li[-1] + ',' #and add a ',' to the last number indexflag = 0 else: li = spl[-8:] ind.extend(li) lli = [] if scaleflag == 1: sc = string.split(d) scale.append(float(sc[1])) scale.append(float(sc[2])) scale.append(float(sc[3])) scaleflag = 0 # make index list. Note that we add -1 as many times as needed # to each index entry in order to make them all the same length lenght = 0 # this is the variable that tells us how many -1 we will ad for n in range(len(ind)): if ind[n] != '-1,': lli.append(int(ind[n][:-1])) else: lli.reverse() # index list has to be inverted lli.append(-1) if len(lli) > length: length = len(lli) indices.append(lli) lli = [] # here we go again over the indices and add the -1 if necessary for m in range(len(indices)): if len(indices[m]) < length: for o in range(length - len(indices[m])): indices[m].append(-1) # apply scale to coords if len(scale): coords = Numeric.multiply(coords, scale) result = [coords] + [indices] return result