/
VascularWall.py
301 lines (231 loc) · 8.91 KB
/
VascularWall.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# Locating coordinates of vascular wall
import vtk
import qt
import ctk
import slicer
from slicer.ScriptedLoadableModule import *
import logging
logging.basicConfig(level=logging.INFO)
import numpy as np
#
# VascularWall
#
class VascularWall(ScriptedLoadableModule):
def __init__(self, parent):
ScriptedLoadableModule.__init__(self, parent)
self.parent.title = "Vascular Wall"
self.parent.categories = ['Examples']
self.dependencies = []
self.parent.contributors = ["Quentan Qi (University of Hull)"]
self.parent.helpText = """
Locating coordinates of vascular wall by initialising a sphere of vessel.
"""
self.parent.acknowledgementText = """
Thanks to tutorials.
"""
#
# VascularWallWidget
#
class VascularWallWidget(ScriptedLoadableModuleWidget):
def setup(self):
ScriptedLoadableModuleWidget.setup(self)
#
# Parameters Area
#
parameterCollapsibleButton = ctk.ctkCollapsibleButton()
parameterCollapsibleButton.text = 'Parameters'
self.layout.addWidget(parameterCollapsibleButton)
# Layout with the collapsible button
parameterFormLayout = qt.QFormLayout(parameterCollapsibleButton)
#
# Input volume selector
#
self.volumeSelector = slicer.qMRMLNodeComboBox()
self.volumeSelector.nodeTypes = ("vtkMRMLScalarVolumeNode", "")
self.volumeSelector.noneEnabled = False
self.volumeSelector.selectNodeUponCreation = True
self.volumeSelector.setMRMLScene(slicer.mrmlScene)
self.volumeSelector.setToolTip("Select input volume")
parameterFormLayout.addRow("Input Volume: ", self.volumeSelector)
#
# Ruler selector
#
self.rulerSelector = slicer.qMRMLNodeComboBox()
self.rulerSelector.nodeTypes = ("vtkMRMLAnnotationRulerNode", "")
self.rulerSelector.noneEnabled = False
self.rulerSelector.selectNodeUponCreation = True
self.rulerSelector.setMRMLScene(slicer.mrmlScene)
self.rulerSelector.setToolTip("Pick the ruler to make a sphere")
parameterFormLayout.addRow("Ruler: ", self.rulerSelector)
#
# Show/Hide sphere ChechBox
#
self.showCheckBox = qt.QCheckBox("Show/Hide Sphere")
# self.showCheckBox.setToolTip("Show or hide the sphere")
self.showCheckBox.toolTip = "Show or hide the sphere"
#
# Apply Button
#
self.applyButton = qt.QPushButton("Apply")
self.applyButton.toolTip = "Generate a sphere according to the sphere"
self.applyButton.enabled = True
# parameterFormLayout.addWidget(self.applyButton)
# parameterFormLayout.addRow("A test label:", self.applyButton)
parameterFormLayout.addRow(self.showCheckBox, self.applyButton)
#
# Results
#
self.currentCenterLabel = qt.QLabel()
self.currentCenterLabel.setText("Current Centre: ")
self.currentCenterCoord = qt.QLabel()
parameterFormLayout.addRow(
self.currentCenterLabel, self.currentCenterCoord)
self.currentRadiusLabel = qt.QLabel()
self.currentRadiusLabel.setText("Current Radius: ")
self.currentRadiusLength = qt.QLabel()
parameterFormLayout.addRow(
self.currentRadiusLabel, self.currentRadiusLength)
#
# Connections
#
self.showCheckBox.connect('toggled(bool)', self.onShowCheckBoxToggled)
self.applyButton.connect('clicked()', self.onApplyButtonClicked)
self.volumeSelector.connect(
'currentNodeChanged(vtkMRMLNode*)', self.onSelect)
# Add vertical spacer
self.layout.addStretch(1)
# Refresh applyButton state
self.onSelect()
def cleanup(self):
pass
def onSelect(self):
self.applyButton.enabled = self.volumeSelector.currentNode()
def onShowCheckBoxToggled(self):
logging.info("Toggled status of showCheckBox - %s" %
self.showCheckBox.isChecked())
if self.showCheckBox.isChecked():
self.modelDisplay.SetVisibility(True)
else:
self.modelDisplay.SetVisibility(False)
def onApplyButtonClicked(self):
"""
Real algorithm should be in class VascularWallLogic.
"""
logging.info("applyButton is clicked.")
# Create models for sphere
self.sphere = vtk.vtkSphereSource()
# Model node
model = slicer.vtkMRMLModelNode()
model.SetAndObservePolyData(self.sphere.GetOutput())
# Display node
self.modelDisplay = slicer.vtkMRMLModelDisplayNode()
self.modelDisplay.SetSliceIntersectionVisibility(True)
self.modelDisplay.SetVisibility(self.showCheckBox.isChecked())
self.modelDisplay.SetOpacity(0.5)
self.modelDisplay.SetRepresentation(1)
slicer.mrmlScene.AddNode(self.modelDisplay)
model.SetAndObserveDisplayNodeID(self.modelDisplay.GetID())
# Add to scene
self.modelDisplay.SetInputPolyDataConnection(
model.GetPolyDataConnection())
slicer.mrmlScene.AddNode(model)
# Callback
self.UpdateSphere(0, 0)
self.rulerSelector.currentNode().AddObserver(
'ModifiedEvent', self.UpdateSphere)
def UpdateSphere(self, obj, event):
logic = VascularWallLogic(self.rulerSelector.currentNode())
centralPoint = logic.getCentralPoint()
radius = logic.getRadius()
self.currentCenterCoord.setText([round(n, 2) for n in centralPoint])
self.currentRadiusLength.setText(round(radius, 2))
self.sphere.SetCenter(centralPoint)
self.sphere.SetRadius(radius)
self.sphere.SetPhiResolution(30)
self.sphere.SetThetaResolution(30)
self.sphere.Update()
#
# Logic
#
class VascularWallLogic(ScriptedLoadableModuleLogic):
def __init__(self, rulerNode):
self.info = {} # Info of the rulerNode
if rulerNode:
startPoint = [0.0] * 3 # central point
endPoint = [0.0] * 3
radius = 0.0
rulerNode.GetPosition1(startPoint)
rulerNode.GetPosition2(endPoint)
radius = rulerNode.GetDistanceMeasurement()
self.info['startPoint'] = startPoint
self.info['endPoint'] = endPoint
self.info['radius'] = radius
self.rulerNode = rulerNode
def hasSphereParameter(self, rulerNode):
if not rulerNode:
logging.debug(
"hasSphereParameter failed: no central point and radius in ruler node!")
return False
return True
def hasImageData(self, volumeNode):
if not volumeNode:
logging.debug("hasImageData failed: no volume node!")
return False
if volumeNode.GetImageData() is None:
logging.debug("hasImageData failed: no image data in volume node")
return False
return True
def isValidInputData(self, inputVolumeNode):
if not inputVolumeNode:
logging.debug(
"isValidInputData failed: no input volume node defined!")
return False
return True
def run(self, volumeNode, rulerNode):
logging.info("VascularWallLogic.run() is called!")
def getCentralPoint(self):
if self.hasSphereParameter(self.rulerNode):
return self.info['startPoint']
def getRadius(self):
if self.hasSphereParameter(self.rulerNode):
return self.info['radius']
def getSphere(self):
sphere = vtk.vtkSphereSource()
centerPoint = self.getCentralPoint()
radius = self.getRadius()
sphere.SetCenter(centerPoint)
sphere.SetRadius(radius)
return sphere
def getImplicitSphere(self):
impSphere = vtk.vtkSphere() # implicit sphere
centerPoint = self.getCentralPoint()
radius = self.getRadius()
impSphere.SetRadius(radius)
impSphere.SetCenter(centerPoint)
return impSphere
def extract(self, volumeNode):
if not self.isValidInputData(volumeNode):
logging.error("No input volume!")
return
imageData = volumeNode.GetImageData()
impSphere = self.getImplicitSphere()
extract = vtk.vtkExtractGeometry()
extract.SetInputData(imageData)
extract.SetImplicitFunction(impSphere)
dataMapper = vtk.vtkDataSetMapper()
dataMapper.SetInputConnection(extract.GetOutputPort())
#
# Test Case
#
class VascularWallTest(ScriptedLoadableModuleTest):
def setUp(self):
slicer.mrmlScene.Clear(0)
def runTest(self):
self.setUp()
self.test1_VascularWall()
def test1_VascularWall(self):
self.delayDisplay("Starting test")
# Get data here
self.delayDisplay("Finished with download and loading")
# Do testing code here
self.delayDisplay("Testing finished.")