毕业设计思路篇(一)之交通图的构建
1. 导入道路地图
道路地图来自网络数据,已预处理为xml格式。
<?xml version='1.0' encoding='UTF-8'?>
<Roads>
<road m_ID="71283896--1" len="620.98">
<from lon="113.9205606" lat="22.9317667" id="848388981"/>
<to lon="113.9260573" lat="22.9341232" id="2522072722"/>
</road>
<road m_ID="553852656--2" len="322.34">
<from lon="113.9529339" lat="22.9448978" id="5345735265"/>
<to lon="113.9516926" lat="22.9475618" id="5345735267"/>
</road>
</Roads>
解析:一个road节点代表一条道路,len代表道路抽象长度,from,to子节点分别表示道路两端端点。
2. 解析道路数据
a. 构建交通图
赋值道路端点.
/**
* 路口类,记录着该路口的点坐标,以及其相连的方向道路节点组
* | |
* | | |
* | 1 2 |
* | | |
* -------- --------
* 3 5
* - - - - - - - -
* 4 6
* -------- --------
* | | |
* | 7 8 |
* | | |
* | |
* 如上图(1,2), (3,4), (5,6), (7,8)在同一个方向,我将其称为四组方向道路节点Node.
* 其中,Node.inRoadID=1, Node.outRoadID=2;
* Node.inRoadID=4, Node.outRoadID=3;
* ...
* 根据车辆靠右行原则以此类推.
*/
class CrossRoad {
public:
CrossRoad(float fLon,float fLat) : m_fLat(fLat),m_fLon(fLon){};
/**
* 重载运算符 (==) 判断两个路口是否为同一个
*/
bool operator==(CrossRoad &crossRoad);
/**
* 添加道路节点ID
* @param in 入度
* @param out 出度
* @param atan2 该点与方向道路的atan2值
*/
void addNode(int in,int out,double atan2);
public:
//唯一标示符
int m_nID;
//经纬度的定义
float m_fLon, m_fLat;
vector<Node> JunctionRoad;
//该路口的交通灯
TrafficLight m_CTrafficLight_Light;
};
CrossRoad A,B
添加道路到交通图
void addRoad(Graph &Map_Graph, CrossRoad A, CrossRoad B, double length) {
// 初始化A,B路口的索引位置为-1
int CrossRoadSiteB = -1, CrossRoadSiteA = -1;
auto CrossRoadNum = Map_Graph.m_CrossRoad_v.size(), RoadNum = Map_Graph.m_Road_v.size();
//循环判断是否有重合点
for (int i = 0; i < CrossRoadNum; i++) {
if (Map_Graph.m_CrossRoad_v[i] == A) {
CrossRoadSiteA = i;
}
if (Map_Graph.m_CrossRoad_v[i] == B) {
CrossRoadSiteB = i;
}
}
//如果不存在与A点重合的路口,添加路口,保存路口索引
if (CrossRoadSiteA == -1) {
Map_Graph.m_CrossRoad_v.push_back(A);
CrossRoadSiteA = CrossRoadNum++;
Map_Graph.m_CrossRoad_v[CrossRoadSiteA].m_nID = CrossRoadSiteA;
}
if (CrossRoadSiteB == -1) {
Map_Graph.m_CrossRoad_v.push_back(B);
CrossRoadSiteB = CrossRoadNum++;
Map_Graph.m_CrossRoad_v[CrossRoadSiteB].m_nID = CrossRoadSiteB;
}
int RoadSiteA = RoadNum, RoadSiteB = RoadNum + 1;
// 确定A,B路的site,加入模型图
Road roadA(RoadSiteA, CrossRoadSiteA, CrossRoadSiteB, length);
Map_Graph.m_Road_v.push_back(roadA);
Road roadB(RoadSiteB, CrossRoadSiteB, CrossRoadSiteA, length);
Map_Graph.m_Road_v.push_back(roadB);
// 对接A,B路口节点数据
Map_Graph.m_CrossRoad_v[CrossRoadSiteA].addNode(RoadSiteA, RoadSiteB,
atan2((B.m_fLat - A.m_fLat), (B.m_fLon - A.m_fLon)));
Map_Graph.m_CrossRoad_v[CrossRoadSiteB].addNode(RoadSiteB, RoadSiteA,atan2((A.m_fLat - B.m_fLat), (A.m_fLon - B.m_fLon)));
}
解析的结果如下
road.txt
RoadTable.txt
版权声明: (https://www.thinkmoon.cn/post/70)
本文首发于指尖魔法屋-毕业设计思路篇(一)之交通图的构建
转载或引用必须申明原指尖魔法屋来源及源地址!