-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarkupsInfo.py
118 lines (91 loc) · 3.9 KB
/
MarkupsInfo.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
# This is a very good example to learn Python script in Slicer!
from __main__ import qt, slicer
import math
#
# MarkupsInfo module
#
class MarkupsInfo:
def __init__(self, parent):
import string
parent.title = "Markups info"
# parent.categories = ["Informatics"]
parent.categories = ["Examples"]
parent.contributors = ["Andras Lasso (PerkLab)"]
parent.helpText = string.Template("""
Use this module to get information about markups. The only metric it computes now is the total distance between the markup points in the selected list.
""").substitute({ 'a': parent.slicerWikiUrl, 'b': slicer.app.majorVersion, 'c': slicer.app.minorVersion })
parent.acknowledgementText = """
Supported by SparKit and the Slicer Community. See http://www.slicerrt.org for details.
"""
self.parent = parent
#
# Widget
#
class MarkupsInfoWidget:
def __init__(self, parent=None):
self.parent = parent
self.logic = None
def setup(self):
frame = qt.QFrame()
layout = qt.QFormLayout()
frame.setLayout(layout)
self.parent.layout().addWidget(frame)
# Markup selector
self.markupSelectorLabel = qt.QLabel()
self.markupSelectorLabel.setText("Markup list: ")
self.markupSelector = slicer.qMRMLNodeComboBox()
self.markupSelector.nodeTypes = ("vtkMRMLMarkupsFiducialNode", "")
self.markupSelector.noneEnabled = False
self.markupSelector.selectNodeUponCreation = True
self.markupSelector.setMRMLScene(slicer.mrmlScene)
self.markupSelector.setToolTip("Pick the markup list to be filled")
layout.addRow(self.markupSelectorLabel, self.markupSelector)
# Apply button
self.computeButton = qt.QPushButton("Compute")
self.computeButton.toolTip = "Compute information for the selected markup"
layout.addWidget(self.computeButton)
self.UpdatecomputeButtonState()
# Results
self.totalDistanceLabel = qt.QLabel()
self.totalDistanceLabel.setText(
"Total distance between fiducials (mm): ")
self.totalDistanceValue = qt.QLabel()
layout.addRow(self.totalDistanceLabel, self.totalDistanceValue)
# connections
self.computeButton.connect('clicked()', self.onCompute)
self.markupSelector.connect(
'currentNodeChanged(vtkMRMLNode*)', self.onMarkupSelect)
def UpdatecomputeButtonState(self):
if not self.markupSelector.currentNode():
self.computeButton.enabled = False
else:
self.computeButton.enabled = True
def onMarkupSelect(self, node):
self.UpdatecomputeButtonState()
def onCompute(self):
slicer.app.processEvents()
self.logic = MarkupsInfoLogic(self.markupSelector.currentNode())
self.totalDistanceValue.setText(
'%.2f' % self.logic.info['totalDistance'])
#
# Logic
#
class MarkupsInfoLogic:
"""Implement the logic to compute markup info
Nodes are passed in as arguments.
Results are stored as 'info' instance variable.
"""
def __init__(self, markupNode):
self.info = {}
# Compute total distance between fiducials
totalDist = 0
startPtCoords = [0.0, 0.0, 0.0]
endPtCoords = [0.0, 0.0, 0.0]
for fidIndex in xrange(markupNode.GetNumberOfFiducials()-1):
markupNode.GetNthFiducialPosition(fidIndex, startPtCoords)
markupNode.GetNthFiducialPosition(fidIndex+1, endPtCoords)
dist = math.sqrt((startPtCoords[0]-endPtCoords[0])**2 +
(startPtCoords[1]-endPtCoords[1])**2 +
(startPtCoords[2]-endPtCoords[2])**2)
totalDist = totalDist+dist
self.info['totalDistance'] = totalDist