Progress.cpp
Go to the documentation of this file.
1 
28  /*
29  * ProgressBar.cpp
30  *
31  * Created on: 14.01.2011
32  * Author: Thomas Wiemann
33  */
34 
35 
36 #include "lvr2/io/Progress.hpp"
37 
38 #include <sstream>
39 #include <iostream>
40 
41 using std::stringstream;
42 using std::cout;
43 using std::endl;
44 using std::flush;
45 
46 namespace lvr2
47 {
48 
51 
52 ProgressBar::ProgressBar(size_t max_val, string prefix)
53 {
54  m_prefix = prefix;
55  m_maxVal = max_val;
56  m_currentVal = 0;
57  m_percent = 0;
58 
59  if(m_titleCallback)
60  {
61  // Remove time brackets
62  unsigned index;
63  index = prefix.find_last_of("]");
64  m_titleCallback(prefix.substr(index+1));
65  }
66 }
67 
69 {
70 
71 }
72 
74 {
75  m_progressCallback = ptr;
76 }
77 
79 {
80  m_titleCallback = ptr;
81 }
82 
84 {
85  boost::mutex::scoped_lock lock(m_mutex);
86 
87  m_currentVal++;
88  short difference = (short)((float)m_currentVal/m_maxVal * 100 - m_percent);
89  if (difference < 1)
90  {
91  return;
92  }
93 
94  while (difference >= 1)
95  {
96  m_percent++;
97  difference--;
98  print_bar();
99 
101  {
103  }
104  }
105 
106 }
107 
109 {
110  boost::mutex::scoped_lock lock(m_mutex);
111 
112  m_currentVal+= n;
113  short difference = (short)((float)m_currentVal/m_maxVal * 100 - m_percent);
114  if (difference < 1)
115  {
116  return;
117  }
118 
119  while (difference >= 1)
120  {
121  m_percent++;
122  difference--;
123  print_bar();
124 
126  {
128  }
129  }
130 
131 }
132 
134 {
135  cout << "\r" << m_prefix << " " << m_percent << "%" << flush;
136 }
137 
138 ProgressCounter::ProgressCounter(int stepVal, string prefix)
139 {
140  m_prefix = prefix;
141  m_stepVal = stepVal;
142  m_currentVal = 0;
143 }
144 
146 {
147  boost::mutex::scoped_lock lock(m_mutex);
148  m_currentVal++;
149  if(m_currentVal % m_stepVal == 0)
150  {
151  print_progress();
152  }
153 }
154 
156 {
157  cout << "\r" << m_prefix << " " << m_currentVal << flush;
158 }
159 
162 
163 PacmanProgressBar::PacmanProgressBar(size_t max_val, string prefix, size_t bar_length)
164 :
165  m_prefix(prefix)
166  ,m_bar_length(bar_length)
167 {
168  m_maxVal = max_val;
169  m_currentVal = 0;
170  m_percent = 0;
171 
172  if(m_titleCallback)
173  {
174  // Remove time brackets
175  unsigned index;
176  index = prefix.find_last_of("]");
177  m_titleCallback(prefix.substr(index+1));
178  }
179 }
180 
182 {
183 
184 }
185 
187 {
188  m_progressCallback = ptr;
189 }
190 
192 {
193  m_titleCallback = ptr;
194 }
195 
197 {
198  boost::mutex::scoped_lock lock(m_mutex);
199 
200  m_currentVal++;
201  short difference = (short)((float)m_currentVal/m_maxVal * 100 - m_percent);
202 
203  if (difference < 1)
204  {
205  return;
206  }
207 
208  while (difference >= 1)
209  {
210  m_percent++;
211  difference--;
212  print_bar();
213 
215  {
217  }
218  }
219 
220 }
221 
223 {
224  int char_idx = static_cast<float>(m_percent)/100.0 * (m_bar_length);
225 
226  cout << "\r" << m_prefix << " " << m_percent << "%" << " | ";
227 
228  for(size_t i=0; i< char_idx; i++)
229  {
230  cout << " ";
231  }
232 
233  if(char_idx % 2 == 0)
234  {
235  cout << "ᗧ";
236  }else{
237  cout << "O";
238  }
239 
240  for(size_t i=char_idx; i < m_bar_length; i++)
241  {
242  if(i%2 == 0)
243  {
244  cout << " ";
245  } else {
246  cout << "•";
247  }
248  }
249 
250  cout << " | " << flush;
251 }
252 
253 } // namespace lvr
254 
lvr2::ProgressCounter::ProgressCounter
ProgressCounter(int stepVal, string prefix="")
Definition: Progress.cpp:138
lvr2::PacmanProgressBar::setProgressTitleCallback
static void setProgressTitleCallback(ProgressTitleCallbackPtr)
Registers a callback that is called when a new progress instance is created.
Definition: Progress.cpp:191
lvr2::ProgressCounter::m_stepVal
size_t m_stepVal
The step value for output generation.
Definition: Progress.hpp:173
lvr2::ProgressBar::m_titleCallback
static ProgressTitleCallbackPtr m_titleCallback
Definition: Progress.hpp:136
lvr2::ProgressCounter::operator++
void operator++()
Definition: Progress.cpp:145
lvr2::ProgressBar::setProgressTitleCallback
static void setProgressTitleCallback(ProgressTitleCallbackPtr)
Registers a callback that is called when a new progress instance is created.
Definition: Progress.cpp:78
lvr2::PacmanProgressBar::PacmanProgressBar
PacmanProgressBar(size_t max_val, string prefix="", size_t bar_length=60)
Ctor.
Definition: Progress.cpp:163
lvr2::ProgressBar::operator+=
void operator+=(size_t n)
Increases the counter of performed by n.
Definition: Progress.cpp:108
lvr2::ProgressBar::m_maxVal
size_t m_maxVal
The number of iterations.
Definition: Progress.hpp:118
lvr2::ProgressBar::ProgressBar
ProgressBar(size_t max_val, string prefix="")
Ctor.
Definition: Progress.cpp:52
lvr2::ProgressBar::m_percent
int m_percent
The current progress in percent.
Definition: Progress.hpp:127
lvr2::ProgressBar::setProgressCallback
static void setProgressCallback(ProgressCallbackPtr)
Registers a callback that is called with the new value when the percentage of the progress changed.
Definition: Progress.cpp:73
lvr2::ProgressBar::print_bar
void print_bar()
Prints the output.
Definition: Progress.cpp:133
lvr2::ProgressCounter::m_prefix
string m_prefix
The prefix string.
Definition: Progress.hpp:170
lvr2::PacmanProgressBar::m_progressCallback
static PacmanProgressCallbackPtr m_progressCallback
Definition: Progress.hpp:266
lvr2::PacmanProgressBar::setProgressCallback
static void setProgressCallback(ProgressCallbackPtr)
Registers a callback that is called with the new value when the percentage of the progress changed.
Definition: Progress.cpp:186
lvr2::PacmanProgressBar::operator++
void operator++()
Increases the counter of performed iterations.
Definition: Progress.cpp:196
lvr2::PacmanProgressBar::~PacmanProgressBar
virtual ~PacmanProgressBar()
Definition: Progress.cpp:181
lvr2::PacmanProgressBar::m_maxVal
size_t m_maxVal
The number of iterations.
Definition: Progress.hpp:246
lvr2::PacmanProgressBar::m_currentVal
size_t m_currentVal
The current counter.
Definition: Progress.hpp:249
Progress.hpp
lvr2::ProgressCounter::m_currentVal
size_t m_currentVal
The current counter value.
Definition: Progress.hpp:176
lvr2::PacmanProgressBar::m_prefix
string m_prefix
The prefix string.
Definition: Progress.hpp:243
lvr2::ProgressBar::m_currentVal
size_t m_currentVal
The current counter.
Definition: Progress.hpp:121
lvr2::PacmanProgressBar::print_bar
void print_bar()
Prints the output.
Definition: Progress.cpp:222
lvr2::PacmanProgressCallbackPtr
void(* PacmanProgressCallbackPtr)(int)
A class to manage progress information output for process where the number of performed operations is...
Definition: Progress.hpp:199
lvr2::PacmanProgressBar::m_percent
int m_percent
The current progress in percent.
Definition: Progress.hpp:255
lvr2::ProgressBar::operator++
void operator++()
Increases the counter of performed iterations.
Definition: Progress.cpp:83
scripts.create_png.max_val
max_val
Definition: create_png.py:35
lvr2
Definition: BaseBufferManipulators.hpp:39
lvr2::ProgressCallbackPtr
void(* ProgressCallbackPtr)(int)
A class to manage progress information output for process where the number of performed operations is...
Definition: Progress.hpp:65
lvr2::ProgressCounter::m_mutex
boost::mutex m_mutex
A mutex object for counter increment (for parallel executions)
Definition: Progress.hpp:179
lvr2::ProgressBar::~ProgressBar
virtual ~ProgressBar()
Definition: Progress.cpp:68
lvr2::ProgressBar::m_prefix
string m_prefix
The prefix string.
Definition: Progress.hpp:115
lvr2::PacmanProgressBar::m_titleCallback
static PacmanProgressTitleCallbackPtr m_titleCallback
Definition: Progress.hpp:267
lvr2::PacmanProgressBar::m_mutex
boost::mutex m_mutex
A mutex object for counter increment (for parallel executions)
Definition: Progress.hpp:252
lvr2::PacmanProgressBar::m_bar_length
int m_bar_length
Definition: Progress.hpp:258
lvr2::ProgressBar::m_mutex
boost::mutex m_mutex
A mutex object for counter increment (for parallel executions)
Definition: Progress.hpp:124
lvr2::ProgressCounter::print_progress
void print_progress()
Prints the current state.
Definition: Progress.cpp:155
lvr2::PacmanProgressTitleCallbackPtr
void(* PacmanProgressTitleCallbackPtr)(string)
Definition: Progress.hpp:200
lvr2::ProgressTitleCallbackPtr
void(* ProgressTitleCallbackPtr)(string)
Definition: Progress.hpp:66
lvr2::ProgressBar::m_progressCallback
static ProgressCallbackPtr m_progressCallback
Definition: Progress.hpp:135


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:24