Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/libtraci/Person.cpp
169665 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2017-2025 German Aerospace Center (DLR) and others.
4
// This program and the accompanying materials are made available under the
5
// terms of the Eclipse Public License 2.0 which is available at
6
// https://www.eclipse.org/legal/epl-2.0/
7
// This Source Code may also be made available under the following Secondary
8
// Licenses when the conditions for such availability set forth in the Eclipse
9
// Public License 2.0 are satisfied: GNU General Public License, version 2
10
// or later which is available at
11
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
/****************************************************************************/
14
/// @file Person.cpp
15
/// @author Leonhard Luecken
16
/// @date 15.09.2017
17
///
18
// C++ TraCI client API implementation
19
/****************************************************************************/
20
#include <config.h>
21
22
#define LIBTRACI 1
23
#include "Domain.h"
24
#include <libsumo/Person.h>
25
#include <libsumo/StorageHelper.h>
26
27
namespace libtraci {
28
29
typedef Domain<libsumo::CMD_GET_PERSON_VARIABLE, libsumo::CMD_SET_PERSON_VARIABLE> Dom;
30
31
// ===========================================================================
32
// static member definitions
33
// ===========================================================================
34
std::vector<std::string>
35
Person::getIDList() {
36
return Dom::getStringVector(libsumo::TRACI_ID_LIST, "");
37
}
38
39
40
int
41
Person::getIDCount() {
42
return Dom::getInt(libsumo::ID_COUNT, "");
43
}
44
45
46
libsumo::TraCIPosition
47
Person::getPosition(const std::string& personID, const bool includeZ) {
48
return includeZ ? getPosition3D(personID) : Dom::getPos(libsumo::VAR_POSITION, personID);
49
}
50
51
52
libsumo::TraCIPosition
53
Person::getPosition3D(const std::string& personID) {
54
return Dom::getPos3D(libsumo::VAR_POSITION3D, personID);
55
}
56
57
58
double
59
Person::getAngle(const std::string& personID) {
60
return Dom::getDouble(libsumo::VAR_ANGLE, personID);
61
}
62
63
64
double
65
Person::getSlope(const std::string& personID) {
66
return Dom::getDouble(libsumo::VAR_SLOPE, personID);
67
}
68
69
70
double
71
Person::getSpeed(const std::string& personID) {
72
return Dom::getDouble(libsumo::VAR_SPEED, personID);
73
}
74
75
76
std::string
77
Person::getRoadID(const std::string& personID) {
78
return Dom::getString(libsumo::VAR_ROAD_ID, personID);
79
}
80
81
82
std::string
83
Person::getLaneID(const std::string& personID) {
84
return Dom::getString(libsumo::VAR_LANE_ID, personID);
85
}
86
87
88
double
89
Person::getLanePosition(const std::string& personID) {
90
return Dom::getDouble(libsumo::VAR_LANEPOSITION, personID);
91
}
92
93
94
double
95
Person::getWalkingDistance(const std::string& personID, const std::string& edgeID, double pos, int laneIndex) {
96
tcpip::Storage content;
97
StoHelp::writeCompound(content, 2);
98
content.writeUnsignedByte(libsumo::POSITION_ROADMAP);
99
content.writeString(edgeID);
100
content.writeDouble(pos);
101
content.writeUnsignedByte(laneIndex);
102
content.writeUnsignedByte(libsumo::REQUEST_DRIVINGDIST);
103
return Dom::getDouble(libsumo::DISTANCE_REQUEST, personID, &content);
104
}
105
106
107
double
108
Person::getWalkingDistance2D(const std::string& personID, double x, double y) {
109
tcpip::Storage content;
110
StoHelp::writeCompound(content, 2);
111
content.writeUnsignedByte(libsumo::POSITION_2D);
112
content.writeDouble(x);
113
content.writeDouble(y);
114
content.writeUnsignedByte(libsumo::REQUEST_DRIVINGDIST);
115
return Dom::getDouble(libsumo::DISTANCE_REQUEST, personID, &content);
116
}
117
118
119
std::vector<libsumo::TraCIReservation>
120
Person::getTaxiReservations(int onlyNew) {
121
std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
122
tcpip::Storage content;
123
StoHelp::writeTypedInt(content, onlyNew);
124
tcpip::Storage& ret = Dom::get(libsumo::VAR_TAXI_RESERVATIONS, "", &content);
125
std::vector<libsumo::TraCIReservation> result;
126
int numReservations = ret.readInt();
127
while (numReservations-- > 0) {
128
libsumo::TraCIReservation r;
129
StoHelp::readReservation(ret, r);
130
result.emplace_back(r);
131
}
132
return result;
133
}
134
135
136
std::string
137
Person::splitTaxiReservation(std::string reservationID, const std::vector<std::string>& personIDs) {
138
tcpip::Storage content;
139
StoHelp::writeTypedStringList(content, personIDs);
140
return Dom::getString(libsumo::SPLIT_TAXI_RESERVATIONS, reservationID, &content);
141
}
142
143
144
libsumo::TraCIColor
145
Person::getColor(const std::string& personID) {
146
return Dom::getCol(libsumo::VAR_COLOR, personID);
147
}
148
149
150
std::string
151
Person::getTypeID(const std::string& personID) {
152
return Dom::getString(libsumo::VAR_TYPE, personID);
153
}
154
155
156
double
157
Person::getWaitingTime(const std::string& personID) {
158
return Dom::getDouble(libsumo::VAR_WAITING_TIME, personID);
159
}
160
161
162
std::string
163
Person::getNextEdge(const std::string& personID) {
164
return Dom::getString(libsumo::VAR_NEXT_EDGE, personID);
165
}
166
167
168
std::vector<std::string>
169
Person::getEdges(const std::string& personID, int nextStageIndex) {
170
tcpip::Storage content;
171
content.writeUnsignedByte(libsumo::TYPE_INTEGER);
172
content.writeInt(nextStageIndex);
173
return Dom::getStringVector(libsumo::VAR_EDGES, personID, &content);
174
}
175
176
177
libsumo::TraCIStage
178
Person::getStage(const std::string& personID, int nextStageIndex) {
179
tcpip::Storage content;
180
content.writeUnsignedByte(libsumo::TYPE_INTEGER);
181
content.writeInt(nextStageIndex);
182
return Dom::getTraCIStage(libsumo::VAR_STAGE, personID, &content);
183
}
184
185
186
int
187
Person::getRemainingStages(const std::string& personID) {
188
return Dom::getInt(libsumo::VAR_STAGES_REMAINING, personID);
189
}
190
191
192
std::string
193
Person::getVehicle(const std::string& personID) {
194
return Dom::getString(libsumo::VAR_VEHICLE, personID);
195
}
196
197
198
std::string
199
Person::getEmissionClass(const std::string& personID) {
200
return Dom::getString(libsumo::VAR_EMISSIONCLASS, personID);
201
}
202
203
204
std::string
205
Person::getShapeClass(const std::string& personID) {
206
return Dom::getString(libsumo::VAR_SHAPECLASS, personID);
207
}
208
209
210
double
211
Person::getLength(const std::string& personID) {
212
return Dom::getDouble(libsumo::VAR_LENGTH, personID);
213
}
214
215
216
double
217
Person::getSpeedFactor(const std::string& personID) {
218
return Dom::getDouble(libsumo::VAR_SPEED_FACTOR, personID);
219
}
220
221
222
double
223
Person::getAccel(const std::string& personID) {
224
return Dom::getDouble(libsumo::VAR_ACCEL, personID);
225
}
226
227
228
double
229
Person::getDecel(const std::string& personID) {
230
return Dom::getDouble(libsumo::VAR_DECEL, personID);
231
}
232
233
234
double Person::getEmergencyDecel(const std::string& personID) {
235
return Dom::getDouble(libsumo::VAR_EMERGENCY_DECEL, personID);
236
}
237
238
239
double Person::getApparentDecel(const std::string& personID) {
240
return Dom::getDouble(libsumo::VAR_APPARENT_DECEL, personID);
241
}
242
243
244
double Person::getActionStepLength(const std::string& personID) {
245
return Dom::getDouble(libsumo::VAR_ACTIONSTEPLENGTH, personID);
246
}
247
248
249
double
250
Person::getTau(const std::string& personID) {
251
return Dom::getDouble(libsumo::VAR_TAU, personID);
252
}
253
254
255
double
256
Person::getImperfection(const std::string& personID) {
257
return Dom::getDouble(libsumo::VAR_IMPERFECTION, personID);
258
}
259
260
261
double
262
Person::getSpeedDeviation(const std::string& personID) {
263
return Dom::getDouble(libsumo::VAR_SPEED_DEVIATION, personID);
264
}
265
266
267
std::string
268
Person::getVehicleClass(const std::string& personID) {
269
return Dom::getString(libsumo::VAR_VEHICLECLASS, personID);
270
}
271
272
273
double
274
Person::getMinGap(const std::string& personID) {
275
return Dom::getDouble(libsumo::VAR_MINGAP, personID);
276
}
277
278
279
double
280
Person::getMinGapLat(const std::string& personID) {
281
return Dom::getDouble(libsumo::VAR_MINGAP_LAT, personID);
282
}
283
284
285
double
286
Person::getMaxSpeed(const std::string& personID) {
287
return Dom::getDouble(libsumo::VAR_MAXSPEED, personID);
288
}
289
290
291
double
292
Person::getMaxSpeedLat(const std::string& personID) {
293
return Dom::getDouble(libsumo::VAR_MAXSPEED_LAT, personID);
294
}
295
296
297
std::string
298
Person::getLateralAlignment(const std::string& personID) {
299
return Dom::getString(libsumo::VAR_LATALIGNMENT, personID);
300
}
301
302
303
double
304
Person::getWidth(const std::string& personID) {
305
return Dom::getDouble(libsumo::VAR_WIDTH, personID);
306
}
307
308
309
double
310
Person::getHeight(const std::string& personID) {
311
return Dom::getDouble(libsumo::VAR_HEIGHT, personID);
312
}
313
314
315
double
316
Person::getMass(const std::string& personID) {
317
return Dom::getDouble(libsumo::VAR_MASS, personID);
318
}
319
320
321
int
322
Person::getPersonCapacity(const std::string& personID) {
323
return Dom::getInt(libsumo::VAR_PERSON_CAPACITY, personID);
324
}
325
326
327
double
328
Person::getBoardingDuration(const std::string& personID) {
329
return Dom::getDouble(libsumo::VAR_BOARDING_DURATION, personID);
330
}
331
332
double
333
Person::getImpatience(const std::string& personID) {
334
return Dom::getDouble(libsumo::VAR_IMPATIENCE, personID);
335
}
336
337
338
339
LIBTRACI_PARAMETER_IMPLEMENTATION(Person, PERSON)
340
341
342
void
343
Person::setSpeed(const std::string& personID, double speed) {
344
Dom::setDouble(libsumo::VAR_SPEED, personID, speed);
345
}
346
347
348
void
349
Person::setType(const std::string& personID, const std::string& typeID) {
350
Dom::setString(libsumo::VAR_TYPE, personID, typeID);
351
}
352
353
354
void
355
Person::setImpatience(const std::string& personID, double impatience) {
356
Dom::setDouble(libsumo::VAR_IMPATIENCE, personID, impatience);
357
}
358
359
void
360
Person::setBoardingDuration(const std::string& personID, double boardingDuration) {
361
Dom::setDouble(libsumo::VAR_BOARDING_DURATION, personID, boardingDuration);
362
}
363
364
void
365
Person::add(const std::string& personID, const std::string& edgeID, double pos, double departInSecs, const std::string typeID) {
366
tcpip::Storage content;
367
content.writeUnsignedByte(libsumo::TYPE_COMPOUND);
368
content.writeInt(4);
369
content.writeUnsignedByte(libsumo::TYPE_STRING);
370
content.writeString(typeID);
371
content.writeUnsignedByte(libsumo::TYPE_STRING);
372
content.writeString(edgeID);
373
content.writeUnsignedByte(libsumo::TYPE_DOUBLE);
374
content.writeDouble(departInSecs);
375
content.writeUnsignedByte(libsumo::TYPE_DOUBLE);
376
content.writeDouble(pos);
377
Dom::set(libsumo::ADD, personID, &content);
378
}
379
380
381
void
382
Person::appendStage(const std::string& personID, const libsumo::TraCIStage& stage) {
383
tcpip::Storage content;
384
libsumo::StorageHelper::writeStage(content, stage);
385
Dom::set(libsumo::APPEND_STAGE, personID, &content);
386
}
387
388
389
void
390
Person::replaceStage(const std::string& personID, const int stageIndex, const libsumo::TraCIStage& stage) {
391
tcpip::Storage content;
392
content.writeUnsignedByte(libsumo::TYPE_COMPOUND);
393
content.writeInt(2);
394
content.writeUnsignedByte(libsumo::TYPE_INTEGER);
395
content.writeInt(stageIndex);
396
libsumo::StorageHelper::writeStage(content, stage);
397
Dom::set(libsumo::REPLACE_STAGE, personID, &content);
398
}
399
400
401
void
402
Person::appendDrivingStage(const std::string& personID, const std::string& toEdge, const std::string& lines, const std::string& stopID) {
403
tcpip::Storage content;
404
content.writeUnsignedByte(libsumo::TYPE_COMPOUND);
405
content.writeInt(4);
406
content.writeUnsignedByte(libsumo::TYPE_INTEGER);
407
content.writeInt(libsumo::STAGE_DRIVING);
408
content.writeUnsignedByte(libsumo::TYPE_STRING);
409
content.writeString(toEdge);
410
content.writeUnsignedByte(libsumo::TYPE_STRING);
411
content.writeString(lines);
412
content.writeUnsignedByte(libsumo::TYPE_STRING);
413
content.writeString(stopID);
414
Dom::set(libsumo::APPEND_STAGE, personID, &content);
415
}
416
417
418
void
419
Person::appendWaitingStage(const std::string& personID, double duration, const std::string& description, const std::string& stopID) {
420
tcpip::Storage content;
421
content.writeUnsignedByte(libsumo::TYPE_COMPOUND);
422
content.writeInt(4);
423
content.writeUnsignedByte(libsumo::TYPE_INTEGER);
424
content.writeInt(libsumo::STAGE_WAITING);
425
content.writeUnsignedByte(libsumo::TYPE_DOUBLE);
426
content.writeDouble(duration);
427
content.writeUnsignedByte(libsumo::TYPE_STRING);
428
content.writeString(description);
429
content.writeUnsignedByte(libsumo::TYPE_STRING);
430
content.writeString(stopID);
431
Dom::set(libsumo::APPEND_STAGE, personID, &content);
432
}
433
434
435
void
436
Person::appendWalkingStage(const std::string& personID, const std::vector<std::string>& edges, double arrivalPos, double duration, double speed, const std::string& stopID) {
437
tcpip::Storage content;
438
content.writeUnsignedByte(libsumo::TYPE_COMPOUND);
439
content.writeInt(6);
440
content.writeUnsignedByte(libsumo::TYPE_INTEGER);
441
content.writeInt(libsumo::STAGE_WALKING);
442
content.writeUnsignedByte(libsumo::TYPE_STRINGLIST);
443
content.writeStringList(edges);
444
content.writeUnsignedByte(libsumo::TYPE_DOUBLE);
445
content.writeDouble(arrivalPos);
446
content.writeUnsignedByte(libsumo::TYPE_DOUBLE);
447
content.writeDouble(duration);
448
content.writeUnsignedByte(libsumo::TYPE_DOUBLE);
449
content.writeDouble(speed);
450
content.writeUnsignedByte(libsumo::TYPE_STRING);
451
content.writeString(stopID);
452
Dom::set(libsumo::APPEND_STAGE, personID, &content);
453
}
454
455
456
void
457
Person::removeStage(const std::string& personID, int nextStageIndex) {
458
Dom::setInt(libsumo::REMOVE_STAGE, personID, nextStageIndex);
459
}
460
461
462
void
463
Person::rerouteTraveltime(const std::string& personID) {
464
tcpip::Storage content;
465
content.writeUnsignedByte(libsumo::TYPE_COMPOUND);
466
content.writeInt(0);
467
Dom::set(libsumo::CMD_REROUTE_TRAVELTIME, personID, &content);
468
}
469
470
471
void
472
Person::moveTo(const std::string& personID, const std::string& laneID, double pos, double posLat) {
473
tcpip::Storage content;
474
content.writeUnsignedByte(libsumo::TYPE_COMPOUND);
475
content.writeInt(3);
476
content.writeUnsignedByte(libsumo::TYPE_STRING);
477
content.writeString(laneID);
478
content.writeUnsignedByte(libsumo::TYPE_DOUBLE);
479
content.writeDouble(pos);
480
content.writeUnsignedByte(libsumo::TYPE_DOUBLE);
481
content.writeDouble(posLat);
482
Dom::set(libsumo::VAR_MOVE_TO, personID, &content);
483
}
484
485
486
void
487
Person::moveToXY(const std::string& personID, const std::string& edgeID, const double x, const double y, double angle, const int keepRoute, double matchThreshold) {
488
tcpip::Storage content;
489
content.writeUnsignedByte(libsumo::TYPE_COMPOUND);
490
content.writeInt(6);
491
content.writeUnsignedByte(libsumo::TYPE_STRING);
492
content.writeString(edgeID);
493
content.writeUnsignedByte(libsumo::TYPE_DOUBLE);
494
content.writeDouble(x);
495
content.writeUnsignedByte(libsumo::TYPE_DOUBLE);
496
content.writeDouble(y);
497
content.writeUnsignedByte(libsumo::TYPE_DOUBLE);
498
content.writeDouble(angle);
499
content.writeUnsignedByte(libsumo::TYPE_BYTE);
500
content.writeByte(keepRoute);
501
StoHelp::writeTypedDouble(content, matchThreshold);
502
Dom::set(libsumo::MOVE_TO_XY, personID, &content);
503
}
504
505
506
/** untested setter functions which alter the person's vtype ***/
507
508
void
509
Person::setLength(const std::string& personID, double length) {
510
Dom::setDouble(libsumo::VAR_LENGTH, personID, length);
511
}
512
513
514
void
515
Person::setMaxSpeed(const std::string& personID, double speed) {
516
Dom::setDouble(libsumo::VAR_MAXSPEED, personID, speed);
517
}
518
519
520
void
521
Person::setVehicleClass(const std::string& personID, const std::string& clazz) {
522
Dom::setString(libsumo::VAR_VEHICLECLASS, personID, clazz);
523
}
524
525
526
void
527
Person::setShapeClass(const std::string& personID, const std::string& clazz) {
528
Dom::setString(libsumo::VAR_SHAPECLASS, personID, clazz);
529
}
530
531
532
void
533
Person::setEmissionClass(const std::string& personID, const std::string& clazz) {
534
Dom::setString(libsumo::VAR_EMISSIONCLASS, personID, clazz);
535
}
536
537
538
void
539
Person::setWidth(const std::string& personID, double width) {
540
Dom::setDouble(libsumo::VAR_WIDTH, personID, width);
541
}
542
543
544
void
545
Person::setHeight(const std::string& personID, double height) {
546
Dom::setDouble(libsumo::VAR_HEIGHT, personID, height);
547
}
548
549
550
void
551
Person::setMass(const std::string& personID, double mass) {
552
Dom::setDouble(libsumo::VAR_HEIGHT, personID, mass);
553
}
554
555
556
void
557
Person::setMinGap(const std::string& personID, double minGap) {
558
Dom::setDouble(libsumo::VAR_MINGAP, personID, minGap);
559
}
560
561
562
void
563
Person::setAccel(const std::string& personID, double accel) {
564
Dom::setDouble(libsumo::VAR_ACCEL, personID, accel);
565
}
566
567
568
void
569
Person::setDecel(const std::string& personID, double decel) {
570
Dom::setDouble(libsumo::VAR_DECEL, personID, decel);
571
}
572
573
574
void
575
Person::setEmergencyDecel(const std::string& personID, double decel) {
576
Dom::setDouble(libsumo::VAR_EMERGENCY_DECEL, personID, decel);
577
}
578
579
580
void
581
Person::setApparentDecel(const std::string& personID, double decel) {
582
Dom::setDouble(libsumo::VAR_APPARENT_DECEL, personID, decel);
583
}
584
585
586
void
587
Person::setImperfection(const std::string& personID, double imperfection) {
588
Dom::setDouble(libsumo::VAR_IMPERFECTION, personID, imperfection);
589
}
590
591
592
void
593
Person::setTau(const std::string& personID, double tau) {
594
Dom::setDouble(libsumo::VAR_TAU, personID, tau);
595
}
596
597
598
void
599
Person::setMinGapLat(const std::string& personID, double minGapLat) {
600
Dom::setDouble(libsumo::VAR_MINGAP_LAT, personID, minGapLat);
601
}
602
603
604
void
605
Person::setMaxSpeedLat(const std::string& personID, double speed) {
606
Dom::setDouble(libsumo::VAR_MAXSPEED_LAT, personID, speed);
607
}
608
609
610
void
611
Person::setLateralAlignment(const std::string& personID, const std::string& latAlignment) {
612
Dom::setString(libsumo::VAR_LATALIGNMENT, personID, latAlignment);
613
}
614
615
616
void
617
Person::setSpeedFactor(const std::string& personID, double factor) {
618
Dom::setDouble(libsumo::VAR_SPEED_FACTOR, personID, factor);
619
}
620
621
622
void
623
Person::setActionStepLength(const std::string& personID, double actionStepLength, bool resetActionOffset) {
624
if (!resetActionOffset) {
625
actionStepLength *= -1;
626
}
627
Dom::setDouble(libsumo::VAR_ACTIONSTEPLENGTH, personID, actionStepLength);
628
}
629
630
void
631
Person::remove(const std::string& personID, char reason) {
632
tcpip::Storage content;
633
content.writeUnsignedByte(libsumo::TYPE_BYTE);
634
content.writeUnsignedByte(reason);
635
Dom::set(libsumo::REMOVE, personID, &content);
636
}
637
638
639
void
640
Person::setColor(const std::string& personID, const libsumo::TraCIColor& color) {
641
Dom::setCol(libsumo::VAR_COLOR, personID, color);
642
}
643
644
645
LIBTRACI_SUBSCRIPTION_IMPLEMENTATION(Person, PERSON)
646
647
648
}
649
650
651
/****************************************************************************/
652
653