diff --git a/tests/common.py b/tests/common.py new file mode 100644 index 000000000..553920551 --- /dev/null +++ b/tests/common.py @@ -0,0 +1,42 @@ +import math + +import numpy as np + +import hexrd.constants as ct + + +def convert_axis_angle_to_rmat(axis, angle): + # Copied from: https://github.com/ovillellas/xrd-transforms/blob/b94f8b2d7839d883829d00a2adc5bec9c80e0116/test_xrd_transforms/common.py#L59 # noqa + # This is based on + # https://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToMatrix/ + + angle = float(angle) + axis = np.array(axis, dtype=float) + assert axis.shape == (3,) + + if abs(angle) < ct.epsf: + return ct.identity_3x3 + + axis_norm = np.linalg.norm(axis) + if axis_norm < ct.epsf: + raise ValueError("axis is zero") + + axis /= axis_norm + + m = np.empty((3, 3), dtype=float) + + c = math.cos(angle) + s = math.sin(angle) + t = 1.0 - c + + m[0, 0] = c + axis[0]*axis[0]*t + m[0, 1] = axis[0]*axis[1]*t - axis[2]*s + m[0, 2] = axis[0]*axis[2]*t + axis[1]*s + m[1, 0] = axis[0]*axis[1]*t + axis[2]*s + m[1, 1] = c + axis[1]*axis[1]*t + m[1, 2] = axis[1]*axis[2]*t - axis[0]*s + m[2, 0] = axis[0]*axis[2]*t - axis[1]*s + m[2, 1] = axis[1]*axis[2]*t + axis[0]*s + m[2, 2] = c + axis[2]*axis[2]*t + + return m diff --git a/tests/data/gvec_to_xy.json b/tests/data/gvec_to_xy.json index 1a36e7f4e..495af9393 100644 --- a/tests/data/gvec_to_xy.json +++ b/tests/data/gvec_to_xy.json @@ -17,6 +17,28 @@ }, "output": [ 0.13349048, -1.61131393] }, + { + "input": { + "gvec_c": [[ 0.57735027, 0.57735028, 0.57735027], + [ 0.57735027, -0.57735027, 0.57735028], + [ 0.57735028, 0.57735027, -0.57735027]], + "rmat_d": [[1.0, 0.0, 0.0], + [0.0, 1.0, 0.0], + [0.0, 0.0, 1.0]], + "rmat_s": [[ 0.77029942, 0.0 , 0.63768237], + [ 0.0 , 1.0 , -0.0 ], + [-0.63768237, 0.0 , 0.77029942]], + "rmat_c": [[ 0.91734473, -0.08166131, 0.38962815], + [ 0.31547749, 0.74606417, -0.58639766], + [-0.24280159, 0.66084771, 0.71016033]], + "tvec_d": [ 0.0 , 1.5 , -5.0 ], + "tvec_s": [ 0.0 , 0.0 , 0.0 ], + "tvec_c": [-0.25, -0.25, -0.25] + }, + "output": [[ 0.13349048, -1.61131393], + [-11.66778585, 10.41129093], + [ -0.67291173, -5.11162229]] + }, { "input": { "gvec_c": [[ 0.57735027, 0.57735028, 0.57735027], diff --git a/tests/test_transforms.py b/tests/test_transforms.py index 446b152e0..79417834a 100644 --- a/tests/test_transforms.py +++ b/tests/test_transforms.py @@ -1,9 +1,12 @@ +import copy import json import numpy as np from hexrd.transforms.xfcapi import gvec_to_xy +from common import convert_axis_angle_to_rmat + def test_gvec_to_xy(test_data_dir): with open(test_data_dir / 'gvec_to_xy.json') as rf: @@ -11,8 +14,25 @@ def test_gvec_to_xy(test_data_dir): for entry in test_data: kwargs = entry['input'] - output = entry['output'] + expected = entry['output'] kwargs = {k: np.asarray(v) for k, v in kwargs.items()} result = gvec_to_xy(**kwargs) - assert np.allclose(result, output) + assert np.allclose(result, expected) + + # Verify that we get the correct answer with a rotation. + rot = convert_axis_angle_to_rmat(np.r_[0.5, 0.2, 0.6], 1.0) + + rotated_kwargs = copy.deepcopy(kwargs) + rotated_kwargs['beam_vec'] = np.r_[0.0, 0.0, -1.0] + + # The following are not rotated: + # gvec_c are relative to the crystal frame + # rMat_c is in sample frame + # tvec_c is relative to sample frame + to_rotate = ['rmat_d', 'rmat_s', 'tvec_d', 'tvec_s', 'beam_vec'] + for k in to_rotate: + rotated_kwargs[k] = rot @ rotated_kwargs[k] + + result = gvec_to_xy(**rotated_kwargs) + assert np.allclose(result, expected)