title: Save a rgl 3D scatterplot in a lossless format like svg or pdf
body: <p>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. I know that I can do this with the <code>rgl.snapshot</code> function like this:</p>
  
  <pre><code>library(rgl)
  
  x &lt;- runif(20)
  y &lt;- runif(20)
  z &lt;- runif(20)
  plot3d(x, y, z)
  rgl.snapshot("rgl.snapshot.png")
  </code></pre>
  
  <p>The problem is, that <code>rgl.snapshot</code> produces a file in the screen resolution, hence not high resolution enough for print. 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.</p>
  
  <p>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 <code>scatter3D</code> from the <code>plot3D</code> package. To save the rotation matrix I did the following: </p>
  
  <pre><code>rotationMatrix &lt;- rgl.projection()
  </code></pre>
  
  <p>You can also do it like this:</p>
  
  <pre><code>rotationMatrix &lt;- par3d()$modelMatrix
  </code></pre>
  
  <p>The rotation matrix looks like this:</p>
  
  <pre><code>$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 
  </code></pre>
  
  <p>Now my question is how I get from this rotation matrix to the arguments <code>phi</code> and <code>theta</code> which are used by the <code>scatter3D</code> function.</p>
  
  <pre><code>library(plot3D)
  
  # phi = ?
  # theta = ?
  pdf("scatter3D.pdf")
  scatter3D(x, y, z, pch=20, phi = 20, theta =30, col="black")
  dev.off()
  </code></pre>
  
  <p>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. I would expect 3 of each... Next problem is that <code>scatter3D</code> 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. I think the rotation axis of phi is defined by the rotation of theta.</p>
  
  <p>If there is another way to save a rgl snapshot in a lossless format I would be happy to learn about it!</p>