These days, I have to produce quite a few 3D surface plots (mostly functions of the type ρ(θ,ϕ) plotted with SphericalPlot3D
). To exchange these results colleagues who don’t use Mathematica, or to include them in presentations, I need to export these plots as movies showing the surface rotating. What I’d love to do is to be able to choose one graph, and just export it as autorotating around the current vertical axis (i.e. the axis contain in the plane of the screen, and directed upwards).
This not being possible, I'm trying to do the next best thing: a procedure that takes as an argument the produced 3D graphics (say, the output of SphericalPlot3D
) and makes it automatically rotate. Here’s what I’ve been able to do with Animate
, but I could follow the same logic and export it directly instead.
rotateMeHarder[g_] := Module[{range},
range = Max /@ (Abs@PlotRange /. AbsoluteOptions[g, PlotRange]);
Animate[
Show[
Graphics3D[{Red, Thick, Line[{{-1.2*range[[1]], 0, 0}, {1.2*range[[1]], 0, 0}}]}],
Graphics3D[{Green, Thick, Line[{{0, -1.2*range[[2]], 0}, {0, 1.2*range[[2]], 0}}]}],
Graphics3D[{Blue, Thick, Line[{{0, 0, -1.2*range[[3]]}, {0, 0, 1.2*range[[3]]}}]}],
g,
ViewPoint -> RotationTransform[\[Theta], {0, 0, 1}][{0, 2*Max@range, 0.2*Max@range}],
SphericalRegion -> True, Boxed -> False],
{\[Theta], \[Pi]/20, 2 \[Pi] + \[Pi]/20, \[Pi]/20}]
];
p = SphericalPlot3D[
10 + 5*Re@SphericalHarmonicY[3, -3, \[Theta], \[Phi]], \[Theta], \[Phi],
Boxed -> False, Axes -> None, PlotStyle -> Opacity[0.7]];
rotateMeHarder[p]
What I like about this:
- It does generate a nicely rotating object in simple cases.
- It draws axis lines on top of my graph, but that's just because it looks great on my pictures, it's not a requirement for the question…
What I don’t like, or am unsure about:
- It doesn’t start with the current orientation of the graph, but works with a fixed rotation vector (along the 3D z axis) and a fixed starting position (slightly above the xy plane). If I understood this answercorrectly, they is not way of retrieving the current viewpoint for a given interactive 3D graph. So bad!.
- It doesn’t seem very robust. In particular, it only works for 3D plots centered around the origin, which extend about the same length in each axis direction (i.e. does not deal with very anisotropic surfaces).
- It leaves a lot of white padding around the graphics.
So, the question is: what do you think of this code? How could I make it more general, more robust, or in any way closer to my initial goal of a one-size-fits-all exporting procedure for 3D graphics?
Graphics`Animation`
package had theSpinShow[]
function... – J. M. Sep 28 '12 at 11:57