184 if not self.scriptedEffect.confirmCurrentSegmentVisible():
187 import vtkSegmentationCorePython
as vtkSegmentationCore
189 self.scriptedEffect.saveStateForUndo()
193 operation = self.scriptedEffect.parameter(
"Operation")
194 bypassMasking = (self.scriptedEffect.integerParameter(
"BypassMasking") != 0)
196 selectedSegmentID = self.scriptedEffect.parameterSetNode().GetSelectedSegmentID()
198 segmentationNode = self.scriptedEffect.parameterSetNode().GetSegmentationNode()
199 segmentation = segmentationNode.GetSegmentation()
205 if not modifierSegmentID:
206 logging.error(f
"Operation {operation} requires a selected modifier segment")
208 modifierSegment = segmentation.GetSegment(modifierSegmentID)
209 modifierSegmentLabelmap = slicer.vtkOrientedImageData()
210 segmentationNode.GetBinaryLabelmapRepresentation(modifierSegmentID, modifierSegmentLabelmap)
213 commonGeometryString = segmentationNode.GetSegmentation().DetermineCommonLabelmapGeometry(
214 vtkSegmentationCore.vtkSegmentation.EXTENT_UNION_OF_SEGMENTS,
None)
215 if not commonGeometryString:
216 logging.info(
"Logical operation skipped: all segments are empty")
218 commonGeometryImage = slicer.vtkOrientedImageData()
219 vtkSegmentationCore.vtkSegmentationConverter.DeserializeImageGeometry(commonGeometryString, commonGeometryImage,
False)
223 if not vtkSegmentationCore.vtkOrientedImageDataResample.DoGeometriesMatch(commonGeometryImage, modifierSegmentLabelmap):
224 modifierSegmentLabelmap_CommonGeometry = slicer.vtkOrientedImageData()
225 vtkSegmentationCore.vtkOrientedImageDataResample.ResampleOrientedImageToReferenceOrientedImage(
226 modifierSegmentLabelmap, commonGeometryImage, modifierSegmentLabelmap_CommonGeometry,
230 modifierSegmentLabelmap = modifierSegmentLabelmap_CommonGeometry
232 if operation == LOGICAL_COPY:
233 self.scriptedEffect.modifySelectedSegmentByLabelmap(
234 modifierSegmentLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeSet, bypassMasking)
235 elif operation == LOGICAL_UNION:
236 self.scriptedEffect.modifySelectedSegmentByLabelmap(
237 modifierSegmentLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeAdd, bypassMasking)
238 elif operation == LOGICAL_SUBTRACT:
239 self.scriptedEffect.modifySelectedSegmentByLabelmap(
240 modifierSegmentLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeRemove, bypassMasking)
241 elif operation == LOGICAL_INTERSECT:
242 selectedSegmentLabelmap = self.scriptedEffect.selectedSegmentLabelmap()
243 intersectionLabelmap = slicer.vtkOrientedImageData()
244 vtkSegmentationCore.vtkOrientedImageDataResample.MergeImage(
245 selectedSegmentLabelmap, modifierSegmentLabelmap, intersectionLabelmap,
246 vtkSegmentationCore.vtkOrientedImageDataResample.OPERATION_MINIMUM, selectedSegmentLabelmap.GetExtent())
247 selectedSegmentLabelmapExtent = selectedSegmentLabelmap.GetExtent()
248 modifierSegmentLabelmapExtent = modifierSegmentLabelmap.GetExtent()
249 commonExtent = [max(selectedSegmentLabelmapExtent[0], modifierSegmentLabelmapExtent[0]),
250 min(selectedSegmentLabelmapExtent[1], modifierSegmentLabelmapExtent[1]),
251 max(selectedSegmentLabelmapExtent[2], modifierSegmentLabelmapExtent[2]),
252 min(selectedSegmentLabelmapExtent[3], modifierSegmentLabelmapExtent[3]),
253 max(selectedSegmentLabelmapExtent[4], modifierSegmentLabelmapExtent[4]),
254 min(selectedSegmentLabelmapExtent[5], modifierSegmentLabelmapExtent[5])]
255 self.scriptedEffect.modifySelectedSegmentByLabelmap(
256 intersectionLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeSet, commonExtent, bypassMasking)
258 elif operation == LOGICAL_INVERT:
259 selectedSegmentLabelmap = self.scriptedEffect.selectedSegmentLabelmap()
261 self.scriptedEffect.modifySelectedSegmentByLabelmap(
262 invertedSelectedSegmentLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeSet, bypassMasking)
264 elif operation == LOGICAL_CLEAR
or operation == LOGICAL_FILL:
265 selectedSegmentLabelmap = self.scriptedEffect.selectedSegmentLabelmap()
266 vtkSegmentationCore.vtkOrientedImageDataResample.FillImage(selectedSegmentLabelmap, 1
if operation == LOGICAL_FILL
else 0, selectedSegmentLabelmap.GetExtent())
267 self.scriptedEffect.modifySelectedSegmentByLabelmap(
268 selectedSegmentLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeSet, bypassMasking)
271 logging.error(f
"Unknown operation: {operation}")