def generateGridPolyDataTriangular(self): polyDataCombiner = vtk.vtkAppendPolyData() lowerHorizontalBoundMm = self.evaluateLowerHorizontalBoundMm() upperHorizontalBoundMm = self.evaluateUpperHorizontalBoundMm() lowerVerticalBoundMm = self.evaluateLowerVerticalBoundMm() upperVerticalBoundMm = self.evaluateUpperVerticalBoundMm() # traditional 'for' loops would be very nice here... :-/ yMm = lowerVerticalBoundMm while (yMm <= upperVerticalBoundMm): # determine whether to offset in horizontal axis - # this makes the triangular pattern if done on odd rows only offsetMm = 0 oddRow = (round(yMm / self.gridSpacingVerticalMm) % 2 == 1) if oddRow: offsetMm = self.gridSpacingHorizontalMm / 2.0 xMm = lowerHorizontalBoundMm + offsetMm while (xMm <= upperHorizontalBoundMm): cylinderPolyData = self.generateCylinderPolyData(xMm, yMm) polyDataCombiner.AddInputData(cylinderPolyData) xMm = xMm + self.gridSpacingHorizontalMm yMm = yMm + self.gridSpacingVerticalMm polyDataCombiner.Update() gridPolyData = polyDataCombiner.GetOutput() return gridPolyData
def generateGridPolyDataTriangular(self): polyDataCombiner = vtk.vtkAppendPolyData() lowerHorizontalBoundMm = self.evaluateLowerHorizontalBoundMm() upperHorizontalBoundMm = self.evaluateUpperHorizontalBoundMm() lowerVerticalBoundMm = self.evaluateLowerVerticalBoundMm() upperVerticalBoundMm = self.evaluateUpperVerticalBoundMm() # traditional 'for' loops would be very nice here... :-/ yMm = lowerVerticalBoundMm while (yMm <= upperVerticalBoundMm): # determine whether to offset in horizontal axis - # this makes the triangular pattern if done on odd rows only offsetMm = 0 oddRow = (round(yMm / self.gridSpacingVerticalMm) % 2 == 1) if oddRow: offsetMm = self.gridSpacingHorizontalMm / 2.0 xMm = lowerHorizontalBoundMm + offsetMm while (xMm <= upperHorizontalBoundMm): cylinderPolyData = self.generateCylinderPolyData(xMm,yMm) polyDataCombiner.AddInputData(cylinderPolyData) xMm = xMm + self.gridSpacingHorizontalMm yMm = yMm + self.gridSpacingVerticalMm polyDataCombiner.Update() gridPolyData = polyDataCombiner.GetOutput() return gridPolyData
def createNeedleModelNode(self, name): locatorModel = self.scene.CreateNodeByClass('vtkMRMLModelNode') # Cylinder represents the locator stick cylinder = vtk.vtkCylinderSource() cylinder.SetRadius(1.5) cylinder.SetHeight(100) cylinder.SetCenter(0, 0, 0) cylinder.Update() # Rotate cylinder tfilter = vtk.vtkTransformPolyDataFilter() trans = vtk.vtkTransform() trans.RotateX(90.0) trans.Translate(0.0, -50.0, 0.0) trans.Update() if vtk.VTK_MAJOR_VERSION <= 5: tfilter.SetInput(cylinder.GetOutput()) else: tfilter.SetInputConnection(cylinder.GetOutputPort()) tfilter.SetTransform(trans) tfilter.Update() # Sphere represents the locator tip sphere = vtk.vtkSphereSource() sphere.SetRadius(3.0) sphere.SetCenter(0, 0, 0) sphere.Update() apd = vtk.vtkAppendPolyData() if vtk.VTK_MAJOR_VERSION <= 5: apd.AddInput(sphere.GetOutput()) apd.AddInput(tfilter.GetOutput()) else: apd.AddInputConnection(sphere.GetOutputPort()) apd.AddInputConnection(tfilter.GetOutputPort()) apd.Update() locatorModel.SetAndObservePolyData(apd.GetOutput()); self.scene.AddNode(locatorModel) locatorModel.SetScene(self.scene); locatorModel.SetName(name) locatorDisp = locatorModel.GetDisplayNodeID() if locatorDisp == None: locatorDisp = self.scene.CreateNodeByClass('vtkMRMLModelDisplayNode') self.scene.AddNode(locatorDisp) locatorDisp.SetScene(self.scene) locatorModel.SetAndObserveDisplayNodeID(locatorDisp.GetID()); color = [0, 0, 0] color[0] = 0.5 color[1] = 0.5 color[2] = 1.0 locatorDisp.SetColor(color) return locatorModel.GetID()
def updateTipModelNode(self, tipModelNode, poly, p0, pe, radius, color, opacity): points = vtk.vtkPoints() cellArray = vtk.vtkCellArray() points.SetNumberOfPoints(2) cellArray.InsertNextCell(2) points.SetPoint(0, p0) cellArray.InsertCellPoint(0) points.SetPoint(1, pe) cellArray.InsertCellPoint(1) poly.Initialize() poly.SetPoints(points) poly.SetLines(cellArray) tubeFilter = vtk.vtkTubeFilter() tubeFilter.SetInputData(poly) tubeFilter.SetRadius(radius) tubeFilter.SetNumberOfSides(20) tubeFilter.CappingOn() tubeFilter.Update() # Sphere represents the locator tip sphere = vtk.vtkSphereSource() sphere.SetRadius(radius * 2.0) sphere.SetCenter(pe) sphere.Update() apd = vtk.vtkAppendPolyData() if vtk.VTK_MAJOR_VERSION <= 5: apd.AddInput(sphere.GetOutput()) apd.AddInput(tubeFilter.GetOutput()) else: apd.AddInputConnection(sphere.GetOutputPort()) apd.AddInputConnection(tubeFilter.GetOutputPort()) apd.Update() tipModelNode.SetAndObservePolyData(apd.GetOutput()) tipModelNode.Modified() tipDispID = tipModelNode.GetDisplayNodeID() if tipDispID == None: tipDispNode = self.scene.AddNewNodeByClass( 'vtkMRMLModelDisplayNode') tipDispNode.SetScene(self.scene) tipModelNode.SetAndObserveDisplayNodeID(tipDispNode.GetID()) tipDispID = tipModelNode.GetDisplayNodeID() tipDispNode = self.scene.GetNodeByID(tipDispID) prevState = tipDispNode.StartModify() tipDispNode.SetColor(color) tipDispNode.SetOpacity(opacity) tipDispNode.SliceIntersectionVisibilityOn() tipDispNode.SetSliceDisplayModeToIntersection() tipDispNode.EndModify(prevState)
def JoinBoundaryPoints( self, hullPoints1, hullPoints2, offset ): if ( hullPoints1.GetNumberOfPoints() != hullPoints2.GetNumberOfPoints() ): return joiningAppend = vtk.vtkAppendPolyData() numPoints = hullPoints1.GetNumberOfPoints() for i in range( 0, numPoints ): currPointsForSurface = vtk.vtkPoints() point1 = [ 0, 0, 0 ] hullPoints1.GetPoint( ( i - offset )% numPoints, point1 ) currPointsForSurface.InsertNextPoint( point1[ 0 ], point1[ 1 ], point1[ 2 ] ) point2 = [ 0, 0, 0 ] hullPoints2.GetPoint( ( - i ) % numPoints, point2 ) currPointsForSurface.InsertNextPoint( point2[ 0 ], point2[ 1 ], point2[ 2 ] ) # Observe that the deep is flipped from the surface # Must proceed in opposite orders point3 = [ 0, 0, 0 ] hullPoints1.GetPoint( ( i + 1 - offset ) % numPoints, point3 ) currPointsForSurface.InsertNextPoint( point3[ 0 ], point3[ 1 ], point3[ 2 ] ) point4 = [ 0, 0, 0 ] hullPoints2.GetPoint( ( - ( i + 1 ) ) % numPoints, point4 ) currPointsForSurface.InsertNextPoint( point4[ 0 ], point4[ 1 ], point4[ 2 ] ) # We know what the triangles should look like, so create them manually # Note: The order here is important - ensure the triangles face the correct way triangle1 = vtk.vtkTriangle() triangle1.GetPointIds().SetId( 0, 0 ) triangle1.GetPointIds().SetId( 1, 1 ) triangle1.GetPointIds().SetId( 2, 2 ) triangle2 = vtk.vtkTriangle() triangle2.GetPointIds().SetId( 0, 3 ) triangle2.GetPointIds().SetId( 1, 2 ) triangle2.GetPointIds().SetId( 2, 1 ) triangles = vtk.vtkCellArray() triangles.InsertNextCell( triangle1 ) triangles.InsertNextCell( triangle2 ) currPolyDataForSurface = vtk.vtkPolyData() currPolyDataForSurface.SetPoints( currPointsForSurface ) currPolyDataForSurface.SetPolys( triangles ) joiningAppend.AddInputData( currPolyDataForSurface ) joiningAppend.Update() return joiningAppend.GetOutput()
def createOptionalPathModel(self, pos, holeInfoList, intNumOfOptionalPath): if intNumOfOptionalPath == 1: self.setOptionalPathVisibility(1) pathListRAS = [] for i in range(len(holeInfoList)): pathListRAS.append(self.templateRAS[holeInfoList[i][3]]) print "pathListRAS: ", pathListRAS #(not generate path -> pass, otherwise go on) self.optModelNode = slicer.mrmlScene.GetNodeByID(self.optionalPathModelNodeID) if self.optModelNode == None: self.optModelNode = slicer.vtkMRMLModelNode() self.optModelNode.SetName('AllOptionalPaths') slicer.mrmlScene.AddNode(self.optModelNode) self.optionalPathModelNodeID = self.optModelNode.GetID() self.dnodeOpt = slicer.vtkMRMLModelDisplayNode() self.dnodeOpt.SetColor(0.96,0.92,0.56) slicer.mrmlScene.AddNode(self.dnodeOpt) self.optModelNode.SetAndObserveDisplayNodeID(self.dnodeOpt.GetID()) optModelAppend = vtk.vtkAppendPolyData() #for path in pathListRAS: for index in range(len(pathListRAS)): optLineSource = vtk.vtkLineSource() optLineSource.SetPoint1(pos) optLineSource.SetPoint2(pathListRAS[index]) optTubeFilter = vtk.vtkTubeFilter() optTubeFilter.SetInputConnection(optLineSource.GetOutputPort()) optTubeFilter.SetRadius(1.0) optTubeFilter.SetNumberOfSides(10) optTubeFilter.CappingOn() optTubeFilter.Update() if vtk.VTK_MAJOR_VERSION <= 5: optModelAppend.AddInput(optTubeFilter.GetOutput()) else: optModelAppend.AddInputData(optTubeFilter.GetOutput()) optModelAppend.Update() self.optModelNode.SetAndObservePolyData(optModelAppend.GetOutput())
def Combine_Surfaces(self, polydata_list): # Combine some number of polydata together # For example, useful for combining all the bones of a joint back together for saving to a .PLY file # Append the meshes together appendFilter = vtk.vtkAppendPolyData() # Loop through each surface in the list of polydata and input into the filter for i in range(0, len(polydata_list)): appendFilter.AddInputData(polydata_list[i]) # Update the combinded polydata filter appendFilter.Update() # Get the output of the filter (i.e. combined_polydata holds the combined surfaces) combined_polydata = appendFilter.GetOutput() return combined_polydata
def generateGridPolyDataRectangular(self): polyDataCombiner = vtk.vtkAppendPolyData() lowerHorizontalBoundMm = self.evaluateLowerHorizontalBoundMm() upperHorizontalBoundMm = self.evaluateUpperHorizontalBoundMm() lowerVerticalBoundMm = self.evaluateLowerVerticalBoundMm() upperVerticalBoundMm = self.evaluateUpperVerticalBoundMm() # traditional 'for' loops would be very nice here... :-/ yMm = lowerVerticalBoundMm while (yMm <= upperVerticalBoundMm): xMm = lowerHorizontalBoundMm while (xMm <= upperHorizontalBoundMm): cylinderPolyData = self.generateCylinderPolyData(xMm, yMm) polyDataCombiner.AddInputData(cylinderPolyData) xMm = xMm + self.gridSpacingHorizontalMm yMm = yMm + self.gridSpacingVerticalMm polyDataCombiner.Update() gridPolyData = polyDataCombiner.GetOutput() return gridPolyData
def generateGridPolyDataRectangular(self): polyDataCombiner = vtk.vtkAppendPolyData() lowerHorizontalBoundMm = self.evaluateLowerHorizontalBoundMm() upperHorizontalBoundMm = self.evaluateUpperHorizontalBoundMm() lowerVerticalBoundMm = self.evaluateLowerVerticalBoundMm() upperVerticalBoundMm = self.evaluateUpperVerticalBoundMm() # traditional 'for' loops would be very nice here... :-/ yMm = lowerVerticalBoundMm while (yMm <= upperVerticalBoundMm): xMm = lowerHorizontalBoundMm while (xMm <= upperHorizontalBoundMm): cylinderPolyData = self.generateCylinderPolyData(xMm,yMm) polyDataCombiner.AddInputData(cylinderPolyData) xMm = xMm + self.gridSpacingHorizontalMm yMm = yMm + self.gridSpacingVerticalMm polyDataCombiner.Update() gridPolyData = polyDataCombiner.GetOutput() return gridPolyData
def createNeedleModelNode(self, name,index): locatorModel = self.scene.CreateNodeByClass('vtkMRMLModelNode') # Cylinder represents the locator stick cylinder = vtk.vtkCylinderSource() cylinder.SetRadius(1.5) cylinder.SetHeight(100) cylinder.SetCenter(0, 0, 0) cylinder.Update() # Rotate cylinder tfilter = vtk.vtkTransformPolyDataFilter() trans = vtk.vtkTransform() trans.RotateX(90.0) trans.Translate(0.0, -50.0, 0.0) trans.Update() if vtk.VTK_MAJOR_VERSION <= 5: tfilter.SetInput(cylinder.GetOutput()) else: tfilter.SetInputConnection(cylinder.GetOutputPort()) tfilter.SetTransform(trans) tfilter.Update() # Sphere represents the locator tip sphere = vtk.vtkSphereSource() sphere.SetRadius(3.0) sphere.SetCenter(0, 0, 0) sphere.Update() apd = vtk.vtkAppendPolyData() if vtk.VTK_MAJOR_VERSION <= 5: apd.AddInput(sphere.GetOutput()) apd.AddInput(tfilter.GetOutput()) else: apd.AddInputConnection(sphere.GetOutputPort()) apd.AddInputConnection(tfilter.GetOutputPort()) apd.Update() locatorModel.SetAndObservePolyData(apd.GetOutput()); self.scene.AddNode(locatorModel) locatorModel.SetScene(self.scene); needleName = "Needle_%s" % name locatorModel.SetName(needleName) locatorDisp = locatorModel.GetDisplayNodeID() if locatorDisp == None: locatorDisp = self.scene.CreateNodeByClass('vtkMRMLModelDisplayNode') self.scene.AddNode(locatorDisp) locatorDisp.SetScene(self.scene) locatorModel.SetAndObserveDisplayNodeID(locatorDisp.GetID()); color = [0, 0, 0] color[0] = 0.5 color[1] = 0.5 color[2] = 1.0 locatorDisp.SetColor(color) print name if self.colorMap.get(name): locatorDisp.SetColor(self.colorMap.get(name)) color = self.colorMap.get(name) colorName = "background:rgb({},{},{})".format(255*color[0], 255*color[1], 255*color[2]) print colorName self.widget.colorSelectors[index].setStyleSheet(colorName) #qss = qt.QString("background-color: %1").arg(col.name()); return locatorModel.GetID()
print "Computing queries" evaluated_queries = tract_querier.eval_queries( query_file_body, tractography_spatial_indexing, ) query_names = evaluated_queries.keys() print query_names if options.query_selection != '': selected_queries = set(options.query_selection.lower().split(',')) query_names = list(set(query_names) & set(selected_queries)) query_names.sort() print 'save_query' polyDataOut=vtk.vtkAppendPolyData() for query_name in query_names: tr_out_temp=save_query( query_name, tr, options, evaluated_queries) if tr_out_temp is not None: tr_polydata=tracts_to_vtkPolyData64(tr_out_temp) polyDataOut.AddInput(tr_polydata) updateOutputNode(polyDataOut.GetOutput()) return def vtkMatrix_2_array(m): import numpy out=numpy.zeros(shape=(4,4)) for i in range(4): for j in range(4): out[i][j]=m.GetElement(i,j) return out
print "Computing queries" evaluated_queries = tract_querier.eval_queries( query_file_body, tractography_spatial_indexing, ) query_names = evaluated_queries.keys() print query_names if options.query_selection != '': selected_queries = set(options.query_selection.lower().split(',')) query_names = list(set(query_names) & set(selected_queries)) query_names.sort() print 'save_query' polyDataOut = vtk.vtkAppendPolyData() for query_name in query_names: tr_out_temp = save_query(query_name, tr, options, evaluated_queries) if tr_out_temp is not None: tr_polydata = tracts_to_vtkPolyData64(tr_out_temp) polyDataOut.AddInput(tr_polydata) updateOutputNode(polyDataOut.GetOutput()) return def vtkMatrix_2_array(m): import numpy out = numpy.zeros(shape=(4, 4)) for i in range(4): for j in range(4): out[i][j] = m.GetElement(i, j)
# This module was tested on 3D Slicer version 4.3.1
def visualNeedlePath(self, pos, pointOnTemplateRAS): if type(pointOnTemplateRAS) == list: pass elif type(pointOnTemplateRAS) == int: pointOnTemplateRAS = self.templateRAS[pointOnTemplateRAS] self.pathModelNode = slicer.mrmlScene.GetNodeByID(self.needlePathModelNodeID) if self.pathModelNode == None: self.pathModelNode = slicer.vtkMRMLModelNode() self.pathModelNode.SetName('AngulatedNeedlePath') slicer.mrmlScene.AddNode(self.pathModelNode) self.needlePathModelNodeID = self.pathModelNode.GetID() self.dnodeSelectedPath = slicer.vtkMRMLModelDisplayNode() self.dnodeSelectedPath.SetColor(0, 1, 1) slicer.mrmlScene.AddNode(self.dnodeSelectedPath) self.pathModelNode.SetAndObserveDisplayNodeID(self.dnodeSelectedPath.GetID()) pathModelAppend = vtk.vtkAppendPolyData() tempLineSource = vtk.vtkLineSource() tempLineSource.SetPoint1(pos) # target in RAS tempLineSource.SetPoint2(pointOnTemplateRAS) # point on template in RAS tempTubeFilter = vtk.vtkTubeFilter() tempTubeFilter.SetInputConnection(tempLineSource.GetOutputPort()) tempTubeFilter.SetRadius(1.0) tempTubeFilter.SetNumberOfSides(18) tempTubeFilter.CappingOn() tempTubeFilter.Update() self.setModelSliceIntersectionVisibilityByID(self.needlePathModelNodeID, 1) if vtk.VTK_MAJOR_VERSION <= 5: pathModelAppend.AddInput(tempTubeFilter.GetOutput()) else: pathModelAppend.AddInputData(tempTubeFilter.GetOutput()) pathModelAppend.Update() self.pathModelNode.SetAndObservePolyData(pathModelAppend.GetOutput()) # # Reslice along with the selected needle # print "(target) pos: ", pos print "pointOnTemplateRAS: ", pointOnTemplateRAS R = pointOnTemplateRAS[0] A = pointOnTemplateRAS[1] S = pointOnTemplateRAS[2] deltaR = pos[0] - R deltaA = pos[1] - A deltaS = pos[2] - S pathVector = numpy.array([deltaR, deltaA, deltaS]) tR = numpy.array([1.0, 0, 0]) vectorA = numpy.cross(pathVector, tR) vectorS = numpy.cross(pathVector, vectorA) pathVectorNorm = numpy.linalg.norm(pathVector) vectorANorm = numpy.linalg.norm(vectorA) vectorSNorm = numpy.linalg.norm(vectorS) matrix = vtk.vtkMatrix4x4() matrix.Identity() ## TODO: if pathVector is parallel to R matrix.SetElement(0, 0, pathVector[0]/pathVectorNorm) matrix.SetElement(1, 0, pathVector[1]/pathVectorNorm) matrix.SetElement(2, 0, pathVector[2]/pathVectorNorm) matrix.SetElement(0, 1, vectorA[0]/vectorANorm) matrix.SetElement(1, 1, vectorA[1]/vectorANorm) matrix.SetElement(2, 1, vectorA[2]/vectorANorm) matrix.SetElement(0, 2, vectorS[0]/vectorSNorm) matrix.SetElement(1, 2, vectorS[1]/vectorSNorm) matrix.SetElement(2, 2, vectorS[2]/vectorSNorm) matrix.SetElement(0, 3, R) matrix.SetElement(1, 3, A) matrix.SetElement(2, 3, S) selectNeedleNode = slicer.mrmlScene.GetNodeByID(self.selectNeedleNodeID) if selectNeedleNode == None: selectNeedleNode = slicer.vtkMRMLLinearTransformNode() selectNeedleNode.SetName('SelectedNeedle') slicer.mrmlScene.AddNode(selectNeedleNode) self.selectNeedleNodeID = selectNeedleNode.GetID() selectNeedleNode.SetAndObserveMatrixTransformToParent(matrix) modelNodes = slicer.mrmlScene.GetNodesByClass("vtkMRMLModelNode") for index in range(modelNodes.GetNumberOfItems()): indexNode = modelNodes.GetItemAsObject(index) if indexNode.GetTransformNodeID() == selectNeedleNode.GetID(): indexNode.SetDisplayVisibility(1) self.selectNeedleModelNode = indexNode print indexNode.GetID() red = slicer.mrmlScene.GetNodeByID("vtkMRMLSliceNodeRed") yellow = slicer.mrmlScene.GetNodeByID("vtkMRMLSliceNodeYellow") green = slicer.mrmlScene.GetNodeByID("vtkMRMLSliceNodeGreen") ## Changed by yz, Sep/25 ## If 3D Slicer includes the extension "SlicerIGT", uncomment the below code # vrdLogic = slicer.modules.volumereslicedriver.logic() # redDriver = vrdLogic.SetDriverForSlice(selectNeedleNode.GetID(), red) # redMode = vrdLogic.SetModeForSlice(vrdLogic.MODE_INPLANE, red) # yellowDriver = vrdLogic.SetDriverForSlice(selectNeedleNode.GetID(), yellow) # yellowMode = vrdLogic.SetModeForSlice(vrdLogic.MODE_INPLANE90, yellow) # greenDriver = vrdLogic.SetDriverForSlice(selectNeedleNode.GetID(), green) # greenMode = vrdLogic.SetModeForSlice(vrdLogic.MODE_TRANSVERSE, green) # vrdLogic.Modified() return pointOnTemplateRAS
def createTemplateModel(self): self.templatePathVectors = [] self.templatePathOrigins = [] self.tempModelNode = slicer.mrmlScene.GetNodeByID(self.templateModelNodeID) if self.tempModelNode is None: self.tempModelNode = slicer.vtkMRMLModelNode() self.tempModelNode.SetName('NeedleGuideTemplate') slicer.mrmlScene.AddNode(self.tempModelNode) self.templateModelNodeID = self.tempModelNode.GetID() dnode = slicer.vtkMRMLModelDisplayNode() #dnode.SetColor(self.ModelColor) slicer.mrmlScene.AddNode(dnode) self.tempModelNode.SetAndObserveDisplayNodeID(dnode.GetID()) self.modelNodetag = self.tempModelNode.AddObserver(slicer.vtkMRMLTransformableNode.TransformModifiedEvent, self.onTemplateTransformUpdated) self.pathModelNode = slicer.mrmlScene.GetNodeByID(self.needlePathModelNodeID) if self.pathModelNode is None: self.pathModelNode = slicer.vtkMRMLModelNode() self.pathModelNode.SetName('NeedleGuideNeedlePath') slicer.mrmlScene.AddNode(self.pathModelNode) self.needlePathModelNodeID = self.pathModelNode.GetID() dnode = slicer.vtkMRMLModelDisplayNode() slicer.mrmlScene.AddNode(dnode) self.pathModelNode.SetAndObserveDisplayNodeID(dnode.GetID()) pathModelAppend = vtk.vtkAppendPolyData() tempModelAppend = vtk.vtkAppendPolyData() for row in self.templateConfig: p1 = numpy.array(row[0:3]) p2 = numpy.array(row[3:6]) tempLineSource = vtk.vtkLineSource() tempLineSource.SetPoint1(p1) tempLineSource.SetPoint2(p2) tempTubeFilter = vtk.vtkTubeFilter() tempTubeFilter.SetInputConnection(tempLineSource.GetOutputPort()) tempTubeFilter.SetRadius(1.0) tempTubeFilter.SetNumberOfSides(18) tempTubeFilter.CappingOn() tempTubeFilter.Update() pathLineSource = vtk.vtkLineSource() v = p2-p1 nl = numpy.linalg.norm(v) n = v/nl # normal vector l = row[6] p3 = p1 + l * n pathLineSource.SetPoint1(p1) pathLineSource.SetPoint2(p3) self.templatePathOrigins.append([row[0], row[1], row[2], 1.0]) self.templatePathVectors.append([n[0], n[1], n[2], 1.0]) self.templateMaxDepth.append(row[6]) pathTubeFilter = vtk.vtkTubeFilter() pathTubeFilter.SetInputConnection(pathLineSource.GetOutputPort()) pathTubeFilter.SetRadius(0.8) pathTubeFilter.SetNumberOfSides(18) pathTubeFilter.CappingOn() pathTubeFilter.Update() if vtk.VTK_MAJOR_VERSION <= 5: tempModelAppend.AddInput(tempTubeFilter.GetOutput()) pathModelAppend.AddInput(pathTubeFilter.GetOutput()) else: tempModelAppend.AddInputData(tempTubeFilter.GetOutput()) pathModelAppend.AddInputData(pathTubeFilter.GetOutput()) tempModelAppend.Update() self.tempModelNode.SetAndObservePolyData(tempModelAppend.GetOutput()) pathModelAppend.Update() self.pathModelNode.SetAndObservePolyData(pathModelAppend.GetOutput())
def createNeedleModelNode(self, name): locatorModel = self.scene.CreateNodeByClass('vtkMRMLModelNode') # Cylinder represents the locator stick cylinder = vtk.vtkCylinderSource() cylinder.SetRadius(1.5) cylinder.SetHeight(100) cylinder.SetCenter(0, 0, 0) cylinder.Update() # Rotate cylinder tfilter = vtk.vtkTransformPolyDataFilter() trans = vtk.vtkTransform() trans.RotateX(90.0) trans.Translate(0.0, -50.0, 0.0) trans.Update() if vtk.VTK_MAJOR_VERSION <= 5: tfilter.SetInput(cylinder.GetOutput()) else: tfilter.SetInputConnection(cylinder.GetOutputPort()) tfilter.SetTransform(trans) tfilter.Update() # Sphere represents the locator tip sphere = vtk.vtkSphereSource() sphere.SetRadius(3.0) sphere.SetCenter(0, 0, 0) sphere.Update() apd = vtk.vtkAppendPolyData() if vtk.VTK_MAJOR_VERSION <= 5: apd.AddInput(sphere.GetOutput()) apd.AddInput(tfilter.GetOutput()) else: apd.AddInputConnection(sphere.GetOutputPort()) apd.AddInputConnection(tfilter.GetOutputPort()) apd.Update() locatorModel.SetAndObservePolyData(apd.GetOutput()) self.scene.AddNode(locatorModel) locatorModel.SetScene(self.scene) locatorModel.SetName(name) locatorDisp = locatorModel.GetDisplayNodeID() if locatorDisp == None: locatorDisp = self.scene.CreateNodeByClass( 'vtkMRMLModelDisplayNode') self.scene.AddNode(locatorDisp) locatorDisp.SetScene(self.scene) locatorModel.SetAndObserveDisplayNodeID(locatorDisp.GetID()) color = [0, 0, 0] color[0] = 0.5 color[1] = 0.5 color[2] = 1.0 locatorDisp.SetColor(color) return locatorModel.GetID()
def createTemplateModel(self): self.templatePathVectors = [] self.templatePathOrigins = [] tempModelNode = slicer.mrmlScene.GetNodeByID(self.templateModelNodeID) if tempModelNode == None: tempModelNode = slicer.vtkMRMLModelNode() tempModelNode.SetName('NeedleGuideTemplate') slicer.mrmlScene.AddNode(tempModelNode) self.templateModelNodeID = tempModelNode.GetID() dnode = slicer.vtkMRMLModelDisplayNode() slicer.mrmlScene.AddNode(dnode) tempModelNode.SetAndObserveDisplayNodeID(dnode.GetID()) self.modelNodetag = tempModelNode.AddObserver(slicer.vtkMRMLTransformableNode.TransformModifiedEvent, self.onTemplateTransformUpdated) tempModelAppend = vtk.vtkAppendPolyData() for row in self.templateConfig: p1 = numpy.array(row[0:3]) p2 = numpy.array(row[3:6]) tempLineSource = vtk.vtkLineSource() tempLineSource.SetPoint1(p1) tempLineSource.SetPoint2(p2) tempTubeFilter = vtk.vtkTubeFilter() tempTubeFilter.SetInputConnection(tempLineSource.GetOutputPort()) tempTubeFilter.SetRadius(1.0) tempTubeFilter.SetNumberOfSides(18) tempTubeFilter.CappingOn() tempTubeFilter.Update() pathLineSource = vtk.vtkLineSource() v = p2-p1 nl = numpy.linalg.norm(v) n = v/nl # normal vector l = row[6] p3 = p1 + l * n pathLineSource.SetPoint1(p1) pathLineSource.SetPoint2(p3) self.templatePathOrigins.append([row[0], row[1], row[2], 1.0]) self.templatePathVectors.append([n[0], n[1], n[2], 1.0]) self.templateMaxDepth.append(row[6]) pathTubeFilter = vtk.vtkTubeFilter() pathTubeFilter.SetInputConnection(pathLineSource.GetOutputPort()) pathTubeFilter.SetRadius(0.8) pathTubeFilter.SetNumberOfSides(18) pathTubeFilter.CappingOn() pathTubeFilter.Update() if vtk.VTK_MAJOR_VERSION <= 5: tempModelAppend.AddInput(tempTubeFilter.GetOutput()) # pathModelAppend.AddInput(pathTubeFilter.GetOutput()) else: tempModelAppend.AddInputData(tempTubeFilter.GetOutput()) # pathModelAppend.AddInputData(pathTubeFilter.GetOutput()) tempModelAppend.Update() tempModelNode.SetAndObservePolyData(tempModelAppend.GetOutput())
def run( self, markupNode, depth, fitPlane, flip ): """ Run the actual algorithm """ # Get all of the fiducial nodes and convert to vtkPoints points = vtk.vtkPoints() for i in range( 0, markupNode.GetNumberOfFiducials() ): currentCoordinates = [ 0, 0, 0 ] markupNode.GetNthFiducialPosition( i, currentCoordinates ) points.InsertNextPoint( currentCoordinates ) # Check that there is non-zero range in all coordinate directions pointsBounds = [ 0, 0, 0, 0, 0, 0 ] points.GetBounds( pointsBounds ) if ( pointsBounds[0] == pointsBounds [1] or pointsBounds[2] == pointsBounds [3] or pointsBounds[4] == pointsBounds [5] ): print "Tissue Model Creator: Points have no extent in one or more coordinate directions." return False # Create a polydata object from the points # The reversiness doesn't matter - we will fix it later if it os wrong if ( fitPlane ): surfacePolyData = self.PointsToPlanePolyData( points, True ) else: surfacePolyData = self.PointsToSurfacePolyData( points, True ) surfaceCleaner = vtk.vtkCleanPolyData() surfaceCleaner.SetInputData( surfacePolyData ) surfaceCleaner.Update() surfacePolyData = surfaceCleaner.GetOutput() mean = self.CalculateMean( points ) surfaceBase = [ 0, 0, 0 ] surfaceDir1 = [ 0, 0, 0 ] surfaceDir2 = [ 0, 0, 0 ] surfaceNormal = [ 0, 0, 0 ] self.CalculatePlane( surfacePolyData.GetPoints(), surfaceBase, surfaceDir1, surfaceDir2, surfaceNormal ) if ( flip == True ): surfaceNormal[ 0 ] = - surfaceNormal[ 0 ] surfaceNormal[ 1 ] = - surfaceNormal[ 1 ] surfaceNormal[ 2 ] = - surfaceNormal[ 2 ] extremePointIndex = self.FindExtremePoint( surfacePolyData.GetPoints(), surfaceBase, surfaceNormal ) reverse = self.ReverseNormals( extremePointIndex, surfacePolyData, surfaceNormal ) # Reverse the normals if necessary reverseFilter = vtk.vtkReverseSense() reverseFilter.SetInputData( surfacePolyData ) reverseFilter.SetReverseCells( reverse ) reverseFilter.SetReverseNormals( reverse ) reverseFilter.Update() surfacePolyData = reverseFilter.GetOutput() untransDeepPolyData = vtk.vtkPolyData() untransDeepPolyData.DeepCopy( surfacePolyData ) # Make the normals opposite the surface's normals reverseFilter = vtk.vtkReverseSense() reverseFilter.SetInputData( untransDeepPolyData ) reverseFilter.SetReverseCells( True ) reverseFilter.SetReverseNormals( True ) reverseFilter.Update() untransDeepPolyData = reverseFilter.GetOutput() deepTransform = vtk.vtkTransform() deepTransform.Translate( depth * surfaceNormal[0], depth * surfaceNormal[1], depth * surfaceNormal[2] ) deepTransformFilter = vtk.vtkTransformPolyDataFilter() deepTransformFilter.SetInputData( untransDeepPolyData ) deepTransformFilter.SetTransform( deepTransform ) deepTransformFilter.Update() deepPolyData = deepTransformFilter.GetOutput() surfaceHullPoints = self.GetBoundaryPoints( surfacePolyData ) deepHullPoints = self.GetBoundaryPoints( deepPolyData ) # Apparently, the joining needs an offset for the plane, but not for the surface reconstruction if ( fitPlane ): jointHullPolyData = self.JoinBoundaryPoints( surfaceHullPoints, deepHullPoints, 1 ) else: jointHullPolyData = self.JoinBoundaryPoints( surfaceHullPoints, deepHullPoints, 0 ) # Append all of the polydata together tissuePolyDataAppend = vtk.vtkAppendPolyData() tissuePolyDataAppend.AddInputData( surfacePolyData ) tissuePolyDataAppend.AddInputData( deepPolyData ) tissuePolyDataAppend.AddInputData( jointHullPolyData ) tissuePolyDataAppend.Update() # Clean up so the surface is closed tissueCleaner = vtk.vtkCleanPolyData() tissueCleaner.SetInputData( tissuePolyDataAppend.GetOutput() ) tissueCleaner.Update() tissueModelPolyData = tissueCleaner.GetOutput() # Add the data to a model tissueModel = slicer.mrmlScene.CreateNodeByClass( "vtkMRMLModelNode" ) slicer.mrmlScene.AddNode( tissueModel ) tissueModel.SetName( "TissueModel" ) tissueModel.SetAndObservePolyData( tissueModelPolyData ) tissueModel.SetScene( slicer.mrmlScene ) # Finally display the model tissueModelDisplay = slicer.mrmlScene.CreateNodeByClass( "vtkMRMLModelDisplayNode" ) slicer.mrmlScene.AddNode( tissueModelDisplay ) tissueModel.SetAndObserveDisplayNodeID( tissueModelDisplay.GetID() ) tissueModelDisplay.SetScene( slicer.mrmlScene ) tissueModelDisplay.SetInputPolyDataConnection( tissueModel.GetPolyDataConnection() ) # Check to make sure the model is a closed surface edgesFilter = vtk.vtkFeatureEdges() edgesFilter.FeatureEdgesOff() edgesFilter.BoundaryEdgesOn() edgesFilter.NonManifoldEdgesOn() edgesFilter.SetInputData( tissueModel.GetPolyData() ) edgesFilter.Update() if ( edgesFilter.GetOutput().GetNumberOfCells() != 0 ): print "Tissue Model Creator: Surface is not closed." return False return True