11

I have a 3D scene generated with the R rgl package.

  • I can save it in RTL and OBJ format via the rgl functions, but these functions don't support colors.
  • I can save it in WebGL, but then I can't find a WebGL to .u3d converter, nor any way to insert WebGL content in a .pdf file (generated with LaTeX).
  • I can save it in PLY format and then export to .u3d (e.g. using Meshlab), but it gives me the following error:

    Error in if (sum(normals[1:3, it[j, i]] * normal) < 0) normals[, it[j,  : 
    missing value where TRUE/FALSE needed 
    

Which I really don't know how to solve.

Here is an example file to reproduce the problem. To reproduce simply download the file in the working directory, execute R and run:

library(rgl)
load("alps3d.Rdata") #This loads the alps3d variable
plot3d(alps3d)
writePLY("alps3d.ply")

How can I save the 3d scene in a format which can be itegrated in a .pdf using LaTeX?

AF7
  • 3,160
  • 28
  • 63
  • The example file is not available for download anymore from the linked site. Can you upload it again, or do you have another easy way to reproduce the issue with PLY? – WhiteViking Sep 12 '15 at 08:44
  • @WhiteViking thank you for notifying me. I have updated the download link, it should work now. – AF7 Sep 13 '15 at 09:41
  • 2
    http://stackoverflow.com/q/12781129/471093 and http://r.789695.n4.nabble.com/Exporting-an-rgl-graph-td1872712.html might prove useful – baptiste Sep 13 '15 at 09:55
  • 1
    You can avoid the error with `writePLY("alps3d.ply", withNormals = F)`. For this particular model, which seems to have only planar geometry, having no vertex normals is not a big deal. So you end up with a PLY file that has geometry and color information. However... MeshLab does not read any color information from PLY files. So using MeshLab to convert to U3D is not a viable approach either. – WhiteViking Sep 16 '15 at 22:31
  • @WhiteViking Thanks. That seems to work, but as you say Meshlab is less then useful in this case. It seems that 3d-tool can have this option... http://www.3d-tool.com/en_cad-viewer-formats.htm – AF7 Sep 17 '15 at 09:21
  • thx @baptiste, had seen those. Was hoping to get a tip on some other strategy. – tim riffe Sep 19 '15 at 14:53
  • 1
    I used to export rgl object using persp3d and rgl.snapshot, the latter can export a picture. For 3D movie, I used movie3d which has an option to export movies. – Lamothy Apr 12 '16 at 02:42

2 Answers2

3

You should try writeASY(). It writes for Asymptote, which can produce PRC rather than U3D, but may be good enough. I tried your sample scene, and it takes about 5 minutes to load the result in Acrobat Reader, but it eventually loads and works.

writeASY() is a recent addition to rgl; you'll need to get it from the R-Forge or Github copies.

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • 2
    This is useful @user2554330 . If you have seen it, could you please direct us to a resource on how to use the output of writeASY() in a LaTeX document for completeness? (Unless this is more of a LaTeX question now?) The standard asymptote documentation and help pages I've seen don't look to exactly cover what's required and I've had a lot of compilation errors using advice from http://tex.stackexchange.com/questions/111655/simple-way-to-switch-inline-asymptote-figures-on-and-off – Jason Whyte Aug 16 '16 at 13:14
3

You can use rgl.postscript, which allows to export to various formats, including pdf. Well, the result is not terrific, but that should depend on the type of plot.

> x <- y <- seq(-10, 10, length = 20)
> z <- outer(x, y, function(x, y) x^2 + y^2)
> persp3d(x, y, z, col = 'lightblue')
> rgl.postscript("persp3d.pdf", "pdf")

enter image description here

You can also export to tex, allowing to do some manual modifications.

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225