Click here to Skip to main content
Click here to Skip to main content

A C++ implementation of an improved contour plotting algorithm

By , 30 Jul 2002
 

Sample Image - contour.jpg

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 parameter that the user must choose in order to have the best quality/performance ratio of the algorithm :

  • Domain of x,y:
         // 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 influence 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

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 to sub-classes :

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

The line strip can be accessed by

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 overriden ExportLine function.

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

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

    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.

    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.

    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.

    CGLContour contour;
    // Setting up contour : setting f, 
    // domain of x, isocurve values
    [...]
    // generating contour
    contour.Generate();
    // Retreiving 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 succesive index of the points
        // See CContourGLDoc.OnDraw for further details
        [...]       
    }

Updates

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

References

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Jonathan de Halleux
Engineer
United States United States
Member
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).

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralWon't run in VC 2005 or VC 2008 [modified]memberMichael B Pliam2 Mar '10 - 6:16 
GeneralRe: Won't run in VC 2005 or VC 2008 [modified]membermdeale10 May '10 - 21:45 
QuestionWhy I can not download the demo?memberbigsun690910 Jan '08 - 16:21 
GeneralHi,I have a contour ocx here.Would you want to try it?Thank you.membersnowedforest3 Feb '07 - 19:49 
GeneralBitmapmemberSangeetha_J25 Jan '07 - 20:04 
Hi Jonathan,
 
I came to know your article while searching for concepts related to contour. Am working as a software engineer in Visual C++ for the past two years. No one is here for me to train. Am reading for myself and doing things as much as my effort makes it possible. Now am in a need to accomplish the following one:
 
1. I need to show a bitmap in a full screen mode
2. Then i need to scatter that bitmap here and there all over the screen and then have to reassemble it to make it return back to its original form.
 
I had posted one message asking about this. Then i got the reply to look for finding contours of that bitmap and then to carry on from there. One more reply suggested me to study about 3DMax.
 
Now i posted this to have your ideas and suggestions. If there any book for me to read and proceed, please refer to me. I am ready to read and implement the procedure.
 
Thanking you,
 
With Regards,
Sangeetha.

GeneralRe: BitmapmemberSangeetha_J25 Jan '07 - 20:09 
GeneralImprovementmemberlaurent garnier7 Feb '05 - 23:37 
GeneralDemo program crashes on redrawsussBen Discoe18 Aug '04 - 9:56 
Generalplotting Contour In an imagememberJobaida Begum6 Apr '04 - 7:45 
Generalfeature line extraction from contourssusssanjayrana10 Jun '03 - 5:21 
GeneralImage ProcessingsussPrit13 Apr '03 - 8:50 
GeneralRe: Image ProcessingmemberChris Losinger13 Apr '03 - 9:11 
GeneralRe: Image ProcessingmemberJonathan de Halleux13 Apr '03 - 21:12 
GeneralHelp!memberWang_lei5 Sep '02 - 22:10 
GeneralNopememberJonathan de Halleux15 Oct '02 - 21:15 
QuestionRe: Nopememberbigsun690910 Jan '08 - 16:19 
GeneralRe: Help!memberTigga_123 Feb '07 - 1:12 
GeneralImprovementmemberC. G. Zhou22 Jul '02 - 20:24 
GeneralDid you do it ?susspeliikhan23 Jul '02 - 3:20 
GeneralRe: Did you do it ?sussC.G. Zhou25 Jul '02 - 8:29 
GeneralUpdatedmemberJonathan de Halleux31 Jul '02 - 1:10 
QuestionHow to fill the contour with color?susswangchi17 Jul '02 - 20:04 
AnswerRe: How to fill the contour with color?memberJonathan de Halleux17 Jul '02 - 22:13 
QuestionHow to fill the contour with color?sussAnonymous17 Jul '02 - 20:04 
AnswerRe: How to fill the contour with color?adminChris Maunder31 Jul '02 - 15:15 
GeneralGreat stuffmemberJonathan de Halleux31 Jul '02 - 22:06 
GeneralRe: Great stuffadminChris Maunder31 Jul '02 - 22:28 
GeneralRe: Great stuffmemberTerence Russell22 Sep '04 - 9:02 
Generalthankmemberzhengyi7 May '02 - 22:01 
GeneralThanks...memberDavide Onofrio10 Jan '02 - 22:30 
GeneralRe: Thanks...memberpelikhan11 Jan '02 - 0:16 
GeneralUse STL...memberPaul Selormey10 Jan '02 - 21:04 
GeneralRe: Use STL...memberpelikhan10 Jan '02 - 21:47 
GeneralRe: Use STL...memberpelikhan11 Jan '02 - 3:10 
GeneralRe: Use STL...memberpelikhan4 Mar '02 - 4:31 
GeneralRe: Use STL...memberPaul Selormey4 Mar '02 - 13:11 
GeneralRe: Use STL...memberpelikhan4 Mar '02 - 22:22 
GeneralRe: Use STL...memberDavid Gallagher1 Aug '02 - 6:01 
GeneralRe: Use STL...memberPaul Selormey1 Aug '02 - 14:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 31 Jul 2002
Article Copyright 2002 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid