##// END OF EJS Templates
Merge branch 'feature/SortDataSeries' into develop
Alexandre Leroux -
r422:e7ac423673d6 merge
parent child
Show More
@@ -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 #include <QReadLocker>
4 #include <QReadLocker>
5 #include <QReadWriteLock>
5 #include <QReadWriteLock>
6 #include <QVector>
6 #include <QVector>
7
8 #include <memory>
9
7 /**
10 /**
8 * @brief The ArrayData class represents a dataset for a data series.
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 * Sets a data at a specified index. The index has to be valid to be effective
65 * Sets a data at a specified index. The index has to be valid to be effective
51 * @param index the index to which the data will be set
66 * @param index the index to which the data will be set
52 * @param data the data to set
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 * @remarks this method is only available for a unidimensional ArrayData
92 * @remarks this method is only available for a unidimensional ArrayData
78 */
93 */
79 template <int D = Dim, typename = std::enable_if_t<D == 1> >
94 template <int D = Dim, typename = std::enable_if_t<D == 1> >
80 QVector<double> data(double tStart, double tEnd) const noexcept
95 const QVector<double> &cdata() const noexcept
81 {
96 {
82 QReadLocker locker{&m_Lock};
97 QReadLocker locker{&m_Lock};
83 return m_Data.at(tStart);
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 template <int D = Dim, typename = std::enable_if_t<D == 1> >
108 template <int D = Dim, typename = std::enable_if_t<D == 1> >
88 void merge(const ArrayData<1> &arrayData)
109 void add(const ArrayData<1> &other, bool prepend = false)
89 {
110 {
90 QWriteLocker locker{&m_Lock};
111 QWriteLocker locker{&m_Lock};
91 if (!m_Data.empty()) {
112 if (!m_Data.empty()) {
92 QReadLocker otherLocker{&arrayData.m_Lock};
113 QReadLocker otherLocker{&other.m_Lock};
93 m_Data[0] += arrayData.data();
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 return m_Data[0].size();
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 void clear()
157 void clear()
105 {
158 {
106 QWriteLocker locker{&m_Lock};
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 #ifndef SCIQLOP_DATASERIES_H
1 #ifndef SCIQLOP_DATASERIES_H
2 #define SCIQLOP_DATASERIES_H
2 #define SCIQLOP_DATASERIES_H
3
3
4 #include <Common/SortUtils.h>
5
4 #include <Data/ArrayData.h>
6 #include <Data/ArrayData.h>
5 #include <Data/IDataSeries.h>
7 #include <Data/IDataSeries.h>
6
8
@@ -17,7 +19,9 Q_LOGGING_CATEGORY(LOG_DataSeries, "DataSeries")
17 /**
19 /**
18 * @brief The DataSeries class is the base (abstract) implementation of IDataSeries.
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 * @tparam Dim The dimension of the values data
26 * @tparam Dim The dimension of the values data
23 *
27 *
@@ -45,18 +49,64 public:
45 m_ValuesData->clear();
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 void merge(IDataSeries *dataSeries) override
54 void merge(IDataSeries *dataSeries) override
50 {
55 {
51 if (auto dimDataSeries = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
56 dataSeries->lockWrite();
52 m_XAxisData->merge(*dimDataSeries->xAxisData());
57 lockWrite();
53 m_ValuesData->merge(*dimDataSeries->valuesData());
58
54 dimDataSeries->clear();
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 else {
104 else {
57 qCWarning(LOG_DataSeries())
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 virtual void lockRead() { m_Lock.lockForRead(); }
112 virtual void lockRead() { m_Lock.lockForRead(); }
@@ -64,7 +114,9 public:
64 virtual void unlock() { m_Lock.unlock(); }
114 virtual void unlock() { m_Lock.unlock(); }
65
115
66 protected:
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 explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit,
120 explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit,
69 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit)
121 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit)
70 : m_XAxisData{xAxisData},
122 : m_XAxisData{xAxisData},
@@ -72,6 +124,15 protected:
72 m_ValuesData{valuesData},
124 m_ValuesData{valuesData},
73 m_ValuesUnit{valuesUnit}
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 /// Copy ctor
138 /// Copy ctor
@@ -81,6 +142,8 protected:
81 m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)},
142 m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)},
82 m_ValuesUnit{other.m_ValuesUnit}
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 /// Assignment operator
149 /// Assignment operator
@@ -96,6 +159,16 protected:
96 }
159 }
97
160
98 private:
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 std::shared_ptr<ArrayData<1> > m_XAxisData;
172 std::shared_ptr<ArrayData<1> > m_XAxisData;
100 Unit m_XAxisUnit;
173 Unit m_XAxisUnit;
101 std::shared_ptr<ArrayData<Dim> > m_ValuesData;
174 std::shared_ptr<ArrayData<Dim> > m_ValuesData;
@@ -9,14 +9,6
9 class ScalarSeries : public DataSeries<1> {
9 class ScalarSeries : public DataSeries<1> {
10 public:
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 * Ctor with two vectors. The vectors must have the same size, otherwise a ScalarSeries with no
12 * Ctor with two vectors. The vectors must have the same size, otherwise a ScalarSeries with no
21 * values will be created.
13 * values will be created.
22 * @param xAxisData x-axis data
14 * @param xAxisData x-axis data
@@ -25,14 +17,6 public:
25 explicit ScalarSeries(QVector<double> xAxisData, QVector<double> valuesData,
17 explicit ScalarSeries(QVector<double> xAxisData, QVector<double> valuesData,
26 const Unit &xAxisUnit, const Unit &valuesUnit);
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 std::unique_ptr<IDataSeries> clone() const;
20 std::unique_ptr<IDataSeries> clone() const;
37 };
21 };
38
22
@@ -1,11 +1,5
1 #include <Data/ScalarSeries.h>
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 ScalarSeries::ScalarSeries(QVector<double> xAxisData, QVector<double> valuesData,
3 ScalarSeries::ScalarSeries(QVector<double> xAxisData, QVector<double> valuesData,
10 const Unit &xAxisUnit, const Unit &valuesUnit)
4 const Unit &xAxisUnit, const Unit &valuesUnit)
11 : DataSeries{std::make_shared<ArrayData<1> >(std::move(xAxisData)), xAxisUnit,
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 std::unique_ptr<IDataSeries> ScalarSeries::clone() const
10 std::unique_ptr<IDataSeries> ScalarSeries::clone() const
23 {
11 {
24 return std::make_unique<ScalarSeries>(*this);
12 return std::make_unique<ScalarSeries>(*this);
@@ -55,11 +55,7 void Variable::setDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept
55 impl->m_DataSeries = dataSeries->clone();
55 impl->m_DataSeries = dataSeries->clone();
56 }
56 }
57 else {
57 else {
58 dataSeries->lockWrite();
59 impl->m_DataSeries->lockWrite();
60 impl->m_DataSeries->merge(dataSeries.get());
58 impl->m_DataSeries->merge(dataSeries.get());
61 impl->m_DataSeries->unlock();
62 dataSeries->unlock();
63 // emit updated();
59 // emit updated();
64 }
60 }
65 }
61 }
@@ -11,7 +11,7 namespace {
11
11
12 class SqpDataContainer : public QCPGraphDataContainer {
12 class SqpDataContainer : public QCPGraphDataContainer {
13 public:
13 public:
14 void appendGraphDataUnsorted(const QCPGraphData &data) { mData.append(data); }
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 qCDebug(LOG_VisualizationGraphHelper()) << "TORM: updateScalarData"
40 qCDebug(LOG_VisualizationGraphHelper()) << "TORM: updateScalarData"
41 << QThread::currentThread()->objectName();
41 << QThread::currentThread()->objectName();
42 if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) {
42 if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) {
43 // Clean the graph
44 // NAIVE approch
45 scalarSeries.lockRead();
43 scalarSeries.lockRead();
46 {
44 {
47 const auto xData = scalarSeries.xAxisData()->data();
45 const auto &xData = scalarSeries.xAxisData()->cdata();
48 const auto valuesData = scalarSeries.valuesData()->data();
46 const auto &valuesData = scalarSeries.valuesData()->cdata();
49 const auto count = xData.count();
47
50 qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points in cache"
48 auto xDataBegin = xData.cbegin();
51 << xData.count();
49 auto xDataEnd = xData.cend();
52
50
53 auto dataContainer = qcpGraph->data();
51 qCInfo(LOG_VisualizationGraphHelper())
54 dataContainer->clear();
52 << "TORM: Current points in cache" << xData.count();
53
55 auto sqpDataContainer = QSharedPointer<SqpDataContainer>::create();
54 auto sqpDataContainer = QSharedPointer<SqpDataContainer>::create();
56 qcpGraph->setData(sqpDataContainer);
55 qcpGraph->setData(sqpDataContainer);
57
56
58 for (auto i = 0; i < count; ++i) {
57 auto lowerIt = std::lower_bound(xDataBegin, xDataEnd, dateTime.m_TStart);
59 const auto x = xData[i];
58 auto upperIt = std::upper_bound(xDataBegin, xDataEnd, dateTime.m_TEnd);
60 if (x >= dateTime.m_TStart && x <= dateTime.m_TEnd) {
59 auto distance = std::distance(xDataBegin, lowerIt);
61 sqpDataContainer->appendGraphDataUnsorted(QCPGraphData(x, valuesData[i]));
60
62 }
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();
66
65 qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points displayed"
67 qCInfo(LOG_VisualizationGraphHelper())
66 << sqpDataContainer->size();
68 << "TORM: Current points displayed" << sqpDataContainer->size();
67 }
69 }
68 scalarSeries.unlock();
70 scalarSeries.unlock();
69
71
@@ -19,8 +19,8 std::shared_ptr<IDataSeries> CosinusProvider::retrieveData(QUuid token, const Sq
19
19
20 // Gets the timerange from the parameters
20 // Gets the timerange from the parameters
21 double freq = 100.0;
21 double freq = 100.0;
22 double start = dateTime.m_TStart * freq; // 100 htz
22 double start = std::ceil(dateTime.m_TStart * freq); // 100 htz
23 double end = dateTime.m_TEnd * freq; // 100 htz
23 double end = std::floor(dateTime.m_TEnd * freq); // 100 htz
24
24
25 // We assure that timerange is valid
25 // We assure that timerange is valid
26 if (end < start) {
26 if (end < start) {
@@ -28,17 +28,23 std::shared_ptr<IDataSeries> CosinusProvider::retrieveData(QUuid token, const Sq
28 }
28 }
29
29
30 // Generates scalar series containing cosinus values (one value per second)
30 // Generates scalar series containing cosinus values (one value per second)
31 auto scalarSeries
31 auto dataCount = end - start;
32 = std::make_shared<ScalarSeries>(end - start, Unit{QStringLiteral("t"), true}, Unit{});
33
32
33 auto xAxisData = QVector<double>{};
34 xAxisData.resize(dataCount);
35
36 auto valuesData = QVector<double>{};
37 valuesData.resize(dataCount);
34
38
35 int progress = 0;
39 int progress = 0;
36 auto progressEnd = end - start;
40 auto progressEnd = dataCount;
37 for (auto time = start; time < end; ++time, ++dataIndex) {
41 for (auto time = start; time < end; ++time, ++dataIndex) {
38 auto it = m_VariableToEnableProvider.find(token);
42 auto it = m_VariableToEnableProvider.find(token);
39 if (it != m_VariableToEnableProvider.end() && it.value()) {
43 if (it != m_VariableToEnableProvider.end() && it.value()) {
40 const auto timeOnFreq = time / freq;
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 // progression
49 // progression
44 int currentProgress = (time - start) * 100.0 / progressEnd;
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 emit dataProvidedProgress(token, 0.0);
65 emit dataProvidedProgress(token, 0.0);
60
66
61
67 return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData),
62 return scalarSeries;
68 Unit{QStringLiteral("t"), true}, Unit{});
63 }
69 }
64
70
65 void CosinusProvider::requestDataLoading(QUuid token, const DataProviderParameters &parameters)
71 void CosinusProvider::requestDataLoading(QUuid token, const DataProviderParameters &parameters)
General Comments 0
You need to be logged in to leave comments. Login now