/
SoundGuidance.py
645 lines (511 loc) · 23.8 KB
/
SoundGuidance.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
import os
import unittest
import vtk, qt, ctk, slicer
from slicer.ScriptedLoadableModule import *
import logging
import math
import time
import SimpleITK
import numpy as np
import OSC
import numpy.linalg
#from sympy import Plane, Point, Point3D
#
# SoundGuidance
#
class SoundGuidance(ScriptedLoadableModule):
"""Uses ScriptedLoadableModule base class, available at:
https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
"""
def __init__(self, parent):
ScriptedLoadableModule.__init__(self, parent)
self.parent.title = "SoundGuidance" # TODO make this more human readable by adding spaces
self.parent.categories = ["Examples"]
self.parent.dependencies = []
self.parent.contributors = ["John Doe (AnyWare Corp.)"] # replace with "Firstname Lastname (Organization)"
self.parent.helpText = """
This is an example of scripted loadable module bundled in an extension.
It performs a simple thresholding on the input volume and optionally captures a screenshot.
"""
self.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.
#
# SoundGuidanceWidget
#
class SoundGuidanceWidget(ScriptedLoadableModuleWidget):
"""Uses ScriptedLoadableModuleWidget base class, available at:
https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
"""
def setup(self):
ScriptedLoadableModuleWidget.setup(self)
self.logic = SoundGuidanceLogic()
# Instantiate and connect widgets ...
self.defaultStyleSheet = "QLabel { color : #000000; \
font: bold 14px}"
# Load models first
self.SoundGuidanceModuleModelsPath = slicer.modules.soundguidance.path.replace("SoundGuidance.py","") + 'Resources/Models/'
self.boxModel = slicer.util.getNode('boxModel')
if not self.boxModel:
slicer.util.loadModel(self.SoundGuidanceModuleModelsPath + 'box.stl')
self.boxModel = slicer.util.getNode(pattern="box")
self.boxModelDisplay=self.boxModel.GetModelDisplayNode()
self.boxModelDisplay.SetColor([0.098,0.5,0.42])
self.boxModelDisplay.SetOpacity(0.3)
self.aroModel = slicer.util.getNode('aroModel')
if not self.aroModel:
slicer.util.loadModel(self.SoundGuidanceModuleModelsPath + 'aro.stl')
self.aroModel = slicer.util.getNode(pattern="aro")
self.aroModelDisplay=self.aroModel.GetModelDisplayNode()
self.aroModelDisplay.SetColor([0.063,0.14,0.5])
#We create models
self.needleModel = slicer.util.getNode('NeedleModel')
if not self.needleModel:
slicer.util.loadModel(self.SoundGuidanceModuleModelsPath + 'NeedleModel.stl')
self.needleModel = slicer.util.getNode(pattern="NeedleModel")
self.needleModelDisplay=self.needleModel.GetModelDisplayNode()
self.needleModelDisplay.SetColor([0,1,1])
self.pointerModel = slicer.util.getNode('PointerModel')
if not self.pointerModel:
slicer.util.loadModel(self.SoundGuidanceModuleModelsPath + 'PointerModel.stl')
self.pointerModel = slicer.util.getNode(pattern="PointerModel")
self.pointerModelDisplay=self.pointerModel.GetModelDisplayNode()
self.pointerModelDisplay.SetColor([0,0,0])
self.targetFiducial = slicer.util.getNode('targetFiducial')
if not self.targetFiducial:
slicer.util.loadMarkupsFiducialList(self.SoundGuidanceModuleModelsPath + 'Target.fcsv')
self.targetFiducial = slicer.util.getNode(pattern="Target")
self.surfaceFiducial = slicer.util.getNode('surfaceFiducial')
if not self.surfaceFiducial:
slicer.util.loadMarkupsFiducialList(self.SoundGuidanceModuleModelsPath + 'surfacePoint.fcsv')
self.surfaceFiducial = slicer.util.getNode(pattern="surfacePoint")
self.xAxisFiducial = slicer.util.getNode('xAxisFiducial')
if not self.xAxisFiducial:
slicer.util.loadMarkupsFiducialList(self.SoundGuidanceModuleModelsPath + 'xAxisFiducial.fcsv')
self.xAxisFiducial= slicer.util.getNode(pattern="xAxisFiducial")
# Parameters Area
# Parameters Area
#
parametersCollapsibleButton = ctk.ctkCollapsibleButton()
parametersCollapsibleButton.text = "Parameters"
self.layout.addWidget(parametersCollapsibleButton)
# Layout within the dummy collapsible button
parametersFormLayout = qt.QFormLayout(parametersCollapsibleButton)
#
# Apply Button
#
self.applyButton = qt.QPushButton("Calculate Distance")
self.applyButton.toolTip = "Run the algorithm."
self.applyButton.enabled = True
self.applyButton.checkable = True
parametersFormLayout.addRow(self.applyButton)
# Calculate Distance Label
self.calculateDistanceLabel = qt.QLabel('-')
self.calculateDistanceLabel.setStyleSheet(self.defaultStyleSheet)
self.QFormLayoutLabel = qt.QLabel('Distance to target (mm): ')
self.QFormLayoutLabel.setStyleSheet(self.defaultStyleSheet)
parametersFormLayout.addRow(self.QFormLayoutLabel, self.calculateDistanceLabel)
#
# Sound Button
#
self.playSoundButton = qt.QPushButton("Play Sound")
self.playSoundButton.enabled = True
self.playSoundButton.checkable = True
parametersFormLayout.addRow(self.playSoundButton)
#Load transformations
self.pointerToTracker = slicer.util.getNode('PointerToTracker')
if not self.pointerToTracker:
print('ERROR: pointerToTracker transform node was not found')
self.needleToTracker = slicer.util.getNode('NeedleToTracker')
if not self.needleToTracker:
print('ERROR: needleToTracker transform node was not found')
self.referenceToTracker = slicer.util.getNode('ReferenceToTracker')
if not self.referenceToTracker:
print('ERROR: referenceToTracker transform node was not found')
self.trackerToReference = slicer.util.getNode('TrackerToReference')
if not self.trackerToReference:
print('ERROR: TrackerToReference transform node was not found')
# Load models first
self.SoundGuidanceModuleDataPath = slicer.modules.soundguidance.path.replace("SoundGuidance.py","") + 'Resources/Data/'
self.needleTipToNeedle = slicer.util.getNode('NeedleTipToNeedle')
if not self.needleTipToNeedle:
slicer.util.loadTransform(self.SoundGuidanceModuleDataPath + 'NeedleTipToNeedle.h5')
self.needleTipToNeedle = slicer.util.getNode(pattern="NeedleTipToNeedle")
self.pointerTipToPointer = slicer.util.getNode('PointerTipToPointer')
if not self.pointerTipToPointer:
slicer.util.loadTransform(self.SoundGuidanceModuleDataPath + 'PointerTipToPointer.h5')
self.pointerTipToPointer = slicer.util.getNode(pattern="PointerTipToPointer")
self.boxToReference = slicer.util.getNode('BoxToReference')
if not self.boxToReference:
slicer.util.loadTransform(self.SoundGuidanceModuleDataPath + 'BoxToReference.h5')
self.boxToReference = slicer.util.getNode(pattern="BoxToReference")
# Tranformations to fix models orientation
self.needleModelToNeedleTip = slicer.util.getNode('needleModelToNeedleTip')
if not self.needleModelToNeedleTip:
self.needleModelToNeedleTip=slicer.vtkMRMLLinearTransformNode()
self.needleModelToNeedleTip.SetName("needleModelToNeedleTip")
matrixNeedleModel = vtk.vtkMatrix4x4()
matrixNeedleModel.SetElement( 0, 0, -1 ) # Row 1
matrixNeedleModel.SetElement( 0, 1, 0 )
matrixNeedleModel.SetElement( 0, 2, 0 )
matrixNeedleModel.SetElement( 0, 3, 0 )
matrixNeedleModel.SetElement( 1, 0, 0 ) # Row 2
matrixNeedleModel.SetElement( 1, 1, 1 )
matrixNeedleModel.SetElement( 1, 2, 0 )
matrixNeedleModel.SetElement( 1, 3, 0 )
matrixNeedleModel.SetElement( 2, 0, 0 ) # Row 3
matrixNeedleModel.SetElement( 2, 1, 0 )
matrixNeedleModel.SetElement( 2, 2, -1 )
matrixNeedleModel.SetElement( 2, 3, 0 )
self.needleModelToNeedleTip.SetMatrixTransformToParent(matrixNeedleModel)
slicer.mrmlScene.AddNode(self.needleModelToNeedleTip)
self.pointerModelToPointerTip = slicer.util.getNode('pointerModelToPointerTip')
if not self.pointerModelToPointerTip:
self.pointerModelToPointerTip=slicer.vtkMRMLLinearTransformNode()
self.pointerModelToPointerTip.SetName("pointerModelToPointerTip")
matrixPointerModel = vtk.vtkMatrix4x4()
matrixPointerModel.SetElement( 0, 0, 0 ) # Row 1
matrixPointerModel.SetElement( 0, 1, 0 )
matrixPointerModel.SetElement( 0, 2, 1 )
matrixPointerModel.SetElement( 0, 3, 0 )
matrixPointerModel.SetElement( 1, 0, 0 ) # Row 2
matrixPointerModel.SetElement( 1, 1, 1 )
matrixPointerModel.SetElement( 1, 2, 0 )
matrixPointerModel.SetElement( 1, 3, 0 )
matrixPointerModel.SetElement( 2, 0, -1 ) # Row 3
matrixPointerModel.SetElement( 2, 1, 0 )
matrixPointerModel.SetElement( 2, 2, 0 )
matrixPointerModel.SetElement( 2, 3, 0 )
self.pointerModelToPointerTip.SetMatrixTransformToParent(matrixPointerModel)
slicer.mrmlScene.AddNode(self.pointerModelToPointerTip)
# connections
self.applyButton.connect('clicked(bool)', self.onCalculateDistanceButton)
self.playSoundButton.connect('clicked(bool)', self.onplaySoundButtonClicked)
#Build Transforms tree
# Pointer
self.pointerModel.SetAndObserveTransformNodeID(self.pointerModelToPointerTip.GetID())
self.pointerModelToPointerTip.SetAndObserveTransformNodeID(self.pointerTipToPointer.GetID())
self.pointerTipToPointer.SetAndObserveTransformNodeID(self.pointerToTracker.GetID())
# Needle
self.needleModel.SetAndObserveTransformNodeID(self.needleModelToNeedleTip.GetID())
self.needleModelToNeedleTip.SetAndObserveTransformNodeID(self.needleTipToNeedle.GetID())
self.needleTipToNeedle.SetAndObserveTransformNodeID(self.needleToTracker.GetID())
#Box and aro
self.boxModel.SetAndObserveTransformNodeID(self.boxToReference.GetID())
self.aroModel.SetAndObserveTransformNodeID(self.boxToReference.GetID())
self.targetFiducial.SetAndObserveTransformNodeID(self.boxToReference.GetID())
self.surfaceFiducial.SetAndObserveTransformNodeID(self.boxToReference.GetID())
self.xAxisFiducial.SetAndObserveTransformNodeID(self.boxToReference.GetID())
self.boxToReference.SetAndObserveTransformNodeID(self.referenceToTracker.GetID())
# Add vertical spacer
self.layout.addStretch(1)
def cleanup(self):
pass
def onCalculateDistanceButton(self):
self.logic.transferValues(self.needleTipToNeedle, self.pointerTipToPointer, self.needleToTracker, self.targetFiducial, self.surfaceFiducial, self.boxToReference, self.xAxisFiducial)
self.logic.setOutPutDistanceLabel(self.calculateDistanceLabel)
self.logic.plotLineZaxis()
self.logic.addCalculateDistanceObserver()
def onplaySoundButtonClicked(self):
#IP = "172.16.203.210"
self.logic.changeSendDataStatus()
#initialization
#c = SendOSC()
#c.connect("localhost", 8080)
#self.logic.activateOSC()
#
# SoundGuidanceLogic
#
class SoundGuidanceLogic(ScriptedLoadableModuleLogic):
"""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.
Uses ScriptedLoadableModuleLogic base class, available at:
https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
"""
def __init__(self):
self.sendDataOK = False
self.OSC_active = False
self.line = slicer.util.getNode('Line')
if not self.line:
self.line = slicer.vtkMRMLModelNode()
self.line.SetName('Line')
linePolyData = vtk.vtkPolyData()
self.line.SetAndObservePolyData(linePolyData)
modelDisplay = slicer.vtkMRMLModelDisplayNode()
modelDisplay.SetSliceIntersectionVisibility(True)
modelDisplay.SetColor(0,1,0)
slicer.mrmlScene.AddNode(modelDisplay)
self.line.SetAndObserveDisplayNodeID(modelDisplay.GetID())
slicer.mrmlScene.AddNode(self.line)
def transferValues(self, needleTTNeedle, pointerTTPointer, needleTTracker, tFiducial,sFiducial, btr, xaf):
self.needleTipToNeedle = needleTTNeedle
self.pointerTipToPointer = pointerTTPointer
self.needleToTracker = needleTTracker
self.targetFiducial = tFiducial
self.surfaceFiducial = sFiducial
self.boxToReference = btr
self.xAxisFiducial = xaf
def addCalculateDistanceObserver(self):
print("[TEST] addCalculateDistanceObserver")
#self.tipFiducial.SetAndObserveTransformNodeID(self.toolTipToTool.GetID())
self.observerTag = self.needleToTracker.AddObserver('ModifiedEvent', self.calculateCallback) # slicer.vtkMRMLMarkupsNode.MarkupAddedEvent
logging.info('addCalculateDistanceObserver')
def removeCalculateDistanceObserver(self):
print("[TEST] removeCalculateDistanceObserver")
self.callbackObserverTag = 1
if self.callbackObserverTag != -1:
self.toolToReference.RemoveObserver(self.observerTag)
self.callbackObserverTag = -1
logging.info('removeCalculateDistanceObserver')
def calculateCallback(self, transformNode, event=None):
self.calculateDistance()
def calculateDistance(self):
pointerTipPoint = [0.0,0.0,0.0]
m = vtk.vtkMatrix4x4()
self.pointerTipToPointer.GetMatrixTransformToWorld(m)
pointerTipPoint[0] = m.GetElement(0, 3)
pointerTipPoint[1] = m.GetElement(1, 3)
pointerTipPoint[2] = m.GetElement(2, 3)
distance = math.sqrt(math.pow(pointerTipPoint[0]-self.pos[0], 2) + math.pow(pointerTipPoint[1]-self.pos[1], 2) + math.pow(pointerTipPoint[2]-self.pos[2], 2))
# La distancia se da en mm ----> 50mm = 5cm
# Voy a normalizar con un tope de 20cm
normalizedDistance = [(distance - 0)/200]
if self.OSC_active:
#print ("HOLAAA")
c.send("/dumpOSC/0/0", 0)
self.outputDistanceLabel.setText('%.1f' % distance)
self.pos = [self.pos[i] for i in (0,1,2)]
self.drawLineBetweenPoints(pointerTipPoint, self.pos)
if self.sendDataOK:
self.sendData(normalizedDistance)
def setOutPutDistanceLabel(self, label):
self.outputDistanceLabel = label
def drawLineBetweenPoints(self, point1, point2):
# Create a vtkPoints object and store the points in it
points = vtk.vtkPoints()
points.InsertNextPoint(point1)
points.InsertNextPoint(point2)
# Create line
line = vtk.vtkLine()
#line.SetLineWidth(12.0)
line.GetPointIds().SetId(0,0)
line.GetPointIds().SetId(1,1)
lineCellArray = vtk.vtkCellArray()
lineCellArray.InsertNextCell(line)
# Update model data
self.line.GetPolyData().SetPoints(points)
self.line.GetPolyData().SetLines(lineCellArray)
def activateOSC(self):
self.OSC_active = True
def sendData(self, distance):
client = OSC.OSCClient()
client.connect(("192.168.0.70",7400))
message = OSC.OSCMessage()
message.setAddress("/dumpOSC/0/0")
message.append(distance)
client.send(message)
def changeSendDataStatus(self):
self.sendDataOK = True
def plotLineZaxis(self):
# Create a vtkPoints object and store the points in it
self.pos = [0.0, 0.0, 0.0, 0.0]
self.targetFiducial.GetNthFiducialWorldCoordinates(0, self.pos)
targetP = [self.pos[i] for i in (0,1,2)]
self.surfPoint = [0.0, 0.0, 0.0, 0.0]
self.surfaceFiducial.GetNthFiducialWorldCoordinates(0, self.surfPoint)
surfaceP = [self.surfPoint[i] for i in (0,1,2)]
self.zVector = np.subtract(targetP,surfaceP)
points = vtk.vtkPoints()
points.InsertNextPoint(targetP)
points.InsertNextPoint(surfaceP)
# Create line
line = vtk.vtkLine()
line.GetPointIds().SetId(0,0)
line.GetPointIds().SetId(1,1)
lineCellArray = vtk.vtkCellArray()
lineCellArray.InsertNextCell(line)
self.lineNode = slicer.vtkMRMLModelNode()
self.lineNode.SetName('LineZ')
linePolyData = vtk.vtkPolyData()
self.lineNode.SetAndObservePolyData(linePolyData)
modelDisplay = slicer.vtkMRMLModelDisplayNode()
modelDisplay.SetSliceIntersectionVisibility(True)
modelDisplay.SetColor(1,1,1)
slicer.mrmlScene.AddNode(modelDisplay)
self.lineNode.SetAndObserveDisplayNodeID(modelDisplay.GetID())
slicer.mrmlScene.AddNode(self.lineNode)
self.lineNode.GetPolyData().SetPoints(points)
self.lineNode.GetPolyData().SetLines(lineCellArray)
#self.lineNode.SetAndObserveTransformNodeID(self.boxToReference.GetID())
self.drawPlane(surfaceP, self.zVector)
self.definePlaneAxis(surfaceP, self.zVector)
def drawPlane(self, m, V_norm):
scene = slicer.mrmlScene
#create a plane to cut,here it cuts in the XZ direction (xz normal=(1,0,0);XY =(0,0,1),YZ =(0,1,0)
planex=vtk.vtkPlane()
planex.SetOrigin(m[0],m[1],m[2])
planex.SetNormal(V_norm[0],V_norm[1],V_norm[2])
renderer = slicer.app.layoutManager().threeDWidget(0).threeDView().renderWindow().GetRenderers().GetFirstRenderer()
viewSize = renderer.ComputeVisiblePropBounds()
#viewSize = (-50.0, 50.0, -50.0, 50.0, -50.0, 50.0)
planexSample = vtk.vtkSampleFunction()
planexSample.SetImplicitFunction(planex)
planexSample.SetModelBounds(viewSize)
#planexSample.SetSampleDimensions(200,200,200)
planexSample.ComputeNormalsOff()
plane1 = vtk.vtkContourFilter()
plane1.SetInputConnection(planexSample.GetOutputPort())
# Create model Plane A node
planeA = slicer.vtkMRMLModelNode()
planeA.SetScene(scene)
planeA.SetName("X-Y Plane")
planeA.SetAndObservePolyData(plane1.GetOutput())
# Create display model Plane A node
planeAModelDisplay = slicer.vtkMRMLModelDisplayNode()
planeAModelDisplay.SetColor(0.145,0.77,0.596)
planeAModelDisplay.BackfaceCullingOff()
planeAModelDisplay.SetScene(scene)
scene.AddNode(planeAModelDisplay)
planeA.SetAndObserveDisplayNodeID(planeAModelDisplay.GetID())
#Add to scene
planeAModelDisplay.SetInputPolyDataConnection(plane1.GetOutputPort())
scene.AddNode(planeA)
# adjust center of 3d view to plane
layoutManager = slicer.app.layoutManager()
threeDWidget = layoutManager.threeDWidget(0)
threeDView = threeDWidget.threeDView()
threeDView.resetFocalPoint()
def definePlaneAxis(self, oPoint, zV):
xP = [0.0, 0.0, 0.0, 0.0]
self.xAxisFiducial.GetNthFiducialWorldCoordinates(0, xP)
# a = Plane(Point3D(1,4,6), normal_vector=(2,4,6))
xAxisPoint = [xP[i] for i in (0,1,2)]
self.zVector = zV
self.xVector = np.subtract(xAxisPoint,oPoint)
self.yVector = np.cross(self.zVector, self.xVector)
#Para hallar la matriz de transformacion es necesario tener vectores unitarios!
xUnitario = self.xVector/numpy.linalg.norm(self.xVector)
yUnitario = self.yVector/numpy.linalg.norm(self.yVector)
zUnitario = self.zVector/numpy.linalg.norm(self.zVector)
print type(self.xVector)
print ('Vector x:')
print (self.xVector)
print (self.xVector[0])
print ('Vector y:')
print (self.yVector)
print ('Vector z:')
print (self.zVector)
print oPoint
#We create the transformation matrix so that change the coordinates system:
R = vtk.vtkMatrix3x3()
R.SetElement( 0, 0, xUnitario[0] ) # Row 1
R.SetElement( 0, 1, xUnitario[1] )
R.SetElement( 0, 2, xUnitario[2] )
R.SetElement( 1, 0, yUnitario[0] ) # Row 2
R.SetElement( 1, 1, yUnitario[1] )
R.SetElement( 1, 2, yUnitario[2] )
R.SetElement( 2, 0, zUnitario[0] ) # Row 3
R.SetElement( 2, 1, zUnitario[1] )
R.SetElement( 2, 2, zUnitario[2] )
resultPoint = [0.0, 0.0, 0.0]
R.MultiplyPoint(oPoint, resultPoint)
matrixTransfBOX = vtk.vtkMatrix4x4()
matrixTransfBOX.SetElement( 0, 0, xUnitario[0] ) # Row 1
matrixTransfBOX.SetElement( 0, 1, xUnitario[1] )
matrixTransfBOX.SetElement( 0, 2, xUnitario[2] )
matrixTransfBOX.SetElement( 0, 3, -resultPoint[0] )
matrixTransfBOX.SetElement( 1, 0, yUnitario[0] ) # Row 2
matrixTransfBOX.SetElement( 1, 1, yUnitario[1] )
matrixTransfBOX.SetElement( 1, 2, yUnitario[2] )
matrixTransfBOX.SetElement( 1, 3, -resultPoint[1] )
matrixTransfBOX.SetElement( 2, 0, zUnitario[0] ) # Row 3
matrixTransfBOX.SetElement( 2, 1, zUnitario[1] )
matrixTransfBOX.SetElement( 2, 2, zUnitario[2] )
matrixTransfBOX.SetElement( 2, 3, -resultPoint[2] )
matrixTransfBOX.SetElement( 3, 0, 0 ) # Row 4
matrixTransfBOX.SetElement( 3, 1, 0)
matrixTransfBOX.SetElement( 3, 2, 0 )
matrixTransfBOX.SetElement( 3, 3, 1 )
print (matrixTransfBOX)
#self.planeA.SetAndObserveTransformNodeID(self.boxToReference.GetID())
oPoint = oPoint + [1.0]
nuevoOrigen = [0.0, 0.0, 0.0 , 0.0]
matrixTransfBOX.MultiplyPoint(oPoint, nuevoOrigen)
print nuevoOrigen
class SoundGuidanceTest(ScriptedLoadableModuleTest):
"""
This is the test case for your scripted module.
Uses ScriptedLoadableModuleTest base class, available at:
https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
"""
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_SoundGuidance1()
def test_SoundGuidance1(self):
""" Ideally you should have several levels of tests. At the lowest level
tests should 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:
logging.info('Requesting download %s from %s...\n' % (name, url))
urllib.urlretrieve(url, filePath)
if loader:
logging.info('Loading %s...' % (name,))
loader(filePath)
self.delayDisplay('Finished with download and loading')
volumeNode = slicer.util.getNode(pattern="FA")
logic = SoundGuidanceLogic()
self.assertTrue( logic.hasImageData(volumeNode) )
self.delayDisplay('Test passed!')
from OSC import OSCClient, OSCMessage
class SendOSC(object):
def __init__(self):
self.osc_message = None
self.osc_client = OSCClient()
self.osc_message = OSCMessage()
self.ip = ""
self.port = 0
def connect(self, ip="localhost", port=8080):
self.ip = ip
self.port = port
self.osc_client.connect((self.ip, self.port))
def send(self, address, value):
self.osc_message.setAddress(address)
self.osc_message.append(value)
self.osc_client.send(self.osc_message)
def send_distane(self, distance):
oscdump = "/dumpOSC/DistanceTipTarget"
self.send(oscdump, distance)
def send_needle_tip_position(self, x, y, z):
oscdump = "/dumpOSC/needltip/x"
self.send(oscdump, x)
oscdump = "/dumpOSC/needltip/y"
self.send(oscdump, y)
oscdump = "/dumpOSC/needltip/z"
self.send(oscdump, z)