def coerce(self, value): """Base coercion mechanism for vector-like field types""" if isinstance(value, (str, unicode)): value = [float(x) for x in value.replace(',', ' ').split()] if isinstance(value, (int, long, float)): value = arrays.zeros(self.dimension, self.targetType) value[:] = float(value) elif isinstance(value, arrays.ArrayType): if arrays.typeCode(value) not in self.acceptedTypes: value = value.astype(self.targetType) value = value.reshape(self.dimension) elif isinstance(value, field.SEQUENCE_TYPES): value = arrays.asarray(map(float, collapse(value)), self.targetType) value.reshape(self.dimension) else: try: value = arrays.asarray(value, self.targetType) except Exception: raise ValueError( """Attempted to set value for an %s field which is not compatible: %s""" % (self.typeName(), repr(value))) else: value.reshape(self.dimension) if value.shape != self.dimension: raise ValueError( """%s value of incorrect shape (is %s, should be %s)""" % ( self.__class__.__name__, value.shape, self.dimension, )) value = arrays.contiguous(value) return value
def coerce( self, value ): """Base coercion mechanism for vector-like field types""" if isinstance( value, (str,unicode)): value = [ float(x) for x in value.replace( ',', ' ').split()] if isinstance(value, (int,long,float)): value = arrays.zeros( self.dimension, self.targetType ) value[:] = float(value) elif isinstance( value, arrays.ArrayType ): if arrays.typeCode(value) not in self.acceptedTypes: value = value.astype(self.targetType) value = value.reshape( self.dimension ) elif isinstance( value, field.SEQUENCE_TYPES): value = arrays.asarray( map(float, collapse(value)), self.targetType ) value.reshape( self.dimension ) else: try: value = arrays.asarray( value, self.targetType ) except Exception: raise ValueError( """Attempted to set value for an %s field which is not compatible: %s"""%( self.typeName(), repr(value) )) else: value.reshape( self.dimension ) if value.shape != self.dimension: raise ValueError( """%s value of incorrect shape (is %s, should be %s)"""%( self.__class__.__name__, value.shape, self.dimension, ) ) value = arrays.contiguous( value ) return value
def OnInit( self ): """Initialisation""" print """Should see a simplistic terrain when you look down (<ctrl+down-arrow>)""" points = Image.open( "heightmap.png" ).convert('L') print points.format ix,iy,data = points.size[0],points.size[1],points.tostring() data = arrays.frombuffer( data, 'B' ).astype( 'f' ) self.data = arrays.zeros( (ix,iy,3), 'f' ) markers = arrays.swapaxes( arrays.indices( (ix,iy), 'f'), 0,2 ) self.data[:,:,0] = markers[:,:,0] self.data[:,:,2] = markers[:,:,1] #self.data[:,:,2] = arrays.arange( 0,iy, dtype='f' ).reshape( (1,iy) ) #self.data[:,:,0] = arrays.arange( 0,ix, dtype='f' ) self.data[:,:,1] = data.reshape( (ix,iy) ) # GL_QUAD_STRIP values (simple rendering) # If iy is not event this goes to heck! assert not iy%2, ("""Need a power-of-2 image for heightmap!""", iy) lefts = arrays.arange( 0, iy*(ix-1), dtype='I' ) # create the right sides of the rectangles lrs = arrays.repeat( lefts, 2 ) lrs[1::2] += iy self.indices = lrs.reshape( (ix-1,iy*2) ) self.shape = IndexedPolygons( polygonSides = GL_QUAD_STRIP, index = self.indices, coord = Coordinate( point = self.data, ), solid= False, normal = Normal( vector= array([0,1,0]*(ix*iy),'f'), ), ) self.sg = sceneGraph( children = [ Transform( translation = (0,-10,0), scale = (1.0, 0.002, 1), children = [ Shape( appearance = Appearance( material = Material( diffuseColor = (.5,1,.5), )), geometry = self.shape, ), ], ), PointLight( location=(10,8,5), ), ], )
def OnInit(self): """Initialisation""" print( """Should see a simplistic terrain when you look down (<ctrl+down-arrow>)""" ) points = Image.open("heightmap.png").convert('L') print(points.format) ix, iy, data = points.size[0], points.size[1], points.tobytes() data = arrays.frombuffer(data, 'B').astype('f') self.data = arrays.zeros((ix, iy, 3), 'f') markers = arrays.swapaxes(arrays.indices((ix, iy), 'f'), 0, 2) self.data[:, :, 0] = markers[:, :, 0] self.data[:, :, 2] = markers[:, :, 1] #self.data[:,:,2] = arrays.arange( 0,iy, dtype='f' ).reshape( (1,iy) ) #self.data[:,:,0] = arrays.arange( 0,ix, dtype='f' ) self.data[:, :, 1] = data.reshape((ix, iy)) # GL_QUAD_STRIP values (simple rendering) # If iy is not event this goes to heck! assert not iy % 2, ("""Need a power-of-2 image for heightmap!""", iy) lefts = arrays.arange(0, iy * (ix - 1), dtype='I') # create the right sides of the rectangles lrs = arrays.repeat(lefts, 2) lrs[1::2] += iy self.indices = lrs.reshape((ix - 1, iy * 2)) self.shape = IndexedPolygons( polygonSides=GL_QUAD_STRIP, index=self.indices, coord=Coordinate(point=self.data, ), solid=False, normal=Normal(vector=array([0, 1, 0] * (ix * iy), 'f'), ), ) self.sg = sceneGraph(children=[ Transform( translation=(0, -10, 0), scale=(1.0, 0.002, 1), children=[ Shape( appearance=Appearance( material=Material(diffuseColor=(.5, 1, .5), )), geometry=self.shape, ), ], ), PointLight(location=(10, 8, 5), ), ], )
def defaultDefault(self): """Default default value for vectors/colours""" return arrays.zeros(self.dimension, self.targetType)
def defaultDefault( self ): """Default default value for vectors/colours""" return arrays.zeros( self.dimension, self.targetType )