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.
 
 
 

283 lines
5.0 KiB

#include "pch.h" // use stdafx.h in Visual Studio 2017 and earlier
#include "BranchTree.h"
BranchTree branchTree;
void Edge::clear()
{
to = 0;
dis = 0;
wirenames.clear();
d.clear();
}
BranchTree::BranchTree()
{
cnt = 0;
bfs = 0;
hashuan = 0;
record = 0;
// branchPointNum=0;
dia = 0;
// bundleid=0;
pclipSet = &clipSet;
}
// 初始化
void BranchTree::init()
{
for (int i = 1; i <= cnt; i++)
{
edge[i].clear();
}
nodeid.clear();
cnt = bfs = hashuan = record = 0;
}
void BranchTree::startRecord()
{
record = 1;
}
void BranchTree::endRecord()
{
record = 0;
pathpoints.clear();
}
int BranchTree::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 BranchTree::getrt(int x)
{
if (x != f[x])
return f[x] = getrt(f[x]);
return x;
}
void BranchTree::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 BranchTree::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;
}
void BranchTree::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);
}
bool BranchTree::hasEdge(int x, int y)
{
for (int i = 0; i < edge[x].size(); i++)
{
if (edge[x][i].to == y)
{
return true;
}
}
return false;
}
vector<int> BranchTree::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;
}
void BranchTree::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;
}
// 添加一条路径,返回去除环路的路径
Path BranchTree::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;
}