##// END OF EJS Templates
Generates vectors (2)...
Alexandre Leroux -
r785:7548fc623435
parent child
Show More
@@ -1,140 +1,164
1 1 #include "CosinusProvider.h"
2 2 #include "MockDefs.h"
3 3
4 4 #include <Data/DataProviderParameters.h>
5 5 #include <Data/ScalarSeries.h>
6 #include <Data/VectorSeries.h>
6 7
7 8 #include <cmath>
8 9
9 10 #include <QFuture>
10 11 #include <QThread>
11 12 #include <QtConcurrent/QtConcurrent>
12 13
13 14 Q_LOGGING_CATEGORY(LOG_CosinusProvider, "CosinusProvider")
14 15
15 16 namespace {
16 17
17 18 /// Abstract cosinus type
18 19 struct ICosinusType {
19 20 virtual ~ICosinusType() = default;
20 21 /// @return the number of components generated for the type
21 22 virtual int componentCount() const = 0;
22 23 /// @return the data series created for the type
23 24 virtual std::shared_ptr<IDataSeries> createDataSeries(std::vector<double> xAxisData,
24 25 std::vector<double> valuesData,
25 26 Unit xAxisUnit,
26 27 Unit valuesUnit) const = 0;
27 28 };
28 29
30 struct ScalarCosinus : public ICosinusType {
31 int componentCount() const override { return 1; }
32
33 std::shared_ptr<IDataSeries> createDataSeries(std::vector<double> xAxisData,
34 std::vector<double> valuesData, Unit xAxisUnit,
35 Unit valuesUnit) const override
36 {
37 return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData),
38 xAxisUnit, valuesUnit);
39 }
40 };
41 struct VectorCosinus : public ICosinusType {
42 int componentCount() const override { return 3; }
43
44 std::shared_ptr<IDataSeries> createDataSeries(std::vector<double> xAxisData,
45 std::vector<double> valuesData, Unit xAxisUnit,
46 Unit valuesUnit) const override
47 {
48 return std::make_shared<VectorSeries>(std::move(xAxisData), std::move(valuesData),
49 xAxisUnit, valuesUnit);
50 }
51 };
52
29 53 } // namespace
30 54
31 55 std::shared_ptr<IDataProvider> CosinusProvider::clone() const
32 56 {
33 57 // No copy is made in clone
34 58 return std::make_shared<CosinusProvider>();
35 59 }
36 60
37 61 std::shared_ptr<IDataSeries> CosinusProvider::retrieveData(QUuid acqIdentifier,
38 62 const SqpRange &dataRangeRequested,
39 63 const QVariantHash &data)
40 64 {
41 65 // TODO: Add Mutex
42 66 auto dataIndex = 0;
43 67
44 68 // Retrieves frequency
45 69 auto freqVariant = data.value(COSINUS_FREQUENCY_KEY, COSINUS_FREQUENCY_DEFAULT_VALUE);
46 70 if (!freqVariant.canConvert<double>()) {
47 71 qCCritical(LOG_CosinusProvider()) << tr("Can't retrieve data: invalid frequency");
48 72 return nullptr;
49 73 }
50 74
51 75 // Gets the timerange from the parameters
52 76 double freq = freqVariant.toDouble();
53 77 double start = std::ceil(dataRangeRequested.m_TStart * freq);
54 78 double end = std::floor(dataRangeRequested.m_TEnd * freq);
55 79
56 80 // We assure that timerange is valid
57 81 if (end < start) {
58 82 std::swap(start, end);
59 83 }
60 84
61 85 // Generates scalar series containing cosinus values (one value per second, end value is
62 86 // included)
63 87 auto dataCount = end - start + 1;
64 88
65 89 auto xAxisData = std::vector<double>{};
66 90 xAxisData.resize(dataCount);
67 91
68 92 auto valuesData = std::vector<double>{};
69 93 valuesData.resize(dataCount);
70 94
71 95 int progress = 0;
72 96 auto progressEnd = dataCount;
73 97 for (auto time = start; time <= end; ++time, ++dataIndex) {
74 98 auto it = m_VariableToEnableProvider.find(acqIdentifier);
75 99 if (it != m_VariableToEnableProvider.end() && it.value()) {
76 100 const auto timeOnFreq = time / freq;
77 101
78 102 xAxisData[dataIndex] = timeOnFreq;
79 103 valuesData[dataIndex] = std::cos(timeOnFreq);
80 104
81 105 // progression
82 106 int currentProgress = (time - start) * 100.0 / progressEnd;
83 107 if (currentProgress != progress) {
84 108 progress = currentProgress;
85 109
86 110 emit dataProvidedProgress(acqIdentifier, progress);
87 111 qCInfo(LOG_CosinusProvider()) << "TORM: CosinusProvider::retrieveData"
88 112 << QThread::currentThread()->objectName() << progress;
89 113 // NOTE: Try to use multithread if possible
90 114 }
91 115 }
92 116 else {
93 117 if (!it.value()) {
94 118 qCDebug(LOG_CosinusProvider())
95 119 << "CosinusProvider::retrieveData: ARRET De l'acquisition detectΓ©"
96 120 << end - time;
97 121 }
98 122 }
99 123 }
100 124 if (progress != 100) {
101 125 // We can close progression beacause all data has been retrieved
102 126 emit dataProvidedProgress(acqIdentifier, 100);
103 127 }
104 128 return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData),
105 129 Unit{QStringLiteral("t"), true}, Unit{});
106 130 }
107 131
108 132 void CosinusProvider::requestDataLoading(QUuid acqIdentifier,
109 133 const DataProviderParameters &parameters)
110 134 {
111 135 // TODO: Add Mutex
112 136 m_VariableToEnableProvider[acqIdentifier] = true;
113 137 qCDebug(LOG_CosinusProvider()) << "TORM: CosinusProvider::requestDataLoading"
114 138 << QThread::currentThread()->objectName();
115 139 // NOTE: Try to use multithread if possible
116 140 const auto times = parameters.m_Times;
117 141
118 142 for (const auto &dateTime : qAsConst(times)) {
119 143 if (m_VariableToEnableProvider[acqIdentifier]) {
120 144 auto scalarSeries = this->retrieveData(acqIdentifier, dateTime, parameters.m_Data);
121 145 qCDebug(LOG_CosinusProvider()) << "TORM: CosinusProvider::dataProvided";
122 146 emit dataProvided(acqIdentifier, scalarSeries, dateTime);
123 147 }
124 148 }
125 149 }
126 150
127 151 void CosinusProvider::requestDataAborting(QUuid acqIdentifier)
128 152 {
129 153 // TODO: Add Mutex
130 154 qCDebug(LOG_CosinusProvider()) << "CosinusProvider::requestDataAborting" << acqIdentifier
131 155 << QThread::currentThread()->objectName();
132 156 auto it = m_VariableToEnableProvider.find(acqIdentifier);
133 157 if (it != m_VariableToEnableProvider.end()) {
134 158 it.value() = false;
135 159 }
136 160 else {
137 161 qCWarning(LOG_CosinusProvider())
138 162 << tr("Aborting progression of inexistant identifier detected !!!");
139 163 }
140 164 }
General Comments 0
You need to be logged in to leave comments. Login now