RTC Toolkit  2.0.0
computation.hpp
Go to the documentation of this file.
1 
12 #ifndef RTCTK_EXAMPLEDATATASK_MEASURETEL_COMPUTATION_HPP
13 #define RTCTK_EXAMPLEDATATASK_MEASURETEL_COMPUTATION_HPP
14 
17 
18 #include <vector>
19 
20 namespace rtctk::exampleDataTask {
21 
22 class Computation {
23 public:
25 
26  enum class Algorithm { Mean, Median };
27 
29  // get number of slopes from array
30  constexpr size_t NUM_SLOPES =
31  std::tuple_size<decltype(decltype(ScaoLoopTopic::wfs)::slopes)>::value;
32  m_result.resize(NUM_SLOPES);
33  Reset();
34  }
35 
36  void Reset() {
37  m_sample_idx = 0;
38  }
39 
40  void SetDynamicConfig(unsigned to_read) {
41  // pre-allocate memory
42  m_samples.resize(to_read);
43 
44  m_samples_to_read = to_read;
45  Reset();
46  }
47 
48  void OnDataAvailable(ScaoLoopTopic const& sample) {
49  m_samples[m_sample_idx++] = sample.wfs.slopes;
50  }
51 
52  struct Result {
53  std::vector<float> const& avg_values;
54  };
55 
56  Result Compute(Algorithm algorithm) {
58 
59  if (m_sample_idx != m_samples_to_read) {
60  CII_THROW(RtctkException, "not all data available");
61  }
62 
63  if (algorithm == Algorithm::Mean) {
64  ComputeMean();
65  } else {
66  CII_THROW(RtctkException, "algorithm not implemented");
67  }
68 
69  Reset();
70  return {m_result};
71  }
72 
73  void ComputeMean() {
74  for (auto& slope : m_result) {
75  slope = 0;
76  }
77 
78  for (auto const& sample : m_samples) {
79  size_t idx = 0;
80  for (auto const& slope : sample) {
81  m_result[idx++] += slope;
82  }
83  }
84 
85  for (auto& slope : m_result) {
86  slope = slope / m_samples.size();
87  }
88  }
89 
90 private:
91  unsigned m_sample_idx;
92  unsigned m_samples_to_read;
93  std::vector<decltype(decltype(ScaoLoopTopic::wfs)::slopes)> m_samples;
94  std::vector<float> m_result;
95 };
96 
97 } //namespace rtctk::exampleDataTask
98 
99 #endif // RTCTK_EXAMPLEDATATASK_MEASURETEL_COMPUTATION_HPP
rtctk::exampleTopic::ScaoLoopTopic::wfs
WfsLoopBaseTopic< N_SUBAPS > wfs
Definition: topics.hpp:30
rtctk::exampleDataTask::Computation::Algorithm::Mean
@ Mean
exceptions.hpp
Provides macros and utilities for exception handling.
rtctk::exampleDataTask::Computation
Definition: computation.hpp:22
rtctk::exampleDataTask::Computation::Algorithm
Algorithm
Definition: computation.hpp:26
rtctk::exampleDataTask::Computation::Algorithm::Median
@ Median
rtctk::exampleDataTask::Computation::Result::avg_values
std::vector< float > const & avg_values
Definition: computation.hpp:53
rtctk::exampleDataTask::Computation::SetDynamicConfig
void SetDynamicConfig(unsigned to_read)
Definition: computation.hpp:40
rtctk::componentFramework::RtctkException
The RtctkException class is the base class for all Rtctk exceptions.
Definition: exceptions.hpp:207
rtctk::exampleDataTask::Computation::Reset
void Reset()
Definition: computation.hpp:36
rtctk::exampleTopic::WfsLoopBaseTopic::slopes
std::array< float, 2 *NSUBAPS > slopes
Definition: topics.hpp:24
rtctk::exampleDataTask::Computation::OnDataAvailable
void OnDataAvailable(ScaoLoopTopic const &sample)
Definition: computation.hpp:48
rtctk::exampleTopic::ScaoLoopTopic
Definition: topics.hpp:28
rtctk::exampleDataTask::Computation::ComputeMean
void ComputeMean()
Definition: computation.hpp:73
rtctk::exampleDataTask
Definition: businessLogic.hpp:24
rtctk::exampleDataTask::Computation::Computation
Computation()
Definition: computation.hpp:28
rtctkExampleDataTaskGenFitsData.slopes
slopes
Definition: rtctkExampleDataTaskGenFitsData.py:9
rtctk::exampleDataTask::Computation::Result
Definition: computation.hpp:52
rtctk::exampleDataTask::Computation::Compute
Result Compute(Algorithm algorithm)
Definition: computation.hpp:56
topics.hpp
Common topic definitions used in various examples.