Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

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

, 3 Mar 2003
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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 4 Mar 2003
Article Copyright 2002 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid