def make_tube(height, radius, resolution, rx=0, ry=0, rz=0): cs1 = tvtk.CylinderSource(height=height, radius=radius[0], resolution=resolution) cs2 = tvtk.CylinderSource(height=height + 0.1, radius=radius[1], resolution=resolution) triangle1 = tvtk.TriangleFilter(input_connection=cs1.output_port) triangle2 = tvtk.TriangleFilter(input_connection=cs2.output_port) tr = tvtk.Transform() tr.rotate_x(rx) tr.rotate_y(ry) tr.rotate_z(rz) tf1 = tvtk.TransformFilter(transform=tr, input_connection=triangle1.output_port) tf2 = tvtk.TransformFilter(transform=tr, input_connection=triangle2.output_port) bf = tvtk.BooleanOperationPolyDataFilter() bf.operation = "difference" bf.set_input_connection(0, tf1.output_port) bf.set_input_connection(1, tf2.output_port) m = tvtk.PolyDataMapper(input_connection=bf.output_port, scalar_visibility=False) a = tvtk.Actor(mapper=m) return bf, a, tf1, tf2
def revoluteJoint(joint): """ Return a cylinder and the appropriate static transform. """ axis = e.Vector3d(joint.motionSubspace()[:3]) s = tvtk.CylinderSource(height=0.1, radius=0.02) quat = e.Quaterniond() # Cylinder is around the Y axis quat.setFromTwoVectors(axis, e.Vector3d.UnitY()) return makeActor(s, tuple(axis)), sva.PTransformd(quat)
def _glyph_dict_default(self): g = {'glyph_source2d': tvtk.GlyphSource2D(glyph_type='arrow', filled=False), 'arrow_source': tvtk.ArrowSource(), 'cone_source': tvtk.ConeSource(height=1.0, radius=0.2, resolution=15), 'cylinder_source': tvtk.CylinderSource(height=1.0, radius=0.15, resolution=10), 'sphere_source': tvtk.SphereSource(), 'cube_source': tvtk.CubeSource(), 'axes': tvtk.Axes(symmetric=1)} return g
def plot(self): r1, r2 = min(self.radius1, self.radius2), max(self.radius1, self.radius2) self.cs1 = cs1 = tvtk.CylinderSource(height=1, radius=r2, resolution=32) self.cs2 = cs2 = tvtk.CylinderSource(height=1.1, radius=r1, resolution=32) triangle1 = tvtk.TriangleFilter(input_connection=cs1.output_port) triangle2 = tvtk.TriangleFilter(input_connection=cs2.output_port) bf = tvtk.BooleanOperationPolyDataFilter() bf.operation = "difference" bf.set_input_connection(0, triangle1.output_port) bf.set_input_connection(1, triangle2.output_port) m = tvtk.PolyDataMapper(input_connection=bf.output_port, scalar_visibility=False) a = tvtk.Actor(mapper=m) a.property.color = 0.5, 0.5, 0.5 self.scene.add_actors([a]) self.scene.background = 1, 1, 1 self.scene.reset_zoom()
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 _update_project_to_surface(self): if not self.glyph: return defaults = DEFAULTS['coreg'] gs = self.glyph.glyph.glyph_source res = getattr(gs.glyph_source, 'theta_resolution', getattr(gs.glyph_source, 'resolution', None)) if self.project_to_surface: gs.glyph_source = tvtk.CylinderSource() gs.glyph_source.height = defaults['eegp_height'] gs.glyph_source.center = (0., -defaults['eegp_height'], 0) gs.glyph_source.resolution = res else: gs.glyph_source = tvtk.SphereSource() gs.glyph_source.phi_resolution = res gs.glyph_source.theta_resolution = res self.glyph.glyph.scale_mode = 'data_scaling_off'
def cylinder_actor(center=(0, 0, 0), radius=0.5, height=1, heiresolution=64, color=colors.green, opacity=1.0): """ Creates a cylinder and returns a tvtk.Actor. """ source = tvtk.CylinderSource(center=center, height=height, radius=radius, resolution=resolution) mapper = tvtk.PolyDataMapper() configure_input_data(mapper, source.output) prop = tvtk.Property(opacity=opacity, color=color) actor = tvtk.Actor(mapper=mapper, property=prop) source.update() return actor
def _update_marker_type(self): # not implemented for arrow if self.glyph is None or self._view == 'arrow': return defaults = DEFAULTS['coreg'] gs = self.glyph.glyph.glyph_source res = getattr(gs.glyph_source, 'theta_resolution', getattr(gs.glyph_source, 'resolution', None)) if self.project_to_surface or self.orient_to_surface: gs.glyph_source = tvtk.CylinderSource() gs.glyph_source.height = defaults['eegp_height'] gs.glyph_source.center = (0., -defaults['eegp_height'], 0) gs.glyph_source.resolution = res else: gs.glyph_source = tvtk.SphereSource() gs.glyph_source.phi_resolution = res gs.glyph_source.theta_resolution = res
def __source_dict_default(self): """Default value for source dict.""" sd = { 'arrow': tvtk.ArrowSource(), 'cone': tvtk.ConeSource(), 'cube': tvtk.CubeSource(), 'cylinder': tvtk.CylinderSource(), 'disk': tvtk.DiskSource(), 'earth': tvtk.EarthSource(), 'line': tvtk.LineSource(), 'outline': tvtk.OutlineSource(), 'plane': tvtk.PlaneSource(), 'point': tvtk.PointSource(), 'polygon': tvtk.RegularPolygonSource(), 'sphere': tvtk.SphereSource(), 'superquadric': tvtk.SuperquadricSource(), 'textured sphere': tvtk.TexturedSphereSource(), 'glyph2d': tvtk.GlyphSource2D() } return sd
def _update_markers(self): if self.glyph is None: return defaults = DEFAULTS['coreg'] gs = self.glyph.glyph.glyph_source res = getattr(gs.glyph_source, 'theta_resolution', getattr(gs.glyph_source, 'resolution', None)) if self.project_to_surface or self.orient_to_surface: gs.glyph_source = tvtk.CylinderSource() gs.glyph_source.height = defaults['eegp_height'] gs.glyph_source.center = (0., -defaults['eegp_height'], 0) gs.glyph_source.resolution = res else: gs.glyph_source = tvtk.SphereSource() gs.glyph_source.phi_resolution = res gs.glyph_source.theta_resolution = res if self.scale_by_distance: self.glyph.glyph.scale_mode = 'scale_by_vector' else: self.glyph.glyph.scale_mode = 'data_scaling_off'
def cylinders(positions, directions, radii, length=1, renderer=renderer): for position, dir, radius in zip(positions, directions, radii): # create source source = tvtk.CylinderSource() #source.center = position source.radius = float(radius) source.height = length source.resolution = 5 source.capping = 0 # 0 no caps, 1 capped # mapper mapper = tvtk.PolyDataMapper() mapper.set_input_data(source.output) # actor actor = tvtk.Actor() actor.mapper = mapper # cylinder is aligned with y-axis so rotation axis is cross product # with target direction initial_axis = (0, 1, 0) axis = np.cross(initial_axis, dir) # Current orientation #w0, x0, y0, z0 = actor.orientation_wxyz # The angle of rotation in degrees. w = np.arccos(np.dot(dir, initial_axis)) w = np.degrees(w) # The rotation is in the plane of dir and [x0, y0, z0], the axis of # rotation should be normal to it #x, y, z = np.cross(dir, [x0, y0, z0]) actor.rotate_wxyz(w, axis[0], axis[1], axis[2]) actor.position = position renderer.add_actor(actor) return actor
def vtk_actors(self): if (self.actors is None): self.actors = [] points = _getfem_to_tvtk_points(self.sl.pts()) (triangles, cv2tr) = self.sl.splxs(2) triangles = numpy.array(triangles.transpose(), 'I') data = tvtk.PolyData(points=points, polys=triangles) if self.scalar_data is not None: data.point_data.scalars = numpy.array(self.scalar_data) if self.vector_data is not None: data.point_data.vectors = numpy.array(self.vector_data) if self.glyph_name is not None: mask = tvtk.MaskPoints() mask.maximum_number_of_points = self.glyph_nb_pts mask.random_mode = True mask.input = data if self.glyph_name == 'default': if self.vector_data is not None: self.glyph_name = 'arrow' else: self.glyph_name = 'ball' glyph = tvtk.Glyph3D() glyph.scale_mode = 'scale_by_vector' glyph.color_mode = 'color_by_scalar' #glyph.scale_mode = 'data_scaling_off' glyph.vector_mode = 'use_vector' # or 'use_normal' glyph.input = mask.output if self.glyph_name == 'arrow': glyph.source = tvtk.ArrowSource().output elif self.glyph_name == 'ball': glyph.source = tvtk.SphereSource().output elif self.glyph_name == 'cone': glyph.source = tvtk.ConeSource().output elif self.glyph_name == 'cylinder': glyph.source = tvtk.CylinderSource().output elif self.glyph_name == 'cube': glyph.source = tvtk.CubeSource().output else: raise Exception("Unknown glyph name..") #glyph.scaling = 1 #glyph.scale_factor = self.glyph_scale_factor data = glyph.output if self.show_faces: ## if self.deform is not None: ## data.point_data.vectors = array(numarray.transpose(self.deform)) ## warper = tvtk.WarpVector(input=data) ## data = warper.output ## lut = tvtk.LookupTable() ## lut.hue_range = 0.667,0 ## c=gf_colormap('tripod') ## lut.number_of_table_values=c.shape[0] ## for i in range(0,c.shape[0]): ## lut.set_table_value(i,c[i,0],c[i,1],c[i,2],1) self.mapper = tvtk.PolyDataMapper(input=data) self.mapper.scalar_range = self.scalar_data_range self.mapper.scalar_visibility = True # Create mesh actor for display self.actors += [tvtk.Actor(mapper=self.mapper)] if self.show_edges: (Pe, E1, E2) = self.sl.edges() if Pe.size: E = numpy.array( numpy.concatenate((E1.transpose(), E2.transpose()), axis=0), 'I') edges = tvtk.PolyData(points=_getfem_to_tvtk_points(Pe), polys=E) mapper_edges = tvtk.PolyDataMapper(input=edges) actor_edges = tvtk.Actor(mapper=mapper_edges) actor_edges.property.representation = 'wireframe' #actor_edges.property.configure_traits() actor_edges.property.color = self.edges_color actor_edges.property.line_width = self.edges_width actor_edges.property.ambient = 0.5 self.actors += [actor_edges] if self.sl.nbsplxs(1): # plot tubes (seg, cv2seg) = self.sl.splxs(1) seg = numpy.array(seg.transpose(), 'I') data = tvtk.Axes(origin=(0, 0, 0), scale_factor=0.5, symmetric=1) data = tvtk.PolyData(points=points, lines=seg) tube = tvtk.TubeFilter(radius=0.4, number_of_sides=10, vary_radius='vary_radius_off', input=data) mapper = tvtk.PolyDataMapper(input=tube.output) actor_tubes = tvtk.Actor(mapper=mapper) #actor_tubes.property.representation = 'wireframe' actor_tubes.property.color = self.tube_color #actor_tubes.property.line_width = 8 #actor_tubes.property.ambient = 0.5 self.actors += [actor_tubes] if self.use_scalar_bar: self.scalar_bar = tvtk.ScalarBarActor( title=self.scalar_data_name, orientation='horizontal', width=0.8, height=0.07) self.scalar_bar.position_coordinate.coordinate_system = 'normalized_viewport' self.scalar_bar.position_coordinate.value = 0.1, 0.01, 0.0 self.actors += [self.scalar_bar] if (self.lookup_table is not None): self.set_colormap(self.lookup_table) return self.actors
# The source generates data points sphere = tvtk.SphereSource(center=(0, 0, 0), radius=0.5) # The mapper converts them into position in, 3D with optionally color (if # scalar information is available). sphere_mapper = tvtk.PolyDataMapper() configure_input_data(sphere_mapper, sphere.output) sphere.update() # The Property will give the parameters of the material. p = tvtk.Property(opacity=0.2, color=(1, 0, 0)) # The actor is the actually object in the scene. sphere_actor = tvtk.Actor(position=(0, 0, 0), mapper=sphere_mapper, property=p) v.scene.add_actor(sphere_actor) # Create a cylinder cylinder = tvtk.CylinderSource(center=(0, 0, 0), radius=0.2, resolution=16) cylinder_mapper = tvtk.PolyDataMapper() configure_input_data(cylinder_mapper, cylinder.output) cylinder.update() p = tvtk.Property(opacity=0.3, color=(0, 0, 1)) cylinder_actor = tvtk.Actor(position=(7, 0, 1), mapper=cylinder_mapper, property=p, orientation=(90, 0, 90)) v.scene.add_actor(cylinder_actor) # Create a line between the two spheres line = tvtk.LineSource(point1=(0, 0, 0), point2=(7, 0, 1)) line_mapper = tvtk.PolyDataMapper() configure_input_data(line_mapper, line.output) line.update() line_actor = tvtk.Actor(mapper=line_mapper) v.scene.add_actor(line_actor)
from tvtk.api import tvtk r1 = 0.5 r2 = 0.9 cs1 = tvtk.CylinderSource(height=1, radius=r2, resolution=32) cs2 = tvtk.CylinderSource(height=1.1, radius=r1, resolution=32) triangle1 = tvtk.TriangleFilter(input_connection=cs1.output_port) triangle2 = tvtk.TriangleFilter(input_connection=cs2.output_port) bf = tvtk.BooleanOperationPolyDataFilter() bf.operation = "difference" bf.set_input_connection(0, triangle1.output_port) bf.set_input_connection(1, triangle2.output_port) m = tvtk.PolyDataMapper(input_connection=bf.output_port, scalar_visibility=False) a = tvtk.Actor(mapper=m) a.property.color = 0.5, 0.5, 0.5 from scpy2.tvtk.tvtkhelp import ivtk_scene, event_loop scene = ivtk_scene([a]) event_loop()
def _get_primitive(self): return tvtk.CylinderSource(center=self.center, height=self.height, radius=self.radius, resolution=self.resolution)