.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/3_registration/plot_rigid_0_2d.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_auto_examples_3_registration_plot_rigid_0_2d.py: Rigid alignment in 2D ===================== This is an example of rigid alignment in 2D for meshes with points in dense correspondence. .. GENERATED FROM PYTHON SOURCE LINES 8-15 .. code-block:: Python import pyvista as pv import torch from pyvista import examples import skshapes as sks .. GENERATED FROM PYTHON SOURCE LINES 16-19 We load two toy shapes in 2D: wireframe meshes representing circles. To be able to see orientations, we remove one edge from each circle. The points of both circles are in dense correspondence. .. GENERATED FROM PYTHON SOURCE LINES 19-32 .. code-block:: Python source = sks.PolyData(examples.download_human()) target = sks.PolyData(examples.download_doorman()) # Load the circles source = sks.Circle() target = sks.Circle() # Remove one edge from each circle source.edges = source.edges[:-1] target.edges = target.edges[:-1] .. rst-class:: sphx-glr-script-out .. code-block:: none /opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/site-packages/skshapes/input_validation/converters.py:102: UserWarning: Mesh has been cleaned and points were removed. point_data is ignored. return func(*new_args, **kwargs) .. GENERATED FROM PYTHON SOURCE LINES 33-35 Rigid motion are parametrized by a rotation and a translation. We apply a rigid motion to one of the circles. .. GENERATED FROM PYTHON SOURCE LINES 35-67 .. code-block:: Python theta = 0.75 * torch.pi # angle in radians translation = torch.tensor([1.0, 0.8]) # translation in the plane # In 2D, the parameter is a 3D vector: [theta, tx, ty] parameter = torch.cat([torch.tensor([theta]), translation]) # Apply the rigid motion to the circle rigid_motion = sks.RigidMotion() source = rigid_motion.morph( shape=source, parameter=torch.cat([torch.tensor([theta]), translation]), ).morphed_shape plotter = pv.Plotter() plotter.add_mesh( source.to_pyvista(), color="blue", show_edges=True, line_width=5, label="Source", ) plotter.add_mesh( target.to_pyvista(), color="red", show_edges=True, line_width=5, label="Target", ) plotter.add_legend() plotter.show() .. tab-set:: .. tab-item:: Static Scene .. image-sg:: /auto_examples/3_registration/images/sphx_glr_plot_rigid_0_2d_001.png :alt: plot rigid 0 2d :srcset: /auto_examples/3_registration/images/sphx_glr_plot_rigid_0_2d_001.png :class: sphx-glr-single-img .. tab-item:: Interactive Scene .. offlineviewer:: /home/runner/work/scikit-shapes/scikit-shapes/doc/source/auto_examples/3_registration/images/sphx_glr_plot_rigid_0_2d_001.vtksz .. GENERATED FROM PYTHON SOURCE LINES 68-70 Registration ------------ .. GENERATED FROM PYTHON SOURCE LINES 70-88 .. code-block:: Python from skshapes.loss import L2Loss from skshapes.morphing import RigidMotion from skshapes.tasks import Registration loss = L2Loss() model = RigidMotion(n_steps=5) registration = Registration( model=model, loss=loss, n_iter=5, verbose=True, ) registration.fit(source=source, target=target) .. rst-class:: sphx-glr-script-out .. code-block:: none Initial loss : 4.99e+01 = 4.99e+01 + 1 * 0.00e+00 (fidelity + regularization_weight * regularization) Loss after 1 iteration(s) : 4.30e-13 = 4.30e-13 + 1 * 0.00e+00 (fidelity + regularization_weight * regularization) Loss after 2 iteration(s) : 4.30e-13 = 4.30e-13 + 1 * 0.00e+00 (fidelity + regularization_weight * regularization) Loss after 3 iteration(s) : 4.30e-13 = 4.30e-13 + 1 * 0.00e+00 (fidelity + regularization_weight * regularization) Loss after 4 iteration(s) : 4.30e-13 = 4.30e-13 + 1 * 0.00e+00 (fidelity + regularization_weight * regularization) Loss after 5 iteration(s) : 4.30e-13 = 4.30e-13 + 1 * 0.00e+00 (fidelity + regularization_weight * regularization) .. GENERATED FROM PYTHON SOURCE LINES 89-90 Animation .. GENERATED FROM PYTHON SOURCE LINES 90-108 .. code-block:: Python path = registration.path_ plotter = pv.Plotter() actor = plotter.add_mesh( source.to_pyvista(), color="blue", show_edges=True, line_width=5 ) plotter.add_mesh( target.to_pyvista(), color="red", show_edges=True, line_width=5 ) plotter.open_gif("rigid_registration.gif", fps=3) for _i, shape in enumerate(path): plotter.remove_actor(actor) actor = plotter.add_mesh( shape.to_pyvista(), color="blue", show_edges=True, line_width=5 ) plotter.write_frame() plotter.close() .. image-sg:: /auto_examples/3_registration/images/sphx_glr_plot_rigid_0_2d_002.gif :alt: plot rigid 0 2d :srcset: /auto_examples/3_registration/images/sphx_glr_plot_rigid_0_2d_002.gif :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.356 seconds) .. _sphx_glr_download_auto_examples_3_registration_plot_rigid_0_2d.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_rigid_0_2d.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_rigid_0_2d.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_