I tried this code :
import slicer
import numpy as np
Fonction to Create a translate transform
def create_translation_transform(x, y, z):
transform = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLTransformNode”)
matrix = vtk.vtkMatrix4x4()
matrix.SetElement(0, 3, -x)
matrix.SetElement(1, 3, -y)
matrix.SetElement(2, 3, -z)
transform.SetMatrixTransformToParent(matrix)
return transform
Fonction to Align with Z axis
def create_rotation_transform(angle, axis):
transform = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLTransformNode”)
matrix = vtk.vtkMatrix4x4()
axis = np.array(axis) / np.linalg.norm(axis)
c, s = np.cos(angle), np.sin(angle)
C = 1 - c
x, y, z = axis
matrix.SetElement(0, 0, xxC + c)
matrix.SetElement(0, 1, xyC - zs)
matrix.SetElement(0, 2, xzC + ys)
matrix.SetElement(1, 0, yxC + zs)
matrix.SetElement(1, 1, yyC + c)
matrix.SetElement(1, 2, yzC - xs)
matrix.SetElement(2, 0, zxC - ys)
matrix.SetElement(2, 1, zyC + xs)
matrix.SetElement(2, 2, zzC + c)
transform.SetMatrixTransformToParent(matrix)
return transform
Define point to align with origin (0.0.0)
point_origin = [13.311, -14.499, -1.052] # Remplacer par les coordonnées réelles du point
Define point of the line to align with z axis
point1 = [16.183, 0.719, 11.181] # Remplacer par les coordonnées réelles du point 1
point2 = [13.277, -14.627, -1.061] # Remplacer par les coordonnées réelles du point 2
Compute line direction
line_direction = np.array(point2) - np.array(point1)
line_direction = line_direction / np.linalg.norm(line_direction)
Compute the angle of the line
axis_z = np.array([0, 0, 1])
angle = np.arccos(np.dot(line_direction, axis_z))
Create and apply the fonction of translate transforme
translation_transform = create_translation_transform(*point_origin)
slicer.util.getNode(‘YourNodeName’).SetAndObserveTransformNodeID(translation_transform.GetID())
Create and apply rotate transform
rotation_transform = create_rotation_transform(angle, np.cross(line_direction, axis_z))
slicer.util.getNode(‘YourNodeName’).SetAndObserveTransformNodeID(rotation_transform.GetID())
slicer.app.processEvents()
And this is note exactly the result I want :
Before
After
But it seems a bit better, but the bounding box and the markups not follow …