def vtkStructure(self): """Generates a vtk mesh structure that can be used in a vtk file. Returns ------- out : pyvtk.VtkData Vtk data structure """ # Generate the quad node locations in x x = self.x.cellEdges y = self.y.cellEdges z = self.z.cellEdges nCells = self.nCells z = self.z.cellEdges nNodes = self.x.nEdges * self.z.nEdges # Constuct the node locations for the vtk file nodes = np.empty([nNodes, 3]) nodes[:, 0] = self.xMesh('x').reshape(self.nNodes) nodes[:, 1] = self.xMesh('y').reshape(self.nNodes) nodes[:, 2] = self.zMesh('absolute').reshape(self.nNodes) tmp = np.int32([0, 1, self.x.nEdges + 1, self.x.nEdges]) a = np.ones(self.x.nCells, dtype=np.int32) a[0] = 2 index = (np.repeat(tmp[:, np.newaxis], nCells, 1) + np.cumsum(np.tile(a, self.z.nCells)) - 2).T return VtkData(PolyData(points=nodes, polygons=index))
def save_vtk(fname, points, colors): """N.B.: Paraview is a good VTK viewer, which supports ray-tracing.""" structure = PolyData(points=points, vertices=np.arange(len(points))) values = PointData(Scalars(colors, name="colors")) vtk = VtkData(structure, values) vtk.tofile(folder + fname, "binary")
def file_quiver(self, q, p, name=' ', **kwargs): if q.shape[1] == 2: q = np.hstack((q, np.zeros((q.shape[0], 1)))) if p.shape[1] == 2: p = np.hstack((p, np.zeros((p.shape[0], 1)))) pd = PolyData(points=q) vec = PointData(Vectors(p, name='momentum')) vtk = VtkData(pd, vec) vtk.tofile(self.foldername + name + '.vtk', 'ascii')
def __init__(self, discr): from pyvtk import PolyData points = [_three_vector(p) for p in discr.nodes] polygons = [] for eg in discr.element_groups: ldis = eg.local_discretization for el, (el_start, el_stop) in zip(eg.members, eg.ranges): polygons += [[el_start + j for j in element] for element in ldis.get_submesh_indices()] self.structure = PolyData(points=points, polygons=polygons)
def patched_polydata_fromfile(f, self): """Use VtkData(<filename>).""" points = [] vertices = [] lines = [] polygons = [] triangle_strips = [] l = common._getline(f) k, n, datatype = [s.strip().lower() for s in l.split()] if k != 'points': raise ValueError('expected points but got %s' % (repr(k))) n = eval(n) assert datatype in [ 'bit', 'unsigned_char', 'char', 'unsigned_short', 'short', 'unsigned_int', 'int', 'unsigned_long', 'long', 'float', 'double' ], repr(datatype) self.message('\tgetting %s points' % n) while len(points) < 3 * n: l = common._getline(f) points += list(map(eval, l.split())) assert len(points) == 3 * n while 1: l = common._getline(f) if l is None: break sl = l.split() k = sl[0].strip().lower() if k not in ['vertices', 'lines', 'polygons', 'triangle_strips']: break assert len(sl) == 3 n, size = list(map(eval, [sl[1], sl[2]])) lst = [] while len(lst) < size: l = common._getline(f) lst += list(map(eval, l.split())) assert len(lst) == size lst2 = [] j = 0 for i in range(n): lst2.append(lst[j + 1:j + lst[j] + 1]) j += lst[j] + 1 exec('%s = lst2' % k) return PolyData(points, vertices, lines, polygons, triangle_strips), l
def save_vtk(fname, xyz, triangles=None, values=None, vectors=None, triangle_values=None): """Saves a point cloud or triangle mesh as a .vtk file. Files can be opened with Paraview or displayed using the PyVista library. Args: fname (string): filename. xyz (Tensor): (N,3) point cloud or vertices. triangles (integer Tensor, optional): (T,3) mesh connectivity. Defaults to None. values (Tensor, optional): (N,D) values, supported by the vertices. Defaults to None. vectors (Tensor, optional): (N,3) vectors, supported by the vertices. Defaults to None. triangle_values (Tensor, optional): (T,D) values, supported by the triangles. Defaults to None. """ # Encode the points/vertices as a VTK structure: if triangles is None: # Point cloud structure = PolyData(points=numpy(xyz), vertices=np.arange(len(xyz))) else: # Surface mesh structure = PolyData(points=numpy(xyz), polygons=numpy(triangles)) data = [structure] pointdata, celldata = [], [] # Point values - one channel per column of the `values` array: if values is not None: values = numpy(values) if len(values.shape) == 1: values = values[:, None] features = values.T pointdata += [ Scalars(f, name=f"features_{i:02d}") for i, f in enumerate(features) ] # Point vectors - one vector per point: if vectors is not None: pointdata += [Vectors(numpy(vectors), name="vectors")] # Store in the VTK object: if pointdata != []: pointdata = PointData(*pointdata) data.append(pointdata) # Triangle values - one channel per column of the `triangle_values` array: if triangle_values is not None: triangle_values = numpy(triangle_values) if len(triangle_values.shape) == 1: triangle_values = triangle_values[:, None] features = triangle_values.T celldata += [ Scalars(f, name=f"features_{i:02d}") for i, f in enumerate(features) ] celldata = CellData(*celldata) data.append(celldata) # Write to hard drive: vtk = VtkData(*data) os.makedirs(os.path.dirname(fname), exist_ok=True) vtk.tofile(fname)