|
|
by Akatsuki
- // 四元数转矩阵[] Quaternions To Matrix
- void QuatMatrix(const float in[4], float out[4][4])
- {
- float xx, xy, xz, xw;
- float yy, yz, yw;
- float zz, zw;
- xx = in[0]*in[0];
- xy = in[0]*in[1];
- xz = in[0]*in[2];
- xw = in[0]*in[3];
- yy = in[1]*in[1];
- yz = in[1]*in[2];
- yw = in[1]*in[3];
- zz = in[2]*in[2];
- zw = in[2]*in[3];
- out[0][0] = 1-2*(yy+zz);
- out[0][1] = 2*(xy-zw);
- out[0][2] = 2*(xz+yw);
- out[0][3] = 0;
- out[1][0] = 2*(xy+zw);
- out[1][1] = 1-2*(xx+zz);
- out[1][2] = 2*(yz-xw);
- out[1][3] = 0;
- out[2][0] = 2*(xz-yw);
- out[2][1] = 2*(yz+xw);
- out[2][2] = 1-2*(xx+yy);
- out[2][3] = 0;
- out[3][0] = 0;
- out[3][1] = 0;
- out[3][2] = 0;
- out[3][3] = 1;
- }
- // 矩阵转四元数[] Matrix To Quaternions
- void MatrixQuat(const float in[4][4], float out[4])
- {
- float s;
- if (in[0][0]>in[1][1] && in[0][0]>in[2][2])
- {
- s = 2*sqrt(1.0+in[0][0]-in[1][1]-in[2][2]);
- out[0] = 0.25*s;
- out[1] = (in[1][0]+in[0][1])/s;
- out[2] = (in[0][2]+in[2][0])/s;
- out[3] = (in[2][1]-in[1][2])/s;
- }
- else if (in[1][1]>in[2][2])
- {
- s = 2*sqrt(1.0+in[1][1]-in[0][0]-in[2][2]);
- out[0] = (in[1][0]+in[0][1])/s;
- out[1] = 0.25*s;
- out[2] = (in[2][1]+in[1][2])/s;
- out[3] = (in[0][2]-in[2][0])/s;
- }
- else
- {
- s = 2*sqrt(1.0+in[2][2]-in[0][0]-in[1][1]);
- out[0] = (in[0][2]+in[2][0])/s;
- out[1] = (in[2][1]+in[1][2])/s;
- out[2] = 0.25*s;
- out[3] = (in[1][0]-in[0][1])/s;
- }
- }
复制代码 |
|