def _read_face_edge_file(self, extension): """ Loads data from 2D {basename}.edge or 3D {basename}.face, and inserts triangle/line cells and cell scalars into the unstructured grid. """ file_name = '%s%s' % (self._basename, extension) if (extension == '.edge'): # 2D. Expect two endpoints which form a line. npoints = 2 cell_type = tvtk.Line().cell_type else: # 3D. Expect three points which form a triangle. npoints = 3 cell_type = tvtk.Triangle().cell_type # Load all data. all_data = self._get_data(file_name) # Grab values from the first line of data file. faces_edges, boundary_marker = map(int, all_data[0:2]) # Reshape remainder of array. data_array = all_data[2:].reshape(faces_edges, npoints + 1 + boundary_marker) nodes_array = data_array[:, 1:npoints + 1] - self._numbered_from self._grid.set_cells(cell_type, nodes_array) if (boundary_marker): boundary_marker_array = data_array[:, npoints + 1:npoints + 2] self._add_boundary_marker_array(boundary_marker_array, 'cell')
def _read_ele_file(self): """ Loads data from {basename}.ele, and inserts triangle/tetrahedron cells and cell scalars into the unstructured grid. """ file_name = '%s.ele' % self._basename # Load all data. all_data = self._get_data(file_name) # Grab values from the first line of data file. tet_tri, nodes_per_tet_tri, attributes = map(int, all_data[0:3]) # Reshape remainder of array. data_array = all_data[3:].reshape(tet_tri, 1 + nodes_per_tet_tri + attributes) nodes_array = data_array[:, 1:(nodes_per_tet_tri + 1)] - self._numbered_from if (nodes_per_tet_tri == 3): cell_type = tvtk.Triangle().cell_type else: cell_type = tvtk.Tetra().cell_type self._grid.set_cells(cell_type, nodes_array) for i in range(attributes): attribute_array = data_array[:, (i + nodes_per_tet_tri + 1):(i + nodes_per_tet_tri + 2)] self._add_attribute_array(attribute_array, i, 'cell')
def initialize(self): """ """ (nvertices, spaceDim) = self.vertices.shape (ncells, ncorners) = self.cells.shape assert(spaceDim == 3) assert(ncorners == 3) cellType = tvtk.Triangle().cell_type dataVtk = tvtk.UnstructuredGrid() dataVtk.points = self.vertices dataVtk.set_cells(cellType, self.cells) self.dataVtk = dataVtk return
def read(self, filename): reader = VTKDataReader() data = reader.read(filename) cells = data['cells'] vertices = data['vertices'] (ncells, ncorners) = cells.shape (nvertices, spaceDim) = vertices.shape vtkData = tvtk.UnstructuredGrid() vtkData.points = vertices assert(spaceDim == 3) assert(ncorners == 3) cellType = tvtk.Triangle().cell_type vtkData.set_cells(cellType, cells) # Displacement vector displacement = data['vertex_fields']['displacement'] array = tvtk.FloatArray() array.from_array(displacement.squeeze()) array.name = "Displacement (m)" vtkData.point_data.vectors = array vtkData.point_data.vectors.name = "Displacement (m)" # Compute velocity magnitude velocity = data['vertex_fields']['velocity'] velocityLog = ((velocity[:,0]**2 + velocity[:,1]**2 + velocity[:,2]**2)**0.5) array = tvtk.FloatArray() array.from_array(velocityLog.squeeze()) array.name = "Velocity (m/s)" vtkData.point_data.scalars = array vtkData.point_data.scalars.name = "Velocity (m/s)" return vtkData
def mshplot3D(mesh, scale=[1, 1, 1], elm_ids=[], labels=[0, 0, 0]): ''' Plot 3D meshes (and optionally label vertices, elements, and edges) @param mesh A 3D mesh object @param elm_ids Numpy array of elements that will be plotted. If left empty, all elements are plotted. @param labels (\c bool) List of three elements indicateting what should be labeled. By default, nothing is labeled labels[0]=True labels vertices labels[1]=True labels elements labels[2]=True labels edges @param scale (\c int) List indicating the scaling of the vertices -- this is used to scale the z-direction for a thin mesh. By default, there is no scaling, i.e. scale = [1, 1, 1] @see msh.mesh.Mesh3D @author Matt Ueckermann ''' if type(elm_ids) is not int: if len(elm_ids) == 0: elm_ids = np.arange(len(mesh.elm)) #Figure out how many vertices are in each element type n_vert_in_type = [len(int_el_pqr(element=element, dim=3)[0]) \ for element in mesh.u_elm_type] #Now create the TVTK data-structures for the 'cells' and 'offsets' #cells give [#verts, vert1id, vert2id...,vertnid, #verts ...] #offsets gives the id's in the cells list where #verts are listed. So above # it is [0, n+1] cells = np.array([], dtype=int) offset = np.array([], dtype=int) for elm, elm_type in zip(mesh.elm[elm_ids, :], mesh.elm_type[elm_ids]): n_vt = n_vert_in_type[elm_type] offset = np.append(offset, len(cells)) cells = np.append(cells, [n_vt] + elm[:n_vt].tolist()) #Also have to create a list of element-types -- to do that we have to #convert from my numbering system to the TVTK numbering system if mesh.dim == 3: type_convert = np.array([tvtk.Tetra().cell_type,\ tvtk.Hexahedron().cell_type, tvtk.Wedge().cell_type]) elif mesh.dim == 2: type_convert = np.array([tvtk.Triangle().cell_type,\ tvtk.Quad().cell_type]) cell_types = mesh.u_elm_type[mesh.elm_type[elm_ids]] cell_types = type_convert[cell_types] #To help visualize the mesh, we color it be the distance from the origin if mesh.dim == 3: x, y, z = mesh.vert[:].T elif mesh.dim == 2: x, y = mesh.vert[:, :2].T z = np.zeros_like(x) dist = np.sqrt(x**2 + y**2 + z**2) #and we scale the x, y, z, coordinates according the the assigned 'scale' points = np.column_stack((x * scale[0], y * scale[1], z * scale[2])) #Now we create the data-structures cell_array = tvtk.CellArray() cell_array.set_cells(len(offset), cells) ug = tvtk.UnstructuredGrid(points=points) ug.set_cells(cell_types, offset, cell_array) ug.point_data.scalars = dist.ravel() ug.point_data.scalars.name = 'Distance from Origin' #Next we set the new data-structures as a mayavi source src = sources.vtk_data_source.VTKDataSource(data=ug) #Extract the edges from the grid edges = mlab.pipeline.extract_edges(src) #Use a shorter name for the elements elm = mesh.elm[elm_ids, :] if any(labels) and len(elm) > 20: string = "WARNING:\nAre you sure you want to label more than 20" + \ "elements in 3D? -- Labels are very memory" + \ "(apparently -- who would have guessed?) \nY(es) to proceed:" answer = raw_input(string) if answer.lower() not in ['yes', 'y', 'yes']: labels = [0, 0, 0] #Next add labels if desired: maxdist = np.max(dist) / 2. if labels[0]: #Vertex labels for i in xrange(len(offset)): for vnum in elm[i, :]: if vnum >= 0: mlab.text3d(points[vnum, 0], points[vnum, 1], \ points[vnum, 2], '%d' % vnum, \ color=(0, 0, 0), scale=0.02*maxdist) if labels[1]: #element labels for i in xrange(len(offset)): if elm_ids[i] < 0: elm_label = mesh.numelm + elm_ids[i] else: elm_label = elm_ids[i] x = np.mean(points[elm[i, :cells[offset[i]]], 0]) y = np.mean(points[elm[i, :cells[offset[i]]], 1]) z = np.mean(points[elm[i, :cells[offset[i]]], 2]) mlab.text3d(x, y, z, '%d' % elm_label, color=(0.3, 0.3, 0.9), \ scale=0.03*maxdist) if labels[2]: #edge labels if len(elm_ids) < len(mesh.elm): elm2ed = connect_elm2ed(mesh.elm2elm[:], mesh.ed2ed) ed_ids = np.unique(elm2ed[elm_ids]) ed_ids = ed_ids[ed_ids >= 0] ed2ed = mesh.ed2ed[ed_ids, :] else: ed_ids = np.arange(len(mesh.ed2ed)) ed2ed = mesh.ed2ed[:] for i in xrange(len(ed2ed)): n = 8 if ed2ed[i, -1] < 0: n = 7 x = np.mean(points[ed2ed[i, 4:n], 0]) y = np.mean(points[ed2ed[i, 4:n], 1]) z = np.mean(points[ed2ed[i, 4:n], 2]) mlab.text3d(x, y, z, '%d' % ed_ids[i], color=(0.3, 0.9, 0.3), \ scale=0.03*maxdist) #And plot them! mlab.pipeline.surface(edges, opacity=0.4, line_width=2) mlab.axes() mlab.title('3D mesh', size=0.5, height=0.95) #mlab.show() return cells, offset, cell_types