19 #ifdef MITK_USE_OPTITRACK_TRACKER
26 #include <NPTrackingTools.h>
40 :
mitk::TrackingDevice(),
48 SetData(mitk::DeviceDataNPOptitrack);
51 this->m_AllTools.clear();
59 MITK_DEBUG <<
"Deleting OptitrackTrackingDevice";
65 MITK_DEBUG <<
"OptitrackTrackingDevice in Tracking State -> Stopping Tracking";
66 result = this->StopTracking();
68 if(result == NPRESULT_SUCCESS){
69 MITK_INFO <<
"OptitrackTrackingDevice Stopped";
81 MITK_DEBUG <<
"OptitrackTrackingDevice in Ready State -> Closing the Connection";
82 result = this->CloseConnection();
86 MITK_INFO <<
"OptitrackTrackingDevice Connection closed";
90 MITK_DEBUG <<
"Error during Closing Connection";
96 m_initialized =
false;
99 MITK_DEBUG <<
"OptitrackTrackingDevice deleted successfully";
110 MITK_DEBUG <<
"Initialize Optitrack Tracking System";
112 if( this->InitializeCameras() )
114 m_initialized =
true;
116 if(this->m_calibrationPath.empty()){
117 MITK_INFO <<
"Numer of connected cameras = " << TT_CameraCount();
118 MITK_WARN <<
"Attention: No calibration File defined !!";
119 return m_initialized;
123 this->LoadCalibration();
128 m_initialized =
false;
130 MITK_INFO <<
"Device initialization failed. Device is still in setup state";
135 return m_initialized;
146 result = TT_Initialize();
148 if(result == NPRESULT_SUCCESS)
150 MITK_DEBUG <<
"Optitrack Initialization Succeed";
169 int resultLoadCalibration;
172 if(this->m_calibrationPath.empty()){
173 MITK_INFO <<
"Calibration Path is empty";
179 if(this->m_initialized)
184 resultLoadCalibration = TT_LoadCalibration(this->m_calibrationPath.c_str());
186 if(resultLoadCalibration != NPRESULT_SUCCESS)
193 MITK_DEBUG <<
"Calibration file has been loaded successfully";
199 MITK_INFO <<
"System cannot load a calibration file";
205 MITK_INFO <<
"System is not ready for load a calibration file because it has not been initialized yet";
223 if(calibrationPath.empty())
225 MITK_INFO <<
"Calibration Path is empty";
230 this->m_calibrationPath = calibrationPath;
231 MITK_INFO <<
"Calibration Path has been updated to: " << this->m_calibrationPath;
241 int resultStop, resultShutdown;
247 MITK_DEBUG <<
"Device state: Tracking -> Stoping the Tracking";
248 resultStop = this->StopTracking();
256 TT_ClearTrackableList();
257 resultShutdown = TT_Shutdown();
259 if(resultShutdown == NPRESULT_SUCCESS)
261 MITK_DEBUG <<
"System has been Shutdown Correctly";
267 MITK_DEBUG <<
"System cannot ShutDown now. Trying again...";
271 MITK_INFO <<
"System cannot ShutDown now";
277 MITK_INFO <<
"System has not been initialized. Close connection cannot be done";
291 bool resultIsTrackableTracked;
295 MITK_INFO <<
"System is not in State Ready -> Cannot StartTracking";
303 this->m_StopTrackingMutex->Lock();
304 this->m_StopTracking =
false;
305 this->m_StopTrackingMutex->Unlock();
315 m_ThreadID = m_MultiThreader->SpawnThread(this->ThreadStartTracking,
this);
318 if(GetToolCount() == 0)
MITK_INFO <<
"No tools are defined";
320 for (
int i = 0; i < GetToolCount(); ++i)
322 resultIsTrackableTracked = TT_IsTrackableTracked(i);
323 if(resultIsTrackableTracked)
325 MITK_DEBUG <<
"Trackable " << i <<
" is inside the Tracking Volume and it is Tracked";
329 MITK_DEBUG <<
"Trackable " << i <<
" is not been tracked. Check if it is inside the Tracking volume";
347 m_StopTrackingMutex->Lock();
348 m_StopTrackingMutex->Unlock();
353 m_TrackingFinishedMutex->Unlock();
354 MITK_INFO <<
"System is not in State Tracking -> Cannot StopTracking";
366 m_TrackingFinishedMutex->Unlock();
378 struct itk::MultiThreader::ThreadInfoStruct * pInfo = (
struct itk::MultiThreader::ThreadInfoStruct*)pInfoStruct;
382 return ITK_THREAD_RETURN_VALUE;
385 if (pInfo->UserData == NULL)
387 return ITK_THREAD_RETURN_VALUE;
390 OptitrackTrackingDevice *trackingDevice =
static_cast<OptitrackTrackingDevice*
>(pInfo->UserData);
392 if (trackingDevice != NULL)
395 trackingDevice->TrackTools();
402 trackingDevice->m_ThreadID = -1;
403 return ITK_THREAD_RETURN_VALUE;
412 OptitrackTrackingTool* t =
nullptr;
415 if(toolNumber < m_AllTools.size())
417 t = m_AllTools.at(toolNumber);
421 MITK_INFO <<
"The tool numbered " << toolNumber <<
" does not exist";
434 return (
int)(this->m_AllTools.size());
449 bool localStopTracking;
450 this->m_StopTrackingMutex->Lock();
451 localStopTracking = this->m_StopTracking;
454 if (!localStopTracking)
456 m_TrackingFinishedMutex->Lock();
459 this->m_StopTrackingMutex->Unlock();
463 for (
int i = 0; i < GetToolCount(); ++i)
465 OptitrackTrackingTool* currentTool = this->GetOptitrackTool(i);
466 if(currentTool !=
nullptr)
468 currentTool->updateTool();
469 MITK_DEBUG <<
"Tool number " << i <<
" updated position";
473 MITK_DEBUG <<
"Get data from tool number " << i <<
" failed";
479 this->m_StopTrackingMutex->Lock();
480 localStopTracking = m_StopTracking;
481 this->m_StopTrackingMutex->Unlock();
485 m_TrackingFinishedMutex->Unlock();
489 m_TrackingFinishedMutex->Unlock();
490 this->StopTracking();
502 if(this->m_initialized)
506 bool resultSetCameraSettings;
510 resultUpdate = TT_Update();
511 if(resultUpdate == NPRESULT_SUCCESS)
514 num_cams = TT_CameraCount();
528 MITK_DEBUG <<
"No cameras are connected to the device";
533 for(
int cam = 0; cam < num_cams; cam++)
537 resultUpdate = TT_Update();
539 if(resultUpdate == NPRESULT_SUCCESS)
541 MITK_DEBUG <<
"Update Succeed for camera number " << cam;
542 resultSetCameraSettings = TT_SetCameraSettings(cam,videoType,exposure,threshold,intensity);
544 if(resultSetCameraSettings)
546 MITK_INFO <<
"Camera # "<<cam<<
" params are set";
566 MITK_INFO <<
"System is not Initialized -> System is not ready to perform the Camera Parameters Setting";
586 bool resultSetToolByFileName;
590 resultSetToolByFileName= t->SetToolByFileName(fileName);
592 if(resultSetToolByFileName)
594 this->m_AllTools.push_back(t);
595 MITK_INFO <<
"Added tool "<<t->GetToolName()<<
". Tool vector size is now: "<<m_AllTools.size();
607 MITK_INFO <<
"System is not Initialized -> Cannot Add tools";
#define OPTITRACK_FRAME_RATE
Time to refresh the tools location (ms)
virtual bool CloseConnection() override
Close the Connection with the Tracker. Also CleanUp the Optitrack variables using the API: TT_CleanUp...
itk::SmartPointer< Self > Pointer
OptitrackTrackingTool * GetOptitrackTool(unsigned int toolNumber) const
Return the tool pointer of the tool number toolNumber.
virtual 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.
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.
#define OPTITRACK_ATTEMPTS
Maximum number of attempts for Initialization, Shutdown and CleanUp.
virtual 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: ...
virtual bool OpenConnection() override
Open the Connection with the Tracker. Calls LoadCalibration function and set the system up with the c...
~OptitrackTrackingDevice()
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)
virtual 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.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.