def _mesh2unstructured_grid(cuds): point2index = {} unstructured_grid = vtk.vtkUnstructuredGrid() unstructured_grid.Allocate() # copy points points = vtk.vtkPoints() point_data = unstructured_grid.GetPointData() data_collector = CUBADataAccumulator(container=point_data) for index, point in enumerate(cuds.iter(item_type=CUBA.POINT)): point2index[point.uid] = index points.InsertNextPoint(*point.coordinates) data_collector.append(point.data) # prepare to copy elements cell_data = unstructured_grid.GetCellData() data_collector = CUBADataAccumulator(container=cell_data) # copy edges mapping = points2edge() for edge in cuds.iter(item_type=CUBA.EDGE): npoints = len(edge.points) ids = vtk.vtkIdList() for uid in edge.points: ids.InsertNextId(point2index[uid]) unstructured_grid.InsertNextCell(mapping[npoints], ids) data_collector.append(edge.data) # copy faces mapping = points2face() for face in cuds.iter(item_type=CUBA.FACE): npoints = len(face.points) ids = vtk.vtkIdList() for uid in face.points: ids.InsertNextId(point2index[uid]) unstructured_grid.InsertNextCell(mapping[npoints], ids) data_collector.append(face.data) # copy cells mapping = points2cell() for cell in cuds.iter(item_type=CUBA.CELL): npoints = len(cell.points) ids = vtk.vtkIdList() for uid in cell.points: ids.InsertNextId(point2index[uid]) unstructured_grid.InsertNextCell(mapping[npoints], ids) data_collector.append(cell.data) unstructured_grid.SetPoints(points) return unstructured_grid
def generate_surface(self, data, color_array_name): """ Render the vtk data as a surface, centered at origin and return the Vizard object. color_array_name defines the name of the CellData array (for example: "equivalent_stress" or "material") """ # Start creating the model viz.startLayer(viz.TRIANGLES) # Use the range of the complete data set to ensure consitency minimum, maximum = self.vtk_data_local.GetCellData().GetArray( color_array_name).GetRange() # Generate surface model by iterating over every polygon polys = data.GetPolys() polys.InitTraversal() id_list = vtk.vtkIdList() cell = polys.GetNextCell(id_list) while not cell == 0: # Get the value of the cell for color calculation value = data.GetCellData().GetArray(color_array_name).GetValue( polys.GetTraversalLocation() / 4) color = Simulation_Data._get_color(minimum, maximum, value) viz.vertexColor(color[0], color[1], color[2]) # Read the points (po) of the polygon po = [] for j in range(id_list.GetNumberOfIds()): po.append(data.GetPoints().GetPoint(id_list.GetId(j))) # Calculate the normal vector of the polygon # Normal vector is needed for better lighting # Calculate the coss product of vectors from point 0 to 1 and 0 to 2 ab = (po[1][0] - po[0][0], po[1][1] - po[0][1], po[1][2] - po[0][2]) ac = (po[2][0] - po[0][0], po[2][1] - po[0][1], po[2][2] - po[0][2]) normal = numpy.cross(ab, ac) # Set the magnitude of the normal vector to 1 normal = normal / numpy.linalg.norm(normal) # Create the polygon with the normal vector and the points viz.normal(normal[0], normal[1], normal[2]) for position in po: viz.vertex(position[0], position[1], position[2]) cell = polys.GetNextCell(id_list) # Finish the model model = viz.endLayer() # Enable lighting for the model (Shadows aren't needed) model.enable(viz.LIGHTING) model.disable(viz.SHADOW_CASTING) model.disable(viz.SHADOWS) # Set the center of the model to (0, 0, 0) model.setPosition(-(self._original_center[0]), -(self._original_center[1]), -(self._original_center[2])) # Create group to get a centered model with coordinates (0, 0, 0) group = viz.addGroup() model.setParent(group) return group
def pvgrid(pvobj, filename): """Generate a ParaView source This function is not meant to be directly run, but is to be run as a ParaView progamable source. In ParaView Sources > Programmable Source in the properties tab set the Output Data Set to 'vtkUnstructuredGrid' and in the script box put from mesh.writers import pvgrid pvgrid(self, "filename.ext") make sure that afepy is on your PYTHONPATH """ from paraview import vtk mesh = parse_xml(filename) output = pvobj.GetOutput() # allocate points pts = vtk.vtkPoints() for point in mesh.coords: if mesh.num_dim == 2: point = np.append(point, 0.) pts.InsertNextPoint(*point) output.SetPoints(pts) num_elem, num_node_per_elem = mesh.connect.shape output.Allocate(num_elem, 1000) for cell in mesh.connect: point_ids = vtk.vtkIdList() for point_id in range(num_node_per_elem): point_ids.InsertId(point_id, int(cell[point_id])) cell_type = VTK.vtk_cell_types(mesh.num_dim, num_node_per_elem) output.InsertNextCell(cell_type, point_ids)
else: ctype = 0 numCellPoints = 0 print("Failed to identify element type") return ctype, numCellPoints # function ends here ctype, numCellPoints = findCellType(celltype) #print ctype, numCellPoints for cell in range(numCells): celltype = words[pos2 + cell * (numCellPoints + 3) + 2] pos_this_cell = pos2 + cell * (numCellPoints + 3) + 3 ctype, numCellPoints = findCellType(celltype) pointIds = vtk.vtkIdList() for pointId in range(numCellPoints): pointIds.InsertId(pointId, int(words[pos_this_cell + pointId])) output.InsertNextCell(ctype, pointIds) # For Material properties of cell numberOfComponents = 1 # For .msh material property by default it is 1 dataArray = vtk.vtkDoubleArray() output.GetCellData().AddArray(dataArray) dataArray.SetNumberOfComponents(numberOfComponents) dataArray.SetName('matgroup') for cell in range(numCells): matgroup = words[pos2 + cell * (numCellPoints + 3) + 1] dataArray.InsertNextValue(float(matgroup))