Skip to content

Commit

Permalink
refactoring refs #12
Browse files Browse the repository at this point in the history
  • Loading branch information
namdre committed Mar 14, 2024
1 parent 6724c56 commit 729288f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 76 deletions.
126 changes: 50 additions & 76 deletions src/microsim/MSBaseVehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1696,44 +1696,9 @@ MSBaseVehicle::replaceStop(int nextStopIndex, SUMOVehicleParameter::Stop stop, c
// let the vehicle jump rather than teleport
// we add a jump-stop at the end of the edge (unless the vehicle is
// already configure to jump before the replaced stop)
bool needJump = true;
if (nextStopIndex > 0) {
auto itPriorStop = myStops.begin();
std::advance(itPriorStop, nextStopIndex - 1);
const MSStop& priorStop = *itPriorStop;
if (priorStop.pars.jump >= 0) {
needJump = false;
}
}
if (needJump) {
SUMOVehicleParameter::Stop jumpStopPars;
jumpStopPars.endPos = (*itStart)->getLength();
jumpStopPars.speed = 1000;
jumpStopPars.jump = 0;
jumpStopPars.edge = (*itStart)->getID();
jumpStopPars.parametersSet = STOP_SPEED_SET | STOP_JUMP_SET;
MSLane* jumpStopLane = nullptr;
for (MSLane* cand : (*itStart)->getLanes()) {
if (cand->allowsVehicleClass(getVClass())) {
jumpStopLane = cand;
break;
}
}
if (jumpStopLane == nullptr) {
errorMsg = "Unable to replace stop with teleporting\n";
return false;
}
MSStop jumpStop(jumpStopPars);
jumpStop.initPars(jumpStopPars);
jumpStop.lane = jumpStopLane;
jumpStop.edge = myRoute->end(); // will be patched in replaceRoute
myStops.insert(itStop, jumpStop);
if (!hasDeparted() && (int)myParameter->stops.size() > nextStopIndex) {
// stops will be rebuilt from scratch so we must patch the stops in myParameter
auto it = myParameter->stops.begin() + nextStopIndex;
const_cast<SUMOVehicleParameter*>(myParameter)->stops.insert(it, jumpStopPars);
}
}
if (!insertJump(nextStopIndex, itStart, errorMsg)) {
return false;
};
}
return replaceRouteEdges(newEdges, routeCost, savings, info, !hasDeparted(), false, false, &errorMsg);
}
Expand All @@ -1752,8 +1717,6 @@ MSBaseVehicle::rerouteBetweenStops(int nextStopIndex, const std::string& info, b
}
const SUMOTime t = MSNet::getInstance()->getCurrentTimeStep();

auto itStop = myStops.begin();
std::advance(itStop, nextStopIndex);
const ConstMSEdgeVector& oldEdges = getRoute().getEdges();
std::vector<MSStop> stops(myStops.begin(), myStops.end());
const int junctionOffset = getLane() != nullptr && getLane()->isInternal() ? 1 : 0;
Expand Down Expand Up @@ -1796,46 +1759,57 @@ MSBaseVehicle::rerouteBetweenStops(int nextStopIndex, const std::string& info, b
// let the vehicle jump rather than teleport
// we add a jump-stop at the end of the edge (unless the vehicle is
// already configure to jump before the replaced stop)
bool needJump = true;
if (nextStopIndex > 0) {
auto itPriorStop = myStops.begin();
std::advance(itPriorStop, nextStopIndex - 1);
const MSStop& priorStop = *itPriorStop;
if (priorStop.pars.jump >= 0) {
needJump = false;
if (!insertJump(nextStopIndex, itStart, errorMsg)) {
return false;
};
}
return replaceRouteEdges(newEdges, routeCost, savings, info, !hasDeparted(), false, false, &errorMsg);
}


bool
MSBaseVehicle::insertJump(int nextStopIndex, MSRouteIterator itStart, std::string& errorMsg) {
bool needJump = true;
if (nextStopIndex > 0) {
auto itPriorStop = myStops.begin();
std::advance(itPriorStop, nextStopIndex - 1);
const MSStop& priorStop = *itPriorStop;
if (priorStop.pars.jump >= 0) {
needJump = false;
}
}
if (needJump) {
SUMOVehicleParameter::Stop jumpStopPars;
jumpStopPars.endPos = (*itStart)->getLength();
jumpStopPars.speed = 1000;
jumpStopPars.jump = 0;
jumpStopPars.edge = (*itStart)->getID();
jumpStopPars.parametersSet = STOP_SPEED_SET | STOP_JUMP_SET;
MSLane* jumpStopLane = nullptr;
for (MSLane* cand : (*itStart)->getLanes()) {
if (cand->allowsVehicleClass(getVClass())) {
jumpStopLane = cand;
break;
}
}
if (needJump) {
SUMOVehicleParameter::Stop jumpStopPars;
jumpStopPars.endPos = (*itStart)->getLength();
jumpStopPars.speed = 1000;
jumpStopPars.jump = 0;
jumpStopPars.edge = (*itStart)->getID();
jumpStopPars.parametersSet = STOP_SPEED_SET | STOP_JUMP_SET;
MSLane* jumpStopLane = nullptr;
for (MSLane* cand : (*itStart)->getLanes()) {
if (cand->allowsVehicleClass(getVClass())) {
jumpStopLane = cand;
break;
}
}
if (jumpStopLane == nullptr) {
errorMsg = "Unable to replace stop with teleporting\n";
return false;
}
MSStop jumpStop(jumpStopPars);
jumpStop.initPars(jumpStopPars);
jumpStop.lane = jumpStopLane;
jumpStop.edge = myRoute->end(); // will be patched in replaceRoute
myStops.insert(itStop, jumpStop);
if (!hasDeparted() && (int)myParameter->stops.size() > nextStopIndex) {
// stops will be rebuilt from scratch so we must patch the stops in myParameter
auto it = myParameter->stops.begin() + nextStopIndex;
const_cast<SUMOVehicleParameter*>(myParameter)->stops.insert(it, jumpStopPars);
}
if (jumpStopLane == nullptr) {
errorMsg = "Unable to replace stop with teleporting\n";
return false;
}
auto itStop = myStops.begin();
std::advance(itStop, nextStopIndex);
MSStop jumpStop(jumpStopPars);
jumpStop.initPars(jumpStopPars);
jumpStop.lane = jumpStopLane;
jumpStop.edge = myRoute->end(); // will be patched in replaceRoute
myStops.insert(itStop, jumpStop);
if (!hasDeparted() && (int)myParameter->stops.size() > nextStopIndex) {
// stops will be rebuilt from scratch so we must patch the stops in myParameter
auto it = myParameter->stops.begin() + nextStopIndex;
const_cast<SUMOVehicleParameter*>(myParameter)->stops.insert(it, jumpStopPars);
}
}
return replaceRouteEdges(newEdges, routeCost, savings, info, !hasDeparted(), false, false, &errorMsg);
return true;
}


Expand Down
3 changes: 3 additions & 0 deletions src/microsim/MSBaseVehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,9 @@ class MSBaseVehicle : public SUMOVehicle {
/// @brief remove route at the end of the simulation
void checkRouteRemoval();

/// @brief helper function
bool insertJump(int nextStopIndex, MSRouteIterator itStart, std::string& errorMsg);

private:
/// invalidated assignment operator
MSBaseVehicle& operator=(const MSBaseVehicle& s) = delete;
Expand Down

0 comments on commit 729288f

Please sign in to comment.