skeleton_kinematics.html 23.5 KB
Newer Older
allai5's avatar
allai5 committed
1
<!DOCTYPE html> <html lang="en-US"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=Edge"> <title>Skeleton Kinematics - </title> <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"> <link rel="stylesheet" href="/assets/css/just-the-docs-default.css"> <script type="text/javascript" src="/assets/js/vendor/lunr.min.js"></script> <script type="text/javascript" src="/assets/js/just-the-docs.js"></script> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Begin Jekyll SEO tag v2.7.1 --> <title>Skeleton Kinematics</title> <meta name="generator" content="Jekyll v4.2.0" /> <meta property="og:title" content="Skeleton Kinematics" /> <meta property="og:locale" content="en_US" /> <meta name="twitter:card" content="summary" /> <meta property="twitter:title" content="Skeleton Kinematics" /> <script type="application/ld+json"> {"headline":"Skeleton Kinematics","@type":"WebPage","url":"/animation/skeleton_kinematics","@context":"https://schema.org"}</script> <!-- End Jekyll SEO tag --> </head> <body> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"> <symbol id="svg-link" viewBox="0 0 24 24"> <title>Link</title> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-link"> <path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path> </svg> </symbol> <symbol id="svg-search" viewBox="0 0 24 24"> <title>Search</title> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-search"> <circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> </symbol> <symbol id="svg-menu" viewBox="0 0 24 24"> <title>Menu</title> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-menu"> <line x1="3" y1="12" x2="21" y2="12"></line><line x1="3" y1="6" x2="21" y2="6"></line><line x1="3" y1="18" x2="21" y2="18"></line> </svg> </symbol> <symbol id="svg-arrow-right" viewBox="0 0 24 24"> <title>Expand</title> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-chevron-right"> <polyline points="9 18 15 12 9 6"></polyline> </svg> </symbol> <symbol id="svg-doc" viewBox="0 0 24 24"> <title>Document</title> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file"> <path d="M13 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9z"></path><polyline points="13 2 13 9 20 9"></polyline> </svg> </symbol> </svg> <div class="side-bar"> <div class="site-header"> <a href="/" class="site-title lh-tight"> </a> <a href="#" id="menu-button" class="site-button"> <svg viewBox="0 0 24 24" class="icon"><use xlink:href="#svg-menu"></use></svg> </a> </div> <nav role="navigation" aria-label="Main" id="site-nav" class="site-nav"> <ul class="nav-list"><li class="nav-list-item"><a href="/" class="nav-list-link">Home</a></li><li class="nav-list-item"><a href="/git/" class="nav-list-link">GitHub Setup</a></li><li class="nav-list-item"><a href="/build/" class="nav-list-link">Building Scotty3D</a></li><li class="nav-list-item"><a href="#" class="nav-list-expander"><svg viewBox="0 0 24 24"><use xlink:href="#svg-arrow-right"></use></svg></a><a href="/guide/" class="nav-list-link">User Guide</a><ul class="nav-list "><li class="nav-list-item "><a href="/guide/animate_mode/" class="nav-list-link">Animate</a></li><li class="nav-list-item "><a href="/guide/layout_mode/" class="nav-list-link">Layout</a></li><li class="nav-list-item "><a href="/guide/model_mode/" class="nav-list-link">Model</a></li><li class="nav-list-item "><a href="/guide/render_mode/" class="nav-list-link">Render</a></li><li class="nav-list-item "><a href="/guide/rigging_mode/" class="nav-list-link">Rig</a></li><li class="nav-list-item "><a href="/guide/simulate_mode/" class="nav-list-link">Simulate</a></li></ul></li><li class="nav-list-item"><a href="#" class="nav-list-expander"><svg viewBox="0 0 24 24"><use xlink:href="#svg-arrow-right"></use></svg></a><a href="/meshedit/" class="nav-list-link">A2: MeshEdit</a><ul class="nav-list "><li class="nav-list-item "><a href="/meshedit/halfedge" class="nav-list-link">Halfedge Mesh</a></li><li class="nav-list-item "><a href="#" class="nav-list-expander"><svg viewBox="0 0 24 24"><use xlink:href="#svg-arrow-right"></use></svg></a><a href="/meshedit/local/" class="nav-list-link">Local Operations</a><ul class="nav-list"><li class="nav-list-item "> <a href="/meshedit/local/edge_flip" class="nav-list-link">Edge Flip Tutorial</a> </li><li class="nav-list-item "> <a href="/meshedit/local/bevel/" class="nav-list-link">Bevelling</a> </li></ul></li><li class="nav-list-item "><a href="#" class="nav-list-expander"><svg viewBox="0 0 24 24"><use xlink:href="#svg-arrow-right"></use></svg></a><a href="/meshedit/global/" class="nav-list-link">Global Operations</a><ul class="nav-list"><li class="nav-list-item "> <a href="/meshedit/global/catmull/" class="nav-list-link">Catmull-Clark Subdivision</a> </li><li class="nav-list-item "> <a href="/meshedit/global/remesh/" class="nav-list-link">Isotropic Remeshing</a> </li><li class="nav-list-item "> <a href="/meshedit/global/linear/" class="nav-list-link">Linear Subdivision</a> </li><li class="nav-list-item "> <a href="/meshedit/global/loop/" class="nav-list-link">Loop Subdivision</a> </li><li class="nav-list-item "> <a href="/meshedit/global/simplify/" class="nav-list-link">Simplification</a> </li><li class="nav-list-item "> <a href="/meshedit/global/triangulate/" class="nav-list-link">Triangulation</a> </li></ul></li></ul></li><li class="nav-list-item"><a href="#" class="nav-list-expander"><svg viewBox="0 0 24 24"><use xlink:href="#svg-arrow-right"></use></svg></a><a href="/pathtracer/" class="nav-list-link">A3: Pathtracer</a><ul class="nav-list "><li class="nav-list-item "><a href="/pathtracer/camera_rays" class="nav-list-link">(Task 1) Camera Rays</a></li><li class="nav-list-item "><a href="#" class="nav-list-expander"><svg viewBox="0 0 24 24"><use xlink:href="#svg-arrow-right"></use></svg></a><a href="/pathtracer/intersecting_objects" class="nav-list-link">(Task 2) Intersections</a><ul class="nav-list"><li class="nav-list-item "> <a href="/pathtracer/ray_triangle_intersection" class="nav-list-link">Ray Triangle Intersection</a> </li><li class="nav-list-item "> <a href="/pathtracer/ray_sphere_intersection" class="nav-list-link">Ray Sphere Intersection</a> </li></ul></li><li class="nav-list-item "><a href="/pathtracer/bounding_volume_hierarchy" class="nav-list-link">(Task 3) BVH</a></li><li class="nav-list-item "><a href="/pathtracer/shadow_rays" class="nav-list-link">(Task 4) Shadow Rays</a></li><li class="nav-list-item "><a href="/pathtracer/path_tracing" class="nav-list-link">(Task 5) Path Tracing</a></li><li class="nav-list-item "><a href="#" class="nav-list-expander"><svg viewBox="0 0 24 24"><use xlink:href="#svg-arrow-right"></use></svg></a><a href="/pathtracer/materials" class="nav-list-link">(Task 6) Materials</a><ul class="nav-list"><li class="nav-list-item "> <a href="/pathtracer/dielectrics_and_transmission" class="nav-list-link">Dielectrics and Transmission</a> </li></ul></li><li class="nav-list-item "><a href="#" class="nav-list-expander"><svg viewBox="0 0 24 24"><use xlink:href="#svg-arrow-right"></use></svg></a><a href="/pathtracer/environment_lighting" class="nav-list-link">(Task 7) Environment Lighting</a><ul class="nav-list"><li class="nav-list-item "> <a href="/pathtracer/importance_sampling" class="nav-list-link">Environment Light Importance Sampling</a> </li></ul></li><li class="nav-list-item "><a href="/pathtracer/visualization_of_normals" class="nav-list-link">Visualization of normals</a></li></ul></li><li class="nav-list-item active"><a href="#" class="nav-list-expander"><svg viewBox="0 0 24 24"><use xlink:href="#svg-arrow-right"></use></svg></a><a href="/animation/" class="nav-list-link">A4: Animation</a><ul class="nav-list "><li class="nav-list-item "><a href="/animation/splines" class="nav-list-link">Splines</a></li><li class="nav-list-item active"><a href="/animation/skeleton_kinematics" class="nav-list-link active">Skeleton Kinematics</a></li><li class="nav-list-item "><a href="/animation/skinning" class="nav-list-link">Skinning</a></li><li class="nav-list-item "><a href="/animation/particles" class="nav-list-link">Particles</a></li></ul></li></ul> </nav> <footer class="site-footer"> This site uses <a href="https://github.com/pmarsceill/just-the-docs">Just the Docs</a>, a documentation theme for Jekyll. </footer> </div> <div class="main" id="top"> <div id="main-header" class="main-header"> <div class="search"> <div class="search-input-wrap"> <input type="text" id="search-input" class="search-input" tabindex="0" placeholder="Search " aria-label="Search " autocomplete="off"> <label for="search-input" class="search-label"><svg viewBox="0 0 24 24" class="search-icon"><use xlink:href="#svg-search"></use></svg></label> </div> <div id="search-results" class="search-results"></div> </div> </div> <div id="main-content-wrap" class="main-content-wrap"> <nav aria-label="Breadcrumb" class="breadcrumb-nav"> <ol class="breadcrumb-nav-list"> <li class="breadcrumb-nav-list-item"><a href="/animation/">A4: Animation</a></li> <li class="breadcrumb-nav-list-item"><span>Skeleton Kinematics</span></li> </ol> </nav> <div id="main-content" class="main-content" role="main"> <h1 id="skeleton-kinematics"> <a href="#skeleton-kinematics" class="anchor-heading" aria-labelledby="skeleton-kinematics"><svg viewBox="0 0 16 16" aria-hidden="true"><use xlink:href="#svg-link"></use></svg></a> Skeleton Kinematics </h1> <p>A <code class="language-plaintext highlighter-rouge">Skeleton</code>(defined in <code class="language-plaintext highlighter-rouge">scene/skeleton.h</code>) is what we use to drive our animation. You can think of them like the set of bones we have in our own bodies and joints that connect these bones. For convenience, we have merged the bones and joints into the <code class="language-plaintext highlighter-rouge">Joint</code> class which holds the orientation of the joint relative to its parent as euler angle in its <code class="language-plaintext highlighter-rouge">pose</code>, and <code class="language-plaintext highlighter-rouge">extent</code> representing the direction and length of the bone with respect to its parent <code class="language-plaintext highlighter-rouge">Joint</code>. Each <code class="language-plaintext highlighter-rouge">Mesh</code> has an associated <code class="language-plaintext highlighter-rouge">Skeleton</code> class which holds a rooted tree of <code class="language-plaintext highlighter-rouge">Joint</code>s, where each <code class="language-plaintext highlighter-rouge">Joint</code> can have an arbitrary number of children.</p> <p>All of our joints are ball <code class="language-plaintext highlighter-rouge">Joint</code>s which have a set of 3 rotations around the <img src="task2_media/0027.png" style="height:14px" />, <img src="task2_media/0028.png" style="height: 16px" />, and <img src="task2_media/0029.png" style="height: 16px" /> axes, called <em>Euler angles</em>. Whenever you deal with angles in this way, a fixed order of operations must be enforced, otherwise the same set of angles will not represent the same rotation. In order to get the full rotational transformation matrix, <img src="task2_media/0030.png" style="height:16px" />, we can create individual rotation matrices around the <img src="task2_media/0031.png" style="height:16px" />, <img src="task2_media/0032.png" style="height:16px" />, and <img src="task2_media/0033.png" style="height:16px" /> axes, which we call <img src="task2_media/0034.png" style="height:20px" />, <img src="task2_media/0035.png" style="height:20px" />, and <img src="task2_media/0036.png" style="height:20px" /> respectively. The particular order of operations that we adopted for this assignment is that <img src="task2_media/0037.png" style="height:20px" />.</p> <h3 id="forward-kinematics"> <a href="#forward-kinematics" class="anchor-heading" aria-labelledby="forward-kinematics"><svg viewBox="0 0 16 16" aria-hidden="true"><use xlink:href="#svg-link"></use></svg></a> Forward Kinematics </h3> <p><em>Note: These diagrams are in 2D for visual clarity, but we will work with a 3D kinematic skeleton.</em></p> <p>When a joint’s parent is rotated, that transformation should be propagated down to all of its children. In the diagram below, <img src="task2_media/0038.png" style="height:18px" /> is the parent of <img src="task2_media/0039.png" style="height:18px" /> and <img src="task2_media/0040.png" style="height:18px" /> is the parent of <img src="task2_media/0041.png" style="height:18px" />. When a translation of <img src="task2_media/0042.png" style="height:18px" /> and rotation of <img src="task2_media/0043.png" style="height:18px" /> is applied to <img src="task2_media/0044.png" style="height:18px" />, all of the descendants are affected by this transformation as well. Then, <img src="task2_media/0045.png" style="height:18px" /> is rotated by <img src="task2_media/0046.png" style="height:18px" /> which affects itself and <img src="task2_media/0047.png" style="height:18px" />. Finally, when rotation of <img src="task2_media/0048.png" style="height:18px" /> is applied to <img src="task2_media/0049.png" style="height:18px" />, it only affects itself because it has no children.</p> <center><img src="task2_media/forward_kinematic_diagram.jpg" style="height:480px" /></center> <p>You need to implement these routines in <code class="language-plaintext highlighter-rouge">student/skeleton.cpp</code> for forward kinematics.</p> <ul> <li><code class="language-plaintext highlighter-rouge">Joint::joint_to_bind</code> Rreturn a matrix transforming points in the space of this joint to points in mesh space in bind position up to the base of this joint (end of its parent joint). You should traverse upwards from this joint’s parent all the way up to the root joint and accumulate their transformations.</li> <li><code class="language-plaintext highlighter-rouge">Joint::joint_to_posed</code> Return a matrix transforming points in the space of this joint to points in mesh space, taking into account joint poses. Again, you should traverse upwards from this joint’s parent to the root joint.</li> <li><code class="language-plaintext highlighter-rouge">Skeleton::end_of</code> Returns the end position of the joint in world coordinate frame, and you should take into account the base position of the skeleton (<code class="language-plaintext highlighter-rouge">Skeleton::base_pos</code>).</li> <li><code class="language-plaintext highlighter-rouge">Skeleton::posed_end_of</code> Returns the end position of the joint in world coordinate frame with poses, and you should take into account <code class="language-plaintext highlighter-rouge">Skeleton::base_pos</code>.</li> <li><code class="language-plaintext highlighter-rouge">Skeleton::joint_to_bind</code> Rreturn a matrix transforming points in the space of this joint to points in mesh space in bind position but with the base position of the skeleton taken in to account. Hint: use some function that you have implemented wisely!</li> <li><code class="language-plaintext highlighter-rouge">Skeleton::joint_to_posed</code> Return a matrix transforming points in the space of this joint to points in mesh space, taking into account joint poses but with the base position of the skeleton taken in to account. Hint: use some function that you have implemented wisely!</li> </ul> <p>Once you have implemented these basic kinematics, you should be able to define skeletons, set their positions at a collection of keyframes, and watch the skeleton smoothly interpolate the motion (see the <a href="../guide/animate.md">user guide</a> for an explanation of the interface). The gif below shows a very hasty demo defining a few joints and interpolating their motion.</p> <center><img src="task2_media/gif1.gif" /></center> <center><img src="task2_media/gif2.gif" /></center> <p>Note that the skeleton does not yet influence the geometry of the cube in this scene – that will come in Task 3!</p> <h3 id="task-2b---inverse-kinematics"> <a href="#task-2b---inverse-kinematics" class="anchor-heading" aria-labelledby="task-2b---inverse-kinematics"><svg viewBox="0 0 16 16" aria-hidden="true"><use xlink:href="#svg-link"></use></svg></a> Task 2b - Inverse Kinematics </h3> <h3 id="single-target-ik"> <a href="#single-target-ik" class="anchor-heading" aria-labelledby="single-target-ik"><svg viewBox="0 0 16 16" aria-hidden="true"><use xlink:href="#svg-link"></use></svg></a> Single Target IK </h3> <p>Now that we have a logical way to move joints around, we can implement Inverse Kinematics, which will move the joints around in order to reach a target point. There are a few different ways we can do this, but for this assignment we’ll implement an iterative method called gradient descent in order to find the minimum of a function. For a function <img src="task2_media/0050.png" style="height:18px" />, we’ll have the update scheme:</p> <center><img src="task2_media/0051.png" style="height:26px" /></center> <p>Where <img src="task2_media/0052.png" style="height:14px" /> is a small timestep. For this task, we’ll be using gradient descent to find the minimum of the cost function:</p> <center><img src="task2_media/0053.png" style="height:50px" /></center> <p>Where <img src="task2_media/0054.png" style="height:24px" /> is the position in world space of the target joint, and <img src="task2_media/0055.png" style="height:18px" /> is the position in world space of the target point. More specifically, we’ll be using a technique called Jacobian Transpose, which relies on the assumption:</p> <center><img src="task2_media/0056.png" style="height:30px" /></center> <p>Where:</p> <ul> <li><img src="task2_media/0057.png" style="height:20px" /> (n x 1) is the function <img src="task2_media/0058.png" style="height:22px" />, where <img src="task2_media/0059.png" style="height:22px" /> is the angle of joint <img src="task2_media/0060.png" style="height:18px" /> around the axis of rotation</li> <li><img src="task2_media/0061.png" style="height:16px" /> is a constant</li> <li><img src="task2_media/0062.png" style="height:22px" /> (3 x n) is the Jacobian of <img src="task2_media/0063.png" style="height:18px" /></li> </ul> <p>Note that here <img src="task2_media/0064.png" style="height:14px" /> refers to the number of joints in the skeleton. Although in reality this can be reduced to just the number of joints between the target joint and the root, inclusive, because all joints not on that path should stay where they are, so their columns in <img src="task2_media/0065.png" style="height:20px" /> will be 0. So <img src="task2_media/0066.png" style="height:14px" /> can just be the number of joints between the target and the root, inclusive. Additionally note that since this will get multiplied by <img src="task2_media/0067.png" style="height:16px" /> anyways, you can ignore the value of <img src="task2_media/0068.png" style="height:14px" />, and just consider the timestep as <img src="task2_media/0069.png" style="height:16px" />.</p> <p>Now we just need a way to calcluate the Jacobian of <img src="task2_media/0070.png" style="height:16px" />. For this, we can use the fact that:</p> <center><img src="task2_media/0071.png" style="height:34px" /></center> <p>Where:</p> <ul> <li><img src="task2_media/0072.png" style="height:24px" /> is the <img src="task2_media/0073.png" style="height:24px" /> column of <img src="task2_media/0074.png" style="height:24px" /></li> <li><img src="task2_media/0075.png" style="height:24px" /> is the axis of rotation</li> <li><img src="task2_media/0076.png" style="height:24px" /> is the vector from the base of joint <img src="task2_media/0077.png" style="height:24px" /> to the end point of the target joint</li> </ul> <p>For a more in-depth derivation of Jacobian transpose (and a look into other inverse kinematics algorithms), please check out <a href="https://web.archive.org/web/20190501035728/https://autorob.org/lectures/autorob_11_ik_jacobian.pdf">this presentation</a>. (Pages 45-56 in particular)</p> <p>Now, all of this will work for updating the angle along a single axis, but we have 3 axes to deal with. Luckily, extending it to 3 dimensions isn’t very difficult, we just need to update the angle along each axis independently.</p> <h3 id="multi-target"> <a href="#multi-target" class="anchor-heading" aria-labelledby="multi-target"><svg viewBox="0 0 16 16" aria-hidden="true"><use xlink:href="#svg-link"></use></svg></a> Multi-Target </h3> <p>We’ll extend this so we can have multiple targets, which will then use the function to minimize:</p> <center><img src="task2_media/0078.png" /></center> <p>which is a simple extension actually. Since each term is independent and added together, we can get the gradient of this new cost function just by summing the gradients of each of the constituent cost functions!</p> <p>You should implement multi-target IK, which will take a <code class="language-plaintext highlighter-rouge">vector</code> of <code class="language-plaintext highlighter-rouge">IK_Handle*</code>s called <code class="language-plaintext highlighter-rouge">active_handles</code> which stores the information a target point for a joint. See <code class="language-plaintext highlighter-rouge">scene/skeleton.h</code> for the definition of <code class="language-plaintext highlighter-rouge">IK_Handle</code> structure.</p> <p>In order to implement this, you should update <code class="language-plaintext highlighter-rouge">Joint::compute_gradient</code> and <code class="language-plaintext highlighter-rouge">Skeleton::step_ik</code>. <code class="language-plaintext highlighter-rouge">Joint::compute_gradient</code> should calculate the gradient of <img src="task2_media/0079.png" style="height:18px" /> in the x,y, and z directions, and add them to <code class="language-plaintext highlighter-rouge">Joint::angle_gradient</code> for all relevant joints. <code class="language-plaintext highlighter-rouge">Skeleton::step_ik</code> should actually do the gradient descent calculations and update the <code class="language-plaintext highlighter-rouge">pose</code> of each joint. In this function, you should probably use a very small timestep, but do several iterations (say, 10s to 100s) of gradient descent in order to speed things up. For even faster and better results, you can also implement a variable timestep instead of just using a fixed one. Note also that the root joint should never be updated.</p> <p>A key thing for this part is to <em>remember what coordinate frame you’re in</em>, because if you calculate the gradients in the wrong coordinate frame or use the axis of rotation in the wrong coordinate frame your answers will come out very wrong!</p> <h3 id="using-your-ik"> <a href="#using-your-ik" class="anchor-heading" aria-labelledby="using-your-ik"><svg viewBox="0 0 16 16" aria-hidden="true"><use xlink:href="#svg-link"></use></svg></a> Using your IK! </h3> <p>Once you have IK implemented, you should be able to create a series of joints, and get a particular joint to move to the desired final position you have selected.</p> </div> </div> <div class="search-overlay"></div> </div> </body> </html>