def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Create and setup the default objects. self.seed = SourceWidget() self.stream_tracer = tvtk.StreamTracer( maximum_propagation=50, integration_direction='forward', compute_vorticity=True, integrator_type='runge_kutta4', ) self.ribbon_filter = tvtk.RibbonFilter() self.tube_filter = tvtk.TubeFilter() self.actor = Actor() # Setup the actor suitably for this module. self.actor.property.line_width = 2.0
def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Create the objects and set them up. self.implicit_plane = ImplicitPlane() self.cutter = Cutter() self.glyph = Glyph(module=self, scale_mode='scale_by_vector', color_mode='color_by_vector', show_scale_mode=False) self.glyph.glyph_source.glyph_position = 'tail' actor = self.actor = Actor() actor.mapper.scalar_visibility = 1 actor.property.set(line_width=2, backface_culling=False, frontface_culling=False)
def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ self.widget.on_trait_change(self._start_position_changed) self.streamline = tvtk.HyperStreamline() self.streamline.start_position = self.widget.position self.streamline.integrate_minor_eigenvector() self.streamline.maximum_propagation_distance = 10.0 self.streamline.integration_step_length = 0.1 self.streamline.step_length = 0.01 self.streamline.radius = 0.25 self.streamline.number_of_sides = 18 self.streamline.integration_direction = 2 #integrate both direction self.streamline.on_trait_change(self.render) self.actor = Actor() self.widgets.append(self.widget)
def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Create the components self.grid_plane = GridPlane() self.contour = Contour(auto_contours=True, number_of_contours=10) self.actor = Actor()
def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Create and setup the default objects. self.seed = SourceWidget() self.stream_tracer = tvtk.StreamTracer(maximum_propagation=50, integration_direction='forward', compute_vorticity=True, integrator_type='runge_kutta4', ) self.ribbon_filter = tvtk.RibbonFilter() self.tube_filter = tvtk.TubeFilter() self.actor = Actor() # Setup the actor suitably for this module. self.actor.property.line_width = 2.0
def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Create the components self.contour = Contour(show_filled_contours=False) self.normals = PolyDataNormals() self.actor = Actor() # Setup the actor suitably for this module. self.actor.mapper.scalar_visibility = 1
def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Create the components self.grid_plane = grid_plane.GridPlane() self.actor = Actor() # Setup the actor suitably for this module. prop = self.actor.property prop.set(backface_culling=0, frontface_culling=0, representation='w') self.actor.mapper.scalar_visibility = 0
def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Create the components and set them up. self.implicit_plane = ImplicitPlane() ex = tvtk.ExtractGeometry(extract_only_boundary_cells=1, extract_boundary_cells=1) self.extract_geometry = ex self.geom_filter = tvtk.GeometryFilter() # Setup the actor suitably for this module. self.actor = Actor() self.actor.property.representation = 'w'
def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Create the objects. self.implicit_plane = ImplicitPlane() self.cutter = Cutter() self.contour = Contour(auto_contours=True, number_of_contours=10) self.warp_scalar = WarpScalar() self.normals = PolyDataNormals() self.actor = Actor() # Setup the actor suitably for this module. prop = self.actor.property prop.line_width = 2.0
def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ self.vector_text = tvtk.VectorText(text=self.text) self.outputs = [self.vector_text.output] self.actor = Actor() self._text_changed(self.text)
def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Setup the glyphs. self.glyph = glyph.Glyph(glyph_type='tensor') self.glyph.glyph_source.glyph_source = self.glyph.glyph_source.glyph_list[ 4] self.actor = Actor()
def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # When any trait on the outline filters change call the render # method. self._full_outline.on_trait_change(self.render) self._cornered_outline.on_trait_change(self.render) self.actor = Actor()
def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Create the objects and set them up. self.implicit_plane = ImplicitPlane() self.cutter = Cutter() self.warp_vector = WarpVector() self.normals = PolyDataNormals() actor = self.actor = Actor() actor.mapper.scalar_visibility = 1
class SliceUnstructuredGrid(Module): """This module takes a slice of the unstructured grid data and shows the cells that intersect or touch the slice.""" # The version of this class. Used for persistence. __version__ = 0 # The implicit plane widget. implicit_plane = Instance(ImplicitPlane, allow_none=False, record=True) # Extract the cells to display. extract_geometry = Instance(tvtk.ExtractGeometry, allow_none=False, record=True) # The geometry filter. geom_filter = Instance(tvtk.GeometryFilter, allow_none=False, record=True) # The actor component that represents the visualization. actor = Instance(Actor, allow_none=False, record=True) input_info = PipelineInfo(datasets=['unstructured_grid'], attribute_types=['any'], attributes=['any']) ######################################## # View related code. view = View(Group(Item(name='implicit_plane', style='custom'), label='ImplicitPlane', show_labels=False), Group(Item(name='extract_geometry', style='custom', resizable=True), label='Extract Geometry', show_labels=False), Group(Item(name='geom_filter', style='custom', resizable=True), label='Geometry Filter', show_labels=False), Group(Item(name='actor', style='custom'), label='Actor', show_labels=False) ) ###################################################################### # `Module` interface ###################################################################### def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Create the components and set them up. self.implicit_plane = ImplicitPlane() ex = tvtk.ExtractGeometry(extract_only_boundary_cells=1, extract_boundary_cells=1) self.extract_geometry = ex self.geom_filter = tvtk.GeometryFilter() # Setup the actor suitably for this module. self.actor = Actor() self.actor.property.representation = 'w' def update_pipeline(self): """Override this method so that it *updates* the tvtk pipeline when data upstream is known to have changed. This method is invoked (automatically) when any of the inputs sends a `pipeline_changed` event. """ mod_mgr = self.module_manager if mod_mgr is None: return # Data is available, so set the input for the grid plane. input = mod_mgr.source.outputs[0] if not input.is_a('vtkUnstructuredGrid'): error('SliceUnstructuredGrid only works with input '\ 'unstructured grids') self.implicit_plane.inputs = [mod_mgr.source] self.extract_geometry.input = self.module_manager.source.outputs[0] # Set the LUT for the mapper. self.actor.set_lut(self.module_manager.scalar_lut_manager.lut) # Now flush the pipeline self.pipeline_changed = True def update_data(self): """Override this method so that it flushes the vtk pipeline if that is necessary. This method is invoked (automatically) when any of the inputs sends a `data_changed` event. """ # Just set data_changed, the components should do the rest if # they are connected. self.data_changed = True ###################################################################### # Non-public methods. ###################################################################### def _implicit_plane_changed(self, old, new): ex = self.extract_geometry if ex is not None: ex.implicit_function = new.plane self._change_components(old, new) def _extract_geometry_changed(self, old, new): if old is not None: old.on_trait_change(self.render, remove=True) mm = self.module_manager if mm is not None: new.input = mm.source.outputs[0] ip = self.implicit_plane if ip is not None: new.implicit_function = ip.plane gf = self.geom_filter if gf is not None: gf.input = new.output new.on_trait_change(self.render) self.update_pipeline() def _geom_filter_changed(self, old, new): if old is not None: old.on_trait_change(self.render, remove=True) ex = self.extract_geometry if ex is not None: new.input = ex.output new.on_trait_change(self.render) self.outputs = [new.output] def _actor_changed(self, old, new): new.scene = self.scene new.inputs = [self] self._change_components(old, new)
class IsoSurface(Module): # The version of this class. Used for persistence. __version__ = 0 # The contour component. contour = Instance(Contour, record=True) # Specify if normals are to be computed to make a smoother surface. compute_normals = Bool(True, desc='if normals are to be computed '\ 'to make the iso-surface smoother') # The component that computes the normals. normals = Instance(PolyDataNormals, record=True) # The actor component that represents the iso-surface. actor = Instance(Actor, record=True) input_info = PipelineInfo(datasets=['any'], attribute_types=['any'], attributes=['scalars']) ######################################## # The view of this object. # Commented out, since we are now using the iso_surface_view.py version. #view = View([Group( # Item( name = 'contour', # style = 'custom' ), # show_labels = False, # show_border = True, # label = 'Contours' ), # Group( # Item( name = 'compute_normals' ), # '_', # Item( name = 'normals', # style = 'custom', # show_label = False, # enabled_when = 'compute_normals' ), # show_border = True, # label = 'Normals' ), # Group( # Item( name = 'actor', # style = 'custom' ), # show_labels = False ) # ] # ) ###################################################################### # `Module` interface ###################################################################### def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Create the components self.contour = Contour(show_filled_contours=False) self.normals = PolyDataNormals() self.actor = Actor() # Setup the actor suitably for this module. self.actor.mapper.scalar_visibility = 1 def update_pipeline(self): """Override this method so that it *updates* the tvtk pipeline when data upstream is known to have changed. This method is invoked (automatically) when any of the inputs sends a `pipeline_changed` event. """ mm = self.module_manager if mm is None: return # Data is available, so set the input for the grid plane. self.contour.inputs = [mm.source] # Force the normals setting to be noted. self._compute_normals_changed(self.compute_normals) # Set the LUT for the mapper. self.actor.set_lut(mm.scalar_lut_manager.lut) self.pipeline_changed = True def update_data(self): """Override this method so that it flushes the vtk pipeline if that is necessary. This method is invoked (automatically) when any of the inputs sends a `data_changed` event. """ # Just set data_changed, the component should do the rest. self.data_changed = True ###################################################################### # Non-public interface. ###################################################################### def _compute_normals_changed(self, value): if self.module_manager is None: return actor = self.actor if value: if actor: actor.inputs = [self.normals] else: if actor: actor.inputs = [self.contour] self.render() def _contour_changed(self, old, new): normals = self.normals if normals is not None: normals.inputs = [new] self._change_components(old, new) def _normals_changed(self, old, new): contour = self.contour if contour is not None: new.inputs = [contour] self._change_components(old, new) def _actor_changed(self, old, new): # Here we set the inputs in any case to avoid VTK pipeline # errors. The pipeline is corrected when update_pipeline is # called anyway. contour = self.contour if contour is not None: new.inputs = [contour] self._change_components(old, new)
class GridPlane(Module): # The version of this class. Used for persistence. __version__ = 0 grid_plane = Instance(grid_plane.GridPlane, allow_none=False, record=True) actor = Instance(Actor, allow_non=False, record=True) input_info = PipelineInfo(datasets=['image_data', 'structured_grid', 'rectilinear_grid'], attribute_types=['any'], attributes=['any']) view = View(Group(Item(name='grid_plane', style='custom'), Item(name='actor', style='custom'), show_labels=False)) ###################################################################### # `Module` interface ###################################################################### def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Create the components self.grid_plane = grid_plane.GridPlane() self.actor = Actor() # Setup the actor suitably for this module. prop = self.actor.property prop.set(backface_culling=0, frontface_culling=0, representation='w') self.actor.mapper.scalar_visibility = 0 def update_pipeline(self): """Override this method so that it *updates* the tvtk pipeline when data upstream is known to have changed. This method is invoked (automatically) when any of the inputs sends a `pipeline_changed` event. """ mm = self.module_manager if mm is None: return # Data is available, so set the input for the grid plane. self.grid_plane.inputs = [mm.source] # Set the LUT for the mapper. self.actor.set_lut(mm.scalar_lut_manager.lut) self.pipeline_changed = True def update_data(self): """Override this method so that it flushes the vtk pipeline if that is necessary. This method is invoked (automatically) when any of the inputs sends a `data_changed` event. """ # Just set data_changed, the component should do the rest. self.data_changed = True ###################################################################### # Non-public methods. ###################################################################### def _grid_plane_changed(self, old, new): actor = self.actor if actor is not None: actor.inputs = [new] self._change_components(old, new) def _actor_changed(self, old, new): new.scene = self.scene gp = self.grid_plane if gp is not None: new.inputs = [gp] self._change_components(old, new)
class ContourGridPlane(Module): # The version of this class. Used for persistence. __version__ = 0 # The grid plane component. grid_plane = Instance(GridPlane, allow_none=False, record=True) # Specifies if contouring is to be done or not. enable_contours = Bool(True, desc='if contours are generated') # The contour component that contours the data. contour = Instance(Contour, allow_none=False, record=True) # The actor component that represents the visualization. actor = Instance(Actor, allow_none=False, record=True) input_info = PipelineInfo( datasets=['image_data', 'structured_grid', 'rectilinear_grid'], attribute_types=['any'], attributes=['any']) view = View([ Group(Item(name='grid_plane', style='custom'), show_labels=False), Group(Item(name='enable_contours')), Group(Item(name='contour', style='custom', enabled_when='object.enable_contours'), Item(name='actor', style='custom'), show_labels=False) ]) ###################################################################### # `Module` interface ###################################################################### def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Create the components self.grid_plane = GridPlane() self.contour = Contour(auto_contours=True, number_of_contours=10) self.actor = Actor() def update_pipeline(self): """Override this method so that it *updates* the tvtk pipeline when data upstream is known to have changed. This method is invoked (automatically) when any of the inputs sends a `pipeline_changed` event. """ mm = self.module_manager if mm is None: return # Data is available, so set the input for the grid plane. self.grid_plane.inputs = [mm.source] # This makes sure that any changes made to enable_contours # when the module is not running are updated when it is # started. self._enable_contours_changed(self.enable_contours) # Set the LUT for the mapper. self.actor.set_lut(mm.scalar_lut_manager.lut) self.pipeline_changed = True def update_data(self): """Override this method so that it flushes the vtk pipeline if that is necessary. This method is invoked (automatically) when any of the inputs sends a `data_changed` event. """ # Just set data_changed, the components should do the rest if # they are connected. self.data_changed = True ###################################################################### # Non-public methods. ###################################################################### def _filled_contours_changed(self, value): """When filled contours are enabled, the mapper should use the the cell data, otherwise it should use the default scalar mode. """ if value: self.actor.mapper.scalar_mode = 'use_cell_data' else: self.actor.mapper.scalar_mode = 'default' self.render() def _enable_contours_changed(self, value): """Turns on and off the contours.""" if self.module_manager is None: return if value: self.actor.inputs = [self.contour] if self.contour.filled_contours: self.actor.mapper.scalar_mode = 'use_cell_data' else: self.actor.inputs = [self.grid_plane] self.actor.mapper.scalar_mode = 'default' self.render() def _grid_plane_changed(self, old, new): cont = self.contour if cont is not None: cont.inputs = [new] self._change_components(old, new) def _contour_changed(self, old, new): if old is not None: old.on_trait_change(self._filled_contours_changed, 'filled_contours', remove=True) new.on_trait_change(self._filled_contours_changed, 'filled_contours') # Setup the contours input. gp = self.grid_plane if gp is not None: new.inputs = [gp] # Setup the actor. actor = self.actor if actor is not None: actor.inputs = [new] self._change_components(old, new) def _actor_changed(self, old, new): if old is None: # First time this is set. new.property.set(line_width=2.0) # Set the actors scene and input. new.scene = self.scene cont = self.contour if cont is not None: new.inputs = [cont] self._change_components(old, new)
def setup_pipeline(self): self.outline_filter = tvtk.StructuredGridOutlineFilter() self.actor = Actor()
class ContourGridPlane(Module): # The version of this class. Used for persistence. __version__ = 0 # The grid plane component. grid_plane = Instance(GridPlane, allow_none=False, record=True) # Specifies if contouring is to be done or not. enable_contours = Bool(True, desc='if contours are generated') # The contour component that contours the data. contour = Instance(Contour, allow_none=False, record=True) # The actor component that represents the visualization. actor = Instance(Actor, allow_none=False, record=True) input_info = PipelineInfo(datasets=['image_data', 'structured_grid', 'rectilinear_grid'], attribute_types=['any'], attributes=['any']) view = View([Group(Item(name='grid_plane', style='custom'), show_labels=False), Group(Item(name='enable_contours')), Group(Item(name='contour', style='custom', enabled_when='object.enable_contours'), Item(name='actor', style='custom'), show_labels=False) ] ) ###################################################################### # `Module` interface ###################################################################### def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Create the components self.grid_plane = GridPlane() self.contour = Contour(auto_contours=True, number_of_contours=10) self.actor = Actor() def update_pipeline(self): """Override this method so that it *updates* the tvtk pipeline when data upstream is known to have changed. This method is invoked (automatically) when any of the inputs sends a `pipeline_changed` event. """ mm = self.module_manager if mm is None: return # Data is available, so set the input for the grid plane. self.grid_plane.inputs = [mm.source] # This makes sure that any changes made to enable_contours # when the module is not running are updated when it is # started. self._enable_contours_changed(self.enable_contours) # Set the LUT for the mapper. self.actor.set_lut(mm.scalar_lut_manager.lut) self.pipeline_changed = True def update_data(self): """Override this method so that it flushes the vtk pipeline if that is necessary. This method is invoked (automatically) when any of the inputs sends a `data_changed` event. """ # Just set data_changed, the components should do the rest if # they are connected. self.data_changed = True ###################################################################### # Non-public methods. ###################################################################### def _filled_contours_changed(self, value): """When filled contours are enabled, the mapper should use the the cell data, otherwise it should use the default scalar mode. """ if value: self.actor.mapper.scalar_mode = 'use_cell_data' else: self.actor.mapper.scalar_mode = 'default' self.render() def _enable_contours_changed(self, value): """Turns on and off the contours.""" if self.module_manager is None: return if value: self.actor.inputs = [self.contour] if self.contour.filled_contours: self.actor.mapper.scalar_mode = 'use_cell_data' else: self.actor.inputs = [self.grid_plane] self.actor.mapper.scalar_mode = 'default' self.render() def _grid_plane_changed(self, old, new): cont = self.contour if cont is not None: cont.inputs = [new] self._change_components(old, new) def _contour_changed(self, old, new): if old is not None: old.on_trait_change(self._filled_contours_changed, 'filled_contours', remove=True) new.on_trait_change(self._filled_contours_changed, 'filled_contours') # Setup the contours input. gp = self.grid_plane if gp is not None: new.inputs = [gp] # Setup the actor. actor = self.actor if actor is not None: actor.inputs = [new] self._change_components(old, new) def _actor_changed(self, old, new): if old is None: # First time this is set. new.property.set(line_width=2.0) # Set the actors scene and input. new.scene = self.scene cont = self.contour if cont is not None: new.inputs = [cont] self._change_components(old, new)
class SliceUnstructuredGrid(Module): """This module takes a slice of the unstructured grid data and shows the cells that intersect or touch the slice.""" # The version of this class. Used for persistence. __version__ = 0 # The implicit plane widget. implicit_plane = Instance(ImplicitPlane, allow_none=False, record=True) # Extract the cells to display. extract_geometry = Instance(tvtk.ExtractGeometry, allow_none=False, record=True) # The geometry filter. geom_filter = Instance(tvtk.GeometryFilter, allow_none=False, record=True) # The actor component that represents the visualization. actor = Instance(Actor, allow_none=False, record=True) input_info = PipelineInfo(datasets=['unstructured_grid'], attribute_types=['any'], attributes=['any']) ######################################## # View related code. view = View( Group(Item(name='implicit_plane', style='custom'), label='ImplicitPlane', show_labels=False), Group(Item(name='extract_geometry', style='custom', resizable=True), label='Extract Geometry', show_labels=False), Group(Item(name='geom_filter', style='custom', resizable=True), label='Geometry Filter', show_labels=False), Group(Item(name='actor', style='custom'), label='Actor', show_labels=False)) ###################################################################### # `Module` interface ###################################################################### def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Create the components and set them up. self.implicit_plane = ImplicitPlane() ex = tvtk.ExtractGeometry(extract_only_boundary_cells=1, extract_boundary_cells=1) self.extract_geometry = ex self.geom_filter = tvtk.GeometryFilter() # Setup the actor suitably for this module. self.actor = Actor() self.actor.property.representation = 'w' def update_pipeline(self): """Override this method so that it *updates* the tvtk pipeline when data upstream is known to have changed. This method is invoked (automatically) when any of the inputs sends a `pipeline_changed` event. """ mod_mgr = self.module_manager if mod_mgr is None: return # Data is available, so set the input for the grid plane. input = mod_mgr.source.outputs[0] if not input.is_a('vtkUnstructuredGrid'): error('SliceUnstructuredGrid only works with input '\ 'unstructured grids') self.implicit_plane.inputs = [mod_mgr.source] self.extract_geometry.input = self.module_manager.source.outputs[0] # Set the LUT for the mapper. self.actor.set_lut(self.module_manager.scalar_lut_manager.lut) # Now flush the pipeline self.pipeline_changed = True def update_data(self): """Override this method so that it flushes the vtk pipeline if that is necessary. This method is invoked (automatically) when any of the inputs sends a `data_changed` event. """ # Just set data_changed, the components should do the rest if # they are connected. self.data_changed = True ###################################################################### # Non-public methods. ###################################################################### def _implicit_plane_changed(self, old, new): ex = self.extract_geometry if ex is not None: ex.implicit_function = new.plane self._change_components(old, new) def _extract_geometry_changed(self, old, new): if old is not None: old.on_trait_change(self.render, remove=True) mm = self.module_manager if mm is not None: new.input = mm.source.outputs[0] ip = self.implicit_plane if ip is not None: new.implicit_function = ip.plane gf = self.geom_filter if gf is not None: gf.input = new.output new.on_trait_change(self.render) self.update_pipeline() def _geom_filter_changed(self, old, new): if old is not None: old.on_trait_change(self.render, remove=True) ex = self.extract_geometry if ex is not None: new.input = ex.output new.on_trait_change(self.render) self.outputs = [new.output] def _actor_changed(self, old, new): new.scene = self.scene new.inputs = [self] self._change_components(old, new)
class Streamline(Module): # The version of this class. Used for persistence. __version__ = 0 # The streamline generator. stream_tracer = Instance(tvtk.StreamTracer, allow_none=False, record=True) # The seed for the streamlines. seed = Instance(SourceWidget, allow_none=False, record=True) # The update mode of the seed -- this is delegated to the # SourceWidget. update_mode = Delegate('seed', modify=True) # Determines if the streamlines are shown as lines or ribbons or # tubes. streamline_type = Trait('line', TraitPrefixList(['line', 'ribbon', 'tube']), desc='draw streamlines as lines/ribbons/tubes') # The ribbon filter. ribbon_filter = Instance(tvtk.RibbonFilter, allow_none=False, record=True) # The tube filter. tube_filter = Instance(tvtk.TubeFilter, allow_none=False, record=True) # The actor component that represents the visualization. actor = Instance(Actor, allow_none=False, record=True) input_info = PipelineInfo(datasets=['any'], attribute_types=['any'], attributes=['vectors']) ######################################## # Private traits. _first = Bool(True) ######################################## # View related code. # A button to update the streamlines. update_streamlines = Button('Update Streamlines') _tube_group = Group(Item(name='capping'), Item(name='sides_share_vertices'), Item(name='vary_radius'), Item(name='number_of_sides'), Item(name='radius'), Item(name='radius_factor'), Item(name='offset'), Item(name='on_ratio')) _ribbon_group = Group(Item(name='vary_width'), Item(name='width'), Item(name='width_factor'), Item(name='angle')) view = View(Group( Group(Item(name='update_mode'), ), Group( Item(name='update_streamlines'), show_labels=False, ), Group(Item(name='streamline_type'), Item(name='ribbon_filter', style='custom', visible_when='object.streamline_type == "ribbon"', editor=InstanceEditor(view=View(_ribbon_group))), Item(name='tube_filter', style='custom', visible_when='object.streamline_type == "tube"', editor=InstanceEditor(view=View(_tube_group))), show_labels=False, label='Streamline'), label='Streamline'), Group(Item(name='seed', style='custom', resizable=True), label='Seed', show_labels=False), Group(Item(name='stream_tracer', style='custom', resizable=True), label='StreamTracer', show_labels=False), Group(Item(name='actor', style='custom'), label='Actor', show_labels=False), resizable=True) ###################################################################### # `Module` interface ###################################################################### def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Create and setup the default objects. self.seed = SourceWidget() self.stream_tracer = tvtk.StreamTracer( maximum_propagation=50, integration_direction='forward', compute_vorticity=True, integrator_type='runge_kutta4', ) self.ribbon_filter = tvtk.RibbonFilter() self.tube_filter = tvtk.TubeFilter() self.actor = Actor() # Setup the actor suitably for this module. self.actor.property.line_width = 2.0 def update_pipeline(self): """Override this method so that it *updates* the tvtk pipeline when data upstream is known to have changed. This method is invoked (automatically) when any of the inputs sends a `pipeline_changed` event. """ mm = self.module_manager if mm is None: return src = mm.source self.stream_tracer.input = src.outputs[0] self.seed.inputs = [src] # Setup the radius/width of the tube/ribbon filters based on # given input. if self._first: b = src.outputs[0].bounds l = [(b[1] - b[0]), (b[3] - b[2]), (b[5] - b[4])] length = sqrt(l[0] * l[0] + l[1] * l[1] + l[2] * l[2]) self.ribbon_filter.width = length * 0.0075 self.tube_filter.radius = length * 0.0075 self._first = False self._streamline_type_changed(self.streamline_type) # Set the LUT for the mapper. self.actor.set_lut(mm.scalar_lut_manager.lut) self.pipeline_changed = True def update_data(self): """Override this method so that it flushes the vtk pipeline if that is necessary. This method is invoked (automatically) when any of the inputs sends a `data_changed` event. """ # Just set data_changed, the components should do the rest if # they are connected. self.data_changed = True ###################################################################### # Non-public methods. ###################################################################### def _streamline_type_changed(self, value): if self.module_manager is None: return st = self.stream_tracer rf = self.ribbon_filter tf = self.tube_filter if value == 'line': self.outputs = [st.output] elif value == 'ribbon': rf.input = st.output self.outputs = [rf.output] elif value == 'tube': tf.input = st.output self.outputs = [tf.output] self.render() def _update_streamlines_fired(self): self.seed.update_poly_data() self.render() def _stream_tracer_changed(self, old, new): if old is not None: old.on_trait_change(self.render, remove=True) seed = self.seed if seed is not None: new.source = seed.poly_data new.on_trait_change(self.render) mm = self.module_manager if mm is not None: new.input = mm.source.outputs[0] # A default output so there are no pipeline errors. The # update_pipeline call corrects this if needed. self.outputs = [new.output] self.update_pipeline() def _seed_changed(self, old, new): st = self.stream_tracer if st is not None: st.source = new.poly_data self._change_components(old, new) def _ribbon_filter_changed(self, old, new): if old is not None: old.on_trait_change(self.render, remove=True) new.on_trait_change(self.render) self._streamline_type_changed(self.streamline_type) def _tube_filter_changed(self, old, new): if old is not None: old.on_trait_change(self.render, remove=True) new.on_trait_change(self.render) self._streamline_type_changed(self.streamline_type) def _actor_changed(self, old, new): new.scene = self.scene new.inputs = [self] self._change_components(old, new)
class GridPlane(Module): # The version of this class. Used for persistence. __version__ = 0 grid_plane = Instance(grid_plane.GridPlane, allow_none=False, record=True) actor = Instance(Actor, allow_non=False, record=True) input_info = PipelineInfo( datasets=['image_data', 'structured_grid', 'rectilinear_grid'], attribute_types=['any'], attributes=['any']) view = View( Group(Item(name='grid_plane', style='custom'), Item(name='actor', style='custom'), show_labels=False)) ###################################################################### # `Module` interface ###################################################################### def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Create the components self.grid_plane = grid_plane.GridPlane() self.actor = Actor() # Setup the actor suitably for this module. prop = self.actor.property prop.set(backface_culling=0, frontface_culling=0, representation='w') self.actor.mapper.scalar_visibility = 0 def update_pipeline(self): """Override this method so that it *updates* the tvtk pipeline when data upstream is known to have changed. This method is invoked (automatically) when any of the inputs sends a `pipeline_changed` event. """ mm = self.module_manager if mm is None: return # Data is available, so set the input for the grid plane. self.grid_plane.inputs = [mm.source] # Set the LUT for the mapper. self.actor.set_lut(mm.scalar_lut_manager.lut) self.pipeline_changed = True def update_data(self): """Override this method so that it flushes the vtk pipeline if that is necessary. This method is invoked (automatically) when any of the inputs sends a `data_changed` event. """ # Just set data_changed, the component should do the rest. self.data_changed = True ###################################################################### # Non-public methods. ###################################################################### def _grid_plane_changed(self, old, new): actor = self.actor if actor is not None: actor.inputs = [new] self._change_components(old, new) def _actor_changed(self, old, new): new.scene = self.scene gp = self.grid_plane if gp is not None: new.inputs = [gp] self._change_components(old, new)
class Surface(Module): # The version of this class. Used for persistence. __version__ = 0 # Specifies if contouring is to be done or not. enable_contours = Bool(False, desc='if contours are generated') # The contour component that contours the data. contour = Instance(Contour, allow_none=False, record=True) # The actor component that represents the visualization. actor = Instance(Actor, allow_none=False, record=True) input_info = PipelineInfo(datasets=['any'], attribute_types=['any'], attributes=['any']) ###################################################################### # `Module` interface ###################################################################### def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Setup the objects. self.contour = Contour(auto_contours=True, number_of_contours=10) self.actor = Actor() # Setup the actor suitably for this module. self.actor.property.line_width = 2.0 def update_pipeline(self): """Override this method so that it *updates* the tvtk pipeline when data upstream is known to have changed. This method is invoked (automatically) when any of the inputs sends a `pipeline_changed` event. """ mm = self.module_manager if mm is None: return # This makes sure that any changes made to enable_contours # when the module is not running are updated when it is # started. Also sets up the pipeline and inputs correctly. self._enable_contours_changed(self.enable_contours) # Set the LUT for the mapper. self.actor.set_lut(mm.scalar_lut_manager.lut) self.pipeline_changed = True def update_data(self): """Override this method so that it flushes the vtk pipeline if that is necessary. This method is invoked (automatically) when any of the inputs sends a `data_changed` event. """ # Just set data_changed, the components should do the rest if # they are connected. self.data_changed = True ###################################################################### # Non-public methods. ###################################################################### def _filled_contours_changed(self, value): """When filled contours are enabled, the mapper should use the the cell data, otherwise it should use the default scalar mode. """ if value: self.actor.mapper.scalar_mode = 'use_cell_data' else: self.actor.mapper.scalar_mode = 'default' self.render() def _enable_contours_changed(self, value): """Turns on and off the contours.""" if self.module_manager is None: return if value: self.contour.inputs = [self.module_manager.source] self.actor.inputs = [self.contour] if self.contour.filled_contours: self.actor.mapper.scalar_mode = 'use_cell_data' else: old_inputs = self.actor.inputs self.actor.inputs = [self.module_manager.source] self.actor.mapper.scalar_mode = 'default' self.render() def _contour_changed(self, old, new): if old is not None: old.on_trait_change(self._filled_contours_changed, 'filled_contours', remove=True) new.on_trait_change(self._filled_contours_changed, 'filled_contours') self._change_components(old, new) def _actor_changed(self, old, new): if old is None: # First time the actor is set. new.mapper = tvtk.DataSetMapper(use_lookup_table_scalar_range=1) new.scene = self.scene mm = self.module_manager if mm is not None: new.inputs = [mm.source] self._change_components(old, new)
class Streamline(Module): # The version of this class. Used for persistence. __version__ = 0 # The streamline generator. stream_tracer = Instance(tvtk.StreamTracer, allow_none=False, record=True) # The seed for the streamlines. seed = Instance(SourceWidget, allow_none=False, record=True) # The update mode of the seed -- this is delegated to the # SourceWidget. update_mode = Delegate('seed', modify=True) # Determines if the streamlines are shown as lines or ribbons or # tubes. streamline_type = Trait('line', TraitPrefixList(['line', 'ribbon', 'tube']), desc='draw streamlines as lines/ribbons/tubes') # The ribbon filter. ribbon_filter = Instance(tvtk.RibbonFilter, allow_none=False, record=True) # The tube filter. tube_filter = Instance(tvtk.TubeFilter, allow_none=False, record=True) # The actor component that represents the visualization. actor = Instance(Actor, allow_none=False, record=True) input_info = PipelineInfo(datasets=['any'], attribute_types=['any'], attributes=['vectors']) ######################################## # Private traits. _first = Bool(True) ######################################## # View related code. # A button to update the streamlines. update_streamlines = Button('Update Streamlines') _tube_group = Group(Item(name='capping'), Item(name='sides_share_vertices'), Item(name='vary_radius'), Item(name='number_of_sides'), Item(name='radius'), Item(name='radius_factor'), Item(name='offset'), Item(name='on_ratio') ) _ribbon_group = Group(Item(name='vary_width'), Item(name='width'), Item(name='width_factor'), Item(name='angle') ) view = View(Group(Group(Item(name='update_mode'), ), Group(Item(name='update_streamlines'), show_labels=False, ), Group(Item(name='streamline_type'), Item(name='ribbon_filter', style='custom', visible_when='object.streamline_type == "ribbon"', editor=InstanceEditor(view=View(_ribbon_group))), Item(name='tube_filter', style='custom', visible_when='object.streamline_type == "tube"', editor=InstanceEditor(view=View(_tube_group))), show_labels=False, label='Streamline' ), label='Streamline' ), Group(Item(name='seed', style='custom', resizable=True), label='Seed', show_labels=False), Group(Item(name='stream_tracer', style='custom', resizable=True), label='StreamTracer', show_labels=False), Group(Item(name='actor', style='custom'), label='Actor', show_labels=False), resizable=True ) ###################################################################### # `Module` interface ###################################################################### def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Create and setup the default objects. self.seed = SourceWidget() self.stream_tracer = tvtk.StreamTracer(maximum_propagation=50, integration_direction='forward', compute_vorticity=True, integrator_type='runge_kutta4', ) self.ribbon_filter = tvtk.RibbonFilter() self.tube_filter = tvtk.TubeFilter() self.actor = Actor() # Setup the actor suitably for this module. self.actor.property.line_width = 2.0 def update_pipeline(self): """Override this method so that it *updates* the tvtk pipeline when data upstream is known to have changed. This method is invoked (automatically) when any of the inputs sends a `pipeline_changed` event. """ mm = self.module_manager if mm is None: return src = mm.source self.stream_tracer.input = src.outputs[0] self.seed.inputs = [src] # Setup the radius/width of the tube/ribbon filters based on # given input. if self._first: b = src.outputs[0].bounds l = [(b[1]-b[0]), (b[3]-b[2]), (b[5]-b[4])] length = sqrt(l[0]*l[0] + l[1]*l[1] + l[2]*l[2]) self.ribbon_filter.width = length*0.0075 self.tube_filter.radius = length*0.0075 self._first = False self._streamline_type_changed(self.streamline_type) # Set the LUT for the mapper. self.actor.set_lut(mm.scalar_lut_manager.lut) self.pipeline_changed = True def update_data(self): """Override this method so that it flushes the vtk pipeline if that is necessary. This method is invoked (automatically) when any of the inputs sends a `data_changed` event. """ # Just set data_changed, the components should do the rest if # they are connected. self.data_changed = True ###################################################################### # Non-public methods. ###################################################################### def _streamline_type_changed(self, value): if self.module_manager is None: return st = self.stream_tracer rf = self.ribbon_filter tf = self.tube_filter if value == 'line': self.outputs = [st.output] elif value == 'ribbon': rf.input = st.output self.outputs = [rf.output] elif value == 'tube': tf.input = st.output self.outputs = [tf.output] self.render() def _update_streamlines_fired(self): self.seed.update_poly_data() self.render() def _stream_tracer_changed(self, old, new): if old is not None: old.on_trait_change(self.render, remove=True) seed = self.seed if seed is not None: new.source = seed.poly_data new.on_trait_change(self.render) mm = self.module_manager if mm is not None: new.input = mm.source.outputs[0] # A default output so there are no pipeline errors. The # update_pipeline call corrects this if needed. self.outputs = [new.output] self.update_pipeline() def _seed_changed(self, old, new): st = self.stream_tracer if st is not None: st.source = new.poly_data self._change_components(old, new) def _ribbon_filter_changed(self, old, new): if old is not None: old.on_trait_change(self.render, remove=True) new.on_trait_change(self.render) self._streamline_type_changed(self.streamline_type) def _tube_filter_changed(self, old, new): if old is not None: old.on_trait_change(self.render, remove=True) new.on_trait_change(self.render) self._streamline_type_changed(self.streamline_type) def _actor_changed(self, old, new): new.scene = self.scene new.inputs = [self] self._change_components(old, new)
class ScalarCutPlane(Module): # The version of this class. Used for persistence. __version__ = 0 # The implicit plane widget used to place the implicit function. implicit_plane = Instance(ImplicitPlane, allow_none=False, record=True) # The cutter. Takes a cut of the data on the implicit plane. cutter = Instance(Cutter, allow_none=False, record=True) # Specifies if contouring is to be done or not. enable_contours = Bool(False, desc='if contours are generated') # The Contour component that contours the data. contour = Instance(Contour, allow_none=False, record=True) # Specifies if scalar warping is to be done or not. enable_warp_scalar = Bool(False, desc='if scalar warping is enabled') # The WarpScalarCutPlane component that warps the data. warp_scalar = Instance(WarpScalar, allow_none=False, record=True) # Specify if scalar normals are to be computed to make a smoother surface. compute_normals = Bool(False, desc='if normals are to be computed '\ 'to make the warped scalar surface smoother') # The component that computes the scalar normals. normals = Instance(PolyDataNormals, allow_none=False, record=True) # The actor component that represents the visualization. actor = Instance(Actor, allow_none=False, record=True) input_info = PipelineInfo(datasets=['any'], attribute_types=['any'], attributes=['scalars']) ######################################## # View related code. _warp_group = Group(Item(name='filter', style='custom', editor=\ InstanceEditor(view= View(Item('scale_factor')))), show_labels=False) view = View(Group(Item(name='implicit_plane', style='custom'), label='ImplicitPlane', show_labels=False), Group(Group(Item(name='enable_contours')), Group(Item(name='contour', style='custom', enabled_when='object.enable_contours'), show_labels=False), label='Contours', show_labels=False), Group(Item(name='enable_warp_scalar'), Group(Item(name='warp_scalar', enabled_when='enable_warp_scalar', style='custom', editor=InstanceEditor(view= View(_warp_group)) ), show_labels=False, ), Item(name='_'), Item(name='compute_normals', enabled_when='enable_warp_scalar'), Item(name='normals', style='custom', show_label=False, enabled_when='compute_normals and enable_warp_scalar'), label='WarpScalar', show_labels=True), Group(Item(name='actor', style='custom'), label='Actor', show_labels=False) ) ###################################################################### # `Module` interface ###################################################################### def setup_pipeline(self): """Override this method so that it *creates* the tvtk pipeline. This method is invoked when the object is initialized via `__init__`. Note that at the time this method is called, the tvtk data pipeline will *not* yet be setup. So upstream data will not be available. The idea is that you simply create the basic objects and setup those parts of the pipeline not dependent on upstream sources and filters. You should also set the `actors` attribute up at this point. """ # Create the objects. self.implicit_plane = ImplicitPlane() self.cutter = Cutter() self.contour = Contour(auto_contours=True, number_of_contours=10) self.warp_scalar = WarpScalar() self.normals = PolyDataNormals() self.actor = Actor() # Setup the actor suitably for this module. prop = self.actor.property prop.line_width = 2.0 def update_pipeline(self): """Override this method so that it *updates* the tvtk pipeline when data upstream is known to have changed. This method is invoked (automatically) when any of the inputs sends a `pipeline_changed` event. """ mm = self.module_manager if mm is None: return # Data is available, so set the input for the grid plane. self.implicit_plane.inputs = [mm.source] # Ensure that the warped scalar surface's normal is setup right. self.warp_scalar.filter.normal = self.implicit_plane.normal # This makes sure that any changes made to enable_warp when # the module is not running are updated when it is started -- # this in turn calls the other functions (normals and # contours) internally. self._enable_warp_scalar_changed(self.enable_warp_scalar) # Set the LUT for the mapper. self.actor.set_lut(mm.scalar_lut_manager.lut) self.pipeline_changed = True def update_data(self): """Override this method so that it flushes the vtk pipeline if that is necessary. This method is invoked (automatically) when any of the inputs sends a `data_changed` event. """ # Just set data_changed, the components should do the rest if # they are connected. self.data_changed = True ###################################################################### # Non-public methods. ###################################################################### def _get_warp_output(self): """Helper function to return the warped (or not) output depending on settings. """ if self.enable_warp_scalar: if self.compute_normals: return self.normals else: return self.warp_scalar else: return self.cutter def _get_contour_output(self): """Helper function to return the contoured (and warped (or not)) output depending on settings. """ if self.enable_contours: return self.contour else: return self._get_warp_output() def _filled_contours_changed_for_contour(self, value): """When filled contours are enabled, the mapper should use the the cell data, otherwise it should use the default scalar mode. """ if value: self.actor.mapper.scalar_mode = 'use_cell_data' else: self.actor.mapper.scalar_mode = 'default' self.render() def _enable_warp_scalar_changed(self, value): """Turns on and off the scalar warping.""" if self.module_manager is None: return if value: self.warp_scalar.inputs = [self.cutter] else: self.warp_scalar.inputs = [] self._compute_normals_changed(self.compute_normals) self.render() def _compute_normals_changed(self, value): if self.module_manager is None: return if self.enable_warp_scalar: normals = self.normals if value: normals.inputs = [self.warp_scalar] else: normals.inputs = [] self._enable_contours_changed(self.enable_contours) self.render() def _enable_contours_changed(self, value): """Turns on and off the contours.""" if self.module_manager is None: return actor = self.actor if value: self.contour.inputs = [self._get_warp_output()] actor.inputs = [self._get_contour_output()] if self.contour.filled_contours: actor.mapper.scalar_mode = 'use_cell_data' else: self.contour.inputs = [] actor.inputs = [self._get_warp_output()] actor.mapper.scalar_mode = 'default' self.render() def _normals_changed(self, old, new): warp_scalar = self.warp_scalar if warp_scalar is not None: new.inputs = [warp_scalar] self._compute_normals_changed(self.compute_normals) self._change_components(old, new) def _implicit_plane_changed(self, old, new): cutter = self.cutter if cutter is not None: cutter.cut_function = new.plane cutter.inputs = [new] # Update the pipeline. self._enable_warp_scalar_changed(self.enable_warp_scalar) # Hook up events to set the normals of the warp filter. if old is not None: old.widget.on_trait_change(self._update_normal, 'normal', remove=True) new.widget.on_trait_change(self._update_normal, 'normal') self._change_components(old, new) def _cutter_changed(self, old, new): ip = self.implicit_plane if ip is not None: new.cut_function = ip.plane new.inputs = [ip] # Update the pipeline. self._enable_warp_scalar_changed(self.enable_warp_scalar) self._change_components(old, new) def _contour_changed(self, old, new): # Update the pipeline. self._enable_contours_changed(self.enable_contours) self._change_components(old, new) def _warp_scalar_changed(self, old, new): # Update the pipeline. self._enable_warp_scalar_changed(self.enable_warp_scalar) self._change_components(old, new) def _actor_changed(self, old, new): # Update the pipeline. self._enable_contours_changed(self.enable_contours) self._change_components(old, new) def _update_normal(self): """Invoked when the orientation of the implicit plane changes. """ ws = self.warp_scalar if ws is not None: ws.filter.normal = self.implicit_plane.widget.normal
def do(self): ############################################################ # Imports. script = self.script from enthought.mayavi.filters.optional import Optional from enthought.mayavi.filters.warp_scalar import WarpScalar from enthought.mayavi.filters.cut_plane import CutPlane from enthought.mayavi.components.poly_data_normals import PolyDataNormals from enthought.mayavi.components.contour import Contour from enthought.mayavi.components.actor import Actor from enthought.mayavi.modules.generic_module import GenericModule from enthought.mayavi.sources.vtk_xml_file_reader import VTKXMLFileReader ############################################################ # Create a new scene and set up the visualization. s = self.new_scene() # Read a VTK (old style) data file. r = VTKXMLFileReader() r.initialize(get_example_data('fire_ug.vtu')) script.add_source(r) # We now create the complete equivalent of a ScalarCutPlane in # the next block! cp = CutPlane() w = WarpScalar() warper = Optional(filter=w, label_text='Enable warping', enabled=False) c = Contour() ctr = Optional(filter=c, label_text='Enable contours', enabled=False) p = PolyDataNormals(name='Normals') normals = Optional(filter=p, label_text='Compute normals', enabled=False) a = Actor() components = [cp, warper, ctr, normals, a] m = GenericModule(name='ScalarCutPlane', components=components, contour=c, actor=a) script.add_module(m) s.scene.isometric_view() ######################################## # do the testing. def check(mod): """Check if test status is OK for the given module.""" cut, warper, ctr, normals, a = mod.components c = ctr.filter # The intermediate ones are disabled. assert normals.outputs[0] is cut.outputs[0] # Enable the contours. ctr.enabled = True assert ctr.outputs[0] is c.outputs[0] assert ctr.outputs[0] is normals.outputs[0] rng = normals.outputs[0].point_data.scalars.range assert (rng[1] - rng[0]) < 1e-4 # Turn on auto-contours c.auto_contours = True assert len(normals.outputs[0].points) == 0 # Increase number of contours and the range should change. c.number_of_contours = 10 assert len(normals.outputs[0].points) != 0 rng = normals.outputs[0].point_data.scalars.range assert rng[0] < rng[1] # Check if pipeline_changed is correctly propagated. old = normals.outputs[0] assert a.mapper.scalar_mode == 'default' c.filled_contours = True assert normals.outputs[0] != old assert normals.outputs[0] is c.outputs[0] # Check if the actor responds correctly to the # filled_contour change. assert a.mapper.scalar_mode == 'use_cell_data' # Set back everything to original state. c.filled_contours = False assert a.mapper.scalar_mode == 'default' c.number_of_contours = 1 c.auto_contours = False ctr.enabled = False assert normals.outputs[0] is cut.outputs[0] check(m) ############################################################ # Test if saving a visualization and restoring it works. # Save visualization. f = StringIO() f.name = abspath('test.mv2') # We simulate a file. script.save_visualization(f) f.seek(0) # So we can read this saved data. # Remove existing scene. engine = script.engine engine.close_scene(s) # Load visualization script.load_visualization(f) s = engine.current_scene s.scene.isometric_view() # Now do the check. m = s.children[0].children[0].children[0] check(m) ############################################################ # Test if the Mayavi2 visualization can be deep-copied. # Pop the source object. source = s.children.pop() # Add it back to see if that works without error. s.children.append(source) # Now do the check. m = s.children[0].children[0].children[0] s.scene.isometric_view() check(m) # Now deepcopy the source and replace the existing one with # the copy. This basically simulates cutting/copying the # object from the UI via the right-click menu on the tree # view, and pasting the copy back. source1 = copy.deepcopy(source) s.children[0] = source1 # Now do the check. m = s.children[0].children[0].children[0] s.scene.isometric_view() check(m)