Click here to Skip to main content
11,635,142 members (75,325 online)
Click here to Skip to main content
Add your own
alternative version

A C++ implementation of an improved contour plotting algorithm

, 30 Jul 2002 252.9K 9.9K 63
This class generates isocurves of a user defined function. Curves are drawn to OpenGL dc or stored in line strips.
// ContourGLDoc.cpp : implementation of the CContourGLDoc class
//

#include "stdafx.h"
#include "Contour.h"
#include "ContourGL.h"
#include "ContourGLDoc.h"

#ifdef LINE
	#undef LINE
#endif

#define LINE(a,b,c,d) line2(a,b,c,d)

void line2(double x1, double y1, double x2, double y2)
{
	glColor3f(0,0,0);
	glBegin(GL_LINES);
		glVertex2d(x1,y1);
		glVertex2d(x2,y2);
	glEnd();
}

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CContourGLDoc

IMPLEMENT_DYNCREATE(CContourGLDoc, CDocument)

BEGIN_MESSAGE_MAP(CContourGLDoc, CDocument)
	//{{AFX_MSG_MAP(CContourGLDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CContourGLDoc construction/destruction

CContourGLDoc::CContourGLDoc()
{
	// TODO: add one-time construction code here

}

CContourGLDoc::~CContourGLDoc()
{
}

BOOL CContourGLDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CContourGLDoc serialization

void CContourGLDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CContourGLDoc diagnostics

#ifdef _DEBUG
void CContourGLDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CContourGLDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CContourGLDoc commands

void CContourGLDoc::Draw()
{
	// Plotting strip generated by CLineStripList
	// generating info
	m_listContour.Generate();

	// draw line strips
	CLineStripList* pStripList;
	CLineStrip* pStrip;
	UINT i,index;
	CLineStripList::iterator pos;
	CLineStrip::iterator pos2;
	double pLimits[4];
	double x,y;

	m_listContour.GetLimits(pLimits);
	for (i=0;i<m_listContour.GetNPlanes();i++)
	{
		pStripList=m_listContour.GetLines(i);
		ASSERT(pStripList);
		for (pos=pStripList->begin(); pos != pStripList->end() ; pos++)
		{
			pStrip=(*pos);
			ASSERT(pStrip);
			if (pStrip->empty())
				continue;


			// putting point at start and end of strip
			glBegin(GL_POINTS);
				glColor4f(1,0,0,.8f);
				// retreiving index
				index=pStrip->front();
				// drawing
				x=m_listContour.GetXi(index);
				y=m_listContour.GetYi(index);
				glVertex2f((GLfloat)(pLimits[0]+x),(GLfloat)(pLimits[2]+y));
				// retreiving index
				glColor4f(0,0,1,.8f);
				index=pStrip->back();
				// drawing
				x=m_listContour.GetXi(index);
				y=m_listContour.GetYi(index);
				glVertex2f((GLfloat)(pLimits[0]+x),(GLfloat)(pLimits[2]+y));
			glEnd();

			glColor3f(i/(float)m_listContour.GetNPlanes(),
				i/(float)m_listContour.GetNPlanes(),
				1.f-i/(float)m_listContour.GetNPlanes());

			// filling strip if closed...
			glBegin(GL_LINE_STRIP);
			for (pos2=pStrip->begin(); pos2 != pStrip->end() ; pos2++)
			{
				// retreiving index
				index=(*pos2);
				// drawing
				x=m_listContour.GetXi(index);
				y=m_listContour.GetYi(index);

				glVertex2f((GLfloat)(pLimits[0]+x),(GLfloat)(pLimits[2]+y));
			}
			glEnd();
		}
	}
}

void CContourGLDoc::GetGLLimits(double pLimits[])
{
	// drawing
	m_listContour.GetLimits(pLimits);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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

Share

About the Author

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

You may also be interested in...

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