-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
190 lines (156 loc) · 6.48 KB
/
main.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <utility>
#include <cstdint>
/*
* Containers algorithms and iterators ->std::for_each( TODO: use functor
* c++11 -> lambda
* Metaprogramming check of types in SetRequiredComponents
* TODO: Throw Render exception hvis component mangler
* TODO: Change CreateCarEntity to use speed instead of velocty
* Namespaces
*/
#include <iostream>
#include "Components/Render.h"
#include "Managers/World.h"
#include "Systems/Logger.h"
#include "Systems/Drive.h"
#include "Components/Movement.h"
#include "Systems/Render.h"
#include "Components/Path.h"
#include "Systems/TrafficLight.h"
#include "Components/TrafficLight.h"
#include "Components/Node.h"
#include "Helpers/Visitor.h"
#include "Systems/VehicleCollisionPrevention.h"
#include "Components/RoadGraph.h"
#include "Helpers/Roads.h"
#include "Systems/TrafficGeneration.h"
using namespace Ecs::Components;
using namespace Ecs::Systems;
using namespace Ecs::DataStructures;
using namespace Ecs::Helpers;
void Loop();
Graph CreateGraph(std::vector<Edge> &edges);
Path GetPath(Graph g, int startPointId, int endpointId);
static World world;
Logger logger(world);
Drive drive(world);
Ecs::Systems::Render render(world, Loop);
Ecs::Systems::TrafficLight trafficLight(world);
VehicleCollisionPrevention vcp(world);
std::unique_ptr<TrafficGeneration> trafficGeneration;
// TODO: Move path to first parameter
void CreateCarEntity(int x, int y, int speed, Path path, Color color) {
auto entity = world.CreateEntity();
world.AddComponent(Movement(speed), entity);
world.AddComponent(Transform(x, y, 0), entity);
world.AddComponent(std::move(path), entity);
world.AddComponent(Ecs::Components::Render("car", color), entity);
}
int CreateTrafficLightEntity(int x, int y) {
auto entity = world.CreateEntity();
world.AddComponent(Ecs::Components::TrafficLight(), entity);
world.AddComponent(Transform(x, y), entity);
world.AddComponent(Ecs::Components::Render("trafficLight", Color(0.7, 0.7, 0.7)), entity);
return entity.GetId();
}
void CreateRoadGraphEntity(std::vector<Edge> edges) {
auto entity = world.CreateEntity();
world.AddComponent(RoadGraph(edges), entity);
world.AddComponent(Transform(), entity);
world.AddComponent(Ecs::Components::Render("roadGraph", Color(0.7, 0.7, 0.7)), entity);
}
int main() {
auto A = CreateTrafficLightEntity(-5000, 5000);
auto B = CreateTrafficLightEntity(-5000, -5000);
auto C = CreateTrafficLightEntity(0, 5000);
auto D = CreateTrafficLightEntity(0, -5000);
auto E = CreateTrafficLightEntity(5000, 5000);
auto F = CreateTrafficLightEntity(5000, -5000);
std::vector<Edge> edges;
edges.emplace_back(A, C);
edges.emplace_back(D, B);
edges.emplace_back(C, D);
edges.emplace_back(C, E);
edges.emplace_back(D, F);
edges.emplace_back(E, F);
auto g = CreateGraph(edges);
CreateRoadGraphEntity(edges);
int StartPoints[] = {A,B,C,D,E,F};
trafficGeneration = std::make_unique<TrafficGeneration>(world, g, StartPoints,6);
//CreateCarEntity(-5500, 5050, 30, GetPath(g, A, F), Color(0, 0, 1));
//CreateCarEntity(-6000, 5050, 35, GetPath(g, A, F), Color(1, 1, 0));
//CreateCarEntity(5800, -5000, 30, GetPath(g, F, C), Color(1, 0, 0));
//CreateCarEntity(6020, -5100, 20, GetPath(g, F, A), Color(1, 1, 0));
//CreateCarEntity(5000, 5000, 30, GetPath(g, E, A), Color(0, 0, 1));
//CreateCarEntity(-6500, 5050, 30, GetPath(g, A, E), Color(0, 0, 1));
//CreateCarEntity(-5050, -5050, 30, GetPath(g, B, C), Color(0, 0, 1));
render.Start();
}
Path GetPath(Graph g, int startPointId, int endpointId) {
// vector for storing distance property
std::vector<int> d(num_vertices(g));
std::vector<Vertex> p(num_vertices(g), boost::graph_traits<Graph>::null_vertex()); //the predecessor array
// get the first vertex
Vertex s = *(vertices(g).first);
// invoke variant 2 of Dijkstra's algorithm
dijkstra_shortest_paths(g, endpointId, boost::distance_map(&d[0]).visitor(Visitor(&p[0])));
std::cout << "parents in the tree of shortest paths:" << std::endl;
for (auto vi = vertices(g).first; vi != vertices(g).second; ++vi) {
std::cout << "parent(" << *vi;
if (p[*vi] == boost::graph_traits<Graph>::null_vertex())
std::cout << ") = no parent" << std::endl;
else
std::cout << ") = " << p[*vi] << std::endl;
}
std::cout << "distances from start vertex:" << std::endl;
boost::graph_traits<Graph>::vertex_iterator vi;
for (vi = vertices(g).first; vi != vertices(g).second; ++vi)
std::cout << d[*vi] << std::endl;
std::cout << std::endl;
Path path;
for (int i = startPointId; i != endpointId; i = p[i]) {
path.Nodes.emplace_back(i);
}
path.Nodes.emplace_back(endpointId);
for (int j = 0; j < path.Nodes.size(); ++j) {
int id = path.Nodes[j].trafficLightEntityId;
auto primaryLight = world.GetComponent<Transform>(id);
if (j != 0) {
auto lastLight = world.GetComponent<Transform>(path.Nodes[j - 1].trafficLightEntityId);
path.Nodes[j].entrancePoint = Roads::GetEntrypoint(lastLight.X, lastLight.Y, primaryLight.X,
primaryLight.Y);
}
if (j != (path.Nodes.size() - 1)) {
auto nextLight = world.GetComponent<Transform>(path.Nodes[j + 1].trafficLightEntityId);
path.Nodes[j].exitPoint = Roads::GetExitPoint(primaryLight.X, primaryLight.Y, nextLight.X, nextLight.Y);
}
}
path.Nodes[0].entrancePoint = (path.Nodes[0].exitPoint+2) %4;
path.Nodes[path.Nodes.size() -1].exitPoint = (path.Nodes[path.Nodes.size() -1].entrancePoint+2) %4;
return path;
}
int GetWeight(Edge edge) {
auto trafficlight1Transform = world.GetComponent<Transform>(edge.first);
auto trafficlight2Transform = world.GetComponent<Transform>(edge.second);
auto x = trafficlight1Transform.X - trafficlight2Transform.X;
auto y = trafficlight1Transform.Y - trafficlight2Transform.Y;
return std::sqrt(x * x + y * y);
}
Graph CreateGraph(std::vector<Edge> &edges) {
const unsigned int num_vertices = edges.size();
int weights[num_vertices];
int j = 0;
for (auto i = edges.begin(); i != edges.end(); ++i) {
weights[j] = GetWeight(*i.base());
j++;
}
return Graph(&edges[0], &edges[num_vertices], weights, num_vertices);
}
void Loop() {
//logger.Update();
trafficLight.Update();
drive.Update();
vcp.Update();
trafficGeneration->Update();
render.Update();
}