ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ProgressReporters.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 #include "CVCoreLib.h"
10 #include "Logging.h" //for ConsoleProgressBar
11 
12 namespace cloudViewer {
13 namespace utility {
14 
20 public:
21  CountingProgressReporter(std::function<bool(double)> f) {
22  update_progress_ = f;
23  }
24  void SetTotal(int64_t total) { total_ = total; }
25  bool Update(int64_t count) {
26  if (!update_progress_) return true;
27  last_count_ = count;
28  double percent = 0;
29  if (total_ > 0) {
30  if (count < total_) {
31  percent = count * 100.0 / total_;
32  } else {
33  percent = 100.0;
34  }
35  }
36  return CallUpdate(percent);
37  }
38  void Finish() { CallUpdate(100); }
39  // for compatibility with ConsoleProgressBar
40  void operator++() { Update(last_count_ + 1); }
41 
42 private:
43  bool CallUpdate(double percent) {
44  if (update_progress_) {
45  return update_progress_(percent);
46  }
47  return true;
48  }
49  std::function<bool(double)> update_progress_;
50  int64_t total_ = -1;
51  int64_t last_count_ = -1;
52 };
53 
54 using utility::ConsoleProgressBar;
57  ConsoleProgressUpdater(const std::string &progress_info,
58  bool active = false)
59  : progress_bar_(100, progress_info, active) {}
60  bool operator()(double pct) {
61  while (last_pct_ < pct) {
62  ++last_pct_;
63  ++progress_bar_;
64  }
65  return true;
66  }
67 
68 private:
70  int last_pct_ = 0;
71 };
72 
73 } // namespace utility
74 } // namespace cloudViewer
#define CV_CORE_LIB_API
Definition: CVCoreLibWin.h:15
int count
utility::CountingProgressReporter * progress_bar_
Definition: FilePLY.cpp:42
CountingProgressReporter(std::function< bool(double)> f)
Generic file read and write utility for python interface.
update_progress(double percent) functor for ConsoleProgressBar
ConsoleProgressUpdater(const std::string &progress_info, bool active=false)