 Adding Motion Plug-Ins 

 Kinemation features an external plug-in module mechanism that lets
 you add your own motion data parsing and processing functions. Your
 plug-in modules are available whenever you start Kinemation. You
 access these plug-ins through the Capture Setup dialog or with a .cpt
 file.

 Plug-in modules are implemented as dynamically shared objects (DSOs).
 Plug-ins can be written using C or C++. 

 You only implement the algorithms that parse the data; the rest of
 the data handling is managed implicitly by Kinemation.

 This document describes the protocol for writing a plug-in module,
 the formats of the data that you need to provide, and the procedure
 for adding the plug-in into Kinemation. A problem- solving section is
 included at the end of the document, to help you correct any errors
 that you may encounter when creating your plug-ins.

 Overview

 Kinemation reads static motion data files just once and stores all
 the data internally. At each frame, the appropriate data is retrieved
 from the internal buffer and applied to animation.

 Each plug-in has five routines for each file type that it supports.
 These routines open and close the file, return data about the file as
 a whole, return data about each channel, and return data for each
 channel. These routines are called when the data file is opened by
 Kinemation and then not used until another data file is opened.

 The plug-in provides the motion data to Kinemation in the form of
 channels. Each channel has a name, a data type, a data usage type,
 and a set of data values. The data type, for example, might be
 "single floating-point value" while the data usage might be "x-
 rotation". Another channel could have a data type of "position and
 orientation" with a usage of "position and orientation" as well.

 Adding a new data file reader

 Before you implement a plug-in, you must decide what the data types
 and data usages will be for the channels that the plug-in will return
 to Kinemation.

 The allowed data types are:

 o Scalar (a single float)

 o Position (3 floats)

 o Orientation (a quaternion of 4 floats)

 Position-Orientation (3 floats + a quaternion)

 The allowed data usages are:

 o Unknown

 o Position

 o Orientation

 o X-Position

 o Y-Position

 o Z-Position

 o X-Rotation

 o Y-Rotation

 o Z-Rotation

 o X-Scale

 o Y-Scale

 o Z-Scale

 o File-Interp

 o Sculpt

 o Weight

 o Hither

 o Yon

 o Lens

 Position-Orientation

 For the data usage values of Position, Orientation, and Position-
 Orientation, the data type must correspond. For all other usages, the
 data type must be Scalar.

 About entry points

 Each plug-in must have a defined number of entry points. The Composer
 uses these entry points to access the plug-in's functionality. 

 Some entry points are variables and others are functions. As
 examples, the name of the plug-in is defined by a variable entry
 point, and the routine which computes the resulting image is defined
 by a function entry point.

 Some entry points are required, others are optional. The mandatory
 entry points are described in the next section. The optional entry
 point are described in "Optional entry points" on page 36. 

 Mandatory entry points

 The following entry points must be defined:

 o program

 o type

 o version

 o CdEntryCount

 CdEntry

--------------------------------------------------------------
|  :  If a mandatory entry point is omitted, the plug-in     |
|  :  will not be loaded, and its name will not appear in    |
|  :  the Capture Setup source type menu.                    |
--------------------------------------------------------------

 program

 Definition

 char *program

 Description

 This entry point denotes Wavefront applications. For motion data plug-
 ins, this must be Kinemation.

 Example

 char *program = "Kinemation";

 type

 Definition

 char *type

 Description

 This entry point denotes the type of plug-in. Kinemation's motion
 data plug-ins are of type CaptureData.

 Example

 char *type = "CaptureData";

 version

 Definition

 char *version

 Description

 This entry point denotes the version of the plug-in protocol. The
 current version is 3.0.

 Example

 char *version = "3.0";

 CdEntryCount

 Definition

 int CdEntryCount;

 Description

 This entry point defines the number of elements in the CdEntry array.
 Each element defines one type of file that is supported. This allows
 a single plug-in to parse more than one type of input data.

 Example

 int CdEntryCount = 				sizeof(CdEntry) /
 				sizeof(CdEntry[0]);

 CdEntry

 Definition

 CdTable Cdentry[];

 Description

 This array defines the name and entry points for each type of data
 that this plug-in can parse. Each entry contains all of the
 information that Kinemation needs to be able to parse that data type.

 The CdTable structure is defined as follows:

 typedef		struct		CdTable
 {
 	char					*name;
 	CdVerifyProc					verify;
 	CdOpenProc					open;
 	CdCloseProc					close;
 	CdFileInfoProc					file_info;
 	CdChannelInfoProc					channel_info;
 	CdChannelDataProc					channel_data;
 	char					**error_msg;
 } CdTable;

 Name

 The name field defines the name that will be used for this plug-in in
 the Capture Setup dialog as well as the name that will be used in
 .cpt files. It should be of a reasonable length and should not have
 spaces or other strange characters in it or it will not be usable
 from .cpt files.

 Verify

 The verifyfield holds a pointer to the verify routine for this data
 type. The verify routine is passed the name of the data source and it
 returns a flag that indicates whether the data source is valid or
 not. The type CdVerifyProc is defined below.

 Open

 The open field holds a pointer to the open routine for this data
 type. The open routine is passed the name of the data source and it
 returns a handle to be used by the rest of the plug-in routines to
 refer to this data source. The type CdOpenProc is defined below. 

 Close

 The close routine takes the handle returned from the open routine and
 closes it, making sure to free any allocated resources. The type
 CdCloseProc is defined below.

 File Info

 The file info routine returns general information about the whole set
 of motion data. Specifically, the number of samples, the number of
 data channels, and the data sample rate in Hz. The CdFileInfoProc
 type is defined below.

 Channel Info

 The channel info procedure returns information about an individual
 channel. The returned information includes the channel's name, data
 type, and data usage. The data type is the number of floating point
 values in a single sample. The data usage indicates how those numbers
 should be applied to skeletal parts in Kinemation. The
 CdChannelInfoProc type is defined below.

 Channel Data

 The channel data procedure returns the actual motion data for a
 specific channel as an array of floating point values. The
 CdChannelDataProc type is defined below.

 The procedure is defined as follows:

 typedef		int		(*CdChannelDataProc)
 	(
 		CdHandle			handle,
 		int			channel_num,
 		float			*data_buffer,
 		int			data_buffer_size
 	);

 The input arguments are the handle returned from the open procedure,
 the channel number (see Channel Info above for a description of the
 channel number), a pointer to a data buffer, and the number of float
 values that will fit in the data buffer.

 On return, the channel data proc copies the data from the source into
 the space pointed to by data buffer. The data buffer should be able
 to hold at least frames * channel_dtype floating point values (frames
 was returned from the file info proc and channel_dtype was returned
 from the channel info proc).

 Error Message

 The error message field contains the address of a pointer to char.
 This pointer should be set to point to an error message string when
 some error occurs. This string will be displayed by Kinemation in an
 Error Message dialog. If there is no message, you may set the pointer
 to NULL.

 CdVerifyProc

 Definition

 typedef		int			(*CdVerifyProc)
 	(
 		const char			*sourcename
 	);

-------------------------------------------------
|  Parameter :  Type  :  Description            |
-------------------------------------------------
|  sourcename:  Input :  The name of the        |
|            :        :  source to verify.      |
-------------------------------------------------
|  Return    :        :  Return 0 if the        |
|  Value     :        :  source is valid or -1  |
|            :        :  if the source is       |
|            :        :  invalid..              |
-------------------------------------------------

 Description

 This function is called to verify a the validity of a source. It is
 passed a string containing the name of the data source from the
 Capture Setup dialog or from a .cpt file. Typically, this name will
 be a file name but in theory, it could be almost anything.

 The verify procedure returns an integer which indicates the validity
 of the source. If the source is valid, 0 is returned. If there is
 some error or the source is invalid, -1 is returned.

 Related functions

 CdOpenProc

 CdOpenProc

 Definition

 typedef		void			*CdHandle;
 
 typedef		CdHandle			(*CdOpenProc)
 	(
 		const char			*sourcename
 	);

-------------------------------------------------
|  Parameter :  Type  :  Description            |
-------------------------------------------------
|  sourcename:  Input :  The name of the        |
|            :        :  source to open.        |
-------------------------------------------------
|  Return    :        :  Some handle value      |
|  Value     :        :  when the source is     |
|            :        :  successfully opened;   |
|            :        :  NULL if there is some  |
|            :        :  error.                 |
-------------------------------------------------

 Description

 This function is called to open the source and create a handle that
 references it. It is passed a string containing the name of the data
 source from the Capture Setup dialog or from a .cpt file. Typically,
 this name will be a file name but in theory, it could be almost
 anything.

 The open procedure returns a CdHandle (which is just a pointer to
 void). Kinemation has no idea what the contents of the handle are
 with the exception that a NULL value is assumed to indicate an error
 opening the source.

 Related functions

 CdVerifyProc
 CdCloseProc

 CdCloseProc

 Definition

 typedef		int		(*CdCloseProc)
 	(
 		CdHandle			handle
 	);

--------------------------------------------------
|  _Parameter_:  Type   :  Description            |
---------------------------------------------------
|  handle    :  Input  :  The handle returned    |
|            :         :  from the CdOpenProc.   |
--------------------------------------------------
|  Return    :         :  Zero if there was no   |
|  Value     :         :  error or -1 if the     |
|            :         :  handle could not be    |
|            :         :  closed.                |
--------------------------------------------------

 Description

 The CdCloseProc is called to close the source and free any allocated
 resources. All data, including the channel names, that Kinemation
 needs has been copied by Kinemation so the CdCloseProc should free
 all memory that it has allocated, close all files that it opened, and
 generally make sure that everything is cleaned up.

 If an error value of -1 is returned, the error_msg variable should
 point to an appropriate error message which will be displayed in an
 Error Message dialog by Kinemation.

 Related functions

 CdOpenProc

 CdFileInfoProc

 Definition

 typedef		int		(*CdFileInfoProc)
 	(
 		CdHandle			handle,
 		int			*samples,
 		int			*channels,
 		float			*samplerate
 	);

--------------------------------------------------
|  Parameter :  Type   :  Description            |
--------------------------------------------------
|  handle    :  Input  :  The handle from the    |
|            :         :  CdOpenProc call.       |
--------------------------------------------------
|  samples   :  Output :  The total number of    |
|            :         :  samples in the data.   |
--------------------------------------------------
|  channels  :  Output :  The number of          |
|            :         :  channels in the data.  |
--------------------------------------------------
|  samplerate:  Output :  The sample rate in     |
|            :         :  samples per second     |
|            :         :  (Hz).                  |
--------------------------------------------------
|  Return    :         :  Zero if there was no   |
|  Value     :         :  error or -1 if some    |
|            :         :  error occurred.        |
--------------------------------------------------

 Description

 This function is called to return global information about the data
 source. This information includes the number of data samples, the
 number of channels of data, and the sample rate for the data. The
 sample rate is measured in samples per second (or Hz).

 If some error occurs, then -1 is returned and the error message
 pointer should point at an appropriate error message.

 Related Functions

 CdOpenProc
 CdChannelInfoProc

 CdChannelInfoProc

 Definition

 typedef		int		(*CdChannelInfoProc)
 	(
 		CdHandle			handle,
 		int			channel_num,
 		char			**channel_name,
 		int			*channel_dtype,
 		PlugInChannelUsage
 					*usage,
 	);

-------------------------------------------------
|  Parameter  :  Type   :  Description          |
-------------------------------------------------
|  handle     :  Input  :  The handle returned  |
|             :         :  from the CdOpenProc.  |
--------------------------------------------------
|  channel_num:  Input  :  The number of the    |
|             :         :  channel for which    |
|             :         :  we are requesting    |
|             :         :  info.                |
-------------------------------------------------
|  channel_name:  Output :  The name of the      |
|             :         :  channel.             |
-------------------------------------------------
|  channel_dty:  Output :  The data type of     |
|  pe         :         :  the channel          |
-------------------------------------------------
|  usage      :  Output :  The usage of the     |
|             :         :  channel.             |
-------------------------------------------------
|  Return Value:         :  Zero if there was    |
|             :         :  no error, or -1 if   |
|             :         :  an error occurred.   |
-------------------------------------------------

 Description

 The handle is the value returned from the call to the CdOpenProc. The
 channel numbering starts with zero. This means that 0 <= channel_num
 < channels, where channels is the value returned from the
 CdFileInfoProc.

 The name of the channel is a string that should not contain spaces or
 other strange characters. To be safe, it should probably be named
 with a name acceptable as a C or C++ identifier.

 The data type should be 1, 3, 4, or 7 which correspond to Scalar,
 Position, Orientation, and Position-Orientation. No other values are
 allowed.

 The usage value is an enum and will be one of the following values:

 	CHN_USAGE_UNKNOWN
 	CHN_USAGE_POSITION
 	CHN_USAGE_ORIENTATION
 	CHN_USAGE_XPOS
 	CHN_USAGE_YPOS
 	CHN_USAGE_ZPOS
 	CHN_USAGE_XROT
 	CHN_USAGE_YROT
 	CHN_USAGE_ZROT
 	CHN_USAGE_XSCALE
 	CHN_USAGE_YSCALE
 	CHN_USAGE_ZSCALE
 	CHN_USAGE_SCALE
 	CHN_USAGE_FILEINTERP
 	CHN_USAGE_SCULPT
 	CHN_USAGE_WEIGHT
 	CHN_USAGE_HITHER
 	CHN_USAGE_YON
 	CHN_USAGE_LENS
 	CHN_USAGE_POS_ORIENT

 If the data type is 3, 4, or 7, the usage _must be
 CHN_USAGE_POSITION, CHN_USAGE_ORIENTATION, or CHN_USAGE_POS_ORIENT
 respectively._

 If some error occurs, then -1 is returned and the error message
 pointer should point at an appropriate error message.

 Related Functions

 CdOpenProc
 CdFileInfoProc
 CdChannelDataProc

 CdChannelDataProc

 Definition

 typedef		int		(*CdChannelDateProc)
 	(
 		CdHandle			handle,
 		int			channel_num,
 		float			*data_buffer,
 		int			data_buffer_size,
 	);

---------------------------------------------------
|  Parameter :  Type   :  Description            |
--------------------------------------------------
|  handle    :  Input  :  The number of          |
|            :         :  arguments passed to    |
|            :         :  the event.             |
--------------------------------------------------
|  channel_num:  Input  :  The frame ranges of    |
|            :         :  all the arguments      |
|            :         :  passed to the event.   |
--------------------------------------------------
|  data_buffer:  Input  :  The argument for       |
|            :         :  which to return        |
|            :         :  neighbors.             |
--------------------------------------------------
|  data_buffe:  Output :  An array of            |
|  r_size    :         :  neighboring frames.    |
--------------------------------------------------
|  Return    :         :  Zero if no error       |
|  Value     :         :  occurred, or -1 if     |
|            :         :  there was some problem. |
---------------------------------------------------

 Description

 This routine returns the actual motion data from the source. The
 channel number is the same as in the CdChannelInfoProc.

 The data_buffer argument should point to enough space to hold
 channel_dtype * samples floating point values. The data_buffer_size
 argument is set to the number of floats that the buffer can hold. The
 data is passed back to Kinemation in a single large array.

 The following table defines the packing of the data into the
 data_buffer array:
-----------------------------------------------
|  Data Type:  Data Packing                    |
------------------------------------------------
|  1       :  Sample 0:	buf[0]	{s}            |
|          :  Sample 1:	buf[1]	{s}...         |
-----------------------------------------------
|  3       :  Sample 0:	buf[0]	{x, y, z}      |
|          :  Sample 1:	buf[3]	{x, y, z}...   |
-----------------------------------------------
|  4       :  Sample 0:	buf[0]	{qw, qx,q y,   |
|          :  qz}                             |
|          :  Sample 1:	buf[4]	{qw, qx, qy,   |
|          :  qz}...                          |
-----------------------------------------------
|  7       :  Sample 0:	buf[0]	{x, y, z, qw,  |
|          :  qx, qy, qz}                     |
|          :  Sample 1:	buf[7]	{x, y, z, qw,  |
|          :  qx, qy, qz}...                  |
-----------------------------------------------

 In this table, s represents a scalar value; x, y, z represents an 3D
 position, and qw, qx, qy, qz represents a quaternion. The data must
 be in this order or Kinemation will not be able to use it.
 Furthermore, if rotations are provided as scalar channels, the data
 must be provided in radians.

 Related Functions

 CdOpenProc
 CdFileInfoProc
 CdChannelInfoProc

 Optional entry points

 A number of additional entry points exist. These generally have
 little or no effect on the operation of the plug-in itself, they
 simply provide hooks for third parties to provide copyright
 information or security calls.

 o copyright

 o _init

 o _fini

 copyright

 Definition

 char *copyright

 Description

 This entry point defines a copyright notice for this plug-in. When
 the plug-in is loaded, if this copyright notice is different than all
 copyright notices that have been displayed so far, the copyright
 notice will be displayed on the terminal that Kinemation was run from
 and will also be displayed in the _Help->On Version... window. If the
 notice is identical to a notice that has already been displayed, then
 it is skipped._

 Example

 char *copyright = "(C) Plug-ins-R-Us, 1995";

 _init

 Definition

 void		_init
 	(
 		void
 	);

-------------------------------------------------
|  Parameter :  Type  :  Description            |
-------------------------------------------------
|  _none_    :        :  No parameters          |
-------------------------------------------------
|  Return    :        :  No return value.       |
|  Value     :        :                         |
-------------------------------------------------

 Description

 The _init() function is called when the plug-in is loaded. It is
 intended to be a hook for one-time initialization or third party
 licensing.

 Example

 void _init(void)
 {
 	/* your code here */
 }

 _fini

 Definition

 void		_fini
 	(
 		void
 	);

-------------------------------------------------
|  Parameter :  Type  :  Description            |
-------------------------------------------------
|  _none_    :        :  No parameters          |
-------------------------------------------------
|  Return    :        :  No return value.       |
|  Value     :        :                         |
-------------------------------------------------

 Description

 The _fini() function is called when the plug-in is unloaded. It is
 intended to be a hook for shutdown or third party licensing.

 Example

 void _fini(void)
 {
 	/* your code here */
 }

 Library Functions

 This section describes the various library routines that you may wish
 to use in your plug-in.

 CdErrorReport

 In addition, the following routines may also be useful to plug-in
 writers and are documented in the document Adding Motion Capture
 Servers.

 o CapQuat2Euler

 o CapEuler2Quat

 o CapQuat2Matrix

 o CapMatrix2Quat

 o CapQuat2Matrix3

 o CapMatrix32Quat

 o CapQuatMult

 o CapQuatRotate

 o CapQuatMagnitude

 o CapQuatNormalize

 o CapQuatInvert

 o CapExpandFilename

 CdErrorReport

 Definition

 typedef enum CdSeverity
 {
 	CdInfo,
 	CdWarning,
 	CdError,
 	CdFatal
 } CdSeverity;
 
 void		CdErrorReport
 	(
 		CdSeverity			sev,
 		char			*msg
 	);

-------------------------------------------------
|  Parameter :  Type  :  Description            |
-------------------------------------------------
|  sev       :  Input :  The severity of the    |
|            :        :  error.                 |
-------------------------------------------------
|  msg       :  Input :  The text of the error  |
|            :        :  message. This text     |
|            :        :  will be displayed by   |
|            :        :  Kinemation in the      |
|            :        :  messages window and    |
|            :        :  possibly in a pop-up   |
|            :        :  dialog depending on    |
|            :        :  the severity of the    |
|            :        :  error                  |
-------------------------------------------------
|  Return    :        :  No return value.       |
|  Value     :        :                         |
-------------------------------------------------

 Description

 The routine CdErrorReport() displays a message in an appropriate
 manner within Kinemation. For CdInfo and CdWarning messages, the
 message is displayed in the Messages Window. For CdError messages,
 the message is displayed in the Messages Window and in a pop-up
 dialog. The user must click the Ok button in the dialog before
 continuing. CdFatal messages are for _fatal conditions only!
 Kinemation assumes that further execution is impossible and exits
 after attepting to display the message._

 Compiling your plug-in

 Once you have written your plug-in, you need to compile it and create
 a DSO. For example, if the source code for your plug-in is called my-
 plug-in.c, use the following commands to build and test the DSO:

 cc -c -O2 my-plug-in.c
 ld -shared -soname MyPlugIn \
 	-check_registry \
 		/usr/local/wave/KIN3.0/bin/kin_registry \
 	-rpath /usr/local/wave/KIN3.0/bin/plug-ins \
 	-o MyPlugIn my-plug-in.o
 mv MyPlugIn /usr/local/wave/KIN3.0/bin/plug-ins/MyPlugIn

