Esempio n. 1
0
    def update_pipeline(self):
        """Override this method so that it *updates* the tvtk pipeline
        when data upstream is known to have changed.

        This method is invoked (automatically) when the input fires a
        `pipeline_changed` event.
        """
        if len(self.inputs) == 0:
            return
        input = self.inputs[0].outputs[0]
        plane = None
        if input.is_a('vtkStructuredGrid'):
            plane = tvtk.StructuredGridGeometryFilter()
        elif input.is_a('vtkStructuredPoints') or input.is_a('vtkImageData'):
            plane = tvtk.ImageDataGeometryFilter ()
        elif input.is_a('vtkRectilinearGrid'):
            plane = tvtk.RectilinearGridGeometryFilter ()
        else:
            msg = "The GridPlane component does not support the %s dataset."\
                  %(input.class_name)
            error(msg)
            raise TypeError, msg

        plane.input = input
        self.plane = plane
        self.outputs = [plane.output]
        self._update_limits()
        self._update_extents()
        # If the data is 2D make sure that we default to the
        # appropriate axis.
        extents = list(_get_extent(input))
        diff = [y-x for x, y in zip(extents[::2], extents[1::2])]
        if diff.count(0) > 0:
            self.axis = ['x', 'y', 'z'][diff.index(0)]
    def update_pipeline(self):
        """Override this method so that it *updates* the tvtk pipeline
        when data upstream is known to have changed.

        This method is invoked (automatically) when the input fires a
        `pipeline_changed` event.
        """
        if len(self.inputs) == 0:
            return
        input = self.inputs[0].get_output_dataset()
        plane = None
        if input.is_a('vtkStructuredGrid'):
            plane = tvtk.StructuredGridGeometryFilter()
        elif input.is_a('vtkStructuredPoints') or input.is_a('vtkImageData'):
            plane = tvtk.ImageDataGeometryFilter()
        elif input.is_a('vtkRectilinearGrid'):
            plane = tvtk.RectilinearGridGeometryFilter()
        else:
            msg = "The GridPlane component does not support the %s dataset."\
                  %(input.class_name)
            error(msg)
            raise TypeError(msg)

        self.configure_connection(plane, self.inputs[0])
        self.plane = plane
        self._update_limits()
        self._update_voi()
        self.outputs = [plane]
Esempio n. 3
0
 def show(self):
     grid = EmptyGridSource(dimensions=(20, 30, 40))
     src_output_port = grid.output_port
     src_output_port = grid._vtk_obj.GetOutputPort()
     assert src_output_port is not None
     extract = tvtk.ImageDataGeometryFilter(
         input_connection=src_output_port)
     mapper = tvtk.PolyDataMapper(input_connection=extract.output_port)
     act = tvtk.Actor(mapper=mapper)
     scene = self.scene
     scene.add_actor(act)
     self.configure_traits()
Esempio n. 4
0
    def add_static_background(self, data2D):

        self.sp2 = copy.copy(self.sp)
        self.z2 = np.reshape(np.transpose(data2D), (-1, ))

        self.sp2.point_data.scalars = self.z2
        self.geom_filter2 = tvtk.ImageDataGeometryFilter(input=self.sp2)
        self.warp2 = tvtk.WarpScalar(input=self.geom_filter2.output)
        self.normals2 = tvtk.PolyDataNormals(input=self.warp2.output)

        # The rest of the VTK pipeline.
        self.m2 = tvtk.PolyDataMapper(
            input=self.normals2.output)  #,scalar_range=(self.vmin, self.vmax))
        self.p = tvtk.Property(opacity=1.0,
                               color=(0.5, 0.5, 0.5),
                               representation="w")
        self.a2 = tvtk.Actor(mapper=self.m2, property=self.p)

        self.ren.add_actor(self.a2)

        self.rwi.initialize()
        self.ren.reset_camera()
Esempio n. 5
0
def make_surf_actor(x,
                    y,
                    z,
                    warp=1,
                    scale=[1.0, 1.0, 1.0],
                    make_actor=True,
                    *args,
                    **kwargs):
    """Creates a surface given regularly spaced values of x, y and the
    corresponding z as arrays.  Also works if z is a function.
    Currently works only for regular data - can be enhanced later.

    Parameters
    ----------

        x -- Array of x points (regularly spaced)

        y -- Array if y points (regularly spaced)

        z -- A 2D array for the x and y points with x varying fastest
        and y next.  Also will work if z is a callable which supports
        x and y arrays as the arguments.

        warp -- If true, warp the data to show a 3D surface
        (default = 1).

        scale -- Scale the x, y and z axis as per passed values.
        Defaults to [1.0, 1.0, 1.0].

        make_actor -- also create actors suitably (default True)

        args -- additional positional arguments for func()
        (default is empty)

        kwargs -- a dict of additional keyword arguments for func()
        (default is empty)
    """

    if callable(z):
        zval = numpy.ravel(sampler(x, y, z, *args, **kwargs))
        x, y = squeeze(x), squeeze(y)
    else:
        x, y = squeeze(x), squeeze(y)
        _check_sanity(x, y, z)
        zval = numpy.ravel(z)
        assert len(zval) > 0, "z is empty - nothing to plot!"

    xs = x * scale[0]
    ys = y * scale[1]
    data = _create_structured_points_direct(xs, ys, zval)
    if not make_actor:
        return data
    if warp:
        geom_f = tvtk.ImageDataGeometryFilter()
        configure_input_data(geom_f, data)

        warper = tvtk.WarpScalar(scale_factor=scale[2])
        configure_input_data(warper, geom_f.output)
        normals = tvtk.PolyDataNormals(feature_angle=45)
        configure_input_data(normals, warper.output)

        mapper = tvtk.PolyDataMapper(scalar_range=(min(zval), max(zval)))
        configure_input_data(mapper, normals.output)
    else:
        mapper = tvtk.PolyDataMapper(scalar_range=(min(zval), max(zval)))
        configure_input_data(mapper, data)
    actor = _make_actor(mapper=mapper)
    return data, actor
Esempio n. 6
0
# We need the transpose so the data is as per VTK's expected format
# where X coords vary fastest, Y next and then Z.
try:
    import scipy.special
    z = numpy.reshape(numpy.transpose(5.0*scipy.special.j0(r)), (-1,) )
except ImportError:
    z = numpy.reshape(numpy.transpose(5.0*numpy.sin(r)/r), (-1,) )

# Now set the scalar data for the StructuredPoints object.  The
# scalars of the structured points object will be a view into our
# Numeric array.  Thus, if we change `z` in-place, the changes will
# automatically affect the VTK arrays.
sp.point_data.scalars = z

# Convert this to a PolyData object.
geom_filter = tvtk.ImageDataGeometryFilter(input=sp)

# Now warp this using the scalar value to generate a carpet plot.
warp = tvtk.WarpScalar(input=geom_filter.output)

# Smooth the resulting data so it looks good.
normals = tvtk.PolyDataNormals(input=warp.output)

# The rest of the VTK pipeline.
m = tvtk.PolyDataMapper(input=normals.output,
                        scalar_range=(min(z), max(z)))
a = tvtk.Actor(mapper=m)

ren = tvtk.Renderer(background=(0.5, 0.5, 0.5))
ren.add_actor(a)
Esempio n. 7
0
# We need the transpose so the data is as per VTK's expected format
# where X coords vary fastest, Y next and then Z.
try:
    import scipy.special
    z = numpy.reshape(numpy.transpose(5.0*scipy.special.j0(r)), (-1,) )
except ImportError:
    z = numpy.reshape(numpy.transpose(5.0*numpy.sin(r)/r), (-1,) )

# Now set the scalar data for the StructuredPoints object.  The
# scalars of the structured points object will be a view into our
# Numeric array.  Thus, if we change `z` in-place, the changes will
# automatically affect the VTK arrays.
sp.point_data.scalars = z

# Convert this to a PolyData object.
geom_filter = tvtk.ImageDataGeometryFilter()
configure_input(geom_filter, sp)

# Now warp this using the scalar value to generate a carpet plot.
warp = tvtk.WarpScalar()
configure_input(warp, geom_filter)

# Smooth the resulting data so it looks good.
normals = tvtk.PolyDataNormals()
configure_input(normals, warp)

# The rest of the VTK pipeline.
m = tvtk.PolyDataMapper(scalar_range=(min(z), max(z)))
configure_input(m, normals)

a = tvtk.Actor(mapper=m)
Esempio n. 8
0
    def initialize(self, X, Y, initial_data=None, vmin=None, vmax=None):
        """
		Create objects to plot on
		"""
        if initial_data == None:
            initial_data = zeros(shape(X))
            if vmin == None:
                self.vmin = -1.0
            if vmax == None:
                self.vmax = 1.0
        else:
            if vmin == None:
                self.vmin = np.min(np.min(initial_data))
            if vmax == None:
                self.vmax = np.max(np.max(initial_data))
        x_min = np.min(np.min(X))
        y_min = np.min(np.min(Y))
        x_max = np.max(np.max(X))
        y_max = np.max(np.max(Y))
        x_middle = (x_min + x_max) / 2
        y_middle = (y_min + y_max) / 2
        z_middle = np.mean(np.mean(initial_data))
        L = x_max - x_min
        diffs = np.shape(X)
        x_diff = diffs[0]
        y_diff = diffs[1]
        z_diff = 1.0  #self.vmax-self.vmin

        self.tvtk = tvtk
        self.sp = tvtk.StructuredPoints(origin=(x_middle, y_middle, z_middle),
                                        dimensions=(x_diff, y_diff, 1),
                                        spacing=(2 * L / (x_diff - 1),
                                                 2 * L / (y_diff - 1), 100.0))

        self.z = np.transpose(initial_data).flatten()

        self.sp.point_data.scalars = self.z
        self.geom_filter = tvtk.ImageDataGeometryFilter(input=self.sp)
        self.warp = tvtk.WarpScalar(input=self.geom_filter.output)
        self.normals = tvtk.PolyDataNormals(input=self.warp.output)

        # The rest of the VTK pipeline.
        self.m = tvtk.PolyDataMapper(input=self.normals.output,
                                     scalar_range=(self.vmin, self.vmax))
        p = tvtk.Property(opacity=0.5, color=(1, 1, 1), representation="s")
        self.a = tvtk.Actor(mapper=self.m, property=p)

        self.ren = tvtk.Renderer(background=(0.0, 0.0, 0.0))

        self.ren.add_actor(self.a)

        # Get a nice view.
        self.cam = self.ren.active_camera
        self.cam.azimuth(-50)
        self.cam.roll(90)

        # Create a RenderWindow, add the renderer and set its size.
        self.rw = tvtk.RenderWindow(size=(800, 800))
        self.rw.add_renderer(self.ren)

        # Create the RenderWindowInteractor
        self.rwi = tvtk.RenderWindowInteractor(render_window=self.rw)

        self.rwi.initialize()
        self.ren.reset_camera()
        self.rwi.render()