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
|
## Program: VMTK
## Module: $RCSfile: vmtksurfaceloopextraction.py,v $
## Language: Python
## Date: $Date: 2014/10/24 16:35:13 $
## Version: $Revision: 1.10 $
## Copyright (c) Luca Antiga, David Steinman. All rights reserved.
## See LICENCE file for details.
## This software is distributed WITHOUT ANY WARRANTY; without even
## the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
## PURPOSE. See the above copyright notices for more information.
## Note: this class was contributed by
## Elena Faggiano (elena.faggiano@gmail.com)
## Politecnico di Milano
import vtk
import sys
import vmtkrenderer
import pypes
import vmtkscripts
vmtksurfaceloopextraction = 'vmtkSurfaceLoopExtraction'
class vmtkSurfaceLoopExtraction(pypes.pypeScript):
def __init__(self):
pypes.pypeScript.__init__(self)
self.Surface = None
self.Opacity = 1.0
self.vmtkRenderer = None
self.OwnRenderer = 0
self.ContourWidget = None
self.Interpolator = None
#Output members
self.Loop = None
self.SetScriptName('vmtksurfaceloopextraction')
self.SetScriptDoc('')
self.SetInputMembers([
['Surface','i','vtkPolyData',1,'','the input surface','vmtksurfacereader'],
['vmtkRenderer','renderer','vmtkRenderer',1,'','external renderer'],
['Opacity','opacity','float',1,'(0.0,1.0)','object opacity in the scene'],
])
self.SetOutputMembers([
['Surface','o','vtkPolyData',1,'','the output surface','vmtksurfacewriter'],
['Loop','oloop','vtkPolyData',1,'','the output loop','vmtksurfacewriter'],
])
def AnswerValidator(self,text):
if not text:
return 0
if not text.split():
return 0
if int(text) != 1 and int(text) != 2 :
return 0
return 1
def AnswerRegionValidator(self,text):
if not text:
return 0
if not text.split():
return 0
if text != "y" and text != "n":
return 0
return 1
def LoopCallback(self, obj):
rep = vtk.vtkOrientedGlyphContourRepresentation.SafeDownCast(self.ContourWidget.GetRepresentation())
polycontour=vtk.vtkPolyData()
polycontour=rep.GetContourRepresentationAsPolyData()
self.Loop = polycontour
self.InputInfo("loop generated. Press q to quit \n")
def DeleteContourCallback(self, obj):
self.ContourWidget.Initialize()
def InteractCallback(self, obj):
if self.ContourWidget.GetEnabled() == 1:
self.ContourWidget.SetEnabled(0)
else:
self.ContourWidget.SetEnabled(1)
def Display(self):
self.vmtkRenderer.Render()
def Execute(self):
if self.Surface == None:
self.PrintError('Error: no Surface.')
if not self.vmtkRenderer:
self.vmtkRenderer = vmtkrenderer.vmtkRenderer()
self.vmtkRenderer.Initialize()
self.OwnRenderer = 1
self.vmtkRenderer.RegisterScript(self)
triangleFilter = vtk.vtkTriangleFilter()
triangleFilter.SetInputData(self.Surface)
triangleFilter.Update()
self.Surface = triangleFilter.GetOutput()
self.tagviewer = vmtkscripts.vmtkSurfaceViewer()
self.tagviewer.Surface = self.Surface
self.tagviewer.vmtkRenderer = self.vmtkRenderer
self.tagviewer.Representation = 'edges'
self.tagviewer.Opacity = self.Opacity
self.tagviewer.Execute()
self.ContourWidget = vtk.vtkContourWidget()
self.ContourWidget.SetInteractor(self.vmtkRenderer.RenderWindowInteractor)
rep = vtk.vtkOrientedGlyphContourRepresentation.SafeDownCast(self.ContourWidget.GetRepresentation())
rep.GetLinesProperty().SetColor(1, 0, 0)
rep.GetLinesProperty().SetLineWidth(4.0)
pointPlacer = vtk.vtkPolygonalSurfacePointPlacer()
pointPlacer.AddProp(self.tagviewer.Actor)
pointPlacer.GetPolys().AddItem(self.Surface)
rep.SetPointPlacer(pointPlacer)
self.Interpolator = vtk.vtkPolygonalSurfaceContourLineInterpolator()
self.Interpolator.GetPolys().AddItem(self.Surface)
rep.SetLineInterpolator(self.Interpolator)
self.InputInfo("Building loop ...\n")
self.vmtkRenderer.AddKeyBinding('space','Generate loop',self.LoopCallback)
self.vmtkRenderer.AddKeyBinding('d','Delete contour',self.DeleteContourCallback)
self.vmtkRenderer.AddKeyBinding('i','Start/stop contour drawing',self.InteractCallback)
self.Display()
if self.OwnRenderer:
self.vmtkRenderer.Deallocate()
if __name__ == '__main__':
main = pypes.pypeMain()
main.Arguments = sys.argv
main.Execute()
|