Click here to Skip to main content
15,891,409 members
Articles / Desktop Programming / MFC

Plot Graphic Library

Rate me:
Please Sign up or sign in to vote.
4.95/5 (70 votes)
7 May 2003LGPL36 min read 1.4M   51.3K   383  
A library to plot data (lines, maps...) in MFC projects
In this article, you will see a library called PGL that encapsulates plot capabilities in a MFC project for VC6 and VC7. It can easily plot data generated in a project without the need of any external software.
// PGLLine2DLOD.cpp: implementation of the CPGLLine2DLOD class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PGL/PGLLine2DLOD.h"
#include "PGL/PGLLine2DLODPropPage.h"

IMPLEMENT_SERIAL(CPGLLine2DLOD, CPGLLine2D,1);

void CPGLLine2DLOD::Serialize(CArchive &archive)
{
    // call base class function first
    // base class is CObject in this case
    CPGLLine2D::Serialize( archive );

    // now do the stuff for our specific class
    if( archive.IsStoring() )
	{
	}
    else
	{
	}
}

#ifdef _DEBUG
void CPGLLine2DLOD::Dump( CDumpContext& dc ) const
{
    // call base class function first
    CPGLLine2D::Dump( dc );

    // now do the stuff for our specific class
	dc << _T("CPGLLine2DLOD ID ") << GetID() << endl;
	dc <<"Tol : "<<m_hull.GetTol()<<endl;
}
void CPGLLine2DLOD::AssertValid() const
{
    // call inherited AssertValid first
    CPGLLine2D::AssertValid();

} 
#endif


CPGLLine2DLOD::CPGLLine2DLOD()
: CPGLLine2D()
{
	// saving old ressource.
	HINSTANCE hOldInst=AfxGetResourceHandle();
	// setting dll ressource
	AfxSetResourceHandle(AfxGetInstanceHandle());
	// creating bitmap
	m_bmp.DeleteObject();
	m_bmp.LoadBitmap(IDB_PGL_LINE2DLOD_BITMAP);
	// restoring old ressource.
	AfxSetResourceHandle(hOldInst);
}

CPGLLine2DLOD::CPGLLine2DLOD(const CPGLLine2DLOD &l)
: CPGLLine2D(l)
{
	// Copying tol...
	m_hull.SetTol(l.m_hull.GetTol());
	
	// setting curve
	DP_POINT* pCurve=new DP_POINT[m_iNPoints];
	m_hull.SetCurve(m_iNPoints,pCurve);

	// filling structure
	for (int i=0;i<m_iNPoints;i++)
	{
		pCurve[i][DP_XX]=m_pX[i];
		pCurve[i][DP_YY]=m_pY[i];
	}

	// Approximating
	m_hull.Simplify();

	// saving old ressource.
	HINSTANCE hOldInst=AfxGetResourceHandle();
	// setting dll ressource
	AfxSetResourceHandle(AfxGetInstanceHandle());
	// creating bitmap
	m_bmp.DeleteObject();
	m_bmp.LoadBitmap(IDB_PGL_LINE2DLOD_BITMAP);
	// restoring old ressource.
	AfxSetResourceHandle(hOldInst);
}

void CPGLLine2DLOD::AddContextMenuItems(CMenu* pMenu)
{
	ASSERT_VALID(pMenu);
	// first call base class function
	CPGLLine2D::AddContextMenuItems(pMenu);

	// add separator
	// add own entries...
//	pMenu->AppendMenu(MF_ENABLED | MF_STRING, ,"Test Line2D");	
}

void CPGLLine2DLOD::AddPropertyPage(CPropertySheet* pPropSheet)
{
	ASSERT_VALID(pPropSheet);
	// call own functions
	CPGLLine2DLODPropPage* propPage=new CPGLLine2DLODPropPage(this);
	pPropSheet->AddPage(propPage);

	// first call base class function
	CPGLLine2D::AddPropertyPage(pPropSheet);
}

CPGLLine2DLOD::~CPGLLine2DLOD()
{
	// cleaning m_hull
	m_hull.DeleteCurve();

}

/// plotting to EPS file
void CPGLLine2DLOD::PlotGfx(gfxinterface::CGfxInterface& gfx)
{
	// first if it is visible
	if (!IsVisible())
		return;

	gfx.AddComment("--- CPGLLine2DLOD ---");

	// calling base class plot
	CPGLLine2D::PlotGfx(gfx);
}

void CPGLLine2DLOD::SetDatas(int _nPoints, double *_x, double *_y)
{
	CPGLLine2D::SetDatas(_nPoints, _x, _y);

	// reseting m_hull
	m_hull.DeleteCurve();

	// setting curve
	DP_POINT* pCurve=new DP_POINT[m_iNPoints];
	m_hull.SetCurve(m_iNPoints,pCurve);

	// filling structure
	for (int i=0;i<m_iNPoints;i++)
	{
		pCurve[i][DP_XX]=m_pX[i];
		pCurve[i][DP_YY]=m_pY[i];
	}

	// Approximating
	m_hull.Simplify();
}

void CPGLLine2DLOD::SetDatas(const std::vector<double>& vx, const std::vector<double>& vy)
{
	CPGLLine2D::SetDatas(vx, vy);

	// reseting m_hull
	m_hull.DeleteCurve();

	// setting curve
	DP_POINT* pCurve=new DP_POINT[m_iNPoints];
	m_hull.SetCurve(m_iNPoints,pCurve);

	// filling structure
	for (int i=0;i<m_iNPoints;i++)
	{
		pCurve[i][DP_XX]=m_pX[i];
		pCurve[i][DP_YY]=m_pY[i];
	}

	// Approximating
	m_hull.Simplify();
}

void CPGLLine2DLOD::UpdateExtent(CPGLView* pView)
{
	// Calling base class function
	CPGLLine2D::UpdateExtent(pView);

	// recomputing hull
	m_hull.Simplify();
}

void CPGLLine2DLOD::SetTol(double tol)
{
	m_hull.SetTol(tol);

	// recompute hull
	m_hull.Simplify();
}

void CPGLLine2DLOD::PlotLineStripGfx(gfxinterface::CGfxInterface& gfx)
{
	DP_POINT** pCurve = m_hull.GetDPCurve();
	double* pX=new double[m_hull.GetDPNPoints()];
	double* pY=new double[m_hull.GetDPNPoints()];
	for (int i=0;i<m_hull.GetDPNPoints();i++)
	{
		pX[i] = (*pCurve[i])[DP_XX];
		pY[i] = (*pCurve[i])[DP_YY];
	}

	if (m_bFilled)
	{
		gfx.PushState();
		gfx.SetFillColor(GetColor().GetRed(), GetColor().GetGreen(), GetColor().GetBlue(), GetColor().GetAlpha());
		gfx.SetColor(0,0,0);
		switch (GetInterpolationType())
		{
		case PGL_INTERPOLATION_STEP:
			gfx.DrawStepStrip(m_hull.GetDPNPoints(),pX,pY, false, true);
			break;
		case PGL_INTERPOLATION_LINEAR:
			gfx.DrawLineStrip(m_hull.GetDPNPoints(),pX,pY, false, true);
			break;
		case PGL_INTERPOLATION_SEGMENT:
			gfx.DrawMultipleLineStrip(m_hull.GetDPNPoints(),m_iStripSize,pX,pY, false, true);
			break;
		}
		gfx.PopState();
	}
	else
	{
		switch (GetInterpolationType())
		{
		case PGL_INTERPOLATION_STEP:
			gfx.DrawStepStrip(m_hull.GetDPNPoints(),pX,pY);
			break;
		case PGL_INTERPOLATION_LINEAR:
			gfx.DrawLineStrip(m_hull.GetDPNPoints(),pX,pY);
			break;
		case PGL_INTERPOLATION_SEGMENT:
			gfx.DrawMultipleLineStrip(m_hull.GetDPNPoints(),m_iStripSize, pX, pY);
			break;
		}
	}


	delete[] pX;
	delete[] pY;
}

void CPGLLine2DLOD::PlotPointStripGfx(gfxinterface::CGfxInterface& gfx)
{
	DP_POINT** pCurve = m_hull.GetDPCurve();
	double* pX=new double[m_hull.GetDPNPoints()];
	double* pY=new double[m_hull.GetDPNPoints()];
	for (int i=0;i<m_hull.GetDPNPoints();i++)
	{
		pX[i] = (*pCurve[i])[DP_XX];
		pY[i] = (*pCurve[i])[DP_YY];
	}

	// choose type of interpolation between points
	switch(GetPointType())
	{
	case PGL_POINT_SIMPLE:
		gfx.DrawCircleStrip(m_hull.GetDPNPoints(), pX, pY, GetPointWidth(), true);
		break;
	case PGL_POINT_CONTOUR:
		gfx.DrawCircleStrip(m_hull.GetDPNPoints(), pX, pY, GetPointWidth());
		break;
	case PGL_POINT_TRIANGLE:
		gfx.DrawTriangleStrip(m_hull.GetDPNPoints(), pX, pY, GetPointWidth());
		break;
	case PGL_POINT_SQUARE:
		gfx.DrawSquareStrip(m_hull.GetDPNPoints(), pX, pY, GetPointWidth());
		break;
	}

	delete[] pX;
	delete[] pY;
}

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, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
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).

Comments and Discussions