14
votes

In my day job as a PhD student, I do geological modeling. In my spare time (mainly for fun), I am learning Python and trying to write a simple program to view 3D geocellular models.

geological model http://img710.imageshack.us/img710/6503/sgems.png geo model2

The geocellular model is just a 3D grid where every grid cell has some value (as shown in the right figure). So, I would want my viewer to be able to display a 3D grid model like the picture on the right side. As well, I would like it to be able to display cross sections through the model in the x, y and z directions (this is shown in the left figure).

I would also want the models to be able to rotate around all three axes and zoom in and out.

I've done some preliminary investigation (mainly here) and it seems like VisVis and VTK are two potential options. I am trying to use wxPython for the main GUI and it looks like both options will work with wxPython as far as I can tell.

Questions:

  1. Am I right when I say that I think VisVis and VTK would work for what I want? Is one preferable to the other?

  2. Which of these two options would be the easiest to implement?

  3. Is there another option that I also should consider?

Keep in mind that I'm newish to Python and very new to wxPython.

5

5 Answers

13
votes

What you are looking for is called voxel visualization, voxel grid or such. I would seriously consider MayaVi (never used it, but I keep eye on it), it seems to have something very close here.

Paraview, built atop VTK just like MayaVi, might be a good option, too.

I think going straight to VTK for visualization is difficult, it is too low-level and will probably make you just frustrated. That said, you will want to save your data in as VTK datasets for opening in MayaVi/Paraview; it is not difficult, you just have to pick the right structure (vtkGrid, vtkUnstructedGrid, ...).

10
votes

In my case, I chose to use directly the VTK bindings for Python. To be honest I found it simpler to get going with VTK than Mayavi, partly because the documentation is better (many many examples!). It felt like Mayavi was adding another layer of complexity on my way to get the job done. But tom10 is right. After you've started, using Mayavi may be easier.

Apart from that, Mayavi offers a library called TVTK which is a more pythonic version of the VTK bindings but in the end I chose plain VTK in order to minimize the number of dependencies. But you should check it out. Perhaps it will be just what you are searching for.

At the beginning I found very helpful this tutorial. It is not about Python, it is about tcl, but translating the examples is trivial and it helps you understand the way vtk works.

Also, in order to get you started, you can check the examples at the VTK Wiki. If they are not enough, you can always check the C++ examples and translate them to Python. The translation is not difficult as the names of methods and properties are the same. If you do, you are encouraged to add the examples at the wiki. There are even more examples in the source.

While you are learning VTK, you will (re)discover that Ipython is awesome! Having the whole VTK namespace at your fingertips helps enormously.

In case you need more specific help, the vtk-users mailing list is quite active. Lastly there are books about VTK, and some of them are free!They are not about Python though.

I haven't tried wxPython and VTK together, but that is because I prefer PyQt4 over wxPython. AFAIK there are no problems with the integration of VTK with either library. In any case, before spending time writing a GUI, check out thoroughly ParaView. It probably already does what you want, and if it doesn't, it is python scriptable too! (I've never checked it though).

7
votes

Just as a simple example of using Mayavi's mlab interface to do this (With some geologic data, even!):

from mayavi import mlab
import geoprobe

vol = geoprobe.volume('Volumes/example.vol')
data = vol.load()  #"data" here is just a 3D numpy array of uint8's

fig = mlab.figure(bgcolor=(1., 1., 1.), fgcolor=(0., 0., 0.), size=(800,800))
grid = mlab.pipeline.scalar_field(data)

# Have things display in kilometers with no vertical exxageration
# Each voxel actually represents a 12.5 by 18.5 by 5 meter volume.
grid.spacing = [vol.dxW / 1000, vol.dyW / 1000, vol.dz / 1000]

# Now, let's display a few cut planes. These are interactive, and are set up to 
# be dragged around through the volume. If you'd prefer non-interactive cut 
# planes, have a look at mlab.pipeline.scalar_cut_plane instead.
orientations = ['x', 'x', 'y', 'z']
starting_positions = [vol.nx//4, 3*vol.nx//4, vol.ny//2, vol.nz]
for orientation, start_pos in zip(orientations, starting_positions):
    plane = mlab.pipeline.image_plane_widget(grid, colormap='gray',
            plane_orientation='%s_axes' % orientation, slice_index=start_pos)

    # High values should be black, low values should be white...
    plane.module_manager.scalar_lut_manager.reverse_lut = True

mlab.show()

enter image description here (The data and data-format-handling code (the geoprobe module) are available here: http://code.google.com/p/python-geoprobe/ )

While I'd agree that learning VTK is better in the long run, you can get up-and-running quite quickly with Mayavi. The big advantage is not having to jump through hoops to get your data into VTK format. TVTK and Mayavi allow you to directly use numpy arrays.

3
votes

If you want an easier way of getting into the VTK/MayaVi world (see eudoxos' fine answer), look at the mlab API to it. This brings matplotlib-like convenience to basic volume visualizations and I've yet to find the need to dig deeper into the underlying platform.

1
votes

vpython is simpler to use than mayavi, but it has fewer features.

http://vpython.org/contents/bounce_example.html