# Calculate axis of rotation

**URL:** <https://discourse.slicer.org/t/calculate-axis-of-rotation/15871>\
**Category:** Support\
**Tags:** registration, segmentation\
**Created:** [February 5, 2021, 9:21pm UTC](https://discourse.slicer.org/t/calculate-axis-of-rotation/15871 "2021-02-05T21:21:35Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [February 7, 2021, 2:32pm UTC](https://discourse.slicer.org/t/calculate-axis-of-rotation/15871/7 "2021-02-07T14:32:06Z")

</div>

If you have a single relative transform then copy-paste [this `rotation_from_matrix` function](https://docs.mdanalysis.org/1.0.0/_modules/MDAnalysis/lib/transformations.html#rotation_from_matrix) to the Python console, and then to get rotation axis from a transform node you can run this:

```python
transformNode = getNode('Transform') # replace 'Transform' with the actual name of your transform node
import numpy as np
import math
transformMatrix = arrayFromTransformMatrix(transformNode)
angle, axisDirection, axisPosition = rotation_from_matrix(transformMatrix)

# Display rotation axis
rotationAxisMarkupsNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsLineNode", "RotationAxis")
rotationAxisMarkupsNode.AddControlPoint(vtk.vtkVector3d(axisPosition[:3]-50*axisDirection))
rotationAxisMarkupsNode.AddControlPoint(vtk.vtkVector3d(axisPosition[:3]+50*axisDirection))

```

Getting rotation axis from a single relative transform is probably not very robust or accurate. It would be better to compute an “average” axis from multiple transforms, similarly how Pivot calibration module in SlicerIGT extension does it (see a recent discussion about the algorithm that the module uses [here](https://discourse.slicer.org/t/what-is-the-algorithm-and-theory-of-spin-calibration/15434/2)).

---

_[View the full topic](https://discourse.slicer.org/t/calculate-axis-of-rotation/15871)._
