marcomusy / vedo

A python module for scientific analysis of 3D data based on VTK and Numpy
https://vedo.embl.es
MIT License
2.03k stars 264 forks source link

Animating vedo.shapes.Spheres #502

Open jonaslindemann opened 2 years ago

jonaslindemann commented 2 years ago

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?

marcomusy commented 2 years ago

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.

jonaslindemann commented 2 years ago

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 commented 2 years ago

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

marcomusy commented 2 years ago

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.

jonaslindemann commented 2 years ago

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

marcomusy commented 2 years ago

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.

jonaslindemann commented 2 years ago

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

marcomusy commented 2 years ago

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!

jonaslindemann commented 2 years ago

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