Project a point onto a plane and get its coordinates

This code snippet may better answer your question. But you should use a plane markups node named ‘P’ instead of 2 lines intersecting at right angle. A fiducial node named ‘F’ with a single point is needed.

It outputs the projection relative to the origin of the plane, and the projection in world coordinates.

Maybe there’s a simpler way, or a right way if it’s incorrect, I’ll be eager to learn it.

p = getNode("P")
f = getNode("F")

normal = [0.0] * 3
binormal = [0.0] * 3
tangent = [0.0] * 3
planeOrigin = p.GetOriginWorld()

# Using a plane avoids much calculation.
p.GetAxesWorld(normal, binormal, tangent)

t = vtk.vtkTransform()
m = t.GetMatrix()
for r in range(3):
    m.SetElement(r, 0, normal[r])
    m.SetElement(r, 1, binormal[r])
    m.SetElement(r, 2, tangent[r])
    m.SetElement(r, 3, planeOrigin[r])
    
t.Inverse()

# Go to origin with the inverse matrix.
f.ApplyTransform(t)

point = [0.0] * 3
f.GetNthControlPointPositionWorld(0, point)

# Project on the plane at origin.
point = [point[0], point[1], 0.0]
f.SetNthControlPointPositionWorld(0, point)
print(point, "Relative to the plane's origin.")

# Go back to the plane.
t.Inverse()
f.ApplyTransform(t)
f.GetNthControlPointPositionWorld(0, point)
print(point, "World coordinates")
1 Like