Beispiel #1
0
def read(filename):
    """This will read any VTK file! It will figure out what reader to use
    then wrap the VTK object for use in ``vtki``
    """
    def legacy(filename):
        reader = vtk.vtkDataSetReader()
        reader.SetFileName(filename)
        reader.Update()
        return reader.GetOutputDataObject(0)

    ext = os.path.splitext(filename)[1]
    if ext.lower() in '.vtk':
        # Use a legacy reader and wrap the result
        return wrap(legacy(filename))
    else:
        # From the extension, decide which reader to use
        if ext.lower() in '.vti':  # ImageData
            return vtki.UniformGrid(filename)
        elif ext.lower() in '.vtr':  # RectilinearGrid
            return vtki.RectilinearGrid(filename)
        elif ext.lower() in '.vtu':  # UnstructuredGrid
            return vtki.UnstructuredGrid(filename)
        elif ext.lower() in '.ply':  # PolyData
            return vtki.PolyData(filename)
        elif ext.lower() in '.vts':  # UnstructuredGrid
            return vtki.StructuredGrid(filename)
        else:
            # Attempt to use the legacy reader...
            try:
                return wrap(legacy(filename))
            except:
                pass
    raise IOError("This file was not able to be automatically read by vtki.")
Beispiel #2
0
def test_create_rectilinear_grid_from_specs():
    # 3D example
    xrng = np.arange(-10, 10, 2)
    yrng = np.arange(-10, 10, 5)
    zrng = np.arange(-10, 10, 1)
    grid = vtki.RectilinearGrid(xrng, yrng, zrng)
    assert grid.n_cells == 9 * 3 * 19
    assert grid.n_points == 10 * 4 * 20
    assert grid.bounds == [-10.0, 8.0, -10.0, 5.0, -10.0, 9.0]
    # 2D example
    cell_spacings = np.array([1., 1., 2., 2., 5., 10.])
    x_coordinates = np.cumsum(cell_spacings)
    y_coordinates = np.cumsum(cell_spacings)
    grid = vtki.RectilinearGrid(x_coordinates, y_coordinates)
    assert grid.n_cells == 5 * 5
    assert grid.n_points == 6 * 6
    assert grid.bounds == [1., 21., 1., 21., 0., 0.]
Beispiel #3
0
def test_create_rectilinear_grid_from_specs():
    xrng = np.arange(-10, 10, 2)
    yrng = np.arange(-10, 10, 5)
    zrng = np.arange(-10, 10, 1)
    grid = vtki.RectilinearGrid(xrng, yrng, zrng)
    assert grid.n_cells == 9*3*19
    assert grid.n_points == 10*4*20
    assert grid.bounds == (-10.0,8.0, -10.0,5.0, -10.0,9.0)
Beispiel #4
0
def test_save_rectilinear(extension, binary, tmpdir):
    filename = str(tmpdir.mkdir("tmpdir").join('tmp.%s' % extension))
    ogrid = examples.load_rectilinear()
    ogrid.save(filename, binary)

    grid = vtki.RectilinearGrid(filename)
    assert grid.n_cells == ogrid.n_cells
    assert np.allclose(grid.x, ogrid.x)
    assert np.allclose(grid.y, ogrid.y)
    assert np.allclose(grid.z, ogrid.z)
    assert grid.dimensions == ogrid.dimensions
Beispiel #5
0
def read(filename, attrs=None):
    """This will read any VTK file! It will figure out what reader to use
    then wrap the VTK object for use in ``vtki``.

    Parameters
    ----------
    attrs : dict, optional
        A dictionary of attributes to call on the reader. Keys of dictionary are
        the attribute/method names and values are the arguments passed to those
        calls. If you do not have any attributes to call, pass ``None`` as the
        value.
    """
    filename = os.path.abspath(os.path.expanduser(filename))
    ext = get_ext(filename)

    # From the extension, decide which reader to use
    if attrs is not None:
        reader = get_reader(filename)
        return standard_reader_routine(reader, filename, attrs=attrs)
    elif ext in '.vti':  # ImageData
        return vtki.UniformGrid(filename)
    elif ext in '.vtr':  # RectilinearGrid
        return vtki.RectilinearGrid(filename)
    elif ext in '.vtu':  # UnstructuredGrid
        return vtki.UnstructuredGrid(filename)
    elif ext in ['.ply', '.obj', '.stl']:  # PolyData
        return vtki.PolyData(filename)
    elif ext in '.vts':  # StructuredGrid
        return vtki.StructuredGrid(filename)
    elif ext in ['.vtm', '.vtmb']:
        return vtki.MultiBlock(filename)
    elif ext in ['.e', '.exo']:
        return read_exodus(filename)
    elif ext in ['.vtk']:
        # Attempt to use the legacy reader...
        return read_legacy(filename)
    else:
        # Attempt find a reader in the readers mapping
        try:
            reader = get_reader(filename)
            return standard_reader_routine(reader, filename)
        except KeyError:
            pass
    raise IOError("This file was not able to be automatically read by vtki.")
Beispiel #6
0
def test_grid_points():
    """Test the points mehtods on UniformGrid and inearGrid"""
    points = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 1],
                       [1, 0, 1], [1, 1, 1], [0, 1, 1]])
    grid = vtki.UniformGrid()
    grid.points = points
    assert grid.dimensions == [2, 2, 2]
    assert grid.spacing == [1, 1, 1]
    assert grid.origin == [0., 0., 0.]
    assert np.allclose(np.unique(grid.points, axis=0), np.unique(points,
                                                                 axis=0))
    opts = np.c_[grid.x, grid.y, grid.z]
    assert np.allclose(np.unique(opts, axis=0), np.unique(points, axis=0))
    # Now test rectilinear grid
    del grid
    grid = vtki.RectilinearGrid()
    grid.points = points
    assert grid.dimensions == [2, 2, 2]
    assert np.allclose(np.unique(grid.points, axis=0), np.unique(points,
                                                                 axis=0))
Beispiel #7
0
def load_rectilinear():
    """ Loads a sample uniform grid """
    return vtki.RectilinearGrid(rectfile)