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 -*-
|
|
|
|
|
2014-12-19 00:56:52 +05:30
|
|
|
import os,string,math,sys
|
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
2016-03-02 18:22:33 +05:30
|
|
|
import vtk
|
|
|
|
import damask
|
2013-05-06 22:07:54 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2013-05-06 22:07:54 +05:30
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
def getHeader(filename,sizeFastIndex,sizeSlowIndex,stepsize):
|
2016-03-02 18:22:33 +05:30
|
|
|
"""returns header for ang file step size in micrometer"""
|
2013-05-06 22:07:54 +05:30
|
|
|
return '\n'.join([ \
|
|
|
|
'# TEM_PIXperUM 1.000000', \
|
|
|
|
'# x-star 1.000000', \
|
|
|
|
'# y-star 1.000000', \
|
|
|
|
'# z-star 1.000000', \
|
|
|
|
'# WorkingDistance 18.000000', \
|
|
|
|
'#', \
|
|
|
|
'# Phase 1', \
|
|
|
|
'# MaterialName XX', \
|
|
|
|
'# Formula XX', \
|
|
|
|
'# Info', \
|
|
|
|
'# Symmetry 43', \
|
|
|
|
'# LatticeConstants 2.870 2.870 2.870 90.000 90.000 90.000', \
|
|
|
|
'# NumberFamilies 1', \
|
|
|
|
'# hklFamilies 1 1 0 1 0.000000 1', \
|
|
|
|
'# Categories 0 0 0 0 0 ', \
|
|
|
|
'#', \
|
|
|
|
'# GRID: SqrGrid', \
|
2013-05-07 18:27:34 +05:30
|
|
|
'# XSTEP: ' + str(stepsize*1e6), \
|
|
|
|
'# YSTEP: ' + str(stepsize*1e6), \
|
2013-05-06 22:07:54 +05:30
|
|
|
'# NCOLS_ODD: ' + str(sizeFastIndex), \
|
|
|
|
'# NCOLS_EVEN: ' + str(sizeFastIndex), \
|
|
|
|
'# NROWS: ' + str(sizeSlowIndex), \
|
|
|
|
'#', \
|
|
|
|
'# OPERATOR: ' + string.replace('$Id$','\n','\\n'), \
|
|
|
|
'#', \
|
|
|
|
'# SAMPLEID: %s'%filename, \
|
|
|
|
'#', \
|
|
|
|
'# SCANID: ', \
|
|
|
|
'#', \
|
|
|
|
]) + '\n'
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
def positiveRadians(angle):
|
2016-03-02 18:22:33 +05:30
|
|
|
"""returns positive angle in radians from angle in degrees"""
|
2013-05-06 22:07:54 +05:30
|
|
|
angle = math.radians(float(angle))
|
|
|
|
while angle < 0.0:
|
|
|
|
angle += 2.0 * math.pi
|
|
|
|
|
|
|
|
return angle
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
2013-05-07 18:27:34 +05:30
|
|
|
def getDataLine(angles,x,y,validData=True):
|
2016-03-02 18:22:33 +05:30
|
|
|
"""
|
|
|
|
returns string of one line in ang file
|
|
|
|
|
|
|
|
convention in ang file: y coordinate comes first and is fastest index
|
|
|
|
positions in micrometer
|
|
|
|
"""
|
2013-05-06 22:07:54 +05:30
|
|
|
info = {True: (9999.9, 1.0, 0,99999,0.0),
|
|
|
|
False: ( -1.0,-1.0,-1, -1,1.0)}
|
2016-03-03 15:13:43 +05:30
|
|
|
return '%9.5f %9.5f %9.5f %12.5f %12.5f %6.1f %6.3f %2i %6i %6.3f \n'\
|
|
|
|
%(tuple(map(positiveRadians,angles))+(y*1e6,x*1e6)+info[validData])
|
2013-05-06 22:07:54 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN FUNCTION STARTS HERE
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
|
|
|
parser = OptionParser(usage='%prog options [file[s]]', description = """
|
|
|
|
Builds a ang files from a vtk file.
|
|
|
|
|
2014-12-19 00:56:52 +05:30
|
|
|
""", version = scriptID)
|
2013-05-06 22:07:54 +05:30
|
|
|
|
|
|
|
|
2016-05-13 12:58:54 +05:30
|
|
|
parser.add_option('--disp','--displacement',dest='dispLabel',
|
|
|
|
metavar ='string',
|
2013-05-06 22:07:54 +05:30
|
|
|
help='label of displacements [%default]')
|
2016-05-13 12:58:54 +05:30
|
|
|
parser.add_option('--euler', dest='eulerLabel', nargs=3,
|
|
|
|
metavar ='string string string',
|
2013-05-06 22:07:54 +05:30
|
|
|
help='labels of euler angles [%default]')
|
2016-05-13 12:58:54 +05:30
|
|
|
parser.add_option('-n','--normal', dest='normal', type='float', nargs=3,
|
|
|
|
metavar ='float float float',
|
2013-05-06 22:07:54 +05:30
|
|
|
help='normal of slices in direction of increasing slice numbers [%default]')
|
|
|
|
parser.add_option('-u','--up', dest='up', type='float', nargs=3,
|
2016-05-13 12:58:54 +05:30
|
|
|
metavar ='float float float',
|
2013-05-06 22:07:54 +05:30
|
|
|
help='up direction of slices [%default]')
|
2016-05-13 12:58:54 +05:30
|
|
|
parser.add_option('-i','--slices', dest='Nslices', type='int',
|
|
|
|
metavar ='int',
|
2013-05-06 22:07:54 +05:30
|
|
|
help='number of slices [%default]')
|
2016-05-13 12:58:54 +05:30
|
|
|
parser.add_option('-d','--distance', dest='distance', type='float',
|
|
|
|
metavar ='float',
|
2013-05-06 22:07:54 +05:30
|
|
|
help='slice distance [%default]')
|
2016-05-13 12:58:54 +05:30
|
|
|
parser.add_option('-s','--scale', dest='scale', type='float',
|
|
|
|
metavar ='float',
|
2013-05-07 18:27:34 +05:30
|
|
|
help='scale length from vtk file [%default]')
|
2013-05-06 22:07:54 +05:30
|
|
|
parser.add_option('-r','--resolution', dest='resolution', type='float',
|
2016-05-13 12:58:54 +05:30
|
|
|
metavar ='float',
|
2013-05-06 22:07:54 +05:30
|
|
|
help='scaling factor for resolution [%default]')
|
|
|
|
parser.add_option('--hex','--hexagonal', dest='hexagonal', action='store_true',
|
2016-05-13 12:58:54 +05:30
|
|
|
help='use in plane hexagonal grid')
|
|
|
|
parser.add_option('--interpolation', dest='interpolation', type='int',
|
|
|
|
metavar='float',
|
2013-05-06 22:07:54 +05:30
|
|
|
help='number of points for linear interpolation [%default]')
|
|
|
|
parser.add_option('--verbose', dest='verbose', action='store_true',
|
2016-05-13 12:58:54 +05:30
|
|
|
help='verbose mode')
|
2013-05-08 21:29:51 +05:30
|
|
|
parser.add_option('--visualize', dest='visualize', action='store_true',
|
2016-05-13 12:58:54 +05:30
|
|
|
help='visualize geometry')
|
2013-05-06 22:07:54 +05:30
|
|
|
|
|
|
|
parser.set_defaults(dispLabel = 'displacement')
|
2013-05-09 17:02:34 +05:30
|
|
|
parser.set_defaults(eulerLabel = ['1_1_eulerangles','1_2_eulerangles','1_3_eulerangles'])
|
2013-05-06 22:07:54 +05:30
|
|
|
parser.set_defaults(hexagonal = False)
|
2013-05-07 18:27:34 +05:30
|
|
|
parser.set_defaults(normal = [0.0,0.0,-1.0])
|
2013-05-06 22:07:54 +05:30
|
|
|
parser.set_defaults(up = [0.0,1.0,0.0])
|
|
|
|
parser.set_defaults(Nslices = 1)
|
|
|
|
parser.set_defaults(distance = 0.0)
|
2013-05-07 18:27:34 +05:30
|
|
|
parser.set_defaults(scale = 1.0)
|
2013-05-06 22:07:54 +05:30
|
|
|
parser.set_defaults(resolution = 1.0)
|
|
|
|
parser.set_defaults(dispScaling = 1.0)
|
|
|
|
parser.set_defaults(interpolation = 1)
|
2013-05-08 21:29:51 +05:30
|
|
|
parser.set_defaults(verbose = False)
|
|
|
|
parser.set_defaults(visualize = False)
|
2013-05-06 22:07:54 +05:30
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
#--- SANITY CHECKS
|
|
|
|
|
|
|
|
# check for valid filenames
|
|
|
|
|
|
|
|
for filename in filenames:
|
|
|
|
if not os.path.exists(filename):
|
|
|
|
parser.error('file "%s" does not exist'%filename)
|
|
|
|
if not os.path.splitext(filename)[1] == '.vtk':
|
|
|
|
parser.error('"%s": need vtk file'%filename)
|
|
|
|
|
|
|
|
|
|
|
|
# check for othogonality of normal and up vector
|
|
|
|
|
2014-12-19 00:56:52 +05:30
|
|
|
if np.dot(np.array(options.normal),np.array(options.up)) > 1e-3:
|
2013-05-06 22:07:54 +05:30
|
|
|
parser.error('normal vector and up vector have to be orthogonal')
|
|
|
|
|
|
|
|
|
|
|
|
# check for options that are not yet implemented
|
|
|
|
|
|
|
|
if options.interpolation > 1:
|
|
|
|
parser.error('interpolation not yet supported')
|
|
|
|
if options.hexagonal:
|
|
|
|
parser.error('hexagonal grid not yet supported')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#--- ITERATE OVER FILES AND PROCESS THEM
|
|
|
|
|
|
|
|
for filename in filenames:
|
|
|
|
|
2013-05-07 18:27:34 +05:30
|
|
|
if options.verbose: sys.stdout.write("\nREADING VTK FILE\n")
|
2016-03-02 18:22:33 +05:30
|
|
|
# Read the source file
|
|
|
|
reader = vtk.vtkUnstructuredGridReader()
|
2013-05-06 22:07:54 +05:30
|
|
|
reader.SetFileName(filename)
|
|
|
|
reader.ReadAllScalarsOn()
|
|
|
|
reader.ReadAllVectorsOn()
|
|
|
|
reader.Update()
|
|
|
|
undeformedMesh = reader.GetOutput()
|
2013-05-07 18:27:34 +05:30
|
|
|
|
2013-05-06 22:07:54 +05:30
|
|
|
|
2016-03-02 18:22:33 +05:30
|
|
|
# Get euler angles from cell data
|
2013-05-06 22:07:54 +05:30
|
|
|
|
2013-05-07 18:27:34 +05:30
|
|
|
if options.verbose: sys.stdout.write("\nGETTING EULER ANGLES\n")
|
2013-05-06 22:07:54 +05:30
|
|
|
angles = {}
|
2013-05-07 18:27:34 +05:30
|
|
|
for i in range(reader.GetNumberOfScalarsInFile()):
|
2013-05-06 22:07:54 +05:30
|
|
|
scalarName = reader.GetScalarsNameInFile(i)
|
|
|
|
if scalarName in options.eulerLabel:
|
|
|
|
angles[scalarName] = undeformedMesh.GetCellData().GetScalars(scalarName)
|
2013-05-07 18:27:34 +05:30
|
|
|
if options.verbose: sys.stdout.write(" found scalar with name %s\n"%scalarName)
|
2013-05-06 22:07:54 +05:30
|
|
|
if len(angles) < 3: # found data for all three euler angles?
|
|
|
|
for label in options.eulerLabel:
|
2016-03-02 18:22:33 +05:30
|
|
|
if label not in angles.keys():
|
2013-05-06 22:07:54 +05:30
|
|
|
parser.error('Could not find scalar data with name %s'%label)
|
|
|
|
|
|
|
|
|
2016-03-02 18:22:33 +05:30
|
|
|
# Get deformed mesh
|
2013-05-06 22:07:54 +05:30
|
|
|
|
2013-05-07 18:27:34 +05:30
|
|
|
if options.verbose: sys.stdout.write("\nDEFORM MESH\n")
|
2016-03-02 18:22:33 +05:30
|
|
|
warpVector = vtk.vtkWarpVector()
|
2013-05-23 02:53:01 +05:30
|
|
|
undeformedMesh.GetPointData().SetActiveVectors(options.dispLabel)
|
2013-05-06 22:07:54 +05:30
|
|
|
warpVector.SetInput(undeformedMesh)
|
|
|
|
warpVector.Update()
|
2013-05-23 02:53:01 +05:30
|
|
|
deformedMesh = warpVector.GetOutput()
|
2013-05-06 22:07:54 +05:30
|
|
|
box = deformedMesh.GetBounds() # bounding box in mesh system
|
|
|
|
if options.verbose:
|
2013-05-07 18:27:34 +05:30
|
|
|
sys.stdout.write(" bounding box in lab system\n")
|
|
|
|
sys.stdout.write(" x (% .8f % .8f)\n"%(box[0],box[1]))
|
|
|
|
sys.stdout.write(" y (% .8f % .8f)\n"%(box[2],box[3]))
|
|
|
|
sys.stdout.write(" z (% .8f % .8f)\n"%(box[4],box[5]))
|
2013-05-06 22:07:54 +05:30
|
|
|
|
|
|
|
|
2016-03-02 18:22:33 +05:30
|
|
|
# Get cell centers of deformed mesh (position of ips)
|
2013-05-06 22:07:54 +05:30
|
|
|
|
2013-05-07 18:27:34 +05:30
|
|
|
if options.verbose: sys.stdout.write("\nGETTING CELL CENTERS OF DEFORMED MESH\n")
|
2016-03-02 18:22:33 +05:30
|
|
|
cellCenter = vtk.vtkCellCenters()
|
2013-05-06 22:07:54 +05:30
|
|
|
cellCenter.SetVertexCells(0) # do not generate vertex cells, just points
|
|
|
|
cellCenter.SetInput(deformedMesh)
|
|
|
|
cellCenter.Update()
|
|
|
|
meshIPs = cellCenter.GetOutput()
|
|
|
|
|
|
|
|
|
2016-03-02 18:22:33 +05:30
|
|
|
# Get outer surface of deformed mesh
|
2013-05-06 22:07:54 +05:30
|
|
|
|
2013-05-07 18:27:34 +05:30
|
|
|
if options.verbose: sys.stdout.write("\nGETTING OUTER SURFACE OF DEFORMED MESH\n")
|
2016-03-02 18:22:33 +05:30
|
|
|
surfaceFilter = vtk.vtkDataSetSurfaceFilter()
|
2013-05-06 22:07:54 +05:30
|
|
|
surfaceFilter.SetInput(deformedMesh)
|
|
|
|
surfaceFilter.Update()
|
|
|
|
surface = surfaceFilter.GetOutput()
|
|
|
|
|
|
|
|
|
2016-03-02 18:22:33 +05:30
|
|
|
# Get coordinate system for ang files
|
|
|
|
# z-vector is normal to slices
|
|
|
|
# x-vector corresponds to the up-direction
|
|
|
|
# "R" rotates coordinates from the mesh system into the TSL system
|
2013-05-06 22:07:54 +05:30
|
|
|
|
2013-05-07 18:27:34 +05:30
|
|
|
if options.verbose: sys.stdout.write("\nGETTING COORDINATE SYSTEM FOR ANG FILES\n")
|
2014-12-19 00:56:52 +05:30
|
|
|
z = np.array(options.normal,dtype='float')
|
|
|
|
z = z / np.linalg.norm(z)
|
|
|
|
x = np.array(options.up,dtype='float')
|
|
|
|
x = x / np.linalg.norm(x)
|
|
|
|
y = np.cross(z,x)
|
|
|
|
R = np.array([x,y,z])
|
2013-05-07 18:27:34 +05:30
|
|
|
if options.verbose:
|
|
|
|
sys.stdout.write(" axis (x: up direction, z: slice normal)\n")
|
|
|
|
sys.stdout.write(" x (% .8f % .8f % .8f)\n"%tuple(x))
|
|
|
|
sys.stdout.write(" y (% .8f % .8f % .8f)\n"%tuple(y))
|
|
|
|
sys.stdout.write(" z (% .8f % .8f % .8f)\n"%tuple(z))
|
2013-05-06 22:07:54 +05:30
|
|
|
|
|
|
|
|
2016-03-02 18:22:33 +05:30
|
|
|
# Get bounding box in rotated system (x,y,z)
|
2013-05-06 22:07:54 +05:30
|
|
|
|
2013-05-07 18:27:34 +05:30
|
|
|
if options.verbose: sys.stdout.write("\nGETTING BOUNDING BOX IN ROTATED SYSTEM\n")
|
2014-12-19 00:56:52 +05:30
|
|
|
rotatedbox = [[np.inf,-np.inf] for i in range(3)] # bounding box in rotated TSL system
|
2013-05-06 22:07:54 +05:30
|
|
|
for n in range(8): # loop over eight vertices of mesh bounding box
|
2014-12-19 00:56:52 +05:30
|
|
|
vert = np.array([box[0+(n/1)%2],
|
2013-05-06 22:07:54 +05:30
|
|
|
box[2+(n/2)%2],
|
|
|
|
box[4+(n/4)%2]]) # vertex in mesh system
|
2014-12-19 00:56:52 +05:30
|
|
|
rotatedvert = np.dot(R,vert) # vertex in rotated system
|
2013-05-06 22:07:54 +05:30
|
|
|
for i in range(3):
|
|
|
|
rotatedbox[i][0] = min(rotatedbox[i][0],rotatedvert[i])
|
|
|
|
rotatedbox[i][1] = max(rotatedbox[i][1],rotatedvert[i])
|
2013-05-07 18:27:34 +05:30
|
|
|
if options.verbose:
|
|
|
|
sys.stdout.write(" bounding box in rotated system\n")
|
|
|
|
sys.stdout.write(" x (% .8f % .8f)\n"%tuple(rotatedbox[0]))
|
|
|
|
sys.stdout.write(" y (% .8f % .8f)\n"%tuple(rotatedbox[1]))
|
|
|
|
sys.stdout.write(" z (% .8f % .8f)\n"%tuple(rotatedbox[2]))
|
2013-05-06 22:07:54 +05:30
|
|
|
|
|
|
|
|
2016-03-02 18:22:33 +05:30
|
|
|
# Correct bounding box so that a multiplicity of the resolution fits into it
|
|
|
|
# and get number of points and extent in each (rotated) axis direction
|
2013-05-06 22:07:54 +05:30
|
|
|
|
2013-05-07 18:27:34 +05:30
|
|
|
if options.verbose: sys.stdout.write("\nCORRECTING EXTENT OF BOUNDING BOX IN ROTATED SYSTEM\n")
|
2013-05-06 22:07:54 +05:30
|
|
|
correction = []
|
|
|
|
Npoints = []
|
|
|
|
extent = [rotatedbox[i][1] - rotatedbox[i][0] for i in range(3)]
|
|
|
|
for i in range(2):
|
|
|
|
Npoints.extend([int(math.ceil(extent[i] / options.resolution))])
|
|
|
|
correction.extend([float(Npoints[i]) * options.resolution - extent[i]])
|
|
|
|
if options.distance > 0.0:
|
|
|
|
Npoints.extend([int(math.ceil(extent[2] / options.distance))])
|
|
|
|
correction.extend([float(Npoints[2]) * options.distance - extent[2]])
|
|
|
|
else:
|
|
|
|
Npoints.extend([options.Nslices])
|
|
|
|
correction.extend([0.0])
|
|
|
|
options.distance = extent[2] / float(options.Nslices)
|
|
|
|
for i in range(3):
|
|
|
|
rotatedbox[i][0] = rotatedbox[i][0] - 0.5 * correction[i]
|
|
|
|
rotatedbox[i][1] = rotatedbox[i][1] + 0.5 * correction[i]
|
|
|
|
extent[i] = rotatedbox[i][1] - rotatedbox[i][0]
|
2013-05-07 18:27:34 +05:30
|
|
|
NpointsPerSlice = Npoints[0] * Npoints[1]
|
|
|
|
totalNpoints = NpointsPerSlice * Npoints[2]
|
2013-05-06 22:07:54 +05:30
|
|
|
if options.verbose:
|
2013-05-07 18:27:34 +05:30
|
|
|
sys.stdout.write(" corrected bounding box in rotated system\n")
|
|
|
|
sys.stdout.write(" x (% .8f % .8f)\n"%tuple(rotatedbox[0]))
|
|
|
|
sys.stdout.write(" y (% .8f % .8f)\n"%tuple(rotatedbox[1]))
|
|
|
|
sys.stdout.write(" z (% .8f % .8f)\n"%tuple(rotatedbox[2]))
|
2013-05-06 22:07:54 +05:30
|
|
|
|
|
|
|
|
2016-03-02 18:22:33 +05:30
|
|
|
# Generate new regular point grid for ang files
|
|
|
|
# Use "polydata" object with points as single vertices
|
|
|
|
# beware of TSL convention: y direction is fastest index
|
2013-05-06 22:07:54 +05:30
|
|
|
|
2013-05-07 18:27:34 +05:30
|
|
|
if options.verbose: sys.stdout.write("\nGENERATING POINTS FOR POINT GRID")
|
2016-03-02 18:22:33 +05:30
|
|
|
points = vtk.vtkPoints()
|
2013-05-06 22:07:54 +05:30
|
|
|
for k in xrange(Npoints[2]):
|
|
|
|
for j in xrange(Npoints[0]):
|
|
|
|
for i in xrange(Npoints[1]): # y is fastest index
|
2014-12-19 00:56:52 +05:30
|
|
|
rotatedpoint = np.array([rotatedbox[0][0] + (float(j) + 0.5) * options.resolution,
|
2013-05-06 22:07:54 +05:30
|
|
|
rotatedbox[1][0] + (float(i) + 0.5) * options.resolution,
|
|
|
|
rotatedbox[2][0] + (float(k) + 0.5) * options.distance ]) # point in rotated system
|
2014-12-19 00:56:52 +05:30
|
|
|
point = np.dot(R.T,rotatedpoint) # point in mesh system
|
2013-05-06 22:07:54 +05:30
|
|
|
points.InsertNextPoint(list(point))
|
2013-05-07 18:27:34 +05:30
|
|
|
if options.verbose:
|
|
|
|
sys.stdout.write("\rGENERATING POINTS FOR POINT GRID %d%%" %(100*(Npoints[1]*(k*Npoints[0]+j)+i+1)/totalNpoints))
|
|
|
|
sys.stdout.flush()
|
|
|
|
if options.verbose:
|
|
|
|
sys.stdout.write("\n number of slices: %i\n"%Npoints[2])
|
|
|
|
sys.stdout.write(" slice spacing: %.8f\n"%options.distance)
|
2013-05-09 17:02:34 +05:30
|
|
|
if Npoints[2] > 1:
|
|
|
|
sys.stdout.write(" number of points per slice: %i = %i rows * %i points in row\n"%(NpointsPerSlice,Npoints[0],Npoints[1]))
|
2013-05-07 18:27:34 +05:30
|
|
|
sys.stdout.write(" grid resolution: %.8f\n"%options.resolution)
|
|
|
|
|
|
|
|
if options.verbose: sys.stdout.write("\nGENERATING VERTICES FOR POINT GRID")
|
2016-03-02 18:22:33 +05:30
|
|
|
vertices = vtk.vtkCellArray()
|
2013-05-07 18:27:34 +05:30
|
|
|
for i in xrange(totalNpoints):
|
2016-03-02 18:22:33 +05:30
|
|
|
vertex = vtk.vtkVertex()
|
2013-05-06 22:07:54 +05:30
|
|
|
vertex.GetPointIds().SetId(0,i) # each vertex consists of exactly one (index 0) point with ID "i"
|
|
|
|
vertices.InsertNextCell(vertex)
|
2013-05-07 18:27:34 +05:30
|
|
|
if options.verbose:
|
|
|
|
sys.stdout.write("\rGENERATING VERTICES FOR POINT GRID %d%%" %(100*(i+1)/totalNpoints))
|
|
|
|
sys.stdout.flush()
|
2013-05-06 22:07:54 +05:30
|
|
|
|
2013-05-07 18:27:34 +05:30
|
|
|
if options.verbose: sys.stdout.write("\n\nGENERATING POINT GRID\n")
|
2016-03-02 18:22:33 +05:30
|
|
|
pointgrid = vtk.vtkPolyData()
|
2013-05-06 22:07:54 +05:30
|
|
|
pointgrid.SetPoints(points)
|
|
|
|
pointgrid.SetVerts(vertices)
|
|
|
|
pointgrid.Update()
|
|
|
|
|
|
|
|
|
2016-03-02 18:22:33 +05:30
|
|
|
# Find out which points reside inside mesh geometry
|
2013-05-06 22:07:54 +05:30
|
|
|
|
2013-05-07 18:27:34 +05:30
|
|
|
if options.verbose: sys.stdout.write("\nIDENTIFYING POINTS INSIDE MESH GEOMETRY\n")
|
2016-03-02 18:22:33 +05:30
|
|
|
enclosedPoints = vtk.vtkSelectEnclosedPoints()
|
2013-05-06 22:07:54 +05:30
|
|
|
enclosedPoints.SetSurface(surface)
|
|
|
|
enclosedPoints.SetInput(pointgrid)
|
|
|
|
enclosedPoints.Update()
|
|
|
|
|
|
|
|
|
2016-03-02 18:22:33 +05:30
|
|
|
# Build kdtree from mesh IPs and match mesh IPs to point grid
|
2013-05-06 22:07:54 +05:30
|
|
|
|
2013-05-07 18:27:34 +05:30
|
|
|
if options.verbose: sys.stdout.write("\nBUILDING MAPPING OF GRID POINTS")
|
2016-03-02 18:22:33 +05:30
|
|
|
kdTree = vtk.vtkKdTree()
|
2013-05-06 22:07:54 +05:30
|
|
|
kdTree.BuildLocatorFromPoints(meshIPs.GetPoints())
|
|
|
|
gridToMesh = []
|
2016-03-02 18:22:33 +05:30
|
|
|
ids = vtk.vtkIdList()
|
2013-05-07 18:27:34 +05:30
|
|
|
NenclosedPoints = 0
|
2013-05-06 22:07:54 +05:30
|
|
|
for i in range(pointgrid.GetNumberOfPoints()):
|
|
|
|
gridToMesh.append([])
|
|
|
|
if enclosedPoints.IsInside(i):
|
2013-05-07 18:27:34 +05:30
|
|
|
NenclosedPoints += 1
|
2016-03-02 18:22:33 +05:30
|
|
|
# here one could use faster(?) "FindClosestPoint" if only first nearest neighbor required
|
|
|
|
kdTree.FindClosestNPoints(options.interpolation,pointgrid.GetPoint(i),ids)
|
2013-05-06 22:07:54 +05:30
|
|
|
for j in range(ids.GetNumberOfIds()):
|
|
|
|
gridToMesh[-1].extend([ids.GetId(j)])
|
2013-05-07 18:27:34 +05:30
|
|
|
if options.verbose:
|
|
|
|
sys.stdout.write("\rBUILDING MAPPING OF GRID POINTS %d%%" %(100*(i+1)/totalNpoints))
|
|
|
|
sys.stdout.flush()
|
|
|
|
if options.verbose:
|
|
|
|
sys.stdout.write("\n Number of points inside mesh geometry %i\n"%NenclosedPoints)
|
|
|
|
sys.stdout.write(" Number of points outside mesh geometry %i\n"%(totalNpoints - NenclosedPoints))
|
2013-05-06 22:07:54 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-02 18:22:33 +05:30
|
|
|
# ITERATE OVER SLICES AND CREATE ANG FILE
|
2013-05-06 22:07:54 +05:30
|
|
|
|
2013-05-07 18:27:34 +05:30
|
|
|
if options.verbose:
|
|
|
|
sys.stdout.write("\nWRITING OUT ANG FILES\n")
|
|
|
|
sys.stdout.write(" scaling all length with %f\n"%options.scale)
|
2014-12-19 00:56:52 +05:30
|
|
|
x0,y0,z0 = np.dot(R,pointgrid.GetPoint(0)) # first point on slice defines origin
|
2013-05-06 22:07:54 +05:30
|
|
|
for sliceN in range(Npoints[2]):
|
|
|
|
|
|
|
|
# Open file and write header
|
|
|
|
|
2013-05-07 18:27:34 +05:30
|
|
|
angfilename = eval('"'+eval("'%%s_slice%%0%ii.ang'%(math.log10(Npoints[2])+1)")+'"%(os.path.splitext(filename)[0],sliceN+1)')
|
2013-05-06 22:07:54 +05:30
|
|
|
with open(angfilename,'w') as angfile:
|
2013-05-07 18:27:34 +05:30
|
|
|
if options.verbose: sys.stdout.write(" %s\n"%angfilename)
|
|
|
|
angfile.write(getHeader(filename,Npoints[1],Npoints[0],options.resolution*options.scale))
|
2013-05-06 22:07:54 +05:30
|
|
|
for i in xrange(sliceN*NpointsPerSlice,(sliceN+1)*NpointsPerSlice): # Iterate over points on slice
|
|
|
|
|
|
|
|
|
|
|
|
# Get euler angles of closest IDs
|
|
|
|
|
|
|
|
if enclosedPoints.IsInside(i):
|
|
|
|
phi = []
|
|
|
|
for j in range(len(gridToMesh[i])):
|
|
|
|
IP = gridToMesh[i][j]
|
|
|
|
phi.append([])
|
|
|
|
for k in range(3):
|
|
|
|
phi[-1].extend([angles[options.eulerLabel[k]].GetValue(IP)])
|
|
|
|
else:
|
|
|
|
phi = [[720,720,720]] # fake angles
|
|
|
|
|
|
|
|
|
|
|
|
# Interpolate Euler angle
|
|
|
|
# NOT YET IMPLEMENTED, simply take the nearest neighbors values
|
|
|
|
|
|
|
|
interpolatedPhi = phi[0]
|
|
|
|
|
|
|
|
|
|
|
|
# write data to ang file
|
|
|
|
|
2014-12-19 00:56:52 +05:30
|
|
|
x,y,z = np.dot(R,pointgrid.GetPoint(i)) # point in rotated TSL system
|
2013-05-08 21:29:51 +05:30
|
|
|
x -= x0 # first point on slice defines origin
|
|
|
|
y -= y0 # first point on slice defines origin
|
2013-05-07 18:27:34 +05:30
|
|
|
x *= options.scale
|
|
|
|
y *= options.scale
|
|
|
|
angfile.write(getDataLine(interpolatedPhi,x,y,enclosedPoints.IsInside(i)))
|
2013-05-06 22:07:54 +05:30
|
|
|
|
2013-05-08 21:29:51 +05:30
|
|
|
|
2016-03-02 18:22:33 +05:30
|
|
|
# Visualize slices
|
2013-05-08 21:29:51 +05:30
|
|
|
|
|
|
|
if options.visualize:
|
2016-03-02 18:22:33 +05:30
|
|
|
meshMapper = vtk.vtkDataSetMapper()
|
2013-05-08 21:29:51 +05:30
|
|
|
meshMapper.SetInput(surface)
|
|
|
|
meshMapper.ScalarVisibilityOff() # do not use scalar data for coloring
|
2016-03-02 18:22:33 +05:30
|
|
|
meshActor = vtk.vtkActor()
|
2013-05-08 21:29:51 +05:30
|
|
|
meshActor.SetMapper(meshMapper)
|
2013-05-13 15:06:19 +05:30
|
|
|
meshActor.GetProperty().SetOpacity(0.2)
|
|
|
|
meshActor.GetProperty().SetColor(1.0,1.0,0)
|
|
|
|
meshActor.GetProperty().BackfaceCullingOn()
|
|
|
|
# meshActor.GetProperty().SetEdgeColor(1,1,0.5)
|
|
|
|
# meshActor.GetProperty().EdgeVisibilityOn()
|
2013-05-08 21:29:51 +05:30
|
|
|
|
2016-03-02 18:22:33 +05:30
|
|
|
boxpoints = vtk.vtkPoints()
|
2013-05-08 21:29:51 +05:30
|
|
|
for n in range(8):
|
|
|
|
P = [rotatedbox[0][(n/1)%2],
|
|
|
|
rotatedbox[1][(n/2)%2],
|
|
|
|
rotatedbox[2][(n/4)%2]]
|
2014-12-19 00:56:52 +05:30
|
|
|
boxpoints.InsertNextPoint(list(np.dot(R.T,np.array(P))))
|
2016-03-02 18:22:33 +05:30
|
|
|
box = vtk.vtkHexahedron()
|
2013-05-08 21:29:51 +05:30
|
|
|
for n,i in enumerate([0,1,3,2,4,5,7,6]):
|
|
|
|
box.GetPointIds().SetId(n,i)
|
2016-03-02 18:22:33 +05:30
|
|
|
boxgrid = vtk.vtkUnstructuredGrid()
|
2013-05-08 21:29:51 +05:30
|
|
|
boxgrid.SetPoints(boxpoints)
|
|
|
|
boxgrid.InsertNextCell(box.GetCellType(), box.GetPointIds())
|
2016-03-02 18:22:33 +05:30
|
|
|
boxsurfaceFilter = vtk.vtkDataSetSurfaceFilter()
|
2013-05-08 21:29:51 +05:30
|
|
|
boxsurfaceFilter.SetInput(boxgrid)
|
|
|
|
boxsurfaceFilter.Update()
|
|
|
|
boxsurface = boxsurfaceFilter.GetOutput()
|
|
|
|
|
2016-03-02 18:22:33 +05:30
|
|
|
boxMapper = vtk.vtkDataSetMapper()
|
2013-05-08 21:29:51 +05:30
|
|
|
boxMapper.SetInput(boxsurface)
|
2016-03-02 18:22:33 +05:30
|
|
|
boxActor = vtk.vtkActor()
|
2013-05-08 21:29:51 +05:30
|
|
|
boxActor.SetMapper(boxMapper)
|
|
|
|
boxActor.GetProperty().SetLineWidth(2.0)
|
2013-05-13 15:06:19 +05:30
|
|
|
boxActor.GetProperty().SetRepresentationToWireframe()
|
2013-05-08 21:29:51 +05:30
|
|
|
|
2016-03-02 18:22:33 +05:30
|
|
|
gridMapper = vtk.vtkDataSetMapper()
|
2013-05-08 21:29:51 +05:30
|
|
|
gridMapper.SetInput(pointgrid)
|
2016-03-02 18:22:33 +05:30
|
|
|
gridActor = vtk.vtkActor()
|
2013-05-08 21:29:51 +05:30
|
|
|
gridActor.SetMapper(gridMapper)
|
|
|
|
gridActor.GetProperty().SetColor(0,0,0)
|
|
|
|
gridActor.GetProperty().SetPointSize(3)
|
|
|
|
|
|
|
|
|
2016-03-02 18:22:33 +05:30
|
|
|
renderer = vtk.vtkRenderer()
|
|
|
|
renderWindow = vtk.vtkRenderWindow()
|
2013-05-13 15:06:19 +05:30
|
|
|
renderWindow.FullScreenOn()
|
2013-05-08 21:29:51 +05:30
|
|
|
renderWindow.AddRenderer(renderer)
|
2016-03-02 18:22:33 +05:30
|
|
|
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
|
2013-05-08 21:29:51 +05:30
|
|
|
renderWindowInteractor.SetRenderWindow(renderWindow)
|
|
|
|
renderer.AddActor(meshActor)
|
|
|
|
renderer.AddActor(boxActor)
|
|
|
|
renderer.AddActor(gridActor)
|
|
|
|
renderer.SetBackground(1,1,1)
|
|
|
|
|
|
|
|
renderWindow.Render()
|
2016-03-02 18:22:33 +05:30
|
|
|
renderWindowInteractor.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
|
2013-05-08 21:29:51 +05:30
|
|
|
renderWindowInteractor.Start()
|
|
|
|
|