def clipping(self):
     planeCollection = vtk.vtkPlaneCollection()
     harden = slicer.vtkSlicerTransformLogic()
     tempTransform = slicer.vtkMRMLLinearTransformNode()
     tempTransform.HideFromEditorsOn()
     slicer.mrmlScene.AddNode(tempTransform)
     numNodes = slicer.mrmlScene.GetNumberOfNodesByClass("vtkMRMLModelNode")
     dictionnaryModel = dict()
     hardenModelIDdict = dict()
     landmarkDescriptionDict = dict()
     modelIDdict = dict()
     for i in range(3, numNodes):
         planeCollection.RemoveAllItems()
         mh = slicer.mrmlScene.GetNthNodeByClass(i, "vtkMRMLModelNode")
         if mh.GetDisplayVisibility() == 0:
             continue
         model = slicer.util.getNode(mh.GetName())
         transform = model.GetParentTransformNode()
         if transform:
             tempTransform.Copy(transform)
             harden.hardenTransform(tempTransform)
             m = vtk.vtkMatrix4x4()
             tempTransform.GetMatrixTransformToParent(m)
             m.Invert(m, m)
         else:
             m = vtk.vtkMatrix4x4()
         for key, planeDef in self.planeDict.iteritems():
             hardenP = m.MultiplyPoint(planeDef.P)
             hardenN = m.MultiplyPoint(planeDef.n)
             if planeDef.boxState:
                 planeDef.vtkPlane.SetOrigin(hardenP[0], hardenP[1], hardenP[2])
                 if planeDef.negState:
                     planeDef.vtkPlane.SetNormal(-hardenN[0], -hardenN[1], -hardenN[2])
                 if planeDef.posState:
                     planeDef.vtkPlane.SetNormal(hardenN[0], hardenN[1], hardenN[2])
                 planeCollection.AddItem(planeDef.vtkPlane)
         dictionnaryModel[model.GetID()]= model.GetPolyData()
         polyData = model.GetPolyData()
         clipper = vtk.vtkClipClosedSurface()
         clipper.SetClippingPlanes(planeCollection)
         clipper.SetInputData(polyData)
         clipper.SetGenerateFaces(1)
         clipper.SetScalarModeToLabels()
         clipper.Update()
         polyDataNew = clipper.GetOutput()
         model.SetAndObservePolyData(polyDataNew)
         # Checking if one ore more fiducial list are connected to this model
         list = slicer.mrmlScene.GetNodesByClass("vtkMRMLMarkupsFiducialNode")
         end = list.GetNumberOfItems()
         for i in range(0,end):
             fidList = list.GetItemAsObject(i)
             if fidList.GetAttribute("connectedModelID"):
                 if fidList.GetAttribute("connectedModelID") == model.GetID():
                     modelIDdict[fidList.GetID()], hardenModelIDdict[fidList.GetID()], landmarkDescriptionDict[fidList.GetID()] = \
                         self.unprojectLandmarks(fidList)
     return dictionnaryModel, modelIDdict, hardenModelIDdict, landmarkDescriptionDict
Exemple #2
0
 def clipping(self):
     planeCollection = vtk.vtkPlaneCollection()
     harden = slicer.vtkSlicerTransformLogic()
     tempTransform = slicer.vtkMRMLLinearTransformNode()
     tempTransform.HideFromEditorsOn()
     slicer.mrmlScene.AddNode(tempTransform)
     numNodes = slicer.mrmlScene.GetNumberOfNodesByClass("vtkMRMLModelNode")
     dictionnaryModel = dict()
     hardenModelIDdict = dict()
     landmarkDescriptionDict = dict()
     modelIDdict = dict()
     for i in range(3, numNodes):
         planeCollection.RemoveAllItems()
         mh = slicer.mrmlScene.GetNthNodeByClass(i, "vtkMRMLModelNode")
         if mh.GetDisplayVisibility() == 0:
             continue
         model = slicer.util.getNode(mh.GetName())
         transform = model.GetParentTransformNode()
         if transform:
             tempTransform.Copy(transform)
             harden.hardenTransform(tempTransform)
             m = vtk.vtkMatrix4x4()
             tempTransform.GetMatrixTransformToParent(m)
             m.Invert(m, m)
         else:
             m = vtk.vtkMatrix4x4()
         for key, planeDef in self.planeDict.iteritems():
             hardenP = m.MultiplyPoint(planeDef.P)
             hardenN = m.MultiplyPoint(planeDef.n)
             if planeDef.boxState:
                 planeDef.vtkPlane.SetOrigin(hardenP[0], hardenP[1],
                                             hardenP[2])
                 if planeDef.negState:
                     planeDef.vtkPlane.SetNormal(-hardenN[0], -hardenN[1],
                                                 -hardenN[2])
                 if planeDef.posState:
                     planeDef.vtkPlane.SetNormal(hardenN[0], hardenN[1],
                                                 hardenN[2])
                 planeCollection.AddItem(planeDef.vtkPlane)
         dictionnaryModel[model.GetID()] = model.GetPolyData()
         polyData = model.GetPolyData()
         clipper = vtk.vtkClipClosedSurface()
         clipper.SetClippingPlanes(planeCollection)
         clipper.SetInputData(polyData)
         clipper.SetGenerateFaces(1)
         clipper.SetScalarModeToLabels()
         clipper.Update()
         polyDataNew = clipper.GetOutput()
         model.SetAndObservePolyData(polyDataNew)
         # Checking if one ore more fiducial list are connected to this model
         list = slicer.mrmlScene.GetNodesByClass(
             "vtkMRMLMarkupsFiducialNode")
         end = list.GetNumberOfItems()
         for i in range(0, end):
             fidList = list.GetItemAsObject(i)
             if fidList.GetAttribute("connectedModelID"):
                 if fidList.GetAttribute(
                         "connectedModelID") == model.GetID():
                     modelIDdict[fidList.GetID()], hardenModelIDdict[fidList.GetID()], landmarkDescriptionDict[fidList.GetID()] = \
                         self.unprojectLandmarks(fidList)
     return dictionnaryModel, modelIDdict, hardenModelIDdict, landmarkDescriptionDict
    def clipping(self,
                 red_plane_boxState,
                 radio_red_NegState,
                 radio_red_PosState,
                 yellow_plane_boxState,
                 radio_yellow_NegState,
                 radio_yellow_PosState,
                 green_plane_boxState,
                 radio_green_NegState,
                 radio_green_PosState):

        # Clipping in the direction of the normal vector
        self.plane_red = vtk.vtkPlane()
        self.plane_yellow = vtk.vtkPlane()
        self.plane_green = vtk.vtkPlane()

        self.planeCollection = vtk.vtkPlaneCollection()

        #Condition for the red plane
        print self.m_Red
        print self.n_NewRedPlan

        print self.A_NewRedPlan
        self.n_NewRedPlan1 = self.n_NewRedPlan
        self.n_NewGreenPlan1 = self.n_NewGreenPlan
        self.n_NewYellowPlan1 = self.n_NewYellowPlan

        self.n_NewRedPlan1[0] = self.n_NewRedPlan[0] - self.A_NewRedPlan[0]
        self.n_NewRedPlan1[1] = self.n_NewRedPlan[1] - self.A_NewRedPlan[1]
        self.n_NewRedPlan1[2] = self.n_NewRedPlan[2] - self.A_NewRedPlan[2]
        print self.n_NewRedPlan1

        if red_plane_boxState:
            if radio_red_NegState:
                self.plane_red.SetOrigin(self.A_NewRedPlan[0], self.A_NewRedPlan[1], self.A_NewRedPlan[2])
                if self.n_NewRedPlan1[2] >= 0:
                    self.plane_red.SetNormal(-self.n_NewRedPlan1[0], -self.n_NewRedPlan1[1], -self.n_NewRedPlan1[2])
                if self.n_NewRedPlan1[2] < 0:
                    self.plane_red.SetNormal(self.n_NewRedPlan1[0], self.n_NewRedPlan1[1], self.n_NewRedPlan1[2])
                self.planeCollection.AddItem(self.plane_red)
                print self.plane_red

            if radio_red_PosState:
                self.plane_red.SetOrigin(self.A_NewRedPlan[0], self.A_NewRedPlan[1], self.A_NewRedPlan[2])
                if self.n_NewRedPlan1[2] >= 0:
                    self.plane_red.SetNormal(self.n_NewRedPlan1[0], self.n_NewRedPlan1[1], self.n_NewRedPlan1[2])
                if self.n_NewRedPlan1[2] < 0:
                    self.plane_red.SetNormal(-self.n_NewRedPlan1[0], -self.n_NewRedPlan1[1], -self.n_NewRedPlan1[2])
                self.planeCollection.AddItem(self.plane_red)
                print self.plane_red

        #Condition for the yellow plane
        print self.m_Yellow
        print self.n_NewYellowPlan

        print self.A_NewYellowPlan
        self.n_NewYellowPlan1[0] = self.n_NewYellowPlan[0] - self.A_NewYellowPlan[0]
        self.n_NewYellowPlan1[1] = self.n_NewYellowPlan[1] - self.A_NewYellowPlan[1]
        self.n_NewYellowPlan1[2] = self.n_NewYellowPlan[2] - self.A_NewYellowPlan[2]
        print self.n_NewYellowPlan1

        if yellow_plane_boxState:
            if radio_yellow_NegState:
                self.plane_yellow.SetOrigin(self.A_NewYellowPlan[0], self.A_NewYellowPlan[1], self.A_NewYellowPlan[2])
                if self.n_NewYellowPlan1[0] >= 0:
                    self.plane_yellow.SetNormal(-self.n_NewYellowPlan1[0], -self.n_NewYellowPlan1[1], -self.n_NewYellowPlan1[2])
                if self.n_NewYellowPlan1[0] < 0:
                    self.plane_yellow.SetNormal(self.n_NewYellowPlan1[0], self.n_NewYellowPlan1[1], self.n_NewYellowPlan1[2])
                self.planeCollection.AddItem(self.plane_yellow)
                print self.plane_yellow

            if radio_yellow_PosState:
                self.plane_yellow.SetOrigin(self.A_NewYellowPlan[0], self.A_NewYellowPlan[1], self.A_NewYellowPlan[2])
                if self.n_NewYellowPlan1[0] >= 0:
                    self.plane_yellow.SetNormal(self.n_NewYellowPlan1[0], self.n_NewYellowPlan1[1], self.n_NewYellowPlan1[2])
                if self.n_NewYellowPlan1[0] < 0:
                    self.plane_yellow.SetNormal(-self.n_NewYellowPlan1[0], -self.n_NewYellowPlan1[1], -self.n_NewYellowPlan1[2])
                self.planeCollection.AddItem(self.plane_yellow)
                print self.plane_yellow

        #Condition for the green plane
        print self.m_Green
        print self.n_NewGreenPlan

        print self.A_NewGreenPlan
        self.n_NewGreenPlan1[0] = self.n_NewGreenPlan[0] - self.A_NewGreenPlan[0]
        self.n_NewGreenPlan1[1] = self.n_NewGreenPlan[1] - self.A_NewGreenPlan[1]
        self.n_NewGreenPlan1[2] = self.n_NewGreenPlan[2] - self.A_NewGreenPlan[2]
        print self.n_NewGreenPlan1

        if green_plane_boxState:
            if radio_green_NegState:
                self.plane_green.SetOrigin(self.A_NewGreenPlan[0], self.A_NewGreenPlan[1], self.A_NewGreenPlan[2])
                if self.n_NewGreenPlan1[1] >= 0:
                    self.plane_green.SetNormal(-self.n_NewGreenPlan1[0], -self.n_NewGreenPlan1[1], -self.n_NewGreenPlan1[2])
                if self.n_NewGreenPlan1[1] < 0:
                    self.plane_green.SetNormal(self.n_NewGreenPlan1[0], self.n_NewGreenPlan1[1], self.n_NewGreenPlan1[2])
                self.planeCollection.AddItem(self.plane_green)
                print self.plane_green

            if radio_green_PosState:
                self.plane_green.SetOrigin(self.A_NewGreenPlan[0], self.A_NewGreenPlan[1], self.A_NewGreenPlan[2])
                if self.n_NewGreenPlan1[1] > 0:
                    self.plane_green.SetNormal(self.n_NewGreenPlan1[0], self.n_NewGreenPlan1[1], self.n_NewGreenPlan1[2])
                if self.n_NewGreenPlan1[1] < 0:
                    self.plane_green.SetNormal(-self.n_NewGreenPlan1[0], -self.n_NewGreenPlan1[1], -self.n_NewGreenPlan1[2])
                self.planeCollection.AddItem(self.plane_green)
                print self.plane_green


        numNodes = slicer.mrmlScene.GetNumberOfNodesByClass("vtkMRMLModelNode")
        self.dictionnaryModel = dict()
        self.dictionnaryModel.clear()


        for i in range(3, numNodes):
            mh = slicer.mrmlScene.GetNthNodeByClass(i, "vtkMRMLModelNode")
            self.model = slicer.util.getNode(mh.GetName())
            print mh.GetName()

            self.dictionnaryModel[self.model.GetID()]=self.model.GetPolyData()

            self.polyData = self.model.GetPolyData()


            PolyAlgorithm = vtk.vtkClipClosedSurface()
            PolyAlgorithm.SetInputData(self.polyData)

            clipper = vtk.vtkClipClosedSurface()
            clipper.SetClippingPlanes(self.planeCollection)
            clipper.SetInputConnection(PolyAlgorithm.GetOutputPort())
            clipper.SetGenerateFaces(1)
            clipper.SetScalarModeToLabels()
            clipper.Update()
            polyDataNew = clipper.GetOutput()
            self.model.SetAndObservePolyData(polyDataNew)
        return self.dictionnaryModel