def field2VTKData (self,name=None,lookupTable=None): """ Creates VTK representation of the receiver. Useful for visualization. Requires pyvtk module. :param str name: human-readable name of the field :param pyvtk.LookupTable lookupTable: color lookup table :return: Instance of pyvtk :rtype: pyvtk """ import pyvtk if name is None: name=self.getFieldIDName() if lookupTable and not isinstance(lookupTable,pyvtk.LookupTable): log.info('ignoring lookupTable which is not a pyvtk.LookupTable instance.') lookupTable=None if lookupTable is None: lookupTable=pyvtk.LookupTable([(0,.231,.298,1.0),(.4,.865,.865,1.0),(.8,.706,.016,1.0)],name='coolwarm') #Scalars use different name than 'coolwarm'. Then Paraview uses its own color mapping instead of taking 'coolwarm' from *.vtk file. This prevents setting Paraview's color mapping. scalarsKw=dict(name=name,lookup_table='default') else: scalarsKw=dict(name=name,lookup_table=lookupTable.name) # see http://cens.ioc.ee/cgi-bin/cvsweb/python/pyvtk/examples/example1.py?rev=1.3 for an example vectorsKw=dict(name=name) # vectors don't have a lookup_table if (self.fieldType == FieldType.FT_vertexBased): if (self.getValueType() == ValueType.Scalar): return pyvtk.VtkData(self.mesh.getVTKRepresentation(), pyvtk.PointData(pyvtk.Scalars([val[0] for val in self.value],**scalarsKw),lookupTable), 'Unstructured Grid Example') elif (self.getValueType() == ValueType.Vector): return pyvtk.VtkData(self.mesh.getVTKRepresentation(), pyvtk.PointData(pyvtk.Vectors(self.value,**vectorsKw),lookupTable), 'Unstructured Grid Example') elif (self.getValueType() == ValueType.Tensor): return pyvtk.VtkData(self.mesh.getVTKRepresentation(), pyvtk.PointData(pyvtk.Tensors(self.getMartixForTensor(self.value),**vectorsKw),lookupTable),'Unstructured Grid Example') else: if (self.getValueType() == ValueType.Scalar): return pyvtk.VtkData(self.mesh.getVTKRepresentation(), pyvtk.CellData(pyvtk.Scalars([val[0] for val in self.value],**scalarsKw),lookupTable), 'Unstructured Grid Example') elif (self.getValueType() == ValueType.Vector): return pyvtk.VtkData(self.mesh.getVTKRepresentation(), pyvtk.CellData(pyvtk.Vectors(self.value,**vectorsKw),lookupTable), 'Unstructured Grid Example') elif (self.getValueType() == ValueType.Tensor): return pyvtk.VtkData(self.mesh.getVTKRepresentation(), pyvtk.CellData(pyvtk.Tensors(self.getMartixForTensor(self.value),**vectorsKw),lookupTable),'Unstructured Grid Example')
import sys sys.path = ['..'] + sys.path #from pyvtk import * import pyvtk as vtk structure = vtk.PolyData(points=[[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1]], polygons=[[0, 1, 2, 3], [4, 5, 6, 7], [0, 1, 5, 4], [2, 3, 7, 6], [0, 4, 7, 3], [1, 2, 6, 5]]) pointdata = vtk.PointData( vtk.Scalars([0, 1, 2, 3, 4, 5, 6, 7], name='sample_scalars', lookup_table='my_table'), vtk.LookupTable([[0, 0, 0, 1], [1, 0, 0, 1], [0, 1, 0, 1], [1, 1, 0, 1], [0, 0, 1, 1], [1, 0, 1, 1], [0, 1, 1, 1], [1, 1, 1, 1]], name='my_table')) celldata = vtk.CellData( vtk.Scalars([0, 1, 2, 3, 4, 5], name='cell_scalars'), vtk.Normals( [[0, 0, -1], [0, 0, 1], [0, -1, 0], [0, 1, 0], [-1, 0, 0], [1, 0, 0]], name='cell_normals'), vtk.Field('FieldData', cellIds=[[0], [1], [2], [3], [4], [5]], faceAttributes=[[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6]])) vtkdata = vtk.VtkData(structure, pointdata, celldata) vtkdata.tofile('example1ascii', 'ascii') #vtkdata.tofile('example1binary','binary')