Search code examples
python3dmayavivispy

Plotting 3d Implicit equation with vispy


Due to server GPU limitations we cannot use K to render implicit equations on the VPS, here's an example code we use to produce 3D models from equations in mayavi:

import numpy as np
from mayavi import mlab

mlab.clf()
x, y, z = np.mgrid[-2.5:2.5:.125, -2.5:2.5:.125, -2.5:2.5:.125]
a,b,c = 0.0,-5.0,11.8
values = x**4+y**4+z**4+a*(x**2+y**2+z**2)**2+b*(x**2+y**2+z**2)+c
mlab.contour3d(x, y, z, values, contours=[0], color=(.1,1,.5))

mlab.savefig("shape.obj")

This outputs this shape in 3D

3d equation rendered by mayavi

My question is, how can I accomplish the same thing with vispy or another python module (if there exists)

We do not have graphics card on VPS, we're using python 3.5.1 (anaconda) on windows server 2008 (not possible to use linux) and we have replaced opengl32.dll with mesa in system32 (GL version 3.0 mesa 10.2.8)(VTK throws error GL Version 2.1 with the gpu_shader4 extension is not supported...)


Solution

  • Here's the answer to my question, this is how it's done with vispy:

    import numpy as np
    
    from vispy import io
    from vispy.geometry.isosurface import isosurface
    
    x, y, z = np.mgrid[-2.5:2.5:.125, -2.5:2.5:.125, -2.5:2.5:.125]
    a,b,c = 0.0,-5.0,11.8
    data = x**4+y**4+z**4+a*(x**2+y**2+z**2)**2+b*(x**2+y**2+z**2)+c
    vertices, faces = isosurface(data, level=1/10000000.)
    io.write_mesh('shape.obj', vertices, faces , None, None, overwrite=True)
    

    Exports a wavefront obj file exactly like the shape above. This is much faster than using mayavi.