Example #1
0
    def __init__(self):
        self.images = [
            visuals.Image(image, method='impostor') for i in range(4)
        ]
        self.images[0].transform = (
            STTransform(scale=(30, 30), translate=(600, 600)) *
            SineTransform() *
            STTransform(scale=(0.1, 0.1), translate=(-5, -5)))

        tr = AffineTransform()
        tr.rotate(30, (0, 0, 1))
        tr.scale((3, 3))
        self.images[1].transform = (STTransform(translate=(200, 600)) * tr *
                                    STTransform(translate=(-50, -50)))

        self.images[2].transform = (
            STTransform(scale=(3, -150), translate=(200, 100)) * LogTransform(
                (0, 2, 0)) *
            STTransform(scale=(1, -0.01), translate=(-50, 1.3)))

        self.images[3].transform = (
            STTransform(scale=(400, 400), translate=(600, 300)) *
            PolarTransform() * STTransform(scale=(np.pi / 200, 0.005),
                                           translate=(-3 * np.pi / 4., 0.1)))

        vispy.scene.SceneCanvas.__init__(self, keys='interactive')
        self.size = (800, 800)
        self.show()
Example #2
0
class Canvas(app.Canvas):
    def __init__(self, **kwarg):
        app.Canvas.__init__(self, close_keys='escape', title='Glyphs', **kwarg)
        self.scale = 200.

    def on_initialize(self, event):
        self.text = Text('Hello world!', bold=True)
        # We need to give a transform to our visual
        self.transform = STTransform()
        self.text._program.vert['transform'] = self.transform.shader_map()
        self.apply_zoom()

    def on_draw(self, event):
        gloo.clear(color='white')
        self.text.draw()
        self.update()

    def on_mouse_wheel(self, event):
        """Use the mouse wheel to zoom."""
        self.scale *= 1.25 if event.delta[1] > 0 else 0.8
        self.scale = max(min(self.scale, 2000.), 10.)
        self.apply_zoom()

    def on_resize(self, event):
        self.apply_zoom()

    def apply_zoom(self):
        gloo.set_viewport(0, 0, *self.size)
        self.transform.scale = (self.scale / self.size[0],
                                self.scale / self.size[1], 1.)
        self.update()
Example #3
0
    def __init__(self):

        self.line = ModularLine()
        self.line.transform = (
            STTransform(scale=(40, 100), translate=(400, 400)) *
            SineTransform() * STTransform(scale=(10, 3)))
        self.wobbler = WobbleComponent(pos)
        self.line.pos_components = [XYPosComponent(pos), self.wobbler]
        dasher = DashComponent(pos)
        self.line.color_components = [VertexColorComponent(color), dasher]

        vispy.scene.SceneCanvas.__init__(self, keys='interactive')
        self.size = (800, 800)
        self.show()

        self.timer = vispy.app.Timer(connect=self.wobble,
                                     interval=0.02,
                                     start=True)
Example #4
0
    def __init__(self):
        self.meshes = []
        self.rotation = AffineTransform()

        # Generate some data to work with
        global mdata
        mdata = create_sphere(20, 40, 1.0)

        # Mesh with pre-indexed vertices, uniform color
        self.meshes.append(Mesh(meshdata=mdata, color='r'))
        #mesh.transform = STTransform(scale=(1, 1, .001), translate=(400, 400))

        ## Mesh with pre-indexed vertices, per-face color
        ##   Because vertices are pre-indexed, we get a different color
        ##   every time a vertex is visited, resulting in sharp color
        ##   differences between edges.
        verts = mdata.vertices(indexed='faces')
        nf = verts.size // 9
        fcolor = np.ones((nf, 3, 4), dtype=np.float32)
        fcolor[..., 0] = np.linspace(1, 0, nf)[:, np.newaxis]
        fcolor[..., 1] = np.random.normal(size=nf)[:, np.newaxis]
        fcolor[..., 2] = np.linspace(0, 1, nf)[:, np.newaxis]
        mesh = Mesh(vertices=verts, face_colors=fcolor)
        self.meshes.append(mesh)

        ## Mesh with unindexed vertices, per-vertex color
        ##   Because vertices are unindexed, we get the same color
        ##   every time a vertex is visited, resulting in no color differences
        ##   between edges.
        verts = mdata.vertices()
        faces = mdata.faces()
        nv = verts.size // 3
        vcolor = np.ones((nv, 4), dtype=np.float32)
        vcolor[:, 0] = np.linspace(1, 0, nv)
        vcolor[:, 1] = np.random.normal(size=nv)
        vcolor[:, 2] = np.linspace(0, 1, nv)
        self.meshes.append(Mesh(verts, faces, vcolor))
        self.meshes.append(Mesh(verts, faces, vcolor, shading='flat'))
        self.meshes.append(Mesh(verts, faces, vcolor, shading='smooth'))

        # Lay out meshes in a grid
        grid = (3, 3)
        s = 300. / max(grid)
        for i, mesh in enumerate(self.meshes):
            x = 800. * (i % grid[0]) / grid[0] + 400. / grid[0] - 2
            y = 800. * (i // grid[1]) / grid[1] + 400. / grid[1] + 2
            mesh.transform = ChainTransform([
                STTransform(translate=(x, y), scale=(s, s, 1)), self.rotation
            ])

        vispy.scene.SceneCanvas.__init__(self, keys='interactive')

        self.size = (800, 800)
        self.show()

        self.timer = vispy.app.Timer(connect=self.rotate)
        self.timer.start(0.016)
Example #5
0
    def __init__(self, fname):
      app.Canvas.__init__(
          self, title='Molecular viewer')

      self.size = 500, 300

      self.program = gloo.Program(semilight_vertex, semilight_fragment)
      self.picking_program = gloo.Program(picking_vertex, picking_fragment)

      soup = pdbatoms.Soup(fname)

      rendered_soup = RenderedSoup(soup)
      self.rendered_soup = rendered_soup

      print "Building arrows..."
      self.arrow_buffer = make_calpha_arrow_mesh(rendered_soup.trace)

      print "Building cylindrical trace..."
      self.cylinder_index_buffer, self.cylinder_vertex_buffer = \
          make_cylinder_trace_mesh(rendered_soup.pieces)

      print "Building cartoon..."
      self.cartoon_index_buffer, self.cartoon_vertex_buffer = \
          make_carton_mesh(rendered_soup.pieces)

      print "Building ball&sticks..."
      self.ballstick_index_buffer, self.ballstick_vertex_buffer = \
          make_ball_and_stick_mesh(rendered_soup)

      self.camera = Camera()
      self.camera.resize(*self.size)
      self.camera.set_center(rendered_soup.center)
      self.camera.rezoom(2.0/rendered_soup.scale)

      self.new_camera = Camera()
      self.n_step_animate = 0 

      self.text = Text('X', bold=True, color=(1., 1., 1., 1.))
      transform = STTransform((1., 1., 1.))
      self.text._program.vert['transform'] = transform.shader_map()

      self.timer = app.Timer(1.0 / 30)  # change rendering speed here
      self.timer.connect(self.on_timer)
      self.timer.start()
Example #6
0
def test_text():
    """Test basic text support"""
    with Canvas(size=(100, 100)) as c:
        text = Text('X', bold=True, color=(1., 1., 1., 1.))
        transform = STTransform((1., 1., 1.))
        text._program.vert['transform'] = transform.shader_map()
        gloo.set_viewport(0, 0, *c.size)
        gloo.clear(color=(0., 0., 0., 1.))
        text.draw()

        s = gloo.util._screenshot()
        assert_equal(s.min(), 0)
        assert_equal(s.max(), 255)

        # let's just peek at the texture, make sure it has something
        gloo.clear(color=(0., 0., 0., 1.))
        gloo.util.draw_texture(text._font._atlas)
        s = gloo.util._screenshot()
        assert_equal(s.max(), 255)
        assert_equal(s.min(), 0)
Example #7
0
File: line.py Project: kod3r/vispy
 def __init__(self):
     vispy.scene.SceneCanvas.__init__(self,
                                      keys='interactive',
                                      size=(800, 800),
                                      show=True)
     # Create several visuals demonstrating different features of Line
     self.lines = [
         # agg-mode lines:
         visuals.Line(pos=pos, color=color, mode='agg'),  # per-vertex color
         visuals.Line(pos=pos, color=(0, 0.5, 0.3, 1), mode='agg'),  # solid
         visuals.Line(pos=pos, color=color, width=5, mode='agg'),  # wide
         # GL-mode lines:
         visuals.Line(pos=pos, color=color, mode='gl'),
         visuals.Line(pos=pos, color=(0, 0.5, 0.3, 1), mode='gl'),
         visuals.Line(pos=pos, color=color, width=5, mode='gl'),
         # GL-mode: "connect" not available in AGG mode yet
         visuals.Line(pos=pos,
                      color=(0, 0.5, 0.3, 1),
                      connect='segments',
                      mode='gl'),  # only connect alternate vert pairs
         visuals.Line(pos=pos,
                      color=(0, 0.5, 0.3, 1),
                      connect=connect,
                      mode='gl'),  # connect specific pairs
     ]
     counts = [0, 0]
     for i, line in enumerate(self.lines):
         # arrange lines in a grid
         tidx = (line.mode == 'agg')
         x = 400 * tidx
         y = 140 * (counts[tidx] + 1)
         counts[tidx] += 1
         line.transform = STTransform(translate=[x, y])
         # redraw the canvas if any visuals request an update
         line.events.update.connect(lambda evt: self.update())
         line.parent = self.central_widget
     self.texts = [
         visuals.Text('GL',
                      bold=True,
                      font_size=24,
                      color='w',
                      pos=(200, 40),
                      parent=self.central_widget),
         visuals.Text('Agg',
                      bold=True,
                      font_size=24,
                      color='w',
                      pos=(600, 40),
                      parent=self.central_widget)
     ]
Example #8
0
    def __init__(self):

        # Define several Line visuals that use the same position data
        # but have different colors and transformations
        colors = [color, (1, 0, 0, 1), (0, 1, 0, 1), (0, 0, 1, 1),
                  (1, 1, 0, 1), (1, 1, 1, 1)]

        self.lines = [visuals.Line(pos=pos, color=colors[i])
                      for i in range(6)]

        center = STTransform(translate=(400, 400))

        self.lines[0].transform = center

        self.lines[1].transform = (center * 
                                   STTransform(scale=(1, 0.1, 1)))

        self.lines[2].transform = (center * 
                                   STTransform(translate=(200, 200, 0)) *
                                   STTransform(scale=(0.3, 0.5, 1)))

        self.lines[3].transform = (center * 
                                   STTransform(translate=(-200, -200, 0),
                                               scale=(200, 1)) *
                                   LogTransform(base=(10, 0, 0)) *
                                   STTransform(translate=(1, 0, 0)))

        self.lines[4].transform = AffineTransform()
        self.lines[4].transform.rotate(45, (0, 0, 1))
        self.lines[4].transform.scale((0.3, 0.3, 1))
        self.lines[4].transform.translate((200, 200, 0))

        self.lines[5].transform = (STTransform(translate=(200, 600, 0),
                                               scale=(5, 5)) *
                                   PolarTransform() *
                                   LogTransform(base=(2, 0, 0)) *
                                   STTransform(scale=(0.01, 0.1),
                                               translate=(4, 20)))

        vispy.scene.SceneCanvas.__init__(self, keys='interactive')
        self.size = (800, 800)
        self.show()
Example #9
0
class Canvas(app.Canvas):

    def __init__(self):
        app.Canvas.__init__(self, keys='interactive', size=(512, 512 + 2*32),
                            title="Marker demo [press space to change marker]")
        self.index = 0
        self.scale = 1.
        self.markers = Markers()
        self.markers.set_data(pos)
        self.markers.set_style(marker_types[self.index])

    def on_initialize(self, event):
        # We need to give a transform to our visual
        self.transform = STTransform()
        self.markers._program.vert['transform'] = self.transform.shader_map()
        self.apply_zoom()

    def on_draw(self, event):
        gloo.clear(color='white')
        self.markers.draw()

    def on_mouse_wheel(self, event):
        """Use the mouse wheel to zoom."""
        self.scale *= 1.25 if event.delta[1] > 0 else 0.8
        self.scale = max(min(self.scale, 1e2), 1e-2)
        self.apply_zoom()

    def on_resize(self, event):
        self.apply_zoom()

    def apply_zoom(self):
        gloo.set_viewport(0, 0, *self.size)
        self.transform.scale = (2 * self.scale / self.size[0],
                                2 * self.scale / self.size[1], 1.)
        self.transform.translate = [-1, -1]
        self.update()

    def on_key_press(self, event):
        if event.text == ' ':
            self.index = (self.index + 1) % (len(marker_types))
            self.markers.set_style(marker_types[self.index])
            self.update()
Example #10
0
    def __init__(self):
        self.meshes = []
        self.rotation = AffineTransform()

        # Generate some data to work with
        global mdata
        mdata = create_sphere(20, 40, 1.0)

        # Mesh with pre-indexed vertices, uniform color
        verts = mdata.get_vertices(indexed='faces')
        mesh = ModularMesh(pos=verts, color=(1, 0, 0, 1))
        self.meshes.append(mesh)

        # Mesh with pre-indexed vertices, per-face color
        #   Because vertices are pre-indexed, we get a different color
        #   every time a vertex is visited, resulting in sharp color
        #   differences between edges.
        nf = verts.size//9
        fcolor = np.ones((nf, 3, 4), dtype=np.float32)
        fcolor[..., 0] = np.linspace(1, 0, nf)[:, np.newaxis]
        fcolor[..., 1] = np.random.normal(size=nf)[:, np.newaxis]
        fcolor[..., 2] = np.linspace(0, 1, nf)[:, np.newaxis]
        mesh = ModularMesh(pos=verts, color=fcolor)
        self.meshes.append(mesh)

        # Mesh with unindexed vertices, per-vertex color
        #   Because vertices are unindexed, we get the same color
        #   every time a vertex is visited, resulting in no color differences
        #   between edges.
        verts = mdata.get_vertices()
        faces = mdata.get_faces()
        nv = verts.size//3
        vcolor = np.ones((nv, 4), dtype=np.float32)
        vcolor[:, 0] = np.linspace(1, 0, nv)
        vcolor[:, 1] = np.random.normal(size=nv)
        vcolor[:, 2] = np.linspace(0, 1, nv)
        mesh = ModularMesh(pos=verts, faces=faces, color=vcolor)
        self.meshes.append(mesh)

        # Mesh colored by vertices + grid contours
        mesh = ModularMesh(pos=verts, faces=faces)
        mesh.color_components = [VertexColorComponent(vcolor),
                                 GridContourComponent(spacing=(0.13, 0.13,
                                                               0.13))]
        self.meshes.append(mesh)

        # Phong shaded mesh
        mesh = ModularMesh(pos=verts, faces=faces)
        normal_comp = VertexNormalComponent(mdata)
        mesh.color_components = [VertexColorComponent(vcolor),
                                 GridContourComponent(spacing=(0.1, 0.1, 0.1)),
                                 ShadingComponent(normal_comp,
                                                  lights=[((-1, 1, -1),
                                                          (1.0, 1.0, 1.0))],
                                                  ambient=0.2)]
        self.meshes.append(mesh)

        # Phong shaded mesh, flat faces
        mesh = ModularMesh(pos=mdata.get_vertices(indexed='faces'))
        normal_comp = VertexNormalComponent(mdata, smooth=False)
        mesh.color_components = [
            VertexColorComponent(vcolor[mdata.get_faces()]),
            GridContourComponent(spacing=(0.1, 0.1, 0.1)),
            ShadingComponent(normal_comp, lights=[((-1, 1, -1),
                                                  (1.0, 1.0, 1.0))],
                             ambient=0.2)]
        self.meshes.append(mesh)

        # Lay out meshes in a grid
        grid = (3, 3)
        s = 300. / max(grid)
        for i, mesh in enumerate(self.meshes):
            x = 800. * (i % grid[0]) / grid[0] + 400. / grid[0] - 2
            y = 800. * (i // grid[1]) / grid[1] + 400. / grid[1] + 2
            mesh.transform = ChainTransform([STTransform(translate=(x, y),
                                                         scale=(s, s, 1)),
                                             self.rotation])

        vispy.scene.SceneCanvas.__init__(self, keys='interactive')

        self.size = (800, 800)
        self.show()

        self.timer = vispy.app.Timer(connect=self.rotate)
        self.timer.start(0.016)
Example #11
0
 def on_initialize(self, event):
     self.text = Text('Hello world!', bold=True)
     # We need to give a transform to our visual
     self.transform = STTransform()
     self.text._program.vert['transform'] = self.transform.shader_map()
     self.apply_zoom()
Example #12
0
 def __init__(self):
     self.image = visuals.Image(image, method='subdivide')
     self.image.transform = STTransform(scale=(7, 7), translate=(50, 50))
     vispy.scene.SceneCanvas.__init__(self, keys='interactive')
     self.size = (800, 800)
     self.show()
Example #13
0
 def on_initialize(self, event):
     # We need to give a transform to our visual
     self.transform = STTransform()
     self.markers._program.vert['transform'] = self.transform.shader_map()
     self.apply_zoom()