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
283 lines
5.0 KiB
9 months ago
|
#include "pch.h" // use stdafx.h in Visual Studio 2017 and earlier
|
||
|
#include "BranchTree.h"
|
||
|
|
||
9 months ago
|
BranchTree branchTree;
|
||
|
|
||
9 months ago
|
void Edge::clear()
|
||
|
{
|
||
9 months ago
|
to = 0;
|
||
|
dis = 0;
|
||
|
wirenames.clear();
|
||
|
d.clear();
|
||
|
}
|
||
|
|
||
9 months ago
|
BranchTree::BranchTree()
|
||
|
{
|
||
|
cnt = 0;
|
||
|
bfs = 0;
|
||
|
hashuan = 0;
|
||
|
record = 0;
|
||
|
// branchPointNum=0;
|
||
|
dia = 0;
|
||
|
// bundleid=0;
|
||
|
pclipSet = &clipSet;
|
||
|
}
|
||
9 months ago
|
|
||
9 months ago
|
// 初始化
|
||
9 months ago
|
void BranchTree::init()
|
||
|
{
|
||
|
for (int i = 1; i <= cnt; i++)
|
||
|
{
|
||
9 months ago
|
edge[i].clear();
|
||
|
}
|
||
|
nodeid.clear();
|
||
9 months ago
|
cnt = bfs = hashuan = record = 0;
|
||
9 months ago
|
}
|
||
9 months ago
|
void BranchTree::startRecord()
|
||
|
{
|
||
|
record = 1;
|
||
9 months ago
|
}
|
||
9 months ago
|
void BranchTree::endRecord()
|
||
|
{
|
||
|
record = 0;
|
||
9 months ago
|
pathpoints.clear();
|
||
|
}
|
||
|
|
||
9 months ago
|
int BranchTree::getid(P &p)
|
||
|
{
|
||
|
if (nodeid[p] == 0)
|
||
|
{
|
||
9 months ago
|
nodeid[p] = ++cnt;
|
||
|
f[cnt] = cnt;
|
||
|
sz[cnt] = 1;
|
||
|
vis[cnt] = pre[cnt] = 0;
|
||
|
endpoint[cnt] = 0;
|
||
|
points[cnt] = p;
|
||
|
}
|
||
|
return nodeid[p];
|
||
|
}
|
||
|
|
||
9 months ago
|
int BranchTree::getrt(int x)
|
||
|
{
|
||
|
if (x != f[x])
|
||
|
return f[x] = getrt(f[x]);
|
||
9 months ago
|
return x;
|
||
|
}
|
||
|
|
||
9 months ago
|
void BranchTree::merge(int x, int y)
|
||
|
{
|
||
|
if (x != f[x])
|
||
|
x = getrt(x);
|
||
|
if (y != f[y])
|
||
|
y = getrt(y);
|
||
9 months ago
|
f[x] = y;
|
||
|
sz[y] += sz[x];
|
||
|
}
|
||
|
|
||
9 months ago
|
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;
|
||
9 months ago
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
9 months ago
|
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];
|
||
9 months ago
|
e.d.push_back(dia);
|
||
|
e.wirenames.insert(e.wirenames.begin(), namelist.begin(), namelist.end());
|
||
|
}
|
||
|
}
|
||
9 months ago
|
for (int i = 0; i < edge[y].size(); i++)
|
||
|
{
|
||
|
if (edge[y][i].to == x)
|
||
|
{
|
||
|
Edge &e = edge[y][i];
|
||
9 months ago
|
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);
|
||
|
}
|
||
|
|
||
9 months ago
|
bool BranchTree::hasEdge(int x, int y)
|
||
|
{
|
||
|
for (int i = 0; i < edge[x].size(); i++)
|
||
|
{
|
||
|
if (edge[x][i].to == y)
|
||
|
{
|
||
9 months ago
|
return true;
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
9 months ago
|
vector<int> BranchTree::BFS(int u, int v)
|
||
|
{
|
||
|
// cout<<u<<" "<<v<<endl;
|
||
9 months ago
|
bfs++;
|
||
|
vector<int> path;
|
||
|
queue<int> q;
|
||
|
q.push(u);
|
||
|
vis[u] = bfs;
|
||
|
pre[u] = 0;
|
||
9 months ago
|
while (!q.empty())
|
||
|
{
|
||
|
int x = q.front();
|
||
|
q.pop();
|
||
|
for (int i = 0; i < edge[x].size(); i++)
|
||
|
{
|
||
9 months ago
|
int y = edge[x][i].to;
|
||
9 months ago
|
if (vis[y] == bfs)
|
||
|
continue;
|
||
9 months ago
|
pre[y] = x;
|
||
|
vis[y] = bfs;
|
||
|
q.push(y);
|
||
|
}
|
||
9 months ago
|
if (vis[v] == bfs)
|
||
|
break;
|
||
9 months ago
|
}
|
||
|
int top = 0;
|
||
|
int tag = v;
|
||
9 months ago
|
while (pre[tag])
|
||
|
{
|
||
|
// cout<<tag<<" "<<pre[tag]<<endl;
|
||
9 months ago
|
st[++top] = tag;
|
||
|
tag = pre[tag];
|
||
|
}
|
||
9 months ago
|
if (top)
|
||
|
{
|
||
9 months ago
|
st[++top] = u;
|
||
|
}
|
||
9 months ago
|
// cout<<"stage6"<<endl;
|
||
|
for (int i = top; i > 0; i--)
|
||
|
{
|
||
9 months ago
|
path.push_back(st[i]);
|
||
|
}
|
||
9 months ago
|
// cout<<path[path.size()-1]<<" "<<path[path.size()-2]<<endl;
|
||
|
// cout<<v<<" "<<pre[v]<<" "<<hasEdge(v,pre[v])<<" "<<hasEdge(pre[v],v)<<endl;
|
||
9 months ago
|
|
||
|
return path;
|
||
|
}
|
||
|
|
||
9 months ago
|
void BranchTree::newPath(vector<int> &path, vector<string> namelist)
|
||
|
{
|
||
9 months ago
|
|
||
9 months ago
|
for (int i = 1; i < path.size(); i++)
|
||
|
{
|
||
|
// cout<<hasEdge(path[i],path[i-1])<<endl;
|
||
9 months ago
|
addEdge(path[i], path[i - 1], namelist);
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
|
||
9 months ago
|
// 添加一条路径,返回去除环路的路径
|
||
|
Path BranchTree::addPath(Path &path)
|
||
|
{
|
||
9 months ago
|
Path path2;
|
||
|
|
||
|
dia = path.dia;
|
||
9 months ago
|
if (path.points.size() == 0)
|
||
|
return path2;
|
||
9 months ago
|
int id1 = getid(path.points[0]);
|
||
|
int id2 = getid(path.points[path.points.size() - 1]);
|
||
9 months ago
|
endpoint[id1] = 1;
|
||
|
endpoint[id2] = 1;
|
||
|
if (record)
|
||
|
pathpoints.push_back(id1);
|
||
9 months ago
|
|
||
9 months ago
|
vector<int> pathid;
|
||
9 months ago
|
pathid.push_back(0);
|
||
9 months ago
|
for (int i = 0; i < path.points.size(); i++)
|
||
|
{
|
||
9 months ago
|
P p = path.points[i];
|
||
|
int id = getid(p);
|
||
|
pathid.push_back(id);
|
||
|
}
|
||
9 months ago
|
map<int, int> cross2;
|
||
9 months ago
|
|
||
9 months ago
|
for (int i = 1; i <= path.points.size(); i++)
|
||
|
{
|
||
9 months ago
|
int id = pathid[i];
|
||
|
int rt = getrt(id);
|
||
9 months ago
|
if (sz[rt] != 1)
|
||
|
{
|
||
9 months ago
|
cross2[rt] = i;
|
||
|
}
|
||
|
}
|
||
|
|
||
9 months ago
|
for (int i = 1; i <= path.points.size(); i++)
|
||
|
{
|
||
9 months ago
|
int id = pathid[i];
|
||
|
int rt = getrt(id);
|
||
9 months ago
|
if (cross2[rt] != 0)
|
||
|
{
|
||
9 months ago
|
int en = cross2[rt];
|
||
|
|
||
9 months ago
|
if (en != i)
|
||
|
{
|
||
9 months ago
|
|
||
|
vector<int> tempPath = BFS(pathid[i], pathid[en]);
|
||
|
|
||
|
newPath(tempPath, path.wirelist);
|
||
|
Path path3;
|
||
9 months ago
|
for (int j = 0; j < tempPath.size(); j++)
|
||
|
{
|
||
9 months ago
|
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];
|
||
9 months ago
|
for (int j = 0; j < path3.size(); j++)
|
||
|
{
|
||
9 months ago
|
path2.points.push_back(path3.points[j]);
|
||
|
path2.inOut.push_back(path3.inOut[j]);
|
||
|
}
|
||
9 months ago
|
// if(testhuan())cout<<"BFS"<<endl;
|
||
9 months ago
|
}
|
||
9 months ago
|
else
|
||
|
{
|
||
9 months ago
|
path2.points.push_back(points[pathid[i]]);
|
||
|
path2.inOut.push_back(path.inOut[i - 1]);
|
||
|
}
|
||
9 months ago
|
if (i != 1)
|
||
|
addEdge(pathid[i], pathid[i - 1], path.wirelist);
|
||
9 months ago
|
i = en;
|
||
|
}
|
||
9 months ago
|
else
|
||
|
{
|
||
|
if (i != 1)
|
||
|
addEdge(pathid[i], pathid[i - 1], path.wirelist);
|
||
9 months ago
|
path2.points.push_back(points[pathid[i]]);
|
||
|
path2.inOut.push_back(path.inOut[i - 1]);
|
||
|
}
|
||
9 months ago
|
// if(testhuan())cout<<"加边"<<endl;
|
||
9 months ago
|
}
|
||
|
|
||
9 months ago
|
// path2=path;
|
||
|
// path2.size();
|
||
9 months ago
|
path2.postProcess();
|
||
|
path2.illegalCheck();
|
||
|
basicChannel.addPath(path2);
|
||
|
basicEdge.addPath(path2);
|
||
|
return path2;
|
||
|
}
|