napari / napari-animation

A napari plugin for making animations
https://napari.github.io/napari-animation/
Other
74 stars 27 forks source link

Interpolation of rotation matrix doesn't quarantee orthonormality #138

Open JoOkuma opened 1 year ago

JoOkuma commented 1 year ago

Hi,

I noticed that when napari-animation interpolates the rotation matrix it deforms the data (changes its scale) because it doesn't guarantee the rotation matrix is orthonormal.

I think it should use something similar to the slerp interpolation of the camera.angles but in the matrix transform domain.

For example, when rotating this 3D image it's actually changing the scale on the z-axis.

https://user-images.githubusercontent.com/21022743/201121747-31e1fd84-7193-4e0d-be86-adf129a1b137.mp4

Script to reproduce:

import napari
import numpy as np
from skimage import data
from napari_animation import Animation

if __name__ == "__main__":
    viewer = napari.view_image(data.cells3d(), channel_axis=1)

    animation = Animation(viewer)
    animation.capture_keyframe()

    R = np.diagflat([1, 1, -1])

    for l in viewer.layers:
        l.rotate = R

    viewer.reset_view()
    animation.capture_keyframe(127)
    animation.animate("video.mp4")

I can open a PR if this is agreed to be a bug.

alisterburt commented 1 year ago

ooh this isn't good! thanks for the report

Definitely a bug, totally ready for a PR with a fix if you have one :-)