Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "mitkTimeStamp.h"
00019 #include <map>
00020
00021 #include "mitkRealTimeClock.h"
00022
00023 mitk::TimeStamp::Pointer mitk::TimeStamp::s_Instance = NULL;
00024
00025 mitk::TimeStamp::TimeStamp() : itk::Object()
00026 , m_Time(-1.0), m_ReferenceTime(0.0)
00027 {
00028 }
00029
00030 mitk::TimeStamp::~TimeStamp()
00031 {
00032 }
00033
00034
00042 mitk::TimeStamp* mitk::TimeStamp::CreateInstance()
00043 {
00044 if ( s_Instance.IsNotNull() )
00045 {
00046 return s_Instance;
00047 }
00048 else
00049 {
00050 try
00051 {
00052 mitk::TimeStamp::Pointer ts = new mitk::TimeStamp;
00053 s_Instance = ts;
00054 }
00055 catch(...)
00056 {
00057 s_Instance = NULL;
00058 throw 1;
00059 }
00060 return s_Instance;
00061 }
00062
00063 }
00064
00071 mitk::TimeStamp* mitk::TimeStamp::GetInstance()
00072 {
00073 if (TimeStamp::s_Instance.IsNull())
00074 {
00075 mitk::TimeStamp::Pointer ts = new mitk::TimeStamp;
00076 s_Instance = ts;
00077 return s_Instance;
00078 }
00079 else
00080 return s_Instance;
00081 }
00082
00093 void mitk::TimeStamp::Start(itk::Object::Pointer device)
00094 {
00095 if (m_RealTimeClock.IsNull())
00096 {
00097 Initialize();
00098 }
00099 if ( s_Instance.IsNotNull() )
00100 {
00101 if (m_DeviceMap.empty())
00102 {
00103 m_ReferenceTime = GetCurrentStamp();
00104 m_Time = 0.0;
00105 }
00106 m_DeviceMap.insert( std::pair<itk::Object::Pointer, double>(device, this->GetElapsed()) );
00107 }
00108 else
00109 {
00110 itkGenericOutputMacro("Trying to use mitk::TimeStamp::Start() "
00111 << "without an available singleton instance. Either no instance has "
00112 << "been created (use TimeStamp::CreateInstance) or it has already "
00113 << "been destroyed.");
00114 }
00115 }
00116
00124 void mitk::TimeStamp::Stop(itk::Object::Pointer device)
00125 {
00126 if ( s_Instance.IsNotNull() )
00127 {
00128 m_MapIterator = m_DeviceMap.find(device);
00129 if ( m_MapIterator != m_DeviceMap.end() )
00130 {
00131 m_DeviceMap.erase( m_MapIterator );
00132 }
00133
00134 if (m_DeviceMap.empty())
00135 {
00136 m_ReferenceTime = NULL;
00137 m_Time = -1;
00138 }
00139 }
00140 else
00141 {
00142 itkGenericOutputMacro("Trying to use mitk::TimeStamp::Stop() "
00143 << "without an available singleton instance. Either no instance has "
00144 << "been created (use TimeStamp::CreateInstance) or it has already "
00145 << "been destroyed.");
00146 }
00147 }
00148
00149
00150 double mitk::TimeStamp::GetElapsed()
00151 {
00152 if (m_Time > -1)
00153 {
00154 m_Time = GetCurrentStamp();
00155 m_Time = m_Time - m_ReferenceTime;
00156 }
00157 return (double) m_Time;
00158 }
00159
00160
00161 double mitk::TimeStamp::GetElapsed(itk::Object::Pointer device)
00162 {
00163 double offset = this->GetOffset( device );
00164 if ( offset > -1 )
00165 {
00166 double time = this->GetElapsed();
00167 return (double) time - this->GetOffset(device);
00168 }
00169 else
00170 {
00171 return (double) -1;
00172 }
00173 }
00174
00175
00181 double mitk::TimeStamp::GetCurrentStamp()
00182 {
00183 if (m_RealTimeClock.IsNotNull())
00184 {
00185 return m_RealTimeClock->GetCurrentStamp();
00186 }
00187 else return 0.0;
00188 }
00189
00201 void mitk::TimeStamp::SetRealTimeClock(mitk::RealTimeClock::Pointer Clock)
00202 {
00203 m_RealTimeClock = Clock;
00204 }
00205
00219 double mitk::TimeStamp::GetOffset(itk::Object::Pointer Device)
00220 {
00221 m_MapIterator = m_DeviceMap.find(Device);
00222 if ( m_MapIterator != m_DeviceMap.end() )
00223 {
00224 return m_MapIterator->second;
00225 }
00226 else
00227 {
00228 return -1.0;
00229 }
00230 }
00231
00240 void mitk::TimeStamp::Initialize()
00241 {
00242 if ( m_RealTimeClock.IsNull() )
00243 m_RealTimeClock = mitk::RealTimeClock::New();
00244 }
00245