2016-07-18 23:05:35 +05:30
|
|
|
#!/usr/bin/env python2.7
|
2015-08-25 06:09:37 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2016-03-01 22:55:14 +05:30
|
|
|
import os,vtk
|
2015-08-25 06:09:37 +05:30
|
|
|
import damask
|
|
|
|
from collections import defaultdict
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2015-08-25 06:09:37 +05:30
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
|
|
|
Add scalars, vectors, and/or an RGB tuple from an ASCIItable to existing VTK rectilinear grid (.vtr/.vtk).
|
|
|
|
|
|
|
|
""", version = scriptID)
|
|
|
|
|
|
|
|
parser.add_option( '--vtk',
|
|
|
|
dest = 'vtk',
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
help = 'VTK file name')
|
2015-12-04 10:03:23 +05:30
|
|
|
parser.add_option( '--inplace',
|
|
|
|
dest = 'inplace',
|
|
|
|
action = 'store_true',
|
|
|
|
help = 'modify VTK file in-place')
|
|
|
|
parser.add_option('-r', '--render',
|
|
|
|
dest = 'render',
|
|
|
|
action = 'store_true',
|
|
|
|
help = 'open output in VTK render window')
|
2015-08-25 06:09:37 +05:30
|
|
|
parser.add_option('-s', '--scalar',
|
|
|
|
dest = 'scalar',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = 'scalar value label(s)')
|
|
|
|
parser.add_option('-v', '--vector',
|
|
|
|
dest = 'vector',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = 'vector value label(s)')
|
|
|
|
parser.add_option('-c', '--color',
|
|
|
|
dest = 'color',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = 'RGB color tuple label')
|
|
|
|
|
|
|
|
parser.set_defaults(scalar = [],
|
|
|
|
vector = [],
|
|
|
|
color = [],
|
2015-12-03 04:40:14 +05:30
|
|
|
inplace = False,
|
2015-08-25 06:09:37 +05:30
|
|
|
render = False,
|
|
|
|
)
|
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
2015-12-04 10:03:23 +05:30
|
|
|
if not options.vtk: parser.error('No VTK file specified.')
|
|
|
|
if not os.path.exists(options.vtk): parser.error('VTK file does not exist.')
|
2015-08-25 06:09:37 +05:30
|
|
|
|
|
|
|
if os.path.splitext(options.vtk)[1] == '.vtr':
|
|
|
|
reader = vtk.vtkXMLRectilinearGridReader()
|
|
|
|
reader.SetFileName(options.vtk)
|
|
|
|
reader.Update()
|
|
|
|
rGrid = reader.GetOutput()
|
|
|
|
elif os.path.splitext(options.vtk)[1] == '.vtk':
|
|
|
|
reader = vtk.vtkGenericDataObjectReader()
|
|
|
|
reader.SetFileName(options.vtk)
|
|
|
|
reader.Update()
|
|
|
|
rGrid = reader.GetRectilinearGridOutput()
|
|
|
|
else:
|
2015-12-04 10:03:23 +05:30
|
|
|
parser.error('Unsupported VTK file type extension.')
|
2015-08-25 06:09:37 +05:30
|
|
|
|
|
|
|
Npoints = rGrid.GetNumberOfPoints()
|
|
|
|
Ncells = rGrid.GetNumberOfCells()
|
|
|
|
|
2015-12-04 10:03:23 +05:30
|
|
|
damask.util.croak('{}: {} points and {} cells...'.format(options.vtk,Npoints,Ncells))
|
2015-08-25 06:09:37 +05:30
|
|
|
|
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
if filenames == []: filenames = [None]
|
|
|
|
|
|
|
|
for name in filenames:
|
2016-04-14 03:18:49 +05:30
|
|
|
try: table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False,
|
|
|
|
readonly = True)
|
2015-08-25 06:09:37 +05:30
|
|
|
except: continue
|
2015-11-10 07:19:32 +05:30
|
|
|
damask.util.report(scriptName, name)
|
2015-08-25 06:09:37 +05:30
|
|
|
|
|
|
|
# --- interpret header ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
table.head_read()
|
|
|
|
|
|
|
|
remarks = []
|
|
|
|
errors = []
|
|
|
|
VTKarray = {}
|
|
|
|
active = defaultdict(list)
|
|
|
|
|
|
|
|
for datatype,dimension,label in [['scalar',1,options.scalar],
|
|
|
|
['vector',3,options.vector],
|
|
|
|
['color',3,options.color],
|
|
|
|
]:
|
|
|
|
for i,dim in enumerate(table.label_dimension(label)):
|
|
|
|
me = label[i]
|
|
|
|
if dim == -1: remarks.append('{} "{}" not found...'.format(datatype,me))
|
2015-11-10 07:19:32 +05:30
|
|
|
elif dim > dimension: remarks.append('"{}" not of dimension {}...'.format(me,dimension))
|
2015-08-25 06:09:37 +05:30
|
|
|
else:
|
2015-11-10 07:19:32 +05:30
|
|
|
remarks.append('adding {} "{}"...'.format(datatype,me))
|
2015-08-25 06:09:37 +05:30
|
|
|
active[datatype].append(me)
|
|
|
|
|
2015-11-10 07:19:32 +05:30
|
|
|
if datatype in ['scalar','vector']: VTKarray[me] = vtk.vtkDoubleArray()
|
|
|
|
elif datatype == 'color': VTKarray[me] = vtk.vtkUnsignedCharArray()
|
2015-08-25 06:09:37 +05:30
|
|
|
|
|
|
|
VTKarray[me].SetNumberOfComponents(dimension)
|
|
|
|
VTKarray[me].SetName(label[i])
|
|
|
|
|
2015-09-24 14:54:42 +05:30
|
|
|
if remarks != []: damask.util.croak(remarks)
|
2015-08-25 06:09:37 +05:30
|
|
|
if errors != []:
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.croak(errors)
|
2015-11-10 07:19:32 +05:30
|
|
|
table.close(dismiss = True)
|
2015-08-25 06:09:37 +05:30
|
|
|
continue
|
|
|
|
|
|
|
|
# ------------------------------------------ process data ---------------------------------------
|
|
|
|
|
2016-04-15 23:36:01 +05:30
|
|
|
datacount = 0
|
|
|
|
|
2015-08-25 06:09:37 +05:30
|
|
|
while table.data_read(): # read next data line of ASCII table
|
2016-04-15 23:36:01 +05:30
|
|
|
|
|
|
|
datacount += 1 # count data lines
|
2015-08-25 06:09:37 +05:30
|
|
|
for datatype,labels in active.items(): # loop over scalar,color
|
|
|
|
for me in labels: # loop over all requested items
|
|
|
|
theData = [table.data[i] for i in table.label_indexrange(me)] # read strings
|
2015-12-04 10:03:23 +05:30
|
|
|
if datatype == 'color': VTKarray[me].InsertNextTuple3(*map(lambda x: int(255.*float(x)),theData))
|
2015-11-10 07:19:32 +05:30
|
|
|
elif datatype == 'vector': VTKarray[me].InsertNextTuple3(*map(float,theData))
|
|
|
|
elif datatype == 'scalar': VTKarray[me].InsertNextValue(float(theData[0]))
|
2015-08-25 06:09:37 +05:30
|
|
|
|
2016-04-15 23:36:01 +05:30
|
|
|
table.close() # close input ASCII table
|
2016-04-14 03:18:49 +05:30
|
|
|
|
2015-08-25 06:09:37 +05:30
|
|
|
# ------------------------------------------ add data ---------------------------------------
|
|
|
|
|
2016-04-15 23:36:01 +05:30
|
|
|
if datacount == Npoints: mode = 'point'
|
|
|
|
elif datacount == Ncells: mode = 'cell'
|
|
|
|
else:
|
|
|
|
damask.util.croak('Data count is incompatible with grid...')
|
|
|
|
continue
|
|
|
|
|
|
|
|
damask.util.croak('{} mode...'.format(mode))
|
|
|
|
|
2015-08-25 06:09:37 +05:30
|
|
|
for datatype,labels in active.items(): # loop over scalar,color
|
|
|
|
if datatype == 'color':
|
2016-04-15 23:36:01 +05:30
|
|
|
if mode == 'cell': rGrid.GetCellData().SetScalars(VTKarray[active['color'][0]])
|
|
|
|
elif mode == 'point': rGrid.GetPointData().SetScalars(VTKarray[active['color'][0]])
|
2015-08-25 06:09:37 +05:30
|
|
|
for me in labels: # loop over all requested items
|
2016-04-15 23:36:01 +05:30
|
|
|
if mode == 'cell': rGrid.GetCellData().AddArray(VTKarray[me])
|
|
|
|
elif mode == 'point': rGrid.GetPointData().AddArray(VTKarray[me])
|
2015-08-25 06:09:37 +05:30
|
|
|
|
|
|
|
rGrid.Modified()
|
|
|
|
if vtk.VTK_MAJOR_VERSION <= 5: rGrid.Update()
|
|
|
|
|
|
|
|
# ------------------------------------------ output result ---------------------------------------
|
|
|
|
|
|
|
|
writer = vtk.vtkXMLRectilinearGridWriter()
|
|
|
|
writer.SetDataModeToBinary()
|
|
|
|
writer.SetCompressorTypeToZLib()
|
2016-03-16 00:22:10 +05:30
|
|
|
writer.SetFileName(os.path.splitext(options.vtk)[0]+('.vtr' if options.inplace else '_added.vtr'))
|
2015-08-25 06:09:37 +05:30
|
|
|
if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(rGrid)
|
|
|
|
else: writer.SetInputData(rGrid)
|
|
|
|
writer.Write()
|
|
|
|
|
|
|
|
# ------------------------------------------ render result ---------------------------------------
|
|
|
|
|
|
|
|
if options.render:
|
|
|
|
mapper = vtk.vtkDataSetMapper()
|
|
|
|
mapper.SetInputData(rGrid)
|
|
|
|
actor = vtk.vtkActor()
|
|
|
|
actor.SetMapper(mapper)
|
|
|
|
|
2016-03-02 15:22:24 +05:30
|
|
|
# Create the graphics structure. The renderer renders into the
|
|
|
|
# render window. The render window interactor captures mouse events
|
|
|
|
# and will perform appropriate camera or actor manipulation
|
|
|
|
# depending on the nature of the events.
|
2015-08-25 06:09:37 +05:30
|
|
|
|
|
|
|
ren = vtk.vtkRenderer()
|
|
|
|
|
|
|
|
renWin = vtk.vtkRenderWindow()
|
|
|
|
renWin.AddRenderer(ren)
|
|
|
|
|
|
|
|
ren.AddActor(actor)
|
|
|
|
ren.SetBackground(1, 1, 1)
|
|
|
|
renWin.SetSize(200, 200)
|
|
|
|
|
|
|
|
iren = vtk.vtkRenderWindowInteractor()
|
|
|
|
iren.SetRenderWindow(renWin)
|
|
|
|
|
|
|
|
iren.Initialize()
|
|
|
|
renWin.Render()
|
|
|
|
iren.Start()
|