3
$\begingroup$

Given a vector n1, I find u = n1 x {0,1,0}. Then I find theta = acos(n1 . {0,1,0}).

Then I form an axis-angle rotation matrix from u and theta. Then I form a rotation quaternion from that rotation matrix.

My question is: Can I easily find n1 given the rotation quaternion?




Another version of this question, with more background information:

Assume the reference "up" is Y, and positive Z goes into your screen.

I have a 3D plane defined by it's normal, n1. n1_y is always positive.

I have a 3-vector, n2, which is relative to the XZ plane. I want to find the vector that results from rotating n2 so that it is relative to the plane defined by n1.

Currently, I accomplish this by finding the axis of rotation, by taking n1 cross the XZ unit vector:

float3 u = cross(n1, float3(0,1,0)); 

Then I find the angle to rotate by:

float theta = acos(dot(float3(0,1,0), n1)); 

Then I form a rotation matrix from the axis and angle:

float3x3 rotMatrix = float3x3(         cos(theta) + u.x*u.x*(1-cos(theta)), u.x*u.y*(1-cos(theta))-u.z*sin(theta), u.x*u.z*(1-cos(theta)) + u.y*sin(theta),         u.y*u.x*(1-cos(theta)) + u.z*sin(theta), cos(theta) + u.y*u.y*(1-cos(theta)), u.y*u.z*(1-cos(theta))-u.x*sin(theta),         u.z*u.x*(1-cos(theta)) - u.y*sin(theta), u.z*u.y*(1-cos(theta))+u.x*sin(theta), cos(theta)+u.z*u.z*(1-cos(theta))     ); 

Then I multiply n2 by the rotation matrix to yield the new vector I wanted.

Currently n1 is precomputed and stored, so I have to find the axis and angle and rotation matrix from n1.

I was hoping there is a way to store a quaternion instead (representing the axis and angle), and easily recover the normal n1 from that quaternion. Is there a way to do that? How?

Or, is there another more efficient way to do this computation?

  • 0
    @TheBigO: no code block. \$ \theta \$ will give $\theta$2011-04-23

1 Answers 1

1

Let $v=[0,1,0]^T$. Suppose $R$ is the rotation matrix you obtained. Then $R$ can rotate vector $n_1$ to $v$. That is $Rn_1=v$. Hence $n_1=R^Tv$.

So once you have a quaternion, you can convert it to a rotation matrix $R$ first. Then recover the $n_1$ as $n_1=R^Tv$.

  • 0
    Thanks to both of you! Rahul, feel free to add that as an answer, for some rep.2011-04-23