-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #613 from HEXRD/test-gvec-to-xy
Add a test for gvec_to_xy
- Loading branch information
Showing
5 changed files
with
170 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
[ | ||
{ | ||
"input": { | ||
"gvec_c": [ 0.57735027, 0.57735028, 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] | ||
}, | ||
{ | ||
"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], | ||
[ 0.57735027, -0.57735027, 0.57735028], | ||
[ 0.57735027, -0.57735028, -0.57735026], | ||
[ 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]], | ||
|
||
[[ 0.97986016, 0.0 , -0.19968493], | ||
[-0.0 , 1.0 , -0.0 ], | ||
[ 0.19968493, 0.0 , 0.97986016]], | ||
|
||
[[ 0.30523954, 0.0 , -0.9522756 ], | ||
[-0.0 , 1.0 , -0.0 ], | ||
[ 0.9522756 , 0.0 , 0.30523954]], | ||
|
||
[[ 0.73506994, 0.0 , -0.67799129], | ||
[-0.0 , 1.0 , -0.0 ], | ||
[ 0.67799129, 0.0 , 0.73506994]]], | ||
"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], | ||
[ 0.19186549, -2.03119741], | ||
[ 0.63614123, -1.70709656], | ||
[ 0.12934705, -1.29999638]] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
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: | ||
test_data = json.load(rf) | ||
|
||
for entry in test_data: | ||
kwargs = entry['input'] | ||
expected = entry['output'] | ||
|
||
kwargs = {k: np.asarray(v) for k, v in kwargs.items()} | ||
result = gvec_to_xy(**kwargs) | ||
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) |