To represent a rotation by Euler angles, first rotate the object about the z-axis, then about the y-axis, and finally about the new z-azis.
Rotations about coordinate axes, lying wholly within the plane perpendicular to the axis, have a simple form:
To perform the indicated rotations in succession, through angles
,
, and
, respectively, means multiplying the corresponding
orthogonal matrices.
/* spheu - generate rotation from euler angles */
void spheu(o,a) double o[][3], a[3]; {
double e0, e1, e2, c0, c1, c2, s0, s1, s2;
e0=(a[0]*3.14159)/180.0;
e1=(a[1]*3.14159)/180.0;
e2=(a[2]*3.14159)/180.0;
c0=cos(e0);
c1=cos(e1);
c2=cos(e2);
s0=sin(e0);
s1=sin(e1);
s2=sin(e2);
o[0][0]= c0*c2-s0*c1*s2;
o[0][1]=-c0*s2-s0*c1*c2;
o[0][2]= s0*s1;
o[1][0]= s0*c2+c0*c1*s2;
o[1][1]=-s0*s2+c0*c1*c2;
o[1][2]=-c0*s1;
o[2][0]= s1*s2;
o[2][1]= s1*c2;
o[2][2]= c1;
}
The formulas for the matrix elements of the Euler angles can be inverted, allowing the angles to be recovered from the matrix. We have:
/* sphgeu - find the euler angles of a rotation */
void sphgeu(a,o) double *a, *o[3]; {
double e1, s1;
e1=acos(o[2][2];
s1=sin(e1);
if (s1==0.0) {a[0]=acos(o[1][1]; a[1]=a[2]=0.0;}
else {a[2]=acos(o[2][1]/s1); a[1]=e1; a[0]=acos(-o[1][2]/s1;}
}