qwt_matrix_raster_data.cpp
Go to the documentation of this file.
1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
2  * Qwt Widget Library
3  * Copyright (C) 1997 Josef Wilgen
4  * Copyright (C) 2002 Uwe Rathmann
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the Qwt License, Version 1.0
8  *****************************************************************************/
9 
10 #include "qwt_matrix_raster_data.h"
11 #include <qnumeric.h>
12 #include <qmath.h>
13 
15 {
16 public:
19  numColumns(0)
20  {
21  }
22 
23  inline double value(int row, int col) const
24  {
25  return values.data()[ row * numColumns + col ];
26  }
27 
30 
31  QVector<double> values;
33  int numRows;
34 
35  double dx;
36  double dy;
37 };
38 
41 {
42  d_data = new PrivateData();
43  update();
44 }
45 
48 {
49  delete d_data;
50 }
51 
59 {
60  d_data->resampleMode = mode;
61 }
62 
68 {
69  return d_data->resampleMode;
70 }
71 
89  Qt::Axis axis, const QwtInterval &interval )
90 {
91  if ( axis >= 0 && axis <= 2 )
92  {
93  d_data->intervals[axis] = interval;
94  update();
95  }
96 }
97 
103 {
104  if ( axis >= 0 && axis <= 2 )
105  return d_data->intervals[ axis ];
106 
107  return QwtInterval();
108 }
109 
124  const QVector<double> &values, int numColumns )
125 {
126  d_data->values = values;
127  d_data->numColumns = qMax( numColumns, 0 );
128  update();
129 }
130 
135 const QVector<double> QwtMatrixRasterData::valueMatrix() const
136 {
137  return d_data->values;
138 }
139 
149 void QwtMatrixRasterData::setValue( int row, int col, double value )
150 {
151  if ( row >= 0 && row < d_data->numRows &&
152  col >= 0 && col < d_data->numColumns )
153  {
154  const int index = row * d_data->numColumns + col;
155  d_data->values.data()[ index ] = value;
156  }
157 }
158 
164 {
165  return d_data->numColumns;
166 }
167 
173 {
174  return d_data->numRows;
175 }
176 
197 QRectF QwtMatrixRasterData::pixelHint( const QRectF &area ) const
198 {
199  Q_UNUSED( area )
200 
201  QRectF rect;
203  {
204  const QwtInterval intervalX = interval( Qt::XAxis );
205  const QwtInterval intervalY = interval( Qt::YAxis );
206  if ( intervalX.isValid() && intervalY.isValid() )
207  {
208  rect = QRectF( intervalX.minValue(), intervalY.minValue(),
209  d_data->dx, d_data->dy );
210  }
211  }
212 
213  return rect;
214 }
215 
224 double QwtMatrixRasterData::value( double x, double y ) const
225 {
226  const QwtInterval xInterval = interval( Qt::XAxis );
227  const QwtInterval yInterval = interval( Qt::YAxis );
228 
229  if ( !( xInterval.contains(x) && yInterval.contains(y) ) )
230  return qQNaN();
231 
232  double value;
233 
234  switch( d_data->resampleMode )
235  {
237  {
238  int col1 = qRound( (x - xInterval.minValue() ) / d_data->dx ) - 1;
239  int row1 = qRound( (y - yInterval.minValue() ) / d_data->dy ) - 1;
240  int col2 = col1 + 1;
241  int row2 = row1 + 1;
242 
243  if ( col1 < 0 )
244  col1 = col2;
245  else if ( col2 >= static_cast<int>( d_data->numColumns ) )
246  col2 = col1;
247 
248  if ( row1 < 0 )
249  row1 = row2;
250  else if ( row2 >= static_cast<int>( d_data->numRows ) )
251  row2 = row1;
252 
253  const double v11 = d_data->value( row1, col1 );
254  const double v21 = d_data->value( row1, col2 );
255  const double v12 = d_data->value( row2, col1 );
256  const double v22 = d_data->value( row2, col2 );
257 
258  const double x2 = xInterval.minValue() +
259  ( col2 + 0.5 ) * d_data->dx;
260  const double y2 = yInterval.minValue() +
261  ( row2 + 0.5 ) * d_data->dy;
262 
263  const double rx = ( x2 - x ) / d_data->dx;
264  const double ry = ( y2 - y ) / d_data->dy;
265 
266  const double vr1 = rx * v11 + ( 1.0 - rx ) * v21;
267  const double vr2 = rx * v12 + ( 1.0 - rx ) * v22;
268 
269  value = ry * vr1 + ( 1.0 - ry ) * vr2;
270 
271  break;
272  }
273  case NearestNeighbour:
274  default:
275  {
276  int row = int( (y - yInterval.minValue() ) / d_data->dy );
277  int col = int( (x - xInterval.minValue() ) / d_data->dx );
278 
279  // In case of intervals, where the maximum is included
280  // we get out of bound for row/col, when the value for the
281  // maximum is requested. Instead we return the value
282  // from the last row/col
283 
284  if ( row >= d_data->numRows )
285  row = d_data->numRows - 1;
286 
287  if ( col >= d_data->numColumns )
288  col = d_data->numColumns - 1;
289 
290  value = d_data->value( row, col );
291  }
292  }
293 
294  return value;
295 }
296 
298 {
299  d_data->numRows = 0;
300  d_data->dx = 0.0;
301  d_data->dy = 0.0;
302 
303  if ( d_data->numColumns > 0 )
304  {
306 
307  const QwtInterval xInterval = interval( Qt::XAxis );
308  const QwtInterval yInterval = interval( Qt::YAxis );
309  if ( xInterval.isValid() )
310  d_data->dx = xInterval.width() / d_data->numColumns;
311  if ( yInterval.isValid() )
312  d_data->dy = yInterval.width() / d_data->numRows;
313  }
314 }
bool contains(double value) const
A class representing an interval.
Definition: qwt_interval.h:26
virtual QRectF pixelHint(const QRectF &) const
Calculate the pixel hint.
double minValue() const
Definition: qwt_interval.h:193
void setResampleMode(ResampleMode mode)
Set the resampling algorithm.
void setInterval(Qt::Axis, const QwtInterval &)
Assign the bounding interval for an axis.
ResampleMode
Resampling algorithm The default setting is NearestNeighbour;.
QwtInterval interval(Qt::Axis axis) const
double value(int row, int col) const
virtual double value(double x, double y) const
virtual ~QwtMatrixRasterData()
Destructor.
A class representing a matrix of values as raster data.
bool isValid() const
Definition: qwt_interval.h:211
void setValue(int row, int col, double value)
Change a single value in the matrix.
const QVector< double > valueMatrix() const
ResampleMode resampleMode() const
void setValueMatrix(const QVector< double > &values, int numColumns)
Assign a value matrix.
double width() const
Return the width of an interval.
Definition: qwt_interval.h:228
QwtMatrixRasterData()
Constructor.
QwtMatrixRasterData::ResampleMode resampleMode


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17