{
  "id": 247834,
  "title": "About IMU: OrientationDeg to Rotation Vector",
  "url": "/competitions/google-smartphone-decimeter-challenge/discussion/247834",
  "author_name": "Alvin.ai",
  "post_date": "2021-06-21T10:11:43.869000",
  "votes": 13,
  "comment_count": 0,
  "views": 0,
  "content": "<p>I believe some people want to use IMU data as top winners did in Indoor competition, but Google's data doesn't provide the Rotation Vector. I searched on many open websites and found that <strong>Orientation Degree (pitch, yaw, roll)</strong> In GnssLog is quite similar to Euler Angles, so I wondering if it is possible to convert  Orientation Degree to Rotation Vector. </p>\n<p>Thanks for <a href=\"https://www.kaggle.com/DiegoJohnson\" target=\"_blank\">@DiegoJohnson</a> Kaggler, he shared \"<a href=\"https://www.kaggle.com/diegojohnson/representing-rotation-with-rotation-vector\" target=\"_blank\">Representing Rotation with Rotation Vector</a>\" is helpful. Also, <a href=\"https://www.kaggle.com/GreatGameDota\" target=\"_blank\">@GreatGameDota</a>'s comment is informative too.</p>\n<p><strong>1. Euler Angles to Rotation Vector</strong></p>\n<pre><code>import math\nimport numpy as np\nfrom cv2 import Rodrigues\nfrom math import sin, cos, atan2, sqrt\n</code></pre>\n<pre><code>def an2v(y_delta, z_delta, x_delta):\n    '''\n    Euler Angles -&gt;Rotation Matrix -&gt; Rotation Vector\n\n    Input：\n        1. y_delta          (float): the angle with rotateing around y-axis.\n        2. z_delta         (float): the angle with rotateing around z-axis. \n        3. x_delta         (float): the angle with rotateing around x-axis. \n   Output：\n        rx/ry/rz             (float): the rotation vector with rotateing around x/y/z-axis.\n\n    Code Ref.: https://www.zacobria.com/universal-robots-knowledge-base-tech-support-forum-hints-tips/python-code-example-of-converting-rpyeuler-angles-to-rotation-vectorangle-axis-for-universal-robots/\n    (Note：In Code Ref: pitch=y,yaw=z,roll=x. But Google is pitch=x,yaw=z,roll=y)\n    '''\n    # yaw: z\n    Rz_Matrix = np.matrix([\n    [math.cos(z_delta), -math.sin(z_delta), 0],\n    [math.sin(z_delta), math.cos(z_delta), 0],\n    [0, 0, 1]\n    ])\n\n    # pitch: y\n    Ry_Matrix = np.matrix([\n    [math.cos(y_delta), 0, math.sin(y_delta)],\n    [0, 1, 0],\n    [-math.sin(y_delta), 0, math.cos(y_delta)]\n    ])\n\n    # roll: x\n    Rx_Matrix = np.matrix([\n    [1, 0, 0],\n    [0, math.cos(x_delta), -math.sin(x_delta)],\n    [0, math.sin(x_delta), math.cos(x_delta)]\n    ])\n\n    R = Rz_Matrix * Ry_Matrix * Rx_Matrix\n\n    theta = math.acos(((R[0, 0] + R[1, 1] + R[2, 2]) - 1) / 2)\n    multi = 1 / (2 * math.sin(theta))\n\n    rx = multi * (R[2, 1] - R[1, 2]) * theta\n    ry = multi * (R[0, 2] - R[2, 0]) * theta\n    rz = multi * (R[1, 0] - R[0, 1]) * theta\n\n    return rx, ry, rz\n</code></pre>\n<p><strong>2. Rotation Vector to Euler Angles</strong></p>\n<pre><code>def v2a(rotation_v):\n    \"\"\"\n    Rotation Vector -&gt; Rotation Matrix -&gt; Euler Angles\n\n    Input：\n        rx/ry/rz             (float): the rotation vector with rotateing around x/y/z-axis.\n   Output：\n        1. y_delta          (float): the angle with rotateing around y-axis.\n        2. z_delta         (float): the angle with rotateing around z-axis. \n        3. x_delta         (float): the angle with rotateing around x-axis.   \n    “”“\n    # Rotation Vector -&gt; Rotation Matrix\n    R = Rodrigues(rotation_v)[0]\n\n    sq = sqrt(R[2,1] ** 2 +  R[2,2] ** 2)\n\n    if  not (sq &lt; 1e-6) :\n        x_delta = atan2(R[2,1] , R[2,2])\n        y_delta = atan2(-R[2,0], sq)\n        z_delta = atan2(R[1,0], R[0,0])\n    else :\n        x_delta = atan2(-R[1,2], R[1,1])\n        y_delta = atan2(-R[2,0], sq)\n        z_delta = 0\n\n    return y_delta, z_delta, x_delta\n</code></pre>\n<p><strong>3. Test</strong></p>\n<pre><code>pitch, yaw, roll = 0.254839, -2.57534, -3.10256\nrotation_v = an2v(pitch, yaw, roll)\nrotation_v\n</code></pre>\n<blockquote>\n  <blockquote>\n    <p>(-0.7992088080637765, 2.772797963612602, 0.04919481302670785)</p>\n  </blockquote>\n</blockquote>\n<pre><code>v2a(rotation_v)\n</code></pre>\n<blockquote>\n  <blockquote>\n    <p>(0.2548389999999962, -2.5753400000000006, -3.102560000000001)</p>\n  </blockquote>\n</blockquote>\n<p>But I wonder if it is right to obtain the Rotation Vector from the Orientation Degree or not? it's my first time playing Kaggle, please feel free to discuss and comment. 🤝</p>",
  "messages": [
    {
      "id": 1359475,
      "postDate": "2021-06-21T10:11:43.870Z",
      "content": "<p>I believe some people want to use IMU data as top winners did in Indoor competition, but Google's data doesn't provide the Rotation Vector. I searched on many open websites and found that <strong>Orientation Degree (pitch, yaw, roll)</strong> In GnssLog is quite similar to Euler Angles, so I wondering if it is possible to convert  Orientation Degree to Rotation Vector. </p>\n<p>Thanks for <a href=\"https://www.kaggle.com/DiegoJohnson\" target=\"_blank\">@DiegoJohnson</a> Kaggler, he shared \"<a href=\"https://www.kaggle.com/diegojohnson/representing-rotation-with-rotation-vector\" target=\"_blank\">Representing Rotation with Rotation Vector</a>\" is helpful. Also, <a href=\"https://www.kaggle.com/GreatGameDota\" target=\"_blank\">@GreatGameDota</a>'s comment is informative too.</p>\n<p><strong>1. Euler Angles to Rotation Vector</strong></p>\n<pre><code>import math\nimport numpy as np\nfrom cv2 import Rodrigues\nfrom math import sin, cos, atan2, sqrt\n</code></pre>\n<pre><code>def an2v(y_delta, z_delta, x_delta):\n    '''\n    Euler Angles -&gt;Rotation Matrix -&gt; Rotation Vector\n\n    Input：\n        1. y_delta          (float): the angle with rotateing around y-axis.\n        2. z_delta         (float): the angle with rotateing around z-axis. \n        3. x_delta         (float): the angle with rotateing around x-axis. \n   Output：\n        rx/ry/rz             (float): the rotation vector with rotateing around x/y/z-axis.\n\n    Code Ref.: https://www.zacobria.com/universal-robots-knowledge-base-tech-support-forum-hints-tips/python-code-example-of-converting-rpyeuler-angles-to-rotation-vectorangle-axis-for-universal-robots/\n    (Note：In Code Ref: pitch=y,yaw=z,roll=x. But Google is pitch=x,yaw=z,roll=y)\n    '''\n    # yaw: z\n    Rz_Matrix = np.matrix([\n    [math.cos(z_delta), -math.sin(z_delta), 0],\n    [math.sin(z_delta), math.cos(z_delta), 0],\n    [0, 0, 1]\n    ])\n\n    # pitch: y\n    Ry_Matrix = np.matrix([\n    [math.cos(y_delta), 0, math.sin(y_delta)],\n    [0, 1, 0],\n    [-math.sin(y_delta), 0, math.cos(y_delta)]\n    ])\n\n    # roll: x\n    Rx_Matrix = np.matrix([\n    [1, 0, 0],\n    [0, math.cos(x_delta), -math.sin(x_delta)],\n    [0, math.sin(x_delta), math.cos(x_delta)]\n    ])\n\n    R = Rz_Matrix * Ry_Matrix * Rx_Matrix\n\n    theta = math.acos(((R[0, 0] + R[1, 1] + R[2, 2]) - 1) / 2)\n    multi = 1 / (2 * math.sin(theta))\n\n    rx = multi * (R[2, 1] - R[1, 2]) * theta\n    ry = multi * (R[0, 2] - R[2, 0]) * theta\n    rz = multi * (R[1, 0] - R[0, 1]) * theta\n\n    return rx, ry, rz\n</code></pre>\n<p><strong>2. Rotation Vector to Euler Angles</strong></p>\n<pre><code>def v2a(rotation_v):\n    \"\"\"\n    Rotation Vector -&gt; Rotation Matrix -&gt; Euler Angles\n\n    Input：\n        rx/ry/rz             (float): the rotation vector with rotateing around x/y/z-axis.\n   Output：\n        1. y_delta          (float): the angle with rotateing around y-axis.\n        2. z_delta         (float): the angle with rotateing around z-axis. \n        3. x_delta         (float): the angle with rotateing around x-axis.   \n    “”“\n    # Rotation Vector -&gt; Rotation Matrix\n    R = Rodrigues(rotation_v)[0]\n\n    sq = sqrt(R[2,1] ** 2 +  R[2,2] ** 2)\n\n    if  not (sq &lt; 1e-6) :\n        x_delta = atan2(R[2,1] , R[2,2])\n        y_delta = atan2(-R[2,0], sq)\n        z_delta = atan2(R[1,0], R[0,0])\n    else :\n        x_delta = atan2(-R[1,2], R[1,1])\n        y_delta = atan2(-R[2,0], sq)\n        z_delta = 0\n\n    return y_delta, z_delta, x_delta\n</code></pre>\n<p><strong>3. Test</strong></p>\n<pre><code>pitch, yaw, roll = 0.254839, -2.57534, -3.10256\nrotation_v = an2v(pitch, yaw, roll)\nrotation_v\n</code></pre>\n<blockquote>\n  <blockquote>\n    <p>(-0.7992088080637765, 2.772797963612602, 0.04919481302670785)</p>\n  </blockquote>\n</blockquote>\n<pre><code>v2a(rotation_v)\n</code></pre>\n<blockquote>\n  <blockquote>\n    <p>(0.2548389999999962, -2.5753400000000006, -3.102560000000001)</p>\n  </blockquote>\n</blockquote>\n<p>But I wonder if it is right to obtain the Rotation Vector from the Orientation Degree or not? it's my first time playing Kaggle, please feel free to discuss and comment. 🤝</p>",
      "rawMarkdown": "I believe some people want to use IMU data as top winners did in Indoor competition, but Google's data doesn't provide the Rotation Vector. I searched on many open websites and found that **Orientation Degree (pitch, yaw, roll)** In GnssLog is quite similar to Euler Angles, so I wondering if it is possible to convert  Orientation Degree to Rotation Vector. \n\nThanks for @DiegoJohnson Kaggler, he shared \"[Representing Rotation with Rotation Vector](https://www.kaggle.com/diegojohnson/representing-rotation-with-rotation-vector)\" is helpful. Also, @GreatGameDota's comment is informative too.\n\n**1. Euler Angles to Rotation Vector**\n\n```\nimport math\nimport numpy as np\nfrom cv2 import Rodrigues\nfrom math import sin, cos, atan2, sqrt\n```\n\n```\ndef an2v(y_delta, z_delta, x_delta):\n    '''\n    Euler Angles ->Rotation Matrix -> Rotation Vector\n    \n    Input：\n        1. y_delta          (float): the angle with rotateing around y-axis.\n        2. z_delta         (float): the angle with rotateing around z-axis. \n        3. x_delta         (float): the angle with rotateing around x-axis. \n   Output：\n        rx/ry/rz             (float): the rotation vector with rotateing around x/y/z-axis.\n    \n    Code Ref.: https://www.zacobria.com/universal-robots-knowledge-base-tech-support-forum-hints-tips/python-code-example-of-converting-rpyeuler-angles-to-rotation-vectorangle-axis-for-universal-robots/\n    (Note：In Code Ref: pitch=y,yaw=z,roll=x. But Google is pitch=x,yaw=z,roll=y)\n    '''\n    # yaw: z\n    Rz_Matrix = np.matrix([\n    [math.cos(z_delta), -math.sin(z_delta), 0],\n    [math.sin(z_delta), math.cos(z_delta), 0],\n    [0, 0, 1]\n    ])\n    \n    # pitch: y\n    Ry_Matrix = np.matrix([\n    [math.cos(y_delta), 0, math.sin(y_delta)],\n    [0, 1, 0],\n    [-math.sin(y_delta), 0, math.cos(y_delta)]\n    ])\n    \n    # roll: x\n    Rx_Matrix = np.matrix([\n    [1, 0, 0],\n    [0, math.cos(x_delta), -math.sin(x_delta)],\n    [0, math.sin(x_delta), math.cos(x_delta)]\n    ])\n\n    R = Rz_Matrix * Ry_Matrix * Rx_Matrix\n\n    theta = math.acos(((R[0, 0] + R[1, 1] + R[2, 2]) - 1) / 2)\n    multi = 1 / (2 * math.sin(theta))\n\n    rx = multi * (R[2, 1] - R[1, 2]) * theta\n    ry = multi * (R[0, 2] - R[2, 0]) * theta\n    rz = multi * (R[1, 0] - R[0, 1]) * theta\n\n    return rx, ry, rz\n```\n\n**2. Rotation Vector to Euler Angles**\n\n```\ndef v2a(rotation_v):\n    \"\"\"\n    Rotation Vector -> Rotation Matrix -> Euler Angles\n    \n    Input：\n        rx/ry/rz             (float): the rotation vector with rotateing around x/y/z-axis.\n   Output：\n        1. y_delta          (float): the angle with rotateing around y-axis.\n        2. z_delta         (float): the angle with rotateing around z-axis. \n        3. x_delta         (float): the angle with rotateing around x-axis.   \n    “”“\n    # Rotation Vector -> Rotation Matrix\n    R = Rodrigues(rotation_v)[0]\n\n    sq = sqrt(R[2,1] ** 2 +  R[2,2] ** 2)\n\n    if  not (sq < 1e-6) :\n        x_delta = atan2(R[2,1] , R[2,2])\n        y_delta = atan2(-R[2,0], sq)\n        z_delta = atan2(R[1,0], R[0,0])\n    else :\n        x_delta = atan2(-R[1,2], R[1,1])\n        y_delta = atan2(-R[2,0], sq)\n        z_delta = 0\n\n    return y_delta, z_delta, x_delta\n```\n\n**3. Test**\n\n```\npitch, yaw, roll = 0.254839, -2.57534, -3.10256\nrotation_v = an2v(pitch, yaw, roll)\nrotation_v\n```\n>> (-0.7992088080637765, 2.772797963612602, 0.04919481302670785)\n\n```\nv2a(rotation_v)\n```\n>> (0.2548389999999962, -2.5753400000000006, -3.102560000000001)\n\nBut I wonder if it is right to obtain the Rotation Vector from the Orientation Degree or not? it's my first time playing Kaggle, please feel free to discuss and comment. 🤝",
      "votes": 13
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "1359475": "I believe some people want to use IMU data as top winners did in Indoor competition, but Google's data doesn't provide the Rotation Vector. I searched on many open websites and found that **Orientation Degree (pitch, yaw, roll)** In GnssLog is quite similar to Euler Angles, so I wondering if it is possible to convert  Orientation Degree to Rotation Vector. \n\nThanks for @DiegoJohnson Kaggler, he shared \"[Representing Rotation with Rotation Vector](https://www.kaggle.com/diegojohnson/representing-rotation-with-rotation-vector)\" is helpful. Also, @GreatGameDota's comment is informative too.\n\n**1. Euler Angles to Rotation Vector**\n\n```\nimport math\nimport numpy as np\nfrom cv2 import Rodrigues\nfrom math import sin, cos, atan2, sqrt\n```\n\n```\ndef an2v(y_delta, z_delta, x_delta):\n    '''\n    Euler Angles ->Rotation Matrix -> Rotation Vector\n    \n    Input：\n        1. y_delta          (float): the angle with rotateing around y-axis.\n        2. z_delta         (float): the angle with rotateing around z-axis. \n        3. x_delta         (float): the angle with rotateing around x-axis. \n   Output：\n        rx/ry/rz             (float): the rotation vector with rotateing around x/y/z-axis.\n    \n    Code Ref.: https://www.zacobria.com/universal-robots-knowledge-base-tech-support-forum-hints-tips/python-code-example-of-converting-rpyeuler-angles-to-rotation-vectorangle-axis-for-universal-robots/\n    (Note：In Code Ref: pitch=y,yaw=z,roll=x. But Google is pitch=x,yaw=z,roll=y)\n    '''\n    # yaw: z\n    Rz_Matrix = np.matrix([\n    [math.cos(z_delta), -math.sin(z_delta), 0],\n    [math.sin(z_delta), math.cos(z_delta), 0],\n    [0, 0, 1]\n    ])\n    \n    # pitch: y\n    Ry_Matrix = np.matrix([\n    [math.cos(y_delta), 0, math.sin(y_delta)],\n    [0, 1, 0],\n    [-math.sin(y_delta), 0, math.cos(y_delta)]\n    ])\n    \n    # roll: x\n    Rx_Matrix = np.matrix([\n    [1, 0, 0],\n    [0, math.cos(x_delta), -math.sin(x_delta)],\n    [0, math.sin(x_delta), math.cos(x_delta)]\n    ])\n\n    R = Rz_Matrix * Ry_Matrix * Rx_Matrix\n\n    theta = math.acos(((R[0, 0] + R[1, 1] + R[2, 2]) - 1) / 2)\n    multi = 1 / (2 * math.sin(theta))\n\n    rx = multi * (R[2, 1] - R[1, 2]) * theta\n    ry = multi * (R[0, 2] - R[2, 0]) * theta\n    rz = multi * (R[1, 0] - R[0, 1]) * theta\n\n    return rx, ry, rz\n```\n\n**2. Rotation Vector to Euler Angles**\n\n```\ndef v2a(rotation_v):\n    \"\"\"\n    Rotation Vector -> Rotation Matrix -> Euler Angles\n    \n    Input：\n        rx/ry/rz             (float): the rotation vector with rotateing around x/y/z-axis.\n   Output：\n        1. y_delta          (float): the angle with rotateing around y-axis.\n        2. z_delta         (float): the angle with rotateing around z-axis. \n        3. x_delta         (float): the angle with rotateing around x-axis.   \n    “”“\n    # Rotation Vector -> Rotation Matrix\n    R = Rodrigues(rotation_v)[0]\n\n    sq = sqrt(R[2,1] ** 2 +  R[2,2] ** 2)\n\n    if  not (sq < 1e-6) :\n        x_delta = atan2(R[2,1] , R[2,2])\n        y_delta = atan2(-R[2,0], sq)\n        z_delta = atan2(R[1,0], R[0,0])\n    else :\n        x_delta = atan2(-R[1,2], R[1,1])\n        y_delta = atan2(-R[2,0], sq)\n        z_delta = 0\n\n    return y_delta, z_delta, x_delta\n```\n\n**3. Test**\n\n```\npitch, yaw, roll = 0.254839, -2.57534, -3.10256\nrotation_v = an2v(pitch, yaw, roll)\nrotation_v\n```\n>> (-0.7992088080637765, 2.772797963612602, 0.04919481302670785)\n\n```\nv2a(rotation_v)\n```\n>> (0.2548389999999962, -2.5753400000000006, -3.102560000000001)\n\nBut I wonder if it is right to obtain the Rotation Vector from the Orientation Degree or not? it's my first time playing Kaggle, please feel free to discuss and comment. 🤝"
  }
}