How to use the pyvista.UnstructuredGrid function in pyvista

To help you get started, we’ve selected a few pyvista examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github akaszynski / pyansys / tests / archive / test_archive.py View on Github external
def test_write_non_ansys_grid(tmpdir):
    grid = pv.UnstructuredGrid(pyvista_examples.hexbeamfile)
    del grid.point_arrays['sample_point_scalars']
    del grid.cell_arrays['sample_cell_scalars']
    archive_file = str(tmpdir.mkdir("tmpdir").join('tmp.cdb'))
    pyansys.save_as_archive(archive_file, grid)
github pyvista / pyvista / tests / test_common.py View on Github external
def test_rotate_y():
    grid = GRID.copy()
    angle = 30
    trans = vtk.vtkTransform()
    trans.RotateY(angle)
    trans.Update()

    trans_filter = vtk.vtkTransformFilter()
    trans_filter.SetTransform(trans)
    trans_filter.SetInputData(grid)
    trans_filter.Update()
    grid_a = pyvista.UnstructuredGrid(trans_filter.GetOutput())

    grid_b = grid.copy()
    grid_b.rotate_y(angle)
    assert np.allclose(grid_a.points, grid_b.points)
github pyvista / pyvista / tests / test_grid.py View on Github external
def test_init_bad_filename():
    filename = os.path.join(test_path, 'test_grid.py')
    with pytest.raises(Exception):
        grid = pyvista.UnstructuredGrid(filename)

    with pytest.raises(Exception):
        grid = pyvista.UnstructuredGrid('not a file')
github pyvista / pyvista / tests / test_grid.py View on Github external
def test_init_bad_input():
    with pytest.raises(Exception):
        unstruct_grid = pyvista.UnstructuredGrid(np.array(1))

    with pytest.raises(Exception):
        unstruct_grid = pyvista.UnstructuredGrid(np.array(1),
                                                 np.array(1),
                                                 np.array(1),
                                                 'woa')
github mne-tools / mne-python / mne / viz / backends / _pyvista.py View on Github external
def quiver3d(self, x, y, z, u, v, w, color, scale, mode, resolution=8,
                 glyph_height=None, glyph_center=None, glyph_resolution=None,
                 opacity=1.0, scale_mode='none', scalars=None,
                 backface_culling=False):
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=FutureWarning)
            from pyvista import UnstructuredGrid
            factor = scale
            vectors = np.c_[u, v, w]
            points = np.vstack(np.c_[x, y, z])
            n_points = len(points)
            offset = np.arange(n_points) * 3
            cell_type = np.full(n_points, vtk.VTK_VERTEX)
            cells = np.c_[np.full(n_points, 1), range(n_points)]
            grid = UnstructuredGrid(offset, cells, cell_type, points)
            grid.point_arrays['vec'] = vectors
            if scale_mode == "scalar":
                grid.point_arrays['mag'] = np.array(scalars)
                scale = 'mag'
            else:
                scale = False
            if mode == "arrow":
                self.plotter.add_mesh(grid.glyph(orient='vec',
                                                 scale=scale,
                                                 factor=factor),
                                      color=color,
                                      opacity=opacity,
                                      backface_culling=backface_culling,
                                      smooth_shading=self.figure.
                                      smooth_shading)
            elif mode == "cone":
github pyvista / pyvista / pyvista / utilities / fileio.py View on Github external
next_offset = offset[-1] + numnodes + 1

        # Extract cell data
        if k in mesh.cell_data.keys():
            for kk, vv in mesh.cell_data[k].items():
                if kk in cell_data:
                    cell_data[kk] = np.concatenate((cell_data[kk], np.array(vv, np.float64)))
                else:
                    cell_data[kk] = np.array(vv, np.float64)

    # Create pyvista.UnstructuredGrid object
    points = mesh.points
    if points.shape[1] == 2:
        points = np.hstack((points, np.zeros((len(points),1))))

    grid = pyvista.UnstructuredGrid(
        np.array(offset),
        np.concatenate(cells),
        np.array(cell_type),
        np.array(points, np.float64),
    )

    # Set point data
    grid.point_arrays.update({k: np.array(v, np.float64) for k, v in mesh.point_data.items()})
    # Set cell data
    grid.cell_arrays.update(cell_data)

    return grid
github pyvista / pyvista / pyvista / core / filters.py View on Github external
def _clean_ugrid_cells(grid):

        output = pyvista.UnstructuredGrid()
        output.Allocate(grid.n_cells)

        # Copy over the original points. Assume there are no degenerate points.
        output.SetPoints(grid.GetPoints())
        output.GetPointData().ShallowCopy(grid.GetPointData())

        out_cell_data = output.GetCellData()
        out_cell_data.CopyGlobalIdsOn()
        out_cell_data.CopyAllocate(grid.GetCellData())

        cell_set = set()
        cell_points = vtk.vtkIdList()
        for i in range(grid.n_cells):
            # duplicate points do not make poly vertices or triangle
            # strips degenerate so don't remove them
            cell_type = grid.GetCellType(i)
github pyvista / pyvista / pyvista / plotting.py View on Github external
"""
        if scalar_bar_args is None:
            scalar_bar_args = {}

        if isinstance(mesh, np.ndarray):
            mesh = pyvista.PolyData(mesh)
            style = 'points'

        # Convert the VTK data object to a pyvista wrapped object if neccessary
        if not is_pyvista_obj(mesh):
            mesh = wrap(mesh)

        # Compute surface normals if using smooth shading
        if smooth_shading:
            # extract surface if mesh is exterior
            if isinstance(mesh, (pyvista.UnstructuredGrid, pyvista.StructuredGrid)):
                grid = mesh
                mesh = grid.extract_surface()
                ind = mesh.point_arrays['vtkOriginalPointIds']
                # remap scalars
                if scalars is not None:
                    scalars = scalars[ind]

            mesh.compute_normals(cell_normals=False, inplace=True, split_vertices=True)

        if show_edges is None:
            show_edges = rcParams['show_edges']

        if edge_color is None:
            edge_color = rcParams['edge_color']

        if show_scalar_bar is None:
github pyvista / pyvista / examples / 00-load / create-unstructured-surface.py View on Github external
[0, 0, 2],
        [1, 0, 2],
        [1, 1, 2],
        [0, 1, 2],
        [0, 0, 3],
        [1, 0, 3],
        [1, 1, 3],
        [0, 1, 3],
    ]
)

# points of the cell array
points = np.vstack((cell1, cell2))

# create the unstructured grid directly from the numpy arrays
grid = pv.UnstructuredGrid(offset, cells, cell_type, points)

# plot the grid
grid.plot(show_edges=True)