How to calculate the rotation of an object wrt container, which itself is rotated wrt universe, so that the object has nil rotation wrt universe?

Long subject, so I am hoping that explains what I am asking, but still let me elaborate with an example.

Let us assume we have an object as part of a container. Now the container is rotated by certain ROTATIONX, ROTATIONY, ROTATIONZ wrt the universe. Now we need to rotate the object WRT the container by rotationx, rotationy, rotationz such that the rotation of the object WRT the universe (rotx, roty, rotz) is zero. How do we do that?

-

There is theorem stating that in 3 dimensions, the composition of any two rotations in 3d-dimensional space is another rotations. This allows you to solve your problem as follows.

Suppose that from knowing the coordinates of the rotated container, and the coordinates of the container before rotations, you can represent $R=R_z\circ R_y\circ R_x$ by a matrix $$R=\left[\begin{matrix}a&d&g\\b&e&h\\c&f&i\end{matrix}\right]$$ where $R(1,0,0)=(a,b,c)$, $R(0,1,0)=(d,e,f)$ and $R(0,0,1)=(g,h,i)$.

Then the inverse rotation of $R$ is, in matrix form:

$$R^T=\left[\begin{matrix}a&b&c\\d&e&f\\g&h&i\end{matrix}\right]$$

So all you need is an algorithm that decomposes the matrix $R^T$ into its constituent rotations around the $x$-, $y$-, and $z$-axis. A good write-up I found via google is this blog post, along with actual computer code!.

-

Depending upon how you express rotations, you can just undo them in reverse order. Rotations do not commute. So if you did ROTATIONX, then ROTATIONY, then ROTATIONZ, you can do rotz=-ROTATIONZ, then roty=-ROTATIONY, and finally rotx=-ROTATIONX.

-
The issue is that I am working with a 3-d library that applies rotation in a specific order - x, y and then z - can't change this order. So I tried the above but it does not work. –  Roopesh Shenoy Mar 20 '11 at 16:49
@Roopesh: I don't understand how your library limits you to a specific order? I mean, can't you just call the function three times? –  Raskolnikov Mar 20 '11 at 17:05
@Raskolnikov - Nope.. I am using papervision3d, and it is an object property rather than a method or a function. I tried searching for solutions that let me call it in a particular order, but have not found it. –  Roopesh Shenoy Mar 20 '11 at 17:29