Skip to content

Commit

Permalink
Improve CenterlineDisassembly logic.
Browse files Browse the repository at this point in the history
CenterlineDisassembly

  - return the split centerline
  - handle an input centerline that does not have EdgeArray and/or EdgePCoordArray
  - do a NULL check
  - get number of branches, bifurcations and centerlines.
  • Loading branch information
chir-set committed Jan 5, 2025
1 parent 5bdf9f2 commit 138a1d6
Showing 1 changed file with 74 additions and 25 deletions.
99 changes: 74 additions & 25 deletions CenterlineDisassembly/CenterlineDisassembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def splitCenterlines(self, inputCenterline: slicer.vtkMRMLModelNode):

if not inputCenterline:
raise ValueError(_("Input centerline is invalid"))

import vtkvmtkComputationalGeometryPython as vtkvmtkComputationalGeometry

branchExtractor = vtkvmtkComputationalGeometry.vtkvmtkCenterlineBranchExtractor()
Expand All @@ -349,15 +349,53 @@ def splitCenterlines(self, inputCenterline: slicer.vtkMRMLModelNode):
branchExtractor.SetTractIdsArrayName(tractIdsArrayName)
branchExtractor.Update()
self._splitCenterlines = branchExtractor.GetOutput()

return self._splitCenterlines

def getNumberOfCenterlines(self):
if not self._splitCenterlines:
raise ValueError(_("Call 'splitCenterlines()' with an input centerline model first."))

import vtkvmtkComputationalGeometryPython as vtkvmtkComputationalGeometry
centerlineIdsArray = self._splitCenterlines.GetCellData().GetArray(centerlineIdsArrayName)
centerlineIdsValueRange = centerlineIdsArray.GetValueRange()
# centerlineIdsValueRange[0] is always seen as 0.
return (centerlineIdsValueRange[1] - centerlineIdsValueRange[0]) + 1

def getNumberOfBifurcations(self):
# Logical bifurcations, not by anatomy.
if not self._splitCenterlines:
raise ValueError(_("Call 'splitCenterlines()' with an input centerline model first."))

groupIdsArray = vtk.vtkIdList()
import vtkvmtkComputationalGeometryPython as vtkvmtkComputationalGeometry
centerlineUtilities = vtkvmtkComputationalGeometry.vtkvmtkCenterlineUtilities()
centerlineUtilities.GetBlankedGroupsIdList(self._splitCenterlines, groupIdsArrayName,
blankingArrayName, groupIdsArray)
return groupIdsArray.GetNumberOfIds()

def getNumberOfBranches(self):
# Logical branches, not by anatomy.
if not self._splitCenterlines:
raise ValueError(_("Call 'splitCenterlines()' with an input centerline model first."))

groupIdsArray = vtk.vtkIdList()
import vtkvmtkComputationalGeometryPython as vtkvmtkComputationalGeometry
centerlineUtilities = vtkvmtkComputationalGeometry.vtkvmtkCenterlineUtilities()
centerlineUtilities.GetNonBlankedGroupsIdList(self._splitCenterlines, groupIdsArrayName,
blankingArrayName, groupIdsArray)
return groupIdsArray.GetNumberOfIds()

def _createPolyData(self, cellIds):
if not self._splitCenterlines:
raise ValueError(_("Call 'splitCenterlines()' with an input centerline model first."))

masterRadiusArray = self._splitCenterlines.GetPointData().GetArray(radiusArrayName)
masterEdgeArray = self._splitCenterlines.GetPointData().GetArray(edgeArrayName)
masterEdgePCoordArray = self._splitCenterlines.GetPointData().GetArray(edgePCoordArrayName)

resultPolyDatas = [] # One per cell
nbIds = cellIds.GetNumberOfIds() # Number of cells

for i in range(nbIds): # For every cell
unitCellPolyData = None
pointId = 0
Expand All @@ -366,11 +404,13 @@ def _createPolyData(self, cellIds):
cellArray = vtk.vtkCellArray()
radiusArray = vtk.vtkDoubleArray()
radiusArray.SetName(radiusArrayName)
edgeArray = vtk.vtkDoubleArray()
edgeArray.SetName("EdgeArray")
edgeArray.SetNumberOfComponents(2)
edgePCoordArray = vtk.vtkDoubleArray()
edgePCoordArray.SetName("EdgePCoordArray")
if masterEdgeArray:
edgeArray = vtk.vtkDoubleArray()
edgeArray.SetName("EdgeArray")
edgeArray.SetNumberOfComponents(2)
if masterEdgePCoordArray:
edgePCoordArray = vtk.vtkDoubleArray()
edgePCoordArray.SetName("EdgePCoordArray")

masterCellId = cellIds.GetId(i)
masterCellPolyLine = self._splitCenterlines.GetCell(masterCellId)
Expand All @@ -384,18 +424,22 @@ def _createPolyData(self, cellIds):
points.InsertNextPoint(point)
cellArray.InsertCellPoint(pointId)
radiusArray.InsertNextValue(masterRadiusArray.GetValue(masterPointId))
edgeArray.InsertNextTuple2(masterEdgeArray.GetTuple2(masterPointId)[0],
masterEdgeArray.GetTuple2(masterPointId)[1])
edgePCoordArray.InsertNextValue(masterEdgePCoordArray.GetValue(masterPointId))
if masterEdgeArray:
edgeArray.InsertNextTuple2(masterEdgeArray.GetTuple2(masterPointId)[0],
masterEdgeArray.GetTuple2(masterPointId)[1])
if masterEdgePCoordArray:
edgePCoordArray.InsertNextValue(masterEdgePCoordArray.GetValue(masterPointId))
pointId = pointId + 1

if (numberOfMasterCellPointIds):
unitCellPolyData = vtk.vtkPolyData()
unitCellPolyData.SetPoints(points)
unitCellPolyData.SetLines(cellArray)
unitCellPolyData.GetPointData().AddArray(radiusArray)
unitCellPolyData.GetPointData().AddArray(edgeArray)
unitCellPolyData.GetPointData().AddArray(edgePCoordArray)
if masterEdgeArray:
unitCellPolyData.GetPointData().AddArray(edgeArray)
if masterEdgePCoordArray:
unitCellPolyData.GetPointData().AddArray(edgePCoordArray)
resultPolyDatas.append(unitCellPolyData)
return resultPolyDatas

Expand Down Expand Up @@ -442,11 +486,13 @@ def createCenterlineCurve(self, centerlinePolyData, curveNode):
cellArray = vtk.vtkCellArray()
radiusArray = vtk.vtkDoubleArray()
radiusArray.SetName(radiusArrayName)
edgeArray = vtk.vtkDoubleArray()
edgeArray.SetName("EdgeArray")
edgeArray.SetNumberOfComponents(2)
edgePCoordArray = vtk.vtkDoubleArray()
edgePCoordArray.SetName("EdgePCoordArray")
if masterEdgeArray:
edgeArray = vtk.vtkDoubleArray()
edgeArray.SetName("EdgeArray")
edgeArray.SetNumberOfComponents(2)
if masterEdgePCoordArray:
edgePCoordArray = vtk.vtkDoubleArray()
edgePCoordArray.SetName("EdgePCoordArray")

# The new cell array must allocate for points of all input cells.
cellArray.InsertNextCell(centerlinePolyData.GetNumberOfPoints())
Expand All @@ -463,9 +509,11 @@ def createCenterlineCurve(self, centerlinePolyData, curveNode):
points.InsertNextPoint(point)
cellArray.InsertCellPoint(pointId)
radiusArray.InsertNextValue(masterRadiusArray.GetValue(masterPointId))
edgeArray.InsertNextTuple2(masterEdgeArray.GetTuple2(masterPointId)[0],
masterEdgeArray.GetTuple2(masterPointId)[1])
edgePCoordArray.InsertNextValue(masterEdgePCoordArray.GetValue(masterPointId))
if masterEdgeArray:
edgeArray.InsertNextTuple2(masterEdgeArray.GetTuple2(masterPointId)[0],
masterEdgeArray.GetTuple2(masterPointId)[1])
if masterEdgePCoordArray:
edgePCoordArray.InsertNextValue(masterEdgePCoordArray.GetValue(masterPointId))
pointId = pointId + 1

# All cells from the input centerline have been processed.
Expand All @@ -474,9 +522,10 @@ def createCenterlineCurve(self, centerlinePolyData, curveNode):
newPolyData.SetPoints(points)
newPolyData.SetLines(cellArray)
newPolyData.GetPointData().AddArray(radiusArray)
newPolyData.GetPointData().AddArray(edgeArray)
newPolyData.GetPointData().AddArray(edgePCoordArray)

if masterEdgeArray:
newPolyData.GetPointData().AddArray(edgeArray)
if masterEdgePCoordArray:
newPolyData.GetPointData().AddArray(edgePCoordArray)

if curveNode and newPolyData:
import ExtractCenterline
Expand Down Expand Up @@ -530,8 +579,8 @@ def processGroupIds(self, bifurcations):
logging.info(_("Processing group ids started"))

groupIdsPolyDatas = []
import vtkvmtkComputationalGeometryPython as vtkvmtkComputationalGeometry
groupIdsArray = vtk.vtkIdList()
import vtkvmtkComputationalGeometryPython as vtkvmtkComputationalGeometry
centerlineUtilities = vtkvmtkComputationalGeometry.vtkvmtkCenterlineUtilities()
if (bifurcations):
# Blanked
Expand Down

0 comments on commit 138a1d6

Please sign in to comment.