 Kinemation 3.0 Motion Capture Plug-ins - Programmers Reference Manual

 Overview

 Kinemation 3.0 uses dynamic shared objects to import motion capture
 data from external format files. An overview of the structure of
 these plug-ins is described in the postscript file Motion.ps. This
 document will describe what plug-ins currently exist, and give
 details about those implementations.

 Note that design of Kinemation plug-ins leaves great flexibility in
 the hands of the plug-in writer. The plug-in described in this
 document are just some possible ways to implement plug-in
 functionality - other situations may require other solutions.

 There are currently five plug-ins supporting a total of seven motion
 capture file formats as outlined in table 1. 

 The Existing Plug-ins.
---------------------------------------------------------------------
|  Plug-in Names:  File Format(s)     :  Remarks                   |
--------------------------------------------------------------------
|  cpt_acc.so   :  Acclaim .asf/.amc  :  The kinds of motion       |
|               :                     :  captured is stored in     |
|               :                     :  the .asf file, and the    |
|               :                     :  actual data is in the     |
|               :                     :  .amc.                     |
--------------------------------------------------------------------
|  cpt_moa.so   :  Motion Analysis    :  .gtr is global position/  |
|               :  .gtr               :  orientation               |
|               :                     :                            |
|               :                     :                            |
|               :  Motion Analysis    :  .htr is hierarchical      |
|               :  .htr               :  position and orientation. |
--------------------------------------------------------------------
|  cpt_mov_mcd.so:  Wavefront PreView  :  scalar data               |
|               :  .mov               :                            |
|               :                     :                            |
|               :                     :  position data             |
|               :  SuperFluo .mcd     :                            |
--------------------------------------------------------------------
|  cpt_pv.so    :  Binary Wavefront   :  This plug-in reads all    |
|               :  PreView .pv files  :  data stored in a PreView  |
|               :                     :  file, providing position  |
|               :                     :  and orientation for each  |
|               :                     :  object as well as any     |
|               :                     :  other kinds of animated   |
|               :                     :  values.                   |
--------------------------------------------------------------------
|  cpt_trc.so   :  Motion Analysis .trc:  position data             |
---------------------------------------------------------------------

 Plug-in Handles

 This section describes how the handles maintained by the existing
 plug-ins are used.

 As needed by Kinemation, each plug-in maintains a handle that points
 to the motion data for a particular motion capture. All the plug-ins
 use some variation of the following handle definition:

 typedef struct Handle

 {

   int    type;         

   int    frames;

   int    channels;

   float *buffer;

   size_t buffer_bytes;

 } *Handle;

 type 	is from the following list:

 #define MOV_HANDLE 0x4d6f7600    /* `Mov\0' */

 #define MCD_HANDLE 0x4d636d00    /* `Mcd\0' */

 #define ACC_HANDLE 0x4163636c    /* `Accl' */

 #define GTR_HANDLE 0x67747200    /* `gtr\0' */

 #define HTR_HANDLE 0x68747200    /* `htr\0' */

 #define PV_HANDLE  0x70760000    /* `pv\0\0' */

 #define TRC_HANDLE 0x54724300    /* `Trc0' */

 frames 	contains the number of frames in the file.

 channels 	is the number of channels.

 buffer	is the actual space for the motion data. 

 buffer_bytes	is the number of bytes in buffer.

 Fat Handles - pros & cons

 All the existing plug-ins create and fill the handle in the
 CdOpenProc() procedure (and free the handle in the CdCloseProc()
 procedure). This is accomplished by computing the number of frames
 and channels and the type of channel data, and allocating the large
 buffer to hold all the data. 

 Note that this could be quite memory intensive:   3 minutes of 24
 channels of position/orientation data sampled at 30 fps would require
 approximately 3.5 megabytes of memory. However, this means that each
 call to the CdChannelDataProc() is very quick: the routine can simply
 return the values out of memory.

 The Source String and Filename Stuff

 A plug-in CdOpenProc() takes a source string, which is a directive as
 to where to get the motion capture data. This is usually a filename,
 but in theory it could be anything. For example, if the plug-in was
 supplying data from the output of an executable, the source string
 might contain parameters to feed to that executable.

 With the exception of cpt_acc.so (see next paragraph), all the plug-
 ins treat the data source string as a filename. They call the
 capserver library routine CapExpandFilename() to expand any
 environment variables or ~[user] prefixes, and then attempt to open
 the resulting filename.

 The Acclaim plug-in needs two filenames of the form
 <asf_file>,<amc_file>. More details are below in the Acclaim section.

 Also note that the plug-in API now has a CdVerifyProc(), which has
 not been implemented to any degree. It is intended for checking that
 a source string refers to a valid source. Plug-in writers are
 encouraged to implement this in future plug-ins.

 Specific Notes for each Plug-in

 This section describes plug-in specific implementation details. We'll
 start with the simpler plug-ins.

  The .mov plug-in

 A .mov file is very simple: each line is one frame of data, composed
 of any number of scalar channels with CHN_USAGE_UNKNOWN usage. 

 o CdOpenProc() reads the first line to get the number of channels, then
   makes a first pass through all the file to get the number of
   frames. It allocates frames * channels * sizeof(float) bytes for
   the buffer, and makes a second pass through the file to fill the
   buffer.

 o CdCloseProc() simply frees the space.

 o CdFileInfo() returns the frames, channels, and -1 for a sample rate.

 o CdChannelInfoProc() labels each channel as "column_#" (numbered
   successively) with dtype 1 and usage CHN_USAGE_UNKNOWN.

 CdChannelDataProc() does a memcpy() straight out of buffer.

 The .mcd plug-in

 A .mcd file is also very simple. Each line is a frame of data
 contains 3d position information for a "marker" (corresponding to a
 channel). The first line of the file tells the number of frames and
 markers and sample rate of the data.

 o CdOpenProc() reads the first line of form 

 		"frames =%d markers =%d Hz =%f,"

 allocates frames * markers * 3 * sizeof(float) bytes for the buffer,
 and makes a pass through the file to fill the buffer.

 o CdCloseProc() simply frees the space.

 o CdFileInfo() returns the frames, channels, and the sample rate as
   reported in the header. 

 o CdChannelInfoProc() labels each channel as "marker_#" (numbered
   successively) with dtype 3 and usage CHN_USAGE_POSITION.

 o CdChannelDataProc() does a memcpy() straight out of buffer.

 The .trc plug-in

 A .trc file contains global position data from a Motion Analysis
 motion capture system. The first line contains a version number, the
 second some header information, the third the data rate, number of
 frames and channels, the fourth the names of the channels, the fifth
 line is ignored (it could be used for scalar channel names), the
 sixth line is blank, and the rest of the lines contain the frame
 number followed by the xyz values for each channel.

 The handle for this plug-in includes a char **channel_names where the
 names of the channels are stored.

 The plug-in makes use of a split() function which takes a line of
 space delimited tokens and builds an array a pointers to each token,
 returning the count of tokens. This makes parsing the file easier.

 o CdOpenProc() parses the header information, makes copies of all the
   channel names, and stores all the values in its buffer.

 o CdCloseProc() frees the values and the channel names.

 o CdFileInfo() returns the frames, channels, and the sample rate as
   reported in the header. 

 o CdChannelInfoProc() returns the channel names and dtype 3 and usage
   CHN_USAGE_POSITION.

 o CdChannelDataProc() does a memcpy() straight out of buffer.

 The .gtr/.htr plug-in.

 .gtr and .htr files are handled by the same plug-in. These files are
 composed of sections, each section delimited by a [<section_name>] on
 its own line (the [] brackets are there, the <section_name> varies).
 Comments are allowed, delimited by a `#' and newline. The first
 section is header information, including whether it's a htr or gtr
 file, version information, the number of channels and frames, et. al.
 The next section contains the names of the names of the channels. If
 its a htr file, this is in pairs, the first being the child, the
 second the parent, although this is not important to the plug-in. The
 htr file has a [BasePosition] section, which is ignored. The next
 sections are [<channel_name>], one for each channel, with one line
 per frame, each line frame data. If it's a gtr file, each line
 contains the frame number and xyz and xyz rotation global data. If
 it's a htr file, each line contains the frame number, xyz position,
 xyz rotations (around its parent), and a bone_length scale factor
 (which is ignored, since Kin 3.0 does not easily handle bone scaling). 

 Since htr files are hierarchical, the data is used to drive joint
 rotations within Kinemation.

 The handle for this plug-in includes a char **channel_names where the
 names of the channels are stored.

 o CdOpenProc() parses the header information, makes copies of all the
   channel names, and stores all the values in its buffer. Unlike the
   preceding plug-ins, cpt_moa.so takes the rotation values and
   converts them to quaternions using the CapEuler2Quat() function in
   the capture server library (see the Wavefront Motion Capture
   Server Library Reference Manual). It then stores 7d values for
   each channel's frame in buffer.

 o CdCloseProc() frees buffer and channel_names.

 o CdFileInfo() returns the frames, channels, and the sample rate as
   reported in the header. 

 o CdChannelInfoProc() returns the channel names and dtype 7 and usage
   CHN_USAGE_POS_ORIENT.

 o CdChannelDataProc() does a memcpy() straight out of buffer.

 The Acclaim plug-in

 The Acclaim plug-in creates a buffer of position and orientation
 information in the same fashion as the cpt_moa.so plug-in. The data
 is hierarchical and therefore is used to drive joint angles.

 The Acclaim plug-in is made complicated by the fact that the motion
 values are stored in one file (the .amc, AcclaimAcclaim motion
 capture file), while necessary information for interpreting those
 values are stored in another file (the .asf, Acclaim skeleton file).
 Also, there is a translator for converting .asf/.amc file pairs into
 Kinemation .bod/.kin/.cpt files. Much of this functionality overlaps,
 so the two efforts share code.

 Acclaim documents their file formats in the following paper:

 Acclaim Skeleton File Definition V1.10, Acclaim Entertainment, Inc.,
 (c) 1995, Internal Technical Memo 36A, by Mark Schaefer. (516)-656-
 5000.

 The format of the .amc file is simple. Data is grouped by frame, each
 line prefixed with the name of the "bone" (which corresponds to a
 channel) followed by values associated with the "dof" token for that
 bone in the associated .asf file. So, for example, if the dof (degree
 of freedom) for h_torso_2 is 

 dof ty rx ry rz

 then the values on the following kind of line in the .amc file

 h_torso_2 0.0729 5.362885 7.8838997 -9.029815

 are a y-translation, a x-rotation, a y-rotation, and a z-rotation.

 There order of rotation is also very important. This is stored in the
 .asf file with each bone on the axis line. 

 The cpt_acc.c handle is typical of the other plug-ins, but the
 CdOpenProc() is much more elaborate in order to deal with the Acclaim
 paradigm.

 o CdOpenProc() expects a source string of the form
   <asf_file>,<amc_file>. Each filename is expanded with
   CapExpandFilename(). Then the .asf file is parsed with a call to
   readAcclaimSkeletonFile(), a translator routine which takes the
   filename and generates an internal representation of a Kinemation
   body file. This Body data structure contains the dof's and the
   rotation order information necessary to interpret the motion data
   for each bone. The body pointer and the .amc filename are passed
   to acc_readMotionFile(), which open the .amc file and processes it
   as follows.

 First, one complete pass is made through the motion data file to
 discover the number of frames and the list of bones that are animated
 (which corresponds to channels). This makes it possible to create a
 handle with a buffer for frames * channels * 7 floats. Also, the
 names of the channels are copied from the list of animated bones.

 Following this, a second pass is made through the .amc file to
 actually grab the values. Each line is the name of bone and its
 associated values. From the Body pointer, the way to interpret the
 values is retrieved. A buffer called chan_data is filled with x,y,z
 translations followed by x,y,z Euler angles. These are defaulted to
 zero if they are not supplied in the .amc file. Then, according the
 rotation order, the Euler angles are converted to quaternions, and
 the result is stored in buffer at the appropriate place for the
 current frame and bone (channel). At the completion of this second
 pass, buffer is miraculously filled with correct motion data.

 The other routines are virtually the same the above plug-ins' routines.

 o CdCloseProc() frees buffer and channel_names.

 o CdFileInfo() returns the frames, channels, and the a sample rate of
   30 Hz.

 o CdChannelInfoProc() returns the channel names and dtype 7 and usage
   CHN_USAGE_POS_ORIENT.

 o CdChannelDataProc() does a memcpy() straight out of buffer.

 The .pv plug-in

 This plug-in reads .pv files, the binary file format of Wavefront's
 PreView. PreView's notion of items that can be animated has some
 differences from Kinemation 3.0, so this plug-in has to jump through
 some extra hurdles. On the easy side, all PreView objects have
 position and orientation values. On the more difficult side, there
 are hundreds of other kinds of things that can be animated in
 PreView, some of which have correspondences to Kinemation and some of
 which do not. For example, a PreView object may animate position,
 orientation, x-scale, z-scale, and shape. This would correspond to
 CHN_USAGE_POS_ORIENT, CHN_USAGE_XSCALE, CHN_USAGE_ZSCALE,
 CHN_USAGE_SHAPE. However, the same object might also animate many
 other possible PreView items (like skew, shear, flare, bend, et. al.)
 which the plug-in interprets as CHN_USAGE_UNKNOWN. 

 Another difficulty is that PreView allows objects with the same name,
 while Kinemation needs unique object names.

 To handle these complexities, the plug-in does several things. All
 PreView objects are assigned a position/orientation channel with the
 same name as the object. (These channels have, of course, dtype 7 and
 usage CHN_USAGE_POS_ORIENT.) If there is already an object with that
 name, a numeric count is appended to the object's name. So, for
 example, the first cube will be name cube while the third cube in the
 file.pv will correspond to a cube03 channel in the Kinemation. 

 Next, if the object animates anything besides position or
 orientation, channels are created for these items also, with the name
 of the type of animation appended to the objects name. So, if that
 same third cube had a shape channel, a roll channel, and a y-scale
 channel, the plug-in creates cube03_shape, cube03_roll, cube_yscal
 channels, all with dtype 1 and with usages CHN_USAGE_SHAPE,
 CHN_USAGE_UNKNOWN, and CHN_USAGE_YSCALE. Note then whenever possible,
 a corresponding kin_plugin.h usage is used, otherwise unknown is
 used. Also note that all these extra channels have a dtype of 1.

 This plug-in is different from the previous plug-ins in that it does
 not use a "square" chunk of memory for storing channel data. Previous
 plug-ins allocate a buffer with dimensions frames by channels.
 Because channels in this plug-in can be either 7d position/
 orientation data or 1d other-usage data, each channel allocates its
 own appropriately sized buffer called buf. 

 The handle data structure is quite different than the previous plug-
 ins in order to accommodate these issues. It is shown in figure 1. 

 The cpt_pv handle.

 pv_names points to a list of the names found in the PreView file,
 with a count of collisions. 

 channels points to information for each channel that the plug-in
 creates. It includes the Kinemation name of the channel, its usage,
 its rotational priority if it's a position/orientation channel, and a
 pointer (buf) to the actual frame data. 

 buf points to space for either frame_cnt or 7 * frame_cnt floats,
 depending on whether the channel is 1d or 7d. Other items in the
 structure of for maintenance of those lists.

 The PreView plug-in makes use of the SCMP library for opening and
 retrieving information from PreView .pv file.

 Now we'll outline the functionality of each entry point.

 o CdOpenProc() calls SCMP_open() with the expanded filename. It
   allocates a handle with initial chunks of space for pv_names and
   channels. It queries the number of objects and frames in the file. 

 A pass through all the object in the .pv file is made. The name of
 the object is looked up in pv_names. If the name has not been seen
 before, it is simply added to the pv_names list and a basename with
 the same name is created. If the name has been seen before, the count
 of this name is incremented and a basename of the form cube03 is
 created until the name is unique.

 The plug-in uses the SCMP library to discover the kinds of channel
 data associated with this PreView object. One plug-in channel is
 created for all xyz translation and rotation PreView channels with
 the name basename. Additionally, plug-in channels are created for all
 other types of PreView channels, with names of the form
 <basename>_<chan_type> (for example, "cube03_yscal"). 

 A pass is then made through all the frames to retrieve the all the
 channel values for this object. It is then necessary to convert all
 Euler angles to quaternions, according to the rotational priority for
 that object as stored in the .pv file.

 o CdCloseProc() takes care of cleaning up all allocated space.

 o CdFileInfo() returns the frames, channels, and a sample rate of 30. 

 o CdChannelInfoProc() returns the name and usage of a channel, and a
   dtype of 7 if it's position/orientation, or 1 otherwise. .

 o CdChannelDataProc() does a memcpy() from the appropriate channel's buf.

 Caveats

 None of the existing plug-ins make use of the CdVerifyProc(), which
 should be completed, or at least used in future plug-ins. More
 version checking should be added to the existing plug-ins, since many
 of the formats do have version number information in the file. Some
 of the plug-ins do not support all options in the file: the cpt_moa
 plug-in should notice the rotational priority in the header of the
 HTR files, and the cpt_acc plug-in should notice whether degrees or
 radians are used.

