#include "ordeviceopticalhybrid_device.h"
#define ORDEVICEOPTICALHYBRIDTEMPLATE_CLASS ORDEVICEOPTICALHYBRIDTEMPLATE_CLASSNAME
#define ORDEVICEOPTICALHYBRIDTEMPLATE_NAME ORDEVICEOPTICALHYBRIDTEMPLATE_CLASSSTR
#define ORDEVICEOPTICALHYBRIDTEMPLATE_LABEL "OR - Optical Device Hybrid Template"
#define ORDEVICEOPTICALHYBRIDTEMPLATE_DESC "OR - Optical Device Hybrid Template Description"
#define ORDEVICEOPTICALHYBRIDTEMPLATE_PREFIX "OpticalTemplate"
#define ORDEVICEOPTICALHYBRIDTEMPLATE_ICON "processor_optical.png"
ORDEVICEOPTICALHYBRIDTEMPLATE_CLASS,
ORDEVICEOPTICALHYBRIDTEMPLATE_LABEL,
ORDEVICEOPTICALHYBRIDTEMPLATE_DESC,
ORDEVICEOPTICALHYBRIDTEMPLATE_ICON );
bool ORDeviceOpticalHybrid_Template::FBCreate()
{
FBDeviceOptical::FBCreate();
mRootTemplate =
new FBModelTemplate(ORDEVICEOPTICALHYBRIDTEMPLATE_PREFIX,
"Reference",
kFBModelTemplateRoot);
ModelTemplate.Children.Add(mRootTemplate);
mHierarchyIsDefined = false;
mHasAnimationToTranspose = false;
mPlotting = false;
SamplingPeriod = FBTime(0,0,1)/SIM_FPS;
UseReferenceTransformation = true;
SupportOcclusion = true;
FBSystem().TheOne().OnUIIdle.Add(
this,(
FBCallback) &ORDeviceOpticalHybrid_Template::EventUIIdle );
return true;
}
void ORDeviceOpticalHybrid_Template::FBDestroy()
{
ParentClass::FBDestroy();
FBSystem().TheOne().OnUIIdle.Remove(
this,(
FBCallback) &ORDeviceOpticalHybrid_Template::EventUIIdle );
}
bool ORDeviceOpticalHybrid_Template::DeviceOperation( kDeviceOperations pOperation )
{
switch (pOperation)
{
case kOpInit: return Init();
case kOpStart: return Start();
case kOpAutoDetect: break;
case kOpStop: return Stop();
case kOpReset: return Reset();
case kOpDone: return Done();
}
return ParentClass::DeviceOperation(pOperation);
}
bool ORDeviceOpticalHybrid_Template::Init()
{
FBProgress lProgress;
lProgress.Caption = "Device";
lProgress.Text = "Initializing device...";
if(!mHardware.GetSetupInfo())
{
Information = "Could not get channel information from device.";
return false;
}
mHierarchyIsDefined = false;
Bind();
return true;
}
bool ORDeviceOpticalHybrid_Template::Done()
{
FBProgress lProgress;
lProgress.Caption = "Device";
lProgress.Text = "Shutting down device...";
UnBind();
return true;
}
bool ORDeviceOpticalHybrid_Template::Reset()
{
Stop();
return Start();
}
bool ORDeviceOpticalHybrid_Template::Start()
{
FBProgress Progress;
Progress.Caption = "Setting up device";
if(! mHardware.Open() )
{
Information = "Could not open device";
return false;
}
Progress.Text = "Device found, scanning for channel information...";
Information = "Retrieving channel information";
if(!mHardware.GetSetupInfo())
{
Information = "Cannot connect to server.";
return false;
}
if(mHardware.GetMarkerCount() <= 0)
{
Information = "No markers found.";
return false;
}
DeviceOpticalBeginSetup();
for(
int i=0;
i<mHardware.GetMarkerCount();
i++)
{
Markers.Add(mHardware.GetMarkerName(
i));
}
DeviceOpticalEndSetup();
if(! mHardware.StartDataStream() )
{
Information = "Could not start data stream.";
return false;
}
return true;
}
bool ORDeviceOpticalHybrid_Template::Stop()
{
FBProgress lProgress;
lProgress.Caption = "Shutting down device";
if( ! mHardware.StopDataStream() )
{
Information = "Could not stop data stream.";
return false;
}
lProgress.Text = "Closing device communication";
Information = "Closing device communication";
if( ! mHardware.Close() )
{
Information = "Could not close device";
return false;
}
return false;
}
void ORDeviceOpticalHybrid_Template::DefineHierarchy()
{
if( !mHierarchyIsDefined && GetChannelCount() > 0 )
{
int lParent;
for(
int i=0;
i< GetChannelCount();
i++)
{
lParent = GetChannelParent(
i);
if(lParent == -1)
{
mRootTemplate->Children.Add(mChannels[
i].mModelTemplate);
}
else
{
mChannels[lParent].mModelTemplate->Children.Add(mChannels[
i].mModelTemplate);
}
}
mHierarchyIsDefined = true;
}
}
bool ORDeviceOpticalHybrid_Template::ReadyForCharacterize()
{
return mHierarchyIsDefined && GetChannelCount() > 0;
}
void ORDeviceOpticalHybrid_Template::ProcessGlobalToLocal()
{
SetupLocalGlobal(true);
FBModel* lModel;
for(i = 0; i < lModels.GetCount(); i++)
lModels[i]->Selected = false;
lModels.Clear();
for(i = 0; i < GetChannelCount(); i++)
{
if(mChannels[i].mModelTemplate && (lModel = mChannels[i].mModelTemplate->Model) !=
NULL)
{
lModel->Selected = true;
lModels.Add(lModel);
}
}
mPlotting = true;
mSystem.CurrentTake->PlotTakeOnSelected(SamplingPeriod);
mPlotting = false;
for(i = 0; i < lModels.GetCount(); i++)
lModels[i]->Selected = false;
SetupLocalGlobal(false);
bool ApplyReferenceTransformation = UseReferenceTransformation && mRootTemplate->Model;
if(ApplyReferenceTransformation)
{
mRootTemplate->Model->Translation.SetData(&x);
mRootTemplate->Model->Rotation.SetData(&x);
}
}
void ORDeviceOpticalHybrid_Template::SetupLocalGlobal(bool pGlobal)
{
for( int i = 0; i < GetChannelCount(); i++ )
{
if( mChannels[i].mTAnimNode )
{
mChannels[
i].mTAnimNode->SetBufferType(pGlobal);
}
if( mChannels[i].mRAnimNode )
{
mChannels[
i].mRAnimNode->SetBufferType(pGlobal);
}
}
}
bool ORDeviceOpticalHybrid_Template::ModelTemplateUnBindNotify( int pIndex, FBModelTemplate* pModelTemplate)
{
if(pModelTemplate->Model && pModelTemplate->Model == mRootTemplate->Model && !ObjectInProcess)
{
FBPlayerControl().TheOne().EvaluationPause();
for( int i = 0; i < GetChannelCount(); i++ )
{
if(mChannels[i].mTAnimNode)
{
mChannels[
i].mModelTemplate->Model->Translation.SetAnimated(
true);
FBAnimationNode* lNode = mChannels[
i].mModelTemplate->Model->Translation.GetAnimationNode();
FBVector3d lVector(mChannels[i].mModelTemplate->Model->Translation);
lNode->SetCandidate(lVector.mValue);
}
if(mChannels[i].mRAnimNode)
{
mChannels[
i].mModelTemplate->Model->Rotation.SetAnimated(
true);
FBAnimationNode* lNode = mChannels[
i].mModelTemplate->Model->Rotation.GetAnimationNode();
FBVector3d lVector(mChannels[i].mModelTemplate->Model->Rotation);
lNode->SetCandidate(lVector.mValue);
}
}
FBPlayerControl().TheOne().EvaluationResume();
}
return true;
}
void ORDeviceOpticalHybrid_Template::Bind()
{
for( i = 0; i < GetChannelCount(); i++ )
{
if( !mChannels[i].mTAnimNode )
{
FBString lName( GetChannelName(i), " T" );
}
if( !mChannels[i].mRAnimNode )
{
FBString lName( GetChannelName(i), " R" );
}
if(!mChannels[i].mModelTemplate)
{
mChannels[
i].mModelTemplate =
new FBModelTemplate(ORDEVICEOPTICALHYBRIDTEMPLATE_PREFIX, GetChannelName(i),
kFBModelTemplateSkeleton);
mChannels[
i].mModelTemplate->Bindings.Add(mChannels[i].mTAnimNode);
mChannels[
i].mModelTemplate->Bindings.Add(mChannels[i].mRAnimNode);
}
}
DefineHierarchy();
}
void ORDeviceOpticalHybrid_Template::UnBind()
{
for( i = 0; i < GetChannelCount(); i++ )
{
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);
}
}
if( mRootTemplate->Children.Find(mChannels[i].mModelTemplate) >= 0 )
{
mRootTemplate->Children.Remove(mChannels[i].mModelTemplate);
}
if( mChannels[i].mTAnimNode )
{
AnimationNodeDestroy(mChannels[i].mTAnimNode);
}
if( mChannels[i].mRAnimNode )
{
AnimationNodeDestroy(mChannels[i].mRAnimNode);
}
mChannels[
i].mTAnimNode =
NULL;
mChannels[
i].mRAnimNode =
NULL;
if( mChannels[i].mModelTemplate )
{
mChannels[
i].mModelTemplate->Children.RemoveAll();
}
}
for( i = 0; i < GetChannelCount(); i++ )
{
delete mChannels[
i].mModelTemplate;
mChannels[
i].mModelTemplate =
NULL;
}
}
bool ORDeviceOpticalHybrid_Template::AnimationNodeNotify(FBAnimationNode* pAnimationNode ,FBEvaluateInfo* pEvaluateInfo)
{
kReference lReference = pAnimationNode->Reference;
if (lReference>=10000)
{
FBSVector Scal;
FBMatrix GlobalNodeTransformation, GlobalReferenceTransformation;
bool ApplyReferenceTransformation = UseReferenceTransformation && mRootTemplate->Model;
if(ApplyReferenceTransformation)
{
mRootTemplate->Model->GetMatrix(GlobalReferenceTransformation,
kModelTransformation,
true,pEvaluateInfo);
}
for(int i=0;i<GetChannelCount();i++)
{
if(mChannels[i].mTAnimNode && mChannels[i].mRAnimNode)
{
bool lDontWrite = false;
if ((!pAnimationNode->Live || mPlotting) && mChannels[
i].mModelTemplate->Model)
{
mChannels[
i].mModelTemplate->Model->Translation.GetAnimationNode()->Evaluate(Pos.mValue,pEvaluateInfo->GetLocalTime(),
false);
mChannels[
i].mModelTemplate->Model->Rotation.GetAnimationNode()->Evaluate(Rot.mValue,pEvaluateInfo->GetLocalTime(),
false);
} else if (pAnimationNode->Live)
{
Pos[0] = mHardware.GetDataTX(i);
Pos[1] = mHardware.GetDataTY(i);
Pos[2] = mHardware.GetDataTZ(i);
Rot[0] = mHardware.GetDataRX(i);
Rot[1] = mHardware.GetDataRY(i);
Rot[2] = mHardware.GetDataRZ(i);
} else
{
lDontWrite = true;
}
if(ApplyReferenceTransformation)
{
FBGetGlobalMatrix(GlobalNodeTransformation,GlobalReferenceTransformation,GlobalNodeTransformation);
}
if(!lDontWrite)
{
if (!pAnimationNode->Live || mPlotting)
{
mChannels[
i].mRAnimNode->WriteData( Rot.mValue, pEvaluateInfo );
mChannels[
i].mTAnimNode->WriteData( Pos.mValue, pEvaluateInfo );
} else
{
mChannels[
i].mRAnimNode->WriteGlobalData( Rot.mValue, pEvaluateInfo );
mChannels[
i].mTAnimNode->WriteGlobalData( Pos.mValue, pEvaluateInfo );
}
}
}
}
}
return FBDeviceOptical::AnimationNodeNotify( pAnimationNode , pEvaluateInfo);
}
void ORDeviceOpticalHybrid_Template::DeviceTransportNotify( kTransportMode pMode, FBTime pTime, FBTime pSystem )
{
if(pMode==kPreparePlay)
{
mHardware.ResetPacketTimeOffset(pTime);
}
}
void ORDeviceOpticalHybrid_Template::DeviceOpticalEvalAllMarkers(FBDeviceNotifyInfo &pDeviceNotifyInfo)
{
FBTime lEvalTime;
lEvalTime = pDeviceNotifyInfo.GetLocalTime();
while(mHardware.FetchDataPacket(lEvalTime))
{
for (i=0;i<Markers.GetCount();i ++)
{
Markers[
i]->SetData(mHardware.GetDataX(i),
mHardware.GetDataY(i),
mHardware.GetDataZ(i),
mHardware.GetDataO(i));
}
DeviceOpticalRecordFrame(lEvalTime,pDeviceNotifyInfo);
DeviceRecordFrame(lEvalTime,pDeviceNotifyInfo);
}
}
void ORDeviceOpticalHybrid_Template::RecordingDoneAnimation( FBAnimationNode* pAnimationNode )
{
FBDeviceOptical::RecordingDoneAnimation( pAnimationNode );
mHasAnimationToTranspose = true;
}
void ORDeviceOpticalHybrid_Template::DeviceRecordFrame(FBTime &pTime,FBDeviceNotifyInfo &pDeviceNotifyInfo)
{
{
FBAnimationNode* Data;
bool ApplyReferenceTransformation = UseReferenceTransformation && mRootTemplate->Model;
FBMatrix GlobalReferenceTransformation;
if(ApplyReferenceTransformation)
for (i=0; i<GetChannelCount(); i++)
{
if (mChannels[i].mTAnimNode)
{
Data = mChannels[
i].mTAnimNode->GetAnimationToRecord();
if (Data)
{
Pos[0] = mHardware.GetDataTX(i);
Pos[1] = mHardware.GetDataTY(i);
Pos[2] = mHardware.GetDataTZ(i);
if(ApplyReferenceTransformation)
Data->KeyAdd(pTime, Pos);
}
}
if (mChannels[i].mRAnimNode)
{
Data = mChannels[
i].mRAnimNode->GetAnimationToRecord();
if (Data)
{
Rot[0] = mHardware.GetDataRX(i);
Rot[1] = mHardware.GetDataRY(i);
Rot[2] = mHardware.GetDataRZ(i);
if(ApplyReferenceTransformation)
{
FBMatrix GRM;
}
Data->KeyAdd(pTime, Rot);
}
}
}
}
}
bool ORDeviceOpticalHybrid_Template::FbxStore(FBFbxObject* pFbxObject,
kFbxObjectStore pStoreWhat)
{
FBDeviceOptical::FbxStore(pFbxObject, pStoreWhat);
{
pFbxObject->FieldWriteC ( "NetworkAddress", mHardware.GetNetworkAddress() );
pFbxObject->FieldWriteI ( "NetworkPort", mHardware.GetNetworkPort() );
}
return true;
}
bool ORDeviceOpticalHybrid_Template::FbxRetrieve(FBFbxObject* pFbxObject,
kFbxObjectStore pStoreWhat)
{
FBDeviceOptical::FbxRetrieve(pFbxObject, pStoreWhat);
if (pStoreWhat & kAttributes)
{
mHardware.SetNetworkAddress ( pFbxObject->FieldReadC( "NetworkAddress") );
mHardware.SetNetworkPort ( pFbxObject->FieldReadI( "NetworkPort") );
}
return true;
}
void ORDeviceOpticalHybrid_Template::EventUIIdle(
HISender pSender,
HKEvent pEvent )
{
if(mHasAnimationToTranspose)
{
mHasAnimationToTranspose = false;
ProcessGlobalToLocal();
}
}
void ORDeviceOpticalHybrid_Template::RigidBodySetup()
{
double a = 10;
double lSnapPos[4][3];
lSnapPos[0][0] = 0.0; lSnapPos[0][1] = 0.0; lSnapPos[0][2] = 0.0;
lSnapPos[1][0] = a; lSnapPos[1][1] = 0.0; lSnapPos[1][2] = 0.0;
lSnapPos[2][0] = 0; lSnapPos[2][1] = a; lSnapPos[2][2] = 0.0;
lSnapPos[3][0] = 0; lSnapPos[3][1] = 0.0; lSnapPos[3][2] = a;
FBString lRBName("FBSDK_RigidBody");
FBString lMarkerName;
FBModelOptical* lModelOptical = (FBModelOptical*) (FBModel*)(this->ModelOptical);
if(lModelOptical)
{
FBString lBase(this->
Name);
FBString lSep(":");
FBString lModelOpticalName(lModelOptical->Name);
FBString lName;
lBase += lSep;
lBase += lModelOpticalName;
lBase += lSep;
for( i=0;i<mHardware.GetMarkerCount();i++)
{
FBString lMarkerName( mHardware.GetMarkerName(i) );
lName = lBase;
lName += lMarkerName;
if( lModel )
{
lPos.Set( lSnapPos[i] );
lList.Add( lModel );
}
}
lModelOptical->RigidBodies.Add( lList, lRBName );
lModelOptical->RigidBodies[lModelOptical->RigidBodies.GetCount() - 1].Snap();
}
}