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): if debug: 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)
class SurfParams(object): label = "Surface" colorName, color_ = colorSeq[0] intensity = 80. _opacity = 1.0 useConnect = True useDecimate = False def __init__(self, imageData, intensity, color=None): self._uuid = uuid.uuid1() if intensity!=None: self.intensity = intensity if color==None: color=self.color_ self.set_color(color) self.connect = ConnectFilter() self.deci = DecimateFilter() self.marchingCubes = vtk.vtkMarchingCubes() self.marchingCubes.SetInput(imageData) self.output = vtk.vtkPassThrough() self.prog = ProgressBarDialog( title='Rendering surface %s' % self.label, parent=None, msg='Marching cubes ....', size=(300,40), ) self.prog.set_modal(True) 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.update_pipeline() self.notify_add_surface(self.output) def update_pipeline(self): pipe = self.marchingCubes if self.useConnect: self.connect.SetInputConnection( pipe.GetOutputPort()) pipe = self.connect if self.useDecimate: self.deci.SetInputConnection( pipe.GetOutputPort()) pipe = self.deci self.output.SetInputConnection( pipe.GetOutputPort() ) self.update_properties() def notify_add_surface(self, pipe): EventHandler().notify("add surface", self._uuid, pipe, self._color) def notify_remove_surface(self): EventHandler().notify("remove surface", self._uuid) def notify_color_surface(self, color): EventHandler().notify("color surface", self._uuid, color) def notify_change_surface_opacity(self, opacity): EventHandler().notify("change surface opacity", self._uuid, opacity) def update_properties(self): self.marchingCubes.SetValue(0, self.intensity) self.notify_color_surface(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): self.notify_remove_surface() def set_color(self,color, color_name=""): #print color, type(color) self.colorName = color_name if type(color)==gtk.gdk.Color: self._color = gdkColor2tuple(color) else: self._color = color self.notify_color_surface(self._color) def get_color(self): return self._color def set_opacity(self,opacity): self._opacity = opacity self.notify_change_surface_opacity(self._opacity) def get_opacity(self): return self._opacity def get_uuid(self): return self._uuid color = property(get_color,set_color) opacity = property(get_opacity,set_opacity) uuid = property(get_uuid) def destroy(self): self.notify_remove_surface()
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): if debug: 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)
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
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) if debug: 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