Isis 3 Programmer Reference
HistogramWidget.cpp
1 #include "HistogramWidget.h"
2 
3 #include <QHBoxLayout>
4 #include <QLayout>
5 #include <QLabel>
6 #include <QString>
7 #include <QColor>
8 
9 #include <qwt_symbol.h>
10 #include <qwt_scale_div.h>
11 #include <qwt_plot_zoomer.h>
12 #include <qwt_scale_engine.h>
13 
14 namespace Isis {
23  HistogramWidget::HistogramWidget(const QString title, const QColor histColor, const QColor stretchColor) :
24  QwtPlot(QwtText(title)) {
25  setCanvasBackground(Qt::white);
26  enableAxis(QwtPlot::yRight);
27  setAxisScale(QwtPlot::xBottom, 0, 255);
28  setAxisLabelRotation(QwtPlot::xBottom, 45);
29  setAxisScale(QwtPlot::yRight, 0, 255);
30 
31  QwtText axisTitle;
32  QFont axisFont;
33  axisFont.setBold(true);
34  axisTitle.setFont(axisFont);
35  axisTitle.setText("Frequency");
36  setAxisTitle(QwtPlot::yLeft, axisTitle);
37  axisTitle.setText("Input (Cube DN)");
38  setAxisTitle(QwtPlot::xBottom, axisTitle);
39  axisTitle.setText("Output");
40  setAxisTitle(QwtPlot::yRight, axisTitle);
41 
42  p_histCurve = new HistogramItem();
43  p_histCurve->setColor(histColor);
44 
46  p_stretchCurve->setYAxis(QwtPlot::yRight);
47  p_stretchCurve->setPen(QPen(QBrush(stretchColor), 2, Qt::DashLine));
48  p_stretchCurve->setSymbol(new QwtSymbol(QwtSymbol::Ellipse, QBrush(stretchColor), QPen(stretchColor), QSize(5, 5)));
49 
50  p_histCurve->attach(this);
51  p_stretchCurve->attach(this);
52 
53  p_zoomer = new QwtPlotZoomer(canvas());
54  p_zoomer->setZoomBase();
55  }
56 
57 
64  std::vector<double> xarray, yarray;
65  for(int i = 0; i < hist.Bins(); i++) {
66  if(hist.BinCount(i) > 0) {
67  xarray.push_back(hist.BinMiddle(i));
68 
69  double freq = (double)hist.BinCount(i) / (double)hist.MaxBinCount();
70  yarray.push_back(freq * 100.0);
71  }
72  }
73 
74  //These are all variables needed in the following for loop.
75  //----------------------------------------------
76  QVector<QwtIntervalSample> intervals(xarray.size());
77  double maxYValue = DBL_MIN;
78  double minYValue = DBL_MAX;
79  // ---------------------------------------------
80 
81  for(unsigned int y = 0; y < yarray.size(); y++) {
82  intervals[y].interval = QwtInterval(xarray[y], xarray[y] + hist.BinSize());
83 
84  intervals[y].value = yarray[y];
85  if(yarray[y] > maxYValue)
86  maxYValue = yarray[y];
87  if(yarray[y] < minYValue)
88  minYValue = yarray[y];
89  }
90 
91  QwtScaleDiv scaleDiv;
92 
93  p_histCurve->setData(QwtIntervalSeriesData(intervals));
94 
95  double min = hist.Minimum();
96  double max = hist.Maximum();
97  int maxMajor = 5;
98  int maxMinor = 20;
99 
100  // Find a good, fixed, axis scale
101  QwtScaleEngine *engine = axisScaleEngine(QwtPlot::xBottom);
102  QwtScaleDiv scale = engine->divideScale(min, max, maxMajor, maxMinor);
103  QwtInterval interval = scale.interval();
104  setAxisScale(QwtPlot::xBottom,
105  interval.minValue() - hist.BinSize(),
106  interval.maxValue() + hist.BinSize());
107  p_zoomer->setZoomBase();
108  }
109 
116  QVector<QPointF> curvePoints(stretch.Pairs());
117  for(int i = 0; i < stretch.Pairs(); i++) {
118  curvePoints[i] = QPointF(stretch.Input(i), stretch.Output(i));
119  }
120 
121  p_stretchCurve->setData(new QwtPointSeriesData(curvePoints));
122  replot();
123  }
124 
130  p_stretchCurve->setData(new QwtPointSeriesData());
131  replot();
132  }
133 }
HistogramItem * p_histCurve
The histogram curve.
double Input(const int index) const
Returns the value of the input side of the stretch pair at the specified index.
Definition: Stretch.cpp:302
double Minimum() const
Returns the absolute minimum double found in all data passed through the AddData method.
Definition: Statistics.cpp:395
void setStretch(Stretch stretch)
Creates a stretch curbe from the given stretch and plots it.
BigInt BinCount(const int index) const
Returns the count at a bin position in the histogram.
Definition: Histogram.cpp:624
double BinMiddle(const int index) const
Returns the value represented by a bin.
Definition: Histogram.cpp:670
int Pairs() const
Returns the number of stretch pairs.
Definition: Stretch.h:176
void clearStretch()
Clears the stretch curve from the plot.
void setColor(const QColor &)
Set the color of the hist.
double Maximum() const
Returns the absolute maximum double found in all data passed through the AddData method.
Definition: Statistics.cpp:416
Container of a cube histogram.
Definition: Histogram.h:86
Pixel value mapper.
Definition: Stretch.h:72
double Output(const int index) const
Returns the value of the output side of the stretch pair at the specified index.
Definition: Stretch.cpp:317
This is the (qwt) plot item for a histogram.
Definition: HistogramItem.h:24
QwtPlotZoomer * p_zoomer
This allows for zooming in/out.
BigInt MaxBinCount() const
Returns the highest bin count.
Definition: Histogram.cpp:714
double BinSize() const
Returns the size of an individual bin.
Definition: Histogram.cpp:691
QwtPlotCurve * p_stretchCurve
The stretch curve.
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
int Bins() const
Returns the number of bins in the histogram.
Definition: Histogram.cpp:704
void setHistogram(const Histogram &hist)
Creates a histogram curve from the given histogram and plots it.
HistogramWidget(const QString title, const QColor histColor=Qt::gray, const QColor stretchColor=Qt::darkGray)
HistogramWidget constructor.
void setData(const QwtIntervalSeriesData &data)
Overridden method to set the data in the histogram.