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.

can you give me a script about creating a plane through point N and perpendicular to P1,thank you very much.

The plane node has a mode where you can define the plane with a point and a normal, see PlaneTypePointNormal
https://apidocs.slicer.org/main/classvtkMRMLMarkupsPlaneNode.html#af83a7dac83b04668a4f2bae10245a7baa5bce04cda74677cd6404657aa0868783

I’m really sorry, but I can’t understand that code and I’m not sure which part of the text I should copy. Could you please provide me with the specific text that I can copy directly? Thank you so much!

I asked Copilot this question:
“Write me a brief code snippet that creates a vtkMRMLMarkupsPlaneNode from a point and a normal vector using the PlaneTypePointNormal mode.”

It gave me the answer in a few seconds. It looks correct:

import slicer
import numpy as np

# Define the point and normal vector
point = np.array([10.0, 20.0, 30.0])
normal = np.array([0.0, 0.0, 1.0])

# Create a new vtkMRMLMarkupsPlaneNode
planeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLMarkupsPlaneNode')
planeNode.SetName('PlaneNode')

# Set the plane type to PlaneTypePointNormal
planeNode.SetPlaneType(slicer.vtkMRMLMarkupsPlaneNode.PlaneTypePointNormal)

# Set the point and normal vector
planeNode.SetOrigin(point)
planeNode.SetNormal(normal)
1 Like

thank you very much :rose: