15#ifdef MITK_USE_OPTITRACK_TRACKER
22#include <NPTrackingTools.h>
36 :
mitk::TrackingDevice(),
40 SetData(mitk::DeviceDataNPOptitrack);
43 this->m_AllTools.clear();
51 MITK_DEBUG <<
"Deleting OptitrackTrackingDevice";
57 MITK_DEBUG <<
"OptitrackTrackingDevice in Tracking State -> Stopping Tracking";
60 if(result == NPRESULT_SUCCESS){
61 MITK_INFO <<
"OptitrackTrackingDevice Stopped";
65 MITK_INFO <<
"Error during Stopping";
73 MITK_DEBUG <<
"OptitrackTrackingDevice in Ready State -> Closing the Connection";
78 MITK_INFO <<
"OptitrackTrackingDevice Connection closed";
82 MITK_DEBUG <<
"Error during Closing Connection";
88 m_initialized =
false;
91 MITK_DEBUG <<
"OptitrackTrackingDevice deleted successfully";
102 MITK_DEBUG <<
"Initialize Optitrack Tracking System";
104 if( this->InitializeCameras() )
106 m_initialized =
true;
108 if(this->m_calibrationPath.empty()){
109 MITK_INFO <<
"Numer of connected cameras = " << TT_CameraCount();
110 MITK_WARN <<
"Attention: No calibration File defined !!";
111 return m_initialized;
115 this->LoadCalibration();
120 m_initialized =
false;
122 MITK_INFO <<
"Device initialization failed. Device is still in setup state";
123 mitkThrowException(
mitk::IGTException) <<
"Device initialization failed. Device is still in setup state";
127 return m_initialized;
135 MITK_DEBUG <<
"Initialize Optitrack";
138 result = TT_Initialize();
140 if(result == NPRESULT_SUCCESS)
142 MITK_DEBUG <<
"Optitrack Initialization Succeed";
160 MITK_DEBUG <<
"Loading System Calibration";
161 int resultLoadCalibration;
164 if(this->m_calibrationPath.empty()){
165 MITK_INFO <<
"Calibration Path is empty";
171 if(this->m_initialized)
176 resultLoadCalibration = TT_LoadCalibration(this->m_calibrationPath.c_str());
178 if(resultLoadCalibration != NPRESULT_SUCCESS)
181 MITK_DEBUG <<
"Trying again...";
185 MITK_DEBUG <<
"Calibration file has been loaded successfully";
191 MITK_INFO <<
"System cannot load a calibration file";
197 MITK_INFO <<
"System is not ready for load a calibration file because it has not been initialized yet";
198 mitkThrowException(
mitk::IGTException) <<
"System is not ready for load a calibration file because it has not been initialized yet";
211 MITK_DEBUG <<
"SetcalibrationPath";
212 MITK_DEBUG << calibrationPath;
215 if(calibrationPath.empty())
217 MITK_INFO <<
"Calibration Path is empty";
222 this->m_calibrationPath = calibrationPath;
223 MITK_INFO <<
"Calibration Path has been updated to: " << this->m_calibrationPath;
232 MITK_DEBUG <<
"CloseConnection";
233 int resultStop, resultShutdown;
239 MITK_DEBUG <<
"Device state: Tracking -> Stoping the Tracking";
240 resultStop = this->StopTracking();
248 TT_ClearTrackableList();
249 resultShutdown = TT_Shutdown();
251 if(resultShutdown == NPRESULT_SUCCESS)
253 MITK_DEBUG <<
"System has been Shutdown Correctly";
259 MITK_DEBUG <<
"System cannot ShutDown now. Trying again...";
263 MITK_INFO <<
"System cannot ShutDown now";
269 MITK_INFO <<
"System has not been initialized. Close connection cannot be done";
270 mitkThrowException(
mitk::IGTException) <<
"System has not been initialized. Close connection cannot be done";
282 MITK_DEBUG <<
"StartTracking";
283 bool resultIsTrackableTracked;
287 MITK_INFO <<
"System is not in State Ready -> Cannot StartTracking";
288 mitkThrowException(
mitk::IGTException) <<
"System is not in State Ready -> Cannot StartTracking";
295 this->m_StopTrackingMutex.lock();
296 this->m_StopTracking =
false;
297 this->m_StopTrackingMutex.unlock();
310 if(GetToolCount() == 0) MITK_INFO <<
"No tools are defined";
312 for (
int i = 0; i < GetToolCount(); ++i)
314 resultIsTrackableTracked = TT_IsTrackableTracked(i);
315 if(resultIsTrackableTracked)
317 MITK_DEBUG <<
"Trackable " << i <<
" is inside the Tracking Volume and it is Tracked";
321 MITK_DEBUG <<
"Trackable " << i <<
" is not been tracked. Check if it is inside the Tracking volume";
334 MITK_DEBUG <<
"StopTracking";
339 m_StopTrackingMutex.lock();
340 m_StopTrackingMutex.unlock();
345 m_TrackingFinishedMutex.unlock();
346 MITK_INFO <<
"System is not in State Tracking -> Cannot StopTracking";
347 mitkThrowException(
mitk::IGTException) <<
"System is not in State Tracking -> Cannot StopTracking";
358 m_TrackingFinishedMutex.unlock();
367 MITK_DEBUG <<
"ThreadStartTracking";
376 MITK_DEBUG <<
"ThreadStartTracking";
377 OptitrackTrackingTool* t =
nullptr;
379 std::lock_guard<std::mutex> toolsMutexLockHolder(m_ToolsMutex);
380 if(toolNumber < m_AllTools.size())
382 t = m_AllTools.at(toolNumber);
386 MITK_INFO <<
"The tool numbered " << toolNumber <<
" does not exist";
387 mitkThrowException(
mitk::IGTException) <<
"The tool numbered " << toolNumber <<
" does not exist";
397 MITK_DEBUG <<
"GetToolCount";
398 std::lock_guard<std::mutex> lock(m_ToolsMutex);
399 return (
int)(this->m_AllTools.size());
407 MITK_DEBUG <<
"TrackTools";
414 bool localStopTracking;
415 this->m_StopTrackingMutex.lock();
416 localStopTracking = this->m_StopTracking;
419 if (!localStopTracking)
421 m_TrackingFinishedMutex.lock();
424 this->m_StopTrackingMutex.unlock();
428 for (
int i = 0; i < GetToolCount(); ++i)
430 OptitrackTrackingTool* currentTool = this->GetOptitrackTool(i);
431 if(currentTool !=
nullptr)
433 currentTool->updateTool();
434 MITK_DEBUG <<
"Tool number " << i <<
" updated position";
438 MITK_DEBUG <<
"Get data from tool number " << i <<
" failed";
439 mitkThrowException(
mitk::IGTException) <<
"Get data from tool number " << i <<
" failed";
444 this->m_StopTrackingMutex.lock();
445 localStopTracking = m_StopTracking;
446 this->m_StopTrackingMutex.unlock();
450 m_TrackingFinishedMutex.unlock();
454 m_TrackingFinishedMutex.unlock();
455 this->StopTracking();
456 mitkThrowException(
mitk::IGTException) <<
"Error while trying to track tools. Thread stopped.";
465 MITK_DEBUG <<
"SetCameraParams";
467 if(this->m_initialized)
471 bool resultSetCameraSettings;
475 resultUpdate = TT_Update();
476 if(resultUpdate == NPRESULT_SUCCESS)
478 MITK_DEBUG <<
"Update Succeed";
479 num_cams = TT_CameraCount();
485 MITK_DEBUG <<
"Trying again...";
493 MITK_DEBUG <<
"No cameras are connected to the device";
498 for(
int cam = 0; cam < num_cams; cam++)
502 resultUpdate = TT_Update();
504 if(resultUpdate == NPRESULT_SUCCESS)
506 MITK_DEBUG <<
"Update Succeed for camera number " << cam;
507 resultSetCameraSettings = TT_SetCameraSettings(cam,videoType,exposure,threshold,intensity);
509 if(resultSetCameraSettings)
511 MITK_INFO <<
"Camera # "<<cam<<
" params are set";
524 MITK_DEBUG <<
"Update: Trying again...";
531 MITK_INFO <<
"System is not Initialized -> System is not ready to perform the Camera Parameters Setting";
532 mitkThrowException(
mitk::IGTException) <<
"System is not Initialized -> System is not ready to perform the Camera Parameters Setting";
551 bool resultSetToolByFileName;
554 OptitrackTrackingTool::Pointer t = OptitrackTrackingTool::New();
555 resultSetToolByFileName= t->SetToolByFileName(fileName);
557 if(resultSetToolByFileName)
559 this->m_AllTools.push_back(t);
560 MITK_INFO <<
"Added tool "<<t->GetToolName()<<
". Tool vector size is now: "<<m_AllTools.size();
565 MITK_INFO <<
"Tool could not be added";
572 MITK_INFO <<
"System is not Initialized -> Cannot Add tools";
573 mitkThrowException(
mitk::IGTException) <<
"System is not Initialized -> Cannot Add tools";
An object of this class represents an exception of the MITK-IGT module.
static IGTTimeStamp * GetInstance()
returns a pointer to the current instance of mitkTimeStamp
void Start(itk::Object::Pointer device)
starts the time-acquisition
void Stop(itk::Object::Pointer device)
stops the time-acqusition
static std::string GetOptitrackErrorMessage(int result)
Helper function to get the error messages from Optitrack API.
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...
bool StartTracking() override
Start to Track the tools already defined. If no tools are defined for this tracker,...
bool IsDeviceInstalled() override
OptitrackTrackingDevice()
bool StopTracking() override
Stop the Tracking Thread and tools will not longer be updated.
TrackingTool * GetTool(unsigned int toolNumber) const override
Return the tool pointer of the tool number toolNumber.
OptitrackTrackingTool * GetOptitrackTool(unsigned int toolNumber) const
Return the tool pointer of the tool number toolNumber.
void ThreadStartTracking()
Start the Tracking Thread for the tools.
bool InitializeCameras()
Initialize the Optitrack System.
void SetCalibrationPath(std::string calibrationPath)
Sets the file where the calibration of the OptitrackTracker can be found.
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: ...
void TrackTools()
Update each tool location in the list m_AllTools.
~OptitrackTrackingDevice() override
unsigned int GetToolCount() const override
Returns the number of defined tools.
bool CloseConnection() override
Close the Connection with the Tracker. Also CleanUp the Optitrack variables using the API: TT_CleanUp...
bool LoadCalibration()
Load the Calibration file to the Optitrack System and set the cameras in calibrated locations.
bool OpenConnection() override
Open the Connection with the Tracker. Calls LoadCalibration function and set the system up with the c...
Interface for all Tracking Devices.
void SetState(TrackingDeviceState state)
change object state
TrackingDeviceState GetState() const
return current object state (Setup, Ready or Tracking)
#define OPTITRACK_ATTEMPTS
Maximum number of attempts for Initialization, Shutdown and CleanUp.
#define OPTITRACK_FRAME_RATE
Time to refresh the tools location (ms)