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
|
2016-09-09 03:35:49 +05:30
|
|
|
from vtk.util import numpy_support
|
2015-08-25 06:09:37 +05:30
|
|
|
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)')
|
2016-09-02 02:27:29 +05:30
|
|
|
parser.add_option('-t', '--tensor',
|
|
|
|
dest = 'tensor',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = 'tensor (3x3) value label(s)')
|
2015-08-25 06:09:37 +05:30
|
|
|
parser.add_option('-c', '--color',
|
|
|
|
dest = 'color',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = 'RGB color tuple label')
|
|
|
|
|
|
|
|
parser.set_defaults(scalar = [],
|
|
|
|
vector = [],
|
2016-09-02 02:27:29 +05:30
|
|
|
tensor = [],
|
2015-08-25 06:09:37 +05:30
|
|
|
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)
|
2016-09-02 02:27:29 +05:30
|
|
|
|
2015-08-25 06:09:37 +05:30
|
|
|
for datatype,dimension,label in [['scalar',1,options.scalar],
|
2016-09-14 22:07:07 +05:30
|
|
|
['vector',99,options.vector],
|
2016-09-02 02:27:29 +05:30
|
|
|
['tensor',9,options.tensor],
|
2016-09-09 03:35:49 +05:30
|
|
|
['color' ,3,options.color],
|
2015-08-25 06:09:37 +05:30
|
|
|
]:
|
|
|
|
for i,dim in enumerate(table.label_dimension(label)):
|
|
|
|
me = label[i]
|
2016-09-09 03:35:49 +05:30
|
|
|
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-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
|
|
|
|
|
2016-09-02 02:27:29 +05:30
|
|
|
# ------------------------------------------ process data ---------------------------------------
|
2015-08-25 06:09:37 +05:30
|
|
|
|
2016-09-09 03:35:49 +05:30
|
|
|
table.data_readArray([item for sublist in active.values() for item in sublist]) # read all requested data
|
|
|
|
|
|
|
|
for datatype,labels in active.items(): # loop over scalar,color
|
|
|
|
for me in labels: # loop over all requested items
|
|
|
|
VTKtype = vtk.VTK_DOUBLE
|
|
|
|
VTKdata = table.data[:, table.label_indexrange(me)].copy() # copy to force contiguous layout
|
2016-04-15 23:36:01 +05:30
|
|
|
|
2016-09-09 03:35:49 +05:30
|
|
|
if datatype == 'color':
|
|
|
|
VTKtype = vtk.VTK_UNSIGNED_CHAR
|
|
|
|
VTKdata = (VTKdata*255).astype(int) # translate to 0..255 UCHAR
|
|
|
|
elif datatype == 'tensor':
|
|
|
|
VTKdata[:,1] = VTKdata[:,3] = 0.5*(VTKdata[:,1]+VTKdata[:,3])
|
|
|
|
VTKdata[:,2] = VTKdata[:,6] = 0.5*(VTKdata[:,2]+VTKdata[:,6])
|
|
|
|
VTKdata[:,5] = VTKdata[:,7] = 0.5*(VTKdata[:,5]+VTKdata[:,7])
|
2016-04-15 23:36:01 +05:30
|
|
|
|
2016-09-15 00:36:00 +05:30
|
|
|
VTKarray[me] = numpy_support.numpy_to_vtk(num_array=VTKdata,deep=True,array_type=VTKtype)
|
2016-09-09 03:35:49 +05:30
|
|
|
VTKarray[me].SetName(me)
|
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
|
|
|
|
2016-09-02 02:27:29 +05:30
|
|
|
# ------------------------------------------ add data ---------------------------------------
|
2015-08-25 06:09:37 +05:30
|
|
|
|
2016-09-09 03:35:49 +05:30
|
|
|
if len(table.data) == Npoints: mode = 'point'
|
|
|
|
elif len(table.data) == Ncells: mode = 'cell'
|
2016-04-15 23:36:01 +05:30
|
|
|
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()
|
|
|
|
|
2016-09-02 02:27:29 +05:30
|
|
|
# ------------------------------------------ output result ---------------------------------------
|
2015-08-25 06:09:37 +05:30
|
|
|
|
|
|
|
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()
|
|
|
|
|
2016-09-02 02:27:29 +05:30
|
|
|
# ------------------------------------------ render result ---------------------------------------
|
2015-08-25 06:09:37 +05:30
|
|
|
|
|
|
|
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)
|
2016-09-02 02:27:29 +05:30
|
|
|
|
2015-08-25 06:09:37 +05:30
|
|
|
iren.Initialize()
|
|
|
|
renWin.Render()
|
|
|
|
iren.Start()
|