basic pipeline to display a set of spheres

156 views
Skip to first unread message

nbbb

unread,
Aug 5, 2015, 7:11:47 AM8/5/15
to vispy
Hi,

I'm trying to make an online visualization for a Brownian Dynamics simulation. OpenGL noob. Here is something that is working but I think is probably the wrong way of doing it efficiently.

import os
import sys
import numpy as np

from vispy import app, scene, geometry, color
from vispy.scene import visuals
from vispy.visuals import transforms

canvas = scene.SceneCanvas(show=1, keys='interactive')
vb = scene.widgets.ViewBox(size=canvas.size, parent=canvas.scene)
vb.camera = scene.cameras.TurntableCamera(elevation=30, center=center)

sphere = geometry.create_sphere(9, 5)

def place_mol(crow):
    id_ = crow['id']
    rad = crow['radius']
    center = crow['center']
    placed_sphere = visuals.Mesh(
            color=make_color(id_),
            meshdata=sphere,
            shading='smooth',
            parent=vb.scene)
    placed_sphere.transform = transforms.ChainTransform([
        transforms.STTransform(translate=center),
        transforms.STTransform(scale= rad * np.ones(3)),
        ])

for ii, crow in enumerate(mem.ma):
    if np.isnan(crow['id']): break
    place_mol(crow)
print(ii)


Beware: the snippet is not fully self contained. In this snippet, mem.ma is a 1d structured array with a data type with fields 'id', 'radius' and 'center'. It represents the molecules' species identity, radius and positions. Basically I'm using a python loop over the array to place copies of the sphere mesh in the scene. I am noticing a slight lag for this procedure to finish already for only around 100 molecules.

What is the correct way of doing this?  Do I have to drop to gloo and write shaders? Any hints are appreciated!


nbbb

unread,
Aug 5, 2015, 8:50:48 AM8/5/15
to vispy
Full disclosure:

The mem.ma array is actually a memmapped array which gets updated with new particle positions by the running simulation. So I ultimately want to update the collection of spheres from this array. Either whenever it changes, or in a timed loop. The number of molecules of various types can change as well.

nbbb

unread,
Aug 5, 2015, 9:04:47 AM8/5/15
to vispy
Also, I don't absolutely need Spheres; I would be happy with rendering a point cloud, but the radius of the displayed circle should have a meaningful value. So I figured using visuals.Markers is not the right thing here?

Eric Larson

unread,
Aug 5, 2015, 11:12:30 AM8/5/15
to vi...@googlegroups.com
It might be faster to modify the MarkerVisual to allow the size to be an array of the same length as the number of markers. It's something we want eventually anyway. You up for making a PR?

Eric


On Wed, Aug 5, 2015 at 9:04 AM, nbbb <bck...@gmail.com> wrote:
Also, I don't absolutely need Spheres; I would be happy with rendering a point cloud, but the radius of the displayed circle should have a meaningful value. So I figured using visuals.Markers is not the right thing here?

--
You received this message because you are subscribed to the Google Groups "vispy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vispy+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

nbbb

unread,
Aug 5, 2015, 11:43:04 AM8/5/15
to vispy

It might be faster to modify the MarkerVisual to allow the size to be an array of the same length as the number of markers. It's something we want eventually anyway. You up for making a PR?
 
Hi, thanks for the reply. First, I'm totally new to opengl and vispy, and I don't think I can understand enough of it in a reasonable amount of time to add a PR. Second, it seemed to me that the markers have a size that is defined relative to the size of the view on screen? For me, it's not enough to give the markers different sizes on screen, I need them to have a definite size in the coordinates of the rendered scene, since they represent physical objects of a definite size. Basically, I do want actual spheres, but it would be ok if they were rendered 'flat' not appearing three-dimensional. On second thought, it's probably something that Markers are not designed to do in principle..

nbbb

unread,
Aug 5, 2015, 11:53:03 AM8/5/15
to vispy
I read in another thread that it's possible to combine many individual meshes to one big Mesh object. Is that something I could try? If yes how can I add several Spheres into one Mesh?

The thing that still seems slow/wrong to me is that I am using a python function to iterate over the array though...

Luke Campagnola

unread,
Aug 5, 2015, 6:45:59 PM8/5/15
to Vispy list
I would have a look at examples/demo/gloo/molecular_viewer.py.
This does not have the simplicity of the scenegraph API, but it should not be too difficult to adapt it for your needs. Eventually we would like the Marker visual to cover this use case.

On Wed, Aug 5, 2015 at 11:53 AM, nbbb <bck...@gmail.com> wrote:
I read in another thread that it's possible to combine many individual meshes to one big Mesh object. Is that something I could try? If yes how can I add several Spheres into one Mesh?

The thing that still seems slow/wrong to me is that I am using a python function to iterate over the array though...

--

nbbb

unread,
Aug 9, 2015, 6:05:11 PM8/9/15
to vispy

I would have a look at examples/demo/gloo/molecular_viewer.py.
This does not have the simplicity of the scenegraph API, but it should not be too difficult to adapt it for your needs. Eventually we would like the Marker visual to cover this use case.

Thanks, I'll have a look. With an example it's hopefully doable. As a general comment: I think a good collection of examples is a great way to get non-GL-enthusiast  scientist programmers to use the library. If I see an example and it's almost what I need, I can judge how much time it will take me and I'm more likely to jump in.
Reply all
Reply to author
Forward
0 new messages