MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkUSNavigationStepTimer.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
14
15#include <fstream>
16#include <sstream>
17
22
24{
25 // make sure that the timer will be stopped and an active
26 // measurement will be written to the output file
27 this->Stop();
28}
29
31{
32 this->FinishCurrentIndex();
33}
34
36{
37 m_Durations.clear();
38 m_TimeProbe = itk::TimeProbe();
39}
40
42{
43 m_OutputFileName = filename;
44
45 // write all durations to the file, which were measured up to now
46 if ( ! m_Durations.empty() ) { this->WriteEverythingToFile(); }
47}
48
49void mitk::USNavigationStepTimer::SetActiveIndex(unsigned int index, std::string description)
50{
51 this->FinishCurrentIndex();
52
53 m_CurrentIndex = index;
54 m_CurrentDescription = description;
55
56 m_TimeProbe.Start();
57}
58
60{
61 double duration = 0;
62
63 // sum all durations
64 for ( DurationsVector::iterator it = m_Durations.begin();
65 it != m_Durations.end(); ++it )
66 {
67 duration += it->duration;
68 }
69
70 return duration;
71}
72
74{
75 double duration = 0;
76
77 // sum durations of the given index
78 for ( DurationsVector::iterator it = m_Durations.begin();
79 it != m_Durations.end(); ++it )
80 {
81 if ( it->index == index ) { duration += it->duration; }
82 }
83
84 return duration;
85}
86
88{
89 if ( m_TimeProbe.GetNumberOfStarts() > 0 )
90 {
91 // save measurement of the current step
92 m_TimeProbe.Stop();
93 m_Durations.push_back(DurationForIndex(m_CurrentIndex, m_TimeProbe.GetTotal(), m_CurrentDescription));
94
95 if ( ! m_OutputFileName.empty() )
96 {
97 this->WriteLineToFile(*(m_Durations.end()-1));
98 }
99 }
100
101 m_TimeProbe = itk::TimeProbe();
102}
103
105{
106 std::ofstream filestream(m_OutputFileName.c_str());
107 if ( filestream.is_open() )
108 {
109 for ( DurationsVector::iterator it = m_Durations.begin();
110 it != m_Durations.end(); ++it )
111 {
112 filestream << it->index << ";" << it->duration << ";" << it->description << std::endl;
113 }
114 filestream.close();
115 }
116 else
117 {
118 std::string filename = m_OutputFileName;
119 m_OutputFileName.clear();
120 mitkThrow() << "Cannot open file '" << filename << "' for writing.";
121 }
122}
123
125{
126 std::ofstream filestream(m_OutputFileName.c_str(), std::ofstream::out | std::ofstream::app);
127 if ( filestream.is_open() )
128 {
129 filestream << element.index << ";" << element.duration << ";" << element.description << std::endl;
130 filestream.close();
131 }
132 else
133 {
134 std::string filename = m_OutputFileName;
135 m_OutputFileName.clear();
136 mitkThrow() << "Cannot open file '" << filename << "' for writing.";
137 }
138}
139
141{
142 m_Durations.clear();
143
144 std::ifstream filestream(filename.c_str());
145 if (! filestream.is_open() )
146 {
147 mitkThrow() << "Cannot open file '" << filename << "' for reading.";
148 }
149
150 std::string line;
151 while ( std::getline(filestream, line) )
152 {
153 size_t position = line.find(";");
154
155 // make sure that the format of the file is correct
156 if ( position == std::string::npos )
157 {
158 mitkThrow() << "File \"" << "\" contains invalid line (no ';' found).";
159 }
160
161 // the index is the first part of the line
162 std::stringstream streamIndex(line.substr(0, position));
163 unsigned int index;
164 streamIndex >> index;
165
166 line = line.substr(position+1);
167 position = line.find(";");
168
169 // make sure that the format of the file is correct
170 if ( position == std::string::npos )
171 {
172 mitkThrow() << "File \"" << "\" contains invalid line (no ';' found).";
173 }
174
175 // the duration is the second part
176 std::stringstream streamDuration(line.substr(0, position+1));
177 double duration;
178 streamDuration >> duration;
179
180 // the description is the third part (last part)
181 std::string description = line.substr(position+1);
182
183 m_Durations.push_back(DurationForIndex(index, duration, description));
184 }
185}
void Reset()
Removes all measured durations.
void ReadFromFile(std::string filename)
Read durations from the file of the given file name. This file must have exactly the same format as i...
void Stop()
Stops the timer for the current index. The measured time to if written to the output file if a file p...
double GetTotalDuration()
Returns the sum of all durations.
double GetDurationForIndex(unsigned int index)
Returns measured duration for the step with the given index. All measurements of this index are summe...
void SetActiveIndex(unsigned int index, std::string description="")
Starts measurement for the step with the given index.
void WriteLineToFile(const DurationForIndex &element)
void SetOutputFileName(std::string filename)
Sets the name of the output file. If there are already measurements of durations, they are written to...