Re: 3D vector rotation to the Z axis [message #74178 is a reply to message #74027] |
Mon, 03 January 2011 17:17  |
MartyL
Messages: 3 Registered: December 2010
|
Junior Member |
|
|
On Dec 16 2010, 11:48 am, James <donje...@gmail.com> wrote:
> Do you have to use a rotation matrix? Quaternions (http://
> en.wikipedia.org/wiki/Quaternion) are more numerically stable, and
> there is an easy-to-use library by Craig Markwardt athttp://www.physics.wisc.edu/~craigm/idl/math.html. I was just using
> them to draw some rotating polyhedra in IDL, and they work great!
> Your code would look like this:
>
> ;define the axis of rotation
> rotaxis = crossp (input, [0,0,1])
>
> ;find the angle of rotation
> rotangle = transpose(input) # [0,0,1]
>
> ;make the quaternion
> q = qtcompose(rotaxis, rotangle)
>
> ;do it
> rotated = qtvrot(input, q)
>
> ;there is even a routine to create a rotation matrix from the
> quaternion:
> rmatx = qtmat(q)
>
> by the way, I like the # operator. It lets you treat 1D arrays as
> column vectors; then defining a matrix is just concatenating a group
> of column vectors across the second dimension. Matrix-by-vector
> multiplication works like you expect.
>
> - James
>
James,
I do not need to use a rotation matrix per se, I just need to rotate
the vectors to the Z axis.
I copied your code and made a quick test procedure and I am not
getting the results I would expect. Here is the code I used:
PRO Rotate_to_Z_axis, input
print, 'input = ', input
;define the axis of rotation
rotaxis = crossp (input, [0,0,1])
;find the angle of rotation
rotangle = transpose(input) # [0,0,1]
;make the quaternion
q = qtcompose(rotaxis, rotangle)
;do it
rotated = qtvrot(input, q)
print, 'rotated = ', rotated
end
I then input the following at the command prompt:
rotate_to_z_axis, [cos(!dtor*30.0), sin(!dtor*30.0), 0.0]
and the results are:
input = 0.866025 0.500000 0.00000
rotated = 0.866025 0.500000 0.00000
Shouldn't the rotated vector be [0,0,1] if it is being rotated to the
Z axis? That is what I am trying to do at least.
|
|
|