devices/deviceinput/ordeviceinput_device.cxx

devices/deviceinput/ordeviceinput_device.cxx
/***************************************************************************************
Autodesk(R) Open Reality(R) Samples
(C) 2009 Autodesk, Inc. and/or its licensors
All rights reserved.
AUTODESK SOFTWARE LICENSE AGREEMENT
Autodesk, Inc. licenses this Software to you only upon the condition that
you accept all of the terms contained in the Software License Agreement ("Agreement")
that is embedded in or that is delivered with this Software. By selecting
the "I ACCEPT" button at the end of the Agreement or by copying, installing,
uploading, accessing or using all or any portion of the Software you agree
to enter into the Agreement. A contract is then formed between Autodesk and
either you personally, if you acquire the Software for yourself, or the company
or other legal entity for which you are acquiring the software.
AUTODESK, INC., MAKES NO WARRANTY, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
PURPOSE REGARDING THESE MATERIALS, AND MAKES SUCH MATERIALS AVAILABLE SOLELY ON AN
"AS-IS" BASIS.
IN NO EVENT SHALL AUTODESK, INC., BE LIABLE TO ANYONE FOR SPECIAL, COLLATERAL,
INCIDENTAL, OR CONSEQUENTIAL DAMAGES IN CONNECTION WITH OR ARISING OUT OF PURCHASE
OR USE OF THESE MATERIALS. THE SOLE AND EXCLUSIVE LIABILITY TO AUTODESK, INC.,
REGARDLESS OF THE FORM OF ACTION, SHALL NOT EXCEED THE PURCHASE PRICE OF THE
MATERIALS DESCRIBED HEREIN.
Autodesk, Inc., reserves the right to revise and improve its products as it sees fit.
Autodesk and Open Reality are registered trademarks or trademarks of Autodesk, Inc.,
in the U.S.A. and/or other countries. All other brand names, product names, or
trademarks belong to their respective holders.
GOVERNMENT USE
Use, duplication, or disclosure by the U.S. Government is subject to restrictions as
set forth in FAR 12.212 (Commercial Computer Software-Restricted Rights) and
DFAR 227.7202 (Rights in Technical Data and Computer Software), as applicable.
Manufacturer is Autodesk, Inc., 10 Duke Street, Montreal, Quebec, Canada, H3C 2L7.
***************************************************************************************/
//--- Class declaration
#include "ordeviceinput_device.h"
//--- Registration defines
#define ORDEVICEINPUT__CLASS ORDEVICEINPUT__CLASSNAME
#define ORDEVICEINPUT__NAME ORDEVICEINPUT__CLASSSTR
#define ORDEVICEINPUT__LABEL "OR - Input Device"
#define ORDEVICEINPUT__DESC "OR - Moving Clock Hands"
#define ORDEVICEINPUT__PREFIX "ClockHands"
//--- FiLMBOX implementation and registration
FBDeviceImplementation ( ORDEVICEINPUT__CLASS );
FBRegisterDevice ( ORDEVICEINPUT__NAME,
ORDEVICEINPUT__CLASS,
ORDEVICEINPUT__LABEL,
ORDEVICEINPUT__DESC,
FB_DEFAULT_SDK_ICON ); // Icon filename (default=Open Reality icon)
/************************************************
* FiLMBOX Constructor.
************************************************/
bool ORDeviceInput::FBCreate()
{
// Add model templates
mRootTemplate = new FBModelTemplate(ORDEVICEINPUT__PREFIX, "Root", kFBModelTemplateRoot);
ModelTemplate.Children.Add(mRootTemplate);
mHierarchyIsDefined = false;
// Device sampling information
SamplingMode = kFBSoftwareTimestamp;
return true;
}
/************************************************
* FiLMBOX Destructor.
************************************************/
void ORDeviceInput::FBDestroy()
{
}
/************************************************
* Device operation.
************************************************/
bool ORDeviceInput::DeviceOperation( kDeviceOperations pOperation )
{
// Must return the online/offline status of device.
switch (pOperation)
{
case kOpInit: return Init();
case kOpStart: return Start();
case kOpStop: return Stop();
case kOpReset: return Reset();
case kOpDone: return Done();
}
return FBDevice::DeviceOperation( pOperation );
}
/************************************************
* Initialization of device.
************************************************/
bool ORDeviceInput::Init()
{
return true;
}
/************************************************
* Removal of device.
************************************************/
bool ORDeviceInput::Done()
{
BeginChannelSetDefinition ();
EndChannelSetDefinition ();
mHierarchyIsDefined = false;
return false;
}
/************************************************
* Device is stopped (offline).
************************************************/
bool ORDeviceInput::Stop()
{
FBProgress lProgress;
lProgress.Caption = "Shutting down device";
// Step 1: Stop data from streaming.
lProgress.Text = "Stopping data stream";
Information = "Stopping data stream";
if(! mHardware.StopDataStream() )
{
Information = "Could not stop data stream.";
}
// Step 2: Close down device
lProgress.Text = "Closing device communication";
Information = "Closing device communication";
if(! mHardware.Close() )
{
Information = "Could not close device";
}
return false;
}
/************************************************
* Device is started (online).
************************************************/
bool ORDeviceInput::Start()
{
FBProgress Progress;
Progress.Caption = "Setting up device";
// Step 1: Open device
if(! mHardware.Open() )
{
Information = "Could not open device";
return false;
}
// Step 2: Ask hardware to get channel information
Progress.Text = "Device found, scanning for channel information...";
Information = "Retrieving channel information";
if(!mHardware.GetSetupInfo())
{
Information = "Could not get channel information from device.";
return false;
}
// Step 3: Create FB channel list and setup hierarchy
BeginChannelSetDefinition();
for(int i=0;i<GetChannelCount();i++)
{
UseChannel(GetChannelName(i));
}
EndChannelSetDefinition();
// Step 4: Start data stream
if(! mHardware.StartDataStream() )
{
Information = "Could not start data stream.";
return false;
}
return true; // if true the device is online
}
/************************************************
* Reset of device.
************************************************/
bool ORDeviceInput::Reset()
{
Stop();
return Start();
}
/************************************************
* Define model template hierarchy.
************************************************/
void ORDeviceInput::DefineHierarchy()
{
if( !mHierarchyIsDefined && mChannels.GetCount() > 0 )
{
//Setup hierarchy
mChannels[PARENT_A]->mModelTemplate->Children.Add(mChannels[CHILD_A]->mModelTemplate);
mChannels[PARENT_B]->mModelTemplate->Children.Add(mChannels[CHILD_B]->mModelTemplate);
// Add new model template as child of template root
mRootTemplate->Children.Add(mChannels[PARENT_A]->mModelTemplate);
mRootTemplate->Children.Add(mChannels[PARENT_B]->mModelTemplate);
mHierarchyIsDefined = true;
}
}
/************************************************
* Start defining the channel set.
************************************************/
void ORDeviceInput::BeginChannelSetDefinition()
{
int i;
// Enter edit mode: tag all channels as unused.
for(i=0; i< mChannels.GetCount(); i++)
{
mChannels[i]->mIsUsed = false;
}
}
/************************************************
* End the channel set definition.
************************************************/
void ORDeviceInput::EndChannelSetDefinition()
{
int i;
// Exit edit mode:
// All used channels: if already defined, don't touch, if new: create animation node and model template
// All unused channels: delete animation nodes and associated model template
for( i = 0; i < mChannels.GetCount(); i++ )
{
if( mChannels[i]->mIsUsed )
{
// Create new translation and rotation animation nodes if necessary
if( !mChannels[i]->mTAnimNode )
{
// We must use a unique name for our connector.
FBString lName( mChannels[i]->mName, " T" );
mChannels[i]->mTAnimNode = AnimationNodeOutCreate( i, lName, ANIMATIONNODE_TYPE_LOCAL_TRANSLATION );
}
if( !mChannels[i]->mRAnimNode )
{
// We must use a unique name for our connector.
FBString lName( mChannels[i]->mName, " R" );
mChannels[i]->mRAnimNode = AnimationNodeOutCreate( i, lName, ANIMATIONNODE_TYPE_LOCAL_ROTATION );
}
// Create new model templates
if(!mChannels[i]->mModelTemplate)
{
mChannels[i]->mModelTemplate = new FBModelTemplate(ORDEVICEINPUT__PREFIX, mChannels[i]->mName, kFBModelTemplateSkeleton);
// Bind model template to T and R animation nodes
mChannels[i]->mModelTemplate->Bindings.Add(mChannels[i]->mTAnimNode);
mChannels[i]->mModelTemplate->Bindings.Add(mChannels[i]->mRAnimNode);
}
}
else //Unused channels: cleanup
{
// Unbind model templates from T and R animation nodes
if( mChannels[i]->mTAnimNode )
{
if( mChannels[i]->mModelTemplate )
{
mChannels[i]->mModelTemplate->Bindings.Remove(mChannels[i]->mTAnimNode);
}
}
if( mChannels[i]->mRAnimNode )
{
if( mChannels[i]->mModelTemplate )
{
mChannels[i]->mModelTemplate->Bindings.Remove(mChannels[i]->mRAnimNode);
}
}
// Remove as child of root template
if( mRootTemplate->Children.Find(mChannels[i]->mModelTemplate) >= 0 )
{
mRootTemplate->Children.Remove(mChannels[i]->mModelTemplate);
}
if( mChannels[i]->mModelTemplate )
{
for( int j = 0; j < mChannels[i]->mModelTemplate->Children.GetCount(); j++ )
{
mChannels[i]->mModelTemplate->Children.RemoveAt(0);
}
}
// Delete model template
delete mChannels[i]->mModelTemplate;
mChannels[i]->mModelTemplate = NULL;
// Destroy unused animation nodes
if( mChannels[i]->mTAnimNode )
{
AnimationNodeDestroy(mChannels[i]->mTAnimNode);
}
if( mChannels[i]->mRAnimNode )
{
AnimationNodeDestroy(mChannels[i]->mRAnimNode);
}
mChannels[i]->mTAnimNode = NULL;
mChannels[i]->mRAnimNode = NULL;
}
}
//Delete entry in list for all unused channels
for( i = mChannels.GetCount() - 1; i >= 0; i-- )
{
if( !mChannels[i]->mIsUsed )
{
delete mChannels[i];
mChannels.SetAt(i, NULL);
mChannels.RemoveAt(i);
}
}
//Make sure reference number still match
for( i = 0; i < mChannels.GetCount(); i++ )
{
if( mChannels[i]->mTAnimNode )
{
mChannels[i]->mTAnimNode->Reference = i;
}
}
//Define hierarchy if needed
DefineHierarchy();
}
/************************************************
* Use the channel pName.
************************************************/
int ORDeviceInput::UseChannel(char *pName)
{
char lName[256];
strcpy(lName,pName);
// FB cannot deal with spaces in names replace with '_'
for(unsigned int j = 0; j < strlen(lName); j++)
{
if (lName[j]==' ') lName[j] = '_';
}
for(int i=0;i<mChannels.GetCount();i++)
{
if(mChannels[i]->mName == lName)
{
mChannels[i]->mIsUsed = true;
return i;
}
}
DataChannel *newChannel = new DataChannel;
newChannel->mName = lName;
newChannel->mIsUsed = true;
return mChannels.Add(newChannel);
}
/************************************************
* Real-Time Engine Evaluation.
************************************************/
bool ORDeviceInput::AnimationNodeNotify(FBAnimationNode* pAnimationNode ,FBEvaluateInfo* pEvaluateInfo)
{
double Pos[3];
double Rot[3];
int Index = pAnimationNode->Reference;
Pos[0] = GetDataTX(Index);
Pos[1] = GetDataTY(Index);
Pos[2] = GetDataTZ(Index);
Rot[0] = GetDataRX(Index);
Rot[1] = GetDataRY(Index);
Rot[2] = GetDataRZ(Index);
if(mChannels[Index]->mTAnimNode && mChannels[Index]->mRAnimNode)
{
mChannels[Index]->mRAnimNode->WriteData( Rot, pEvaluateInfo );
mChannels[Index]->mTAnimNode->WriteData( Pos, pEvaluateInfo );
}
return true;
}
/************************************************
* Real-Time Synchronous Device IO.
************************************************/
void ORDeviceInput::DeviceIONotify( kDeviceIOs pAction,FBDeviceNotifyInfo &pDeviceNotifyInfo)
{
FBTime lEvalTime;
switch (pAction)
{
case kIOPlayModeWrite:
case kIOStopModeWrite:
{
// Output devices
}
break;
case kIOStopModeRead:
case kIOPlayModeRead:
{
// Input devices
while(mHardware.FetchDataPacket(lEvalTime))
{
//if( pAction == kIOPlayModeRead )
//{
DeviceRecordFrame (lEvalTime,pDeviceNotifyInfo);
//}
AckOneSampleReceived();
}
}
break;
}
}
/************************************************
* Record a frame of the device (recording).
************************************************/
void ORDeviceInput::DeviceRecordFrame(FBTime &pTime,FBDeviceNotifyInfo &pDeviceNotifyInfo)
{
int i;
FBAnimationNode* Data;
pTime = mSystem.LocalTime;
double Pos[3];
double Rot[3];
for (i=0; i<mChannels.GetCount(); i++)
{
if(mChannels[i]->mIsUsed)
{
// Translation information.
if (mChannels[i]->mTAnimNode)
{
Data = mChannels[i]->mTAnimNode->GetAnimationToRecord();
if (Data)
{
if( mPlayerControl.GetTransportMode() == kFBTransportPlay )
{
Pos[0] = GetDataTX(i);
Pos[1] = GetDataTY(i);
Pos[2] = GetDataTZ(i);
switch( SamplingMode )
{
Data->KeyAdd(pTime, Pos);
break;
Data->KeyAdd(Pos);
break;
}
}
}
}
// Rotation information.
if (mChannels[i]->mRAnimNode)
{
Data = mChannels[i]->mRAnimNode->GetAnimationToRecord();
if (Data)
{
if( mPlayerControl.GetTransportMode() == kFBTransportPlay )
{
Rot[0] = GetDataRX(i);
Rot[1] = GetDataRY(i);
Rot[2] = GetDataRZ(i);
switch( SamplingMode )
{
Data->KeyAdd(pTime, Rot);
break;
Data->KeyAdd(Rot);
break;
}
}
}
}
}
}
}
//--- FBX load/save tags
#define FBX_CHANNEL_TAG "Channels"
#define FBX_VERSION_TAG "Version"
/************************************************
* Store data in FBX.
************************************************/
bool ORDeviceInput::FbxStore(FBFbxObject* pFbxObject,kFbxObjectStore pStoreWhat)
{
if (pStoreWhat & kAttributes)
{
pFbxObject->FieldWriteI(FBX_VERSION_TAG,100);
pFbxObject->FieldWriteBegin(FBX_CHANNEL_TAG);
for (int i=0;i<mChannels.GetCount();i++)
{
if(mChannels[i]->mIsUsed)
{
pFbxObject->FieldWriteC(mChannels[i]->mName);
}
}
pFbxObject->FieldWriteEnd();
}
return true;
}
/************************************************
* Retrieve data from FBX.
************************************************/
bool ORDeviceInput::FbxRetrieve(FBFbxObject* pFbxObject,kFbxObjectStore pStoreWhat)
{
int Version;
FBString name;
if( pStoreWhat & kAttributes )
{
BeginChannelSetDefinition();
Version = pFbxObject->FieldReadI(FBX_VERSION_TAG);
if( pFbxObject->FieldReadBegin(FBX_CHANNEL_TAG) )
{
int channelCount = pFbxObject->FieldReadGetCount();
for( int i = 0; i < channelCount; i++ )
{
name = pFbxObject->FieldReadC();
UseChannel(name);
}
pFbxObject->FieldReadEnd();
}
EndChannelSetDefinition();
}
return true;
}