riCOM_cpp
This repository contains the C++ implementation of the riCOM (Real Time Centre Of Mass) algorithm for 4D Scanning electron microscopy.
Loading...
Searching...
No Matches
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
16ProgressMonitor::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
28int 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
35void 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 = 0;
65 report_set = false;
66 time_stamp = chc::high_resolution_clock::now();
67}
68
69void ProgressMonitor::Report(unsigned long idx, float print_val)
70{
71 try
72 {
73 if (idx > fr_total)
74 throw idx;
75
76 if (b_bar) // Print out the Progressbar and Frequency
77 {
78 // calculate percentage of progress
79 double progress_percent = idx * TOTAL_PERCENTAGE / fr_total;
80
81 // calculate the size of the progress bar
82 int bar_size = GetBarLength();
83
84 // calculate the percentage value of a unit bar
85 double percent_per_unit_bar = TOTAL_PERCENTAGE / bar_size;
86
87 // display progress bar
88 *out << "\r"
89 << "[";
90
91 for (int bar_length = 0; bar_length <= bar_size - 1; ++bar_length)
92 {
93 if (bar_length * percent_per_unit_bar < progress_percent)
94 {
95 *out << unit_bar;
96 }
97 else
98 {
99 *out << unit_space;
100 }
101 }
102 *out << "]" << std::setw(CHARACTER_WIDTH_PERCENTAGE + 1);
103 *out << std::setprecision(2) << std::fixed << print_val << std::fixed << " " << unit << std::flush;
104 }
105 if (idx >= fr_total)
106 {
107 *out << " " << std::endl
108 << std::flush;
109 }
110 }
111 catch (unsigned long e)
112 {
113 ClearBarField();
114 std::cerr << "EXCEPTION: frame index (" << e << ") went out of bounds (fr_total = " << fr_total << ")." << std::endl
115 << std::flush;
116 }
117}
#define CHARACTER_WIDTH_PERCENTAGE
#define TERMINAL_WIDTH
#define TOTAL_PERCENTAGE
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