{"action":"created","issue":{"url":"https://api.github.com/repos/simbody/simbody/issues/519","repository_url":"https://api.github.com/repos/simbody/simbody","labels_url":"https://api.github.com/repos/simbody/simbody/issues/519/labels{/name}","comments_url":"https://api.github.com/repos/simbody/simbody/issues/519/comments","events_url":"https://api.github.com/repos/simbody/simbody/issues/519/events","html_url":"https://github.com/simbody/simbody/issues/519","id":168846265,"number":519,"title":"convertRotationToQuaternion flips the sign of the quaternion elements","user":{"login":"mitkof6","id":4024976,"avatar_url":"https://avatars.githubusercontent.com/u/4024976?v=3","gravatar_id":"","url":"https://api.github.com/users/mitkof6","html_url":"https://github.com/mitkof6","followers_url":"https://api.github.com/users/mitkof6/followers","following_url":"https://api.github.com/users/mitkof6/following{/other_user}","gists_url":"https://api.github.com/users/mitkof6/gists{/gist_id}","starred_url":"https://api.github.com/users/mitkof6/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mitkof6/subscriptions","organizations_url":"https://api.github.com/users/mitkof6/orgs","repos_url":"https://api.github.com/users/mitkof6/repos","events_url":"https://api.github.com/users/mitkof6/events{/privacy}","received_events_url":"https://api.github.com/users/mitkof6/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-08-02T10:03:16Z","updated_at":"2016-08-02T17:56:13Z","closed_at":null,"body":"It seems that in some cases almost identical Euler angles are reproduced by quaternion with flip signs. \r\n\r\nFor example:\r\n\r\n [0.003 -0.803  0.588 -0.099] ->  [173.508  9.350  72.959]\r\n [0.003  0.803  -0.588  0.099] -> [ 172.953  8.941  72.979]\r\n\r\nIn case that someone is calculating numerical derivatives, will get large values because of the sign change. I find that when I use the following implementation I avoid this kind of problem when I have to convert a rotation matrix to quaternion representation.\r\n\r\n\r\n```\r\nSimTK::Quaternion convertMatrixToQuaternion(const SimTK::Rotation & R)\r\n{\r\n```\r\n   ```\r\n    double qw, qx, qy, qz;\r\n    double tr = R[0][0] + R[1][1] + R[2][2];\r\n    if (tr > 0)\r\n    {\r\n        double S = sqrt(tr + 1.0) * 2; // S=4*qw\r\n        qw = 0.25 * S;\r\n        qx = (R[2][1] - R[1][2]) / S;\r\n        qy = (R[0][2] - R[2][0]) / S;\r\n        qz = (R[1][0] - R[0][1]) / S;\r\n    }\r\n    else if ((R[0][0] > R[1][1]) && (R[0][0] > R[2][2]))\r\n    {\r\n        double S = sqrt(1.0 + R[0][0] - R[1][1] - R[2][2]) * 2; // S=4*qx\r\n        qw = (R[2][1] - R[1][2]) / S;\r\n        qx = 0.25 * S;\r\n        qy = (R[0][1] + R[1][0]) / S;\r\n        qz = (R[0][2] + R[2][0]) / S;\r\n    }\r\n    else if (R[1][1] > R[2][2])\r\n    {\r\n        double S = sqrt(1.0 + R[1][1] - R[0][0] - R[2][2]) * 2; // S=4*qy\r\n        qw = (R[0][2] - R[2][0]) / S;\r\n        qx = (R[0][1] + R[1][0]) / S;\r\n        qy = 0.25 * S;\r\n        qz = (R[1][2] + R[2][1]) / S;\r\n    }\r\n    else\r\n    {\r\n        double S = sqrt(1.0 + R[2][2] - R[0][0] - R[1][1]) * 2; // S=4*qz\r\n        qw = (R[1][0] - R[0][1]) / S;\r\n        qx = (R[0][2] + R[2][0]) / S;\r\n        qy = (R[1][2] + R[2][1]) / S;\r\n        qz = 0.25 * S;\r\n    }\r\n    SimTK::Quaternion q(qw, qx, qy, qz);\r\n    q.normalize();\r\n```\r\n\r\n```\r\n    return q;\r\n} \r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/simbody/simbody/issues/comments/236987136","html_url":"https://github.com/simbody/simbody/issues/519#issuecomment-236987136","issue_url":"https://api.github.com/repos/simbody/simbody/issues/519","id":236987136,"user":{"login":"sherm1","id":4088016,"avatar_url":"https://avatars.githubusercontent.com/u/4088016?v=3","gravatar_id":"","url":"https://api.github.com/users/sherm1","html_url":"https://github.com/sherm1","followers_url":"https://api.github.com/users/sherm1/followers","following_url":"https://api.github.com/users/sherm1/following{/other_user}","gists_url":"https://api.github.com/users/sherm1/gists{/gist_id}","starred_url":"https://api.github.com/users/sherm1/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sherm1/subscriptions","organizations_url":"https://api.github.com/users/sherm1/orgs","repos_url":"https://api.github.com/users/sherm1/repos","events_url":"https://api.github.com/users/sherm1/events{/privacy}","received_events_url":"https://api.github.com/users/sherm1/received_events","type":"User","site_admin":false},"created_at":"2016-08-02T17:56:13Z","updated_at":"2016-08-02T17:56:13Z","body":"Hi, Dimitar. Can you be more specific about what derivative you are attempting to calculate? The main utility of quaternions is that they are smooth under differentiation, while Euler angle are discontinuous. I'm not sure how a Rotation-to-Quaternion conversion method relates to differentiation in this case. Regards, Sherm"}}
{"actor":{"display_login":"sherm1"}}