Berkeley SfM
progress_bar.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, The Regents of the University of California (Regents).
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Please contact the author(s) of this library if you have any questions.
34  * Authors: Erik Nelson ( eanelson@eecs.berkeley.edu )
35  * David Fridovich-Keil ( dfk@eecs.berkeley.edu )
36  */
37 
38 #include "progress_bar.h"
39 
40 namespace bsfm {
41 namespace util {
42 
43 ProgressBar::ProgressBar(const std::string &message, double max)
44  : message_(message), progress_(0), max_(max), last_message_length_(0) {
45  timer_.Tic();
46 }
47 
49 
50 std::string ProgressBar::Update(double progress) {
51  progress_ = progress;
52  double elapsed = timer_.Toc();
53  return PrintProgress(elapsed);
54 }
55 
56 void ProgressBar::Reset(double max) {
57  max_ = max;
58  progress_ = 0;
59  timer_.Tic();
60 }
61 
62 double ProgressBar::GetMax() const {
63  return max_;
64 }
65 
66 double ProgressBar::GetProgress() const {
67  return progress_;
68 }
69 
71  return progress_ / max_ * 100.;
72 }
73 
75  return ElapsedToRemaining(timer_.Toc());
76 }
77 
78 double ProgressBar::ElapsedToRemaining(double elapsed) const {
79  if (progress_ == 0.0)
80  return 0.0;
81 
82  return elapsed * (max_ / progress_ - 1.0);
83 }
84 
85 std::string ProgressBar::FormatRemaining(double elapsed) const {
86  // Elapsed is in seconds. Convert to HH:MM:SS.
87  int hours = static_cast<int>(elapsed) / 3600;
88  int minutes = (static_cast<int>(elapsed) - 3600 * hours) / 60;
89  int seconds = static_cast<int>(elapsed) % 60;
90 
91  std::stringstream ss;
92  ss << std::setfill('0') << std::setw(2) << hours << "h:";
93  ss << std::setfill('0') << std::setw(2) << minutes << "m:";
94  ss << std::setfill('0') << std::setw(2) << seconds << "s";
95 
96  return ss.str();
97 }
98 
99 std::string ProgressBar::ProgressBarString() const {
100  std::stringstream progress_bar;
101  std::string equals(floor((bar_length_ - 3) * progress_ / max_), '=');
102  progress_bar << "[" << equals << ">";
103 
104  std::string remainder(bar_length_ - 3 - equals.length(), ' ');
105  progress_bar << remainder << "]";
106 
107  return progress_bar.str();
108 }
109 
110 std::string ProgressBar::PrintProgress(double elapsed) {
111  double remaining = ElapsedToRemaining(elapsed);
112  std::string remaining_string = FormatRemaining(remaining);
113 
114  std::stringstream ss;
115  ss << " " << message_ << ": " << progress_ << "/" << max_ << " ("
116  << std::fixed << std::setprecision(2) << GetPercentage()
117  << "%%), remaining: " << remaining_string << ".";
118 
119  std::string progress_bar = ProgressBarString();
120 
121  size_t num_backspaces = bar_length_ + last_message_length_;
122  printf("%s", std::string(num_backspaces, '\b').c_str());
123 
124  printf("%s", progress_bar.c_str());
125  printf("%s", ss.str().c_str());
126 
127  last_message_length_ = ss.str().length();
128 
129  return ss.str();
130 }
131 
132 } //\namespace util
133 } //\namespace bsfm
double GetMax() const
std::string ProgressBarString() const
static const size_t bar_length_
Definition: progress_bar.h:79
std::string PrintProgress(double elapsed)
void Reset(double max)
double GetPercentage() const
double GetProgress() const
std::string FormatRemaining(double elapsed) const
std::string Update(double progress)
ProgressBar(const std::string &message, double max)
Definition: camera.cpp:50
double Toc()
Definition: timer.cpp:73
double ElapsedToRemaining(double elapsed) const
double Tic()
Definition: timer.cpp:52