-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgraph.h
More file actions
149 lines (116 loc) · 3.32 KB
/
graph.h
File metadata and controls
149 lines (116 loc) · 3.32 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef GRAPH_H
#define GRAPH_H
#include <vector>
// Holds WGS84 geoedetic coordinates of a vertex
struct GeoNode
{
// c'tors
GeoNode() { }
GeoNode(double x, double y) { lon = x; lat = y; }
GeoNode(int i, double x, double y) { id = i; lon = x; lat = y; }
// math operations
GeoNode operator- (GeoNode& rhs)
{
GeoNode res;
res.lat = this->lat - rhs.lat;
res.lon = this->lon - rhs.lon;
return res;
}
// geocoordinates
int id;
double lat;
double lon;
};
//--------------------------------------------------------------
// Holds an edge
struct GeoEdge
{
int source;
int target;
int weight;
};
//--------------------------------------------------------------
// Naive representation of graph
class GeoGraph {
public:
GeoGraph() {}
GeoEdge GetEdge(size_t index) { return m_edges[index]; }
GeoNode GetNode(size_t index) { return m_nodes[index]; }
protected:
// Return edges for a given NODE via scanning
std::vector<GeoEdge> GetUndirectedEdgesScanned(int node)
{
std::vector<GeoEdge> res;
for(size_t i = 0; i < m_edges.size(); ++i)
{
GeoEdge& e = m_edges[i];
if(e.source == node || e.target == node)
res.push_back(e);
}
return res;
}
std::vector<GeoEdge> GetDirectedEdgesScanned(int node)
{
std::vector<GeoEdge> res;
for(size_t i = 0; i < m_edges.size(); ++i)
{
GeoEdge& e = m_edges[i];
if(e.source == node)
res.push_back(e);
}
return res;
}
std::vector<GeoNode> m_nodes;
std::vector<GeoEdge> m_edges;
};
//--------------------------------------------------------------
struct GeoTurn {
// set of points
GeoNode p0;
GeoNode p1;
GeoNode p2;
// true = right, false = left
bool orientation;
// true = forbidden, false = not forbidden
bool forbidden;
};
//--------------------------------------------------------------
// Static representation of a graph for fast access to arbitrary incident edges
class StaticGeoGraph : public GeoGraph {
public:
// c'tors
StaticGeoGraph() {}
StaticGeoGraph(std::vector<GeoNode> nodes, std::vector<GeoEdge> edges)
{
m_nodes = nodes;
m_edges = edges;
size_t num_nodes = m_nodes.size();
// handle first node
std::vector<GeoEdge> incident_edges = GetDirectedEdgesScanned(0);
size_t num_incident_edges = incident_edges.size();
m_offsets[0] = num_incident_edges;
// handle further nodes
for(int i = 1; i < num_nodes; ++i)
{
std::vector<GeoEdge> incident_edges = GetDirectedEdgesScanned(i);
size_t num_incident_edges = incident_edges.size();
m_offsets[i] = m_offsets[i - 1] + num_incident_edges;
}
}
// accessors
std::vector<GeoEdge> GetEdges(size_t node_index)
{
std::vector<GeoEdge> res;
if(node_index == 0)
{
for(size_t i = 0; i <= m_offsets[0]; ++i)
res.push_back(m_edges[i]);
} else {
for(size_t i = m_offsets[node_index - 1]; i <= m_offsets[node_index]; ++i)
res.push_back(m_edges[i]);
}
}
private:
std::vector<size_t> m_offsets;
};
#endif // GRAPH_H