Skip to content

Commit

Permalink
Retrieve segment id and segment index from vehicle in both libsumo an…
Browse files Browse the repository at this point in the history
…d traci. Refs #14681

Signed-off-by: Zi-Hao WANG <zhwang0721@gmail.com>
  • Loading branch information
zihaooo authored and namdre committed Aug 30, 2024
1 parent e14040a commit 384dc38
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/libsumo/TraCIConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,12 @@ TRACI_CONST int VAR_LANE_ID = 0x51;
// lane index (get: vehicle, edge)
TRACI_CONST int VAR_LANE_INDEX = 0x52;

// segment id (get: vehicle)
TRACI_CONST int VAR_SEGMENT_ID = 0xa1;

// segment index (get: vehicle)
TRACI_CONST int VAR_SEGMENT_INDEX = 0xa2;

// route id (get & set: vehicles)
TRACI_CONST int VAR_ROUTE_ID = 0x53;

Expand Down
17 changes: 16 additions & 1 deletion src/libsumo/Vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
#include "Vehicle.h"

#define CALL_MICRO_FUN(veh, fun, mesoResult) ((dynamic_cast<MSVehicle*>(veh) == nullptr ? (mesoResult) : dynamic_cast<MSVehicle*>(veh)->fun))

#define CALL_MESO_FUN(veh, fun, microResult) ((dynamic_cast<MEVehicle*>(veh) == nullptr ? (microResult) : dynamic_cast<MEVehicle*>(veh)->fun))

// ===========================================================================
// debug defines
Expand Down Expand Up @@ -185,6 +185,17 @@ Vehicle::getLaneIndex(const std::string& vehID) {
return veh->isOnRoad() ? CALL_MICRO_FUN(veh, getLane()->getIndex(), INVALID_INT_VALUE) : INVALID_INT_VALUE;
}

std::string
Vehicle::getSegmentID(const std::string& vehID) {
MSBaseVehicle* veh = Helper::getVehicle(vehID);
return veh->isOnRoad() ? CALL_MESO_FUN(veh, getSegment()->getID(), "") : "";
}

int
Vehicle::getSegmentIndex(const std::string& vehID) {
MSBaseVehicle* veh = Helper::getVehicle(vehID);
return veh->isOnRoad()? CALL_MESO_FUN(veh, getSegment()->getIndex(), INVALID_INT_VALUE): INVALID_INT_VALUE;
}

std::string
Vehicle::getTypeID(const std::string& vehID) {
Expand Down Expand Up @@ -2808,6 +2819,10 @@ Vehicle::handleVariable(const std::string& objID, const int variable, VariableWr
return wrapper->wrapString(objID, variable, getLaneID(objID));
case VAR_LANE_INDEX:
return wrapper->wrapInt(objID, variable, getLaneIndex(objID));
case VAR_SEGMENT_ID:
return wrapper->wrapString(objID, variable, getSegmentID(objID));
case VAR_SEGMENT_INDEX:
return wrapper->wrapInt(objID, variable, getSegmentIndex(objID));
case VAR_TYPE:
return wrapper->wrapString(objID, variable, getTypeID(objID));
case VAR_ROUTE_ID:
Expand Down
2 changes: 2 additions & 0 deletions src/libsumo/Vehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class Vehicle {
static std::string getRoadID(const std::string& vehID);
static std::string getLaneID(const std::string& vehID);
static int getLaneIndex(const std::string& vehID);
static std::string getSegmentID(const std::string& vehID);
static int getSegmentIndex(const std::string& vehID);
static std::string getTypeID(const std::string& vehID);
static std::string getRouteID(const std::string& vehID);
static double getDeparture(const std::string& vehID);
Expand Down
12 changes: 12 additions & 0 deletions src/libtraci/Vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ Vehicle::getLaneIndex(const std::string& vehID) {
}


std::string
Vehicle::getSegmentID(const std::string& vehID) {
return Dom::getString(libsumo::VAR_SEGMENT_ID, vehID);
}


int
Vehicle::getSegmentIndex(const std::string& vehID) {
return Dom::getInt(libsumo::VAR_SEGMENT_INDEX, vehID);
}


std::string
Vehicle::getTypeID(const std::string& vehID) {
return Dom::getString(libsumo::VAR_TYPE, vehID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/****************************************************************************/
/// @file Constants.java
/// @author generated by "rebuildConstants.py"
/// @date 2024-07-24 13:13:31.122594
/// @date 2024-08-12 23:32:52.197804
///
// This script contains TraCI constant definitions from <SUMO_HOME>/src/libsumo/TraCIConstants.h.
/****************************************************************************/
Expand Down Expand Up @@ -1004,6 +1004,12 @@ public class Constants {
// lane index (get: vehicle, edge)
public static final int VAR_LANE_INDEX = 0x52;

// segment id (get: vehicle)
public static final int VAR_SEGMENT_ID = 0xa1;

// segment index (get: vehicle)
public static final int VAR_SEGMENT_INDEX = 0xa2;

// route id (get & set: vehicles)
public static final int VAR_ROUTE_ID = 0x53;

Expand Down
15 changes: 15 additions & 0 deletions tools/traci/_vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,21 @@ def getLaneIndex(self, vehID):
"""
return self._getUniversal(tc.VAR_LANE_INDEX, vehID)

def getSegmentID(self, vehID):
"""getSegmentID(string) -> string
Returns the id of the segment the named vehicle was at within the last step.
"""
return self._getUniversal(tc.VAR_SEGMENT_ID, vehID)

def getSegmentIndex(self, vehID):
"""getSegmentIndex(string) -> integer
Returns the index of the segment the named vehicle was at within the last step.
"""
return self._getUniversal(tc.VAR_SEGMENT_INDEX, vehID)


def getTypeID(self, vehID):
"""getTypeID(string) -> string
Expand Down
8 changes: 7 additions & 1 deletion tools/traci/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# @file constants.py
# @author generated by "rebuildConstants.py"
# @date 2024-07-24 13:13:31.122594
# @date 2024-08-12 23:32:52.197804

"""
This script contains TraCI constant definitions from <SUMO_HOME>/src/libsumo/TraCIConstants.h.
Expand Down Expand Up @@ -1004,6 +1004,12 @@
# lane index (get: vehicle, edge)
VAR_LANE_INDEX = 0x52

# segment id (get: vehicle)
VAR_SEGMENT_ID = 0xa1

# segment index (get: vehicle)
VAR_SEGMENT_INDEX = 0xa2

# route id (get & set: vehicles)
VAR_ROUTE_ID = 0x53

Expand Down

0 comments on commit 384dc38

Please sign in to comment.