@@ -0,0 +1,38 | |||
|
1 | #ifndef SCIQLOP_SORTUTILS_H | |
|
2 | #define SCIQLOP_SORTUTILS_H | |
|
3 | ||
|
4 | #include <algorithm> | |
|
5 | #include <vector> | |
|
6 | ||
|
7 | /** | |
|
8 | * Utility class with methods for sorting data | |
|
9 | */ | |
|
10 | struct SortUtils { | |
|
11 | /** | |
|
12 | * Generates a vector representing the index of insertion of each data of a container if this | |
|
13 | * one had to be sorted according to a comparison function. | |
|
14 | * | |
|
15 | * For example: | |
|
16 | * If the container is a vector {1; 4; 2; 5; 3} and the comparison function is std::less, the | |
|
17 | * result would be : {0; 3; 1; 4; 2} | |
|
18 | * | |
|
19 | * @tparam Container the type of the container. | |
|
20 | * @tparam Compare the type of the comparison function | |
|
21 | * @param container the container from which to generate the result. The container must have a | |
|
22 | * at() method that returns a value associated to an index | |
|
23 | * @param compare the comparison function | |
|
24 | */ | |
|
25 | template <typename Container, typename Compare> | |
|
26 | static std::vector<int> sortPermutation(const Container &container, const Compare &compare) | |
|
27 | { | |
|
28 | auto permutation = std::vector<int>{}; | |
|
29 | permutation.resize(container.size()); | |
|
30 | ||
|
31 | std::iota(permutation.begin(), permutation.end(), 0); | |
|
32 | std::sort(permutation.begin(), permutation.end(), | |
|
33 | [&](int i, int j) { return compare(container.at(i), container.at(j)); }); | |
|
34 | return permutation; | |
|
35 | } | |
|
36 | }; | |
|
37 | ||
|
38 | #endif // SCIQLOP_SORTUTILS_H |
@@ -0,0 +1,164 | |||
|
1 | #include "Data/DataSeries.h" | |
|
2 | #include "Data/ScalarSeries.h" | |
|
3 | ||
|
4 | #include <QObject> | |
|
5 | #include <QtTest> | |
|
6 | ||
|
7 | Q_DECLARE_METATYPE(std::shared_ptr<ScalarSeries>) | |
|
8 | ||
|
9 | class TestDataSeries : public QObject { | |
|
10 | Q_OBJECT | |
|
11 | private slots: | |
|
12 | /// Input test data | |
|
13 | /// @sa testCtor() | |
|
14 | void testCtor_data(); | |
|
15 | ||
|
16 | /// Tests construction of a data series | |
|
17 | void testCtor(); | |
|
18 | ||
|
19 | /// Input test data | |
|
20 | /// @sa testMerge() | |
|
21 | void testMerge_data(); | |
|
22 | ||
|
23 | /// Tests merge of two data series | |
|
24 | void testMerge(); | |
|
25 | }; | |
|
26 | ||
|
27 | void TestDataSeries::testCtor_data() | |
|
28 | { | |
|
29 | // ////////////// // | |
|
30 | // Test structure // | |
|
31 | // ////////////// // | |
|
32 | ||
|
33 | // x-axis data | |
|
34 | QTest::addColumn<QVector<double> >("xAxisData"); | |
|
35 | // values data | |
|
36 | QTest::addColumn<QVector<double> >("valuesData"); | |
|
37 | ||
|
38 | // expected x-axis data | |
|
39 | QTest::addColumn<QVector<double> >("expectedXAxisData"); | |
|
40 | // expected values data | |
|
41 | QTest::addColumn<QVector<double> >("expectedValuesData"); | |
|
42 | ||
|
43 | // ////////// // | |
|
44 | // Test cases // | |
|
45 | // ////////// // | |
|
46 | ||
|
47 | QTest::newRow("invalidData (different sizes of vectors)") | |
|
48 | << QVector<double>{1., 2., 3., 4., 5.} << QVector<double>{100., 200., 300.} | |
|
49 | << QVector<double>{} << QVector<double>{}; | |
|
50 | ||
|
51 | QTest::newRow("sortedData") << QVector<double>{1., 2., 3., 4., 5.} | |
|
52 | << QVector<double>{100., 200., 300., 400., 500.} | |
|
53 | << QVector<double>{1., 2., 3., 4., 5.} | |
|
54 | << QVector<double>{100., 200., 300., 400., 500.}; | |
|
55 | ||
|
56 | QTest::newRow("unsortedData") << QVector<double>{5., 4., 3., 2., 1.} | |
|
57 | << QVector<double>{100., 200., 300., 400., 500.} | |
|
58 | << QVector<double>{1., 2., 3., 4., 5.} | |
|
59 | << QVector<double>{500., 400., 300., 200., 100.}; | |
|
60 | ||
|
61 | QTest::newRow("unsortedData2") | |
|
62 | << QVector<double>{1., 4., 3., 5., 2.} << QVector<double>{100., 200., 300., 400., 500.} | |
|
63 | << QVector<double>{1., 2., 3., 4., 5.} << QVector<double>{100., 500., 300., 200., 400.}; | |
|
64 | } | |
|
65 | ||
|
66 | void TestDataSeries::testCtor() | |
|
67 | { | |
|
68 | // Creates series | |
|
69 | QFETCH(QVector<double>, xAxisData); | |
|
70 | QFETCH(QVector<double>, valuesData); | |
|
71 | ||
|
72 | auto series = std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), | |
|
73 | Unit{}, Unit{}); | |
|
74 | ||
|
75 | // Validates results : we check that the data series is sorted on its x-axis data | |
|
76 | QFETCH(QVector<double>, expectedXAxisData); | |
|
77 | QFETCH(QVector<double>, expectedValuesData); | |
|
78 | ||
|
79 | auto seriesXAxisData = series->xAxisData()->data(); | |
|
80 | auto seriesValuesData = series->valuesData()->data(); | |
|
81 | ||
|
82 | QVERIFY( | |
|
83 | std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin())); | |
|
84 | QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(), | |
|
85 | seriesValuesData.cbegin())); | |
|
86 | } | |
|
87 | ||
|
88 | namespace { | |
|
89 | ||
|
90 | std::shared_ptr<ScalarSeries> createSeries(QVector<double> xAxisData, QVector<double> valuesData) | |
|
91 | { | |
|
92 | return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), Unit{}, | |
|
93 | Unit{}); | |
|
94 | } | |
|
95 | ||
|
96 | } // namespace | |
|
97 | ||
|
98 | void TestDataSeries::testMerge_data() | |
|
99 | { | |
|
100 | // ////////////// // | |
|
101 | // Test structure // | |
|
102 | // ////////////// // | |
|
103 | ||
|
104 | // Data series to merge | |
|
105 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries"); | |
|
106 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries2"); | |
|
107 | ||
|
108 | // Expected values in the first data series after merge | |
|
109 | QTest::addColumn<QVector<double> >("expectedXAxisData"); | |
|
110 | QTest::addColumn<QVector<double> >("expectedValuesData"); | |
|
111 | ||
|
112 | // ////////// // | |
|
113 | // Test cases // | |
|
114 | // ////////// // | |
|
115 | ||
|
116 | QTest::newRow("sortedMerge") | |
|
117 | << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |
|
118 | << createSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.}) | |
|
119 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} | |
|
120 | << QVector<double>{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.}; | |
|
121 | ||
|
122 | QTest::newRow("unsortedMerge") | |
|
123 | << createSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.}) | |
|
124 | << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |
|
125 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} | |
|
126 | << QVector<double>{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.}; | |
|
127 | ||
|
128 | QTest::newRow("unsortedMerge2") | |
|
129 | << createSeries({1., 2., 8., 9., 10}, {100., 200., 300., 400., 500.}) | |
|
130 | << createSeries({3., 4., 5., 6., 7.}, {600., 700., 800., 900., 1000.}) | |
|
131 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} | |
|
132 | << QVector<double>{100., 200., 600., 700., 800., 900., 1000., 300., 400., 500.}; | |
|
133 | ||
|
134 | QTest::newRow("unsortedMerge3") | |
|
135 | << createSeries({3., 5., 8., 7., 2}, {100., 200., 300., 400., 500.}) | |
|
136 | << createSeries({6., 4., 9., 10., 1.}, {600., 700., 800., 900., 1000.}) | |
|
137 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} | |
|
138 | << QVector<double>{1000., 500., 100., 700., 200., 600., 400., 300., 800., 900.}; | |
|
139 | } | |
|
140 | ||
|
141 | void TestDataSeries::testMerge() | |
|
142 | { | |
|
143 | // Merges series | |
|
144 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries); | |
|
145 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries2); | |
|
146 | ||
|
147 | dataSeries->merge(dataSeries2.get()); | |
|
148 | ||
|
149 | // Validates results : we check that the merge is valid and the data series is sorted on its | |
|
150 | // x-axis data | |
|
151 | QFETCH(QVector<double>, expectedXAxisData); | |
|
152 | QFETCH(QVector<double>, expectedValuesData); | |
|
153 | ||
|
154 | auto seriesXAxisData = dataSeries->xAxisData()->data(); | |
|
155 | auto seriesValuesData = dataSeries->valuesData()->data(); | |
|
156 | ||
|
157 | QVERIFY( | |
|
158 | std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin())); | |
|
159 | QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(), | |
|
160 | seriesValuesData.cbegin())); | |
|
161 | } | |
|
162 | ||
|
163 | QTEST_MAIN(TestDataSeries) | |
|
164 | #include "TestDataSeries.moc" |
@@ -4,6 +4,9 | |||
|
4 | 4 | #include <QReadLocker> |
|
5 | 5 | #include <QReadWriteLock> |
|
6 | 6 | #include <QVector> |
|
7 | ||
|
8 | #include <memory> | |
|
9 | ||
|
7 | 10 | /** |
|
8 | 11 | * @brief The ArrayData class represents a dataset for a data series. |
|
9 | 12 | * |
@@ -47,6 +50,18 public: | |||
|
47 | 50 | } |
|
48 | 51 | |
|
49 | 52 | /** |
|
53 | * @return the data at a specified index | |
|
54 | * @remarks index must be a valid position | |
|
55 | * @remarks this method is only available for a unidimensional ArrayData | |
|
56 | */ | |
|
57 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
|
58 | double at(int index) const noexcept | |
|
59 | { | |
|
60 | QReadLocker locker{&m_Lock}; | |
|
61 | return m_Data[0].at(index); | |
|
62 | } | |
|
63 | ||
|
64 | /** | |
|
50 | 65 | * Sets a data at a specified index. The index has to be valid to be effective |
|
51 | 66 | * @param index the index to which the data will be set |
|
52 | 67 | * @param data the data to set |
@@ -73,24 +88,44 public: | |||
|
73 | 88 | } |
|
74 | 89 | |
|
75 | 90 | /** |
|
76 | * @return the data as a vector | |
|
91 | * @return the data as a vector, as a const reference | |
|
77 | 92 | * @remarks this method is only available for a unidimensional ArrayData |
|
78 | 93 | */ |
|
79 | 94 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
80 |
QVector<double> data( |
|
|
95 | const QVector<double> &cdata() const noexcept | |
|
81 | 96 | { |
|
82 | 97 | QReadLocker locker{&m_Lock}; |
|
83 |
return m_Data.at( |
|
|
98 | return m_Data.at(0); | |
|
84 | 99 | } |
|
85 | 100 | |
|
86 | // TODO Comment | |
|
101 | /** | |
|
102 | * Merges into the array data an other array data | |
|
103 | * @param other the array data to merge with | |
|
104 | * @param prepend if true, the other array data is inserted at the beginning, otherwise it is | |
|
105 | * inserted at the end | |
|
106 | * @remarks this method is only available for a unidimensional ArrayData | |
|
107 | */ | |
|
87 | 108 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
88 |
void |
|
|
109 | void add(const ArrayData<1> &other, bool prepend = false) | |
|
89 | 110 | { |
|
90 | 111 | QWriteLocker locker{&m_Lock}; |
|
91 | 112 | if (!m_Data.empty()) { |
|
92 |
QReadLocker otherLocker{& |
|
|
93 | m_Data[0] += arrayData.data(); | |
|
113 | QReadLocker otherLocker{&other.m_Lock}; | |
|
114 | ||
|
115 | if (prepend) { | |
|
116 | const auto &otherData = other.data(); | |
|
117 | const auto otherDataSize = otherData.size(); | |
|
118 | ||
|
119 | auto &data = m_Data[0]; | |
|
120 | data.insert(data.begin(), otherDataSize, 0.); | |
|
121 | ||
|
122 | for (auto i = 0; i < otherDataSize; ++i) { | |
|
123 | data.replace(i, otherData.at(i)); | |
|
124 | } | |
|
125 | } | |
|
126 | else { | |
|
127 | m_Data[0] += other.data(); | |
|
128 | } | |
|
94 | 129 | } |
|
95 | 130 | } |
|
96 | 131 | |
@@ -101,10 +136,28 public: | |||
|
101 | 136 | return m_Data[0].size(); |
|
102 | 137 | } |
|
103 | 138 | |
|
139 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
|
140 | std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> sortPermutation) | |
|
141 | { | |
|
142 | QReadLocker locker{&m_Lock}; | |
|
143 | ||
|
144 | const auto &data = m_Data.at(0); | |
|
145 | ||
|
146 | // Inits result | |
|
147 | auto sortedData = QVector<double>{}; | |
|
148 | sortedData.resize(data.size()); | |
|
149 | ||
|
150 | std::transform(sortPermutation.cbegin(), sortPermutation.cend(), sortedData.begin(), | |
|
151 | [&data](int i) { return data[i]; }); | |
|
152 | ||
|
153 | return std::make_shared<ArrayData<Dim> >(std::move(sortedData)); | |
|
154 | } | |
|
155 | ||
|
156 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
|
104 | 157 | void clear() |
|
105 | 158 | { |
|
106 | 159 | QWriteLocker locker{&m_Lock}; |
|
107 | m_Data.clear(); | |
|
160 | m_Data[0].clear(); | |
|
108 | 161 | } |
|
109 | 162 | |
|
110 | 163 |
@@ -1,6 +1,8 | |||
|
1 | 1 | #ifndef SCIQLOP_DATASERIES_H |
|
2 | 2 | #define SCIQLOP_DATASERIES_H |
|
3 | 3 | |
|
4 | #include <Common/SortUtils.h> | |
|
5 | ||
|
4 | 6 | #include <Data/ArrayData.h> |
|
5 | 7 | #include <Data/IDataSeries.h> |
|
6 | 8 | |
@@ -17,7 +19,9 Q_LOGGING_CATEGORY(LOG_DataSeries, "DataSeries") | |||
|
17 | 19 | /** |
|
18 | 20 | * @brief The DataSeries class is the base (abstract) implementation of IDataSeries. |
|
19 | 21 | * |
|
20 | * It proposes to set a dimension for the values ββdata | |
|
22 | * It proposes to set a dimension for the values ββdata. | |
|
23 | * | |
|
24 | * A DataSeries is always sorted on its x-axis data. | |
|
21 | 25 | * |
|
22 | 26 | * @tparam Dim The dimension of the values data |
|
23 | 27 | * |
@@ -45,18 +49,64 public: | |||
|
45 | 49 | m_ValuesData->clear(); |
|
46 | 50 | } |
|
47 | 51 | |
|
48 | /// @sa IDataSeries::merge() | |
|
52 | /// Merges into the data series an other data series | |
|
53 | /// @remarks the data series to merge with is cleared after the operation | |
|
49 | 54 | void merge(IDataSeries *dataSeries) override |
|
50 | 55 | { |
|
51 | if (auto dimDataSeries = dynamic_cast<DataSeries<Dim> *>(dataSeries)) { | |
|
52 | m_XAxisData->merge(*dimDataSeries->xAxisData()); | |
|
53 | m_ValuesData->merge(*dimDataSeries->valuesData()); | |
|
54 | dimDataSeries->clear(); | |
|
56 | dataSeries->lockWrite(); | |
|
57 | lockWrite(); | |
|
58 | ||
|
59 | if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) { | |
|
60 | const auto &otherXAxisData = other->xAxisData()->cdata(); | |
|
61 | const auto &xAxisData = m_XAxisData->cdata(); | |
|
62 | ||
|
63 | // As data series are sorted, we can improve performances of merge, by call the sort | |
|
64 | // method only if the two data series overlap. | |
|
65 | if (!otherXAxisData.empty()) { | |
|
66 | auto firstValue = otherXAxisData.front(); | |
|
67 | auto lastValue = otherXAxisData.back(); | |
|
68 | ||
|
69 | auto xAxisDataBegin = xAxisData.cbegin(); | |
|
70 | auto xAxisDataEnd = xAxisData.cend(); | |
|
71 | ||
|
72 | bool prepend; | |
|
73 | bool sortNeeded; | |
|
74 | ||
|
75 | if (std::lower_bound(xAxisDataBegin, xAxisDataEnd, firstValue) == xAxisDataEnd) { | |
|
76 | // Other data series if after data series | |
|
77 | prepend = false; | |
|
78 | sortNeeded = false; | |
|
79 | } | |
|
80 | else if (std::upper_bound(xAxisDataBegin, xAxisDataEnd, lastValue) | |
|
81 | == xAxisDataBegin) { | |
|
82 | // Other data series if before data series | |
|
83 | prepend = true; | |
|
84 | sortNeeded = false; | |
|
85 | } | |
|
86 | else { | |
|
87 | // The two data series overlap | |
|
88 | prepend = false; | |
|
89 | sortNeeded = true; | |
|
90 | } | |
|
91 | ||
|
92 | // Makes the merge | |
|
93 | m_XAxisData->add(*other->xAxisData(), prepend); | |
|
94 | m_ValuesData->add(*other->valuesData(), prepend); | |
|
95 | ||
|
96 | if (sortNeeded) { | |
|
97 | sort(); | |
|
98 | } | |
|
99 | } | |
|
100 | ||
|
101 | // Clears the other data series | |
|
102 | other->clear(); | |
|
55 | 103 | } |
|
56 | 104 | else { |
|
57 | 105 | qCWarning(LOG_DataSeries()) |
|
58 | << QObject::tr("Dection of a type of IDataSeries we cannot merge with !"); | |
|
106 | << QObject::tr("Detection of a type of IDataSeries we cannot merge with !"); | |
|
59 | 107 | } |
|
108 | unlock(); | |
|
109 | dataSeries->unlock(); | |
|
60 | 110 | } |
|
61 | 111 | |
|
62 | 112 | virtual void lockRead() { m_Lock.lockForRead(); } |
@@ -64,7 +114,9 public: | |||
|
64 | 114 | virtual void unlock() { m_Lock.unlock(); } |
|
65 | 115 | |
|
66 | 116 | protected: |
|
67 | /// Protected ctor (DataSeries is abstract) | |
|
117 | /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a | |
|
118 | /// DataSeries with no values will be created. | |
|
119 | /// @remarks data series is automatically sorted on its x-axis data | |
|
68 | 120 | explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit, |
|
69 | 121 | std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit) |
|
70 | 122 | : m_XAxisData{xAxisData}, |
@@ -72,6 +124,15 protected: | |||
|
72 | 124 | m_ValuesData{valuesData}, |
|
73 | 125 | m_ValuesUnit{valuesUnit} |
|
74 | 126 | { |
|
127 | if (m_XAxisData->size() != m_ValuesData->size()) { | |
|
128 | clear(); | |
|
129 | } | |
|
130 | ||
|
131 | // Sorts data if it's not the case | |
|
132 | const auto &xAxisCData = m_XAxisData->cdata(); | |
|
133 | if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) { | |
|
134 | sort(); | |
|
135 | } | |
|
75 | 136 | } |
|
76 | 137 | |
|
77 | 138 | /// Copy ctor |
@@ -81,6 +142,8 protected: | |||
|
81 | 142 | m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)}, |
|
82 | 143 | m_ValuesUnit{other.m_ValuesUnit} |
|
83 | 144 | { |
|
145 | // Since a series is ordered from its construction and is always ordered, it is not | |
|
146 | // necessary to call the sort method here ('other' is sorted) | |
|
84 | 147 | } |
|
85 | 148 | |
|
86 | 149 | /// Assignment operator |
@@ -96,6 +159,16 protected: | |||
|
96 | 159 | } |
|
97 | 160 | |
|
98 | 161 | private: |
|
162 | /** | |
|
163 | * Sorts data series on its x-axis data | |
|
164 | */ | |
|
165 | void sort() noexcept | |
|
166 | { | |
|
167 | auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>()); | |
|
168 | m_XAxisData = m_XAxisData->sort(permutation); | |
|
169 | m_ValuesData = m_ValuesData->sort(permutation); | |
|
170 | } | |
|
171 | ||
|
99 | 172 | std::shared_ptr<ArrayData<1> > m_XAxisData; |
|
100 | 173 | Unit m_XAxisUnit; |
|
101 | 174 | std::shared_ptr<ArrayData<Dim> > m_ValuesData; |
@@ -9,14 +9,6 | |||
|
9 | 9 | class ScalarSeries : public DataSeries<1> { |
|
10 | 10 | public: |
|
11 | 11 | /** |
|
12 | * Ctor | |
|
13 | * @param size the number of data the series will hold | |
|
14 | * @param xAxisUnit x-axis unit | |
|
15 | * @param valuesUnit values unit | |
|
16 | */ | |
|
17 | explicit ScalarSeries(int size, const Unit &xAxisUnit, const Unit &valuesUnit); | |
|
18 | ||
|
19 | /** | |
|
20 | 12 | * Ctor with two vectors. The vectors must have the same size, otherwise a ScalarSeries with no |
|
21 | 13 | * values will be created. |
|
22 | 14 | * @param xAxisData x-axis data |
@@ -25,14 +17,6 public: | |||
|
25 | 17 | explicit ScalarSeries(QVector<double> xAxisData, QVector<double> valuesData, |
|
26 | 18 | const Unit &xAxisUnit, const Unit &valuesUnit); |
|
27 | 19 | |
|
28 | /** | |
|
29 | * Sets data for a specific index. The index has to be valid to be effective | |
|
30 | * @param index the index to which the data will be set | |
|
31 | * @param x the x-axis data | |
|
32 | * @param value the value data | |
|
33 | */ | |
|
34 | void setData(int index, double x, double value) noexcept; | |
|
35 | ||
|
36 | 20 | std::unique_ptr<IDataSeries> clone() const; |
|
37 | 21 | }; |
|
38 | 22 |
@@ -1,11 +1,5 | |||
|
1 | 1 | #include <Data/ScalarSeries.h> |
|
2 | 2 | |
|
3 | ScalarSeries::ScalarSeries(int size, const Unit &xAxisUnit, const Unit &valuesUnit) | |
|
4 | : DataSeries{std::make_shared<ArrayData<1> >(size), xAxisUnit, | |
|
5 | std::make_shared<ArrayData<1> >(size), valuesUnit} | |
|
6 | { | |
|
7 | } | |
|
8 | ||
|
9 | 3 | ScalarSeries::ScalarSeries(QVector<double> xAxisData, QVector<double> valuesData, |
|
10 | 4 | const Unit &xAxisUnit, const Unit &valuesUnit) |
|
11 | 5 | : DataSeries{std::make_shared<ArrayData<1> >(std::move(xAxisData)), xAxisUnit, |
@@ -13,12 +7,6 ScalarSeries::ScalarSeries(QVector<double> xAxisData, QVector<double> valuesData | |||
|
13 | 7 | { |
|
14 | 8 | } |
|
15 | 9 | |
|
16 | void ScalarSeries::setData(int index, double x, double value) noexcept | |
|
17 | { | |
|
18 | xAxisData()->setData(index, x); | |
|
19 | valuesData()->setData(index, value); | |
|
20 | } | |
|
21 | ||
|
22 | 10 | std::unique_ptr<IDataSeries> ScalarSeries::clone() const |
|
23 | 11 | { |
|
24 | 12 | return std::make_unique<ScalarSeries>(*this); |
@@ -55,11 +55,7 void Variable::setDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept | |||
|
55 | 55 | impl->m_DataSeries = dataSeries->clone(); |
|
56 | 56 | } |
|
57 | 57 | else { |
|
58 | dataSeries->lockWrite(); | |
|
59 | impl->m_DataSeries->lockWrite(); | |
|
60 | 58 | impl->m_DataSeries->merge(dataSeries.get()); |
|
61 | impl->m_DataSeries->unlock(); | |
|
62 | dataSeries->unlock(); | |
|
63 | 59 | // emit updated(); |
|
64 | 60 | } |
|
65 | 61 | } |
@@ -11,7 +11,7 namespace { | |||
|
11 | 11 | |
|
12 | 12 | class SqpDataContainer : public QCPGraphDataContainer { |
|
13 | 13 | public: |
|
14 |
void appendGraphData |
|
|
14 | void appendGraphData(const QCPGraphData &data) { mData.append(data); } | |
|
15 | 15 | }; |
|
16 | 16 | |
|
17 | 17 | |
@@ -40,30 +40,32 void updateScalarData(QCPAbstractPlottable *component, ScalarSeries &scalarSerie | |||
|
40 | 40 | qCDebug(LOG_VisualizationGraphHelper()) << "TORM: updateScalarData" |
|
41 | 41 | << QThread::currentThread()->objectName(); |
|
42 | 42 | if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) { |
|
43 | // Clean the graph | |
|
44 | // NAIVE approch | |
|
45 | 43 | scalarSeries.lockRead(); |
|
46 | 44 | { |
|
47 | const auto xData = scalarSeries.xAxisData()->data(); | |
|
48 | const auto valuesData = scalarSeries.valuesData()->data(); | |
|
49 | const auto count = xData.count(); | |
|
50 | qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points in cache" | |
|
51 | << xData.count(); | |
|
52 | ||
|
53 | auto dataContainer = qcpGraph->data(); | |
|
54 | dataContainer->clear(); | |
|
45 | const auto &xData = scalarSeries.xAxisData()->cdata(); | |
|
46 | const auto &valuesData = scalarSeries.valuesData()->cdata(); | |
|
47 | ||
|
48 | auto xDataBegin = xData.cbegin(); | |
|
49 | auto xDataEnd = xData.cend(); | |
|
50 | ||
|
51 | qCInfo(LOG_VisualizationGraphHelper()) | |
|
52 | << "TORM: Current points in cache" << xData.count(); | |
|
53 | ||
|
55 | 54 | auto sqpDataContainer = QSharedPointer<SqpDataContainer>::create(); |
|
56 | 55 | qcpGraph->setData(sqpDataContainer); |
|
57 | 56 | |
|
58 | for (auto i = 0; i < count; ++i) { | |
|
59 | const auto x = xData[i]; | |
|
60 | if (x >= dateTime.m_TStart && x <= dateTime.m_TEnd) { | |
|
61 | sqpDataContainer->appendGraphDataUnsorted(QCPGraphData(x, valuesData[i])); | |
|
62 | } | |
|
57 | auto lowerIt = std::lower_bound(xDataBegin, xDataEnd, dateTime.m_TStart); | |
|
58 | auto upperIt = std::upper_bound(xDataBegin, xDataEnd, dateTime.m_TEnd); | |
|
59 | auto distance = std::distance(xDataBegin, lowerIt); | |
|
60 | ||
|
61 | auto valuesDataIt = valuesData.cbegin() + distance; | |
|
62 | for (auto xAxisDataIt = lowerIt; xAxisDataIt != upperIt; | |
|
63 | ++xAxisDataIt, ++valuesDataIt) { | |
|
64 | sqpDataContainer->appendGraphData(QCPGraphData(*xAxisDataIt, *valuesDataIt)); | |
|
63 | 65 | } |
|
64 | sqpDataContainer->sort(); | |
|
65 |
qCInfo(LOG_VisualizationGraphHelper()) |
|
|
66 |
|
|
|
66 | ||
|
67 | qCInfo(LOG_VisualizationGraphHelper()) | |
|
68 | << "TORM: Current points displayed" << sqpDataContainer->size(); | |
|
67 | 69 | } |
|
68 | 70 | scalarSeries.unlock(); |
|
69 | 71 |
@@ -19,8 +19,8 std::shared_ptr<IDataSeries> CosinusProvider::retrieveData(QUuid token, const Sq | |||
|
19 | 19 | |
|
20 | 20 | // Gets the timerange from the parameters |
|
21 | 21 | double freq = 100.0; |
|
22 | double start = dateTime.m_TStart * freq; // 100 htz | |
|
23 |
double end = dateTime.m_TEnd * freq; |
|
|
22 | double start = std::ceil(dateTime.m_TStart * freq); // 100 htz | |
|
23 | double end = std::floor(dateTime.m_TEnd * freq); // 100 htz | |
|
24 | 24 | |
|
25 | 25 | // We assure that timerange is valid |
|
26 | 26 | if (end < start) { |
@@ -28,17 +28,23 std::shared_ptr<IDataSeries> CosinusProvider::retrieveData(QUuid token, const Sq | |||
|
28 | 28 | } |
|
29 | 29 | |
|
30 | 30 | // Generates scalar series containing cosinus values (one value per second) |
|
31 | auto scalarSeries | |
|
32 | = std::make_shared<ScalarSeries>(end - start, Unit{QStringLiteral("t"), true}, Unit{}); | |
|
31 | auto dataCount = end - start; | |
|
33 | 32 | |
|
33 | auto xAxisData = QVector<double>{}; | |
|
34 | xAxisData.resize(dataCount); | |
|
35 | ||
|
36 | auto valuesData = QVector<double>{}; | |
|
37 | valuesData.resize(dataCount); | |
|
34 | 38 | |
|
35 | 39 | int progress = 0; |
|
36 |
auto progressEnd = |
|
|
40 | auto progressEnd = dataCount; | |
|
37 | 41 | for (auto time = start; time < end; ++time, ++dataIndex) { |
|
38 | 42 | auto it = m_VariableToEnableProvider.find(token); |
|
39 | 43 | if (it != m_VariableToEnableProvider.end() && it.value()) { |
|
40 | 44 | const auto timeOnFreq = time / freq; |
|
41 | scalarSeries->setData(dataIndex, timeOnFreq, std::cos(timeOnFreq)); | |
|
45 | ||
|
46 | xAxisData.replace(dataIndex, timeOnFreq); | |
|
47 | valuesData.replace(dataIndex, std::cos(timeOnFreq)); | |
|
42 | 48 | |
|
43 | 49 | // progression |
|
44 | 50 | int currentProgress = (time - start) * 100.0 / progressEnd; |
@@ -58,8 +64,8 std::shared_ptr<IDataSeries> CosinusProvider::retrieveData(QUuid token, const Sq | |||
|
58 | 64 | } |
|
59 | 65 | emit dataProvidedProgress(token, 0.0); |
|
60 | 66 | |
|
61 | ||
|
62 | return scalarSeries; | |
|
67 | return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), | |
|
68 | Unit{QStringLiteral("t"), true}, Unit{}); | |
|
63 | 69 | } |
|
64 | 70 | |
|
65 | 71 | void CosinusProvider::requestDataLoading(QUuid token, const DataProviderParameters ¶meters) |
General Comments 0
You need to be logged in to leave comments.
Login now