IGSTK

/build/buildd/igstk-4.2.0/Source/igstkTracker.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Image Guided Surgery Software Toolkit
00004   Module:    $RCSfile: igstkTracker.h,v $
00005   Language:  C++
00006   Date:      $Date: 2009-06-11 21:53:53 $
00007   Version:   $Revision: 1.63 $
00008 
00009   Copyright (c) ISC  Insight Software Consortium.  All rights reserved.
00010   See IGSTKCopyright.txt or http://www.igstk.org/copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00014      PURPOSE.  See the above copyright notices for more information.
00015 
00016 =========================================================================*/
00017 
00018 #ifndef __igstkTracker_h
00019 #define __igstkTracker_h
00020 
00021 #include <vector>
00022 #include <map>
00023 
00024 #include "itkMutexLock.h"
00025 #include "itkConditionVariable.h"
00026 #include "itkMultiThreader.h"
00027 
00028 #include "igstkObject.h"
00029 #include "igstkStateMachine.h"
00030 #include "igstkTransform.h"
00031 #include "igstkPulseGenerator.h"
00032 #include "igstkTrackerTool.h"
00033 
00034 #include "igstkCoordinateSystemInterfaceMacros.h"
00035 
00036 
00037 namespace igstk
00038 {
00039 
00040 igstkEventMacro( TrackerEvent,                             StringEvent);
00041 igstkEventMacro( TrackerErrorEvent,                IGSTKErrorWithStringEvent);
00042 
00043 igstkEventMacro( TrackerOpenEvent,                         TrackerEvent);
00044 igstkEventMacro( TrackerOpenErrorEvent,                    TrackerErrorEvent);
00045 
00046 igstkEventMacro( TrackerCloseEvent,                        TrackerEvent);
00047 igstkEventMacro( TrackerCloseErrorEvent,                   TrackerErrorEvent);
00048 
00049 igstkEventMacro( TrackerInitializeEvent,                   TrackerEvent);
00050 igstkEventMacro( TrackerInitializeErrorEvent,              TrackerErrorEvent);
00051 
00052 igstkEventMacro( TrackerStartTrackingEvent,                TrackerEvent);
00053 igstkEventMacro( TrackerStartTrackingErrorEvent,           TrackerErrorEvent);
00054 
00055 igstkEventMacro( TrackerStopTrackingEvent,                 TrackerEvent);
00056 igstkEventMacro( TrackerStopTrackingErrorEvent,            TrackerErrorEvent);
00057 
00058 igstkEventMacro( TrackerUpdateStatusEvent,                 TrackerEvent);
00059 igstkEventMacro( TrackerUpdateStatusErrorEvent,            TrackerErrorEvent);
00060 
00061 igstkEventMacro( TrackerToolTransformUpdateEvent,          TrackerEvent);
00062 
00063 
00096 class Tracker : public Object
00097 {
00098 
00099 public:
00101   igstkStandardAbstractClassTraitsMacro( Tracker, Object ) 
00102 
00103 public:
00104 
00105   igstkFriendClassMacro( TrackerTool );
00106 
00108   typedef TrackerTool       TrackerToolType;
00109 
00113   void RequestOpen( void );
00114 
00118   void RequestClose( void );
00119 
00122   void RequestReset( void );
00123 
00126   void RequestStartTracking( void );
00127 
00129   void RequestStopTracking( void );
00130   
00136   void RequestSetFrequency( double frequencyInHz );
00137 
00139   void RequestSetReferenceTool( TrackerToolType * trackerTool );
00140 
00142   igstkSetMacro( ThreadingEnabled, bool );
00143 
00145   igstkGetMacro( ThreadingEnabled, bool );
00146 
00147 protected:
00148 
00149   Tracker(void);
00150 
00151   virtual ~Tracker(void);
00152 
00154   typedef Transform::TimePeriodType         TimePeriodType;
00155 
00157   igstkGetMacro( ValidityTime, TimePeriodType );
00158 
00159   typedef enum 
00160     { 
00161     FAILURE=0, 
00162     SUCCESS
00163     } ResultType;
00164 
00166   typedef Transform                      TransformType;
00167 
00171   virtual ResultType InternalOpen( void ) = 0;
00172 
00176   virtual ResultType InternalClose( void ) = 0;
00177 
00181   virtual ResultType InternalReset( void ) = 0;
00182 
00186   virtual ResultType InternalStartTracking( void ) = 0;
00187 
00191   virtual ResultType InternalStopTracking( void ) = 0;
00192 
00193 
00197   virtual ResultType InternalUpdateStatus( void ) = 0;
00198 
00203   virtual ResultType InternalThreadedUpdateStatus( void ) = 0;
00204 
00206   virtual void PrintSelf( std::ostream& os, itk::Indent indent ) const; 
00207 
00220   virtual ResultType 
00221         VerifyTrackerToolInformation( const TrackerToolType * ) = 0; 
00222 
00229   virtual ResultType ValidateSpecifiedFrequency( double frequencyInHz );
00230 
00233   virtual ResultType RemoveTrackerToolFromInternalDataContainers(
00234                                      const TrackerToolType * trackerTool ) = 0; 
00235 
00237   virtual ResultType AddTrackerToolToInternalDataContainers( 
00238                                     const TrackerToolType * trackerTool ) = 0;
00239 
00241   typedef std::map< std::string, TrackerToolType *>  TrackerToolsContainerType;
00242 
00246   const TrackerToolsContainerType & GetTrackerToolContainer() const;
00247 
00249   void ReportTrackingToolNotAvailable( TrackerToolType * trackerTool ) const;
00250 
00252   void ReportTrackingToolVisible( TrackerToolType * trackerTool ) const;
00253 
00255   void SetTrackerToolRawTransform( TrackerToolType * trackerTool, 
00256                                    const TransformType transform );
00257 
00259   void SetTrackerToolTransformUpdate( TrackerToolType * trackerTool,
00260                                       bool flag ) const;
00261 
00271   void ExitTrackingStateProcessing( void );
00272 
00274   void ExitTrackingWithoutTerminatingTrackingThread();
00275 
00277   void ExitTrackingTerminatingTrackingThread();
00278 
00279 
00280 private:
00281   Tracker(const Self&);           //purposely not implemented
00282   void operator=(const Self&);    //purposely not implemented
00283 
00285   PulseGenerator::Pointer   m_PulseGenerator;
00286   
00288   typedef itk::SimpleMemberCommand< Self >   ObserverType;
00289   ObserverType::Pointer     m_PulseObserver;
00290 
00291   // An associative container of TrackerTool Pointer with 
00292   // TrackerTool identifier used as a Key
00293   TrackerToolsContainerType           m_TrackerTools;
00294   
00296   typedef TrackerToolType::Pointer                   TrackerToolPointer;
00297 
00299   bool                                m_ApplyingReferenceTool;
00300   TrackerToolPointer                  m_ReferenceTool;
00301 
00303   TimePeriodType                      m_ValidityTime;
00304 
00307   bool                                m_ThreadingEnabled;
00308 
00311   bool                                m_TrackingThreadStarted;
00312 
00314   itk::MultiThreader::Pointer     m_Threader;
00315 
00317   int                             m_ThreadID;
00318 
00321   itk::ConditionVariable::Pointer m_ConditionNextTransformReceived;
00322   
00325   itk::SimpleMutexLock            m_LockForConditionNextTransformReceived;
00326 
00328   igstkDeclareStateMacro( Idle );
00329   igstkDeclareStateMacro( AttemptingToEstablishCommunication );
00330   igstkDeclareStateMacro( AttemptingToCloseCommunication );
00331   igstkDeclareStateMacro( CommunicationEstablished );
00332   igstkDeclareStateMacro( AttemptingToAttachTrackerTool );
00333   igstkDeclareStateMacro( TrackerToolAttached );
00334   igstkDeclareStateMacro( AttemptingToTrack );
00335   igstkDeclareStateMacro( Tracking );
00336   igstkDeclareStateMacro( AttemptingToUpdate );
00337   igstkDeclareStateMacro( AttemptingToStopTracking );
00338 
00340   igstkDeclareInputMacro( EstablishCommunication );
00341   igstkDeclareInputMacro( StartTracking );
00342   igstkDeclareInputMacro( AttachTrackerTool );
00343   igstkDeclareInputMacro( UpdateStatus );
00344   igstkDeclareInputMacro( StopTracking );
00345   igstkDeclareInputMacro( Reset );
00346   igstkDeclareInputMacro( CloseCommunication );
00347   igstkDeclareInputMacro( ValidFrequency );
00348 
00349   igstkDeclareInputMacro( Success );
00350   igstkDeclareInputMacro( Failure );
00351 
00354   void RequestAttachTool( TrackerToolType * trackerTool );
00355 
00357   ResultType RequestRemoveTool( TrackerToolType * trackerTool );
00358 
00360   static ITK_THREAD_RETURN_TYPE TrackingThreadFunction(void* pInfoStruct);
00361 
00365   void UpdateStatus( void );
00366 
00369   void AttemptToOpenProcessing( void );
00370   
00373   void AttemptToStartTrackingProcessing( void );
00374 
00376   void AttemptToStopTrackingProcessing( void );
00377 
00380   void AttemptToAttachTrackerToolProcessing( void );
00381 
00384   void AttemptToUpdateStatusProcessing( void );
00385 
00388   void UpdateStatusSuccessProcessing( void );
00389 
00392   void UpdateStatusFailureProcessing( void );
00393 
00396   void CloseFromTrackingStateProcessing( void );
00397 
00400   void CloseFromCommunicatingStateProcessing( void );
00401 
00404   void ResetFromTrackingStateProcessing( void );
00405 
00408   void ResetFromToolsActiveStateProcessing( void);
00409 
00412   void ResetFromCommunicatingStateProcessing( void );
00413 
00415   void CommunicationEstablishmentSuccessProcessing( void );
00416 
00418   void CommunicationEstablishmentFailureProcessing( void );
00419 
00421   void ToolsActivationSuccessProcessing( void );
00422 
00424   void ToolsActivationFailureProcessing( void );
00425 
00427   void StartTrackingSuccessProcessing( void );
00428 
00430   void StartTrackingFailureProcessing( void );
00431 
00434   void AttachingTrackerToolSuccessProcessing( void );
00435 
00438   void AttachingTrackerToolFailureProcessing( void );
00439 
00441   void StopTrackingSuccessProcessing( void );
00442 
00444   void StopTrackingFailureProcessing( void );
00445 
00447   void CloseCommunicationSuccessProcessing( void );
00448 
00450   void CloseCommunicationFailureProcessing( void );
00451 
00453   void EnterTrackingStateProcessing( void );
00454 
00456   void DetachAllTrackerToolsFromTracker();
00457 
00459   void ReportInvalidRequestProcessing( void );
00460 
00462   void SetFrequencyProcessing( void );
00463 
00466   igstkCoordinateSystemClassInterfaceMacro();
00467 
00468   TrackerToolType   * m_TrackerToolToBeAttached;
00469 
00470   double              m_FrequencyToBeSet;
00471 };
00472 
00473 }
00474 
00475 #endif //__igstk_Tracker_h_