示例#1
0
def line_source(*args, **kwargs):
    """
    Creates line data.

    **Function signatures**::

        line_source(x, y, z, ...)
        line_source(x, y, z, s, ...)
        line_source(x, y, z, f, ...)

        If 4 positional arguments are passed the last one must be an array s,
        or a callable, f, that returns an array.

    **Keyword arguments**:

        :name: the name of the vtk object created.

        :figure: optionally, the figure on which to add the data source.
                 If None, the source is not added to any figure, and will
                 be added automatically by the modules or
                 filters. If False, no figure will be created by modules
                 or filters applied to the source: the source can only
                 be used for testing, or numerical algorithms, not
                 visualization."""
    if len(args) == 1:
        raise ValueError("wrong number of arguments")
    x, y, z, s = process_regular_scalars(*args)

    data_source = MLineSource()
    data_source.reset(x=x, y=y, z=z, scalars=s)

    name = kwargs.pop('name', 'LineSource')
    ds = tools.add_dataset(data_source.dataset, name, **kwargs)
    data_source.m_data = ds
    return ds
示例#2
0
def vertical_vectors_source(*args, **kwargs):
    """
    Creates a set of vectors pointing upward, useful eg for bar graphs.

    **Function signatures**::

        vertical_vectors_source(s, ...)
        vertical_vectors_source(x, y, s, ...)
        vertical_vectors_source(x, y, f, ...)
        vertical_vectors_source(x, y, z, s, ...)
        vertical_vectors_source(x, y, z, f, ...)

    If only one positional argument is passed, it can be a 1D, 2D, or 3D
    array giving the length of the vectors. The positions of the data
    points are deducted from the indices of array, and an
    uniformly-spaced data set is created.

    If 3 positional arguments (x, y, s) are passed the last one must be
    an array s, or a callable, f, that returns an array. x and y give the
    2D coordinates of positions corresponding to the s values. The
    vertical position is assumed to be 0.

    If 4 positional arguments (x, y, z, s) are passed, the 3 first are
    arrays giving the 3D coordinates of the data points, and the last one
    is an array s, or a callable, f, that returns an array giving the
    data value.

    **Keyword arguments**:

        :name: the name of the vtk object created.

        :figure: optionally, the figure on which to add the data source.
                 If None, the source is not added to any figure, and will
                 be added automatically by the modules or
                 filters. If False, no figure will be created by modules
                 or filters applied to the source: the source can only
                 be used for testing, or numerical algorithms, not
                 visualization.
    """
    if len(args) == 3:
        x, y, data = args
        if np.isscalar(x):
            z = 0
        else:
            z = np.zeros_like(x)
        args = (x, y, z, data)

    x, y, z, s = process_regular_scalars(*args)

    if s is not None:
        s = np.ravel(s)

    data_source = MVerticalGlyphSource()
    data_source.reset(x=x, y=y, z=z, scalars=s)

    name = kwargs.pop('name', 'VerticalVectorsSource')
    ds = tools.add_dataset(data_source.dataset, name, **kwargs)
    data_source.m_data = ds
    return ds
示例#3
0
    def __init__(self, parent, **kwargs):
        # We are not passing the traits to the parent class
        super(PipeFactory, self).__init__()
        # Try to find the right engine and scene to work with
        ancester = parent
        while hasattr(ancester, 'parent'):
            ancester = getattr(ancester, 'parent')
            if isinstance(ancester, Scene):
                self._scene = ancester
                self._engine = ancester.parent
                break
        else:
            if self.figure is not None:
                self._scene = self.figure
            else:
                self._scene = tools.gcf()
                self._engine = get_engine()
        scene = self._scene.scene
        if self.figure is not None and self.figure is not self._scene:
            warnings.warn('Trying to add a module on the wrong scene')
        if isinstance(parent, (Source, tvtk.DataSet)) \
                and not isinstance(parent, Filter) and scene is not None:
            # Search the current scene to see if the  source is already
            # in it, if not add it.
            if not parent in self._scene.children:
                parent = tools.add_dataset(parent, figure=self._scene)

        if scene is not None:
            self._do_redraw = not scene.disable_render
            scene.disable_render = True
        if issubclass(self._target.__class__, Filter):
            self._engine.add_filter(self._target, obj=parent)
        else:
            self.add_module(parent, kwargs)

        # Inject the magical mlab source trait.
        if hasattr(parent, 'mlab_source'):
            ms = parent.mlab_source
            self._target.add_trait('mlab_source', Instance(ms.__class__))
            self._target.mlab_source = ms

        traits = self.get(self.class_trait_names())
        [
            traits.pop(key) for key in traits.keys()
            if key[0] == '_' or key is None
        ]
        traits.update(kwargs)
        # Now calling the traits setter, so that traits handlers are
        # called
        self.set(**traits)
        if scene is not None:
            scene.disable_render = not self._do_redraw
示例#4
0
    def __init__(self, parent, **kwargs):
        # We are not passing the traits to the parent class
        super(PipeFactory, self).__init__()
        # Try to find the right engine and scene to work with
        ancester = parent
        while hasattr(ancester, 'parent'):
            ancester = getattr(ancester, 'parent')
            if isinstance(ancester, Scene):
                self._scene = ancester
                self._engine = ancester.parent
                break
        else:
            if self.figure is not None:
                self._scene = self.figure
            else:
                self._scene = tools.gcf()
                self._engine = get_engine()
        scene = self._scene.scene
        if self.figure is not None and self.figure is not self._scene:
            warnings.warn('Trying to add a module on the wrong scene')
        if isinstance(parent, (Source, tvtk.DataSet)) \
                and not isinstance(parent, Filter) and scene is not None:
            # Search the current scene to see if the  source is already
            # in it, if not add it.
            if not parent in self._scene.children:
                parent = tools.add_dataset(parent, figure=self._scene)


        if scene is not None:
            self._do_redraw = not scene.disable_render
            scene.disable_render = True
        if issubclass(self._target.__class__, Filter):
            self._engine.add_filter(self._target, obj=parent)
        else:
            self.add_module(parent, kwargs)

        # Inject the magical mlab source trait.
        if hasattr(parent, 'mlab_source'):
            ms = parent.mlab_source
            self._target.add_trait('mlab_source', Instance(ms.__class__))
            self._target.mlab_source = ms

        traits = self.get(self.class_trait_names())
        [traits.pop(key) for key in traits.keys()
                                    if key[0]=='_' or key is None]
        traits.update(kwargs)
        # Now calling the traits setter, so that traits handlers are
        # called
        self.set(**traits)
        if scene is not None:
            scene.disable_render = not self._do_redraw
示例#5
0
def array2d_source(*args, **kwargs):
    """
    Creates structured 2D data from a 2D array.

    **Function signatures**::

        array2d_source(s, ...)
        array2d_source(x, y, s, ...)
        array2d_source(x, y, f, ...)

    If 3 positional arguments are passed the last one must be an array s,
    or a callable, f, that returns an array. x and y give the
    coordinnates of positions corresponding to the s values.

    x and y can be 1D or 2D arrays (such as returned by numpy.ogrid or
    numpy.mgrid), but the points should be located on an orthogonal grid
    (possibly non-uniform). In other words, all the points sharing a same
    index in the s array need to have the same x or y value.

    If only 1 array s is passed the x and y arrays are assumed to be
    made from the indices of arrays, and an uniformly-spaced data set is
    created.

    **Keyword arguments**:

        :name: the name of the vtk object created.

        :figure: optionally, the figure on which to add the data source.
                 If None, the source is not added to any figure, and will
                 be added automatically by the modules or
                 filters. If False, no figure will be created by modules
                 or filters applied to the source: the source can only
                 be used for testing, or numerical algorithms, not
                 visualization.

        :mask: Mask points specified in a boolean masking array.
    """
    data_source = MArray2DSource()
    mask = kwargs.pop('mask', None)
    if len(args) == 1:
        args = convert_to_arrays(args)
        s = np.atleast_2d(args[0])
        data_source.reset(scalars=s, mask=mask)
    else:
        x, y, s = process_regular_2d_scalars(*args, **kwargs)
        data_source.reset(x=x, y=y, scalars=s, mask=mask)

    name = kwargs.pop('name', 'Array2DSource')
    return tools.add_dataset(data_source.m_data, name, **kwargs)
示例#6
0
def vector_field(*args, **kwargs):
    """ Creates vector field data.

    **Function signatures**::

        vector_field(u, v, w, ...)
        vector_field(x, y, z, u, v, w, ...)
        vector_field(x, y, z, f, ...)

    If only 3 arrays u, v, w are passed the x, y and z arrays are assumed to be
    made from the indices of vectors.

    If the x, y and z arrays are passed, they should have been generated
    by `numpy.mgrid` or `numpy.ogrid`. The function builds a scalar field
    assuming the points are regularily spaced on an orthogonal grid.

    If 4 positional arguments are passed the last one must be a callable, f,
    that returns vectors.

    **Keyword arguments**:

        :name: the name of the vtk object created.

        :scalars: optional scalar data.

        :figure: optionally, the figure on which to add the data source.
                 If None, the source is not added to any figure, and will
                 be added automatically by the modules or
                 filters. If False, no figure will be created by modules
                 or filters applied to the source: the source can only
                 be used for testing, or numerical algorithms, not
                 visualization."""
    if len(args) == 3:
        x = y = z = np.atleast_3d(1)
        u, v, w = [np.atleast_3d(a) for a in args]
    else:
        x, y, z, u, v, w = [
            np.atleast_3d(a) for a in process_regular_vectors(*args)
        ]

    scalars = kwargs.pop('scalars', None)
    if scalars is not None:
        scalars = np.atleast_3d(scalars)
    data_source = MArraySource()
    data_source.reset(x=x, y=y, z=z, u=u, v=v, w=w, scalars=scalars)
    name = kwargs.pop('name', 'VectorField')
    return tools.add_dataset(data_source.m_data, name, **kwargs)
示例#7
0
文件: sources.py 项目: JLHelm/mayavi
def vector_field(*args, **kwargs):
    """ Creates vector field data.

    **Function signatures**::

        vector_field(u, v, w, ...)
        vector_field(x, y, z, u, v, w, ...)
        vector_field(x, y, z, f, ...)

    If only 3 arrays u, v, w are passed the x, y and z arrays are assumed to be
    made from the indices of vectors.

    If the x, y and z arrays are passed, they should have been generated
    by `numpy.mgrid` or `numpy.ogrid`. The function builds a scalar field
    assuming the points are regularily spaced on an orthogonal grid.

    If 4 positional arguments are passed the last one must be a callable, f,
    that returns vectors.

    **Keyword arguments**:

        :name: the name of the vtk object created.

        :scalars: optional scalar data.

        :figure: optionally, the figure on which to add the data source.
                 If None, the source is not added to any figure, and will
                 be added automatically by the modules or
                 filters. If False, no figure will be created by modules
                 or filters applied to the source: the source can only
                 be used for testing, or numerical algorithms, not
                 visualization."""
    if len(args) == 3:
        x = y = z = np.atleast_3d(1)
        u, v, w = [np.atleast_3d(a) for a in args]
    else:
        x, y, z, u, v, w = [np.atleast_3d(a)
                        for a in process_regular_vectors(*args)]

    scalars = kwargs.pop('scalars', None)
    if scalars is not None:
        scalars = np.atleast_3d(scalars)
    data_source = MArraySource()
    data_source.reset(x=x, y=y, z=z, u=u, v=v, w=w, scalars=scalars)
    name = kwargs.pop('name', 'VectorField')
    return tools.add_dataset(data_source.m_data, name, **kwargs)
示例#8
0
def scalar_field(*args, **kwargs):
    """
    Creates a scalar field data.

    **Function signatures**::

        scalar_field(s, ...)
        scalar_field(x, y, z, s, ...)
        scalar_field(x, y, z, f, ...)

    If only 1 array s is passed the x, y and z arrays are assumed to be
    made from the indices of arrays.

    If the x, y and z arrays are passed they are supposed to have been
    generated by `numpy.mgrid`. The function builds a scalar field assuming
    the points are regularily spaced.

    If 4 positional arguments are passed the last one must be an array s, or
    a callable, f, that returns an array.

    **Keyword arguments**:

        :name: the name of the vtk object created.

        :figure: optionally, the figure on which to add the data source.
                 If None, the source is not added to any figure, and will
                 be added automatically by the modules or
                 filters. If False, no figure will be created by modules
                 or filters applied to the source: the source can only
                 be used for testing, or numerical algorithms, not
                 visualization."""
    if len(args) == 1:
        # Be lazy, don't create three big arrays for 1 input array. The
        # MArraySource is clever-enough to handle flat arrays
        x = y = z = np.atleast_1d(1)
        s = args[0]
    else:
        x, y, z, s = process_regular_scalars(*args)

    data_source = MArraySource()
    data_source.reset(x=x, y=y, z=z, scalars=s)

    name = kwargs.pop('name', 'ScalarField')
    return tools.add_dataset(data_source.m_data, name, **kwargs)
示例#9
0
def vector_scatter(*args, **kwargs):
    """ Creates scattered vector data.

    **Function signatures**::

        vector_scatter(u, v, w, ...)
        vector_scatter(x, y, z, u, v, w, ...)
        vector_scatter(x, y, z, f, ...)

    If only 3 arrays u, v, w are passed the x, y and z arrays are assumed to be
    made from the indices of vectors.

    If 4 positional arguments are passed the last one must be a callable, f,
    that returns vectors.

    **Keyword arguments**:

        :name: the name of the vtk object created.

        :scalars: optional scalar data.

        :figure: optionally, the figure on which to add the data source.
                 If None, the source is not added to any figure, and will
                 be added automatically by the modules or
                 filters. If False, no figure will be created by modules
                 or filters applied to the source: the source can only
                 be used for testing, or numerical algorithms, not
                 visualization."""
    x, y, z, u, v, w = process_regular_vectors(*args)

    scalars = kwargs.pop('scalars', None)
    if scalars is not None:
        scalars = np.ravel(scalars)
    name = kwargs.pop('name', 'VectorScatter')

    data_source = MGlyphSource()
    data_source.reset(x=x, y=y, z=z, u=u, v=v, w=w, scalars=scalars)

    ds = tools.add_dataset(data_source.dataset, name, **kwargs)
    data_source.m_data = ds
    return ds
示例#10
0
def triangular_mesh_source(x, y, z, triangles, **kwargs):
    """
    Creates 2D mesh by specifying points and triangle connectivity.

    x, y, z are 2D arrays giving the positions of the vertices of the surface.
    The connectivity between these points is given by listing triplets of
    vertices inter-connected. These vertices are designed by there
    position index.

    **Keyword arguments**:

        :name: the name of the vtk object created.

        :scalars: optional scalar data.

        :figure: optionally, the figure on which to add the data source.
                 If None, the source is not added to any figure, and will
                 be added automatically by the modules or
                 filters. If False, no figure will be created by modules
                 or filters applied to the source: the source can only
                 be used for testing, or numerical algorithms, not
                 visualization.
        """
    x, y, z, triangles = convert_to_arrays((x, y, z, triangles))

    if triangles.min() < 0:
        raise ValueError('The triangles array has negative values')
    if triangles.max() > x.size:
        raise ValueError('The triangles array has values larger than' \
                                    'the number of points')
    scalars = kwargs.pop('scalars', None)
    if scalars is None:
        scalars = z

    data_source = MTriangularMeshSource()
    data_source.reset(x=x, y=y, z=z, triangles=triangles, scalars=scalars)

    name = kwargs.pop('name', 'TriangularMeshSource')
    ds = tools.add_dataset(data_source.dataset, name, **kwargs)
    data_source.m_data = ds
    return ds
示例#11
0
def grid_source(x, y, z, **kwargs):
    """
    Creates 2D grid data.

    x, y, z are 2D arrays giving the positions of the vertices of the surface.
    The connectivity between these points is implied by the connectivity on
    the arrays.

    For simple structures (such as orthogonal grids) prefer the array2dsource
    function, as it will create more efficient data structures.

    **Keyword arguments**:

        :name: the name of the vtk object created.

        :scalars: optional scalar data.

        :figure: optionally, the figure on which to add the data source.
                 If None, the source is not added to any figure, and will
                 be added automatically by the modules or
                 filters. If False, no figure will be created by modules
                 or filters applied to the source: the source can only
                 be used for testing, or numerical algorithms, not
                 visualization.

        :mask: Mask points specified in a boolean masking array.

        """
    scalars = kwargs.pop('scalars', None)
    if scalars is None:
        scalars = z
    mask = kwargs.pop('mask', None)

    x, y, z, scalars = convert_to_arrays((x, y, z, scalars))
    data_source = MGridSource()
    data_source.reset(x=x, y=y, z=z, scalars=scalars, mask=mask)

    name = kwargs.pop('name', 'GridSource')
    ds = tools.add_dataset(data_source.dataset, name, **kwargs)
    data_source.m_data = ds
    return ds