def from_grid(dgrid): nodes = dgrid['nodes'] elems = dgrid['elements'] points = vtk.vtkPoints() points.SetNumberOfPoints(len(nodes)) for n in nodes: points.SetPoint(n[0] - 1, n[1], n[2], n[3]) grid = vtk.vtkUnstructuredGrid() grid.SetPoints(points) for g in elems: dim = g['dimension'] etype = g['type'] eshape = g['numnodes'] for c in g['connectivity']: if dim == 3 and etype == 'solid': if eshape == 8: e = vtk.vtkHexahedron() elif eshape == 6: e = vtk.vtkWedge() elif dim == 2 and etype == 'solid': if eshape == 3: e = vtk.vtkTriangle() elif eshape == 4: e = vtk.vtkQuad() ids = e.GetPointIds() i = 0 for nid in c[1:]: ids.SetId(i, nid - 1) i += 1 grid.InsertNextCell(e.GetCellType(), ids) return grid
def make_cell_array(index_list): cells = vtk.vtkCellArray() n_nodes = len(index_list[0]) for node_list in index_list: if n_nodes == 8: cell = vtk.vtkHexahedron() elif n_nodes == 4: cell = vtk.vtkTetra() elif n_nodes == 5: cell = vtk.vtkPyramid() elif n_nodes == 6: cell = vtk.vtkWedge() else: print(f'{n_nodes} nodes are not supported at this time') for ind, value in enumerate(node_list): cell.GetPointIds().SetId(ind, value) cells.InsertNextCell(cell) if n_nodes == 8: cell_type = vtk.VTK_HEXAHEDRON elif n_nodes == 4: cell_type = vtk.VTK_TETRA elif n_nodes == 5: cell_type = vtk.VTK_PYRAMID elif n_nodes == 6: cell_type = vtk.VTK_WEDGE return cells, cell_type
def create_from_points_and_elements(cls, points, elements): """ Converts a list of points and a list of elements to a mesh checker :param points: List of Point objects :param elements: List of node ids for each element (correspond to the node id of the Point object) :return: Mesh checker :rtype: VTKMeshChecker """ # create VTK points vtk_points = vtk.vtkPoints() for pt in points: vtk_points.InsertNextPoint(pt.xyz) # create the VTK mesh and add the points vtk_mesh = vtk.vtkUnstructuredGrid() vtk_mesh.SetPoints(vtk_points) # create the VTK elements for ele in elements: # NOTE: only hexahedra have metrics in VTK vtk_ele = vtk.vtkHexahedron() if len(ele) == 8 else ( vtk.vtkWedge() if len(ele) == 6 else None) for vtk_ele_idx, nid in enumerate(ele): # node id is 1-based and the vtk index is 0-based vtk_ele.GetPointIds().SetId(vtk_ele_idx, nid - 1) vtk_mesh.InsertNextCell(vtk_ele.GetCellType(), vtk_ele.GetPointIds()) return VTKMeshChecker(vtk_mesh)
def MakeWedge(): ''' A wedge consists of two triangular ends and three rectangular faces. ''' numberOfVertices = 6 points = vtk.vtkPoints() points.InsertNextPoint(0, 1, 0) points.InsertNextPoint(0, 0, 0) points.InsertNextPoint(0, .5, .5) points.InsertNextPoint(1, 1, 0) points.InsertNextPoint(1, 0.0, 0.0) points.InsertNextPoint(1, .5, .5) wedge = vtk.vtkWedge() for i in range(0, numberOfVertices): wedge.GetPointIds().SetId(i, i) ug = vtk.vtkUnstructuredGrid() ug.SetPoints(points) ug.InsertNextCell(wedge.GetCellType(), wedge.GetPointIds()) return ug
def main(): filenames = list() uGrids = list() uGrids.append(MakeUnstructuredGrid(vtk.vtkVertex())) filenames.append('Vertex.vtu') uGrids.append(MakePolyVertex()) filenames.append('PolyVertex.vtu') uGrids.append(MakeUnstructuredGrid(vtk.vtkLine())) filenames.append('Line.vtu') uGrids.append(MakePolyLine()) filenames.append('PolyLine.vtu') uGrids.append(MakeUnstructuredGrid(vtk.vtkTriangle())) filenames.append('Triangle.vtu') uGrids.append(MakeTriangleStrip()) filenames.append('TriangleStrip.vtu') uGrids.append(MakePolygon()) filenames.append('Polygon.vtu') uGrids.append(MakeUnstructuredGrid(vtk.vtkPixel())) filenames.append('Pixel.vtu') uGrids.append(MakeUnstructuredGrid(vtk.vtkQuad())) filenames.append('Quad.vtu') uGrids.append(MakeUnstructuredGrid(vtk.vtkTetra())) filenames.append('Tetra.vtu') uGrids.append(MakeUnstructuredGrid(vtk.vtkVoxel())) filenames.append('Voxel.vtu') uGrids.append(MakeUnstructuredGrid(vtk.vtkHexahedron())) filenames.append('Hexahedron.vtu') uGrids.append(MakeUnstructuredGrid(vtk.vtkWedge())) filenames.append('Wedge.vtu') uGrids.append(MakeUnstructuredGrid(vtk.vtkPyramid())) filenames.append('Pyramid.vtu') uGrids.append(MakeUnstructuredGrid(vtk.vtkPentagonalPrism())) filenames.append('PentagonalPrism.vtu') uGrids.append(MakeUnstructuredGrid(vtk.vtkHexagonalPrism())) filenames.append('HexagonalPrism.vtu') # Write each grid into a file for i in range(0, len(uGrids)): print('Writing: ', filenames[i]) writer = vtk.vtkXMLDataSetWriter() writer.SetFileName(filenames[i]) writer.SetInputData(uGrids[i]) writer.Write()
def main(): filenames = list() uGrids = list() uGrids.append(MakeUnstructuredGrid(vtk.vtkVertex())) filenames.append("Vertex.vtk") uGrids.append(MakePolyVertex()) filenames.append("PolyVertex.vtk") uGrids.append(MakeUnstructuredGrid(vtk.vtkLine())) filenames.append("Line.vtk") uGrids.append(MakePolyLine()) filenames.append("PolyLine.vtk") uGrids.append(MakeUnstructuredGrid(vtk.vtkTriangle())) filenames.append("Triangle.vtk") uGrids.append(MakeTriangleStrip()) filenames.append("TriangleStrip.vtk") uGrids.append(MakePolygon()) filenames.append("Polygon.vtk") uGrids.append(MakeUnstructuredGrid(vtk.vtkPixel())) filenames.append("Pixel.vtk") uGrids.append(MakeUnstructuredGrid(vtk.vtkQuad())) filenames.append("Quad.vtk") uGrids.append(MakeUnstructuredGrid(vtk.vtkTetra())) filenames.append("Tetra.vtk") uGrids.append(MakeUnstructuredGrid(vtk.vtkVoxel())) filenames.append("Voxel.vtk") uGrids.append(MakeUnstructuredGrid(vtk.vtkHexahedron())) filenames.append("Hexahedron.vtk") uGrids.append(MakeUnstructuredGrid(vtk.vtkWedge())) filenames.append("Wedge.vtk") uGrids.append(MakeUnstructuredGrid(vtk.vtkPyramid())) filenames.append("Pyramid.vtk") uGrids.append(MakeUnstructuredGrid(vtk.vtkPentagonalPrism())) filenames.append("PentagonalPrism.vtk") uGrids.append(MakeUnstructuredGrid(vtk.vtkHexagonalPrism())) filenames.append("HexagonalPrism.vtk") # Write each grid into a file for i in range(0, len(uGrids)): print("Writing: ", filenames[i]) writer = vtk.vtkUnstructuredGridWriter() writer.SetFileName(filenames[i]) writer.SetInputData(uGrids[i]) writer.Write()
def Vtk_cell_types(): vtk_types = [ ] #(vtk_type, vtk_id, vtkCell) <= linear cell types found in VTK vtk_types.append(("VTK_TRIANGLE", 5, vtk.vtkTriangle())) vtk_types.append(("VTK_QUAD", 9, vtk.vtkQuad())) vtk_types.append(("VTK_TETRA", 10, vtk.vtkTetra())) vtk_types.append(("VTK_PYRAMID", 14, vtk.vtkPyramid())) vtk_types.append(("VTK_WEDGE", 13, vtk.vtkWedge())) vtk_types.append(("VTK_HEXAHEDRON", 12, vtk.vtkHexahedron())) return vtk_types
def buildPartialVTKGrid(self, factag): #get points required for factag pointsList = self.getPointsWithFactag(factag) #create a lookup table so we can map the #cells from the global list to a local list points = vtk.vtkPoints() localIdx = 0 ptMap = {} for pt in pointsList: ptMap[int(pt)] = localIdx localIdx = localIdx + 1 p = self.mesh.coords[(pt * 3):(pt * 3 + 3)] points.InsertNextPoint(p) vtkgrid = vtk.vtkUnstructuredGrid() vtkgrid.SetPoints(points) #get elements that have desired factag felements = self.getElementsWithFactag(factag) #build the vtk elements for element in felements: type = element.getType() nodes = element.nodes if type == eTypes.TRI: cell = vtk.vtkTriangle() elif type == eTypes.QUAD: cell = vtk.vtkQuad() elif type == eTypes.TET: cell = vtk.vtkTetra() elif type == eTypes.PYRAMID: cell = vtk.vtkPyramid() elif type == eTypes.PRISM: cell = vtk.vtkWedge() #prism elif type == eTypes.HEX: cell = vtk.vtkHexahedron() else: raise # throw an exception j = 0 for n in nodes: localId = ptMap[int(n)] cell.GetPointIds().SetId(j, localId) j = j + 1 vtkgrid.InsertNextCell(cell.GetCellType(), cell.GetPointIds()) return vtkgrid
def buildPartialVTKGrid(self, factag): #get points required for factag pointsList = self.getPointsWithFactag(factag) #create a lookup table so we can map the #cells from the global list to a local list points = vtk.vtkPoints() localIdx = 0 ptMap = {} for pt in pointsList: ptMap[int(pt)] = localIdx localIdx = localIdx + 1 p = self.mesh.coords[(pt*3):(pt*3+3)] points.InsertNextPoint(p) vtkgrid = vtk.vtkUnstructuredGrid() vtkgrid.SetPoints(points) #get elements that have desired factag felements = self.getElementsWithFactag(factag) #build the vtk elements for element in felements: type = element.getType() nodes = element.nodes if type == eTypes.TRI: cell = vtk.vtkTriangle() elif type == eTypes.QUAD: cell = vtk.vtkQuad() elif type == eTypes.TET: cell = vtk.vtkTetra() elif type == eTypes.PYRAMID: cell = vtk.vtkPyramid() elif type == eTypes.PRISM: cell = vtk.vtkWedge() #prism elif type == eTypes.HEX: cell = vtk.vtkHexahedron() else: raise # throw an exception j = 0 for n in nodes: localId = ptMap[int(n)] cell.GetPointIds().SetId(j,localId) j = j+1 vtkgrid.InsertNextCell(cell.GetCellType(), cell.GetPointIds()) return vtkgrid
def __init__(self): self.obj = None self.num_pts = -1 self.vtk_pts = vtk.vtkPoints() self.vtk_cells = vtk.vtkCellArray() self.vtk_tetra = vtk.vtkTetra() self.vtk_props = [] self.vtk_cells_type = [] self.vtk_cells_id = [] self.vtk_types = [ ] #(vtk_type, vtk_id, vtkCell) <= linear cell types found in VTK self.vtk_types.append(("VTK_TRIANGLE", 5, vtk.vtkTriangle())) self.vtk_types.append(("VTK_QUAD", 9, vtk.vtkQuad())) self.vtk_types.append(("VTK_TETRA", 10, vtk.vtkTetra())) self.vtk_types.append(("VTK_PYRAMID", 14, vtk.vtkPyramid())) self.vtk_types.append(("VTK_WEDGE", 13, vtk.vtkWedge())) self.vtk_types.append(("VTK_HEXAHEDRON", 12, vtk.vtkHexahedron())) print "\n+Vtk_writer"
def buildFullVTKGrid(self): # Create the points for VTK points = vtk.vtkPoints() for i in range(0, len(self.mesh.coords) / 3): p = self.mesh.coords[(i * 3):(i * 3 + 3)] points.InsertNextPoint(p) #add the points and cells to unstructured grid vtkgrid = vtk.vtkUnstructuredGrid() vtkgrid.SetPoints(points) #add the VTK elements to the mesh for element in self.mesh.elements: type = element.getType() nodes = element.nodes cell = vtk.vtkTriangle() if type == eTypes.TRI: cell = vtk.vtkTriangle() elif type == eTypes.QUAD: cell = vtk.vtkQuad() elif type == eTypes.TET: cell = vtk.vtkTetra() elif type == eTypes.PYRAMID: cell = vtk.vtkPyramid() elif type == eTypes.PRISM: cell = vtk.vtkWedge() #prism elif type == eTypes.HEX: cell = vtk.vtkHexahedron() else: raise # throw an exception j = 0 for n in nodes: cell.GetPointIds().SetId(j, n) j = j + 1 vtkgrid.InsertNextCell(cell.GetCellType(), cell.GetPointIds()) return vtkgrid
def buildFullVTKGrid(self): # Create the points for VTK points = vtk.vtkPoints() for i in range(0, len(self.mesh.coords)/3): p = self.mesh.coords[(i*3):(i*3+3)] points.InsertNextPoint(p) #add the points and cells to unstructured grid vtkgrid = vtk.vtkUnstructuredGrid() vtkgrid.SetPoints(points) #add the VTK elements to the mesh for element in self.mesh.elements: type = element.getType() nodes = element.nodes cell = vtk.vtkTriangle() if type == eTypes.TRI: cell = vtk.vtkTriangle() elif type == eTypes.QUAD: cell = vtk.vtkQuad() elif type == eTypes.TET: cell = vtk.vtkTetra() elif type == eTypes.PYRAMID: cell = vtk.vtkPyramid() elif type == eTypes.PRISM: cell = vtk.vtkWedge() #prism elif type == eTypes.HEX: cell = vtk.vtkHexahedron() else: raise # throw an exception j = 0 for n in nodes: cell.GetPointIds().SetId(j,n) j = j+1 vtkgrid.InsertNextCell(cell.GetCellType(), cell.GetPointIds()) return vtkgrid
pts.InsertPoint(ptid, xyz) pts.InsertPoint(ptid + num_pts, xyz_vertical) if opts.verbose: print "Creating solid unstructured grid and adding points" solid = vtk.vtkUnstructuredGrid() solid.SetPoints(pts) solid.Update() if opts.verbose: print "Inserting", base.GetNumberOfPolys(), "wedges into", out_vtu for polyid in range(base.GetNumberOfPolys()): if base.GetCellType(polyid) != 5: print "Cannot form a mesh with non-triangular polygons in the base" sys.exit(2) vtk_cell = base.GetCell(polyid) wedge = vtk.vtkWedge().GetPointIds() for pt in range(3): poly_pt = vtk_cell.GetPointId(pt) wedge.SetId(pt, poly_pt) wedge.SetId(pt + 3, poly_pt + num_pts) solid.InsertNextCell(13, wedge) if opts.verbose: print "Adding provenance" if base.GetFieldData().HasArray("provenance"): command_used = base.GetFieldData().GetAbstractArray("provenance") else: command_used = vtk.vtkStringArray() command_used.SetName("provenance") command_used.InsertNextValue(" ".join(sys.argv)) solid.GetFieldData().AddArray(command_used)
def testCells(self): # Demonstrates all cell types # # NOTE: the use of NewInstance/DeepCopy is included to increase # regression coverage. It is not required in most applications. ren = vtk.vtkRenderer() # turn off all cullers ren.GetCullers().RemoveAllItems() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren) renWin.SetSize(300, 150) iRen = vtk.vtkRenderWindowInteractor() iRen.SetRenderWindow(renWin) # create a scene with one of each cell type # Voxel voxelPoints = vtk.vtkPoints() voxelPoints.SetNumberOfPoints(8) voxelPoints.InsertPoint(0, 0, 0, 0) voxelPoints.InsertPoint(1, 1, 0, 0) voxelPoints.InsertPoint(2, 0, 1, 0) voxelPoints.InsertPoint(3, 1, 1, 0) voxelPoints.InsertPoint(4, 0, 0, 1) voxelPoints.InsertPoint(5, 1, 0, 1) voxelPoints.InsertPoint(6, 0, 1, 1) voxelPoints.InsertPoint(7, 1, 1, 1) aVoxel = vtk.vtkVoxel() aVoxel.GetPointIds().SetId(0, 0) aVoxel.GetPointIds().SetId(1, 1) aVoxel.GetPointIds().SetId(2, 2) aVoxel.GetPointIds().SetId(3, 3) aVoxel.GetPointIds().SetId(4, 4) aVoxel.GetPointIds().SetId(5, 5) aVoxel.GetPointIds().SetId(6, 6) aVoxel.GetPointIds().SetId(7, 7) bVoxel = aVoxel.NewInstance() bVoxel.DeepCopy(aVoxel) aVoxelGrid = vtk.vtkUnstructuredGrid() aVoxelGrid.Allocate(1, 1) aVoxelGrid.InsertNextCell(aVoxel.GetCellType(), aVoxel.GetPointIds()) aVoxelGrid.SetPoints(voxelPoints) aVoxelMapper = vtk.vtkDataSetMapper() aVoxelMapper.SetInputData(aVoxelGrid) aVoxelActor = vtk.vtkActor() aVoxelActor.SetMapper(aVoxelMapper) aVoxelActor.GetProperty().BackfaceCullingOn() # Hexahedron hexahedronPoints = vtk.vtkPoints() hexahedronPoints.SetNumberOfPoints(8) hexahedronPoints.InsertPoint(0, 0, 0, 0) hexahedronPoints.InsertPoint(1, 1, 0, 0) hexahedronPoints.InsertPoint(2, 1, 1, 0) hexahedronPoints.InsertPoint(3, 0, 1, 0) hexahedronPoints.InsertPoint(4, 0, 0, 1) hexahedronPoints.InsertPoint(5, 1, 0, 1) hexahedronPoints.InsertPoint(6, 1, 1, 1) hexahedronPoints.InsertPoint(7, 0, 1, 1) aHexahedron = vtk.vtkHexahedron() aHexahedron.GetPointIds().SetId(0, 0) aHexahedron.GetPointIds().SetId(1, 1) aHexahedron.GetPointIds().SetId(2, 2) aHexahedron.GetPointIds().SetId(3, 3) aHexahedron.GetPointIds().SetId(4, 4) aHexahedron.GetPointIds().SetId(5, 5) aHexahedron.GetPointIds().SetId(6, 6) aHexahedron.GetPointIds().SetId(7, 7) bHexahedron = aHexahedron.NewInstance() bHexahedron.DeepCopy(aHexahedron) aHexahedronGrid = vtk.vtkUnstructuredGrid() aHexahedronGrid.Allocate(1, 1) aHexahedronGrid.InsertNextCell(aHexahedron.GetCellType(), aHexahedron.GetPointIds()) aHexahedronGrid.SetPoints(hexahedronPoints) aHexahedronMapper = vtk.vtkDataSetMapper() aHexahedronMapper.SetInputData(aHexahedronGrid) aHexahedronActor = vtk.vtkActor() aHexahedronActor.SetMapper(aHexahedronMapper) aHexahedronActor.AddPosition(2, 0, 0) aHexahedronActor.GetProperty().BackfaceCullingOn() # Tetra tetraPoints = vtk.vtkPoints() tetraPoints.SetNumberOfPoints(4) tetraPoints.InsertPoint(0, 0, 0, 0) tetraPoints.InsertPoint(1, 1, 0, 0) tetraPoints.InsertPoint(2, .5, 1, 0) tetraPoints.InsertPoint(3, .5, .5, 1) aTetra = vtk.vtkTetra() aTetra.GetPointIds().SetId(0, 0) aTetra.GetPointIds().SetId(1, 1) aTetra.GetPointIds().SetId(2, 2) aTetra.GetPointIds().SetId(3, 3) bTetra = aTetra.NewInstance() bTetra.DeepCopy(aTetra) aTetraGrid = vtk.vtkUnstructuredGrid() aTetraGrid.Allocate(1, 1) aTetraGrid.InsertNextCell(aTetra.GetCellType(), aTetra.GetPointIds()) aTetraGrid.SetPoints(tetraPoints) aTetraCopy = vtk.vtkUnstructuredGrid() aTetraCopy.ShallowCopy(aTetraGrid) aTetraMapper = vtk.vtkDataSetMapper() aTetraMapper.SetInputData(aTetraCopy) aTetraActor = vtk.vtkActor() aTetraActor.SetMapper(aTetraMapper) aTetraActor.AddPosition(4, 0, 0) aTetraActor.GetProperty().BackfaceCullingOn() # Wedge wedgePoints = vtk.vtkPoints() wedgePoints.SetNumberOfPoints(6) wedgePoints.InsertPoint(0, 0, 1, 0) wedgePoints.InsertPoint(1, 0, 0, 0) wedgePoints.InsertPoint(2, 0, .5, .5) wedgePoints.InsertPoint(3, 1, 1, 0) wedgePoints.InsertPoint(4, 1, 0, 0) wedgePoints.InsertPoint(5, 1, .5, .5) aWedge = vtk.vtkWedge() aWedge.GetPointIds().SetId(0, 0) aWedge.GetPointIds().SetId(1, 1) aWedge.GetPointIds().SetId(2, 2) aWedge.GetPointIds().SetId(3, 3) aWedge.GetPointIds().SetId(4, 4) aWedge.GetPointIds().SetId(5, 5) bWedge = aWedge.NewInstance() bWedge.DeepCopy(aWedge) aWedgeGrid = vtk.vtkUnstructuredGrid() aWedgeGrid.Allocate(1, 1) aWedgeGrid.InsertNextCell(aWedge.GetCellType(), aWedge.GetPointIds()) aWedgeGrid.SetPoints(wedgePoints) aWedgeCopy = vtk.vtkUnstructuredGrid() aWedgeCopy.DeepCopy(aWedgeGrid) aWedgeMapper = vtk.vtkDataSetMapper() aWedgeMapper.SetInputData(aWedgeCopy) aWedgeActor = vtk.vtkActor() aWedgeActor.SetMapper(aWedgeMapper) aWedgeActor.AddPosition(6, 0, 0) aWedgeActor.GetProperty().BackfaceCullingOn() # Pyramid pyramidPoints = vtk.vtkPoints() pyramidPoints.SetNumberOfPoints(5) pyramidPoints.InsertPoint(0, 0, 0, 0) pyramidPoints.InsertPoint(1, 1, 0, 0) pyramidPoints.InsertPoint(2, 1, 1, 0) pyramidPoints.InsertPoint(3, 0, 1, 0) pyramidPoints.InsertPoint(4, .5, .5, 1) aPyramid = vtk.vtkPyramid() aPyramid.GetPointIds().SetId(0, 0) aPyramid.GetPointIds().SetId(1, 1) aPyramid.GetPointIds().SetId(2, 2) aPyramid.GetPointIds().SetId(3, 3) aPyramid.GetPointIds().SetId(4, 4) bPyramid = aPyramid.NewInstance() bPyramid.DeepCopy(aPyramid) aPyramidGrid = vtk.vtkUnstructuredGrid() aPyramidGrid.Allocate(1, 1) aPyramidGrid.InsertNextCell(aPyramid.GetCellType(), aPyramid.GetPointIds()) aPyramidGrid.SetPoints(pyramidPoints) aPyramidMapper = vtk.vtkDataSetMapper() aPyramidMapper.SetInputData(aPyramidGrid) aPyramidActor = vtk.vtkActor() aPyramidActor.SetMapper(aPyramidMapper) aPyramidActor.AddPosition(8, 0, 0) aPyramidActor.GetProperty().BackfaceCullingOn() # Pixel pixelPoints = vtk.vtkPoints() pixelPoints.SetNumberOfPoints(4) pixelPoints.InsertPoint(0, 0, 0, 0) pixelPoints.InsertPoint(1, 1, 0, 0) pixelPoints.InsertPoint(2, 0, 1, 0) pixelPoints.InsertPoint(3, 1, 1, 0) aPixel = vtk.vtkPixel() aPixel.GetPointIds().SetId(0, 0) aPixel.GetPointIds().SetId(1, 1) aPixel.GetPointIds().SetId(2, 2) aPixel.GetPointIds().SetId(3, 3) bPixel = aPixel.NewInstance() bPixel.DeepCopy(aPixel) aPixelGrid = vtk.vtkUnstructuredGrid() aPixelGrid.Allocate(1, 1) aPixelGrid.InsertNextCell(aPixel.GetCellType(), aPixel.GetPointIds()) aPixelGrid.SetPoints(pixelPoints) aPixelMapper = vtk.vtkDataSetMapper() aPixelMapper.SetInputData(aPixelGrid) aPixelActor = vtk.vtkActor() aPixelActor.SetMapper(aPixelMapper) aPixelActor.AddPosition(0, 0, 2) aPixelActor.GetProperty().BackfaceCullingOn() # Quad quadPoints = vtk.vtkPoints() quadPoints.SetNumberOfPoints(4) quadPoints.InsertPoint(0, 0, 0, 0) quadPoints.InsertPoint(1, 1, 0, 0) quadPoints.InsertPoint(2, 1, 1, 0) quadPoints.InsertPoint(3, 0, 1, 0) aQuad = vtk.vtkQuad() aQuad.GetPointIds().SetId(0, 0) aQuad.GetPointIds().SetId(1, 1) aQuad.GetPointIds().SetId(2, 2) aQuad.GetPointIds().SetId(3, 3) bQuad = aQuad.NewInstance() bQuad.DeepCopy(aQuad) aQuadGrid = vtk.vtkUnstructuredGrid() aQuadGrid.Allocate(1, 1) aQuadGrid.InsertNextCell(aQuad.GetCellType(), aQuad.GetPointIds()) aQuadGrid.SetPoints(quadPoints) aQuadMapper = vtk.vtkDataSetMapper() aQuadMapper.SetInputData(aQuadGrid) aQuadActor = vtk.vtkActor() aQuadActor.SetMapper(aQuadMapper) aQuadActor.AddPosition(2, 0, 2) aQuadActor.GetProperty().BackfaceCullingOn() # Triangle trianglePoints = vtk.vtkPoints() trianglePoints.SetNumberOfPoints(3) trianglePoints.InsertPoint(0, 0, 0, 0) trianglePoints.InsertPoint(1, 1, 0, 0) trianglePoints.InsertPoint(2, .5, .5, 0) triangleTCoords = vtk.vtkFloatArray() triangleTCoords.SetNumberOfComponents(2) triangleTCoords.SetNumberOfTuples(3) triangleTCoords.InsertTuple2(0, 1, 1) triangleTCoords.InsertTuple2(1, 2, 2) triangleTCoords.InsertTuple2(2, 3, 3) aTriangle = vtk.vtkTriangle() aTriangle.GetPointIds().SetId(0, 0) aTriangle.GetPointIds().SetId(1, 1) aTriangle.GetPointIds().SetId(2, 2) bTriangle = aTriangle.NewInstance() bTriangle.DeepCopy(aTriangle) aTriangleGrid = vtk.vtkUnstructuredGrid() aTriangleGrid.Allocate(1, 1) aTriangleGrid.InsertNextCell(aTriangle.GetCellType(), aTriangle.GetPointIds()) aTriangleGrid.SetPoints(trianglePoints) aTriangleGrid.GetPointData().SetTCoords(triangleTCoords) aTriangleMapper = vtk.vtkDataSetMapper() aTriangleMapper.SetInputData(aTriangleGrid) aTriangleActor = vtk.vtkActor() aTriangleActor.SetMapper(aTriangleMapper) aTriangleActor.AddPosition(4, 0, 2) aTriangleActor.GetProperty().BackfaceCullingOn() # Polygon polygonPoints = vtk.vtkPoints() polygonPoints.SetNumberOfPoints(4) polygonPoints.InsertPoint(0, 0, 0, 0) polygonPoints.InsertPoint(1, 1, 0, 0) polygonPoints.InsertPoint(2, 1, 1, 0) polygonPoints.InsertPoint(3, 0, 1, 0) aPolygon = vtk.vtkPolygon() aPolygon.GetPointIds().SetNumberOfIds(4) aPolygon.GetPointIds().SetId(0, 0) aPolygon.GetPointIds().SetId(1, 1) aPolygon.GetPointIds().SetId(2, 2) aPolygon.GetPointIds().SetId(3, 3) bPolygon = aPolygon.NewInstance() bPolygon.DeepCopy(aPolygon) aPolygonGrid = vtk.vtkUnstructuredGrid() aPolygonGrid.Allocate(1, 1) aPolygonGrid.InsertNextCell(aPolygon.GetCellType(), aPolygon.GetPointIds()) aPolygonGrid.SetPoints(polygonPoints) aPolygonMapper = vtk.vtkDataSetMapper() aPolygonMapper.SetInputData(aPolygonGrid) aPolygonActor = vtk.vtkActor() aPolygonActor.SetMapper(aPolygonMapper) aPolygonActor.AddPosition(6, 0, 2) aPolygonActor.GetProperty().BackfaceCullingOn() # Triangle Strip triangleStripPoints = vtk.vtkPoints() triangleStripPoints.SetNumberOfPoints(5) triangleStripPoints.InsertPoint(0, 0, 1, 0) triangleStripPoints.InsertPoint(1, 0, 0, 0) triangleStripPoints.InsertPoint(2, 1, 1, 0) triangleStripPoints.InsertPoint(3, 1, 0, 0) triangleStripPoints.InsertPoint(4, 2, 1, 0) triangleStripTCoords = vtk.vtkFloatArray() triangleStripTCoords.SetNumberOfComponents(2) triangleStripTCoords.SetNumberOfTuples(3) triangleStripTCoords.InsertTuple2(0, 1, 1) triangleStripTCoords.InsertTuple2(1, 2, 2) triangleStripTCoords.InsertTuple2(2, 3, 3) triangleStripTCoords.InsertTuple2(3, 4, 4) triangleStripTCoords.InsertTuple2(4, 5, 5) aTriangleStrip = vtk.vtkTriangleStrip() aTriangleStrip.GetPointIds().SetNumberOfIds(5) aTriangleStrip.GetPointIds().SetId(0, 0) aTriangleStrip.GetPointIds().SetId(1, 1) aTriangleStrip.GetPointIds().SetId(2, 2) aTriangleStrip.GetPointIds().SetId(3, 3) aTriangleStrip.GetPointIds().SetId(4, 4) bTriangleStrip = aTriangleStrip.NewInstance() bTriangleStrip.DeepCopy(aTriangleStrip) aTriangleStripGrid = vtk.vtkUnstructuredGrid() aTriangleStripGrid.Allocate(1, 1) aTriangleStripGrid.InsertNextCell(aTriangleStrip.GetCellType(), aTriangleStrip.GetPointIds()) aTriangleStripGrid.SetPoints(triangleStripPoints) aTriangleStripGrid.GetPointData().SetTCoords(triangleStripTCoords) aTriangleStripMapper = vtk.vtkDataSetMapper() aTriangleStripMapper.SetInputData(aTriangleStripGrid) aTriangleStripActor = vtk.vtkActor() aTriangleStripActor.SetMapper(aTriangleStripMapper) aTriangleStripActor.AddPosition(8, 0, 2) aTriangleStripActor.GetProperty().BackfaceCullingOn() # Line linePoints = vtk.vtkPoints() linePoints.SetNumberOfPoints(2) linePoints.InsertPoint(0, 0, 0, 0) linePoints.InsertPoint(1, 1, 1, 0) aLine = vtk.vtkLine() aLine.GetPointIds().SetId(0, 0) aLine.GetPointIds().SetId(1, 1) bLine = aLine.NewInstance() bLine.DeepCopy(aLine) aLineGrid = vtk.vtkUnstructuredGrid() aLineGrid.Allocate(1, 1) aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds()) aLineGrid.SetPoints(linePoints) aLineMapper = vtk.vtkDataSetMapper() aLineMapper.SetInputData(aLineGrid) aLineActor = vtk.vtkActor() aLineActor.SetMapper(aLineMapper) aLineActor.AddPosition(0, 0, 4) aLineActor.GetProperty().BackfaceCullingOn() # Poly line polyLinePoints = vtk.vtkPoints() polyLinePoints.SetNumberOfPoints(3) polyLinePoints.InsertPoint(0, 0, 0, 0) polyLinePoints.InsertPoint(1, 1, 1, 0) polyLinePoints.InsertPoint(2, 1, 0, 0) aPolyLine = vtk.vtkPolyLine() aPolyLine.GetPointIds().SetNumberOfIds(3) aPolyLine.GetPointIds().SetId(0, 0) aPolyLine.GetPointIds().SetId(1, 1) aPolyLine.GetPointIds().SetId(2, 2) bPolyLine = aPolyLine.NewInstance() bPolyLine.DeepCopy(aPolyLine) aPolyLineGrid = vtk.vtkUnstructuredGrid() aPolyLineGrid.Allocate(1, 1) aPolyLineGrid.InsertNextCell(aPolyLine.GetCellType(), aPolyLine.GetPointIds()) aPolyLineGrid.SetPoints(polyLinePoints) aPolyLineMapper = vtk.vtkDataSetMapper() aPolyLineMapper.SetInputData(aPolyLineGrid) aPolyLineActor = vtk.vtkActor() aPolyLineActor.SetMapper(aPolyLineMapper) aPolyLineActor.AddPosition(2, 0, 4) aPolyLineActor.GetProperty().BackfaceCullingOn() # Vertex vertexPoints = vtk.vtkPoints() vertexPoints.SetNumberOfPoints(1) vertexPoints.InsertPoint(0, 0, 0, 0) aVertex = vtk.vtkVertex() aVertex.GetPointIds().SetId(0, 0) bVertex = aVertex.NewInstance() bVertex.DeepCopy(aVertex) aVertexGrid = vtk.vtkUnstructuredGrid() aVertexGrid.Allocate(1, 1) aVertexGrid.InsertNextCell(aVertex.GetCellType(), aVertex.GetPointIds()) aVertexGrid.SetPoints(vertexPoints) aVertexMapper = vtk.vtkDataSetMapper() aVertexMapper.SetInputData(aVertexGrid) aVertexActor = vtk.vtkActor() aVertexActor.SetMapper(aVertexMapper) aVertexActor.AddPosition(0, 0, 6) aVertexActor.GetProperty().BackfaceCullingOn() # Poly Vertex polyVertexPoints = vtk.vtkPoints() polyVertexPoints.SetNumberOfPoints(3) polyVertexPoints.InsertPoint(0, 0, 0, 0) polyVertexPoints.InsertPoint(1, 1, 0, 0) polyVertexPoints.InsertPoint(2, 1, 1, 0) aPolyVertex = vtk.vtkPolyVertex() aPolyVertex.GetPointIds().SetNumberOfIds(3) aPolyVertex.GetPointIds().SetId(0, 0) aPolyVertex.GetPointIds().SetId(1, 1) aPolyVertex.GetPointIds().SetId(2, 2) bPolyVertex = aPolyVertex.NewInstance() bPolyVertex.DeepCopy(aPolyVertex) aPolyVertexGrid = vtk.vtkUnstructuredGrid() aPolyVertexGrid.Allocate(1, 1) aPolyVertexGrid.InsertNextCell(aPolyVertex.GetCellType(), aPolyVertex.GetPointIds()) aPolyVertexGrid.SetPoints(polyVertexPoints) aPolyVertexMapper = vtk.vtkDataSetMapper() aPolyVertexMapper.SetInputData(aPolyVertexGrid) aPolyVertexActor = vtk.vtkActor() aPolyVertexActor.SetMapper(aPolyVertexMapper) aPolyVertexActor.AddPosition(2, 0, 6) aPolyVertexActor.GetProperty().BackfaceCullingOn() # Pentagonal prism pentaPoints = vtk.vtkPoints() pentaPoints.SetNumberOfPoints(10) pentaPoints.InsertPoint(0, 0.25, 0.0, 0.0) pentaPoints.InsertPoint(1, 0.75, 0.0, 0.0) pentaPoints.InsertPoint(2, 1.0, 0.5, 0.0) pentaPoints.InsertPoint(3, 0.5, 1.0, 0.0) pentaPoints.InsertPoint(4, 0.0, 0.5, 0.0) pentaPoints.InsertPoint(5, 0.25, 0.0, 1.0) pentaPoints.InsertPoint(6, 0.75, 0.0, 1.0) pentaPoints.InsertPoint(7, 1.0, 0.5, 1.0) pentaPoints.InsertPoint(8, 0.5, 1.0, 1.0) pentaPoints.InsertPoint(9, 0.0, 0.5, 1.0) aPenta = vtk.vtkPentagonalPrism() aPenta.GetPointIds().SetId(0, 0) aPenta.GetPointIds().SetId(1, 1) aPenta.GetPointIds().SetId(2, 2) aPenta.GetPointIds().SetId(3, 3) aPenta.GetPointIds().SetId(4, 4) aPenta.GetPointIds().SetId(5, 5) aPenta.GetPointIds().SetId(6, 6) aPenta.GetPointIds().SetId(7, 7) aPenta.GetPointIds().SetId(8, 8) aPenta.GetPointIds().SetId(9, 9) bPenta = aPenta.NewInstance() bPenta.DeepCopy(aPenta) aPentaGrid = vtk.vtkUnstructuredGrid() aPentaGrid.Allocate(1, 1) aPentaGrid.InsertNextCell(aPenta.GetCellType(), aPenta.GetPointIds()) aPentaGrid.SetPoints(pentaPoints) aPentaCopy = vtk.vtkUnstructuredGrid() aPentaCopy.DeepCopy(aPentaGrid) aPentaMapper = vtk.vtkDataSetMapper() aPentaMapper.SetInputData(aPentaCopy) aPentaActor = vtk.vtkActor() aPentaActor.SetMapper(aPentaMapper) aPentaActor.AddPosition(10, 0, 0) aPentaActor.GetProperty().BackfaceCullingOn() # Hexagonal prism hexaPoints = vtk.vtkPoints() hexaPoints.SetNumberOfPoints(12) hexaPoints.InsertPoint(0, 0.0, 0.0, 0.0) hexaPoints.InsertPoint(1, 0.5, 0.0, 0.0) hexaPoints.InsertPoint(2, 1.0, 0.5, 0.0) hexaPoints.InsertPoint(3, 1.0, 1.0, 0.0) hexaPoints.InsertPoint(4, 0.5, 1.0, 0.0) hexaPoints.InsertPoint(5, 0.0, 0.5, 0.0) hexaPoints.InsertPoint(6, 0.0, 0.0, 1.0) hexaPoints.InsertPoint(7, 0.5, 0.0, 1.0) hexaPoints.InsertPoint(8, 1.0, 0.5, 1.0) hexaPoints.InsertPoint(9, 1.0, 1.0, 1.0) hexaPoints.InsertPoint(10, 0.5, 1.0, 1.0) hexaPoints.InsertPoint(11, 0.0, 0.5, 1.0) aHexa = vtk.vtkHexagonalPrism() aHexa.GetPointIds().SetId(0, 0) aHexa.GetPointIds().SetId(1, 1) aHexa.GetPointIds().SetId(2, 2) aHexa.GetPointIds().SetId(3, 3) aHexa.GetPointIds().SetId(4, 4) aHexa.GetPointIds().SetId(5, 5) aHexa.GetPointIds().SetId(6, 6) aHexa.GetPointIds().SetId(7, 7) aHexa.GetPointIds().SetId(8, 8) aHexa.GetPointIds().SetId(9, 9) aHexa.GetPointIds().SetId(10, 10) aHexa.GetPointIds().SetId(11, 11) bHexa = aHexa.NewInstance() bHexa.DeepCopy(aHexa) aHexaGrid = vtk.vtkUnstructuredGrid() aHexaGrid.Allocate(1, 1) aHexaGrid.InsertNextCell(aHexa.GetCellType(), aHexa.GetPointIds()) aHexaGrid.SetPoints(hexaPoints) aHexaCopy = vtk.vtkUnstructuredGrid() aHexaCopy.DeepCopy(aHexaGrid) aHexaMapper = vtk.vtkDataSetMapper() aHexaMapper.SetInputData(aHexaCopy) aHexaActor = vtk.vtkActor() aHexaActor.SetMapper(aHexaMapper) aHexaActor.AddPosition(12, 0, 0) aHexaActor.GetProperty().BackfaceCullingOn() # RIB property if hasattr(vtk, 'vtkRIBProperty'): aRIBProperty = vtk.vtkRIBProperty() aRIBProperty.SetVariable("Km", "float") aRIBProperty.SetSurfaceShader("LGVeinedmarble") aRIBProperty.SetVariable("veinfreq", "float") aRIBProperty.AddVariable("warpfreq", "float") aRIBProperty.AddVariable("veincolor", "color") aRIBProperty.AddSurfaceShaderParameter("veinfreq", " 2") aRIBProperty.AddSurfaceShaderParameter("veincolor", "1.0000 1.0000 0.9412") bRIBProperty = vtk.vtkRIBProperty() bRIBProperty.SetVariable("Km", "float") bRIBProperty.SetSurfaceShaderParameter("Km", "1.0") bRIBProperty.SetDisplacementShader("dented") bRIBProperty.SetSurfaceShader("plastic") aProperty = vtk.vtkProperty() bProperty = vtk.vtkProperty() aTriangleActor.SetProperty(aProperty) aTriangleStripActor.SetProperty(bProperty) ren.SetBackground(.1, .2, .4) ren.AddActor(aVoxelActor) aVoxelActor.GetProperty().SetDiffuseColor(1, 0, 0) ren.AddActor(aHexahedronActor) aHexahedronActor.GetProperty().SetDiffuseColor(1, 1, 0) ren.AddActor(aTetraActor) aTetraActor.GetProperty().SetDiffuseColor(0, 1, 0) ren.AddActor(aWedgeActor) aWedgeActor.GetProperty().SetDiffuseColor(0, 1, 1) ren.AddActor(aPyramidActor) aPyramidActor.GetProperty().SetDiffuseColor(1, 0, 1) ren.AddActor(aPixelActor) aPixelActor.GetProperty().SetDiffuseColor(0, 1, 1) ren.AddActor(aQuadActor) aQuadActor.GetProperty().SetDiffuseColor(1, 0, 1) ren.AddActor(aTriangleActor) aTriangleActor.GetProperty().SetDiffuseColor(.3, 1, .5) ren.AddActor(aPolygonActor) aPolygonActor.GetProperty().SetDiffuseColor(1, .4, .5) ren.AddActor(aTriangleStripActor) aTriangleStripActor.GetProperty().SetDiffuseColor(.3, .7, 1) ren.AddActor(aLineActor) aLineActor.GetProperty().SetDiffuseColor(.2, 1, 1) ren.AddActor(aPolyLineActor) aPolyLineActor.GetProperty().SetDiffuseColor(1, 1, 1) ren.AddActor(aVertexActor) aVertexActor.GetProperty().SetDiffuseColor(1, 1, 1) ren.AddActor(aPolyVertexActor) aPolyVertexActor.GetProperty().SetDiffuseColor(1, 1, 1) ren.AddActor(aPentaActor) aPentaActor.GetProperty().SetDiffuseColor(.2, .4, .7) ren.AddActor(aHexaActor) aHexaActor.GetProperty().SetDiffuseColor(.7, .5, 1) if hasattr(vtk, 'vtkRIBLight'): aRIBLight = vtk.vtkRIBLight() aRIBLight.SetIntensity(0.7) ren.AddLight(aRIBLight) aLight = vtk.vtkLight() aLight.PositionalOn() aLight.SetConeAngle(10.0) aLight.SetIntensity(0.7) ren.AddLight(aLight) ren.ResetCamera() ren.GetActiveCamera().Azimuth(30) ren.GetActiveCamera().Elevation(20) ren.GetActiveCamera().Dolly(2.8) ren.ResetCameraClippingRange() dir = VTK_TEMP_DIR atext = vtk.vtkTexture() pnmReader = vtk.vtkBMPReader() pnmReader.SetFileName(VTK_DATA_ROOT + "/Data/masonry.bmp") atext.SetInputConnection(pnmReader.GetOutputPort()) atext.InterpolateOff() aTriangleActor.SetTexture(atext) aRIBLight.SetFocalPoint(ren.GetActiveCamera().GetFocalPoint()) aRIBLight.SetPosition(ren.GetActiveCamera().GetPosition()) aLight.SetFocalPoint(ren.GetActiveCamera().GetFocalPoint()) aLight.SetPosition(ren.GetActiveCamera().GetPosition()) # bascially have IO/Export ? if hasattr(vtk, 'vtkRIBExporter'): rib = vtk.vtkRIBExporter() rib.SetInput(renWin) rib.SetFilePrefix(dir + '/cells') rib.SetTexturePrefix(dir + '/cells') rib.Write() iv = vtk.vtkIVExporter() iv.SetInput(renWin) iv.SetFileName(dir + "/cells.iv") iv.Write() os.remove(dir + '/cells.iv') obj = vtk.vtkOBJExporter() obj.SetInput(renWin) obj.SetFilePrefix(dir + "/cells") obj.Write() os.remove(dir + '/cells.obj') os.remove(dir + '/cells.mtl') vrml = vtk.vtkVRMLExporter() vrml.SetInput(renWin) #vrml.SetStartWrite(vrml.SetFileName(dir + "/cells.wrl")) #vrml.SetEndWrite(vrml.SetFileName("/a/acells.wrl")) vrml.SetFileName(dir + "/cells.wrl") vrml.SetSpeed(5.5) vrml.Write() os.remove(dir + '/cells.wrl') oogl = vtk.vtkOOGLExporter() oogl.SetInput(renWin) oogl.SetFileName(dir + "/cells.oogl") oogl.Write() os.remove(dir + '/cells.oogl') # the UnRegister calls are because make object is the same as New, # and causes memory leaks. (Python does not treat NewInstance the same as New). def DeleteCopies(): bVoxel.UnRegister(None) bHexahedron.UnRegister(None) bTetra.UnRegister(None) bWedge.UnRegister(None) bPyramid.UnRegister(None) bPixel.UnRegister(None) bQuad.UnRegister(None) bTriangle.UnRegister(None) bPolygon.UnRegister(None) bTriangleStrip.UnRegister(None) bLine.UnRegister(None) bPolyLine.UnRegister(None) bVertex.UnRegister(None) bPolyVertex.UnRegister(None) bPenta.UnRegister(None) bHexa.UnRegister(None) DeleteCopies() # render and interact with data renWin.Render() img_file = "cells.png" vtk.test.Testing.compareImage( iRen.GetRenderWindow(), vtk.test.Testing.getAbsImagePath(img_file)) vtk.test.Testing.interact()
def mapElements(self, points, points2, nidMap, model, j): for eid, element in sorted(model.elements.iteritems()): if isinstance(element, CTRIA3): #print "ctria3" elem = vtkTriangle() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTRIA6): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticTriangle() elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) else: elem = vtkTriangle() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CQUAD4): nodeIDs = element.nodeIDs() elem = vtkQuad() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CQUAD8): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticQuad() elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) else: elem = vtkQuad() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTETRA4): elem = vtkTetra() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTETRA10): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticTetra() elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) else: elem = vtkTetra() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CPENTA6): elem = vtkWedge() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CPENTA15): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticWedge() elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) elem.GetPointIds().SetId(10, nidMap[nodeIDs[10]]) elem.GetPointIds().SetId(11, nidMap[nodeIDs[11]]) elem.GetPointIds().SetId(12, nidMap[nodeIDs[12]]) elem.GetPointIds().SetId(13, nidMap[nodeIDs[13]]) elem.GetPointIds().SetId(14, nidMap[nodeIDs[14]]) else: elem = vtkWedge() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CHEXA8): nodeIDs = element.nodeIDs() elem = vtkHexahedron() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CHEXA20): nodeIDs = element.nodeIDs() #print "nodeIDs = ",nodeIDs if None not in nodeIDs: elem = vtkQuadraticHexahedron() elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) elem.GetPointIds().SetId(10, nidMap[nodeIDs[10]]) elem.GetPointIds().SetId(11, nidMap[nodeIDs[11]]) elem.GetPointIds().SetId(12, nidMap[nodeIDs[12]]) elem.GetPointIds().SetId(13, nidMap[nodeIDs[13]]) elem.GetPointIds().SetId(14, nidMap[nodeIDs[14]]) elem.GetPointIds().SetId(15, nidMap[nodeIDs[15]]) elem.GetPointIds().SetId(16, nidMap[nodeIDs[16]]) elem.GetPointIds().SetId(17, nidMap[nodeIDs[17]]) elem.GetPointIds().SetId(18, nidMap[nodeIDs[18]]) elem.GetPointIds().SetId(19, nidMap[nodeIDs[19]]) else: elem = vtkHexahedron() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, LineElement) or isinstance( element, SpringElement): elem = vtk.vtkLine() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) ### elif isinstance(element, CONM2): # not perfectly located nid = element.Nid() c = element.Centroid() elem = vtk.vtkVertex() #elem = vtk.vtkSphere() #elem.SetRadius(1.0) #print str(element) points2.InsertPoint(j, *c) elem.GetPointIds().SetId(0, j) #elem.SetCenter(points.GetPoint(nidMap[nid])) self.grid2.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) j += 1 else: print "skipping %s" % (element.type) ### self.grid.SetPoints(points) self.grid2.SetPoints(points2) self.grid.Update() self.grid2.Update()
aTetraMapper = vtk.vtkDataSetMapper() aTetraMapper.SetInputData(aTetraGrid) aTetraActor = vtk.vtkActor() aTetraActor.SetMapper(aTetraMapper) aTetraActor.AddPosition(4,0,0) aTetraActor.GetProperty().BackfaceCullingOn() # Wedge wedgePoints = vtk.vtkPoints() wedgePoints.SetNumberOfPoints(6) wedgePoints.InsertPoint(0,0,1,0) wedgePoints.InsertPoint(1,0,0,0) wedgePoints.InsertPoint(2,0,.5,.5) wedgePoints.InsertPoint(3,1,1,0) wedgePoints.InsertPoint(4,1,0,0) wedgePoints.InsertPoint(5,1,.5,.5) aWedge = vtk.vtkWedge() aWedge.GetPointIds().SetId(0,0) aWedge.GetPointIds().SetId(1,1) aWedge.GetPointIds().SetId(2,2) aWedge.GetPointIds().SetId(3,3) aWedge.GetPointIds().SetId(4,4) aWedge.GetPointIds().SetId(5,5) aWedgeGrid = vtk.vtkUnstructuredGrid() aWedgeGrid.Allocate(1,1) aWedgeGrid.InsertNextCell(aWedge.GetCellType(),aWedge.GetPointIds()) aWedgeGrid.SetPoints(wedgePoints) aWedgeMapper = vtk.vtkDataSetMapper() aWedgeMapper.SetInputData(aWedgeGrid) aWedgeActor = vtk.vtkActor() aWedgeActor.SetMapper(aWedgeMapper) aWedgeActor.AddPosition(6,0,0)
def testCells(self): # Demonstrates all cell types # # NOTE: the use of NewInstance/DeepCopy is included to increase # regression coverage. It is not required in most applications. ren = vtk.vtkRenderer() # turn off all cullers ren.GetCullers().RemoveAllItems() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren) renWin.SetSize(300, 150) iRen = vtk.vtkRenderWindowInteractor() iRen.SetRenderWindow(renWin) # create a scene with one of each cell type # Voxel voxelPoints = vtk.vtkPoints() voxelPoints.SetNumberOfPoints(8) voxelPoints.InsertPoint(0, 0, 0, 0) voxelPoints.InsertPoint(1, 1, 0, 0) voxelPoints.InsertPoint(2, 0, 1, 0) voxelPoints.InsertPoint(3, 1, 1, 0) voxelPoints.InsertPoint(4, 0, 0, 1) voxelPoints.InsertPoint(5, 1, 0, 1) voxelPoints.InsertPoint(6, 0, 1, 1) voxelPoints.InsertPoint(7, 1, 1, 1) aVoxel = vtk.vtkVoxel() aVoxel.GetPointIds().SetId(0, 0) aVoxel.GetPointIds().SetId(1, 1) aVoxel.GetPointIds().SetId(2, 2) aVoxel.GetPointIds().SetId(3, 3) aVoxel.GetPointIds().SetId(4, 4) aVoxel.GetPointIds().SetId(5, 5) aVoxel.GetPointIds().SetId(6, 6) aVoxel.GetPointIds().SetId(7, 7) bVoxel = aVoxel.NewInstance() bVoxel.DeepCopy(aVoxel) aVoxelGrid = vtk.vtkUnstructuredGrid() aVoxelGrid.Allocate(1, 1) aVoxelGrid.InsertNextCell(aVoxel.GetCellType(), aVoxel.GetPointIds()) aVoxelGrid.SetPoints(voxelPoints) aVoxelMapper = vtk.vtkDataSetMapper() aVoxelMapper.SetInputData(aVoxelGrid) aVoxelActor = vtk.vtkActor() aVoxelActor.SetMapper(aVoxelMapper) aVoxelActor.GetProperty().BackfaceCullingOn() # Hexahedron hexahedronPoints = vtk.vtkPoints() hexahedronPoints.SetNumberOfPoints(8) hexahedronPoints.InsertPoint(0, 0, 0, 0) hexahedronPoints.InsertPoint(1, 1, 0, 0) hexahedronPoints.InsertPoint(2, 1, 1, 0) hexahedronPoints.InsertPoint(3, 0, 1, 0) hexahedronPoints.InsertPoint(4, 0, 0, 1) hexahedronPoints.InsertPoint(5, 1, 0, 1) hexahedronPoints.InsertPoint(6, 1, 1, 1) hexahedronPoints.InsertPoint(7, 0, 1, 1) aHexahedron = vtk.vtkHexahedron() aHexahedron.GetPointIds().SetId(0, 0) aHexahedron.GetPointIds().SetId(1, 1) aHexahedron.GetPointIds().SetId(2, 2) aHexahedron.GetPointIds().SetId(3, 3) aHexahedron.GetPointIds().SetId(4, 4) aHexahedron.GetPointIds().SetId(5, 5) aHexahedron.GetPointIds().SetId(6, 6) aHexahedron.GetPointIds().SetId(7, 7) bHexahedron = aHexahedron.NewInstance() bHexahedron.DeepCopy(aHexahedron) aHexahedronGrid = vtk.vtkUnstructuredGrid() aHexahedronGrid.Allocate(1, 1) aHexahedronGrid.InsertNextCell(aHexahedron.GetCellType(), aHexahedron.GetPointIds()) aHexahedronGrid.SetPoints(hexahedronPoints) aHexahedronMapper = vtk.vtkDataSetMapper() aHexahedronMapper.SetInputData(aHexahedronGrid) aHexahedronActor = vtk.vtkActor() aHexahedronActor.SetMapper(aHexahedronMapper) aHexahedronActor.AddPosition(2, 0, 0) aHexahedronActor.GetProperty().BackfaceCullingOn() # Tetra tetraPoints = vtk.vtkPoints() tetraPoints.SetNumberOfPoints(4) tetraPoints.InsertPoint(0, 0, 0, 0) tetraPoints.InsertPoint(1, 1, 0, 0) tetraPoints.InsertPoint(2, 0.5, 1, 0) tetraPoints.InsertPoint(3, 0.5, 0.5, 1) aTetra = vtk.vtkTetra() aTetra.GetPointIds().SetId(0, 0) aTetra.GetPointIds().SetId(1, 1) aTetra.GetPointIds().SetId(2, 2) aTetra.GetPointIds().SetId(3, 3) bTetra = aTetra.NewInstance() bTetra.DeepCopy(aTetra) aTetraGrid = vtk.vtkUnstructuredGrid() aTetraGrid.Allocate(1, 1) aTetraGrid.InsertNextCell(aTetra.GetCellType(), aTetra.GetPointIds()) aTetraGrid.SetPoints(tetraPoints) aTetraCopy = vtk.vtkUnstructuredGrid() aTetraCopy.ShallowCopy(aTetraGrid) aTetraMapper = vtk.vtkDataSetMapper() aTetraMapper.SetInputData(aTetraCopy) aTetraActor = vtk.vtkActor() aTetraActor.SetMapper(aTetraMapper) aTetraActor.AddPosition(4, 0, 0) aTetraActor.GetProperty().BackfaceCullingOn() # Wedge wedgePoints = vtk.vtkPoints() wedgePoints.SetNumberOfPoints(6) wedgePoints.InsertPoint(0, 0, 1, 0) wedgePoints.InsertPoint(1, 0, 0, 0) wedgePoints.InsertPoint(2, 0, 0.5, 0.5) wedgePoints.InsertPoint(3, 1, 1, 0) wedgePoints.InsertPoint(4, 1, 0, 0) wedgePoints.InsertPoint(5, 1, 0.5, 0.5) aWedge = vtk.vtkWedge() aWedge.GetPointIds().SetId(0, 0) aWedge.GetPointIds().SetId(1, 1) aWedge.GetPointIds().SetId(2, 2) aWedge.GetPointIds().SetId(3, 3) aWedge.GetPointIds().SetId(4, 4) aWedge.GetPointIds().SetId(5, 5) bWedge = aWedge.NewInstance() bWedge.DeepCopy(aWedge) aWedgeGrid = vtk.vtkUnstructuredGrid() aWedgeGrid.Allocate(1, 1) aWedgeGrid.InsertNextCell(aWedge.GetCellType(), aWedge.GetPointIds()) aWedgeGrid.SetPoints(wedgePoints) aWedgeCopy = vtk.vtkUnstructuredGrid() aWedgeCopy.DeepCopy(aWedgeGrid) aWedgeMapper = vtk.vtkDataSetMapper() aWedgeMapper.SetInputData(aWedgeCopy) aWedgeActor = vtk.vtkActor() aWedgeActor.SetMapper(aWedgeMapper) aWedgeActor.AddPosition(6, 0, 0) aWedgeActor.GetProperty().BackfaceCullingOn() # Pyramid pyramidPoints = vtk.vtkPoints() pyramidPoints.SetNumberOfPoints(5) pyramidPoints.InsertPoint(0, 0, 0, 0) pyramidPoints.InsertPoint(1, 1, 0, 0) pyramidPoints.InsertPoint(2, 1, 1, 0) pyramidPoints.InsertPoint(3, 0, 1, 0) pyramidPoints.InsertPoint(4, 0.5, 0.5, 1) aPyramid = vtk.vtkPyramid() aPyramid.GetPointIds().SetId(0, 0) aPyramid.GetPointIds().SetId(1, 1) aPyramid.GetPointIds().SetId(2, 2) aPyramid.GetPointIds().SetId(3, 3) aPyramid.GetPointIds().SetId(4, 4) bPyramid = aPyramid.NewInstance() bPyramid.DeepCopy(aPyramid) aPyramidGrid = vtk.vtkUnstructuredGrid() aPyramidGrid.Allocate(1, 1) aPyramidGrid.InsertNextCell(aPyramid.GetCellType(), aPyramid.GetPointIds()) aPyramidGrid.SetPoints(pyramidPoints) aPyramidMapper = vtk.vtkDataSetMapper() aPyramidMapper.SetInputData(aPyramidGrid) aPyramidActor = vtk.vtkActor() aPyramidActor.SetMapper(aPyramidMapper) aPyramidActor.AddPosition(8, 0, 0) aPyramidActor.GetProperty().BackfaceCullingOn() # Pixel pixelPoints = vtk.vtkPoints() pixelPoints.SetNumberOfPoints(4) pixelPoints.InsertPoint(0, 0, 0, 0) pixelPoints.InsertPoint(1, 1, 0, 0) pixelPoints.InsertPoint(2, 0, 1, 0) pixelPoints.InsertPoint(3, 1, 1, 0) aPixel = vtk.vtkPixel() aPixel.GetPointIds().SetId(0, 0) aPixel.GetPointIds().SetId(1, 1) aPixel.GetPointIds().SetId(2, 2) aPixel.GetPointIds().SetId(3, 3) bPixel = aPixel.NewInstance() bPixel.DeepCopy(aPixel) aPixelGrid = vtk.vtkUnstructuredGrid() aPixelGrid.Allocate(1, 1) aPixelGrid.InsertNextCell(aPixel.GetCellType(), aPixel.GetPointIds()) aPixelGrid.SetPoints(pixelPoints) aPixelMapper = vtk.vtkDataSetMapper() aPixelMapper.SetInputData(aPixelGrid) aPixelActor = vtk.vtkActor() aPixelActor.SetMapper(aPixelMapper) aPixelActor.AddPosition(0, 0, 2) aPixelActor.GetProperty().BackfaceCullingOn() # Quad quadPoints = vtk.vtkPoints() quadPoints.SetNumberOfPoints(4) quadPoints.InsertPoint(0, 0, 0, 0) quadPoints.InsertPoint(1, 1, 0, 0) quadPoints.InsertPoint(2, 1, 1, 0) quadPoints.InsertPoint(3, 0, 1, 0) aQuad = vtk.vtkQuad() aQuad.GetPointIds().SetId(0, 0) aQuad.GetPointIds().SetId(1, 1) aQuad.GetPointIds().SetId(2, 2) aQuad.GetPointIds().SetId(3, 3) bQuad = aQuad.NewInstance() bQuad.DeepCopy(aQuad) aQuadGrid = vtk.vtkUnstructuredGrid() aQuadGrid.Allocate(1, 1) aQuadGrid.InsertNextCell(aQuad.GetCellType(), aQuad.GetPointIds()) aQuadGrid.SetPoints(quadPoints) aQuadMapper = vtk.vtkDataSetMapper() aQuadMapper.SetInputData(aQuadGrid) aQuadActor = vtk.vtkActor() aQuadActor.SetMapper(aQuadMapper) aQuadActor.AddPosition(2, 0, 2) aQuadActor.GetProperty().BackfaceCullingOn() # Triangle trianglePoints = vtk.vtkPoints() trianglePoints.SetNumberOfPoints(3) trianglePoints.InsertPoint(0, 0, 0, 0) trianglePoints.InsertPoint(1, 1, 0, 0) trianglePoints.InsertPoint(2, 0.5, 0.5, 0) triangleTCoords = vtk.vtkFloatArray() triangleTCoords.SetNumberOfComponents(2) triangleTCoords.SetNumberOfTuples(3) triangleTCoords.InsertTuple2(0, 1, 1) triangleTCoords.InsertTuple2(1, 2, 2) triangleTCoords.InsertTuple2(2, 3, 3) aTriangle = vtk.vtkTriangle() aTriangle.GetPointIds().SetId(0, 0) aTriangle.GetPointIds().SetId(1, 1) aTriangle.GetPointIds().SetId(2, 2) bTriangle = aTriangle.NewInstance() bTriangle.DeepCopy(aTriangle) aTriangleGrid = vtk.vtkUnstructuredGrid() aTriangleGrid.Allocate(1, 1) aTriangleGrid.InsertNextCell(aTriangle.GetCellType(), aTriangle.GetPointIds()) aTriangleGrid.SetPoints(trianglePoints) aTriangleGrid.GetPointData().SetTCoords(triangleTCoords) aTriangleMapper = vtk.vtkDataSetMapper() aTriangleMapper.SetInputData(aTriangleGrid) aTriangleActor = vtk.vtkActor() aTriangleActor.SetMapper(aTriangleMapper) aTriangleActor.AddPosition(4, 0, 2) aTriangleActor.GetProperty().BackfaceCullingOn() # Polygon polygonPoints = vtk.vtkPoints() polygonPoints.SetNumberOfPoints(4) polygonPoints.InsertPoint(0, 0, 0, 0) polygonPoints.InsertPoint(1, 1, 0, 0) polygonPoints.InsertPoint(2, 1, 1, 0) polygonPoints.InsertPoint(3, 0, 1, 0) aPolygon = vtk.vtkPolygon() aPolygon.GetPointIds().SetNumberOfIds(4) aPolygon.GetPointIds().SetId(0, 0) aPolygon.GetPointIds().SetId(1, 1) aPolygon.GetPointIds().SetId(2, 2) aPolygon.GetPointIds().SetId(3, 3) bPolygon = aPolygon.NewInstance() bPolygon.DeepCopy(aPolygon) aPolygonGrid = vtk.vtkUnstructuredGrid() aPolygonGrid.Allocate(1, 1) aPolygonGrid.InsertNextCell(aPolygon.GetCellType(), aPolygon.GetPointIds()) aPolygonGrid.SetPoints(polygonPoints) aPolygonMapper = vtk.vtkDataSetMapper() aPolygonMapper.SetInputData(aPolygonGrid) aPolygonActor = vtk.vtkActor() aPolygonActor.SetMapper(aPolygonMapper) aPolygonActor.AddPosition(6, 0, 2) aPolygonActor.GetProperty().BackfaceCullingOn() # Triangle Strip triangleStripPoints = vtk.vtkPoints() triangleStripPoints.SetNumberOfPoints(5) triangleStripPoints.InsertPoint(0, 0, 1, 0) triangleStripPoints.InsertPoint(1, 0, 0, 0) triangleStripPoints.InsertPoint(2, 1, 1, 0) triangleStripPoints.InsertPoint(3, 1, 0, 0) triangleStripPoints.InsertPoint(4, 2, 1, 0) triangleStripTCoords = vtk.vtkFloatArray() triangleStripTCoords.SetNumberOfComponents(2) triangleStripTCoords.SetNumberOfTuples(3) triangleStripTCoords.InsertTuple2(0, 1, 1) triangleStripTCoords.InsertTuple2(1, 2, 2) triangleStripTCoords.InsertTuple2(2, 3, 3) triangleStripTCoords.InsertTuple2(3, 4, 4) triangleStripTCoords.InsertTuple2(4, 5, 5) aTriangleStrip = vtk.vtkTriangleStrip() aTriangleStrip.GetPointIds().SetNumberOfIds(5) aTriangleStrip.GetPointIds().SetId(0, 0) aTriangleStrip.GetPointIds().SetId(1, 1) aTriangleStrip.GetPointIds().SetId(2, 2) aTriangleStrip.GetPointIds().SetId(3, 3) aTriangleStrip.GetPointIds().SetId(4, 4) bTriangleStrip = aTriangleStrip.NewInstance() bTriangleStrip.DeepCopy(aTriangleStrip) aTriangleStripGrid = vtk.vtkUnstructuredGrid() aTriangleStripGrid.Allocate(1, 1) aTriangleStripGrid.InsertNextCell(aTriangleStrip.GetCellType(), aTriangleStrip.GetPointIds()) aTriangleStripGrid.SetPoints(triangleStripPoints) aTriangleStripGrid.GetPointData().SetTCoords(triangleStripTCoords) aTriangleStripMapper = vtk.vtkDataSetMapper() aTriangleStripMapper.SetInputData(aTriangleStripGrid) aTriangleStripActor = vtk.vtkActor() aTriangleStripActor.SetMapper(aTriangleStripMapper) aTriangleStripActor.AddPosition(8, 0, 2) aTriangleStripActor.GetProperty().BackfaceCullingOn() # Line linePoints = vtk.vtkPoints() linePoints.SetNumberOfPoints(2) linePoints.InsertPoint(0, 0, 0, 0) linePoints.InsertPoint(1, 1, 1, 0) aLine = vtk.vtkLine() aLine.GetPointIds().SetId(0, 0) aLine.GetPointIds().SetId(1, 1) bLine = aLine.NewInstance() bLine.DeepCopy(aLine) aLineGrid = vtk.vtkUnstructuredGrid() aLineGrid.Allocate(1, 1) aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds()) aLineGrid.SetPoints(linePoints) aLineMapper = vtk.vtkDataSetMapper() aLineMapper.SetInputData(aLineGrid) aLineActor = vtk.vtkActor() aLineActor.SetMapper(aLineMapper) aLineActor.AddPosition(0, 0, 4) aLineActor.GetProperty().BackfaceCullingOn() # Poly line polyLinePoints = vtk.vtkPoints() polyLinePoints.SetNumberOfPoints(3) polyLinePoints.InsertPoint(0, 0, 0, 0) polyLinePoints.InsertPoint(1, 1, 1, 0) polyLinePoints.InsertPoint(2, 1, 0, 0) aPolyLine = vtk.vtkPolyLine() aPolyLine.GetPointIds().SetNumberOfIds(3) aPolyLine.GetPointIds().SetId(0, 0) aPolyLine.GetPointIds().SetId(1, 1) aPolyLine.GetPointIds().SetId(2, 2) bPolyLine = aPolyLine.NewInstance() bPolyLine.DeepCopy(aPolyLine) aPolyLineGrid = vtk.vtkUnstructuredGrid() aPolyLineGrid.Allocate(1, 1) aPolyLineGrid.InsertNextCell(aPolyLine.GetCellType(), aPolyLine.GetPointIds()) aPolyLineGrid.SetPoints(polyLinePoints) aPolyLineMapper = vtk.vtkDataSetMapper() aPolyLineMapper.SetInputData(aPolyLineGrid) aPolyLineActor = vtk.vtkActor() aPolyLineActor.SetMapper(aPolyLineMapper) aPolyLineActor.AddPosition(2, 0, 4) aPolyLineActor.GetProperty().BackfaceCullingOn() # Vertex vertexPoints = vtk.vtkPoints() vertexPoints.SetNumberOfPoints(1) vertexPoints.InsertPoint(0, 0, 0, 0) aVertex = vtk.vtkVertex() aVertex.GetPointIds().SetId(0, 0) bVertex = aVertex.NewInstance() bVertex.DeepCopy(aVertex) aVertexGrid = vtk.vtkUnstructuredGrid() aVertexGrid.Allocate(1, 1) aVertexGrid.InsertNextCell(aVertex.GetCellType(), aVertex.GetPointIds()) aVertexGrid.SetPoints(vertexPoints) aVertexMapper = vtk.vtkDataSetMapper() aVertexMapper.SetInputData(aVertexGrid) aVertexActor = vtk.vtkActor() aVertexActor.SetMapper(aVertexMapper) aVertexActor.AddPosition(0, 0, 6) aVertexActor.GetProperty().BackfaceCullingOn() # Poly Vertex polyVertexPoints = vtk.vtkPoints() polyVertexPoints.SetNumberOfPoints(3) polyVertexPoints.InsertPoint(0, 0, 0, 0) polyVertexPoints.InsertPoint(1, 1, 0, 0) polyVertexPoints.InsertPoint(2, 1, 1, 0) aPolyVertex = vtk.vtkPolyVertex() aPolyVertex.GetPointIds().SetNumberOfIds(3) aPolyVertex.GetPointIds().SetId(0, 0) aPolyVertex.GetPointIds().SetId(1, 1) aPolyVertex.GetPointIds().SetId(2, 2) bPolyVertex = aPolyVertex.NewInstance() bPolyVertex.DeepCopy(aPolyVertex) aPolyVertexGrid = vtk.vtkUnstructuredGrid() aPolyVertexGrid.Allocate(1, 1) aPolyVertexGrid.InsertNextCell(aPolyVertex.GetCellType(), aPolyVertex.GetPointIds()) aPolyVertexGrid.SetPoints(polyVertexPoints) aPolyVertexMapper = vtk.vtkDataSetMapper() aPolyVertexMapper.SetInputData(aPolyVertexGrid) aPolyVertexActor = vtk.vtkActor() aPolyVertexActor.SetMapper(aPolyVertexMapper) aPolyVertexActor.AddPosition(2, 0, 6) aPolyVertexActor.GetProperty().BackfaceCullingOn() # Pentagonal prism pentaPoints = vtk.vtkPoints() pentaPoints.SetNumberOfPoints(10) pentaPoints.InsertPoint(0, 0.25, 0.0, 0.0) pentaPoints.InsertPoint(1, 0.75, 0.0, 0.0) pentaPoints.InsertPoint(2, 1.0, 0.5, 0.0) pentaPoints.InsertPoint(3, 0.5, 1.0, 0.0) pentaPoints.InsertPoint(4, 0.0, 0.5, 0.0) pentaPoints.InsertPoint(5, 0.25, 0.0, 1.0) pentaPoints.InsertPoint(6, 0.75, 0.0, 1.0) pentaPoints.InsertPoint(7, 1.0, 0.5, 1.0) pentaPoints.InsertPoint(8, 0.5, 1.0, 1.0) pentaPoints.InsertPoint(9, 0.0, 0.5, 1.0) aPenta = vtk.vtkPentagonalPrism() aPenta.GetPointIds().SetId(0, 0) aPenta.GetPointIds().SetId(1, 1) aPenta.GetPointIds().SetId(2, 2) aPenta.GetPointIds().SetId(3, 3) aPenta.GetPointIds().SetId(4, 4) aPenta.GetPointIds().SetId(5, 5) aPenta.GetPointIds().SetId(6, 6) aPenta.GetPointIds().SetId(7, 7) aPenta.GetPointIds().SetId(8, 8) aPenta.GetPointIds().SetId(9, 9) bPenta = aPenta.NewInstance() bPenta.DeepCopy(aPenta) aPentaGrid = vtk.vtkUnstructuredGrid() aPentaGrid.Allocate(1, 1) aPentaGrid.InsertNextCell(aPenta.GetCellType(), aPenta.GetPointIds()) aPentaGrid.SetPoints(pentaPoints) aPentaCopy = vtk.vtkUnstructuredGrid() aPentaCopy.DeepCopy(aPentaGrid) aPentaMapper = vtk.vtkDataSetMapper() aPentaMapper.SetInputData(aPentaCopy) aPentaActor = vtk.vtkActor() aPentaActor.SetMapper(aPentaMapper) aPentaActor.AddPosition(10, 0, 0) aPentaActor.GetProperty().BackfaceCullingOn() # Hexagonal prism hexaPoints = vtk.vtkPoints() hexaPoints.SetNumberOfPoints(12) hexaPoints.InsertPoint(0, 0.0, 0.0, 0.0) hexaPoints.InsertPoint(1, 0.5, 0.0, 0.0) hexaPoints.InsertPoint(2, 1.0, 0.5, 0.0) hexaPoints.InsertPoint(3, 1.0, 1.0, 0.0) hexaPoints.InsertPoint(4, 0.5, 1.0, 0.0) hexaPoints.InsertPoint(5, 0.0, 0.5, 0.0) hexaPoints.InsertPoint(6, 0.0, 0.0, 1.0) hexaPoints.InsertPoint(7, 0.5, 0.0, 1.0) hexaPoints.InsertPoint(8, 1.0, 0.5, 1.0) hexaPoints.InsertPoint(9, 1.0, 1.0, 1.0) hexaPoints.InsertPoint(10, 0.5, 1.0, 1.0) hexaPoints.InsertPoint(11, 0.0, 0.5, 1.0) aHexa = vtk.vtkHexagonalPrism() aHexa.GetPointIds().SetId(0, 0) aHexa.GetPointIds().SetId(1, 1) aHexa.GetPointIds().SetId(2, 2) aHexa.GetPointIds().SetId(3, 3) aHexa.GetPointIds().SetId(4, 4) aHexa.GetPointIds().SetId(5, 5) aHexa.GetPointIds().SetId(6, 6) aHexa.GetPointIds().SetId(7, 7) aHexa.GetPointIds().SetId(8, 8) aHexa.GetPointIds().SetId(9, 9) aHexa.GetPointIds().SetId(10, 10) aHexa.GetPointIds().SetId(11, 11) bHexa = aHexa.NewInstance() bHexa.DeepCopy(aHexa) aHexaGrid = vtk.vtkUnstructuredGrid() aHexaGrid.Allocate(1, 1) aHexaGrid.InsertNextCell(aHexa.GetCellType(), aHexa.GetPointIds()) aHexaGrid.SetPoints(hexaPoints) aHexaCopy = vtk.vtkUnstructuredGrid() aHexaCopy.DeepCopy(aHexaGrid) aHexaMapper = vtk.vtkDataSetMapper() aHexaMapper.SetInputData(aHexaCopy) aHexaActor = vtk.vtkActor() aHexaActor.SetMapper(aHexaMapper) aHexaActor.AddPosition(12, 0, 0) aHexaActor.GetProperty().BackfaceCullingOn() # RIB property aRIBProperty = vtk.vtkRIBProperty() aRIBProperty.SetVariable("Km", "float") aRIBProperty.SetSurfaceShader("LGVeinedmarble") aRIBProperty.SetVariable("veinfreq", "float") aRIBProperty.AddVariable("warpfreq", "float") aRIBProperty.AddVariable("veincolor", "color") aRIBProperty.AddParameter("veinfreq", " 2") aRIBProperty.AddParameter("veincolor", "1.0000 1.0000 0.9412") bRIBProperty = vtk.vtkRIBProperty() bRIBProperty.SetVariable("Km", "float") bRIBProperty.SetParameter("Km", "1.0") bRIBProperty.SetDisplacementShader("dented") bRIBProperty.SetSurfaceShader("plastic") aProperty = vtk.vtkProperty() bProperty = vtk.vtkProperty() aTriangleActor.SetProperty(aProperty) aTriangleStripActor.SetProperty(bProperty) ren.SetBackground(0.1, 0.2, 0.4) ren.AddActor(aVoxelActor) aVoxelActor.GetProperty().SetDiffuseColor(1, 0, 0) ren.AddActor(aHexahedronActor) aHexahedronActor.GetProperty().SetDiffuseColor(1, 1, 0) ren.AddActor(aTetraActor) aTetraActor.GetProperty().SetDiffuseColor(0, 1, 0) ren.AddActor(aWedgeActor) aWedgeActor.GetProperty().SetDiffuseColor(0, 1, 1) ren.AddActor(aPyramidActor) aPyramidActor.GetProperty().SetDiffuseColor(1, 0, 1) ren.AddActor(aPixelActor) aPixelActor.GetProperty().SetDiffuseColor(0, 1, 1) ren.AddActor(aQuadActor) aQuadActor.GetProperty().SetDiffuseColor(1, 0, 1) ren.AddActor(aTriangleActor) aTriangleActor.GetProperty().SetDiffuseColor(0.3, 1, 0.5) ren.AddActor(aPolygonActor) aPolygonActor.GetProperty().SetDiffuseColor(1, 0.4, 0.5) ren.AddActor(aTriangleStripActor) aTriangleStripActor.GetProperty().SetDiffuseColor(0.3, 0.7, 1) ren.AddActor(aLineActor) aLineActor.GetProperty().SetDiffuseColor(0.2, 1, 1) ren.AddActor(aPolyLineActor) aPolyLineActor.GetProperty().SetDiffuseColor(1, 1, 1) ren.AddActor(aVertexActor) aVertexActor.GetProperty().SetDiffuseColor(1, 1, 1) ren.AddActor(aPolyVertexActor) aPolyVertexActor.GetProperty().SetDiffuseColor(1, 1, 1) ren.AddActor(aPentaActor) aPentaActor.GetProperty().SetDiffuseColor(0.2, 0.4, 0.7) ren.AddActor(aHexaActor) aHexaActor.GetProperty().SetDiffuseColor(0.7, 0.5, 1) aRIBLight = vtk.vtkRIBLight() aRIBLight.ShadowsOn() aLight = vtk.vtkLight() aLight.PositionalOn() aLight.SetConeAngle(25) ren.AddLight(aLight) ren.ResetCamera() ren.GetActiveCamera().Azimuth(30) ren.GetActiveCamera().Elevation(20) ren.GetActiveCamera().Dolly(2.8) ren.ResetCameraClippingRange() aLight.SetFocalPoint(ren.GetActiveCamera().GetFocalPoint()) aLight.SetPosition(ren.GetActiveCamera().GetPosition()) # write to the temp directory if possible, otherwise use . dir = tempfile.gettempdir() atext = vtk.vtkTexture() pnmReader = vtk.vtkBMPReader() pnmReader.SetFileName(VTK_DATA_ROOT + "/Data/masonry.bmp") atext.SetInputConnection(pnmReader.GetOutputPort()) atext.InterpolateOff() aTriangleActor.SetTexture(atext) rib = vtk.vtkRIBExporter() rib.SetInput(renWin) rib.SetFilePrefix(dir + "/cells") rib.SetTexturePrefix(dir + "/cells") rib.Write() os.remove(dir + "/cells.rib") iv = vtk.vtkIVExporter() iv.SetInput(renWin) iv.SetFileName(dir + "/cells.iv") iv.Write() os.remove(dir + "/cells.iv") obj = vtk.vtkOBJExporter() obj.SetInput(renWin) obj.SetFilePrefix(dir + "/cells") obj.Write() os.remove(dir + "/cells.obj") os.remove(dir + "/cells.mtl") vrml = vtk.vtkVRMLExporter() vrml.SetInput(renWin) # vrml.SetStartWrite(vrml.SetFileName(dir + "/cells.wrl")) # vrml.SetEndWrite(vrml.SetFileName("/a/acells.wrl")) vrml.SetFileName(dir + "/cells.wrl") vrml.SetSpeed(5.5) vrml.Write() os.remove(dir + "/cells.wrl") oogl = vtk.vtkOOGLExporter() oogl.SetInput(renWin) oogl.SetFileName(dir + "/cells.oogl") oogl.Write() os.remove(dir + "/cells.oogl") # the UnRegister calls are because make object is the same as New, # and causes memory leaks. (Python does not treat NewInstance the same as New). def DeleteCopies(): bVoxel.UnRegister(None) bHexahedron.UnRegister(None) bTetra.UnRegister(None) bWedge.UnRegister(None) bPyramid.UnRegister(None) bPixel.UnRegister(None) bQuad.UnRegister(None) bTriangle.UnRegister(None) bPolygon.UnRegister(None) bTriangleStrip.UnRegister(None) bLine.UnRegister(None) bPolyLine.UnRegister(None) bVertex.UnRegister(None) bPolyVertex.UnRegister(None) bPenta.UnRegister(None) bHexa.UnRegister(None) DeleteCopies() # render and interact with data renWin.Render() img_file = "cells.png" vtk.test.Testing.compareImage(iRen.GetRenderWindow(), vtk.test.Testing.getAbsImagePath(img_file), threshold=25) vtk.test.Testing.interact()
def mapElements(self, points, points2, nidMap, model, j): #self.eidMap = {} i = 0 for (eid, element) in sorted(model.elements.iteritems()): self.eidMap[eid] = i #print element.type if isinstance(element, CTRIA3) or isinstance(element, CTRIAR): elem = vtkTriangle() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTRIA6): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticTriangle() elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) else: elem = vtkTriangle() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTRIAX6): # midside nodes are required, nodes out of order nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticTriangle() elem.GetPointIds().SetId(3, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) else: elem = vtkTriangle() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[4]]) #a = [0,2,4] #msg = "CTRIAX6 %i %i %i" %(nidMap[nodeIDs[a[0]]], # nidMap[nodeIDs[a[1]]], # nidMap[nodeIDs[a[2]]] ) #raise RuntimeError(msg) #sys.stdout.flush() #elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) #elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) #elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif (isinstance(element, CQUAD4) or isinstance(element, CSHEAR) or isinstance(element, CQUADR)): nodeIDs = element.nodeIDs() elem = vtkQuad() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CQUAD8): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticQuad() elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) else: elem = vtkQuad() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTETRA4): elem = vtkTetra() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTETRA10): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticTetra() elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) else: elem = vtkTetra() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CPENTA6): elem = vtkWedge() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CPENTA15): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticWedge() elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) elem.GetPointIds().SetId(10, nidMap[nodeIDs[10]]) elem.GetPointIds().SetId(11, nidMap[nodeIDs[11]]) elem.GetPointIds().SetId(12, nidMap[nodeIDs[12]]) elem.GetPointIds().SetId(13, nidMap[nodeIDs[13]]) elem.GetPointIds().SetId(14, nidMap[nodeIDs[14]]) else: elem = vtkWedge() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CHEXA8): nodeIDs = element.nodeIDs() elem = vtkHexahedron() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CHEXA20): nodeIDs = element.nodeIDs() #print "nodeIDs = ",nodeIDs if None not in nodeIDs: elem = vtkQuadraticHexahedron() elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) elem.GetPointIds().SetId(10, nidMap[nodeIDs[10]]) elem.GetPointIds().SetId(11, nidMap[nodeIDs[11]]) elem.GetPointIds().SetId(12, nidMap[nodeIDs[12]]) elem.GetPointIds().SetId(13, nidMap[nodeIDs[13]]) elem.GetPointIds().SetId(14, nidMap[nodeIDs[14]]) elem.GetPointIds().SetId(15, nidMap[nodeIDs[15]]) elem.GetPointIds().SetId(16, nidMap[nodeIDs[16]]) elem.GetPointIds().SetId(17, nidMap[nodeIDs[17]]) elem.GetPointIds().SetId(18, nidMap[nodeIDs[18]]) elem.GetPointIds().SetId(19, nidMap[nodeIDs[19]]) else: elem = vtkHexahedron() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif (isinstance(element, LineElement) or isinstance(element, SpringElement) or element.type in ['CBUSH', 'CBUSH1D', 'CFAST', 'CROD', 'CONROD', 'CELAS1', 'CELAS2', 'CELAS3', 'CELAS4', 'CDAMP1', 'CDAMP2', 'CDAMP3', 'CDAMP4', 'CDAMP5', 'CVISC', ]): nodeIDs = element.nodeIDs() if None not in nodeIDs: # used to be 0... elem = vtk.vtkLine() try: elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) except KeyError: print "nodeIDs =", nodeIDs print str(element) continue self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CONM2): # not perfectly located del self.eidMap[eid] i -= 1 #nid = element.Nid() c = element.Centroid() elem = vtk.vtkVertex() #elem = vtk.vtkSphere() #elem.SetRadius(1.0) #print str(element) points2.InsertPoint(j, *c) elem.GetPointIds().SetId(0, j) #elem.SetCenter(points.GetPoint(nidMap[nid])) self.grid2.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) j += 1 else: del self.eidMap[eid] self.log_info("skipping %s" % element.type) continue i += 1 self.grid.SetPoints(points) self.grid2.SetPoints(points2) #self.grid.GetPointData().SetScalars(self.gridResult) #print dir(self.grid) #.SetNumberOfComponents(0) #self.grid.GetCellData().SetNumberOfTuples(1); #self.grid.GetCellData().SetScalars(self.gridResult) self.grid.Modified() self.grid2.Modified() self.grid.Update() self.grid2.Update() self.log_info("updated grid") cases = {} nelements = len(model.elements) print "len(elements) =", nelements pids = [] # zeros(nelements, 'int32') nxs = [] nys = [] nzs = [] i = 0 for eid, element in sorted(model.elements.iteritems()): pids.append(element.Pid()) if isinstance(element, ShellElement): (nx, ny, nz) = element.Normal() else: nx = ny = nz = 0.0 nxs.append(nx) nys.append(ny) nzs.append(nz) self.iSubcaseNameMap = {1: ['Nastran', '']} # subcaseID, resultType, vectorSize, location, dataFormat if 1: cases[(0, 'Pid', 1, 'centroid', '%.0f')] = pids # if not a flat plate??? #if min(nxs) == max(nxs) and min(nxs) != 0.0: # subcaseID, resultType, vectorSize, location, dataFormat cases[(0, 'Normal_x', 1, 'centroid', '%.1f')] = nxs cases[(0, 'Normal_y', 1, 'centroid', '%.1f')] = nys cases[(0, 'Normal_z', 1, 'centroid', '%.1f')] = nzs self.log.info(cases.keys()) self.finish_io(cases)
def from_fea(mesh: pywim.fea.model.Mesh, inc: pywim.fea.result.Increment, outputs: List[str]): points = vtk.vtkPoints() points.SetNumberOfPoints(len(mesh.nodes)) for n in mesh.nodes: points.SetPoint(n.id - 1, n.x, n.y, n.z) grid = vtk.vtkUnstructuredGrid() grid.SetPoints(points) nels = 0 for g in mesh.elements: for c in g.connectivity: if g.type == 'HEXL8' or g.type == 'VOXL' or g.type == 'VOXLA': e = vtk.vtkHexahedron() elif g.type == 'TETL4': e = vtk.vtkTetra() elif g.type == 'WEDL6': e = vtk.vtkWedge() elif g.type == 'PSL4': e = vtk.vtkQuad() elif g.type == 'PSL3': e = vtk.vtkTriangle() elif g.type == 'PSQ6': e = vtk.vtkQuadraticTriangle() else: raise Exception('Unsupported element type: %s' % g.type) ids = e.GetPointIds() i = 0 for nid in c.nodes: ids.SetId(i, nid - 1) i += 1 grid.InsertNextCell(e.GetCellType(), ids) nels += 1 celldata = grid.GetCellData() pointdata = grid.GetPointData() def add_node_results(res: List[pywim.fea.result.Result]): array = vtk.vtkFloatArray() array.SetName(res.name) array.SetNumberOfComponents(res.size) for v in res.values: if res.size == 1: array.InsertNextTuple1(v.data[0]) elif res.size == 3: array.InsertNextTuple3(v.data[0], v.data[1], v.data[2]) elif res.size == 6: array.InsertNextTuple6(v.data[0], v.data[1], v.data[2], v.data[3], v.data[5], v.data[4]) pointdata.AddArray(array) def add_gp_results(res: List[pywim.fea.result.ResultMult]): ngps = 1 for v in res.values: ngps = max(ngps, len(v.values)) # Build a dictionary of element Id to index for quicker searching eid2index = {} index = 0 nlayers = 0 nsectpts = 0 nonlay_ngps = 0 for v in res.values: eid2index[v.id] = index index += 1 if v.values[0].layer == 0: nonlay_ngps = max(nonlay_ngps, len(v.values)) else: nlayers = max(nlayers, max([sv.layer for sv in v.values])) nsectpts = max(nsectpts, max([sv.section_point for sv in v.values])) lay_gp_iter = None nonlay_gp_iter = list(range(nonlay_ngps)) if nlayers > 0: ngps = int(round(ngps / (nlayers * nsectpts))) lay_gp_iter = [] for l in range(nlayers): for k in range(nsectpts): for g in range(ngps): lay_gp_iter.append((l, k, g)) # This is a severe limitation right now: # For layered data we are assuming all elements have the same number of # layers and section points, but we do check each element for total # gauss # pts to handle differences (e.g. WEDL6 vs HEXL8) def get_gauss_point_data(layered_output, elv, gpid): if not layered_output: g = gpid g_out_of_range = elv.values[0].layer > 0 else: this_ngps = int(round(len(elv.values) / (nlayers * nsectpts))) g = this_ngps * (gpid[0] * nsectpts + gpid[1]) + gpid[2] g_out_of_range = gpid[2] >= this_ngps or elv.values[ 0].layer == 0 if len(elv.values) < (g + 1) or g_out_of_range: return [0., 0., 0., 0., 0., 0.] else: return elv.values[g].data for gp_iter in (nonlay_gp_iter, lay_gp_iter): if gp_iter is None: continue for gp in gp_iter: if type(gp) is int: layered_output = False out_name = '{}_G{}'.format(res.name, gp + 1) else: layered_output = True out_name = '{}_L{}_K{}_G{}'.format(res.name, gp[0] + 1, gp[1] + 1, gp[2] + 1) array = vtk.vtkFloatArray() array.SetName(out_name) array.SetNumberOfComponents(res.size) for eid in range(1, nels + 1): elv = res.values[eid2index[eid]] if elv.id != eid: print('Element id mismatch: {} != {}'.format( eid, elv.id)) gpdata = get_gauss_point_data(layered_output, elv, gp) # last two vals intentionally swapped because VTU ordering is XX, YY, ZZ, XY, YZ, XZ if res.size == 1: array.InsertNextTuple1(gpdata[0]) elif res.size == 3: array.InsertNextTuple3(gpdata[0], gpdata[1], gpdata[2]) elif res.size == 6: array.InsertNextTuple6(gpdata[0], gpdata[1], gpdata[2], gpdata[3], gpdata[5], gpdata[4]) celldata.AddArray(array) def add_elem_results(res: List[pywim.fea.result.Result]): array = vtk.vtkFloatArray() array.SetName(res.name) array.SetNumberOfComponents(res.size) for e in res.values: if res.size == 1: array.InsertNextTuple1(e.data[0]) elif res.size == 3: array.InsertNextTuple3(e.data[0], e.data[1], e.data[2]) elif res.size == 6: array.InsertNextTuple6(e.data[0], e.data[1], e.data[2], e.data[3], e.data[5], e.data[4]) celldata.AddArray(array) def add_region_results_by_element(reg_result: RegionResult): elem_stats = reg_result.element_stats() wall_array = vtk.vtkFloatArray() wall_array.SetName('{}_wall'.format(reg_result.name)) wall_array.SetNumberOfComponents(3) skin_array = vtk.vtkFloatArray() skin_array.SetName('{}_skin'.format(reg_result.name)) skin_array.SetNumberOfComponents(3) infill_array = vtk.vtkFloatArray() infill_array.SetName('{}_infill'.format(reg_result.name)) infill_array.SetNumberOfComponents(3) for e in elem_stats.element_regions: if len(e.walls.gauss_point_data) > 0: wall_array.InsertNextTuple3(e.walls.min, e.walls.mean, e.walls.max) else: wall_array.InsertNextTuple3(float('NaN'), float('NaN'), float('NaN')) if len(e.skin.gauss_point_data) > 0: skin_array.InsertNextTuple3(e.skin.min, e.skin.mean, e.skin.max) else: skin_array.InsertNextTuple3(float('NaN'), float('NaN'), float('NaN')) if len(e.infill.gauss_point_data) > 0: infill_array.InsertNextTuple3(e.infill.min, e.infill.mean, e.infill.max) else: infill_array.InsertNextTuple3(float('NaN'), float('NaN'), float('NaN')) celldata.AddArray(wall_array) celldata.AddArray(skin_array) celldata.AddArray(infill_array) if 'node' in outputs: for res in inc.node_results: print('\t\tTranslating {} Node Result'.format(res.name)) add_node_results(res) if 'element' in outputs: for res in inc.element_results: print('\t\tTranslating {} Element Result'.format(res.name)) add_elem_results(res) ''' For now, region specific results are restricted to gauss point data. ''' if 'region' in outputs: try: mat_type = inc.gauss_point_results['material_type'] except StopIteration: raise Exception( 'No material_type information at the gauss points exists. Unable to detect regions.' ) for res in inc.gauss_point_results: if res.name == 'material_type': continue reg_result = region_filter(mat_type, res) if reg_result is None: print( '\tRegion filter on {} gauss point result not supported, skipping this result' .format(res.name)) continue print('\t\tTranslating {} Region Result By Element'.format( res.name)) add_region_results_by_element(reg_result) if 'gauss_point' in outputs: for res in inc.gauss_point_results: if res.name == 'material_type': continue print('\t\tTranslating {} Gauss Point Result'.format(res.name)) add_gp_results(res) return grid
def mapElements(self, points, points2, nidMap, model, j): self.eidMap = {} i = 0 for (eid, element) in sorted(model.elements.iteritems()): self.eidMap[eid] = i #print element.type if isinstance(element, CTRIA3) or isinstance(element, CTRIAR): #print "ctria3" elem = vtkTriangle() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTRIA6): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticTriangle() elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) else: elem = vtkTriangle() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTRIAX6): # midside nodes are required, nodes out of order nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticTriangle() elem.GetPointIds().SetId(3, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) else: elem = vtkTriangle() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[4]]) #a = [0,2,4] #msg = "CTRIAX6 %i %i %i" %(nidMap[nodeIDs[a[0]]], # nidMap[nodeIDs[a[1]]], # nidMap[nodeIDs[a[2]]] ) #raise RuntimeError(msg) #sys.stdout.flush() #elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) #elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) #elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif (isinstance(element, CQUAD4) or isinstance(element, CSHEAR) or isinstance(element, CQUADR)): nodeIDs = element.nodeIDs() elem = vtkQuad() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CQUAD8): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticQuad() elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) else: elem = vtkQuad() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTETRA4): elem = vtkTetra() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTETRA10): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticTetra() elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) else: elem = vtkTetra() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CPENTA6): elem = vtkWedge() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CPENTA15): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticWedge() elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) elem.GetPointIds().SetId(10, nidMap[nodeIDs[10]]) elem.GetPointIds().SetId(11, nidMap[nodeIDs[11]]) elem.GetPointIds().SetId(12, nidMap[nodeIDs[12]]) elem.GetPointIds().SetId(13, nidMap[nodeIDs[13]]) elem.GetPointIds().SetId(14, nidMap[nodeIDs[14]]) else: elem = vtkWedge() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CHEXA8): nodeIDs = element.nodeIDs() elem = vtkHexahedron() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CHEXA20): nodeIDs = element.nodeIDs() #print "nodeIDs = ",nodeIDs if None not in nodeIDs: elem = vtkQuadraticHexahedron() elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) elem.GetPointIds().SetId(10, nidMap[nodeIDs[10]]) elem.GetPointIds().SetId(11, nidMap[nodeIDs[11]]) elem.GetPointIds().SetId(12, nidMap[nodeIDs[12]]) elem.GetPointIds().SetId(13, nidMap[nodeIDs[13]]) elem.GetPointIds().SetId(14, nidMap[nodeIDs[14]]) elem.GetPointIds().SetId(15, nidMap[nodeIDs[15]]) elem.GetPointIds().SetId(16, nidMap[nodeIDs[16]]) elem.GetPointIds().SetId(17, nidMap[nodeIDs[17]]) elem.GetPointIds().SetId(18, nidMap[nodeIDs[18]]) elem.GetPointIds().SetId(19, nidMap[nodeIDs[19]]) else: elem = vtkHexahedron() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) elif (isinstance(element, LineElement) or isinstance(element, SpringElement)): elem = vtk.vtkLine() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) ### elif isinstance(element, CONM2): # not perfectly located del self.eidMap[eid] i -= 1 nid = element.Nid() c = element.Centroid() elem = vtk.vtkVertex() #elem = vtk.vtkSphere() #elem.SetRadius(1.0) #print str(element) points2.InsertPoint(j, *c) elem.GetPointIds().SetId(0, j) #elem.SetCenter(points.GetPoint(nidMap[nid])) self.grid2.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) j += 1 else: del self.eidMap[eid] i -= 1 print("skipping %s" % (element.type)) i += 1 ### self.grid.SetPoints(points) self.grid2.SetPoints(points2) #self.grid.GetPointData().SetScalars(self.gridResult) #print dir(self.grid) #.SetNumberOfComponents(0) #self.grid.GetCellData().SetNumberOfTuples(1); #self.grid.GetCellData().SetScalars(self.gridResult) self.grid.Modified() self.grid2.Modified() self.grid.Update() self.grid2.Update() print("updated grid")
wedgePoints.InsertPoint(1, 0, 0, 0) wedgePoints.InsertPoint(2, 0, .5, .5) wedgePoints.InsertPoint(3, 1, 1, 0) wedgePoints.InsertPoint(4, 1, 0, 0) wedgePoints.InsertPoint(5, 1, .5, .5) wedgeScalars = vtk.vtkFloatArray() wedgeScalars.SetNumberOfTuples(6) wedgeScalars.InsertValue(0, 1) wedgeScalars.InsertValue(1, 1) wedgeScalars.InsertValue(2, 0) wedgeScalars.InsertValue(3, 1) wedgeScalars.InsertValue(4, 1) wedgeScalars.InsertValue(5, 0) aWedge = vtk.vtkWedge() aWedge.GetPointIds().SetId(0, 0) aWedge.GetPointIds().SetId(1, 1) aWedge.GetPointIds().SetId(2, 2) aWedge.GetPointIds().SetId(3, 3) aWedge.GetPointIds().SetId(4, 4) aWedge.GetPointIds().SetId(5, 5) aWedgeGrid = vtk.vtkUnstructuredGrid() aWedgeGrid.Allocate(1, 1) aWedgeGrid.InsertNextCell(aWedge.GetCellType(), aWedge.GetPointIds()) aWedgeGrid.SetPoints(wedgePoints) aWedgeGrid.GetPointData().SetScalars(wedgeScalars) wedgeContours = vtk.vtkContourFilter() wedgeContours.SetInputData(aWedgeGrid)
def Plot_uGridQual(grid): """ Plots quality of a unstructured grid while ignoring pyramids and wedges """ # Create quality filter qual_filter = vtk.vtkMeshQuality() if vtk.vtkVersion().GetVTKMajorVersion() > 5: qual_filter.SetInput(grid) else: qual_filter.SetInputData(grid) qual_filter.SetHexQualityMeasureToScaledJacobian() qual_filter.SetTetQualityMeasureToScaledJacobian() qual_filter.SaveCellQualityOn qual_filter.Update() qual_out = qual_filter.GetOutput() # Get quality as array qual = VN.vtk_to_numpy(qual_out.GetCellData().GetScalars()) # If unstructured then replace quality of pyramids and wedges wtih nans if str(grid)[:5] == 'vtkUnstructuredGrid': # Get cell types celltypes = VN.vtk_to_numpy(qual_out.GetCellTypesArray()) # Set quality of wedges and pyramids to 1 wedge_pyr_celltypes = [ vtk.vtkWedge().GetCellType(), vtk.vtkPyramid().GetCellType() ] qual[np.in1d(celltypes, wedge_pyr_celltypes)] = 1 # Reinsert quality array back into uGrid vtkfloat = VN.numpy_to_vtk(np.ascontiguousarray(qual), deep=True) qual_out.GetCellData().SetScalars(vtkfloat) # otherwise, negate the grid quality else: qual = -qual vtkfloat = VN.numpy_to_vtk(np.ascontiguousarray(qual), deep=True) qual_out.GetCellData().SetScalars(vtkfloat) ################################# Plotting ################################# # Create mapper mapper = vtk.vtkDataSetMapper() if vtk.vtkVersion().GetVTKMajorVersion() > 5: mapper.SetInput(qual_out) else: mapper.SetInputData(qual_out) mapper.SetScalarRange(np.nanmin(qual), np.nanmax(qual)) # Create Actor actor = vtk.vtkActor() actor.SetMapper(mapper) actor.GetProperty().SetRepresentationToSurface() actor.GetProperty().EdgeVisibilityOn() actor.GetProperty().SetColor(1, 1, 1) actor.GetProperty().LightingOff() # Add FEM Actor to renderer window ren = vtk.vtkRenderer() ren.SetBackground(0.3, 0.3, 0.3) renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) # Allow user to interact istyle = vtk.vtkInteractorStyleTrackballCamera() iren.SetInteractorStyle(istyle) # Add surface to display ren.AddActor(actor) # Add scalar bar scalarBar = vtk.vtkScalarBarActor() scalarBar.SetLookupTable(mapper.GetLookupTable()) scalarBar.SetTitle('Quality') scalarBar.SetNumberOfLabels(5) ren.AddActor(scalarBar) # Render iren.Initialize() renWin.Render() renWin.SetWindowName('FEM Element Quality') iren.Start()
def __init__(self, inputobj=None): vtk.vtkActor.__init__(self) BaseGrid.__init__(self) inputtype = str(type(inputobj)) self._data = None self._polydata = None self.name = "UGrid" ################### if inputobj is None: self._data = vtk.vtkUnstructuredGrid() elif utils.isSequence(inputobj): pts, cells, celltypes = inputobj self._data = vtk.vtkUnstructuredGrid() if not utils.isSequence(cells[0]): tets = [] nf = cells[0] + 1 for i, cl in enumerate(cells): if i == nf or i == 0: k = i + 1 nf = cl + k cell = [cells[j + k] for j in range(cl)] tets.append(cell) cells = tets # This would fill the points and use those to define orientation vpts = utils.numpy2vtk(pts, dtype=float) points = vtk.vtkPoints() points.SetData(vpts) self._data.SetPoints(points) # This fill the points and use cells to define orientation # points = vtk.vtkPoints() # for c in cells: # for pid in c: # points.InsertNextPoint(pts[pid]) # self._data.SetPoints(points) # Fill cells # https://vtk.org/doc/nightly/html/vtkCellType_8h_source.html for i, ct in enumerate(celltypes): cell_conn = cells[i] if ct == vtk.VTK_HEXAHEDRON: cell = vtk.vtkHexahedron() elif ct == vtk.VTK_TETRA: cell = vtk.vtkTetra() elif ct == vtk.VTK_VOXEL: cell = vtk.vtkVoxel() elif ct == vtk.VTK_WEDGE: cell = vtk.vtkWedge() elif ct == vtk.VTK_PYRAMID: cell = vtk.vtkPyramid() elif ct == vtk.VTK_HEXAGONAL_PRISM: cell = vtk.vtkHexagonalPrism() elif ct == vtk.VTK_PENTAGONAL_PRISM: cell = vtk.vtkPentagonalPrism() else: print("UGrid: cell type", ct, "not implemented. Skip.") continue cpids = cell.GetPointIds() for j, pid in enumerate(cell_conn): cpids.SetId(j, pid) self._data.InsertNextCell(ct, cpids) elif "UnstructuredGrid" in inputtype: self._data = inputobj elif isinstance(inputobj, str): from vedo.io import download, loadUnStructuredGrid if "https://" in inputobj: inputobj = download(inputobj, verbose=False) self._data = loadUnStructuredGrid(inputobj) self.filename = inputobj else: colors.printc("UGrid(): cannot understand input type:\n", inputtype, c='r') return # self._mapper = vtk.vtkDataSetMapper() self._mapper = vtk.vtkPolyDataMapper() self._mapper.SetInterpolateScalarsBeforeMapping( settings.interpolateScalarsBeforeMapping) if settings.usePolygonOffset: self._mapper.SetResolveCoincidentTopologyToPolygonOffset() pof, pou = settings.polygonOffsetFactor, settings.polygonOffsetUnits self._mapper.SetResolveCoincidentTopologyPolygonOffsetParameters( pof, pou) self.GetProperty().SetInterpolationToFlat() if not self._data: return # now fill the representation of the vtk unstr grid sf = vtk.vtkShrinkFilter() sf.SetInputData(self._data) sf.SetShrinkFactor(1.0) sf.Update() gf = vtk.vtkGeometryFilter() gf.SetInputData(sf.GetOutput()) gf.Update() self._polydata = gf.GetOutput() self._mapper.SetInputData(self._polydata) sc = None if self.useCells: sc = self._polydata.GetCellData().GetScalars() else: sc = self._polydata.GetPointData().GetScalars() if sc: self._mapper.SetScalarRange(sc.GetRange()) self.SetMapper(self._mapper) self.property = self.GetProperty()
def mapElements(self, points, points2, nidMap, model, j): for eid, element in sorted(model.elements.iteritems()): if isinstance(element, CTRIA3): #print "ctria3" elem = vtkTriangle() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTRIA6): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticTriangle() elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) else: elem = vtkTriangle() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CQUAD4): nodeIDs = element.nodeIDs() elem = vtkQuad() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CQUAD8): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticQuad() elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) else: elem = vtkQuad() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTETRA4): elem = vtkTetra() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CTETRA10): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticTetra() elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) else: elem = vtkTetra() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CPENTA6): elem = vtkWedge() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CPENTA15): nodeIDs = element.nodeIDs() if None not in nodeIDs: elem = vtkQuadraticWedge() elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) elem.GetPointIds().SetId(10, nidMap[nodeIDs[10]]) elem.GetPointIds().SetId(11, nidMap[nodeIDs[11]]) elem.GetPointIds().SetId(12, nidMap[nodeIDs[12]]) elem.GetPointIds().SetId(13, nidMap[nodeIDs[13]]) elem.GetPointIds().SetId(14, nidMap[nodeIDs[14]]) else: elem = vtkWedge() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CHEXA8): nodeIDs = element.nodeIDs() elem = vtkHexahedron() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, CHEXA20): nodeIDs = element.nodeIDs() #print "nodeIDs = ",nodeIDs if None not in nodeIDs: elem = vtkQuadraticHexahedron() elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) elem.GetPointIds().SetId(10, nidMap[nodeIDs[10]]) elem.GetPointIds().SetId(11, nidMap[nodeIDs[11]]) elem.GetPointIds().SetId(12, nidMap[nodeIDs[12]]) elem.GetPointIds().SetId(13, nidMap[nodeIDs[13]]) elem.GetPointIds().SetId(14, nidMap[nodeIDs[14]]) elem.GetPointIds().SetId(15, nidMap[nodeIDs[15]]) elem.GetPointIds().SetId(16, nidMap[nodeIDs[16]]) elem.GetPointIds().SetId(17, nidMap[nodeIDs[17]]) elem.GetPointIds().SetId(18, nidMap[nodeIDs[18]]) elem.GetPointIds().SetId(19, nidMap[nodeIDs[19]]) else: elem = vtkHexahedron() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) elem.GetPointIds().SetId(6, nidMap[nodeIDs[6]]) elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) elif isinstance(element, LineElement) or isinstance(element, SpringElement): elem = vtk.vtkLine() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) self.grid.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) ### elif isinstance(element, CONM2): # not perfectly located nid = element.Nid() c = element.Centroid() elem = vtk.vtkVertex() #elem = vtk.vtkSphere() #elem.SetRadius(1.0) #print str(element) points2.InsertPoint(j, *c) elem.GetPointIds().SetId(0, j) #elem.SetCenter(points.GetPoint(nidMap[nid])) self.grid2.InsertNextCell( elem.GetCellType(), elem.GetPointIds()) j += 1 else: print "skipping %s" % (element.type) ### self.grid.SetPoints(points) self.grid2.SetPoints(points2) self.grid.Update() self.grid2.Update()
elem.GetPointIds().SetId(7, nidMap[nodeIDs[7]]) elem.GetPointIds().SetId(8, nidMap[nodeIDs[8]]) elem.GetPointIds().SetId(9, nidMap[nodeIDs[9]]) else: elem = vtk.vtkTetra() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) Tetra10cell = grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) Color.InsertTuple1(Tetra10cell, 4) elif isinstance(element, CPENTA6): continue elem = vtk.vtkWedge() nodeIDs = element.nodeIDs() elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]]) elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]]) elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]]) elem.GetPointIds().SetId(3, nidMap[nodeIDs[3]]) elem.GetPointIds().SetId(4, nidMap[nodeIDs[4]]) elem.GetPointIds().SetId(5, nidMap[nodeIDs[5]]) Penta6cell = grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) Color.InsertTuple1(Penta6cell, 4) elif isinstance(element, CPENTA15): continue nodeIDs = element.nodeIDs() if None not in nodeIDs:
# note that running in paraview is a bit slower than running in local python. # 23.20 / 25.91s is spent building cells. # call cell_to_nodes once per 2D cell, and cache the type ids... # 5.29 / 8.04s is spent building cells. import memoize #@memoize.memoize(lru=1) def load_data(ncmap_fn): ncmap = qnc.QDataset(ncmap_fn) g = dfm_grid.DFMGrid(ncmap) return ncmap, g wedge_type = vtk.vtkWedge().GetCellType() hexahedron_type = vtk.vtkHexahedron().GetCellType() def load_map(output=None, ncmap_fn=None, tidx=None): output = output or vtk.vtkUnstructuredGrid() if ncmap_fn is None: ncmap_fn = "/home/rusty/models/delft/nms/nms_00_project.dsproj_data/nms_hydro_00_output/DFM_OUTPUT_nms_hydro_00/nms_hydro_00_map.nc" ncmap, g = load_data(ncmap_fn) if tidx is None: tidx = len(ncmap.time) - 1 # force to last time step # Still have about 1.6s in all of this stuff
def Plot_uGridQual(grid): """ Plots quality of a unstructured grid while ignoring pyramids and wedges """ # Create quality filter qual_filter = vtk.vtkMeshQuality() if vtk.vtkVersion().GetVTKMajorVersion() >5: qual_filter.SetInput(grid) else: qual_filter.SetInputData(grid) qual_filter.SetHexQualityMeasureToScaledJacobian() qual_filter.SetTetQualityMeasureToScaledJacobian() qual_filter.SaveCellQualityOn qual_filter.Update() qual_out = qual_filter.GetOutput() # Get quality as array qual = VN.vtk_to_numpy(qual_out.GetCellData().GetScalars()) # If unstructured then replace quality of pyramids and wedges wtih nans if str(grid)[:5] =='vtkUnstructuredGrid': # Get cell types celltypes = VN.vtk_to_numpy(qual_out.GetCellTypesArray()) # Set quality of wedges and pyramids to 1 wedge_pyr_celltypes = [vtk.vtkWedge().GetCellType(), vtk.vtkPyramid().GetCellType()] qual[np.in1d(celltypes, wedge_pyr_celltypes)] = 1 # Reinsert quality array back into uGrid vtkfloat = VN.numpy_to_vtk(np.ascontiguousarray(qual), deep=True) qual_out.GetCellData().SetScalars(vtkfloat) # otherwise, negate the grid quality else: qual = -qual vtkfloat = VN.numpy_to_vtk(np.ascontiguousarray(qual), deep=True) qual_out.GetCellData().SetScalars(vtkfloat) ################################# Plotting ################################# # Create mapper mapper = vtk.vtkDataSetMapper() if vtk.vtkVersion().GetVTKMajorVersion() >5: mapper.SetInput(qual_out) else: mapper.SetInputData(qual_out) mapper.SetScalarRange(np.nanmin(qual), np.nanmax(qual)) # Create Actor actor = vtk.vtkActor() actor.SetMapper(mapper) actor.GetProperty().SetRepresentationToSurface() actor.GetProperty().EdgeVisibilityOn() actor.GetProperty().SetColor(1, 1, 1) actor.GetProperty().LightingOff() # Add FEM Actor to renderer window ren = vtk.vtkRenderer() ren.SetBackground(0.3, 0.3, 0.3) renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) # Allow user to interact istyle = vtk.vtkInteractorStyleTrackballCamera() iren.SetInteractorStyle(istyle) # Add surface to display ren.AddActor(actor) # Add scalar bar scalarBar = vtk.vtkScalarBarActor() scalarBar.SetLookupTable(mapper.GetLookupTable()) scalarBar.SetTitle('Quality') scalarBar.SetNumberOfLabels(5) ren.AddActor(scalarBar) # Render iren.Initialize() renWin.Render() renWin.SetWindowName('FEM Element Quality') iren.Start()
np = points.GetNumberOfPoints() lst = normalize([( 1, 1, 0),(-1, 1, 0),(-1,-1, 0),( 1,-1, 0),( 0, 0, 1)]) for x,y,z in lst: points.InsertNextPoint(x+2,y,z) cell = vtk.vtkPyramid() for i in range(len(lst)): cell.GetPointIds().SetId(i, i+np) UG.InsertNextCell(cell.GetCellType(), cell.GetPointIds() ) # vtkWedge np = points.GetNumberOfPoints() lst = normalize([(0, 1, 0),(0, 0, 0),(0,.5,.5),(1, 1, 0),(1,.0,.0),(1,.5,.5)]) for x,y,z in lst: points.InsertNextPoint(x+4,y,z) cell = vtk.vtkWedge() for i in range(len(lst)): cell.GetPointIds().SetId(i, i+np) UG.InsertNextCell(cell.GetCellType(), cell.GetPointIds() ) # vtkHexahedron np = points.GetNumberOfPoints() lst = normalize([(0, 0, 0),(1, 0, 0),(1, 1, 0),(0, 1, 0), (0, 0, 1),(1, 0,.6),(1, 1,.6),(0, 1, 1)]) for x,y,z in lst: points.InsertNextPoint(x+6,y,z) cell = vtk.vtkHexahedron() for i in range(len(lst)): cell.GetPointIds().SetId(i, i+np) UG.InsertNextCell(cell.GetCellType(), cell.GetPointIds() )