-
Notifications
You must be signed in to change notification settings - Fork 0
/
Highway.cc
1444 lines (1271 loc) · 54.6 KB
/
Highway.cc
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
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2005-2011 Old Dominion University [ARBABI]
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Hadi Arbabi <[email protected]>
* Bradley Dupont <[email protected]>
*/
#include <iostream>
#include "Highway.h"
#include "ns3/simulator.h"
#include "Geometry.h"
#include "Obstacle.h"
#include "IdGenerator.h"
#include <math.h>
namespace ns3 {
TypeId Highway::GetTypeId(void) {
static TypeId tid = TypeId("ns3::Highway")
.SetParent<Object > ()
.AddConstructor<Highway > ()
;
return tid;
}
//*******************************************************************
//
//Constructors/Destructors
//
//*******************************************************************
Highway::Highway() {
//Default direction, dplacement, length, and width
m_direction = 0.0;
m_xPos = 0.0;
m_yPos = 0.0;
m_dt = 0.1;
m_highwayLength = 1000;
m_laneWidth = 5;
//Default is 10 MPH
m_leftTurnSpeed = 4.4704;
m_rightTurnSpeed = 4.4704;
//Default number of lanes is 1
m_numberOfLanes = 1;
//These arrays are initialized to a size of 1 with NULL entries
m_frontHighways = new Ptr<Highway> [1];
m_frontHighways[0] = NULL;
m_frontOffsets = new int [1];
m_backHighways = new Ptr<Highway>[1];
m_backHighways[0] = NULL;
m_backOffsets = new int [1];
m_rightHighways = new Ptr<Highway> [1];
m_rightHighways[0] = NULL;
m_rightOffsets = new int [1];
m_leftHighways = new Ptr<Highway> [1];
m_leftHighways[0] = NULL;
m_leftOffsets = new int [1];
m_changeLaneSet = true;
m_loop = 0;
//Initialize the routing map
m_routingMap = std::map<int, TurningType > ();
//Create the transfer list
transferList = new list<Ptr<Vehicle> >();
}
//Cleans up memeory
Highway::~Highway() {
m_tempVehicles[0] = 0;
m_tempVehicles[1] = 0;
transferList->clear();
delete transferList;
delete [] m_frontHighways;
delete [] m_frontOffsets;
delete [] m_backHighways;
delete [] m_backOffsets;
delete [] m_rightHighways;
delete [] m_rightOffsets;
delete [] m_leftHighways;
delete [] m_leftOffsets;
ClearLanes();
}
void Highway::SetHighwayId(int highwayId) {
m_highwayId = highwayId;
}
int Highway::GetHighwayId() {
return m_highwayId;
}
void Highway::SetRoutingMap(std::map<int, TurningType> routingMap) {
m_routingMap = routingMap;
}
//Initializing the Highway
void Highway::InitHighway() {
InitLanes();
}
//This function deletes all vehicles from all lanes and clears the lane
//map
void Highway::ClearLanes() {
LaneMap::iterator it;
for (it = m_laneMap.begin(); it != m_laneMap.end(); it++) {
it->second->vehicles->clear();
delete it->second->vehicles;
delete it->second;
}
m_laneMap.clear();
}
void Highway::InitLanes() {
//The purpose of this function is to initialize the lanes based on
//the highway location and the number of lanes. The lanes are centered
//on the Highway start and are arranged along the Y axis unless the highway
//is straight up and down (where they are arranged along the X axis)
//Note that the first lanes is the "leftmost"
//Normalizing the angle to [0,2pi)
double normalizedAngle = normalizeAngle(m_direction);
double laneXShift = 0.0;
double laneYShift = 0.0;
double sineValue = 0.0;
double startXVal = 0.0;
double startYVal = 0.0;
double result[2];
//If the angle is 90 degrees
if (anglesEqual(normalizedAngle, M_PI / 2.0)) {
//The lanes shift in the X their entire length
laneXShift = m_laneWidth;
//If the angle is 270 degrees
} else if (anglesEqual(normalizedAngle, 3.0 * M_PI / 2.0)) {
//The lanes shift in the X their entire length (but in the
//opposite direction)
laneXShift = -m_laneWidth;
} else {
//Any other angles, all lane positions are moved only in the Y
//location. The following calculations determine, based on the
//lane width, how much the start point of the lane has to shift
//between lanes
if (normalizedAngle >= 0 && normalizedAngle < M_PI / 2.0) {
sineValue = -sin((M_PI / 2.0) - normalizedAngle);
} else if (normalizedAngle > M_PI / 2.0 && normalizedAngle < M_PI) {
sineValue = sin(normalizedAngle - (M_PI / 2.0));
} else if (anglesEqual(normalizedAngle, M_PI)) {
sineValue = 1.0;
} else if (normalizedAngle > M_PI && normalizedAngle < 3.0 * M_PI / 2.0) {
sineValue = sin(3.0 * M_PI / 2.0 - normalizedAngle);
} else {
sineValue = -sin(normalizedAngle - 3.0 * M_PI / 2.0);
}
laneYShift = m_laneWidth / sineValue;
}
//This is the distance for half of the lanes
double distance = (m_laneWidth * (m_numberOfLanes - 1)) / 2.0;
//If the angle is 90 degrees
if (anglesEqual(normalizedAngle, M_PI / 2.0)) {
//The shift is entire in the X direction
startXVal = m_xPos - distance;
startYVal = m_yPos;
//If the angle is 270 degrees
} else if (anglesEqual(normalizedAngle, 3.0 * M_PI / 2.0)) {
//The shift is in X, but in the other direction
startXVal = m_xPos + distance;
startYVal = m_yPos;
} else {
//Shift is in Y, modified by the geometry of the angle
startYVal = m_yPos - distance / sineValue;
startXVal = m_xPos;
}
//For each lane
for (int i = 1; i <= m_numberOfLanes; i++) {
//Create the lane, assigning the current start X and Y,
//calculate the end X and Y, and prepare the startXVal and
//startYVal for the next lane
Lane* lane = new Lane;
lane->id = i;
lane->direction = m_direction;
lane->vehicles = new VehicleList();
lane->startX = startXVal;
lane->startY = startYVal;
result[0] = startXVal;
result[1] = startYVal;
translatePoint(result, m_direction, m_highwayLength);
lane->endX = result[0];
lane->endY = result[1];
startXVal += laneXShift;
startYVal += laneYShift;
//Add to the map based on the ID
m_laneMap[i] = lane;
}
}
bool Highway::GetChangeLane() {
return m_changeLaneSet;
}
void Highway::SetChangeLane(bool value) {
m_changeLaneSet = value;
}
double Highway::GetDeltaT() {
return m_dt;
}
void Highway::SetDeltaT(double value) {
if (value <= 0)
value = 0.1;
m_dt = value;
}
//Add a Highway for going straight
void Highway::AddFrontHighway(Ptr<Highway> frontHighway, int laneOffset, int frontOffset) {
//Check that the lane offset is viable
if (laneOffset < 0 || laneOffset > (m_numberOfLanes - 1)) {
return;
}
//For each location from the laneOffset for as many lanes as
//possible
int start = laneOffset;
int end = min(laneOffset + frontHighway->GetNumberOfLanes(), m_numberOfLanes);
for (int i = start; i < end; i++) {
//Assign the highway and the offset
m_frontHighways[i] = frontHighway;
m_frontOffsets[i] = frontOffset;
}
//For each lane
for (int i = 1; i <= m_numberOfLanes; i++) {
Lane* aLane = m_laneMap[i];
//Clear any current obstacles
aLane->vehicles->clear();
//If there is no connecting highway
if (m_frontHighways[i - 1] == NULL && m_rightHighways[i - 1] == NULL && m_leftHighways[i - 1] == NULL) {
//Create an obstacle to force vehicles to get out of the lane
Ptr<Vehicle> obstacle = CreateObject<Obstacle > ();
obstacle->SetVehicleId(IdGenerator::nextVehicleId());
obstacle->SetLane(i);
obstacle->SetVehicleType(1);
//Add it at the end of the highway
AddVehicle(obstacle, m_highwayLength);
}
}
}
//Add a highway behind (for lane change calculations mostly)
void Highway::AddBackHighway(Ptr<Highway> backHighway, int laneOffset, int backOffset) {
if (laneOffset < 0 || laneOffset > (m_numberOfLanes - 1)) {
return;
}
int start = laneOffset;
int end = min(laneOffset + backHighway->GetNumberOfLanes(), m_numberOfLanes);
for (int i = start; i < end; i++) {
m_backHighways[i] = backHighway;
m_backOffsets[i] = backOffset;
}
//Vehicles don't travel backwards, so we don't have to place
//obstalces
}
//Add a Right Highway
//These Right Highways are used instead of a Front highway if the vehicle wants to
//turn right
void Highway::AddRightHighway(Ptr<Highway> rightHighway, int laneOffset, int rightOffset) {
if (laneOffset < 0 || laneOffset > (m_numberOfLanes - 1)) {
return;
}
int start = laneOffset;
int end = min(laneOffset + rightHighway->GetNumberOfLanes(), m_numberOfLanes);
for (int i = start; i < end; i++) {
m_rightHighways[i] = rightHighway;
m_rightOffsets[i] = rightOffset;
}
for (int i = 1; i <= m_numberOfLanes; i++) {
Lane* aLane = m_laneMap[i];
aLane->vehicles->clear();
if (m_frontHighways[i - 1] == NULL && m_rightHighways[i - 1] == NULL && m_leftHighways[i - 1] == NULL) {
Ptr<Vehicle> obstacle = CreateObject<Obstacle > ();
obstacle->SetVehicleId(IdGenerator::nextVehicleId());
obstacle->SetLane(i);
obstacle->SetVehicleType(1);
AddVehicle(obstacle, m_highwayLength);
}
}
}
//Add a Left Highway
//These Left Highways are used instead of a Front Highway if the vehicle wants to
//turn left
void Highway::AddLeftHighway(Ptr<Highway> leftHighway, int laneOffset, int leftOffset) {
if (laneOffset < 0 || laneOffset > (m_numberOfLanes - 1)) {
return;
}
int start = laneOffset;
int end = min(laneOffset + leftHighway->GetNumberOfLanes(), m_numberOfLanes);
for (int i = start; i < end; i++) {
m_leftHighways[i] = leftHighway;
m_leftOffsets[i] = leftOffset;
}
for (int i = 1; i <= m_numberOfLanes; i++) {
Lane* aLane = m_laneMap[i];
aLane->vehicles->clear();
if (m_frontHighways[i - 1] == NULL && m_rightHighways[i - 1] == NULL && m_leftHighways[i - 1] == NULL) {
Ptr<Vehicle> obstacle = CreateObject<Obstacle > ();
obstacle->SetVehicleId(IdGenerator::nextVehicleId());
obstacle->SetLane(i);
obstacle->SetVehicleType(1);
AddVehicle(obstacle, m_highwayLength);
}
}
}
Highway::TurningType Highway::GetTurningTypeForHighwayId(int destHighwayId) {
//The default value is STRAIGHT, even if the highway is not connected
TurningType retValue = STRAIGHT;
//Search all lanes for the highway
for (int i = 0; i < m_numberOfLanes; i++) {
if (m_leftHighways[i] != NULL && m_leftHighways[i]->GetHighwayId() == destHighwayId) {
retValue = LEFT;
break;
}
if (m_rightHighways[i] != NULL && m_rightHighways[i]->GetHighwayId() == destHighwayId) {
retValue = RIGHT;
break;
}
}
return retValue;
}
int Highway::GetNumberOfLanes() {
return m_numberOfLanes;
}
void Highway::SetNumberOfLanes(int value) {
if (value < 1)
value = 1;
m_numberOfLanes = value;
//Anytime the number of lanes changes, we need to reset the
//highway connection arrays
m_frontHighways = new Ptr<Highway>[m_numberOfLanes];
m_frontOffsets = new int[m_numberOfLanes];
m_backHighways = new Ptr<Highway>[m_numberOfLanes];
m_backOffsets = new int[m_numberOfLanes];
m_rightHighways = new Ptr<Highway>[m_numberOfLanes];
m_rightOffsets = new int[m_numberOfLanes];
m_leftHighways = new Ptr<Highway>[m_numberOfLanes];
m_leftOffsets = new int[m_numberOfLanes];
for (int i = 0; i < m_numberOfLanes; i++) {
m_frontHighways[i] = NULL;
m_backHighways[i] = NULL;
m_rightHighways[i] = NULL;
m_leftHighways[i] = NULL;
}
}
double Highway::GetHighwayLength() {
return m_highwayLength;
}
void Highway::SetHighwayLength(double value) {
if (value < 0)
value = 10000;
m_highwayLength = value;
}
double Highway::GetLaneWidth() {
return m_laneWidth;
}
void Highway::SetLaneWidth(double value) {
if (value < 0)
value = 5;
m_laneWidth = value;
}
double Highway::GetXPos() {
return m_xPos;
}
void Highway::SetXPos(double xPos) {
m_xPos = xPos;
}
double Highway::GetYPos() {
return m_yPos;
}
void Highway::SetYPos(double yPos) {
m_yPos = yPos;
}
double Highway::GetRightTurnSpeed() {
return m_rightTurnSpeed;
}
void Highway::SetRightTurnSpeed(double rightTurnSpeed) {
m_rightTurnSpeed = rightTurnSpeed;
}
double Highway::GetLeftTurnSpeed() {
return m_leftTurnSpeed;
}
void Highway::SetLeftTurnSpeed(double leftTurnSpeed) {
m_leftTurnSpeed = leftTurnSpeed;
}
void Highway::SetDirection(double direction) {
m_direction = direction;
}
double Highway::GetDirection() {
return m_direction;
}
//This returns the turning type based on the routing map supplied to
//this Highway instance (STRAIGHT for default)
Highway::TurningType Highway::GetTurningType(int highwayId) {
std::map<int, TurningType>::iterator it;
it = m_routingMap.find(highwayId);
if (it == m_routingMap.end()) {
return STRAIGHT;
} else {
return it->second;
}
}
//Searches all lanes for the vehicle and removes it
void Highway::RemoveVehicle(int vid) {
int minIndex = 1;
int maxIndex = m_numberOfLanes;
for (int i = minIndex; i <= maxIndex; i++) {
if (i == 0) {
continue;
}
VehicleList* vList = m_laneMap[i]->vehicles;
for (VehicleList::iterator itr = vList->begin(); itr != vList->end(); itr++) {
if ((*itr)->GetVehicleId() == vid) {
vList->erase(itr);
break;
}
}
}
}
//Same as RemoveVehicle(int)
void Highway::RemoveVehicle(Ptr<Vehicle> vehicle) {
RemoveVehicle(vehicle->GetVehicleId());
}
//A utility function for iterating through the linked list used
Ptr<Vehicle> Highway::GetVehicleFromList(VehicleList* v, int index) {
VehicleList::iterator i = v->begin();
advance(i, index);
return *i;
}
//Returns the vehicle in the lane specified at the index specified
Ptr<Vehicle> Highway::GetVehicle(int laneNumber, int index) {
//Check and make sure that the laneNumber is valid
if (laneNumber < 0 || laneNumber > m_numberOfLanes) {
return NULL;
}
//Get the vehicle list
VehicleList* v = m_laneMap[laneNumber]->vehicles;
//If the index is less than 0
if (index < 0) {
//We are looking for a vehicle that is past the end of the current
//Highway
//If the front highway is not NULL
if (m_frontHighways[laneNumber - 1] != NULL) {
//return that Highway's first Vehicle
return m_frontHighways[laneNumber - 1]->GetFirstVehicle(laneNumber - m_frontOffsets[laneNumber - 1]);
} else {
return NULL;
}
//If the index is greater than any we current have
} else if (index >= (int) v->size()) {
//Then we need to look for a vehicle that is before the start of
//the current Highway
//If the back Highway is not null
if (m_backHighways[laneNumber - 1] != NULL) {
//return that Highway's last vehicle
return m_backHighways[laneNumber - 1]->GetLastVehicle(laneNumber - m_backOffsets[laneNumber - 1]);
} else {
return NULL;
}
//If it is a vehicle available in this highway
} else {
//Return the vehicle
VehicleList::iterator i = v->begin();
advance(i, index);
return *i;
}
}
//Returns the distance to a vehicle, either in this Highway or a Highway
//connected to this one. If no Vehicle is available, return NAN
double Highway::GetDistance(int laneNumber, int index, bool fromStart) {
//Get the current lane
Lane* vehiclesLane = m_laneMap[laneNumber];
//If the index is less than 0
if (index < 0) {
//We are looking for a vehicle that is after the end of this Highway
if (m_frontHighways[laneNumber - 1] != NULL) {
//Get the result from the connected highway
double result = m_frontHighways[laneNumber - 1]->GetDistanceToFirstVehicle(laneNumber - m_frontOffsets[laneNumber - 1]);
//If there is a result
if (!isnan(result)) {
//Based on where we are measuring from, add m_highwayLength plus
//the result
return fromStart ? result + m_highwayLength : result;
} else {
return NAN;
}
} else {
return NAN;
}
//If the index is greater than the current number of vehicles
} else if (index >= (int) vehiclesLane->vehicles->size()) {
//We are looking for a vehicle that is before the start of this Highway
if (m_backHighways[laneNumber - 1] != NULL) {
//Get the result from the connected highway
double result = m_backHighways[laneNumber - 1]->GetDistanceToLastVehicle(laneNumber - m_backOffsets[laneNumber - 1]);
//If there is a result
if (!isnan(result)) {
//Based on where we are measuring from, add m_highwayLength plus
//the result
return fromStart ? result : result + m_highwayLength;
} else {
return NAN;
}
} else {
return NAN;
}
//Otherwise, it is a vehicle in this lane
} else {
//Get the distance
Ptr<Vehicle> vehicle = GetVehicle(laneNumber, index);
if (vehicle == NULL) {
return NAN;
} else {
if (fromStart) {
return directDistance(vehicle->GetPosition().x, vehicle->GetPosition().y, vehiclesLane->startX, vehiclesLane->startY);
} else {
return directDistance(vehicle->GetPosition().x, vehicle->GetPosition().y, vehiclesLane->endX, vehiclesLane->endY);
}
}
}
}
//When asking for the First vehicle, what we are looking for is the
//vehicle that is closest in the lane to startX, startY
Ptr<Vehicle> Highway::GetFirstVehicle(int lane) {
//Check that the Lane is correct
if (lane < 1 || lane > m_numberOfLanes) {
return NULL;
}
//Get the lane
VehicleList* vehicles = m_laneMap[lane]->vehicles;
//If the list is empty
if (vehicles->empty()) {
//If there is a connected highway
if (m_frontHighways[lane - 1] != NULL) {
//Get the first vehicle from that Highway
return m_frontHighways[lane - 1]->GetFirstVehicle(lane - m_frontOffsets[lane - 1]);
} else {
return NULL;
}
}
//We need the vehicle with the highest index
return vehicles->back();
}
//We are asking for the distance to the first vehicle from the start
//of the lane
double Highway::GetDistanceToFirstVehicle(int lane) {
//Check that the lane is correct
if (lane < 1 || lane > m_numberOfLanes) {
return NAN;
}
VehicleList* vehicles = m_laneMap[lane]->vehicles;
//If the list is empty
if (vehicles->empty()) {
//Make sure that we geth the distance to the vehicle in the
//connecting lane by passing in a negative index (always < 0)
return GetDistance(lane, -1, true);
} else {
//Otherwise, get the distance to the vehicle with the largest index
return GetDistance(lane, vehicles->size() - 1, true);
}
}
//We are asking for the vehicle that is the closest to the end of the lane
Ptr<Vehicle> Highway::GetLastVehicle(int lane) {
if (lane < 1 || lane > m_numberOfLanes) {
return NULL;
}
VehicleList* vehicles = m_laneMap[lane]->vehicles;
//If the lane is current empty
if (vehicles->empty()) {
//If we have a back highway
if (m_backHighways[lane - 1] != NULL) {
//Get the last vehilce from that Highway
return m_backHighways[lane - 1]->GetLastVehicle(lane - m_backOffsets[lane - 1]);
} else {
return NULL;
}
}
return vehicles->front();
}
//We are asking for the distance from the end of the lane to the
//vehicle closest to the end of the lane
double Highway::GetDistanceToLastVehicle(int lane) {
if (lane < 1 || lane > m_numberOfLanes) {
return NAN;
}
VehicleList* vehicles = m_laneMap[lane]->vehicles;
//If the current lane is empty
if (vehicles->empty()) {
//Ensure that we get the distance for the vehicle in the
//connecting lane by passing in an index greater than
return GetDistance(lane, 1, false);
} else {
//Otherwise, get the distance to the vehilce with the smallest
//index
return GetDistance(lane, 0, false);
}
}
//Returns the start location of the requested lane
Vector Highway::GetLaneStart(int lane) {
Vector v(-10000.0, -10000.0, 0.0);
if (lane >= 1 && lane <= m_numberOfLanes) {
v.x = m_laneMap[lane]->startX;
v.y = m_laneMap[lane]->startY;
}
return v;
}
//This function is the point of entry for controlling vehicles in the Highway
void Highway::TranslateVehicles() {
//Only every tenth loop do we try to change lanes
// NOTE: ORDER OF CALLING THIS FUNCTIONS IS VERY VERY IMPORTANT (EFFECT OF CURRENT SPEED, POSITION, DECICION)
if (m_loop == 10) m_loop = 0;
if (m_loop == 0 && m_changeLaneSet == true) {
if (m_changeLaneSet) {
ChangeLane();
}
}
//First, we translate the position and velocities of all vehicles
TranslatePositionVelocity(m_dt);
//Then we modify the accelerations of the vehicles for the next loop
Accelerate(m_dt);
m_loop++;
}
//This function iterators over all possible lane combinations and
//checks lane changing from any combination
void Highway::ChangeLane() {
if (m_numberOfLanes <= 1) {
return;
}
for (int i = 1; i <= m_numberOfLanes; i++) {
if (i <= 1) {
DoChangeLaneIfPossible(i, i + 1);
continue;
}
if (i + 1 >= (m_numberOfLanes + 1)) {
DoChangeLaneIfPossible(i, i - 1);
continue;
}
DoChangeLaneIfPossible(i, i + 1);
DoChangeLaneIfPossible(i, i - 1);
}
}
void Highway::DoChangeLaneIfPossible(int curLane, int desLane) {
//result is used to store point transforms
double result[2];
//This list indicates that a vehicle is ready to be moved to the
//destination lane
std::list<Ptr<Vehicle> >* canChange = new std::list<Ptr<Vehicle> >();
//The current lane
VehicleList* currentLane = m_laneMap[curLane]->vehicles;
//The desintation lane
Lane* destLane = m_laneMap[desLane];
//These booleans indicate whether the source and destination lanes
//have left connections, right connections, and/or straight connections
bool curLeft = (m_leftHighways[curLane - 1] != NULL);
bool curRight = (m_rightHighways[curLane - 1] != NULL);
bool desLeft = (m_leftHighways[desLane - 1] != NULL);
bool desRight = (m_rightHighways[desLane - 1] != NULL);
bool desStraight = (m_frontHighways[desLane - 1] != NULL);
//For each vehicle in the lane
for (uint j = 0; j < currentLane->size(); j++) {
Ptr<Vehicle> current = GetVehicle(curLane, j);
//Some objects (traffic lights, obstacles) do not have lane changing
//ability and should not be processed
if (current->GetLaneChange() == NULL) {
continue;
}
//We will have to modify the right bias of the vehicle, and so keep
//a copy here
double oldBias = current->GetLaneChange()->GetBiasRight();
//We determine the direction this vehicle wants to turn based on
//its current destination
TurningType turningType = GetTurningType(current->GetDestination());
switch (turningType) {
//If we are turning left
case LEFT:
{
//If the current lane is a left turning lane and the
//destination is not, we do not want to make the switch
if (curLeft && !desLeft) {
continue;
}
//If the current lane is not a left turning lane, we want
//to force the vehicle as far left as we can, so we set
//the right bias to -100
if (!curLeft) {
current->GetLaneChange()->SetBiasRight(-100);
}
break;
}
//If we are turning right
case RIGHT:
{
//If the current lane is a right turning lane and the
//destination is not, we do not want to make the switch
if (curRight && !desRight) {
continue;
}
//If the current lane is not a right turning lane, we want
//to force the vehicle as far right as we can, so we set
//the right bias to 100
if (!curRight) {
current->GetLaneChange()->SetBiasRight(100);
}
break;
}
//If we are going straight
case STRAIGHT:
{
//We do not want to get into a turn-only lane
//if we want to go straight
//However, if there is no connecting highway at all,
//we will still be able to change lanes
if ((desLeft || desRight) && !desStraight) {
continue;
}
break;
}
}
//Get the distance to the current vehicle from the start of the
//lane
double currentDistance = GetDistance(curLane, j, true);
//Get the vehicle in front of the current vehicle
//note that GetVehicle handles searching connecting Highways
Ptr<Vehicle> fOld = GetVehicle(curLane, j - 1);
//Get the distance between the current vehicle and the vehicle in front
//Subtracting the current vehicles length is because location is tracked from
//the vehicle's back bumper
double distance = GetDistance(curLane, j - 1, true) - currentDistance - current->GetLength();
//This function finds the side vehicles in the destination lane
//It places the results into m_tempVehicles and m_tempDistances
FindSideVehicles(current, currentDistance, desLane);
//Pass control to the Vehicle's LaneChange model with all the necessary
//information
if (current->CheckLaneChange(fOld, distance, m_tempVehicles[0], m_tempDistances[0], m_tempVehicles[1], m_tempDistances[1], (desLane < curLane) ? true : false)) {
//If the lane change is desired, push the vehicle into the "canChange" list
canChange->push_back(GetVehicle(curLane, j));
}
//Reset the vehicle's bias
current->GetLaneChange()->SetBiasRight(oldBias);
}
//For each vehicle in the canChange list
for (uint j = 0; j < canChange->size(); j++) {
//Get the vehicle
Ptr<Vehicle> curVehicle = GetVehicleFromList(canChange, j);
//Translate the vehicle from its current position
Vector position = curVehicle->GetPosition();
result[0] = position.x;
result[1] = position.y;
double angleChange = (curLane < desLane) ? -M_PI / 2.0 : M_PI / 2.0;
//to the point in the adjacent lane
translatePoint(result, GetVehicleFromList(canChange, j)->GetDirection() + angleChange, m_laneWidth);
position.x = result[0];
position.y = result[1];
//Adjust the vehicles parameters
curVehicle->SetLane(desLane);
curVehicle->SetPosition(position);
currentLane->remove(curVehicle);
//Insert the vehicle in the lane
InsertIntoLane(curVehicle, destLane);
}
//Clean up
canChange->clear();
delete canChange;
}
//This function does the main body of the work of translating vehicles along
//the highway
void Highway::TranslatePositionVelocity(double dt) {
//This array is for storing the point translations
double tempPos[2];
int maxIndex = m_numberOfLanes;
//for each lane
for (int i = 1; i <= maxIndex; i++) {
//Get the lane
Lane *theLane = m_laneMap[i];
VehicleList* vList = theLane->vehicles;
//For every vehicle in the lane
for (uint j = 0; j < vList->size(); j++) {
//Preset the variables
bool translate = true;
Ptr<Vehicle> veh = GetVehicle(i, j);
bool controled = false;
if (!m_controlVehicle.IsNull()) {
controled = m_controlVehicle(Ptr<Highway > (this), veh, dt);
}
if(controled) {
continue;
}
tempPos[0] = theLane->startX;
tempPos[1] = theLane->startY;
double vehDistance = GetDistance(i, j, true);
if (veh->GetLaneChange() == NULL) {
//No need to accelerate or change lanes on something with no
//lane change
continue;
}
//Get the vehicle in front of the current vehicle
//note that GetVehicle handles searching connecting Highways
Ptr<Vehicle> fwdVeh = GetVehicle(i, j - 1);
//Get the distance between the current vehicle and the vehicle in front
//Subtracting the current vehicles length is because location is tracked from
//the vehicle's back bumper
double distance = GetDistance(i, j - 1, true) - vehDistance - veh->GetLength();
//We can translate If
//There is no forward vehicle
if (fwdVeh == NULL) {
translate = true;
//Or if the acceleration value is NAN
} else if (isnan(veh->Acceleration(fwdVeh, distance))) {
translate = true;
//Or if the gap is greater than the minimum gap
} else if (distance >= veh->GetLaneChange()->GetGapMin()) {
translate = true;
//otherwise
} else {
//We do not translate
translate = false;
}
if (translate) {
//Use a total distance metric rather than a step metric to
//improve accuracy over long paths.
double totalDistance = vehDistance + dt * veh->GetVelocity();
translatePoint(tempPos, m_direction, totalDistance);
if (fwdVeh != NULL) {
//Move vehicle up in list if we pass, say, a traffic light without stopping
if (!anglesEqual(veh->GetDirection(), angleToPoint(tempPos[0], tempPos[1], fwdVeh->GetPosition().x, fwdVeh->GetPosition().y))) {
VehicleList::iterator itr = vList->begin();
advance(itr, j);
itr = vList->erase(itr);
if (itr == vList->begin()) {
vList->push_front(veh);
} else {
itr--;
vList->insert(itr, veh);
}
}
}
//Adjust the vehicles position
veh->SetPosition(Vector3D(tempPos[0], tempPos[1], 0.0));
//If we reach the end of the highway, we push the vehicle to the transfer list for
//further processing
if (!anglesEqual(angleToPoint(tempPos[0], tempPos[1], theLane->endX, theLane->endY), theLane->direction)) {
transferList->push_back(veh);
}
}
veh->TranslateVelocity(dt);
}
//Anything in the transfer list is ready to be removed
for (uint r = 0; r < transferList->size(); r++) {
Ptr<Vehicle> rm = GetVehicleFromList(transferList, r);
for (VehicleList::iterator itr = vList->begin(); itr != vList->end(); itr++) {
if ((*itr) == rm) {
vList->erase(itr);
break;
}
}
}
}
}
//This function changes the acceleration value of the vehicle
//for the next loop
void Highway::Accelerate(double dt) {
int maxIndex = m_numberOfLanes;