def draw_contourmap(self, gfxwindow, device): self.lock.acquire() try: if self.vmax is not None: aspect_ratio = gfxwindow.settings.aspectratio height = self.vmax - self.vmin width = height / aspect_ratio delta = height / (self.contourmaplevels - 1.) device.comment("Colorbar minimum: %s" % self.vmin) device.comment("Colorbar maximum: %s" % self.vmax) device.set_colormap(self.colormap) for i in range(self.contourmaplevels): low = i * delta high = (i + 1) * delta rect_bdy = [ primitives.Point(0.0, low), primitives.Point(0.0, high), primitives.Point(width, high), primitives.Point(width, low) ] rectangle = primitives.Polygon(rect_bdy) if height > 0.0: device.set_fillColor(low / height) else: device.set_fillColor(0.0) device.fill_polygon(rectangle) finally: self.lock.release()
def draw(self, gfxwindow, device): skel = self.who().resolve(gfxwindow) if skel is not None: device.set_lineColor(self.color) device.set_fillColorAlpha(self.color, color.alpha(self.opacity)) skel.elementselection.begin_reading() try: if config.dimension() == 2: for e in skel.elementselection.retrieve(): device.fill_polygon( primitives.Polygon([x.position() for x in e.nodes])) elif config.dimension() == 3: if len(skel.elementselection.retrieve()): gridPoints = skel.getObject().getPoints() grid = vtk.vtkUnstructuredGrid() numCells = len(skel.elementselection.retrieve()) grid.Allocate(numCells, numCells) grid.SetPoints(gridPoints) for e in skel.elementselection.retrieve(): grid.InsertNextCell(e.getCellType(), e.getPointIds()) device.draw_filled_unstructuredgrid(grid) finally: skel.elementselection.end_reading()
def draw(self, gfxwindow, canvas): # 2D only #canvas.comment("Material Color") if config.dimension() == 2: themesh = self.who().resolve(gfxwindow) polygons = self.polygons(gfxwindow, themesh) # colorcache is a dictionary of colors keyed by Material. It # prevents us from having to call material.fetchProperty for # each element. colorcache = {} for polygon, material in zip(polygons, self.materials(gfxwindow, themesh)): if material is not None: try: # If material has been seen already, retrieve its color. color = colorcache[material] except KeyError: # This material hasn't been seen yet. try: colorprop = material.fetchProperty('Color') color = colorprop.color() except ooferror.ErrNoSuchProperty: color = None colorcache[material] = color if color is not None: canvas.set_fillColor(color) canvas.fill_polygon(primitives.Polygon(polygon))
def draw(self, gfxwindow, device): self.lock.acquire() try: device.comment("Skeleton Energy") skel = self.who().resolve(gfxwindow).getObject() if config.dimension() == 3: grid = skel.skelgrid numCells = grid.GetNumberOfCells() # TODO 3D: handle this array within the canvas # This can overwrite or be overwritten by SkeletonMaterialDisplay energydata = vtk.vtkDoubleArray() energydata.SetNumberOfValues(numCells) grid.GetCellData().SetScalars(energydata) # get polygons and element energy in one pass polyenergy = [(el.perimeter(), el.energyTotal(skel, self.alpha)) for el in skel.element_iterator() if not el.illegal()] # find actual range of data self.vmax = self.vmin = polyenergy[0][1] for (p, e) in polyenergy[1:]: if e > self.vmax: self.vmax = e if e < self.vmin: self.vmin = e # Set plot limits to either the actual data extremes, or # to the passed in values. Store the actual limits in # vmin and vmax. if self.max == automatic.automatic: max = self.vmax else: max = self.max self.vmax = max if self.min == automatic.automatic: min = self.vmin else: min = self.min self.vmin = min if max == min: max += 1.0 self.vmax += 1.0 min -= 1.0 self.vmin -= 1.0 if config.dimension() == 2: device.set_colormap(self.colormap) for polygon, energy in polyenergy: device.set_fillColor((energy - min) / (max - min)) device.fill_polygon(primitives.Polygon(polygon)) elif config.dimension() == 3: lut = self.colormap.getVtkLookupTable(self.contourmaplevels, min, max) for i in xrange(numCells): energydata.SetValue( i, skel.elements[i].energyTotal(skel, self.alpha)) device.draw_unstructuredgrid_with_lookuptable(grid, lut) finally: self.lock.release()
def draw(self, gfxwindow, canvas): # only used in 2D themesh = self.who().resolve(gfxwindow) ##canvas.comment("EdgeDisplay") # if config.dimension() == 2: self.canvaslayer.set_lineColor(self.color) self.canvaslayer.set_lineWidth(self.width) polygons = self.polygons(gfxwindow, themesh) for polygonset in polygons: self.canvaslayer.draw_polygon(primitives.Polygon(polygonset))
def draw(self, gfxwindow, device): themesh = self.who().resolve(gfxwindow) device.comment("EdgeDisplay") device.set_lineColor(self.color) device.set_lineWidth(self.width) if config.dimension() == 2: polygons = self.polygons(gfxwindow, themesh) for polygonset in polygons: device.draw_polygon(primitives.Polygon(polygonset)) elif config.dimension() == 3: polyhedra = self.polyhedra(themesh) device.draw_unstructuredgrid(polyhedra)
def draw_contourmap(self, gfxwindow, device): # If the drawing failed, then contour_max won't have been set # yet. self.lock.acquire() try: if self.contour_max is not None: if config.dimension() == 2: aspect_ratio = gfxwindow.settings.aspectratio height = self.contour_max - self.contour_min width = height / aspect_ratio device.comment("Colorbar minimum: %s" % self.contour_min) device.comment("Colorbar maximum: %s" % self.contour_max) device.set_colormap(self.colormap) for low, high in utils.list_pairs(self.contour_levels): # Subtract "contour_min" off the y coords, so that # the drawn object will include the point (0,0) -- # otherwise, the canvas bounds are wrong. r_low = low - self.contour_min r_high = high - self.contour_min rect_bndy = map(lambda x: primitives.Point(x[0], x[1]), [(0.0, r_low), (0.0, r_high), (width, r_high), (width, r_low)]) rectangle = primitives.Polygon(rect_bndy) # In the collapsed case, height can be zero. This is # not hugely informative, but should be handled without # crashing. if height > 0.0: device.set_fillColor(r_low / height) else: device.set_fillColor(0.0) device.fill_polygon(rectangle) elif config.dimension() == 3: if self.lookuptable: device.draw_scalarbar(self.lookuptable) finally: self.lock.release()
def draw(self, gfxwindow, canvas): # 2D only self.lock.acquire() try: #canvas.comment("Skeleton Energy") skel = self.who().resolve(gfxwindow).getObject() # get polygons and element energy in one pass polyenergy = [(el.perimeter(), el.energyTotal(skel, self.alpha)) for el in skel.element_iterator() if not el.illegal()] # find actual range of data self.vmax = self.vmin = polyenergy[0][1] for (p, e) in polyenergy[1:]: if e > self.vmax: self.vmax = e if e < self.vmin: self.vmin = e # Set plot limits to either the actual data extremes, or # to the passed in values. Store the actual limits in # vmin and vmax. if self.max == automatic.automatic: dmax = self.vmax else: dmax = self.max self.vmax = dmax if self.min == automatic.automatic: dmin = self.vmin else: dmin = self.min self.vmin = dmin if dmax == dmin: dmax += 1.0 self.vmax += 1.0 dmin -= 1.0 self.vmin -= 1.0 canvas.set_colormap(self.colormap) for polygon, energy in polyenergy: canvas.set_fillColor((energy - dmin) / (dmax - dmin)) canvas.fill_polygon(primitives.Polygon(polygon)) finally: self.lock.release()
def draw(self, gfxwindow, device): device.comment("Material Color") if config.dimension() == 2: themesh = self.who().resolve(gfxwindow) polygons = self.polygons(gfxwindow, themesh) # colorcache is a dictionary of colors keyed by Material. It # prevents us from having to call material.fetchProperty for # each element. colorcache = {} for polygon, material in zip(polygons, self.materials(gfxwindow, themesh)): if material is not None: try: # If material has been seen already, retrieve its color. color = colorcache[material] except KeyError: # This material hasn't been seen yet. try: colorprop = material.fetchProperty('Color') color = colorprop.color() except ooferror.ErrNoSuchProperty: color = None colorcache[material] = color if color is not None: device.set_fillColor(color) device.fill_polygon(primitives.Polygon(polygon)) elif config.dimension() == 3: # TODO 3D: clean up this code in general, perhaps the look # up table should be a member of the microstructure... themesh = self.who().resolve(gfxwindow).getObject() grid = themesh.skelgrid numCells = grid.GetNumberOfCells() # TODO 3D: will need to handle the creation and deletion of this array within canvas... materialdata = vtk.vtkIntArray() materialdata.SetNumberOfValues(numCells) grid.GetCellData().SetScalars(materialdata) lut = vtk.vtkLookupTable() colordict = {} for i in xrange(numCells): cat = themesh.elements[i].dominantPixel(themesh.MS) materialdata.SetValue(i, cat) mat = themesh.elements[i].material(themesh) if mat is not None: try: color = colordict[cat] except KeyError: colorprop = mat.fetchProperty('Color') color = colorprop.color() colordict[cat] = color lut.SetNumberOfColors(max(colordict.keys()) + 1) lut.SetTableRange(min(colordict.keys()), max(colordict.keys())) for i in colordict: color = colordict[i] if color is not None: lut.SetTableValue(i, color.getRed(), color.getGreen(), color.getBlue(), 1) else: lut.SetTableValue(i, 0, 0, 0, 0) device.draw_unstructuredgrid_with_lookuptable(grid, lut, mode="cell", scalarbar=False)
def draw(self, gfxwindow, device): self.lock.acquire() meshctxt = mainthread.runBlock(self.who().resolve, (gfxwindow, )) mesh = meshctxt.getObject() meshctxt.restoreCachedData(self.getTime(meshctxt, gfxwindow)) prog = progress.getProgress("Contour plot", progress.DEFINITE) try: self.contour_max = None self.contour_min = None self.contour_levels = [] meshctxt.precompute_all_subproblems() device.comment("FilledContourDisplay") # clevels is a list of contour values. # evalues is a list of lists of function values # for the nodes of each element. clevels, evalues = self.find_levels(mesh, self.what) minval = min(clevels) maxval = max(clevels) valrange = maxval - minval if valrange == 0.0: valrange = 1 factor = 1. / valrange offset = -minval * factor if config.dimension() == 2: device.set_colormap(self.colormap) ecount = 0 # TODO: we might want to use itertools here for element, values in zip(mesh.element_iterator(), evalues): if ( not gfxwindow.settings.hideEmptyElements ) or \ ( element.material() is not None ) : (contours, elmin, elmax) = contour.findContours(mesh, element, self.where, self.what, clevels, self.nbins, 1) # Before drawing anything, fill the element with the # largest contour value below its lowest detected value. vmin = min(values) prevcntour = None for cntour in contours: if cntour.value > elmin: if prevcntour: device.set_fillColor(offset + prevcntour.value * factor) else: device.set_fillColor(0.0) break prevcntour = cntour else: # If all of the contours were below the # element, fill the element with the color # from the top of the colormap. device.set_fillColor(1.0) # Find element perimeter edges = element.perimeter() mcorners = [[0.0]] * element.ncorners() corners = self.where.evaluate(mesh, edges, mcorners) device.fill_polygon( primitives.pontify(primitives.Polygon(corners))) # Now fill contours for cntour in contours: # This is harder than it looks. if len(cntour.loops) == 1: device.set_fillColor(offset + cntour.value * factor) device.fill_polygon(cntour.loops[0]) elif len(cntour.loops) > 1: device.set_fillColor(offset + cntour.value * factor) # Compound Polygon fill device.fill_polygon(cntour.loops) ecount += 1 prog.setFraction((1.0 * ecount) / mesh.nelements()) prog.setMessage("drawing %d/%d elements" % (ecount, mesh.nelements())) # self.draw_subcells(mesh, device) contour.clearCache() elif config.dimension() == 3: # TODO 3D: this should be more seamless when meshes # use vtk objects. Also, will need to update for # quadratic elements. lut = self.colormap.getVtkLookupTable(self.levels, minval, maxval) numnodes = mesh.nnodes() nodes = mesh.node_iterator() points = vtk.vtkPoints() points.Allocate(numnodes, numnodes) data = vtk.vtkDoubleArray() data.SetNumberOfComponents(0) data.SetNumberOfValues(numnodes) while not nodes.end(): node = nodes.node() points.InsertNextPoint(node[0], node[1], node[2]) nodes.next() grid = vtk.vtkUnstructuredGrid() nelements = mesh.nelements() grid.Allocate(nelements, nelements) elements = mesh.element_iterator() # this will reset some values. TODO 3D: think about # plotting discontinuous stuff with vtk - could add # points to points object here for element, values in zip(elements, evalues): elnodes = element.ncorners() for i in xrange(elnodes): data.SetValue(element.getPointIds().GetId(i), values[i]) grid.InsertNextCell(element.GetCellType(), element.getPointIds()) grid.SetPoints(points) grid.GetPointData().SetScalars(data) device.draw_unstructuredgrid_with_lookuptable(grid, lut, mode="point") self.lookuptable = lut self.contour_min = minval self.contour_max = maxval self.contour_levels = clevels finally: self.lock.release() meshctxt.releaseCachedData() prog.finish()
def draw(self, gfxwindow, device): # 2D only self.lock.acquire() meshctxt = mainthread.runBlock(self.who().resolve, (gfxwindow,)) mesh = meshctxt.getObject() meshctxt.restoreCachedData(self.getTime(meshctxt)) prog = progress.getProgress("Contour plot", progress.DEFINITE) try: self.contour_max = None self.contour_min = None self.contour_levels = [] meshctxt.precompute_all_subproblems() # device.comment("FilledContourDisplay") # clevels is a list of contour values. # evalues is a list of lists of function values # for the nodes of each element. clevels, evalues = self.find_levels(mesh, self.what) minval = min(clevels) maxval = max(clevels) nlevels = len(clevels) valrange = maxval - minval if valrange == 0.0: valrange = 1 factor = 1./valrange offset = -minval*factor device.set_colormap(self.colormap) ecount = 0 for element, values in zip(mesh.elements(), evalues): ## TODO MERGE: hideEmptyElements used to be a ## gfxwindow setting, but in 3D it was removed. ## Mesh filters now accomplish the same thing. if ( not gfxwindow.settings.hideEmptyElements ) or \ ( element.material() is not None ) : (contours, elmin, elmax) = contour.findContours( mesh, element, self.where, self.what, clevels, self.nbins, 1) # Before drawing anything, fill the element with the # largest contour value below its lowest detected value. vmin = min(values) prevcntour = None for cntour in contours: if cntour.value > elmin: if prevcntour: device.set_fillColor( offset + prevcntour.value*factor) else: device.set_fillColor(0.0) break prevcntour = cntour else: # If all of the contours were below the # element, fill the element with the color # from the top of the colormap. device.set_fillColor(1.0) # Find element perimeter edges = element.perimeter() mcorners = [[0.0]]*element.ncorners() corners = self.where.evaluate(mesh, edges, mcorners) device.fill_polygon(primitives.pontify( primitives.Polygon(corners))) # Now fill contours for cntour in contours: # This is harder than it looks. if len(cntour.loops) == 1: device.set_fillColor( offset + cntour.value*factor) device.fill_polygon(cntour.loops[0]) elif len(cntour.loops) > 1: device.set_fillColor( offset + cntour.value*factor) # Compound Polygon fill device.fill_polygon(cntour.loops) ecount += 1 prog.setFraction((1.0*ecount)/mesh.nelements()) prog.setMessage("drawing %d/%d elements" % (ecount, mesh.nelements())) # self.draw_subcells(mesh, device) contour.clearCache() self.contour_min = minval self.contour_max = maxval self.contour_levels = clevels finally: self.lock.release() meshctxt.releaseCachedData()
def draw(self, gfxwindow, device): self.contour_max = None self.contour_min = None self.contour_levels = None meshctxt = self.who().resolve(gfxwindow) themesh = meshctxt.mesh() # device.comment("SolidFill") device.set_colormap(self.colormap) polygons = self.polygons(gfxwindow, meshctxt) elements = tuple(themesh.element_iterator()) evaluationpoints = [[el.center()] for el in elements] values = map(float, self.what.evaluate(themesh, elements, evaluationpoints)) max_value = max(values) min_value = min(values) # Perform equivalent of "find_levels" computation -- at the # end of this, contour_levels, contour_max, and contour_min # must be set from self.min, self.max, and self.levels, # provided by the user. if type(self.levels) == TupleType: self.contour_levels = list(self.levels).sort() elif type(self.levels) == ListType: self.contour_levels = self.levels[:].sort() # Respect min and max. if self.min == automatic.automatic: self.contour_min = float(min_value) else: self.contour_min = float(self.min) if self.max == automatic.automatic: self.contour_max = float(max_value) else: self.contour_max = float(self.max) # If levels is an int, then make that many evenly-spaced levels. if type(self.levels) == IntType: if self.levels == 1: self.contour_levels = [ 0.5 * (self.contour_max + self.contour_min) ] else: dz = (self.contour_max - self.contour_min) / (self.levels - 1.0) self.contour_levels = [ self.contour_min + i * dz for i in range(self.levels) ] else: # Levels must have been automatic -- create a level # for each actual data level that is between the # established max and min. data_levels = set(values) self.contour_levels = [ x for x in data_levels if x >= self.contour_min and x <= self.contour_max ] # print "CenterFillDisplay: " # print self.contour_min, self.contour_max, self.contour_levels # HACK: If the values are equal, put the actual value in # the middle of an arbitrary range. # if max_value==min_value: # max_value += 1.0 # min_value -= 1.0 # Actually draw things. for polygon, value in zip(polygons, values): if value is not None: if self.contour_max == self.contour_min: cmap_value = 0.5 # arbitrary. else: # Find the largest contour level not larger than v. last_v = self.contour_levels[0] for v in self.contour_levels: if v > value: break last_v = v cmap_value = (last_v - self.contour_min) / ( self.contour_max - self.contour_min) device.set_fillColor(cmap_value) # device.set_fillColor((value-min_value)/(max_value-min_value)) device.fill_polygon(primitives.Polygon(polygon))