speed up of VTK data transformation
This commit is contained in:
parent
59e8109ced
commit
da538fbce9
|
@ -6,6 +6,7 @@ import damask
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
from vtk.util import numpy_support
|
||||||
|
|
||||||
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
||||||
scriptID = ' '.join([scriptName,damask.version])
|
scriptID = ' '.join([scriptName,damask.version])
|
||||||
|
@ -14,10 +15,10 @@ scriptID = ' '.join([scriptName,damask.version])
|
||||||
# MAIN
|
# MAIN
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
|
|
||||||
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
parser = OptionParser(option_class=damask.extendableOption,
|
||||||
Add scalar and RGB tuples from ASCIItable to existing VTK point cloud (.vtp).
|
usage='%prog options [file[s]]',
|
||||||
|
description = """Add scalar and RGB tuples from ASCIItable to existing VTK point cloud (.vtp).""",
|
||||||
""", version = scriptID)
|
version = scriptID)
|
||||||
|
|
||||||
parser.add_option( '--vtk',
|
parser.add_option( '--vtk',
|
||||||
dest = 'vtk',
|
dest = 'vtk',
|
||||||
|
@ -104,60 +105,57 @@ for name in filenames:
|
||||||
for datatype,dimension,label in [['scalar',1,options.scalar],
|
for datatype,dimension,label in [['scalar',1,options.scalar],
|
||||||
['vector',3,options.vector],
|
['vector',3,options.vector],
|
||||||
['tensor',9,options.tensor],
|
['tensor',9,options.tensor],
|
||||||
['color',3,options.color],
|
['color' ,3,options.color],
|
||||||
]:
|
]:
|
||||||
for i,dim in enumerate(table.label_dimension(label)):
|
for i,dim in enumerate(table.label_dimension(label)):
|
||||||
me = label[i]
|
me = label[i]
|
||||||
if dim == -1: remarks.append('{} "{}" not found...'.format(datatype,me))
|
if dim == -1: remarks.append('{} "{}" not found...'.format(datatype,me))
|
||||||
elif dim > dimension: remarks.append('"{}" not of dimension {}...'.format(me,dimension))
|
elif dim > dimension: remarks.append('"{}" not of dimension {}...'.format(me,dimension))
|
||||||
else:
|
else:
|
||||||
remarks.append('adding {} "{}"...'.format(datatype,me))
|
remarks.append('adding {} "{}"...'.format(datatype,me))
|
||||||
active[datatype].append(me)
|
active[datatype].append(me)
|
||||||
|
|
||||||
if datatype in ['scalar','vector', 'tensor']: 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)
|
if remarks != []: damask.util.croak(remarks)
|
||||||
if errors != []:
|
if errors != []:
|
||||||
damask.util.croak(errors)
|
damask.util.croak(errors)
|
||||||
table.close(dismiss = True)
|
table.close(dismiss = True)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# ------------------------------------------ process data ---------------------------------------
|
# --------------------------------------- process and add data -----------------------------------
|
||||||
|
|
||||||
while table.data_read(): # read next data line of ASCII table
|
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 datatype,labels in active.items(): # loop over scalar,color
|
||||||
for me in labels: # loop over all requested items
|
for me in labels: # loop over all requested items
|
||||||
theData = [float(table.data[i]) for i in table.label_indexrange(me)] # read strings
|
VTKtype = vtk.VTK_DOUBLE
|
||||||
if datatype == 'color': VTKarray[me].InsertNextTuple3(*map(lambda x: int(255.*x),theData))
|
VTKdata = table.data[:, table.label_indexrange(me)].copy() # copy to force contiguous layout
|
||||||
elif datatype == 'scalar': VTKarray[me].InsertNextValue(theData[0])
|
|
||||||
elif datatype == 'vector': VTKarray[me].InsertNextTuple3(*theData)
|
|
||||||
elif datatype == 'tensor': VTKarray[me].InsertNextTuple9(*0.5*(np.array(theData)+
|
|
||||||
np.array(theData) \
|
|
||||||
.reshape(3,3).T \
|
|
||||||
.reshape(9)))
|
|
||||||
|
|
||||||
table.input_close() # close input ASCII table
|
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])
|
||||||
|
|
||||||
# ------------------------------------------ add data ---------------------------------------
|
VTKarray[me] = numpy_support.numpy_to_vtk(num_array=VTKdata,array_type=VTKtype)
|
||||||
|
VTKarray[me].SetName(me)
|
||||||
|
|
||||||
for datatype,labels in active.items(): # loop over scalar,color
|
if datatype == 'color':
|
||||||
if datatype == 'color':
|
Polydata.GetPointData().SetScalars(VTKarray[me])
|
||||||
Polydata.GetPointData().SetScalars(VTKarray[active['color'][0]])
|
Polydata.GetCellData().SetScalars(VTKarray[me])
|
||||||
Polydata.GetCellData().SetScalars(VTKarray[active['color'][0]])
|
else:
|
||||||
for me in labels: # loop over all requested items
|
Polydata.GetPointData().AddArray(VTKarray[me])
|
||||||
Polydata.GetPointData().AddArray(VTKarray[me])
|
Polydata.GetCellData().AddArray(VTKarray[me])
|
||||||
Polydata.GetCellData().AddArray(VTKarray[me])
|
|
||||||
|
|
||||||
|
table.input_close() # close input ASCII table
|
||||||
|
|
||||||
|
# ------------------------------------------ output result ---------------------------------------
|
||||||
|
|
||||||
Polydata.Modified()
|
Polydata.Modified()
|
||||||
if vtk.VTK_MAJOR_VERSION <= 5: Polydata.Update()
|
if vtk.VTK_MAJOR_VERSION <= 5: Polydata.Update()
|
||||||
|
|
||||||
# ------------------------------------------ output result ---------------------------------------
|
|
||||||
|
|
||||||
writer = vtk.vtkXMLPolyDataWriter()
|
writer = vtk.vtkXMLPolyDataWriter()
|
||||||
writer.SetDataModeToBinary()
|
writer.SetDataModeToBinary()
|
||||||
writer.SetCompressorTypeToZLib()
|
writer.SetCompressorTypeToZLib()
|
||||||
|
@ -175,7 +173,7 @@ if options.render:
|
||||||
actor.SetMapper(mapper)
|
actor.SetMapper(mapper)
|
||||||
|
|
||||||
# Create the graphics structure. The renderer renders into the
|
# Create the graphics structure. The renderer renders into the
|
||||||
# render window. The render window interactor captures mouse events
|
# render window. The render window interactively captures mouse events
|
||||||
# and will perform appropriate camera or actor manipulation
|
# and will perform appropriate camera or actor manipulation
|
||||||
# depending on the nature of the events.
|
# depending on the nature of the events.
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
import os,vtk
|
import os,vtk
|
||||||
import damask
|
import damask
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from vtk.util import numpy_support
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
|
||||||
|
@ -102,22 +103,16 @@ for name in filenames:
|
||||||
for datatype,dimension,label in [['scalar',1,options.scalar],
|
for datatype,dimension,label in [['scalar',1,options.scalar],
|
||||||
['vector',3,options.vector],
|
['vector',3,options.vector],
|
||||||
['tensor',9,options.tensor],
|
['tensor',9,options.tensor],
|
||||||
['color',3,options.color],
|
['color' ,3,options.color],
|
||||||
]:
|
]:
|
||||||
for i,dim in enumerate(table.label_dimension(label)):
|
for i,dim in enumerate(table.label_dimension(label)):
|
||||||
me = label[i]
|
me = label[i]
|
||||||
if dim == -1: remarks.append('{} "{}" not found...'.format(datatype,me))
|
if dim == -1: remarks.append('{} "{}" not found...'.format(datatype,me))
|
||||||
elif dim > dimension: remarks.append('"{}" not of dimension {}...'.format(me,dimension))
|
elif dim > dimension: remarks.append('"{}" not of dimension {}...'.format(me,dimension))
|
||||||
else:
|
else:
|
||||||
remarks.append('adding {} "{}"...'.format(datatype,me))
|
remarks.append('adding {} "{}"...'.format(datatype,me))
|
||||||
active[datatype].append(me)
|
active[datatype].append(me)
|
||||||
|
|
||||||
if datatype in ['scalar','vector','tensor']: 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)
|
if remarks != []: damask.util.croak(remarks)
|
||||||
if errors != []:
|
if errors != []:
|
||||||
damask.util.croak(errors)
|
damask.util.croak(errors)
|
||||||
|
@ -126,28 +121,30 @@ for name in filenames:
|
||||||
|
|
||||||
# ------------------------------------------ process data ---------------------------------------
|
# ------------------------------------------ process data ---------------------------------------
|
||||||
|
|
||||||
datacount = 0
|
table.data_readArray([item for sublist in active.values() for item in sublist]) # read all requested data
|
||||||
|
|
||||||
while table.data_read(): # read next data line of ASCII table
|
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
|
||||||
|
|
||||||
datacount += 1 # count data lines
|
if datatype == 'color':
|
||||||
for datatype,labels in active.items(): # loop over scalar,color
|
VTKtype = vtk.VTK_UNSIGNED_CHAR
|
||||||
for me in labels: # loop over all requested items
|
VTKdata = (VTKdata*255).astype(int) # translate to 0..255 UCHAR
|
||||||
theData = [table.data[i] for i in table.label_indexrange(me)] # read strings
|
elif datatype == 'tensor':
|
||||||
if datatype == 'color': VTKarray[me].InsertNextTuple3(*map(lambda x: int(255.*float(x)),theData))
|
VTKdata[:,1] = VTKdata[:,3] = 0.5*(VTKdata[:,1]+VTKdata[:,3])
|
||||||
elif datatype == 'scalar': VTKarray[me].InsertNextValue(float(theData[0]))
|
VTKdata[:,2] = VTKdata[:,6] = 0.5*(VTKdata[:,2]+VTKdata[:,6])
|
||||||
elif datatype == 'vector': VTKarray[me].InsertNextTuple3(*map(float,theData))
|
VTKdata[:,5] = VTKdata[:,7] = 0.5*(VTKdata[:,5]+VTKdata[:,7])
|
||||||
elif datatype == 'tensor': VTKarray[me].InsertNextTuple9(*0.5*(np.array(theData)+
|
|
||||||
np.array(theData) \
|
VTKarray[me] = numpy_support.numpy_to_vtk(num_array=VTKdata,array_type=VTKtype)
|
||||||
.reshape(3,3).T \
|
VTKarray[me].SetName(me)
|
||||||
.reshape(9)))
|
|
||||||
|
|
||||||
table.close() # close input ASCII table
|
table.close() # close input ASCII table
|
||||||
|
|
||||||
# ------------------------------------------ add data ---------------------------------------
|
# ------------------------------------------ add data ---------------------------------------
|
||||||
|
|
||||||
if datacount == Npoints: mode = 'point'
|
if len(table.data) == Npoints: mode = 'point'
|
||||||
elif datacount == Ncells: mode = 'cell'
|
elif len(table.data) == Ncells: mode = 'cell'
|
||||||
else:
|
else:
|
||||||
damask.util.croak('Data count is incompatible with grid...')
|
damask.util.croak('Data count is incompatible with grid...')
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Reference in New Issue