bwoodsend / vtkplotlib

VTK (C++ 3D graphics library) wrapped into an easy to use 3D equivalent of matplotlib
MIT License
40 stars 6 forks source link

Performance improvement #13

Closed MarcelHeu closed 2 years ago

MarcelHeu commented 2 years ago

Hello,

Is there maybe a way to improve the overall renderer performance for handling lot of actors within a vpl.QtFigure2. Below i attached some images as an example. In the first image you see a model i want to prepare for 3d metal printing.

01

For the preparation i need to attach support structures and i visualised these support structures by plotting each of them with the _vpl.meshplot function. For sure i could concatenate the support structures to one mesh, but i want to have the opportunity to manually delete and add support structures before combining them to one mesh.

02

There are about 2500 actors within the figure and i got the issue that it starts lagging while handling these amount of actors.

03

Best regards, Marcel

bwoodsend commented 2 years ago

I can't offer you a magic bullet but if you're not already doing it, passing (vertices, faces) where vertices contains no duplicates to mesh_plot() (input form 4 from here) instead of the usual stl.Mesh() or a 3D vectors array should reduce the number of vertices roughly 3-fold. If you're not too fussy about pre-render processing speed, something using numpy.unique() will do:

import numpy as np

def uniquify(vectors):
    unique, faces = np.unique(vectors.reshape((-1, 3)), axis=0, return_inverse=True)
    return unique, faces.reshape((-1, 3))

Which you'd then apply to the usual bunny example:

import vtkplotlib as vpl
from stl.mesh import Mesh

mesh = Mesh.from_file(vpl.data.get_rabbit_stl())
vpl.mesh_plot(uniquify(mesh.vectors))
vpl.show()

That's the only thing I can suggest short of dropping the resolution of your support structure meshes.

bwoodsend commented 2 years ago

i want to prepare for 3d metal printing.

Wow, I had no idea that that was a thing.

MarcelHeu commented 2 years ago

Thanks for your answer and for the hint! I have tried it, but unfortunately it has no big effect on the performance. I got a overall performance improvement, if i turn off several display details, like the lighting, edgevisibility, shading, etc. But then it looks a little bit strange.

bwoodsend commented 2 years ago

Can you get away with reducing those red supports to triangular prisms or cuboids? Or even as just lines (i.e. use vpl.plot()) - it'll be hideous but more renderer friendly? Other than that, I'm out of suggestions.

MarcelHeu commented 2 years ago

The resolution of each support is quite low. Each support structure is a cylinder combined with a cone with 8 corners each. I dont want to reduce it even more or change it for example to a line because of optical reasons. Maybe for these much actors i just need to buy better hardware haha.

bwoodsend commented 2 years ago

Maybe for these much actors i just need to buy better hardware haha.

I think you might. I'm slightly surprised that 2500 actors is causing significant slow down. For me, if I run vpl.scatter(np.random.uniform(-100, 100, (2500, 3))) (so 2500 meshes with 50 points and 96 triangles each), the frame rate is noticeably slower than it is normally but not nearly slow enough to be cumbersome.

bwoodsend commented 2 years ago

Ohh, hang on. When you say you're using QtFigure2() are you using QtFigure.add_cursor_tracker() (possibly using indirectly via add_all())? I notice that that makes it run much slower.

MarcelHeu commented 2 years ago

You made my day! I was using the cursor tracker and the performance is much better if i turn it off!!!