Пример #1
0
def axis_visual(scale=1.0, parent=None):
    """
    Returns a :class:`vispy.scene.visuals.XYZAxis` class instance using given
    scale.

    Parameters
    ----------
    scale : numeric, optional
        Axis visual scale.
    parent : Node, optional
        Parent of the axis visual in the `SceneGraph`.

    Returns
    -------
    XYZAxis
        Axis visual.
    """

    axis = XYZAxis(parent=parent)

    transform = MatrixTransform()
    transform.scale((scale, scale, scale))
    axis.transform = transform

    return axis
Пример #2
0
class dummy(scene.visuals.Cube):
    """
    creates a custom draft to demonstrate what is not working
    """
    def __init__(self,view,x,y,z,face_color,edge_color):
        scene.visuals.Cube.__init__(self,(x,y,z),color=face_color,edge_color=edge_color,parent=view)

    def trafo(self,x=0.,y=0.,z=0.,angle=0.,al=0.,be=0.,ga=0.):
        #Not Working
        self.transform = MatrixTransform()
        self.transform = self.transform.rotate(angle,[al,be,ga])
        self.transform = self.transform.translate([x,y,z])
Пример #3
0
    def __init__(self):
        vispy.app.Canvas.__init__(self, keys='interactive', size=(800, 800))
        
        # Create 4 copies of an image to be displayed with different transforms
        image = get_image()
        self.images = [visuals.ImageVisual(image, method='impostor')
                       for i in range(4)]
        
        # Transform all images to a standard size / location (because
        # get_image() might return unexpected sizes)
        s = 100. / max(self.images[0].size)
        tx = 0.5 * (100 - (self.images[0].size[0] * s))
        ty = 0.5 * (100 - (self.images[0].size[1] * s))
        base_tr = STTransform(scale=(s, s), translate=(tx, ty))
        
        self.images[0].transform = (STTransform(scale=(30, 30),
                                                translate=(600, 600)) *
                                    SineTransform() *
                                    STTransform(scale=(0.1, 0.1),
                                                translate=(-5, -5)) *
                                    base_tr)

        tr = MatrixTransform()
        tr.rotate(40, (0, 0, 1))
        tr.rotate(30, (1, 0, 0))
        tr.translate((0, -20, -60))
        
        p = MatrixTransform()
        p.set_perspective(0.5, 1, 0.1, 1000)
        tr = p * tr
        
        tr1 = (STTransform(translate=(200, 600)) *
               tr *
               STTransform(translate=(-50, -50)) *
               base_tr)
        self.images[1].transform = tr1

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

        tr3 = (STTransform(scale=(400, 400), translate=(570, 400)) *
               PolarTransform() *
               STTransform(scale=(np.pi/150, -0.005),
                           translate=(-3.3*np.pi/4., 0.7)) *
               base_tr)
        self.images[3].transform = tr3

        text = visuals.TextVisual(
            text=['logarithmic', 'polar', 'perspective', 'custom (sine)'],
            pos=[(100, 20), (500, 20), (100, 410), (500, 410)],
            color='k', font_size=16)
        
        self.visuals = self.images + [text]

        self.show()
Пример #4
0
class Canvas(app.Canvas):

    def __init__(self):
        app.Canvas.__init__(self, keys='interactive', size=(800, 550))

        vertices, faces, outline = create_box(width=1, height=1, depth=1,
                                              width_segments=4,
                                              height_segments=8,
                                              depth_segments=16)

        self.box = visuals.BoxVisual(width=1, height=1, depth=1,
                                     width_segments=4,
                                     height_segments=8,
                                     depth_segments=16,
                                     vertex_colors=vertices['color'],
                                     edge_color='b')

        self.theta = 0
        self.phi = 0

        self.transform = MatrixTransform()

        self.box.transform = self.transform
        self.show()

        self.timer = app.Timer(connect=self.rotate)
        self.timer.start(0.016)

    def rotate(self, event):
        self.theta += .5
        self.phi += .5
        self.transform.reset()
        self.transform.rotate(self.theta, (0, 0, 1))
        self.transform.rotate(self.phi, (0, 1, 0))
        self.transform.scale((100, 100, 0.001))
        self.transform.translate((200, 200))
        self.update()

    def on_resize(self, event):
        # Set canvas viewport and reconfigure visual transforms to match.
        vp = (0, 0, self.physical_size[0], self.physical_size[1])
        self.context.set_viewport(*vp)

        self.box.transforms.configure(canvas=self, viewport=vp)

    def on_draw(self, ev):
        gloo.clear(color='white', depth=True)
        self.box.draw()
Пример #5
0
    def __init__(self):
        app.Canvas.__init__(self, keys='interactive', size=(800, 550))

        vertices, faces, outline = create_box(width=1, height=1, depth=1,
                                              width_segments=4,
                                              height_segments=8,
                                              depth_segments=16)

        self.box = visuals.BoxVisual(width=1, height=1, depth=1,
                                     width_segments=4,
                                     height_segments=8,
                                     depth_segments=16,
                                     vertex_colors=vertices['color'],
                                     edge_color='b')

        self.theta = 0
        self.phi = 0

        self.transform = MatrixTransform()

        self.box.transform = self.transform
        self.show()

        self.timer = app.Timer(connect=self.rotate)
        self.timer.start(0.016)
Пример #6
0
    def __init__(self):
        app.Canvas.__init__(self, keys='interactive', size=(800, 550))

        self.meshes = []
        self.rotation = MatrixTransform()

        # 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(visuals.MeshVisual(meshdata=mdata, color='b'))

        # 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.get_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 = visuals.MeshVisual(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.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)
        self.meshes.append(visuals.MeshVisual(verts, faces, vcolor))
        self.meshes.append(visuals.MeshVisual(verts, faces, vcolor,
                                              shading='flat'))
        self.meshes.append(visuals.MeshVisual(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
            transform = ChainTransform([STTransform(translate=(x, y),
                                                    scale=(s, s, s)),
                                        self.rotation])
            mesh.transform = transform
            mesh.transforms.scene_transform = STTransform(scale=(1, 1, 0.01))

        self.show()

        self.timer = app.Timer(connect=self.rotate)
        self.timer.start(0.016)
Пример #7
0
    def __init__(self, h):
        self.h = h
        app.Canvas.__init__(self, keys='interactive', size=(800, 550))
        
        hcyl = mplcyl.TruncatedCone()
        print('1')
        #plot_tc(p0=np.array([1, 3, 2]), p1=np.array([8, 5, 9]), R=[5.0, 2.0])
        verts, edges = h.get_geometry()

        self.meshes = []
        self.rotation = MatrixTransform()
        sec_ids = []
        s = 1.0
        x, y = 0., 0.
        for edge in edges:
            ends = verts['pos'][edge]  # xyz coordinate of one end [x,y,z]
            dia = verts['dia'][edge]  # diameter at that end
            sec_id = verts['sec_index'][edge[0]]  # save the section index
            
            dif = ends[1]-ends[0]  # distance between the ends
            length = (dif**2).sum() ** 0.5
            # print length
            # print dia
            #C, T, B = hcyl.make_truncated_cone(p0=ends[0], p1=ends[1], R=[dia[0]/2., dia[1]/2.])
            mesh_verts =  create_cylinder(8, 8, radius=[dia[0]/2., dia[1]/2.], length=length, offset=False)
            #mesh_verts = create_grid_mesh(C[0], C[1], C[2])

            
            # sec_id_array = np.empty(mesh_verts.shape[0]*3, dtype=int)
            # # sec_id_array[:] = sec_id
            # meshes.append(mesh_verts)
            # sec_ids.append(sec_id_array)
            self.meshes.append(visuals.MeshVisual(meshdata=mesh_verts, color='r'))

#             transform = ChainTransform([STTransform(translate=(x, y),
#                                                     scale=(s, s, s)),
#                                         self.rotation])
#
#         for i, mesh in enumerate(self.meshes):
# #            x = 800. * (i % grid[0]) / grid[0] + 40
#             mesh.transform = transform
#             mesh.transforms.scene_transform = STTransform(scale=(1, 1, 0.01))
        
        gloo.set_viewport(0, 0, *self.physical_size)
        gloo.clear(color='white', depth=True)

        for mesh in self.meshes:
            mesh.draw()

        print('running')
        self.show()
        if sys.flags.interactive != 1:
            app.run()
Пример #8
0
class vispy_Cylinders(app.Canvas):
    """
    Input:
        h: HocReader instance
    """

    def __init__(self, h):
        self.h = h
        app.Canvas.__init__(self, keys='interactive', size=(800, 550))
        
        hcyl = mplcyl.TruncatedCone()
        print('1')
        #plot_tc(p0=np.array([1, 3, 2]), p1=np.array([8, 5, 9]), R=[5.0, 2.0])
        verts, edges = h.get_geometry()

        self.meshes = []
        self.rotation = MatrixTransform()
        sec_ids = []
        s = 1.0
        x, y = 0., 0.
        for edge in edges:
            ends = verts['pos'][edge]  # xyz coordinate of one end [x,y,z]
            dia = verts['dia'][edge]  # diameter at that end
            sec_id = verts['sec_index'][edge[0]]  # save the section index
            
            dif = ends[1]-ends[0]  # distance between the ends
            length = (dif**2).sum() ** 0.5
            # print length
            # print dia
            #C, T, B = hcyl.make_truncated_cone(p0=ends[0], p1=ends[1], R=[dia[0]/2., dia[1]/2.])
            mesh_verts =  create_cylinder(8, 8, radius=[dia[0]/2., dia[1]/2.], length=length, offset=False)
            #mesh_verts = create_grid_mesh(C[0], C[1], C[2])

            
            # sec_id_array = np.empty(mesh_verts.shape[0]*3, dtype=int)
            # # sec_id_array[:] = sec_id
            # meshes.append(mesh_verts)
            # sec_ids.append(sec_id_array)
            self.meshes.append(visuals.MeshVisual(meshdata=mesh_verts, color='r'))

#             transform = ChainTransform([STTransform(translate=(x, y),
#                                                     scale=(s, s, s)),
#                                         self.rotation])
#
#         for i, mesh in enumerate(self.meshes):
# #            x = 800. * (i % grid[0]) / grid[0] + 40
#             mesh.transform = transform
#             mesh.transforms.scene_transform = STTransform(scale=(1, 1, 0.01))
        
        gloo.set_viewport(0, 0, *self.physical_size)
        gloo.clear(color='white', depth=True)

        for mesh in self.meshes:
            mesh.draw()

        print('running')
        self.show()
        if sys.flags.interactive != 1:
            app.run()
        #exit(1)
        
    def rotate(self, event):
        # rotate with an irrational amount over each axis so there is no
        # periodicity
        self.rotation.rotate(0.2 ** 0.5, (1, 0, 0))
        self.rotation.rotate(0.3 ** 0.5, (0, 1, 0))
        self.rotation.rotate(0.5 ** 0.5, (0, 0, 1))
        self.update()

    def on_resize(self, event):
        # Set canvas viewport and reconfigure visual transforms to match.
        vp = (0, 0, self.physical_size[0], self.physical_size[1])
        self.context.set_viewport(*vp)

        for mesh in self.meshes:
            mesh.transforms.configure(canvas=self, viewport=vp)

    def on_draw(self, ev):
        gloo.set_viewport(0, 0, *self.physical_size)
        gloo.clear(color='black', depth=True)

        for mesh in self.meshes:
            mesh.draw()
Пример #9
0
    'D:\\Databases\\toy_db\\db_mne\\meg_causal\\subject_02\\src\\subject_02_cortical'
)
rr = src[0]['rr']

fname_mesh = op.join(db_bv, 'hiphop138-multiscale', 'Decimated', '4K',
                     'hiphop138_Lwhite_dec_4K_parcels_marsAtlas.gii')
giftiImage = gifti.giftiio.read(fname_mesh)
values = giftiImage.darrays[0].data
parcels, counts = np.unique(values, return_counts=True)

trans = mne.read_trans(
    'D:\\Databases\\toy_db\\db_mne\\meg_causal\\subject_02\\trans\\subject_02-trans.fif'
)
trans = trans['trans']

mt = MatrixTransform(trans)
rr = mt.map(rr * 1000)[:, 0:-1]

cmap = np.zeros(rr.shape)
col = np.vstack((np.linspace(1, 0,
                             len(parcels)), np.linspace(0, 1, len(parcels)),
                 np.roll(np.linspace(1, 0, len(parcels)), 21))).T
for p in parcels:
    cmap[values == p] = col[parcels == p]

se = SceneObj(bgcolor='black')
brain = BrainObj(
    'D:\\Databases\\toy_db\\db_mne\\meg_causal\\subject_02\\surf\\subject_02_Lwhite.gii'
)
src = SourceObj('coords', rr, color=cmap)
se.add_to_subplot(brain)
Пример #10
0
class Canvas(app.Canvas):
    def __init__(self):
        app.Canvas.__init__(self, keys='interactive', size=(800, 550))

        self.meshes = []
        self.rotation = MatrixTransform()

        # 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(visuals.MeshVisual(meshdata=mdata, color='b'))

        # 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.get_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 = visuals.MeshVisual(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.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)
        self.meshes.append(visuals.MeshVisual(verts, faces, vcolor))
        self.meshes.append(visuals.MeshVisual(verts, faces, vcolor,
                                              shading='flat'))
        self.meshes.append(visuals.MeshVisual(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
            transform = ChainTransform([STTransform(translate=(x, y),
                                                    scale=(s, s, s)),
                                        self.rotation])
            mesh.transform = transform
            mesh.transforms.scene_transform = STTransform(scale=(1, 1, 0.01))

        self.show()

        self.timer = app.Timer(connect=self.rotate)
        self.timer.start(0.016)

    def rotate(self, event):
        # rotate with an irrational amount over each axis so there is no
        # periodicity
        self.rotation.rotate(0.2 ** 0.5, (1, 0, 0))
        self.rotation.rotate(0.3 ** 0.5, (0, 1, 0))
        self.rotation.rotate(0.5 ** 0.5, (0, 0, 1))
        self.update()

    def on_resize(self, event):
        # Set canvas viewport and reconfigure visual transforms to match.
        vp = (0, 0, self.physical_size[0], self.physical_size[1])
        self.context.set_viewport(*vp)

        for mesh in self.meshes:
            mesh.transforms.configure(canvas=self, viewport=vp)

    def on_draw(self, ev):
        gloo.set_viewport(0, 0, *self.physical_size)
        gloo.clear(color='black', depth=True)

        for mesh in self.meshes:
            mesh.draw()
Пример #11
0
from playground.utils import rgb2gray
from vispy.visuals.transforms import MatrixTransform
from vispy.io.mesh import read_mesh


filename = './constraint_cue.obj'

canvas = scene.SceneCanvas(keys='interactive')
view = canvas.central_widget.add_view()

(vertices, faces, vertex_colors, _) = read_mesh(filename)
vertex_colors = rgb2gray(vertex_colors) 
print(vertex_colors)
# color = np.zeros((ys.shape[0], 4)) * np.array([0,1,1,1])
N = vertex_colors.shape[0]
mesh = scene.visuals.Mesh(vertices, faces, vertex_colors, shading=None)
view.camera = 'turntable'

maze_scale_factor = 100
transform = MatrixTransform()
transform.scale(scale=[100,100,100,100])
# transform.translate(pos=[8100.4286, 8000])
mesh.transform = transform 

view.add(mesh)

canvas.show()

if __name__ == '__main__':
    canvas.app.run()
Пример #12
0
def _niimg_rot():
    """Get rotation trnasformations of each slice."""
    # Sagittal
    sg_rot = MatrixTransform()
    sg_rot.rotate(90., (0, 0, 1))
    sg_rot.rotate(180., (0, 1, 0))
    # Coronal
    cr_rot = MatrixTransform()
    cr_rot.rotate(90., (0, 0, 1))
    cr_rot.rotate(180., (0, 1, 0))
    # Axial
    ax_rot = MatrixTransform()
    ax_rot.rotate(180., (1, 0, 0))
    return sg_rot, cr_rot, ax_rot
Пример #13
0
def test_multiprogram():
    vert = """
    uniform float u_scale;
    
    void main() {
        gl_Position = $transform(vec4(0, 0, 0, 0));
    }
    """

    frag = """
    void main() {
        gl_FragColor = $color;
        $post_hook
    }
    """

    # test adding programs
    mp = MultiProgram(vert, frag)
    p1 = mp.add_program()
    p2 = mp.add_program('p2')
    assert 'p2' in mp._programs

    # test weak reference to program
    mp.add_program('junk')
    assert 'junk' not in mp._programs and len(mp._programs) == 2

    # test setting variables on multiprogram or individual programs
    mp['u_scale'] = 2
    assert p1['u_scale'] == 2
    assert p2['u_scale'] == 2

    p1['u_scale'] = 3
    assert p1['u_scale'] == 3
    assert p2['u_scale'] == 2

    # test setting template variables globally
    mp.frag['color'] = (1, 1, 1, 1)
    assert p1.frag['color'].value == (1, 1, 1, 1)
    assert p2.frag['color'].value == (1, 1, 1, 1)

    # test setting template variables per-program
    func = Function("""
    void filter() {
        gl_FragColor.r = 0.5;
    }
    """)
    p1.frag['post_hook'] = StatementList()
    p2.frag['post_hook'] = StatementList()
    p2.frag['post_hook'].add(func())

    tr1 = STTransform()
    tr2 = MatrixTransform()
    p1.vert['transform'] = tr1
    p2.vert['transform'] = tr2

    assert 'st_transform_map' in p1.vert.compile()
    assert 'affine_transform_map' in p2.vert.compile()
    assert 'filter' not in p1.frag.compile()
    assert 'filter' in p2.frag.compile()

    # test changing shader code
    mp.vert = vert + '\n//test\n'
    mp.vert['transform'] = tr1
    assert '//test' in p1.vert.compile()

    # test that newly-added programs inherit all previously set variables
    p3 = mp.add_program()
    assert p3['u_scale'] == 2
    assert p3.frag['color'].value == (1, 1, 1, 1)
    assert '//test' in p3.vert.compile()
    assert 'st_transform_map' in p3.vert.compile()
Пример #14
0
 def close(self):
     """Vispy visual is closing."""
     disconnect_events(self.layer.events, self)
     self.node.transforms = MatrixTransform()
     self.node.parent = None
Пример #15
0

    #print(bodyT)    #ok
    #print(jointT)   #ok

    verts, faces, normals, nothin = io.read_mesh(objPath+obj)   #verts preserve the number of vertex but the order is not preserved
    verts*=0.01

    vertexMod = scaleMatDict[name] @ np.vstack([verts.transpose(),np.ones([1,verts.shape[0]])]) #(4,n)
    verts = vertexMod[:3,:].transpose()

    objMesh = scene.visuals.Mesh(parent=view.scene,vertices=verts, shading='flat', faces=faces,color=(0.8, 0.8, 0.8,0.2)) #'flat' is much faster than 'smooth', None removes lighting
    objMesh.set_gl_state('translucent', cull_face=False,depth_test=False)
    
    aBox=scene.visuals.Box(parent=view.scene,width=size[0],depth=size[1],height=size[2],color=(0.8,0.1,0.1,0.2))
    aBox.transform=MatrixTransform(bodyT.transpose())   #transpose to match openGL format
    aBox.set_gl_state('translucent', cull_face=False,depth_test=False)



    #if(obj in markerDict):
    #    v=trimesh.load(objPath+obj, process=False).vertices #use this library because it preserve the order of vertex 
    #    v*=0.01
    #    for tup in markerDict[obj]:
    #        vertexIndex,markerName=tup  #markerDict[obj][i]
    #        markerPosition.append(v[vertexIndex,:])
    #        print(obj,markerName,vertexIndex,v[vertexIndex,:])
    #else:
    #    print(obj)

humanJointPositionList=np.stack(humanJointPositionList)
Пример #16
0
class Canvas(app.Canvas):
    def __init__(self):
        app.Canvas.__init__(self, keys='interactive', size=(800, 550))

        self.meshes = []
        self.rotation = MatrixTransform()

        # Generate some data to work with
        global mdata
        mdata = create_sphere(20, 40, 1.0)
        verts = mdata.get_vertices()
        faces = mdata.get_faces()
        gamma, phi = xyz_to_gp(verts)
        data = np.cos(5*gamma)+np.cos(phi*4)/2.
        mesh = visuals.MeshVisual(vertices=verts, faces=faces,
                                  banded=False, nband=8,
                                  vertex_values=data)
        mesh.cmap = 'viridis'
        self.meshes.append(mesh)
        mesh = visuals.MeshVisual(vertices=verts,
                                  faces=faces,  banded=True, nband=8,
                                  vertex_values=data)
        mesh.cmap = 'viridis'
        self.meshes.append(mesh)
        mesh = visuals.MeshVisual(vertices=verts,
                                  faces=faces,  banded=False, nband=8,
                                  vertex_values=data, shading='flat')
        mesh.cmap = 'viridis'
        self.meshes.append(mesh)
        mesh = visuals.MeshVisual(vertices=verts,
                                  faces=faces,  banded=True, nband=8,
                                  vertex_values=data, shading='flat')
        mesh.cmap = 'viridis'
        self.meshes.append(mesh)
        mesh = visuals.MeshVisual(vertices=verts,
                                  faces=faces,  banded=False, nband=8,
                                  vertex_values=data, shading='smooth')
        mesh.cmap = 'viridis'
        mesh.shininess = 0.01
        self.meshes.append(mesh)
        # Mesh with color indexed into a colormap
        mesh = visuals.MeshVisual(vertices=verts,
                                  faces=faces,  banded=True, nband=8,
                                  vertex_values=data, shading='smooth')
        mesh.cmap = 'viridis'
        mesh.shininess = 0.01
        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
            transform = ChainTransform([STTransform(translate=(x, y),
                                                    scale=(s, s, s)),
                                        self.rotation])
            mesh.transform = transform
            mesh.transforms.scene_transform = STTransform(scale=(1, 1, 0.01))

        self.show()

        self.timer = app.Timer(connect=self.rotate)
        self.timer.start(0.016)

    def rotate(self, event):
        # rotate with an irrational amount over each axis so there is no
        # periodicity
        self.rotation.rotate(0.2 ** 0.5, (1, 0, 0))
        self.rotation.rotate(0.3 ** 0.5, (0, 1, 0))
        self.rotation.rotate(0.5 ** 0.5, (0, 0, 1))
        self.update()

    def on_resize(self, event):
        # Set canvas viewport and reconfigure visual transforms to match.
        vp = (0, 0, self.physical_size[0], self.physical_size[1])
        self.context.set_viewport(*vp)

        for mesh in self.meshes:
            mesh.transforms.configure(canvas=self, viewport=vp)

    def on_draw(self, ev):
        gloo.set_viewport(0, 0, *self.physical_size)
        gloo.clear(color='black', depth=True)

        for mesh in self.meshes:
            mesh.draw()
Пример #17
0
    def __init__(self):
        app.Canvas.__init__(self, keys='interactive', size=(800, 550))

        self.meshes = []
        self.rotation = MatrixTransform()

        # 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(visuals.MeshVisual(meshdata=mdata, color='b'))

        # 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.
        rng = np.random.RandomState(0)
        verts = mdata.get_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] = rng.randn(nf, 1)
        fcolor[..., 2] = np.linspace(0, 1, nf)[:, np.newaxis]
        mesh = visuals.MeshVisual(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.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] = rng.randn(nv)
        vcolor[:, 2] = np.linspace(0, 1, nv)
        self.meshes.append(visuals.MeshVisual(verts, faces, vcolor))
        self.meshes.append(visuals.MeshVisual(verts, faces, vcolor,
                                              shading='flat'))
        self.meshes.append(visuals.MeshVisual(verts, faces, vcolor,
                                              shading='smooth'))

        # Mesh with color indexed into a colormap
        verts = mdata.get_vertices(None)
        faces = mdata.get_faces()
        values = rng.randn(len(verts))
        mesh = visuals.MeshVisual(vertices=verts, faces=faces,
                                  vertex_values=values, shading='smooth')
        mesh.clim = [-1, 1]
        mesh.cmap = 'viridis'
        mesh.shininess = 0.01
        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
            transform = ChainTransform([STTransform(translate=(x, y),
                                                    scale=(s, s, s)),
                                        self.rotation])
            mesh.transform = transform
            mesh.transforms.scene_transform = STTransform(scale=(1, 1, 0.01))

        self.show()

        self.timer = app.Timer(connect=self.rotate)
        self.timer.start(0.016)
Пример #18
0
class Canvas(app.Canvas):
    def __init__(self):
        app.Canvas.__init__(self, keys='interactive', size=(800, 550))

        self.meshes = []
        self.rotation = MatrixTransform()

        # 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(visuals.MeshVisual(meshdata=mdata, color='b'))

        # 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.
        rng = np.random.RandomState(0)
        verts = mdata.get_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] = rng.randn(nf, 1)
        fcolor[..., 2] = np.linspace(0, 1, nf)[:, np.newaxis]
        mesh = visuals.MeshVisual(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.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] = rng.randn(nv)
        vcolor[:, 2] = np.linspace(0, 1, nv)
        self.meshes.append(visuals.MeshVisual(verts, faces, vcolor))
        self.meshes.append(visuals.MeshVisual(verts, faces, vcolor,
                                              shading='flat'))
        self.meshes.append(visuals.MeshVisual(verts, faces, vcolor,
                                              shading='smooth'))

        # Mesh with color indexed into a colormap
        verts = mdata.get_vertices(None)
        faces = mdata.get_faces()
        values = rng.randn(len(verts))
        mesh = visuals.MeshVisual(vertices=verts, faces=faces,
                                  vertex_values=values, shading='smooth')
        mesh.clim = [-1, 1]
        mesh.cmap = 'viridis'
        mesh.shininess = 0.01
        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
            transform = ChainTransform([STTransform(translate=(x, y),
                                                    scale=(s, s, s)),
                                        self.rotation])
            mesh.transform = transform
            mesh.transforms.scene_transform = STTransform(scale=(1, 1, 0.01))

        self.show()

        self.timer = app.Timer(connect=self.rotate)
        self.timer.start(0.016)

    def rotate(self, event):
        # rotate with an irrational amount over each axis so there is no
        # periodicity
        self.rotation.rotate(0.2 ** 0.5, (1, 0, 0))
        self.rotation.rotate(0.3 ** 0.5, (0, 1, 0))
        self.rotation.rotate(0.5 ** 0.5, (0, 0, 1))
        self.update()

    def on_resize(self, event):
        # Set canvas viewport and reconfigure visual transforms to match.
        vp = (0, 0, self.physical_size[0], self.physical_size[1])
        self.context.set_viewport(*vp)

        for mesh in self.meshes:
            mesh.transforms.configure(canvas=self, viewport=vp)

    def on_draw(self, ev):
        gloo.set_viewport(0, 0, *self.physical_size)
        gloo.clear(color='black', depth=True)

        for mesh in self.meshes:
            mesh.draw()
Пример #19
0
  def __init__(self, image_funcs, axis='z', pos=0, limit=None,
               seismic_coord_system=True,
               cmaps=['grays'], clims=None,
               interpolation='nearest', method='auto'):

    assert clims is not None, 'clim must be specified explicitly.'

    # Create an Image obj and unfreeze it so we can add more
    # attributes inside.
    # First image (from image_funcs[0])
    scene.visuals.Image.__init__(self, parent=None, # no image func yet
      cmap=cmaps[0], clim=clims[0],
      interpolation=interpolation, method=method)
    self.unfreeze()
    self.interactive = True

    # Other images ...
    self.overlaid_images = [self]
    for i_img in range(1, len(image_funcs)):
      overlaid_image = scene.visuals.Image(parent=self,
        cmap=cmaps[i_img], clim=clims[i_img],
        interpolation=interpolation, method=method)
      self.overlaid_images.append(overlaid_image)

    # Set GL state. Must check depth test, otherwise weird in 3D.
    self.set_gl_state(depth_test=True, depth_func='lequal',
      blend_func=('src_alpha', 'one_minus_src_alpha'))

    # Determine the axis and position of this plane.
    self.axis = axis
    # Check if pos is within the range.
    if limit is not None:
      assert (pos>=limit[0]) and (pos<=limit[1]), \
        'pos={} is outside limit={} range.'.format(pos, limit)
    self.pos = pos
    self.limit = limit
    self.seismic_coord_system = seismic_coord_system

    # Get the image_func that returns either image or image shape.
    self.image_funcs = image_funcs # a list of functions!
    shape = self.image_funcs[0](self.pos, get_shape=True)

    # The selection highlight (a Plane visual with transparent color).
    # The plane is initialized before any rotation, on '+z' direction.
    self.highlight = scene.visuals.Plane(parent=self,
      width=shape[0], height=shape[1], direction='+z',
      color=(1, 1, 0, 0.1)) # transparent yellow color
    # Move the plane to align with the image.
    self.highlight.transform = STTransform(
      translate=(shape[0]/2, shape[1]/2, 0))
    # This is to make sure we can see highlight plane through the images.
    self.highlight.set_gl_state('additive', depth_test=True)
    self.highlight.visible = False # only show when selected

    # Set the anchor point (2D local world coordinates). The mouse will
    # drag this image by anchor point moving in the normal direction.
    self.anchor = None # None by default
    self.offset = 0

    # Apply SRT transform according to the axis attribute.
    self.transform = MatrixTransform()
    # Move the image plane to the corresponding location.
    self._update_location()

    self.freeze()
Пример #20
0
class AxisAlignedImage(scene.visuals.Image):
  """ Visual subclass displaying an image that aligns to an axis.
  This image should be able to move along the perpendicular direction when
  user gives corresponding inputs.

  Parameters:

  """
  def __init__(self, image_funcs, axis='z', pos=0, limit=None,
               seismic_coord_system=True,
               cmaps=['grays'], clims=None,
               interpolation='nearest', method='auto'):

    assert clims is not None, 'clim must be specified explicitly.'

    # Create an Image obj and unfreeze it so we can add more
    # attributes inside.
    # First image (from image_funcs[0])
    scene.visuals.Image.__init__(self, parent=None, # no image func yet
      cmap=cmaps[0], clim=clims[0],
      interpolation=interpolation, method=method)
    self.unfreeze()
    self.interactive = True

    # Other images ...
    self.overlaid_images = [self]
    for i_img in range(1, len(image_funcs)):
      overlaid_image = scene.visuals.Image(parent=self,
        cmap=cmaps[i_img], clim=clims[i_img],
        interpolation=interpolation, method=method)
      self.overlaid_images.append(overlaid_image)

    # Set GL state. Must check depth test, otherwise weird in 3D.
    self.set_gl_state(depth_test=True, depth_func='lequal',
      blend_func=('src_alpha', 'one_minus_src_alpha'))

    # Determine the axis and position of this plane.
    self.axis = axis
    # Check if pos is within the range.
    if limit is not None:
      assert (pos>=limit[0]) and (pos<=limit[1]), \
        'pos={} is outside limit={} range.'.format(pos, limit)
    self.pos = pos
    self.limit = limit
    self.seismic_coord_system = seismic_coord_system

    # Get the image_func that returns either image or image shape.
    self.image_funcs = image_funcs # a list of functions!
    shape = self.image_funcs[0](self.pos, get_shape=True)

    # The selection highlight (a Plane visual with transparent color).
    # The plane is initialized before any rotation, on '+z' direction.
    self.highlight = scene.visuals.Plane(parent=self,
      width=shape[0], height=shape[1], direction='+z',
      color=(1, 1, 0, 0.1)) # transparent yellow color
    # Move the plane to align with the image.
    self.highlight.transform = STTransform(
      translate=(shape[0]/2, shape[1]/2, 0))
    # This is to make sure we can see highlight plane through the images.
    self.highlight.set_gl_state('additive', depth_test=True)
    self.highlight.visible = False # only show when selected

    # Set the anchor point (2D local world coordinates). The mouse will
    # drag this image by anchor point moving in the normal direction.
    self.anchor = None # None by default
    self.offset = 0

    # Apply SRT transform according to the axis attribute.
    self.transform = MatrixTransform()
    # Move the image plane to the corresponding location.
    self._update_location()

    self.freeze()

  @property
  def axis(self):
    """The dimension that this image is perpendicular aligned to."""
    return self._axis

  @axis.setter
  def axis(self, value):
    value = value.lower()
    if value not in ('z', 'y', 'x'):
      raise ValueError('Invalid value for axis.')
    self._axis = value

  def set_anchor(self, mouse_press_event):
    """ Set an anchor point (2D coordinate on the image plane) when left click
    in the selection mode (<Ctrl> pressed). After that, the dragging called
    in func 'drag_visual_node' will try to move along the normal direction
    and let the anchor follows user's mouse position as close as possible.
    """
    # Get the screen-to-local transform to get camera coordinates.
    tr = self.canvas.scene.node_transform(self)

    # Get click (camera) coordinate in the local world.
    click_pos = tr.map([*mouse_press_event.pos, 0, 1])
    click_pos /= click_pos[3] # rescale to cancel out the pos.w factor
    # Get the view direction (camera-to-target) vector in the local world.
    view_vector = tr.map([*mouse_press_event.pos, 1, 1])[:3]
    view_vector /= np.linalg.norm(view_vector) # normalize to unit vector

    # Get distance from camera to the drag anchor point on the image plane.
    # Eq 1: click_pos + distance * view_vector = anchor
    # Eq 2: anchor[2] = 0 <- intersects with the plane
    # The following equation can be derived by Eq 1 and Eq 2.
    distance = (0. - click_pos[2]) / view_vector[2]
    self.anchor = click_pos[:2] + distance * view_vector[:2] # only need vec2

  def drag_visual_node(self, mouse_move_event):
    """ Drag this visual node while holding left click in the selection mode
    (<Ctrl> pressed). The plane will move in the normal direction
    perpendicular to this image, and the anchor point (set with func
    'set_anchor') will move along the normal direction to stay as close to
    the mouse as possible, so that user feels like 'dragging' the plane.
    """
    # Get the screen-to-local transform to get camera coordinates.
    tr = self.canvas.scene.node_transform(self)

    # Unlike in 'set_anchor', we now convert most coordinates to the screen
    # coordinate system, because it's more intuitive for user to do operations
    # in 2D and get 2D feedbacks, e.g. mouse leading the anchor point.
    anchor = [*self.anchor, self.pos, 1] # 2D -> 3D
    anchor_screen = tr.imap(anchor) # screen coordinates of the anchor point
    anchor_screen /= anchor_screen[3] # rescale to cancel out 'w' term
    anchor_screen = anchor_screen[:2] # only need vec2

    # Compute the normal vector, starting from the anchor point and
    # perpendicular to the image plane.
    normal = [*self.anchor, self.pos+1, 1] # +[0,0,1,0] from anchor
    normal_screen = tr.imap(normal) # screen coordinates of anchor + [0,0,1,0]
    normal_screen /= normal_screen[3] # rescale to cancel out 'w' term
    normal_screen = normal_screen[:2] # only need vec2
    normal_screen -= anchor_screen # end - start = vector
    normal_screen /= np.linalg.norm(normal_screen) # normalize to unit vector

    # Use the vector {anchor_screen -> mouse.pos} and project to the
    # normal_screen direction using dot product, we can get how far the plane
    # should be moved (on the screen!).
    drag_vector = mouse_move_event.pos[:2] - anchor_screen
    drag = np.dot(drag_vector, normal_screen) # normal_screen must be length 1

    # We now need to convert the move distance from screen coordinates to
    # local world coordinates. First, find where the anchor is on the screen
    # after dragging; then, convert that screen point to a local line shooting
    # across the normal vector; finally, find where the line comes directly
    # above/below the anchor point (before dragging) and get that distance as
    # the true dragging distance in local coordinates.
    new_anchor_screen = anchor_screen + normal_screen * drag
    new_anchor = tr.map([*new_anchor_screen, 0, 1])
    new_anchor /= new_anchor[3] # rescale to cancel out the pos.w factor
    view_vector = tr.map([*new_anchor_screen, 1, 1])[:3]
    view_vector /= np.linalg.norm(view_vector) # normalize to unit vector
    # Solve this equation:
    # new_anchor := new_anchor + view_vector * ?,
    # ^^^ describe a 3D line of possible new anchor positions
    # arg min (?) |new_anchor[:2] - anchor[:2]|
    # ^^^ find a point on that 3D line that minimize the 2D distance between
    #     new_anchor and anchor.
    numerator = anchor[:2] - new_anchor[:2]
    numerator *= view_vector[:2] # element-wise multiplication
    numerator = np.sum(numerator)
    denominator = view_vector[0]**2 + view_vector[1]**2
    shoot_distance = numerator / denominator
    # Shoot from new_anchor to get the new intersect point. The z- coordinate
    # of this point will be our dragging offset.
    offset = new_anchor[2] + view_vector[2] * shoot_distance

    # Note: must reverse normal direction from -y direction to +y!
    if self.axis == 'y': offset = -offset
    # Limit the dragging within range.
    if self.limit is not None:
      if self.pos + offset < self.limit[0]: offset = self.limit[0] - self.pos
      if self.pos + offset > self.limit[1]: offset = self.limit[1] - self.pos
    self.offset = offset
    # Note: must reverse normal direction from +y direction to -y!
    if self.axis == 'y': offset = -offset

    self._update_location()

  def _update_location(self):
    """ Update the image plane to the dragged location and redraw this image.
    """
    self.pos += self.offset
    self.pos = int(np.round(self.pos)) # must round to nearest integer location

    # Update the transformation in order to move to new location.
    self.transform.reset()
    if self.axis == 'z':
      # 1. No rotation to do for z axis (y-x) slice. Only translate.
      self.transform.translate((0, 0, self.pos))
    elif self.axis == 'y':
      # 2. Rotation(s) for the y axis (z-x) slice, then translate:
      self.transform.rotate(90, (1, 0, 0))
      self.transform.translate((0, self.pos, 0))
    elif self.axis == 'x':
      # 3. Rotation(s) for the x axis (z-y) slice, then translate:
      self.transform.rotate(90, (1, 0, 0))
      self.transform.rotate(90, (0, 0, 1))
      self.transform.translate((self.pos, 0, 0))

    # Update image on the slice based on current position. The numpy array
    # is transposed due to a conversion from i-j to x-y axis system.
    # First image, the primary one:
    self.set_data(self.image_funcs[0](self.pos).T)
    # Other images, overlaid on the primary image:
    for i_img in range(1, len(self.image_funcs)):
      self.overlaid_images[i_img].set_data(
        self.image_funcs[i_img](self.pos).T)

    # Reset attributes after dragging completes.
    self.offset = 0
    self._bounds_changed() # update the bounds with new self.pos

  def _compute_bounds(self, axis_3d, view):
    """ Overwrite the original 2D bounds of the Image class. This will correct 
    the automatic range setting for the camera in the scene canvas. In the
    original Image class, the code assumes that the image always lies in x-y
    plane; here we generalize that to x-z and y-z plane.
    
    Parameters:
    axis_3d: int in {0, 1, 2}, represents the axis in 3D view box.
    view: the ViewBox object that connects to the parent.

    The function returns a tuple (low_bounds, high_bounds) that represents
    the spatial limits of self obj in the 3D scene.
    """
    # Note: self.size[0] is slow dim size, self.size[1] is fast dim size.
    if self.axis == 'z':
      if   axis_3d==0: return (0, self.size[0])
      elif axis_3d==1: return (0, self.size[1])
      elif axis_3d==2: return (self.pos, self.pos)
    elif self.axis == 'y':
      if   axis_3d==0: return (0, self.size[0])
      elif axis_3d==1: return (self.pos, self.pos)
      elif axis_3d==2: return (0, self.size[1])
    elif self.axis == 'x':
      if   axis_3d==0: return (self.pos, self.pos)
      elif axis_3d==1: return (0, self.size[0])
      elif axis_3d==2: return (0, self.size[1])
Пример #21
0
 def trafo(self,x=0.,y=0.,z=0.,angle=0.,al=0.,be=0.,ga=0.):
     #Not Working
     self.transform = MatrixTransform()
     self.transform = self.transform.rotate(angle,[al,be,ga])
     self.transform = self.transform.translate([x,y,z])
Пример #22
0
    def __init__(self):
        app.Canvas.__init__(self, keys='interactive', size=(800, 600))
        brain = np.load(load_data_file('brain/brain.npz', force_download='2014-09-04'))

        #brain1 = scipy.io.loadmat('NewBESATri.mat')
        brain2 = brain1['t']


        from numpy import genfromtxt
        my_data = genfromtxt('BESACOORD61.csv', delimiter=',')

        dataTest = brain['vertex_buffer']
        color = dataTest['a_color']
        color = color[750:1500]
        facesTest = brain['index_buffer']

        n = 750
        ps = 100
        data = np.zeros(n, [('a_position', np.float32, 3),
                            ('a_normal', np.float32, 3),
                            ('a_color', np.float32, 3)])
                            #('a_size', np.float32, 1)])

        import scipy.spatial
        tri = scipy.spatial.Delaunay(my_data) # points: np.array() of 3d points
        convex = tri.convex_hull
        indices = tri.simplices
        vertices = my_data[indices]
        data['a_position'] = my_data
        data['a_color'] = np.random.uniform(0, 1, (n, 3))
        faces = brain2
        faces = faces-1
        vertices = my_data
        norm = np.zeros( vertices.shape, dtype=vertices.dtype )
        #Create an indexed view into the vertex array using the array of three indices for triangles
        tris = vertices[faces]

        #Calculate the normal for all the triangles, by taking the cross product of the vectors v1-v0, and v2-v0 in each triangle
        n = np.cross( tris[::,1 ] - tris[::,0]  , tris[::,2 ] - tris[::,0] )
        # n is now an array of normals per triangle. The length of each normal is dependent the vertices,
        # we need to normalize these, so that our next step weights each normal equally.
        lens = np.sqrt( n[:,0]**2 + n[:,1]**2 + n[:,2]**2 )
        n[:,0] /= lens
        n[:,1] /= lens
        n[:,2] /= lens



        norm[ faces[:,0] ] += n
        norm[ faces[:,1] ] += n
        norm[ faces[:,2] ] += n

        ''' Normalize a numpy array of 3 component vectors shape=(n,3) '''
        lens = np.sqrt( norm[:,0]**2 + norm[:,1]**2 + norm[:,2]**2 )
        norm[:,0] /= lens
        norm[:,1] /= lens
        norm[:,2] /= lens

        data['a_normal'] = norm #np.random.uniform(0, 1.0, (n, 3)) #10, 3, 3
        #data['a_size'] = np.random.uniform(5*ps, 10*ps, n)
        u_linewidth = 1.0
        u_antialias = 1.0



        brain2 = np.uint32(brain2)
        convex = np.uint32(convex)
        data = data
        faces = brain2-1

        #data = dataTest
        #faces = facesTest

        self.meshes = []
        self.rotation = MatrixTransform()

        # Generate some data to work with
        global mdata
        mdata = create_sphere(20, 40, 1.0)
        #mdata['_faces'] =faces
        #mdata['_vertices']=data
        # Mesh with pre-indexed vertices, uniform color
        meshData=vispy.geometry.MeshData(vertices=data['a_position'], faces=None, edges=None, vertex_colors=None, face_colors=None)
        meshData._vertices = data['a_position']
        meshData._faces = faces
        #meshData._face_colors = data['a_color']
        #meshData._vertex_colors = data['a_color']

        self.meshes.append(visuals.MeshVisual(meshdata=meshData, color='b'))
        #for mesh in self.meshes:
        #    mesh.draw()

        # 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.
        tris = vertices[faces]
        verts = data['a_position'] #mdata.get_vertices(indexed='faces')

        nf = 1496#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]
        #fcolor = data['a_color']
        mesh = visuals.MeshVisual(vertices=tris, 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.get_vertices()
        faces = faces #mdata.get_faces()
        nv = verts.size//3
        vcolor = np.ones((nv, 4), dtype=np.float32)
        vcolor[:, 0] = np.linspace(.6, .6, nv)
        vcolor[:, 1] = np.random.normal(size=nv)
        vcolor[:, 2] = np.linspace(0.6, .6, nv)
        self.meshes.append(visuals.MeshVisual(verts, faces, vcolor))
        self.meshes.append(visuals.MeshVisual(verts, faces, vcolor,
                                              shading='flat'))
        #
        self.meshes.append(visuals.MeshVisual(verts, faces, vcolor,
                                              shading='smooth'))

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

        self.show()

        self.timer = app.Timer(connect=self.rotate)
        self.timer.start(0.00016)
Пример #23
0
class Colorbar(scene.visuals.Image):
    """ A colorbar visual fixed to the right side of the canvas. This is
  based on the rendering from Matplotlib, then display this rendered
  image as a scene.visuals.Image visual node on the canvas.

  Parameters:

  """
    def __init__(self,
                 size=(500, 10),
                 cmap='grays',
                 clim=None,
                 label_str="Colorbar",
                 label_color='black',
                 label_size=12,
                 tick_size=10,
                 border_width=1.0,
                 border_color='black',
                 visible=True,
                 parent=None):

        assert clim is not None, 'clim must be specified explicitly.'

        # Create a scene.visuals.Image (without parent by default).
        scene.visuals.Image.__init__(self,
                                     parent=None,
                                     interpolation='nearest',
                                     method='auto')
        self.unfreeze()
        self.visible = visible
        self.canvas_size = None  # will be set when parent is linked

        # Record the important drawing parameters.
        self.pos = (0, 0)
        self.bar_size = size  # tuple
        self.cmap = get_colormap(cmap)  # vispy Colormap
        self.clim = clim  # tuple

        # Record the styling parameters.
        self.label_str = label_str
        self.label_color = label_color
        self.label_size = label_size
        self.tick_size = tick_size
        self.border_width = border_width
        self.border_color = border_color

        # Draw colorbar using Matplotlib.
        self.set_data(self._draw_colorbar())

        # Give a Matrix transform to self in order to move around canvas.
        self.transform = MatrixTransform()

        self.freeze()

    def on_resize(self, event):
        """ When window is resized, only need to move the position in vertical
    direction, because the coordinate is relative to the secondary ViewBox
    that stays on the right side of the canvas.
    """
        pos = np.array(self.pos).astype(np.single)
        pos[1] *= event.size[1] / self.canvas_size[1]
        self.pos = tuple(pos)

        # Move the colorbar to specified position (with half-size padding, because
        # Image visual uses a different anchor (top-left corner) rather than the
        # center-left corner used by ColorBar visual.).
        self.transform.reset()
        self.transform.translate((
            self.pos[0] / 2.618,  # make the gap smaller :)
            self.pos[1] - self.size[1] / 2.))

        # Update the canvas size.
        self.canvas_size = event.size

    def _draw_colorbar(self):
        """ Draw a Matplotlib colorbar, save this figure without any boundary to a
    rendering buffer, and return this buffer as a numpy array.
    """
        dpi = get_dpi()
        # Compute the Matplotlib figsize: note the order of width, height.
        # The width is doubled because we will only keep the colorbar portion
        # before we export this image to buffer!
        figsize = (self.bar_size[1] / dpi * 2, self.bar_size[0] / dpi)

        # Convert cmap and clim to Matplotlib format.
        rgba = self.cmap.colors.rgba
        # Blend to white to avoid this Matplotlib rendering issue:
        # https://github.com/matplotlib/matplotlib/issues/1188
        for i in range(3):
            rgba[:, i] = (1 - rgba[:, -1]) + rgba[:, -1] * rgba[:, i]
        rgba[:, -1] = 1.
        if len(rgba) < 2:  # in special case of 'grays' cmap!
            rgba = np.array([[0, 0, 0, 1.], [1, 1, 1, 1.]])
        cmap = LinearSegmentedColormap.from_list('vispy_cmap', rgba)
        norm = mpl.colors.Normalize(vmin=self.clim[0], vmax=self.clim[1])
        sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)

        # Put the colorbar at proper location on the Matplotlib fig.
        fig = plt.figure(figsize=figsize, dpi=dpi)
        ax = plt.Axes(fig, [0, 0, 1, 1])
        ax.set_axis_off()
        fig.add_axes(ax)
        divider = make_axes_locatable(ax)
        cax = divider.append_axes('right', size='100%', pad=0.)
        cb = fig.colorbar(sm, cax=cax)
        ax.remove()

        # Apply styling to the colorbar.
        cb.set_label(self.label_str,
                     color=self.label_color,
                     fontsize=self.label_size)
        plt.setp(plt.getp(cb.ax.axes, 'yticklabels'),
                 color=self.label_color,
                 fontsize=self.tick_size)
        cb.ax.yaxis.set_tick_params(color=self.label_color)
        cb.outline.set_linewidth(self.border_width)
        cb.outline.set_edgecolor(self.border_color)

        # Export the rendering to a numpy array in the buffer.
        buf = io.BytesIO()
        fig.savefig(buf,
                    format='png',
                    bbox_inches='tight',
                    pad_inches=0,
                    dpi=dpi,
                    transparent=True)
        buf.seek(0)

        return plt.imread(buf)
Пример #24
0
    def __init__(self):
        app.Canvas.__init__(self, keys='interactive', size=(800, 550))

        self.meshes = []
        self.rotation = MatrixTransform()

        # Generate some data to work with
        global mdata
        mdata = create_sphere(20, 40, 1.0)
        verts = mdata.get_vertices()
        faces = mdata.get_faces()
        gamma, phi = xyz_to_gp(verts)
        data = np.cos(5*gamma)+np.cos(phi*4)/2.
        mesh = visuals.MeshVisual(vertices=verts, faces=faces,
                                  banded=False, nband=8,
                                  vertex_values=data)
        mesh.cmap = 'viridis'
        self.meshes.append(mesh)
        mesh = visuals.MeshVisual(vertices=verts,
                                  faces=faces,  banded=True, nband=8,
                                  vertex_values=data)
        mesh.cmap = 'viridis'
        self.meshes.append(mesh)
        mesh = visuals.MeshVisual(vertices=verts,
                                  faces=faces,  banded=False, nband=8,
                                  vertex_values=data, shading='flat')
        mesh.cmap = 'viridis'
        self.meshes.append(mesh)
        mesh = visuals.MeshVisual(vertices=verts,
                                  faces=faces,  banded=True, nband=8,
                                  vertex_values=data, shading='flat')
        mesh.cmap = 'viridis'
        self.meshes.append(mesh)
        mesh = visuals.MeshVisual(vertices=verts,
                                  faces=faces,  banded=False, nband=8,
                                  vertex_values=data, shading='smooth')
        mesh.cmap = 'viridis'
        mesh.shininess = 0.01
        self.meshes.append(mesh)
        # Mesh with color indexed into a colormap
        mesh = visuals.MeshVisual(vertices=verts,
                                  faces=faces,  banded=True, nband=8,
                                  vertex_values=data, shading='smooth')
        mesh.cmap = 'viridis'
        mesh.shininess = 0.01
        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
            transform = ChainTransform([STTransform(translate=(x, y),
                                                    scale=(s, s, s)),
                                        self.rotation])
            mesh.transform = transform
            mesh.transforms.scene_transform = STTransform(scale=(1, 1, 0.01))

        self.show()

        self.timer = app.Timer(connect=self.rotate)
        self.timer.start(0.016)
Пример #25
0
class Body():
    """
    binds tranfos to the bodies and the order of doing it
    """
    def __init__(self, state_vec, p):
        self.state_vec = state_vec
        self.n = 0
        self.n_max = len(state_vec)
        self.p = p
        self.radius = p/6.0

        self.rot = MatrixTransform()
        self.v_orient = None
        self.x = 0.
        self.y = 0.
        self.z = 0.
        #print("-------", self.n_max, self.p)

    def set_orient(self,v_orient):
        """
        set orientation to v_orient.

        :param v_orient: new orientation of point object
        """
        self.v_orient = v_orient

    def _update(self):
        """
        update

        rotation and then translation
        """
        global OO
        self.n += 1
        if self.n > self.n_max-1:
            self.n = 0
            return
        
        dx = self.state_vec[self.n][0] 
        dy = self.state_vec[self.n][1] 
        dz = self.state_vec[self.n][2]
            
        if self.v_orient:
            new_base = self.v_orient[self.n]
        else:
            new_base = None

        self.trafo(dx,dy,dz,base=new_base)

    def _norm(self, n):
        """ normalizing a vector n = (x,y,z) """
        x, y, z = n
        norm = np.sqrt(x*x + y*y + z*z)
        return x/norm, y/norm, z/norm

    def _cross(self, n0, n1):
        """ doing a crossproduct i.e. n1 x n2 """
        x0, y0, z0 = n0
        x1, y1, z1 = n1
        x = y0*z1 - z0*y1
        y = z0*x1 - x0*z1
        z = x0*y1 - y0*x1
        return x,y,z

    def _ortho(self, n):
        """ finding an arbitrary orthogonal vector to another in 3d """
        x,y,z = n
        if z!=0. and y!=0.:
            return 0., z, -y
        elif x!=0. and y!=0.:
            return y, -x, 0.
        elif x!=0. and z!=0.:
            return z, 0., -x
        else:
            return x,y,z+1

    def _get_ortho_base(self, n):
        """ 
        calc an ottho base for one direction, such that ex is pointing in the end to that direction 

        """
        ex = self._norm(n)        
        ey = self._norm(self._ortho(ex))
        ez = self._cross(ex, ey)
        return ex, ey, ez


    def trafo(self, x=0.,y=0.,z=0., base=None, scale=None, reset=True):
        """
        doing first the scale then rotation and afterwards translate
        """
        if reset:
            self.rot.reset()
        if scale is not None:
            self.rot.scale(scale)
        if base is not None:
            self.rot.mult_rotation(base)
        self.rot.translate((x,y,z))
        self.transform = self.rot