def __init__(self, module_manager):
     SimpleVTKClassModuleBase.__init__(
         self, module_manager,
         vtk.vtkXMLRectilinearGridReader(), 'Reading vtkXMLRectilinearGrid.',
         (), ('vtkXMLRectilinearGrid',),
         replaceDoc=True,
         inputFunctions=None, outputFunctions=None)
# extract commandline arguments
(opts, args) = p.parse_args()
if len(args) < 2:
    sys.stderr.write("Number of arguments incorrect\n")
    sys.exit(1)
vtr_files = args[0:-1]
grid_file = args[-1]


# read vtr data
r = {}
for vtr_file in vtr_files:
    if opts.verbose: sys.stdout.write("Reading " + vtr_file + "\n");
    if vtr_file.endswith(".vtr"):
        r[vtr_file] = vtk.vtkXMLRectilinearGridReader()
    else:
        sys.stderr.write("Not currently configured to read file of type " + vtr_file + "\n");
        sys.exit(2)
    r[vtr_file].SetFileName(vtr_file)
    r[vtr_file].Update()
    r[vtr_file] = r[vtr_file].GetOutput()


# get function_vals and check it exists
if opts.verbose: sys.stdout.write("Extracting function_vals\n")
fcn_vals = {}
for vtr_file in vtr_files:
    fcn_vals[vtr_file] = r[vtr_file].GetPointData().GetArray("function_vals")
    if not fcn_vals[vtr_file]:
        sys.stderr.write(vtr_file + " does not have PointData called function_vals\n")
Exemple #3
0
# get the com filename
if len(args) != 1:
   p.print_help()
   sys.exit(1)
in_file = args[0]

if opts.verbose: print "Reading", in_file
if in_file.endswith(".vtp"):
   indata = vtk.vtkXMLPolyDataReader()
   writer = vtk.vtkXMLPolyDataWriter()
elif in_file.endswith(".vtu"):
   indata = vtk.vtkXMLUnstructuredGridReader()
   writer = vtk.vtkXMLUnstructuredGridWriter()
elif in_file.endswith(".vtr"):
   indata = vtk.vtkXMLRectilinearGridReader()
   writer = vtk.vtkXMLRectilinearGridWriter()
else:
   print "This program has not yet been configured to read files of type", in_file
   sys.exit(2)


indata.SetFileName(in_file)
indata.Update()
indata = indata.GetOutput()

if opts.verbose: print "Generating", opts.name

yf = vtk.vtkDoubleArray()
yf.SetName(opts.name)
yf.SetNumberOfValues(indata.GetNumberOfPoints())
sphere = vtk.vtkSphere()
sphere.SetCenter(0.0, 0.0, 0.0)
sphere.SetRadius(0.25)

sample = vtk.vtkSampleFunction()
sample.SetImplicitFunction(sphere)
sample.SetModelBounds(-0.5, 0.5, -0.5, 0.5, -0.5, 0.5)
sample.SetSampleDimensions(res, res, res)
sample.Update()

# Converts image data to structured grid
convert = vtk.vtkImageDataToPointSet()
convert.SetInputConnection(sample.GetOutputPort())
convert.Update()

cthvtr = vtk.vtkXMLRectilinearGridReader()
cthvtr.SetFileName("" + str(VTK_DATA_ROOT) + "/Data/cth.vtr")
cthvtr.CellArrayStatus = ['Pressure', 'Void Volume Fraction', 'X Velocity', 'Y Velocity', 'Z Velocity', 'Volume Fraction for Armor Plate', 'Mass for Armor Plate', 'Volume Fraction for Body, Nose', 'Mass for Body, Nose']
cthvtr.Update()
input = cthvtr.GetOutput()

# Create a cutting plane
plane = vtk.vtkPlane()
plane.SetOrigin(input.GetCenter())
plane.SetNormal(1, 1, 1)

# First create the usual cutter
cutter = vtk.vtkCutter()
cutter.SetInputData(input)
cutter.SetCutFunction(plane)
Exemple #5
0
import vtk
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.axes_grid1 import make_axes_locatable

plt.rcParams['animation.ffmpeg_path'] = '/usr/bin/ffmpeg'

from vtk.util.numpy_support import vtk_to_numpy

reader = vtk.vtkXMLRectilinearGridReader()

fig, ax = plt.subplots()

NrTs = 1500
ims = []
for k in np.arange(0, NrTs, 1):
    # create the filename from sequence number
    if k < 10:
        filename = "tmp/Et_000000000" + str(k) + '.vtr'
    elif k < 100:
        filename = "tmp/Et_00000000" + str(k) + '.vtr'
    elif k < 1000:
        filename = "tmp/Et_0000000" + str(k) + '.vtr'

    reader.SetFileName(filename)
    reader.Update()
    Efield = reader.GetOutput().GetPointData().GetArray(0)
    u = vtk_to_numpy(Efield)
    im = ax.imshow(u, interpolation='nearest')
    a = im.get_axes()
Exemple #6
0
	def readVTRFile(fileName):
	    '''Function to read vtk structured grid (vtr) and return a grid object.'''
	    Reader = vtk.vtkXMLRectilinearGridReader()
	    Reader.SetFileName(fileName)
	    Reader.Update()
	    return Reader.GetOutput()
Exemple #7
0
# get the com filename
if len(args) != 1:
   p.print_help()
   sys.exit(1)
in_file = args[0]

if opts.verbose: print "Reading", in_file
if in_file.endswith(".vtp"):
   indata = vtk.vtkXMLPolyDataReader()
   writer = vtk.vtkXMLPolyDataWriter()
elif in_file.endswith(".vtu"):
   indata = vtk.vtkXMLUnstructuredGridReader()
   writer = vtk.vtkXMLUnstructuredGridWriter()
elif in_file.endswith(".vtr"):
   indata = vtk.vtkXMLRectilinearGridReader()
   writer = vtk.vtkXMLRectilinearGridWriter()
else:
   print "This program has not yet been configured to read files of type", in_file
   sys.exit(2)


indata.SetFileName(in_file)
indata.Update()
indata = indata.GetOutput()

if opts.verbose: print "Generating", opts.name

yf = vtk.vtkDoubleArray()
yf.SetName(opts.name)
yf.SetNumberOfValues(indata.GetNumberOfPoints())
Exemple #8
0
def _load_file(filename, c, alpha, threshold, spacing, unpack):
    fl = filename.lower()

    ################################################################# other formats:
    if fl.endswith(".xml") or fl.endswith(".xml.gz") or fl.endswith(".xdmf"):
        # Fenics tetrahedral file
        actor = loadDolfin(filename)
    elif fl.endswith(".neutral") or fl.endswith(
            ".neu"):  # neutral tetrahedral file
        actor = loadNeutral(filename)
    elif fl.endswith(".gmsh"):  # gmesh file
        actor = loadGmesh(filename)
    elif fl.endswith(".pcd"):  # PCL point-cloud format
        actor = loadPCD(filename)
        actor.GetProperty().SetPointSize(2)
    elif fl.endswith(".off"):
        actor = loadOFF(filename)
    elif fl.endswith(".3ds"):  # 3ds format
        actor = load3DS(filename)
    elif fl.endswith(".wrl"):
        importer = vtk.vtkVRMLImporter()
        importer.SetFileName(filename)
        importer.Read()
        importer.Update()
        actors = importer.GetRenderer().GetActors()  #vtkActorCollection
        actors.InitTraversal()
        wacts = []
        for i in range(actors.GetNumberOfItems()):
            act = actors.GetNextActor()
            wacts.append(act)
        actor = Assembly(wacts)

        ################################################################# volumetric:
    elif fl.endswith(".tif") or fl.endswith(".slc") or fl.endswith(".vti") \
        or fl.endswith(".mhd") or fl.endswith(".nrrd") or fl.endswith(".nii") \
        or fl.endswith(".dem"):
        img = loadImageData(filename, spacing)
        if threshold is False:
            if c is None and alpha == 1:
                c = ['b', 'lb', 'lg', 'y',
                     'r']  # good for blackboard background
                alpha = (0.0, 0.0, 0.2, 0.4, 0.8, 1)
            actor = Volume(img, c, alpha)
        else:
            actor = Volume(img).isosurface(threshold=threshold)
            actor.color(c).alpha(alpha)

        ################################################################# 2D images:
    elif fl.endswith(".png") or fl.endswith(".jpg") or fl.endswith(
            ".bmp") or fl.endswith(".jpeg"):
        if ".png" in fl:
            picr = vtk.vtkPNGReader()
        elif ".jpg" in fl or ".jpeg" in fl:
            picr = vtk.vtkJPEGReader()
        elif ".bmp" in fl:
            picr = vtk.vtkBMPReader()
        picr.SetFileName(filename)
        picr.Update()
        actor = Picture()  # object derived from vtk.vtkImageActor()
        actor.SetInputData(picr.GetOutput())
        if alpha is None:
            alpha = 1
        actor.SetOpacity(alpha)

        ################################################################# multiblock:
    elif fl.endswith(".vtm") or fl.endswith(".vtmb"):
        read = vtk.vtkXMLMultiBlockDataReader()
        read.SetFileName(filename)
        read.Update()
        mb = read.GetOutput()
        if unpack:
            acts = []
            for i in range(mb.GetNumberOfBlocks()):
                b = mb.GetBlock(i)
                if isinstance(b,
                              (vtk.vtkPolyData, vtk.vtkImageData,
                               vtk.vtkUnstructuredGrid, vtk.vtkStructuredGrid,
                               vtk.vtkRectilinearGrid)):
                    acts.append(b)
            return acts
        else:
            return mb

        ################################################################# numpy:
    elif fl.endswith(".npy"):
        acts = loadNumpy(filename)
        if unpack == False:
            return Assembly(acts)
        return acts

    elif fl.endswith(".geojson") or fl.endswith(".geojson.gz"):
        return loadGeoJSON(fl)

        ################################################################# polygonal mesh:
    else:
        if fl.endswith(".vtk"):  # read all legacy vtk types

            #output can be:
            # PolyData, StructuredGrid, StructuredPoints, UnstructuredGrid, RectilinearGrid
            reader = vtk.vtkDataSetReader()
            reader.ReadAllScalarsOn()
            reader.ReadAllVectorsOn()
            reader.ReadAllTensorsOn()
            reader.ReadAllFieldsOn()
            reader.ReadAllNormalsOn()
            reader.ReadAllColorScalarsOn()

        elif fl.endswith(".ply"):
            reader = vtk.vtkPLYReader()
        elif fl.endswith(".obj"):
            reader = vtk.vtkOBJReader()
        elif fl.endswith(".stl"):
            reader = vtk.vtkSTLReader()
        elif fl.endswith(".byu") or fl.endswith(".g"):
            reader = vtk.vtkBYUReader()
        elif fl.endswith(".foam"):  # OpenFoam
            reader = vtk.vtkOpenFOAMReader()
        elif fl.endswith(".pvd"):
            reader = vtk.vtkXMLGenericDataObjectReader()
        elif fl.endswith(".vtp"):
            reader = vtk.vtkXMLPolyDataReader()
        elif fl.endswith(".vts"):
            reader = vtk.vtkXMLStructuredGridReader()
        elif fl.endswith(".vtu"):
            reader = vtk.vtkXMLUnstructuredGridReader()
        elif fl.endswith(".vtr"):
            reader = vtk.vtkXMLRectilinearGridReader()
        elif fl.endswith(".pvtk"):
            reader = vtk.vtkPDataSetReader()
        elif fl.endswith(".pvtr"):
            reader = vtk.vtkXMLPRectilinearGridReader()
        elif fl.endswith("pvtu"):
            reader = vtk.vtkXMLPUnstructuredGridReader()
        elif fl.endswith(".txt") or fl.endswith(".xyz"):
            reader = vtk.vtkParticleReader()  # (format is x, y, z, scalar)
        elif fl.endswith(".facet"):
            reader = vtk.vtkFacetReader()
        else:
            return None

        reader.SetFileName(filename)
        reader.Update()
        routput = reader.GetOutput()

        if not routput:
            colors.printc("~noentry Unable to load", filename, c=1)
            return None

        actor = Actor(routput, c, alpha)
        if fl.endswith(".txt") or fl.endswith(".xyz"):
            actor.GetProperty().SetPointSize(4)

    actor.filename = filename
    return actor
def readVTK(fname):
    reader = vtkXMLRectilinearGridReader()
    reader.SetFileName(fname)
    reader.Update()
    grid = reader.GetOutput(0)
    return grid
Exemple #10
0
def readVTRFile(fileName):
    '''Function to read vtk structured grid (vtr) and return a grid object.'''
    Reader = vtk.vtkXMLRectilinearGridReader()
    Reader.SetFileName(fileName)
    Reader.Update()
    return Reader.GetOutput()
# work out readers and writers to use
if datafn.endswith(".vtu"):
    old_mesh_reader = vtk.vtkXMLUnstructuredGridReader()
elif datafn.endswith(".vtp"):
    old_mesh_reader = vtk.vtkXMLPolyDataReader()
else:
    print "Program not yet configured for type of input file", datafn
    sys.exit(2)

if meshfn.endswith(".vtu"):
    new_mesh_reader = vtk.vtkXMLUnstructuredGridReader()
elif meshfn.endswith(".vtp"):
    new_mesh_reader = vtk.vtkXMLPolyDataReader()
elif meshfn.endswith(".vtr"):
    new_mesh_reader = vtk.vtkXMLRectilinearGridReader()
else:
    print "Program not yet configured for type of input file", meshfn
    sys.exit(2)

if outputfn.endswith(".vtu"):
    writer = vtk.vtkXMLUnstructuredGridWriter()
elif outputfn.endswith(".vtp"):
    writer = vtk.vtkXMLPolyDataWriter()
else:
    print "Program not yet configured for type of output file", outputfn
    sys.exit(2)

# to get rid of z values
flattener = vtk.vtkTransform()
flattener.Scale(1.0, 1.0, 0.0)