Hi all
I would like to track a specific transform matrix and change the Qt label to ‘on’ when it changing and to ‘off’ when it doesn’t.
I’ve implemented the code below, but is there a more efficient way?
I’m particularly concerned about the while loop causing performance issues in the program.
Thanks
ReferenceToTrackerNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLinearTransformNode", "ReferenceToTracker")
TranCenterToRefernceNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLinearTransformNode", "TranCenterToReferenc")
StylusTipToReferenceNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLinearTransformNode", "StylusTipToReference")
needleNode.SetAndObserveTransformNodeID(StylusTipToReferenceNode.GetID())
self.lastMatrix = [np.zeros((4, 4)), np.zeros((4, 4)), np.zeros((4, 4))]
transformMatrix = vtk.vtkMatrix4x4()
def checkTransformNode(node, lastMatrix):
name = node.GetName()
node.GetMatrixTransformToWorld(transformMatrix)
matrix_arr_origin = np.zeros((4, 4))
for i in range(0, 4):
for j in range(0, 4):
matrix_arr_origin[i, j] = (transformMatrix.GetElement(i, j))
if np.array_equal(lastMatrix, matrix_arr_origin):
if name== "ReferenceToTracker":
self.ui.referenceLabel.setText("Reference Off")
self.ui.referenceLabel.setStyleSheet("color: #FA8072; width: 15px; ")
elif name== "TranCenterToReferenc":
self.ui.transducerLabel.setText("Transducer Off")
self.ui.transducerLabel.setStyleSheet("color: #FA8072; width: 15px; ")
elif name== "StylusTipToReference":
self.ui.stylusLabel.setText("Stylus Off")
self.ui.stylusLabel.setStyleSheet("color: #FA8072; width: 15px; ")
else:
if name== "ReferenceToTracker":
self.ui.referenceLabel.setText("Reference On")
self.ui.referenceLabel.setStyleSheet("color: #4D69E8; width: 15px; ")
elif name== "TranCenterToReferenc":
self.ui.transducerLabel.setText("Transducer On")
self.ui.transducerLabel.setStyleSheet("color: #4D69E8; width: 15px; ")
elif name== "StylusTipToReference":
self.ui.stylusLabel.setText("Stylus On")
self.ui.stylusLabel.setStyleSheet("color: #4D69E8; width: 15px; ")
# save current matrix
if name== "ReferenceToTracker":
self.lastMatrix[0] = matrix_arr_origin
elif name== "TranCenterToReferenc":
self.lastMatrix[1] = matrix_arr_origin
elif name== "StylusTipToReference":
self.lastMatrix[2] = matrix_arr_origin
timer = QTimer()
timer.timeout.connect(lambda: checkTransformNode(ReferenceToTrackerNode, self.lastMatrix[0]))
timer.timeout.connect(lambda: checkTransformNode(TranCenterToRefernceNode, self.lastMatrix[1]))
timer.timeout.connect(lambda: checkTransformNode(StylusTipToReferenceNode, self.lastMatrix[2]))
timer.start(100)
# start event loop
while True:
slicer.app.processEvents()