From da538fbce9a9a72a55ca1e36f93ee14b1a6068d2 Mon Sep 17 00:00:00 2001 From: chen Date: Thu, 8 Sep 2016 18:05:49 -0400 Subject: [PATCH] speed up of VTK data transformation --- processing/post/vtk_addPointcloudData.py | 72 +++++++++---------- processing/post/vtk_addRectilinearGridData.py | 43 ++++++----- 2 files changed, 55 insertions(+), 60 deletions(-) diff --git a/processing/post/vtk_addPointcloudData.py b/processing/post/vtk_addPointcloudData.py index 9736e54d0..6a8324567 100755 --- a/processing/post/vtk_addPointcloudData.py +++ b/processing/post/vtk_addPointcloudData.py @@ -6,6 +6,7 @@ import damask import numpy as np from collections import defaultdict from optparse import OptionParser +from vtk.util import numpy_support scriptName = os.path.splitext(os.path.basename(__file__))[0] scriptID = ' '.join([scriptName,damask.version]) @@ -14,10 +15,10 @@ scriptID = ' '.join([scriptName,damask.version]) # MAIN # -------------------------------------------------------------------- -parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """ -Add scalar and RGB tuples from ASCIItable to existing VTK point cloud (.vtp). - -""", version = scriptID) +parser = OptionParser(option_class=damask.extendableOption, + usage='%prog options [file[s]]', + description = """Add scalar and RGB tuples from ASCIItable to existing VTK point cloud (.vtp).""", + version = scriptID) parser.add_option( '--vtk', dest = 'vtk', @@ -104,60 +105,57 @@ for name in filenames: for datatype,dimension,label in [['scalar',1,options.scalar], ['vector',3,options.vector], ['tensor',9,options.tensor], - ['color',3,options.color], + ['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)) + if dim == -1: remarks.append('{} "{}" not found...'.format(datatype,me)) + elif dim > dimension: remarks.append('"{}" not of dimension {}...'.format(me,dimension)) else: remarks.append('adding {} "{}"...'.format(datatype,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 errors != []: damask.util.croak(errors) table.close(dismiss = True) 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 me in labels: # loop over all requested items - theData = [float(table.data[i]) for i in table.label_indexrange(me)] # read strings - if datatype == 'color': VTKarray[me].InsertNextTuple3(*map(lambda x: int(255.*x),theData)) - 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))) + 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 - 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': - Polydata.GetPointData().SetScalars(VTKarray[active['color'][0]]) - Polydata.GetCellData().SetScalars(VTKarray[active['color'][0]]) - for me in labels: # loop over all requested items - Polydata.GetPointData().AddArray(VTKarray[me]) - Polydata.GetCellData().AddArray(VTKarray[me]) + if datatype == 'color': + Polydata.GetPointData().SetScalars(VTKarray[me]) + Polydata.GetCellData().SetScalars(VTKarray[me]) + else: + Polydata.GetPointData().AddArray(VTKarray[me]) + Polydata.GetCellData().AddArray(VTKarray[me]) + + + table.input_close() # close input ASCII table + +# ------------------------------------------ output result --------------------------------------- Polydata.Modified() if vtk.VTK_MAJOR_VERSION <= 5: Polydata.Update() -# ------------------------------------------ output result --------------------------------------- - writer = vtk.vtkXMLPolyDataWriter() writer.SetDataModeToBinary() writer.SetCompressorTypeToZLib() @@ -175,7 +173,7 @@ if options.render: actor.SetMapper(mapper) # 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 # depending on the nature of the events. diff --git a/processing/post/vtk_addRectilinearGridData.py b/processing/post/vtk_addRectilinearGridData.py index df85288af..f8d483bf0 100755 --- a/processing/post/vtk_addRectilinearGridData.py +++ b/processing/post/vtk_addRectilinearGridData.py @@ -4,6 +4,7 @@ import os,vtk import damask import numpy as np +from vtk.util import numpy_support from collections import defaultdict from optparse import OptionParser @@ -102,22 +103,16 @@ for name in filenames: for datatype,dimension,label in [['scalar',1,options.scalar], ['vector',3,options.vector], ['tensor',9,options.tensor], - ['color',3,options.color], + ['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)) + if dim == -1: remarks.append('{} "{}" not found...'.format(datatype,me)) elif dim > dimension: remarks.append('"{}" not of dimension {}...'.format(me,dimension)) else: remarks.append('adding {} "{}"...'.format(datatype,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 errors != []: damask.util.croak(errors) @@ -126,28 +121,30 @@ for name in filenames: # ------------------------------------------ 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 - 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': VTKarray[me].InsertNextTuple3(*map(lambda x: int(255.*float(x)),theData)) - elif datatype == 'scalar': VTKarray[me].InsertNextValue(float(theData[0])) - elif datatype == 'vector': VTKarray[me].InsertNextTuple3(*map(float,theData)) - elif datatype == 'tensor': VTKarray[me].InsertNextTuple9(*0.5*(np.array(theData)+ - np.array(theData) \ - .reshape(3,3).T \ - .reshape(9))) + 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]) + + VTKarray[me] = numpy_support.numpy_to_vtk(num_array=VTKdata,array_type=VTKtype) + VTKarray[me].SetName(me) table.close() # close input ASCII table # ------------------------------------------ add data --------------------------------------- - if datacount == Npoints: mode = 'point' - elif datacount == Ncells: mode = 'cell' + if len(table.data) == Npoints: mode = 'point' + elif len(table.data) == Ncells: mode = 'cell' else: damask.util.croak('Data count is incompatible with grid...') continue