📄 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;i<m_iNPlanes;i++)
{
for (pos = m_pStripLists[i].begin(); pos!=m_pStripLists[i].end() ; pos++)
{
pStrip=(*pos);
ASSERT(pStrip);
pStrip->clear();
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;i<m_iNPlanes;i++)
{
for (pos=m_pStripLists[i].begin(); pos!=m_pStripLists[i].end();pos++)
{
pStrip=(*pos);
ASSERT(pStrip);
pStrip->clear();
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(iPlane<m_iNPlanes);
// check that the two points are not at the beginning or end of the some line strip
int i1=y1*(m_iColSec+1)+x1;
int i2=y2*(m_iColSec+1)+x2;
CLineStrip* pStrip;
CLineStripList::iterator pos;
BOOL added=FALSE;
for(pos=m_pStripLists[iPlane].begin(); pos!=m_pStripLists[iPlane].end() && !added; pos++)
{
pStrip=(*pos);
ASSERT(pStrip);
// check if points are appendable to this strip
if (i1==pStrip->front())
{
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;i<m_iNPlanes;i++)
{
again=true;
while(again)
{
again=false;
// building compacted list
ASSERT(newList.empty());
for (pos=m_pStripLists[i].begin(); pos!=m_pStripLists[i].end();pos++)
{
pStrip=(*pos);
for (pos2=newList.begin(); pos2!=newList.end();pos2++)
{
pStripBase=(*pos2);
changed=MergeStrips(pStripBase,pStrip);
if (changed)
again=true;
if (pStrip->empty())
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(iPlane<GetNPlanes());
out<<"Level : "<<GetPlane(iPlane)<<std::endl;
out<<"Number of strips : "<<m_pStripLists[iPlane].size()<<"\r\n";
out<<"i np start end xstart ystart xend yend\r\n";
for (pos = m_pStripLists[iPlane].begin(), i=0; pos != m_pStripLists[iPlane].end(); pos++, i++)
{
pStrip=*pos;
ASSERT(pStrip);
out<<i<<""<<pStrip->size()<<" "<<pStrip->front()<<" "<<pStrip->back()
<<""<<GetXi(pStrip->front())<<" "<<GetYi(pStrip->front())
<<""<<GetXi(pStrip->back())<<" "<<GetYi(pStrip->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(iPlane<GetNPlanes());
str.Format(_T("Level : %d\r\n"),GetPlane(iPlane));
out+=str;
str.Format(_T("Number of strips : %d\r\n"),m_pStripLists[iPlane].size());
out+=str;
str.Format(_T("i np start end xstart ystart xend yend\r\n"));
out+=str;
for (pos = m_pStripLists[iPlane].begin(), i=0; pos != m_pStripLists[iPlane].end(); pos++, i++)
{
pStrip=*pos;
ASSERT(pStrip);
str.Format(_T("%d %d %d %d %f %f %f %f\r\n"),
i, pStrip->size(), pStrip->front(),pStrip->back(),
GetXi(pStrip->front()),GetYi(pStrip->front()),
GetXi(pStrip->back()),GetYi(pStrip->back())
);
out+=str;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -