Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkNavigationDataPlayer.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 
19 #include <itksys/SystemTools.hxx>
20 #include <mitkIGTTimeStamp.h>
21 #include <fstream>
22 
23 #include "mitkIGTException.h"
24 
26  : m_CurPlayerState(PlayerStopped),
27  m_StartPlayingTimeStamp(0.0), m_PauseTimeStamp(0.0)
28 {
29  // to get a start time
31 }
32 
34 {
35  StopPlaying();
36 }
37 
39 {
40  if ( m_NavigationDataSet->Size() == 0 )
41  {
42  MITK_WARN << "Cannot do anything with empty set of navigation datas.";
43  return;
44  }
45 
46  //Only produce new output if the player is started
47  if (m_CurPlayerState != PlayerRunning)
48  {
49  //The output is not valid anymore
50  this->GraftEmptyOutput();
51  return;
52  }
53 
54  // get elapsed time since start of playing
55  m_TimeStampSinceStart = mitk::IGTTimeStamp::GetInstance()->GetElapsed() - m_StartPlayingTimeStamp;
56 
57  // add offset of the first navigation data to the timestamp to start playing
58  // imediatly with the first navigation data (not to wait till the first time
59  // stamp is reached)
60  TimeStampType timeStampSinceStartWithOffset = m_TimeStampSinceStart
61  + m_NavigationDataSet->Begin()->at(0)->GetIGTTimeStamp();
62 
63  // iterate through all NavigationData objects of the given tool index
64  // till the timestamp of the NavigationData is greater then the given timestamp
65  for (; m_NavigationDataSetIterator != m_NavigationDataSet->End(); ++m_NavigationDataSetIterator)
66  {
67  // test if the timestamp of the successor is greater than the time stamp
68  if ( m_NavigationDataSetIterator+1 == m_NavigationDataSet->End() ||
69  (m_NavigationDataSetIterator+1)->at(0)->GetIGTTimeStamp() > timeStampSinceStartWithOffset )
70  {
71  break;
72  }
73  }
74 
75  for (unsigned int index = 0; index < GetNumberOfOutputs(); index++)
76  {
77  mitk::NavigationData* output = this->GetOutput(index);
78  if( !output ) { mitkThrowException(mitk::IGTException) << "Output of index "<<index<<" is null."; }
79 
80  output->Graft(m_NavigationDataSetIterator->at(index));
81  }
82 
83  // stop playing if the last NavigationData objects were grafted
84  if (m_NavigationDataSetIterator+1 == m_NavigationDataSet->End())
85  {
86  this->StopPlaying();
87 
88  // start playing again if repeat is enabled
89  if ( m_Repeat ) { this->StartPlaying(); }
90  }
91 }
92 
94 {
95  this->Modified(); // make sure that we need to be updated
96  Superclass::UpdateOutputInformation();
97 }
98 
100 {
101  // make sure that player is initialized before playing starts
102  this->InitPlayer();
103 
104  // set state and iterator for playing from start
105  m_CurPlayerState = PlayerRunning;
106  m_NavigationDataSetIterator = m_NavigationDataSet->Begin();
107 
108  // reset playing timestamps
109  m_PauseTimeStamp = 0;
110  m_TimeStampSinceStart = 0;
111 
112  // timestamp for indicating playing start set to current elapsed time
113  m_StartPlayingTimeStamp = mitk::IGTTimeStamp::GetInstance()->GetElapsed();
114 }
115 
117 {
118  m_CurPlayerState = PlayerStopped;
119 }
120 
122 {
123  //player runs and pause was called -> pause the player
124  if(m_CurPlayerState == PlayerRunning)
125  {
126  m_CurPlayerState = PlayerPaused;
127  m_PauseTimeStamp = mitk::IGTTimeStamp::GetInstance()->GetElapsed();
128  }
129  else
130  {
131  MITK_ERROR << "Player is either not started or already is paused" << std::endl;
132  }
133 }
134 
136 {
137  // player is in pause mode -> play at the last position
138  if(m_CurPlayerState == PlayerPaused)
139  {
140  m_CurPlayerState = PlayerRunning;
141 
142  // in this case m_StartPlayingTimeStamp is set to the total elapsed time with NO playback
143  m_StartPlayingTimeStamp = mitk::IGTTimeStamp::GetInstance()->GetElapsed()
144  - (m_PauseTimeStamp - m_StartPlayingTimeStamp);
145  }
146  else
147  {
148  MITK_ERROR << "Player is not paused!" << std::endl;
149  }
150 }
151 
153 {
154  return m_CurPlayerState;
155 }
156 
158 {
159  return m_TimeStampSinceStart;
160 }
void Resume()
This method resumes the player when it was paused.
void Pause()
This method pauses the player. If you want to play again call Resume()
#define MITK_ERROR
Definition: mitkLogMacros.h:24
Navigation Data.
double GetElapsed()
returns the time elapsed since calling Start() for the first time in milliseconds ...
mitk::NavigationData::TimeStampType TimeStampType
An object of this class represents an exception of the MITK-IGT module.
virtual void UpdateOutputInformation() override
Used for pipeline update just to tell the pipeline that we always have to update. ...
virtual void GenerateData() override
Set outputs to the navigation data object corresponding to current time.
#define MITK_WARN
Definition: mitkLogMacros.h:23
void StartPlaying()
This method starts the player.
static IGTTimeStamp * GetInstance()
returns a pointer to the current instance of mitkTimeStamp
void Start(itk::Object::Pointer device)
starts the time-acquisition
void StopPlaying()
Stops the player and closes the stream. After a call of StopPlaying(), StartPlaying() must be called ...
#define mitkThrowException(classname)
virtual void Graft(const DataObject *data) override
Graft the data and information from one NavigationData to another.