2014-01-27 19:35:03 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2014-01-27 19:35:03 +05:30
|
|
|
|
2015-09-24 14:54:42 +05:30
|
|
|
import os,sys,string,vtk
|
2014-01-27 19:35:03 +05:30
|
|
|
import damask
|
2015-09-08 20:56:40 +05:30
|
|
|
from collections import defaultdict
|
2014-12-19 00:56:52 +05:30
|
|
|
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])
|
2014-12-19 00:56:52 +05:30
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2015-01-08 00:59:27 +05:30
|
|
|
Add scalar and RGB tuples from ASCIItable to existing VTK voxel cloud (.vtu/.vtk).
|
2014-12-19 00:56:52 +05:30
|
|
|
|
|
|
|
""", version = scriptID)
|
2014-01-27 19:35:03 +05:30
|
|
|
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('-v', '--vtk', dest='vtk', \
|
2014-01-27 19:35:03 +05:30
|
|
|
help = 'VTK file name')
|
|
|
|
parser.add_option('-s', '--scalar', dest='scalar', action='extend', \
|
|
|
|
help = 'scalar values')
|
|
|
|
parser.add_option('-c', '--color', dest='color', action='extend', \
|
|
|
|
help = 'RGB color tuples')
|
|
|
|
|
2015-09-08 20:56:40 +05:30
|
|
|
parser.set_defaults(scalar = [],
|
|
|
|
color = [],
|
|
|
|
render = False,
|
|
|
|
)
|
2014-01-27 19:35:03 +05:30
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
2014-04-03 06:30:00 +05:30
|
|
|
if options.vtk == None or not os.path.exists(options.vtk):
|
2014-01-27 20:00:37 +05:30
|
|
|
parser.error('VTK file does not exist')
|
2014-01-27 19:35:03 +05:30
|
|
|
|
2014-02-25 00:53:08 +05:30
|
|
|
if os.path.splitext(options.vtk)[1] == '.vtu':
|
|
|
|
reader = vtk.vtkXMLUnstructuredGridReader()
|
|
|
|
reader.SetFileName(options.vtk)
|
|
|
|
reader.Update()
|
|
|
|
uGrid = reader.GetOutput()
|
|
|
|
elif os.path.splitext(options.vtk)[1] == '.vtk':
|
|
|
|
reader = vtk.vtkGenericDataObjectReader()
|
|
|
|
reader.SetFileName(options.vtk)
|
|
|
|
reader.Update()
|
|
|
|
uGrid = reader.GetUnstructuredGridOutput()
|
|
|
|
else:
|
|
|
|
parser.error('unsupported VTK file type extension')
|
|
|
|
|
|
|
|
Npoints = uGrid.GetNumberOfPoints()
|
|
|
|
Ncells = uGrid.GetNumberOfCells()
|
2014-01-27 19:35:03 +05:30
|
|
|
|
2015-09-08 20:56:40 +05:30
|
|
|
sys.stderr.write('{}: {} points and {} cells...\n'.format(damask.util.emph(options.vtk),Npoints,Ncells))
|
2014-01-27 19:35:03 +05:30
|
|
|
|
2015-09-08 20:56:40 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2014-01-27 19:35:03 +05:30
|
|
|
|
2015-09-08 20:56:40 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2014-01-27 19:35:03 +05:30
|
|
|
|
2015-09-08 20:56:40 +05:30
|
|
|
for name in filenames:
|
|
|
|
try:
|
|
|
|
table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False, readonly = True)
|
|
|
|
except: continue
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.croak(damask.util.emph(scriptName)+(': '+name if name else ''))
|
2014-01-27 19:35:03 +05:30
|
|
|
|
2015-09-08 20:56:40 +05:30
|
|
|
# --- interpret header ----------------------------------------------------------------------------
|
2014-01-27 19:35:03 +05:30
|
|
|
|
2015-09-08 20:56:40 +05:30
|
|
|
table.head_read()
|
2014-01-27 19:35:03 +05:30
|
|
|
|
2015-09-08 20:56:40 +05:30
|
|
|
remarks = []
|
|
|
|
errors = []
|
|
|
|
VTKarray = {}
|
|
|
|
active = defaultdict(list)
|
2014-01-27 19:35:03 +05:30
|
|
|
|
2015-09-08 20:56:40 +05:30
|
|
|
for datatype,dimension,label in [['scalar',1,options.scalar],
|
|
|
|
['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))
|
|
|
|
elif dim > dimension: remarks.append('"{}" not of dimension{}...'.format(me,dimension))
|
|
|
|
else:
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.croak('adding {} {}'.format(datatype,me))
|
2015-09-08 20:56:40 +05:30
|
|
|
active[datatype].append(me)
|
|
|
|
|
|
|
|
if datatype in ['scalar']:
|
|
|
|
VTKarray[me] = vtk.vtkDoubleArray()
|
|
|
|
elif datatype == 'color':
|
|
|
|
VTKarray[me] = vtk.vtkUnsignedCharArray()
|
|
|
|
|
|
|
|
VTKarray[me].SetNumberOfComponents(dimension)
|
|
|
|
VTKarray[me].SetName(label[i])
|
|
|
|
|
2015-09-24 14:54:42 +05:30
|
|
|
if remarks != []: damask.util.croak(remarks)
|
2015-09-08 20:56:40 +05:30
|
|
|
if errors != []:
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.croak(errors)
|
2015-09-08 20:56:40 +05:30
|
|
|
table.close(dismiss=True)
|
|
|
|
continue
|
2014-01-27 19:35:03 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ process data ---------------------------------------
|
|
|
|
|
2015-09-08 20:56:40 +05:30
|
|
|
while table.data_read(): # read next data line of ASCII table
|
2014-01-27 19:35:03 +05:30
|
|
|
|
2015-09-08 20:56:40 +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
|
2014-01-27 19:35:03 +05:30
|
|
|
if datatype == 'color':
|
2015-09-08 20:56:40 +05:30
|
|
|
VTKarray[me].InsertNextTuple3(*map(lambda x: int(255.*float(x)),theData))
|
2014-01-27 19:35:03 +05:30
|
|
|
elif datatype == 'scalar':
|
2015-09-08 20:56:40 +05:30
|
|
|
VTKarray[me].InsertNextValue(float(theData[0]))
|
2014-01-27 19:35:03 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ add data ---------------------------------------
|
|
|
|
|
|
|
|
for datatype,labels in active.items(): # loop over scalar,color
|
|
|
|
if datatype == 'color':
|
2015-09-08 20:56:40 +05:30
|
|
|
uGrid.GetCellData().SetScalars(VTKarray[active['color'][0]])
|
2014-01-27 19:35:03 +05:30
|
|
|
for label in labels: # loop over all requested items
|
2015-09-08 20:56:40 +05:30
|
|
|
uGrid.GetCellData().AddArray(VTKarray[me])
|
2014-01-27 19:35:03 +05:30
|
|
|
|
|
|
|
uGrid.Modified()
|
|
|
|
if vtk.VTK_MAJOR_VERSION <= 5:
|
|
|
|
uGrid.Update()
|
|
|
|
|
|
|
|
# ------------------------------------------ output result ---------------------------------------
|
|
|
|
|
|
|
|
writer = vtk.vtkXMLUnstructuredGridWriter()
|
|
|
|
writer.SetDataModeToBinary()
|
|
|
|
writer.SetCompressorTypeToZLib()
|
|
|
|
writer.SetFileName(os.path.splitext(options.vtk)[0]+'_added.vtu')
|
|
|
|
if vtk.VTK_MAJOR_VERSION <= 5:
|
|
|
|
writer.SetInput(uGrid)
|
|
|
|
else:
|
|
|
|
writer.SetInputData(uGrid)
|
|
|
|
writer.Write()
|