.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated\examples\transform\plot_rotate_and_translate.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. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_generated_examples_transform_plot_rotate_and_translate.py: ==================== Rotate and translate ==================== Rotating and translating a 3D volume. This example illustrates the use of the function `.rotate()` from the dbdicom extension :ref:`extension-vreg`. We use an ellipsoid digital reference object as test data and `.plot_surface()` from extension :ref:`extension-matplotlib` to visualise the results. .. GENERATED FROM PYTHON SOURCE LINES 10-19 .. code-block:: Python import numpy as np from dbdicom.dro import double_ellipsoid from dbdicom.extensions.vreg import rotate from dbdicom.extensions.matplotlib import plot_surface, plot_mosaic # Choose image 1 as a thumbnail for the gallery # sphinx_gallery_thumbnail_number = 2 .. GENERATED FROM PYTHON SOURCE LINES 20-21 Generate and display an ellipsoid test object .. GENERATED FROM PYTHON SOURCE LINES 21-25 .. code-block:: Python ellipsoid_orig = double_ellipsoid(12, 40, 32, spacing=(2,3,1), levelset=True) plot_surface(ellipsoid_orig) .. image-sg:: /generated/examples/transform/images/sphx_glr_plot_rotate_and_translate_001.png :alt: plot rotate and translate :srcset: /generated/examples/transform/images/sphx_glr_plot_rotate_and_translate_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 26-27 Define a rotation vector, apply it and display the result again. .. GENERATED FROM PYTHON SOURCE LINES 27-37 .. code-block:: Python # Define an anticlockwise rotation of 30 degrees around the y-axis rotation = -30*(np.pi/180)*np.array([0,1,0]) # Perform the rotation and return a rotated series ellipsoid_rot = rotate(ellipsoid_orig, rotation, reshape=True, mode='nearest') # Display the surface of the rotated shape plot_surface(ellipsoid_rot) .. image-sg:: /generated/examples/transform/images/sphx_glr_plot_rotate_and_translate_002.png :alt: plot rotate and translate :srcset: /generated/examples/transform/images/sphx_glr_plot_rotate_and_translate_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 38-39 Display the rotated shape as a mosaic .. GENERATED FROM PYTHON SOURCE LINES 39-42 .. code-block:: Python plot_mosaic(ellipsoid_rot) .. image-sg:: /generated/examples/transform/images/sphx_glr_plot_rotate_and_translate_003.png :alt: plot rotate and translate :srcset: /generated/examples/transform/images/sphx_glr_plot_rotate_and_translate_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 43-44 When applying the rotation we used the `mode='nearest'` so that the values outside the boundaries of the volume are filled by nearest neighbour sampling. The default setting would fill these with a `constant=0` value, producing an additional surface at the edge of the volume: .. GENERATED FROM PYTHON SOURCE LINES 44-49 .. code-block:: Python ellipsoid_rot = rotate(ellipsoid_orig, rotation, reshape=True) plot_surface(ellipsoid_rot) .. image-sg:: /generated/examples/transform/images/sphx_glr_plot_rotate_and_translate_004.png :alt: plot rotate and translate :srcset: /generated/examples/transform/images/sphx_glr_plot_rotate_and_translate_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 50-51 We used `reshape=True` so the new volume would encompass the entire shape. Running this with the default setting of `reshape=False` retains the original image shape and therefore misses part of the rotated volume: .. GENERATED FROM PYTHON SOURCE LINES 51-53 .. code-block:: Python ellipsoid_rot = rotate(ellipsoid_orig, rotation, reshape=False, mode='nearest') plot_surface(ellipsoid_rot) .. image-sg:: /generated/examples/transform/images/sphx_glr_plot_rotate_and_translate_005.png :alt: plot rotate and translate :srcset: /generated/examples/transform/images/sphx_glr_plot_rotate_and_translate_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 24.695 seconds) .. _sphx_glr_download_generated_examples_transform_plot_rotate_and_translate.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/QIB-Sheffield/dbdicom/main?urlpath=lab/tree/notebooks/generated/examples/transform/plot_rotate_and_translate.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_rotate_and_translate.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_rotate_and_translate.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_rotate_and_translate.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_