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 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
|
/*=========================================================================
Program: Image Guided Surgery Software Toolkit
Module: $RCSfile: igstkTracker.cxx,v $
Language: C++
Date: $Date: 2011-01-18 21:40:17 $
Version: $Revision: 1.70 $
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 "igstkTracker.h"
#define NON_FLICKERING_CONSTANT 20
namespace igstk
{
/** Constructor */
Tracker::Tracker(void) : m_StateMachine( this )
{
/** Coordinate system interface */
igstkCoordinateSystemClassInterfaceConstructorMacro();
// Set the state descriptors
igstkAddStateMacro( Idle );
igstkAddStateMacro( AttemptingToEstablishCommunication );
igstkAddStateMacro( AttemptingToCloseCommunication);
igstkAddStateMacro( AttemptingToAttachTrackerTool );
igstkAddStateMacro( TrackerToolAttached );
igstkAddStateMacro( CommunicationEstablished );
igstkAddStateMacro( AttemptingToTrack );
igstkAddStateMacro( AttemptingToStopTracking);
igstkAddStateMacro( Tracking );
igstkAddStateMacro( AttemptingToUpdate );
// Set the input descriptors
igstkAddInputMacro( EstablishCommunication);
igstkAddInputMacro( AttachTrackerTool);
igstkAddInputMacro( StartTracking);
igstkAddInputMacro( UpdateStatus);
igstkAddInputMacro( StopTracking);
igstkAddInputMacro( Reset);
igstkAddInputMacro( CloseCommunication);
igstkAddInputMacro( Success);
igstkAddInputMacro( Failure);
igstkAddInputMacro( ValidFrequency);
// Programming the state machine transitions:
// Transitions from the Idle
igstkAddTransitionMacro( Idle,
EstablishCommunication,
AttemptingToEstablishCommunication,
AttemptToOpen );
igstkAddTransitionMacro( Idle,
StartTracking,
Idle,
ReportInvalidRequest );
igstkAddTransitionMacro( Idle,
StopTracking,
Idle,
ReportInvalidRequest );
igstkAddTransitionMacro( Idle,
AttachTrackerTool,
Idle,
ReportInvalidRequest );
igstkAddTransitionMacro( Idle,
UpdateStatus,
Idle,
ReportInvalidRequest );
igstkAddTransitionMacro( Idle,
Reset,
Idle,
ReportInvalidRequest );
igstkAddTransitionMacro( Idle,
CloseCommunication,
Idle,
ReportInvalidRequest );
igstkAddTransitionMacro( Idle,
ValidFrequency,
Idle,
SetFrequency );
// Transitions from the AttemptingToEstablishCommunication
igstkAddTransitionMacro( AttemptingToEstablishCommunication,
Success,
CommunicationEstablished,
CommunicationEstablishmentSuccess );
igstkAddTransitionMacro( AttemptingToEstablishCommunication,
Failure,
Idle,
CommunicationEstablishmentFailure );
igstkAddTransitionMacro( AttemptingToEstablishCommunication,
ValidFrequency,
AttemptingToEstablishCommunication,
ReportInvalidRequest );
// Transitions from CommunicationEstablished
igstkAddTransitionMacro( CommunicationEstablished,
AttachTrackerTool,
AttemptingToAttachTrackerTool,
AttemptToAttachTrackerTool );
igstkAddTransitionMacro( CommunicationEstablished,
StartTracking,
AttemptingToTrack,
AttemptToStartTracking );
igstkAddTransitionMacro( CommunicationEstablished,
CloseCommunication,
AttemptingToCloseCommunication,
CloseFromCommunicatingState );
igstkAddTransitionMacro( CommunicationEstablished,
Reset,
CommunicationEstablished,
ResetFromCommunicatingState );
igstkAddTransitionMacro( CommunicationEstablished,
StopTracking,
CommunicationEstablished,
ReportInvalidRequest );
igstkAddTransitionMacro( CommunicationEstablished,
EstablishCommunication,
CommunicationEstablished,
ReportInvalidRequest );
igstkAddTransitionMacro( CommunicationEstablished,
UpdateStatus,
CommunicationEstablished,
ReportInvalidRequest );
igstkAddTransitionMacro( CommunicationEstablished,
ValidFrequency,
CommunicationEstablished,
SetFrequency );
// Transitions from AttemptingToAttachTrackerTool
igstkAddTransitionMacro( AttemptingToAttachTrackerTool,
Success,
TrackerToolAttached,
AttachingTrackerToolSuccess );
igstkAddTransitionMacro( AttemptingToAttachTrackerTool,
Failure,
CommunicationEstablished,
AttachingTrackerToolFailure );
igstkAddTransitionMacro( AttemptingToAttachTrackerTool,
ValidFrequency,
AttemptingToAttachTrackerTool,
ReportInvalidRequest );
// Transitions from TrackerToolAttached
igstkAddTransitionMacro( TrackerToolAttached,
StartTracking,
AttemptingToTrack,
AttemptToStartTracking );
igstkAddTransitionMacro( TrackerToolAttached,
AttachTrackerTool,
AttemptingToAttachTrackerTool,
AttemptToAttachTrackerTool );
igstkAddTransitionMacro( TrackerToolAttached,
ValidFrequency,
TrackerToolAttached,
SetFrequency );
igstkAddTransitionMacro( TrackerToolAttached,
StopTracking,
TrackerToolAttached,
ReportInvalidRequest );
igstkAddTransitionMacro( TrackerToolAttached,
CloseCommunication,
AttemptingToCloseCommunication,
CloseFromCommunicatingState );
// Transitions from AttemptingToTrack
igstkAddTransitionMacro( AttemptingToTrack,
Success,
Tracking,
StartTrackingSuccess );
igstkAddTransitionMacro( AttemptingToTrack,
Failure,
CommunicationEstablished,
StartTrackingFailure );
igstkAddTransitionMacro( AttemptingToTrack,
ValidFrequency,
AttemptingToTrack,
ReportInvalidRequest );
// Transitions from Tracking
igstkAddTransitionMacro( Tracking,
UpdateStatus,
AttemptingToUpdate,
AttemptToUpdateStatus );
igstkAddTransitionMacro( Tracking,
StopTracking,
AttemptingToStopTracking,
AttemptToStopTracking );
igstkAddTransitionMacro( Tracking,
Reset,
CommunicationEstablished,
ResetFromTrackingState );
igstkAddTransitionMacro( Tracking,
CloseCommunication,
AttemptingToCloseCommunication,
CloseFromTrackingState );
igstkAddTransitionMacro( Tracking,
ValidFrequency,
Tracking,
ReportInvalidRequest );
// Transitions from AttemptingToUpdate
igstkAddTransitionMacro( AttemptingToUpdate,
Success,
Tracking,
UpdateStatusSuccess );
igstkAddTransitionMacro( AttemptingToUpdate,
Failure,
Tracking,
UpdateStatusFailure );
igstkAddTransitionMacro( AttemptingToUpdate,
ValidFrequency,
AttemptingToUpdate,
ReportInvalidRequest );
// Transitions from AttemptingToStopTracking
igstkAddTransitionMacro( AttemptingToStopTracking,
Success,
CommunicationEstablished,
StopTrackingSuccess );
igstkAddTransitionMacro( AttemptingToStopTracking,
Failure,
Tracking,
StopTrackingFailure );
igstkAddTransitionMacro( AttemptingToStopTracking,
ValidFrequency,
AttemptingToStopTracking,
ReportInvalidRequest );
igstkAddTransitionMacro( AttemptingToCloseCommunication,
Success,
Idle,
CloseCommunicationSuccess );
igstkAddTransitionMacro( AttemptingToCloseCommunication,
Failure,
CommunicationEstablished,
CloseCommunicationFailure );
igstkAddTransitionMacro( AttemptingToCloseCommunication,
ValidFrequency,
AttemptingToCloseCommunication,
ReportInvalidRequest );
// Select the initial state of the state machine
igstkSetInitialStateMacro( Idle );
// Finish the programming and get ready to run
m_StateMachine.SetReadyToRun();
// Create a PulseGenerator object.
m_PulseGenerator = PulseGenerator::New();
m_PulseObserver = ObserverType::New();
m_PulseObserver->SetCallbackFunction( this, & Tracker::UpdateStatus );
m_PulseGenerator->AddObserver( PulseEvent(), m_PulseObserver );
// This is update rate for sending tracking information to the
// spatial objects, it should be set to at least 30 Hz
const double DEFAULT_REFRESH_RATE = 30.0;
m_PulseGenerator->RequestSetFrequency( DEFAULT_REFRESH_RATE );
// This is the time period for which transformation should be
// considered valid. After this time, they expire. This time
// is in milliseconds. The default validity time is computed
// from the default refresh rate and nonflickering constant
//const double nonFlickeringConstant = 20;
const TimePeriodType DEFAULT_VALIDITY_TIME =
( 1000.0 /DEFAULT_REFRESH_RATE) + NON_FLICKERING_CONSTANT;
m_ValidityTime = DEFAULT_VALIDITY_TIME;
// By default, the reference is not used
m_ApplyingReferenceTool = false;
m_ConditionNextTransformReceived = itk::ConditionVariable::New();
m_Threader = itk::MultiThreader::New();
m_ThreadingEnabled = false;
m_TrackingThreadStarted = false;
}
/** Destructor */
Tracker::~Tracker(void)
{
}
/** This method sets the reference tool. */
void Tracker::RequestSetReferenceTool( TrackerToolType * trackerTool )
{
igstkLogMacro( DEBUG, "igstk::Tracker::RequestSetReferenceTool called ...\n");
// connect the reference tracker tool the tracker
TransformType identityTransform;
identityTransform.SetToIdentity(
igstk::TimeStamp::GetZeroValue() );
if( trackerTool != NULL )
{
// check if it is already attached to the tracker
typedef TrackerToolsContainerType::iterator InputIterator;
InputIterator toolItr =
m_TrackerTools.find( trackerTool->GetTrackerToolIdentifier() );
if( toolItr != m_TrackerTools.end() )
{
m_ApplyingReferenceTool = true;
m_ReferenceTool = trackerTool;
m_ReferenceTool->RequestDetachFromParent();
//VERY IMPORTANT: Make the reference tracker tool the parent of the
// tracker
this->RequestSetTransformAndParent( identityTransform, trackerTool );
}
else
{
igstkLogMacro( CRITICAL, "Request to use a tracker tool as a reference"
<< "has failed. The tracker tool is not attached to the tracker ");
}
}
}
/** The "RequestOpen" method attempts to open communication with the
* tracking device. */
void Tracker::RequestOpen( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::RequestOpen called...\n");
igstkPushInputMacro( EstablishCommunication );
this->m_StateMachine.ProcessInputs();
}
/** The "RequestClose" method closes communication with the device. */
void Tracker::RequestClose( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::RequestClose called ...\n");
igstkPushInputMacro( CloseCommunication );
m_StateMachine.ProcessInputs();
}
/** The "RequestReset" tracker method should be used to the tracker
* to some defined default state. */
void Tracker::RequestReset( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::RequestReset called ...\n");
igstkPushInputMacro( Reset );
m_StateMachine.ProcessInputs();
}
/** The "RequestStartTracking" method readies the tracker for tracking the
* tools connected to the tracker. */
void Tracker::RequestStartTracking( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::RequestStartTracking called ...\n");
igstkPushInputMacro( StartTracking );
m_StateMachine.ProcessInputs();
}
/** The "RequestStopTracking" stops tracker from tracking the tools. */
void Tracker::RequestStopTracking( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::RequestStopTracking called ...\n");
igstkPushInputMacro( StopTracking );
m_StateMachine.ProcessInputs();
}
/** The "UpdateStatus" method is used for updating the status of
* tools when the tracker is in tracking state. */
void Tracker::UpdateStatus( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::UpdateStatus called ...\n");
igstkPushInputMacro( UpdateStatus );
m_StateMachine.ProcessInputs();
}
/** The "RequestSetFrequency" method is used for defining the rate at which
* Transforms are queried from the Tracker device */
void Tracker::RequestSetFrequency( double frequencyInHz )
{
igstkLogMacro( DEBUG, "igstk::Tracker::RequestSetFrequency called ...\n");
if( this->ValidateSpecifiedFrequency( frequencyInHz ) )
{
this->m_FrequencyToBeSet = frequencyInHz;
igstkPushInputMacro( ValidFrequency );
m_StateMachine.ProcessInputs();
}
}
/** The "ValidateFrequency" method checks if the specified frequency is
* valid for the tracking device that is being used. This method is to be
* overridden in the derived tracking-device specific classes to take
* into account the maximum frequency possible in the tracking device */
Tracker::ResultType
Tracker::ValidateSpecifiedFrequency( double frequencyInHz )
{
if ( frequencyInHz < 0.0 )
{
return FAILURE;
}
return SUCCESS;
}
/** The "AttemptToOpen" method attempts to open communication with a
* tracking device. */
void Tracker::AttemptToOpenProcessing( void )
{
igstkLogMacro( DEBUG,
"igstk::Tracker::AttemptToOpenProcessing called ...\n");
ResultType result = this->InternalOpen();
m_StateMachine.PushInputBoolean( (bool)result,
m_SuccessInput,
m_FailureInput );
}
/** Post-processing after communication setup has been successful. */
void Tracker::CommunicationEstablishmentSuccessProcessing( void )
{
igstkLogMacro( DEBUG,
"igstk::Tracker::CommunicationEstablishmentSuccessProcessing called ...\n");
this->InvokeEvent( TrackerOpenEvent() );
}
/** Post-processing after communication setup has failed. */
void Tracker::CommunicationEstablishmentFailureProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::"
"CommunicationEstablishmentFailureProcessing called ...\n");
this->InvokeEvent( TrackerOpenErrorEvent() );
}
/** The Reset methods force the tracker to the
* CommunicationEstablished state */
void Tracker::ResetFromTrackingStateProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::"
"ResetFromTrackingStateProcessing() called ...\n");
// leaving TrackingState, going to CommunicationEstablishedState
this->ExitTrackingStateProcessing();
this->ResetFromToolsActiveStateProcessing();
// Report to all the tracker tools that the tracking state is reset and
// tracking should be stopped
typedef TrackerToolsContainerType::iterator InputConstIterator;
InputConstIterator inputItr = m_TrackerTools.begin();
InputConstIterator inputEnd = m_TrackerTools.end();
while( inputItr != inputEnd )
{
(inputItr->second)->RequestReportTrackingStopped();
++inputItr;
}
this->InvokeEvent( TrackerStopTrackingEvent() );
}
/** The Reset methods force the tracker to the
* CommunicationEstablished state */
void Tracker::ResetFromToolsActiveStateProcessing( void )
{
igstkLogMacro( DEBUG,
"igstk::Tracker::ResetFromToolsActiveStateProcessing() called ...\n");
this->ResetFromCommunicatingStateProcessing();
}
/** The Reset methods force the tracker to the
* CommunicationEstablished state */
void Tracker::ResetFromCommunicatingStateProcessing( void )
{
ResultType result = this->InternalReset();
if( result == SUCCESS )
{
igstkLogMacro( DEBUG, "igstk::Tracker::InternalReset succeeded ...\n");
}
else if( result == FAILURE )
{
igstkLogMacro( DEBUG, "igstk::Tracker::InternalReset failed ...\n");
}
}
/** Post-processing after ports and tools setup has been successful. */
void Tracker::ToolsActivationSuccessProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::ToolsActivationSuccessProcessing "
"called ...\n");
this->InvokeEvent( TrackerInitializeEvent() );
}
/** Post-processing after ports and tools setup has failed. */
void Tracker::ToolsActivationFailureProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::ToolsActivationFailureProcessing "
"called ...\n");
this->InvokeEvent( TrackerInitializeErrorEvent() );
}
/** The "AttemptToStartTracking" method attempts to start tracking. */
void Tracker::AttemptToStartTrackingProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::AttemptToStartTrackingProcessing "
"called ...\n");
ResultType result = this->InternalStartTracking();
m_StateMachine.PushInputBoolean( (bool)result,
m_SuccessInput,
m_FailureInput );
}
/** Post-processing after start tracking has been successful. */
void Tracker::StartTrackingSuccessProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::StartTrackingSuccessProcessing "
"called ...\n");
// Report to all the tracker tools that tracking has been started
typedef TrackerToolsContainerType::iterator InputConstIterator;
InputConstIterator inputItr = m_TrackerTools.begin();
InputConstIterator inputEnd = m_TrackerTools.end();
while( inputItr != inputEnd )
{
(inputItr->second)->RequestReportTrackingStarted();
++inputItr;
}
// going from AttemptingToTrackState to TrackingState
this->EnterTrackingStateProcessing();
this->InvokeEvent( TrackerStartTrackingEvent() );
}
/** Post-processing after start tracking has failed. */
void Tracker::StartTrackingFailureProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::StartTrackingFailureProcessing "
"called ...\n");
this->InvokeEvent( TrackerStartTrackingErrorEvent() );
}
/** Post-processing after attaching a tracker tool to the tracker
* has been successful. */
void Tracker::AttachingTrackerToolSuccessProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::AttachingTrackerToolSuccessProcessing "
"called ...\n");
m_TrackerTools[ m_TrackerToolToBeAttached->GetTrackerToolIdentifier() ]
= m_TrackerToolToBeAttached;
// report to the tracker tool that the attachment has been
// successful
m_TrackerToolToBeAttached->RequestReportSuccessfulTrackerToolAttachment();
// Add the tracker tool to the internal data containers
this->AddTrackerToolToInternalDataContainers( m_TrackerToolToBeAttached );
//connect the tracker tool coordinate system to the tracker
//system. By default, make the tracker coordinate system to
//be a parent of the tracker tool coordinate system
//If a reference tracker tool is specified, the reference
//tracker tool will become the parent of all the tracker tools.
TransformType identityTransform;
identityTransform.SetToIdentity(
igstk::TimeStamp::GetZeroValue() );
m_TrackerToolToBeAttached->RequestSetTransformAndParent(
identityTransform, this );
}
/** Post-processing after attaching a tracker tool to the tracker
* has failed. */
void Tracker::AttachingTrackerToolFailureProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::AttachingTrackerToolFailureProcessing "
"called ...\n");
// report to the tracker tool that the attachment has failed
m_TrackerToolToBeAttached->RequestReportFailedTrackerToolAttachment();
}
/** The "AttemptToStopTracking" method attempts to stop tracking. */
void Tracker::AttemptToStopTrackingProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::AttemptToStopTrackingProcessing "
"called ...\n");
// leaving TrackingState, going to AttemptingToStopTrackingState
this->ExitTrackingStateProcessing();
ResultType result = this->InternalStopTracking();
m_StateMachine.PushInputBoolean( (bool)result,
m_SuccessInput,
m_FailureInput );
}
/** Post-processing after stop tracking has been successful. */
void Tracker::StopTrackingSuccessProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::StopTrackingSuccessProcessing "
"called ...\n");
// Report to all the tracker tools that tracking has been stopped
typedef TrackerToolsContainerType::iterator InputConstIterator;
InputConstIterator inputItr = m_TrackerTools.begin();
InputConstIterator inputEnd = m_TrackerTools.end();
while( inputItr != inputEnd )
{
(inputItr->second)->RequestReportTrackingStopped();
++inputItr;
}
this->InvokeEvent( TrackerStopTrackingEvent() );
}
/** Post-processing after start tracking has failed. */
void Tracker::StopTrackingFailureProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::StopTrackingFailureProcessing "
"called ...\n");
// going from AttemptingToStopTrackingState to TrackingState
this->EnterTrackingStateProcessing();
this->InvokeEvent( TrackerStopTrackingErrorEvent() );
}
/** Needs to be called every time when entering tracking state. */
void Tracker::EnterTrackingStateProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::EnterTrackingStateProcessing "
"called ...\n");
if ( ! m_TrackingThreadStarted && this->GetThreadingEnabled() )
{
m_ThreadID = m_Threader->SpawnThread( TrackingThreadFunction, this );
m_TrackingThreadStarted= true;
}
m_PulseGenerator->RequestStart();
}
/** Needs to be called every time when exiting tracking state. */
void Tracker::ExitTrackingStateProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::ExitTrackingStateProcessing "
"called ...\n");
// by default, we will exit tracking by terminating tracking thread
this->ExitTrackingTerminatingTrackingThread();
}
/** Exit tracking by terminating tracking thread */
void Tracker::ExitTrackingWithoutTerminatingTrackingThread( void )
{
igstkLogMacro( DEBUG,
"igstk::Tracker::ExitTrackingWithoutTerminatingTrackingThread "
"called ...\n");
m_PulseGenerator->RequestStop();
}
/** Exit tracking by terminating tracking thread */
void Tracker::ExitTrackingTerminatingTrackingThread( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::ExitTrackingTerminatingTrackingThread "
"called ...\n");
m_PulseGenerator->RequestStop();
// Terminating the TrackingThread.
if ( this->GetThreadingEnabled() )
{
m_Threader->TerminateThread( m_ThreadID );
m_TrackingThreadStarted = false;
}
}
/** The "AttemptToUpdateStatus" method attempts to update status
during tracking. */
void Tracker::AttemptToUpdateStatusProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::AttemptToUpdateStatusProcessing "
"called ...\n");
// Set all tools to "not updated"
//
typedef TrackerToolsContainerType::iterator InputConstIterator;
InputConstIterator inputItr = m_TrackerTools.begin();
InputConstIterator inputEnd = m_TrackerTools.end();
while( inputItr != inputEnd )
{
(inputItr->second)->SetUpdated(false);
++inputItr;
}
// wait for a new transform to be available, it would be nice if
// "Wait" had a time limit like pthread_cond_timedwait() on Unix or
// WaitForSingleObject() on Windows
if ( this->GetThreadingEnabled() )
{
m_ConditionNextTransformReceived->Wait(
& m_LockForConditionNextTransformReceived );
}
else
{
this->InternalThreadedUpdateStatus();
}
ResultType result = this->InternalUpdateStatus();
m_StateMachine.PushInputBoolean( (bool)result,
m_SuccessInput,
m_FailureInput );
}
/** This method is called when a call to UpdateStatus succeeded */
void Tracker::UpdateStatusSuccessProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::UpdateStatusSuccessProcessing "
"called ...\n");
typedef TrackerToolsContainerType::iterator InputConstIterator;
InputConstIterator inputItr = m_TrackerTools.begin();
InputConstIterator inputEnd = m_TrackerTools.end();
while( inputItr != inputEnd )
{
if ( (inputItr->second)->GetUpdated() &&
( !m_ApplyingReferenceTool || m_ReferenceTool->GetUpdated() ) )
{
const TransformType transform = (inputItr->second)->GetRawTransform();
const double timeToExpiration = transform.GetExpirationTime() -
transform.GetStartTime();
TransformType::VersorType rotationRaw;
TransformType::VectorType translationRaw;
translationRaw = transform.GetTranslation();
rotationRaw = transform.GetRotation();
TransformType toolRawTransform;
toolRawTransform.SetTranslationAndRotation( translationRaw, rotationRaw,
transform.GetError(),
timeToExpiration );
(inputItr->second)->SetRawTransform( toolRawTransform );
TransformType toolCalibrationTransform
= (inputItr->second)->GetCalibrationTransform();
TransformType toolCalibratedTransform;
toolCalibratedTransform =
toolCalibratedTransform.TransformCompose(
toolRawTransform, toolCalibrationTransform );
(inputItr->second)->SetCalibratedTransform( toolCalibratedTransform );
//throw an event
(inputItr->second)->InvokeEvent( TrackerToolTransformUpdateEvent() );
// if a reference tracker tool has been specified, then if the tracker
// tool that is being updated is the selected reference tracker tool,
// then update the transform that is from the tracker to the
// reference tracker tool. Otherwise, update the transform from the
// tracker tool to the tracker.
if( m_ApplyingReferenceTool )
{
if ( (inputItr->first) == m_ReferenceTool->GetTrackerToolIdentifier())
{
this->RequestSetTransformAndParent(
toolCalibratedTransform.GetInverse(), inputItr->second );
}
else
{
(inputItr->second)->RequestSetTransformAndParent(
toolCalibratedTransform, this );
}
}
else
{
(inputItr->second)->RequestSetTransformAndParent(
toolCalibratedTransform, this );
}
}
++inputItr;
}
this->InvokeEvent( TrackerUpdateStatusEvent() );
}
/** This method is called when a UpdateStatus failed */
void Tracker::UpdateStatusFailureProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::UpdateStatusFailureProcessing "
"called ...\n");
this->InvokeEvent( TrackerUpdateStatusErrorEvent() );
}
/** The "CloseFromTrackingStateProcessing" method closes tracker in
* use, when the tracker is in tracking state. */
void Tracker::CloseFromTrackingStateProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::CloseFromTrackingStateProcessing "
"called ...\n");
// leaving TrackingState, going to AttemptingToCloseState
this->ExitTrackingStateProcessing();
ResultType result = this->InternalStopTracking();
// detach all the tracker tools from the tracker
this->DetachAllTrackerToolsFromTracker();
// Terminating the TrackingThread and if it is started
if ( m_TrackingThreadStarted && this->GetThreadingEnabled() )
{
m_Threader->TerminateThread( m_ThreadID );
m_TrackingThreadStarted = false;
}
if( result == SUCCESS )
{
result = this->InternalClose();
}
m_StateMachine.PushInputBoolean( (bool)result,
m_SuccessInput,
m_FailureInput );
}
/** Detach all tracker tools from the tracker */
void Tracker::DetachAllTrackerToolsFromTracker()
{
typedef TrackerToolsContainerType::iterator InputConstIterator;
InputConstIterator inputItr = m_TrackerTools.begin();
InputConstIterator inputEnd = m_TrackerTools.end();
while( inputItr != inputEnd )
{
this->RemoveTrackerToolFromInternalDataContainers( inputItr->second );
++inputItr;
}
m_TrackerTools.clear();
}
/** The "CloseFromCommunicatingStateProcessing" method closes
* tracker in use, when the tracker is in communicating state. */
void Tracker::CloseFromCommunicatingStateProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::"
"CloseFromCommunicatingStateProcessing called ...\n");
// Detach all the tracker tools from the tracker
this->DetachAllTrackerToolsFromTracker();
// Terminating the TrackingThread and if it is started
if ( m_TrackingThreadStarted && this->GetThreadingEnabled() )
{
m_Threader->TerminateThread( m_ThreadID );
m_TrackingThreadStarted = false;
}
ResultType result = this->InternalClose();
m_StateMachine.PushInputBoolean( (bool)result,
m_SuccessInput,
m_FailureInput );
}
/** Post-processing after close tracking has been successful. */
void Tracker::CloseCommunicationSuccessProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::"
"CloseCommunicationSuccessProcessing called ...\n");
this->InvokeEvent( TrackerCloseEvent() );
}
/** Post-processing after close tracking has failed. */
void Tracker::CloseCommunicationFailureProcessing( void )
{
igstkLogMacro( DEBUG, "igstk::Tracker::"
"CloseCommunicationFailureProcessing called ...\n");
this->InvokeEvent( TrackerCloseErrorEvent() );
}
/** Print object information */
void Tracker::PrintSelf( std::ostream& os, itk::Indent indent ) const
{
Superclass::PrintSelf(os, indent);
if( this->m_PulseGenerator )
{
os << indent << this->m_PulseGenerator << std::endl;
}
if( this->m_PulseObserver )
{
os << indent << this->m_PulseObserver << std::endl;
}
os << indent << "ValidityTime: " << this->m_ValidityTime << std::endl;
os << indent << "CoordinateSystemDelegator: ";
this->m_CoordinateSystemDelegator->PrintSelf( os, indent );
}
/** The "SetFrequencyProcessing" passes the frequency value to the Pulse
* Generator. Note that it is still possible for the PulseGenerator to reject
* the value and stay at its current frequency. */
void Tracker::SetFrequencyProcessing( void )
{
igstkLogMacro( DEBUG,
"igstk::Tracker::SetFrequencyProcessing called ...\n");
this->m_PulseGenerator->RequestSetFrequency( this->m_FrequencyToBeSet );
//Set the validity time of the transforms based on the tracker frequency
//Add a constant to avoid any flickering effect
//const double nonFlickeringConstant = 20;
this->m_ValidityTime = (1000/m_FrequencyToBeSet) + NON_FLICKERING_CONSTANT;
}
/** Request adding a tool to the tracker */
void
Tracker::
RequestAttachTool( TrackerToolType * trackerTool )
{
igstkLogMacro( DEBUG, "igstk::Tracker::"
"RequestAttachTool called ...\n");
m_TrackerToolToBeAttached = trackerTool;
igstkPushInputMacro( AttachTrackerTool );
this->m_StateMachine.ProcessInputs();
}
/** The "AttemptToAttachTrackerToolProcessing" method attempts to
* add a tracker tool to the tracker */
void Tracker::AttemptToAttachTrackerToolProcessing( void )
{
igstkLogMacro( DEBUG,
"igstk::Tracker::AttemptToAttachTrackerToolProcessing called ...\n");
// Verify the tracker tool information before adding it to the
// tracker. The conditions that need be verified depend on
// the tracker type. For example, for MicronTracker, the
// the tracker should have access to the template file of the
// Marker that is attached to the tracker tool.
ResultType result =
this->VerifyTrackerToolInformation( m_TrackerToolToBeAttached );
m_StateMachine.PushInputBoolean( (bool)result,
m_SuccessInput,
m_FailureInput );
}
/** Request remove a tool from the tracker */
Tracker::ResultType
Tracker::
RequestRemoveTool( TrackerToolType * trackerTool )
{
this->m_TrackerTools.erase( trackerTool->GetTrackerToolIdentifier() );
this->RemoveTrackerToolFromInternalDataContainers( trackerTool );
return SUCCESS;
}
const Tracker::TrackerToolsContainerType &
Tracker::GetTrackerToolContainer() const
{
return m_TrackerTools;
}
/** Thread function for tracking */
ITK_THREAD_RETURN_TYPE Tracker::TrackingThreadFunction(void* pInfoStruct)
{
struct itk::MultiThreader::ThreadInfoStruct * pInfo =
(struct itk::MultiThreader::ThreadInfoStruct*)pInfoStruct;
if( pInfo == NULL )
{
return ITK_THREAD_RETURN_VALUE;
}
if( pInfo->UserData == NULL )
{
return ITK_THREAD_RETURN_VALUE;
}
Tracker *pTracker = (Tracker*)pInfo->UserData;
// counters for error rates
unsigned long errorCount = 0;
unsigned long totalCount = 0;
int activeFlag = 1;
while ( activeFlag )
{
ResultType result = pTracker->InternalThreadedUpdateStatus();
pTracker->m_ConditionNextTransformReceived->Signal();
totalCount++;
if (result != SUCCESS)
{
errorCount++;
}
// check to see if we are being told to quit
pInfo->ActiveFlagLock->Lock();
activeFlag = *pInfo->ActiveFlag;
pInfo->ActiveFlagLock->Unlock();
}
igstkLogMacroStatic(pTracker, DEBUG, "TrackingThreadFunction was "
"terminated, " << errorCount << " errors "
"out of " << totalCount << "updates." << std::endl );
return ITK_THREAD_RETURN_VALUE;
}
/** Report to the tracker tool that the tool is not available */
void
Tracker::ReportTrackingToolNotAvailable( TrackerToolType * trackerTool ) const
{
igstkLogMacro( DEBUG,
"igstk::Tracker::ReportTrackingToolNotAvailable called...\n");
trackerTool->RequestReportTrackingToolNotAvailable();
}
/** Report to the tracker tool that the tool is Visible */
void
Tracker::ReportTrackingToolVisible( TrackerToolType * trackerTool ) const
{
igstkLogMacro( DEBUG,
"igstk::Tracker::ReportTrackingToolVisible called...\n");
trackerTool->RequestReportTrackingToolVisible();
}
/** Set raw transform */
void
Tracker::SetTrackerToolRawTransform(
TrackerToolType * trackerTool, const TransformType transform )
{
igstkLogMacro( DEBUG,
"igstk::Tracker::SetTrackerToolRawTransform called...\n");
trackerTool->SetRawTransform( transform );
}
/** Turn on/off update flag of the tracker tool */
void
Tracker::SetTrackerToolTransformUpdate(
TrackerToolType * trackerTool, bool flag ) const
{
igstkLogMacro( DEBUG,
"igstk::Tracker::SetTrackerToolTransformUpdate called...\n");
trackerTool->SetUpdated( flag );
}
/** Report invalid request */
void Tracker::ReportInvalidRequestProcessing( void )
{
igstkLogMacro( DEBUG,
"igstk::Tracker::ReportInvalidRequestProcessing called...\n");
this->InvokeEvent( InvalidRequestErrorEvent() );
}
}
|