Example #1
0
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
Example #2
0
 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'
Example #3
0
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
Example #4
0
    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)
Example #5
0
 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
Example #6
0
 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
Example #7
0
    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))
Example #8
0
# -*- coding: utf-8 -*-
"""
Created on Wed Nov  9 08:36:22 2016

@author: Gordon
"""

from enthought.tvtk.api import tvtk

# 创建一个圆锥数据源,并且同时设置其高度,底面半径和底面圆的分辨率(用36边形近似)
cs = tvtk.ConeSource(height=3.0, radius=1.0, resolution=36)
# 使用PolyDataMapper将数据转换为图形数据
m = tvtk.PolyDataMapper(input = cs.output)
# 创建一个Actor
a = tvtk.Actor(mapper=m)
# 创建一个Renderer,将Actor添加进去
ren = tvtk.Renderer(background=(0.1, 0.2, 0.4))
ren.add_actor(a)
# 创建一个RenderWindow(窗口),将Renderer添加进去
rw = tvtk.RenderWindow(size=(300,300))
rw.add_renderer(ren)
# 创建一个RenderWindowInteractor(窗口的交互工具)
rwi = tvtk.RenderWindowInteractor(render_window=rw)
# 开启交互
rwi.initialize()
rwi.start()
Example #9
0
    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
Example #10
0
from enthought.tvtk.api import tvtk
import numpy as N
from model import Joint, Element, Construction, ElementMaterial

# ----------- 3D models ------------
# completely free joint: represented by a sphere
joint_model_movable = tvtk.SphereSource(phi_resolution=24, theta_resolution=24, radius=0.15).output
# unmovable joint: represented by a solid cube
joint_model_unmovable = tvtk.CubeSource(x_length=0.7, y_length=0.7, z_length=0.5).output
# ground in x direction
collect = tvtk.AppendPolyData()
collect.add_input(tvtk.CubeSource(x_length=0.7, y_length=0.2, z_length=0.25, center=(0, -0.5, 0)).output)
collect.add_input(tvtk.SphereSource(radius=0.11, center=(-0.18, -0.3, 0), phi_resolution=24, theta_resolution=24).output)
collect.add_input(tvtk.SphereSource(radius=0.11, center=(0.18, -0.3, 0), phi_resolution=24, theta_resolution=24).output)
collect.add_input(tvtk.ConeSource(center=(0,-0.05,0), direction=(0,1,0), height=0.3, radius=0.35, resolution=16).output)
joint_model_movable_x = collect.output
# ground in y direction
collect = tvtk.AppendPolyData()
collect.add_input(tvtk.CubeSource(x_length=0.2, y_length=0.7, z_length=0.25, center=(-0.5, 0, 0)).output)
collect.add_input(tvtk.SphereSource(radius=0.11, center=(-0.3, -0.18, 0), phi_resolution=24, theta_resolution=24).output)
collect.add_input(tvtk.SphereSource(radius=0.11, center=(-0.3, 0.18, 0), phi_resolution=24, theta_resolution=24).output)
collect.add_input(tvtk.ConeSource(center=(-0.05,0,0), direction=(1,0,0), height=0.3, radius=0.35, resolution=16).output)
joint_model_movable_y = collect.output


# ----------- Materials ------------
air = ElementMaterial(E = 100.0, density = 1.0, radius = 0.000001, maximum_stress = 1e+99, name='deleted')
steels = []
#radii = N.concatenate((N.arange(0.005, 0.05, 0.005), N.arange(0.05, 0.25, 0.01)))
radii = N.concatenate((N.arange(0.005, 0.05, 0.005), N.arange(0.05, 0.25, 0.01)))