Open3D (C++ API)  0.11.0
ProgressReporters.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include "open3d/utility/Console.h" //for ConsoleProgressBar
30 
31 namespace open3d {
32 namespace utility {
33 
39 public:
40  CountingProgressReporter(std::function<bool(double)> f) {
41  update_progress_ = f;
42  }
43  void SetTotal(int64_t total) { total_ = total; }
44  bool Update(int64_t count) {
45  if (!update_progress_) return true;
46  last_count_ = count;
47  double percent = 0;
48  if (total_ > 0) {
49  if (count < total_) {
50  percent = count * 100.0 / total_;
51  } else {
52  percent = 100.0;
53  }
54  }
55  return CallUpdate(percent);
56  }
57  void Finish() { CallUpdate(100); }
58  // for compatibility with ConsoleProgressBar
59  void operator++() { Update(last_count_ + 1); }
60 
61 private:
62  bool CallUpdate(double percent) {
63  if (update_progress_) {
64  return update_progress_(percent);
65  }
66  return true;
67  }
68  std::function<bool(double)> update_progress_;
69  int64_t total_ = -1;
70  int64_t last_count_ = -1;
71 };
72 
76  ConsoleProgressUpdater(const std::string &progress_info,
77  bool active = false)
78  : progress_bar_(100, progress_info, active) {}
79  bool operator()(double pct) {
80  while (last_pct_ < pct) {
81  ++last_pct_;
82  ++progress_bar_;
83  }
84  return true;
85  }
86 
87 private:
88  ConsoleProgressBar progress_bar_;
89  int last_pct_ = 0;
90 };
91 
92 } // namespace utility
93 } // namespace open3d
void SetTotal(int64_t total)
Definition: ProgressReporters.h:43
void operator++()
Definition: ProgressReporters.h:59
void Finish()
Definition: ProgressReporters.h:57
bool Update(int64_t count)
Definition: ProgressReporters.h:44
CountingProgressReporter(std::function< bool(double)> f)
Definition: ProgressReporters.h:40
int count
Definition: FilePCD.cpp:61
update_progress(double percent) functor for ConsoleProgressBar
Definition: ProgressReporters.h:75
Definition: PinholeCameraIntrinsic.cpp:35
ConsoleProgressUpdater(const std::string &progress_info, bool active=false)
Definition: ProgressReporters.h:76
Definition: ProgressReporters.h:38
Definition: Console.h:211
bool operator()(double pct)
Definition: ProgressReporters.h:79