예제 #1
0
    def pick_world(self, x, y):
        """ Picks a world point and probes for data there. Returns a
        `PickedData` instance."""
        self.worldpicker.pick((float(x), float(y), 0.0),
                              self.data.renwin.renderer)

        # Use the cell picker to get the data that needs to be probed.
        try:
            self.cellpicker.pick((float(x), float(y), 0.0),
                                 self.data.renwin.renderer)
        except TypeError:
            self.cellpicker.pick(float(x), float(y), 0.0,
                                 self.data.renwin.renderer)

        wp = self.worldpicker
        cp = self.cellpicker
        coord = wp.pick_position
        self.probe_data.points = [list(coord)]
        picked_data = PickedData(renwin=self.data.renwin,
                                 text_actor=self.data.text_actor)
        picked_data.coordinate = coord

        if cp.mapper:
            data = get_last_input(cp.mapper.input)
            # Need to create the probe each time because otherwise it
            # does not seem to work properly.
            probe = tvtk.ProbeFilter()
            if vtk_major_version >= 6:
                probe.set_source_data(data)
                probe.set_input_data(self.probe_data)
            else:
                probe.source = data
                probe.input = self.probe_data
            probe.update()
            data = probe.output.point_data
            bounds = cp.mapper.input.bounds

            picked_data.valid = 1
            picked_data.world_pick = 1
            picked_data.point_id = 0
            picked_data.data = data

            self._update_actor(coord, bounds)
        else:
            self.p_actor.visibility = 0

        self.data.renwin.render()
        return picked_data
예제 #2
0
def probe_data(mayavi_object, x, y, z, type='scalars', location='points'):
    """ Retrieve the data from a described by Mayavi visualization object
        at points x, y, z.

        **Parameters**

        :viz_obj: A Mayavi visualization object, or a VTK dataset
                  The object describing the data you are interested in.
        :x: float or ndarray.
            The x position where you want to retrieve the data.
        :y: float or ndarray.
            The y position where you want to retrieve the data.
        :z: float or ndarray
            The z position where you want to retrieve the data.
        :type: 'scalars', 'vectors' or 'tensors', optional
            The type of the data to retrieve.
        :location: 'points' or 'cells', optional
            The location of the data to retrieve.

        **Returns**

        The values of the data at the given point, as an ndarray
        (or multiple arrays, in the case of vectors or tensors) of the
        same shape as x, y, and z.
    """
    dataset = tools.get_vtk_src(mayavi_object)[0]
    assert type in ('scalars', 'vectors', 'cells'), (
        "Invalid value for type: must be 'scalars', 'vectors' or "
        "'cells', but '%s' was given" % type)
    x = np.atleast_1d(x)
    y = np.atleast_1d(y)
    z = np.atleast_1d(z)
    shape = x.shape
    assert y.shape == z.shape == shape, \
                        'The x, y and z arguments must have the same shape'
    probe_data = mesh = tvtk.PolyData(points=np.c_[x.ravel(),
                                                   y.ravel(),
                                                   z.ravel()])
    shape = list(shape)
    probe = tvtk.ProbeFilter()
    tvtk_common.configure_input_data(probe, probe_data)
    tvtk_common.configure_source_data(probe, dataset)
    probe.update()

    if location == 'points':
        data = probe.output.point_data
    elif location == 'cells':
        data = probe.output.cell_data
    else:
        raise ValueError("Invalid value for data location, must be "
                         "'points' or 'cells', but '%s' was given." % location)

    values = getattr(data, type)
    if values is None:
        raise ValueError("The object given has no %s data of type %s" %
                         (location, type))
    values = values.to_array()
    if type == 'scalars':
        values = np.reshape(values, shape)
    elif type == 'vectors':
        values = np.reshape(values, shape + [
            3,
        ])
        values = np.rollaxis(values, -1)
    else:
        values = np.reshape(values, shape + [
            -1,
        ])
        values = np.rollaxis(values, -1)
    return values
예제 #3
0
from tvtk.api import tvtk

# The angular par of the spherical harmonic (3, 2)
x, y, z = np.mgrid[-.5:.5:100j, -.5:.5:100j, -.5:.5:100j]
Phi = np.angle((x + y * 1j)**2 * z)

field = mlab.pipeline.scalar_field(x, y, z, Phi)
ipw = mlab.pipeline.image_plane_widget(field)
mlab.outline(field)

surface = mlab.pipeline.builtin_surface()
surface.source = 'sphere'
surface.data_source.radius = .4
surface.data_source.phi_resolution = 200
surface.data_source.theta_resolution = 200
probe_filter = tvtk.ProbeFilter(source=field.outputs[0])
probe = mlab.pipeline.user_defined(surface, filter=probe_filter)

surf = mlab.pipeline.surface(probe)

fig = mlab.gcf()

################################################################################
# Finally, to inspect the VTK Pipeline (and not the Mayavi one, we
# use the TVTK pipeline browser)
# Note that for Mayavi version < 3.4.1, there is a bug in the
# PipelineBrowser preventing a good display of this pipeline.
from tvtk.pipeline.browser import PipelineBrowser

browser = PipelineBrowser(fig.scene)
browser.show()
예제 #4
0
from tvtk.common import configure_source_data

# The angular par of the spherical harmonic (3, 2)
x, y, z = np.mgrid[-.5:.5:100j, -.5:.5:100j, -.5:.5:100j]
Phi = np.angle((x+y*1j)**2*z)

field = mlab.pipeline.scalar_field(x, y, z, Phi)
ipw = mlab.pipeline.image_plane_widget(field)
mlab.outline(field)

surface = mlab.pipeline.builtin_surface()
surface.source = 'sphere'
surface.data_source.radius = .4
surface.data_source.phi_resolution = 200
surface.data_source.theta_resolution = 200
probe_filter = tvtk.ProbeFilter()
configure_source_data(probe_filter, field.outputs[0])
probe = mlab.pipeline.user_defined(surface, filter=probe_filter)

surf = mlab.pipeline.surface(probe)

fig = mlab.gcf()

################################################################################
# Finally, to inspect the VTK Pipeline (and not the Mayavi one, we
# use the TVTK pipeline browser)
# Note that for Mayavi version < 3.4.1, there is a bug in the
# PipelineBrowser preventing a good display of this pipeline.
from tvtk.pipeline.browser import PipelineBrowser
browser = PipelineBrowser(fig.scene)
browser.show()