devices/deviceoptical/ordeviceoptical_device.cxx

devices/deviceoptical/ordeviceoptical_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 "ordeviceoptical_device.h"
//--- Registration defines
#define ORDEVICEOPTICALTEMPLATE__CLASS ORDEVICEOPTICALTEMPLATE__CLASSNAME
#define ORDEVICEOPTICALTEMPLATE__NAME ORDEVICEOPTICALTEMPLATE__CLASSSTR
#define ORDEVICEOPTICALTEMPLATE__LABEL "OR - Optical Device Template"
#define ORDEVICEOPTICALTEMPLATE__DESC "OR - Optical Device Template Description"
#define ORDEVICEOPTICALTEMPLATE__PREFIX "OpticalTemplate"
//--- FiLMBOX implementation and registration
FBDeviceImplementation ( ORDEVICEOPTICALTEMPLATE__CLASS );
FBRegisterDevice ( ORDEVICEOPTICALTEMPLATE__NAME,
ORDEVICEOPTICALTEMPLATE__CLASS,
ORDEVICEOPTICALTEMPLATE__LABEL,
ORDEVICEOPTICALTEMPLATE__DESC,
"processor_optical.png" );
/************************************************
* FiLMBOX Constructor.
************************************************/
bool ORDeviceOptical_Template::FBCreate()
{
// Propagate to parent
FBDeviceOptical::FBCreate();
// Optical device properties
SupportOcclusion = true;
return true;
}
/************************************************
* FiLMBOX Destructor.
************************************************/
void ORDeviceOptical_Template::FBDestroy()
{
// Propagate to parent
FBDeviceOptical::FBDestroy();
}
/************************************************
* Operate the device.
************************************************/
bool ORDeviceOptical_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 FBDeviceOptical::DeviceOperation(pOperation);
}
/************************************************
* Real-time Evaluation Engine.
* In the case of an optical device, this function is not to
* be modified.
************************************************/
bool ORDeviceOptical_Template::AnimationNodeNotify(FBAnimationNode* pConnector ,FBEvaluateInfo* pEvaluateInfo)
{
return FBDeviceOptical::AnimationNodeNotify( pConnector , pEvaluateInfo);
}
/************************************************
* Real-Time Synchronous Device IO.
* In the case of an optical device, this function is not to
* be modified.
************************************************/
void ORDeviceOptical_Template::DeviceIONotify( kDeviceIOs pAction,FBDeviceNotifyInfo &pDeviceNotifyInfo)
{
FBDeviceOptical::DeviceIONotify(pAction,pDeviceNotifyInfo);
}
/************************************************
* Transport notication.
* Useful to compute time offset between Hardware time and System time
* PreparePlay is called just before play
************************************************/
void ORDeviceOptical_Template::DeviceTransportNotify( kTransportMode pMode, FBTime pTime, FBTime pSystem )
{
if(pMode==kPreparePlay)
{
mHardware.ResetPacketTimeOffset(pTime);
}
}
/************************************************
* Initialize the device.
************************************************/
bool ORDeviceOptical_Template::Init()
{
FBProgress lProgress;
lProgress.Caption = "Optical device";
lProgress.Text = "Initializing device...";
/*
* Add device creation code here.
*/
return true;
}
/************************************************
* Removal of the device.
************************************************/
bool ORDeviceOptical_Template::Done()
{
FBProgress lProgress;
lProgress.Caption = "Optical device";
lProgress.Text = "Shutting down device...";
/*
* Add device removal code here.
*/
return true;
}
/************************************************
* Reset of the device.
************************************************/
bool ORDeviceOptical_Template::Reset()
{
Stop();
return Start();
}
/************************************************
* Start up the device.
************************************************/
bool ORDeviceOptical_Template::Start()
{
FBProgress lProgress;
bool lSuccess;
int i;
lProgress.Caption = "Optical Device Template";
lProgress.Text = "Connecting to device";
lSuccess = mHardware.Open();
// Get the server information
lProgress.Text = "Requesting information from server...";
lSuccess = mHardware.GetSetupInfo();
if(!lSuccess)
{
Information = "Cannot connect to server.";
return false;
}
lSuccess = (mHardware.GetMarkerCount() > 0 );
if(!lSuccess)
{
Information = "No markers found.";
return false;
}
// Create Marker Set
DeviceOpticalBeginSetup();
for(i=0;i<mHardware.GetMarkerCount();i++)
{
Markers.Add(mHardware.GetMarkerName(i));
}
DeviceOpticalEndSetup();
lProgress.Text = "Starting data stream...";
lSuccess = mHardware.StartDataStream();
if(!lSuccess)
{
Information = "Could not put device in streaming mode";
return false;
}
return lSuccess;
}
/************************************************
* Rigid body setup.
************************************************/
void ORDeviceOptical_Template::RigidBodySetup()
{
//---- Steps to create a rigid body
// 1. Establish a snap position
// 2. Create rigid body set with snap positions.
// 3. Add rigid body set to optical model.
double a = 10; // base distance for markers
double lSnapPos[4][3]; // Snap positions (used to calculate relative distances)
int i;
// Set snap positions for markers
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;
// Rigidbody & marker names
FBString lRBName("FBSDK_RigidBody");
FBString lMarkerName;
FBVector3d lPos;
// Get a handle onto the optical model of the device.
FBModelOptical* lModelOptical = (FBModelOptical*) (FBModel*)(this->ModelOptical);
// Affect model optical
if(lModelOptical)
{
FBModelList lList;
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;
FBModel* lModel = FBFindModelByLabelName( lName );
if( lModel )
{
lPos.Set( lSnapPos[i] );
lModel->SetVector( lPos, kModelTranslation, true );
lList.Add( lModel );
}
}
lModelOptical->RigidBodies.Add( lList, lRBName );
lModelOptical->RigidBodies[lModelOptical->RigidBodies.GetCount() - 1].Snap();
}
}
/************************************************
* Stop the device.
************************************************/
bool ORDeviceOptical_Template::Stop()
{
FBProgress lProgress;
lProgress.Caption = "Optical device";
lProgress.Text = "Stopping data stream...";
if( ! mHardware.StopDataStream() )
{
Information = "Could not stop streaming";
return false;
}
lProgress.Text = "Closing connection...";
if( ! mHardware.Close() )
{
Information = "Could not close device";
return false;
}
return false;
}
/************************************************
* Real-time Optical Evaluation Engine.
* This is the core of the evaluation for the optical
* device, please read the comment carefully.
************************************************/
void ORDeviceOptical_Template::DeviceOpticalEvalAllMarkers(FBDeviceNotifyInfo &pDeviceNotifyInfo)
{
FBTime lEvalTime;
int i;
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);
}
return;
}
/************************************************
* FBX Storage.
************************************************/
bool ORDeviceOptical_Template::FbxStore(FBFbxObject* pFbxObject,kFbxObjectStore pStoreWhat)
{
// Propagate to parent
FBDeviceOptical::FbxStore(pFbxObject, pStoreWhat);
if (pStoreWhat & kAttributes)
{
pFbxObject->FieldWriteC ( "NetworkAddress", mHardware.GetNetworkAddress() );
pFbxObject->FieldWriteI ( "NetworkPort", mHardware.GetNetworkPort() );
}
return true;
}
/************************************************
* FBX Retrieval.
************************************************/
bool ORDeviceOptical_Template::FbxRetrieve(FBFbxObject* pFbxObject,kFbxObjectStore pStoreWhat)
{
// Propagate to parent
FBDeviceOptical::FbxRetrieve(pFbxObject, pStoreWhat);
if (pStoreWhat & kAttributes)
{
mHardware.SetNetworkAddress ( pFbxObject->FieldReadC( "NetworkAddress") );
mHardware.SetNetworkPort ( pFbxObject->FieldReadI( "NetworkPort") );
}
return true;
}