Open3D (C++ API)  0.18.0
ProgressReporters.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <functional>
11 #include <string>
12 
14 
15 namespace open3d {
16 namespace utility {
17 
23 public:
24  CountingProgressReporter(std::function<bool(double)> f) {
25  update_progress_ = f;
26  }
27  void SetTotal(int64_t total) { total_ = total; }
28  bool Update(int64_t count) {
29  if (!update_progress_) return true;
30  last_count_ = count;
31  double percent = 0;
32  if (total_ > 0) {
33  if (count < total_) {
34  percent = count * 100.0 / total_;
35  } else {
36  percent = 100.0;
37  }
38  }
39  return CallUpdate(percent);
40  }
41  void Finish() { CallUpdate(100); }
42  // for compatibility with ProgressBar
43  void operator++() { Update(last_count_ + 1); }
44 
45 private:
46  bool CallUpdate(double percent) {
47  if (update_progress_) {
48  return update_progress_(percent);
49  }
50  return true;
51  }
52  std::function<bool(double)> update_progress_;
53  int64_t total_ = -1;
54  int64_t last_count_ = -1;
55 };
56 
59  ConsoleProgressUpdater(const std::string &progress_info,
60  bool active = false)
61  : progress_bar_(100, progress_info, active) {}
62  bool operator()(double pct) {
63  while (last_pct_ < pct) {
64  ++last_pct_;
65  ++progress_bar_;
66  }
67  return true;
68  }
69 
70 private:
71  utility::ProgressBar progress_bar_;
72  int last_pct_ = 0;
73 };
74 
75 } // namespace utility
76 } // namespace open3d
Definition: ProgressReporters.h:22
void SetTotal(int64_t total)
Definition: ProgressReporters.h:27
void operator++()
Definition: ProgressReporters.h:43
bool Update(int64_t count)
Definition: ProgressReporters.h:28
CountingProgressReporter(std::function< bool(double)> f)
Definition: ProgressReporters.h:24
void Finish()
Definition: ProgressReporters.h:41
Definition: ProgressBar.h:15
int count
Definition: FilePCD.cpp:42
Definition: PinholeCameraIntrinsic.cpp:16
update_progress(double percent) functor for ProgressBar
Definition: ProgressReporters.h:58
bool operator()(double pct)
Definition: ProgressReporters.h:62
ConsoleProgressUpdater(const std::string &progress_info, bool active=false)
Definition: ProgressReporters.h:59