Hello All,
I’m trying to use vtkClipPolyData()
to split a model into two separate segments within a scripted segment editor effect. The operation itself works, but the resulting models are hollow shells (open surfaces) rather than closed, solid segments. I need these segments to be closed surfaces after clipping.
Here is my current code snippet:
def cutSurfaceWithModel(self, segmentMarkupNode, segmentModel):
parameterSetNode = self.scriptedEffect.parameterSetNode()
segmentationNode = parameterSetNode.GetSegmentationNode()
if not segmentationNode:
logging.warning("No segmentation node is available.")
return
segmentID = parameterSetNode.GetSelectedSegmentID()
if not segmentID:
logging.warning("No selected segment to cut.")
return
segmentIDs = vtk.vtkStringArray()
segmentIDs.InsertNextValue(segmentID)
shNode = slicer.mrmlScene.GetSubjectHierarchyNode()
exportFolderItemId = shNode.CreateFolderItem(shNode.GetSceneItemID(), "TempExportFolder")
slicer.modules.segmentations.logic().ExportSegmentsToModels(segmentationNode, segmentIDs, exportFolderItemId)
modelItemIDs = vtk.vtkIdList()
shNode.GetItemChildren(exportFolderItemId, modelItemIDs)
if modelItemIDs.GetNumberOfIds() < 1:
logging.warning("No segment was exported to model.")
return
sourceNode = slicer.mrmlScene.GetNodeByID(shNode.GetItemDataNode(modelItemIDs.GetId(0)).GetID())
sourcePolyData = vtk.vtkPolyData()
sourcePolyData.DeepCopy(sourceNode.GetPolyData())
shNode.RemoveItem(exportFolderItemId)
segmentPolyData = segmentModel.GetPolyData()
clipFunction = vtk.vtkImplicitPolyDataDistance()
clipFunction.SetInput(segmentPolyData)
clipper = vtk.vtkClipPolyData()
clipper.SetInputData(sourcePolyData)
clipper.SetClipFunction(clipFunction)
clipper.GenerateClippedOutputOn()
clipper.InsideOutOn()
clipper.Update()
insidePolyData = vtk.vtkPolyData()
insidePolyData.DeepCopy(clipper.GetOutput())
outsidePolyData = vtk.vtkPolyData()
outsidePolyData.DeepCopy(clipper.GetClippedOutput())
segmentationNode.AddSegmentFromClosedSurfaceRepresentation(insidePolyData, "TPSCut_Inside")
segmentationNode.AddSegmentFromClosedSurfaceRepresentation(outsidePolyData, "TPSCut_Outside")