I am working on designing and following pre-defined and in real-time trajectories in brain interventions in Slicer.
I am wondering whether there is a module/way to calculate in real-time the error between desired and current trajectory for a pre-inserted cannula so the surgeon can re-align the current trajectory to meet desired one before insertion, i made a rough image from 3D Slicer,

I donâ€™t think there is a specific module for this. I would measure the distance between the planned and actual entry points and target (tip) points of the cannula. You can also calculate the angle between the two lines. While there may be no module that does exactly this, you could export the line end-points as tables and do the calculations in Excel, or if you are familiar with Python, you could quickly implement a script or a module that does these calculations inside Slicer.

I worked on the code to further fit my needs that is to decompose the measured angle the its x, y and z components,
My summary of what i did is that in order to get the x, y, and z angles between two vectors, i took the dot product of the projections of the two vectors onto the orthogonal plane of the axis i want. That is, if i wanted the z-angle between the two vectors, create xy-plane vectors of the originals. To do this, a vector that ignores the z-component of the vectors is made.

I went through linear algebra and the code got missy little bit, i shared a section of it for illustration.

Is there a function/library for example similar to â€śvtk.vtkMath.AngleBetweenVectorsâ€ť that can decompose the measured angle into x, y and z?

You could probably simplify the code. To normalize a vector (in numpy format), you can just divide it by its norm: v = v / np.linalg.norm(v)
Since X, Y, and Z axes in your case are (1,0,0), (0,1,0), (0,0,1), you could simplify the projection of a vector v to the YZ plane by doing this: v[0] = 0.0
So the angle in the â€śsagittalâ€ť plane between two vectors (v and w) would be:
v[0] = 0.0
w[0] = 0.0
vtk.vtkMath.AngleBetweenVectors(v, w)

For the record, I donâ€™t alway like to use the AngleBetweenVectors, because the result doesnâ€™t give me a sense of direction. E.g. I cannot tell if w is in flexion or extension relative to v, if we are talking in anatomical terms. If you calculate the angle using cross product, and you have a reference direction vector d, then you can compute the dot product of the cross product: np.dot(np.cross(v,w),d) and the sign of the result will tell you if w is clockwise or counter-clockwise relative to v if you look at them from that particular direction d. You can safely ignore this paragraph if you are only interested in the absolute value of the angle.