dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
histogramplotdata.cpp
Go to the documentation of this file.
1 
12 /*
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  * for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with this program; if not, see <http://www.gnu.org/licenses/>
25  */
26 
27 #include <QDebug>
28 #include <math.h>
29 
34 #include "scopegadgetwidget.h"
35 
36 #include "qwt/src/qwt.h"
37 #include "qwt/src/qwt_plot_histogram.h"
38 
39 #define MAX_NUMBER_OF_INTERVALS 1000
40 
48 HistogramData::HistogramData(QString uavObject, QString uavField, double binWidth,
49  uint numberOfBins)
50  : Plot2dData(uavObject, uavField)
51  , histogram(nullptr)
52  , histogramBins(nullptr)
53  , histogramInterval(nullptr)
54  , intervalSeriesData(nullptr)
55 {
56  this->binWidth = binWidth;
57  this->numberOfBins = numberOfBins;
58  scalePower = 1;
59 
60  // Create histogram data set
61  histogramBins = new QVector<QwtIntervalSample>();
62  histogramInterval = new QVector<QwtInterval>();
63 
64  // Generate the interval series
65  intervalSeriesData = new QwtIntervalSeriesData(*histogramBins);
66 }
67 
72 void HistogramData::plotNewData(PlotData *plot2dData, ScopeConfig *scopeConfig,
73  ScopeGadgetWidget *scopeGadgetWidget)
74 {
75  Q_UNUSED(plot2dData);
76  Q_UNUSED(scopeGadgetWidget);
77  Q_UNUSED(scopeConfig);
78 
79  // Plot new data
80  histogram->setData(intervalSeriesData);
81  intervalSeriesData->setSamples(*histogramBins);
82 }
83 
90 {
91 
92  // Empty histogram data set
93  xData->clear();
94  yData->clear();
95 
96  if (uavObjectName == obj->getName()) {
97 
98  // Get the field of interest
99  UAVObjectField *field = obj->getField(uavFieldName);
100 
101  // Bad place to do this
102  double step = binWidth;
103  if (step < 1e-6) // Don't allow step size to be 0.
104  step = 1e-6;
105 
106  if (numberOfBins > MAX_NUMBER_OF_INTERVALS)
107  numberOfBins = MAX_NUMBER_OF_INTERVALS;
108 
109  if (field) {
110  double currentValue =
111  valueAsDouble(obj, field, haveSubField, uavSubFieldName) * pow(10, scalePower);
112 
113  // Extend interval, if necessary
114  if (!histogramInterval->empty()) {
115  while (currentValue < histogramInterval->front().minValue()
116  && histogramInterval->size() <= (int)numberOfBins) {
117  histogramInterval->prepend(
118  QwtInterval(histogramInterval->front().minValue() - step,
119  histogramInterval->front().minValue()));
120  histogramBins->prepend(QwtIntervalSample(0, histogramInterval->front()));
121  }
122 
123  while (currentValue > histogramInterval->back().maxValue()
124  && histogramInterval->size() <= (int)numberOfBins) {
125  histogramInterval->append(
126  QwtInterval(histogramInterval->back().maxValue(),
127  histogramInterval->back().maxValue() + step));
128  histogramBins->append(QwtIntervalSample(0, histogramInterval->back()));
129  }
130 
131  // If the histogram reaches its max size, pop one off the end and return
132  // This is a graceful way not to lock up the GCS if the bin width
133  // is inappropriate, or if there is an extremely distant outlier.
134  if (histogramInterval->size() > (int)numberOfBins) {
135  histogramBins->pop_back();
136  histogramInterval->pop_back();
137  return false;
138  }
139 
140  // Test all intervals. This isn't particularly effecient, especially if we have just
141  // extended the interval and thus know for sure that the point lies on the
142  // extremity.
143  // On top of that, some kind of search by bisection would be better.
144  for (int i = 0; i < histogramInterval->size(); i++) {
145  if (histogramInterval->at(i).contains(currentValue)) {
146  histogramBins->replace(i, QwtIntervalSample(histogramBins->at(i).value + 1,
147  histogramInterval->at(i)));
148  break;
149  }
150  }
151  } else {
152  // Create first interval
153  double tmp = 0;
154  if (tmp < currentValue) {
155  while (tmp < currentValue) {
156  tmp += step;
157  }
158  histogramInterval->append(QwtInterval(tmp - step, tmp));
159  } else {
160  while (tmp > step) {
161  tmp -= step;
162  }
163  histogramInterval->append(QwtInterval(tmp, tmp + step));
164  }
165 
166  histogramBins->append(QwtIntervalSample(0, histogramInterval->front()));
167  }
168 
169  return true;
170  }
171  }
172 
173  return false;
174 }
175 
180 {
181  histogram->detach();
182 
183  // Delete data bins
184  delete histogramInterval;
185  delete histogramBins;
186 
187  // Don't delete intervalSeriesData, this is done by the histogram's destructor
188  /* delete intervalSeriesData; */
189 
190  // Delete histogram (also deletes intervalSeriesData)
191  delete histogram;
192 
193  delete histogramData;
194 }
195 
200 {
201  histogramBins->clear();
202  histogramInterval->clear();
203 }
QVector< double > * xData
Definition: plotdata.h:90
QString uavFieldName
Definition: plotdata.h:100
The Plot2dData class Base class that keeps the data for each curve in the plot.
Definition: plotdata2d.h:39
virtual void deletePlots(PlotData *)
HistogramScopeConfig::deletePlots Delete all plot data.
for i
Definition: OPPlots.m:140
QVector< double > * yData
Definition: plotdata.h:91
virtual void plotNewData(PlotData *, ScopeConfig *, ScopeGadgetWidget *)
HistogramScopeConfig::plotNewData Update plot with new data.
UAVObjectField * getField(const QString &name)
Definition: uavobject.cpp:236
The ScopeConfig class The parent class for scope configuration classes data sources.
Definition: scopesconfig.h:56
QString uavSubFieldName
Definition: plotdata.h:101
int scalePower
Definition: plotdata.h:104
QString getName()
Definition: uavobject.cpp:131
bool haveSubField
Definition: plotdata.h:102
HistogramData(QString uavObject, QString uavField, double binWidth, uint numberOfBins)
HistogramData::HistogramData.
Scope Plugin Gadget Widget.
void clearPlots()
HistogramScopeConfig::clearPlots Clear all plot data.
bool append(UAVObject *obj)
HistogramData::append Appends data to histogram.
double valueAsDouble(UAVObject *obj, UAVObjectField *field, bool haveSubField, QString uavSubFieldName)
valueAsDouble Fetch the value from the UAVO and return it as a double
Definition: plotdata.cpp:148
QString uavObjectName
Definition: plotdata.h:99
e
Definition: OPPlots.m:99