2016-07-18 23:05:35 +05:30
|
|
|
#!/usr/bin/env python2.7
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2016-03-01 22:55:14 +05:30
|
|
|
import os,sys,vtk
|
2015-11-14 07:22:31 +05:30
|
|
|
import numpy as np
|
2013-11-27 01:49:27 +05:30
|
|
|
import damask
|
2014-12-19 00:56:52 +05:30
|
|
|
from optparse import OptionParser
|
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
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-05-21 05:43:50 +05:30
|
|
|
Produce a VTK point cloud dataset based on coordinates given in an ASCIItable.
|
2014-12-19 00:56:52 +05:30
|
|
|
|
|
|
|
""", version = scriptID)
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2016-04-22 23:39:23 +05:30
|
|
|
parser.add_option('-p',
|
|
|
|
'--pos', '--position',
|
2016-04-14 03:19:48 +05:30
|
|
|
dest = 'pos',
|
2015-08-24 19:12:29 +05:30
|
|
|
type = 'string', metavar = 'string',
|
2016-04-23 00:50:36 +05:30
|
|
|
help = 'label of coordinates [%default]')
|
2016-04-22 23:39:23 +05:30
|
|
|
|
|
|
|
parser.set_defaults(pos = 'pos',
|
2015-08-24 19:12:29 +05:30
|
|
|
)
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2015-08-24 19:12:29 +05:30
|
|
|
(options, filenames) = parser.parse_args()
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2015-08-24 19:12:29 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2015-08-24 19:12:29 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2013-11-27 01:49:27 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2016-11-30 01:14:34 +05:30
|
|
|
try: table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False,
|
|
|
|
readonly = True)
|
2015-08-24 19:12:29 +05:30
|
|
|
except: continue
|
2015-10-09 16:54:26 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2015-08-24 19:12:29 +05:30
|
|
|
|
|
|
|
# --- interpret header ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
table.head_read()
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
remarks = []
|
2016-04-14 03:19:48 +05:30
|
|
|
coordDim = table.label_dimension(options.pos)
|
|
|
|
if not 3 >= coordDim >= 1: errors.append('coordinates "{}" need to have one, two, or three dimensions.'.format(options.pos))
|
2016-04-25 20:53:08 +05:30
|
|
|
elif coordDim < 3: remarks.append('appending {} dimension{} to coordinates "{}"...'.format(3-coordDim,
|
|
|
|
's' if coordDim < 2 else '',
|
|
|
|
options.pos))
|
2015-10-09 16:54:26 +05:30
|
|
|
|
2015-09-24 14:54:42 +05:30
|
|
|
if remarks != []: damask.util.croak(remarks)
|
2015-08-24 19:12:29 +05:30
|
|
|
if errors != []:
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.croak(errors)
|
2015-08-24 19:12:29 +05:30
|
|
|
table.close(dismiss=True)
|
|
|
|
continue
|
2013-11-27 01:49:27 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ process data ---------------------------------------
|
|
|
|
|
2016-04-14 03:19:48 +05:30
|
|
|
table.data_readArray(options.pos)
|
2015-11-14 07:22:31 +05:30
|
|
|
if len(table.data.shape) < 2: table.data.shape += (1,) # expand to 2D shape
|
|
|
|
if table.data.shape[1] < 3:
|
|
|
|
table.data = np.hstack((table.data,
|
|
|
|
np.zeros((table.data.shape[0],
|
|
|
|
3-table.data.shape[1]),dtype='f'))) # fill coords up to 3D with zeros
|
2015-08-24 19:12:29 +05:30
|
|
|
|
2013-11-27 01:49:27 +05:30
|
|
|
Polydata = vtk.vtkPolyData()
|
2015-11-14 07:22:31 +05:30
|
|
|
Points = vtk.vtkPoints()
|
2013-11-27 01:49:27 +05:30
|
|
|
Vertices = vtk.vtkCellArray()
|
|
|
|
|
2014-11-18 20:52:00 +05:30
|
|
|
for p in table.data:
|
2015-10-09 16:54:26 +05:30
|
|
|
pointID = Points.InsertNextPoint(p)
|
2013-11-27 01:49:27 +05:30
|
|
|
Vertices.InsertNextCell(1)
|
2015-10-09 16:54:26 +05:30
|
|
|
Vertices.InsertCellPoint(pointID)
|
2013-11-27 01:49:27 +05:30
|
|
|
|
|
|
|
Polydata.SetPoints(Points)
|
|
|
|
Polydata.SetVerts(Vertices)
|
|
|
|
Polydata.Modified()
|
2015-08-24 19:12:29 +05:30
|
|
|
if vtk.VTK_MAJOR_VERSION <= 5: Polydata.Update()
|
2013-11-27 01:49:27 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ output result ---------------------------------------
|
|
|
|
|
2016-04-22 23:39:23 +05:30
|
|
|
if name:
|
2016-04-25 11:58:00 +05:30
|
|
|
writer = vtk.vtkXMLPolyDataWriter()
|
|
|
|
writer.SetCompressorTypeToZLib()
|
2016-04-24 23:04:01 +05:30
|
|
|
writer.SetDataModeToBinary()
|
2016-04-22 23:39:23 +05:30
|
|
|
writer.SetFileName(os.path.join(os.path.split(name)[0],
|
|
|
|
os.path.splitext(os.path.split(name)[1])[0] +
|
2016-04-24 23:04:01 +05:30
|
|
|
'.' + writer.GetDefaultFileExtension()))
|
2015-08-24 19:12:29 +05:30
|
|
|
else:
|
2016-04-25 11:58:00 +05:30
|
|
|
writer = vtk.vtkDataSetWriter()
|
|
|
|
writer.SetHeader('# powered by '+scriptID)
|
2015-08-24 19:12:29 +05:30
|
|
|
writer.WriteToOutputStringOn()
|
2015-10-09 16:54:26 +05:30
|
|
|
|
|
|
|
if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(Polydata)
|
|
|
|
else: writer.SetInputData(Polydata)
|
2016-04-22 23:39:23 +05:30
|
|
|
|
2015-10-09 16:54:26 +05:30
|
|
|
writer.Write()
|
2016-04-22 23:39:23 +05:30
|
|
|
|
2016-04-25 11:58:00 +05:30
|
|
|
if name is None: sys.stdout.write(writer.GetOutputString()[:writer.GetOutputStringLength()]) # limiting of outputString is fix for vtk <7.0
|
2015-08-24 19:12:29 +05:30
|
|
|
|
2015-10-09 16:54:26 +05:30
|
|
|
table.close()
|