Beispiel #1
0
    def getPatch(self, inputModel, inputCurve):
        # Clip model with curve
        loop = vtk.vtkSelectPolyData()
        loop.SetLoop(inputCurve)
        loop.GenerateSelectionScalarsOn()
        loop.SetInputData(inputModel)
        loop.SetSelectionModeToLargestRegion()
        clip = vtk.vtkClipPolyData()
        clip.InsideOutOn()
        clip.SetInputConnection(loop.GetOutputPort())
        clip.GenerateClippedOutputOn()
        clip.Update()
        extractLargestPart = False
        clippedOutput = clip.GetOutput(
        ) if extractLargestPart else clip.GetClippedOutput()

        connectivity = vtk.vtkConnectivityFilter()
        connectivity.SetInputData(clippedOutput)
        connectivity.Update()
        clippedOutput2 = connectivity.GetOutput()

        # Remove unused points
        cleaner = vtk.vtkCleanPolyData()
        cleaner.SetInputData(clippedOutput2)
        cleaner.Update()
        return cleaner.GetOutput()
Beispiel #2
0
 def __init__(self, module_manager):
     SimpleVTKClassModuleBase.__init__(
         self, module_manager,
         vtk.vtkSelectPolyData(), 'Processing.',
         ('vtkPolyData',), ('vtkPolyData', 'vtkPolyData', 'vtkPolyData'),
         replaceDoc=True,
         inputFunctions=None, outputFunctions=None)
Beispiel #3
0
def LeftButtonRelease(obj, ev):

    global pickedPoint
    pickedPoint = -1

    #Select Polydata
    loop = vtk.vtkSelectPolyData()
    loop.SetInputData(target)
    loop.SetLoop(landmark)
    loop.GenerateSelectionScalarsOn()
    # loop.SetSelectionModeToLargestRegion()
    loop.SetSelectionModeToSmallestRegion()
    loop.Update()

    outputScalar = loop.GetOutput().GetPointData().GetScalars()

    #Add
    if outputScalar == None:

        for i in range(landmark.GetNumberOfPoints()):
            print(landmark.GetPoint(i), end=" ")
        print()
        # return

    target.GetPointData().SetScalars(outputScalar)
    target.GetPointData().Modified()

    targetActor.GetMapper().SetScalarRange(outputScalar.GetRange())
    def ScalarsCallback(self, obj):

        rep = vtk.vtkOrientedGlyphContourRepresentation.SafeDownCast(
            self.ContourWidget.GetRepresentation())

        pointIds = vtk.vtkIdList()
        self.Interpolator.GetContourPointIds(rep, pointIds)

        points = vtk.vtkPoints()
        points.SetNumberOfPoints(pointIds.GetNumberOfIds())

        for i in range(pointIds.GetNumberOfIds()):
            pointId = pointIds.GetId(i)
            point = self.Surface.GetPoint(pointId)
            points.SetPoint(i, point)

        selectionFilter = vtk.vtkSelectPolyData()
        selectionFilter.SetInputData(self.Surface)
        selectionFilter.SetLoop(points)
        selectionFilter.GenerateSelectionScalarsOn()
        selectionFilter.SetSelectionModeToSmallestRegion()
        selectionFilter.Update()

        selectionScalars = selectionFilter.GetOutput().GetPointData(
        ).GetScalars()

        contourScalars = self.Surface.GetPointData().GetArray(
            self.ContourScalarsArrayName)

        for i in range(contourScalars.GetNumberOfTuples()):
            selectionValue = selectionScalars.GetTuple1(i)
            if self.Binary:
                if selectionValue < 0.0:
                    contourScalars.SetTuple1(i, self.InsideValue)
            else:
                contourValue = contourScalars.GetTuple1(i)
                if (not contourValue < 0.0 and selectionValue < 0.0) or (
                        contourValue < 0.0 and selectionValue < contourValue):
                    contourScalars.SetTuple1(i, selectionValue)

        self.Actor.GetMapper().SetScalarRange(contourScalars.GetRange(0))

        self.Surface.Modified()

        self.ContourWidget.Initialize()
    def ScalarsCallback(self, obj):

        rep = vtk.vtkOrientedGlyphContourRepresentation.SafeDownCast(self.ContourWidget.GetRepresentation())

        pointIds = vtk.vtkIdList()
        self.Interpolator.GetContourPointIds(rep,pointIds)

        points = vtk.vtkPoints()
        points.SetNumberOfPoints(pointIds.GetNumberOfIds())

        for i in range(pointIds.GetNumberOfIds()):
            pointId = pointIds.GetId(i)
            point = self.Surface.GetPoint(pointId)
            points.SetPoint(i,point)

        selectionFilter = vtk.vtkSelectPolyData()
        selectionFilter.SetInputData(self.Surface)
        selectionFilter.SetLoop(points)
        selectionFilter.GenerateSelectionScalarsOn()
        selectionFilter.SetSelectionModeToSmallestRegion()
        selectionFilter.Update()

        selectionScalars = selectionFilter.GetOutput().GetPointData().GetScalars()

        contourScalars = self.Surface.GetPointData().GetArray(self.ContourScalarsArrayName)

        for i in range(contourScalars.GetNumberOfTuples()):
            selectionValue = selectionScalars.GetTuple1(i)
            if self.Binary:
                if selectionValue < 0.0:
                    contourScalars.SetTuple1(i,self.InsideValue)
            else:
                contourValue = contourScalars.GetTuple1(i)
                if (not contourValue < 0.0 and selectionValue < 0.0) or (contourValue < 0.0 and selectionValue < contourValue):
                    contourScalars.SetTuple1(i,selectionValue)

        self.Actor.GetMapper().SetScalarRange(contourScalars.GetRange(0))

        self.Surface.Modified()

        self.ContourWidget.Initialize()
Beispiel #6
0
selectionPoints.InsertPoint(20, 0.263789, 0.174982, 0.387308)
selectionPoints.InsertPoint(21, 0.213034, 0.175485, 0.417142)
selectionPoints.InsertPoint(22, 0.169113, 0.261974, 0.390286)
selectionPoints.InsertPoint(23, 0.102552, 0.25997, 0.414814)
selectionPoints.InsertPoint(24, 0.131512, 0.161254, 0.454705)
selectionPoints.InsertPoint(25, 0.000192443, 0.156264, 0.475307)
selectionPoints.InsertPoint(26, -0.0392091, 0.000251724, 0.499943)
selectionPoints.InsertPoint(27, -0.096161, 0.159646, 0.46438)

sphere = vtk.vtkSphereSource()
sphere.SetPhiResolution(50)
sphere.SetThetaResolution(100)
sphere.SetStartPhi(0)
sphere.SetEndPhi(90)

loop = vtk.vtkSelectPolyData()
loop.SetInputConnection(sphere.GetOutputPort())
loop.SetLoop(selectionPoints)
loop.GenerateSelectionScalarsOn()
# negative scalars inside
loop.SetSelectionModeToSmallestRegion()

# clips out positive region
clip = vtk.vtkClipPolyData()
clip.SetInputConnection(loop.GetOutputPort())

clipMapper = vtk.vtkPolyDataMapper()
clipMapper.SetInputConnection(clip.GetOutputPort())

clipActor = vtk.vtkLODActor()
clipActor.SetMapper(clipMapper)
Beispiel #7
0
    def extractSurfaceByBoundary(self):
        """
    Update model to enclose all points in the input markup list
    """

        inputMarkup = self.surfaceBoundary.controlPointsMarkupNode
        numberOfPoints = self.getNumberOfControlPoints(inputMarkup)
        fullLeafletPolydata = self.getLeafletPolydata()
        if not fullLeafletPolydata or fullLeafletPolydata.GetNumberOfCells(
        ) == 0 or not inputMarkup or numberOfPoints < 3:
            # empty input mesh
            if self.surfaceModelNode.GetPolyData(
            ) and self.surfaceModelNode.GetPolyData().GetNumberOfCells() > 0:
                # existing mesh is not empty
                emptyPolyData = vtk.vtkPolyData()
                self.surfaceModelNode.SetAndObservePolyData(emptyPolyData)
            return None

        selectionPoints = vtk.vtkPoints()
        new_coord = [0.0, 0.0, 0.0]
        for i in range(numberOfPoints):
            inputMarkup.GetNthFiducialPosition(i, new_coord)
            selectionPoints.InsertPoint(i, new_coord)

        loop = vtk.vtkSelectPolyData()
        loop.SetLoop(selectionPoints)
        loop.GenerateSelectionScalarsOn()
        loop.SetInputData(fullLeafletPolydata)

        # loop.SetSelectionModeToClosestPointRegion() does not seem to work
        # (always extracts the same side of the mesh, regardless of what
        # point is set in loop.SetClosestPoint(...)
        # Therefore, we extract both sides of the surface and choose the one
        # that is closer to the requested closestPoint.
        closestPoint = None
        valvePlaneNormal = self.getValvePlaneNormal()
        valvePlanePosition = self.getValvePlanePosition()
        if valvePlaneNormal is not None and valvePlanePosition is not None:
            # we go 30mm opposite to the valve plane normal direction from the center of the plane
            # to make sure we pick a point on the inflow side
            closestPoint = valvePlanePosition - valvePlaneNormal * 30.0

        loop.SetSelectionModeToLargestRegion()

        clip = vtk.vtkClipPolyData()
        clip.InsideOutOn()
        clip.SetInputConnection(loop.GetOutputPort())
        clip.GenerateClippedOutputOn()
        # attributes = vtk.vtkDataSetAttributes()
        # attributes.SetInputConnection(clip.GetOutputPort())
        # attributes.CopyScalarsOff()

        clip.Update()
        clippedOutput = clip.GetOutput()

        if closestPoint is not None:
            clippedOutputs = [clip.GetOutput(), clip.GetClippedOutput()]
            closestDistances = []

            for part in clippedOutputs:
                cleaner = vtk.vtkCleanPolyData()  # removeUnusedPoints
                cleaner.SetInputData(part)
                cleaner.Update()
                cleanedPart = cleaner.GetOutput()
                loc = vtk.vtkPointLocator()
                loc.SetDataSet(cleanedPart)
                foundClosestPointId = loc.FindClosestPoint(closestPoint)
                if foundClosestPointId >= 0 and foundClosestPointId < cleanedPart.GetPoints(
                ).GetNumberOfPoints():
                    nearestPointOnLeafletSurface = np.array(
                        cleanedPart.GetPoints().GetPoint(foundClosestPointId))
                    closestDistances.append(
                        np.linalg.norm(nearestPointOnLeafletSurface -
                                       closestPoint))
                else:
                    # empty mesh, use some large number to not find it as the closest distance
                    closestDistances.append(1e6)

            if self.getSelectLargestRegion():
                # inflow side
                minimumClosestDistanceIndex = closestDistances.index(
                    min(closestDistances))
            else:
                # outflow side
                minimumClosestDistanceIndex = closestDistances.index(
                    max(closestDistances))
            clippedOutput = clippedOutputs[minimumClosestDistanceIndex]

        else:

            if self.getSelectLargestRegion():
                clippedOutput = clip.GetClippedOutput()
            else:
                clippedOutput = clip.GetOutput()

        #print("Result: {0} points, {1} cells".format(clippedOutput.GetNumberOfPoints(), clippedOutput.GetNumberOfCells()))
        self.surfaceModelNode.SetAndObservePolyData(clippedOutput)
        if self.surfaceModelNode.GetDisplayNode():
            self.surfaceModelNode.GetDisplayNode().SetActiveScalarName("")
Beispiel #8
0
selectionPoints.InsertPoint(20, 0.263789, 0.174982, 0.387308)
selectionPoints.InsertPoint(21, 0.213034, 0.175485, 0.417142)
selectionPoints.InsertPoint(22, 0.169113, 0.261974, 0.390286)
selectionPoints.InsertPoint(23, 0.102552, 0.25997, 0.414814)
selectionPoints.InsertPoint(24, 0.131512, 0.161254, 0.454705)
selectionPoints.InsertPoint(25, 0.000192443, 0.156264, 0.475307)
selectionPoints.InsertPoint(26, -0.0392091, 0.000251724, 0.499943)
selectionPoints.InsertPoint(27, -0.096161, 0.159646, 0.46438)

sphere = vtk.vtkSphereSource()
sphere.SetPhiResolution(50)
sphere.SetThetaResolution(100)
sphere.SetStartPhi(0)
sphere.SetEndPhi(90)

loop = vtk.vtkSelectPolyData()
loop.SetInputConnection(sphere.GetOutputPort())
loop.SetLoop(selectionPoints)
loop.GenerateSelectionScalarsOn()
# negative scalars inside
loop.SetSelectionModeToSmallestRegion()

# clips out positive region
clip = vtk.vtkClipPolyData()
clip.SetInputConnection(loop.GetOutputPort())

clipMapper = vtk.vtkPolyDataMapper()
clipMapper.SetInputConnection(clip.GetOutputPort())

clipActor = vtk.vtkLODActor()
clipActor.SetMapper(clipMapper)
Beispiel #9
0
       [ 3.4850955 ,  1.784371  , -0.3970445 ],
       [ 3.6015983 ,  1.8645154 , -0.41358784],
       [ 3.731929  ,  1.9395119 , -0.43495524],
       [ 3.8700097 ,  2.0061252 , -0.4594413 ],
       [ 4.0097623 ,  2.0611203 , -0.48534057],
       [ 4.145108  ,  2.1012616 , -0.5109477 ],
       [ 4.2699695 ,  2.1233141 , -0.5345572 ],
       [ 4.3782682 ,  2.1240425 , -0.55446374],
       [ 4.463926  ,  2.1002119 , -0.5689619 ],
       [ 4.5208645 ,  2.0485868 , -0.5763462 ]]

loopPoints = vtk.vtkPoints()
for xyz in loopPointPositions:
    loopPoints.InsertNextPoint(xyz)

selectionFilter = vtk.vtkSelectPolyData()
selectionFilter.SetInputConnection(reader.GetOutputPort())
selectionFilter.SetLoop(loopPoints)
selectionFilter.GenerateSelectionScalarsOn()
selectionFilter.SetSelectionModeToSmallestRegion()

# Run selection filter using greedy method (expected to fail)

# Add error observer to catch the expected error (uncaught error would make the test fail)
cb = callback()
cb.CallDataType = vtk.VTK_STRING
observerId = selectionFilter.AddObserver(vtk.vtkCommand.ErrorEvent, cb)
# Run the computation
selectionFilter.SetEdgeSearchModeToGreedy()
selectionFilter.Update()
# Remove the error observer
Beispiel #10
0
def main():  # pragma: no cover
    sphere_source = vtk.vtkSphereSource()
    sphere_source.Update()

    selection_points = vtk.vtkPoints()

    selection_points.InsertPoint(0, -0.16553, 0.135971, 0.451972)
    selection_points.InsertPoint(1, -0.0880123, -0.134952, 0.4747)
    selection_points.InsertPoint(2,  0.00292618, -0.134604, 0.482459)
    selection_points.InsertPoint(3, 0.0641941, 0.067112, 0.490947)
    selection_points.InsertPoint(4, 0.15577, 0.0734765, 0.469245)
    selection_points.InsertPoint(5, 0.166667, -0.129217, 0.454622)
    selection_points.InsertPoint(6, 0.241259, -0.123363, 0.420581)
    selection_points.InsertPoint(7,  0.240334, 0.0727106, 0.432555)
    selection_points.InsertPoint(8, 0.308529, 0.0844311, 0.384357)
    selection_points.InsertPoint(9, 0.32672, -0.121674, 0.359187)
    selection_points.InsertPoint(10, 0.380721, -0.117342, 0.302527)
    selection_points.InsertPoint(11, 0.387804, 0.0455074, 0.312375)
    selection_points.InsertPoint(12, 0.43943, -0.111673, 0.211707)
    selection_points.InsertPoint(13, 0.470984, -0.0801913, 0.147919)
    selection_points.InsertPoint(14, 0.436777, 0.0688872, 0.233021)
    selection_points.InsertPoint(15, 0.44874, 0.188852, 0.109882)
    selection_points.InsertPoint(16, 0.391352, 0.254285, 0.176943)
    selection_points.InsertPoint(17, 0.373274, 0.154162, 0.294296)
    selection_points.InsertPoint(18, 0.274659, 0.311654, 0.276609)
    selection_points.InsertPoint(19, 0.206068, 0.31396, 0.329702)
    selection_points.InsertPoint(20, 0.263789, 0.174982, 0.387308)
    selection_points.InsertPoint(21, 0.213034, 0.175485, 0.417142)
    selection_points.InsertPoint(22, 0.169113, 0.261974, 0.390286)
    selection_points.InsertPoint(23, 0.102552, 0.25997, 0.414814)
    selection_points.InsertPoint(24, 0.131512, 0.161254, 0.454705)
    selection_points.InsertPoint(25, 0.000192443, 0.156264, 0.475307)
    selection_points.InsertPoint(26, -0.0392091, 0.000251724, 0.499943)
    selection_points.InsertPoint(27, -0.096161, 0.159646, 0.46438)

    loop = vtk.vtkSelectPolyData()
    loop.SetInputConnection(sphere_source.GetOutputPort())
    loop.SetLoop(selection_points)
    loop.GenerateSelectionScalarsOn()
    loop.SetSelectionModeToSmallestRegion() # negative scalars inside

    clip = vtk.vtkClipPolyData() # clips out positive region

    clip.SetInputConnection(loop.GetOutputPort())

    clip_mapper = vtk.vtkPolyDataMapper()
    clip_mapper.SetInputConnection(clip.GetOutputPort())

    clip_actor = vtk.vtkLODActor()
    clip_actor.SetMapper(clip_mapper)

    renderer = vtk.vtkRenderer()

    render_window = vtk.vtkRenderWindow()
    render_window.AddRenderer(renderer)

    interactor = vtk.vtkRenderWindowInteractor()
    interactor.SetRenderWindow(render_window)

    # Add the actors to the renderer, set the background and size
    renderer.AddActor(clip_actor)
    renderer.SetBackground(.1, .2, .4)

    render_window.SetSize(500, 250)

    render_window.Render()
    interactor.Start()
Beispiel #11
0
def drawingMakeROI(obj, ev):

    if obj.inDrawMode == 1:
        print('closing and filling ROI')
        
        # get the required surface
        currPolyData = obj.GetPicker().GetActor().GetMapper().GetInput()
        
        #  ... and points
        currPoints = obj.pickedPoints

        # we're not actually going to change the original: we're going to clip out the region we want and 
        #  show it as a separate surface "patch" - this allows easy control of transparency/colour etc
        
        # a vtk selection tool that loops around our points
        if debug: print "before select polydata"
        selecta = vtk.vtkSelectPolyData()
        selecta.SetInputData(currPolyData)
        selecta.SetLoop(currPoints)
        selecta.GenerateSelectionScalarsOn()
        ## selecta.SetSelectionModeToSmallestRegion()
        selecta.SetSelectionModeToLargestRegion()
        selecta.Update()
        if debug: print "after select polydata"


        ## TODO - consider a different clipping function
        ## the one below appears to clip front AND back surfaces?
        #loop = vtk.vtkImplicitSelectionLoop()
        #loop.SetLoop(currPoints)
        #clipROI = vtk.vtkExtractPolyDataGeometry()
        #clipROI.SetInputData(currPolyData)
        #clipROI.SetImplicitFunction(loop)
        #clipROI.ExtractBoundaryCellsOn()
        #clipROI.PassPointsOff()
        #clipROI.Update()


        # the tool that actually clips out the selected region
        clipROI = vtk.vtkClipPolyData()
        clipROI.SetInputConnection(selecta.GetOutputPort())
        clipROI.Update()
        

        if debug: print(clipROI.GetOutput())

        # --- try to get an ROI
        if clipROI.GetOutput().GetNumberOfPoints() == 0:
            #loop could not clip data for some reason - we need to clean up
            obj.pickedPoints = vtk.vtkPoints() #new
            #stop drawing mode and reset the interactor to normal behaviour
            obj.inDrawMode = 0
            
            print 'Error while trying to close the ROI loop - resetting colours - please try again.'
            print obj.ScalarsCopyForRevert

            tmpScalarsCopyForRevert = vtk.vtkUnsignedCharArray()
            tmpScalarsCopyForRevert.DeepCopy(obj.ScalarsCopyForRevert)

            obj.curr_polydata.GetPointData().SetScalars(obj.ScalarsCopyForRevert)
            obj.curr_polydata.Modified()                            

            obj.ScalarsCopyForRevert = tmpScalarsCopyForRevert; #AND LOCK IN CURRENT STATE

            obj.curr_smoother.Update()
            obj.curr_mapper.SetColorModeToDefault()
            obj.curr_mapper.Modified()

            style = vtk.vtkInteractorStyleTrackballCamera()
            obj.SetInteractorStyle(style)

            obj.ren.Render()
            obj.Render()

            obj.parent_ui.statusbar.showMessage("Failed to close and fill ROI! Perhaps the surface is too spikey? - try smoothing.")

        else:

            ## -- track the point IDs
            # unfortunately the clip tool does not keep a reference of the original surface
            # point IDs (that we need to send bakc to other programs later
            #  -- we need a pretty nasty bit or code to track the xyz positions extracted 
            #     so that we can go back to the original surface and label these


            # first, get the xyz position of all points in original surface
            origDat = currPolyData.GetPoints().GetData()
            origArr = []
            
            for i in range(currPolyData.GetNumberOfPoints()):        
                origArr.append(origDat.GetTuple(i))


            # next get the xyz position of select points
            selectedDat = clipROI.GetOutput().GetPoints().GetData()
            selectedArr = [] #place holder for loop

            if debug: print(selectedDat)
            
            for i in range(clipROI.GetOutput().GetNumberOfPoints()):        
                selectedArr.append(selectedDat.GetTuple(i)) #xyz

            ## now find matching indices in original using numpy manipulation
            numpyOrigArr = array(origArr)
            numpySelectedArr = array(selectedArr)

            #if debug:
            #    numpyOrigArr.tofile('orig.txt',',')
            #    numpySelectedArr.tofile('new.txt',',')

            # TODO - we only check the first 6 characters of the coordinate data - this may cause precision issues later - check
            origAll = char.array(numpyOrigArr[:,0],'S6') + '-' + char.array(numpyOrigArr[:,1],'S6') + '-' + char.array(numpyOrigArr[:,2],'S6')
            selectedAll = char.array(numpySelectedArr[:,0],'S6') + '-' + char.array(numpySelectedArr[:,1],'S6') + '-' + char.array(numpySelectedArr[:,2],'S6')

            # and pipe it back to the original
            obj.filledROIPoints = where(in1d(origAll, selectedAll))[0]
            obj.ROI_ready = 1

            if debug: print(obj.filledROIPoints)
            if debug: print(size(obj.filledROIPoints))


            ## -- back to drawing
            # draw the new surface
            roiMapper = vtk.vtkPolyDataMapper()
            roiMapper.SetInputConnection(clipROI.GetOutputPort())
            roiMapper.ScalarVisibilityOff()
            roiMapper.Update()
            
            roiActor = vtk.vtkActor()
            roiActor.SetVisibility(1)
            roiActor.SetMapper(roiMapper)
            roiActor.GetProperty().SetOpacity(0.5)
            roiActor.GetProperty().SetColor(1.0,1.0,1.0)

            obj.roiActor = roiActor;
            obj.ren.AddActor(obj.roiActor)
            obj.Render()

            #stop drawing mode and reset hte interactor to normal behaviour
            obj.inDrawMode = 0

            #remove our black dots
            tmpScalarsCopyForRevert = vtk.vtkUnsignedCharArray()
            tmpScalarsCopyForRevert.DeepCopy(obj.ScalarsCopyForRevert)

            obj.curr_polydata.GetPointData().SetScalars(obj.ScalarsCopyForRevert)
            obj.curr_polydata.Modified()                            

            obj.ScalarsCopyForRevert = tmpScalarsCopyForRevert; #AND LOCK IN CURRENT STATE

            obj.curr_smoother.Update()
            obj.curr_mapper.SetColorModeToDefault()
            obj.curr_mapper.Modified()

            style = vtk.vtkInteractorStyleTrackballCamera()
            obj.SetInteractorStyle(style)

            # and clean up for the next loop? TODO? - or button to reset?
            obj.pickedPoints = vtk.vtkPoints() #new

    else:
        if debug: print('ignored right mouse click - not in draw mode')
        pass