How to GetIJKToRASDirections from a volume node

Dear all,

I would like to get IJKToRASDirections from a node and set IJKToRASDirections to another in python script. But I do not think I get correct one.

    dirs = [[0] * 3] * 3
    body_roi_labelmap_node.GetIJKToRASDirections(dirs)
    print(dirs)
    copied_labelmap_node.SetIJKToRASDirections(dirs)

The dirs I got is [[0.0, 0.0, 1.0], [0.0, 0.0, 1.0], [0.0, 0.0, 1.0]]. The value is same for the three directions.

The corresponding C++ code:

//----------------------------------------------------------------------------
void vtkMRMLVolumeNode::CopyOrientation(vtkMRMLVolumeNode *node)
{
  double dirs[3][3];
  node->GetIJKToRASDirections(dirs);

  int disabledModify = this->StartModify();
  this->SetIJKToRASDirections(dirs);
  this->SetOrigin(node->GetOrigin());
  this->SetSpacing(node->GetSpacing());
  this->EndModify(disabledModify);
}

Thanks for any help.

Can you explain why you think that it should not be identity?

If it’s because you put it under a transform, then it’s normal, because the volume itself still has identity directions. You can either set the same transform as parent of the other volume, or you can harden the transform, which will “burn” the matrix in the volume directions and then you can use it the way you want. However, we typically simply use the transform hierarchy and the convenience functions to get the transform between objects when necessary.

it is not identity. The returned value is:
0 0 1
0 0 1
0 0 1

In python script, I could not reproduce the result of
copied_labelmap_node.CopyOrientation(body_roi_labelmap_node) with:

disableModify = copied_labelmap_node.StartModify()
dirs = [[0] * 3] * 3
body_roi_labelmap_node.GetIJKToRASDirections(dirs)
print(dirs)
copied_labelmap_node.SetIJKToRASDirections(dirs)
copied_labelmap_node.SetOrigin(body_roi_labelmap_node.GetOrigin())
copied_labelmap_node.SetSpacing(body_roi_labelmap_node.GetSpacing())
copied_labelmap_node.EndModify(disableModify)

The above code is identical to the C++ code of CopyOrientation.
So the only place could be wrong is where getting GetIJKToRASDirections and SetIJKToRASDirections.

This creation of a “matrix” caused the unexpected output: a list was created that referred to the same list 3 times. Effectively, the code did this: dirs = [v,v,v].

Instead, you need to provide unique values for all the 3x3 values in the matrix. Probably the simplest is to create it as a numpy array: dirs = np.eye(3).

I think what he needs is a zero matrix in which case is dirs = np.zeros([3, 3])

Values will be overwritten anyway by calling GetIJKToRASDirections, so in this specific example, either np.zeros or np.eye works.

However, since a null-matrix is not a valid direction matrix, it is not good practice to store it in a variable called “direction”, even temporarily. np.eye creates an identity matrix, which is what you want as a default direction matrix; and its syntax is a bit simpler than np.zeros, too.

1 Like

Thank you very much. This works.