-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPointToPointRouter.cpp
128 lines (114 loc) · 3.31 KB
/
PointToPointRouter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "provided.h"
#include <list>
#include <queue>
#include "ExpandableHashMap.h"
using namespace std;
class PointToPointRouterImpl
{
public:
PointToPointRouterImpl(const StreetMap* sm);
~PointToPointRouterImpl();
DeliveryResult generatePointToPointRoute(
const GeoCoord& start,
const GeoCoord& end,
list<StreetSegment>& route,
double& totalDistanceTravelled) const;
private:
const StreetMap* m_sm;
};
PointToPointRouterImpl::PointToPointRouterImpl(const StreetMap* sm): m_sm(sm)
{
}
PointToPointRouterImpl::~PointToPointRouterImpl()
{
}
DeliveryResult PointToPointRouterImpl::generatePointToPointRoute(
const GeoCoord& start,
const GeoCoord& end,
list<StreetSegment>& route,
double& totalDistanceTravelled) const
{
totalDistanceTravelled = 0;
if (start == end)
{
return DELIVERY_SUCCESS;
}
ExpandableHashMap<GeoCoord, GeoCoord> m_visited;
queue<GeoCoord> geoQ;
vector<StreetSegment> segs;
geoQ.push(start);
m_visited.associate(start, start);
while (!geoQ.empty())
{
GeoCoord a = geoQ.front();
geoQ.pop();
if (a == end)
break;
m_sm->getSegmentsThatStartWith(a, segs);
vector<StreetSegment>::iterator vi = segs.begin();
while (vi != segs.end())
{
if (m_visited.find(vi->end) == nullptr)
{
geoQ.push(vi->end);
m_visited.associate(vi->end, vi->start);
}
vi++;
}
segs.clear();
}
GeoCoord *gc = m_visited.find(end);
GeoCoord nextgc = end;
if(gc != nullptr)
{
route.clear();
while( *gc != start)
{
m_sm->getSegmentsThatStartWith(*gc, segs);
string name = segs[0].name;
for(int i=0; i<segs.size(); i++)
{
if(segs[i].end == nextgc)
{
name = segs[i].name;
}
}
route.push_front(StreetSegment(*gc, nextgc, name ));
totalDistanceTravelled += distanceEarthMiles(*gc, nextgc);
nextgc = *gc;
gc = m_visited.find(nextgc);
}
m_sm->getSegmentsThatStartWith(start, segs);
string name = segs[0].name;
for(int i=0; i<segs.size(); i++)
{
if(segs[i].end == *gc)
{
name = segs[i].name;
}
}
totalDistanceTravelled += distanceEarthMiles(start, nextgc);
route.push_front(StreetSegment(start, nextgc, name ));
return DELIVERY_SUCCESS;
}
return NO_ROUTE;
}
//******************** PointToPointRouter functions ***************************
// These functions simply delegate to PointToPointRouterImpl's functions.
// You probably don't want to change any of this code.
PointToPointRouter::PointToPointRouter(const StreetMap* sm)
{
m_impl = new PointToPointRouterImpl(sm);
}
PointToPointRouter::~PointToPointRouter()
{
delete m_impl;
}
DeliveryResult PointToPointRouter::generatePointToPointRoute(
const GeoCoord& start,
const GeoCoord& end,
list<StreetSegment>& route,
double& totalDistanceTravelled) const
{
return m_impl->generatePointToPointRoute(start, end, route, totalDistanceTravelled);
}