www.pudn.com > contour_demo.zip > ListContour.cpp
// ListContour.cpp: implementation of the CListContour class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ContourGL.h"
#include "ListContour.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CListContour::CListContour()
: CContour()
{
m_pStripLists=NULL;
}
CListContour::~CListContour()
{
CleanMemory();
}
void CListContour::Generate()
{
// generate line strips
CContour::Generate();
// compact strips
CompactStrips();
}
void CListContour::InitMemory()
{
CContour::InitMemory();
CLineStripList::iterator pos;
CLineStrip* pStrip;
if (m_pStripLists)
{
int i;
// reseting lists
for (i=0;iclear();
delete pStrip;
}
m_pStripLists[i].clear();
}
}
else
m_pStripLists=new CLineStripList[m_iNPlanes];
}
void CListContour::CleanMemory()
{
CContour::CleanMemory();
CLineStripList::iterator pos;
CLineStrip* pStrip;
if (m_pStripLists)
{
int i;
// reseting lists
for (i=0;iclear();
delete pStrip;
}
m_pStripLists[i].clear();
}
delete[] m_pStripLists;
m_pStripLists=NULL;
}
}
void CListContour::ExportLine(int iPlane,int x1, int y1, int x2, int y2)
{
ASSERT(iPlane>=0);
ASSERT(iPlanefront())
{
pStrip->insert(pStrip->begin(),i2);
return;
}
if (i1==pStrip->back())
{
pStrip->insert(pStrip->end(),i2);
return;
}
if (i2==pStrip->front())
{
pStrip->insert(pStrip->begin(),i1);
return;
}
if (i2==pStrip->back())
{
pStrip->insert(pStrip->end(),i1);
return;
}
}
// segment was not part of any line strip, creating new one
pStrip=new CLineStrip;
pStrip->insert(pStrip->begin(),i1);
pStrip->insert(pStrip->end(),i2);
m_pStripLists[iPlane].insert(m_pStripLists[iPlane].begin(),pStrip);
}
bool CListContour::MergeStrips(CLineStrip* pStrip1, CLineStrip* pStrip2)
{
CLineStrip::iterator pos;
CLineStrip::reverse_iterator rpos;
if (pStrip2->empty())
return false;
int index;
// debugging stuff
if (pStrip2->front()==pStrip1->front())
{
// retreiving first element
pStrip2->pop_front();
// adding the rest to strip1
for (pos=pStrip2->begin(); pos!=pStrip2->end();pos++)
{
index=(*pos);
ASSERT(index>=0);
pStrip1->insert(pStrip1->begin(),index);
}
pStrip2->clear();
return true;
}
if (pStrip2->front()==pStrip1->back())
{
pStrip2->pop_front();
// adding the rest to strip1
for (pos=pStrip2->begin(); pos!=pStrip2->end();pos++)
{
index=(*pos);
ASSERT(index>=0);
pStrip1->insert(pStrip1->end(),index);
}
pStrip2->clear();
return true;
}
if (pStrip2->back()==pStrip1->front())
{
pStrip2->pop_back();
// adding the rest to strip1
for (rpos=pStrip2->rbegin(); rpos!=pStrip2->rend();rpos++)
{
index=(*rpos);
ASSERT(index>=0);
pStrip1->insert(pStrip1->begin(),index);
}
pStrip2->clear();
return true;
}
if (pStrip2->back()==pStrip1->back())
{
pStrip2->pop_back();
// adding the rest to strip1
for (rpos=pStrip2->rbegin(); rpos!=pStrip2->rend();rpos++)
{
index=(*rpos);
ASSERT(index>=0);
pStrip1->insert(pStrip1->end(),index);
}
pStrip2->clear();
return true;
}
return false;
}
void CListContour::CompactStrips()
{
CLineStrip* pStrip;
CLineStrip* pStripBase;
int i;
CLineStripList::iterator pos,pos2;
CLineStripList newList;
bool again, changed;
for (i=0;iempty())
break;
}
if (pStrip->empty())
delete pStrip;
else
newList.insert(newList.begin(),pStrip);
}
// deleting old list
m_pStripLists[i].clear();
// Copying all
for (pos2=newList.begin(); pos2 != newList.end(); pos2++)
{
pStrip=(*pos2);
if (!(pStrip->front()==pStrip->back() && pStrip->size()==2))
m_pStripLists[i].insert(m_pStripLists[i].begin(),pStrip );
else
delete pStrip;
}
// emptying temp list
newList.clear();
}
}
}
void CListContour::DumpPlane(int iPlane, std::ostream& out) const
{
CLineStripList::const_iterator pos;
int i;
CLineStrip* pStrip;
ASSERT(iPlane>=0);
ASSERT(iPlanesize()<<" "<front()<<" "<back()
<<""<front())<<" "<front())
<<""<back())<<" "<back())
<<"\r\n";
}
}
void CListContour::DumpPlane(int iPlane, CString& out) const
{
CLineStripList::const_iterator pos;
int i;
CLineStrip* pStrip;
CString str;
ASSERT(iPlane>=0);
ASSERT(iPlanesize(), pStrip->front(),pStrip->back(),
GetXi(pStrip->front()),GetYi(pStrip->front()),
GetXi(pStrip->back()),GetYi(pStrip->back())
);
out+=str;
}
}