A couple of years ago, Alexey Popkov asked this question: Which ray tracing software is compatible with Graphics3D? It is my opinion, for various reasons, that out of the many ray tracing programs that exist today, POV-Ray is most compatible with Mathematica-generated 3D graphics. During the past few months I've developed and gradually improved a Mathematica and POV-Ray workflow, and I think it would be interesting and useful to describe it here in some detail.

Should any of the users or moderators object to this "question", which of course isn't really a question at all, then I'd be willing to remove it or to post it as an answer to Alexey Popkov's question instead. My main reasons for not doing the latter are, first, that it is a rather long answer, and, secondly, that the matter at hand is slightly different from the one implied in Mr. Popkov's question.

4 Answers
4

I sometimes use Pov-Ray to render quantum mechanical wave function data, and I wrote a very basic package that exports simple Mathematica plots and call povray to render the graphics, and then imports it into the notebook. In this way, I can render better looking graphics without leaving Mathematica. Moreover, since the graphics are rendered outside Mathematica, I can generate movies in parallel without the Mathematica frontend. This is important for me because I usually use the university HPC system to generate simulation data, but rendering graphics in Mathematica in general requires the frontend, which are not available in the command line mode on the HPC.

$\begingroup$I've already seen this work of yours, and I like it very much. In fact, it's what inspired me to try and combine Mathematica & POV-Ray in the first place. However, importing rendered images back into Mathematica isn't something I really need or want (because I want to edit the POV file anyway), so I decided to begin from scratch.$\endgroup$
– Kim FierensJan 31 '15 at 7:42

2

$\begingroup$@KimFierens Thanks :) I made this initially for rendering a movie of wave function evolution, which requires many frames, so that's why I use Mathematica to edit the pov file. Also some of the povray code modifications (like background and light) are hard wired for my need, so I guess it's a good idea to start from scratch for a Povray expert like you. I like your scene :)$\endgroup$
– xslittlegrassJan 31 '15 at 21:16

$\begingroup$@shrx I use the workflow discussed in this answer to render wave function. The wave function is plotted in Mathematica and then rendered in Povray using the "PovrayRender" package. About making the plot of the wave function in Mathematica, I think it depends on the data format, but some examples are here.$\endgroup$
– xslittlegrassApr 20 '15 at 11:50

$\begingroup$Since the bug report on bitbucket is off, I report the bug here. The latest version of the package cannot be imported correctly. The linked version is successfully imported.$\endgroup$
– KatternJun 23 '15 at 12:38

I'm going to describe the workflow with the aid of an example. In this example, my aim is to produce an accurate and beautiful ray traced image of Richmond's minimal surface (the variation with five "petals"). I want to include Mathematica's parameter grid lines and visually emphasize the surface's boundary. See here for a description of Richmond's surface: Wikipedia.

First step

Obtain some parametric equation for the surface and write Mathematica code for it. Since the actual process of deriving the parametric equation for Richmond's surface (via its Weierstrass-Enneper representation) lies outside the scope of this particular Stack Exchange, I will simply provide the code: rich[z_] := {-1/(2 z) - z^11/22, -I/(2 z) + I z^11/22, z^5/5}. Note that I've chosen a surface that doesn't involve any complex branch cuts in evaluating its Weierstrass-Enneper integrals.

Second step

Next draw the surface with Mathematica, without any mesh lines, boundary, etc. For my example, this is accomplished as follows:

I've found the proper parameter domain by trial-and-error, fiddling with the parameter bounds until I obtained an aesthetically pleasing image. Better mathematicians than I will perhaps require less time doing this.

Third Step

Here's where things become interesting! I export the surface plot as a bunch of smooth triangles. A smooth triangle is a triangle which not only specifies its vertices, but also its vertex normals. In order to obtain aesthetically pleasing results, it is absolutely essential to include the vertex normals. Otherwise POV-Ray will seriously mess up things like phong shading, texturing and especially interior textures, so that things will look choppy. (Which is bad unless you want a "low polygon count" look.) I export to a POV-Ray include file, so as not to clutter up my main POV file with several thousand smooth triangle declarations. For this task, I use code adapted from one of the answers to this question: Mathematica's "POV" export format ignores ColorFunction.

Next, I open the include file in a text editor (POV-Ray itself becomes horribly slow when dealing with large files) and enclose the smooth triangle statements in a #declare richmondsurface = mesh{ [here go all the triangles] } declaration.

Fourth step

I draw just the parameter mesh lines without the surface itself being visible. This is accomplished by typing and executing:

Following that, I export the mesh frame, this time as a POV file: Export["richmondframe.pov", richmondframe].

Fifth step

I clean up the resulting POV file (which contains a bunch of cylinder declarations) in my text editor. First, I remove all camera and lighting info. This is easy, because the info stands at the front of the POV file. Next, I replace (via the text editor's find-and-replace function) all pigment declarations for the cylinders by an empty expression. Additionally, I replace the cylinder's radius with the variable meshthickness, which I will later declare in the main POV file. Then I enclose the cylinders in a statement #declare richmondframe = union{ [bunch of cylinders here] }. Finally, I rename the POV file so that it ends in ".inc" instead of ".pov". This way I make another include file out of it.

Sixth step

I draw the surface boundary and essentially repeat step five. Of course I use a different variable, meshthickness2 (which is going to be larger than the first meshthickness), for the cylinder's radius.

Create and edit the main POV file according to taste. POV-Ray's many procedures, options, and codes are of course entirely outside the scope of this Stack Exchange. Let me limit myself to reminding the readers that in order to include the objects created in Mathematica, you type things like:

Eighth step
When everything is as you imagined it, render the final image. Then simply lean back and enjoy the fruits of your hard work.

Epilogue

I would like to create a program or script that will perform most of the steps outlined here automatically. I'm especially interested in making something suited for the creation of animations. Any ideas, comments, or criticisms will be warmly appreciated.

$\begingroup$It's been a while since I've seriously worked with POV-Ray, but IIRC, you could skip the step where you edit richmond.inc by including the wrapper in your main code, as in #declare richmondsurface = mesh{ #include "richmond.inc" }.$\endgroup$
– Ilmari KaronenFeb 1 '15 at 13:44

In Step 4, define richmondframe, but don't export a *.pov file because you edit away almost all the info in the *.pov in Step 5 anyway. Instead, eliminate all the editing by generating richmondframe.inc entirely within Mathematica. For example,

The addition of the small triple to #[[2]] is to avoid degenerate cylinders (which we all want to do). The cylinder radius has the meshthickness variable ready for a declaration in the *.pov file as in the following.

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.