vedo icon indicating copy to clipboard operation
vedo copied to clipboard

Animating vedo.shapes.Spheres

Open jonaslindemann opened this issue 2 years ago • 9 comments

I am trying to visualise a particle system using the vedo.shapes.Spheres class. This is the main loop:

    def run(self):
        print("Run simulation...")

        reset_cam = True

        while True:
            #particle_driver.collision_check()
            
            particle_driver.boundary_check()
            particle_driver.update()
            particle_driver.get_positions(self.positions)
            
            #self.points.points(self.positions)
            self.spheres.points(self.positions)

            plt.show(resetcam=reset_cam, axes=1)
            if reset_cam:
                reset_cam = False
            if plt.escaped:
                break  # if ESC is hit during the loop

Running this will display a single set of points and then crash without an exception.

particle_driver is a fortran extension module. Using vedo.mesh.Points class works.

It also works when you recreate a Spheres instance every loop iteration, but that is really slow...

Any ideas?

jonaslindemann avatar Oct 31 '21 12:10 jonaslindemann

Uhm... does this work for you?

import vedo
import numpy as np

class RunnerTest:

    def __init__(self):
        self.positions = np.random.rand(100, 3)
        self.plt = vedo.Plotter(axes=1, interactive=False)
        self.points = vedo.Points(self.positions)
        self.text = vedo.Text2D(c='r4')
        self.plt += [self.points, self.text]

    def run(self):
        print("Run simulation...")

        reset_cam = True

        while True:
            self.positions *= 0.99

            self.points.points(self.positions)
            self.text.text(f'cm={self.points.centerOfMass()}')

            self.plt.show(resetcam=reset_cam)
            if reset_cam:
                reset_cam = False
            if self.plt.escaped:
                break  # if ESC is hit during the loop


rt = RunnerTest()
rt.run()

Screenshot from 2021-11-01 12-56-32

You may also use Sphere instead of Spheres, so you don't need to recreate the object in the loop.

marcomusy avatar Nov 01 '21 11:11 marcomusy

Thanks for your quick reply. It almost worked. After removing the text object it works:

import vedo
import numpy as np

class RunnerTest:

    def __init__(self):
        self.positions = np.random.rand(100, 3)
        self.plt = vedo.Plotter(axes=1, interactive=False)
        self.points = vedo.Points(self.positions)
        #self.text = vedo.Text2D(c='r4')
        self.plt += [self.points]

    def run(self):
        print("Run simulation...")

        reset_cam = True

        while True:
            self.positions *= 0.99

            self.points.points(self.positions)
            #self.text.text(f'cm={self.points.centerOfMass()}')

            self.plt.show(resetcam=reset_cam)
            if reset_cam:
                reset_cam = False
            if self.plt.escaped:
                break  # if ESC is hit during the loop


rt = RunnerTest()
rt.run()

I have an example that uses a list of spheres that are updated manually, but then I have to use a python loop which slows down the animation significantly. I could make my own example work with Points. I would like the self.spheres.points(self.positions) to work ;)

Jonas

jonaslindemann avatar Nov 01 '21 13:11 jonaslindemann

BTW: Replacing Points with Spheres also crashes the above code.

jonaslindemann avatar Nov 01 '21 13:11 jonaslindemann

Uhm.. what happens to the Text2D? Is it crashing?

I would like the self.spheres.points(self.positions) to work ;)

You can use Sphere instead of Spheres, so you don't need to recreate the object in the loop. You'll then update their position with a loop calling spheres[i].pos(self.positions[i] (this will be fast).

BTW: Replacing Points with Spheres also crashes the above code.

Probably because you're trying to set the points (mesh vertices!!) of the Spheres object which is a single Mesh.

marcomusy avatar Nov 01 '21 13:11 marcomusy

Uhm.. what happens to the Text2D? Is it crashing?

Yes. Black window appears and then disappears. No drawing from what I can see.

I would like the self.spheres.points(self.positions) to work ;)

You can use Sphere instead of Spheres, so you don't need to recreate the object in the loop. You'll then update their position with a loop calling spheres[i].pos(self.positions[i] (this will be fast).

Ok, That was in principle what I did. I will try it again.

BTW: Replacing Points with Spheres also crashes the above code.

Probably because you're trying to set the points (mesh vertices!!) of the Spheres object which is a single Mesh.

Ok, I was a bit confused by the API that looked a lot like the Points API. So the spheres in Spheres are a single mesh?

Jonas

jonaslindemann avatar Nov 01 '21 13:11 jonaslindemann

Yes. Black window appears and then disappears. No drawing from what I can see.

that's pretty weird! But the small standalone script I sent is also failing ?

Ok, I was a bit confused by the API that looked a lot like the Points API. So the spheres in Spheres are a single mesh?

Yes. And - you are right - the API similarity can be misleading... I need to clarify it in the docs.

marcomusy avatar Nov 01 '21 13:11 marcomusy

Yes. Black window appears and then disappears. No drawing from what I can see.

that's pretty weird! But the small standalone script I sent is also failing ? Yes

Ok, I was a bit confused by the API that looked a lot like the Points API. So the spheres in Spheres are a single mesh?

Yes. And - you are right - the API similarity can be misleading... I need to clarify it in the docs.

Thanks, looks like a very impressive library. Evaluating right now for a project assignment in a Scientific Programming course.

Jonas

jonaslindemann avatar Nov 01 '21 13:11 jonaslindemann

the small standalone script I sent is also failing ? Yes

can you do: vedo --info

to know your system specs, I will try reproduce the problem if possible. Thanks for reporting!

marcomusy avatar Nov 01 '21 13:11 marcomusy

Perhaps a bug, but on Windows you can't type vedo in the command line. For all the other scripts in this folder there are .exe equivalents. I had to do:

python vedo --info in the Scripts directory 
_________________________________________________________________
vedo version      : 2021.0.6   https://vedo.embl.es
vtk version       : 9.0.1
python version    : 3.7.11 (default, Jul 27 2021, 09:42:29) [MSC v.1916 64 bit (AMD64)]
python interpreter: C:\Anaconda3\envs\sciprog-project\python.exe
vedo installation : C:\Anaconda3\envs\sciprog-project\lib\site-packages\vedo
system            : Windows 10 nt AMD64

jonaslindemann avatar Nov 01 '21 13:11 jonaslindemann