-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
GNERide.cpp
578 lines (504 loc) · 20.8 KB
/
GNERide.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
/****************************************************************************/
// 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 GNERide.cpp
/// @author Pablo Alvarez Lopez
/// @date Jun 2019
///
// A class for visualizing rides in Netedit
/****************************************************************************/
#include <config.h>
#include <utils/gui/windows/GUIAppEnum.h>
#include <netedit/changes/GNEChange_Attribute.h>
#include <netedit/GNENet.h>
#include <netedit/GNEUndoList.h>
#include <netedit/GNEViewNet.h>
#include <utils/gui/div/GUIDesigns.h>
#include "GNERide.h"
// ===========================================================================
// method definitions
// ===========================================================================
GNERide::GNERide(SumoXMLTag tag, GNENet* net) :
GNEDemandElement("", net, GLO_RIDE, tag, GUIIconSubSys::getIcon(GUIIcon::RIDE_FROMTO),
GNEPathManager::PathElement::Options::DEMAND_ELEMENT, {}, {}, {}, {}, {}, {}),
GNEDemandElementPlan(this),
myArrivalPosition(0) {
// reset default values
resetDefaultValues();
}
GNERide::GNERide(GNENet* net, GNEDemandElement* personParent, GNEEdge* fromEdge, GNEEdge* toEdge,
double arrivalPosition, const std::vector<std::string>& lines) :
GNEDemandElement(personParent, net, GLO_RIDE, GNE_TAG_RIDE_EDGE, GUIIconSubSys::getIcon(GUIIcon::RIDE_FROMTO),
GNEPathManager::PathElement::Options::DEMAND_ELEMENT, {}, {fromEdge, toEdge}, {}, {}, {personParent}, {}),
GNEDemandElementPlan(this),
myArrivalPosition(arrivalPosition),
myLines(lines) {
}
GNERide::GNERide(bool isTrain, GNENet* net, GNEDemandElement* personParent, GNEEdge* fromEdge, GNEAdditional* toBusStop,
double arrivalPosition, const std::vector<std::string>& lines) :
GNEDemandElement(personParent, net, GLO_RIDE, isTrain ? GNE_TAG_RIDE_TRAINSTOP : GNE_TAG_RIDE_BUSSTOP,
GUIIconSubSys::getIcon(isTrain ? GUIIcon::RIDE_TRAINSTOP : GUIIcon::RIDE_BUSSTOP),
GNEPathManager::PathElement::Options::DEMAND_ELEMENT, {}, {fromEdge}, {}, {toBusStop}, {personParent}, {}),
GNEDemandElementPlan(this),
myArrivalPosition(arrivalPosition),
myLines(lines) {
}
GNERide::~GNERide() {}
GNEMoveOperation*
GNERide::getMoveOperation() {
// avoid move person plan that ends in busStop
if (getParentAdditionals().size() > 0) {
return nullptr;
}
// get geometry end pos
const Position geometryEndPos = getPathElementArrivalPos();
// calculate circle width squared
const double circleWidthSquared = myPersonPlanArrivalPositionDiameter * myPersonPlanArrivalPositionDiameter;
// check if we clicked over a geometry end pos
if (myNet->getViewNet()->getPositionInformation().distanceSquaredTo2D(geometryEndPos) <= ((circleWidthSquared + 2))) {
return new GNEMoveOperation(this, getParentEdges().back()->getLaneByDisallowedVClass(getVClass()), myArrivalPosition, false);
} else {
return nullptr;
}
}
GUIGLObjectPopupMenu*
GNERide::getPopUpMenu(GUIMainWindow& app, GUISUMOAbstractView& parent) {
GUIGLObjectPopupMenu* ret = new GUIGLObjectPopupMenu(app, parent, *this);
// build header
buildPopupHeader(ret, app);
// build menu command for center button and copy cursor position to clipboard
buildCenterPopupEntry(ret);
buildPositionCopyEntry(ret, app);
// build menu commands for names
GUIDesigns::buildFXMenuCommand(ret, "Copy " + getTagStr() + " name to clipboard", nullptr, ret, MID_COPY_NAME);
GUIDesigns::buildFXMenuCommand(ret, "Copy " + getTagStr() + " typed name to clipboard", nullptr, ret, MID_COPY_TYPED_NAME);
new FXMenuSeparator(ret);
// build selection and show parameters menu
myNet->getViewNet()->buildSelectionACPopupEntry(ret, this);
buildShowParamsPopupEntry(ret);
// show option to open demand element dialog
if (myTagProperty.hasDialog()) {
GUIDesigns::buildFXMenuCommand(ret, "Open " + getTagStr() + " Dialog", getACIcon(), &parent, MID_OPEN_ADDITIONAL_DIALOG);
new FXMenuSeparator(ret);
}
GUIDesigns::buildFXMenuCommand(ret, "Cursor position in view: " + toString(getPositionInView().x()) + "," + toString(getPositionInView().y()), nullptr, nullptr, 0);
return ret;
}
void
GNERide::writeDemandElement(OutputDevice& device) const {
// open tag
device.openTag(SUMO_TAG_RIDE);
// check if from attribute is enabled
if (isAttributeEnabled(SUMO_ATTR_FROM)) {
device.writeAttr(SUMO_ATTR_FROM, getParentEdges().front()->getID());
}
// write to depending if personplan ends in a busStop
if (getParentAdditionals().size() > 0) {
if (getParentAdditionals().back()->getTagProperty().getTag() == SUMO_TAG_BUS_STOP) {
device.writeAttr(SUMO_ATTR_BUS_STOP, getParentAdditionals().back()->getID());
} else {
device.writeAttr(SUMO_ATTR_TRAIN_STOP, getParentAdditionals().back()->getID());
}
} else {
device.writeAttr(SUMO_ATTR_TO, getParentEdges().back()->getID());
}
// avoid write arrival positions in ride to busStop
if ((myTagProperty.getTag() != GNE_TAG_RIDE_BUSSTOP) && (myTagProperty.getTag() != GNE_TAG_RIDE_TRAINSTOP) &&
(myArrivalPosition > 0)) {
device.writeAttr(SUMO_ATTR_ARRIVALPOS, myArrivalPosition);
}
// write lines
if (myLines.empty()) {
device.writeAttr(SUMO_ATTR_LINES, "ANY");
} else {
device.writeAttr(SUMO_ATTR_LINES, myLines);
}
// close tag
device.closeTag();
}
GNEDemandElement::Problem
GNERide::isDemandElementValid() const {
return isPersonPlanValid();
}
std::string
GNERide::getDemandElementProblem() const {
return getPersonPlanProblem();
}
void
GNERide::fixDemandElementProblem() {
// currently the only solution is removing Ride
}
SUMOVehicleClass
GNERide::getVClass() const {
return getParentDemandElements().front()->getVClass();
}
const RGBColor&
GNERide::getColor() const {
return getParentDemandElements().front()->getColor();
}
void
GNERide::updateGeometry() {
// update child demand elements
for (const auto& i : getChildDemandElements()) {
i->updateGeometry();
}
}
Position
GNERide::getPositionInView() const {
return getParentEdges().front()->getPositionInView();
}
std::string
GNERide::getParentName() const {
return getParentDemandElements().front()->getID();
}
Boundary
GNERide::getCenteringBoundary() const {
Boundary rideBoundary;
// return the combination of all parent edges's boundaries
for (const auto& i : getParentEdges()) {
rideBoundary.add(i->getCenteringBoundary());
}
// check if is valid
if (rideBoundary.isInitialised()) {
return rideBoundary;
} else {
return Boundary(-0.1, -0.1, 0.1, 0.1);
}
}
void
GNERide::splitEdgeGeometry(const double /*splitPosition*/, const GNENetworkElement* /*originalElement*/, const GNENetworkElement* /*newElement*/, GNEUndoList* /*undoList*/) {
// geometry of this element cannot be splitted
}
void
GNERide::drawGL(const GUIVisualizationSettings& s) const {
// force draw path
myNet->getPathManager()->forceDrawPath(s, this);
}
void
GNERide::computePathElement() {
// get lanes
const std::vector<GNELane*> lanes = {getFirstPathLane(), getLastPathLane()};
// calculate path
myNet->getPathManager()->calculatePathLanes(this, SVC_PASSENGER, lanes);
// check path (taxis)
if (!myNet->getPathManager()->isPathValid(this)) {
myNet->getPathManager()->calculatePathLanes(this, SVC_TAXI, lanes);
}
// check path (bus)
if (!myNet->getPathManager()->isPathValid(this)) {
myNet->getPathManager()->calculatePathLanes(this, SVC_BUS, lanes);
}
// check path (bicycle)
if (!myNet->getPathManager()->isPathValid(this)) {
myNet->getPathManager()->calculatePathLanes(this, SVC_BICYCLE, lanes);
}
// check path (pedestrian)
if (!myNet->getPathManager()->isPathValid(this)) {
myNet->getPathManager()->calculatePathLanes(this, SVC_PEDESTRIAN, lanes);
}
// update geometry
updateGeometry();
}
void
GNERide::drawPartialGL(const GUIVisualizationSettings& s, const GNELane* lane, const GNEPathManager::Segment* segment, const double offsetFront) const {
// draw person plan over lane
drawPersonPlanPartial(drawPersonPlan(), s, lane, segment, offsetFront, s.widthSettings.rideWidth, s.colorSettings.rideColor);
}
void
GNERide::drawPartialGL(const GUIVisualizationSettings& s, const GNELane* fromLane, const GNELane* toLane, const GNEPathManager::Segment* segment, const double offsetFront) const {
// draw person plan over junction
drawPersonPlanPartial(drawPersonPlan(), s, fromLane, toLane, segment, offsetFront, s.widthSettings.rideWidth, s.colorSettings.rideColor);
}
GNELane*
GNERide::getFirstPathLane() const {
return getParentEdges().front()->getLaneByDisallowedVClass(SVC_PEDESTRIAN);
}
GNELane*
GNERide::getLastPathLane() const {
// check if personPlan ends in a BusStop
if (getParentAdditionals().size() > 0) {
return getParentAdditionals().front()->getParentLanes().front();
} else {
return getParentEdges().back()->getLaneByDisallowedVClass(SVC_PEDESTRIAN);
}
}
std::string
GNERide::getAttribute(SumoXMLAttr key) const {
switch (key) {
// Common person plan attributes
case SUMO_ATTR_ID:
case GNE_ATTR_PARENT:
return getParentDemandElements().front()->getID();
case SUMO_ATTR_FROM:
return getParentEdges().front()->getID();
case SUMO_ATTR_TO:
return getParentEdges().back()->getID();
case GNE_ATTR_TO_BUSSTOP:
case GNE_ATTR_TO_TRAINSTOP:
return getParentAdditionals().back()->getID();
// specific person plan attributes
case SUMO_ATTR_LINES:
return joinToString(myLines, " ");
case SUMO_ATTR_ARRIVALPOS:
if (myArrivalPosition == -1) {
return "";
} else {
return toString(myArrivalPosition);
}
case GNE_ATTR_SELECTED:
return toString(isAttributeCarrierSelected());
default:
throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
}
}
double
GNERide::getAttributeDouble(SumoXMLAttr key) const {
switch (key) {
case SUMO_ATTR_ARRIVALPOS:
if (myArrivalPosition != -1) {
return myArrivalPosition;
} else {
return (getLastPathLane()->getLaneShape().length() - POSITION_EPS);
}
default:
throw InvalidArgument(getTagStr() + " doesn't have a double attribute of type '" + toString(key) + "'");
}
}
Position
GNERide::getAttributePosition(SumoXMLAttr key) const {
switch (key) {
case SUMO_ATTR_ARRIVALPOS: {
// get lane shape
const PositionVector& laneShape = getLastPathLane()->getLaneShape();
// continue depending of arrival position
if (myArrivalPosition == 0) {
return laneShape.front();
} else if ((myArrivalPosition == -1) || (myArrivalPosition >= laneShape.length2D())) {
return laneShape.back();
} else {
return laneShape.positionAtOffset2D(myArrivalPosition);
}
}
default:
throw InvalidArgument(getTagStr() + " doesn't have a position attribute of type '" + toString(key) + "'");
}
}
void
GNERide::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
switch (key) {
// Common person plan attributes
case SUMO_ATTR_FROM:
case SUMO_ATTR_ARRIVALPOS:
case SUMO_ATTR_LINES:
case GNE_ATTR_SELECTED:
case GNE_ATTR_PARENT:
GNEChange_Attribute::changeAttribute(this, key, value, undoList);
break;
// special case for "to" attributes
case SUMO_ATTR_TO: {
// get next personPlan
GNEDemandElement* nextPersonPlan = getParentDemandElements().at(0)->getNextChildDemandElement(this);
// continue depending of nextPersonPlan
if (nextPersonPlan) {
undoList->begin(this, "Change from attribute of next personPlan");
nextPersonPlan->setAttribute(SUMO_ATTR_FROM, value, undoList);
GNEChange_Attribute::changeAttribute(this, key, value, undoList);
undoList->end();
} else {
GNEChange_Attribute::changeAttribute(this, key, value, undoList);
}
break;
}
case GNE_ATTR_TO_BUSSTOP: {
// get next person plan
GNEDemandElement* nextPersonPlan = getParentDemandElements().at(0)->getNextChildDemandElement(this);
// continue depending of nextPersonPlan
if (nextPersonPlan) {
// obtain busStop
const GNEAdditional* busStop = myNet->getAttributeCarriers()->retrieveAdditional(SUMO_TAG_BUS_STOP, value);
// change from attribute using edge ID
undoList->begin(this, "Change from attribute of next personPlan");
nextPersonPlan->setAttribute(SUMO_ATTR_FROM, busStop->getParentLanes().front()->getParentEdge()->getID(), undoList);
GNEChange_Attribute::changeAttribute(this, key, value, undoList);
undoList->end();
} else {
GNEChange_Attribute::changeAttribute(this, key, value, undoList);
}
break;
}
case GNE_ATTR_TO_TRAINSTOP: {
// get next person plan
GNEDemandElement* nextPersonPlan = getParentDemandElements().at(0)->getNextChildDemandElement(this);
// continue depending of nextPersonPlan
if (nextPersonPlan) {
// obtain trainStop
const GNEAdditional* trainStop = myNet->getAttributeCarriers()->retrieveAdditional(SUMO_TAG_TRAIN_STOP, value);
// change from attribute using edge ID
undoList->begin(this, "Change from attribute of next personPlan");
nextPersonPlan->setAttribute(SUMO_ATTR_FROM, trainStop->getParentLanes().front()->getParentEdge()->getID(), undoList);
GNEChange_Attribute::changeAttribute(this, key, value, undoList);
undoList->end();
} else {
GNEChange_Attribute::changeAttribute(this, key, value, undoList);
}
break;
}
default:
throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
}
}
bool
GNERide::isValid(SumoXMLAttr key, const std::string& value) {
switch (key) {
// Common person plan attributes
case SUMO_ATTR_FROM:
case SUMO_ATTR_TO:
return (myNet->getAttributeCarriers()->retrieveEdge(value, false) != nullptr);
case GNE_ATTR_TO_BUSSTOP:
return (myNet->getAttributeCarriers()->retrieveAdditional(SUMO_TAG_BUS_STOP, value, false) != nullptr);
case GNE_ATTR_TO_TRAINSTOP:
return (myNet->getAttributeCarriers()->retrieveAdditional(SUMO_TAG_TRAIN_STOP, value, false) != nullptr);
// specific person plan attributes
case SUMO_ATTR_LINES:
return canParse<std::vector<std::string> >(value);
case SUMO_ATTR_ARRIVALPOS:
if (value.empty()) {
return true;
} else if (canParse<double>(value)) {
if (isTemplate()) {
return true;
}
const double parsedValue = canParse<double>(value);
if ((parsedValue < 0) || (parsedValue > getLastPathLane()->getLaneShape().length())) {
return false;
} else {
return true;
}
} else {
return false;
}
case GNE_ATTR_SELECTED:
return canParse<bool>(value);
case GNE_ATTR_PARENT:
if (myNet->getAttributeCarriers()->retrieveDemandElement(SUMO_TAG_PERSON, value, false) != nullptr) {
return true;
} else if (myNet->getAttributeCarriers()->retrieveDemandElement(SUMO_TAG_PERSONFLOW, value, false) != nullptr) {
return true;
} else {
return false;
}
default:
throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
}
}
bool
GNERide::isAttributeEnabled(SumoXMLAttr key) const {
if (key == SUMO_ATTR_FROM) {
return (getParentDemandElements().at(0)->getPreviousChildDemandElement(this) == nullptr);
} else {
return true;
}
}
std::string
GNERide::getPopUpID() const {
return getTagStr();
}
std::string
GNERide::getHierarchyName() const {
if (myTagProperty.getTag() == GNE_TAG_RIDE_EDGE) {
return "ride: " + getParentEdges().front()->getID() + " -> " + getParentEdges().back()->getID();
} else if ((myTagProperty.getTag() == GNE_TAG_RIDE_BUSSTOP) || (myTagProperty.getTag() == GNE_TAG_RIDE_TRAINSTOP)) {
return "ride: " + getParentEdges().front()->getID() + " -> " + getParentAdditionals().back()->getID();
} else {
throw ("Invalid ride tag");
}
}
const Parameterised::Map&
GNERide::getACParametersMap() const {
return getParametersMap();
}
// ===========================================================================
// private
// ===========================================================================
void
GNERide::setAttribute(SumoXMLAttr key, const std::string& value) {
switch (key) {
// Common person plan attributes
case SUMO_ATTR_FROM:
// change first edge
replaceFirstParentEdge(value);
// compute ride
computePathElement();
break;
case SUMO_ATTR_TO:
// change last edge
replaceLastParentEdge(value);
// compute ride
computePathElement();
break;
case GNE_ATTR_TO_BUSSTOP:
replaceFirstParentAdditional(SUMO_TAG_BUS_STOP, value);
// compute ride
computePathElement();
break;
case GNE_ATTR_TO_TRAINSTOP:
replaceFirstParentAdditional(SUMO_TAG_TRAIN_STOP, value);
// compute ride
computePathElement();
break;
// specific person plan attributes
case SUMO_ATTR_LINES:
myLines = GNEAttributeCarrier::parse<std::vector<std::string> >(value);
break;
case SUMO_ATTR_ARRIVALPOS:
if (value.empty()) {
myArrivalPosition = -1;
} else {
myArrivalPosition = parse<double>(value);
}
updateGeometry();
break;
case GNE_ATTR_SELECTED:
if (parse<bool>(value)) {
selectAttributeCarrier();
} else {
unselectAttributeCarrier();
}
break;
case GNE_ATTR_PARENT:
if (myNet->getAttributeCarriers()->retrieveDemandElement(SUMO_TAG_PERSON, value, false) != nullptr) {
replaceDemandElementParent(SUMO_TAG_PERSON, value, 0);
} else if (myNet->getAttributeCarriers()->retrieveDemandElement(SUMO_TAG_PERSONFLOW, value, false) != nullptr) {
replaceDemandElementParent(SUMO_TAG_PERSONFLOW, value, 0);
}
updateGeometry();
break;
default:
throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
}
}
void
GNERide::setMoveShape(const GNEMoveResult& moveResult) {
// change both position
myArrivalPosition = moveResult.newSecondPos;
// update geometry
updateGeometry();
}
void
GNERide::commitMoveShape(const GNEMoveResult& moveResult, GNEUndoList* undoList) {
undoList->begin(this, "arrivalPos of " + getTagStr());
// now adjust start position
setAttribute(SUMO_ATTR_ARRIVALPOS, toString(moveResult.newFirstPos), undoList);
undoList->end();
}
/****************************************************************************/