forked from PerkTutor/TissueModelCreator
/
TissueModelCreator.py
728 lines (570 loc) · 24.8 KB
/
TissueModelCreator.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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
import os
import unittest
from __main__ import vtk, qt, ctk, slicer
#
# TissueModelCreator
#
class TissueModelCreator:
def __init__(self, parent):
parent.title = "Tissue Model Creator" # TODO make this more human readable by adding spaces
parent.categories = ["Perk Tutor"]
parent.dependencies = []
parent.contributors = ["Matthew Holden (Queen's University)"] # replace with "Firstname Lastname (Org)"
parent.helpText = """
The purpose of the Tissue Model Creator module is to create a model based on a set of collected fiducial points. For help on how to use this module visit: <a href='http://www.github.com/PerkTutor/TissueModelCreator/wiki'>Tissue Model Creator</a>.
"""
parent.acknowledgementText = """
This work was was funded by Cancer Care Ontario and the Ontario Consortium for Adaptive Interventions in Radiation Oncology (OCAIRO).
""" # replace with organization, grant and thanks.
parent.icon = qt.QIcon( "TissueModelCreator.png" )
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['TissueModelCreator'] = self.runTest
def runTest(self):
tester = TissueModelCreatorTest()
tester.runTest()
#
# qTissueModelCreatorWidget
#
class TissueModelCreatorWidget:
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 ...
# Comment these out when not debugging
#
# 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 = "TissueModelCreator 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)
#
# Display Area
#
displayCollapsibleButton = ctk.ctkCollapsibleButton()
displayCollapsibleButton.text = "Display"
self.layout.addWidget(displayCollapsibleButton)
# Layout within the dummy collapsible button
displayFormLayout = qt.QFormLayout(displayCollapsibleButton)
#
# input fiducials selector
#
self.markupSelector = slicer.qMRMLNodeComboBox()
self.markupSelector.nodeTypes = ( ("vtkMRMLMarkupsFiducialNode"), "" )
self.markupSelector.addEnabled = False
self.markupSelector.removeEnabled = False
self.markupSelector.noneEnabled = False
self.markupSelector.showHidden = False
self.markupSelector.showChildNodeTypes = False
self.markupSelector.setMRMLScene( slicer.mrmlScene )
self.markupSelector.setToolTip( "Pick the markup node for the algorithm." )
displayFormLayout.addRow( "Select Markup Node: ", self.markupSelector )
#
# Depth slider
#
self.depthSlider = ctk.ctkSliderWidget()
self.depthSlider.maximum = 1000
self.depthSlider.minimum = 0
self.depthSlider.value = 100
self.depthSlider.setToolTip( "Select the depth of the tissue." )
displayFormLayout.addRow( "Depth (mm): ", self.depthSlider )
#
# Flip (ie flip) checkbox
#
self.flipCheckBox = qt.QCheckBox()
self.flipCheckBox.setCheckState( False )
self.flipCheckBox.setToolTip( "Flip the tissue so it is in the other direction." )
self.flipCheckBox.setText( "Flip" )
displayFormLayout.addRow( self.flipCheckBox )
#
# Force plane (ie plane) checkbox
#
self.planeCheckBox = qt.QCheckBox()
self.planeCheckBox.setCheckState( False )
self.planeCheckBox.setToolTip( "Force a plane to fit the collected points on the surface." )
self.planeCheckBox.setText( "Plane" )
displayFormLayout.addRow( self.planeCheckBox )
#
# Create Button
#
self.createButton = qt.QPushButton( "Create" )
self.createButton.toolTip = "Create a tissue model."
self.createButton.enabled = True
displayFormLayout.addRow( self.createButton )
#
# Status Label
#
self.statusLabel = qt.QLabel( "Status:" )
self.statusLabel.toolTip = "Status of whether the tissue model was successfully created."
self.statusLabel.enabled = True
displayFormLayout.addRow( self.statusLabel )
# connections
self.createButton.connect( 'clicked(bool)', self.onCreateButtonClicked )
# Add vertical spacer
self.layout.addStretch(1)
def cleanup(self):
pass
def onCreateButtonClicked(self):
logic = TissueModelCreatorLogic()
surfaceClosed = logic.run( self.markupSelector.currentNode(), self.depthSlider.value, self.planeCheckBox.checked, self.flipCheckBox.checked )
if ( surfaceClosed == True ):
self.statusLabel.setText( "Status: Success!" )
else:
self.statusLabel.setText( "Status: Failed!" )
def onReload(self,moduleName="TissueModelCreator"):
"""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="TissueModelCreator"):
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")
#
# TissueModelCreatorLogic
#
class TissueModelCreatorLogic:
"""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 run( self, markupNode, depth, fitPlane, flip ):
"""
Run the actual algorithm
"""
# Get all of the fiducial nodes and convert to vtkPoints
points = vtk.vtkPoints()
for i in range( 0, markupNode.GetNumberOfFiducials() ):
currentCoordinates = [ 0, 0, 0 ]
markupNode.GetNthFiducialPosition( i, currentCoordinates )
points.InsertNextPoint( currentCoordinates )
# Check that there is non-zero range in all coordinate directions
pointsBounds = [ 0, 0, 0, 0, 0, 0 ]
points.GetBounds( pointsBounds )
if ( pointsBounds[0] == pointsBounds [1] or pointsBounds[2] == pointsBounds [3] or pointsBounds[4] == pointsBounds [5] ):
print "Tissue Model Creator: Points have no extent in one or more coordinate directions."
return False
# Create a polydata object from the points
# The reversiness doesn't matter - we will fix it later if it os wrong
if ( fitPlane ):
surfacePolyData = self.PointsToPlanePolyData( points, True )
else:
surfacePolyData = self.PointsToSurfacePolyData( points, True )
surfaceCleaner = vtk.vtkCleanPolyData()
surfaceCleaner.SetInputData( surfacePolyData )
surfaceCleaner.Update()
surfacePolyData = surfaceCleaner.GetOutput()
mean = self.CalculateMean( points )
surfaceBase = [ 0, 0, 0 ]
surfaceDir1 = [ 0, 0, 0 ]
surfaceDir2 = [ 0, 0, 0 ]
surfaceNormal = [ 0, 0, 0 ]
self.CalculatePlane( surfacePolyData.GetPoints(), surfaceBase, surfaceDir1, surfaceDir2, surfaceNormal )
if ( flip == True ):
surfaceNormal[ 0 ] = - surfaceNormal[ 0 ]
surfaceNormal[ 1 ] = - surfaceNormal[ 1 ]
surfaceNormal[ 2 ] = - surfaceNormal[ 2 ]
extremePointIndex = self.FindExtremePoint( surfacePolyData.GetPoints(), surfaceBase, surfaceNormal )
reverse = self.ReverseNormals( extremePointIndex, surfacePolyData, surfaceNormal )
# Reverse the normals if necessary
reverseFilter = vtk.vtkReverseSense()
reverseFilter.SetInputData( surfacePolyData )
reverseFilter.SetReverseCells( reverse )
reverseFilter.SetReverseNormals( reverse )
reverseFilter.Update()
surfacePolyData = reverseFilter.GetOutput()
untransDeepPolyData = vtk.vtkPolyData()
untransDeepPolyData.DeepCopy( surfacePolyData )
# Make the normals opposite the surface's normals
reverseFilter = vtk.vtkReverseSense()
reverseFilter.SetInputData( untransDeepPolyData )
reverseFilter.SetReverseCells( True )
reverseFilter.SetReverseNormals( True )
reverseFilter.Update()
untransDeepPolyData = reverseFilter.GetOutput()
deepTransform = vtk.vtkTransform()
deepTransform.Translate( depth * surfaceNormal[0], depth * surfaceNormal[1], depth * surfaceNormal[2] )
deepTransformFilter = vtk.vtkTransformPolyDataFilter()
deepTransformFilter.SetInputData( untransDeepPolyData )
deepTransformFilter.SetTransform( deepTransform )
deepTransformFilter.Update()
deepPolyData = deepTransformFilter.GetOutput()
surfaceHullPoints = self.GetBoundaryPoints( surfacePolyData )
deepHullPoints = self.GetBoundaryPoints( deepPolyData )
# Apparently, the joining needs an offset for the plane, but not for the surface reconstruction
if ( fitPlane ):
jointHullPolyData = self.JoinBoundaryPoints( surfaceHullPoints, deepHullPoints, 1 )
else:
jointHullPolyData = self.JoinBoundaryPoints( surfaceHullPoints, deepHullPoints, 0 )
# Append all of the polydata together
tissuePolyDataAppend = vtk.vtkAppendPolyData()
tissuePolyDataAppend.AddInputData( surfacePolyData )
tissuePolyDataAppend.AddInputData( deepPolyData )
tissuePolyDataAppend.AddInputData( jointHullPolyData )
tissuePolyDataAppend.Update()
# Clean up so the surface is closed
tissueCleaner = vtk.vtkCleanPolyData()
tissueCleaner.SetInputData( tissuePolyDataAppend.GetOutput() )
tissueCleaner.Update()
tissueModelPolyData = tissueCleaner.GetOutput()
# Add the data to a model
tissueModel = slicer.mrmlScene.CreateNodeByClass( "vtkMRMLModelNode" )
slicer.mrmlScene.AddNode( tissueModel )
tissueModel.SetName( "TissueModel" )
tissueModel.SetAndObservePolyData( tissueModelPolyData )
tissueModel.SetScene( slicer.mrmlScene )
# Finally display the model
tissueModelDisplay = slicer.mrmlScene.CreateNodeByClass( "vtkMRMLModelDisplayNode" )
slicer.mrmlScene.AddNode( tissueModelDisplay )
tissueModel.SetAndObserveDisplayNodeID( tissueModelDisplay.GetID() )
tissueModelDisplay.SetScene( slicer.mrmlScene )
tissueModelDisplay.SetInputPolyDataConnection( tissueModel.GetPolyDataConnection() )
# Check to make sure the model is a closed surface
edgesFilter = vtk.vtkFeatureEdges()
edgesFilter.FeatureEdgesOff()
edgesFilter.BoundaryEdgesOn()
edgesFilter.NonManifoldEdgesOn()
edgesFilter.SetInputData( tissueModel.GetPolyData() )
edgesFilter.Update()
if ( edgesFilter.GetOutput().GetNumberOfCells() != 0 ):
print "Tissue Model Creator: Surface is not closed."
return False
return True
def PointsToSurfacePolyData( self, inPoints, reverse ):
# Create a polydata object from the points
pointsPolyData = vtk.vtkPolyData()
pointsPolyData.SetPoints( inPoints )
# Create the surface filter from the polydata
surfaceFilter = vtk.vtkSurfaceReconstructionFilter()
surfaceFilter.SetInputData( pointsPolyData )
surfaceFilter.Update()
# Do the contouring filter, and reverse to ensure it works properly
contourFilter = vtk.vtkContourFilter()
contourFilter.SetValue( 0, 0.0 )
contourFilter.SetInputData( surfaceFilter.GetOutput() )
contourFilter.Update()
# Reverse the normals if necessary
reverseFilter = vtk.vtkReverseSense()
reverseFilter.SetInputData( contourFilter.GetOutput() )
reverseFilter.SetReverseCells( reverse )
reverseFilter.SetReverseNormals( reverse )
reverseFilter.Update()
# Reset the scaling to let the surface match the points
fiducialBounds = [ 0, 0, 0, 0, 0, 0 ]
pointsPolyData.GetBounds( fiducialBounds )
tissueBounds = [ 0, 0, 0, 0, 0, 0 ]
reverseFilter.GetOutput().GetBounds( tissueBounds )
scaleX = ( fiducialBounds[1] - fiducialBounds[0] ) / ( tissueBounds[1] - tissueBounds[0] )
scaleY = ( fiducialBounds[3] - fiducialBounds[2] ) / ( tissueBounds[3] - tissueBounds[2] )
scaleZ = ( fiducialBounds[5] - fiducialBounds[4] ) / ( tissueBounds[5] - tissueBounds[4] )
transform = vtk.vtkTransform()
transform.Translate( fiducialBounds[0], fiducialBounds[2], fiducialBounds[4] )
transform.Scale( scaleX, scaleY, scaleZ )
transform.Translate( - tissueBounds[0], - tissueBounds[2], - tissueBounds[4] )
transformFilter = vtk.vtkTransformPolyDataFilter()
transformFilter.SetInputData( reverseFilter.GetOutput() )
transformFilter.SetTransform( transform )
transformFilter.Update()
return transformFilter.GetOutput()
def PointsToPlanePolyData( self, inPoints, reverse ):
# Create the oriented bounding box
# This gives us one plane on the bounding box, not the plane of best fit
corner = [ 0, 0, 0 ]
maxVector = [ 0, 0, 0 ]
midVector = [ 0, 0, 0 ]
minVector = [ 0, 0, 0 ]
size = [ 0, 0, 0 ]
obb = vtk.vtkOBBTree()
obb.ComputeOBB( inPoints, corner, maxVector, midVector, minVector, size )
# Calculate the mean of the points on the plane
base = self.CalculateMean( inPoints )
relBase = [ 0, 0, 0 ]
# Find the projection of the mean point in the minVector direction
vtk.vtkMath().Subtract( base, corner, relBase )
normal = minVector[:]
vtk.vtkMath().Normalize( normal )
dot = vtk.vtkMath().Dot( relBase, normal )
dot = 0
proj = normal[:]
vtk.vtkMath().MultiplyScalar( proj, dot )
print dot
# Find the points on the plane
origin = [ 0, 0, 0 ]
point1 = [ 0, 0, 0 ]
point2 = [ 0, 0, 0 ]
vtk.vtkMath().Add( corner, proj, origin )
vtk.vtkMath().Add( origin, maxVector, point1 )
vtk.vtkMath().Add( origin, midVector, point2 )
# Construct the plane
plane = vtk.vtkPlaneSource()
plane.SetOrigin( origin )
plane.SetPoint1( point1 )
plane.SetPoint2( point2 )
plane.Update()
return plane.GetOutput()
def CalculateMean( self, inPoints ):
mean = [ 0, 0, 0 ]
for i in range( 0, inPoints.GetNumberOfPoints() ):
currPoint = [ 0, 0, 0 ]
inPoints.GetPoint( i, currPoint )
mean[ 0 ] = mean[ 0 ] + currPoint[ 0 ]
mean[ 1 ] = mean[ 1 ] + currPoint[ 1 ]
mean[ 2 ] = mean[ 2 ] + currPoint[ 2 ]
if ( inPoints.GetNumberOfPoints() > 0 ):
mean[ 0 ] = mean[ 0 ] / inPoints.GetNumberOfPoints()
mean[ 1 ] = mean[ 1 ] / inPoints.GetNumberOfPoints()
mean[ 2 ] = mean[ 2 ] / inPoints.GetNumberOfPoints()
return mean
def CalculatePlane( self, inPoints, base, dir1, dir2, normal ):
# Create arrays for the dataset
points2D = vtk.vtkPoints()
arrayX = vtk.vtkDoubleArray()
arrayX.SetNumberOfComponents( 1 )
arrayX.SetName ( 'X' )
arrayY = vtk.vtkDoubleArray()
arrayY.SetNumberOfComponents( 1 )
arrayY.SetName ( 'Y' )
arrayZ = vtk.vtkDoubleArray()
arrayZ.SetNumberOfComponents( 1 )
arrayZ.SetName ( 'Z' )
# Add the points to the table
for i in range( 0, inPoints.GetNumberOfPoints() ):
currPoint = [ 0, 0, 0 ]
inPoints.GetPoint( i, currPoint )
arrayX.InsertNextValue( currPoint[ 0 ] )
arrayY.InsertNextValue( currPoint[ 1 ] )
arrayZ.InsertNextValue( currPoint[ 2 ] )
# Create a table for the dataset
table = vtk.vtkTable()
table.AddColumn( arrayX )
table.AddColumn( arrayY )
table.AddColumn( arrayZ )
# Setting up the PCA
pca = vtk.vtkPCAStatistics()
pca.SetInputData( vtk.vtkStatisticsAlgorithm.INPUT_DATA, table )
pca.SetColumnStatus( 'X', 1 )
pca.SetColumnStatus( 'Y', 1 )
pca.SetColumnStatus( 'Z', 1 )
pca.RequestSelectedColumns()
pca.SetDeriveOption( True )
pca.Update()
eigvec = vtk.vtkDoubleArray()
pca.GetEigenvectors( eigvec )
eigvec.GetTuple( 0, dir1 )
eigvec.GetTuple( 1, dir2 )
eigvec.GetTuple( 2, normal )
mean = self.CalculateMean( inPoints )
base[0] = mean[0]
base[1] = mean[1]
base[2] = mean[2]
def GetBoundaryPoints( self, inPolyData ):
featureEdges = vtk.vtkFeatureEdges()
featureEdges.FeatureEdgesOff()
featureEdges.NonManifoldEdgesOff()
featureEdges.ManifoldEdgesOff()
featureEdges.BoundaryEdgesOn()
featureEdges.SetInputData( inPolyData )
featureEdges.Update()
stripper = vtk.vtkStripper()
stripper.SetInputData( featureEdges.GetOutput() )
stripper.Update()
cleaner = vtk.vtkCleanPolyData()
cleaner.SetInputData( stripper.GetOutput() )
cleaner.Update()
return cleaner.GetOutput().GetPoints()
def JoinBoundaryPoints( self, hullPoints1, hullPoints2, offset ):
if ( hullPoints1.GetNumberOfPoints() != hullPoints2.GetNumberOfPoints() ):
return
joiningAppend = vtk.vtkAppendPolyData()
numPoints = hullPoints1.GetNumberOfPoints()
for i in range( 0, numPoints ):
currPointsForSurface = vtk.vtkPoints()
point1 = [ 0, 0, 0 ]
hullPoints1.GetPoint( ( i - offset )% numPoints, point1 )
currPointsForSurface.InsertNextPoint( point1[ 0 ], point1[ 1 ], point1[ 2 ] )
point2 = [ 0, 0, 0 ]
hullPoints2.GetPoint( ( - i ) % numPoints, point2 )
currPointsForSurface.InsertNextPoint( point2[ 0 ], point2[ 1 ], point2[ 2 ] )
# Observe that the deep is flipped from the surface
# Must proceed in opposite orders
point3 = [ 0, 0, 0 ]
hullPoints1.GetPoint( ( i + 1 - offset ) % numPoints, point3 )
currPointsForSurface.InsertNextPoint( point3[ 0 ], point3[ 1 ], point3[ 2 ] )
point4 = [ 0, 0, 0 ]
hullPoints2.GetPoint( ( - ( i + 1 ) ) % numPoints, point4 )
currPointsForSurface.InsertNextPoint( point4[ 0 ], point4[ 1 ], point4[ 2 ] )
# We know what the triangles should look like, so create them manually
# Note: The order here is important - ensure the triangles face the correct way
triangle1 = vtk.vtkTriangle()
triangle1.GetPointIds().SetId( 0, 0 )
triangle1.GetPointIds().SetId( 1, 1 )
triangle1.GetPointIds().SetId( 2, 2 )
triangle2 = vtk.vtkTriangle()
triangle2.GetPointIds().SetId( 0, 3 )
triangle2.GetPointIds().SetId( 1, 2 )
triangle2.GetPointIds().SetId( 2, 1 )
triangles = vtk.vtkCellArray()
triangles.InsertNextCell( triangle1 )
triangles.InsertNextCell( triangle2 )
currPolyDataForSurface = vtk.vtkPolyData()
currPolyDataForSurface.SetPoints( currPointsForSurface )
currPolyDataForSurface.SetPolys( triangles )
joiningAppend.AddInputData( currPolyDataForSurface )
joiningAppend.Update()
return joiningAppend.GetOutput()
def FindExtremePoint( self, surfacePoints, surfaceBase, surfaceNormal ):
extremePointIndex = 1
extremePointProjection = 0
# Find the point with the smallest projection (ie most negative) onto the surface normal
for i in range( surfacePoints.GetNumberOfPoints() ):
currPoint = [ 0, 0, 0 ]
surfacePoints.GetPoint( i, currPoint )
currProjection = ( currPoint[ 0 ] - surfaceBase [ 0 ] ) * surfaceNormal [ 0 ] + ( currPoint[ 1 ] - surfaceBase [ 1 ] ) * surfaceNormal [ 1 ] + ( currPoint[ 2 ] - surfaceBase [ 2 ] ) * surfaceNormal [ 2 ]
if ( currProjection < extremePointProjection ):
extremePointProjection = currProjection
extremePointIndex = i
return extremePointIndex
def ReverseNormals( self, extremePointIndex, surfacePolyData, surfaceNormal ):
normalArray = surfacePolyData.GetPointData().GetNormals()
extremePointNormal = [ 0, 0, 0 ]
normalArray.GetTuple( extremePointIndex, extremePointNormal )
if ( vtk.vtkMath.Dot( extremePointNormal, surfaceNormal ) > 0 ):
return True
return False
class TissueModelCreatorTest(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_TissueModelCreator1()
def test_TissueModelCreator1(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 = TissueModelCreatorLogic()
self.assertTrue( logic.hasImageData(volumeNode) )
self.delayDisplay('Test passed!')