Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render a mayavi scene with a large pipeline faster

Tags:

python

mayavi

I am using mayavi.mlab to display 3D data extracted from images. The data is as follows:

  1. 3D camera parameters as 3 lines in the x, y, x direction around the camera center, usually for about 20 cameras using mlab.plot3d().
  2. 3D coloured points in space for about 4000 points using mlab.points3d().

For (1) I have a function to draw each line for each camera seperately. If I am correct, all these lines are added to the mayavi pipeline for the current scene. Upon mlab.show() the scene takes about 10 seconds to render all these lines.

For (2) I couldn't find a way to plot all the points at once with each point a different color, so at the moment I iterate with mlab.points3d(x,y,z, color = color). I have newer waited for this routine to finish as it takes to long. If I plot all the points at once with the same color, it takes about 2 seconds.

I already tried to start my script with fig.scene.disable_render = True and resetting fig.scene.disable_render = False before displaying the scene with mlab.show().

How can I display my data with mayavi within a reasonable waiting time?

like image 950
Simon Streicher Avatar asked May 03 '13 17:05

Simon Streicher


1 Answers

The general principle is that vtk objects have a lot of overhead, and so you for rendering performance you want to pack as many things into one object as possible. When you call mlab convenience functions like points3d it creates a new vtk object to handle that data. Thus iterating and creating thousands of single points as vtk objects is a very bad idea.

The trick of temporarily disabling the rendering as in that other question -- the "right" way to do it is to have one VTK object that holds all of the different points.

To set the different points as different colors, give scalar values to the vtk object.

x,y,z=np.random.random((3,100)) some_data=mlab.points3d(x,y,z,colormap='cool') some_data.mlab_source.dataset.point_data.scalars=np.random.random((100,))

This only works if you can adequately represent the color values you need in a colormap. This is easy if you need a small finite number of colors or a small finite number of simple colormaps, but very difficult if you need completely arbitrary colors.

like image 133
aestrivex Avatar answered Oct 11 '22 06:10

aestrivex