def test_property(self): """Test if Property's color works ok in all circumstances.""" p = tvtk.Property() val = (0., 1., 0.) p.color = val p.specular = 1.0 self.assertEqual(p.specular_color, val) self.assertEqual(p.diffuse_color, val) self.assertEqual(p.ambient_color, val) sc = (1., 0., 1.) p.specular_color = sc self.assertEqual(p.specular_color, sc) self.assertEqual(p.diffuse_color, val) self.assertEqual(p.ambient_color, val) self.assertEqual(p.color, (0.5, 0.5, 0.5)) # Test pickling. s = cPickle.dumps(p) del p p = cPickle.loads(s) self.assertEqual(p.specular_color, sc) self.assertEqual(p.diffuse_color, val) self.assertEqual(p.ambient_color, val) self.assertEqual(p.color, (0.5, 0.5, 0.5))
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 test_obj_del(self): """Test object deletion and reference cycles.""" p = tvtk.Property() p.representation = 0 ref = weakref.ref(p) del p self.assertEqual(ref(), None)
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 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 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 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 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 test_do_change(self): """Test if VTK object changes when trait is changed.""" p = tvtk.Property() p.edge_visibility = not p.edge_visibility p.representation = 'p' p.interpolation = 'phong' p.opacity = 0.5 p.color = (0, 1, 0) p.diffuse_color = (1, 1, 1) p.specular_color = (1, 1, 0) for t, g in p._updateable_traits_: val = getattr(p._vtk_obj, g)() if t in ['representation', 'interpolation']: self.assertEqual(val, getattr(p, t + '_')) else: self.assertEqual(val, getattr(p, t))
def __init__(self): self.b = False self.i = 7 self.l = 1234567890123456789 self.f = math.pi self.c = complex(1.01234, 2.3) self.n = None self.s = 'String' self.u = u'Unicode' self.inst = A() self.tuple = (1, 2, 'a', A()) self.list = [1, 1.1, 'a', 1j, self.inst] self.pure_list = range(5) self.dict = {'a': 1, 'b': 2, 'ref': self.inst} self.numeric = numpy.ones((2, 2, 2), 'f') self.ref = self.numeric self._tvtk = tvtk.Property()
def test_init_traits(self): """Test if the objects traits can be set in __init__.""" p = tvtk.Property(opacity=0.1, color=(1, 0, 0), representation='p') self.assertEqual(p.opacity, 0.1) self.assertEqual(p.color, (1.0, 0.0, 0.0)) self.assertEqual(p.representation, 'points') # Test case where the object traits are wrong. self.assertRaises(TraitError, tvtk.Property, foo='bar') cs = tvtk.ConeSource(radius=0.1, height=0.5, resolution=32) self.assertEqual(cs.radius, 0.1) self.assertEqual(cs.height, 0.5) self.assertEqual(cs.resolution, 32) # Test case where the object traits are wrong. self.assertRaises(TraitError, tvtk.ConeSource, foo=1)
def test_tvtk_dataset_name(self): "Can tvtk datasets can be converted to names correctly." datasets = [ tvtk.ImageData(), tvtk.StructuredPoints(), tvtk.RectilinearGrid(), tvtk.StructuredGrid(), tvtk.PolyData(), tvtk.UnstructuredGrid(), tvtk.Property(), # Not a dataset! 'foo', # Not a TVTK object. ] expect = [ 'image_data', 'image_data', 'rectilinear_grid', 'structured_grid', 'poly_data', 'unstructured_grid', 'none', 'none' ] result = [pipeline_info.get_tvtk_dataset_name(d) for d in datasets] self.assertEqual(result, expect)
def test_property_change_notification(self): """Test if changes to properties generate notification events.""" # Create a dummy class to test with. class Junk: def f(self, obj, name, old, new): self.data = obj, name, old, new z = Junk() cs = tvtk.ConeSource() m = tvtk.PolyDataMapper() m.on_trait_change(z.f, 'input') m.input = cs.output self.assertEqual(z.data, (m, 'input', None, cs.output)) m.input = None self.assertEqual(z.data, (m, 'input', cs.output, None)) m.on_trait_change(z.f, 'input', remove=True) m.input = cs.output a = tvtk.Actor() a.on_trait_change(z.f, 'mapper') a.on_trait_change(z.f, 'property') a.mapper = m self.assertEqual(z.data, (a, 'mapper', None, m)) old = a.property new = tvtk.Property() a.property = new self.assertEqual(z.data, (a, 'property', old, new)) # Check if property notification occurs on add_input/remove_input a = tvtk.AppendPolyData() a.on_trait_change(z.f, 'input') pd = tvtk.PolyData() a.add_input(pd) old, new = None, pd self.assertEqual(z.data, (a, 'input', old, new)) a.remove_input(pd) old, new = pd, None self.assertEqual(z.data, (a, 'input', old, new)) a.remove_all_inputs() old, new = None, None self.assertEqual(z.data, (a, 'input', old, new))
def test_auto_update(self): """Test if traits are updated when the VTK object changes.""" p = tvtk.Property() obj = p._vtk_obj obj.SetEdgeVisibility(1) self.assertEqual(p.edge_visibility, 1) obj.SetOpacity(0.5) self.assertEqual(p.opacity, 0.5) obj.SetRepresentationToPoints() self.assertEqual(p.representation, 'points') val = (1.0, 1.0, 0.0) obj.SetColor(val) self.assertEqual(p.color, val) val = (1.0, 0.0, 0.0) obj.SetDiffuseColor(val) self.assertEqual(p.diffuse_color, val) val = (0.0, 1.0, 0.0) obj.SetSpecularColor(val) self.assertEqual(p.specular_color, val)
# To access any VTK object, we use 'tvtk', which is a Python wrapping of # VTK replacing C++ setters and getters by Python properties and # converting numpy arrays to VTK arrays when setting data. from enthought.tvtk.api import tvtk v = mlab.figure() # Create a first sphere # 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(input=sphere.output) # 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(mapper=sphere_mapper, property=p) v.scene.add_actor(sphere_actor) # Create a second sphere sphere2 = tvtk.SphereSource(center=(7, 0, 1), radius=0.2) sphere_mapper2 = tvtk.PolyDataMapper(input=sphere2.output) p = tvtk.Property(opacity=0.3, color=(1, 0, 0)) sphere_actor2 = tvtk.Actor(mapper=sphere_mapper2, property=p) v.scene.add_actor(sphere_actor2) # Create a line between the two spheres line = tvtk.LineSource(point1=(0, 0, 0), point2=(7, 0, 1)) line_mapper = tvtk.PolyDataMapper(input=line.output) line_actor = tvtk.Actor(mapper=line_mapper)