def __init__(self, triangles, points, scalars=None, **traits): """ Parameters ---------- - triangles : array This contains a list of vertex indices forming the triangles. - points : array Contains the list of points referred to in the triangle list. - scalars : array (optional) Scalars to associate with the points. """ super(FancyTriMesh, self).__init__(**traits) self.points = points self.pd = make_triangle_polydata(triangles, points, scalars) # Update the radii so the default is computed correctly. self._tube_radius_changed(self.tube_radius) self._sphere_radius_changed(self.sphere_radius) scalar_vis = self.scalar_visibility # Extract the edges and show the lines as tubes. self.extract_filter = tvtk.ExtractEdges(input=self.pd) extract_f = self.extract_filter self.tube_filter.set(input=extract_f.output, radius=self.tube_radius) edge_mapper = tvtk.PolyDataMapper(input=self.tube_filter.output, lookup_table=self.lut, scalar_visibility=scalar_vis) edge_actor = _make_actor(mapper=edge_mapper) edge_actor.property.color = self.color # Create the spheres for the points. self.sphere_source.radius = self.sphere_radius spheres = tvtk.Glyph3D(scaling=0, source=self.sphere_source.output, input=extract_f.output) sphere_mapper = tvtk.PolyDataMapper(input=spheres.output, lookup_table=self.lut, scalar_visibility=scalar_vis) sphere_actor = _make_actor(mapper=sphere_mapper) sphere_actor.property.color = self.color if scalars is not None: rs = numpy.ravel(scalars) dr = min(rs), max(rs) self.lut.table_range = dr edge_mapper.scalar_range = dr sphere_mapper.scalar_range = dr self.actors.extend([edge_actor, sphere_actor])
def __init__(self, filename, colour=None, position=[0, 0, 0], orientation=None, rotation=None): self.filename = filename self.reader = tvtk.STLReader(file_name=filename) self.mapper = tvtk.PolyDataMapper() configure_input_data(self.mapper, self.reader.output) self.reader.update() self.actor = tvtk.Actor(mapper=self.mapper) if colour: self.actor.property.color = colour self.actor.position = position if rotation is None: if orientation is None: self.actor.orientation = [0, 0, 0] else: a2 = -180 * np.arctan(orientation[0] / orientation[1]) / np.pi a1 = 180 * np.arcsin(orientation[2]) / np.pi self.actor.orientation = [a1, 0, a2] else: self.actor.orientation = rotation
def _scene_default(self): cube = self.cube map = tvtk.PolyDataMapper(input=cube.output) act = tvtk.Actor(mapper=map) scene = SceneModel() scene.add_actor(act) return scene
def __init__(self, x, y, z, warp=1, scale=[1.0, 1.0, 1.0], f_args=(), f_kwargs=None, **traits): super(SurfRegularC, self).__init__(**traits) if f_kwargs is None: f_kwargs = {} data, actor = make_surf_actor(x, y, z, warp, scale, *f_args, **f_kwargs) mapper = actor.mapper mapper.lookup_table = self.lut self.lut.table_range = mapper.scalar_range self.data = data dr = data.point_data.scalars.range cf = self.contour_filter cf.input = data cf.generate_values(self.number_of_contours, dr[0], dr[1]) mapper = tvtk.PolyDataMapper(input=cf.output, lookup_table=self.lut) cont_actor = _make_actor(mapper=mapper) self.actors.extend([actor, cont_actor])
def __init__(self, x, y, z, scalars, **traits): """ Parameters ---------- - x : array A list of x coordinate values formed using numpy.mgrid. - y : array A list of y coordinate values formed using numpy.mgrid. - z : array A list of z coordinate values formed using numpy.mgrid. - scalars : array Scalars to associate with the points. """ super(Contour3, self).__init__(**traits) triangles, points = make_triangles_points(x, y, z, scalars) self.pd = make_triangle_polydata(triangles, points, scalars) dr = self.pd.point_data.scalars.range self.lut.table_range = dr cf = self.contour_filter cf.input = self.pd cf.generate_values(self.number_of_contours, dr[0], dr[1]) mapper = tvtk.PolyDataMapper(input=cf.output, lookup_table=self.lut, scalar_range=dr) cont_actor = _make_actor(mapper=mapper) self.actors.append(cont_actor)
def __init__(self, renwin, **traits): super(Picker, self).__init__(**traits) self.renwin = renwin self.pointpicker = tvtk.PointPicker() self.cellpicker = tvtk.CellPicker() self.worldpicker = tvtk.WorldPointPicker() self.probe_data = tvtk.PolyData() self._tolerance_changed(self.tolerance) # Use a set of axis to show the picked point. self.p_source = tvtk.Axes() self.p_mapper = tvtk.PolyDataMapper() self.p_actor = tvtk.Actor() self.p_source.symmetric = 1 self.p_actor.pickable = 0 self.p_actor.visibility = 0 prop = self.p_actor.property prop.line_width = 2 prop.ambient = 1.0 prop.diffuse = 0.0 self.p_mapper.input = self.p_source.output self.p_actor.mapper = self.p_mapper self.probe_data.points = [[0.0, 0.0, 0.0]] self.ui = None
def __init__(self, points, vectors=None, scalars=None, **traits): super(Glyphs, self).__init__(**traits) if vectors is not None: assert len(points) == len(vectors) if scalars is not None: assert len(points) == len(scalars) self.points = points self.vectors = vectors self.scalars = scalars polys = numpy.arange(0, len(points), 1, 'l') polys = numpy.reshape(polys, (len(points), 1)) pd = tvtk.PolyData(points=points, polys=polys) if self.vectors is not None: pd.point_data.vectors = vectors pd.point_data.vectors.name = 'vectors' if self.scalars is not None: pd.point_data.scalars = scalars pd.point_data.scalars.name = 'scalars' self.poly_data = pd self.glyph.input = pd if self.glyph_source: self.glyph.source = self.glyph_source.output mapper = tvtk.PolyDataMapper(input=self.glyph.output) actor = _make_actor(mapper=mapper) actor.property.color = self.color self.actors.append(actor)
def _redraw_background_plane(self): if self.construction and self.scene: if self._bg_plane_width == self.construction.width and self._bg_plane_height == self.construction.height: return if self._bg_plane_actor: self.scene.remove_actor(self._bg_plane_actor) if self._axis: self.scene.remove_actor(self._axis) w, h = self.construction.width, self.construction.height plane = tvtk.PlaneSource(x_resolution=int(w), y_resolution=int(h)) scalation = tvtk.Transform() scalation.scale((w, h, 1)) scale_plane = tvtk.TransformPolyDataFilter(transform=scalation, input=plane.output) self._bg_plane_actor = tvtk.Actor(mapper=tvtk.PolyDataMapper( input=scale_plane.output)) self._bg_plane_actor.property.set(representation='wireframe', line_stipple_pattern=0xF0F0, opacity=0.15) self.scene.add_actor(self._bg_plane_actor) self._axis = tvtk.CubeAxesActor2D( camera=self.scene.camera, z_axis_visibility=False, corner_offset=0, bounds=[-w / 2, w / 2, -h / 2, h / 2, 0, 0]) self.scene.add_actor(self._axis) self._bg_plane_picker = tvtk.CellPicker(tolerance=0, pick_from_list=True) self._bg_plane_picker.pick_list.append(self._bg_plane_actor) self._bg_plane_width, self._bg_plane_height = self.construction.width, self.construction.height
def make_probe(self): src = self.ipl.poly_data_algorithm map = tvtk.PolyDataMapper(lookup_table=self.lm.lut) act = tvtk.Actor(mapper=map) calc_B = self.calc_B calc_B.input = src.output def execute(): print "calc fields!" output = calc_B.poly_data_output points = output.points.to_array().astype('d') nodes = self.wire.nodes.astype('d') vectors = calc_wire_B_field(nodes, points, self.wire.radius) output.point_data.vectors = vectors mag = np.sqrt((vectors**2).sum(axis=1)) map.scalar_range = (mag.min(), mag.max()) calc_B.set_execute_method(execute) cone = tvtk.ConeSource(height=0.05, radius=0.01, resolution=15) cone.update() glyph = self.glyph glyph.input_connection = calc_B.output_port glyph.source = cone.output glyph.scale_mode = 'scale_by_vector' glyph.color_mode = 'color_by_vector' map.input_connection = glyph.output_port self.scene.add_actor(act)
def earth_actor(radius=0.5, opacity=1.0): """ Creates an earth source and returns the actor. """ source = tvtk.EarthSource(radius=radius, on_ratio=16, outline=0) mapper = tvtk.PolyDataMapper(input=source.output) prop = tvtk.Property(opacity=opacity) actor = tvtk.Actor(mapper=mapper, property=prop) return actor
def __init__(self, x, y, z, scalars=None, **traits): """ Parameters ---------- - x : array A list of x coordinate values formed using numpy.mgrid. - y : array A list of y coordinate values formed using numpy.mgrid. - z : array A list of z coordinate values formed using numpy.mgrid. - scalars : array (optional) Scalars to associate with the points. """ super(Surf, self).__init__(**traits) triangles, points = make_triangles_points(x, y, z, scalars) self.pd = make_triangle_polydata(triangles, points, scalars) mapper = tvtk.PolyDataMapper(input=self.pd, lookup_table=self.lut, scalar_visibility=self.scalar_visibility) if scalars is not None: rs = numpy.ravel(scalars) dr = min(rs), max(rs) mapper.scalar_range = dr self.lut.table_range = dr actor = _make_actor(mapper=mapper) actor.property.set(color=self.color) self.actors.append(actor)
def __init__(self, points, **traits): super(MLabBase, self).__init__(**traits) assert len(points[0]) == 3, "The points must be 3D" self.points = points np = len(points) - 1 lines = numpy.zeros((np, 2), 'l') lines[:, 0] = numpy.arange(0, np - 0.5, 1, 'l') lines[:, 1] = numpy.arange(1, np + 0.5, 1, 'l') pd = tvtk.PolyData(points=points, lines=lines) self.poly_data = pd mapper = tvtk.PolyDataMapper() self.mapper = mapper tf = self.tube_filter tf.radius = self.radius if self.use_tubes: tf.input = pd mapper.input = tf.output a = _make_actor(mapper=mapper) a.property.color = self.color self.actors.append(a)
def __init__(self, *args, **kwargs): Primitive.__init__(self, **kwargs) self.source = tvtk.ConeSource() self.polyDataMapper = tvtk.PolyDataMapper() self.polyDataMapper.input = self.source.output self.actor = tvtk.Actor(mapper=self.polyDataMapper) self.handle_arguments(*args, **kwargs)
def arrow_actor(color=colors.peacock, opacity=1.0, resolution=24): """ Creates a 3D Arrow and returns an actor. """ source = tvtk.ArrowSource(tip_resolution=resolution, shaft_resolution=resolution) mapper = tvtk.PolyDataMapper(input=source.output) prop = tvtk.Property(opacity=opacity, color=color) actor = tvtk.Actor(mapper=mapper, property=prop) return actor
def cube_actor(center=(0, 0, 0), color=colors.blue, opacity=1.0): """ Creates a cube and returns the tvtk.Actor. """ source = tvtk.CubeSource(center=center) mapper = tvtk.PolyDataMapper(input=source.output) p = tvtk.Property(opacity=opacity, color=color) actor = tvtk.Actor(mapper=mapper, property=p) return actor
def __init__(self, *args, **kwargs): Primitive.__init__(self, *kwargs) self.source = tvtk.Axes(symmetric=1) self.tube = tvtk.TubeFilter(vary_radius='vary_radius_off', input=self.source.output) self.mapper = tvtk.PolyDataMapper(input=self.tube.output) self.actor = tvtk.Actor(mapper=self.mapper) self.handle_arguments(*args, **kwargs)
def cylinder_actor(center=(0, 0, 0), radius=0.5, resolution=64, color=colors.green, opacity=1.0): """ Creates a cylinder and returns a tvtk.Actor. """ source = tvtk.CylinderSource(center=center, radius=radius, resolution=resolution) mapper = tvtk.PolyDataMapper(input=source.output) prop = tvtk.Property(opacity=opacity, color=color) actor = tvtk.Actor(mapper=mapper, property=prop) return actor
def _make_pipeline(self): self.func.on_trait_change(self.on_change, "anytrait") src = self.source src.on_trait_change(self.on_change, "anytrait") src.parametric_function = self.func map = tvtk.PolyDataMapper(input_connection=src.output_port) act = tvtk.Actor(mapper=map) self.scene.add_actor(act) self.src = src
def axes_actor(origin=(0, 0, 0), scale_factor=1.0, radius=0.02, sides=12): """Creates a simple axes actor and returns a tvtk.Actor object.""" axes = tvtk.Axes(origin=origin, scale_factor=scale_factor, symmetric=1) tube = tvtk.TubeFilter(radius=radius, number_of_sides=sides, vary_radius='vary_radius_off', input=axes.output) mapper = tvtk.PolyDataMapper(input=tube.output) actor = tvtk.Actor(mapper=mapper) return actor
def sphere_actor(center=(0, 0, 0), radius=0.5, resolution=32, color=colors.purple, opacity=1.0): """ Creates a sphere and returns the actor. """ source = tvtk.SphereSource(center=center, radius=radius, theta_resolution=resolution, phi_resolution=resolution) mapper = tvtk.PolyDataMapper(input=source.output) prop = tvtk.Property(opacity=opacity, color=color) actor = tvtk.Actor(mapper=mapper, property=prop) return actor
def _setup_elements_picker(self): if self.scene and self.construction: #print "setup elements picker" pd = tvtk.PolyData(points=pts2dto3d( self.construction.joint_positions), lines=self.construction.element_index_table) self._pick_elements_actor = tvtk.Actor(mapper=tvtk.PolyDataMapper( input=pd)) self._element_picker = tvtk.CellPicker(pick_from_list=True, tolerance=0.005) self._element_picker.pick_list.append(self._pick_elements_actor)
def cone_actor(center=(0, 0, 0), height=1.0, radius=0.5, direction=(1, 0, 0), resolution=100, color=colors.red, opacity=1.0): """ Sets up a cone actor and returns the tvtk.Actor object.""" source = tvtk.ConeSource(center=center, height=height, radius=radius, direction=direction, resolution=resolution) mapper = tvtk.PolyDataMapper(input=source.output) p = tvtk.Property(opacity=opacity, color=color) actor = tvtk.Actor(mapper=mapper, property=p) return actor
def visual_test(jpos, jfreedom, loads, elements, E, r, p, max_stress=3e+8): displacements, strains, stresses, mass, status, times = analyse_truss(jpos, jfreedom, loads, elements, E, r, p) jpos_d = jpos + displacements*100 print status print times print 'displacements: ', displacements print 'strains: ', strains print 'stresses: ', stresses #strains_abs = N.abs(strains) from enthought.tvtk.api import tvtk from enthought.tvtk.tools import ivtk from enthought.pyface.api import GUI v = ivtk.viewer(False, False) v.scene.z_plus_view() pd = tvtk.PolyData() pts = jpos[elements].reshape((-1,2)) pd.points = N.column_stack((pts[:,0], pts[:,1], N.zeros(len(pts)))) pd.lines = N.r_[:len(elements)*2].reshape((-1,2)) pd.cell_data.scalars = -strains pd.point_data.scalars = N.column_stack((r, r)).ravel() #tubes = tvtk.TubeFilter(input=pd, radius=N.sqrt(element_A.max() / N.pi), number_of_sides=16) tubes = tvtk.TubeFilter(input=pd, number_of_sides=16, vary_radius='vary_radius_by_absolute_scalar', capping=True) #tubes = tvtk.RibbonFilter(input=pd, use_default_normal=True, vary_width=True) b = tvtk.Actor(mapper=tvtk.PolyDataMapper(input=tubes.output, scalar_range=(-N.abs(strains).max(), N.abs(strains).max()), scalar_mode='use_cell_data')) b.mapper.lookup_table.hue_range = (0, 0.66) v.scene.add_actor(b) pd1 = tvtk.PolyData() pd1.points = N.column_stack((jpos_d[:,0], jpos_d[:,1], N.zeros(len(jpos)))) pd1.lines = elements tubes1 = tvtk.TubeFilter(input=pd1, radius=0.01, number_of_sides=16) a = tvtk.Actor(mapper=tvtk.PolyDataMapper(input=tubes1.output)) a.property.opacity = 0.3 v.scene.add_actor(a) print "strain: ", strains.min(), strains.max() v.scene.reset_zoom() GUI().start_event_loop()
def __init__(self, **traits): super(Outline, self).__init__(**traits) out_mapper = tvtk.PolyDataMapper(input=self.outline.output) out_actor = _make_actor(mapper=out_mapper) axis = self.axis if hasattr(axis, 'view_prop'): axis.view_prop = out_actor else: axis.prop = out_actor self.actors.extend([out_actor, axis])
def test_basic(self): """Test a simple tvtk pipeline.""" # If this works without any problems, we are ok. cs = tvtk.ConeSource() m = tvtk.PolyDataMapper() m.input = cs.output # This should work. m.input = cs.get_output() # This should also work. a = tvtk.Actor() a.mapper = m cs.resolution = 36 p = a.property p.representation = 'w'
def show(d): l = tvtk.LookupTable(table_range=(0, 1)) m = tvtk.PolyDataMapper(input=d.output, scalar_visibility=True, scalar_mode="use_cell_data") p = tvtk.Property(representation="s") a = tvtk.Actor(mapper=m, property=p) ren = tvtk.Renderer(background=(.1, .2, .4)) ren.add_actor(a) rw = tvtk.RenderWindow(size=(600, 600)) rw.add_renderer(ren) rwi = tvtk.RenderWindowInteractor(render_window=rw) rwi.initialize() rwi.start()
def main(instantiate_gui=True): """Simple test case.""" from enthought.tvtk.tools import ivtk v = ivtk.viewer(browser=False, instantiate_gui=instantiate_gui) cs = tvtk.ConeSource() m = tvtk.PolyDataMapper(input=cs.output) a = tvtk.Actor(mapper=m) v.scene.add_actor(a) v.scene.reset_zoom() b = PipelineBrowser(v.scene) b.show() return v, b, a
def __init__(self, **traits): self.property = self.actor.property HasTraits.__init__(self, **traits) self._create_points(self.coords, self.indices) self._color_changed(self.color) self._visibility_changed(self.visibility) normals = tvtk.PolyDataNormals(input=self.polydata) m = tvtk.PolyDataMapper( input=normals.output) # the usual vtk pipleine countinuation self.actor.mapper = m self.property = self.actor.property self.property.representation = self.representation show_actor(self.actor) # passing the actors function for rendering self.viewer = get_viewer() # getting the ivtk viewer self.property.on_trait_change(self.viewer.scene.render) self.actor.on_trait_change(self.viewer.scene.render)
def _drawLines(self, points, color=[1,1,1]): nrays = points.shape[0] npts = points.shape[2] nsegs = npts - 1 lines = np.zeros((nsegs*nrays, 2), 'l') pts_cat = np.zeros([npts*nrays, 3], 'f') for j in range(nrays): pts_cat[(j*npts):((j+1)*npts), :] = points[j,:,:].T lines[(j*nsegs):((j+1)*nsegs),0] = (j*npts) + np.arange(0, nsegs-0.5, 1, 'l') lines[(j * nsegs):((j + 1) * nsegs),1] = (j * npts) + np.arange(1, nsegs + 0.5, 1, 'l') #lines[:,1] = np.arange(1, nsegs+0.5, 1, 'l') d = tvtk.PolyData(points=pts_cat, lines=lines) m = tvtk.PolyDataMapper() configure_input_data(m, d) a = tvtk.Actor(mapper=m) a.property.color = color self.f.scene.add_actor(a) return a
def __init__(self, triangles, points, scalars=None, **traits): """ Parameters ---------- - triangles : array This contains a list of vertex indices forming the triangles. - points : array Contains the list of points referred to in the triangle list. - scalars : array (optional) Scalars to associate with the points. """ super(TriMesh, self).__init__(**traits) self.pd = make_triangle_polydata(triangles, points, scalars) mapper = tvtk.PolyDataMapper(input=self.pd, lookup_table=self.lut, scalar_visibility=self.scalar_visibility) if scalars is not None: rs = numpy.ravel(scalars) dr = min(rs), max(rs) mapper.scalar_range = dr self.lut.table_range = dr actor = _make_actor(mapper=mapper) representation = 'w' if self.surface: representation = 's' if representation == 'w': actor.property.set(diffuse=0.0, ambient=1.0, color=self.color, representation=representation) else: actor.property.set(diffuse=1.0, ambient=0.0, color=self.color, representation=representation) self.actors.append(actor)