ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
NormalizedProgress.cpp
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 
9 
10 // system
11 #include <cassert>
12 
13 #ifdef USE_QT
14 
15 // we use Qt for the atomic counter
16 #include <QAtomicInt>
17 
18 class AtomicCounter : public QAtomicInt {};
19 
20 #else
21 
22 // we use a fake QAtomicInt
23 
25 
28 public:
30  inline int load() { return m_value; }
31  inline void store(int value) { m_value = value; }
32  inline int fetchAndAddRelaxed(int add) {
33  int original = m_value;
34  m_value += add;
35  return original;
36  }
37  int m_value;
38 };
39 
40 #endif
41 
42 using namespace cloudViewer;
43 
45  unsigned totalSteps,
46  unsigned totalPercentage /*=100*/)
47  : m_percent(0),
48  m_step(1),
49  m_percentAdd(1.0f),
50  m_counter(new AtomicCounter),
51  progressCallback(callback) {
52  scale(totalSteps, totalPercentage);
53 }
54 
56  if (m_counter) {
57  delete m_counter;
58  }
59 }
60 
61 void NormalizedProgress::scale(unsigned totalSteps,
62  unsigned totalPercentage /*=100*/,
63  bool updateCurrentProgress /*=false*/) {
64  if (progressCallback) {
65  if (totalSteps == 0 || totalPercentage == 0) {
66  m_step = 1;
67  m_percentAdd = 0;
68  return;
69  }
70 
71  if (totalSteps >= 2 * totalPercentage) {
72  m_step = static_cast<unsigned>(
73  ceil(static_cast<float>(totalSteps) / totalPercentage));
74  assert(m_step != 0 && m_step < totalSteps);
75  m_percentAdd =
76  static_cast<float>(totalPercentage) / (totalSteps / m_step);
77  } else {
78  m_step = 1;
79  m_percentAdd = static_cast<float>(totalPercentage) / totalSteps;
80  }
81 
82  if (updateCurrentProgress) {
83  m_percent = static_cast<float>(totalPercentage) / totalSteps *
84  static_cast<float>(m_counter->load());
85  } else {
86  m_counter->store(0);
87  }
88  }
89 }
90 
92  m_percent = 0;
93  m_counter->store(0);
94  if (progressCallback) {
96  }
97 }
98 
100  if (!progressCallback) {
101  return true;
102  }
103 
104  unsigned currentCount =
105  static_cast<unsigned>(m_counter->fetchAndAddRelaxed(1)) + 1;
106  if ((currentCount % m_step) == 0) {
109  }
110 
112 }
113 
114 bool NormalizedProgress::steps(unsigned n) {
115  if (!progressCallback) {
116  return true;
117  }
118 
119  unsigned currentCount =
120  static_cast<unsigned>(m_counter->fetchAndAddRelaxed(n)) + n;
121  unsigned d1 = currentCount / m_step;
122  unsigned d2 = (currentCount + n) / m_step;
123 
124  if (d2 != d1) // thread safe? Well '++int' is a kind of atomic operation ;)
125  {
126  m_percent += static_cast<float>(d2 - d1) * m_percentAdd;
128  }
129 
131 }
std::function< void(std::shared_ptr< core::Tensor >)> callback
Fake QAtomicInt.
int fetchAndAddRelaxed(int add)
void store(int value)
virtual void update(float percent)=0
Notifies the algorithm progress.
virtual bool isCancelRequested()=0
Checks if the process should be canceled.
bool steps(unsigned n)
Increments total progress value of more than a single unit.
void reset()
Resets progress state.
float m_percentAdd
Percentage added to total progress value at each step.
NormalizedProgress(GenericProgressCallback *callback, unsigned totalSteps, unsigned totalPercentage=100)
Default constructor.
bool oneStep()
Increments total progress value of a single unit.
virtual ~NormalizedProgress()
Destructor.
AtomicCounter * m_counter
Current number of calls to 'oneStep'.
void scale(unsigned totalSteps, unsigned totalPercentage=100, bool updateCurrentProgress=false)
float m_percent
Total progress value (in percent)
GenericProgressCallback * progressCallback
associated GenericProgressCallback
MiniVec< float, N > ceil(const MiniVec< float, N > &a)
Definition: MiniVec.h:89
Generic file read and write utility for python interface.