示例#1
0
    def __init__(self):
        prog = ProgressBarDialog(
            title='Rendering surface',
            parent=None,
            msg='Decimating data....',
            size=(300, 40),
        )

        def start(o, event):
            prog.show()
            while gtk.events_pending():
                gtk.main_iteration()

        def progress(o, event):
            val = o.GetProgress()
            prog.bar.set_fraction(val)
            while gtk.events_pending():
                gtk.main_iteration()

        def end(o, event):
            prog.hide()
            while gtk.events_pending():
                gtk.main_iteration()

        self.AddObserver('StartEvent', start)
        self.AddObserver('ProgressEvent', progress)
        self.AddObserver('EndEvent', end)
示例#2
0
    def __init__(self, renderer, interactor):

        self.connect = ConnectFilter()
        self.deci = DecimateFilter()
        self.marchingCubes = vtk.vtkMarchingCubes()

        self.prog = ProgressBarDialog(
            title='Rendering surface %s' % self.label,
            parent=None,
            msg='Marching cubes ....',
            size=(300,40),
                                 )
        def start(o, event):
            self.prog.show()
            while gtk.events_pending(): gtk.main_iteration()


        def progress(o, event):
            val = o.GetProgress()
            self.prog.bar.set_fraction(val)            
            while gtk.events_pending(): gtk.main_iteration()
            
        def end(o, event):
            self.prog.hide()
            while gtk.events_pending(): gtk.main_iteration()

        self.marchingCubes.AddObserver('StartEvent', start)
        self.marchingCubes.AddObserver('ProgressEvent', progress)
        self.marchingCubes.AddObserver('EndEvent', end)
        self.renderer = renderer
        self.interactor = interactor
        self.isoActor = None
        
        self.update_pipeline()
示例#3
0
    def __init__(self, renderer, interactor):

        self.connect = ConnectFilter()
        self.deci = DecimateFilter()
        self.marchingCubes = vtk.vtkMarchingCubes()

        self.prog = ProgressBarDialog(
            title='Rendering surface %s' % self.label,
            parent=None,
            msg='Marching cubes ....',
            size=(300, 40),
        )

        def start(o, event):
            self.prog.show()
            while gtk.events_pending():
                gtk.main_iteration()

        def progress(o, event):
            val = o.GetProgress()
            self.prog.bar.set_fraction(val)
            while gtk.events_pending():
                gtk.main_iteration()

        def end(o, event):
            self.prog.hide()
            while gtk.events_pending():
                gtk.main_iteration()

        self.marchingCubes.AddObserver('StartEvent', start)
        self.marchingCubes.AddObserver('ProgressEvent', progress)
        self.marchingCubes.AddObserver('EndEvent', end)
        self.renderer = renderer
        self.interactor = interactor
        self.isoActor = None

        self.update_pipeline()
示例#4
0
def get_reader(o):

    if o.readerClass=='vtkBMPReader':
        ReaderClass = vtk.vtkBMPReader
    elif o.readerClass=='vtkImageReader2':
        ReaderClass = vtk.vtkImageReader2
    elif o.readerClass=='vtkDICOMImageReader':
        ReaderClass = vtk.vtkDICOMImageReader
    elif o.readerClass=='vtkNiftiImageReader':
        ReaderClass = vtkNiftiImageReader
    reader = ReaderClass()
    
    if ReaderClass==vtk.vtkImageReader2:
        reader.SetDataScalarTypeToUnsignedShort()
        if o.order=='big endian':
            reader.SetDataByteOrderToBigEndian()
        else:
            reader.SetDataByteOrderToLittleEndian()
        rows, cols = o.dimensions
        reader.SetFileNameSliceOffset(o.first)
        reader.SetDataExtent(0, rows-1, 0, cols-1, 0, o.last-o.first)
        reader.FileLowerLeftOn()
        if o.mask is not None:
            reader.SetDataMask(o.mask)

        if o.header!=0:
            reader.SetHeaderSize(o.header)   

    elif ReaderClass==vtk.vtkBMPReader:
        reader.SetDataExtent(0, o.dimensions[0]-1,
                             0, o.dimensions[1]-1,
                             o.first, o.last)
    elif ReaderClass==vtk.vtkDICOMImageReader:
        reader.SetDirectoryName(o.dir)
        reader.Update()
        
        #Update Param file from the reader 
        o.dimensions = [reader.GetWidth() , reader.GetHeight()]
        
        if reader.GetDataByteOrder()==1:
            o.order=='big endian'
        else:
            o.order=='little endian'
            
        o.spacing = spc[2]
        o.dfov    = o.dimensions[0] * spc[0]
    elif ReaderClass==vtkNiftiImageReader:
        reader.SetDirectoryName(o.dir)
        pattern = o.pattern 
        if len(o.extension) > 0:
            pattern += '.' + o.extension
        reader.SetFilePattern(pattern)
        reader.Update()
        o.dimensions = [reader.GetWidth() , reader.GetHeight()]

        spc = reader.GetDataSpacing()
        o.spacing = spc[2]
        o.dfov    = o.dimensions[0] * spc[0]
        
    else:
        raise NotImplementedError, "Can't handle reader %s" % o.readerClass

    if ReaderClass!=vtkNiftiImageReader:
        progressDlg = ProgressBarDialog(title='Loading files',
                                        parent=widgets['dlgReader'],
                                        msg='Almost there....',
                                        size=(300,40)
                                        )
        progressDlg.show()

        def progress(r, event):
            #print 'abort progress', r.GetAbortExecute()
            val = r.GetProgress()
            progressDlg.bar.set_fraction(val)            
            if val==1: progressDlg.destroy()
            while gtk.events_pending(): gtk.main_iteration()
        reader.AddObserver('ProgressEvent', progress)


    if os.path.isdir(o.dir): dir = o.dir
    else:
       infopath = widgets['entryInfoFile'].get_text()
       dir, fname = os.path.split(infopath)
    
    if ReaderClass!=vtkNiftiImageReader:
        reader.SetFilePrefix(dir)
        pattern = os.path.join( '%s', o.prefix + o.pattern )
        if len(o.extension) > 0:
            pattern += '.' + o.extension
        reader.SetFilePattern(pattern)
    
        print "reader.SetDataSpacing(", o.dfov/o.dimensions[0], o.dfov/o.dimensions[1], o.spacing , "). dfov=", o.dfov, "dimensions=", o.dimensions, "spacing=", o.spacing
                              
       
        reader.SetDataSpacing(float(o.dfov)/float(o.dimensions[0]),
                              float(o.dfov)/float(o.dimensions[1]),
                              float(o.spacing) )
    reader.Update()
    return reader
示例#5
0
class SurfParams(Viewer):
    """
    CLASS: SurfParams
    DESCR:

      Public attrs:
    
      color       # a normed rgb
      intensity   # intensity to segment on
      label       # name of segment
      useConnect  # boolean, whether to use ConnectFilter
      useDecimate # boolean, whether to use DecimateFilter
      connect     # a ConnectFilter or None
      deci        # a DecimateFilter or None
      imageData   # default None
    """

    label, color = colorSeq[0]
    intensity = 3000

    useConnect = True
    useDecimate = False

    def __init__(self, renderer, interactor):

        self.connect = ConnectFilter()
        self.deci = DecimateFilter()
        self.marchingCubes = vtk.vtkMarchingCubes()

        self.prog = ProgressBarDialog(
            title='Rendering surface %s' % self.label,
            parent=None,
            msg='Marching cubes ....',
            size=(300, 40),
        )

        def start(o, event):
            self.prog.show()
            while gtk.events_pending():
                gtk.main_iteration()

        def progress(o, event):
            val = o.GetProgress()
            self.prog.bar.set_fraction(val)
            while gtk.events_pending():
                gtk.main_iteration()

        def end(o, event):
            self.prog.hide()
            while gtk.events_pending():
                gtk.main_iteration()

        self.marchingCubes.AddObserver('StartEvent', start)
        self.marchingCubes.AddObserver('ProgressEvent', progress)
        self.marchingCubes.AddObserver('EndEvent', end)
        self.renderer = renderer
        self.interactor = interactor
        self.isoActor = None

        self.update_pipeline()

    def update_pipeline(self):

        if self.isoActor is not None:
            self.renderer.RemoveActor(self.isoActor)

        pipe = self.marchingCubes

        if self.useConnect:
            self.connect.SetInput(pipe.GetOutput())
            pipe = self.connect

        if self.useDecimate:
            self.deci.SetInput(pipe.GetOutput())
            pipe = self.deci

        if 0:
            plane = vtk.vtkPlane()
            clipper = vtk.vtkClipPolyData()
            polyData = pipe.GetOutput()

            clipper.SetInput(polyData)
            clipper.SetClipFunction(plane)
            clipper.InsideOutOff()
            pipe = clipper

            def callback(pw, event):
                pw.GetPlane(plane)
                self.interactor.Render()

            self.planeWidget = vtk.vtkImplicitPlaneWidget()
            self.planeWidget.SetInteractor(self.interactor)
            self.planeWidget.On()
            self.planeWidget.SetPlaceFactor(1.0)
            self.planeWidget.SetInput(polyData)
            self.planeWidget.PlaceWidget()
            self.planeWidget.AddObserver("InteractionEvent", callback)

        self.isoMapper = vtk.vtkPolyDataMapper()
        self.isoMapper.SetInput(pipe.GetOutput())
        self.isoMapper.ScalarVisibilityOff()

        self.isoActor = vtk.vtkActor()
        self.isoActor.SetMapper(self.isoMapper)
        self.renderer.AddActor(self.isoActor)
        self.update_properties()

    def set_image_data(self, imageData):
        print "SurfParams.set_image_data(", imageData, ")"
        self.marchingCubes.SetInput(imageData)
        x1, x2, y1, y2, z1, z2 = imageData.GetExtent()
        sx, sy, sz = imageData.GetSpacing()
        if 0:
            self.planeWidget.PlaceWidget(
                (x1 * sx, x2 * sx, y1 * sy, y2 * sy, z1 * sz, z2 * sz))

    def update_properties(self):
        self.marchingCubes.SetValue(0, self.intensity)
        self.isoActor.GetProperty().SetColor(self.color)

        if self.useConnect: self.connect.update()
        if self.useDecimate: self.deci.update()

    def update_viewer(self, event, *args):
        if event == 'set image data':
            imageData = args[0]
            self.set_image_data(imageData)

    def __del__(self):
        if self.isoActor is not None:
            self.renderer.RemoveActor(self.isoActor)
示例#6
0
class SurfParams(Viewer):
    """
    CLASS: SurfParams
    DESCR:

      Public attrs:
    
      color       # a normed rgb
      intensity   # intensity to segment on
      label       # name of segment
      useConnect  # boolean, whether to use ConnectFilter
      useDecimate # boolean, whether to use DecimateFilter
      connect     # a ConnectFilter or None
      deci        # a DecimateFilter or None
      imageData   # default None
    """

    label, color  = colorSeq[0]
    intensity     = 3000

    useConnect    = True
    useDecimate   = False

    def __init__(self, renderer, interactor):

        self.connect = ConnectFilter()
        self.deci = DecimateFilter()
        self.marchingCubes = vtk.vtkMarchingCubes()

        self.prog = ProgressBarDialog(
            title='Rendering surface %s' % self.label,
            parent=None,
            msg='Marching cubes ....',
            size=(300,40),
                                 )
        def start(o, event):
            self.prog.show()
            while gtk.events_pending(): gtk.main_iteration()


        def progress(o, event):
            val = o.GetProgress()
            self.prog.bar.set_fraction(val)            
            while gtk.events_pending(): gtk.main_iteration()
            
        def end(o, event):
            self.prog.hide()
            while gtk.events_pending(): gtk.main_iteration()

        self.marchingCubes.AddObserver('StartEvent', start)
        self.marchingCubes.AddObserver('ProgressEvent', progress)
        self.marchingCubes.AddObserver('EndEvent', end)
        self.renderer = renderer
        self.interactor = interactor
        self.isoActor = None
        
        self.update_pipeline()

    def update_pipeline(self):

        if self.isoActor is not None:
            self.renderer.RemoveActor(self.isoActor)

        
        
        pipe = self.marchingCubes


        if self.useConnect:
            self.connect.SetInput( pipe.GetOutput())
            pipe = self.connect

        if self.useDecimate:
            self.deci.SetInput( pipe.GetOutput())
            pipe = self.deci

        if 0:
            plane = vtk.vtkPlane()
            clipper = vtk.vtkClipPolyData()
            polyData = pipe.GetOutput()

            clipper.SetInput(polyData)
            clipper.SetClipFunction(plane)
            clipper.InsideOutOff()
            pipe = clipper

            def callback(pw, event):
                pw.GetPlane(plane)
                self.interactor.Render()
            self.planeWidget = vtk.vtkImplicitPlaneWidget()
            self.planeWidget.SetInteractor(self.interactor)
            self.planeWidget.On()
            self.planeWidget.SetPlaceFactor(1.0)
            self.planeWidget.SetInput(polyData)
            self.planeWidget.PlaceWidget()
            self.planeWidget.AddObserver("InteractionEvent", callback)
        
        
        self.isoMapper = vtk.vtkPolyDataMapper()
        self.isoMapper.SetInput(pipe.GetOutput())
        self.isoMapper.ScalarVisibilityOff()

        self.isoActor = vtk.vtkActor()
        self.isoActor.SetMapper(self.isoMapper)
        self.renderer.AddActor(self.isoActor)
        self.update_properties()

    def set_image_data(self, imageData):
        print "SurfParams.set_image_data(", imageData,")"
        self.marchingCubes.SetInput(imageData)
        x1,x2,y1,y2,z1,z2 = imageData.GetExtent()
        sx, sy, sz = imageData.GetSpacing()
        if 0:
            self.planeWidget.PlaceWidget((x1*sx, x2*sx, y1*sy, y2*sy, z1*sz, z2*sz))

    def update_properties(self):
        self.marchingCubes.SetValue(0, self.intensity)
        self.isoActor.GetProperty().SetColor(self.color)

        if self.useConnect:  self.connect.update()
        if self.useDecimate: self.deci.update()

    def update_viewer(self, event, *args):
        if event=='set image data':
            imageData = args[0]
            self.set_image_data(imageData)       


    def __del__(self):
        if self.isoActor is not None:
            self.renderer.RemoveActor(self.isoActor)