Axis Angle to quaternion and quaternion to Axis angle question
Greetings All (matlab / octave code)
Link to text file in case formatting gets messed up http://db.tt/nVv8Ivj
I created two functions one to convert axis angle to quaternion and another one to convert quaternion to axis angle as far as I can tell the formulas are correct the question I have is when I create a quaternion from the axis angle format example: x=.3 y=.1 z=.6 and the angle is 45 degrees I get (0.962730w 0.119633i 0.039878j 0.239266k) which is correct when I check it. But when I plug this quaternion number back into my function I get back angle is 31.384 degrees x=.44233, y=.14744 z=.88465 I was expecting to get back x=.3 y=.1 z=.6 and the angle of 45 degrees the same thing I put in
Does anyone know what I'm doing wrong? The reason I'm doing it this way is to do rotations on values in arrays and not get gimbal lock. I also plan to plot it and animate it, and the axis angle format (x,y,z angle) is easier to understand when it comes to plotting
Iv'e included the code below with a link above to the text file since the formating sometimes gets messed up.
`%Vector taken in [i j k], angle in degrees %quaterions given in Format [w xi yj zk] or [w x y z] % %Example of using function: ccc=quat([.3 .1 .6],45) %Answer should be -> %0.962730w 0.119633i 0.039878j 0.239266k function Q= quat(vect,theta) Q = zeros(1,4); xyz_and_angle=[vect theta] %displays inputed data and theta angle before deg to rad conversion %thetaconvert = (theta*pi/180)./norm(theta*pi/180) %convert deg to rad thetaconvert=theta*pi/180; px=vect(1,1);py=vect(1,2);pz=vect(1,3); Q(1,1) = cos(thetaconvert/2); %w value Q(1,2)= px*sin(thetaconvert/2); % x value Q(1,3)= py*sin(thetaconvert/2); % y value Q(1,4)= pz*sin(thetaconvert/2); % z value q1w=Q(1,1);%qw value q1x=Q(1,2);%qx value q1y=Q(1,3);%qy value q1z=Q(1,4);%qz value %Normalize Quaternion due to floating point errors qnorm=sqrt(q1w^2+q1x^2+q1y^2+q1z^2) Q=Q./qnorm %normalize Q array %quaterions taken in Format [w xi yj zk] or [w x y z] %example ccc=[0.962730 0.119633 0.039878 0.239266] %ccc2=quattoaxis(ccc) %Answer comes back as angle is 31.384 degrees x=.44233, y=.14744 z=.88465 %not sure if the answer is right I thought it would be %x=.3 y=.1 z=.6 and the angle of 45 degrees the same thing I put in above function Q= quattoaxis(quat) Q = zeros(1,4); q1w=quat(1,1);q1x=quat(1,2);q1y=quat(1,3);q1z=quat(1,4); %Get angle from quat %convert Quaternion to Axis Q(1,1) = 2*acos(q1w) *180/pi %w / angle, I also included radians to degrees Q(1,2)= q1x/sqrt(1-q1w^2) %x Q(1,3)= q1y/sqrt(1-q1w^2) %y Q(1,4)= q1z/sqrt(1-q1w^2) %z axisangle=Q(1,1) %w / angle, I also included radians to degrees axisx=Q(1,2) %x axisy=Q(1,3) %y axisz=Q(1,4) %z %Normalize axis angle due to floating point errors qnorm=sqrt(axisx^2+axisy^2+axisz^2) axisx=Q(1,2)/qnorm; %x axisy=Q(1,3)/qnorm; %y axisz=Q(1,4)/qnorm; %z Q=[axisangle axisx axisy axisz]