DAMASK_EICMD/processing/post/vtk_addVoxelcloudData.py

136 lines
5.0 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- coding: UTF-8 no BOM -*-
import os,sys,vtk
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
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)
parser.add_option('-v', '--vtk', dest='vtk', \
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,
)
(options, filenames) = parser.parse_args()
2014-04-03 06:30:00 +05:30
if options.vtk == None or not os.path.exists(options.vtk):
parser.error('VTK file does not exist')
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()
2015-09-08 20:56:40 +05:30
sys.stderr.write('{}: {} points and {} cells...\n'.format(damask.util.emph(options.vtk),Npoints,Ncells))
2015-09-08 20:56:40 +05:30
# --- loop over input files -------------------------------------------------------------------------
2015-09-08 20:56:40 +05:30
if filenames == []: filenames = [None]
2015-09-08 20:56:40 +05:30
for name in filenames:
try:
table = damask.ASCIItable(name = name,
buffered = False, readonly = True)
except: continue
damask.util.croak(damask.util.emph(scriptName)+(': '+name if name else ''))
2015-09-08 20:56:40 +05:30
# --- interpret header ----------------------------------------------------------------------------
2015-09-08 20:56:40 +05:30
table.head_read()
2015-09-08 20:56:40 +05:30
remarks = []
errors = []
VTKarray = {}
active = defaultdict(list)
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:
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])
if remarks != []: damask.util.croak(remarks)
2015-09-08 20:56:40 +05:30
if errors != []:
damask.util.croak(errors)
2015-09-08 20:56:40 +05:30
table.close(dismiss=True)
continue
# ------------------------------------------ process data ---------------------------------------
2015-09-08 20:56:40 +05:30
while table.data_read(): # read next data line of ASCII table
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
if datatype == 'color':
2015-09-08 20:56:40 +05:30
VTKarray[me].InsertNextTuple3(*map(lambda x: int(255.*float(x)),theData))
elif datatype == 'scalar':
2015-09-08 20:56:40 +05:30
VTKarray[me].InsertNextValue(float(theData[0]))
# ------------------------------------------ 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]])
for label in labels: # loop over all requested items
2015-09-08 20:56:40 +05:30
uGrid.GetCellData().AddArray(VTKarray[me])
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()