-
Notifications
You must be signed in to change notification settings - Fork 0
/
PercutaneousApproachAnalysis.py
663 lines (566 loc) · 24.3 KB
/
PercutaneousApproachAnalysis.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
import os
import unittest
from __main__ import vtk, qt, ctk, slicer
import time
import math
#
# PercutaneousApproachAnalysis
#
class PercutaneousApproachAnalysis:
def __init__(self, parent):
parent.title = "PercutaneousApproachAnalysis" # TODO make this more human readable by adding spaces
parent.categories = ["Examples"]
parent.dependencies = []
parent.contributors = ["Jean-Christophe Fillion-Robin (Kitware), Steve Pieper (Isomics)"] # replace with "Firstname Lastname (Org)"
parent.helpText = """
This is an example of scripted loadable module bundled in an extension.
"""
parent.acknowledgementText = """
This file was originally developed by Jean-Christophe Fillion-Robin, Kitware Inc. and Steve Pieper, Isomics, Inc. and was partially funded by NIH grant 3P41RR013218-12S1.
""" # replace with organization, grant and thanks.
self.parent = parent
# Add this test to the SelfTest module's list for discovery when the module
# is created. Since this module may be discovered before SelfTests itself,
# create the list if it doesn't already exist.
try:
slicer.selfTests
except AttributeError:
slicer.selfTests = {}
slicer.selfTests['PercutaneousApproachAnalysis'] = self.runTest
def runTest(self):
tester = PercutaneousApproachAnalysisTest()
tester.runTest()
#
# PercutaneousApproachAnalysisWidget
#
class PercutaneousApproachAnalysisWidget:
def __init__(self, parent = None):
if not parent:
self.parent = slicer.qMRMLWidget()
self.parent.setLayout(qt.QVBoxLayout())
self.parent.setMRMLScene(slicer.mrmlScene)
else:
self.parent = parent
self.layout = self.parent.layout()
if not parent:
self.setup()
self.parent.show()
def setup(self):
# Instantiate and connect widgets ...
#
# Reload and Test area
#
reloadCollapsibleButton = ctk.ctkCollapsibleButton()
reloadCollapsibleButton.text = "Reload && Test"
self.layout.addWidget(reloadCollapsibleButton)
reloadFormLayout = qt.QFormLayout(reloadCollapsibleButton)
# reload button
# (use this during development, but remove it when delivering
# your module to users)
self.reloadButton = qt.QPushButton("Reload")
self.reloadButton.toolTip = "Reload this module."
self.reloadButton.name = "PercutaneousApproachAnalysis Reload"
reloadFormLayout.addWidget(self.reloadButton)
self.reloadButton.connect('clicked()', self.onReload)
# reload and test button
# (use this during development, but remove it when delivering
# your module to users)
self.reloadAndTestButton = qt.QPushButton("Reload and Test")
self.reloadAndTestButton.toolTip = "Reload this module and then run the self tests."
reloadFormLayout.addWidget(self.reloadAndTestButton)
self.reloadAndTestButton.connect('clicked()', self.onReloadAndTest)
#
# Parameters Area
#
parametersCollapsibleButton = ctk.ctkCollapsibleButton()
parametersCollapsibleButton.text = "Parameters"
self.layout.addWidget(parametersCollapsibleButton)
# Layout within the dummy collapsible button
parametersFormLayout = qt.QFormLayout(parametersCollapsibleButton)
#
# Target point (vtkMRMLMarkupsFiducialNode)
#
self.targetSelector = slicer.qMRMLNodeComboBox()
self.targetSelector.nodeTypes = ( ("vtkMRMLMarkupsFiducialNode"), "" )
self.targetSelector.addEnabled = False
self.targetSelector.removeEnabled = False
self.targetSelector.noneEnabled = True
self.targetSelector.showHidden = False
self.targetSelector.showChildNodeTypes = False
self.targetSelector.setMRMLScene( slicer.mrmlScene )
self.targetSelector.setToolTip( "Pick up the target point" )
parametersFormLayout.addRow("Target Point: ", self.targetSelector)
#
# target model (vtkMRMLScalarVolumeNode)
#
self.targetLabelSelector = slicer.qMRMLNodeComboBox()
self.targetLabelSelector.nodeTypes = ( ("vtkMRMLScalarVolumeNode"), "" )
self.targetLabelSelector.addEnabled = False
self.targetLabelSelector.removeEnabled = False
self.targetLabelSelector.noneEnabled = True
self.targetLabelSelector.showHidden = False
self.targetLabelSelector.showChildNodeTypes = False
self.targetLabelSelector.setMRMLScene( slicer.mrmlScene )
self.targetLabelSelector.setToolTip( "Pick the target label to the algorithm." )
parametersFormLayout.addRow("Target Label: ", self.targetLabelSelector)
##
## output volume selector
##
#self.outputModelSelector = slicer.qMRMLNodeComboBox()
#self.outputModelSelector.nodeTypes = ( ("vtkMRMLScalarVolumeNode"), "" )
#self.outputModelSelector.addAttribute( "vtkMRMLScalarVolumeNode", "LabelMap", 0 )
#self.outputModelSelector.selectNodeUponCreation = False
#self.outputModelSelector.addEnabled = True
#self.outputModelSelector.removeEnabled = True
#self.outputModelSelector.noneEnabled = True
#self.outputModelSelector.showHidden = False
#self.outputModelSelector.showChildNodeTypes = False
#self.outputModelSelector.setMRMLScene( slicer.mrmlScene )
#self.outputModelSelector.setToolTip( "Pick the output to the algorithm." )
#parametersFormLayout.addRow("Output Volume: ", self.outputSelector)
#
# Obstacle model (vtkMRMLModelNode)
#
self.obstacleModelSelector = slicer.qMRMLNodeComboBox()
self.obstacleModelSelector.nodeTypes = ( ("vtkMRMLModelNode"), "" )
self.obstacleModelSelector.addEnabled = False
self.obstacleModelSelector.removeEnabled = False
self.obstacleModelSelector.noneEnabled = True
self.obstacleModelSelector.showHidden = False
self.obstacleModelSelector.showChildNodeTypes = False
self.obstacleModelSelector.setMRMLScene( slicer.mrmlScene )
self.obstacleModelSelector.setToolTip( "Pick the input to the algorithm." )
parametersFormLayout.addRow("Obstacle Model: ", self.obstacleModelSelector)
##
## scale factor for Obstacle level
##
#self.ObstacleScaleFactorSliderWidget = ctk.ctkSliderWidget()
#self.ObstacleScaleFactorSliderWidget.singleStep = 1.0
#self.ObstacleScaleFactorSliderWidget.minimum = 1.0
#self.ObstacleScaleFactorSliderWidget.maximum = 50.0
#self.ObstacleScaleFactorSliderWidget.value = 1.0
#self.ObstacleScaleFactorSliderWidget.setToolTip("Set the Obstacle scale.")
#parametersFormLayout.addRow("Obstacle scale factor", self.ObstacleScaleFactorSliderWidget)
#
# Skin model (vtkMRMLModelNode)
#
self.skinModelSelector = slicer.qMRMLNodeComboBox()
self.skinModelSelector.nodeTypes = ( ("vtkMRMLModelNode"), "" )
self.skinModelSelector.addEnabled = False
self.skinModelSelector.removeEnabled = False
self.skinModelSelector.noneEnabled = True
self.skinModelSelector.showHidden = False
self.skinModelSelector.showChildNodeTypes = False
self.skinModelSelector.setMRMLScene( slicer.mrmlScene )
self.skinModelSelector.setToolTip( "Pick the input to the algorithm." )
parametersFormLayout.addRow("Skin Model: ", self.skinModelSelector)
##
## check box to trigger taking screen shots for later use in tutorials
##
#self.enableScreenshotsFlagCheckBox = qt.QCheckBox()
#self.enableScreenshotsFlagCheckBox.checked = 0
#self.enableScreenshotsFlagCheckBox.setToolTip("If checked, take screen shots for tutorials. Use Save Data to write them to disk.")
#parametersFormLayout.addRow("Enable Screenshots", self.enableScreenshotsFlagCheckBox)
##
## scale factor for screen shots
##
#self.screenshotScaleFactorSliderWidget = ctk.ctkSliderWidget()
#self.screenshotScaleFactorSliderWidget.singleStep = 1.0
#self.screenshotScaleFactorSliderWidget.minimum = 1.0
#self.screenshotScaleFactorSliderWidget.maximum = 50.0
#self.screenshotScaleFactorSliderWidget.value = 1.0
#self.screenshotScaleFactorSliderWidget.setToolTip("Set scale factor for the screen shots.")
#parametersFormLayout.addRow("Screenshot scale factor", self.screenshotScaleFactorSliderWidget)
#
# Apply Button
#
self.applyButton = qt.QPushButton("Apply")
self.applyButton.toolTip = "Run the algorithm."
self.applyButton.enabled = False
parametersFormLayout.addRow(self.applyButton)
# connections
self.applyButton.connect('clicked(bool)', self.onApplyButton)
self.targetSelector.connect("currentNodeChanged(vtkMRMLNode*)", self.onSelect)
self.targetLabelSelector.connect("currentNodeChanged(vtkMRMLNode*)", self.onSelect)
self.obstacleModelSelector.connect("currentNodeChanged(vtkMRMLNode*)", self.onSelect)
self.skinModelSelector.connect("currentNodeChanged(vtkMRMLNode*)", self.onSelect)
#
# Output Area
#
outputCollapsibleButton = ctk.ctkCollapsibleButton()
outputCollapsibleButton.text = "Output / Results"
self.layout.addWidget(outputCollapsibleButton)
# Layout within the dummy collapsible button
outputFormLayout = qt.QFormLayout(outputCollapsibleButton)
# Accessibility score results
self.accessibilityScore = qt.QLineEdit()
self.accessibilityScore.toolTip = "Accessibility Score"
self.accessibilityScore.enabled = True
self.accessibilityScore.maximumWidth = 70
self.accessibilityScore.setReadOnly(True)
self.accessibilityScore.inputMask = "0.000"
self.accessibilityScore.maxLength = 4
outputFormLayout.addRow("Accesibility Score:",self.accessibilityScore)
# Minimum distance results
self.minimumDistance = qt.QLineEdit()
self.minimumDistance.toolTip = "Minimum Distance"
self.minimumDistance.enabled = True
self.minimumDistance.maximumWidth = 70
self.minimumDistance.setReadOnly(True)
self.minimumDistance.inputMask = "0.000"
self.minimumDistance.maxLength = 4
outputFormLayout.addRow("Minimum Distance:",self.minimumDistance)
#
# Minimum distance point (vtkMRMLMarkupsFiducialNode)
#
self.minimumDistancePoint = slicer.qMRMLNodeComboBox()
self.minimumDistancePoint.nodeTypes = ( ("vtkMRMLMarkupsFiducialNode"), "" )
self.minimumDistancePoint.addEnabled = True
self.minimumDistancePoint.removeEnabled = True
self.minimumDistancePoint.noneEnabled = True
self.minimumDistancePoint.showHidden = False
self.minimumDistancePoint.showChildNodeTypes = False
self.minimumDistancePoint.setMRMLScene( slicer.mrmlScene )
self.minimumDistancePoint.setToolTip( "Display the minimum distance point" )
self.minimumDistancePoint.baseName = "PAA-MinimumDistance"
outputFormLayout.addRow("Minimum Distance Point: ", self.minimumDistancePoint)
# Add vertical spacer
self.layout.addStretch(1)
def cleanup(self):
pass
def onSelect(self):
if (self.targetSelector.currentNode() != None) and (self.targetLabelSelector.currentNode() == None) and (self.obstacleModelSelector.currentNode() != None) and (self.skinModelSelector.currentNode() != None):
self.applyButton.enabled = True
elif (self.targetSelector.currentNode() == None) and (self.targetLabelSelector.currentNode() != None) and (self.obstacleModelSelector.currentNode() != None) and (self.skinModelSelector.currentNode() != None):
self.applyButton.enabled = True
else:
self.applyButton.enabled = False
def onApplyButton(self):
logic = PercutaneousApproachAnalysisLogic()
print("onApplyButton() is called ")
obstacleModel = self.obstacleModelSelector.currentNode()
skinModel = self.skinModelSelector.currentNode()
if self.targetLabelSelector.currentNode() != None:
print("label ")
targetLabel = self.targetLabelSelector.currentNode()
start = time.time()
logic.runLabelWise(targetLabel, obstacleModel, skinModel)
end = time.time()
print end - start
else:
print("point")
targetPoint = self.targetSelector.currentNode()
start = time.time()
(score, mind, mindp) = logic.runPointWise(targetPoint, obstacleModel, skinModel)
self.accessibilityScore.text = score
self.minimumDistance.text = mind
markupNode = self.minimumDistancePoint.currentNode()
if markupNode != None:
markupNode.RemoveAllMarkups()
displayNode = markupNode.GetDisplayNode()
if displayNode != None:
# Change these values to modified fiducial and text size
displayNode.SetGlyphScale(0.5)
displayNode.SetTextScale(0.5)
vtkMinDistPoint = vtk.vtkVector3d(mindp)
markupNode.AddPointToNewMarkup(vtkMinDistPoint)
end = time.time()
print end - start
def onReload(self,moduleName="PercutaneousApproachAnalysis"):
"""Generic reload method for any scripted module.
ModuleWizard will subsitute correct default moduleName.
"""
import imp, sys, os, slicer
widgetName = moduleName + "Widget"
# reload the source code
# - set source file path
# - load the module to the global space
filePath = eval('slicer.modules.%s.path' % moduleName.lower())
p = os.path.dirname(filePath)
if not sys.path.__contains__(p):
sys.path.insert(0,p)
fp = open(filePath, "r")
globals()[moduleName] = imp.load_module(
moduleName, fp, filePath, ('.py', 'r', imp.PY_SOURCE))
fp.close()
# rebuild the widget
# - find and hide the existing widget
# - create a new widget in the existing parent
parent = slicer.util.findChildren(name='%s Reload' % moduleName)[0].parent().parent()
for child in parent.children():
try:
child.hide()
except AttributeError:
pass
# Remove spacer items
item = parent.layout().itemAt(0)
while item:
parent.layout().removeItem(item)
item = parent.layout().itemAt(0)
# delete the old widget instance
if hasattr(globals()['slicer'].modules, widgetName):
getattr(globals()['slicer'].modules, widgetName).cleanup()
# create new widget inside existing parent
globals()[widgetName.lower()] = eval(
'globals()["%s"].%s(parent)' % (moduleName, widgetName))
globals()[widgetName.lower()].setup()
setattr(globals()['slicer'].modules, widgetName, globals()[widgetName.lower()])
def onReloadAndTest(self,moduleName="PercutaneousApproachAnalysis"):
try:
self.onReload()
evalString = 'globals()["%s"].%sTest()' % (moduleName, moduleName)
tester = eval(evalString)
tester.runTest()
except Exception, e:
import traceback
traceback.print_exc()
qt.QMessageBox.warning(slicer.util.mainWindow(),
"Reload and Test", 'Exception!\n\n' + str(e) + "\n\nSee Python Console for Stack Trace")
#
# PercutaneousApproachAnalysisLogic
#
class PercutaneousApproachAnalysisLogic:
"""This class should implement all the actual
computation done by your module. The interface
should be such that other python code can import
this class and make use of the functionality without
requiring an instance of the Widget
"""
def __init__(self):
pass
def hasImageData(self,volumeNode):
"""This is a dummy logic method that
returns true if the passed in volume
node has valid image data
"""
if not volumeNode:
print('no volume node')
return False
if volumeNode.GetImageData() == None:
print('no image data')
return False
return True
def delayDisplay(self,message,msec=1000):
#
# logic version of delay display
#
print(message)
self.info = qt.QDialog()
self.infoLayout = qt.QVBoxLayout()
self.info.setLayout(self.infoLayout)
self.label = qt.QLabel(message,self.info)
self.infoLayout.addWidget(self.label)
qt.QTimer.singleShot(msec, self.info.close)
self.info.exec_()
def takeScreenshot(self,name,description,type=-1):
# show the message even if not taking a screen shot
self.delayDisplay(description)
if self.enableScreenshots == 0:
return
lm = slicer.app.layoutManager()
# switch on the type to get the requested window
widget = 0
if type == -1:
# full window
widget = slicer.util.mainWindow()
elif type == slicer.qMRMLScreenShotDialog().FullLayout:
# full layout
widget = lm.viewport()
elif type == slicer.qMRMLScreenShotDialog().ThreeD:
# just the 3D window
widget = lm.threeDWidget(0).threeDView()
elif type == slicer.qMRMLScreenShotDialog().Red:
# red slice window
widget = lm.sliceWidget("Red")
elif type == slicer.qMRMLScreenShotDialog().Yellow:
# yellow slice window
widget = lm.sliceWidget("Yellow")
elif type == slicer.qMRMLScreenShotDialog().Green:
# green slice window
widget = lm.sliceWidget("Green")
# grab and convert to vtk image data
qpixMap = qt.QPixmap().grabWidget(widget)
qimage = qpixMap.toImage()
imageData = vtk.vtkImageData()
slicer.qMRMLUtils().qImageToVtkImageData(qimage,imageData)
annotationLogic = slicer.modules.annotations.logic()
annotationLogic.CreateSnapShot(name, description, type, self.screenshotScaleFactor, imageData)
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 runLabelWise(self, targetLabelNode, obstacleModelNode, skinModelNode):
"""
Run label-wise analysis
"""
poly = skinModelNode.GetPolyData()
polyDataNormals = vtk.vtkPolyDataNormals()
polyDataNormals.SetInput(poly)
polyDataNormals.ComputeCellNormalsOn()
polyDataNormals.Update()
polyData = polyDataNormals.GetOutput()
bspTree = vtk.vtkModifiedBSPTree()
bspTree.SetDataSet(obstacleModelNode.GetPolyData())
bspTree.BuildLocator()
trans = vtk.vtkMatrix4x4()
targetLabelNode.GetIJKToRASMatrix(trans)
pos = [0.0, 0.0, 0.0, 0.0]
imageData = targetLabelNode.GetImageData()
(x0, x1, y0, y1, z0, z1) = imageData.GetExtent()
for z in range(z0, z1+1):
for y in range(y0, y1+1):
for x in range(x0, x1+1):
if imageData.GetScalarComponentAsDouble(x, y, z, 0) > 0:
trans.MultiplyPoint([x, y, z, 1.0], pos);
(score, mind, mindp) = self.calcApproachScore(pos[0:3], polyData, bspTree, None)
imageData.SetScalarComponentFromDouble(x, y, z, 0, score*100.0+1.0)
#print ("Index(%f, %f, %f) -> RAS(%f, %f, %f)" % (x, y, z, pos[0], pos[1], pos[2]))
#print ("Approach Score (<accessible area> / (<accessible area> + <inaccessible area>)) = %f" % (score))
return True
def runPointWise(self, targetPointNode, obstacleModelNode, skinModelNode):
"""
Run point-wise analysis
"""
print ("runPointWise()")
tPoint = targetPointNode.GetMarkupPointVector(0, 0)
pTarget = [tPoint[0], tPoint[1], tPoint[2]]
poly = skinModelNode.GetPolyData()
polyDataNormals = vtk.vtkPolyDataNormals()
polyDataNormals.SetInput(poly)
polyDataNormals.ComputeCellNormalsOn()
polyDataNormals.Update()
polyData = polyDataNormals.GetOutput()
bspTree = vtk.vtkModifiedBSPTree()
bspTree.SetDataSet(obstacleModelNode.GetPolyData())
bspTree.BuildLocator()
(score, mind, mindp) = self.calcApproachScore(pTarget, polyData, bspTree, skinModelNode)
print ("Approach Score (<accessible area> / (<accessible area> + <inaccessible area>)) = %f" % (score))
print ("Minmum Distance = %f" % (mind))
return (score, mind, mindp)
class PercutaneousApproachAnalysisTest(unittest.TestCase):
"""
This is the test case for your scripted module.
"""
def delayDisplay(self,message,msec=1000):
"""This utility method displays a small dialog and waits.
This does two things: 1) it lets the event loop catch up
to the state of the test so that rendering and widget updates
have all taken place before the test continues and 2) it
shows the user/developer/tester the state of the test
so that we'll know when it breaks.
"""
print(message)
self.info = qt.QDialog()
self.infoLayout = qt.QVBoxLayout()
self.info.setLayout(self.infoLayout)
self.label = qt.QLabel(message,self.info)
self.infoLayout.addWidget(self.label)
qt.QTimer.singleShot(msec, self.info.close)
self.info.exec_()
def setUp(self):
""" Do whatever is needed to reset the state - typically a scene clear will be enough.
"""
slicer.mrmlScene.Clear(0)
def runTest(self):
"""Run as few or as many tests as needed here.
"""
self.setUp()
self.test_PercutaneousApproachAnalysis1()
def test_PercutaneousApproachAnalysis1(self):
""" Ideally you should have several levels of tests. At the lowest level
tests sould exercise the functionality of the logic with different inputs
(both valid and invalid). At higher levels your tests should emulate the
way the user would interact with your code and confirm that it still works
the way you intended.
One of the most important features of the tests is that it should alert other
developers when their changes will have an impact on the behavior of your
module. For example, if a developer removes a feature that you depend on,
your test should break so they know that the feature is needed.
"""
self.delayDisplay("Starting the test")
#
# first, get some data
#
import urllib
downloads = (
('http://slicer.kitware.com/midas3/download?items=5767', 'FA.nrrd', slicer.util.loadVolume),
)
for url,name,loader in downloads:
filePath = slicer.app.temporaryPath + '/' + name
if not os.path.exists(filePath) or os.stat(filePath).st_size == 0:
print('Requesting download %s from %s...\n' % (name, url))
urllib.urlretrieve(url, filePath)
if loader:
print('Loading %s...\n' % (name,))
loader(filePath)
self.delayDisplay('Finished with download and loading\n')
volumeNode = slicer.util.getNode(pattern="FA")
logic = PercutaneousApproachAnalysisLogic()
self.assertTrue( logic.hasImageData(volumeNode) )
self.delayDisplay('Test passed!')