[Rd] rgl rotations

Duncan Murdoch murdoch.duncan at gmail.com
Thu Apr 17 20:31:47 CEST 2014

On 17/04/2014 1:56 PM, Dominick Samperi wrote:
> Is there a way to change the viewpoint using view3d (or rgl.viewpoint)
> with respect to
> the image that currently appears rather than the perspective that rgl
> thinks is the
> default? For example, if I create an image and then perform what
> should be a no-op:
> rgl.viewpoint(userMatrix=rotationMatrix(0,1,0,0))
> the image is rotated. Then I can perform rotations on the result
> without surprises.
> But I want to specify rotation with respect to the image as it
> originally appeared,
> or with respect to the viewpoint that is currently in effect.
> Experimentation has revealed that when using rotationMatrix the z-axis
> points out
> of the canvas instead of pointing up as it often is in mathematics,
> especially when
> using the spherical coordinates (theta,phi).
> I guess the question is: can we query rgl for the current viewpoint and then
> apply changes to that? Another way of putting it is: can we do programmatically
> what we can easily do using the mouse in interactive mode?

There are two matrices involved in rgl rendering, and they are computed 
in a relatively complicated way that is described in ?par3d.  I'm not 
sure I understand all of what you want to do, but I think the following 
achieves it; if not, take a look at that help page:

example(plot3d)  # just get a sample plot on screen.  Rotate it by mouse 
if you like.
U <- par3d("userMatrix")
for (theta in seq(0, pi, len=100)) {
   par3d(userMatrix = rotate3d(U, theta, 0,0,1)) # Rotate about model's 
z axis

for (theta in seq(0, pi, len=100)) {
   par3d(userMatrix = rotationMatrix(theta, 0,0,1) %*%U ) # Rotate about 
viewer's z axis

In this case, the "viewer's z-axis" is perpendicular to the screen; the 
"model's z-axis" is parallel to the z axis in the box around the plot.

Duncan Murdoch

More information about the R-devel mailing list