This repository has been archived by the owner on Apr 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TrafficSimulation.cpp
1202 lines (1085 loc) · 46.4 KB
/
TrafficSimulation.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// custom libs
#include "ParserXML.h"
#include "TrafficSimulation.h"
// c++ libs
#include <stdexcept>
#include <typeinfo>
#include <sstream>
#include <filesystem>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
using namespace std;
//==== Constructors and Destructor ====//
TrafficSimulation::TrafficSimulation(const string &filename) : filename(filename), time(0), stopTime(0) {
REQUIRE(*typeid(filename).name() == 'N' , "constructor was called with invalid filename");
_initCheck = this;
dummyRoad = new Road();
dummyRoad->setLength(0);
dummyRoad->setRoadName("Dummy");
if (filename.find(XMLL) != string::npos or filename.find(XMLU) != string::npos){
parseXML();
}
else if (filename.find(JSONU) != string::npos or filename.find(JSONL) != string::npos){
parseJSON();
}
else {
cout << "No compatible parser for this type of file" << endl;
}
ENSURE(TrafficSimulation::filename == filename , "filename was not properly initialized");
ENSURE(properlyInitialized(), "constructor must end in properlyInitialized state");
}
TrafficSimulation::TrafficSimulation(): time(0), stopTime(0) {
_initCheck = this;
dummyRoad = new Road();
dummyRoad->setLength(0);
dummyRoad->setRoadName("Dummy");
ENSURE(properlyInitialized(), "constructor must end in properlyInitialized state");
}
bool TrafficSimulation::properlyInitialized() const{
return _initCheck == this;
}
const vector<VehicleGenerator *> &TrafficSimulation::getVehicleGenerators() {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling getVehicleGenerators");
return vehicleGenerators;
}
const vector<Road *> & TrafficSimulation::getRoads() {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling getRoads");
return roads;
}
void TrafficSimulation::setRoads(const vector<Road *> &newRoads) {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling setRoads");
REQUIRE(*typeid(newRoads).name() == 'S' , "setRoads was called with invalid parameter");
TrafficSimulation::roads = newRoads;
ENSURE(TrafficSimulation::roads == newRoads , "setRoads failed");
}
const vector<TrafficLight *> &TrafficSimulation::getLights() {
REQUIRE(this->properlyInitialized(), "TrafficSimulation wasn't properly initialized when calling getLights");
return lights;
}
vector<Vehicle *> TrafficSimulation::getVehicles(){
REQUIRE(this->properlyInitialized(), "TrafficSimulation wasn't properly initialized when calling getVehicles");
return this->vehicles;
}
void TrafficSimulation::addTrafficLight(TrafficLight *&newLight) {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling addTrafficLight");
REQUIRE(*typeid(newLight).name() == 'P' , "addTrafficLight was called with invalid parameter");
REQUIRE(newLight->properlyInitialized() , "addTrafficLight was called with uninitialized parameter");
unsigned int* oldSize = new unsigned int;
*oldSize = lights.size();
this->lights.push_back(newLight);
ENSURE(*oldSize == lights.size() - 1 , "addTrafficLight failed");
delete oldSize;
}
void TrafficSimulation::addVehicle(Vehicle *&newVehicle) {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling addVehicle");
REQUIRE(*typeid(newVehicle).name() == 'P' , "addVehicle was called with invalid parameter");
REQUIRE(newVehicle->properlyInitialized() , "addVehicle was called with uninitialized parameter");
unsigned int* oldSize = new unsigned int;
*oldSize = vehicles.size();
this->vehicles.push_back(newVehicle);
ENSURE(*oldSize == vehicles.size() - 1 , "addVehicle failed");
delete oldSize;
}
bool TrafficSimulation::addRoad(Road *newRoad) {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling addRoad");
REQUIRE(*typeid(newRoad).name() == 'P' , "addRoad was called with invalid parameter");
REQUIRE(newRoad->properlyInitialized() , "addRoad was called with uninitialized parameter");
unsigned int* oldSize = new unsigned int;
*oldSize = static_cast<unsigned int>(roads.size());
for (unsigned int i = 0; i < this->roads.size(); ++i) {
if(this->roads[i] == newRoad){
ENSURE(*oldSize == roads.size() , "addRoad: vector modified when it shouldn't");
return false;
}
}
this->roads.push_back(newRoad);
ENSURE(*oldSize == roads.size() - 1 , "addRoad failed");
delete oldSize;
return true;
}
bool TrafficSimulation::addVehicleGenerator(VehicleGenerator *newVehicleGenerator) {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling addVehicleGenerator");
REQUIRE (*typeid(newVehicleGenerator).name() == 'P' , "addVehicleGenerator was called with invalid parameter");
REQUIRE (newVehicleGenerator->properlyInitialized() , "addVehicleGenerator was called with uninitialized parameter");
unsigned int* oldSize = new unsigned int;
*oldSize = static_cast<unsigned int>(vehicleGenerators.size());
// Check if the vehicle generator is already present
for (unsigned int i = 0; i < this->vehicleGenerators.size(); ++i) {
if (this->vehicleGenerators[i]->getRoad() == newVehicleGenerator->getRoad()){
ENSURE(*oldSize == vehicleGenerators.size() , "addVehicleGenerator : vector modified when it shouldn't");
return false;
}
}
this->vehicleGenerators.push_back(newVehicleGenerator);
ENSURE(*oldSize == vehicleGenerators.size() - 1 , "addVehicleGenerator failed");
return true;
}
const vector<CrossRoad *> &TrafficSimulation::getCrossRoads(){
REQUIRE(this->properlyInitialized(), "TrafficSimulation wasn't properly initialized when calling getCrossRoads");
return crossRoads;
}
const string &TrafficSimulation::getFilename(){
REQUIRE(this->properlyInitialized(), "TrafficSimulation wasn't properly initialized when calling getFilename");
return filename;
}
Road *TrafficSimulation::getDummyRoad() const {
REQUIRE(this->properlyInitialized(), "TrafficSimulation wasn't properly initialized when calling getDummyRoad");
return dummyRoad;
}
void TrafficSimulation::setStopTime(int newStopTime) {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling setStopTime");
TrafficSimulation::stopTime = newStopTime;
ENSURE(newStopTime == stopTime, "stopTime was not assigned to newStopTime, when calling setStopTime");
}
void TrafficSimulation::parseXML() {
REQUIRE(this->properlyInitialized(), "Trafficsimulation is properly initialized when calling parseXML");
parseTrafficSimulationX(*this);
}
void TrafficSimulation::parseJSON() {
REQUIRE(this->properlyInitialized(), "Trafficsimulation is properly initialized when calling parseJSON");
// parse JSON file
}
void TrafficSimulation::addCrossRoad(CrossRoad* crossRoad) {
REQUIRE(this->properlyInitialized(), "TrafficSimulation wasn't properly initialized when calling addCrossRoad");
REQUIRE(*typeid(crossRoad).name() == 'P' , "addCrossRoad was called with invalid parameter: wrong type");
REQUIRE(crossRoad->properlyInitialized(), "crossRoad wasn't properly initialized when calling addCrossRoad");
crossRoads.push_back(crossRoad);
}
void TrafficSimulation::addBusStop(BusStop* busStop) {
REQUIRE(this->properlyInitialized(), "TrafficSimulation wasn't properly initialized when calling addBusStop");
REQUIRE(*typeid(busStop).name() == 'P' , "addBusStop was called with invalid parameter : wrong type");
REQUIRE(busStop->properlyInitialized(), "busstop wasn't properly initialized when calling addBusStop");
busStops.push_back(busStop);
}
void addSpaces(fstream &file, int spaceAmount){
for (int i = 0; i < spaceAmount; ++i) {
file << " ";
}
}
void TrafficSimulation::outputFile(fileFunctionType type, int timestamp) {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling outpputFile");
REQUIRE(type == create || type == update || type == closing, "type not matching with any of the file functions");
// find file name
string newFileName = OUTPUT_DIRECTORY + filename.substr(0, filename.find('.'));
string newFileNameHTML = newFileName + HTMLL;
newFileName += TXTL;
if (type == create){
// create file
FILE* codefile;
// TXT
bool doesntExists = true;
fstream outputNewFile;
int i = 0;
while (doesntExists){
i++;
codefile = fopen(newFileName.c_str(), "r");
if (codefile){
newFileName = OUTPUT_DIRECTORY + filename.substr(0, filename.size() - 4);
stringstream intStr;
intStr << i;
newFileName += '(';
newFileName += intStr.str();
newFileName += ')';
newFileName += TXTL;
}
else {
outputNewFile.open(newFileName.c_str(), ios::app | ios::ate);
outputFileName = newFileName;
doesntExists = false;
}
}
// HTML
doesntExists = true;
fstream outputNewFileHTML;
i = 0;
while (doesntExists){
i++;
codefile = fopen(newFileNameHTML.c_str(), "r");
if (codefile){
newFileNameHTML = OUTPUT_DIRECTORY + filename.substr(0, filename.size() - 4);
stringstream intStr;
intStr << i;
newFileNameHTML += '(';
newFileNameHTML += intStr.str();
newFileNameHTML += ')';
newFileNameHTML += HTMLL;
}
else {
outputNewFileHTML.open(newFileNameHTML.c_str(), ios::app | ios::ate);
outputFileNameHTML = newFileNameHTML;
doesntExists = false;
}
}
// write in file
// TXT
outputNewFile << "TrafficSimulation: " << filename << '\n' << '\n';
// HTML
outputNewFileHTML << "<!DOCTYPE html>\n"
"<html>\n"
"<head>\n"
"<title>";
outputNewFileHTML << "TrafficSimulation: " << filename << "</title>\n" << '\n';
outputNewFileHTML << "</head>\n"
"<body>\n";
outputNewFileHTML << "<h1>";
outputNewFileHTML << "TrafficSimulation: " << filename << "</h1>\n" << '\n';
// close file
outputNewFile.close();
outputNewFileHTML.close();
ENSURE(!outputNewFile.is_open(), "File is still open");
ENSURE(!outputNewFileHTML.is_open(), "File is still open");
codefile = fopen(outputFileName.c_str(), "r");
ENSURE(codefile, "TXT outputfile doesn't exist");
codefile = fopen(outputFileNameHTML.c_str(), "r");
ENSURE(codefile, "HTML outputfile doesn't exist");
}
else if (type == update) {
// open file
fstream outputFile;
fstream outputFileHTML;
outputFile.open(outputFileName.c_str(),ios::app | ios::ate | ios::binary);
outputFileHTML.open(outputFileNameHTML.c_str(), ios::app | ios::ate | ios::binary);
FILE* codefile;
codefile = fopen(outputFileName.c_str(), "r");
REQUIRE(codefile, "TXT outputfile doesn't exist");
codefile = fopen(outputFileNameHTML.c_str(), "r");
REQUIRE(codefile, "HTML outputfile doesn't exist");
if (outputFile.fail()) {
cout << "TXT File doesn't exist" << endl;
}
if (outputFileHTML.fail()) {
cout << "HTML File doesn't exist" << endl;
}
// write file
if(roads.empty()){
outputFile << "No roads in this simulation\n";
outputFileHTML << "<p>No roads in this simulation</p>";
}
else {
outputFile << "Time: ";
outputFile << timestamp;
outputFile << '\n';
outputFileHTML << "<h2>";
outputFileHTML << "Time: ";
outputFileHTML << timestamp;
outputFileHTML << "</h2>\n";
for (unsigned int j = 0; j < roads.size(); ++j) {
int nameLenghth = 26;
int roadLenghth = roads[j]->getLength();
nameLenghth -= roads[j]->getRoadName().size();
string roadnm = roads[j]->getRoadName();
outputFile << roads[j]->getRoadName();
addSpaces(outputFile, nameLenghth);
outputFile << "| ";
outputFileHTML << "<h3>";
outputFileHTML << roads[j]->getRoadName();
addSpaces(outputFileHTML, nameLenghth);
outputFileHTML << "| ";
for (int k = 0; k < roadLenghth; ++k) {
int count = 0;
for (int l = 0; l < roads[j]->getVehicleAmount(); ++l) {
if (k >= (int) roads[j]->getVehicle(l)->getVehiclePosition() and
k < (int) roads[j]->getVehicle(l)->getVehiclePosition() + POLICE_LENGTH and
roads[j]->getVehicle(l)->getType() == T_POLICE) {
outputFile << "P";
outputFileHTML << "<span style=\"color: #0000ff\">P</span>";
} else if (k >= (int) roads[j]->getVehicle(l)->getVehiclePosition() and
k < (int) roads[j]->getVehicle(l)->getVehiclePosition() + AMBULANCE_LENGTH and
roads[j]->getVehicle(l)->getType() == T_AMBULANCE) {
outputFile << "Z";
outputFileHTML << "<span style = \"color: #00ff00\">Z</span>";
} else if (k >= (int) roads[j]->getVehicle(l)->getVehiclePosition() and
k < (int) roads[j]->getVehicle(l)->getVehiclePosition() + LENGTH and
roads[j]->getVehicle(l)->getType() == T_AUTO) {
outputFile << "A";
outputFileHTML << "<span style = \" color: #fd7924\">A</span>";
} else if (k >= (int) roads[j]->getVehicle(l)->getVehiclePosition() and
k < (int) roads[j]->getVehicle(l)->getVehiclePosition() + BUS_LENGTH and
roads[j]->getVehicle(l)->getType() == T_BUS) {
outputFile << "B";
outputFileHTML << "<span style = \" color: #ffff00\">B</span>";
} else if (k >= (int) roads[j]->getVehicle(l)->getVehiclePosition() and
k < (int) roads[j]->getVehicle(l)->getVehiclePosition() + FIRETRUCK_LENGTH and
roads[j]->getVehicle(l)->getType() == T_FIRETRUCK) {
outputFile << "F";
outputFileHTML << "<span style = \"color: #ff0000\">F</span>";
} else {
count++;
}
}
if (count == roads[j]->getVehicleAmount()) {
outputFile << "=";
outputFileHTML << "=";
}
}
outputFileHTML << "</h3>\n";
outputFile << '\n';
if (roads[j]->getTrafficLightsAmount() != 0) {
outputFile << '\t';
outputFile << "> verkeerslichten";
addSpaces(outputFile, 5);
outputFile << "| ";
outputFileHTML << "<h3>";
outputFileHTML << '\t';
outputFileHTML << "> verkeerslichten";
outputFileHTML << " |";
outputFileHTML << "<span style = \" color: #ffffff\">";
for (int i = 0; i < roadLenghth; ++i) {
int lightsAmount = 0;
for (int k = 0; k < roads[j]->getTrafficLightsAmount(); ++k) {
if (roads[j]->getTrafficLights()[k]->getPosition() == (unsigned) i) {
if (roads[j]->getTrafficLight(k)->getCurrentColor() == green) {
outputFile << "G";
outputFileHTML << "</span><span style = \" color: #00ff00\">G</span><span style = \" color: #ffffff\">";
} else {
outputFile << "R";
outputFileHTML << "</span><span style = \" color: #ff0000\">R</span><span style = \" color: #ffffff\">";
}
} else if (roads[j]->getTrafficLight(k)->getPosition() - STOPPING_DISTANCE == (unsigned) i) {
outputFile << '|';
outputFileHTML << "</span>|<span style = \" color: #ffffff\">";
} else {
lightsAmount++;
}
}
if (lightsAmount == roads[j]->getTrafficLightsAmount()) {
outputFile << ' ';
outputFileHTML << "=";
}
}
outputFileHTML << "</span></h3>\n";
outputFile << '\n';
}
if (!roads[j]->getBusStops().empty()) {
outputFile << '\t';
outputFile << "> bushaltes";
addSpaces(outputFile, 11);
outputFile << "| ";
outputFileHTML << "<h3>";
outputFileHTML << '\t';
outputFileHTML << "> bushaltes";
outputFileHTML << " | ";
outputFileHTML << "<span style = \" color: #ffffff\">";
for (int i = 0; i < roadLenghth; ++i) {
bool bushalteExist = false;
for (unsigned int k = 0; k < roads[j]->getBusStops().size(); ++k) {
if (roads[j]->getBusStops()[k]->getPosition() == i) {
outputFile << "B";
outputFileHTML
<< "</span><span style = \"color: #FFA500 ; background-color: #ffff00\">B</span><span style = \" color: #ffffff\">";
bushalteExist = true;
} else if (roads[j]->getBusStops()[k]->getPosition() - STOPPING_DISTANCE == i) {
outputFile << "|";
outputFileHTML << "</span>|<span style = \" color: #ffffff\"><span style = \" color: #ffffff\">";
bushalteExist = true;
}
}
if (!bushalteExist) {
outputFile << " ";
outputFileHTML << "=";
}
}
outputFileHTML << "</span></h3>\n";
outputFile << '\n';
}
if (!roads[j]->getCrossRaods().empty()) {
outputFile << '\t';
outputFile << "> kruispunten";
addSpaces(outputFile, 9);
outputFile << " | ";
outputFileHTML << "<h3>";
outputFileHTML << '\t';
outputFileHTML << "> kruispunten";
outputFileHTML << "| ";
outputFileHTML << "<span style = \" color: #ffffff\">";
for (int i = 0; i < roadLenghth; ++i) {
bool crossRoadExist = false;
for (unsigned int k = 0; k < roads[j]->getCrossRaods().size(); ++k) {
map<Road *, int>::iterator it = roads[j]->getCrossRaods().at(k)->getRoads().begin();
while (it != roads[j]->getCrossRaods().at(k)->getRoads().end()) {
if (it->first == roads[j] and i == it->second) {
outputFile << "K";
outputFileHTML << "</span>K<span style = \" color: #ffffff\">";
crossRoadExist = true;
break;
}
it++;
}
}
if (!crossRoadExist) {
outputFile << " ";
outputFileHTML << "=";
}
}
outputFileHTML << "</span></h3>\n";
outputFile << "\n";
}
}
}
// close file
outputFile.close();
ENSURE(!outputFile.is_open(), "TXT-file is closed");
outputFileHTML.close();
ENSURE(!outputFileHTML.is_open(), "HTML-file is closed");
}
else {
// open file
fstream outputFile;
outputFile.open(newFileNameHTML.c_str(), ios::app | ios::ate | ios::binary);
FILE* codefile;
codefile = fopen(outputFileNameHTML.c_str(), "r");
REQUIRE(codefile, "HTML outputfile doesn't exist");
// write file
outputFile << "</body>\n"
"</html>";
// close file
outputFile.close();
ENSURE(!outputFile.is_open(), "HTML-file is closed");
}
}
void TrafficSimulation::outputStats() {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling outputsStats");
fstream file("Stats.txt");
file.open("Stats/Stats.txt", fstream::out | fstream::in | ios::trunc);
file << "Amount of vehicles: ";
file << vehicles.size();
file << '\n';
file << "Amount of roads: ";
file << roads.size();
file << '\n';
file << "Amount of bus stops: ";
file << busStops.size();
file << '\n';
file << "Amount of crossroads: ";
file << crossRoads.size();
file << '\n';
file << "Amount of VehicleGenerators: ";
file << vehicleGenerators.size();
file << '\n';
file << "File is properly initialized: ";
file << boolalpha << (_initCheck == this) << '\n';
file.close();
ENSURE(!file.is_open(), "file is still open when ending outputStats");
}
void TrafficSimulation::printAll() {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling printAll");
if (!this->roads.empty()){
if(this->roads.size() == 1){
cout <<"Consists of " << this->roads.size() << " road: " << endl;
}
else{
cout <<"Consists of " << this->roads.size() << " roads: " << endl;
}
for(unsigned int i = 0; i < this->roads.size(); i++){
cout << endl << "--------------------";
cout << endl;
this->roads[i]->print();
cout << endl;
if(this->roads[i]->getTrafficLightsAmount() == 1){
cout << "Consist of " << this->roads[i]->getTrafficLightsAmount() << " traffic light." << endl;
}
else{
cout << "Consist of " << this->roads[i]->getTrafficLightsAmount() << " traffic lights." << endl;
}
cout << endl;
for (int j = 0; j < this->roads[i]->getTrafficLightsAmount(); ++j) {
cout << "Traffic light " << j + 1 << ":" << endl;
this->roads[i]->getTrafficLight(j)->print();
}
cout << endl;
if(this->roads[i]->getVehicleAmount() == 1){
cout << "Consist of " << this->roads[i]->getVehicleAmount() << " vehicle." << endl;
}
else{
cout << "Consist of " << this->roads[i]->getVehicleAmount() << " vehicles." << endl;
}
cout << endl;
for (int k = 0; k < this->roads[i]->getVehicleAmount(); ++k) {
cout << "Vehicle " << k + 1 << ":" << endl;
this->roads[i]->getVehicle(k)->print();
cout << endl;
}
if(this->vehicleGenerators.size() == 1){
cout << "Consists of " << vehicleGenerators.size() << " Vehicle Generator: " << endl;
}
else if(this->vehicleGenerators.size() > 1){
cout << "Consists of " << vehicleGenerators.size() << " Vehicle Generators: " << endl;
}
cout << endl;
for (long unsigned int l = 0; l < this->vehicleGenerators.size(); ++l) {
cout << "Vehicle Generator " << l << " : " << endl;
cout << '\t' << "-> Road: " << this->vehicleGenerators[l]->getRoad()->getRoadName() << endl;
cout << '\t' << "-> Frequency: " << this->vehicleGenerators[l]->getFrequentie() << endl;
cout << endl;
}
}
}
else{
cout << "There are no roads." << endl;
}
}
void TrafficSimulation::print( int &count) {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling print");
if (!this->vehicles.empty() || !this->vehicleGenerators.empty()){
cout << "Time: " << count << endl;
for (long unsigned int i = 0; i < this->vehicles.size(); ++i) {
cout << "Vehicle " << i+1 << ": " << endl;
this->vehicles.at(i)->print();
cout << endl;
}
}
}
void TrafficSimulation::startSimulation(bool printE, bool outputE, bool countE, bool timeE , bool visualise, bool override, bool lighting, viewPosition viewPos) {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling startSimulation");
// Declare variables
int count = time;
double vehiclePosition;
int roadLength;
Vehicle* currentVehicle;
Road* currentRoad;
if (printE){
cout << "- Starting simulation" << endl;
}
if (outputE){
outputFile(create);
}
while (!this->vehicles.empty() || !this->vehicleGenerators.empty()){
// Simulate vehicle generators
for (unsigned int l = 0; l < this->vehicleGenerators.size(); l++){
VehicleGenerator* currentGenerator;
currentGenerator = this->vehicleGenerators.at(l);
// Check if we can generate
if (currentGenerator->simulate())
{
// Generate new vehicle
Vehicle* newVehicle;
newVehicle = new Vehicle();
// Add road to vehicle
newVehicle->setRoad(currentGenerator->getRoad());
// Add vehicle to road
newVehicle->setRoad(currentGenerator->getRoad());
currentGenerator->getRoad()->addVehicle(newVehicle);
// Add vehicle to our simulation
this->addVehicle(newVehicle);
}
}
// Simulate traffic lights
for (unsigned int j = 0; j < this->lights.size(); ++j) {
// Simulate traffic light
currentRoad = lights.at(j)->getRoad();
if(currentRoad->getVehicleAmount() > 0){
this->lights.at(j)->simulate(count);
}
}
// Simulate bus stops
for (unsigned int k = 0; k < this->busStops.size(); ++k) {
this->busStops.at(k)->simulateBusStop();
}
// Simulate vehicles
for (unsigned int i = 0; i < this->vehicles.size(); ++i){
// Get current vehicle
currentVehicle = this->vehicles.at(i);
// Get current road
currentRoad = currentVehicle->getRoad();
// Simulate vehicle
currentVehicle->simulate();
// Get vehicle position
vehiclePosition = currentVehicle->getVehiclePosition();
roadLength = currentRoad->getLength();
// Check if vehicle had gone off the road
if (vehiclePosition > roadLength){
// Remove the vehicle from the simulation
currentRoad->removeVehicle(currentVehicle);
delete currentVehicle;
this->vehicles.erase(vehicles.begin() + i);
}
}
// Simulate crossroads
for (unsigned int i = 0; i < this->crossRoads.size(); i++){
CrossRoad* currentCross = this->crossRoads.at(i);
currentCross->simulateCrossroad(false , count);
}
// Execute required outputs
if(printE){
print(count);
}
if(outputE){
outputFile(update, count);
}
if (countE and this->vehicles.size() == MAX_VEHICLES * this->getVehicleGenerators().size()){
break;
}
if (timeE and count == stopTime){
break;
}
count ++;
time = count;
if(time % FPS == 0 && visualise){
generateIni(override , lighting , viewPos);
generateImage();
}
}
if (printE){
cout << "- There are no vehicles on the road network." << endl;
cout << "- Ending simulation" << endl;
}
if (outputE){
outputFile(closing);
}
if (!countE and !timeE){
ENSURE(vehicles.empty() , "Simulation ended when it shouldn't");
} else if (countE){
ENSURE(vehicles.size() == MAX_VEHICLES * this->getVehicleGenerators().size(), "Amount of vehicles on road is not the same as what expected");
}
}
void TrafficSimulation::setVehicles(const vector<Vehicle *> &newvehicles) {
TrafficSimulation::vehicles = newvehicles;
}
TrafficSimulation::~TrafficSimulation() {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling destructor");
// Deleting every object, from memory
for (unsigned int i = 0; i < roads.size(); ++i) {
delete roads[i];
}
for (unsigned int i = 0; i < lights.size(); ++i) {
delete lights[i];
}
for (unsigned int i = 0; i < busStops.size(); ++i) {
delete busStops[i];
}
for (unsigned int i = 0; i < crossRoads.size(); ++i) {
delete crossRoads[i];
}
for (unsigned int i = 0; i < vehicles.size(); ++i) {
delete vehicles[i];
}
for (unsigned int i = 0; i < vehicleGenerators.size(); ++i) {
delete vehicleGenerators[i];
}
// clearing lists
roads.clear();
lights.clear();
busStops.clear();
crossRoads.clear();
vehicles.clear();
vehicleGenerators.clear();
// ensuring every list is empty
ENSURE(roads.empty(), "Roads are not properly destructed");
ENSURE(lights.empty(), "Lights are not properly destructed");
ENSURE(busStops.empty(), "Bus stops are not properly destructed");
ENSURE(crossRoads.empty(), "Crossroads are not properly destructed");
ENSURE(vehicles.empty(), "Vehicles are not properly destructed");
ENSURE(vehicleGenerators.empty(), "Vehicle generators are not properly destructed");
}
void TrafficSimulation::generateIni(bool override , bool lighting , viewPosition viewPos) {
REQUIRE(properlyInitialized() , "TrafficSimulation was not properly initialized when calling generateIni");
// create file
string newFileName = INI_DIRECTORY;
string fileList;
newFileName += filename;
newFileName = newFileName.substr(0,newFileName.size() - 4);
newFileName += INIL;
fileList = filename.substr(0, filename.size() - 4);
fileList += INIL;
// Create output file
fstream outputNewFile;
// No override
if(!override){
FILE* codefile;
// TXT
bool doesntExists = true;
int i = 0;
// Check if file already exists
while (doesntExists){
i++;
codefile = fopen(newFileName.c_str(), "r");
if (codefile){
newFileName = INI_DIRECTORY + filename.substr(0, filename.size() - 4);
stringstream intStr;
intStr << i;
newFileName += '(';
newFileName += intStr.str();
newFileName += ')';
newFileName += INIL;
fileList = filename.substr(0, filename.size() - 4);
fileList += '(';
fileList += intStr.str();
fileList += ')';
fileList += INIL;
}
else {
outputNewFile.open(newFileName.c_str(), ios::app | ios::ate);
doesntExists = false;
}
}
}
// Override
else{
outputNewFile.open(newFileName.c_str(), ios::trunc | ios::out);
}
// If there should be lighting
if(lighting){
// Praise the sun
outputNewFile << "\n"
"[Light0]\n"
"infinity = TRUE\n"
"direction = (0, 0, -1)\n"
"ambientLight = (1, 1, 1)\n"
"diffuseLight = (1, 1, 1)"
"\n\n";
}
// Initiate figureCount
int figureCount = 0;
// Initiate lightCount
int lightCount = 1;
// Initiate road alignment
double roadAlign = 0;
// Generate figures for roads
double size = 0;
for(unsigned int i = 0; i < roads.size(); i++){
// Create road figure
outputNewFile << "[Figure";
outputNewFile << figureCount;
outputNewFile << "]\n"
"type = \"Rectangular Plane\"\n"
"scale = 1\n"
"length = ";
outputNewFile << roads[i]->getLength();
outputNewFile << "\n"
"width = ";
if(roads[i]->getLength() < ROAD_WIDTH){
outputNewFile << ROAD_WIDTH / 2;
}
else{
outputNewFile << ROAD_WIDTH;
}
outputNewFile << "\n"
"height = 1\n"
"rotateX = 0\n"
"rotateY = 0\n"
"rotateZ = 0\n"
"center = (";
outputNewFile << roadAlign;
outputNewFile << ", 0, 0.1)\n";
if(lighting){
outputNewFile << "ambientReflection = (0, 0, 0)\n"
"diffuseReflection = (0, 0, 0)\n"
"specularReflection = (0, 0, 0)\n"
"\n\n";
}
else{
outputNewFile << "color = (0, 0, 0)\n";
}
if(roads[i]->getLength() > size){
size = roads[i]->getLength();
}
figureCount++;
// Generate vehicles
for(int j = 0; j < roads[i]->getVehicleAmount(); j++){
// Vehicle base
outputNewFile << "[Figure";
outputNewFile << figureCount;
outputNewFile << "]\n"
"type = \"Rectangular Prism\"\n"
"scale = 1\n"
"length = ";
outputNewFile << roads[i]->getVehicle(j)->getV_length();
outputNewFile << "\n"
"width = ";
outputNewFile << roads[i]->getVehicle(j)->getV_length() / 2;
outputNewFile << "\n"
"height = 1\n"
"rotateX = 0\n"
"rotateY = 0\n"
"rotateZ = 0\n";
outputNewFile << "center = (";
outputNewFile << roadAlign;
outputNewFile << ",";
outputNewFile << (size / 2) - roads[i]->getVehicle(j)->getVehiclePosition() - roads[i]->getVehicle(j)->getV_length() / 2;
outputNewFile << ",";
outputNewFile << 0.1;
outputNewFile << ")\n";
if(lighting){
outputNewFile << "ambientReflection = (1, 0.5, 0.00)\n"
"diffuseReflection = (1, 0.5, 0.00)\n"
"specularReflection = (1, 0.5, 0.00)\n"
"\n\n";
}
else{
outputNewFile << "color = (1, 0.5, 0)\n";
}
figureCount++;
// Vehicle top
outputNewFile << "[Figure";
outputNewFile << figureCount;
outputNewFile << "]\n"
"type = \"Rectangular Prism\"\n"
"scale = 1\n"
"length = ";
outputNewFile << roads[i]->getVehicle(j)->getV_length() - 1;
outputNewFile << "\n"
"width = ";
outputNewFile << roads[i]->getVehicle(j)->getV_length() / 2;
outputNewFile << "\n"
"height = 1\n"
"rotateX = 0\n"
"rotateY = 0\n"
"rotateZ = 0\n";
outputNewFile << "center = (";
outputNewFile << roadAlign;
outputNewFile << ",";
outputNewFile << (size / 2) - roads[i]->getVehicle(j)->getVehiclePosition() - roads[i]->getVehicle(j)->getV_length() / 2 + 0.5;
outputNewFile << ",";
outputNewFile << 0.6;
outputNewFile << ")\n";
if(lighting){
outputNewFile << "ambientReflection = (1, 0.5, 0.00)\n"
"diffuseReflection = (1, 0.5, 0.00)\n"
"specularReflection = (1, 0.5, 0.00)\n"
"\n\n";
}
else{
outputNewFile << "color = (1, 0.5, 0)\n";
}
figureCount++;
}
double position;
// Generate traffic lights
for(int l = 0; l < roads[i]->getTrafficLightsAmount(); l++){
// Get traffic light position
position = (size / 2) - roads[i]->getTrafficLight(l)->getPosition();
// Create pole
outputNewFile << "[Figure";
outputNewFile << figureCount;
outputNewFile << "]\n"
"type = \"Cylinder\"\n";
outputNewFile << "height = ";
outputNewFile << TRAFFICLIGHT_HEIGHT;
outputNewFile << "\n"
"n = 36\n"
"scale = 0.3\n"
"rotateX = 0\n"
"rotateY = 0\n"
"rotateZ = 0\n";
outputNewFile << "center = (";
if(roads[i]->getLength() < ROAD_WIDTH){
outputNewFile << 1.05 * ROAD_WIDTH / 4;
}
else{
outputNewFile << 1.05 * ROAD_WIDTH / 2;
}
outputNewFile << ",";
position < 0 ? outputNewFile << "-" << abs(position) : outputNewFile << position;
outputNewFile << ",";
outputNewFile << 0.1;
outputNewFile << ")\n";
if(lighting){
outputNewFile << "ambientReflection = (0.5, 0.5, 0.5)\n"
"diffuseReflection = (0.5, 0.5, 0.5)\n"
"specularReflection = (0.5, 0.5, 0.5)\n"
"\n\n";
}
else{
outputNewFile << "color = (0.5, 0.5, 0.5)\n";
}
figureCount++;
// Create light
outputNewFile << "[Figure";
outputNewFile << figureCount;
outputNewFile << "]\n"
"type = \"Rectangular Prism\"\n"
"scale = 0.35\n"
"length = 1.5\n"
"height = 3\n"
"width = 1.4\n"
"rotateX = 0\n"
"rotateY = 0\n"
"rotateZ = 0\n";
outputNewFile << "center = (";