##// END OF EJS Templates
Implements spectrograms display (2)...
Alexandre Leroux -
r903:b5399b2ef3a8
parent child
Show More
@@ -1,269 +1,331
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/SpectrogramSeries.h>
7 #include <Data/SpectrogramSeries.h>
8 #include <Data/VectorSeries.h>
8 #include <Data/VectorSeries.h>
9
9
10 #include <Variable/Variable.h>
10 #include <Variable/Variable.h>
11
11
12 Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper")
12 Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper")
13
13
14 namespace {
14 namespace {
15
15
16 class SqpDataContainer : public QCPGraphDataContainer {
16 class SqpDataContainer : public QCPGraphDataContainer {
17 public:
17 public:
18 void appendGraphData(const QCPGraphData &data) { mData.append(data); }
18 void appendGraphData(const QCPGraphData &data) { mData.append(data); }
19 };
19 };
20
20
21 /**
21 /**
22 * Struct used to create plottables, depending on the type of the data series from which to create
22 * Struct used to create plottables, depending on the type of the data series from which to create
23 * them
23 * them
24 * @tparam T the data series' type
24 * @tparam T the data series' type
25 * @remarks Default implementation can't create plottables
25 * @remarks Default implementation can't create plottables
26 */
26 */
27 template <typename T, typename Enabled = void>
27 template <typename T, typename Enabled = void>
28 struct PlottablesCreator {
28 struct PlottablesCreator {
29 static PlottablesMap createPlottables(T &, QCustomPlot &)
29 static PlottablesMap createPlottables(T &, QCustomPlot &)
30 {
30 {
31 qCCritical(LOG_DataSeries())
31 qCCritical(LOG_DataSeries())
32 << QObject::tr("Can't create plottables: unmanaged data series type");
32 << QObject::tr("Can't create plottables: unmanaged data series type");
33 return {};
33 return {};
34 }
34 }
35 };
35 };
36
36
37 /**
37 /**
38 * Specialization of PlottablesCreator for scalars and vectors
38 * Specialization of PlottablesCreator for scalars and vectors
39 * @sa ScalarSeries
39 * @sa ScalarSeries
40 * @sa VectorSeries
40 * @sa VectorSeries
41 */
41 */
42 template <typename T>
42 template <typename T>
43 struct PlottablesCreator<T,
43 struct PlottablesCreator<T,
44 typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
44 typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
45 or std::is_base_of<VectorSeries, T>::value> > {
45 or std::is_base_of<VectorSeries, T>::value> > {
46 static PlottablesMap createPlottables(T &dataSeries, QCustomPlot &plot)
46 static PlottablesMap createPlottables(T &dataSeries, QCustomPlot &plot)
47 {
47 {
48 PlottablesMap result{};
48 PlottablesMap result{};
49
49
50 // Gets the number of components of the data series
50 // Gets the number of components of the data series
51 dataSeries.lockRead();
51 dataSeries.lockRead();
52 auto componentCount = dataSeries.valuesData()->componentCount();
52 auto componentCount = dataSeries.valuesData()->componentCount();
53 dataSeries.unlock();
53 dataSeries.unlock();
54
54
55 auto colors = ColorUtils::colors(Qt::blue, Qt::red, componentCount);
55 auto colors = ColorUtils::colors(Qt::blue, Qt::red, componentCount);
56
56
57 // For each component of the data series, creates a QCPGraph to add to the plot
57 // For each component of the data series, creates a QCPGraph to add to the plot
58 for (auto i = 0; i < componentCount; ++i) {
58 for (auto i = 0; i < componentCount; ++i) {
59 auto graph = plot.addGraph();
59 auto graph = plot.addGraph();
60 graph->setPen(QPen{colors.at(i)});
60 graph->setPen(QPen{colors.at(i)});
61
61
62 result.insert({i, graph});
62 result.insert({i, graph});
63 }
63 }
64
64
65 plot.replot();
65 plot.replot();
66
66
67 return result;
67 return result;
68 }
68 }
69 };
69 };
70
70
71 /**
71 /**
72 * Specialization of PlottablesCreator for spectrograms
72 * Specialization of PlottablesCreator for spectrograms
73 * @sa SpectrogramSeries
73 * @sa SpectrogramSeries
74 */
74 */
75 template <typename T>
75 template <typename T>
76 struct PlottablesCreator<T,
76 struct PlottablesCreator<T,
77 typename std::enable_if_t<std::is_base_of<SpectrogramSeries, T>::value> > {
77 typename std::enable_if_t<std::is_base_of<SpectrogramSeries, T>::value> > {
78 static PlottablesMap createPlottables(T &dataSeries, QCustomPlot &plot)
78 static PlottablesMap createPlottables(T &dataSeries, QCustomPlot &plot)
79 {
79 {
80 PlottablesMap result{};
80 PlottablesMap result{};
81 result.insert({0, new QCPColorMap{plot.xAxis, plot.yAxis}});
81 result.insert({0, new QCPColorMap{plot.xAxis, plot.yAxis}});
82
82
83 plot.replot();
83 plot.replot();
84
84
85 return result;
85 return result;
86 }
86 }
87 };
87 };
88
88
89 /**
89 /**
90 * Struct used to update plottables, depending on the type of the data series from which to update
90 * Struct used to update plottables, depending on the type of the data series from which to update
91 * them
91 * them
92 * @tparam T the data series' type
92 * @tparam T the data series' type
93 * @remarks Default implementation can't update plottables
93 * @remarks Default implementation can't update plottables
94 */
94 */
95 template <typename T, typename Enabled = void>
95 template <typename T, typename Enabled = void>
96 struct PlottablesUpdater {
96 struct PlottablesUpdater {
97 static void setPlotYAxisRange(T &, const SqpRange &, QCustomPlot &)
97 static void setPlotYAxisRange(T &, const SqpRange &, QCustomPlot &)
98 {
98 {
99 qCCritical(LOG_DataSeries())
99 qCCritical(LOG_DataSeries())
100 << QObject::tr("Can't set plot y-axis range: unmanaged data series type");
100 << QObject::tr("Can't set plot y-axis range: unmanaged data series type");
101 }
101 }
102
102
103 static void updatePlottables(T &, PlottablesMap &, const SqpRange &, bool)
103 static void updatePlottables(T &, PlottablesMap &, const SqpRange &, bool)
104 {
104 {
105 qCCritical(LOG_DataSeries())
105 qCCritical(LOG_DataSeries())
106 << QObject::tr("Can't update plottables: unmanaged data series type");
106 << QObject::tr("Can't update plottables: unmanaged data series type");
107 }
107 }
108 };
108 };
109
109
110 /**
110 /**
111 * Specialization of PlottablesUpdater for scalars and vectors
111 * Specialization of PlottablesUpdater for scalars and vectors
112 * @sa ScalarSeries
112 * @sa ScalarSeries
113 * @sa VectorSeries
113 * @sa VectorSeries
114 */
114 */
115 template <typename T>
115 template <typename T>
116 struct PlottablesUpdater<T,
116 struct PlottablesUpdater<T,
117 typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
117 typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
118 or std::is_base_of<VectorSeries, T>::value> > {
118 or std::is_base_of<VectorSeries, T>::value> > {
119 static void setPlotYAxisRange(T &dataSeries, const SqpRange &xAxisRange, QCustomPlot &plot)
119 static void setPlotYAxisRange(T &dataSeries, const SqpRange &xAxisRange, QCustomPlot &plot)
120 {
120 {
121 auto minValue = 0., maxValue = 0.;
121 auto minValue = 0., maxValue = 0.;
122
122
123 dataSeries.lockRead();
123 dataSeries.lockRead();
124 auto valuesBounds = dataSeries.valuesBounds(xAxisRange.m_TStart, xAxisRange.m_TEnd);
124 auto valuesBounds = dataSeries.valuesBounds(xAxisRange.m_TStart, xAxisRange.m_TEnd);
125 auto end = dataSeries.cend();
125 auto end = dataSeries.cend();
126 if (valuesBounds.first != end && valuesBounds.second != end) {
126 if (valuesBounds.first != end && valuesBounds.second != end) {
127 auto rangeValue = [](const auto &value) { return std::isnan(value) ? 0. : value; };
127 auto rangeValue = [](const auto &value) { return std::isnan(value) ? 0. : value; };
128
128
129 minValue = rangeValue(valuesBounds.first->minValue());
129 minValue = rangeValue(valuesBounds.first->minValue());
130 maxValue = rangeValue(valuesBounds.second->maxValue());
130 maxValue = rangeValue(valuesBounds.second->maxValue());
131 }
131 }
132 dataSeries.unlock();
132 dataSeries.unlock();
133
133
134 plot.yAxis->setRange(QCPRange{minValue, maxValue});
134 plot.yAxis->setRange(QCPRange{minValue, maxValue});
135 }
135 }
136
136
137 static void updatePlottables(T &dataSeries, PlottablesMap &plottables, const SqpRange &range,
137 static void updatePlottables(T &dataSeries, PlottablesMap &plottables, const SqpRange &range,
138 bool rescaleAxes)
138 bool rescaleAxes)
139 {
139 {
140
140
141 // For each plottable to update, resets its data
141 // For each plottable to update, resets its data
142 std::map<int, QSharedPointer<SqpDataContainer> > dataContainers{};
142 std::map<int, QSharedPointer<SqpDataContainer> > dataContainers{};
143 for (const auto &plottable : plottables) {
143 for (const auto &plottable : plottables) {
144 if (auto graph = dynamic_cast<QCPGraph *>(plottable.second)) {
144 if (auto graph = dynamic_cast<QCPGraph *>(plottable.second)) {
145 auto dataContainer = QSharedPointer<SqpDataContainer>::create();
145 auto dataContainer = QSharedPointer<SqpDataContainer>::create();
146 graph->setData(dataContainer);
146 graph->setData(dataContainer);
147
147
148 dataContainers.insert({plottable.first, dataContainer});
148 dataContainers.insert({plottable.first, dataContainer});
149 }
149 }
150 }
150 }
151 dataSeries.lockRead();
151 dataSeries.lockRead();
152
152
153 // - Gets the data of the series included in the current range
153 // - Gets the data of the series included in the current range
154 // - Updates each plottable by adding, for each data item, a point that takes x-axis data
154 // - Updates each plottable by adding, for each data item, a point that takes x-axis data
155 // and value data. The correct value is retrieved according to the index of the component
155 // and value data. The correct value is retrieved according to the index of the component
156 auto subDataIts = dataSeries.xAxisRange(range.m_TStart, range.m_TEnd);
156 auto subDataIts = dataSeries.xAxisRange(range.m_TStart, range.m_TEnd);
157 for (auto it = subDataIts.first; it != subDataIts.second; ++it) {
157 for (auto it = subDataIts.first; it != subDataIts.second; ++it) {
158 for (const auto &dataContainer : dataContainers) {
158 for (const auto &dataContainer : dataContainers) {
159 auto componentIndex = dataContainer.first;
159 auto componentIndex = dataContainer.first;
160 dataContainer.second->appendGraphData(
160 dataContainer.second->appendGraphData(
161 QCPGraphData(it->x(), it->value(componentIndex)));
161 QCPGraphData(it->x(), it->value(componentIndex)));
162 }
162 }
163 }
163 }
164
164
165 dataSeries.unlock();
165 dataSeries.unlock();
166
166
167 if (!plottables.empty()) {
167 if (!plottables.empty()) {
168 auto plot = plottables.begin()->second->parentPlot();
168 auto plot = plottables.begin()->second->parentPlot();
169
169
170 if (rescaleAxes) {
170 if (rescaleAxes) {
171 plot->rescaleAxes();
171 plot->rescaleAxes();
172 }
172 }
173
173
174 plot->replot();
174 plot->replot();
175 }
175 }
176 }
176 }
177 };
177 };
178
178
179 /**
179 /**
180 * Specialization of PlottablesUpdater for spectrograms
181 * @sa SpectrogramSeries
182 */
183 template <typename T>
184 struct PlottablesUpdater<T,
185 typename std::enable_if_t<std::is_base_of<SpectrogramSeries, T>::value> > {
186 static void updatePlottables(T &dataSeries, PlottablesMap &plottables, const SqpRange &range,
187 bool rescaleAxes)
188 {
189 if (plottables.empty()) {
190 qCDebug(LOG_VisualizationGraphHelper())
191 << QObject::tr("Can't update spectrogram: no colormap has been associated");
192 return;
193 }
194
195 // Gets the colormap to update (normally there is only one colormap)
196 Q_ASSERT(plottables.size() == 1);
197 auto colormap = dynamic_cast<QCPColorMap *>(plottables.at(0));
198 Q_ASSERT(colormap != nullptr);
199
200 dataSeries.lockRead();
201
202 auto its = dataSeries.xAxisRange(range.m_TStart, range.m_TEnd);
203 /// @todo ALX: use iterators here
204 auto yAxis = dataSeries.yAxis();
205
206 // Gets properties of x-axis and y-axis to set size and range of the colormap
207 auto nbX = std::distance(its.first, its.second);
208 auto xMin = nbX != 0 ? its.first->x() : 0.;
209 auto xMax = nbX != 0 ? (its.second - 1)->x() : 0.;
210
211 auto nbY = yAxis.size();
212 auto yMin = 0., yMax = 0.;
213 if (nbY != 0) {
214 std::tie(yMin, yMax) = yAxis.bounds();
215 }
216
217 colormap->data()->setSize(nbX, nbY);
218 colormap->data()->setRange(QCPRange{xMin, xMax}, QCPRange{yMin, yMax});
219
220 // Sets values
221 auto xIndex = 0;
222 for (auto it = its.first; it != its.second; ++it, ++xIndex) {
223 for (auto yIndex = 0; yIndex < nbY; ++yIndex) {
224 colormap->data()->setCell(xIndex, yIndex, it->value(yIndex));
225 }
226 }
227
228 dataSeries.unlock();
229
230 // Rescales axes
231 auto plot = colormap->parentPlot();
232
233 if (rescaleAxes) {
234 plot->rescaleAxes();
235 }
236
237 plot->replot();
238 }
239 };
240
241 /**
180 * Helper used to create/update plottables
242 * Helper used to create/update plottables
181 */
243 */
182 struct IPlottablesHelper {
244 struct IPlottablesHelper {
183 virtual ~IPlottablesHelper() noexcept = default;
245 virtual ~IPlottablesHelper() noexcept = default;
184 virtual PlottablesMap create(QCustomPlot &plot) const = 0;
246 virtual PlottablesMap create(QCustomPlot &plot) const = 0;
185 virtual void setYAxisRange(const SqpRange &xAxisRange, QCustomPlot &plot) const = 0;
247 virtual void setYAxisRange(const SqpRange &xAxisRange, QCustomPlot &plot) const = 0;
186 virtual void update(PlottablesMap &plottables, const SqpRange &range,
248 virtual void update(PlottablesMap &plottables, const SqpRange &range,
187 bool rescaleAxes = false) const = 0;
249 bool rescaleAxes = false) const = 0;
188 };
250 };
189
251
190 /**
252 /**
191 * Default implementation of IPlottablesHelper, which takes data series to create/update plottables
253 * Default implementation of IPlottablesHelper, which takes data series to create/update plottables
192 * @tparam T the data series' type
254 * @tparam T the data series' type
193 */
255 */
194 template <typename T>
256 template <typename T>
195 struct PlottablesHelper : public IPlottablesHelper {
257 struct PlottablesHelper : public IPlottablesHelper {
196 explicit PlottablesHelper(T &dataSeries) : m_DataSeries{dataSeries} {}
258 explicit PlottablesHelper(T &dataSeries) : m_DataSeries{dataSeries} {}
197
259
198 PlottablesMap create(QCustomPlot &plot) const override
260 PlottablesMap create(QCustomPlot &plot) const override
199 {
261 {
200 return PlottablesCreator<T>::createPlottables(m_DataSeries, plot);
262 return PlottablesCreator<T>::createPlottables(m_DataSeries, plot);
201 }
263 }
202
264
203 void update(PlottablesMap &plottables, const SqpRange &range, bool rescaleAxes) const override
265 void update(PlottablesMap &plottables, const SqpRange &range, bool rescaleAxes) const override
204 {
266 {
205 PlottablesUpdater<T>::updatePlottables(m_DataSeries, plottables, range, rescaleAxes);
267 PlottablesUpdater<T>::updatePlottables(m_DataSeries, plottables, range, rescaleAxes);
206 }
268 }
207
269
208 void setYAxisRange(const SqpRange &xAxisRange, QCustomPlot &plot) const override
270 void setYAxisRange(const SqpRange &xAxisRange, QCustomPlot &plot) const override
209 {
271 {
210 return PlottablesUpdater<T>::setPlotYAxisRange(m_DataSeries, xAxisRange, plot);
272 return PlottablesUpdater<T>::setPlotYAxisRange(m_DataSeries, xAxisRange, plot);
211 }
273 }
212
274
213 T &m_DataSeries;
275 T &m_DataSeries;
214 };
276 };
215
277
216 /// Creates IPlottablesHelper according to a data series
278 /// Creates IPlottablesHelper according to a data series
217 std::unique_ptr<IPlottablesHelper> createHelper(std::shared_ptr<IDataSeries> dataSeries) noexcept
279 std::unique_ptr<IPlottablesHelper> createHelper(std::shared_ptr<IDataSeries> dataSeries) noexcept
218 {
280 {
219 if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(dataSeries)) {
281 if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(dataSeries)) {
220 return std::make_unique<PlottablesHelper<ScalarSeries> >(*scalarSeries);
282 return std::make_unique<PlottablesHelper<ScalarSeries> >(*scalarSeries);
221 }
283 }
222 else if (auto spectrogramSeries = std::dynamic_pointer_cast<SpectrogramSeries>(dataSeries)) {
284 else if (auto spectrogramSeries = std::dynamic_pointer_cast<SpectrogramSeries>(dataSeries)) {
223 return std::make_unique<PlottablesHelper<SpectrogramSeries> >(*spectrogramSeries);
285 return std::make_unique<PlottablesHelper<SpectrogramSeries> >(*spectrogramSeries);
224 }
286 }
225 else if (auto vectorSeries = std::dynamic_pointer_cast<VectorSeries>(dataSeries)) {
287 else if (auto vectorSeries = std::dynamic_pointer_cast<VectorSeries>(dataSeries)) {
226 return std::make_unique<PlottablesHelper<VectorSeries> >(*vectorSeries);
288 return std::make_unique<PlottablesHelper<VectorSeries> >(*vectorSeries);
227 }
289 }
228 else {
290 else {
229 return std::make_unique<PlottablesHelper<IDataSeries> >(*dataSeries);
291 return std::make_unique<PlottablesHelper<IDataSeries> >(*dataSeries);
230 }
292 }
231 }
293 }
232
294
233 } // namespace
295 } // namespace
234
296
235 PlottablesMap VisualizationGraphHelper::create(std::shared_ptr<Variable> variable,
297 PlottablesMap VisualizationGraphHelper::create(std::shared_ptr<Variable> variable,
236 QCustomPlot &plot) noexcept
298 QCustomPlot &plot) noexcept
237 {
299 {
238 if (variable) {
300 if (variable) {
239 auto helper = createHelper(variable->dataSeries());
301 auto helper = createHelper(variable->dataSeries());
240 auto plottables = helper->create(plot);
302 auto plottables = helper->create(plot);
241 return plottables;
303 return plottables;
242 }
304 }
243 else {
305 else {
244 qCDebug(LOG_VisualizationGraphHelper())
306 qCDebug(LOG_VisualizationGraphHelper())
245 << QObject::tr("Can't create graph plottables : the variable is null");
307 << QObject::tr("Can't create graph plottables : the variable is null");
246 return PlottablesMap{};
308 return PlottablesMap{};
247 }
309 }
248 }
310 }
249
311
250 void VisualizationGraphHelper::setYAxisRange(std::shared_ptr<Variable> variable,
312 void VisualizationGraphHelper::setYAxisRange(std::shared_ptr<Variable> variable,
251 QCustomPlot &plot) noexcept
313 QCustomPlot &plot) noexcept
252 {
314 {
253 if (variable) {
315 if (variable) {
254 auto helper = createHelper(variable->dataSeries());
316 auto helper = createHelper(variable->dataSeries());
255 helper->setYAxisRange(variable->range(), plot);
317 helper->setYAxisRange(variable->range(), plot);
256 }
318 }
257 else {
319 else {
258 qCDebug(LOG_VisualizationGraphHelper())
320 qCDebug(LOG_VisualizationGraphHelper())
259 << QObject::tr("Can't set y-axis range of plot: the variable is null");
321 << QObject::tr("Can't set y-axis range of plot: the variable is null");
260 }
322 }
261 }
323 }
262
324
263 void VisualizationGraphHelper::updateData(PlottablesMap &plottables,
325 void VisualizationGraphHelper::updateData(PlottablesMap &plottables,
264 std::shared_ptr<IDataSeries> dataSeries,
326 std::shared_ptr<IDataSeries> dataSeries,
265 const SqpRange &dateTime)
327 const SqpRange &dateTime)
266 {
328 {
267 auto helper = createHelper(dataSeries);
329 auto helper = createHelper(dataSeries);
268 helper->update(plottables, dateTime);
330 helper->update(plottables, dateTime);
269 }
331 }
General Comments 0
You need to be logged in to leave comments. Login now