A `Skeleton`(defined in `scene/skeleton.h`) 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 `Joint` class which holds the orientation of the joint relative to its parent as euler angle in its `pose`, and `extent` representing the direction and length of the bone with respect to its parent `Joint`. Each `Mesh` has an associated `Skeleton` class which holds a rooted tree of `Joint`s, where each `Joint` can have an arbitrary number of children.
A `Skeleton`(defined in `scene/skeleton.h`) 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 `Joint` class which holds the orientation of the joint relative to its parent as euler angle in its `pose`, and `extent` representing the direction and length of the bone with respect to its parent `Joint`. Each `Mesh` has an associated `Skeleton` class which holds a rooted tree of `Joint`s, where each `Joint` can have an arbitrary number of children.
All of our joints are ball `Joint`s which have a set of 3 rotations around the <imgsrc="task2_media/0027.png"height="20">, <imgsrc="task2_media/0028.png"height="20">, and <imgsrc="task2_media/0029.png"height="20"> axes, called _Euler angles_. 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, <imgsrc="task2_media/0030.png"height="20">, we can create individual rotation matrices around the<imgsrc="task2_media/0031.png"height="20">, <imgsrc="task2_media/0032.png"height="20">, and <imgsrc="task2_media/0033.png"height="20"> axes, which we call <imgsrc="task2_media/0034.png"height="20">, <imgsrc="task2_media/0035.png"height="20">, and <imgsrc="task2_media/0036.png"height="20"> respectively. The particular order of operations that we adopted for this assignment is that <imgsrc="task2_media/0037.png"height="20">.
All of our joints are ball `Joint`s which have a set of 3 rotations around the <imgsrc="task2_media/0027.png"style="height:14px">, <imgsrc="task2_media/0028.png"style="height: 16px">, and <imgsrc="task2_media/0029.png"style="height: 16px"> axes, called _Euler angles_. 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, <imgsrc="task2_media/0030.png"style="height:16px">, we can create individual rotation matrices around the<imgsrc="task2_media/0031.png"style="height:16px">, <imgsrc="task2_media/0032.png"style="height:16px">, and <imgsrc="task2_media/0033.png"style="height:16px"> axes, which we call <imgsrc="task2_media/0034.png"style="height:20px">, <imgsrc="task2_media/0035.png"style="height:20px">, and <imgsrc="task2_media/0036.png"style="height:20px"> respectively. The particular order of operations that we adopted for this assignment is that <imgsrc="task2_media/0037.png"style="height:20px">.
### Forward Kinematics
### Forward Kinematics
_Note: These diagrams are in 2D for visual clarity, but we will work with a 3D kinematic skeleton._
_Note: These diagrams are in 2D for visual clarity, but we will work with a 3D kinematic skeleton._
When a joint's parent is rotated, that transformation should be propagated down to all of its children. In the diagram below, <imgsrc="task2_media/0038.png"height="20"> is the parent of <imgsrc="task2_media/0039.png"height="20"> and <imgsrc="task2_media/0040.png"height="20"> is the parent of <imgsrc="task2_media/0041.png"height="20">. When a translation of <imgsrc="task2_media/0042.png"height="20"> and rotation of <imgsrc="task2_media/0043.png"height="20"> is applied to <imgsrc="task2_media/0044.png"height="20">, all of the descendants are affected by this transformation as well. Then, <imgsrc="task2_media/0045.png"height="20"> is rotated by <imgsrc="task2_media/0046.png"height="20"> which affects itself and <imgsrc="task2_media/0047.png"height="20">. Finally, when rotation of <imgsrc="task2_media/0048.png"height="20"> is applied to <imgsrc="task2_media/0049.png"height="20">, it only affects itself because it has no children.
When a joint's parent is rotated, that transformation should be propagated down to all of its children. In the diagram below, <imgsrc="task2_media/0038.png"style="height:18px"> is the parent of <imgsrc="task2_media/0039.png"style="height:18px"> and <imgsrc="task2_media/0040.png"style="height:18px"> is the parent of <imgsrc="task2_media/0041.png"style="height:18px">. When a translation of <imgsrc="task2_media/0042.png"style="height:18px"> and rotation of <imgsrc="task2_media/0043.png"style="height:18px"> is applied to <imgsrc="task2_media/0044.png"style="height:18px">, all of the descendants are affected by this transformation as well. Then, <imgsrc="task2_media/0045.png"style="height:18px"> is rotated by <imgsrc="task2_media/0046.png"style="height:18px"> which affects itself and <imgsrc="task2_media/0047.png"style="height:18px">. Finally, when rotation of <imgsrc="task2_media/0048.png"style="height:18px"> is applied to <imgsrc="task2_media/0049.png"style="height:18px">, it only affects itself because it has no children.
You need to implement these routines in `student/skeleton.cpp` for forward kinematics.
You need to implement these routines in `student/skeleton.cpp` for forward kinematics.
*`Joint::joint_to_bind`
*`Joint::joint_to_bind`
Rreturn a matrix transforming points in the space of this joint
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.
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.
*`Joint::joint_to_posed`
*`Joint::joint_to_posed`
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.
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.
*`Skeleton::end_of`
*`Skeleton::end_of`
Returns the end position of the joint in world coordinate frame, and you should take into account the base position of the skeleton (`Skeleton::base_pos`).
Returns the end position of the joint in world coordinate frame, and you should take into account the base position of the skeleton (`Skeleton::base_pos`).
*`Skeleton::posed_end_of`
*`Skeleton::posed_end_of`
Returns the end position of the joint in world coordinate frame with poses, and you should take into account `Skeleton::base_pos`.
Returns the end position of the joint in world coordinate frame with poses, and you should take into account `Skeleton::base_pos`.
*`Skeleton::joint_to_bind`
*`Skeleton::joint_to_bind`
Rreturn a matrix transforming points in the space of this joint
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!
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!
*`Skeleton::joint_to_posed`
*`Skeleton::joint_to_posed`
...
@@ -37,68 +39,64 @@ You need to implement these routines in `student/skeleton.cpp` for forward kinem
...
@@ -37,68 +39,64 @@ You need to implement these routines in `student/skeleton.cpp` for forward kinem
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 [user guide](../guide/animate.md) for an explanation of the interface). The gif below shows a very hasty demo defining a few joints and interpolating their motion.
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 [user guide](../guide/animate.md) for an explanation of the interface). The gif below shows a very hasty demo defining a few joints and interpolating their motion.
![gif1](task2_media/gif1.gif)
<center><imgsrc="task2_media/gif1.gif"></center>
<center><imgsrc="task2_media/gif2.gif"></center>
![gif1](task2_media/gif2.gif)
Note that the skeleton does not yet influence the geometry of the cube in this scene -- that will come in Task 3!
Note that the skeleton does not yet influence the geometry of the cube in this scene -- that will come in Task 3!
### Task 2b - Inverse Kinematics
### Task 2b - Inverse Kinematics
#### Single Target IK
### Single Target IK
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 <imgsrc="task2_media/0050.png"height="20">, we'll have the update scheme:
<imgsrc="task2_media/0051.png"height="20">
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 <imgsrc="task2_media/0050.png"style="height:18px">, we'll have the update scheme:
Where <imgsrc="task2_media/0052.png"height="9"> is a small timestep. For this task, we'll be using gradient descent to find the minimum of the cost function:
Where <imgsrc="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:
Where <imgsrc="task2_media/0054.png"height="20"> is the position in world space of the target joint, and <imgsrc="task2_media/0055.png"height="20"> 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 that:
Where <imgsrc="task2_media/0054.png"style="height:24px"> is the position in world space of the target joint, and <imgsrc="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:
*<imgsrc="task2_media/0057.png"height="14"> (n x 1) is the function <imgsrc="task2_media/0058.png"height="19">, where <imgsrc="task2_media/0059.png"height="19"> is the angle of joint <imgsrc="task2_media/0060.png"height="14"> around the axis of rotation
*<imgsrc="task2_media/0057.png"style="height:20px"> (n x 1) is the function <imgsrc="task2_media/0058.png"style="height:22px">, where <imgsrc="task2_media/0059.png"style="height:22px"> is the angle of joint <imgsrc="task2_media/0060.png"style="height:18px"> around the axis of rotation
*<imgsrc="task2_media/0061.png"height="9"> is a constant
*<imgsrc="task2_media/0061.png"style="height:16px"> is a constant
*<imgsrc="task2_media/0062.png"height="16"> (3 x n) is the Jacobian of <imgsrc="task2_media/0063.png"height="14">
*<imgsrc="task2_media/0062.png"style="height:22px"> (3 x n) is the Jacobian of <imgsrc="task2_media/0063.png"style="height:18px">
Note that here <imgsrc="task2_media/0064.png"height="9"> 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 <imgsrc="task2_media/0065.png"height="16"> will be 0\. So <imgsrc="task2_media/0066.png"height="9"> can just be the number of joints between the target and the root, inclusive. Additionally note that since this will get multiplied by <imgsrc="task2_media/0067.png"height="9"> anyways, you can ignore the value of <imgsrc="task2_media/0068.png"height="9">, and just consider the timestep as <imgsrc="task2_media/0069.png"height="18">.
Now we just need a way to calcluate the Jacobian of<imgsrc="task2_media/0070.png"height="14">. For this, we can use the fact that:
Note that here <imgsrc="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 <imgsrc="task2_media/0065.png"style="height:20px"> will be 0\. So<imgsrc="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 <imgsrc="task2_media/0067.png"style="height:16px"> anyways, you can ignore the value of <imgsrc="task2_media/0068.png"style="height:14px">, and just consider the timestep as <imgsrc="task2_media/0069.png"style="height:16px">.
<imgsrc="task2_media/0071.png"height="20">
Now we just need a way to calcluate the Jacobian of <imgsrc="task2_media/0070.png"style="height:16px">. For this, we can use the fact that:
*<imgsrc="task2_media/0072.png"height="16"> is the <imgsrc="task2_media/0073.png"height="16"> column of <imgsrc="task2_media/0074.png"height="19">
*<imgsrc="task2_media/0072.png"style="height:24px"> is the <imgsrc="task2_media/0073.png"style="height:24px"> column of <imgsrc="task2_media/0074.png"style="height:24px">
*<imgsrc="task2_media/0075.png"height="14"> is the axis of rotation
*<imgsrc="task2_media/0075.png"style="height:24px"> is the axis of rotation
*<imgsrc="task2_media/0076.png"height="16"> is the vector from the base of joint <imgsrc="task2_media/0077.png"height="14"> to the end point of the target joint
*<imgsrc="task2_media/0076.png"style="height:24px"> is the vector from the base of joint <imgsrc="task2_media/0077.png"style="height:24px"> to the end point of the target joint
For a more in-depth derivation of Jacobian transpose (and a look into other inverse kinematics algorithms), please check out [this presentation](https://web.archive.org/web/20190501035728/https://autorob.org/lectures/autorob_11_ik_jacobian.pdf). (Pages 45-56 in particular)
For a more in-depth derivation of Jacobian transpose (and a look into other inverse kinematics algorithms), please check out [this presentation](https://web.archive.org/web/20190501035728/https://autorob.org/lectures/autorob_11_ik_jacobian.pdf). (Pages 45-56 in particular)
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.
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.
#### Multi-Target
### Multi-Target
We'll extend this so we can have multiple targets, which will then use the function to minimize:
We'll extend this so we can have multiple targets, which will then use the function to minimize:
<imgsrc="task2_media/0078.png">
<center><imgsrc="task2_media/0078.png"></center>
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!
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!
You should implement multi-target IK, which will take a `vector` of `IK_Handle*`s called `active_handles` which stores the information a target point for a joint. See `scene/skeleton.h` for the definition of `IK_Handle` structure.
You should implement multi-target IK, which will take a `vector` of `IK_Handle*`s called `active_handles` which stores the information a target point for a joint. See `scene/skeleton.h` for the definition of `IK_Handle` structure.
In order to implement this, you should update `Joint::compute_gradient` and `Skeleton::step_ik`. `Joint::compute_gradient` should calculate the gradient of <imgsrc="task2_media/0079.png"height="14"> in the x,y, and z directions, and add them to `Joint::angle_gradient` for all relevant joints. `Skeleton::step_ik` should actually do the gradient descent calculations and update the `pose` 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.
In order to implement this, you should update `Joint::compute_gradient` and `Skeleton::step_ik`. `Joint::compute_gradient` should calculate the gradient of <imgsrc="task2_media/0079.png"style="height:18px"> in the x,y, and z directions, and add them to `Joint::angle_gradient` for all relevant joints. `Skeleton::step_ik` should actually do the gradient descent calculations and update the `pose` 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.
A key thing for this part is to _remember what coordinate frame you're in_, 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!
A key thing for this part is to _remember what coordinate frame you're in_, 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!
#### Using your IK!
### Using your IK!
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.
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.
Now that we have a skeleton set up, we need to link the skeleton to the mesh in order to get the mesh to follow the movements of the skeleton. We will implement linear blend skinning using the following functions: `Skeleton::skin()`, `Skeleton::find_joints()`, and `closest_on_line_segment`.
Now that we have a skeleton set up, we need to link the skeleton to the mesh in order to get the mesh to follow the movements of the skeleton. We will implement linear blend skinning using the following functions: `Skeleton::skin()`, `Skeleton::find_joints()`, and `closest_on_line_segment`.
The easiest way to do this is to update each of mesh vertices' positions in relation to the bones (Joints) in the skeleton. There are 3 types of coordinate spaces: bind, joint, and pose. Bind is the initial coordinate frame of the vertices of where they are bound to relative to the mesh. Joint is the position of the vertex relative to a given joint. Pose is the world-spacce position after the joint transforms have been applied. You'll want to compute transforms that take vertices in bind space and convert them to posed space (Hint: `joint_to_bind`, `joint_to_posed`, and `inverse()` will come in handy.)
The easiest way to do this is to update each of mesh vertices' positions in relation to the bones (Joints) in the skeleton. There are 3 types of coordinate spaces: bind, joint, and pose. Bind is the initial coordinate frame of the vertices of where they are bound to relative to the mesh. Joint is the position of the vertex relative to a given joint. Pose is the world-space position after the joint transforms have been applied. You'll want to compute transforms that take vertices in bind space and convert them to posed space (Hint: `joint_to_bind`, `joint_to_posed`, and `inverse()` will come in handy.)
Your implementation should have the following basic steps for each vertex:
Your implementation should have the following basic steps for each vertex:
...
@@ -16,14 +18,19 @@ Your implementation should have the following basic steps for each vertex:
...
@@ -16,14 +18,19 @@ Your implementation should have the following basic steps for each vertex:
- Compute the vertex's position with respect to each joint j in the skeleton in j's coordinate frame when no transformations have been applied to the skeleton (bind pose, vertex bind position).
- Compute the vertex's position with respect to each joint j in the skeleton in j's coordinate frame when no transformations have been applied to the skeleton (bind pose, vertex bind position).
- Find where this vertex would end up (in world coordinates) if it were transformed along with bone j.
- Find where this vertex would end up (in world coordinates) if it were transformed along with bone j.
- Find the closest point on joint j's bone segment (axis) and compute the distance to this closest point (Hint: `closest_on_line_segment` might come in handy).
- Find the closest point on joint j's bone segment (axis) and compute the distance to this closest point (Hint: `closest_on_line_segment` might come in handy).
- Compute the resulting position of the vertex by doing a weighted average of the bind-to-posed transforms from each bone and applying it to the vertex. The weights for the weighted average should be the inverse distance to the joint, so closer bones have a stronger influence.
- Compute the resulting position of the vertex by doing a weighted average of the bind-to-posed transforms from each bone and applying it to the vertex. The weights for the weighted average should be the inverse distance to the joint, so closer bones have a stronger influence.
Below we have an equation representation. The ith vertex v is the new vertex position. The weight w is the weight metric computed as the inverse of distance between the ith vertex and the closest point on joint j. We multiply this term with the position of the ith vertex v with respect to joint j after joint's transformations has been applied.
Below we have an equation representation. The ith vertex v is the new vertex position. The weight w is the weight metric computed as the inverse of distance between the ith vertex and the closest point on joint j. We multiply this term with the position of the ith vertex v with respect to joint j after joint's transformations has been applied.
In Scotty3D, the `Skeleton::skin()` function gets called on every frame draw iteration, recomputing all skinning related quantities. In this function, you should read vertices from `input.verts()` and indices from `input.indices()`, and write the resulting positions and norms to `v.pos` and `v.norm` for every vertex in the input vertices list.
In Scotty3D, the `Skeleton::skin()` function gets called on every frame draw iteration, recomputing all skinning related quantities. In this function, you should read vertices from `input.verts()` and indices from `input.indices()`, and write the resulting positions and norms to `v.pos` and `v.norm` for every vertex in the input vertices list.
You will be implementing a Capsul-Radius Linear Blend Skin method, which only moves vertices with a joint if they lie in the joint's radius. The `Skeleton::skin()` function also takes in a `map` of vertex index to relevant joints that you must compute the above distance/transformation metrics on. You are also responsible for creating this `map`, which is done so in `Skeleton::find_joints()`. Don't worry about calling this function, it is called automatically before skin is called, populating the `map` field and sending it over to the `skin()` function. Your `Skeleton::find_joints()` implementation should iterate over all the vertices and add joint j to vertex index i in the map if the distance between the vertex and joint is less than `j->radius` (remember make sure they're both in the same coordinate frame.)
You will be implementing a Capsule-Radius Linear Blend Skin method, which only moves vertices with a joint if they lie in the joint's radius. The `Skeleton::skin()` function also takes in a `map` of vertex index to relevant joints that you must compute the above distance/transformation metrics on. You are also responsible for creating this `map`, which is done so in `Skeleton::find_joints()`. Don't worry about calling this function, it is called automatically before skin is called, populating the `map` field and sending it over to the `skin()` function. Your `Skeleton::find_joints()` implementation should iterate over all the vertices and add joint j to vertex index i in the map if the distance between the vertex and joint is less than `j->radius` (remember make sure they're both in the same coordinate frame.)
As we discussed in class, data points in time can be interpolated by constructing an approximating piecewise polynomial or spline. In this assignment you will implement a particular kind of spline, called a Catmull-Rom spline. A Catmull-Rom spline is a piecewise cubic spline defined purely in terms of the points it interpolates. It is a popular choice in real animation systems, because the animator does not need to define additional data like tangents, etc. (However, your code may still need to numerically evaluate these tangents after the fact; more on this point later.) All of the methods relevant to spline interpolation can be found in `spline.h` with implementations in `spline.inl`.
As we discussed in class, data points in time can be interpolated by constructing an approximating piecewise polynomial or spline. In this assignment you will implement a particular kind of spline, called a Catmull-Rom spline. A Catmull-Rom spline is a piecewise cubic spline defined purely in terms of the points it interpolates. It is a popular choice in real animation systems, because the animator does not need to define additional data like tangents, etc. (However, your code may still need to numerically evaluate these tangents after the fact; more on this point later.) All of the methods relevant to spline interpolation can be found in `spline.h` with implementations in `spline.inl`.
### Task 1a - Hermite Curve over the Unit Interval
### Task 1a - Hermite Curve over the Unit Interval
Recall that a cubic polynomial is a function of the form
Recall that a cubic polynomial is a function of the form:
where <imgsrc="task1_media/0001.png"height="20">, and <imgsrc="task1_media/0002.png"height="20"> are fixed coefficients. However, there are many different ways of specifying a cubic polynomial. In particular, rather than specifying the coefficients directly, we can specify the endpoints and tangents we wish to interpolate. This construction is called the "Hermite form" of the polynomial. In particular, the Hermite form is given by
where <imgsrc="task1_media/0001.png"style="height:24px">, and <imgsrc="task1_media/0002.png"style="height:20px"> are fixed coefficients. However, there are many different ways of specifying a cubic polynomial. In particular, rather than specifying the coefficients directly, we can specify the endpoints and tangents we wish to interpolate. This construction is called the "Hermite form" of the polynomial. In particular, the Hermite form is given by
where <imgsrc="task1_media/0004.png"height="20"> are the endpoint positions, <imgsrc="task1_media/0005.png"height="20"> are the endpoint tangents, and <imgsrc="task1_media/0006.png"height="20"> are the Hermite bases
where <imgsrc="task1_media/0004.png"style="height:16px"> are endpoint positions, <imgsrc="task1_media/0005.png"style="height:16px"> are endpoint tangents, and <imgsrc="task1_media/0006.png"style="height:24px"> are the Hermite bases
Your first task is to implement the method `Spline::cubic_unit_spline()`, which evaluates a spline defined over the time interval <imgsrc="task1_media/0011.png"style="height:24px"> given a pair of endpoints and tangents at endpoints.
<imgsrc="task1_media/0009.png"height="30"><br/>
<imgsrc="task1_media/0010.png"height="30"><br/>
Your first task is to implement the method `Spline::cubic_unit_spline()`, which evaluates a spline defined over the time interval <imgsrc="task1_media/0011.png"height="20"> given a pair of endpoints and tangents at endpoints.
Your basic strategy for implementing this routine should be:
Your basic strategy for implementing this routine should be:
* Evaluate the time, its square, and its cube (for readability, you may want to make a local copy).
* Evaluate the time, its square, and its cube (for readability, you may want to make a local copy).
* Using these values, as well as the position and tangent values, compute the four basis functions <imgsrc="task1_media/0012.png"height="20"><imgsrc="task1_media/0013.png"height="20"> of a cubic polynomial in Hermite form.
* Using these values, as well as the position and tangent values, compute the four basis functions <imgsrc="task1_media/0012.png"style="height:20px"><imgsrc="task1_media/0013.png"style="height:20px"> of a cubic polynomial in Hermite form.
* Finally, combine the endpoint and tangent data using the evaluated bases, and return the result.
* Finally, combine the endpoint and tangent data using the evaluated bases, and return the result.
Notice that this function is templated on a type T. In C++, a templated class can operate on data of a variable type. In the case of a spline, for instance, we want to be able to interpolate all sorts of data: angles, vectors, colors, etc. So it wouldn't make sense to rewrite our spline class once for each of these types; instead, we use templates. In terms of implementation, your code will look no different than if you were operating on a basic type (e.g., doubles). However, the compiler will complain if you try to interpolate a type for which interpolation doesn't make sense! For instance, if you tried to interpolate `Skeleton` objects, the compiler would likely complain that there is no definition for the sum of two skeletons (via a + operator). In general, our spline interpolation will only make sense for data that comes from a vector space, since we need to add T values and take scalar multiples.
Notice that this function is templated on a type T. In C++, a templated class can operate on data of a variable type. In the case of a spline, for instance, we want to be able to interpolate all sorts of data: angles, vectors, colors, etc. So it wouldn't make sense to rewrite our spline class once for each of these types; instead, we use templates. In terms of implementation, your code will look no different than if you were operating on a basic type (e.g., doubles). However, the compiler will complain if you try to interpolate a type for which interpolation doesn't make sense! For instance, if you tried to interpolate `Skeleton` objects, the compiler would likely complain that there is no definition for the sum of two skeletons (via a + operator). In general, our spline interpolation will only make sense for data that comes from a vector space, since we need to add T values and take scalar multiples.
...
@@ -42,32 +42,34 @@ The routine from part 1A just defines the interpolated spline between two points
...
@@ -42,32 +42,34 @@ The routine from part 1A just defines the interpolated spline between two points
The basic idea behind Catmull-Rom is that for a given time t, we first find the four closest knots at times
The basic idea behind Catmull-Rom is that for a given time t, we first find the four closest knots at times
In other words, a reasonable guess for the tangent is given by the difference between neighboring points. (See the Wikipedia and our course slides for more details.)
In other words, a reasonable guess for the tangent is given by the difference between neighboring points. (See the Wikipedia and our course slides for more details.)
This scheme works great if we have two well-defined knots on either side of the query time t. But what happens if we get a query time near the beginning or end of the spline? Or what if the spline contains fewer than four knots? We still have to somehow come up with a reasonable definition for the positions and tangents of the curve at these times. For this assignment, your Catmull-Rom spline interpolation should satisfy the following properties:
This scheme works great if we have two well-defined knots on either side of the query time t. But what happens if we get a query time near the beginning or end of the spline? Or what if the spline contains fewer than four knots? We still have to somehow come up with a reasonable definition for the positions and tangents of the curve at these times. For this assignment, your Catmull-Rom spline interpolation should satisfy the following properties:
* If there are no knots at all in the spline, interpolation should return the default value for the interpolated type. This value can be computed by simply calling the constructor for the type: T(). For instance, if the spline is interpolating Vector3D objects, then the default value will be <imgsrc="task1_media/0017.png"height="20">.
* If there are no knots at all in the spline, interpolation should return the default value for the interpolated type. This value can be computed by simply calling the constructor for the type: T(). For instance, if the spline is interpolating Vector3D objects, then the default value will be <imgsrc="task1_media/0017.png"style="height:20px">.
* If there is only one knot in the spline, interpolation should always return the value of that knot (independent of the time). In other words, we simply have a constant interpolant.
* If there is only one knot in the spline, interpolation should always return the value of that knot (independent of the time). In other words, we simply have a constant interpolant.
* If the query time is less than or equal to the initial knot, return the initial knot's value.
* If the query time is less than or equal to the initial knot, return the initial knot's value.
* If the query time is greater than or equal to the final knot, return the final knot's value.
* If the query time is greater than or equal to the final knot, return the final knot's value.
Once we have two or more knots, interpolation can be handled using general-purpose code. In particular, we can adopt the following "mirroring" strategy to obtain the four knots used in our computation:
Once we have two or more knots, interpolation can be handled using general-purpose code. In particular, we can adopt the following "mirroring" strategy to obtain the four knots used in our computation:
* Any query time between the first and last knot will have at least one knot "to the left" <imgsrc="task1_media/0018.png"height="20"> and one "to the right" <imgsrc="task1_media/0019.png"height="20">.
* Any query time between the first and last knot will have at least one knot "to the left" <imgsrc="task1_media/0018.png"style="height:22px"> and one "to the right" <imgsrc="task1_media/0019.png"style="height:22px">.
* Suppose we don't have a knot "two to the left" <imgsrc="task1_media/0020.png"height="20">. Then we will define a "virtual" knot <imgsrc="task1_media/0021.png"height="20">. In other words, we will "mirror" the difference be observe between <imgsrc="task1_media/0022.png"height="20"> and <imgsrc="task1_media/0023.png"height="20"> to the other side of <imgsrc="task1_media/0024.png"height="20">.
* Suppose we don't have a knot "two to the left" <imgsrc="task1_media/0020.png"style="height:22px">. Then we will define a "virtual" knot <imgsrc="task1_media/0021.png"style="height:22px">. In other words, we will "mirror" the difference be observe between <imgsrc="task1_media/0022.png"style="height:22px"> and <imgsrc="task1_media/0023.png"style="height:22px"> to the other side of <imgsrc="task1_media/0024.png"style="height:22px">.
* Likewise, if we don't have a knot "two to the right" <imgsrc="task1_media/0025.png"height="20">), then we will "mirror" the difference to get a "virtual" knot <imgsrc="task1_media/0026.png"height="20">.
* Likewise, if we don't have a knot "two to the right" <imgsrc="task1_media/0025.png"style="height:22px">), then we will "mirror" the difference to get a "virtual" knot <imgsrc="task1_media/0026.png"style="height:22px">.
* At this point, we have four valid knot values (whether "real" or "virtual"), and can compute our tangents and positions as usual.
* At this point, we have four valid knot values (whether "real" or "virtual"), and can compute our tangents and positions as usual.
* These values are then handed off to our subroutine that computes cubic interpolation over the unit interval.
* These values are then handed off to our subroutine that computes cubic interpolation over the unit interval.
An important thing to keep in mind is that `Spline::cubic_unit_spline()` assumes that the time value t is between 0 and 1, whereas the distance between two knots on our Catmull-Rom spline can be arbitrary. Therefore, when calling this subroutine you will have to normalize t such that it is between 0 and 1, i.e., you will have to divide by the length of the current interval over which you are interpolating. You should think very carefully about how this normalization affects the value computed by the subroutine, in comparison to the values we want to return. A transformation is necessary for both the tangents that you feed in to specify the unit spline.
An important thing to keep in mind is that `Spline::cubic_unit_spline()` assumes that the time value t is between 0 and 1, whereas the distance between two knots on our Catmull-Rom spline can be arbitrary. Therefore, when calling this subroutine you will have to normalize t such that it is between 0 and 1, i.e., you will have to divide by the length of the current interval over which you are interpolating. You should think very carefully about how this normalization affects the value computed by the subroutine, in comparison to the values we want to return. A transformation is necessary for both the tangents that you feed in to specify the unit spline.
Internally, a Spline object stores its data in an STL map that maps knot times to knot values. A nice thing about an STL map is that it automatically keeps knots in sorted order. Therefore, we can quickly access the knot closest to a given time using the method `map::upper_bound()`, which returns an iterator to knot with the smallest time greater than the given query time (you can find out more about this method via online documentation for the Standard Template Library).
Internally, a Spline object stores its data in an STL map that maps knot times to knot values. A nice thing about an STL map is that it automatically keeps knots in sorted order. Therefore, we can quickly access the knot closest to a given time using the method `map::upper_bound()`, which returns an iterator to knot with the smallest time greater than the given query time (you can find out more about this method via online documentation for the Standard Template Library).
...
@@ -81,4 +83,4 @@ Once you have implemented the functions in `spline.cpp`, you should be able to m
...
@@ -81,4 +83,4 @@ Once you have implemented the functions in `spline.cpp`, you should be able to m
@@ -12,7 +13,7 @@ To get a copy of the codebase, see [Git Setup](git).
...
@@ -12,7 +13,7 @@ To get a copy of the codebase, see [Git Setup](git).
Note: the first build on any platform will be very slow, as it must compile most dependencies. Subsequent builds will only need to re-compile your edited Scotty3D code.
Note: the first build on any platform will be very slow, as it must compile most dependencies. Subsequent builds will only need to re-compile your edited Scotty3D code.
### Linux
### Linux
The following packages (ubuntu/debian) are required, as well as CMake and either gcc or clang:
The following packages (ubuntu/debian) are required, as well as CMake and either gcc or clang:
```
```
...
@@ -29,7 +30,7 @@ Finally, to build the project:
...
@@ -29,7 +30,7 @@ Finally, to build the project:
```
```
mkdir build
mkdir build
cd build
cd build
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ..
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ..
make -j4
make -j4
```
```
...
@@ -46,7 +47,7 @@ The windows build is easiest to set up using the Visual Studio compiler (for now
...
@@ -46,7 +47,7 @@ The windows build is easiest to set up using the Visual Studio compiler (for now
You can download CMake for windows [here](https://cmake.org/download/).
You can download CMake for windows [here](https://cmake.org/download/).
Once the Visual Studio compiler (MSVC) is installed, you can access it by running "Developer Command Prompt for VS 2019," which opens a terminal with the utilities in scope. The compiler is called ``cl``. You can also import these utilities in any terminal session by running the script installed at ``C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat``.
Once the Visual Studio compiler (MSVC) is installed, you can access it by running "Developer Command Prompt for VS 2019," which opens a terminal with the utilities in scope. The compiler is called ``cl``. You can also import these utilities in any terminal session by running the script installed at ``C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat``.
We also provide a simple script, ``build_win.bat``, that will automatically import the compiler and build the project. You should be able to simply run it in the project root to build. ``Scotty3D.exe`` will be generated under ``build/RelWithDebInfo/``.
We also provide a simple script, ``build_win.bat``, that will automatically import the compiler and build the project. You should be able to simply run it in the project root to build. ``Scotty3D.exe`` will be generated under ``build/RelWithDebInfo/``.
Please do not use a public github fork of this repository! We do not want solutions to be public. You should work in your own private repo.
Please do not use a public github fork of this repository! We do not want solutions to be public. You should work in your own private repo.
We recommended creating a mirrored private repository with multiple remotes. The following steps go over how to achieve this.
We recommended creating a mirrored private repository with multiple remotes. The following steps go over how to achieve this.
...
@@ -20,7 +22,7 @@ The easiest (but not recommended) way is to download a zip from GitHub and make
...
@@ -20,7 +22,7 @@ The easiest (but not recommended) way is to download a zip from GitHub and make
- When you clone a git repository, the default remote is named 'origin' and set to the URL you cloned from.
- When you clone a git repository, the default remote is named 'origin' and set to the URL you cloned from.
- We will set the `origin` of our local clone to point to `MyScotty3D.git`, but also have a remote called `sourcerepo` for the public `Scotty3D` repository.
- We will set the `origin` of our local clone to point to `MyScotty3D.git`, but also have a remote called `sourcerepo` for the public `Scotty3D` repository.
4. Now go back to your clone of Scotty3D. This is how we add the private remote:
4. Now go back to your clone of Scotty3D. This is how we add the private remote:
- Since we cloned from the `CMU-Graphics/Scotty3D.git` repository, the current value of `origin` should be `https://github.com/CMU-Graphics/Scotty3D.git`
- Since we cloned from the `CMU-Graphics/Scotty3D.git` repository, the current value of `origin` should be `https://github.com/CMU-Graphics/Scotty3D.git`
- You can check this using `git remote -v`, which should show:
- You can check this using `git remote -v`, which should show:
```
```
...
@@ -37,7 +39,7 @@ The easiest (but not recommended) way is to download a zip from GitHub and make
...
@@ -37,7 +39,7 @@ The easiest (but not recommended) way is to download a zip from GitHub and make
5. Congratulations! you have successfully _mirrored_ a git repository with all past commits intact. Let's see a case where this becomes very useful: we start doing an assignment and commit regularly to our private repo (our `origin`). Then the 15-462 staff push some new changes to the Scotty3D skeleton code. We now want to pull the changes from our `sourcerepo`. But, we don't want to mess up the changes we've added to our private copy. Here's where git comes to the rescue:
5. Congratulations! you have successfully _mirrored_ a git repository with all past commits intact. Let's see a case where this becomes very useful: we start doing an assignment and commit regularly to our private repo (our `origin`). Then the 15-462 staff push some new changes to the Scotty3D skeleton code. We now want to pull the changes from our `sourcerepo`. But, we don't want to mess up the changes we've added to our private copy. Here's where git comes to the rescue:
- First commit all current changes to your `origin`
- First commit all current changes to your `origin`
- Run `git pull sourcerepo master` - this pulls all the changes from `sourcerepo` into your local folder
- Run `git pull sourcerepo master` - this pulls all the changes from `sourcerepo` into your local folder
- If there are files that differ in your `origin` and in the `sourcerepo`, git will attempt to automatically merge the changes. Git may create a "merge" commit for this.
- If there are files that differ in your `origin` and in the `sourcerepo`, git will attempt to automatically merge the changes. Git may create a "merge" commit for this.
- Unfortunately, there may be merge conflicts. Git will handle as many merges as it can, and then will then tell you which files have conflicts that need manual resolution. You can resolve those conflicts in your text editor and create a new commit to complete the `merge` process.
- Unfortunately, there may be merge conflicts. Git will handle as many merges as it can, and then will then tell you which files have conflicts that need manual resolution. You can resolve those conflicts in your text editor and create a new commit to complete the `merge` process.
- After you have completed the merge, you now have all the updates locally. Push to your private origin to include the changes there too:
- After you have completed the merge, you now have all the updates locally. Push to your private origin to include the changes there too:
When you select the Animate tab, a timeline window will show up at the bottom of your screen.
When you select the Animate tab, a timeline window will show up at the bottom of your screen.
Animation is performed by creating **keyframes** and **interpolating** between them.
Animation is performed by creating **keyframes** and **interpolating** between them.
### Keyframes
### Keyframes
A keyframe associates an object's pose with a specific frame in the timeline.
A keyframe associates an object's pose and properties with a specific frame in the timeline.
Keyframes can be associated with all the objects (including the camera and rig joints associated with an object) in the scene.
Keyframes can be associated with all objects (including the camera and individual joints associated with an object) in the scene.
To create a keyframe for an object:
To create a keyframe for an object:
- Select a frame location along that objects timeline by clicking on the timeline. Remember to do this *before* editing the pose.
- Select a frame location along that objects timeline by clicking on the timeline. Remember to do this *before* editing the pose.
- Change the pose of the selected object and press `Set`. To associate keyframes with all the objects, use `Set All`. Note that for rig joints, only joint rotation can be performed.
- Change the pose or properties of the selected object and press `Set`. To set a keyframe for every object in the scene, use `Set All`. Note that only the poses (rotations) of joints can be animated, not their extents.
- Click on a keyframe in the timeline, and press `Clear` to remove it.
To remove a keyframe in the timeline, click on it and press `Clear` to remove it. Press `Clear All` to clear the current keyframe of every object in the scene.
To see your animation, press `Play [space]` . Once you've implemented **spline interpolation**, intermediate frames are generated by interpolating object poses between keyframes.
To see your animation, press `Play [space]` . Once you've implemented **spline interpolation**, intermediate frames are generated by interpolating object poses between keyframes.
Check `Draw Splines` to visualize the spline along which objects are animated.
Check `Draw Splines` to visualize the spline along which objects are animated.
`Add Frames` inserts 90 empty frames into the timeline. `Crop End` deletes frames from the selected location to the end of the timeline.
`Add Frames` inserts 90 empty frames into the timeline. `Crop End` deletes frames from the selected location to the end of the timeline.
### Posing
### Posing
Once you have [rigged](../rig) an object with a skeleton, it can now be posed by selecting a joint and changing its pose i.e., rotating the joint. This is called Forward Kinematics.
Once you have [rigged](../rig) an object with a skeleton, it can now be posed by selecting a joint and changing its pose i.e., rotating the joint. This is called Forward Kinematics.
Joint poses can also be indirectly changed by using the IK (Inverse Kinematics) handles to provide target positions.
Joint poses can also be indirectly changed by using the IK (Inverse Kinematics) handles to provide target positions.
Note that IK handles need to be explicitly enabled using the checkbox.
Note that IK handles need to be explicitly enabled using the checkbox.
Once you've implemented **forward kinematics**, **inverse kinematics** and **skinning**, as you change the pose, the mesh will deform.
Once you've implemented **forward kinematics**, **inverse kinematics** and **skinning**, as you change the pose, the mesh will deform.
Different poses can be set as keyframes to animate the object.
Different poses can be set as keyframes to animate the object.
The basic paradigm in Scotty3D is that there are six different _modes_, each
The basic paradigm in Scotty3D is that there are six different _modes_, each
of which lets you perform certain class of actions. For instance, in `Model` mode, you
of which lets you perform certain class of actions. For instance, in `Model` mode, you
can perform actions associated with modeling, such as moving mesh elements and performing global mesh operations.
can perform actions associated with modeling, such as moving mesh elements and performing global mesh operations.
When in `Animate` mode, you can perform actions associated with animation. Etc.
When in `Animate` mode, you can perform actions associated with animation. Etc.
Within a given mode, you can
Within a given mode, you can
switch between actions by hitting the appropriate key; keyboard commands are
switch between actions by hitting the appropriate key; keyboard commands are
listed below for each mode. Note that the input scheme may change depending on
listed below for each mode. Note that the input scheme may change depending on
...
@@ -23,7 +26,7 @@ are are detailed in the left sidebar. Note that some actions are only available
...
@@ -23,7 +26,7 @@ are are detailed in the left sidebar. Note that some actions are only available
## Global Navigation
## Global Navigation
In all modes, you can move the camera around and select scene elements. Information about your selection will be shown in the left sidebar.
In all modes, you can move the camera around and select scene elements. Information about your selection will be shown in the left sidebar.
The camera can be manipulated in three ways:
The camera can be manipulated in three ways:
- Rotate: holding shift, left-clicking, and dragging will orbit the camera about the scene. Holding middle click and dragging has the same effect.
- Rotate: holding shift, left-clicking, and dragging will orbit the camera about the scene. Holding middle click and dragging has the same effect.
...
@@ -33,19 +36,8 @@ The camera can be manipulated in three ways:
...
@@ -33,19 +36,8 @@ The camera can be manipulated in three ways:
## Global Preferences
## Global Preferences
You can open the preferences window from the edit option in the menu bar.
You can open the preferences window from the edit option in the menu bar.
- Multisampling: this controls how many samples are used for MSAA when rendering scene objects in the Scotty3D interface. If your computer struggles to render complex scenes, try changing this to `1`.
- Multisampling: this controls how many samples are used for MSAA when rendering scene objects in the Scotty3D interface. If your computer struggles to render complex scenes, try changing this to `1`.
## Global Undo
## Global Undo
As is typical, all operations on scene objects, meshes, etc. are un and re-doable using Control/Command-Z to undo and Control/Command-Y to redo. These actions are also available from the `Edit` option in the menu bar.
As is typical, all operations on scene objects, meshes, etc. are un and re-doable using Control/Command-Z to undo and Control/Command-Y to redo. These actions are also available from the `Edit` option in the menu bar.
This is the main scene editing mode in Scotty3D, and does not contain tasks for the student to implement.
This is the main scene editing mode in Scotty3D, and does not contain tasks for the student to implement.
This mode allows you to load full scenes from disk, create or load new objects, export your scene (COLLADA format), and edit transformations that place each object into your scene.
This mode allows you to load full scenes from disk, create or load new objects, export your scene (COLLADA format), and edit transformations that place each object into your scene.
## Creating Objects
## Creating Objects
...
@@ -32,9 +33,9 @@ Scotty3D only supports exporting scenes to COLLADA.
...
@@ -32,9 +33,9 @@ Scotty3D only supports exporting scenes to COLLADA.
## Managing Objects
## Managing Objects
Left clicking on or enabling the check box of your object under `Select an Object` will select it. Information about that object's transformation will appear under `Edit Object` beneath the "Select an Object" options.
Left clicking on or enabling the check box of your object under `Select an Object` will select it. Information about that object's transformation will appear under `Edit Object` beneath the "Select an Object" options.
Under `Edit Object`, you may directly edit the values of the object's position, rotation (X->Y->Z Euler angles), and scale. Note that clicking and dragging on the values will smoothly scale them, and Control/Command-clicking on the value will let you edit it as text.
Under `Edit Object`, you may directly edit the values of the object's position, rotation (X->Y->Z Euler angles), and scale. Note that clicking and dragging on the values will smoothly scale them, and Control/Command-clicking on the value will let you edit it as text.
You can also edit the transformation using the `Move`, `Rotate`, and `Scale` tools. One of these options is always active. This determines the transformation widgets that appear at the origin of the object model.
You can also edit the transformation using the `Move`, `Rotate`, and `Scale` tools. One of these options is always active. This determines the transformation widgets that appear at the origin of the object model.
-`Move`: click and drag on the red (X), green (Y), or blue (Z) arrow to move the object along the X/Y/Z axis. Click and drag on the red (YZ), green (XZ), or blue (XY) squares to move the object in the YZ/XZ/XY plane.
-`Move`: click and drag on the red (X), green (Y), or blue (Z) arrow to move the object along the X/Y/Z axis. Click and drag on the red (YZ), green (XZ), or blue (XY) squares to move the object in the YZ/XZ/XY plane.
...
@@ -54,4 +55,4 @@ Finally, you may remove the object from the scene by pressing `Delete` or hittin
...
@@ -54,4 +55,4 @@ Finally, you may remove the object from the scene by pressing `Delete` or hittin