15 #ifdef MITK_USE_OPTITRACK_TRACKER 22 #include <NPTrackingTools.h> 40 this->m_MultiThreader = itk::MultiThreader::New();
41 this->m_ToolsMutex = itk::FastMutexLock::New();
44 SetData(mitk::DeviceDataNPOptitrack);
47 this->m_AllTools.clear();
55 MITK_DEBUG <<
"Deleting OptitrackTrackingDevice";
61 MITK_DEBUG <<
"OptitrackTrackingDevice in Tracking State -> Stopping Tracking";
62 result = this->StopTracking();
64 if(result == NPRESULT_SUCCESS){
65 MITK_INFO <<
"OptitrackTrackingDevice Stopped";
77 MITK_DEBUG <<
"OptitrackTrackingDevice in Ready State -> Closing the Connection";
78 result = this->CloseConnection();
82 MITK_INFO <<
"OptitrackTrackingDevice Connection closed";
86 MITK_DEBUG <<
"Error during Closing Connection";
92 m_initialized =
false;
95 MITK_DEBUG <<
"OptitrackTrackingDevice deleted successfully";
106 MITK_DEBUG <<
"Initialize Optitrack Tracking System";
108 if( this->InitializeCameras() )
110 m_initialized =
true;
112 if(this->m_calibrationPath.empty()){
113 MITK_INFO <<
"Numer of connected cameras = " << TT_CameraCount();
114 MITK_WARN <<
"Attention: No calibration File defined !!";
115 return m_initialized;
119 this->LoadCalibration();
124 m_initialized =
false;
126 MITK_INFO <<
"Device initialization failed. Device is still in setup state";
131 return m_initialized;
142 result = TT_Initialize();
144 if(result == NPRESULT_SUCCESS)
146 MITK_DEBUG <<
"Optitrack Initialization Succeed";
165 int resultLoadCalibration;
168 if(this->m_calibrationPath.empty()){
169 MITK_INFO <<
"Calibration Path is empty";
175 if(this->m_initialized)
180 resultLoadCalibration = TT_LoadCalibration(this->m_calibrationPath.c_str());
182 if(resultLoadCalibration != NPRESULT_SUCCESS)
189 MITK_DEBUG <<
"Calibration file has been loaded successfully";
195 MITK_INFO <<
"System cannot load a calibration file";
201 MITK_INFO <<
"System is not ready for load a calibration file because it has not been initialized yet";
219 if(calibrationPath.empty())
221 MITK_INFO <<
"Calibration Path is empty";
226 this->m_calibrationPath = calibrationPath;
227 MITK_INFO <<
"Calibration Path has been updated to: " << this->m_calibrationPath;
237 int resultStop, resultShutdown;
243 MITK_DEBUG <<
"Device state: Tracking -> Stoping the Tracking";
244 resultStop = this->StopTracking();
252 TT_ClearTrackableList();
253 resultShutdown = TT_Shutdown();
255 if(resultShutdown == NPRESULT_SUCCESS)
257 MITK_DEBUG <<
"System has been Shutdown Correctly";
263 MITK_DEBUG <<
"System cannot ShutDown now. Trying again...";
267 MITK_INFO <<
"System cannot ShutDown now";
273 MITK_INFO <<
"System has not been initialized. Close connection cannot be done";
287 bool resultIsTrackableTracked;
291 MITK_INFO <<
"System is not in State Ready -> Cannot StartTracking";
299 this->m_StopTrackingMutex->Lock();
300 this->m_StopTracking =
false;
301 this->m_StopTrackingMutex->Unlock();
311 m_ThreadID = m_MultiThreader->SpawnThread(this->ThreadStartTracking,
this);
314 if(GetToolCount() == 0)
MITK_INFO <<
"No tools are defined";
316 for (
int i = 0; i < GetToolCount(); ++i)
318 resultIsTrackableTracked = TT_IsTrackableTracked(i);
319 if(resultIsTrackableTracked)
321 MITK_DEBUG <<
"Trackable " << i <<
" is inside the Tracking Volume and it is Tracked";
325 MITK_DEBUG <<
"Trackable " << i <<
" is not been tracked. Check if it is inside the Tracking volume";
343 m_StopTrackingMutex->Lock();
344 m_StopTrackingMutex->Unlock();
349 m_TrackingFinishedMutex->Unlock();
350 MITK_INFO <<
"System is not in State Tracking -> Cannot StopTracking";
362 m_TrackingFinishedMutex->Unlock();
374 struct itk::MultiThreader::ThreadInfoStruct * pInfo = (
struct itk::MultiThreader::ThreadInfoStruct*)pInfoStruct;
376 if (pInfo ==
nullptr)
378 return ITK_THREAD_RETURN_VALUE;
381 if (pInfo->UserData ==
nullptr)
383 return ITK_THREAD_RETURN_VALUE;
386 OptitrackTrackingDevice *trackingDevice =
static_cast<OptitrackTrackingDevice*
>(pInfo->UserData);
388 if (trackingDevice !=
nullptr)
391 trackingDevice->TrackTools();
398 trackingDevice->m_ThreadID = -1;
399 return ITK_THREAD_RETURN_VALUE;
408 OptitrackTrackingTool* t =
nullptr;
411 if(toolNumber < m_AllTools.size())
413 t = m_AllTools.at(toolNumber);
417 MITK_INFO <<
"The tool numbered " << toolNumber <<
" does not exist";
430 return (
int)(this->m_AllTools.size());
445 bool localStopTracking;
446 this->m_StopTrackingMutex->Lock();
447 localStopTracking = this->m_StopTracking;
450 if (!localStopTracking)
452 m_TrackingFinishedMutex->Lock();
455 this->m_StopTrackingMutex->Unlock();
459 for (
int i = 0; i < GetToolCount(); ++i)
461 OptitrackTrackingTool* currentTool = this->GetOptitrackTool(i);
462 if(currentTool !=
nullptr)
464 currentTool->updateTool();
465 MITK_DEBUG <<
"Tool number " << i <<
" updated position";
469 MITK_DEBUG <<
"Get data from tool number " << i <<
" failed";
475 this->m_StopTrackingMutex->Lock();
476 localStopTracking = m_StopTracking;
477 this->m_StopTrackingMutex->Unlock();
481 m_TrackingFinishedMutex->Unlock();
485 m_TrackingFinishedMutex->Unlock();
486 this->StopTracking();
498 if(this->m_initialized)
502 bool resultSetCameraSettings;
506 resultUpdate = TT_Update();
507 if(resultUpdate == NPRESULT_SUCCESS)
510 num_cams = TT_CameraCount();
524 MITK_DEBUG <<
"No cameras are connected to the device";
529 for(
int cam = 0; cam < num_cams; cam++)
533 resultUpdate = TT_Update();
535 if(resultUpdate == NPRESULT_SUCCESS)
537 MITK_DEBUG <<
"Update Succeed for camera number " << cam;
538 resultSetCameraSettings = TT_SetCameraSettings(cam,videoType,exposure,threshold,intensity);
540 if(resultSetCameraSettings)
542 MITK_INFO <<
"Camera # "<<cam<<
" params are set";
562 MITK_INFO <<
"System is not Initialized -> System is not ready to perform the Camera Parameters Setting";
582 bool resultSetToolByFileName;
586 resultSetToolByFileName= t->SetToolByFileName(fileName);
588 if(resultSetToolByFileName)
590 this->m_AllTools.push_back(t);
591 MITK_INFO <<
"Added tool "<<t->GetToolName()<<
". Tool vector size is now: "<<m_AllTools.size();
603 MITK_INFO <<
"System is not Initialized -> Cannot Add tools";
#define OPTITRACK_FRAME_RATE
Time to refresh the tools location (ms)
bool CloseConnection() override
Close the Connection with the Tracker. Also CleanUp the Optitrack variables using the API: TT_CleanUp...
bool StopTracking() override
Stop the Tracking Thread and tools will not longer be updated.
An object of this class represents an exception of the MITK-IGT module.
OptitrackTrackingTool * GetOptitrackTool(unsigned int toolNumber) const
Return the tool pointer of the tool number toolNumber.
DataCollection - Class to facilitate loading/accessing structured data.
bool LoadCalibration()
Load the Calibration file to the Optitrack System and set the cameras in calibrated locations...
static std::string GetOptitrackErrorMessage(int result)
Helper function to get the error messages from Optitrack API.
static ITK_THREAD_RETURN_TYPE ThreadStartTracking(void *data)
Start the Tracking Thread for the tools.
void SetCalibrationPath(std::string calibrationPath)
Sets the file where the calibration of the OptitrackTracker can be found.
~OptitrackTrackingDevice() override
#define OPTITRACK_ATTEMPTS
Maximum number of attempts for Initialization, Shutdown and CleanUp.
bool StartTracking() override
Start to Track the tools already defined. If no tools are defined for this tracker, it returns an error. Tools can be added using either AddToolByDescriptionFile or AddToolsByConfigurationFiles.
bool SetCameraParams(int exposure, int threshold, int intensity, int videoType=4)
Set the Cameras Exposure, Threshold and Intensity of IR LEDs. By Default it set the Video type to 4: ...
bool OpenConnection() override
Open the Connection with the Tracker. Calls LoadCalibration function and set the system up with the c...
OptitrackTrackingDevice()
This function load a file with Tools definitions provided for the software.
Interface for all Tracking Devices.
static IGTTimeStamp * GetInstance()
returns a pointer to the current instance of mitkTimeStamp
TrackingTool * GetTool(unsigned int toolNumber) const override
Return the tool pointer of the tool number toolNumber.
void Start(itk::Object::Pointer device)
starts the time-acquisition
bool AddToolByDefinitionFile(std::string fileName)
Add a new tool using a text file which described the tool. The file must to have the next structure T...
Point< ScalarType, 3 > Point3D
unsigned int GetToolCount() const override
Returns the number of defined tools.
#define mitkThrowException(classname)
bool IsDeviceInstalled() override
void TrackTools()
Update each tool location in the list m_AllTools.
void Stop(itk::Object::Pointer device)
stops the time-acqusition
bool InitializeCameras()
Initialize the Optitrack System.