riCOM_cpp
This repository contains the C++ implementation of the riCOM (Real Time Centre Of Mass) algorithm for 4D Scanning electron microscopy.
ProgressMonitor.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2021 Thomas Friedrich, Chu-Ping Yu,
2  * University of Antwerp - All Rights Reserved.
3  * You may use, distribute and modify
4  * this code under the terms of the GPL3 license.
5  * You should have received a copy of the GPL3 license with
6  * this file. If not, please visit:
7  * https://www.gnu.org/licenses/gpl-3.0.en.html
8  *
9  * Authors:
10  * Thomas Friedrich <thomas.friedrich@uantwerpen.be>
11  * Chu-Ping Yu <chu-ping.yu@uantwerpen.be>
12  */
13 
14 #include "ProgressMonitor.h"
15 
16 ProgressMonitor::ProgressMonitor(size_t fr_total, bool b_bar, float report_interval, std::ostream &out) : fr_count(0), fr_count_i(0), fr_freq(0),
17  report_set(false), report_set_public(false),
18  first_frame(true), fr(0), fr_avg(0), fr_count_a(0),
19  time_stamp(chc::high_resolution_clock::now()), unit("kHz"),
20  unit_bar("#"), unit_space("-")
21 {
22  this->fr_total = fr_total;
23  this->b_bar = b_bar;
24  this->out = &out;
25  this->report_interval = report_interval;
26 }
27 
28 int ProgressMonitor::GetBarLength()
29 {
30  // get console width and according adjust the length of the progress bar
31  int bar_length = static_cast<int>((TERMINAL_WIDTH - CHARACTER_WIDTH_PERCENTAGE) / 2.);
32  return bar_length;
33 }
34 
35 void ProgressMonitor::ClearBarField()
36 {
37  for (int i = 0; i < TERMINAL_WIDTH; ++i)
38  {
39  *out << " ";
40  }
41  *out << "\r" << std::flush;
42 }
43 
45 {
46  fr_count_i++;
47  fr_count++;
48  auto mil_secs = chc::duration_cast<float_ms>(chc::high_resolution_clock::now() - time_stamp).count();
49  if (mil_secs > report_interval || fr_count >= fr_total)
50  {
51  fr = fr_count_i / mil_secs;
52  fr_avg += fr;
53  fr_count_a++;
54  fr_freq = fr_avg / fr_count_a;
55  Report(fr_count, fr_avg / fr_count_a);
56  report_set = true;
57  report_set_public = true;
58  }
59  return *this;
60 }
61 
63 {
64  fr_count_i += step;
65  fr_count += step;
66  auto mil_secs = chc::duration_cast<float_ms>(chc::high_resolution_clock::now() - time_stamp).count();
67  if (mil_secs > report_interval || fr_count >= fr_total)
68  {
69  fr = fr_count_i / mil_secs;
70  fr_avg += fr;
71  fr_count_a++;
72  fr_freq = fr_avg / fr_count_a;
73  Report(fr_count, fr_avg / fr_count_a);
74  report_set = true;
75  report_set_public = true;
76  }
77  return *this;
78 }
79 
80 
82 {
83  fr_count_i = 0;
84  report_set = false;
85  time_stamp = chc::high_resolution_clock::now();
86 }
87 
88 void ProgressMonitor::Report(size_t idx, float print_val)
89 {
90  try
91  {
92  if (idx > fr_total)
93  throw idx;
94 
95  if (b_bar) // Print out the Progressbar and Frequency
96  {
97  // calculate percentage of progress
98  double progress_percent = idx * TOTAL_PERCENTAGE / fr_total;
99 
100  // calculate the size of the progress bar
101  int bar_size = GetBarLength();
102 
103  // calculate the percentage value of a unit bar
104  double percent_per_unit_bar = TOTAL_PERCENTAGE / bar_size;
105 
106  // display progress bar
107  *out << "\r"
108  << "[";
109 
110  for (int bar_length = 0; bar_length <= bar_size - 1; ++bar_length)
111  {
112  if (bar_length * percent_per_unit_bar < progress_percent)
113  {
114  *out << unit_bar;
115  }
116  else
117  {
118  *out << unit_space;
119  }
120  }
121  *out << "]" << std::setw(CHARACTER_WIDTH_PERCENTAGE + 1);
122  *out << std::setprecision(2) << std::fixed << print_val << std::fixed << " " << unit << std::flush;
123  }
124  if (idx >= fr_total)
125  {
126  *out << " " << std::endl
127  << std::flush;
128  }
129  }
130  catch (size_t e)
131  {
132  ClearBarField();
133  std::cerr << "EXCEPTION: frame index (" << e << ") went out of bounds (fr_total = " << fr_total << ")." << std::endl
134  << std::flush;
135  }
136 }
#define CHARACTER_WIDTH_PERCENTAGE
#define TERMINAL_WIDTH
#define TOTAL_PERCENTAGE
ProgressMonitor & operator+=(int step)
std::atomic< bool > report_set
std::atomic< size_t > fr_count_i
ProgressMonitor(size_t fr_total, bool b_bar=true, float report_interval=250.0, std::ostream &out=std::cerr)
ProgressMonitor & operator++()
std::atomic< size_t > fr_count
std::atomic< bool > report_set_public