How to creat a plane according 2 points

Dear friends

I know there are many ways in slicer to creat a plane

Then how to use two points to create a plane that passes through the middle point of these two points and is perpendicular to the line formed by the two points?

any way or script will be useful!

thankyou!

It should be doable with vtkMath::Perpendiculars() with a direction vector relative to the middle point.

Calculate

  • the middle point,
  • a direction vector with either point relative to the middle point,
  • the perpendiculars at the middle point using the direction vector.

By adding the perpendiculars to the middle point, you have the 3 points of a plane.

I would really appreciate it if you could help me write this slicer script

This should work when pasted in the Python console.

Add a fiducial node named ‘F’ with 2 points, and a plane named ‘P’ before.

def findMiddleCrossSectionPlane(p1, p2):
    middlePoint = ((p1[0] + p2[0]) / 2.0, (p1[1] + p2[1]) / 2.0, (p1[2] + p2[2]) / 2.0)
    directionVector = [0.0, 0.0, 0.0]
    vtk.vtkMath().Subtract(p2, middlePoint, directionVector)
    
    perpendicular1 = [0.0, 0.0, 0.0]
    perpendicular2 = [0.0, 0.0, 0.0]
    vtk.vtkMath().Perpendiculars(directionVector, perpendicular1, perpendicular2, 0.0)
    
    planePoint1 = [0.0, 0.0, 0.0]
    planePoint2 = [0.0, 0.0, 0.0]
    vtk.vtkMath().Add(middlePoint, perpendicular1, planePoint1)
    vtk.vtkMath().Add(middlePoint, perpendicular2, planePoint2)
    
    return (middlePoint, planePoint1, planePoint2)
    

# Add a fiducial node named 'F', and a plane named 'P'
f = slicer.util.getNode("F")
p1 = [0.0, 0.0, 0.0]
p2 = [0.0, 0.0, 0.0]
f.GetNthControlPointPositionWorld(0, p1)
f.GetNthControlPointPositionWorld(1, p2)

planePoints = findMiddleCrossSectionPlane(p1, p2)

# Just for visualisation.
plane = slicer.util.getNode("P")
plane.SetPlaneType(slicer.vtkMRMLMarkupsPlaneNode.PlaneType3Points)
plane.SetNthControlPointPositionWorld(0, planePoints[0])
plane.SetNthControlPointPositionWorld(1, planePoints[1])
plane.SetNthControlPointPositionWorld(2, planePoints[2])

thankyou very much! it works well!

Sorry to disturbe you,Mr!

In the last version slicer,the script does not work.

It shows the errors.

Would you like to help solve the problem?

Hi, I checked with 5.3.0-2023-07-23 r31895 / 98f1a5a on Linux and the script does work.

The error messages mean that the plane P is found but does not have any control point. I could reproduce the errors by adding a plane without any control point to the scene. Use one that you can see and interact with normally.