15#ifdef MITK_USE_OPTITRACK_TRACKER
20#include <NPTrackingTools.h>
26 :
mitk::TrackingTool(),
29 MITK_DEBUG <<
"Creating OptitrackTrackingTool Object";
40 MITK_DEBUG <<
"Deleting OptitrackTrackingTool Object";
48 MITK_DEBUG <<
"SetToolByFileName";
49 MITK_INFO<<
"Name of the file for configuration: "<<nameFile;
50 this->m_fileConfiguration = nameFile;
51 int resultFscan, resultUpdate, resultCreateTrackable, resultTrackableTranslatePivot;
54 if(this->m_fileConfiguration.empty())
56 MITK_INFO <<
"Calibration File for Tool is empty";
62 FILE* calib_file = fopen(this->m_fileConfiguration.c_str(),
"r");
63 if (calib_file ==
nullptr)
65 MITK_INFO <<
"Error using opening file";
70 MITK_DEBUG<<
"Reading configuration file...";
73 this->m_ToolName =
"";
74 char* aux =
new char[200];
75 resultFscan = fscanf(calib_file,
"%s\n",aux);
76 this->m_ToolName.append(aux);
78 if ((resultFscan < 1) || this->m_ToolName.empty())
80 MITK_INFO <<
"No name found in the tool configuration file";
81 mitkThrowException(
mitk::IGTException) <<
"No name found in the tool configuration file";
85 MITK_INFO<<
"ToolName: " << this->m_ToolName;
88 resultFscan = fscanf(calib_file,
"%i\n",&(this->m_numMarkers));
89 if (this->m_numMarkers < 3)
91 MITK_INFO <<
"The minimum number for define a tool is 3 markers";
96 MITK_INFO<<
"\tNumer of Markers: " << this->m_numMarkers;
99 this->m_calibrationPoints =
new float[3*this->m_numMarkers];
101 for(
int i=0; i<this->m_numMarkers; i++)
103 resultFscan = fscanf(calib_file,
"%fe ", &this->m_calibrationPoints[i*3+0]);
106 MITK_INFO <<
"Cannot read X location for marker " << i;
111 resultFscan = fscanf(calib_file,
"%fe ", &this->m_calibrationPoints[i*3+1]);
114 MITK_INFO <<
"Cannot read Y location for marker " << i;
119 resultFscan = fscanf(calib_file,
"%fe\n", &this->m_calibrationPoints[i*3+2]);
122 MITK_INFO <<
"Cannot read Z location for marker " << i;
126 MITK_DEBUG <<
"\t\tMarker " << i;
127 MITK_DEBUG <<
"\t\t X: " << this->m_calibrationPoints[i*3+0] <<
" Y: " << this->m_calibrationPoints[i*3+1] <<
" Z: " << this->m_calibrationPoints[i*3+2];
129 this->m_calibrationPoints[i*3+0] = this->m_calibrationPoints[i*3+0]/1000;
130 this->m_calibrationPoints[i*3+1] = this->m_calibrationPoints[i*3+1]/1000;
131 this->m_calibrationPoints[i*3+2] = -this->m_calibrationPoints[i*3+2]/1000;
136 this->m_pivotPoint =
new float[3];
137 resultFscan = fscanf(calib_file,
"%fe ", &this->m_pivotPoint[0]);
140 MITK_INFO <<
"Cannot read X location for Pivot Point ";
145 resultFscan = fscanf(calib_file,
"%fe ", &this->m_pivotPoint[1]);
148 MITK_INFO <<
"Cannot read Y location for Pivot Point " ;
153 resultFscan = fscanf(calib_file,
"%fe\n", &this->m_pivotPoint[2]);
156 MITK_INFO <<
"Cannot read Z location for Pivot Point " ;
161 MITK_INFO <<
"\tPivotPoint " ;
162 MITK_INFO <<
"\t\t X: " << this->m_pivotPoint[0] <<
" Y: " << this->m_pivotPoint[1] <<
" Z: " << this->m_pivotPoint[2];
165 this->m_pivotPoint[0] = this->m_pivotPoint[0]/1000;
166 this->m_pivotPoint[1] = this->m_pivotPoint[1]/1000;
167 this->m_pivotPoint[2] = -this->m_pivotPoint[2]/1000;
170 this->m_ID = this->get_IDnext();
175 resultCreateTrackable = TT_CreateTrackable(m_ToolName.c_str(), this->m_ID,this->m_numMarkers,this->m_calibrationPoints);
176 if(NPRESULT_SUCCESS == resultCreateTrackable)
178 MITK_INFO <<
"Trackable Created Successfully";
184 MITK_DEBUG <<
"Trying again...";
190 resultUpdate = TT_Update();
191 if(NPRESULT_SUCCESS == resultUpdate)
193 resultTrackableTranslatePivot = TT_TrackableTranslatePivot(this->m_ID,this->m_pivotPoint[0],this->m_pivotPoint[1],this->m_pivotPoint[2]);
194 if(NPRESULT_SUCCESS == resultCreateTrackable)
196 MITK_INFO <<
"Pivot Translation Successfull";
204 MITK_DEBUG <<
"Trying again...";
210 MITK_DEBUG <<
"Trying again...";
214 MITK_INFO <<
"Cannot create tool ";
224 MITK_DEBUG <<
"get_ID";
225 int num_trackables = -1;
230 resultUpdate = TT_Update();
231 if(NPRESULT_SUCCESS == resultUpdate)
233 num_trackables = TT_TrackableCount();
234 MITK_DEBUG <<
" Next ID: " << num_trackables;
235 if(num_trackables > -1)
237 return num_trackables;
241 MITK_DEBUG <<
"get_IDnext failed";
249 MITK_DEBUG <<
"Trying again...";
254 return num_trackables;
262 MITK_DEBUG <<
"DeleteTrackable";
263 int resultRemoveTrackable;
264 resultRemoveTrackable = TT_RemoveTrackable(this->m_ID);
266 if(resultRemoveTrackable != NPRESULT_SUCCESS)
268 MITK_INFO <<
"Cannot Remove Trackable";
275 MITK_INFO<<
"Trackable " << this->m_ToolName <<
" removed";
286 MITK_DEBUG <<
"SetPosition";
288 this->m_Position[0] = position[0];
289 this->m_Position[1] = position[1];
290 this->m_Position[2] = position[2];
298 MITK_DEBUG <<
"SetOrientation";
300 this->m_Orientation.x() = orientation.x();
301 this->m_Orientation.y() = orientation.y();
302 this->m_Orientation.z() = orientation.z();
303 this->m_Orientation.r() = orientation.r();
311 MITK_DEBUG <<
"GetPosition";
313 positionOutput[0] = this->m_Position[0];
314 positionOutput[1] = this->m_Position[1];
315 positionOutput[2] = this->m_Position[2];
323 MITK_DEBUG <<
"GetOrientation";
325 orientation.x() = this->m_Orientation.x();
326 orientation.y() = this->m_Orientation.y();
327 orientation.z() = this->m_Orientation.z();
328 orientation.r() = this->m_Orientation.r();
336 MITK_DEBUG <<
"Enable";
338 TT_SetTrackableEnabled(this->m_ID,
true);
340 if(TT_TrackableEnabled(this->m_ID) ==
true)
342 this->m_Enabled =
true;
347 this->m_Enabled =
false;
348 MITK_INFO <<
"Enable failed";
359 MITK_DEBUG <<
"Disable";
361 TT_SetTrackableEnabled(this->m_ID,
false);
363 if(TT_TrackableEnabled(this->m_ID) ==
true)
365 this->m_Enabled =
false;
370 this->m_Enabled =
true;
371 MITK_INFO <<
"Disable failed";
383 MITK_DEBUG <<
"IsEnabled";
385 return TT_TrackableEnabled(this->m_ID);
393 MITK_DEBUG <<
"IsDataValid";
395 return this->m_DataValid;
403 MITK_DEBUG <<
"GetTrackingError";
405 return this->m_TrackingError;
413 MITK_DEBUG <<
"GetTrackingError";
417 this->m_TrackingError = error;
425 MITK_DEBUG <<
"SetDataValid";
427 this->m_DataValid = validate;
435 MITK_DEBUG <<
"updateTool";
436 float yaw,pitch,roll;
439 if(TT_Update() == NPRESULT_SUCCESS)
441 if(this->IsEnabled())
443 TT_TrackableLocation(this->m_ID, &data[0], &data[1], &data[2],
444 &data[3], &data[4], &data[5], &data[6],
445 &yaw, &pitch, &roll);
458 this->m_Position[0] = data[0]*1000;
459 this->m_Position[1] = data[1]*1000;
460 this->m_Position[2] = -data[2]*1000;
462 this->m_Orientation.x() = data[3];
463 this->m_Orientation.y() = data[4];
464 this->m_Orientation.z() = -data[5];
465 this->m_Orientation.r() = data[6];
467 this->SetDataValid(
true);
469 MITK_DEBUG << this->m_Position[0] <<
" " << this->m_Position[1] <<
" " << this->m_Position[2];
470 MITK_DEBUG << data[3] <<
" " << data[4] <<
" " << data[5] <<
" " << data[6];
475 this->SetDataValid(
false);
476 MITK_DEBUG <<
"Trackable: "<< this->m_ToolName <<
"is not Tracked";
481 this->SetDataValid(
false);
482 MITK_DEBUG <<
"Update Failed";
An object of this class represents an exception of the MITK-IGT module.
static std::string GetOptitrackErrorMessage(int result)
Helper function to get the error messages from Optitrack API.
#define OPTITRACK_ATTEMPTS
Maximum number of attempts for Initialization, Shutdown and CleanUp.