def generate(self, params): if self.settings['moving']: for_pos = params['for_pos'] else: for_pos = np.zeros((3,)) nx = np.abs(int((self.x1-self.x0)/self.dx + 1)) ny = np.abs(int((self.y1-self.y0)/self.dy + 1)) nz = np.abs(int((self.z1-self.z0)/self.dz + 1)) xarray = np.linspace(self.x0, self.x1, nx) + for_pos[0] yarray = np.linspace(self.y0, self.y1, ny) + for_pos[1] zarray = np.linspace(self.z0, self.z1, nz) + for_pos[2] grid = [] for iz in range(nz): grid.append(np.zeros((3, nx, ny), dtype=ct.c_double)) for ix in range(nx): for iy in range(ny): grid[iz][0, ix, iy] = xarray[ix] grid[iz][1, ix, iy] = yarray[iy] grid[iz][2, ix, iy] = zarray[iz] vtk_info = tvtk.RectilinearGrid() vtk_info.dimensions = np.array([nx, ny, nz], dtype=int) vtk_info.x_coordinates = xarray vtk_info.y_coordinates = yarray vtk_info.z_coordinates = zarray return vtk_info, grid
def rectilinear_grid(): data = random.random((3, 3, 3)) r = tvtk.RectilinearGrid() r.point_data.scalars = data.ravel() r.point_data.scalars.name = 'scalars' r.dimensions = data.shape r.x_coordinates = array((0, 0.7, 1.4)) r.y_coordinates = array((0, 1, 3)) r.z_coordinates = array((0, .5, 2)) return r
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 test5(self): # imageData数据类型 img = tvtk.ImageData(spacing=(1, 1, 1), origin=(0, 0, 0), dimensions=(3, 4, 5)) #RectilinearGrid数据类型 x = np.array([0, 3, 5, 6]) y = np.array([3, 5, 9, 12]) z = np.array([4, 5, 7, 9]) r = tvtk.RectilinearGrid() r.x_coordinates = x r.y_coordinates = y r.z_coordinates = z r.dimensions = len(x), len(y), len(z) for i in range(64): print(r.get_point(i))
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 _mk_rectilinear_grid(self): """ Creates a RectilinearGrid VTK data set using the factory's attributes. """ rg = tvtk.RectilinearGrid() x = self.position_x.squeeze() if x.ndim == 3: x = x[:, 0, 0] y = self.position_y.squeeze() if y.ndim == 3: y = y[0, :, 0] z = self.position_z.squeeze() if z.ndim == 3: z = z[0, 0, :] # FIXME: We should check array size here. rg.dimensions = (x.size, y.size, z.size) rg.x_coordinates = x rg.y_coordinates = y rg.z_coordinates = z self._vtk_source = rg self._mayavi_source = VTKDataSource(data=self._vtk_source)
def main(): parser = argparse.ArgumentParser(description='Convert 3D data to VTK.') parser.add_argument('input', help='input file') parser.add_argument('output', help='output file') args = parser.parse_args() jd, d = load_bin(args.input) b = jd['bbox'] s = jd['size'] x = np.linspace(b[0], b[1], s[0]) y = np.linspace(b[2], b[3], s[1]) z = np.linspace(b[4], b[5], s[2]) hx = x[1] - x[0] hy = y[1] - y[0] hz = z[1] - z[0] n = [s[0] - 1, s[1] - 1, s[2] - 1] newx = np.linspace(b[0] + hx / 2, b[1] - hx / 2, n[0]) newy = np.linspace(b[2] + hy / 2, b[3] - hy / 2, n[1]) newz = np.linspace(b[4] + hz / 2, b[5] - hz / 2, n[2]) f = RegularGridInterpolator(points=(z, y, x), values=d, fill_value=None) zv, yv, xv = np.meshgrid(newz, newy, newx, indexing='ij') data = f((zv.ravel(), yv.ravel(), xv.ravel())) #grid = tvtk.ImageData(spacing=(hx, hy, hz), origin=(b[0], b[2], b[4]), dimensions=s) grid = tvtk.RectilinearGrid(dimensions=s) grid.x_coordinates = x grid.y_coordinates = y grid.z_coordinates = z grid.cell_data.scalars = np.ravel(data, order='F') grid.cell_data.scalars.name = 'data' # Writes legacy ".vtk" format if filename ends with "vtk", otherwise # this will write data using the newer xml-based format. write_data(grid, args.output)
from tvtk.api import tvtk import numpy as np print("\n----------------------ImageData----------------------") img = tvtk.ImageData( # origin: 三维网格数据的起点坐标 # spacing: 三维数据再X、Y、Z上点的间距 # dimensions:X、Y、Z上的网格数 spacing=(1, 1, 1), origin=(1, 2, 3), dimensions=(3, 4, 5)) print(img.get_point(0)) print("前6个点坐标\n") for n in range(6): print("%.1f, %.1f, %.1f" % img.get_point(n)) print("\n----------------------RectilinearGrid----------------------") x = np.array([0, 3, 9, 15]) y = np.array([0, 1, 5]) z = np.array([0, 2, 3]) r = tvtk.RectilinearGrid() r.x_coordinates = x r.y_coordinates = y r.z_coordinates = z r.dimensions = len(x), len(y), len(z) print("前6个点坐标\n") for n in range(6): print(r.get_point(n))