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)
parser.add_option('-p','--position',
dest = 'position',
parser.add_option('-d', '--deformed',
dest = 'deformed',
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()
@ -36,7 +36,7 @@ for name in filenames:
table = damask.ASCIItable(name = name,
buffered = False, readonly = True)
except: continue
damask.util.croak(damask.util.emph(scriptName)+(': '+name if name else ''))
damask.util.report(scriptName,name)
# --- interpret header ----------------------------------------------------------------------------
@ -44,8 +44,10 @@ for name in filenames:
errors = []
remarks = []
if table.label_dimension(options.position) != 3: errors.append('columns "{}" have dimension {}'.format(options.position,
table.label_dimension(options.position)))
if table.label_dimension(options.deformed) != 3:
errors.append('columns "{}" have dimension {}'.format(options.deformed,
table.label_dimension(options.deformed)))
if remarks != []: damask.util.croak(remarks)
if errors != []:
damask.util.croak(errors)
@ -54,16 +56,16 @@ for name in filenames:
# ------------------------------------------ process data ---------------------------------------
table.data_readArray(options.position)
table.data_readArray(options.deformed)
Polydata = vtk.vtkPolyData()
Points = vtk.vtkPoints()
Vertices = vtk.vtkCellArray()
for p in table.data:
id = Points.InsertNextPoint(p)
pointID = Points.InsertNextPoint(p)
Vertices.InsertNextCell(1)
Vertices.InsertCellPoint(id)
Vertices.InsertCellPoint(pointID)
Polydata.SetPoints(Points)
Polydata.SetVerts(Vertices)
@ -73,24 +75,20 @@ for name in filenames:
# ------------------------------------------ output result ---------------------------------------
if name:
(dir,filename) = os.path.split(name)
writer = vtk.vtkXMLPolyDataWriter()
(directory,filename) = os.path.split(name)
writer.SetDataModeToBinary()
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()))
if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(Polydata)
else: writer.SetInputData(Polydata)
writer.Write()
else:
writer = vtk.vtkPolyDataWriter()
writer = vtk.vtkDataSetWriter()
writer.WriteToOutputStringOn()
writer.SetFileTypeToASCII()
writer.SetHeader('# powered by '+scriptID)
if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(Polydata)
else: writer.SetInputData(Polydata)
writer.Write()
sys.stdout.write(writer.GetOutputString()[0:writer.GetOutputStringLength()])
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)
parser.add_option('-p', '--positions',
dest = 'position',
parser.add_option('-d', '--deformed',
dest = 'deformed',
type = 'string', metavar = 'string',
help = 'coordinate label [%default]')
parser.add_option('-s', '--size',
dest = 'size',
type = 'float', nargs = 3, metavar = 'float float float',
help = 'x,y,z size of voxel')
parser.add_option('-o', '--origin',
dest = 'origin',
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,
help = 'deformed coordinate label [%default]')
parser.add_option('-c','--coordinates',
dest = 'coords',
type = 'string', metavar='string',
help = 'undeformed coordinates label [%default]')
parser.set_defaults(deformed = 'ipdeformedcoord',
coords = 'ipinitialcoord',
)
(options, filenames) = parser.parse_args()
if options.size == None and not options.geom:
parser.error('no size specified.')
# --- loop over input files -------------------------------------------------------------------------
@ -53,43 +43,32 @@ for name in filenames:
table = damask.ASCIItable(name = name,
buffered = False, readonly = True)
except: continue
damask.util.croak(damask.util.emph(scriptName)+(': '+name if name else ''))
# --- interpret header ----------------------------------------------------------------------------
damask.util.report(scriptName,name)
# --------------- interprete header -----------------------------------------------------------------
table.head_read()
if options.geom:
info,extra_header = table.head_getGeom()
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']))),
'origin x y z: %s'%(' : '.join(map(str,info['origin']))),
'homogenization: %i'%info['homogenization'],
'microstructures: %i'%info['microstructures'],
])
else:
info = {}
info['size'] = np.ones(3)
info['grid'] = info['size'] / options.size
info['origin'] = options.origin
errors=[]
if table.label_dimension(options.position) != 3: errors.append('columns "{}" have dimension {}'.format(options.position,
table.label_dimension(options.position)))
if np.any(info['grid'] < 1): errors.append('invalid grid a b c.')
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
if table.label_dimension(options.deformed) != 3: errors.append('columns "{}" have dimension {}'.format(options.deformed,
table.label_dimension(options.deformed)))
if table.label_dimension(options.coords) != 3: errors.append('coordinates {} are not a vector.'.format(options.coords))
table.data_readArray([options.coords,options.deformed])
# --------------- figure out size and grid ---------------------------------------------------------
coords = [{},{},{}]
for i in xrange(len(table.data)):
for j in xrange(3):
coords[j][str(table.data[i,table.label_index(options.coords)+j])] = True
grid = np.array(map(len,coords),'i')
size = grid/np.maximum(np.ones(3,'d'),grid-1.0)* \
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())),\
max(map(float,coords[2].keys()))-min(map(float,coords[2].keys())),\
],'d') # size from bounding box, corrected for cell-centeredness
size = np.where(grid > 1, size, min(size[grid > 1]/grid[grid > 1])) # spacing for grid==1 equal to smallest among other spacings
# ------------------------------------------ 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],
[ 1,-1,-1],
[ 1, 1,-1],
@ -100,7 +79,7 @@ for name in filenames:
[-1, 1, 1],
])
halfDelta = 0.5*info['size']/info['grid']
halfDelta = 0.5*size/grid
Points = vtk.vtkPoints()
Hex = vtk.vtkHexahedron()
@ -108,8 +87,8 @@ for name in filenames:
for p in table.data:
for i,h in enumerate(hexPoints):
id = Points.InsertNextPoint(p+h*halfDelta)
Hex.GetPointIds().SetId(i,id)
pointID = Points.InsertNextPoint(p[table.label_index(options.deformed):table.label_index(options.deformed)+3]+h*halfDelta)
Hex.GetPointIds().SetId(i,pointID)
uGrid.InsertNextCell(Hex.GetCellType(), Hex.GetPointIds())
@ -118,25 +97,21 @@ for name in filenames:
# ------------------------------------------ output result ---------------------------------------
if name:
(dir,filename) = os.path.split(name)
writer = vtk.vtkXMLUnstructuredGridWriter()
(directory,filename) = os.path.split(name)
writer.SetDataModeToBinary()
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()))
if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(uGrid)
else: writer.SetInputData(uGrid)
writer.Write()
else:
writer = vtk.vtkUnstructuredGridWriter()
writer = vtk.vtkDataSetWriter()
writer.WriteToOutputStringOn()
writer.SetFileTypeToASCII()
writer.SetHeader('# powered by '+scriptID)
if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(uGrid)
else: writer.SetInputData(uGrid)
writer.Write()
sys.stdout.write(writer.GetOutputString()[0:writer.GetOutputStringLength()])
if name == None: sys.stdout.write(writer.GetOutputString()[0:writer.GetOutputStringLength()])
table.close() # close input ASCII table