def _create_structured_points_direct(x, y, z=None): """Creates a StructuredPoints object given input data in the form of numpy arrays. Input Arguments: x -- Array of x-coordinates. These should be regularly spaced. y -- Array of y-coordinates. These should be regularly spaced. z -- Array of z values for the x, y values given. The values should be computed such that the z values are computed as x varies fastest and y next. If z is None then no scalars are associated with the structured points. Only the structured points data set is created. """ nx = len(x) ny = len(y) if z is not None: nz = numpy.size(z) assert nx*ny == nz, "len(x)*len(y) != len(z)"\ "You passed nx=%d, ny=%d, nz=%d"%(nx, ny, nz) xmin, ymin = x[0], y[0] dx, dy = (x[1] - x[0]), (y[1] - y[0]) sp = tvtk.StructuredPoints(dimensions=(nx, ny, 1), origin=(xmin, ymin, 0), spacing=(dx, dy, 1)) if z is not None: sp.point_data.scalars = numpy.ravel(z) sp.point_data.scalars.name = 'scalars' return sp
def plot_stiffness(self, editor, object): '''This method gets the input data from the current tstepper which is the root of the tree. Sets up the context and gets the stiffness matrix. ''' K = self._get_stiffness(editor, object) K_dense = DenseMtx(assemb=K) # prepare plotting of the matrix in Mayavi # z_data = K_dense.mtx.flatten() z_max = max(z_data) n_dofs = K.n_dofs spoints = tvtk.StructuredPoints(origin=(0, 0, 0), spacing=(1, -1, 1), dimensions=(n_dofs, n_dofs, 1)) spoints.point_data.scalars = z_data spoints.point_data.scalars.name = 'Stiffness' e = get_engine() src = VTKDataSource(data=spoints) e.add_source(src) scale_factor = .1 / float(z_max) * n_dofs ws = WarpScalar() ws.filter.scale_factor = scale_factor e.add_filter(ws) e.add_filter(PolyDataNormals()) s = Surface() e.add_module(s)
def UnstructuredScalar(InputCoords, OutFileName): #mlab.options.backend = 'envisage' #------------ Writing .vti files ---------------------- # Make the data. dims = np.array((ngr, ngr, ngr)) vol = np.array((nx1, nx2, ny1, ny2, nz1, nz2)) origin = vol[::2] spacing = (vol[1::2] - origin) / (dims - 1) xmin, xmax, ymin, ymax, zmin, zmax = vol x, y, z = np.ogrid[xmin:xmax:dims[0] * 1j, ymin:ymax:dims[1] * 1j, zmin:zmax:dims[2] * 1j] x, y, z = [t.astype('f') for t in (x, y, z)] scalars = r # Make the tvtk dataset. spoints = tvtk.StructuredPoints(origin=origin, spacing=spacing, dimensions=dims) s = scalars.transpose().copy() spoints.point_data.scalars = np.ravel(s) spoints.point_data.scalars.name = 'scalars' fileOut = OutFileName + '.vti' w = tvtk.XMLImageDataWriter(input=spoints, file_name=fileOut) w.write()
def to_vtk(self, prefix): """ export the result to vtk the mesh and its triangle data go to prefix_name_mesh.vtk, the potential/field/pseudopotential go to prefix_name.vtk all arrays are named """ if self.configuration is not None: self.configuration.to_vtk(prefix) sp = tvtk.StructuredPoints( origin=self.grid.get_origin(), spacing=self.grid.step, dimensions=self.grid.shape) # spw = tvtk.StructuredPointsWriter(input=sp) spw = tvtk.StructuredPointsWriter() spw.set_input_data(sp) spw.file_name = "%s_%s.vtk" % (prefix, self.configuration.name) #xidr = tvtk.XMLImageDataWriter(input=sp) for data_name in "potential field pseudo_potential".split(): data = getattr(self, data_name) if data is None: continue if len(data.shape) > 3: data = data.T.reshape(-1, data.shape[0]) else: data = data.T.flatten() d = tvtk.DoubleArray(name=data_name) d.from_array(data) sp.point_data.add_array(d) spw.write() logging.info("wrote %s", spw.file_name)
def do_save(grid, arrays): from tvtk.api import tvtk from tvtk.api import write_data pd = tvtk.PolyData() pd.points = grid.leaf_view.vertices.T pd.polys = grid.leaf_view.elements pd.point_data.scalars = grid.leaf_view.domain_indices pd.point_data.scalars.name = "domains" write_data(pd, "test_kitchen_sink_grid.vtk") abn = {a.type: a.data for a in arrays} mgrid = abn["mgrid"] potential = abn["gscalar"] gradient = abn["gvector"] print dimensions, potential.shape print 'spacing:', spacing print 'origin:', origin print 'dimensions:', dimensions sp = tvtk.StructuredPoints(spacing=spacing, origin=origin, dimensions=dimensions) sp.point_data.scalars = potential.ravel(order='F') sp.point_data.scalars.name = "potential" sp.point_data.vectors = gradient.ravel(order='F') sp.point_data.vectors.name = "gradient" write_data(sp, "test_kitchen_sink_potential.vtk") numpy.savez_compressed("test_kitchen_sink.npz", **abn)
def setUp(self): datasets = [tvtk.ImageData(), tvtk.StructuredPoints(), tvtk.RectilinearGrid(), tvtk.StructuredGrid(), tvtk.PolyData(), tvtk.UnstructuredGrid(), ] exts = ['.vti', '.vti', '.vtr', '.vts', '.vtp', '.vtu'] self.datasets = datasets self.exts = exts
def test_tvtk_dataset_name(self): "Can tvtk datasets can be converted to names correctly." datasets = [ tvtk.ImageData(), tvtk.StructuredPoints(), tvtk.RectilinearGrid(), tvtk.StructuredGrid(), tvtk.PolyData(), tvtk.UnstructuredGrid(), tvtk.Property(), # Not a dataset! 'foo', # Not a TVTK object. ] expect = [ 'image_data', 'image_data', 'rectilinear_grid', 'structured_grid', 'poly_data', 'unstructured_grid', 'none', 'none' ] result = [pipeline_info.get_tvtk_dataset_name(d) for d in datasets] self.assertEqual(result, expect)
def StructuredScalar(InputArray, OutFileName, lim1, lim2): nGr = np.shape(InputArray)[0] # Make the data. nx1IC = ny1 = nz1 = int(lim1) nx2IC = ny2 = nz2 = int(lim2) im = jm = km = nGr * 1j nf0 = nf1 = nf2 = nGr Xg, Yg, Zg = np.mgrid[nx1IC:nx2IC:im, ny1:ny2:jm, nz1:nz2:km] # Make the data. dims = np.array((nf0, nf1, nf2)) vol = np.array((lim1, lim2, lim1, lim2, lim1, lim2)) origin = vol[::2] spacing = (vol[1::2] - origin) / (dims - 1) xmin, xmax, ymin, ymax, zmin, zmax = vol x, y, z = np.ogrid[xmin:xmax:dims[0] * 1j, ymin:ymax:dims[1] * 1j, zmin:zmax:dims[2] * 1j] x, y, z = [t.astype('f') for t in (x, y, z)] scalars = InputArray # Make the tvtk dataset. spoints = tvtk.StructuredPoints(origin=origin, spacing=spacing, dimensions=dims) s = scalars.transpose().copy() spoints.point_data.scalars = np.ravel(s) spoints.point_data.scalars.name = 'scalars' spoints.scalar_type = get_vtk_array_type(s.dtype) fileOut = OutFileName + '.vti' print fileOut w = tvtk.XMLImageDataWriter(input=spoints, file_name=fileOut) w.write()
# License: BSD style. from numpy import arange, sqrt, sin from tvtk.api import tvtk from mayavi.scripts import mayavi2 # Generate the scalar values. x = (arange(0.1, 50.0)-25)/2.0 y = (arange(0.1, 50.0)-25)/2.0 r = sqrt(x[:,None]**2+y**2) z = 5.0*sin(r)/r # # Make the tvtk dataset. # tvtk.ImageData is identical and could also be used here. spoints = tvtk.StructuredPoints(origin=(-12.5,-12.5,0), spacing=(0.5,0.5,1), dimensions=(50,50,1)) # Transpose the array data due to VTK's implicit ordering. VTK assumes # an implicit ordering of the points: X co-ordinate increases first, Y # next and Z last. We flatten it so the number of components is 1. spoints.point_data.scalars = z.T.flatten() spoints.point_data.scalars.name = 'scalar' # Uncomment the next two lines to save the dataset to a VTK XML file. #w = tvtk.XMLImageDataWriter(input=spoints, file_name='spoints2d.vti') #w.write() # Now view the data. @mayavi2.standalone def view(): from mayavi.sources.vtk_data_source import VTKDataSource
def test_information_keys(self): """Test if vtk information objects can be created.""" s = tvtk.StructuredPoints() x = s.FIELD_ARRAY_TYPE() y = tvtk.Information() x.get(y)
from mayavi.scripts import mayavi2 # Make the data. dims = array((128, 128, 128)) vol = array((-5., 5, -5, 5, -5, 5)) origin = vol[::2] spacing = (vol[1::2] - origin) / (dims - 1) xmin, xmax, ymin, ymax, zmin, zmax = vol x, y, z = ogrid[xmin:xmax:dims[0] * 1j, ymin:ymax:dims[1] * 1j, zmin:zmax:dims[2] * 1j] x, y, z = [t.astype('f') for t in (x, y, z)] scalars = sin(x * y * z) / (x * y * z) # Make the tvtk dataset. spoints = tvtk.StructuredPoints(origin=origin, spacing=spacing, dimensions=dims) # The copy makes the data contiguous and the transpose makes it # suitable for display via tvtk. Note that it is not necessary to # make the data contiguous since in that case the array is copied # internally. s = scalars.transpose().copy() spoints.point_data.scalars = ravel(s) spoints.point_data.scalars.name = 'scalars' # Uncomment the next two lines to save the dataset to a VTK XML file. #w = tvtk.XMLImageDataWriter(input=spoints, file_name='spoints3d.vti') #w.write() # Now view the data.
The example is a little contrived since there are better ways of achieving the same effect but the present form nicely illustrates item 2 mentioned above. """ # Author: Prabhu Ramachandran <*****@*****.**> # Copyright (c) 2004-2007, Enthought, Inc. # License: BSD Style. from tvtk.api import tvtk import numpy import time # First create a structured points data set. sp = tvtk.StructuredPoints(origin=(-10., -10., 0.0), dimensions=(80, 80, 1), spacing=(0.25, 0.25, 0.0)) # Create some nice data at these points. x = numpy.arange(-10., 10., 0.25) y = x r = numpy.sqrt(x[:,None]**2+y**2) # 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
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()
def setUp(self): sgrid = tvtk.StructuredPoints( dimensions=(2, 2, 1), spacing=(1, 1, 1), origin=(0, 0, 0) ) self.sgrid = sgrid
# surface.actor.mapper.input_connection = <tvtk.tvtk_classes.algorithm_output.AlgorithmOutput object at 0x7fecec3f27d8> surface.enable_contours = True src = mlab.pipeline.array2d_source(a) warp = mlab.pipeline.warp_scalar(src) poly = mlab.pipeline.poly_data_normals(warp) mlab.pipeline.surface(poly) mlab.test_flow() from tvtk.api import tvtk from scipy import special x,y = np.mgrid[-10:10:20j,-10:10:20j] r = np.sqrt(x**2 + y**2) z = 5*special.j0(r) spoints = tvtk.StructuredPoints(origin=(-12.5,-12.5,0), spacing=(0.5,0.5,1), dimensions=(20,20,1)) spoints.point_data.scalars = z.T.ravel() spoints.point_data.scalars.name = 'scalar' src = mlab.pipeline.add_dataset(spoints) warp = mlab.pipeline.warp_scalar(src) poly = mlab.pipeline.poly_data_normals(warp) surf = mlab.pipeline.surface(poly) x,y,z = np.mgrid[-5:5:128j,-5:5:128j,-5:5:128j] scalars = np.sin(x*y*z)/(x*y*z) spoints = tvtk.StructuredPoints(origin=(-5,-5,-5), spacing=(10/127,10/127,10/127), dimensions=(128,128,128)) s = scalars.T spoints.point_data.scalars = s.ravel()