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