.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/rotations/combine_rotations.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_rotations_combine_rotations.py: =================== Combining rotations =================== This example demonstrates how to combine two rotations :math:`R_1` and :math:`R_2`, i.e. from right to left like so .. math:: R_{12} = R_2 \cdot R_1. To convince ourselves that this order is correct, we rotate a vector :math:`\mathbf{v} = (0, 0, z)` by two rotations: rotation :math:`R_1` by :math:`120^{\circ}` around :math:`[1 1 1]` followed by rotation :math:`R_2` by :math:`180^{\circ}` around :math:`[1 1 0]`. Rotation :math:`R_1` is carried out first, followed by rotation :math:`R_2`. Note that a negative angle :math:`\omega` when *defining* a rotation in the axis-angle representation :math:`(\hat{\mathbf{n}}, \omega)` is necessary for consistent transformations between rotation representations. .. GENERATED FROM PYTHON SOURCE LINES 41-101 .. image-sg:: /examples/rotations/images/sphx_glr_combine_rotations_001.png :alt: combine rotations :srcset: /examples/rotations/images/sphx_glr_combine_rotations_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none R1, R2 and R12: * As quaternions: Rotation (3,) [[ 0.5 -0.5 -0.5 -0.5 ] [ 0. -0.7071 -0.7071 -0. ] [-0.7071 -0. -0.7071 -0. ]] * As orientation matrices: [[[ 0. 1. -0.] [-0. 0. 1.] [ 1. -0. 0.]] [[ 0. 1. -0.] [ 1. 0. 0.] [ 0. -0. -1.]] [[-0. 0. 1.] [ 0. 1. -0.] [-1. 0. -0.]]] Point rotated by R12: [0, 0, 1] -> [1.0, -0.0, -0.0] | .. code-block:: Python import matplotlib.pyplot as plt from orix.plot import register_projections from orix.quaternion import Rotation from orix.vector import Vector3d plt.rcParams.update({"font.size": 12, "grid.alpha": 0.5}) register_projections() # Register our custom Matplotlib projections R1 = Rotation.from_axes_angles([1, 1, 1], -120, degrees=True) R2 = Rotation.from_axes_angles([1, 1, 0], -180, degrees=True) R12 = R2 * R1 R_all = Rotation.stack([R1, R2, R12]).squeeze() print("R1, R2 and R12:\n* As quaternions:\n", R_all) print("* As orientation matrices:\n", R_all.to_matrix().squeeze().round(10)) v_start = Vector3d.zvector() v_end = R12 * v_start print( "Point rotated by R12:\n", v_start.data.squeeze().tolist(), "->", v_end.data.squeeze().round(10).tolist(), ) # Illustrate the steps of the rotation by plotting the vector before # (red), during (green), and after (blue) the rotation and the rotation # paths (first: cyan; second: magenta) v_intermediate = R1 * v_start v_si_path = Vector3d.from_path_ends(Vector3d.stack([v_start, v_intermediate])) v_sie_path = Vector3d.from_path_ends(Vector3d.stack([v_intermediate, v_end])) fig = plt.figure(layout="tight") ax0 = fig.add_subplot(121, projection="stereographic") ax1 = fig.add_subplot(122, projection="stereographic", hemisphere="lower") ax0.stereographic_grid(), ax1.stereographic_grid() Vector3d.stack([v_start, v_intermediate, v_end]).scatter( figure=fig, s=50, c=["r", "g", "b"], axes_labels=["e1", "e2"], ) ax0.plot(v_si_path, color="c"), ax1.plot(v_si_path, color="c") ax0.plot(v_sie_path, color="m"), ax1.plot(v_sie_path, color="m") R1.axis.scatter(figure=fig, c="orange") R2.axis.scatter(figure=fig, c="k") text_kw = { "bbox": {"alpha": 0.5, "fc": "w", "boxstyle": "round,pad=0.1"}, "ha": "right", "va": "top", } ax0.text(v_start, s="Start", **text_kw) ax1.text(v_intermediate, s="Intermediate", **text_kw) ax1.text(v_end, s="End", **text_kw) ax1.text(R1.axis, s=r"$\hat{\mathbf{n}}_1$", **text_kw) ax0.text(R2.axis, s=r"$\hat{\mathbf{n}}_2$", **text_kw) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.381 seconds) **Estimated memory usage:** 548 MB .. _sphx_glr_download_examples_rotations_combine_rotations.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: combine_rotations.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: combine_rotations.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: combine_rotations.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_