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

271 lines
5.3 KiB

10 months ago
#include "stdafx.h"
#include "BasicEdge.h"
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;
struct Edge{
vector<string> wirenames;
vector<double> d;
int to;
double dis;
void clear(){
to=0;
dis=0;
wirenames.clear();
d.clear();
}
};
//��֧���ṹ���Ѹ���·����Ϣ�����ɷ�֧��Ϣ����������CATIA
struct BranchTree{
ClipSet* pclipSet; //ָ�򿨹���
double dia; //��ʱ����
vector<Edge>edge[N]; //��
P points[MAXPointNum]; //��
int f[N],sz[N]; //���鼯��Ϣ
int pre[N],vis[N],st[N];
bool endpoint[N],hashuan;
//Bundle bundles[N];
//vector<int> bundlesOfClip[N];
bool record;
vector<int>pathpoints;
map<P,int> nodeid; //����ӳ���ڲ�����
int cnt,bfs; //�����������Լ�bfs�Ĵ���
//int bundleid,branchPointNum;
BranchTree(){
cnt=0;bfs=0;
hashuan=0;record=0;
//branchPointNum=0;
dia=0;
//bundleid=0;
pclipSet=&clipSet;
}
//��ʼ��
void init(){
for(int i=1;i<=cnt;i++){
edge[i].clear();
}
nodeid.clear();
cnt=bfs=hashuan=record=0;
}
void startRecord(){
record=1;
}
void endRecord(){
record=0;
pathpoints.clear();
}
inline int getid(P &p){
if(nodeid[p]==0){
nodeid[p]=++cnt;
f[cnt]=cnt;
sz[cnt]=1;
vis[cnt]=pre[cnt]=0;
endpoint[cnt]=0;
points[cnt]=p;
}
return nodeid[p];
}
int getrt(int x){
if(x!=f[x])return f[x]=getrt(f[x]);
return x;
}
inline void merge(int x,int y){
if(x!=f[x])x=getrt(x);
if(y!=f[y])y=getrt(y);
f[x]=y;
sz[y]+=sz[x];
}
bool check(P &p){
int id=getid(p);
for(int i=0;i<pathpoints.size();i++){
if(getrt(id)==getrt(pathpoints[i]))return false;
}
return true;
}
inline void addEdge(int x,int y,vector<string> namelist){
if(record)pathpoints.push_back(x);
for(int i=0;i<edge[x].size();i++){
if(edge[x][i].to==y){
Edge & e=edge[x][i];
e.d.push_back(dia);
e.wirenames.insert(e.wirenames.begin(),namelist.begin(),namelist.end());
}
}
for(int i=0;i<edge[y].size();i++){
if(edge[y][i].to==x){
Edge & e=edge[y][i];
e.d.push_back(dia);
e.wirenames.insert(e.wirenames.begin(),namelist.begin(),namelist.end());
return;
}
}
Edge e;
e.to=y;
e.wirenames.insert(e.wirenames.begin(),namelist.begin(),namelist.end());
e.d.push_back(dia);
edge[x].push_back(e);
e.to=x;
edge[y].push_back(e);
merge(x,y);
pclipSet->addwire(points[y],dia);
}
inline bool hasEdge(int x,int y){
for(int i=0;i<edge[x].size();i++){
if(edge[x][i].to==y){
return true;
}
}
return false;
}
inline vector<int> BFS(int u,int v){
//cout<<u<<" "<<v<<endl;
bfs++;
vector<int> path;
queue<int> q;
q.push(u);
vis[u]=bfs;
pre[u]=0;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=0;i<edge[x].size();i++){
int y=edge[x][i].to;
if(vis[y]==bfs)continue;
pre[y]=x;
vis[y]=bfs;
q.push(y);
}
if(vis[v]==bfs)break;
}
int top=0;
int tag=v;
while(pre[tag]){
//cout<<tag<<" "<<pre[tag]<<endl;
st[++top]=tag;
tag=pre[tag];
}
if(top){
st[++top]=u;
}
//cout<<"stage6"<<endl;
for(int i=top;i>0;i--){
path.push_back(st[i]);
}
//cout<<path[path.size()-1]<<" "<<path[path.size()-2]<<endl;
//cout<<v<<" "<<pre[v]<<" "<<hasEdge(v,pre[v])<<" "<<hasEdge(pre[v],v)<<endl;
return path;
}
inline void newPath(vector<int>& path,vector<string> namelist){
for(int i=1;i<path.size();i++){
//cout<<hasEdge(path[i],path[i-1])<<endl;
addEdge(path[i],path[i-1],namelist);
}
return;
}
//����һ��·��,����ȥ����·��·��
inline Path addPath(Path &path){
Path path2;
dia=path.dia;
if(path.points.size()==0)return path2;
int id1=getid(path.points[0]);
int id2=getid(path.points[path.points.size()-1]);
endpoint[id1]=1;endpoint[id2]=1;
if(record)pathpoints.push_back(id1);
vector<int>pathid;
pathid.push_back(0);
for(int i=0;i<path.points.size();i++){
P p=path.points[i];
int id=getid(p);
pathid.push_back(id);
}
map<int,int>cross2;
for(int i=1;i<=path.points.size();i++){
int id=pathid[i];
int rt=getrt(id);
if(sz[rt]!=1){
cross2[rt]=i;
}
}
for(int i=1;i<=path.points.size();i++){
int id=pathid[i];
int rt=getrt(id);
if(cross2[rt]!=0){
int en=cross2[rt];
if(en!=i){
vector<int> tempPath=BFS(pathid[i],pathid[en]);
newPath(tempPath,path.wirelist);
Path path3;
for(int j=0;j<tempPath.size();j++){
path3.points.push_back(points[tempPath[j]]);
}
path3.processDir();
path3.inOut[0]=path.inOut[i-1];
path3.inOut[path3.size()-1]=path.inOut[en-1];
for(int j=0;j<path3.size();j++){
path2.points.push_back(path3.points[j]);
path2.inOut.push_back(path3.inOut[j]);
}
//if(testhuan())cout<<"BFS"<<endl;
}
else {
path2.points.push_back(points[pathid[i]]);
path2.inOut.push_back(path.inOut[i-1]);
}
if(i!=1) addEdge(pathid[i],pathid[i-1],path.wirelist);
i=en;
}
else {
if(i!=1) addEdge(pathid[i],pathid[i-1],path.wirelist);
path2.points.push_back(points[pathid[i]]);
path2.inOut.push_back(path.inOut[i-1]);
}
//if(testhuan())cout<<"�ӱ�"<<endl;
}
//path2=path;
//path2.size();
path2.postProcess();
path2.illegalCheck();
basicChannel.addPath(path2);
basicEdge.addPath(path2);
return path2;
}
}branchTree;