# Thread: Help understanding QWT Contour Plot

1. Novice
Join Date
Feb 2009
Posts
51
Thanks
2
Qt products
Platforms

## Help understanding QWT Contour Plot

Hi all,

I am trying to use qwt library to create my contour plot and I notice somebody on the forum has done that also but I couldn't quite figure out how to do it yet looking at the spectrogram example. I'd appreciate all help regarding this and please pardon my stupidity.

Basically what I have is a set of points in the format of (x, y, v) with x, y being the coordinates of the points and v is the value at the point. Do i then need to apply some sort of interpolation algorithm to find the points in between those points because certainly the points that I have don't fill up the entire display viewport, or this has been done inside the qwt library? Also, where do I need to enter my points?

Thank you so much for all your help.

2. Uwe
Expert
Join Date
Feb 2006
Location
Munich, Germany
Posts
3,308
Thanked 879 Times in 827 Posts
Qt products
Platforms

## Re: Help understanding QWT Contour Plot

Basically what I have is a set of points in the format of (x, y, v) with x, y being the coordinates of the points and v is the value at the point. Do i then need to apply some sort of interpolation algorithm to find the points in between those points because certainly the points that I have don't fill up the entire display viewport, or this has been done inside the qwt library? Also, where do I need to enter my points?
You have to subclass/implement QwtRasterData, what is used as bridge between the spectrogram item and your data. When rendering an image the spectrogram item requests values from your raster data class for each coordinate that corresponds with a pixel position of the image.

1) double QwtRasterData::value(double x, double y)
Here you have to calculate (f.e next neighbor) the value for a position from the values of your raster data. The implementation needs to be fast !

2) QwtDoubleInterval QwtRasterData::range()
The range is necessary to map the values to a color. (see QwtColorMap::rgb)

These methods are called before/after the spectrogram renders the image ( calling value for each pixel ). It's intended to do some initializations to allow a fast implementation of value()

4) QSize QwtRasterData::rasterHint(const QwtDoubleRect &)
The hint is used to find the best resolution of the image. If you return an invalid size ( = default implementation ) the image resolution is always the resolution of the paint device ( f.e. screen, printer ). A lower resolution improves the performance of the rendering process ( less calls of value ), so it makes sense to return the step size of your raster data.

5) QwtRasterData::setBoundingRect
Necessary for the autoscaler and you can avoid value requests for positions where you don't have any values.

Uwe

3. Novice
Join Date
Feb 2009
Posts
51
Thanks
2
Qt products
Platforms

## Re: Help understanding QWT Contour Plot

Hi Uwe,

Thank you so much for explaining this to me. I started to vaguely grasp the essence of this game but still need more education if you don't mind going extra steps to help me. What I am trying to do is this: I have a few (less than 20) environment sensors collecting data such as CO2 level, wind speed, humidity, etc., which need to be displayed as contour maps. These sensors can occasionally move to follow the air plume. Given that scenario, my contour map should have 20 or less points in the format of (X, Y, V) where X,Y is the coordinates and V is the CO2 or humidity level, etc. But 20 points is not enough to fill the whole map, so my most important question is: From these 20 points, do I have to implement some sort of interpolation algorithm to figure out data at a given point based on its nearest neighbors or this has been done in Qwt Contour library? And if I do, any suggestion where I should look at?

>1) double QwtRasterData::value(double x, double y)
>Here you have to calculate (f.e next neighbor) the value for a position from the values of >your raster data. The implementation needs to be fast !

What is the next neighbor in this case? Any pointer here?

>4) QSize QwtRasterData::rasterHint(const QwtDoubleRect &)
>5) QwtRasterData::setBoundingRect

I am confused between these two. Aren't they the same and pointed to the size of the display, like 600x400, or whatever the size of the QtWidget used to display the contour map?

Again, thank you so much Uwe. I hope you are not upset by my lengthy questions.

4. Uwe
Expert
Join Date
Feb 2006
Location
Munich, Germany
Posts
3,308
Thanked 879 Times in 827 Posts
Qt products
Platforms

## Re: Help understanding QWT Contour Plot

>1) double QwtRasterData::value(double x, double y)
>Here you have to calculate (f.e next neighbor) the value for a position from the values of >your raster data. The implementation needs to be fast !
What is the next neighbor in this case? Any pointer here?
The next neighbor is the point with the shortest distance to QPointF(x, y) in the 2D plane. Of course you could also return a value that is calculated from the z value from all neighbors ( this is the interpolation you are missing ). You can use initRaster/discardRaster to set up some sort of temporary matrix to improve the performance of your interpolation.
>4) QSize QwtRasterData::rasterHint(const QwtDoubleRect &)
>5) QwtRasterData::setBoundingRect

I am confused between these two. Aren't they the same and pointed to the size of the display, like 600x400, or whatever the size of the QtWidget used to display the contour map?
QwtRasterData ( and all code you have to deal with ) has nothing to do with widget coordinates or any indices for a matrix, where you might have stored your values. All coordinates, rects, sizes are about the coordinate system of the real world ( = what you see on the scales ) !

F.e if you have elevation data of Berlin the bounding rect would be the area of Berlin in geographical coordinates. If the resolution of your elevation data is a value for each 10x10 m you would have to translate this size corresponding to geographical coordinates and return it as raster hint ( better might be to return 5x5 to avoid rounding problems ).

Uwe

5. Novice
Join Date
Feb 2009
Posts
51
Thanks
2
Qt products
Platforms

## Re: Help understanding QWT Contour Plot

Hi Uwe,

Thanks again for getting back with me on this. I just wanted to clarify a few points to help me understand the problem better:

Originally Posted by Uwe
The next neighbor is the point with the shortest distance to QPointF(x, y) in the 2D plane. Of course you could also return a value that is calculated from the z value from all neighbors ( this is the interpolation you are missing ). You can use initRaster/discardRaster to set up some sort of temporary matrix to improve the performance of your interpolation.
Uwe
1. So given the scenario of my problem (having only about 20 data points in real world), what I should do is implement some sort of contour algorithm like CONREC or Perlin Noise to fill a large enough array of data points based on my original 20 data points. Then, in the value(x, y) function, I will search in my database to find the point closest to this (x, y) and return the z value at that point. Correct?

QwtRasterData ( and all code you have to deal with ) has nothing to do with widget coordinates or any indices for a matrix, where you might have stored your values. All coordinates, rects, sizes are about the coordinate system of the real world ( = what you see on the scales ) ! ...
2. Or if I when created my database use the screen coordinates for each of my data point instead of the real world coordinates then the sizes and coordinates here are the widget coordinates. Correct?

3. My covered area may change in size from time to time. But when I try to reset the bounding rect (after it's been initialized to some size), nothing happens. I did it like this:

myRasterData->setBoundingRect(QwtDoubleRect(-new_xlen/2, -new_ylen/2, new_xlen, new_ylen);
myPlot->replot();

What did I do wrong?

Again, thanks so much Uwe for sticking with me on this.

6. Uwe
Expert
Join Date
Feb 2006
Location
Munich, Germany
Posts
3,308
Thanked 879 Times in 827 Posts
Qt products
Platforms

## Re: Help understanding QWT Contour Plot

1. So given the scenario of my problem (having only about 20 data points in real world), what I should do is implement some sort of contour algorithm like CONREC or Perlin Noise to fill a large enough array of data points based on my original 20 data points.
A contour plot shows border line between areas with values above/below a threshold. But a point is no area and 20 points somewhere in space will never result in a useful contour plot ! This has nothing to do with Qwt or the algorithm that is used for contouring. So before you continue better try to find out where the areas are you want to display and if your points are raster data at all !
2. Or if I when created my database use the screen coordinates for each of my data point instead of the real world coordinates then the sizes and coordinates here are the widget coordinates. Correct?
No.
3. My covered area may change in size from time to time. But when I try to reset the bounding rect (after it's been initialized to some size), nothing happens. I did it like this:

myRasterData->setBoundingRect(QwtDoubleRect(-new_xlen/2, -new_ylen/2, new_xlen, new_ylen);
myPlot->replot();
The image of the spectrogram is cached. If you change something relevant behind the back of the spectrogram item you have to invalidate the cache ( spectrogram->invalidateCache() ) before you replot.

Uwe

7. ## The following user says thank you to Uwe for this useful post:

jwieland (12th September 2009)

8. Novice
Join Date
Feb 2009
Posts
51
Thanks
2
Qt products
Platforms

## Re: Help understanding QWT Contour Plot

Hi Uwe,

I almost figure it out now! There still some problem I need your help with: Since our app is constantly monitoring data for changes and update the contour plot whenever it does, we need to update the plot very quite often. However, when I updated the data and try to call replot() to update the plot, nothing happen! The plot is still showing old data.

What am I missing that results in this plot not refreshing? What is the proper way to do this?

Thank you so much for your help.

9. Uwe
Expert
Join Date
Feb 2006
Location
Munich, Germany
Posts
3,308
Thanked 879 Times in 827 Posts
Qt products
Platforms

## Re: Help understanding QWT Contour Plot

Originally Posted by jwieland
However, when I updated the data and try to call replot() to update the plot, nothing happen! The plot is still showing old data.

Uwe

10. Novice
Join Date
Feb 2009
Posts
51
Thanks
2
Qt products
Platforms

## Re: Help understanding QWT Contour Plot

Hi Uwe,

Sorry but that didn't work. Taking the spectrogram example and modified it, here is what I did:
SpectrogramData( ... ) : QwtRasterData( ... )), ... {
...
val = 0.2;
}

virtual double value(double x, double y) const
{
// do some calculation here to figure data at x, y
...

return val;
}

double val;
...

And then I tried changing the value of val variable and call the invalidateCache() before replot() to no avail!

Any idea what could went wrong?

11. Uwe
Expert
Join Date
Feb 2006
Location
Munich, Germany
Posts
3,308
Thanked 879 Times in 827 Posts
Qt products
Platforms

## Re: Help understanding QWT Contour Plot

You forgot to adjust SpectrogramData::copy(). Note, that when this operation does a deep copy you need to rassign ( spectrogram->setData()) your data whenever you change the original.

Uwe

12. Novice
Join Date
Feb 2009
Posts
51
Thanks
2
Qt products
Platforms

## Re: Help understanding QWT Contour Plot

Thanks, Uwe. You are a life saver!!!

It all works out now. wonderful!@

13. Novice
Join Date
Sep 2009
Location
phoenix, AZ
Posts
41
Qt products
Platforms

## Re: Help understanding QWT Contour Plot

jwieland,

could you post your code to plot contours of yours data ???

thanks,

Michael

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.