##// END OF EJS Templates
(Refactoring) Renames IDataSeries::subData()
Alexandre Leroux -
r605:06b66012b93b
parent child
Show More
@@ -1,306 +1,305
1 #ifndef SCIQLOP_DATASERIES_H
1 #ifndef SCIQLOP_DATASERIES_H
2 #define SCIQLOP_DATASERIES_H
2 #define SCIQLOP_DATASERIES_H
3
3
4 #include "CoreGlobal.h"
4 #include "CoreGlobal.h"
5
5
6 #include <Common/SortUtils.h>
6 #include <Common/SortUtils.h>
7
7
8 #include <Data/ArrayData.h>
8 #include <Data/ArrayData.h>
9 #include <Data/IDataSeries.h>
9 #include <Data/IDataSeries.h>
10
10
11 #include <QLoggingCategory>
11 #include <QLoggingCategory>
12 #include <QReadLocker>
12 #include <QReadLocker>
13 #include <QReadWriteLock>
13 #include <QReadWriteLock>
14 #include <memory>
14 #include <memory>
15
15
16 // We don't use the Qt macro since the log is used in the header file, which causes multiple log
16 // We don't use the Qt macro since the log is used in the header file, which causes multiple log
17 // definitions with inheritance. Inline method is used instead
17 // definitions with inheritance. Inline method is used instead
18 inline const QLoggingCategory &LOG_DataSeries()
18 inline const QLoggingCategory &LOG_DataSeries()
19 {
19 {
20 static const QLoggingCategory category{"DataSeries"};
20 static const QLoggingCategory category{"DataSeries"};
21 return category;
21 return category;
22 }
22 }
23
23
24 template <int Dim>
24 template <int Dim>
25 class DataSeries;
25 class DataSeries;
26
26
27 namespace dataseries_detail {
27 namespace dataseries_detail {
28
28
29 template <int Dim>
29 template <int Dim>
30 class IteratorValue : public DataSeriesIteratorValue::Impl {
30 class IteratorValue : public DataSeriesIteratorValue::Impl {
31 public:
31 public:
32 explicit IteratorValue(const DataSeries<Dim> &dataSeries, bool begin)
32 explicit IteratorValue(const DataSeries<Dim> &dataSeries, bool begin)
33 : m_XIt(begin ? dataSeries.xAxisData()->cbegin() : dataSeries.xAxisData()->cend()),
33 : m_XIt(begin ? dataSeries.xAxisData()->cbegin() : dataSeries.xAxisData()->cend()),
34 m_ValuesIt(begin ? dataSeries.valuesData()->cbegin()
34 m_ValuesIt(begin ? dataSeries.valuesData()->cbegin()
35 : dataSeries.valuesData()->cend())
35 : dataSeries.valuesData()->cend())
36 {
36 {
37 }
37 }
38 IteratorValue(const IteratorValue &other) = default;
38 IteratorValue(const IteratorValue &other) = default;
39
39
40 std::unique_ptr<DataSeriesIteratorValue::Impl> clone() const override
40 std::unique_ptr<DataSeriesIteratorValue::Impl> clone() const override
41 {
41 {
42 return std::make_unique<IteratorValue<Dim> >(*this);
42 return std::make_unique<IteratorValue<Dim> >(*this);
43 }
43 }
44
44
45 bool equals(const DataSeriesIteratorValue::Impl &other) const override try {
45 bool equals(const DataSeriesIteratorValue::Impl &other) const override try {
46 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
46 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
47 return std::tie(m_XIt, m_ValuesIt) == std::tie(otherImpl.m_XIt, otherImpl.m_ValuesIt);
47 return std::tie(m_XIt, m_ValuesIt) == std::tie(otherImpl.m_XIt, otherImpl.m_ValuesIt);
48 }
48 }
49 catch (const std::bad_cast &) {
49 catch (const std::bad_cast &) {
50 return false;
50 return false;
51 }
51 }
52
52
53 void next() override
53 void next() override
54 {
54 {
55 ++m_XIt;
55 ++m_XIt;
56 ++m_ValuesIt;
56 ++m_ValuesIt;
57 }
57 }
58
58
59 void prev() override
59 void prev() override
60 {
60 {
61 --m_XIt;
61 --m_XIt;
62 --m_ValuesIt;
62 --m_ValuesIt;
63 }
63 }
64
64
65 double x() const override { return m_XIt->at(0); }
65 double x() const override { return m_XIt->at(0); }
66 double value() const override { return m_ValuesIt->at(0); }
66 double value() const override { return m_ValuesIt->at(0); }
67 double value(int componentIndex) const override { return m_ValuesIt->at(componentIndex); }
67 double value(int componentIndex) const override { return m_ValuesIt->at(componentIndex); }
68
68
69 private:
69 private:
70 ArrayData<1>::Iterator m_XIt;
70 ArrayData<1>::Iterator m_XIt;
71 typename ArrayData<Dim>::Iterator m_ValuesIt;
71 typename ArrayData<Dim>::Iterator m_ValuesIt;
72 };
72 };
73 } // namespace dataseries_detail
73 } // namespace dataseries_detail
74
74
75 /**
75 /**
76 * @brief The DataSeries class is the base (abstract) implementation of IDataSeries.
76 * @brief The DataSeries class is the base (abstract) implementation of IDataSeries.
77 *
77 *
78 * It proposes to set a dimension for the values ​​data.
78 * It proposes to set a dimension for the values ​​data.
79 *
79 *
80 * A DataSeries is always sorted on its x-axis data.
80 * A DataSeries is always sorted on its x-axis data.
81 *
81 *
82 * @tparam Dim The dimension of the values data
82 * @tparam Dim The dimension of the values data
83 *
83 *
84 */
84 */
85 template <int Dim>
85 template <int Dim>
86 class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries {
86 class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries {
87 public:
87 public:
88 /// @sa IDataSeries::xAxisData()
88 /// @sa IDataSeries::xAxisData()
89 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
89 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
90 const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; }
90 const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; }
91
91
92 /// @sa IDataSeries::xAxisUnit()
92 /// @sa IDataSeries::xAxisUnit()
93 Unit xAxisUnit() const override { return m_XAxisUnit; }
93 Unit xAxisUnit() const override { return m_XAxisUnit; }
94
94
95 /// @return the values dataset
95 /// @return the values dataset
96 std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; }
96 std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; }
97 const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; }
97 const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; }
98
98
99 /// @sa IDataSeries::valuesUnit()
99 /// @sa IDataSeries::valuesUnit()
100 Unit valuesUnit() const override { return m_ValuesUnit; }
100 Unit valuesUnit() const override { return m_ValuesUnit; }
101
101
102
102
103 SqpRange range() const override
103 SqpRange range() const override
104 {
104 {
105 if (!m_XAxisData->cdata().isEmpty()) {
105 if (!m_XAxisData->cdata().isEmpty()) {
106 return SqpRange{m_XAxisData->cdata().first(), m_XAxisData->cdata().last()};
106 return SqpRange{m_XAxisData->cdata().first(), m_XAxisData->cdata().last()};
107 }
107 }
108
108
109 return SqpRange{};
109 return SqpRange{};
110 }
110 }
111
111
112 void clear()
112 void clear()
113 {
113 {
114 m_XAxisData->clear();
114 m_XAxisData->clear();
115 m_ValuesData->clear();
115 m_ValuesData->clear();
116 }
116 }
117
117
118 /// Merges into the data series an other data series
118 /// Merges into the data series an other data series
119 /// @remarks the data series to merge with is cleared after the operation
119 /// @remarks the data series to merge with is cleared after the operation
120 void merge(IDataSeries *dataSeries) override
120 void merge(IDataSeries *dataSeries) override
121 {
121 {
122 dataSeries->lockWrite();
122 dataSeries->lockWrite();
123 lockWrite();
123 lockWrite();
124
124
125 if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
125 if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
126 const auto &otherXAxisData = other->xAxisData()->cdata();
126 const auto &otherXAxisData = other->xAxisData()->cdata();
127 const auto &xAxisData = m_XAxisData->cdata();
127 const auto &xAxisData = m_XAxisData->cdata();
128
128
129 // As data series are sorted, we can improve performances of merge, by call the sort
129 // As data series are sorted, we can improve performances of merge, by call the sort
130 // method only if the two data series overlap.
130 // method only if the two data series overlap.
131 if (!otherXAxisData.empty()) {
131 if (!otherXAxisData.empty()) {
132 auto firstValue = otherXAxisData.front();
132 auto firstValue = otherXAxisData.front();
133 auto lastValue = otherXAxisData.back();
133 auto lastValue = otherXAxisData.back();
134
134
135 auto xAxisDataBegin = xAxisData.cbegin();
135 auto xAxisDataBegin = xAxisData.cbegin();
136 auto xAxisDataEnd = xAxisData.cend();
136 auto xAxisDataEnd = xAxisData.cend();
137
137
138 bool prepend;
138 bool prepend;
139 bool sortNeeded;
139 bool sortNeeded;
140
140
141 if (std::lower_bound(xAxisDataBegin, xAxisDataEnd, firstValue) == xAxisDataEnd) {
141 if (std::lower_bound(xAxisDataBegin, xAxisDataEnd, firstValue) == xAxisDataEnd) {
142 // Other data series if after data series
142 // Other data series if after data series
143 prepend = false;
143 prepend = false;
144 sortNeeded = false;
144 sortNeeded = false;
145 }
145 }
146 else if (std::upper_bound(xAxisDataBegin, xAxisDataEnd, lastValue)
146 else if (std::upper_bound(xAxisDataBegin, xAxisDataEnd, lastValue)
147 == xAxisDataBegin) {
147 == xAxisDataBegin) {
148 // Other data series if before data series
148 // Other data series if before data series
149 prepend = true;
149 prepend = true;
150 sortNeeded = false;
150 sortNeeded = false;
151 }
151 }
152 else {
152 else {
153 // The two data series overlap
153 // The two data series overlap
154 prepend = false;
154 prepend = false;
155 sortNeeded = true;
155 sortNeeded = true;
156 }
156 }
157
157
158 // Makes the merge
158 // Makes the merge
159 m_XAxisData->add(*other->xAxisData(), prepend);
159 m_XAxisData->add(*other->xAxisData(), prepend);
160 m_ValuesData->add(*other->valuesData(), prepend);
160 m_ValuesData->add(*other->valuesData(), prepend);
161
161
162 if (sortNeeded) {
162 if (sortNeeded) {
163 sort();
163 sort();
164 }
164 }
165 }
165 }
166
166
167 // Clears the other data series
167 // Clears the other data series
168 other->clear();
168 other->clear();
169 }
169 }
170 else {
170 else {
171 qCWarning(LOG_DataSeries())
171 qCWarning(LOG_DataSeries())
172 << QObject::tr("Detection of a type of IDataSeries we cannot merge with !");
172 << QObject::tr("Detection of a type of IDataSeries we cannot merge with !");
173 }
173 }
174 unlock();
174 unlock();
175 dataSeries->unlock();
175 dataSeries->unlock();
176 }
176 }
177
177
178 // ///////// //
178 // ///////// //
179 // Iterators //
179 // Iterators //
180 // ///////// //
180 // ///////// //
181
181
182 DataSeriesIterator cbegin() const override
182 DataSeriesIterator cbegin() const override
183 {
183 {
184 return DataSeriesIterator{DataSeriesIteratorValue{
184 return DataSeriesIterator{DataSeriesIteratorValue{
185 std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, true)}};
185 std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, true)}};
186 }
186 }
187
187
188 DataSeriesIterator cend() const override
188 DataSeriesIterator cend() const override
189 {
189 {
190 return DataSeriesIterator{DataSeriesIteratorValue{
190 return DataSeriesIterator{DataSeriesIteratorValue{
191 std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, false)}};
191 std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, false)}};
192 }
192 }
193
193
194 /// @sa IDataSeries::minXAxisData()
194 /// @sa IDataSeries::minXAxisData()
195 DataSeriesIterator minXAxisData(double minXAxisData) const override
195 DataSeriesIterator minXAxisData(double minXAxisData) const override
196 {
196 {
197 return std::lower_bound(
197 return std::lower_bound(
198 cbegin(), cend(), minXAxisData,
198 cbegin(), cend(), minXAxisData,
199 [](const auto &itValue, const auto &value) { return itValue.x() < value; });
199 [](const auto &itValue, const auto &value) { return itValue.x() < value; });
200 }
200 }
201
201
202 /// @sa IDataSeries::maxXAxisData()
202 /// @sa IDataSeries::maxXAxisData()
203 DataSeriesIterator maxXAxisData(double maxXAxisData) const override
203 DataSeriesIterator maxXAxisData(double maxXAxisData) const override
204 {
204 {
205 // Gets the first element that greater than max value
205 // Gets the first element that greater than max value
206 auto it = std::upper_bound(
206 auto it = std::upper_bound(
207 cbegin(), cend(), maxXAxisData,
207 cbegin(), cend(), maxXAxisData,
208 [](const auto &value, const auto &itValue) { return value < itValue.x(); });
208 [](const auto &value, const auto &itValue) { return value < itValue.x(); });
209
209
210 return it == cbegin() ? cend() : --it;
210 return it == cbegin() ? cend() : --it;
211 }
211 }
212
212
213 std::pair<DataSeriesIterator, DataSeriesIterator> subData(double min, double max) const override
213 std::pair<DataSeriesIterator, DataSeriesIterator> xAxisRange(double minXAxisData,
214 double maxXAxisData) const override
214 {
215 {
215 if (min > max) {
216 if (minXAxisData > maxXAxisData) {
216 std::swap(min, max);
217 std::swap(minXAxisData, maxXAxisData);
217 }
218 }
218
219
219 auto begin = cbegin();
220 auto begin = cbegin();
220 auto end = cend();
221 auto end = cend();
221
222
222 auto lowerIt
223 auto lowerIt = std::lower_bound(
223 = std::lower_bound(begin, end, min, [](const auto &itValue, const auto &value) {
224 begin, end, minXAxisData,
224 return itValue.x() < value;
225 [](const auto &itValue, const auto &value) { return itValue.x() < value; });
225 });
226 auto upperIt = std::upper_bound(
226 auto upperIt
227 begin, end, maxXAxisData,
227 = std::upper_bound(begin, end, max, [](const auto &value, const auto &itValue) {
228 [](const auto &value, const auto &itValue) { return value < itValue.x(); });
228 return value < itValue.x();
229 });
230
229
231 return std::make_pair(lowerIt, upperIt);
230 return std::make_pair(lowerIt, upperIt);
232 }
231 }
233
232
234 // /////// //
233 // /////// //
235 // Mutexes //
234 // Mutexes //
236 // /////// //
235 // /////// //
237
236
238 virtual void lockRead() { m_Lock.lockForRead(); }
237 virtual void lockRead() { m_Lock.lockForRead(); }
239 virtual void lockWrite() { m_Lock.lockForWrite(); }
238 virtual void lockWrite() { m_Lock.lockForWrite(); }
240 virtual void unlock() { m_Lock.unlock(); }
239 virtual void unlock() { m_Lock.unlock(); }
241
240
242 protected:
241 protected:
243 /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a
242 /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a
244 /// DataSeries with no values will be created.
243 /// DataSeries with no values will be created.
245 /// @remarks data series is automatically sorted on its x-axis data
244 /// @remarks data series is automatically sorted on its x-axis data
246 explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit,
245 explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit,
247 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit)
246 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit)
248 : m_XAxisData{xAxisData},
247 : m_XAxisData{xAxisData},
249 m_XAxisUnit{xAxisUnit},
248 m_XAxisUnit{xAxisUnit},
250 m_ValuesData{valuesData},
249 m_ValuesData{valuesData},
251 m_ValuesUnit{valuesUnit}
250 m_ValuesUnit{valuesUnit}
252 {
251 {
253 if (m_XAxisData->size() != m_ValuesData->size()) {
252 if (m_XAxisData->size() != m_ValuesData->size()) {
254 clear();
253 clear();
255 }
254 }
256
255
257 // Sorts data if it's not the case
256 // Sorts data if it's not the case
258 const auto &xAxisCData = m_XAxisData->cdata();
257 const auto &xAxisCData = m_XAxisData->cdata();
259 if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) {
258 if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) {
260 sort();
259 sort();
261 }
260 }
262 }
261 }
263
262
264 /// Copy ctor
263 /// Copy ctor
265 explicit DataSeries(const DataSeries<Dim> &other)
264 explicit DataSeries(const DataSeries<Dim> &other)
266 : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)},
265 : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)},
267 m_XAxisUnit{other.m_XAxisUnit},
266 m_XAxisUnit{other.m_XAxisUnit},
268 m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)},
267 m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)},
269 m_ValuesUnit{other.m_ValuesUnit}
268 m_ValuesUnit{other.m_ValuesUnit}
270 {
269 {
271 // Since a series is ordered from its construction and is always ordered, it is not
270 // Since a series is ordered from its construction and is always ordered, it is not
272 // necessary to call the sort method here ('other' is sorted)
271 // necessary to call the sort method here ('other' is sorted)
273 }
272 }
274
273
275 /// Assignment operator
274 /// Assignment operator
276 template <int D>
275 template <int D>
277 DataSeries &operator=(DataSeries<D> other)
276 DataSeries &operator=(DataSeries<D> other)
278 {
277 {
279 std::swap(m_XAxisData, other.m_XAxisData);
278 std::swap(m_XAxisData, other.m_XAxisData);
280 std::swap(m_XAxisUnit, other.m_XAxisUnit);
279 std::swap(m_XAxisUnit, other.m_XAxisUnit);
281 std::swap(m_ValuesData, other.m_ValuesData);
280 std::swap(m_ValuesData, other.m_ValuesData);
282 std::swap(m_ValuesUnit, other.m_ValuesUnit);
281 std::swap(m_ValuesUnit, other.m_ValuesUnit);
283
282
284 return *this;
283 return *this;
285 }
284 }
286
285
287 private:
286 private:
288 /**
287 /**
289 * Sorts data series on its x-axis data
288 * Sorts data series on its x-axis data
290 */
289 */
291 void sort() noexcept
290 void sort() noexcept
292 {
291 {
293 auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>());
292 auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>());
294 m_XAxisData = m_XAxisData->sort(permutation);
293 m_XAxisData = m_XAxisData->sort(permutation);
295 m_ValuesData = m_ValuesData->sort(permutation);
294 m_ValuesData = m_ValuesData->sort(permutation);
296 }
295 }
297
296
298 std::shared_ptr<ArrayData<1> > m_XAxisData;
297 std::shared_ptr<ArrayData<1> > m_XAxisData;
299 Unit m_XAxisUnit;
298 Unit m_XAxisUnit;
300 std::shared_ptr<ArrayData<Dim> > m_ValuesData;
299 std::shared_ptr<ArrayData<Dim> > m_ValuesData;
301 Unit m_ValuesUnit;
300 Unit m_ValuesUnit;
302
301
303 QReadWriteLock m_Lock;
302 QReadWriteLock m_Lock;
304 };
303 };
305
304
306 #endif // SCIQLOP_DATASERIES_H
305 #endif // SCIQLOP_DATASERIES_H
@@ -1,96 +1,98
1 #ifndef SCIQLOP_IDATASERIES_H
1 #ifndef SCIQLOP_IDATASERIES_H
2 #define SCIQLOP_IDATASERIES_H
2 #define SCIQLOP_IDATASERIES_H
3
3
4 #include <Common/MetaTypes.h>
4 #include <Common/MetaTypes.h>
5 #include <Data/DataSeriesIterator.h>
5 #include <Data/DataSeriesIterator.h>
6 #include <Data/SqpRange.h>
6 #include <Data/SqpRange.h>
7
7
8 #include <memory>
8 #include <memory>
9
9
10 #include <QString>
10 #include <QString>
11
11
12 template <int Dim>
12 template <int Dim>
13 class ArrayData;
13 class ArrayData;
14
14
15 struct Unit {
15 struct Unit {
16 explicit Unit(const QString &name = {}, bool timeUnit = false)
16 explicit Unit(const QString &name = {}, bool timeUnit = false)
17 : m_Name{name}, m_TimeUnit{timeUnit}
17 : m_Name{name}, m_TimeUnit{timeUnit}
18 {
18 {
19 }
19 }
20
20
21 inline bool operator==(const Unit &other) const
21 inline bool operator==(const Unit &other) const
22 {
22 {
23 return std::tie(m_Name, m_TimeUnit) == std::tie(other.m_Name, other.m_TimeUnit);
23 return std::tie(m_Name, m_TimeUnit) == std::tie(other.m_Name, other.m_TimeUnit);
24 }
24 }
25 inline bool operator!=(const Unit &other) const { return !(*this == other); }
25 inline bool operator!=(const Unit &other) const { return !(*this == other); }
26
26
27 QString m_Name; ///< Unit name
27 QString m_Name; ///< Unit name
28 bool m_TimeUnit; ///< The unit is a unit of time (UTC)
28 bool m_TimeUnit; ///< The unit is a unit of time (UTC)
29 };
29 };
30
30
31 /**
31 /**
32 * @brief The IDataSeries aims to declare a data series.
32 * @brief The IDataSeries aims to declare a data series.
33 *
33 *
34 * A data series is an entity that contains at least :
34 * A data series is an entity that contains at least :
35 * - one dataset representing the x-axis
35 * - one dataset representing the x-axis
36 * - one dataset representing the values
36 * - one dataset representing the values
37 *
37 *
38 * Each dataset is represented by an ArrayData, and is associated with a unit.
38 * Each dataset is represented by an ArrayData, and is associated with a unit.
39 *
39 *
40 * An ArrayData can be unidimensional or two-dimensional, depending on the implementation of the
40 * An ArrayData can be unidimensional or two-dimensional, depending on the implementation of the
41 * IDataSeries. The x-axis dataset is always unidimensional.
41 * IDataSeries. The x-axis dataset is always unidimensional.
42 *
42 *
43 * @sa ArrayData
43 * @sa ArrayData
44 */
44 */
45 class IDataSeries {
45 class IDataSeries {
46 public:
46 public:
47 virtual ~IDataSeries() noexcept = default;
47 virtual ~IDataSeries() noexcept = default;
48
48
49 /// Returns the x-axis dataset
49 /// Returns the x-axis dataset
50 virtual std::shared_ptr<ArrayData<1> > xAxisData() = 0;
50 virtual std::shared_ptr<ArrayData<1> > xAxisData() = 0;
51
51
52 /// Returns the x-axis dataset (as const)
52 /// Returns the x-axis dataset (as const)
53 virtual const std::shared_ptr<ArrayData<1> > xAxisData() const = 0;
53 virtual const std::shared_ptr<ArrayData<1> > xAxisData() const = 0;
54
54
55 virtual Unit xAxisUnit() const = 0;
55 virtual Unit xAxisUnit() const = 0;
56
56
57 virtual Unit valuesUnit() const = 0;
57 virtual Unit valuesUnit() const = 0;
58
58
59 virtual void merge(IDataSeries *dataSeries) = 0;
59 virtual void merge(IDataSeries *dataSeries) = 0;
60 /// @todo Review the name and signature of this method
60 /// @todo Review the name and signature of this method
61 virtual std::shared_ptr<IDataSeries> subDataSeries(const SqpRange &range) = 0;
61 virtual std::shared_ptr<IDataSeries> subDataSeries(const SqpRange &range) = 0;
62
62
63 virtual std::unique_ptr<IDataSeries> clone() const = 0;
63 virtual std::unique_ptr<IDataSeries> clone() const = 0;
64 virtual SqpRange range() const = 0;
64 virtual SqpRange range() const = 0;
65
65
66 // ///////// //
66 // ///////// //
67 // Iterators //
67 // Iterators //
68 // ///////// //
68 // ///////// //
69
69
70 virtual DataSeriesIterator cbegin() const = 0;
70 virtual DataSeriesIterator cbegin() const = 0;
71 virtual DataSeriesIterator cend() const = 0;
71 virtual DataSeriesIterator cend() const = 0;
72
72
73 /// @return the iterator to the first entry of the data series whose x-axis data is greater than
73 /// @return the iterator to the first entry of the data series whose x-axis data is greater than
74 /// or equal to the value passed in parameter, or the end iterator if there is no matching value
74 /// or equal to the value passed in parameter, or the end iterator if there is no matching value
75 virtual DataSeriesIterator minXAxisData(double minXAxisData) const = 0;
75 virtual DataSeriesIterator minXAxisData(double minXAxisData) const = 0;
76
76
77 /// @return the iterator to the last entry of the data series whose x-axis data is less than or
77 /// @return the iterator to the last entry of the data series whose x-axis data is less than or
78 /// equal to the value passed in parameter, or the end iterator if there is no matching value
78 /// equal to the value passed in parameter, or the end iterator if there is no matching value
79 virtual DataSeriesIterator maxXAxisData(double maxXAxisData) const = 0;
79 virtual DataSeriesIterator maxXAxisData(double maxXAxisData) const = 0;
80
80
81 virtual std::pair<DataSeriesIterator, DataSeriesIterator> subData(double min,
81 /// @return the iterators pointing to the range of data whose x-axis values are between min and
82 double max) const = 0;
82 /// max passed in parameters
83 virtual std::pair<DataSeriesIterator, DataSeriesIterator>
84 xAxisRange(double minXAxisData, double maxXAxisData) const = 0;
83
85
84 // /////// //
86 // /////// //
85 // Mutexes //
87 // Mutexes //
86 // /////// //
88 // /////// //
87
89
88 virtual void lockRead() = 0;
90 virtual void lockRead() = 0;
89 virtual void lockWrite() = 0;
91 virtual void lockWrite() = 0;
90 virtual void unlock() = 0;
92 virtual void unlock() = 0;
91 };
93 };
92
94
93 // Required for using shared_ptr in signals/slots
95 // Required for using shared_ptr in signals/slots
94 SCIQLOP_REGISTER_META_TYPE(IDATASERIES_PTR_REGISTRY, std::shared_ptr<IDataSeries>)
96 SCIQLOP_REGISTER_META_TYPE(IDATASERIES_PTR_REGISTRY, std::shared_ptr<IDataSeries>)
95
97
96 #endif // SCIQLOP_IDATASERIES_H
98 #endif // SCIQLOP_IDATASERIES_H
@@ -1,31 +1,31
1 #include <Data/ScalarSeries.h>
1 #include <Data/ScalarSeries.h>
2
2
3 ScalarSeries::ScalarSeries(QVector<double> xAxisData, QVector<double> valuesData,
3 ScalarSeries::ScalarSeries(QVector<double> xAxisData, QVector<double> valuesData,
4 const Unit &xAxisUnit, const Unit &valuesUnit)
4 const Unit &xAxisUnit, const Unit &valuesUnit)
5 : DataSeries{std::make_shared<ArrayData<1> >(std::move(xAxisData)), xAxisUnit,
5 : DataSeries{std::make_shared<ArrayData<1> >(std::move(xAxisData)), xAxisUnit,
6 std::make_shared<ArrayData<1> >(std::move(valuesData)), valuesUnit}
6 std::make_shared<ArrayData<1> >(std::move(valuesData)), valuesUnit}
7 {
7 {
8 }
8 }
9
9
10 std::unique_ptr<IDataSeries> ScalarSeries::clone() const
10 std::unique_ptr<IDataSeries> ScalarSeries::clone() const
11 {
11 {
12 return std::make_unique<ScalarSeries>(*this);
12 return std::make_unique<ScalarSeries>(*this);
13 }
13 }
14
14
15 std::shared_ptr<IDataSeries> ScalarSeries::subDataSeries(const SqpRange &range)
15 std::shared_ptr<IDataSeries> ScalarSeries::subDataSeries(const SqpRange &range)
16 {
16 {
17 auto subXAxisData = QVector<double>();
17 auto subXAxisData = QVector<double>();
18 auto subValuesData = QVector<double>();
18 auto subValuesData = QVector<double>();
19 this->lockRead();
19 this->lockRead();
20 {
20 {
21 auto bounds = subData(range.m_TStart, range.m_TEnd);
21 auto bounds = xAxisRange(range.m_TStart, range.m_TEnd);
22 for (auto it = bounds.first; it != bounds.second; ++it) {
22 for (auto it = bounds.first; it != bounds.second; ++it) {
23 subXAxisData.append(it->x());
23 subXAxisData.append(it->x());
24 subValuesData.append(it->value());
24 subValuesData.append(it->value());
25 }
25 }
26 }
26 }
27 this->unlock();
27 this->unlock();
28
28
29 return std::make_shared<ScalarSeries>(subXAxisData, subValuesData, this->xAxisUnit(),
29 return std::make_shared<ScalarSeries>(subXAxisData, subValuesData, this->xAxisUnit(),
30 this->valuesUnit());
30 this->valuesUnit());
31 }
31 }
@@ -1,39 +1,39
1 #include "Data/VectorSeries.h"
1 #include "Data/VectorSeries.h"
2
2
3 VectorSeries::VectorSeries(QVector<double> xAxisData, QVector<double> xValuesData,
3 VectorSeries::VectorSeries(QVector<double> xAxisData, QVector<double> xValuesData,
4 QVector<double> yValuesData, QVector<double> zValuesData,
4 QVector<double> yValuesData, QVector<double> zValuesData,
5 const Unit &xAxisUnit, const Unit &valuesUnit)
5 const Unit &xAxisUnit, const Unit &valuesUnit)
6 : DataSeries{std::make_shared<ArrayData<1> >(std::move(xAxisData)), xAxisUnit,
6 : DataSeries{std::make_shared<ArrayData<1> >(std::move(xAxisData)), xAxisUnit,
7 std::make_shared<ArrayData<2> >(QVector<QVector<double> >{
7 std::make_shared<ArrayData<2> >(QVector<QVector<double> >{
8 std::move(xValuesData), std::move(yValuesData), std::move(zValuesData)}),
8 std::move(xValuesData), std::move(yValuesData), std::move(zValuesData)}),
9 valuesUnit}
9 valuesUnit}
10 {
10 {
11 }
11 }
12
12
13 std::unique_ptr<IDataSeries> VectorSeries::clone() const
13 std::unique_ptr<IDataSeries> VectorSeries::clone() const
14 {
14 {
15 return std::make_unique<VectorSeries>(*this);
15 return std::make_unique<VectorSeries>(*this);
16 }
16 }
17
17
18 std::shared_ptr<IDataSeries> VectorSeries::subDataSeries(const SqpRange &range)
18 std::shared_ptr<IDataSeries> VectorSeries::subDataSeries(const SqpRange &range)
19 {
19 {
20 auto subXAxisData = QVector<double>();
20 auto subXAxisData = QVector<double>();
21 auto subXValuesData = QVector<double>();
21 auto subXValuesData = QVector<double>();
22 auto subYValuesData = QVector<double>();
22 auto subYValuesData = QVector<double>();
23 auto subZValuesData = QVector<double>();
23 auto subZValuesData = QVector<double>();
24
24
25 this->lockRead();
25 this->lockRead();
26 {
26 {
27 auto bounds = subData(range.m_TStart, range.m_TEnd);
27 auto bounds = xAxisRange(range.m_TStart, range.m_TEnd);
28 for (auto it = bounds.first; it != bounds.second; ++it) {
28 for (auto it = bounds.first; it != bounds.second; ++it) {
29 subXAxisData.append(it->x());
29 subXAxisData.append(it->x());
30 subXValuesData.append(it->value(0));
30 subXValuesData.append(it->value(0));
31 subYValuesData.append(it->value(1));
31 subYValuesData.append(it->value(1));
32 subZValuesData.append(it->value(2));
32 subZValuesData.append(it->value(2));
33 }
33 }
34 }
34 }
35 this->unlock();
35 this->unlock();
36
36
37 return std::make_shared<VectorSeries>(subXAxisData, subXValuesData, subYValuesData,
37 return std::make_shared<VectorSeries>(subXAxisData, subXValuesData, subYValuesData,
38 subZValuesData, this->xAxisUnit(), this->valuesUnit());
38 subZValuesData, this->xAxisUnit(), this->valuesUnit());
39 }
39 }
@@ -1,354 +1,362
1 #include "Data/DataSeries.h"
1 #include "Data/DataSeries.h"
2 #include "Data/ScalarSeries.h"
2 #include "Data/ScalarSeries.h"
3
3
4 #include <QObject>
4 #include <QObject>
5 #include <QtTest>
5 #include <QtTest>
6
6
7 Q_DECLARE_METATYPE(std::shared_ptr<ScalarSeries>)
7 Q_DECLARE_METATYPE(std::shared_ptr<ScalarSeries>)
8
8
9 class TestDataSeries : public QObject {
9 class TestDataSeries : public QObject {
10 Q_OBJECT
10 Q_OBJECT
11 private slots:
11 private slots:
12 /// Input test data
12 /// Input test data
13 /// @sa testCtor()
13 /// @sa testCtor()
14 void testCtor_data();
14 void testCtor_data();
15
15
16 /// Tests construction of a data series
16 /// Tests construction of a data series
17 void testCtor();
17 void testCtor();
18
18
19 /// Input test data
19 /// Input test data
20 /// @sa testMerge()
20 /// @sa testMerge()
21 void testMerge_data();
21 void testMerge_data();
22
22
23 /// Tests merge of two data series
23 /// Tests merge of two data series
24 void testMerge();
24 void testMerge();
25
25
26 /// Input test data
26 /// Input test data
27 /// @sa testMinXAxisData()
27 /// @sa testMinXAxisData()
28 void testMinXAxisData_data();
28 void testMinXAxisData_data();
29
29
30 /// Tests get min x-axis data of a data series
30 /// Tests get min x-axis data of a data series
31 void testMinXAxisData();
31 void testMinXAxisData();
32
32
33 /// Input test data
33 /// Input test data
34 /// @sa testMaxXAxisData()
34 /// @sa testMaxXAxisData()
35 void testMaxXAxisData_data();
35 void testMaxXAxisData_data();
36
36
37 /// Tests get max x-axis data of a data series
37 /// Tests get max x-axis data of a data series
38 void testMaxXAxisData();
38 void testMaxXAxisData();
39
39
40 /// Input test data
40 /// Input test data
41 /// @sa testSubdata()
41 /// @sa testXAxisRange()
42 void testSubdata_data();
42 void testXAxisRange_data();
43
43
44 /// Tests get subdata of two data series
44 /// Tests get x-axis range of a data series
45 void testSubdata();
45 void testXAxisRange();
46 };
46 };
47
47
48 void TestDataSeries::testCtor_data()
48 void TestDataSeries::testCtor_data()
49 {
49 {
50 // ////////////// //
50 // ////////////// //
51 // Test structure //
51 // Test structure //
52 // ////////////// //
52 // ////////////// //
53
53
54 // x-axis data
54 // x-axis data
55 QTest::addColumn<QVector<double> >("xAxisData");
55 QTest::addColumn<QVector<double> >("xAxisData");
56 // values data
56 // values data
57 QTest::addColumn<QVector<double> >("valuesData");
57 QTest::addColumn<QVector<double> >("valuesData");
58
58
59 // expected x-axis data
59 // expected x-axis data
60 QTest::addColumn<QVector<double> >("expectedXAxisData");
60 QTest::addColumn<QVector<double> >("expectedXAxisData");
61 // expected values data
61 // expected values data
62 QTest::addColumn<QVector<double> >("expectedValuesData");
62 QTest::addColumn<QVector<double> >("expectedValuesData");
63
63
64 // ////////// //
64 // ////////// //
65 // Test cases //
65 // Test cases //
66 // ////////// //
66 // ////////// //
67
67
68 QTest::newRow("invalidData (different sizes of vectors)")
68 QTest::newRow("invalidData (different sizes of vectors)")
69 << QVector<double>{1., 2., 3., 4., 5.} << QVector<double>{100., 200., 300.}
69 << QVector<double>{1., 2., 3., 4., 5.} << QVector<double>{100., 200., 300.}
70 << QVector<double>{} << QVector<double>{};
70 << QVector<double>{} << QVector<double>{};
71
71
72 QTest::newRow("sortedData") << QVector<double>{1., 2., 3., 4., 5.}
72 QTest::newRow("sortedData") << QVector<double>{1., 2., 3., 4., 5.}
73 << QVector<double>{100., 200., 300., 400., 500.}
73 << QVector<double>{100., 200., 300., 400., 500.}
74 << QVector<double>{1., 2., 3., 4., 5.}
74 << QVector<double>{1., 2., 3., 4., 5.}
75 << QVector<double>{100., 200., 300., 400., 500.};
75 << QVector<double>{100., 200., 300., 400., 500.};
76
76
77 QTest::newRow("unsortedData") << QVector<double>{5., 4., 3., 2., 1.}
77 QTest::newRow("unsortedData") << QVector<double>{5., 4., 3., 2., 1.}
78 << QVector<double>{100., 200., 300., 400., 500.}
78 << QVector<double>{100., 200., 300., 400., 500.}
79 << QVector<double>{1., 2., 3., 4., 5.}
79 << QVector<double>{1., 2., 3., 4., 5.}
80 << QVector<double>{500., 400., 300., 200., 100.};
80 << QVector<double>{500., 400., 300., 200., 100.};
81
81
82 QTest::newRow("unsortedData2")
82 QTest::newRow("unsortedData2")
83 << QVector<double>{1., 4., 3., 5., 2.} << QVector<double>{100., 200., 300., 400., 500.}
83 << QVector<double>{1., 4., 3., 5., 2.} << QVector<double>{100., 200., 300., 400., 500.}
84 << QVector<double>{1., 2., 3., 4., 5.} << QVector<double>{100., 500., 300., 200., 400.};
84 << QVector<double>{1., 2., 3., 4., 5.} << QVector<double>{100., 500., 300., 200., 400.};
85 }
85 }
86
86
87 void TestDataSeries::testCtor()
87 void TestDataSeries::testCtor()
88 {
88 {
89 // Creates series
89 // Creates series
90 QFETCH(QVector<double>, xAxisData);
90 QFETCH(QVector<double>, xAxisData);
91 QFETCH(QVector<double>, valuesData);
91 QFETCH(QVector<double>, valuesData);
92
92
93 auto series = std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData),
93 auto series = std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData),
94 Unit{}, Unit{});
94 Unit{}, Unit{});
95
95
96 // Validates results : we check that the data series is sorted on its x-axis data
96 // Validates results : we check that the data series is sorted on its x-axis data
97 QFETCH(QVector<double>, expectedXAxisData);
97 QFETCH(QVector<double>, expectedXAxisData);
98 QFETCH(QVector<double>, expectedValuesData);
98 QFETCH(QVector<double>, expectedValuesData);
99
99
100 auto seriesXAxisData = series->xAxisData()->data();
100 auto seriesXAxisData = series->xAxisData()->data();
101 auto seriesValuesData = series->valuesData()->data();
101 auto seriesValuesData = series->valuesData()->data();
102
102
103 QVERIFY(
103 QVERIFY(
104 std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin()));
104 std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin()));
105 QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(),
105 QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(),
106 seriesValuesData.cbegin()));
106 seriesValuesData.cbegin()));
107 }
107 }
108
108
109 namespace {
109 namespace {
110
110
111 std::shared_ptr<ScalarSeries> createSeries(QVector<double> xAxisData, QVector<double> valuesData)
111 std::shared_ptr<ScalarSeries> createSeries(QVector<double> xAxisData, QVector<double> valuesData)
112 {
112 {
113 return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), Unit{},
113 return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), Unit{},
114 Unit{});
114 Unit{});
115 }
115 }
116
116
117 } // namespace
117 } // namespace
118
118
119 void TestDataSeries::testMerge_data()
119 void TestDataSeries::testMerge_data()
120 {
120 {
121 // ////////////// //
121 // ////////////// //
122 // Test structure //
122 // Test structure //
123 // ////////////// //
123 // ////////////// //
124
124
125 // Data series to merge
125 // Data series to merge
126 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
126 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
127 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries2");
127 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries2");
128
128
129 // Expected values in the first data series after merge
129 // Expected values in the first data series after merge
130 QTest::addColumn<QVector<double> >("expectedXAxisData");
130 QTest::addColumn<QVector<double> >("expectedXAxisData");
131 QTest::addColumn<QVector<double> >("expectedValuesData");
131 QTest::addColumn<QVector<double> >("expectedValuesData");
132
132
133 // ////////// //
133 // ////////// //
134 // Test cases //
134 // Test cases //
135 // ////////// //
135 // ////////// //
136
136
137 QTest::newRow("sortedMerge")
137 QTest::newRow("sortedMerge")
138 << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
138 << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
139 << createSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.})
139 << createSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.})
140 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
140 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
141 << QVector<double>{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.};
141 << QVector<double>{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.};
142
142
143 QTest::newRow("unsortedMerge")
143 QTest::newRow("unsortedMerge")
144 << createSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.})
144 << createSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.})
145 << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
145 << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
146 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
146 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
147 << QVector<double>{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.};
147 << QVector<double>{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.};
148
148
149 QTest::newRow("unsortedMerge2")
149 QTest::newRow("unsortedMerge2")
150 << createSeries({1., 2., 8., 9., 10}, {100., 200., 300., 400., 500.})
150 << createSeries({1., 2., 8., 9., 10}, {100., 200., 300., 400., 500.})
151 << createSeries({3., 4., 5., 6., 7.}, {600., 700., 800., 900., 1000.})
151 << createSeries({3., 4., 5., 6., 7.}, {600., 700., 800., 900., 1000.})
152 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
152 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
153 << QVector<double>{100., 200., 600., 700., 800., 900., 1000., 300., 400., 500.};
153 << QVector<double>{100., 200., 600., 700., 800., 900., 1000., 300., 400., 500.};
154
154
155 QTest::newRow("unsortedMerge3")
155 QTest::newRow("unsortedMerge3")
156 << createSeries({3., 5., 8., 7., 2}, {100., 200., 300., 400., 500.})
156 << createSeries({3., 5., 8., 7., 2}, {100., 200., 300., 400., 500.})
157 << createSeries({6., 4., 9., 10., 1.}, {600., 700., 800., 900., 1000.})
157 << createSeries({6., 4., 9., 10., 1.}, {600., 700., 800., 900., 1000.})
158 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
158 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
159 << QVector<double>{1000., 500., 100., 700., 200., 600., 400., 300., 800., 900.};
159 << QVector<double>{1000., 500., 100., 700., 200., 600., 400., 300., 800., 900.};
160 }
160 }
161
161
162 void TestDataSeries::testMerge()
162 void TestDataSeries::testMerge()
163 {
163 {
164 // Merges series
164 // Merges series
165 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
165 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
166 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries2);
166 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries2);
167
167
168 dataSeries->merge(dataSeries2.get());
168 dataSeries->merge(dataSeries2.get());
169
169
170 // Validates results : we check that the merge is valid and the data series is sorted on its
170 // Validates results : we check that the merge is valid and the data series is sorted on its
171 // x-axis data
171 // x-axis data
172 QFETCH(QVector<double>, expectedXAxisData);
172 QFETCH(QVector<double>, expectedXAxisData);
173 QFETCH(QVector<double>, expectedValuesData);
173 QFETCH(QVector<double>, expectedValuesData);
174
174
175 auto seriesXAxisData = dataSeries->xAxisData()->data();
175 auto seriesXAxisData = dataSeries->xAxisData()->data();
176 auto seriesValuesData = dataSeries->valuesData()->data();
176 auto seriesValuesData = dataSeries->valuesData()->data();
177
177
178 QVERIFY(
178 QVERIFY(
179 std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin()));
179 std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin()));
180 QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(),
180 QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(),
181 seriesValuesData.cbegin()));
181 seriesValuesData.cbegin()));
182 }
182 }
183
183
184 void TestDataSeries::testMinXAxisData_data()
184 void TestDataSeries::testMinXAxisData_data()
185 {
185 {
186 // ////////////// //
186 // ////////////// //
187 // Test structure //
187 // Test structure //
188 // ////////////// //
188 // ////////////// //
189
189
190 // Data series to get min data
190 // Data series to get min data
191 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
191 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
192
192
193 // Min data
193 // Min data
194 QTest::addColumn<double>("min");
194 QTest::addColumn<double>("min");
195
195
196 // Expected results
196 // Expected results
197 QTest::addColumn<bool>(
197 QTest::addColumn<bool>(
198 "expectedOK"); // if true, expects to have a result (i.e. the iterator != end iterator)
198 "expectedOK"); // if true, expects to have a result (i.e. the iterator != end iterator)
199 QTest::addColumn<double>(
199 QTest::addColumn<double>(
200 "expectedMin"); // Expected value when method doesn't return end iterator
200 "expectedMin"); // Expected value when method doesn't return end iterator
201
201
202 // ////////// //
202 // ////////// //
203 // Test cases //
203 // Test cases //
204 // ////////// //
204 // ////////// //
205
205
206 QTest::newRow("minData1") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
206 QTest::newRow("minData1") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
207 << 0. << true << 1.;
207 << 0. << true << 1.;
208 QTest::newRow("minData2") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
208 QTest::newRow("minData2") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
209 << 1. << true << 1.;
209 << 1. << true << 1.;
210 QTest::newRow("minData3") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
210 QTest::newRow("minData3") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
211 << 1.1 << true << 2.;
211 << 1.1 << true << 2.;
212 QTest::newRow("minData4") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
212 QTest::newRow("minData4") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
213 << 5. << true << 5.;
213 << 5. << true << 5.;
214 QTest::newRow("minData5") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
214 QTest::newRow("minData5") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
215 << 5.1 << false << std::numeric_limits<double>::quiet_NaN();
215 << 5.1 << false << std::numeric_limits<double>::quiet_NaN();
216 QTest::newRow("minData6") << createSeries({}, {}) << 1.1 << false
216 QTest::newRow("minData6") << createSeries({}, {}) << 1.1 << false
217 << std::numeric_limits<double>::quiet_NaN();
217 << std::numeric_limits<double>::quiet_NaN();
218 }
218 }
219
219
220 void TestDataSeries::testMinXAxisData()
220 void TestDataSeries::testMinXAxisData()
221 {
221 {
222 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
222 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
223 QFETCH(double, min);
223 QFETCH(double, min);
224
224
225 QFETCH(bool, expectedOK);
225 QFETCH(bool, expectedOK);
226 QFETCH(double, expectedMin);
226 QFETCH(double, expectedMin);
227
227
228 auto it = dataSeries->minXAxisData(min);
228 auto it = dataSeries->minXAxisData(min);
229
229
230 QCOMPARE(expectedOK, it != dataSeries->cend());
230 QCOMPARE(expectedOK, it != dataSeries->cend());
231
231
232 // If the method doesn't return a end iterator, checks with expected value
232 // If the method doesn't return a end iterator, checks with expected value
233 if (expectedOK) {
233 if (expectedOK) {
234 QCOMPARE(expectedMin, it->x());
234 QCOMPARE(expectedMin, it->x());
235 }
235 }
236 }
236 }
237
237
238 void TestDataSeries::testMaxXAxisData_data()
238 void TestDataSeries::testMaxXAxisData_data()
239 {
239 {
240 // ////////////// //
240 // ////////////// //
241 // Test structure //
241 // Test structure //
242 // ////////////// //
242 // ////////////// //
243
243
244 // Data series to get max data
244 // Data series to get max data
245 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
245 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
246
246
247 // Max data
247 // Max data
248 QTest::addColumn<double>("max");
248 QTest::addColumn<double>("max");
249
249
250 // Expected results
250 // Expected results
251 QTest::addColumn<bool>(
251 QTest::addColumn<bool>(
252 "expectedOK"); // if true, expects to have a result (i.e. the iterator != end iterator)
252 "expectedOK"); // if true, expects to have a result (i.e. the iterator != end iterator)
253 QTest::addColumn<double>(
253 QTest::addColumn<double>(
254 "expectedMax"); // Expected value when method doesn't return end iterator
254 "expectedMax"); // Expected value when method doesn't return end iterator
255
255
256 // ////////// //
256 // ////////// //
257 // Test cases //
257 // Test cases //
258 // ////////// //
258 // ////////// //
259
259
260 QTest::newRow("maxData1") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
260 QTest::newRow("maxData1") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
261 << 6. << true << 5.;
261 << 6. << true << 5.;
262 QTest::newRow("maxData2") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
262 QTest::newRow("maxData2") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
263 << 5. << true << 5.;
263 << 5. << true << 5.;
264 QTest::newRow("maxData3") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
264 QTest::newRow("maxData3") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
265 << 4.9 << true << 4.;
265 << 4.9 << true << 4.;
266 QTest::newRow("maxData4") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
266 QTest::newRow("maxData4") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
267 << 1.1 << true << 1.;
267 << 1.1 << true << 1.;
268 QTest::newRow("maxData5") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
268 QTest::newRow("maxData5") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
269 << 1. << true << 1.;
269 << 1. << true << 1.;
270 QTest::newRow("maxData6") << createSeries({}, {}) << 1.1 << false
270 QTest::newRow("maxData6") << createSeries({}, {}) << 1.1 << false
271 << std::numeric_limits<double>::quiet_NaN();
271 << std::numeric_limits<double>::quiet_NaN();
272 }
272 }
273
273
274 void TestDataSeries::testMaxXAxisData()
274 void TestDataSeries::testMaxXAxisData()
275 {
275 {
276 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
276 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
277 QFETCH(double, max);
277 QFETCH(double, max);
278
278
279 QFETCH(bool, expectedOK);
279 QFETCH(bool, expectedOK);
280 QFETCH(double, expectedMax);
280 QFETCH(double, expectedMax);
281
281
282 auto it = dataSeries->maxXAxisData(max);
282 auto it = dataSeries->maxXAxisData(max);
283
283
284 QCOMPARE(expectedOK, it != dataSeries->cend());
284 QCOMPARE(expectedOK, it != dataSeries->cend());
285
285
286 // If the method doesn't return a end iterator, checks with expected value
286 // If the method doesn't return a end iterator, checks with expected value
287 if (expectedOK) {
287 if (expectedOK) {
288 QCOMPARE(expectedMax, it->x());
288 QCOMPARE(expectedMax, it->x());
289 }
289 }
290 }
290 }
291
291
292 void TestDataSeries::testSubdata_data()
292 void TestDataSeries::testXAxisRange_data()
293 {
293 {
294 // ////////////// //
294 // ////////////// //
295 // Test structure //
295 // Test structure //
296 // ////////////// //
296 // ////////////// //
297
297
298 // Data series to get subdata
298 // Data series to get x-axis range
299 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
299 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
300
300
301 // Min/max values
301 // Min/max values
302 QTest::addColumn<double>("min");
302 QTest::addColumn<double>("min");
303 QTest::addColumn<double>("max");
303 QTest::addColumn<double>("max");
304
304
305 // Expected values after subdata
305 // Expected values
306 QTest::addColumn<QVector<double> >("expectedXAxisData");
306 QTest::addColumn<QVector<double> >("expectedXAxisData");
307 QTest::addColumn<QVector<double> >("expectedValuesData");
307 QTest::addColumn<QVector<double> >("expectedValuesData");
308
308
309 // ////////// //
309 // ////////// //
310 // Test cases //
310 // Test cases //
311 // ////////// //
311 // ////////// //
312
312
313 QTest::newRow("subData1") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
313 QTest::newRow("xAxisRange1") << createSeries({1., 2., 3., 4., 5.},
314 << -1. << 3.2 << QVector<double>{1., 2., 3.}
314 {100., 200., 300., 400., 500.})
315 << QVector<double>{100., 200., 300.};
315 << -1. << 3.2 << QVector<double>{1., 2., 3.}
316 QTest::newRow("subData2") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
316 << QVector<double>{100., 200., 300.};
317 << 1. << 4. << QVector<double>{1., 2., 3., 4.}
317 QTest::newRow("xAxisRange2") << createSeries({1., 2., 3., 4., 5.},
318 << QVector<double>{100., 200., 300., 400.};
318 {100., 200., 300., 400., 500.})
319 QTest::newRow("subData3") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
319 << 1. << 4. << QVector<double>{1., 2., 3., 4.}
320 << 1. << 3.9 << QVector<double>{1., 2., 3.}
320 << QVector<double>{100., 200., 300., 400.};
321 << QVector<double>{100., 200., 300.};
321 QTest::newRow("xAxisRange3") << createSeries({1., 2., 3., 4., 5.},
322 QTest::newRow("subData4") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
322 {100., 200., 300., 400., 500.})
323 << 0. << 0.9 << QVector<double>{} << QVector<double>{};
323 << 1. << 3.9 << QVector<double>{1., 2., 3.}
324 QTest::newRow("subData5") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
324 << QVector<double>{100., 200., 300.};
325 << 0. << 1. << QVector<double>{1.} << QVector<double>{100.};
325 QTest::newRow("xAxisRange4") << createSeries({1., 2., 3., 4., 5.},
326 QTest::newRow("subData6") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
326 {100., 200., 300., 400., 500.})
327 << 2.1 << 6. << QVector<double>{3., 4., 5.}
327 << 0. << 0.9 << QVector<double>{} << QVector<double>{};
328 << QVector<double>{300., 400., 500.};
328 QTest::newRow("xAxisRange5") << createSeries({1., 2., 3., 4., 5.},
329 QTest::newRow("subData7") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
329 {100., 200., 300., 400., 500.})
330 << 6. << 9. << QVector<double>{} << QVector<double>{};
330 << 0. << 1. << QVector<double>{1.} << QVector<double>{100.};
331 QTest::newRow("subData8") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
331 QTest::newRow("xAxisRange6") << createSeries({1., 2., 3., 4., 5.},
332 << 5. << 9. << QVector<double>{5.} << QVector<double>{500.};
332 {100., 200., 300., 400., 500.})
333 << 2.1 << 6. << QVector<double>{3., 4., 5.}
334 << QVector<double>{300., 400., 500.};
335 QTest::newRow("xAxisRange7") << createSeries({1., 2., 3., 4., 5.},
336 {100., 200., 300., 400., 500.})
337 << 6. << 9. << QVector<double>{} << QVector<double>{};
338 QTest::newRow("xAxisRange8") << createSeries({1., 2., 3., 4., 5.},
339 {100., 200., 300., 400., 500.})
340 << 5. << 9. << QVector<double>{5.} << QVector<double>{500.};
333 }
341 }
334
342
335 void TestDataSeries::testSubdata()
343 void TestDataSeries::testXAxisRange()
336 {
344 {
337 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
345 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
338 QFETCH(double, min);
346 QFETCH(double, min);
339 QFETCH(double, max);
347 QFETCH(double, max);
340
348
341 QFETCH(QVector<double>, expectedXAxisData);
349 QFETCH(QVector<double>, expectedXAxisData);
342 QFETCH(QVector<double>, expectedValuesData);
350 QFETCH(QVector<double>, expectedValuesData);
343
351
344 auto bounds = dataSeries->subData(min, max);
352 auto bounds = dataSeries->xAxisRange(min, max);
345 QVERIFY(std::equal(bounds.first, bounds.second, expectedXAxisData.cbegin(),
353 QVERIFY(std::equal(bounds.first, bounds.second, expectedXAxisData.cbegin(),
346 expectedXAxisData.cend(),
354 expectedXAxisData.cend(),
347 [](const auto &it, const auto &expectedX) { return it.x() == expectedX; }));
355 [](const auto &it, const auto &expectedX) { return it.x() == expectedX; }));
348 QVERIFY(std::equal(
356 QVERIFY(std::equal(
349 bounds.first, bounds.second, expectedValuesData.cbegin(), expectedValuesData.cend(),
357 bounds.first, bounds.second, expectedValuesData.cbegin(), expectedValuesData.cend(),
350 [](const auto &it, const auto &expectedVal) { return it.value() == expectedVal; }));
358 [](const auto &it, const auto &expectedVal) { return it.value() == expectedVal; }));
351 }
359 }
352
360
353 QTEST_MAIN(TestDataSeries)
361 QTEST_MAIN(TestDataSeries)
354 #include "TestDataSeries.moc"
362 #include "TestDataSeries.moc"
@@ -1,243 +1,243
1 #include "Visualization/VisualizationGraphHelper.h"
1 #include "Visualization/VisualizationGraphHelper.h"
2 #include "Visualization/qcustomplot.h"
2 #include "Visualization/qcustomplot.h"
3
3
4 #include <Common/ColorUtils.h>
4 #include <Common/ColorUtils.h>
5
5
6 #include <Data/ScalarSeries.h>
6 #include <Data/ScalarSeries.h>
7 #include <Data/VectorSeries.h>
7 #include <Data/VectorSeries.h>
8
8
9 #include <Variable/Variable.h>
9 #include <Variable/Variable.h>
10
10
11 Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper")
11 Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper")
12
12
13 namespace {
13 namespace {
14
14
15 class SqpDataContainer : public QCPGraphDataContainer {
15 class SqpDataContainer : public QCPGraphDataContainer {
16 public:
16 public:
17 void appendGraphData(const QCPGraphData &data) { mData.append(data); }
17 void appendGraphData(const QCPGraphData &data) { mData.append(data); }
18 };
18 };
19
19
20
20
21 /// Format for datetimes on a axis
21 /// Format for datetimes on a axis
22 const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss");
22 const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss");
23
23
24 /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or
24 /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or
25 /// non-time data
25 /// non-time data
26 QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis)
26 QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis)
27 {
27 {
28 if (isTimeAxis) {
28 if (isTimeAxis) {
29 auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create();
29 auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create();
30 dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT);
30 dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT);
31 dateTicker->setDateTimeSpec(Qt::UTC);
31 dateTicker->setDateTimeSpec(Qt::UTC);
32
32
33 return dateTicker;
33 return dateTicker;
34 }
34 }
35 else {
35 else {
36 // default ticker
36 // default ticker
37 return QSharedPointer<QCPAxisTicker>::create();
37 return QSharedPointer<QCPAxisTicker>::create();
38 }
38 }
39 }
39 }
40
40
41 /// Sets axes properties according to the properties of a data series
41 /// Sets axes properties according to the properties of a data series
42 template <int Dim>
42 template <int Dim>
43 void setAxesProperties(const DataSeries<Dim> &dataSeries, QCustomPlot &plot) noexcept
43 void setAxesProperties(const DataSeries<Dim> &dataSeries, QCustomPlot &plot) noexcept
44 {
44 {
45 /// @todo : for the moment, no control is performed on the axes: the units and the tickers
45 /// @todo : for the moment, no control is performed on the axes: the units and the tickers
46 /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph
46 /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph
47 auto setAxisProperties = [](auto axis, const auto &unit) {
47 auto setAxisProperties = [](auto axis, const auto &unit) {
48 // label (unit name)
48 // label (unit name)
49 axis->setLabel(unit.m_Name);
49 axis->setLabel(unit.m_Name);
50
50
51 // ticker (depending on the type of unit)
51 // ticker (depending on the type of unit)
52 axis->setTicker(axisTicker(unit.m_TimeUnit));
52 axis->setTicker(axisTicker(unit.m_TimeUnit));
53 };
53 };
54 setAxisProperties(plot.xAxis, dataSeries.xAxisUnit());
54 setAxisProperties(plot.xAxis, dataSeries.xAxisUnit());
55 setAxisProperties(plot.yAxis, dataSeries.valuesUnit());
55 setAxisProperties(plot.yAxis, dataSeries.valuesUnit());
56 }
56 }
57
57
58 /**
58 /**
59 * Struct used to create plottables, depending on the type of the data series from which to create
59 * Struct used to create plottables, depending on the type of the data series from which to create
60 * them
60 * them
61 * @tparam T the data series' type
61 * @tparam T the data series' type
62 * @remarks Default implementation can't create plottables
62 * @remarks Default implementation can't create plottables
63 */
63 */
64 template <typename T, typename Enabled = void>
64 template <typename T, typename Enabled = void>
65 struct PlottablesCreator {
65 struct PlottablesCreator {
66 static PlottablesMap createPlottables(T &, QCustomPlot &)
66 static PlottablesMap createPlottables(T &, QCustomPlot &)
67 {
67 {
68 qCCritical(LOG_DataSeries())
68 qCCritical(LOG_DataSeries())
69 << QObject::tr("Can't create plottables: unmanaged data series type");
69 << QObject::tr("Can't create plottables: unmanaged data series type");
70 return {};
70 return {};
71 }
71 }
72 };
72 };
73
73
74 /**
74 /**
75 * Specialization of PlottablesCreator for scalars and vectors
75 * Specialization of PlottablesCreator for scalars and vectors
76 * @sa ScalarSeries
76 * @sa ScalarSeries
77 * @sa VectorSeries
77 * @sa VectorSeries
78 */
78 */
79 template <typename T>
79 template <typename T>
80 struct PlottablesCreator<T,
80 struct PlottablesCreator<T,
81 typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
81 typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
82 or std::is_base_of<VectorSeries, T>::value> > {
82 or std::is_base_of<VectorSeries, T>::value> > {
83 static PlottablesMap createPlottables(T &dataSeries, QCustomPlot &plot)
83 static PlottablesMap createPlottables(T &dataSeries, QCustomPlot &plot)
84 {
84 {
85 PlottablesMap result{};
85 PlottablesMap result{};
86
86
87 // Gets the number of components of the data series
87 // Gets the number of components of the data series
88 auto componentCount = dataSeries.valuesData()->componentCount();
88 auto componentCount = dataSeries.valuesData()->componentCount();
89
89
90 auto colors = ColorUtils::colors(Qt::blue, Qt::red, componentCount);
90 auto colors = ColorUtils::colors(Qt::blue, Qt::red, componentCount);
91
91
92 // For each component of the data series, creates a QCPGraph to add to the plot
92 // For each component of the data series, creates a QCPGraph to add to the plot
93 for (auto i = 0; i < componentCount; ++i) {
93 for (auto i = 0; i < componentCount; ++i) {
94 auto graph = plot.addGraph();
94 auto graph = plot.addGraph();
95 graph->setPen(QPen{colors.at(i)});
95 graph->setPen(QPen{colors.at(i)});
96
96
97 result.insert({i, graph});
97 result.insert({i, graph});
98 }
98 }
99
99
100 // Axes properties
100 // Axes properties
101 setAxesProperties(dataSeries, plot);
101 setAxesProperties(dataSeries, plot);
102
102
103 plot.replot();
103 plot.replot();
104
104
105 return result;
105 return result;
106 }
106 }
107 };
107 };
108
108
109 /**
109 /**
110 * Struct used to update plottables, depending on the type of the data series from which to update
110 * Struct used to update plottables, depending on the type of the data series from which to update
111 * them
111 * them
112 * @tparam T the data series' type
112 * @tparam T the data series' type
113 * @remarks Default implementation can't update plottables
113 * @remarks Default implementation can't update plottables
114 */
114 */
115 template <typename T, typename Enabled = void>
115 template <typename T, typename Enabled = void>
116 struct PlottablesUpdater {
116 struct PlottablesUpdater {
117 static void updatePlottables(T &, PlottablesMap &, const SqpRange &, bool)
117 static void updatePlottables(T &, PlottablesMap &, const SqpRange &, bool)
118 {
118 {
119 qCCritical(LOG_DataSeries())
119 qCCritical(LOG_DataSeries())
120 << QObject::tr("Can't update plottables: unmanaged data series type");
120 << QObject::tr("Can't update plottables: unmanaged data series type");
121 }
121 }
122 };
122 };
123
123
124 /**
124 /**
125 * Specialization of PlottablesUpdater for scalars and vectors
125 * Specialization of PlottablesUpdater for scalars and vectors
126 * @sa ScalarSeries
126 * @sa ScalarSeries
127 * @sa VectorSeries
127 * @sa VectorSeries
128 */
128 */
129 template <typename T>
129 template <typename T>
130 struct PlottablesUpdater<T,
130 struct PlottablesUpdater<T,
131 typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
131 typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
132 or std::is_base_of<VectorSeries, T>::value> > {
132 or std::is_base_of<VectorSeries, T>::value> > {
133 static void updatePlottables(T &dataSeries, PlottablesMap &plottables, const SqpRange &range,
133 static void updatePlottables(T &dataSeries, PlottablesMap &plottables, const SqpRange &range,
134 bool rescaleAxes)
134 bool rescaleAxes)
135 {
135 {
136 dataSeries.lockRead();
136 dataSeries.lockRead();
137
137
138 // For each plottable to update, resets its data
138 // For each plottable to update, resets its data
139 std::map<int, QSharedPointer<SqpDataContainer> > dataContainers{};
139 std::map<int, QSharedPointer<SqpDataContainer> > dataContainers{};
140 for (const auto &plottable : plottables) {
140 for (const auto &plottable : plottables) {
141 if (auto graph = dynamic_cast<QCPGraph *>(plottable.second)) {
141 if (auto graph = dynamic_cast<QCPGraph *>(plottable.second)) {
142 auto dataContainer = QSharedPointer<SqpDataContainer>::create();
142 auto dataContainer = QSharedPointer<SqpDataContainer>::create();
143 graph->setData(dataContainer);
143 graph->setData(dataContainer);
144
144
145 dataContainers.insert({plottable.first, dataContainer});
145 dataContainers.insert({plottable.first, dataContainer});
146 }
146 }
147 }
147 }
148
148
149 // - Gets the data of the series included in the current range
149 // - Gets the data of the series included in the current range
150 // - Updates each plottable by adding, for each data item, a point that takes x-axis data
150 // - Updates each plottable by adding, for each data item, a point that takes x-axis data
151 // and value data. The correct value is retrieved according to the index of the component
151 // and value data. The correct value is retrieved according to the index of the component
152 auto subDataIts = dataSeries.subData(range.m_TStart, range.m_TEnd);
152 auto subDataIts = dataSeries.xAxisRange(range.m_TStart, range.m_TEnd);
153 for (auto it = subDataIts.first; it != subDataIts.second; ++it) {
153 for (auto it = subDataIts.first; it != subDataIts.second; ++it) {
154 for (const auto &dataContainer : dataContainers) {
154 for (const auto &dataContainer : dataContainers) {
155 auto componentIndex = dataContainer.first;
155 auto componentIndex = dataContainer.first;
156 dataContainer.second->appendGraphData(
156 dataContainer.second->appendGraphData(
157 QCPGraphData(it->x(), it->value(componentIndex)));
157 QCPGraphData(it->x(), it->value(componentIndex)));
158 }
158 }
159 }
159 }
160
160
161 dataSeries.unlock();
161 dataSeries.unlock();
162
162
163 if (!plottables.empty()) {
163 if (!plottables.empty()) {
164 auto plot = plottables.begin()->second->parentPlot();
164 auto plot = plottables.begin()->second->parentPlot();
165
165
166 if (rescaleAxes) {
166 if (rescaleAxes) {
167 plot->rescaleAxes();
167 plot->rescaleAxes();
168 }
168 }
169
169
170 plot->replot();
170 plot->replot();
171 }
171 }
172 }
172 }
173 };
173 };
174
174
175 /**
175 /**
176 * Helper used to create/update plottables
176 * Helper used to create/update plottables
177 */
177 */
178 struct IPlottablesHelper {
178 struct IPlottablesHelper {
179 virtual ~IPlottablesHelper() noexcept = default;
179 virtual ~IPlottablesHelper() noexcept = default;
180 virtual PlottablesMap create(QCustomPlot &plot) const = 0;
180 virtual PlottablesMap create(QCustomPlot &plot) const = 0;
181 virtual void update(PlottablesMap &plottables, const SqpRange &range,
181 virtual void update(PlottablesMap &plottables, const SqpRange &range,
182 bool rescaleAxes = false) const = 0;
182 bool rescaleAxes = false) const = 0;
183 };
183 };
184
184
185 /**
185 /**
186 * Default implementation of IPlottablesHelper, which takes data series to create/update plottables
186 * Default implementation of IPlottablesHelper, which takes data series to create/update plottables
187 * @tparam T the data series' type
187 * @tparam T the data series' type
188 */
188 */
189 template <typename T>
189 template <typename T>
190 struct PlottablesHelper : public IPlottablesHelper {
190 struct PlottablesHelper : public IPlottablesHelper {
191 explicit PlottablesHelper(T &dataSeries) : m_DataSeries{dataSeries} {}
191 explicit PlottablesHelper(T &dataSeries) : m_DataSeries{dataSeries} {}
192
192
193 PlottablesMap create(QCustomPlot &plot) const override
193 PlottablesMap create(QCustomPlot &plot) const override
194 {
194 {
195 return PlottablesCreator<T>::createPlottables(m_DataSeries, plot);
195 return PlottablesCreator<T>::createPlottables(m_DataSeries, plot);
196 }
196 }
197
197
198 void update(PlottablesMap &plottables, const SqpRange &range, bool rescaleAxes) const override
198 void update(PlottablesMap &plottables, const SqpRange &range, bool rescaleAxes) const override
199 {
199 {
200 PlottablesUpdater<T>::updatePlottables(m_DataSeries, plottables, range, rescaleAxes);
200 PlottablesUpdater<T>::updatePlottables(m_DataSeries, plottables, range, rescaleAxes);
201 }
201 }
202
202
203 T &m_DataSeries;
203 T &m_DataSeries;
204 };
204 };
205
205
206 /// Creates IPlottablesHelper according to a data series
206 /// Creates IPlottablesHelper according to a data series
207 std::unique_ptr<IPlottablesHelper> createHelper(std::shared_ptr<IDataSeries> dataSeries) noexcept
207 std::unique_ptr<IPlottablesHelper> createHelper(std::shared_ptr<IDataSeries> dataSeries) noexcept
208 {
208 {
209 if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(dataSeries)) {
209 if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(dataSeries)) {
210 return std::make_unique<PlottablesHelper<ScalarSeries> >(*scalarSeries);
210 return std::make_unique<PlottablesHelper<ScalarSeries> >(*scalarSeries);
211 }
211 }
212 else if (auto vectorSeries = std::dynamic_pointer_cast<VectorSeries>(dataSeries)) {
212 else if (auto vectorSeries = std::dynamic_pointer_cast<VectorSeries>(dataSeries)) {
213 return std::make_unique<PlottablesHelper<VectorSeries> >(*vectorSeries);
213 return std::make_unique<PlottablesHelper<VectorSeries> >(*vectorSeries);
214 }
214 }
215 else {
215 else {
216 return std::make_unique<PlottablesHelper<IDataSeries> >(*dataSeries);
216 return std::make_unique<PlottablesHelper<IDataSeries> >(*dataSeries);
217 }
217 }
218 }
218 }
219
219
220 } // namespace
220 } // namespace
221
221
222 PlottablesMap VisualizationGraphHelper::create(std::shared_ptr<Variable> variable,
222 PlottablesMap VisualizationGraphHelper::create(std::shared_ptr<Variable> variable,
223 QCustomPlot &plot) noexcept
223 QCustomPlot &plot) noexcept
224 {
224 {
225 if (variable) {
225 if (variable) {
226 auto helper = createHelper(variable->dataSeries());
226 auto helper = createHelper(variable->dataSeries());
227 auto plottables = helper->create(plot);
227 auto plottables = helper->create(plot);
228 return plottables;
228 return plottables;
229 }
229 }
230 else {
230 else {
231 qCDebug(LOG_VisualizationGraphHelper())
231 qCDebug(LOG_VisualizationGraphHelper())
232 << QObject::tr("Can't create graph plottables : the variable is null");
232 << QObject::tr("Can't create graph plottables : the variable is null");
233 return PlottablesMap{};
233 return PlottablesMap{};
234 }
234 }
235 }
235 }
236
236
237 void VisualizationGraphHelper::updateData(PlottablesMap &plottables,
237 void VisualizationGraphHelper::updateData(PlottablesMap &plottables,
238 std::shared_ptr<IDataSeries> dataSeries,
238 std::shared_ptr<IDataSeries> dataSeries,
239 const SqpRange &dateTime)
239 const SqpRange &dateTime)
240 {
240 {
241 auto helper = createHelper(dataSeries);
241 auto helper = createHelper(dataSeries);
242 helper->update(plottables, dateTime);
242 helper->update(plottables, dateTime);
243 }
243 }
General Comments 0
You need to be logged in to leave comments. Login now