-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
GNENetHelper.cpp
2955 lines (2576 loc) · 102 KB
/
GNENetHelper.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
/****************************************************************************/
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
// Copyright (C) 2001-2023 German Aerospace Center (DLR) and others.
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0/
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License 2.0 are satisfied: GNU General Public License, version 2
// or later which is available at
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
/****************************************************************************/
/// @file GNENetHelper.cpp
/// @author Pablo Alvarez Lopez
/// @date Mar 2020
///
// Helper for GNENet
/****************************************************************************/
#include <netbuild/NBNetBuilder.h>
#include <netedit/GNENet.h>
#include <netedit/GNEViewNet.h>
#include <netedit/GNEViewParent.h>
#include <netedit/elements/data/GNEDataInterval.h>
#include <netedit/elements/demand/GNEVType.h>
#include <netedit/elements/network/GNEConnection.h>
#include <netedit/elements/network/GNECrossing.h>
#include <netedit/elements/network/GNEWalkingArea.h>
#include <netedit/elements/network/GNEEdgeTemplate.h>
#include <netedit/elements/network/GNEEdgeType.h>
#include <netedit/elements/data/GNEMeanData.h>
#include <netedit/frames/common/GNEInspectorFrame.h>
#include <netedit/frames/demand/GNEPersonPlanFrame.h>
#include <netedit/frames/network/GNECreateEdgeFrame.h>
#include <netedit/frames/demand/GNEVehicleFrame.h>
#include <netedit/frames/demand/GNETypeFrame.h>
#include <netedit/frames/demand/GNETypeDistributionFrame.h>
#include <netedit/frames/demand/GNEStopFrame.h>
#include <netedit/frames/demand/GNEPersonFrame.h>
#include <netedit/frames/demand/GNEPersonPlanFrame.h>
#include <netedit/frames/demand/GNEContainerFrame.h>
#include <netedit/frames/demand/GNEContainerPlanFrame.h>
#include <utils/gui/div/GUIGlobalSelection.h>
#include <utils/gui/globjects/GUIGlObjectStorage.h>
#include <utils/options/OptionsCont.h>
#include <utils/xml/NamespaceIDs.h>
#include "GNENetHelper.h"
// ---------------------------------------------------------------------------
// GNENetHelper::AttributeCarriers - methods
// ---------------------------------------------------------------------------
GNENetHelper::AttributeCarriers::AttributeCarriers(GNENet* net) :
myNet(net),
myStopIndex(0) {
// fill additionals with tags
auto additionalTags = GNEAttributeCarrier::getTagPropertiesByType(GNETagProperties::TagType::ADDITIONALELEMENT |
GNETagProperties::TagType::SHAPE | GNETagProperties::TagType::SYMBOL | GNETagProperties::TagType::TAZELEMENT | GNETagProperties::TagType::WIRE);
for (const auto& additionalTag : additionalTags) {
myAdditionals.insert(std::make_pair(additionalTag.getTag(), std::set<GNEAdditional*>()));
}
// fill demand elements with tags
auto demandElementTags = GNEAttributeCarrier::getTagPropertiesByType(GNETagProperties::TagType::DEMANDELEMENT);
for (const auto& demandElementTag : demandElementTags) {
myDemandElements.insert(std::make_pair(demandElementTag.getTag(), std::set<GNEDemandElement*>()));
}
auto stopTags = GNEAttributeCarrier::getTagPropertiesByType(GNETagProperties::TagType::STOP);
for (const auto& stopTag : stopTags) {
myDemandElements.insert(std::make_pair(stopTag.getTag(), std::set<GNEDemandElement*>()));
}
// fill data elements with tags
auto genericDataElementTags = GNEAttributeCarrier::getTagPropertiesByType(GNETagProperties::TagType::GENERICDATA);
for (const auto& genericDataElementTag : genericDataElementTags) {
myGenericDatas.insert(std::make_pair(genericDataElementTag.getTag(), std::set<GNEGenericData*>()));
}
// fill meanDatas with tags
auto meanDataTags = GNEAttributeCarrier::getTagPropertiesByType(GNETagProperties::TagType::MEANDATA);
for (const auto& meanDataTag : meanDataTags) {
myMeanDatas.insert(std::make_pair(meanDataTag.getTag(), std::set<GNEMeanData*>()));
}
}
GNENetHelper::AttributeCarriers::~AttributeCarriers() {
// Drop EdgeTypes
for (const auto& edgeType : myEdgeTypes) {
edgeType.second->decRef("GNENetHelper::~GNENet");
// show extra information for tests
WRITE_DEBUG("Deleting unreferenced " + edgeType.second->getTagStr() + " '" + edgeType.second->getID() + "' in AttributeCarriers destructor");
delete edgeType.second;
}
// Drop Edges
for (const auto& edge : myEdges) {
edge.second->decRef("GNENetHelper::~GNENet");
// show extra information for tests
WRITE_DEBUG("Deleting unreferenced " + edge.second->getTagStr() + " '" + edge.second->getID() + "' in AttributeCarriers destructor");
delete edge.second;
}
// Drop myJunctions
for (const auto& junction : myJunctions) {
junction.second->decRef("GNENetHelper::~GNENet");
// show extra information for tests
WRITE_DEBUG("Deleting unreferenced " + junction.second->getTagStr() + " '" + junction.second->getID() + "' in AttributeCarriers destructor");
delete junction.second;
}
// Drop Additionals (Only used for additionals that were inserted without using GNEChange_Additional)
for (const auto& additionalTag : myAdditionals) {
for (const auto& additional : additionalTag.second) {
// decrease reference manually (because it was increased manually in GNEAdditionalHandler)
additional->decRef();
// show extra information for tests
WRITE_DEBUG("Deleting unreferenced " + additional->getTagStr() + " in AttributeCarriers destructor");
delete additional;
}
}
// Drop demand elements (Only used for demand elements that were inserted without using GNEChange_DemandElement, for example the default VType")
for (const auto& demandElementTag : myDemandElements) {
for (const auto& demandElement : demandElementTag.second) {
// decrease reference manually (because it was increased manually in GNERouteHandler)
demandElement->decRef();
// show extra information for tests
if (demandElement->getTagProperty().isType()) {
// special case for default VTypes
if (DEFAULT_VTYPES.count(demandElement->getID()) == 0) {
WRITE_DEBUG("Deleting unreferenced " + demandElement->getTagStr() + " in AttributeCarriers destructor");
}
} else {
WRITE_DEBUG("Deleting unreferenced " + demandElement->getTagStr() + " in AttributeCarriers destructor");
}
delete demandElement;
}
}
// Drop dataSets (Only used for TAZElements that were inserted without using GNEChange_DataSets)
for (const auto& dataSet : myDataSets) {
// decrease reference manually (because it was increased manually in GNEDataHandler)
dataSet->decRef();
// show extra information for tests
WRITE_DEBUG("Deleting unreferenced " + dataSet->getTagStr() + " in AttributeCarriers destructor");
delete dataSet;
}
// Drop MeanDatas (Only used for meanDatas that were inserted without using GNEChange_MeanData)
for (const auto& meanDataTag : myMeanDatas) {
for (const auto& meanData : meanDataTag.second) {
// decrease reference manually (because it was increased manually in GNEMeanDataHandler)
meanData->decRef();
// show extra information for tests
WRITE_DEBUG("Deleting unreferenced " + meanData->getTagStr() + " in AttributeCarriers destructor");
delete meanData;
}
}
}
void
GNENetHelper::AttributeCarriers::remapJunctionAndEdgeIds() {
std::map<std::string, GNEEdge*> newEdgeMap;
std::map<std::string, GNEJunction*> newJunctionMap;
// fill newEdgeMap
for (const auto& edge : myEdges) {
edge.second->setEdgeID(edge.second->getNBEdge()->getID());
newEdgeMap[edge.second->getNBEdge()->getID()] = edge.second;
}
for (const auto& junction : myJunctions) {
newJunctionMap[junction.second->getNBNode()->getID()] = junction.second;
junction.second->setNetworkElementID(junction.second->getNBNode()->getID());
}
myEdges = newEdgeMap;
myJunctions = newJunctionMap;
}
bool
GNENetHelper::AttributeCarriers::isNetworkElementAroundShape(GNEAttributeCarrier* AC, const PositionVector& shape) const {
// check what type of AC
if (AC->getTagProperty().getTag() == SUMO_TAG_JUNCTION) {
// Junction
const GNEJunction* junction = myJunctions.at(AC->getID());
if (junction->getNBNode()->getShape().size() == 0) {
return shape.around(junction->getNBNode()->getCenter());
} else {
return (shape.overlapsWith(junction->getNBNode()->getShape()));
}
} else if (AC->getTagProperty().getTag() == SUMO_TAG_EDGE) {
// Edge
for (const auto& lane : myEdges.at(AC->getID())->getLanes()) {
if (shape.overlapsWith(lane->getLaneShape())) {
return true;
}
}
return false;
} else if (AC->getTagProperty().getTag() == SUMO_TAG_LANE) {
// Lane
return shape.overlapsWith(retrieveLane(AC->getID())->getLaneShape());
} else if (AC->getTagProperty().getTag() == SUMO_TAG_CONNECTION) {
// connection
return shape.overlapsWith(dynamic_cast<GNEConnection*>(AC)->getConnectionShape());
} else if (AC->getTagProperty().getTag() == SUMO_TAG_CROSSING) {
// crossing
return shape.overlapsWith(dynamic_cast<GNECrossing*>(AC)->getCrossingShape());
} else if (AC->getTagProperty().isAdditionalElement()) {
// Additional (including shapes and TAZs
const GNEAdditional* additional = retrieveAdditional(AC);
if (additional->getAdditionalGeometry().getShape().size() <= 1) {
return shape.around(additional->getPositionInView());
} else {
return shape.overlapsWith(additional->getAdditionalGeometry().getShape());
}
} else {
return false;
}
}
GNEAttributeCarrier*
GNENetHelper::AttributeCarriers::retrieveAttributeCarrier(const GUIGlID id, bool hardFail) const {
// obtain blocked GUIGlObject
GUIGlObject* object = GUIGlObjectStorage::gIDStorage.getObjectBlocking(id);
// Make sure that object exists
if (object != nullptr) {
// unblock and try to parse to AttributeCarrier
GUIGlObjectStorage::gIDStorage.unblockObject(id);
GNEAttributeCarrier* ac = dynamic_cast<GNEAttributeCarrier*>(object);
// If was successfully parsed, return it
if (ac == nullptr) {
throw ProcessError("GUIGlObject does not match the declared type");
} else {
return ac;
}
} else if (hardFail) {
throw ProcessError("Attempted to retrieve non-existant GUIGlObject");
} else {
return nullptr;
}
}
std::vector<GNEAttributeCarrier*>
GNENetHelper::AttributeCarriers::retrieveAttributeCarriers(SumoXMLTag tag) {
std::vector<GNEAttributeCarrier*> result;
if ((tag == SUMO_TAG_NOTHING) || (tag == SUMO_TAG_JUNCTION)) {
for (const auto& junction : myJunctions) {
result.push_back(junction.second);
}
} else if ((tag == SUMO_TAG_NOTHING) || (tag == SUMO_TAG_EDGE)) {
for (const auto& edge : myEdges) {
result.push_back(edge.second);
}
} else if ((tag == SUMO_TAG_NOTHING) || (tag == SUMO_TAG_LANE)) {
for (const auto& lane : myLanes) {
result.push_back(lane);
}
} else if ((tag == SUMO_TAG_NOTHING) || (tag == SUMO_TAG_CONNECTION)) {
for (const auto& connection : myConnections) {
result.push_back(connection);
}
} else if ((tag == SUMO_TAG_NOTHING) || (tag == SUMO_TAG_CROSSING)) {
for (const auto& crossing : myCrossings) {
result.push_back(crossing);
}
} else if ((tag == SUMO_TAG_NOTHING) || (tag == SUMO_TAG_WALKINGAREA)) {
for (const auto& walkingArea : myWalkingAreas) {
result.push_back(walkingArea);
}
} else if ((tag == SUMO_TAG_NOTHING) || (GNEAttributeCarrier::getTagProperty(tag).isAdditionalElement())) {
for (const auto& additional : myAdditionals.at(tag)) {
result.push_back(additional);
}
} else if ((tag == SUMO_TAG_NOTHING) || (GNEAttributeCarrier::getTagProperty(tag).isDemandElement())) {
for (const auto& demandElemet : myDemandElements.at(tag)) {
result.push_back(demandElemet);
}
} else if ((tag == SUMO_TAG_NOTHING) || (tag == SUMO_TAG_DATASET)) {
for (const auto& dataSet : myDataSets) {
result.push_back(dataSet);
}
} else if ((tag == SUMO_TAG_NOTHING) || (tag == SUMO_TAG_DATAINTERVAL)) {
for (const auto& dataInterval : myDataIntervals) {
result.push_back(dataInterval);
}
} else if ((tag == SUMO_TAG_NOTHING) || (GNEAttributeCarrier::getTagProperty(tag).isGenericData())) {
for (const auto& genericData : myGenericDatas.at(tag)) {
result.push_back(genericData);
}
} else if ((tag == SUMO_TAG_NOTHING) || (GNEAttributeCarrier::getTagProperty(tag).isMeanData())) {
for (const auto& meanData : myMeanDatas.at(tag)) {
result.push_back(meanData);
}
}
return result;
}
std::vector<GNEAttributeCarrier*>
GNENetHelper::AttributeCarriers::retrieveAttributeCarriers(Supermode supermode, const bool onlySelected) {
std::vector<GNEAttributeCarrier*> result;
// continue depending of supermode
if (supermode == Supermode::NETWORK) {
// network
for (const auto& junction : myJunctions) {
if (!onlySelected || junction.second->isAttributeCarrierSelected()) {
result.push_back(junction.second);
}
}
for (const auto& crossing : myCrossings) {
if (!onlySelected || crossing->isAttributeCarrierSelected()) {
result.push_back(crossing);
}
}
for (const auto& edge : myEdges) {
if (!onlySelected || edge.second->isAttributeCarrierSelected()) {
result.push_back(edge.second);
}
}
for (const auto& lane : myLanes) {
if (!onlySelected || lane->isAttributeCarrierSelected()) {
result.push_back(lane);
}
}
for (const auto& connection : myConnections) {
if (!onlySelected || connection->isAttributeCarrierSelected()) {
result.push_back(connection);
}
}
for (const auto& additionalSet : myAdditionals) {
for (const auto& additional : additionalSet.second) {
if (!onlySelected || additional->isAttributeCarrierSelected()) {
result.push_back(additional);
}
}
}
} else if (supermode == Supermode::DEMAND) {
for (const auto& demandElementSet : myDemandElements) {
for (const auto& demandElement : demandElementSet.second) {
if (!onlySelected || demandElement->isAttributeCarrierSelected()) {
result.push_back(demandElement);
}
}
}
} else if (supermode == Supermode::DATA) {
for (const auto& dataSet : myDataSets) {
if (!onlySelected || dataSet->isAttributeCarrierSelected()) {
result.push_back(dataSet);
}
}
for (const auto& dataInterval : myDataIntervals) {
if (!onlySelected || dataInterval->isAttributeCarrierSelected()) {
result.push_back(dataInterval);
}
}
for (const auto& genericDataSet : myGenericDatas) {
for (const auto& genericData : genericDataSet.second) {
if (!onlySelected || genericData->isAttributeCarrierSelected()) {
result.push_back(genericData);
}
}
}
for (const auto& meanDataSet : myMeanDatas) {
for (const auto& meanData : meanDataSet.second) {
if (!onlySelected || meanData->isAttributeCarrierSelected()) {
result.push_back(meanData);
}
}
}
}
return result;
}
std::vector<GNEAttributeCarrier*>
GNENetHelper::AttributeCarriers::getSelectedAttributeCarriers(const bool ignoreCurrentSupermode) {
// get modes
const auto& editModes = myNet->getViewNet()->getEditModes();
// declare vector to save result
std::vector<GNEAttributeCarrier*> result;
result.reserve(gSelected.getSelected().size());
// iterate over all elements of global selection
for (const auto& glID : gSelected.getSelected()) {
// obtain AC
GNEAttributeCarrier* AC = retrieveAttributeCarrier(glID, false);
// check if attribute carrier exist and is selected
if (AC && AC->isAttributeCarrierSelected()) {
bool insert = false;
if (ignoreCurrentSupermode) {
insert = true;
} else if (editModes.isCurrentSupermodeNetwork() && (AC->getTagProperty().isNetworkElement() || AC->getTagProperty().isAdditionalElement())) {
insert = true;
} else if (editModes.isCurrentSupermodeDemand() && AC->getTagProperty().isDemandElement()) {
insert = true;
} else if (editModes.isCurrentSupermodeData() && AC->getTagProperty().isDataElement()) {
insert = true;
}
if (insert) {
result.push_back(AC);
}
}
}
return result;
}
GNEJunction*
GNENetHelper::AttributeCarriers::retrieveJunction(const std::string& id, bool hardFail) const {
if (myJunctions.count(id)) {
return myJunctions.at(id);
} else if (hardFail) {
// If junction wasn't found, throw exception
throw UnknownElement("Junction " + id);
} else {
return nullptr;
}
}
const std::map<std::string, GNEJunction*>&
GNENetHelper::AttributeCarriers::getJunctions() const {
return myJunctions;
}
std::vector<GNEJunction*>
GNENetHelper::AttributeCarriers::getSelectedJunctions() const {
std::vector<GNEJunction*> result;
// returns junctions depending of selection
for (const auto& junction : myJunctions) {
if (junction.second->isAttributeCarrierSelected()) {
result.push_back(junction.second);
}
}
return result;
}
GNEJunction*
GNENetHelper::AttributeCarriers::registerJunction(GNEJunction* junction) {
// increase reference
junction->incRef("GNENet::registerJunction");
junction->setResponsible(false);
myJunctions[junction->getMicrosimID()] = junction;
// expand net boundary
myNet->expandBoundary(junction->getCenteringBoundary());
// add edge into grid
myNet->addGLObjectIntoGrid(junction);
// update geometry
junction->updateGeometry();
// add z in net boundary
myNet->addZValueInBoundary(junction->getNBNode()->getPosition().z());
return junction;
}
void
GNENetHelper::AttributeCarriers::clearJunctions() {
myJunctions.clear();
}
void
GNENetHelper::AttributeCarriers::addPrefixToJunctions(const std::string& prefix) {
// make a copy of junctions
std::map<std::string, GNEJunction*> junctionCopy = myJunctions;
// clear junctions
myJunctions.clear();
// fill junctions again
for (const auto& junction : junctionCopy) {
// update microsim ID
junction.second->setNetworkElementID(prefix + junction.first);
// insert in myJunctions again
myJunctions[prefix + junction.first] = junction.second;
}
}
void
GNENetHelper::AttributeCarriers::updateJunctionID(GNEJunction* junction, const std::string& newID) {
if (myJunctions.count(junction->getID()) == 0) {
throw ProcessError(junction->getTagStr() + " with ID='" + junction->getID() + "' doesn't exist in AttributeCarriers.junction");
} else if (myJunctions.count(newID) != 0) {
throw ProcessError("There is another " + junction->getTagStr() + " with new ID='" + newID + "' in myJunctions");
} else {
// remove junction from container
myJunctions.erase(junction->getNBNode()->getID());
// rename in NetBuilder
myNet->getNetBuilder()->getNodeCont().rename(junction->getNBNode(), newID);
// update microsim ID
junction->setNetworkElementID(newID);
// add it into myJunctions again
myJunctions[junction->getID()] = junction;
// build crossings
junction->getNBNode()->buildCrossings();
// net has to be saved
myNet->getSavingStatus()->requireSaveNetwork();
}
}
int
GNENetHelper::AttributeCarriers::getNumberOfSelectedJunctions() const {
int counter = 0;
for (const auto& junction : myJunctions) {
if (junction.second->isAttributeCarrierSelected()) {
counter++;
}
}
return counter;
}
GNECrossing*
GNENetHelper::AttributeCarriers::retrieveCrossing(GNEAttributeCarrier* AC, bool hardFail) const {
// cast crossing
GNECrossing* crossing = dynamic_cast<GNECrossing*>(AC);
if (crossing && (myCrossings.count(crossing) > 0)) {
return crossing;
} else if (hardFail) {
if (AC) {
throw UnknownElement("Crossing " + AC->getID());
} else {
throw UnknownElement("Crossing");
}
} else {
return nullptr;
}
}
const std::set<GNECrossing*>&
GNENetHelper::AttributeCarriers::getCrossings() const {
return myCrossings;
}
std::vector<GNECrossing*>
GNENetHelper::AttributeCarriers::getSelectedCrossings() const {
std::vector<GNECrossing*> result;
// iterate over crossings
for (const auto& crossing : myCrossings) {
if (crossing->isAttributeCarrierSelected()) {
result.push_back(crossing);
}
}
return result;
}
void
GNENetHelper::AttributeCarriers::insertCrossing(GNECrossing* crossing) {
if (myCrossings.insert(crossing).second == false) {
throw ProcessError(crossing->getTagStr() + " with ID='" + crossing->getID() + "' already exist");
}
}
void
GNENetHelper::AttributeCarriers::deleteCrossing(GNECrossing* crossing) {
const auto finder = myCrossings.find(crossing);
if (finder == myCrossings.end()) {
throw ProcessError(crossing->getTagStr() + " with ID='" + crossing->getID() + "' wasn't previously inserted");
} else {
myCrossings.erase(finder);
}
}
int
GNENetHelper::AttributeCarriers::getNumberOfSelectedCrossings() const {
int counter = 0;
for (const auto& crossing : myCrossings) {
if (crossing->isAttributeCarrierSelected()) {
counter++;
}
}
return counter;
}
GNEWalkingArea*
GNENetHelper::AttributeCarriers::retrieveWalkingArea(GNEAttributeCarrier* AC, bool hardFail) const {
// cast walkingArea
GNEWalkingArea* walkingArea = dynamic_cast<GNEWalkingArea*>(AC);
if (walkingArea && (myWalkingAreas.count(walkingArea) > 0)) {
return walkingArea;
} else if (hardFail) {
if (AC) {
throw UnknownElement("WalkingArea " + AC->getID());
} else {
throw UnknownElement("WalkingArea");
}
} else {
return nullptr;
}
}
const std::set<GNEWalkingArea*>&
GNENetHelper::AttributeCarriers::getWalkingAreas() const {
return myWalkingAreas;
}
std::vector<GNEWalkingArea*>
GNENetHelper::AttributeCarriers::getSelectedWalkingAreas() const {
std::vector<GNEWalkingArea*> result;
// iterate over walkingAreas
for (const auto& walkingArea : myWalkingAreas) {
if (walkingArea->isAttributeCarrierSelected()) {
result.push_back(walkingArea);
}
}
return result;
}
void
GNENetHelper::AttributeCarriers::insertWalkingArea(GNEWalkingArea* walkingArea) {
if (myWalkingAreas.insert(walkingArea).second == false) {
throw ProcessError(walkingArea->getTagStr() + " with ID='" + walkingArea->getID() + "' already exist");
}
}
void
GNENetHelper::AttributeCarriers::deleteWalkingArea(GNEWalkingArea* walkingArea) {
const auto finder = myWalkingAreas.find(walkingArea);
if (finder == myWalkingAreas.end()) {
throw ProcessError(walkingArea->getTagStr() + " with ID='" + walkingArea->getID() + "' wasn't previously inserted");
} else {
myWalkingAreas.erase(finder);
}
}
int
GNENetHelper::AttributeCarriers::getNumberOfSelectedWalkingAreas() const {
int counter = 0;
for (const auto& walkingArea : myWalkingAreas) {
if (walkingArea->isAttributeCarrierSelected()) {
counter++;
}
}
return counter;
}
GNEEdgeType*
GNENetHelper::AttributeCarriers::retrieveEdgeType(const std::string& id, bool hardFail) const {
if (myEdgeTypes.count(id) > 0) {
return myEdgeTypes.at(id);
} else if (hardFail) {
// If edge wasn't found, throw exception
throw UnknownElement("EdgeType " + id);
} else {
return nullptr;
}
}
GNEEdgeType*
GNENetHelper::AttributeCarriers::registerEdgeType(GNEEdgeType* edgeType) {
// increase reference
edgeType->incRef("GNENet::registerEdgeType");
// add it in container
myEdgeTypes[edgeType->getMicrosimID()] = edgeType;
return edgeType;
}
const std::map<std::string, GNEEdgeType*>&
GNENetHelper::AttributeCarriers::getEdgeTypes() const {
return myEdgeTypes;
}
void GNENetHelper::AttributeCarriers::clearEdgeTypes() {
myEdgeTypes.clear();
}
void
GNENetHelper::AttributeCarriers::updateEdgeTypeID(GNEEdgeType* edgeType, const std::string& newID) {
if (myEdgeTypes.count(edgeType->getID()) == 0) {
throw ProcessError(edgeType->getTagStr() + " with ID='" + edgeType->getID() + "' doesn't exist in AttributeCarriers.edgeType");
} else if (myEdgeTypes.count(newID) != 0) {
throw ProcessError("There is another " + edgeType->getTagStr() + " with new ID='" + newID + "' in myEdgeTypes");
} else {
// remove edgeType from container
myEdgeTypes.erase(edgeType->getID());
// rename in typeCont
myNet->getNetBuilder()->getTypeCont().updateEdgeTypeID(edgeType->getID(), newID);
// update microsim ID
edgeType->setNetworkElementID(newID);
// add it into myEdgeTypes again
myEdgeTypes[edgeType->getID()] = edgeType;
// net has to be saved
myNet->getSavingStatus()->requireSaveNetwork();
}
}
std::string
GNENetHelper::AttributeCarriers::generateEdgeTypeID() const {
int counter = 0;
while (myEdgeTypes.count("edgeType_" + toString(counter)) != 0) {
counter++;
}
return ("edgeType_" + toString(counter));
}
GNEEdge*
GNENetHelper::AttributeCarriers::retrieveEdge(const std::string& id, bool hardFail) const {
if (myEdges.count(id) > 0) {
return myEdges.at(id);
} else if (hardFail) {
// If edge wasn't found, throw exception
throw UnknownElement("Edge " + id);
} else {
return nullptr;
}
}
std::vector<GNEEdge*>
GNENetHelper::AttributeCarriers::retrieveEdges(GNEJunction* from, GNEJunction* to) const {
if ((from == nullptr) || (to == nullptr)) {
throw UnknownElement("Junctions cannot be nullptr");
}
std::vector<GNEEdge*> edges;
// iterate over Junctions
for (const auto& edge : myEdges) {
if ((edge.second->getFromJunction() == from) && (edge.second->getToJunction() == to)) {
edges.push_back(edge.second);
}
}
return edges;
}
const std::map<std::string, GNEEdge*>&
GNENetHelper::AttributeCarriers::getEdges() const {
return myEdges;
}
std::vector<GNEEdge*>
GNENetHelper::AttributeCarriers::getSelectedEdges() const {
std::vector<GNEEdge*> result;
// returns edges depending of selection
for (const auto& edge : myEdges) {
if (edge.second->isAttributeCarrierSelected()) {
result.push_back(edge.second);
}
}
return result;
}
GNEEdge*
GNENetHelper::AttributeCarriers::registerEdge(GNEEdge* edge) {
edge->incRef("GNENet::registerEdge");
edge->setResponsible(false);
// add edge to internal container of GNENet
myEdges[edge->getMicrosimID()] = edge;
// expand edge boundary
myNet->expandBoundary(edge->getCenteringBoundary());
// add edge into grid
myNet->addGLObjectIntoGrid(edge);
// insert all lanes
for (const auto& lane : edge->getLanes()) {
insertLane(lane);
}
// Add references into GNEJunctions
edge->getFromJunction()->addOutgoingGNEEdge(edge);
edge->getToJunction()->addIncomingGNEEdge(edge);
return edge;
}
void
GNENetHelper::AttributeCarriers::clearEdges() {
myEdges.clear();
}
void
GNENetHelper::AttributeCarriers::addPrefixToEdges(const std::string& prefix) {
// make a copy of edges
std::map<std::string, GNEEdge*> edgeCopy = myEdges;
// clear edges
myEdges.clear();
// fill edges again
for (const auto& edge : edgeCopy) {
// update microsim ID
edge.second->setNetworkElementID(prefix + edge.first);
// insert in myEdges again
myEdges[prefix + edge.first] = edge.second;
}
}
void
GNENetHelper::AttributeCarriers::updateEdgeID(GNEEdge* edge, const std::string& newID) {
if (myEdges.count(edge->getID()) == 0) {
throw ProcessError(edge->getTagStr() + " with ID='" + edge->getID() + "' doesn't exist in AttributeCarriers.edge");
} else if (myEdges.count(newID) != 0) {
throw ProcessError("There is another " + edge->getTagStr() + " with new ID='" + newID + "' in myEdges");
} else {
// remove edge from container
myEdges.erase(edge->getNBEdge()->getID());
// rename in NetBuilder
myNet->getNetBuilder()->getEdgeCont().rename(edge->getNBEdge(), newID);
// update microsim ID
edge->setEdgeID(newID);
// add it into myEdges again
myEdges[edge->getID()] = edge;
// rename all connections related to this edge
for (const auto& lane : edge->getLanes()) {
lane->updateConnectionIDs();
}
// net has to be saved
myNet->getSavingStatus()->requireSaveNetwork();
}
}
int
GNENetHelper::AttributeCarriers::getNumberOfSelectedEdges() const {
int counter = 0;
for (const auto& edge : myEdges) {
if (edge.second->isAttributeCarrierSelected()) {
counter++;
}
}
return counter;
}
GNELane*
GNENetHelper::AttributeCarriers::retrieveLane(const std::string& id, bool hardFail, bool checkVolatileChange) const {
const std::string edge_id = SUMOXMLDefinitions::getEdgeIDFromLane(id);
const GNEEdge* edge = retrieveEdge(edge_id, false);
if (edge != nullptr) {
GNELane* lane = nullptr;
// search lane in lane's edges
for (auto laneIt : edge->getLanes()) {
if (laneIt->getID() == id) {
lane = laneIt;
}
}
// throw exception or return nullptr if lane wasn't found
if (lane == nullptr) {
if (hardFail) {
// Throw exception if hardFail is enabled
throw UnknownElement(toString(SUMO_TAG_LANE) + " " + id);
}
} else {
// check if the recomputing with volatile option has changed the number of lanes (needed for additionals and demand elements)
if (checkVolatileChange && (myNet->getEdgesAndNumberOfLanes().count(edge_id) == 1) &&
myNet->getEdgesAndNumberOfLanes().at(edge_id) != (int)edge->getLanes().size()) {
return edge->getLanes().at(lane->getIndex() + 1);
}
return lane;
}
} else if (hardFail) {
// Throw exception if hardFail is enabled
throw UnknownElement(toString(SUMO_TAG_EDGE) + " " + edge_id);
}
return nullptr;
}
GNELane*
GNENetHelper::AttributeCarriers::retrieveLane(GNEAttributeCarrier* AC, bool hardFail) const {
// cast lane
GNELane* lane = dynamic_cast<GNELane*>(AC);
if (lane && (myLanes.count(lane) > 0)) {
return lane;
} else if (hardFail) {
if (AC) {
throw UnknownElement("Lane " + AC->getID());
} else {
throw UnknownElement("Lane");
}
} else {
return nullptr;
}
}
const std::set<GNELane*>&
GNENetHelper::AttributeCarriers::getLanes() const {
return myLanes;
}
std::vector<GNELane*>
GNENetHelper::AttributeCarriers::getSelectedLanes() const {
std::vector<GNELane*> result;
// returns lanes depending of selection
for (const auto& lane : myLanes) {
if (lane->isAttributeCarrierSelected()) {
result.push_back(lane);
}
}
return result;
}
void
GNENetHelper::AttributeCarriers::insertLane(GNELane* lane) {
if (myLanes.insert(lane).second == false) {
throw ProcessError(lane->getTagStr() + " with ID='" + lane->getID() + "' already exist");
}
}
void
GNENetHelper::AttributeCarriers::deleteLane(GNELane* lane) {
const auto finder = myLanes.find(lane);
if (finder == myLanes.end()) {
throw ProcessError(lane->getTagStr() + " with ID='" + lane->getID() + "' wasn't previously inserted");
} else {
myLanes.erase(finder);
}
}
int
GNENetHelper::AttributeCarriers::getNumberOfSelectedLanes() const {
int counter = 0;
for (const auto& lane : myLanes) {
if (lane->isAttributeCarrierSelected()) {
counter++;
}
}
return counter;
}
GNEConnection*
GNENetHelper::AttributeCarriers::retrieveConnection(const std::string& id, bool hardFail) const {
// iterate over connections
for (const auto& connection : myConnections) {
if (connection->getID() == id) {
return connection;
}
}
if (hardFail) {
// If POI wasn't found, throw exception
throw UnknownElement("Connection " + id);
} else {
return nullptr;
}
}
GNEConnection*
GNENetHelper::AttributeCarriers::retrieveConnection(GNEAttributeCarrier* AC, bool hardFail) const {
// cast connection
GNEConnection* connection = dynamic_cast<GNEConnection*>(AC);
if (connection && (myConnections.count(connection) > 0)) {
return connection;
} else if (hardFail) {
if (AC) {
throw UnknownElement("Connection " + AC->getID());
} else {
throw UnknownElement("Connection");
}
} else {
return nullptr;
}
}
const std::set<GNEConnection*>&
GNENetHelper::AttributeCarriers::getConnections() const {
return myConnections;
}
std::vector<GNEConnection*>
GNENetHelper::AttributeCarriers::getSelectedConnections() const {
std::vector<GNEConnection*> result;
// returns connections depending of selection
for (const auto& connection : myConnections) {
if (connection->isAttributeCarrierSelected()) {
result.push_back(connection);
}
}
return result;
}
void
GNENetHelper::AttributeCarriers::insertConnection(GNEConnection* connection) {
if (myConnections.insert(connection).second == false) {
throw ProcessError(connection->getTagStr() + " with ID='" + connection->getID() + "' already exist");
}
}