QwtFunctionPlot.cc 8.41 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision: 13770 $                                                       *
*   $LastChangedBy: moebius $                                                *
*   $Date: 2012-02-13 16:19:41 +0100 (Mo, 13. Feb 2012) $                     *
*                                                                            *
\*===========================================================================*/

//=============================================================================
//
//  CLASS QtFunctionPlot - IMPLEMENTATION
//
//=============================================================================

//== INCLUDES =================================================================

#ifdef WITH_QWT

#include "QwtFunctionPlot.hh"

#include <iostream>
#include <algorithm>

#include <QPen>

60 61 62
#include <QLineEdit>
#include <QLabel>
#include <QPainter>
Jan Möbius's avatar
Jan Möbius committed
63 64 65 66 67 68 69 70
#include <qwt_curve_fitter.h>
#include <qwt_plot_panner.h>
#include <qwt_symbol.h>
#include <qwt_plot_histogram.h>

#include <ACG/Utils/ColorCoder.hh>


71 72
#include <cfloat>
#include <cmath>
Jan Möbius's avatar
Jan Möbius committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

//== NAMESPACES ===============================================================

//== IMPLEMENTATION ==========================================================


/// Default constructor
QwtFunctionPlot::QwtFunctionPlot(QWidget* _parent) :
    QDialog( _parent ),
    Ui::QwtFunctionPlotBase(),
    plot_zoomer_(0),
    clampMinMarker_(0),
    minSymbol_(0),
    clampMaxMarker_(0),
    maxSymbol_(0),
    min_(FLT_MAX),
    max_(FLT_MIN)
{
  setupUi( this );

93 94
  qwtPlot->setAxisTitle(QwtPlot::yLeft,   "count" );

Jan Möbius's avatar
Jan Möbius committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108
  plot_zoomer_ = new QwtPlotZoomer( qwtPlot->canvas());
  plot_zoomer_->initKeyPattern();
  
  QwtPlotPanner *panner = new QwtPlotPanner( qwtPlot->canvas() );
  panner->setMouseButton( Qt::MidButton );
  
  // delete widget on close
  setAttribute(Qt::WA_DeleteOnClose, true);

  histogram_ = new Histogram();
}

//------------------------------------------------------------------------------

Jan Möbius's avatar
Jan Möbius committed
109
void QwtFunctionPlot::setFunction(const std::vector<double>& _values)
Jan Möbius's avatar
Jan Möbius committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
{
  values_ = _values;
}

//------------------------------------------------------------------------------

void QwtFunctionPlot::replot()
{
  //create intervals
  const int intervalCount = 100;

  QVector<QwtIntervalSample> intervals(intervalCount);
  std::vector< QColor > colors;

  double realMin = FLT_MAX;
  double realMax = -FLT_MAX;

  for ( unsigned int i=0; i < values_.size(); i++) {
    realMin = std::min(realMin,values_[i]);
    realMax = std::max(realMax,values_[i]);
  }

  double pos   = realMin;
  double width = ( realMax - realMin ) / intervalCount;

  QColor lastColor = Qt::black;

  ACG::ColorCoder cCoder(std::max(min_,realMin),std::min(max_,realMax));

  for ( int i = 0; i < (int)intervals.size(); i++ )
  {

    intervals[i] = QwtIntervalSample(0.0,pos, pos + width);
    pos += width;

    //compute a color for the given interval
    const double intervalCenter = pos + (width/2.0);

    colors.push_back( cCoder.color_qcolor(intervalCenter) );
    lastColor = colors.back();
  }

  // sort values into intervals
  // Could be more efficient when we calculate the correct interval based on the above loop directly
  for ( uint i=0; i < values_.size(); i++)
    for ( int j = 0; j < (int)intervals.size(); j++ )
      if ( intervals[j].interval.contains( values_[i]  ) ) {
        intervals[j].value++;
        break;
      }

  //get max Count for scaling the y-axis
  double maxCount = 0;

  for ( int i = 0; i < (int)intervals.size(); i++ )
    maxCount = std::max(maxCount, intervals[i].value);


  QwtIntervalSeriesData* data = new QwtIntervalSeriesData(intervals);
  histogram_->setData(data);
  histogram_->setColors(colors);
  histogram_->attach(qwtPlot);

  qwtPlot->setAxisScale(QwtPlot::yLeft, 0.0, maxCount);
  qwtPlot->setAxisScale(QwtPlot::xBottom, realMin, realMax);

  //define this scaling as the zoomBase
  plot_zoomer_->setZoomBase();

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
  //Mark the clamp values in the histogram.
  if ( min_ > realMin  ) {
    if ( ! clampMinMarker_ ) {
      clampMinMarker_ = new QwtPlotMarker();
      minSymbol_ = new QwtSymbol(QwtSymbol::VLine);
      minSymbol_->setColor(cCoder.color_qcolor(std::max(min_,realMin)));
      minSymbol_->setSize(200,1000);
      QPen pen = minSymbol_->pen();
      pen.setWidth(3);
      minSymbol_->setPen(pen);
      clampMinMarker_->setSymbol(minSymbol_);
      clampMinMarker_->attach(qwtPlot);
      clampMinMarker_->show();
    }

    // Draw at right boundary if greater than the maximal value we get from the function
    clampMinMarker_->setXValue(std::max(min_,realMin));

  } else {
    if ( clampMinMarker_ ) {
      clampMinMarker_->detach();
      delete clampMinMarker_;
      clampMinMarker_ = 0;
    }
  }
Jan Möbius's avatar
Jan Möbius committed
204

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
  //Mark the clamp values in the histogram.
  if ( max_ < realMax ) {
    if ( ! clampMaxMarker_ ) {
      clampMaxMarker_ = new QwtPlotMarker();
      maxSymbol_ = new QwtSymbol(QwtSymbol::VLine);
      maxSymbol_->setColor(cCoder.color_qcolor(std::min(max_,realMax)));
      maxSymbol_->setSize(200,1000);
      QPen pen = maxSymbol_->pen();
      pen.setWidth(3);
      maxSymbol_->setPen(pen);
      clampMaxMarker_->setSymbol(maxSymbol_);
      clampMaxMarker_->attach(qwtPlot);
      clampMaxMarker_->show();
    }

    // Draw at right boundary if greater than the maximal value we get from the function
    clampMaxMarker_->setXValue(std::min(max_,realMax));

  } else {
    if ( clampMaxMarker_ ) {
      clampMaxMarker_->detach();
      delete clampMaxMarker_;
      clampMaxMarker_ = 0;
    }
  }
Jan Möbius's avatar
Jan Möbius committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

  // an plot it
  qwtPlot->replot();
}

void QwtFunctionPlot::setMinMax(double _min, double _max) {
  min_ = _min;
  max_ = _max;
}



//=============================================================================
//=============================================================================

#endif // WITH_QWT