1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
|
/*=========================================================================
Program: Image Guided Surgery Software Toolkit
Module: $RCSfile: igstkInfiniTrackTracker.cxx,v $
Language: C++
Date: $Date: 2011-01-18 21:40:16 $
Version: $Revision: 1.9 $
Copyright (c) ISC Insight Software Consortium. All rights reserved.
See IGSTKCopyright.txt or http://www.igstk.org/copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#if defined(_MSC_VER)
// Warning about: identifier was truncated to '255' characters in the
// debug information (MVC6.0 Debug)
#pragma warning( disable : 4786 )
#endif
#include "igstkInfiniTrackTracker.h"
#include "itksys/SystemTools.hxx"
#include "itkLoggerBase.h"
#include "itkInterface.h"
#include "itkHelper.h"
#include "itkMatrix.h"
#include "iostream"
#include "fstream"
namespace igstk
{
void deviceEnumerator (uint64 u64DeviceSerialNumber, void* pUserData)
{
*((uint64*) pUserData) = u64DeviceSerialNumber;
}
/** Constructor */
InfiniTrackTracker::InfiniTrackTracker (void) :
m_StateMachine(this)
, m_Handle (NULL)
, m_u64DeviceSerialNumber (0)
, m_BufferLock (NULL)
, m_pvecMarkerPos (NULL)
, m_iProcessed (-1)
, m_iAvailable (-1)
, m_iInAcquisition (-1)
, m_vecTrackerToolID ()
{
this->m_pvecMarkerPos = new std::vector<itkMarkerPos*>[3];
this->m_vecTrackerToolID.clear ();
// Lock for the data buffer that is used to transfer the
// transformations from thread that is communicating
// with the tracker to the main thread.
this->m_BufferLock = itk::MutexLock::New();
}
/** Destructor */
InfiniTrackTracker::~InfiniTrackTracker(void)
{
for (size_t t = 0; t < this->m_pvecMarkerPos [0].size (); t++)
{
delete this->m_pvecMarkerPos [0][t];
}
for (size_t t = 0; t < this->m_pvecMarkerPos [1].size (); t++)
{
delete this->m_pvecMarkerPos [1][t];
}
for (size_t t = 0; t < this->m_pvecMarkerPos [2].size (); t++)
{
delete this->m_pvecMarkerPos [2][t];
}
if (this->m_pvecMarkerPos)
{
delete [] this->m_pvecMarkerPos;
}
this->m_vecTrackerToolID.clear ();
}
/** The "InternalOpen" method opens communication with a tracking device. */
InfiniTrackTracker::ResultType InfiniTrackTracker::InternalOpen (void)
{
igstkLogMacro(DEBUG, "igstk::InfiniTrackTracker::InternalOpen called ...\n")
this->m_Handle = itkInitializeDriver ();
if(!this->m_Handle)
{
igstkLogMacro(CRITICAL, itkLastErrorStr ())
return FAILURE;
}
if (itkScanDevices((itkHandle)this->m_Handle,
deviceEnumerator,
&this->m_u64DeviceSerialNumber))
{
igstkLogMacro(CRITICAL, itkLastErrorStr ())
return FAILURE;
}
if (this->m_u64DeviceSerialNumber == 0)
{
igstkLogMacro(CRITICAL, "No device detected")
return FAILURE;
}
return SUCCESS;
}
/** The "InternalClose" method closes communication with a tracking device. */
InfiniTrackTracker::ResultType InfiniTrackTracker::InternalClose( void )
{
igstkLogMacro(DEBUG, "igstk::InfiniTrackTracker::InternalClose called ...\n")
if (this->m_Handle)
{
itkCloseDriver ((itkHandle)this->m_Handle);
m_Handle = NULL;
}
return SUCCESS;
}
/** The "InternalReset" method resets tracker to a known configuration. */
InfiniTrackTracker::ResultType InfiniTrackTracker::InternalReset( void )
{
igstkLogMacro(DEBUG, "igstk::InfiniTrackTracker::InternalReset called ...\n")
itkOption option;
if (itkSetOption ((itkHandle)this->m_Handle,
this->m_u64DeviceSerialNumber,
"RESET", &option))
{
igstkLogMacro(CRITICAL, itkLastErrorStr ())
return FAILURE;
}
return SUCCESS;
}
/** The "InternalStartTracking" method starts tracking. */
InfiniTrackTracker::ResultType InfiniTrackTracker::InternalStartTracking( void )
{
igstkLogMacro(DEBUG,
"igstk::InfiniTrackTracker::InternalStartTracking called ...\n")
if (itkStartAcquisition ((itkHandle)this->m_Handle,
this->m_u64DeviceSerialNumber))
{
igstkLogMacro(CRITICAL, itkLastErrorStr ())
return FAILURE;
}
return SUCCESS;
}
/** The "InternalStopTracking" method stops tracking. */
InfiniTrackTracker::ResultType InfiniTrackTracker::InternalStopTracking( void )
{
igstkLogMacro(DEBUG,
"igstk::InfiniTrackTracker::InternalStopTracking called ...\n")
if (itkStopAcquisition ((itkHandle)this->m_Handle,
this->m_u64DeviceSerialNumber))
{
igstkLogMacro(CRITICAL, itkLastErrorStr ())
return FAILURE;
}
return SUCCESS;
}
/** The "InternalUpdateStatus" method updates tracker status. */
InfiniTrackTracker::ResultType InfiniTrackTracker::InternalUpdateStatus( void )
{
igstkLogMacro(DEBUG,
"igstk::InfiniTrackTracker::InternalUpdateStatus called ...\n")
this->m_BufferLock->Lock ();
setNextArrayForUser ();
this->m_BufferLock->Unlock ();
if (m_iProcessed < 0)
{
igstkLogMacro(WARNING, "No frame available")
return FAILURE;
}
TrackerToolsContainerType trackerToolContainer = GetTrackerToolContainer();
for (size_t t = 0; t < this->m_pvecMarkerPos [m_iProcessed].size (); t++)
{
itkMarkerPos* pMarker = this->m_pvecMarkerPos [m_iProcessed] [t];
std::string tool = "";
for (size_t u = 0; u < this->m_vecTrackerToolID.size (); u++)
{
if (this->m_vecTrackerToolID [u].m_u32GeometryID ==
pMarker->mu32GeometryID)
{
tool = this->m_vecTrackerToolID [u].m_TrackerToolName;
}
}
if (tool == "")
{
continue;
}
// report to the tracker tool that the tracker is Visible
this->ReportTrackingToolVisible(trackerToolContainer[tool]);
// create the transform
TransformType transform;
typedef TransformType::VectorType TranslationType;
TranslationType translation;
translation[0] = pMarker->maf64Translation [0];
translation[1] = pMarker->maf64Translation [1];
translation[2] = pMarker->maf64Translation [2];
typedef TransformType::VersorType RotationType;
RotationType rotation;
itk::Matrix<double> matrix;
matrix [0][0] = pMarker->maf64Rotation [0][0];
matrix [0][1] = pMarker->maf64Rotation [0][1];
matrix [0][2] = pMarker->maf64Rotation [0][2];
matrix [1][0] = pMarker->maf64Rotation [1][0];
matrix [1][1] = pMarker->maf64Rotation [1][1];
matrix [1][2] = pMarker->maf64Rotation [1][2];
matrix [2][0] = pMarker->maf64Rotation [2][0];
matrix [2][1] = pMarker->maf64Rotation [2][1];
matrix [2][2] = pMarker->maf64Rotation [2][2];
rotation.Set (matrix);
// report error value
// Get error value from the tracker.
typedef TransformType::ErrorType ErrorType;
ErrorType errorValue = pMarker->mf64RegistrationMeanError;
long lTime = this->GetValidityTime ();
transform.SetToIdentity(this->GetValidityTime());
transform.SetTranslationAndRotation(translation,
rotation,
errorValue,
this->GetValidityTime());
// set the raw transform
this->SetTrackerToolRawTransform(trackerToolContainer [tool], transform );
this->SetTrackerToolTransformUpdate(trackerToolContainer [tool], true );
}
return SUCCESS;
}
/** The "InternalThreadedUpdateStatus" method updates tracker status. */
InfiniTrackTracker::ResultType
InfiniTrackTracker::InternalThreadedUpdateStatus( void )
{
igstkLogMacro(DEBUG,
"igstk::InfiniTrackTracker::InternalThreadedUpdateStatus called ...\n")
this->m_BufferLock->Lock ();
setNextArrayForAcquisition ();
this->m_BufferLock->Unlock ();
for (size_t t = 0; t < this->m_pvecMarkerPos [m_iInAcquisition].size (); t++)
{
delete this->m_pvecMarkerPos [m_iInAcquisition][t];
}
this->m_pvecMarkerPos [m_iInAcquisition].clear ();
uint32 uNoFrame;
// Declaration of the image structure
if (itkProcess ((itkHandle)this->m_Handle,
this->m_u64DeviceSerialNumber,
&uNoFrame))
{
if (itkLastError () > 0)
{
igstkLogMacro(CRITICAL, itkLastErrorStr ())
}
else
{
igstkLogMacro(DEBUG, itkLastErrorStr ())
}
return FAILURE;
}
for (size_t t = 0; t < this->m_vecTrackerToolID.size (); t++)
{
uint32 u32ID = this->m_vecTrackerToolID [t].m_u32GeometryID;
itkMarkerPos* pMarkerPosition = new itkMarkerPos;
if (!itkGetMarkerPos((itkHandle)this->m_Handle,
this->m_u64DeviceSerialNumber, u32ID,
pMarkerPosition))
{
this->m_pvecMarkerPos [m_iInAcquisition].push_back (pMarkerPosition);
}
else
{
delete pMarkerPosition;
}
}
this->m_BufferLock->Lock ();
acquisitionFinished ();
this->m_BufferLock->Unlock ();
return SUCCESS;
}
/** Print the object information in a stream. */
void InfiniTrackTracker::PrintSelf( std::ostream& os, itk::Indent indent ) const
{
Superclass::PrintSelf(os, indent);
if(!this->m_Handle || !this->m_u64DeviceSerialNumber)
{
return;
}
// Declaration of the device structure
itkDevice device;
// Retrieve information about the device
if (itkGetDevice ((itkHandle)this->m_Handle,
this->m_u64DeviceSerialNumber,
&device))
{
return;
}
uint32 uLeft = ((uint32)(device.mu64SerialNumber)),
uRight = ((uint32)((device.mu64SerialNumber)>>32));
char strSerial [100];
sprintf (strSerial, "0x%X%X", uLeft, uRight);
os << indent << "Tracker: " << device.mstr64Tracker << std::endl;
os << indent << "Model:" << device.mstr64Model << std::endl;
os << indent << "Device serial number:" << strSerial << std::endl;
}
/** Verify if a tracker tool information is correct before attaching */
InfiniTrackTracker::ResultType
InfiniTrackTracker::VerifyTrackerToolInformation (
const TrackerToolType* trackerTool)
{
igstkLogMacro(DEBUG,
"igstk::InfiniTrackTracker::VerifyTrackerToolInformation called ...\n")
if ( trackerTool == NULL )
{
igstkLogMacro(CRITICAL, "TrackerTool is not defined")
return FAILURE;
}
if(this->m_MarkerTemplatesDirectory == "" ||
!itksys::SystemTools::FileExists(this->m_MarkerTemplatesDirectory.c_str()))
{
igstkLogMacro(CRITICAL, "Marker templates directory is not properly set")
return FAILURE;
}
TrackerToolType* pTrackerToolNonConst =
const_cast<TrackerToolType *>(trackerTool);
InfiniTrackTrackerTool* pInfiniTrackTrackerTool
= dynamic_cast<InfiniTrackTrackerTool *> (pTrackerToolNonConst);
if(!pInfiniTrackTrackerTool)
{
igstkLogMacro(CRITICAL, "Tracker tool is not a InfiniTrack tool")
return FAILURE;
}
std::string strMarkerFileName =
this->m_MarkerTemplatesDirectory + "\\" +
pInfiniTrackTrackerTool->GetMarkerName () +
".xml";
if(!itksys::SystemTools::FileExists(strMarkerFileName.c_str()))
{
igstkLogMacro(CRITICAL, "Marker does not exist")
return FAILURE;
}
itkMarker marker;
if (ITK_OK != itkHelperLoadGeometry (strMarkerFileName.c_str (), &marker))
{
igstkLogMacro(CRITICAL, "File is not a InfiniTrack marker")
return FAILURE;
}
return SUCCESS;
}
/** The "ValidateSpecifiedFrequency" method checks if the specified */
InfiniTrackTracker::ResultType
InfiniTrackTracker::ValidateSpecifiedFrequency( double frequencyInHz )
{
igstkLogMacro(DEBUG,
"igstk::InfiniTrackTracker::ValidateSpecifiedFrequency called ...\n")
return SUCCESS;
}
/** This method will remove entries of the traceker tool
* from internal data containers */
InfiniTrackTracker::ResultType
InfiniTrackTracker::RemoveTrackerToolFromInternalDataContainers(
const TrackerToolType * trackerTool )
{
igstkLogMacro(DEBUG,
"igstk::InfiniTrackTracker::RemoveTrackerToolFromInternalDataContainers \
called ...\n")
TrackerToolType* pTrackerToolNonConst =
const_cast<TrackerToolType *>(trackerTool);
InfiniTrackTrackerTool* pInfiniTrackTrackerTool =
dynamic_cast<InfiniTrackTrackerTool *> (pTrackerToolNonConst);
std::string strMarkerFileName = this->m_MarkerTemplatesDirectory + "\\" +
pInfiniTrackTrackerTool->GetMarkerName () + ".xml";
itkMarker marker;
if (ITK_OK != itkHelperLoadGeometry (strMarkerFileName.c_str (), &marker))
{
igstkLogMacro(CRITICAL, itkLastErrorStr ())
return FAILURE;
}
if (itkRemoveGeometry ((itkHandle)this->m_Handle,
this->m_u64DeviceSerialNumber,
marker.mu32GeometryID))
{
igstkLogMacro(CRITICAL, itkLastErrorStr ())
return FAILURE;
}
for (size_t t = 0; t < m_vecTrackerToolID.size (); t++)
{
if (this->m_vecTrackerToolID [t].m_TrackerToolName ==
pInfiniTrackTrackerTool->GetMarkerName ())
{
this->m_vecTrackerToolID.erase (this->m_vecTrackerToolID.begin () + t);
break;
}
}
return SUCCESS;
}
/** Add tracker tool entry to internal containers */
InfiniTrackTracker::ResultType
InfiniTrackTracker::AddTrackerToolToInternalDataContainers (
const TrackerToolType * trackerTool)
{
igstkLogMacro(DEBUG,
"igstk::InfiniTrackTracker::AddTrackerToolToInternalDataContainers \
called ...\n")
TrackerToolType* pTrackerToolNonConst =
const_cast<TrackerToolType *>(trackerTool);
InfiniTrackTrackerTool* pInfiniTrackTrackerTool =
dynamic_cast<InfiniTrackTrackerTool *> (pTrackerToolNonConst);
std::string strMarkerFileName = this->m_MarkerTemplatesDirectory + "\\" +
pInfiniTrackTrackerTool->GetMarkerName () +
".xml";
itkMarker marker;
if (ITK_OK != itkHelperLoadGeometry (strMarkerFileName.c_str (), &marker))
{
igstkLogMacro(CRITICAL, itkLastErrorStr ())
return FAILURE;
}
if (itkSetGeometry ((itkHandle)this->m_Handle,
this->m_u64DeviceSerialNumber, &marker))
{
igstkLogMacro(CRITICAL, itkLastErrorStr ())
return FAILURE;
}
TrackerToolIdentification TrackerID;
TrackerID.m_TrackerToolName = pInfiniTrackTrackerTool->GetMarkerName ();
TrackerID.m_u32GeometryID = marker.mu32GeometryID;
this->m_vecTrackerToolID.push_back (TrackerID);
return SUCCESS;
}
/** Set the next available object array for acquisition */
void InfiniTrackTracker::setNextArrayForAcquisition ()
{
if (this->m_iAvailable != 0 && this->m_iProcessed != 0)
{
this->m_iInAcquisition = 0;
}
else if (this->m_iAvailable != 1 && this->m_iProcessed != 1)
{
this->m_iInAcquisition = 1;
}
else
{
this->m_iInAcquisition = 2;
}
}
/** Set the next object array for the user. True if a new array is available */
bool InfiniTrackTracker::setNextArrayForUser ()
{
this->m_iProcessed = this->m_iAvailable;
this->m_iAvailable = -1;
if(this->m_iProcessed > -1)
{
return true;
}
else
{
return false;
}
}
/** Called when the acquisition is finished */
void InfiniTrackTracker::acquisitionFinished ()
{
this->m_iAvailable = this->m_iInAcquisition;
this->m_iInAcquisition = -1;
}
} // end of namespace igstk
|