ARToolKit | Mailing List Archive |
![]() |
From: | Fernando Giovanini <flgiovanini@f ..........> | Received: | Nov 14, 2006 |
To | ARToolkit <artoolkit@h ..................> | ||
Subject: | arGetAngle | ||
Hi all, I need to get the rotation from a rotation matrix. I found the arGetAngle but in the ARToolkit forum some people said it have some problems. I Want to kown if it really have problems. If it's true, is there a way to fix it? If it's false, how can I use this function? Thanks, Fernando. |
From: | Daniel Wagner <daniel@i ...............> | Received: | Nov 14, 2006 |
To | ARToolKit Mailinglist <artoolkit@h ..................> | ||
Subject: | Re: arGetAngle | ||
Fernando Giovanini wrote: > I need to get the rotation from a rotation matrix. I found the > arGetAngle but in the ARToolkit forum some people said it have some > problems. > I Want to kown if it really have problems. If it's true, is there a > way to fix it? If it's false, how can I use this function? there is lots of code to extract the rotation of a matrix in the net. here is a code fragment from OpenTracker's MathUtils class, which should do what you need. bye, Daniel float* MathUtils::matrixToQuaternion(const float matrix[3][3], float* qResult) { double tr, s; int i, j, k; int nxt[3] = {1, 2, 0}; tr = matrix[0][0] + matrix[1][1] + matrix[2][2]; // check the diagonal if (tr > 0.0) { s = sqrt (tr + 1.0); qResult[3] =(float)( s / 2.0 ); s = 0.5 / s; qResult[0] = (float)((matrix[2][1] - matrix[1][2]) * s); qResult[1] = (float)((matrix[0][2] - matrix[2][0]) * s); qResult[2] = (float)((matrix[1][0] - matrix[0][1]) * s); } else { // diagonal is negative i = 0; if (matrix[1][1] > matrix[0][0]) i = 1; if (matrix[2][2] > matrix[i][i]) i = 2; j = nxt[i]; k = nxt[j]; s = sqrt((matrix[i][i] - (matrix[j][j] + matrix[k][k])) + 1.0); qResult[i] = (float)( s * 0.5 ); if (s != 0.0) s = 0.5 / s; qResult[3] =(float)((matrix[k][j] - matrix[j][k]) * s ); qResult[j] =(float)( (matrix[j][i] + matrix[i][j]) * s ); qResult[k] =(float)( (matrix[k][i] + matrix[i][k]) * s ); } return qResult; } |
From: | Fernando Giovanini <flgiovanini@f ..........> | Received: | Nov 14, 2006 |
To | |||
Subject: | Re: arGetAngle | ||
This is a multi-part message in MIME format. --------------030807000005080807050203 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Hello, Daniel. I think I didn't understand this code. It return a quaternion containing the rotation angle for each axis? Thanks, Fernando. Daniel Wagner escreveu: > Fernando Giovanini wrote: > >> I need to get the rotation from a rotation matrix. I found the >> arGetAngle but in the ARToolkit forum some people said it have some >> problems. >> I Want to kown if it really have problems. If it's true, is there a >> way to fix it? If it's false, how can I use this function? >> > there is lots of code to extract the rotation of a matrix in the net. > here is a code fragment from OpenTracker's MathUtils class, which should > do what you need. > > bye, > Daniel > > > float* MathUtils::matrixToQuaternion(const float matrix[3][3], > float* qResult) > { > double tr, s; > int i, j, k; > int nxt[3] = {1, 2, 0}; > > tr = matrix[0][0] + matrix[1][1] + matrix[2][2]; > > // check the diagonal > if (tr > 0.0) > { > s = sqrt (tr + 1.0); > qResult[3] =(float)( s / 2.0 ); > s = 0.5 / s; > qResult[0] = (float)((matrix[2][1] - matrix[1][2]) * s); > qResult[1] = (float)((matrix[0][2] - matrix[2][0]) * s); > qResult[2] = (float)((matrix[1][0] - matrix[0][1]) * s); > } > else > { > // diagonal is negative > i = 0; > if (matrix[1][1] > matrix[0][0]) i = 1; > if (matrix[2][2] > matrix[i][i]) i = 2; > j = nxt[i]; > k = nxt[j]; > > s = sqrt((matrix[i][i] - (matrix[j][j] + matrix[k][k])) + 1.0); > > qResult[i] = (float)( s * 0.5 ); > > if (s != 0.0) s = 0.5 / s; > > qResult[3] =(float)((matrix[k][j] - matrix[j][k]) * s ); > qResult[j] =(float)( (matrix[j][i] + matrix[i][j]) * s ); > qResult[k] =(float)( (matrix[k][i] + matrix[i][k]) * s ); > } > return qResult; > } > > > --------------030807000005080807050203 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type"> </head> <body bgcolor="#ffffff" text="#000000"> <br> Hello, Daniel.<br> <br> I think I didn't understand this code. It return a quaternion containing the rotation angle for each axis?<br> <br> Thanks,<br> Fernando.<br> <br> Daniel Wagner escreveu: <blockquote cite="mid455973A3.50303@i ..............." type="cite"> <pre wrap="">Fernando Giovanini wrote: </pre> <blockquote type="cite"> <pre wrap="">I need to get the rotation from a rotation matrix. I found the arGetAngle but in the ARToolkit forum some people said it have some problems. I Want to kown if it really have problems. If it's true, is there a way to fix it? If it's false, how can I use this function? </pre> </blockquote> <pre wrap=""><!---->there is lots of code to extract the rotation of a matrix in the net. here is a code fragment from OpenTracker's MathUtils class, which should do what you need. bye, Daniel float* MathUtils::matrixToQuaternion(const float matrix[3][3], float* qResult) { double tr, s; int i, j, k; int nxt[3] = {1, 2, 0}; tr = matrix[0][0] + matrix[1][1] + matrix[2][2]; // check the diagonal if (tr > 0.0) { s = sqrt (tr + 1.0); qResult[3] =(float)( s / 2.0 ); s = 0.5 / s; qResult[0] = (float)((matrix[2][1] - matrix[1][2]) * s); qResult[1] = (float)((matrix[0][2] - matrix[2][0]) * s); qResult[2] = (float)((matrix[1][0] - matrix[0][1]) * s); } else { // diagonal is negative i = 0; if (matrix[1][1] > matrix[0][0]) i = 1; if (matrix[2][2] > matrix[i][i]) i = 2; j = nxt[i]; k = nxt[j]; s = sqrt((matrix[i][i] - (matrix[j][j] + matrix[k][k])) + 1.0); qResult[i] = (float)( s * 0.5 ); if (s != 0.0) s = 0.5 / s; qResult[3] =(float)((matrix[k][j] - matrix[j][k]) * s ); qResult[j] =(float)( (matrix[j][i] + matrix[i][j]) * s ); qResult[k] =(float)( (matrix[k][i] + matrix[i][k]) * s ); } return qResult; } </pre> </blockquote> </body> </html> --------------030807000005080807050203-- |
From: | Fernando Giovanini <flgiovanini@f ..........> | Received: | Nov 14, 2006 |
To | |||
Subject: | Re: arGetAngle | ||
This is a multi-part message in MIME format. --------------090605060707010509030102 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit And I forgot to aks: the arGetAngle and arGetRot works fine? Fernando Giovanini escreveu: > > Hello, Daniel. > > I think I didn't understand this code. It return a quaternion > containing the rotation angle for each axis? > > Thanks, > Fernando. > > Daniel Wagner escreveu: >> Fernando Giovanini wrote: >> >>> I need to get the rotation from a rotation matrix. I found the >>> arGetAngle but in the ARToolkit forum some people said it have some >>> problems. >>> I Want to kown if it really have problems. If it's true, is there a >>> way to fix it? If it's false, how can I use this function? >>> >> there is lots of code to extract the rotation of a matrix in the net. >> here is a code fragment from OpenTracker's MathUtils class, which should >> do what you need. >> >> bye, >> Daniel >> >> >> float* MathUtils::matrixToQuaternion(const float matrix[3][3], >> float* qResult) >> { >> double tr, s; >> int i, j, k; >> int nxt[3] = {1, 2, 0}; >> >> tr = matrix[0][0] + matrix[1][1] + matrix[2][2]; >> >> // check the diagonal >> if (tr > 0.0) >> { >> s = sqrt (tr + 1.0); >> qResult[3] =(float)( s / 2.0 ); >> s = 0.5 / s; >> qResult[0] = (float)((matrix[2][1] - matrix[1][2]) * s); >> qResult[1] = (float)((matrix[0][2] - matrix[2][0]) * s); >> qResult[2] = (float)((matrix[1][0] - matrix[0][1]) * s); >> } >> else >> { >> // diagonal is negative >> i = 0; >> if (matrix[1][1] > matrix[0][0]) i = 1; >> if (matrix[2][2] > matrix[i][i]) i = 2; >> j = nxt[i]; >> k = nxt[j]; >> >> s = sqrt((matrix[i][i] - (matrix[j][j] + matrix[k][k])) + 1.0); >> >> qResult[i] = (float)( s * 0.5 ); >> >> if (s != 0.0) s = 0.5 / s; >> >> qResult[3] =(float)((matrix[k][j] - matrix[j][k]) * s ); >> qResult[j] =(float)( (matrix[j][i] + matrix[i][j]) * s ); >> qResult[k] =(float)( (matrix[k][i] + matrix[i][k]) * s ); >> } >> return qResult; >> } >> >> >> --------------090605060707010509030102 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type"> <title></title> </head> <body bgcolor="#ffffff" text="#000000"> And I forgot to aks: the arGetAngle and arGetRot works fine?<br> <br> Fernando Giovanini escreveu: <blockquote cite="mid4559F804.8000208@f .........." type="cite"> <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type"> <br> Hello, Daniel.<br> <br> I think I didn't understand this code. It return a quaternion containing the rotation angle for each axis?<br> <br> Thanks,<br> Fernando.<br> <br> Daniel Wagner escreveu: <blockquote cite="mid455973A3.50303@i ..............." type="cite"> <pre wrap="">Fernando Giovanini wrote: </pre> <blockquote type="cite"> <pre wrap="">I need to get the rotation from a rotation matrix. I found the arGetAngle but in the ARToolkit forum some people said it have some problems. I Want to kown if it really have problems. If it's true, is there a way to fix it? If it's false, how can I use this function? </pre> </blockquote> <pre wrap=""><!---->there is lots of code to extract the rotation of a matrix in the net. here is a code fragment from OpenTracker's MathUtils class, which should do what you need. bye, Daniel float* MathUtils::matrixToQuaternion(const float matrix[3][3], float* qResult) { double tr, s; int i, j, k; int nxt[3] = {1, 2, 0}; tr = matrix[0][0] + matrix[1][1] + matrix[2][2]; // check the diagonal if (tr > 0.0) { s = sqrt (tr + 1.0); qResult[3] =(float)( s / 2.0 ); s = 0.5 / s; qResult[0] = (float)((matrix[2][1] - matrix[1][2]) * s); qResult[1] = (float)((matrix[0][2] - matrix[2][0]) * s); qResult[2] = (float)((matrix[1][0] - matrix[0][1]) * s); } else { // diagonal is negative i = 0; if (matrix[1][1] > matrix[0][0]) i = 1; if (matrix[2][2] > matrix[i][i]) i = 2; j = nxt[i]; k = nxt[j]; s = sqrt((matrix[i][i] - (matrix[j][j] + matrix[k][k])) + 1.0); qResult[i] = (float)( s * 0.5 ); if (s != 0.0) s = 0.5 / s; qResult[3] =(float)((matrix[k][j] - matrix[j][k]) * s ); qResult[j] =(float)( (matrix[j][i] + matrix[i][j]) * s ); qResult[k] =(float)( (matrix[k][i] + matrix[i][k]) * s ); } return qResult; } </pre> </blockquote> </blockquote> </body> </html> --------------090605060707010509030102-- |
From: | Daniel Wagner <daniel@i ...............> | Received: | Nov 14, 2006 |
To | ARToolKit Mailinglist <artoolkit@h ..................> | ||
Subject: | Re: arGetAngle | ||
Fernando Giovanini wrote: > I think I didn't understand this code. It return a quaternion > containing the rotation angle for each axis? no, a quaternion is different. it is actually a little bit difficult to explain, but very handy to use. you should be able to find lots of information about using quaternions for rotations on the net. here's some more code (again from OpenTracker) to convert between quaternions and axis/angle rotations. if you need more (euler, etc.) look just it up yourself... Daniel float* MathUtils::axisAngleToQuaternion(const float* axisa, float* qResult) { double s = sin( axisa[3]/2 ); qResult[3] = (float)cos( axisa[3]/2 ); qResult[0] = (float)(axisa[0]*s); qResult[1] = (float)(axisa[1]*s); qResult[2] = (float)(axisa[2]*s); return qResult; } float *MathUtils::quaternionToAxisAngle(const float q[4], float axisa[4]) { double len; len = sqrt(q[0]*q[0] + q[1]*q[1] + q[2]*q[2]); // check quaternion if (len > Q_EPSILON) { // if valid compute vec and angle // normalize vec float invLen = (float)(1.0/ len); axisa[0] = q[0] * invLen; axisa[1] = q[1] * invLen; axisa[2] = q[2] * invLen; axisa[3] =(float)( 2.0f * (float)acos(q[3])); } else { // return identity quaternion axisa[0] = 0.0; axisa[1] = 0.0; axisa[2] = 1.0; axisa[3] = 0.0; } return axisa; } |