Rotate vectors from sample to detector reference frame#

This example shows how to transform column vectors given in the sample reference frame to the detector reference frame using sample_to_detector.

Imports.

import numpy as np
import orix.vector as ove

import kikuchipy as kp

Create a detector.

EBSDDetector
  shape (Ny, Nx):     (1, 1)
  pc (PCx, PCy, PCz): (0.5, 0.5, 0.5)
  sample_tilt:        70.0°
  tilt:               0.0°
  azimuthal:          0.0°
  twist:              0.0°
  binning:            1
  px_size:            1.0 um

Get rotation and orientation matrix.

R_s2d = det.sample_to_detector
om_s2d = R_s2d.to_matrix().squeeze()

print(R_s2d)
print(om_s2d.round(4))
Rotation (1,)
[[ 0.6964 -0.1228 -0.1228 -0.6964]]
[[-0.      1.      0.    ]
 [-0.9397 -0.      0.342 ]
 [ 0.342   0.      0.9397]]

We see that the detector \(X_d\) is parallel to the sample \(Y_s\). If we change the detector azimuthal angle, this would not be the case. We can check the effect of various other angles easily.

print(
    kp.detectors.EBSDDetector(azimuthal=5)
    .sample_to_detector.to_matrix()
    .squeeze()
    .round(4)
)
[[ 0.0298  0.9962  0.0819]
 [-0.9397  0.      0.342 ]
 [ 0.3407 -0.0872  0.9361]]

Create some vectors given in the sample reference frame.

Vector3d (4,)
[[ 0.3668  0.8736 -0.3198]
 [ 0.1965  0.3186  0.9273]
 [ 0.5925  0.2468  0.7668]
 [-0.3085  0.8944  0.3239]]

Rotate to the detector using two paths: * Use the returned Rotation directly * Via NumPy’s matrix multiplication sign ‘@’ (making sure to transpose the underlying

vectors data to make the column vectors)

  • Via NumPy’s dot() function (making sure to transpose the underlying vectors data to make the column vectors)

v_d = R_s2d * v_s
v_d_np1 = (om_s2d @ v_s.data.T).T
v_d_np2 = np.dot(om_s2d, v_s.data.T).T

print(v_d.data.round(4))
print(v_d_np1.round(4))
print(v_d_np2.round(4))

# Check equality
print(np.allclose(v_d.data, v_d_np1, atol=1e-4))
print(np.allclose(v_d.data, v_d_np2, atol=1e-4))
[[ 0.8736 -0.454  -0.175 ]
 [ 0.3186  0.1325  0.9386]
 [ 0.2468 -0.2945  0.9232]
 [ 0.8944  0.4007  0.1989]]
[[ 0.8736 -0.454  -0.175 ]
 [ 0.3186  0.1325  0.9386]
 [ 0.2468 -0.2945  0.9232]
 [ 0.8944  0.4007  0.1989]]
[[ 0.8736 -0.454  -0.175 ]
 [ 0.3186  0.1325  0.9386]
 [ 0.2468 -0.2945  0.9232]
 [ 0.8944  0.4007  0.1989]]
True
True

Rotate from the detector to the sample.

R_d2s = ~R_s2d
om_d2s = om_s2d.T

v_s2 = R_d2s * v_d
v_s2_np1 = (om_d2s @ v_d.data.T).T
v_s2_np2 = np.dot(om_d2s, v_d.data.T).T

print(v_s2.data.round(4))
print(v_s2_np1.round(4))
print(v_s2_np2.round(4))

# Check equality
print(np.allclose(v_s2.data, v_s2_np1, atol=1e-4))
print(np.allclose(v_s2.data, v_s2_np2, atol=1e-4))
[[ 0.3668  0.8736 -0.3198]
 [ 0.1965  0.3186  0.9273]
 [ 0.5925  0.2468  0.7668]
 [-0.3085  0.8944  0.3239]]
[[ 0.3668  0.8736 -0.3198]
 [ 0.1965  0.3186  0.9273]
 [ 0.5925  0.2468  0.7668]
 [-0.3085  0.8944  0.3239]]
[[ 0.3668  0.8736 -0.3198]
 [ 0.1965  0.3186  0.9273]
 [ 0.5925  0.2468  0.7668]
 [-0.3085  0.8944  0.3239]]
True
True

Total running time of the script: (0 minutes 3.186 seconds)

Estimated memory usage: 790 MB

Gallery generated by Sphinx-Gallery