15,920,603 members
Articles / Desktop Programming / MFC

# A C++ Implementation of an Improved Contour Plotting Algorithm

Rate me:
30 Jul 20023 min read 328.2K   12.7K   69   41
This class generates isocurves of a user defined function. Curves are drawn to OpenGL dc or stored in line strips.
In this article, you will learn about a contour plot class, which is an improved version of the Level Curve Tracing Algorithm. It was designed to draw iso-contour of a user-defined function f(x,y).

## Introduction

This article presents a contour plot class. It is designed to draw iso-contour of a user-defined function f(x,y). I wrote it to integrate it in a graphic library: Plot Graphic Library

The class is based on the algorithm presented in [1] (Check References section). It is basically an improved version of the Level Curve Tracing Algorithm.

## Understanding the Algorithm

The algorithm uses several tuning parameters that the user must choose in order to have the best quality/performance ratio of the algorithm:

• Domain of x,y:
C++
```// Setting domain x=[0,1], y=[2,3]
double pLimits[4]={0,1,2,3,4};
CContour contour;
contour.SetLimits(pLimits);
```
• Size of the primary grid: The grid on which evaluate the function `f(x,y)`. See `SetFirGrid, GetColFir, GetRowFir`. The parameter influences greatly the quality of the contour.
• Size of the secondary grid: The grid where the function is going to be evaluated. This grid can be much finer that the first grid. See `SetSecGrid, GetColSec, GetRowSec`.

## Classes

### CContour

Main contour class. This class cannot be directly but has to be inherited. The inherited class must implement the `ExportLine` function. To generate contours, use

C++
`void Generate()`

Make to have set the field function (`f(x,y)`) before calling this function. The function will call `ExportLine` for each new segment.

### CGLContour

Use this class to draw contours to an OpenGL device context.

### CListContour

Use this class to generate contour and store them as line strip. The user can retrieve each contour and use it as he wills. This function uses two sub-classes:

• `CLineStrip`, a `list < int > `containing the index of the points
• `CLineStripList`, a `list <CLineStrip*>`

The line strip can be accessed by:

C++
`CLineStripList* GetList(iPane);`

where `iPane` is the index of the contour.

## How To...

### Set Up a Contour Object

Suppose that we have inherited a class from `CContour` and overridden `ExportLine` function.

C++
```class CMyContour : CContour
{
void ExportLine(...);
}
```

Now, first set the function `f(x,y)`:

C++
```double  myF(double x, double y)
{ [...] return ... };

CMyContour contour;
// Setting f(x,y)=myF
contour.SetFieldFcn(myF);
```

Then set the iso-contour values, i.e.:

C++
```int n;
CMyContour contour;
vector<double> vIso(n);
for (int i=0;i<n;i++)
{ ... }
// setting iso-lines
contour.SetPlanes(vIso);
```

The contour is ready to be used.

### Draw Contours using OpenGL

Use `CGLContour` as inherited function of `CContour`.

C++
```CGLContour contour;
// Setting up contour : setting f, domain of x,
// isocurve values
[...]
// generating contour
contour.Generate();
```

### Retrieve Contours in Line Strip

Use `CListContour` as inherited function of `CContour`. Only the index of the points with respect to second grid are stored in the list. You can access their real value by using `GetXi()` and `GetYi()` functions.

C++
```CGLContour contour;
// Setting up contour : setting f,
// domain of x, isocurve values
[...]
// generating contour
contour.Generate();
// Retrieving info
CLineStripList* pStripList;
// getting 0-th iso-curve
pStripList=contour.GetLines(0);
ASSERT(pStripList);
// iterating liststrip vertices
CLineStrip::iterator pos;
for (pos=pStripList->begin();
pos != pStripList->end() ; pos++)
{
pStrip=(*pos);
ASSERT(pStrip);
if (pStrip->empty())
continue;
// using info of strip list
// pStrip contains the successive index of the points
// See CContourGLDoc.OnDraw for further details
[...]
}
```

• 31st August, 2002: Added contribution from Chenggang Zhou: better strip compression, threshold merging, area of strip, boundary detection, also some minor changes I don't remember...
• 4th March, 2002: All the code is now using STL. :)

## References

A list of licenses authors might use can be found here.

Written By
Engineer
United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

 First PrevNext
 Won't run in VC 2005 or VC 2008 [modified] Michael B Pliam2-Mar-10 6:16 Michael B Pliam 2-Mar-10 6:16
 Re: Won't run in VC 2005 or VC 2008 [modified] michael_asher10-May-10 21:45 michael_asher 10-May-10 21:45
 Re: Won't run in VC 2005 or VC 2008 [modified] Matthew_Z13-Aug-14 8:33 Matthew_Z 13-Aug-14 8:33
 Re: Won't run in VC 2005 or VC 2008 [modified] Member 429892922-Oct-14 16:58 Member 4298929 22-Oct-14 16:58
 Why I can not download the demo? bigsun690910-Jan-08 16:21 bigsun6909 10-Jan-08 16:21
 Hi,I have a contour ocx here.Would you want to try it?Thank you. snowedforest3-Feb-07 19:49 snowedforest 3-Feb-07 19:49
 Bitmap Sangeetha_J25-Jan-07 20:04 Sangeetha_J 25-Jan-07 20:04
 Re: Bitmap Sangeetha_J25-Jan-07 20:09 Sangeetha_J 25-Jan-07 20:09
 Improvement 7-Feb-05 23:37 7-Feb-05 23:37
 Demo program crashes on redraw bdiscoe18-Aug-04 9:56 bdiscoe 18-Aug-04 9:56
 plotting Contour In an image Jobaida Begum6-Apr-04 7:45 Jobaida Begum 6-Apr-04 7:45
 feature line extraction from contours sanjayrana10-Jun-03 5:21 sanjayrana 10-Jun-03 5:21
 Image Processing prit13-Apr-03 8:50 prit 13-Apr-03 8:50
 Re: Image Processing Chris Losinger13-Apr-03 9:11 Chris Losinger 13-Apr-03 9:11
 Re: Image Processing Jonathan de Halleux13-Apr-03 21:12 Jonathan de Halleux 13-Apr-03 21:12
 Help! Wang_lei5-Sep-02 22:10 Wang_lei 5-Sep-02 22:10
 Nope Jonathan de Halleux15-Oct-02 21:15 Jonathan de Halleux 15-Oct-02 21:15
 Re: Nope bigsun690910-Jan-08 16:19 bigsun6909 10-Jan-08 16:19
 Re: Help! Tigga_123-Feb-07 1:12 Tigga_1 23-Feb-07 1:12
 Improvement 22-Jul-02 20:24 22-Jul-02 20:24
 Did you do it ? peliikhan23-Jul-02 3:20 peliikhan 23-Jul-02 3:20
 Re: Did you do it ? C.G. Zhou25-Jul-02 8:29 C.G. Zhou 25-Jul-02 8:29
 Updated Jonathan de Halleux31-Jul-02 1:10 Jonathan de Halleux 31-Jul-02 1:10
 How to fill the contour with color? wangchi17-Jul-02 20:04 wangchi 17-Jul-02 20:04
 Re: How to fill the contour with color? Jonathan de Halleux17-Jul-02 22:13 Jonathan de Halleux 17-Jul-02 22:13
 Last Visit: 31-Dec-99 18:00     Last Update: 20-Jun-24 11:45 Refresh 12 Next ᐅ