Working with human movement data, sometimes you need to make visualizations or infographics based on the data: you can better analyze the movement by visualizing it and its features see this, or maybe you just want to tell a story using movement visualizations.
Among different techniques for recording movement data, motion capture (mocap) is a well-known method for capturing the exact movements of an actor. Motion capture data are typically constructed from the 3D coordinates of body joints for a series of time-steps (frames). Mocap is often used to create animations for video games or feature films.
In a way, we can treat a segment of movement data like any tabular data: we have a sequence of frames (rows), and for each frame, we have the positions or rotations of a group of body joints (columns). However, there is a pre-defined spatial relationship among the body joints, which is the skeleton. We can use the skeleton to display not only the joints but also use the information of which joints are connected to each other to visualize the bones.
A great tool for creating web-based and interactive data-driven visualizations is the D3.js library. D3 allows you to create and manipulate web elements (the elements used in SVG, HTML, …) based on data. In addition, you can add transformations, transitions, and interactions to the visualizations using both D3 and the standard web practices that are available on modern web browsers.
1. Motion Capture Data Format
Mocap data are provided in different formats such as C3D (3D marker positions); Acclaim, Biovision, and Vicon (both the skeleton and the movement data); text (comma or space delimited); and more general 3D asset formats such as COLLADA.
No matter of the file formats, each frame of motion capture data consists of a root node, which defines the body’s absolute position and orientation with respect to a global coordinate system, and a set of nodes each representing a joint’s angle or bone’s relative orientation in a three-dimensional space.
For this guide, to avoid dealing with format-specific considerations and the mechanics of parsing them, I have provided sample mocap files in JSON format, available from the GitHub repository for this tutorial.
In our ad hoc format, we use two structures: one which contains the position of each joint at each time frame, and one for defining the skeleton (that is, the bones).
The positions are defined using an N x M matrix, representing N frames, each containing M joint objects. The position of each joint is defined by its three parameters x, y, and z. So, positions.x represents the position of the 5th joint along the x axis, at the first frame, and positions.z represents the position of the 15th joint along the z axis.
The skeleton is defined using an array of bones. Each element of the array contains two elements, representing the indexes of the two joint at each end of the bone. The indexes match the index of the joints as they appear in the positions matrix. For example, skeleton = [6,7] represents the first bone in the skeleton, connecting the joint with index 6 (skeleton = 6) to the joint with index 7(skeleton = 7).
2. Setting Up the Scene
Before we start, we should include the D3.js and jQuery scripts in our HTML file:
We also need to create a div tag to put our visualization inside of it:
There are a number of variables and parameters that we are going to use throughout or project:
We will put all of the code for doing the visualization inside of the draw() function.
Our data is already in JSON format and ready to be used. We can use jQuery (or any other method of choice) to download and parse them:
Since the $.getJSON() function is asynchronous, we can only start drawing once all the data is loaded. Thus, we put all of the visualization code inside the draw() function and call it in the callback function of the last JSON file. Once the draw() function is called, we can access the data using the positions and skeleton global variables.
4. Setting Up the Environment
First, we create the SVG tag inside of our container, which will house all of the elements we visualize:
Before visualizing the data, we need to scale and translate them to make sure they are within a range that fits our intended design.
Note that the y axis in motion capture data start from the bottom of the screen, while D3 expects the y axis to start from the top. Thus, we need to multiply it by -1 and translate it by the hight of our container.
We start by drawing the pose of only one frame, identified by the index variable.
5. Visualizing the Joints
We visualize joints with circles. Based on the data from the currentFrame, we append an SVG circle element for each joint and set its center to the position of the joint. We further specify the radius of the circles and their filling color. We can make specific joints bigger, or paint them with a different color, by checking the index i passed to the function function(d,i).
The code above will give you this:
6. Adding the Bones
We visualize each bone with a line connecting the two joints at each end. We use the skeleton array as our main data source. For each bone, we add an SVG line element. The locations of the beginning and the end of the line is determined by the positions of the first and second joints of the bone. Note that the position data are stored in the currentFrame variable:
Now, your visualization should look like this:
7. Drawing More Frames
So far we have only visualized a single frame. Now, we go one step further and draw a sequence of consecutive frames.
We use two more parameters. The distance between each two consecutive frames will be defined by the gap variable. Also, we may not necessarily want to draw every single frame and instead only draw every 4th frame (downsampling). We specify this fraction by the skip variable.
We visualize a sequence of frames by applying the D3 functions on a nested datasource. First, we apply a filter on the positions_scaled array to skip the frames we don’t want to draw:
Next, we loop through the frames, and append the elements for the data for each frame. We create an SVG group element for each frame and shift its position by the pixels specified in gap:
We then add the joints or bones inside of this SVG group element.
Now, you should be able to see this:
That’s it for the first part of this tutorial. In the following posts, I will explain how to extract movement features and incorporate them into the visualizations.
8. The Complete Code
The complete source code and data are in the github repo: https://github.com/omimo/d3-mocap-demo.
You can also play around with the code using this fiddle:
9. More Examples
If you are interested, you can take a look at more examples in the link below:
10. Further Resources
If you are interested in learning more about D3.js, you can find many tutorials here: https://github.com/d3/d3/wiki/Tutorials
To learn more about motion capture formats, here are some useful links: