00001 /*========================================================================= 00002 00003 Program: Medical Imaging & Interaction Toolkit 00004 Language: C++ 00005 Date: $Date$ 00006 Version: $Revision$ 00007 00008 Copyright (c) German Cancer Research Center, Division of Medical and 00009 Biological Informatics. All rights reserved. 00010 See MITKCopyright.txt or https://www.mitk.org/copyright.html 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 #include "mitkTrackingDevice.h" 00019 #include "mitkTimeStamp.h" 00020 #include "mitkTrackingTool.h" 00021 #include <itkMutexLockHolder.h> 00022 00023 typedef itk::MutexLockHolder<itk::FastMutexLock> MutexLockHolder; 00024 00025 00026 mitk::TrackingDevice::TrackingDevice() : 00027 m_Type(TrackingSystemNotSpecified), 00028 m_State(mitk::TrackingDevice::Setup), 00029 m_StopTracking(false), m_ErrorMessage("") 00030 00031 { 00032 m_StopTrackingMutex = itk::FastMutexLock::New(); 00033 m_StateMutex = itk::FastMutexLock::New(); 00034 m_TrackingFinishedMutex = itk::FastMutexLock::New(); 00035 m_TrackingFinishedMutex->Lock(); // execution rights are owned by the application thread at the beginning 00036 } 00037 00038 00039 mitk::TrackingDevice::~TrackingDevice() 00040 { 00041 m_TrackingFinishedMutex = NULL; 00042 m_StopTrackingMutex = NULL; 00043 } 00044 00045 00046 mitk::TrackingDevice::TrackingDeviceState mitk::TrackingDevice::GetState() const 00047 { 00048 MutexLockHolder lock(*m_StateMutex); 00049 return m_State; 00050 } 00051 00052 00053 void mitk::TrackingDevice::SetState( TrackingDeviceState state ) 00054 { 00055 itkDebugMacro("setting m_State to " << state); 00056 00057 MutexLockHolder lock(*m_StateMutex); // lock and unlock the mutex 00058 if (m_State == state) 00059 { 00060 return; 00061 } 00062 m_State = state; 00063 this->Modified(); 00064 } 00065 00066 00067 bool mitk::TrackingDevice::StopTracking() 00068 { 00069 if (this->GetState() == Tracking) // Only if the object is in the correct state 00070 { 00071 m_StopTrackingMutex->Lock(); // m_StopTracking is used by two threads, so we have to ensure correct thread handling 00072 m_StopTracking = true; 00073 m_StopTrackingMutex->Unlock(); 00074 //we have to wait here that the other thread recognizes the STOP-command and executes it 00075 m_TrackingFinishedMutex->Lock(); 00076 mitk::TimeStamp::GetInstance()->Stop(this); // notify realtime clock 00077 // StopTracking was called, thus the mode should be changed back 00078 // to Ready now that the tracking loop has ended. 00079 this->SetState(Ready); 00080 } 00081 return true; 00082 } 00083 00084 00085 mitk::TrackingTool* mitk::TrackingDevice::GetToolByName( std::string name ) const 00086 { 00087 unsigned int toolCount = this->GetToolCount(); 00088 for (unsigned int i = 0; i < toolCount; ++i) 00089 if (name == this->GetTool(i)->GetToolName()) 00090 return this->GetTool(i); 00091 return NULL; 00092 } 00093