Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ConsoleProgress.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #ifndef __CONSOLE_PROGRESS_HH__
26 #define __CONSOLE_PROGRESS_HH__
27 
28 // std
29 #include <iostream>
30 
31 // BIAS
32 #include <Base/Debug/Error.hh>
33 
34 namespace BIAS
35 {
36  /**
37  * @brief Visualises the progress of some computation in the console.
38  *
39  * This class is intended to show the progress of an iteration over a large
40  * number of elements. It only prints itegral percentage values and so doesn't
41  * spam the console.
42  *
43  * If you only iterate over a small number of elements (e.g. 100), performance
44  * may be hit drastically because output is flushed after each integral
45  * percentage (i.e. an I/O operation is performed).
46  *
47  * @author rwulff
48  * @date 03/2011
49  */
50  class BIASUtils_EXPORT ConsoleProgress
51  {
52 
53  public:
54 
55  /**
56  * @brief Constructor.
57  *
58  * @param maxValue
59  * the maximum value to reach, equivalent to 100%
60  */
61  explicit inline
62  ConsoleProgress(unsigned int maxValue = 0) :
63  percentageShown_(0), maxValue_(0)
64  {
65  Reset(maxValue);
66  }
67 
68  /**
69  * @brief Resets the internal counter and sets the maximum value to reach.
70  *
71  * @param maxValue
72  * the maximum value to reach, equivalent to 100%. if set to 0, the
73  * current maxValue will not be changed.
74  */
75  inline
76  void Reset(unsigned int maxValue = 0)
77  {
78  percentageShown_ = 0;
79  if (maxValue > 0) {
80  maxValue_ = maxValue;
81  }
82  }
83 
84  /**
85  * @brief Prints the current percentage if it is equal or greater than all
86  * integral percentage values that have been printed before.
87  *
88  * @note No endl is appended. You may want to print it on your own after the
89  * iteration is finished.
90  *
91  * @param curValue
92  * the current value within interval [0, maxValue]
93  */
94  inline
95  void PrintPercentage(unsigned int curValue)
96  {
97  BIASASSERT(curValue <= maxValue_)
98 
99  const float curPercentage =
100  (maxValue_ > 0 ? float(curValue) / float(maxValue_) * 100.0f : 0.0f);
101  const unsigned int curPercRounded = (unsigned int)(curPercentage + 0.5f);
102 
103  if (curPercentage >= percentageShown_) {
104  std::cout << "\r";
105  if (curPercRounded < 10) {
106  std::cout << " ";
107  }
108  if (curPercRounded < 100) {
109  std::cout << " ";
110  }
111  std::cout << curPercRounded << "% " << std::flush;
112 
113  percentageShown_ = curPercRounded + 1;
114  }
115  }
116 
117  /**
118  * @brief Prints progress bar showing the current percentage.
119  * @note No end line is appended.
120  * @param curValue The current value within interval [0, maxValue].
121  * @param size The length of the progress bar in characters.
122  */
123  inline
124  void PrintProgressBar(unsigned int curValue, unsigned int size = 25)
125  {
126  BIASASSERT(curValue <= maxValue_);
127 
128  const float curPercentage =
129  (maxValue_ > 0 ? float(curValue) / float(maxValue_) * 100.0f : 0.0f);
130  const unsigned int curPercRounded = (unsigned int)(curPercentage + 0.5f);
131 
132  if (curPercentage >= percentageShown_) {
133  std::cout << "\r[";
134  const float step = 100.0f / (float) size;
135  float perc = 0.0f;
136  for (unsigned int i = 0; i < size; i++, perc += step) {
137  std::cout << (perc < curPercentage ? "=" : " ");
138  }
139  std::cout << "] " << curPercRounded << "% " << std::flush;
140 
141  percentageShown_ = curPercRounded + 1;
142  }
143  }
144 
145  private:
146 
147  /// @brief counter to ensure that only integral values are printed
148  unsigned int percentageShown_;
149 
150  /// @brief the maximum value, equivalent to 100%
151  unsigned int maxValue_;
152  };
153 }
154 
155 #endif // __CONSOLE_PROGRESS_HH__
void PrintProgressBar(unsigned int curValue, unsigned int size=25)
Prints progress bar showing the current percentage.
Visualises the progress of some computation in the console.
void PrintPercentage(unsigned int curValue)
Prints the current percentage if it is equal or greater than all integral percentage values that have...
ConsoleProgress(unsigned int maxValue=0)
Constructor.
void Reset(unsigned int maxValue=0)
Resets the internal counter and sets the maximum value to reach.