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()
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
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) # Get a nice view. cam = ren.active_camera cam.azimuth(-60)
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) ren = tvtk.Renderer(background=(0.5, 0.5, 0.5)) ren.add_actor(a)
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()