0

I'm trying to draw a sphere inside a cone with Python, rotate it to the desired view, and export it as a vector graphics file (svg or pdf for example).

I can easily save a matplotlib figure as an svg/pdf, but unfortunately matplotlib cannot handle occlusions correctly, i.e., either the entire cone occludes the sphere or the other way around (even though the sphere should occlude the back of the cone, while the front of the cone should occlude the sphere) (see question regarding this behavior).

I managed to draw it with Plotly and get the correct occlusions but I couldn't export the view as a real vector graphics file, because, as written in the Plotly documentation:

figures containing WebGL traces (i.e. of type scattergl, heatmapgl, contourgl, scatter3d, surface, mesh3d, scatterpolargl, cone, streamtube, splom, or parcoords) that are exported in a vector format will include encapsulated rasters

Is there a library that handles 3D occlusions correctly and allows exporting the current view of the scene to a vectorized SVG/PDF file? If there is a way to implement thick outlines in addition to the surface color (that change with rotating the view), it would be even better.

Thank you!

Beatdown
  • 187
  • 2
  • 7
  • 20
  • Which vector format supports three-d views? Certainly neither svg nor pdf. To save a 3d scene you need to either look at it in a 3D renderer or flatten it to a raster. – Jody Klymak Jun 26 '22 at 10:35
  • I don't need to retain 3d capabilities. I'm fine with a 2d "projection" of the scene without the option to rotate it after I save the file, as long as it's vectorized. I think that this is how matplotlib works, but it can't handle occlusions well. – Yaron Veksler Jun 27 '22 at 13:26
  • **Is there a library that handles 3D occlusions...** should be closed as an off-topic question; it is Seeking recommendations for books, tools, software libraries, and more. – Trenton McKinney May 04 '23 at 21:54

1 Answers1

1

I would suggest to use the mayavi library, which has a lot more powerful graphic engine and definitely suitable for 3D. Please read the documentation carefully to fully understand the library capabilities (e.g. export options). Below I paste the code for a cone and sphere intersecting, with lights and export to pdf format:

import numpy as np
from colour import Color
from mayavi import mlab

fig = mlab.figure("figure_title")
mlab.clf()
fig.scene.background = Color("white").rgb
fig.scene.foreground = Color("black").rgb

x, y, z = np.ogrid[-4:4:100j, -4:4:100j, -4:4:100j]
r = 0.5
F = x**2 + y**2 - r*z**2

mlab.contour3d(F, contours = [0], extent=[-3,3,-3,3,-3,3], opacity=1.0)
mlab.points3d(
    1.0, 0.0, 0.0, color=Color("red").rgb, resolution=32, scale_factor=2, figure=fig
)
mlab.savefig("test.pdf")
mlab.show()

Another very cool library is the pyvista library. enter image description here

blunova
  • 2,122
  • 3
  • 9
  • 21
  • Thanks! I've been toying with the mayavi library today and finally made it work and show the scene. However I can't find a way to save a vectorized file. Even when saving as pdf, the result is rasterized (pixelated). When using savefig I get a window of GL2PSExporter with many options, and even though "Write3D props as raster image" is turned off, it seems to turn it into a raster image anyway... It says that this option has no effect when using opengl2 as a backend as all 3d props are rasterized in opengl, but I'm not sure how to change it – Yaron Veksler Jun 25 '22 at 17:08
  • Unfortunately, I tried a little bit but as you said all 3d props are rasterized in opengl so I do not know either how to create a fully vectorized image, I am sorry. I hope the rest of the answer is useful. – blunova Jun 26 '22 at 10:34