-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from akaszynski/readers
Support all VTK readers
- Loading branch information
Showing
8 changed files
with
218 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
""" | ||
Contains a dictionary that maps file extensions to VTK readers | ||
""" | ||
import os | ||
|
||
import vtk | ||
import vtki | ||
|
||
|
||
READERS = { | ||
# Standard dataset readers: | ||
'.vtk': vtk.vtkDataSetReader, | ||
'.vti': vtk.vtkXMLImageDataReader, | ||
'.vtr': vtk.vtkXMLRectilinearGridReader, | ||
'.vtu': vtk.vtkXMLUnstructuredGridReader, | ||
'.ply': vtk.vtkPLYReader, | ||
'.obj': vtk.vtkOBJReader, | ||
'.stl': vtk.vtkSTLReader, | ||
'.vts': vtk.vtkXMLStructuredGridReader, | ||
'.vtm': vtk.vtkXMLMultiBlockDataReader, | ||
'.vtmb': vtk.vtkXMLMultiBlockDataReader, | ||
# Image formats: | ||
'.bmp': vtk.vtkBMPReader, | ||
'.dem': vtk.vtkDEMReader, | ||
'.dcm': vtk.vtkDICOMImageReader, | ||
'.jpeg': vtk.vtkJPEGReader, | ||
'.jpg': vtk.vtkJPEGReader, | ||
'.png': vtk.vtkPNGReader, | ||
'.pnm': vtk.vtkPNMReader, | ||
'.slc': vtk.vtkSLCReader, | ||
'.tiff': vtk.vtkTIFFReader, | ||
'.tif': vtk.vtkTIFFReader, | ||
# ExodusII files: | ||
#.g, .e, .ex2, .ex2v2, .exo, .gen, .exoII, .exii, .0, .00, .000 | ||
'.g': vtk.vtkExodusIIReader, | ||
'.e': vtk.vtkExodusIIReader, | ||
'.ex2': vtk.vtkExodusIIReader, | ||
'.ex2v2': vtk.vtkExodusIIReader, | ||
'.exo': vtk.vtkExodusIIReader, | ||
'.gen': vtk.vtkExodusIIReader, | ||
'.exoii': vtk.vtkExodusIIReader, | ||
'.exii': vtk.vtkExodusIIReader, | ||
'.0': vtk.vtkExodusIIReader, | ||
'.00': vtk.vtkExodusIIReader, | ||
'.000': vtk.vtkExodusIIReader, | ||
# Other formats: | ||
'.byu': vtk.vtkBYUReader, | ||
'.chemml': vtk.vtkCMLMoleculeReader, | ||
'.cml': vtk.vtkCMLMoleculeReader, | ||
# TODO: '.csv': vtk.vtkCSVReader, # vtkTables are currently not supported | ||
'.facet': vtk.vtkFacetReader, | ||
'.cas': vtk.vtkFLUENTReader, | ||
'.dat': vtk.vtkFLUENTReader, | ||
'.cube': vtk.vtkGaussianCubeReader, | ||
'.res': vtk.vtkMFIXReader, | ||
'.foam': vtk.vtkOpenFOAMReader, | ||
'.pdb': vtk.vtkPDBReader, | ||
'.p3d': vtk.vtkPlot3DMetaReader, | ||
'.pts': vtk.vtkPTSReader, | ||
'.particles': vtk.vtkParticleReader, | ||
#TODO: '.pht': vtk.vtkPhasta??????, | ||
#TODO: '.vpc': vtk.vtkVPIC?????, | ||
'.xyz': vtk.vtkXYZMolReader, | ||
} | ||
|
||
|
||
if (vtk.vtkVersion().GetVTKMajorVersion() >= 8 and | ||
vtk.vtkVersion().GetVTKMinorVersion() >= 2): | ||
READERS['.sgy'] = vtk.vtkSegYReader | ||
READERS['.segy'] = vtk.vtkSegYReader | ||
|
||
|
||
def get_ext(filename): | ||
"""Extract the extension of the filename""" | ||
ext = os.path.splitext(filename)[1].lower() | ||
return ext | ||
|
||
|
||
def get_reader(filename): | ||
"""Gets the corresponding reader based on file extension and instantiates it | ||
""" | ||
ext = get_ext(filename) | ||
return READERS[ext]() # Get and instantiate the reader | ||
|
||
|
||
def standard_reader_routine(reader, filename, attrs=None): | ||
"""Use a given reader from the ``READERS`` mapping in the common VTK reading | ||
pipeline routine. | ||
Parameters | ||
---------- | ||
reader : vtkReader | ||
Any instantiated VTK reader class | ||
filename : str | ||
The string filename to the data file to read. | ||
attrs : dict, optional | ||
A dictionary of attributes to call on the reader. Keys of dictionary are | ||
the attribute/method names and values are the arguments passed to those | ||
calls. If you do not have any attributes to call, pass ``None`` as the | ||
value. | ||
""" | ||
if attrs is None: | ||
attrs = {} | ||
if not isinstance(attrs, dict): | ||
raise TypeError('Attributes must be a dictionary of name and arguments.') | ||
reader.SetFileName(filename) | ||
# Apply any attributes listed | ||
for name, args in attrs.items(): | ||
attr = getattr(reader, name) | ||
if args is not None: | ||
if not isinstance(args, (list, tuple)): | ||
args = [args] | ||
attr(*args) | ||
else: | ||
attr() | ||
# Perform the read | ||
reader.Update() | ||
return vtki.wrap(reader.GetOutputDataObject(0)) | ||
|
||
|
||
def read_legacy(filename): | ||
"""Use VTK's legacy reader to read a file""" | ||
reader = vtk.vtkDataSetReader() | ||
reader.SetFileName(filename) | ||
# Ensure all data is fetched with poorly formated legacy files | ||
reader.ReadAllScalarsOn() | ||
reader.ReadAllColorScalarsOn() | ||
reader.ReadAllNormalsOn() | ||
reader.ReadAllTCoordsOn() | ||
reader.ReadAllVectorsOn() | ||
# Perform the read | ||
reader.Update() | ||
output = reader.GetOutputDataObject(0) | ||
if output is None: | ||
raise AssertionError('No output when using VTKs legacy reader') | ||
return vtki.wrap(output) | ||
|
||
|
||
def read(filename, attrs=None): | ||
"""This will read any VTK file! It will figure out what reader to use | ||
then wrap the VTK object for use in ``vtki``. | ||
Parameters | ||
---------- | ||
attrs : dict, optional | ||
A dictionary of attributes to call on the reader. Keys of dictionary are | ||
the attribute/method names and values are the arguments passed to those | ||
calls. If you do not have any attributes to call, pass ``None`` as the | ||
value. | ||
""" | ||
filename = os.path.abspath(os.path.expanduser(filename)) | ||
ext = get_ext(filename) | ||
|
||
# From the extension, decide which reader to use | ||
if attrs is not None: | ||
reader = get_reader(filename) | ||
return standard_reader_routine(reader, filename, attrs=attrs) | ||
elif ext in '.vti': # ImageData | ||
return vtki.UniformGrid(filename) | ||
elif ext in '.vtr': # RectilinearGrid | ||
return vtki.RectilinearGrid(filename) | ||
elif ext in '.vtu': # UnstructuredGrid | ||
return vtki.UnstructuredGrid(filename) | ||
elif ext in ['.ply', '.obj', '.stl']: # PolyData | ||
return vtki.PolyData(filename) | ||
elif ext in '.vts': # StructuredGrid | ||
return vtki.StructuredGrid(filename) | ||
elif ext in ['.vtm', '.vtmb']: | ||
return vtki.MultiBlock(filename) | ||
elif ext in ['.vtk']: | ||
# Attempt to use the legacy reader... | ||
return read_legacy(filename) | ||
else: | ||
# Attempt find a reader in the readers mapping | ||
try: | ||
reader = get_reader(filename) | ||
return standard_reader_routine(reader, filename) | ||
except KeyError: | ||
pass | ||
raise IOError("This file was not able to be automatically read by vtki.") | ||
|
||
|
||
def read_texture(filename, attrs=None): | ||
"""Loads a ``vtkTexture`` from an image file.""" | ||
filename = os.path.abspath(os.path.expanduser(filename)) | ||
try: | ||
# intitialize the reader using the extnesion to find it | ||
reader = get_reader(filename) | ||
image = standard_reader_routine(reader, filename, attrs=attrs) | ||
return vtki.image_to_texture(image) | ||
except KeyError: | ||
# Otherwise, use the imageio reader | ||
pass | ||
return vtki.numpy_to_texture(imageio.imread(filename)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters