def hexVolume(hexahedron): tets=((0,1,2,5),(2,5,6,7),(0,2,3,7),(0,2,5,7),(0,4,5,7)) points = [] for i in xrange(8): points.append(hexahedron.GetPoints().GetPoint(i)) ps = primitives.pontify(points) volume=0 for tet in tets: volume += tetVolume(ps[tet[0]],ps[tet[1]],ps[tet[2]],ps[tet[3]]) return volume
def hexVolume(hexahedron): tets = ((0, 1, 2, 5), (2, 5, 6, 7), (0, 2, 3, 7), (0, 2, 5, 7), (0, 4, 5, 7)) points = [] for i in xrange(8): points.append(hexahedron.GetPoints().GetPoint(i)) ps = primitives.pontify(points) volume = 0 for tet in tets: volume += tetVolume(ps[tet[0]], ps[tet[1]], ps[tet[2]], ps[tet[3]]) return volume
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 tetCellVolume(cell): points=[] for i in xrange(4): points.append(cell.GetPoints().GetPoint(i)) ps = primitives.pontify(points) return tetVolume(ps[0],ps[1],ps[2],ps[3])
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()