unified structure and names, added test for vtk{point/voxel}cloud

This commit is contained in:
Martin Diehl 2015-10-09 11:24:26 +00:00
parent bbb6ac8852
commit fc9d290339
2 changed files with 68 additions and 95 deletions

View File

@ -17,12 +17,12 @@ Produce a VTK point cloud dataset based on coordinates given in an ASCIItable.
""", version = scriptID) """, version = scriptID)
parser.add_option('-p','--position', parser.add_option('-d', '--deformed',
dest = 'position', dest = 'deformed',
type = 'string', metavar = 'string', type = 'string', metavar = 'string',
help = 'column label for coordinates [%default]') help = 'deformed coordinate label [%default]')
parser.set_defaults(position = 'pos', parser.set_defaults(deformed = 'ipdeformedcoord'
) )
(options, filenames) = parser.parse_args() (options, filenames) = parser.parse_args()
@ -36,7 +36,7 @@ for name in filenames:
table = damask.ASCIItable(name = name, table = damask.ASCIItable(name = name,
buffered = False, readonly = True) buffered = False, readonly = True)
except: continue except: continue
damask.util.croak(damask.util.emph(scriptName)+(': '+name if name else '')) damask.util.report(scriptName,name)
# --- interpret header ---------------------------------------------------------------------------- # --- interpret header ----------------------------------------------------------------------------
@ -44,8 +44,10 @@ for name in filenames:
errors = [] errors = []
remarks = [] remarks = []
if table.label_dimension(options.position) != 3: errors.append('columns "{}" have dimension {}'.format(options.position, if table.label_dimension(options.deformed) != 3:
table.label_dimension(options.position))) errors.append('columns "{}" have dimension {}'.format(options.deformed,
table.label_dimension(options.deformed)))
if remarks != []: damask.util.croak(remarks) if remarks != []: damask.util.croak(remarks)
if errors != []: if errors != []:
damask.util.croak(errors) damask.util.croak(errors)
@ -54,16 +56,16 @@ for name in filenames:
# ------------------------------------------ process data --------------------------------------- # ------------------------------------------ process data ---------------------------------------
table.data_readArray(options.position) table.data_readArray(options.deformed)
Polydata = vtk.vtkPolyData() Polydata = vtk.vtkPolyData()
Points = vtk.vtkPoints() Points = vtk.vtkPoints()
Vertices = vtk.vtkCellArray() Vertices = vtk.vtkCellArray()
for p in table.data: for p in table.data:
id = Points.InsertNextPoint(p) pointID = Points.InsertNextPoint(p)
Vertices.InsertNextCell(1) Vertices.InsertNextCell(1)
Vertices.InsertCellPoint(id) Vertices.InsertCellPoint(pointID)
Polydata.SetPoints(Points) Polydata.SetPoints(Points)
Polydata.SetVerts(Vertices) Polydata.SetVerts(Vertices)
@ -73,24 +75,20 @@ for name in filenames:
# ------------------------------------------ output result --------------------------------------- # ------------------------------------------ output result ---------------------------------------
if name: if name:
(dir,filename) = os.path.split(name)
writer = vtk.vtkXMLPolyDataWriter() writer = vtk.vtkXMLPolyDataWriter()
(directory,filename) = os.path.split(name)
writer.SetDataModeToBinary() writer.SetDataModeToBinary()
writer.SetCompressorTypeToZLib() writer.SetCompressorTypeToZLib()
writer.SetFileName(os.path.join(dir,os.path.splitext(filename)[0]+'_{}'.format(options.position) \ writer.SetFileName(os.path.join(directory,os.path.splitext(filename)[0]
+'.'+writer.GetDefaultFileExtension())) +'.'+writer.GetDefaultFileExtension()))
if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(Polydata)
else: writer.SetInputData(Polydata)
writer.Write()
else: else:
writer = vtk.vtkPolyDataWriter() writer = vtk.vtkDataSetWriter()
writer.WriteToOutputStringOn() writer.WriteToOutputStringOn()
writer.SetFileTypeToASCII()
writer.SetHeader('# powered by '+scriptID) writer.SetHeader('# powered by '+scriptID)
if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(Polydata)
else: writer.SetInputData(Polydata) if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(Polydata)
writer.Write() else: writer.SetInputData(Polydata)
sys.stdout.write(writer.GetOutputString()[0:writer.GetOutputStringLength()]) writer.Write()
if name == None: sys.stdout.write(writer.GetOutputString()[0:writer.GetOutputStringLength()])
table.close() # close input ASCII table table.close()

View File

@ -19,30 +19,20 @@ Create hexahedral voxels around points in an ASCIItable.
""", version = scriptID) """, version = scriptID)
parser.add_option('-p', '--positions', parser.add_option('-d', '--deformed',
dest = 'position', dest = 'deformed',
type = 'string', metavar = 'string', type = 'string', metavar = 'string',
help = 'coordinate label [%default]') help = 'deformed coordinate label [%default]')
parser.add_option('-s', '--size', parser.add_option('-c','--coordinates',
dest = 'size', dest = 'coords',
type = 'float', nargs = 3, metavar = 'float float float', type = 'string', metavar='string',
help = 'x,y,z size of voxel') help = 'undeformed coordinates label [%default]')
parser.add_option('-o', '--origin', parser.set_defaults(deformed = 'ipdeformedcoord',
dest = 'origin', coords = 'ipinitialcoord',
type = 'float', nargs = 3, metavar = 'float float float',
help = 'x,y,z origin of coordinate system')
parser.add_option('-g', '--geom',
dest = 'geom', action='store_true',
help = 'derive geometry from geom-file header information [%default]')
parser.set_defaults(position = 'pos',
origin = (0.0,0.0,0.0),
geom = False,
) )
(options, filenames) = parser.parse_args() (options, filenames) = parser.parse_args()
if options.size == None and not options.geom:
parser.error('no size specified.')
# --- loop over input files ------------------------------------------------------------------------- # --- loop over input files -------------------------------------------------------------------------
@ -53,43 +43,32 @@ for name in filenames:
table = damask.ASCIItable(name = name, table = damask.ASCIItable(name = name,
buffered = False, readonly = True) buffered = False, readonly = True)
except: continue except: continue
damask.util.croak(damask.util.emph(scriptName)+(': '+name if name else '')) damask.util.report(scriptName,name)
# --- interpret header ----------------------------------------------------------------------------
# --------------- interprete header -----------------------------------------------------------------
table.head_read() table.head_read()
errors=[]
if options.geom: if table.label_dimension(options.deformed) != 3: errors.append('columns "{}" have dimension {}'.format(options.deformed,
info,extra_header = table.head_getGeom() table.label_dimension(options.deformed)))
if table.label_dimension(options.coords) != 3: errors.append('coordinates {} are not a vector.'.format(options.coords))
damask.util.croak(['grid a b c: %s'%(' x '.join(map(str,info['grid']))),
'size x y z: %s'%(' x '.join(map(str,info['size']))), table.data_readArray([options.coords,options.deformed])
'origin x y z: %s'%(' : '.join(map(str,info['origin']))),
'homogenization: %i'%info['homogenization'], # --------------- figure out size and grid ---------------------------------------------------------
'microstructures: %i'%info['microstructures'], coords = [{},{},{}]
]) for i in xrange(len(table.data)):
else: for j in xrange(3):
info = {} coords[j][str(table.data[i,table.label_index(options.coords)+j])] = True
info['size'] = np.ones(3) grid = np.array(map(len,coords),'i')
info['grid'] = info['size'] / options.size size = grid/np.maximum(np.ones(3,'d'),grid-1.0)* \
info['origin'] = options.origin np.array([max(map(float,coords[0].keys()))-min(map(float,coords[0].keys())),\
max(map(float,coords[1].keys()))-min(map(float,coords[1].keys())),\
errors = [] max(map(float,coords[2].keys()))-min(map(float,coords[2].keys())),\
if table.label_dimension(options.position) != 3: errors.append('columns "{}" have dimension {}'.format(options.position, ],'d') # size from bounding box, corrected for cell-centeredness
table.label_dimension(options.position)))
if np.any(info['grid'] < 1): errors.append('invalid grid a b c.') size = np.where(grid > 1, size, min(size[grid > 1]/grid[grid > 1])) # spacing for grid==1 equal to smallest among other spacings
if np.any(info['size'] <= 0.0): errors.append('invalid size x y z.')
if errors != []:
damask.util.croak(errors)
table.close(dismiss = True)
continue
# ------------------------------------------ process data --------------------------------------- # ------------------------------------------ process data ---------------------------------------
table.data_readArray(options.position)
table.data[:,0:3] *= info['size']
table.data[:,0:3] += info['origin']
hexPoints = np.array([[-1,-1,-1], hexPoints = np.array([[-1,-1,-1],
[ 1,-1,-1], [ 1,-1,-1],
[ 1, 1,-1], [ 1, 1,-1],
@ -100,43 +79,39 @@ for name in filenames:
[-1, 1, 1], [-1, 1, 1],
]) ])
halfDelta = 0.5*info['size']/info['grid'] halfDelta = 0.5*size/grid
Points = vtk.vtkPoints() Points = vtk.vtkPoints()
Hex = vtk.vtkHexahedron() Hex = vtk.vtkHexahedron()
uGrid = vtk.vtkUnstructuredGrid() uGrid = vtk.vtkUnstructuredGrid()
for p in table.data: for p in table.data:
for i,h in enumerate(hexPoints): for i,h in enumerate(hexPoints):
id = Points.InsertNextPoint(p+h*halfDelta) pointID = Points.InsertNextPoint(p[table.label_index(options.deformed):table.label_index(options.deformed)+3]+h*halfDelta)
Hex.GetPointIds().SetId(i,id) Hex.GetPointIds().SetId(i,pointID)
uGrid.InsertNextCell(Hex.GetCellType(), Hex.GetPointIds()) uGrid.InsertNextCell(Hex.GetCellType(), Hex.GetPointIds())
uGrid.SetPoints(Points) uGrid.SetPoints(Points)
# ------------------------------------------ output result --------------------------------------- # ------------------------------------------ output result ---------------------------------------
if name: if name:
(dir,filename) = os.path.split(name)
writer = vtk.vtkXMLUnstructuredGridWriter() writer = vtk.vtkXMLUnstructuredGridWriter()
(directory,filename) = os.path.split(name)
writer.SetDataModeToBinary() writer.SetDataModeToBinary()
writer.SetCompressorTypeToZLib() writer.SetCompressorTypeToZLib()
writer.SetFileName(os.path.join(dir,os.path.splitext(filename)[0]+'_{}'.format(options.position) \ writer.SetFileName(os.path.join(directory,os.path.splitext(filename)[0]
+'.'+writer.GetDefaultFileExtension())) +'.'+writer.GetDefaultFileExtension()))
if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(uGrid)
else: writer.SetInputData(uGrid)
writer.Write()
else: else:
writer = vtk.vtkUnstructuredGridWriter() writer = vtk.vtkDataSetWriter()
writer.WriteToOutputStringOn() writer.WriteToOutputStringOn()
writer.SetFileTypeToASCII()
writer.SetHeader('# powered by '+scriptID) writer.SetHeader('# powered by '+scriptID)
if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(uGrid)
else: writer.SetInputData(uGrid) if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(uGrid)
writer.Write() else: writer.SetInputData(uGrid)
sys.stdout.write(writer.GetOutputString()[0:writer.GetOutputStringLength()]) writer.Write()
if name == None: sys.stdout.write(writer.GetOutputString()[0:writer.GetOutputStringLength()])
table.close() # close input ASCII table table.close() # close input ASCII table