Get relative translation and rotation of two models

Thank you Professor @lassoan.
It only gives translation along the xyz axes. Is it because the displacement here is based on the centre of mass/ centroid? If I want the rotation of a the rigid body/ bone- what should I do. In the transforms matrix rotation is indicated as 0 in all 3 axes. Do i have to define an axis of rotation please? Thanks

Tom

The sliders are only for changing the transforms, they don’t necessarily represent the current state.

The transformation is specified by a 4x4 homogeneous transformation matrix (top-left 3x3 matrix defines orientation and scaling, top-right 3x1 vector defines translation).

1 Like

Apologies Prof Lasso @lassoan.
I did not understand I am afraid- I am trying to see the change of position in terms of translation and rotation of two segments. Could you please let me know the numerical values given in the transform correspond to the translation of segment 2, compared to segment 1. If not, what should I do to calculate that, please? Also, how can I calculate rotation, please? Apologies for taking your time- I am a medical person with no software background. Thanks a lot.

Hi All

Sorry about all this amateur questions and new post. But could someone explain me how to get the displacement of one segment to another in terms of rotations please. I did segment registration and got a the linear transform, but I don’t know how to calculate Euler s angles of rotation of my segment out of this numbers. Thanks a lot

You can get Euler angles orientation matrix using this code snippet:

transformNode = getNode('LinearTransform_3')
transformMatrix = vtk.vtkMatrix4x4()
transformNode.GetMatrixTransformToParent(transformMatrix)
transform = vtk.vtkTransform()
transform.SetMatrix(transformMatrix)
orientation = transform.GetOrientation()
print(orientation)

Note that the same orientation can be encoded using many different combinations of rotations, even Euler angles have many variants.

If you need to use a particular convention then you can implement the angle computations from the corresponding mathematical formulae directly you can access orientation matrix element in the transformMatrix object in the example above.

1 Like

I can’t thank you enough Professor @lassoan
I will try this- I don’t get all the mathematical side of it, but my project is to segment bones and calculate the translation and rotation with time points. so I m planning to take the translation values , off the transform module top right and rotation in Euler angles with this. hope my steps are correct. Thanks again.

Hi Prof @lassoan

I have tried to use this code for my calculations of Euler angles.
When I apply to my linear transformation of known roll yaw and pitch, the values I get is only 2 values correct. What could be the possible.

Thanks

The same orientation can be encoded using many different combinations of rotations, even Euler angles have many variants. The code snippet above uses ZXY rotation order - see complete conversion between matrix and Euler angles representation in this post:

1 Like

Thank you Professor @lassoan

Hello Andras,

I’m wondering what the Z, X, and Y axes are in terms of LR, PA, and IS axes?
Thank you,

Michele

Slicer’s coordinate system axis directions are right, anterior, superior.

Yes, but you mentioned in your earlier post that the code snippet is uses the ZXY rotation order, and I was wondering which axes these correspond to, in the slicer axes?

Thank you,

Michele

I meant first, second, third coordinate system axis by X, Y, Z . Therefore, X=R, Y=A, Z=S.

Oh my apologies. Thank you very much!!

Michele

Sorry to revive this, but how would you put this into the XYZ order

Could you describe a bit more detail ecset you want to achieve?

So currently i have 3 models of a femur. A preoperative, postoperative and planning. I made a coordinate system that i made from three points of this femur. I allign the coordinate system with the World coordinate system and allign all my femoral shafts as shown here:
final coordinatesystem, everything alligned

All transformations have been hardened and i measure from the point ive set. Through alpaca i allign the proximal parts to see how they’ve moved.
post to pre second
t and use this transformation
post to planning 22

I return the euler angles from the matrices with the script as described above. This gives the euler angles in a ZXY order, i would like it to be in a XYZ order if possible.

Thank you so much for your help.

Each column of the transformation matrix contain direction of one coordinate system axis. Therefore, you can use the same vtkTransform.GetOrientation() method and just reorder the columns of the input transformation matrix. It is probably a good idea to keep the transformation matrix right-handed by swapping columns even numner of times; or swapping columns odd number of times and inverting the direction of one axis.

Note that Euler angles is generally considered to be a poor representation of orientation. Problems include gimbal lock and that the angles don’t have an intuitive meaning when more than one angle is significantly different than 0 (because the second and third rotations are performed around already rotated axes). If you want to characterize misalignment, I would recommend measuring angle between axes in anterior-posterior and lateral projections - similarly how you would measure angles in two planar X-ray projection images. To compute these angles you would not need to arbitrarily choose an ordering of rotation axes.

I am unfortunately not well acquanted with euler angles. This might be a quite uninformed question.

So i get the euler rotations in ZXY order above. Translation i get from the 4x4 matrix.
For instance as above: i perform a model registration through ALPACA, it gives me a matrix. From this matrix i get the ZXY of euler angles. Now when i put these angles in the ZXY order into a new transformation (with the translational component of the 4x4 matrix) to see if i would again get the same allignment, this does not occur. I know this is a flaw in my understanding of Euler angles, however i cannot seem to find a answer to this.

Im very gratefull for the time you’ve taken to answer my questions.

Conversion from orientation matrix to Euler angles is not well defined (the same orientation may be represented with various combination of rotations). However, conversion from Euler angles to orientation matrix should always provide the same matrix as the one that the Euler angles are computed from.

You need to play a bit with the order of rotation axes, but the vtkTransform class can be used for both conversions. For example:

print("=============================")
import random
random.random()
rx = random.random() * 180 - 90
ry = random.random() * 180 - 90
rz = random.random() * 180 - 90
transform = vtk.vtkTransform()
transform.RotateZ(rz)
transform.RotateX(rx)
transform.RotateY(ry)
print(f"Euler angles (original): {rx}, {ry}, {rz}")
print(f"Matrix (original): {transform.GetMatrix()}")

print("--------------")
[rx2, ry2, rz2] = transform.GetOrientation()
transform2 = vtk.vtkTransform()
transform2.RotateZ(rz2)
transform2.RotateX(rx2)
transform2.RotateY(ry2)
print(f"Euler angles (computed from matrix): {rx2}, {ry2}, {rz2}")
print(f"Matrix (from computed Euler angles): {transform2.GetMatrix()}")

Output:

=============================
Euler angles (original): 31.012387417137504, -0.18095415482437716, 65.38408784135231
Matrix (original): vtkMatrix4x4 (000001BBE31DC5C0)
  Debug: Off
  Modified Time: 243130
  Reference Count: 2
  Registered Events: (none)
  Elements:
    0.418011 -0.779167 0.467082 0 
    0.908438 0.356992 -0.217478 0 
    0.00270679 0.515223 0.857052 0 
    0 0 0 1 
--------------
Euler angles (computed from matrix): 31.012387417137504, -0.18095415482437716, 65.38408784135231
Matrix (from computed Euler angles): vtkMatrix4x4 (000001BBE31DBC60)
  Debug: Off
  Modified Time: 243145
  Reference Count: 2
  Registered Events: (none)
  Elements:
    0.418011 -0.779167 0.467082 0 
    0.908438 0.356992 -0.217478 0 
    0.00270679 0.515223 0.857052 0 
    0 0 0 1