def _update_elemental_vectors(self, forces_array, set_scalars=True, scale=None): """changes the glyphs""" grid = self.grid if scale is not None: # TODO: glyhs_centroid? self.glyphs_centroid.SetScaleFactor(self.glyph_scale_factor * scale) mag = np.linalg.norm(forces_array, axis=1) #assert len(forces_array) == len(mag) mag_max = mag.max() new_forces = np.copy(forces_array / mag_max) #mag /= mag_max #inonzero = np.where(mag > 0)[0] #print('new_forces_max =', new_forces.max()) #print('new_forces =', new_forces[inonzero]) #print('mag =', mag[inonzero]) vtk_vectors = numpy_to_vtk(new_forces, deep=1) vtk_mag = numpy_to_vtk(mag, deep=1) grid.GetPointData().SetVectors(None) #print('_update_elemental_vectors; shape=%s' % (str(new_forces.shape))) grid.GetCellData().SetVectors(vtk_vectors) if set_scalars: grid.GetCellData().SetScalars(vtk_mag) self.arrow_actor_centroid.SetVisibility(True) grid.Modified() self.grid_selected.Modified()
def create_highlighted_actors( gui, grid: vtk.vtkUnstructuredGrid, all_nodes=None, nodes=None, set_node_scalars=True, all_elements=None, elements=None, set_element_scalars=True, add_actors: bool = False) -> List[vtk.vtkLODActor]: """creates nodes & element highlighted objects""" actors = [] nnodes = 0 nelements = 0 if nodes is not None: nnodes = len(nodes) assert len(all_nodes) >= nnodes if elements is not None: nelements = len(elements) assert len(all_elements) >= nelements assert nnodes + nelements > 0 if nnodes: point_ids = np.searchsorted(all_nodes, nodes) output_data = grid.GetPoints().GetData() points_array = vtk_to_numpy(output_data) # yeah! point_array2 = points_array[point_ids, :] points2 = numpy_to_vtk_points(point_array2) ugrid = create_unstructured_point_grid(points2, nnodes) if set_node_scalars: point_ids_array = numpy_to_vtk(nodes) ugrid.GetPointData().SetScalars(point_ids_array) actor = create_highlighted_actor(gui, ugrid, representation='points', add_actor=add_actors) actors.append(actor) if nelements: cell_ids = np.searchsorted(all_elements, elements) selection_node = create_vtk_selection_node_by_cell_ids(cell_ids) ugrid = extract_selection_node_from_grid_to_ugrid(grid, selection_node) if set_element_scalars: element_ids_array = numpy_to_vtk(elements) ugrid.GetPointData().SetScalars(None) ugrid.GetCellData().SetScalars(element_ids_array) actor = create_highlighted_actor(gui, ugrid, representation='wire', add_actor=add_actors) actors.append(actor) return actors
def update_grid_function(nid_map, ugrid, points, nodes): """custom function to update the 3d bars""" points_list = [] node0b = 0 for eid in bar_beam_eids: elem = self.model.elements[eid] pid_ref = elem.pid_ref if pid_ref is None: pid_ref = self.model.Property(elem.pid) assert not isinstance(pid_ref, integer_types), elem ptype = pid_ref.type bar_type = _get_bar_type(ptype, pid_ref) nids = elem.nodes (nid1, nid2) = elem.node_ids node1 = model.nodes[nid1] node2 = model.nodes[nid2] i1, i2 = np.searchsorted(self.node_ids, [nid1, nid2]) n1 = nodes[i1, :] n2 = nodes[i2, :] #centroid = (n1 + n2) / 2. i = n2 - n1 Li = norm(i) ihat = i / Li v, wa, wb, xform = self._rotate_v_wa_wb( model, elem, n1, n2, node1, node2, ihat, i, eid, Li, debug) if wb is None: # one or more of v, wa, wb are bad continue yhat = xform[1, :] zhat = xform[2, :] ugridi = None node0b = add_3d_bar_element( bar_type, ptype, pid_ref, n1+wa, n2+wb, xform, ugridi, node0b, points_list, add_to_ugrid=False) points_array = _make_points_array(points_list) points_array2 = numpy_to_vtk( num_array=points_array, deep=1, array_type=vtk.VTK_FLOAT, ) points.SetData(points_array2) ugrid.SetPoints(points) points.Modified() ugrid.Modified() return
def _create_vtk_points_from_list(points_list: [List[np.ndarray]]): #points_array = _make_points_array(points_list) points_array = np.vstack(points_list) points = vtk.vtkPoints() vtk_points = numpy_to_vtk( num_array=points_array, deep=1, array_type=vtk.VTK_FLOAT, ) points.SetData(vtk_points) points.Modified() return points
def load_abaqus_geometry(self, abaqus_filename, name='main', plot=True): """loads abaqus input files into the gui""" model_name = name skip_reading = self.gui._remove_old_geometry(abaqus_filename) if skip_reading: return self.gui.eid_maps[name] = {} self.gui.nid_maps[name] = {} model = Abaqus(log=self.gui.log, debug=False) self.gui.model_type = 'abaqus' #self.model_type = model.model_type model.read_abaqus_inp(abaqus_filename) self.gui.nid_map = {} nnodes, all_nodes, nelements = get_nodes_nnodes_nelements(model) self.gui.log.info('nnodes=%s nelements=%s' % (nnodes, nelements)) assert nelements > 0, nelements #nodes = model.nodes #elements = model.elements self.gui.nnodes = nnodes self.gui.nelements = nelements grid = self.gui.grid grid.Allocate(self.gui.nelements, 1000) assert len(all_nodes) > 0 is not None, len(all_nodes) if len(all_nodes) == 1: nodes = all_nodes[0] else: nodes = np.vstack(all_nodes) mmax = np.amax(nodes, axis=0) mmin = np.amin(nodes, axis=0) dim_max = (mmax - mmin).max() self.gui.create_global_axes(dim_max) points = vtk.vtkPoints() points.SetNumberOfPoints(self.gui.nnodes) data_type = vtk.VTK_FLOAT points_array = numpy_to_vtk( num_array=nodes, deep=True, array_type=data_type ) points.SetData(points_array) nid_offset = -1 nids = [] for unused_part_name, part in model.parts.items(): self.gui.log.info('part_name = %r' % unused_part_name) nnodesi = part.nodes.shape[0] nidsi = part.nids nids.append(nidsi) add_lines(grid, nidsi, part.r2d2, nid_offset) add_tris(grid, nidsi, part.cps3, nid_offset) add_tris(grid, nidsi, part.cpe3, nid_offset) add_quads(grid, nidsi, part.cpe4, nid_offset) add_quads(grid, nidsi, part.cpe4r, nid_offset) add_quads(grid, nidsi, part.cps4, nid_offset) add_quads(grid, nidsi, part.cps4r, nid_offset) add_quads(grid, nidsi, part.coh2d4, nid_offset) add_quads(grid, nidsi, part.cohax4, nid_offset) add_tris(grid, nidsi, part.cax3, nid_offset) #add_quads(grid, nidsi, part.cax4, nid_offset) add_quads(grid, nidsi, part.cax4r, nid_offset) # solids add_tetras(grid, nidsi, part.c3d10h, nid_offset) add_hexas(grid, nidsi, part.c3d8r, nid_offset) nid_offset += nnodesi nids = np.hstack(nids) grid.SetPoints(points) grid.Modified() # loadCart3dResults - regions/loads self.gui.scalar_bar_actor.VisibilityOn() self.gui.scalar_bar_actor.Modified() note = '' self.gui.isubcase_name_map = {1: ['Abaqus%s' % note, '']} #form = [] cases = OrderedDict() ID = 1 form, cases, unused_icase, node_ids, element_ids = self._fill_abaqus_case( cases, ID, nids, nodes, nelements, model) #self._fill_cart3d_results(cases, form, icase, ID, model) self.gui.node_ids = node_ids self.gui.element_ids = element_ids self.gui._finish_results_io2(model_name, form, cases)
def load_abaqus_geometry(self, abaqus_filename, name='main', plot=True): """loads abaqus input files into the gui""" skip_reading = self.gui._remove_old_geometry(abaqus_filename) if skip_reading: return self.gui.eid_maps[name] = {} self.gui.nid_maps[name] = {} model = Abaqus(log=self.gui.log, debug=False) self.gui.model_type = 'abaqus' #self.model_type = model.model_type model.read_abaqus_inp(abaqus_filename) n_r2d2 = 0 n_cpe3 = 0 n_cpe4 = 0 n_cpe4r = 0 n_coh2d4 = 0 n_c3d10h = 0 n_cohax4 = 0 n_cax3 = 0 n_cax4r = 0 nnodes = 0 nelements = 0 all_nodes = [] for unused_part_name, part in iteritems(model.parts): unused_nids = part.nids - 1 nodes = part.nodes nnodes += nodes.shape[0] if part.r2d2 is not None: n_r2d2 += part.r2d2.shape[0] if part.cpe3 is not None: n_cpe3 += part.cpe3.shape[0] if part.cpe4 is not None: n_cpe4 += part.cpe4.shape[0] if part.cpe4r is not None: n_cpe4r += part.cpe4r.shape[0] if part.coh2d4 is not None: n_coh2d4 += part.coh2d4.shape[0] if part.cohax4 is not None: n_cohax4 += part.cohax4.shape[0] if part.cax3 is not None: n_cax3 += part.cax3.shape[0] if part.cax4r is not None: n_cax4r += part.cax4r.shape[0] if part.c3d10h is not None: n_c3d10h += part.c3d10h.shape[0] all_nodes.append(nodes) nelements += (n_r2d2 + n_cpe3 + n_cpe4 + n_cpe4r + n_coh2d4 + n_c3d10h + n_cohax4 + n_cax3 + n_cax4r) assert nelements > 0, nelements #nodes = model.nodes #elements = model.elements self.gui.nnodes = nnodes self.gui.nelements = nelements grid = self.gui.grid grid.Allocate(self.gui.nelements, 1000) points = vtk.vtkPoints() points.SetNumberOfPoints(self.gui.nnodes) self.gui.nid_map = {} assert nodes is not None nnodes = nodes.shape[0] if len(all_nodes) == 1: nodes = all_nodes[0] else: nodes = np.vstack(all_nodes) mmax = np.amax(nodes, axis=0) mmin = np.amin(nodes, axis=0) dim_max = (mmax - mmin).max() self.gui.create_global_axes(dim_max) data_type = vtk.VTK_FLOAT points_array = numpy_to_vtk(num_array=nodes, deep=True, array_type=data_type) points.SetData(points_array) nid_offset = -1 for unused_part_name, part in iteritems(model.parts): nnodesi = part.nodes.shape[0] n_r2d2 = 0 n_cpe3 = 0 n_cpe4 = 0 n_cpe4r = 0 n_coh2d4 = 0 n_c3d10h = 0 n_cohax4 = 0 n_cax3 = 0 n_cax4r = 0 if part.r2d2 is not None: n_r2d2 += part.r2d2.shape[0] if part.cpe3 is not None: n_cpe3 += part.cpe3.shape[0] if part.cpe4 is not None: n_cpe4 += part.cpe4.shape[0] if part.cpe4r is not None: n_cpe4r += part.cpe4r.shape[0] if part.coh2d4 is not None: n_coh2d4 += part.coh2d4.shape[0] if part.cohax4 is not None: n_cohax4 += part.cohax4.shape[0] if part.cax3 is not None: n_cax3 += part.cax3.shape[0] if part.cax4r is not None: n_cax4r += part.cax4r.shape[0] # solids if part.c3d10h is not None: n_c3d10h += part.c3d10h.shape[0] if n_r2d2: eids = part.r2d2[:, 0] node_ids = part.r2d2[:, 1:] + nid_offset for unused_eid, node_ids in zip(eids, node_ids): elem = vtkLine() elem.GetPointIds().SetId(0, node_ids[0]) elem.GetPointIds().SetId(1, node_ids[1]) grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) if n_cpe3: eids = part.cpe3[:, 0] node_ids = part.cpe3[:, 1:] + nid_offset for unused_eid, node_ids in zip(eids, node_ids): elem = vtkTriangle() elem.GetPointIds().SetId(0, node_ids[0]) elem.GetPointIds().SetId(1, node_ids[1]) elem.GetPointIds().SetId(2, node_ids[2]) grid.InsertNextCell(5, elem.GetPointIds()) if n_cpe4: eids = part.cpe4[:, 0] node_ids = part.cpe4[:, 1:] + nid_offset for unused_eid, node_ids in zip(eids, node_ids): elem = vtkQuad() elem.GetPointIds().SetId(0, node_ids[0]) elem.GetPointIds().SetId(1, node_ids[1]) elem.GetPointIds().SetId(2, node_ids[2]) elem.GetPointIds().SetId(3, node_ids[3]) grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) if n_cpe4r: eids = part.cpe4r[:, 0] node_ids = part.cpe4r[:, 1:] + nid_offset for unused_eid, node_ids in zip(eids, node_ids): elem = vtkQuad() elem.GetPointIds().SetId(0, node_ids[0]) elem.GetPointIds().SetId(1, node_ids[1]) elem.GetPointIds().SetId(2, node_ids[2]) elem.GetPointIds().SetId(3, node_ids[3]) grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) if n_coh2d4: eids = part.coh2d4[:, 0] node_ids = part.coh2d4[:, 1:] + nid_offset for unused_eid, node_ids in zip(eids, node_ids): elem = vtkQuad() elem.GetPointIds().SetId(0, node_ids[0]) elem.GetPointIds().SetId(1, node_ids[1]) elem.GetPointIds().SetId(2, node_ids[2]) elem.GetPointIds().SetId(3, node_ids[3]) grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) if n_cohax4: eids = part.cohax4[:, 0] node_ids = part.cohax4[:, 1:] + nid_offset for unused_eid, node_ids in zip(eids, node_ids): elem = vtkQuad() elem.GetPointIds().SetId(0, node_ids[0]) elem.GetPointIds().SetId(1, node_ids[1]) elem.GetPointIds().SetId(2, node_ids[2]) elem.GetPointIds().SetId(3, node_ids[3]) grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) if n_cax3: eids = part.cax3[:, 0] node_ids = part.cax3[:, 1:] + nid_offset for unused_eid, node_ids in zip(eids, node_ids): elem = vtkTriangle() elem.GetPointIds().SetId(0, node_ids[0]) elem.GetPointIds().SetId(1, node_ids[1]) elem.GetPointIds().SetId(2, node_ids[2]) grid.InsertNextCell(5, elem.GetPointIds()) if n_cax4r: eids = part.cax4r[:, 0] node_ids = part.cax4r[:, 1:] + nid_offset for unused_eid, node_ids in zip(eids, node_ids): elem = vtkQuad() elem.GetPointIds().SetId(0, node_ids[0]) elem.GetPointIds().SetId(1, node_ids[1]) elem.GetPointIds().SetId(2, node_ids[2]) elem.GetPointIds().SetId(3, node_ids[3]) grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) # solids if n_c3d10h: eids = part.c3d10h[:, 0] node_ids = part.c3d10h[:, 1:] + nid_offset for unused_eid, node_ids in zip(eids, node_ids): #for unused_eid, node_ids in part.c3d10h: elem = vtkTetra() elem.GetPointIds().SetId(0, node_ids[0]) elem.GetPointIds().SetId(1, node_ids[1]) elem.GetPointIds().SetId(2, node_ids[2]) elem.GetPointIds().SetId(3, node_ids[3]) grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) nid_offset += nnodesi grid.SetPoints(points) grid.Modified() if hasattr(grid, 'Update'): # pragma: no cover grid.Update() # loadCart3dResults - regions/loads self.gui.scalarBar.VisibilityOn() self.gui.scalarBar.Modified() note = '' self.gui.isubcase_name_map = {1: ['Abaqus%s' % note, '']} #form = [] cases = OrderedDict() ID = 1 form, cases, unused_icase, node_ids, element_ids = self._fill_abaqus_case( cases, ID, nodes, nelements, model) #self._fill_cart3d_results(cases, form, icase, ID, model) self.gui.node_ids = node_ids self.gui.element_ids = element_ids self.gui._finish_results_io2(form, cases)
def mixed_type_unstructured_grid(): """A slightly more complex example of how to generate an unstructured grid with different cell types. Returns a created unstructured grid. """ pts = np.array( [ [0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1], # tetra [2, 0, 0], [3, 0, 0], [3, 1, 0], [2, 1, 0], [2, 0, 1], [3, 0, 1], [3, 1, 1], [2, 1, 1], # Hex ], dtype='float32') # shift the points so we can show both. pts[:, 1] += 2.0 npoints = len(pts) forces = pts # The cells must be int64 because numpy_to_vtkIdTypeArray requires that. # I think it depends on what vtkIdTypeArray() was built with. # nnodes_tetra, (nodes_tetra1) # nnodes_hexa, (nodes_hexa1) cells = np.array( [ 4, 0, 1, 2, 3, # tetra 8, 4, 5, 6, 7, 8, 9, 10, 11 # hex ], dtype='int64') # The offsets for the cells (i.e., the indices where the cells start) # one for each element cell_offsets = np.array([0, 5], dtype='int32') # add one element_type for each element tetra_type = vtk.vtkTetra().GetCellType() # VTK_TETRA == 10 hex_type = vtk.vtkHexahedron().GetCellType() # VTK_HEXAHEDRON == 12 cell_types = np.array([tetra_type, hex_type], dtype='int32') # Create the array of cells vtk_cells = vtk.vtkCellArray() vtk_cells_id_type = numpy_to_vtkIdTypeArray(cells, deep=1) # ncells = 2 vtk_cells.SetCells(2, vtk_cells_id_type) # Now create the unstructured grid ug = vtk.vtkUnstructuredGrid() points_data = numpy_to_vtk(pts, deep=1) points = vtk.vtkPoints() points.SetNumberOfPoints(npoints) points.SetData(points_data) ug.SetPoints(points) # Now just set the cell types and reuse the ug locations and cells #ug.SetCells(cell_types, cell_offsets, vtk_cell_array) ug.SetCells( numpy_to_vtk( cell_types, deep=1, array_type=vtk.vtkUnsignedCharArray().GetDataType(), ), numpy_to_vtk(cell_offsets, deep=1, array_type=vtk.vtkIdTypeArray().GetDataType()), vtk_cells, ) return ug, forces
def main(): #print('VTK_VERSION = %r' % vtk.VTK_VERSION) ug, forces = mixed_type_unstructured_grid() forces_array = numpy_to_vtk(forces, deep=1) # get the magnitude of the force vectors scalars = np.linalg.norm(forces, axis=1) # scalars are defined from [0., 1.] scalars = scalars / scalars.max() print(scalars) #scalars[3] = 0. assert len(forces) == len(scalars) force_scalar_array = numpy_to_vtk(scalars, deep=1) grid_mapper = vtk.vtkDataSetMapper() #vtk_version = int(VTK_VERSION[0]) grid_mapper.SetInputData(ug) if make_glyphs: glyphs = vtk.vtkGlyph3D() #if filter_small_forces: #glyphs.SetRange(0.5, 1.) glyphs.SetVectorModeToUseVector() if apply_color_to_glyph: glyphs.SetColorModeToColorByScale() #glyphs.SetColorModeToColorByScalar() #glyphs.SetColorModeToColorByVector() glyphs.ScalingOn() glyphs.ClampingOn() #glyphs.Update() glyphSource = vtk.vtkArrowSource() glyphSource.InvertOn() # flip this arrow direction glyphs.SetInputData(ug) #glyphs.SetSource(glyphSource.GetOutput()) glyphs.SetSourceConnection(glyphSource.GetOutputPort()) #glyphs.SetScaleModeToDataScalingOff() #glyphs.SetScaleFactor(0.1) glyph_mapper = vtk.vtkPolyDataMapper() #glyph_mapper.SetInput(glyphs.GetOutput()) glyph_mapper.SetInputConnection(glyphs.GetOutputPort()) #glyph_mapper.SetVectors(forces_array) # You can set what arrays to use for the (scalars, vectors, normals, and colors) scalars # by using the SetInputArrayToProcess methods in vtkAlgorithm. # The first array is scalars, the next vectors, the next normals and finally color scalars. #glyphs.SetInputArrayToProcess() # Tell glyph which attribute arrays to use for what #glyph.SetInputArrayToProcess(0,0,0,0,'Elevation') # scalars #glyph.SetInputArrayToProcess(1,0,0,0,'RTDataGradient') # vectors #glyph.SetInputArrayToProcess(2,0,0,0,'nothing') # normals #glyph.SetInputArrayToProcess(3,0,0,0,'RTData') # colors #grid_mapper.SetInputData(ug) #if filter_small_forces: ## we need the data to be contiguous or VTK will fail ## np.copy (I think) always works, while deep=1 doesn't always work ## ## It seems like it makes more sense to just use numpy's copy method #show_data = np.copy(np.where(forces > 0.6)[0]) #ids = numpy_to_vtkIdTypeArray(show_data, deep=0) #selection = vtk.vtkSelection() #selection_node = vtk.vtkSelectionNode() #extract_selection = vtk.vtkExtractSelection() #selection_node.SetFieldType(vtk.vtkSelectionNode.POINT) # POINT/CELL #selection_node.SetContentType(vtk.vtkSelectionNode.INDICES) #if vtk.VTK_MAJOR_VERSION <= 5: #extract_selection.SetInput(0, ug) #extract_selection.SetInput(1, selection) #else: #extract_selection.SetInputData(0, ug) #extract_selection.SetInputData(1, selection) ##ug.ShallowCopy(extract_selection.GetOutput()) #selection.AddNode(selection_node) ##if 0: ##if flip_flag: ##selection.RemoveAllNodes() ##selection_node.SetSelectionList(ids) arrow_actor = vtk.vtkLODActor() arrow_actor.SetMapper(glyph_mapper) if not apply_color_to_glyph: prop = arrow_actor.GetProperty() prop.SetColor(1., 0., 0.) ug.GetPointData().SetScalars(force_scalar_array) if make_glyphs: ug.GetPointData().SetVectors(forces_array) if not apply_color_to_glyph: glyph_mapper.ScalarVisibilityOff() geom_actor = vtk.vtkActor() geom_actor.SetMapper(grid_mapper) # Setup renderer renderer = vtk.vtkRenderer() renderer.AddActor(geom_actor) if make_glyphs: renderer.AddActor(arrow_actor) renderer.ResetCamera() renderer.SetBackground(0.7, 0.8, 1.0) # Setup render window renderWindow = vtk.vtkRenderWindow() renderWindow.AddRenderer(renderer) # Setup render window renderWindow = vtk.vtkRenderWindow() renderWindow.AddRenderer(renderer) # Setup render window interactor renderWindowInteractor = vtk.vtkRenderWindowInteractor() style = vtk.vtkInteractorStyleImage() # Render and start interaction renderWindowInteractor.SetRenderWindow(renderWindow) renderWindowInteractor.Initialize() renderWindowInteractor.Start()
if norm_value == 0: case2 = full((nvalues), 1.0 - min_value, dtype='float32') else: case2 = 1.0 - (case - min_value) / norm_value else: if norm_value == 0: case2 = full((nvalues), min_value, dtype='float32') else: case2 = (case - min_value) / norm_value if case.flags.contiguous: case2 = case else: case2 = deepcopy(case) grid_result = numpy_to_vtk(num_array=case2, deep=True, array_type=data_type) #print('grid_result = %s' % grid_result) #print('max2 =', grid_result.GetRange()) else: # vector_size=3 if case.flags.contiguous: case2 = case else: case2 = deepcopy(case) grid_result = numpy_to_vtk(num_array=case2, deep=True, array_type=data_type) return grid_result def update_grid_by_icase_scale_phase(self, icase, scale, phase=0.0):