def ComputeMeanDistance(self, inputFiducials, inputModel, transform): surfacePoints = vtk.vtkPoints() cellId = vtk.mutable(0) subId = vtk.mutable(0) dist2 = vtk.mutable(0.0) locator = vtk.vtkCellLocator() locator.SetDataSet(inputModel.GetPolyData()) locator.SetNumberOfCellsPerBucket(1) locator.BuildLocator() totalDistance = 0.0 n = inputFiducials.GetNumberOfFiducials() m = vtk.vtkMath() for fiducialIndex in range(0, n): originalPoint = [0, 0, 0] inputFiducials.GetNthFiducialPosition(fiducialIndex, originalPoint) transformedPoint = [0, 0, 0, 1] #transform.GetTransformToParent().TransformVector(originalPoint, transformedPoint) originalPoint.append(1) transform.GetTransformToParent().MultiplyPoint(originalPoint, transformedPoint) #transformedPoints.InsertNextPoint(transformedPoint) surfacePoint = [0, 0, 0] transformedPoint.pop() locator.FindClosestPoint(transformedPoint, surfacePoint, cellId, subId, dist2) totalDistance = totalDistance + math.sqrt(dist2) return (totalDistance / n)
def ComputeMeanDistance(self, inputSourceModel, inputTargetModel, transform ): sourcePolyData = inputSourceModel.GetPolyData() targetPolyData = inputTargetModel.GetPolyData() cellId = vtk.mutable(0) subId = vtk.mutable(0) dist2 = vtk.mutable(0.0) locator = vtk.vtkCellLocator() locator.SetDataSet( targetPolyData ) locator.SetNumberOfCellsPerBucket( 1 ) locator.BuildLocator() totalDistance = 0.0 sourcePoints = sourcePolyData.GetPoints() n = sourcePoints.GetNumberOfPoints() m = vtk.vtkMath() for sourcePointIndex in range(n): sourcePointPos = [0, 0, 0] sourcePoints.GetPoint( sourcePointIndex, sourcePointPos ) transformedSourcePointPos = [0, 0, 0, 1] #transform.GetTransformToParent().TransformVector( sourcePointPos, transformedSourcePointPos ) sourcePointPos.append(1) transform.GetTransformToParent().MultiplyPoint( sourcePointPos, transformedSourcePointPos ) #transformedPoints.InsertNextPoint( transformedSourcePointPos ) surfacePoint = [0, 0, 0] transformedSourcePointPos.pop() locator.FindClosestPoint( transformedSourcePointPos, surfacePoint, cellId, subId, dist2 ) totalDistance = totalDistance + math.sqrt(dist2) return ( totalDistance / n )
def ComputeMeanDistance(self, inputSourceModel, inputTargetModel, transform ): sourcePolyData = inputSourceModel.GetPolyData() targetPolyData = inputTargetModel.GetPolyData() cellId = vtk.mutable(0) subId = vtk.mutable(0) dist2 = vtk.mutable(0.0) locator = vtk.vtkCellLocator() locator.SetDataSet( targetPolyData ) locator.SetNumberOfCellsPerBucket( 1 ) locator.BuildLocator() totalDistance = 0.0 sourcePoints = sourcePolyData.GetPoints() n = sourcePoints.GetNumberOfPoints() m = vtk.vtkMath() for sourcePointIndex in xrange(n): sourcePointPos = [0, 0, 0] sourcePoints.GetPoint( sourcePointIndex, sourcePointPos ) transformedSourcePointPos = [0, 0, 0, 1] #transform.GetTransformToParent().TransformVector( sourcePointPos, transformedSourcePointPos ) sourcePointPos.append(1) transform.GetTransformToParent().MultiplyPoint( sourcePointPos, transformedSourcePointPos ) #transformedPoints.InsertNextPoint( transformedSourcePointPos ) surfacePoint = [0, 0, 0] transformedSourcePointPos.pop() locator.FindClosestPoint( transformedSourcePointPos, surfacePoint, cellId, subId, dist2 ) totalDistance = totalDistance + math.sqrt(dist2) return ( totalDistance / n )
def ComputeMeanDistance(self, inputFiducials, inputModel, transform ): surfacePoints = vtk.vtkPoints() cellId = vtk.mutable(0) subId = vtk.mutable(0) dist2 = vtk.mutable(0.0) locator = vtk.vtkCellLocator() locator.SetDataSet( inputModel.GetPolyData() ) locator.SetNumberOfCellsPerBucket( 1 ) locator.BuildLocator() totalDistance = 0.0 n = inputFiducials.GetNumberOfFiducials() m = vtk.vtkMath() for fiducialIndex in range( 0, n ): originalPoint = [0, 0, 0] inputFiducials.GetNthFiducialPosition( fiducialIndex, originalPoint ) transformedPoint = [0, 0, 0, 1] #transform.GetTransformToParent().TransformVector( originalPoint, transformedPoint ) originalPoint.append(1) transform.GetTransformToParent().MultiplyPoint( originalPoint, transformedPoint ) #transformedPoints.InsertNextPoint( transformedPoint ) surfacePoint = [0, 0, 0] transformedPoint.pop() locator.FindClosestPoint( transformedPoint, surfacePoint, cellId, subId, dist2 ) totalDistance = totalDistance + math.sqrt(dist2) return ( totalDistance / n )
def calculateFiducialDistance(self, modelNode, fiducial): closestFiducial = slicer.util.getNode('CP') if not closestFiducial: closestFiducial = slicer.vtkMRMLMarkupsFiducialNode() closestFiducial.SetName('CP') closestFiducial.AddFiducial(0, 0, 0) closestFiducial.SetNthFiducialLabel(0, 'CP') slicer.mrmlScene.AddNode(closestFiducial) closestFiducial.SetDisplayVisibility(False) line = slicer.util.getNode('Line') if not line: line = slicer.vtkMRMLModelNode() line.SetName('Line') linePolyData = vtk.vtkPolyData() line.SetAndObservePolyData(linePolyData) modelDisplay = slicer.vtkMRMLModelDisplayNode() modelDisplay.SetSliceIntersectionVisibility(True) modelDisplay.SetColor(0,1,0) slicer.mrmlScene.AddNode(modelDisplay) line.SetAndObserveDisplayNodeID(modelDisplay.GetID()) slicer.mrmlScene.AddNode(line) cellLocator = vtk.vtkCellLocator() cellLocator.SetDataSet(modelNode.GetPolyData()) cellLocator.BuildLocator() if fiducial.GetNumberOfFiducials() > 0: ras = [0.0, 0.0, 0.0] closestPoint = [0.0, 0.0, 0.0] fiducial.GetNthFiducialPosition(0, ras) distanceSquared = vtk.mutable(0.0) subId = vtk.mutable(0) cellId = vtk.mutable(0) cell = vtk.vtkGenericCell() cellLocator.FindClosestPoint(ras, closestPoint, cell, cellId, subId, distanceSquared); distance = math.sqrt(distanceSquared) closestFiducial.SetNthFiducialPosition(0, closestPoint[0], closestPoint[1], closestPoint[2]) closestFiducial.SetDisplayVisibility(True) self.drawLineBetweenPoints(line, ras, closestPoint) self.set3dViewConernerAnnotation('Distance = ' + "%.2f" % distance + 'mm') else: logging.warning('No fiducials in list!')
def updateScalarBarRange(self, sliceLogic, volumeNode, scalarBar, selectedLayer): vdn = volumeNode.GetDisplayNode() if vdn: vcn = vdn.GetColorNode() lut = vcn.GetLookupTable() lut2 = vtk.vtkLookupTable() lut2.DeepCopy(lut) width = vtk.mutable(0) level = vtk.mutable(0) rangeLow = vtk.mutable(0) rangeHigh = vtk.mutable(0) if selectedLayer == 'background': sliceLogic.GetBackgroundWindowLevelAndRange(width,level,rangeLow,rangeHigh) else: sliceLogic.GetForegroundWindowLevelAndRange(width,level,rangeLow,rangeHigh) lut2.SetRange(level-width/2,level+width/2) scalarBar.SetLookupTable(lut2)
def updateScalarBarRange(self, sliceLogic, volumeNode, scalarBar, selectedLayer): vdn = volumeNode.GetDisplayNode() if vdn: vcn = vdn.GetColorNode() lut = vcn.GetLookupTable() lut2 = vtk.vtkLookupTable() lut2.DeepCopy(lut) width = vtk.mutable(0) level = vtk.mutable(0) rangeLow = vtk.mutable(0) rangeHigh = vtk.mutable(0) if selectedLayer == 'background': sliceLogic.GetBackgroundWindowLevelAndRange(width,level,rangeLow,rangeHigh) else: sliceLogic.GetForegroundWindowLevelAndRange(width,level,rangeLow,rangeHigh) lut2.SetRange(int(level-width/2),int(level+width/2)) scalarBar.SetLookupTable(lut2) '''
def calculateDistance(self): point = [0.0,0.0,0.0] closestPoint = [0.0, 0.0, 0.0] m = vtk.vtkMatrix4x4() self.toolTipToTool.GetMatrixTransformToWorld(m) point[0] = m.GetElement(0, 3) point[1] = m.GetElement(1, 3) point[2] = m.GetElement(2, 3) distanceSquared = vtk.mutable(0.0) subId = vtk.mutable(0) cellId = vtk.mutable(0) cell = vtk.vtkGenericCell() self.cellLocator.FindClosestPoint(point, closestPoint, cell, cellId, subId, distanceSquared); distance = math.sqrt(distanceSquared) self.closestFiducial.SetNthFiducialPosition(0, closestPoint[0], closestPoint[1], closestPoint[2]) self.closestFiducial.SetDisplayVisibility(True) self.drawLineBetweenPoints(point, closestPoint) self.tipFiducial.SetNthFiducialLabel(0, ' %.0f' % distance + 'mm')
def calcApproachScore(self, point, skinPolyData, obstacleBspTree, skinModelNode=None): pTarget = point polyData = skinPolyData nPoints = polyData.GetNumberOfPoints() nCells = polyData.GetNumberOfCells() pSurface=[0.0, 0.0, 0.0] minDistancePoint = [0.0, 0.0, 0.0] tolerance = 0.001 t = vtk.mutable(0.0) x = [0.0, 0.0, 0.0] pcoords = [0.0, 0.0, 0.0] subId = vtk.mutable(0) #print ("nPoints = %d" % (nPoints)) #print ("nCells = %d" % (nCells)) # Map surface model if skinModelNode != None: pointValue = vtk.vtkDoubleArray() pointValue.SetName("Colors") pointValue.SetNumberOfComponents(1) pointValue.SetNumberOfTuples(nPoints) pointValue.Reset() pointValue.FillComponent(0,0.0); bspTree = obstacleBspTree cp0=[0.0, 0.0, 0.0] cp1=[0.0, 0.0, 0.0] cp2=[0.0, 0.0, 0.0] accessibleArea = 0.0 inaccessibleArea = 0.0 ids=vtk.vtkIdList() minDistance = -1; for index in range(nCells): cell = polyData.GetCell(index) if cell.GetCellType() == vtk.VTK_TRIANGLE: area = cell.ComputeArea() polyData.GetCellPoints(index, ids) polyData.GetPoint(ids.GetId(0), cp0) polyData.GetPoint(ids.GetId(1), cp1) polyData.GetPoint(ids.GetId(2), cp2) vtk.vtkTriangle.TriangleCenter(cp0, cp1, cp2, pSurface) iD = bspTree.IntersectWithLine(pSurface, pTarget, tolerance, t, x, pcoords, subId) if iD < 1: if skinModelNode != None: d = vtk.vtkMath.Distance2BetweenPoints(pSurface, pTarget) d = math.sqrt(d) if d < minDistance or minDistance < 0: minDistance = d minDistancePoint = [pSurface[0],pSurface[1],pSurface[2]] v = d+101 pointValue.InsertValue(ids.GetId(0), v) pointValue.InsertValue(ids.GetId(1), v) pointValue.InsertValue(ids.GetId(2), v) accessibleArea = accessibleArea + area else: if skinModelNode != None: v = -1.0 pointValue.InsertValue(ids.GetId(0), v) pointValue.InsertValue(ids.GetId(1), v) pointValue.InsertValue(ids.GetId(2), v) inaccessibleArea = inaccessibleArea + area else: print ("ERROR: Non-triangular cell.") score = accessibleArea / (accessibleArea + inaccessibleArea) if skinModelNode != None: skinModelNode.AddPointScalars(pointValue) skinModelNode.SetActivePointScalars("Colors", vtk.vtkDataSetAttributes.SCALARS) skinModelNode.Modified() displayNode = skinModelNode.GetModelDisplayNode() displayNode.SetActiveScalarName("Colors") displayNode.SetScalarRange(0.0,200.0) return (score, minDistance, minDistancePoint)
def calcApproachScore(self, point, skinPolyData, obstacleBspTree, skinModelNode=None): pTarget = point polyData = skinPolyData nPoints = polyData.GetNumberOfPoints() nCells = polyData.GetNumberOfCells() pSurface = [0.0, 0.0, 0.0] minDistancePoint = [0.0, 0.0, 0.0] tolerance = 0.001 t = vtk.mutable(0.0) x = [0.0, 0.0, 0.0] pcoords = [0.0, 0.0, 0.0] subId = vtk.mutable(0) #print ("nPoints = %d" % (nPoints)) #print ("nCells = %d" % (nCells)) # Map surface model if skinModelNode != None: pointValue = vtk.vtkDoubleArray() pointValue.SetName("Colors") pointValue.SetNumberOfComponents(1) pointValue.SetNumberOfTuples(nPoints) pointValue.Reset() pointValue.FillComponent(0, 0.0) bspTree = obstacleBspTree cp0 = [0.0, 0.0, 0.0] cp1 = [0.0, 0.0, 0.0] cp2 = [0.0, 0.0, 0.0] accessibleArea = 0.0 inaccessibleArea = 0.0 ids = vtk.vtkIdList() minDistance = -1 for index in range(nCells): cell = polyData.GetCell(index) if cell.GetCellType() == vtk.VTK_TRIANGLE: area = cell.ComputeArea() polyData.GetCellPoints(index, ids) polyData.GetPoint(ids.GetId(0), cp0) polyData.GetPoint(ids.GetId(1), cp1) polyData.GetPoint(ids.GetId(2), cp2) vtk.vtkTriangle.TriangleCenter(cp0, cp1, cp2, pSurface) iD = bspTree.IntersectWithLine(pSurface, pTarget, tolerance, t, x, pcoords, subId) if iD < 1: if skinModelNode != None: d = vtk.vtkMath.Distance2BetweenPoints( pSurface, pTarget) d = math.sqrt(d) if d < minDistance or minDistance < 0: minDistance = d minDistancePoint = [ pSurface[0], pSurface[1], pSurface[2] ] v = d + 101 pointValue.InsertValue(ids.GetId(0), v) pointValue.InsertValue(ids.GetId(1), v) pointValue.InsertValue(ids.GetId(2), v) accessibleArea = accessibleArea + area else: if skinModelNode != None: v = -1.0 pointValue.InsertValue(ids.GetId(0), v) pointValue.InsertValue(ids.GetId(1), v) pointValue.InsertValue(ids.GetId(2), v) inaccessibleArea = inaccessibleArea + area else: print("ERROR: Non-triangular cell.") score = accessibleArea / (accessibleArea + inaccessibleArea) if skinModelNode != None: skinModelNode.AddPointScalars(pointValue) skinModelNode.SetActivePointScalars( "Colors", vtk.vtkDataSetAttributes.SCALARS) skinModelNode.Modified() displayNode = skinModelNode.GetModelDisplayNode() displayNode.SetActiveScalarName("Colors") displayNode.SetScalarRange(0.0, 200.0) return (score, minDistance, minDistancePoint)
def calcApproachScore(self, targetPointNode, skinPolyData, obstacleBspTree, skinModelNode=None): #pTargetA = targetPointNode.GetMarkupPointVector(0, 0) tListNumber = targetPointNode.GetNumberOfFiducials() #pTargetA = pointA #pTargetB = pointB #pTargetC = pointC polyData = skinPolyData nPoints = polyData.GetNumberOfPoints() nCells = polyData.GetNumberOfCells() pSurface = [0.0, 0.0, 0.0] minDistancePoint = [0.0, 0.0, 0.0] tolerance = 0.001 t = vtk.mutable(0.0) x = [0.0, 0.0, 0.0] pcoords = [0.0, 0.0, 0.0] subId = vtk.mutable(0) #print ("nPoints = %d" % (nPoints)) #print ("nCells = %d" % (nCells)) # Map surface model if skinModelNode != None: pointValue = vtk.vtkDoubleArray() pointValue.SetName("Colors") pointValue.SetNumberOfComponents(1) pointValue.SetNumberOfTuples(nPoints) pointValue.Reset() pointValue.FillComponent(0, 0.0) bspTree = obstacleBspTree cp0 = [0.0, 0.0, 0.0] cp1 = [0.0, 0.0, 0.0] cp2 = [0.0, 0.0, 0.0] accessibleArea = 0.0 inaccessibleArea = 0.0 ids = vtk.vtkIdList() minDistance = -1 #iDA = 0 #iDAFlag = 0 #d = 0 for index in range(nCells): iDA = 0 iDAFlag = 0 d = 0 i = 0 j = 0 cell = polyData.GetCell(index) if cell.GetCellType() == vtk.VTK_TRIANGLE: area = cell.ComputeArea() polyData.GetCellPoints(index, ids) polyData.GetPoint(ids.GetId(0), cp0) polyData.GetPoint(ids.GetId(1), cp1) polyData.GetPoint(ids.GetId(2), cp2) vtk.vtkTriangle.TriangleCenter(cp0, cp1, cp2, pSurface) ##### for i in range(0, tListNumber, 1): pTargetA = targetPointNode.GetMarkupPointVector(i, 0) iDA = bspTree.IntersectWithLine(pSurface, pTargetA, tolerance, t, x, pcoords, subId) if iDA >= 1: iDAFlag = 10 ##### if iDAFlag < 1: if skinModelNode != None: for j in range(0, tListNumber, 1): pTargetA = targetPointNode.GetMarkupPointVector( j, 0) d += (vtk.vtkMath.Distance2BetweenPoints( pSurface, pTargetA)) d = d / tListNumber d = math.sqrt(d) if 100 < d < 240: if d < minDistance or minDistance < 0: minDistance = d minDistancePoint = [ pSurface[0], pSurface[1], pSurface[2] ] v = d + 51 pointValue.InsertValue(ids.GetId(0), v) pointValue.InsertValue(ids.GetId(1), v) pointValue.InsertValue(ids.GetId(2), v) accessibleArea = accessibleArea + area else: v = -1.0 pointValue.InsertValue(ids.GetId(0), v) pointValue.InsertValue(ids.GetId(1), v) pointValue.InsertValue(ids.GetId(2), v) inaccessibleArea = inaccessibleArea + area else: if skinModelNode != None: v = -1.0 pointValue.InsertValue(ids.GetId(0), v) pointValue.InsertValue(ids.GetId(1), v) pointValue.InsertValue(ids.GetId(2), v) inaccessibleArea = inaccessibleArea + area else: print("ERROR: Non-triangular cell.") score = accessibleArea if skinModelNode != None: skinModelNode.AddPointScalars(pointValue) skinModelNode.SetActivePointScalars( "Colors", vtk.vtkDataSetAttributes.SCALARS) skinModelNode.Modified() displayNode = skinModelNode.GetModelDisplayNode() displayNode.SetActiveScalarName("Colors") displayNode.SetScalarRange(0.0, 200.0) return (score, minDistance, minDistancePoint)