You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

305 lines
9.0 KiB
C++

#include "StdAfx.h"
#include "ObjSortMgr.h"
#include "ObjContainer.h"
#include "Layer.h"
#include "GlobalFunction.h"
#include "ObjPoint.h"
#if 1//<2F><><EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><C3BA><EFBFBD>
bool CompareDouble1(double val1,double val2)
{
return val1>val2;
}
bool CompareDouble2(double val1,double val2)
{
return val1<val2;
}
bool CompareObjX1(Sptr<CObjBase> &Obj1,Sptr<CObjBase> &Obj2)
{
Dbxy pt1 = Obj1->GetCenterPt();
Dbxy pt2 = Obj2->GetCenterPt();
return pt1.x>pt2.x;
}
bool CompareObjX2(Sptr<CObjBase> &Obj1,Sptr<CObjBase> &Obj2)
{
Dbxy pt1 = Obj1->GetCenterPt();
Dbxy pt2 = Obj2->GetCenterPt();
return pt1.x<pt2.x;
}
bool CompareObjY1(Sptr<CObjBase> &Obj1,Sptr<CObjBase> &Obj2)
{
Dbxy pt1 = Obj1->GetCenterPt();
Dbxy pt2 = Obj2->GetCenterPt();
return pt1.y>pt2.y;
}
bool CompareObjY2(Sptr<CObjBase> &Obj1,Sptr<CObjBase> &Obj2)
{
Dbxy pt1 = Obj1->GetCenterPt();
Dbxy pt2 = Obj2->GetCenterPt();
return pt1.y<pt2.y;
}
#endif
#define BUCKET_EPS 0.01 //Ͱ<>ľ<EFBFBD><C4BE><EFBFBD>
CObjSortMgr::CObjSortMgr(void)
{
m_ScanDir = _DIR_L;
}
CObjSortMgr::~CObjSortMgr(void)
{
}
#if 1
//ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>obj <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void CObjSortMgr::SortObjByDir(DIRECTION dir)
{
m_ScanDir = dir;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>obj <20><><EFBFBD>Ѽ<EFBFBD>״̬
CObjContainer &LayerObjContainer = gLayer.GetObjContainer();
LayerObjContainer.ResetAllCollectedState();
//<2F><>ȡlayer <20><>obj <20><><EFBFBD><EFBFBD>
vector<Sptr<CObjBase>> &LayerObjVec = LayerObjContainer.GetObjVec();
//<2F><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD>ͷ<EFBFBD>ѡ<EFBFBD><D1A1>obj <20><><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
vector<Sptr<CObjBase>> SelObjVec;
vector<Sptr<CObjBase>> NotSelObjVec;
SeparateSelObj(LayerObjVec,SelObjVec,NotSelObjVec);
if(SelObjVec.empty())
return;
//<2F><>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD>obj <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
SortObjVec(SelObjVec);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ż<EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
LayerObjVec.clear();
PushToObjVec(LayerObjVec,SelObjVec);
PushToObjVec(LayerObjVec,NotSelObjVec);
}
//<2F><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD>ͷ<EFBFBD>ѡ<EFBFBD><D1A1>obj <20><><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void CObjSortMgr::SeparateSelObj(vector<Sptr<CObjBase>> &ObjVec,vector<Sptr<CObjBase>> &SelObjVec,vector<Sptr<CObjBase>> &NotSelObjVec)
{
vector<Sptr<CObjBase>>::iterator iter = ObjVec.begin();
vector<Sptr<CObjBase>>::iterator iter_end = ObjVec.end();
for(;iter!=iter_end;iter++)
{
if((*iter)->IsSelected())
SelObjVec.push_back(*iter);
else
NotSelObjVec.push_back(*iter);
}
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ż<EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ObjVec2---->ObjVec1
void CObjSortMgr::PushToObjVec(vector<Sptr<CObjBase>> &ObjVec1,vector<Sptr<CObjBase>> &ObjVec2)
{
vector<Sptr<CObjBase>>::iterator iter = ObjVec2.begin();
vector<Sptr<CObjBase>>::iterator iter_end = ObjVec2.end();
for(;iter!=iter_end;iter++)
{
ObjVec1.push_back(*iter);
}
}
//<2F>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>ɨ<EFBFBD><C9A8>
bool CObjSortMgr::IsHorizontalScan()
{
if(m_ScanDir == _DIR_L || m_ScanDir == _DIR_R)
return true;
return false;
}
//<2F>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>ɨ<EFBFBD><C9A8>
bool CObjSortMgr::IsForwardDirection()
{
if(m_ScanDir == _DIR_R || m_ScanDir == _DIR_D)
return true;
return false;
}
#endif
#if 1
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>obj <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><><CDB0><EFBFBD><EFBFBD>˼·)
void CObjSortMgr::SortObjVec(vector<Sptr<CObjBase>> &ObjVec)
{
vector<vector<Sptr<CObjBase>>> BucketVec;//Ͱ<><CDB0><EFBFBD><EFBFBD>(ÿ<><C3BF>Ͱ<EFBFBD>ĵ<EFBFBD>һ<EFBFBD><D2BB>obj <20><>ʾͰ<CABE><CDB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
//<2F><><EFBFBD><EFBFBD>Ͱ
CreatBucketVec(BucketVec,ObjVec);
//<2F>Ѽ<EFBFBD>Ͱ<EFBFBD><CDB0>obj
CollectBucketObj(BucketVec,ObjVec);
//<2F><>ÿ<EFBFBD><C3BF>Ͱ<EFBFBD><CDB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
BucketSort(BucketVec);
//<2F><>Ͱ<EFBFBD><CDB0>obj <20><><EFBFBD><EFBFBD>ObjVec
BucketToObjVec(BucketVec,ObjVec);
}
//<2F><><EFBFBD><EFBFBD>Ͱ
void CObjSortMgr::CreatBucketVec(vector<vector<Sptr<CObjBase>>> &BucketVec,vector<Sptr<CObjBase>> &ObjVec)
{
//<2F><><EFBFBD><EFBFBD>Ͱ<EFBFBD><CDB0>׼<EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
vector<double> BucketBaseCoordVec;
CreatBucketBaseCoordVec(BucketBaseCoordVec,ObjVec);
//<2F><>Ͱ<EFBFBD><CDB0>׼<EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(IsForwardDirection())
sort(BucketBaseCoordVec.begin(),BucketBaseCoordVec.end(),CompareDouble1);
else
sort(BucketBaseCoordVec.begin(),BucketBaseCoordVec.end(),CompareDouble2);
//<2F><><EFBFBD>ݻ<EFBFBD>׼<EFBFBD><EFBFBD><E3B4B4>Ͱ
vector<double>::iterator iter = BucketBaseCoordVec.begin();
vector<double>::iterator iter_end = BucketBaseCoordVec.end();
for(;iter!=iter_end;iter++)
{
//ÿ<><C3BF>Ͱ<EFBFBD>ĵ<EFBFBD>һ<EFBFBD><D2BB>obj Ϊ<><CEAA>׼
CObjPoint *p = new CObjPoint;
p->SetPt(Dbxy((*iter),(*iter)));//x y<><79><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊһ<CEAA><D2BB>,<2C><><EFBFBD><EFBFBD><EFBFBD>Ͳ<EFBFBD><CDB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
//<2F><><EFBFBD><EFBFBD><E6B5BD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
Sptr<CObjBase> sPtr(p);
vector<Sptr<CObjBase>> Bucket;//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>Ͱ
Bucket.push_back(sPtr);
BucketVec.push_back(Bucket);
}
}
//<2F><><EFBFBD><EFBFBD>Ͱ<EFBFBD><CDB0>׼<EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD>ֺ<EFBFBD><D6BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
void CObjSortMgr::CreatBucketBaseCoordVec(vector<double> &BucketBaseCoordVec,vector<Sptr<CObjBase>> &ObjVec)
{
vector<Sptr<CObjBase>>::iterator iter = ObjVec.begin();
vector<Sptr<CObjBase>>::iterator iter_end = ObjVec.end();
for(;iter!=iter_end;iter++)
{
Dbxy pt = (*iter)->GetCenterPt();//obj <20><><EFBFBD>ĵ<EFBFBD>
double Val = IsHorizontalScan()?pt.x:pt.y;
bool bFlg = true;//<2F>Ƿ<EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
int size = BucketBaseCoordVec.size();
for(int k=0;k<size;k++)
{
if(IsTwoDbEqual(Val,BucketBaseCoordVec[k],BUCKET_EPS))
{
bFlg = false;
break;
}
}
if(bFlg)//ֻ<><D6BB><EFBFBD>µ<EFBFBD>val <20>ż<EFBFBD><C5BC><EFBFBD>
{
BucketBaseCoordVec.push_back(Val);
}
}
}
//<2F>Ѽ<EFBFBD>Ͱ<EFBFBD><CDB0>obj
void CObjSortMgr::CollectBucketObj(vector<vector<Sptr<CObjBase>>> &BucketVec,vector<Sptr<CObjBase>> &ObjVec)
{
vector<vector<Sptr<CObjBase>>>::iterator iter = BucketVec.begin();
vector<vector<Sptr<CObjBase>>>::iterator iter_end = BucketVec.end();
for(;iter!=iter_end;iter++)
{
vector<Sptr<CObjBase>> &Bucket = (*iter);
Dbxy BasePt = Bucket[0]->GetCenterPt();
double BaseVal = IsHorizontalScan()?BasePt.x:BasePt.y;//<2F><>׼ֵ
Bucket.clear();//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׼<EFBFBD><D7BC>obj
vector<Sptr<CObjBase>>::iterator ObjIter = ObjVec.begin();
vector<Sptr<CObjBase>>::iterator ObjIter_end = ObjVec.end();
for(;ObjIter!=ObjIter_end;ObjIter++)
{
Sptr<CObjBase> &Obj = (*ObjIter);
if(!Obj->IsbCollected())//<2F><><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8>ռ<EFBFBD>
{
Dbxy ObjPt = Obj->GetCenterPt();
double ObjVal = IsHorizontalScan()?ObjPt.x:ObjPt.y;//<2F>Ƚ<EFBFBD>ֵ
if(IsTwoDbEqual(BaseVal,ObjVal,BUCKET_EPS))//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͱ<EFBFBD><CDB0>obj
{
Bucket.push_back(Obj);
Obj->SetbCollected(true);//<2F><><EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD>״̬
}
}
}
}
}
//<2F><>ÿ<EFBFBD><C3BF>Ͱ<EFBFBD><CDB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void CObjSortMgr::BucketSort(vector<vector<Sptr<CObjBase>>> &BucketVec)
{
bool bDir = true;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>S <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
vector<vector<Sptr<CObjBase>>>::iterator iter = BucketVec.begin();
vector<vector<Sptr<CObjBase>>>::iterator iter_end = BucketVec.end();
for(;iter!=iter_end;iter++)
{
vector<Sptr<CObjBase>> &Bucket = (*iter);
if(IsHorizontalScan())//<2F><><EFBFBD><EFBFBD>ɨ<EFBFBD>谴Y <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
if(bDir)
sort(Bucket.begin(),Bucket.end(),CompareObjY1);
else
sort(Bucket.begin(),Bucket.end(),CompareObjY2);
}
else//<2F><><EFBFBD><EFBFBD>ɨ<EFBFBD>谴X <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
if(bDir)
sort(Bucket.begin(),Bucket.end(),CompareObjX1);
else
sort(Bucket.begin(),Bucket.end(),CompareObjX2);
}
bDir =!bDir;//<2F><><EFBFBD><EFBFBD>
}
}
//<2F><>Ͱ<EFBFBD><CDB0>obj <20><><EFBFBD><EFBFBD>ObjVec
void CObjSortMgr::BucketToObjVec(vector<vector<Sptr<CObjBase>>> &BucketVec,vector<Sptr<CObjBase>> &ObjVec)
{
ObjVec.clear();
vector<vector<Sptr<CObjBase>>>::iterator iter = BucketVec.begin();
vector<vector<Sptr<CObjBase>>>::iterator iter_end = BucketVec.end();
for(;iter!=iter_end;iter++)
{
vector<Sptr<CObjBase>> &Bucket = (*iter);
vector<Sptr<CObjBase>>::iterator BucketIter = Bucket.begin();
vector<Sptr<CObjBase>>::iterator BuckeIter_end = Bucket.end();
for(;BucketIter!=BuckeIter_end;BucketIter++)
{
ObjVec.push_back(*BucketIter);
}
}
}
#endif
#if 0//<2F>ܽܵ<DCBD><DCB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void CObjSortMgr::PlineSort(vector<vector<Dbxy>> &LineVec)
{
double gl;
double Min_gl=0;
double sum1,sum2;
int n=0;
for (vector<vector<Dbxy>>::size_type i=0;i!=(LineVec.size()-1);++i)
{
Dbxy ed1=LineVec[i].back();
for (vector<vector<Dbxy>>::size_type j=i+1;j!=LineVec.size();++j)
{
Dbxy ed2=LineVec[j].back();
Dbxy beg=LineVec[j].front();
sum1=sqrt((ed1.x-beg.x)*(ed1.x-beg.x)+(ed1.y-beg.y)*(ed1.y-beg.y));
sum2=sqrt((ed1.x-ed2.x)*(ed1.x-ed2.x)+(ed1.y-ed2.y)*(ed1.y-ed2.y));
if (sum2<sum1)
{
reverse(LineVec[j].begin(),LineVec[j].end());
gl=sum2;
}
else
{
gl=sum1;
}
if (j==i+1)
{
Min_gl=gl;
n=j;
}
if (gl<Min_gl)
{
Min_gl=gl;
n=j;
}
}
swap(LineVec[i+1],LineVec[n]);
}
}
#endif