2018-11-17 12:42:12 +05:30
|
|
|
#!/usr/bin/env python3
|
2015-11-12 01:21:17 +05:30
|
|
|
|
2019-05-25 02:01:34 +05:30
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from io import StringIO
|
2015-11-12 01:21:17 +05:30
|
|
|
from optparse import OptionParser
|
2019-05-26 13:19:56 +05:30
|
|
|
|
|
|
|
from scipy import ndimage
|
|
|
|
import numpy as np
|
|
|
|
|
2019-05-25 17:21:05 +05:30
|
|
|
import damask
|
2015-11-12 01:21:17 +05:30
|
|
|
|
2019-05-26 15:41:30 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2015-11-12 01:21:17 +05:30
|
|
|
|
2019-05-26 15:41:30 +05:30
|
|
|
|
2015-11-12 01:21:17 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
|
2016-05-12 12:24:34 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [geomfile(s)]', description = """
|
2019-05-26 21:39:34 +05:30
|
|
|
Rotates original microstructure and embeddeds it into buffer material.
|
2015-11-12 01:21:17 +05:30
|
|
|
|
|
|
|
""", version=scriptID)
|
|
|
|
|
|
|
|
parser.add_option('-r', '--rotation',
|
|
|
|
dest='rotation',
|
|
|
|
type = 'float', nargs = 4, metavar = ' '.join(['float']*4),
|
2019-05-28 06:44:09 +05:30
|
|
|
help = 'rotation given as axis and angle')
|
2015-11-12 01:21:17 +05:30
|
|
|
parser.add_option('-e', '--eulers',
|
|
|
|
dest = 'eulers',
|
|
|
|
type = 'float', nargs = 3, metavar = ' '.join(['float']*3),
|
|
|
|
help = 'rotation given as Euler angles')
|
|
|
|
parser.add_option('-d', '--degrees',
|
|
|
|
dest = 'degrees',
|
|
|
|
action = 'store_true',
|
2019-06-03 23:00:14 +05:30
|
|
|
help = 'Euler angles/axis angle are given in degrees')
|
2015-11-12 01:21:17 +05:30
|
|
|
parser.add_option('-m', '--matrix',
|
|
|
|
dest = 'matrix',
|
|
|
|
type = 'float', nargs = 9, metavar = ' '.join(['float']*9),
|
|
|
|
help = 'rotation given as matrix')
|
|
|
|
parser.add_option('-q', '--quaternion',
|
|
|
|
dest = 'quaternion',
|
|
|
|
type = 'float', nargs = 4, metavar = ' '.join(['float']*4),
|
|
|
|
help = 'rotation given as quaternion')
|
|
|
|
parser.add_option('-f', '--fill',
|
|
|
|
dest = 'fill',
|
2019-05-26 21:39:34 +05:30
|
|
|
type = 'float', metavar = 'int',
|
|
|
|
help = 'background microstructure index, defaults to max microstructure index + 1')
|
2015-11-12 01:21:17 +05:30
|
|
|
|
2019-05-26 15:41:30 +05:30
|
|
|
parser.set_defaults(degrees = False)
|
2015-11-12 01:21:17 +05:30
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
2019-05-26 13:19:56 +05:30
|
|
|
if [options.rotation,options.eulers,options.matrix,options.quaternion].count(None) < 3:
|
2019-05-25 20:30:41 +05:30
|
|
|
parser.error('more than one rotation specified.')
|
2019-05-26 13:19:56 +05:30
|
|
|
if [options.rotation,options.eulers,options.matrix,options.quaternion].count(None) > 3:
|
|
|
|
parser.error('no rotation specified.')
|
2015-11-12 01:21:17 +05:30
|
|
|
|
2019-02-24 02:43:24 +05:30
|
|
|
if options.quaternion is not None:
|
2020-05-16 14:47:12 +05:30
|
|
|
rot = damask.Rotation.from_quaternion(np.array(options.quaternion)) # we might need P=+1 here, too...
|
2019-02-24 02:43:24 +05:30
|
|
|
if options.rotation is not None:
|
2020-05-16 14:47:12 +05:30
|
|
|
rot = damask.Rotation.from_axis_angle(np.array(options.rotation),degrees=options.degrees,normalise=True,P=+1)
|
2019-02-24 02:43:24 +05:30
|
|
|
if options.matrix is not None:
|
2020-05-16 14:47:12 +05:30
|
|
|
rot = damask.Rotation.from_matrix(np.array(options.Matrix))
|
2019-02-24 02:43:24 +05:30
|
|
|
if options.eulers is not None:
|
2020-05-16 14:47:12 +05:30
|
|
|
rot = damask.Rotation.from_Eulers(np.array(options.eulers),degrees=options.degrees)
|
2017-08-23 23:39:33 +05:30
|
|
|
|
2020-05-17 11:31:34 +05:30
|
|
|
eulers = rot.as_Eulers(degrees=True)
|
2015-11-12 01:21:17 +05:30
|
|
|
|
2019-05-30 14:56:47 +05:30
|
|
|
|
2015-11-12 01:21:17 +05:30
|
|
|
if filenames == []: filenames = [None]
|
|
|
|
|
|
|
|
for name in filenames:
|
2019-05-25 13:44:53 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2019-05-28 06:44:09 +05:30
|
|
|
|
|
|
|
geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name)
|
2019-06-03 23:00:14 +05:30
|
|
|
size = geom.get_size()
|
|
|
|
grid = geom.get_grid()
|
|
|
|
origin = geom.get_origin()
|
2019-05-26 13:19:56 +05:30
|
|
|
microstructure = geom.get_microstructure()
|
2019-05-28 06:44:09 +05:30
|
|
|
fill = np.nanmax(microstructure)+1 if options.fill is None else options.fill
|
|
|
|
dtype = float if np.isnan(fill) or int(fill) != fill or microstructure.dtype==np.float else int
|
|
|
|
|
2019-05-26 13:19:56 +05:30
|
|
|
# These rotations are always applied in the reference coordinate system, i.e. (z,x,z) not (z,x',z'')
|
|
|
|
# this seems to be ok, see https://www.cs.utexas.edu/~theshark/courses/cs354/lectures/cs354-14.pdf
|
2019-05-25 02:01:34 +05:30
|
|
|
microstructure = ndimage.rotate(microstructure,eulers[2],(0,1),order=0,
|
2019-05-28 06:44:09 +05:30
|
|
|
prefilter=False,output=dtype,cval=fill) # rotation around z
|
2019-05-25 02:01:34 +05:30
|
|
|
microstructure = ndimage.rotate(microstructure,eulers[1],(1,2),order=0,
|
2019-05-28 06:44:09 +05:30
|
|
|
prefilter=False,output=dtype,cval=fill) # rotation around x
|
2019-05-25 02:01:34 +05:30
|
|
|
microstructure = ndimage.rotate(microstructure,eulers[0],(0,1),order=0,
|
2019-05-28 06:44:09 +05:30
|
|
|
prefilter=False,output=dtype,cval=fill) # rotation around z
|
2019-05-30 17:37:49 +05:30
|
|
|
|
2019-06-03 23:00:14 +05:30
|
|
|
damask.util.croak(geom.update(microstructure,origin=origin-(np.asarray(microstructure.shape)-grid)/2*size/grid,rescale=True))
|
2019-05-28 06:44:09 +05:30
|
|
|
geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:]))
|
2019-05-30 17:37:49 +05:30
|
|
|
|
2019-11-24 23:32:19 +05:30
|
|
|
geom.to_file(sys.stdout if name is None else name,pack=False)
|