-
Notifications
You must be signed in to change notification settings - Fork 0
/
seedselector.py
150 lines (122 loc) · 5.16 KB
/
seedselector.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
# From https://github.com/catactg/SUM
# adapted from VMTK centerlines
import vtk
from vmtk import vmtkrenderer
class vmtkSeedSelector(object):
def __init__(self):
self._Surface = None
self._SeedIds = None
self._SourceSeedIds = vtk.vtkIdList()
def SetSurface(self, surface):
self._Surface = surface
def GetSurface(self):
return self._Surface
def GetSourceSeedIds(self):
return self._SourceSeedIds
def Execute(self):
pass
class vmtkPickPointSeedSelector(vmtkSeedSelector):
def __init__(self):
vmtkSeedSelector.__init__(self)
self.PickedSeedIds = vtk.vtkIdList()
self.PickedSeeds = vtk.vtkPolyData()
self.vmtkRenderer = None
self.OwnRenderer = 0
def UndoCallback(self, obj):
self.InitializeSeeds()
self.PickedSeeds.Modified()
self.vmtkRenderer.RenderWindow.Render()
def PickCallback(self, obj):
picker = vtk.vtkCellPicker()
picker.SetTolerance(1E-4 * self._Surface.GetLength())
eventPosition = self.vmtkRenderer.RenderWindowInteractor.GetEventPosition()
result = picker.Pick(float(eventPosition[0]),float(eventPosition[1]),0.0,self.vmtkRenderer.Renderer)
if result == 0:
return
pickPosition = picker.GetPickPosition()
pickedCellPointIds = self._Surface.GetCell(picker.GetCellId()).GetPointIds()
minDistance = 1E10
pickedSeedId = -1
for i in range(pickedCellPointIds.GetNumberOfIds()):
distance = vtk.vtkMath.Distance2BetweenPoints(pickPosition,self._Surface.GetPoint(pickedCellPointIds.GetId(i)))
if distance < minDistance:
minDistance = distance
pickedSeedId = pickedCellPointIds.GetId(i)
if pickedSeedId == -1:
pickedSeedId = pickedCellPointIds.GetId(0)
self.PickedSeedIds.InsertNextId(pickedSeedId)
point = self._Surface.GetPoint(pickedSeedId)
self.PickedSeeds.GetPoints().InsertNextPoint(point)
self.PickedSeeds.Modified()
self.vmtkRenderer.RenderWindow.Render()
def InitializeSeeds(self):
self.PickedSeedIds.Initialize()
self.PickedSeeds.Initialize()
seedPoints = vtk.vtkPoints()
self.PickedSeeds.SetPoints(seedPoints)
def Execute(self):
self._SourceSeedIds.Initialize()
if not self.vmtkRenderer:
self.vmtkRenderer = vmtkrenderer.vmtkRenderer()
self.vmtkRenderer.Initialize()
self.OwnRenderer = 1
glyphs = vtk.vtkGlyph3D()
glyphSource = vtk.vtkSphereSource()
if vtk.vtkVersion.GetVTKMajorVersion() > 5:
glyphs.SetInputData(self.PickedSeeds)
else:
glyphs.SetInput(self.PickedSeeds)
# if vtk.vtkVersion.GetVTKMajorVersion() > 5:
# glyphs.SetSourceData(glyphSource.GetOutput())
# else:
# glyphs.SetSource(glyphSource.GetOutput())
if vtk.vtkVersion.GetVTKMajorVersion() > 5:
glyphs.SetSourceConnection(glyphSource.GetOutputPort())
else:
glyphs.SetSourceConnection(glyphSource.GetOutputPort())
glyphs.SetScaleModeToDataScalingOff()
glyphs.SetScaleFactor(self._Surface.GetLength()*0.01)
glyphMapper = vtk.vtkPolyDataMapper()
# if vtk.vtkVersion.GetVTKMajorVersion() > 5:
# glyphMapper.SetInputData(glyphs.GetOutput())
# else:
# glyphMapper.SetInput(glyphs.GetOutput())
glyphMapper.SetInputConnection(glyphs.GetOutputPort())
self.SeedActor = vtk.vtkActor()
self.SeedActor.SetMapper(glyphMapper)
self.SeedActor.GetProperty().SetColor(1.0,0.0,0.0)
self.SeedActor.PickableOff()
self.vmtkRenderer.Renderer.AddActor(self.SeedActor)
self.vmtkRenderer.AddKeyBinding('u','Undo.',self.UndoCallback)
self.vmtkRenderer.AddKeyBinding('space','Add points.',self.PickCallback)
surfaceMapper = vtk.vtkPolyDataMapper()
if vtk.vtkVersion.GetVTKMajorVersion() > 5:
surfaceMapper.SetInputData(self._Surface)
else:
surfaceMapper.SetInput(self._Surface)
surfaceMapper.ScalarVisibilityOff()
surfaceActor = vtk.vtkActor()
surfaceActor.SetMapper(surfaceMapper)
surfaceActor.GetProperty().SetOpacity(1)
self.vmtkRenderer.Renderer.AddActor(surfaceActor)
# # create a text actor
# txt = vtk.vtkTextActor()
# info = "Position mouse and press space. "
# info += "Select seeds in this order: RSpv, RIpv, LIpv, LSpv. "
# info += "Fifth seed requires --use_laa_seed command."
# txt.SetInput(info)
# txtprop=txt.GetTextProperty()
# txtprop.SetFontFamilyToArial()
# txtprop.SetFontSize(13)
# txtprop.SetColor(1, 1, 1)
# txt.SetDisplayPosition(0, 10)
# self.vmtkRenderer.Renderer.AddActor(txt)
#
any = 0
while any == 0:
self.InitializeSeeds()
self.vmtkRenderer.Render()
any = self.PickedSeedIds.GetNumberOfIds()
self._SourceSeedIds.DeepCopy(self.PickedSeedIds)
if self.OwnRenderer:
self.vmtkRenderer.Deallocate()