Click here to Skip to main content
15,896,207 members
Articles / Programming Languages / C++

A C++ implementation of Douglas-Peucker Line Approximation Algorithm

Rate me:
Please Sign up or sign in to vote.
4.87/5 (39 votes)
3 Mar 20037 min read 425.8K   13.9K   126  
DP Line approximation algorithm is a well-known method to approximate 2D lines. It is quite fast, O(nlog_2(n)) for a n-points line and can drastically compress a data curve. Here, a fully OOP implementation is given.
// DPHullGL.cpp: implementation of the CDPHullGL class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "DPHullGL.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDPHullGL::CDPHullGL()
{
	m_bHull=true;
}

CDPHullGL::~CDPHullGL()
{

}

void CDPHullGL::PlotPoints()
{
	int i;
	glBegin(GL_LINE_STRIP);
	PointContainer& pc = m_dpHull.GetPoints();
	for (i=0;i<pc.size();i++)
	{
		glVertex2f((GLfloat)pc[i].x,(GLfloat)pc[i].y);
	}
	glEnd();
}

void CDPHullGL::PlotKeys(CWGL& wgl)
{
	CString str;

	if (m_bHull)
	{
		glColor3f(1,0,0);
		PlotKeyMethod(m_dpHull);
	}

}

void CDPHullGL::PlotKeyMethod( const TLineApproximator<float, PointContainer, KeyContainer>& l)
{
	TLineApproximator<float, PointContainer, KeyContainer>& la = const_cast<TLineApproximator<float, PointContainer, KeyContainer>&> (l);

	const KeyContainer& kc = la.GetKeys();
	KeyContainer::const_iterator it;

	glBegin(GL_LINE_STRIP);
	for (it=kc.begin();it!=kc.end();it++)
	{
		glVertex2f((GLfloat)(*it)->x,(GLfloat)(*it)->y);
	}
	glEnd();
}

void CDPHullGL::SetTol( double dTol)
{
	m_dpHull.SetTol(dTol);
}

void CDPHullGL::ResizePoints(UINT nPoints)
{
	if (nPoints < 2)
		return;

	m_dpHull.GetPoints().resize(nPoints);
}

void CDPHullGL::SetPoint( UINT i, double x, double y)
{
	using namespace hull;
	ASSERT(i<GetPointSize());

	m_dpHull.GetPoints()[i]=hull::TPoint<float>(x,y);
}

void CDPHullGL::ComputeBoundingBox()
{
	m_dpHull.ComputeBoundingBox();
}

void CDPHullGL::Simplify()
{
	if (m_bHull)
		m_dpHull.Simplify();
}

UINT CDPHullGL::ShrinkNorm(double dScale, double dScaleTol, double eTolRight, UINT nMaxIter)
{
	return m_iterHull=m_dpHull.ShrinkNorm(dScale, dScaleTol, eTolRight,nMaxIter);
}

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


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