Berkeley SfM
Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
bsfm::util::ProgressBar Class Reference

#include <progress_bar.h>

Public Member Functions

 ProgressBar (const std::string &message, double max)
 
 ~ProgressBar ()
 
std::string Update (double progress)
 
void Reset (double max)
 
double GetMax () const
 
double GetProgress () const
 
double GetPercentage () const
 
double GetRemainingSeconds ()
 

Private Member Functions

double ElapsedToRemaining (double elapsed) const
 
std::string FormatRemaining (double elapsed) const
 
std::string ProgressBarString () const
 
std::string PrintProgress (double elapsed)
 

Private Attributes

Timer timer_
 
std::string message_
 
double progress_
 
double max_
 
size_t last_message_length_
 

Static Private Attributes

static const size_t bar_length_ = 40
 

Detailed Description

Definition at line 40 of file progress_bar.h.

Constructor & Destructor Documentation

bsfm::util::ProgressBar::ProgressBar ( const std::string &  message,
double  max 
)

Definition at line 43 of file progress_bar.cpp.

44  : message_(message), progress_(0), max_(max), last_message_length_(0) {
45  timer_.Tic();
46 }
double Tic()
Definition: timer.cpp:52
bsfm::util::ProgressBar::~ProgressBar ( )

Definition at line 48 of file progress_bar.cpp.

48 {}

Member Function Documentation

double bsfm::util::ProgressBar::ElapsedToRemaining ( double  elapsed) const
private

Definition at line 78 of file progress_bar.cpp.

78  {
79  if (progress_ == 0.0)
80  return 0.0;
81 
82  return elapsed * (max_ / progress_ - 1.0);
83 }
std::string bsfm::util::ProgressBar::FormatRemaining ( double  elapsed) const
private

Definition at line 85 of file progress_bar.cpp.

85  {
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 }
double bsfm::util::ProgressBar::GetMax ( ) const

Definition at line 62 of file progress_bar.cpp.

62  {
63  return max_;
64 }
double bsfm::util::ProgressBar::GetPercentage ( ) const

Definition at line 70 of file progress_bar.cpp.

70  {
71  return progress_ / max_ * 100.;
72 }
double bsfm::util::ProgressBar::GetProgress ( ) const

Definition at line 66 of file progress_bar.cpp.

66  {
67  return progress_;
68 }
double bsfm::util::ProgressBar::GetRemainingSeconds ( )

Definition at line 74 of file progress_bar.cpp.

74  {
75  return ElapsedToRemaining(timer_.Toc());
76 }
double Toc()
Definition: timer.cpp:73
double ElapsedToRemaining(double elapsed) const
std::string bsfm::util::ProgressBar::PrintProgress ( double  elapsed)
private

Definition at line 110 of file progress_bar.cpp.

110  {
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 }
std::string ProgressBarString() const
static const size_t bar_length_
Definition: progress_bar.h:79
double GetPercentage() const
std::string FormatRemaining(double elapsed) const
double ElapsedToRemaining(double elapsed) const
std::string bsfm::util::ProgressBar::ProgressBarString ( ) const
private

Definition at line 99 of file progress_bar.cpp.

99  {
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 }
static const size_t bar_length_
Definition: progress_bar.h:79
void bsfm::util::ProgressBar::Reset ( double  max)

Definition at line 56 of file progress_bar.cpp.

56  {
57  max_ = max;
58  progress_ = 0;
59  timer_.Tic();
60 }
double Tic()
Definition: timer.cpp:52
std::string bsfm::util::ProgressBar::Update ( double  progress)

Definition at line 50 of file progress_bar.cpp.

50  {
51  progress_ = progress;
52  double elapsed = timer_.Toc();
53  return PrintProgress(elapsed);
54 }
std::string PrintProgress(double elapsed)
double Toc()
Definition: timer.cpp:73

Member Data Documentation

const size_t bsfm::util::ProgressBar::bar_length_ = 40
staticprivate

Definition at line 79 of file progress_bar.h.

size_t bsfm::util::ProgressBar::last_message_length_
private

Definition at line 78 of file progress_bar.h.

double bsfm::util::ProgressBar::max_
private

Definition at line 76 of file progress_bar.h.

std::string bsfm::util::ProgressBar::message_
private

Definition at line 73 of file progress_bar.h.

double bsfm::util::ProgressBar::progress_
private

Definition at line 75 of file progress_bar.h.

Timer bsfm::util::ProgressBar::timer_
private

Definition at line 72 of file progress_bar.h.


The documentation for this class was generated from the following files: