简体   繁体   English

以 svg 或 pdf 等无损格式保存 rgl 3D 散点图

[英]Save a rgl 3D scatterplot in a lossless format like svg or pdf

What I want to achieve is, to show a 3D graph in rgl, rotate it into the view I would like to show and then save it to a file.我想要实现的是,在 rgl 中显示 3D 图形,将其旋转到我想要显示的视图中,然后将其保存到文件中。 I know that I can do this with the rgl.snapshot function like this:我知道我可以使用rgl.snapshot函数来做到这一点,如下所示:

library(rgl)

x <- runif(20)
y <- runif(20)
z <- runif(20)
plot3d(x, y, z)
rgl.snapshot("rgl.snapshot.png")

The problem is, that rgl.snapshot produces a file in the screen resolution, hence not high resolution enough for print.问题是, rgl.snapshot以屏幕分辨率生成文件,因此分辨率不足以打印。 I have no way to influence the resolution the file get`s saved in. In general even better would be if I would be able to save the file in a vector format like pdf or svg.我无法影响保存文件的分辨率。总的来说,如果我能够将文件保存为 pdf 或 svg 等矢量格式,那就更好了。

My Idea was to save the rotation of the current view and use it with another function that produces a non-interactive 3d scatterplot like scatter3D from the plot3D package.我的想法是保存当前视图的旋转并将其与另一个函数一起使用,该函数从plot3D包中生成非交互式 3d 散点图,如scatter3D To save the rotation matrix I did the following:为了保存旋转矩阵,我执行了以下操作:

rotationMatrix <- rgl.projection()

You can also do it like this:你也可以这样做:

rotationMatrix <- par3d()$modelMatrix

The rotation matrix looks like this:旋转矩阵如下所示:

$model
          [,1]       [,2]      [,3]       [,4]
[1,] 0.9584099  0.0000000 0.0000000 -0.4726846
[2,] 0.0000000  0.3436644 0.9792327 -0.6819317
[3,] 0.0000000 -0.9442102 0.3564116 -3.6946754
[4,] 0.0000000  0.0000000 0.0000000  1.0000000

$proj
         [,1]     [,2]      [,3]      [,4]
[1,] 3.732051 0.000000  0.000000   0.00000
[2,] 0.000000 3.732051  0.000000   0.00000
[3,] 0.000000 0.000000 -3.863703 -14.36357
[4,] 0.000000 0.000000 -1.000000   0.00000

$view
     x      y  width height 
     0      0    256    256 

Now my question is how I get from this rotation matrix to the arguments phi and theta which are used by the scatter3D function.现在我的问题是我如何从这个旋转矩阵获得scatter3D函数使用的参数phitheta

library(plot3D)

# phi = ?
# theta = ?
pdf("scatter3D.pdf")
scatter3D(x, y, z, pch=20, phi = 20, theta =30, col="black")
dev.off()

I know there is math to extract a rotation angle from a rotation matrix.我知道有数学可以从旋转矩阵中提取旋转角度。 I don't really get how to apply it in my case.我真的不知道如何在我的情况下应用它。 Especially since the matrix has 4 rows and columns.特别是因为矩阵有 4 行和 4 列。 I would expect 3 of each... Next problem is that scatter3D uses only two rotation axes (theta gives the azimuthal direction and phi the colatitude), so I would have to convert from a 3-axis rotation to the same rotation resulting from a two-axis rotation.我希望每个都有 3 个......下一个问题是scatter3D只使用两个旋转轴(theta 给出方位角方向,phi 给出坐标),所以我必须从 3 轴旋转转换为由 a 产生的相同旋转两轴旋转​​。 I think the rotation axis of phi is defined by the rotation of theta.我认为 phi 的旋转轴是由 theta 的旋转定义的。

If there is another way to save a rgl snapshot in a lossless format I would be happy to learn about it!如果有另一种方法可以以无损格式保存 rgl 快照,我很乐意了解它!

The latest (R-forge only; see How do I install the latest version of rgl? for how to get it) version of rgl has a function rglToBase() that returns the phi and theta values you'd need. rgl 的最新版本(仅限 R- forge ;请参阅如何安装最新版本的 rgl?了解如何获取它)版本的 rgl 有一个函数 rglToBase() 可返回您需要的 phi 和 theta 值。 There's also rgl.postscript() as mentioned in my Apr 24 comment that saves in a lossless format (but can't save everything).还有我在 4 月 24 日的评论中提到的 rgl.postscript() 以无损格式保存(但不能保存所有内容)。

Edited to add: A very new addition is the writeASY() function.编辑添加:一个非常新的添加是writeASY()函数。 This writes out Asymptote source code to draw the image in various formats, mainly intended for LaTeX documents.这写出 Asymptote 源代码来绘制各种格式的图像,主要用于 LaTeX 文档。 See http://asymptote.sourceforge.net .请参阅http://asymptote.sourceforge.net This is still a little limited (subscenes aren't supported, surface lighting isn't perfect, etc.) but it's getting there.这仍然有一些限制(不支持子场景,表面照明不完美等)但它正在实现。 Suggestions would be welcome.欢迎提出建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM