让3D Plot类型的图片旋转起来...(Mathematica)
来源:互联网 发布:小啰啰崩没哏网络直播 编辑:程序博客网 时间:2024/05/16 18:12
比葫芦画瓢一个
http://mathematica.stackexchange.com/questions/3759/autorotating-3d-plots
Autorotating 3D plots
These days, I have to produce quite a few 3D surface plots (mostly functions of the type 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 thexy 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?
Note that ViewPoint
is given in specially scaled coordinates which depend on amongst things the size of the bounding box. To get better control over the positioning of the camera you could use ViewVector
instead, which is given in terms of the coordinates of the plot. You could for example do something like this:
rotateMeHarder1[g_, vertical_, viewpoint0_, center_List: {0, 0, 0}, nframes_Integer: 15, opts : OptionsPattern[]] := Module[{grlist}, grlist = Table[Show[g, ViewVertical -> vertical, ViewVector -> {RotationMatrix[2 Pi/nframes i, vertical].(viewpoint0 - center) + center, center}, SphericalRegion -> True, opts], {i, 0, nframes - 1}]]
where g
is the Graphics3D
object, vertical
is the vertical axis, viewpoint0
is the starting position of the camera, center
is the center of the graph, nframes
is the number of frames, and opts
are additional options of the graph.
Usage
To create an animation of the plot p
in the original question rotating around the axis anchored at {10, 0, 0}
and direction {0, 1, 0}
, and initial camera position {30, 30, 30}
you could do something like
p = SphericalPlot3D[10 + 5*Re@ SphericalHarmonicY[3, -3, \[Theta], \[Phi]], \[Theta], \[Phi], Boxed -> False, Axes -> True, AxesOrigin -> {0, 0, 0}, AxesStyle -> {{Thick, Red}, {Thick, Green}, {Thick, Blue}}, Ticks -> None, PlotStyle -> Opacity[0.7]];grlist = rotateMeHarder1[p, {0, 1, 0}, {30, 30, 30}, {10, 0, 0}, 20, ViewAngle -> 50 Degree]ListAnimate[grlist]
Note that I'm using ViewAngle
to adjust the size of the graphics relative to the viewing area. By increasing the ViewAngle
you effectively zoom out and vice versa. Another option to change the relative size of the graphics would be to change the distance of the initial camera position to the axes of rotation.
You can use Export
to create an animated .gif or other type of movie, e.g.
Export["movie.gif", grlist];
This solution requires some manual tuning especially in choosing the initial view vector and the view angle. You could for example use Manipulate
to experiment with values for these parameters.
ViewPoint
and ViewVector
, which was still unclear to me… thanks! However, this still requires lots of manual intervention, choosing all those values by hand. Any chance of getting closer to a nicer user experience? – F'x Mar 31 '12 at 13:43- 让3D Plot类型的图片旋转起来...(Mathematica)
- 让你的浏览器3D起来
- 旋转木马----图片跑起来的效果
- css3的3D图片旋转
- Mathematica数据处理(5)--Plot
- 让图片抖动起来
- 让图片动起来
- plot 3D
- 如何实现图片的3D旋转,而且是不停旋转?
- CSS3让登陆面板旋转起来
- jQuery 实现 图片3D 旋转
- HTML5 3D旋转图片相册
- CSS图片3D旋转效果
- 图片2d旋转
- 图片技巧:3分钟教你用Photoshop CS4,让您的店招靓起来
- iOS开发 CGAffineTransform 让图片旋转, 旋转后获得图片旋转的角度
- 3D旋转的球
- Android的3D旋转
- Reverse Integer
- 是对新生活的期待与向往
- java开发规范
- 关于sed命令的打印总结
- 数组-10. 求整数序列中出现次数最多的数(15)
- 让3D Plot类型的图片旋转起来...(Mathematica)
- eclipse常用快捷键
- How to create this array in Ruby?
- java_ssh_架包缺失异常总结
- Java synchronized详解
- 故乡的河
- java中的lang类型
- JD 1172:哈夫曼树
- NGUI小地图制作教程 1
Graphics`Animation`
package had theSpinShow[]
function... – J. M. Sep 28 '12 at 11:57