示例#1
0
    def _update_camera(self):
        if np.sum(self.viewer.dims.display) == 3:
            # Set a 3D camera
            self.view.camera = ArcballCamera(name="ArcballCamera")
            # flip y-axis to have correct alignment
            self.view.camera.flip = (0, 1, 0)
            min_shape, max_shape = self.viewer._calc_bbox()
            centroid = np.add(max_shape, min_shape) / 2
            size = np.subtract(max_shape, min_shape)
            # Scale the camera to the contents in the scene
            if len(centroid) > 0:
                centroid = centroid[-3:]
                self.view.camera.center = centroid[::-1]
                self.view.camera.scale_factor = 1.5 * np.mean(size[-3:])
        elif np.sum(self.viewer.dims.display) == 2:
            # Set 2D camera
            self.view.camera = PanZoomCamera(aspect=1, name="PanZoomCamera")
            # flip y-axis to have correct alignment
            self.view.camera.flip = (0, 1, 0)
            # Scale the camera to the contents in the scene
            self.view.camera.set_range()
        else:
            raise ValueError(
                "Invalid display flags set in dimensions {}".format(
                    self.viewer.dims.display
                )
            )

        self.view.camera.viewbox_key_event = viewbox_key_event
        # TO DO: Remove
        self.viewer._view = self.view
示例#2
0
    def __init__(self):

        self._canvas = SceneCanvas()

        self._viewbox = ViewBox(parent=self._canvas.scene)
        self._canvas.central_widget.add_widget(self._viewbox)
        self._viewbox.camera = ArcballCamera(fov=0)

        self._atlas_volume = Volume(
            array([[[1, 2]]]) * 1000,
            parent=self._viewbox.scene)  # , interpolation='nearest')
        self._atlas_volume.attach(filters.ColorFilter((1., .5, 0., 1.)))
        self._atlas_volume.set_gl_state('additive', depth_test=False)

        self._section_image = Image(parent=self._viewbox.scene, cmap='grays')
        self._section_image.attach(filters.ColorFilter((0., .5, 1., 1.)))
        self._section_image.set_gl_state('additive', depth_test=False)

        self._canvas.events.key_press.connect(
            self._handle_vispy_key_press_events)
示例#3
0
    def _update_camera(self):
        if self.viewer.dims.ndisplay == 3:
            # Set a 3D camera
            if not isinstance(self.view.camera, ArcballCamera):
                self.view.camera = ArcballCamera(name="ArcballCamera", fov=0)
                # flip y-axis to have correct alignment
                # self.view.camera.flip = (0, 1, 0)

                self.view.camera.viewbox_key_event = viewbox_key_event
                self.viewer.reset_view()
        else:
            # Set 2D camera
            if not isinstance(self.view.camera, PanZoomCamera):
                self.view.camera = PanZoomCamera(aspect=1,
                                                 name="PanZoomCamera")
                # flip y-axis to have correct alignment
                self.view.camera.flip = (0, 1, 0)

                self.view.camera.viewbox_key_event = viewbox_key_event
                self.viewer.reset_view()
示例#4
0
    def __init__(self, _model: VolumeViewModel):

        self._model = _model
        self._model.register(self.update)

        self._canvas = SceneCanvas()

        self._viewbox = ViewBox(parent=self._canvas.scene)
        self._canvas.central_widget.add_widget(self._viewbox)
        self._viewbox.camera = ArcballCamera(fov=0)

        self._atlas_volume = Volume(self._model.atlas_volume, parent=self._viewbox.scene)  # , interpolation='nearest')
        self._atlas_volume.attach(filters.ColorFilter((1., .5, 0., 1.)))
        self._atlas_volume.set_gl_state('additive', depth_test=False)

        self._section_image = Image(parent=self._viewbox.scene, cmap='grays')
        self._section_image.attach(filters.ColorFilter((0., .5, 1., 1.)))
        self._section_image.set_gl_state('additive', depth_test=False)

        self._canvas.events.key_press.connect(lambda event: self._model.press_key(event.key.name))
示例#5
0
    def __init__(self, view, camera, dims):
        self._view = view
        self._camera = camera
        self._dims = dims

        # Create 2D camera
        self._2D_camera = PanZoomCamera(aspect=1)
        # flip y-axis to have correct alignment
        self._2D_camera.flip = (0, 1, 0)
        self._2D_camera.viewbox_key_event = viewbox_key_event

        # Create 3D camera
        self._3D_camera = ArcballCamera(fov=0)
        self._3D_camera.viewbox_key_event = viewbox_key_event

        self._dims.events.ndisplay.connect(self._on_ndisplay_change,
                                           position='first')

        self._camera.events.center.connect(self._on_center_change)
        self._camera.events.zoom.connect(self._on_zoom_change)
        self._camera.events.angles.connect(self._on_angles_change)

        self._on_ndisplay_change(None)
示例#6
0
# Prepare atomic data:
atoms = [atom for atom in structure.get_atoms()]
natoms = len(atoms)
coordinates = np.array([atom.coord for atom in atoms])

# Move to center of coordinates:
center = centroid(coordinates)
coordinates -= center

#atom color
color = [colorrgba(atom.get_id()) for atom in atoms]

#atom radius
radius = [vrad(atom.get_id()) for atom in atoms]

# Window size
W, H = 1200, 800

MySpheres = scene.visuals.create_visual_node(MySpheresVisual)

canvas = scene.SceneCanvas(keys='interactive',
                           app='pyqt4',
                           bgcolor='white',
                           size=(W, H),
                           show=True)

view = canvas.central_widget.add_view()
view.camera = ArcballCamera()  #(fov=20, distance=300)
MySpheres(coordinates, color, radius, W, H, parent=view.scene)

canvas.app.run()
示例#7
0
color = []
chains = []
chain_coords = []
chain_colors = []
chain_radius = []
for chain in structure.get_chains():
    chains.append(chain)
    can_coord = np.array([atom.coord for atom in chain.get_atoms() if atom.get_name() =='CA' or atom.get_name() =='N'])
    can_coord -= center
    chain_coords.append(can_coord)
    chain_length = len(can_coord)
    chain_color = np.random.rand(1,3)
    chain_colors.append(chain_color)
    color.append(np.tile(chain_color,(chain_length,1)))
    chain_radius.append(np.array([4*vrad(atom.get_id()) for atom in chain.get_atoms() if atom.get_name() =='CA' or atom.get_name() =='N'])) 
if len(chains)>1:
    color = np.concatenate(color)

W,H = 1200, 800

Plot3D = scene.visuals.create_visual_node(visuals.LinePlotVisual)

canvas = scene.SceneCanvas(keys='interactive', app='pyqt4', bgcolor='white', title='Spheres',
                           size=(W,H), show=True)

view = canvas.central_widget.add_view()
view.camera = ArcballCamera(fov=95, distance=(max(abs(np.concatenate(coordinates))) + 40))
for i in range(len(chains)):
    Plot3D(chain_coords[i], marker_size=chain_radius[i], width=10.0, color=chain_colors[i], edge_color='b', symbol='o', face_color=chain_colors[i], parent=view.scene)

canvas.app.run()
示例#8
0
def plot_sphere_shell_vispy(func,
                            rows=100,
                            cols=100,
                            radius=1.,
                            opacity=1.,
                            translation=(0., 0., 0.),
                            method='latitude',
                            edge_color=None,
                            cmap='hsv',
                            smooth=0,
                            cutoff=0.4,
                            cutoff_max=0.8,
                            transparent_side=True,
                            **kwargs):
    '''func must be a function of sphere angles theta, phi'''

    from vispy.scene import Sphere, ArcballCamera

    s = Sphere(rows=rows,
               cols=cols,
               method=method,
               edge_color=edge_color,
               radius=radius)
    mesh = s.mesh
    md = mesh._meshdata
    vertices = md.get_vertices()
    md.set_vertices(vertices + n.array(translation))

    values = n.zeros(len(vertices))
    opacities = n.ones(len(vertices))

    print('pre')
    for i, vertex in enumerate(vertices):
        if i % 10 == 0:
            vprint('\ri = {} / {}'.format(i, len(vertices)), newline=False)
        vertex = vertex / n.sqrt(n.sum(vertex * vertex))
        theta = n.arccos(vertex[2])
        phi = n.arctan2(vertex[1], vertex[0])

        if n.isnan(theta):
            theta = 0.0
        values[i] = func(theta, phi)
        distance = vertex[1] - cutoff
        distance_frac = distance / (cutoff_max - cutoff)
        opacity = max(0, 1. - distance_frac)
        opacities[i] = 1. if vertex[1] < cutoff else opacity
    vprint()

    colours = n.zeros((len(values), 4))
    max_val = n.max(values)
    min_val = n.min(values)
    unique_values = n.unique(colours)
    # max_val += (1. + 1./len(unique_values))*(max_val - min_val)
    diff = (max_val - min_val)

    import matplotlib.pyplot as plt
    cm = plt.get_cmap(cmap)
    for i in range(len(colours)):
        colours[i] = cm(((values[i] - min_val) / diff))

    colours[:, -1] = opacity

    faces = md.get_faces()
    for si in range(smooth):
        new_colours = [[n.array(row) for _ in range(3)] for row in colours]
        for i, face in enumerate(faces):
            new_colours[face[0]].append(colours[face[1]])
            new_colours[face[0]].append(colours[face[2]])
            new_colours[face[1]].append(colours[face[0]])
            new_colours[face[1]].append(colours[face[2]])
            new_colours[face[2]].append(colours[face[0]])
            new_colours[face[2]].append(colours[face[1]])

        new_colours = n.array([n.average(cs, axis=0) for cs in new_colours])

        colours = new_colours

    colours = [(c[0], c[1], c[2], o) for c, o in zip(colours, opacities)]

    md.set_vertex_colors(colours)

    ensure_vispy_canvas()
    vispy_canvas.view.camera = ArcballCamera(fov=30)
    vispy_canvas.view.add(s)
    vispy_canvas.show()

    return colours