Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix in autoMapStreetLanes function #291

Merged
merged 8 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dsm/dsm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

static constexpr uint8_t DSM_VERSION_MAJOR = 2;
static constexpr uint8_t DSM_VERSION_MINOR = 6;
static constexpr uint8_t DSM_VERSION_PATCH = 11;
static constexpr uint8_t DSM_VERSION_PATCH = 12;

static auto const DSM_VERSION =
std::format("{}.{}.{}", DSM_VERSION_MAJOR, DSM_VERSION_MINOR, DSM_VERSION_PATCH);
Expand Down
13 changes: 13 additions & 0 deletions src/dsm/headers/Road.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <memory>
#include <optional>
#include <set>
#include <string>

namespace dsm {
Expand All @@ -15,6 +16,7 @@ namespace dsm {
int m_nLanes;
std::string m_name;
int m_priority;
std::set<Id> m_forbiddenTurns; // Stores the forbidden turns (road ids)

public:
/// @brief Construct a new Road object
Expand Down Expand Up @@ -50,6 +52,12 @@ namespace dsm {
/// @brief Set the road's priority
/// @param priority The road's priority
void setPriority(int priority);
/// @brief Add a road id to the forbidden turns
/// @param roadId The road id to add
void addForbiddenTurn(Id roadId);
/// @brief Replace the road's forbidden turns with the given set
/// @param forbiddenTurns The set of forbidden turns
void setForbiddenTurns(std::set<Id> const& forbiddenTurns);

/// @brief Get the length, in meters
/// @return double The length, in meters
Expand All @@ -66,6 +74,11 @@ namespace dsm {
/// @brief Get the priority
/// @return int The priority
int priority() const;
/// @brief Get the road's forbidden turns
/// @return std::set<Id> The road's forbidden turns
/// @details The forbidden turns are the road ids that are not allowed to be used by the agents
/// when they are on the road.
std::set<Id> const& forbiddenTurns() const;

virtual int nAgents() const = 0;
virtual int nMovingAgents() const = 0;
Expand Down
18 changes: 15 additions & 3 deletions src/dsm/headers/RoadDynamics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@
pair.second->deltaAngle(this->graph().edge(previousStreetId)->angle())};
if (std::abs(delta) < std::numbers::pi) {
if (delta < 0.) {
m_turnMapping[pair.first][dsm::Direction::RIGHT] = previousStreetId;
; // right
m_turnMapping[pair.first][dsm::Direction::RIGHT] =
previousStreetId; // right
} else if (delta > 0.) {
m_turnMapping[pair.first][dsm::Direction::LEFT] =
previousStreetId; // left
Expand Down Expand Up @@ -502,6 +502,18 @@
}
}
assert(!possibleMoves.empty());
if (streetId.has_value()) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 14.4 rule Note

MISRA 14.4 rule
auto const& pStreet{this->graph().edge(*streetId)};
for (auto const& foirbiddenStreetId : pStreet->forbiddenTurns()) {
auto const& pForbiddenStreet{this->graph().edge(foirbiddenStreetId)};

Check warning on line 508 in src/dsm/headers/RoadDynamics.hpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/headers/RoadDynamics.hpp#L508

Added line #L508 was not covered by tests
// if possible moves contains the forbidden street, remove it
auto it = std::find(
possibleMoves.begin(), possibleMoves.end(), pForbiddenStreet->target());

Check warning on line 511 in src/dsm/headers/RoadDynamics.hpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/headers/RoadDynamics.hpp#L510-L511

Added lines #L510 - L511 were not covered by tests
if (it != possibleMoves.end()) {
possibleMoves.erase(it);

Check warning on line 513 in src/dsm/headers/RoadDynamics.hpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/headers/RoadDynamics.hpp#L513

Added line #L513 was not covered by tests
}
}
}
std::uniform_int_distribution<Size> moveDist{
0, static_cast<Size>(possibleMoves.size() - 1)};
// while loop to avoid U turns in non-roundabout junctions
Expand Down Expand Up @@ -1049,7 +1061,7 @@
auto const deltaAngle{pNextStreet->deltaAngle(pStreet->angle())};
if (std::abs(deltaAngle) < std::numbers::pi) {
// Lanes are counted as 0 is the far right lane
if (std::abs(deltaAngle) < std::numbers::pi / 4) {
if (std::abs(deltaAngle) < std::numbers::pi / 8) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.1 rule Note

MISRA 12.1 rule
std::vector<double> weights;
for (auto const& queue : pStreet->exitQueues()) {
weights.push_back(1. / (queue.size() + 1));
Expand Down
1 change: 0 additions & 1 deletion src/dsm/headers/Street.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ namespace dsm {
/// @tparam Id The type of the street's id
/// @tparam Size The type of the street's capacity
class SpireStreet : public Street, public Counter {
private:
public:
using Street::Street;
SpireStreet(Street&& street) : Street(std::move(street)) {}
Expand Down
6 changes: 6 additions & 0 deletions src/dsm/sources/Road.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
}
double Road::meanVehicleLength() { return m_meanVehicleLength; }

void Road::addForbiddenTurn(Id roadId) { m_forbiddenTurns.insert(roadId); }
void Road::setForbiddenTurns(std::set<Id> const& forbiddenTurns) {
m_forbiddenTurns = forbiddenTurns;
}

Check warning on line 55 in src/dsm/sources/Road.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/Road.cpp#L52-L55

Added lines #L52 - L55 were not covered by tests

void Road::setMaxSpeed(double speed) {
if (speed < 0.) {
Logger::error(
Expand All @@ -66,4 +71,5 @@
int Road::nLanes() const { return m_nLanes; }
std::string Road::name() const { return m_name; }
int Road::priority() const { return m_priority; }
std::set<Id> const& Road::forbiddenTurns() const { return m_forbiddenTurns; }
}; // namespace dsm
135 changes: 97 additions & 38 deletions src/dsm/sources/RoadNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@
}
newStreetIds.emplace(streetId, newStreetId);
}
std::for_each(
m_edges.cbegin(), m_edges.cend(), [this, &newStreetIds](auto const& pair) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
auto const& pStreet{pair.second};
auto const& forbiddenTurns{pStreet->forbiddenTurns()};
if (forbiddenTurns.empty()) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 14.4 rule Note

MISRA 14.4 rule
return;
}
std::set<Id> newForbiddenTurns;

Check warning on line 76 in src/dsm/sources/RoadNetwork.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/RoadNetwork.cpp#L76

Added line #L76 was not covered by tests
for (auto const& streetId : forbiddenTurns) {
newForbiddenTurns.insert(newStreetIds.at(streetId));

Check warning on line 78 in src/dsm/sources/RoadNetwork.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/RoadNetwork.cpp#L78

Added line #L78 was not covered by tests
}
pStreet->setForbiddenTurns(newForbiddenTurns);
});

Check warning on line 81 in src/dsm/sources/RoadNetwork.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/RoadNetwork.cpp#L80-L81

Added lines #L80 - L81 were not covered by tests
for (const auto& [nodeId, node] : m_nodes) {
// This is probably not the best way to do this
if (node->isIntersection()) {
Expand Down Expand Up @@ -264,10 +277,9 @@
[this, &pair, &outNeighbours, &maxPriority](auto const& inNodeId) {
auto const& pInStreet{m_edges.at(inNodeId * m_nodes.size() + pair.first)};
auto const nLanes{pInStreet->nLanes()};
if (nLanes == 1) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 15.5 rule Note

MISRA 15.5 rule
return;
}
// auto const& laneMapping{pInStreet->laneMapping()};
std::multiset<Direction> allowedTurns;
std::for_each(
outNeighbours.cbegin(),
Expand All @@ -276,14 +288,17 @@
auto const& outNodeId) {
auto const& pOutStreet{
m_edges.at(pair.first * m_nodes.size() + outNodeId)};
if (pOutStreet->target() == pInStreet->source()) {
if (pOutStreet->target() == pInStreet->source() ||
pInStreet->forbiddenTurns().contains(pOutStreet->id())) {
return;
}
auto const deltaAngle{pOutStreet->deltaAngle(pInStreet->angle())};

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
auto const& outOppositeStreet{this->street(pair.first, outNodeId)};
if (!outOppositeStreet) {
return;

Check warning on line 298 in src/dsm/sources/RoadNetwork.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/RoadNetwork.cpp#L298

Added line #L298 was not covered by tests
}
// Actually going straight means remain on the same road, thus...
if (outOppositeStreet &&
((pInStreet->priority() == maxPriority) ==
if (((pInStreet->priority() == maxPriority) ==
(outOppositeStreet->get()->priority() == maxPriority)) &&
!allowedTurns.contains(Direction::STRAIGHT)) {
Logger::debug(
Expand All @@ -298,43 +313,29 @@
// allowedTurns.emplace(Direction::STRAIGHT);
// return;
// }
} else if (std::abs(deltaAngle) < 5 * std::numbers::pi / 6) {
} else if (std::abs(deltaAngle) < std::numbers::pi) {
// Logger::debug(std::format("Angle in {} - angle out {}",
// pInStreet->angle(),
// pOutStreet->angle()));
// Logger::debug(std::format("Delta: {}", deltaAngle));

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
if (deltaAngle < -std::numbers::pi / 6.) {
Logger::debug(
std::format("Street {} can turn RIGHT", pInStreet->id()));
if (std::abs(deltaAngle) < std::numbers::pi / 8) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.1 rule Note

MISRA 12.1 rule
Logger::debug(std::format("Street {} -> {} can turn STRAIGHT",

Check warning on line 322 in src/dsm/sources/RoadNetwork.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/RoadNetwork.cpp#L322

Added line #L322 was not covered by tests
pInStreet->source(),
pInStreet->target()));
allowedTurns.emplace(Direction::STRAIGHT);

Check warning on line 325 in src/dsm/sources/RoadNetwork.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/RoadNetwork.cpp#L325

Added line #L325 was not covered by tests
} else if (deltaAngle < 0.) {
Logger::debug(std::format("Street {} -> {} can turn RIGHT",

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
pInStreet->source(),
pInStreet->target()));
allowedTurns.emplace(Direction::RIGHT);
} else if (deltaAngle > std::numbers::pi / 6.) {
Logger::debug(
std::format("Street {} can turn LEFT", pInStreet->id()));
} else if (deltaAngle > 0.) {
Logger::debug(std::format("Street {} -> {} can turn LEFT",

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
pInStreet->source(),
pInStreet->target()));
allowedTurns.emplace(Direction::LEFT);
} else {
Logger::debug(
std::format("Street {} can go STRAIGHT", pInStreet->id()));
if (!allowedTurns.contains(Direction::STRAIGHT)) {
allowedTurns.emplace(Direction::STRAIGHT);
} else if (deltaAngle > 0.) {
allowedTurns.emplace(Direction::LEFT);
} else {
allowedTurns.emplace(Direction::RIGHT);
}
}
}
});
// if (!allowedTurns.contains(Direction::RIGHT)) {
// std::multiset<Direction> updatedTurns;
// for (auto turn : allowedTurns) {
// if (turn > Direction::RIGHT && turn <= Direction::UTURN) {
// updatedTurns.insert(static_cast<Direction>(turn - 1));
// } else {
// updatedTurns.insert(turn);
// }
// }
// allowedTurns = std::move(updatedTurns); // Replace with the updated set
// }
while (allowedTurns.size() < static_cast<size_t>(nLanes)) {
if (allowedTurns.contains(Direction::STRAIGHT)) {
allowedTurns.emplace(Direction::STRAIGHT);
Expand All @@ -347,11 +348,36 @@
}
}
// If allowedTurns contains all RIGHT, STRAIGHT and LEFT, transform RIGHT into RIGHTANDSTRAIGHT
if (allowedTurns.contains(Direction::STRAIGHT) &&
allowedTurns.contains(Direction::RIGHT) &&
allowedTurns.contains(Direction::LEFT)) {
allowedTurns.erase(Direction::RIGHT);
allowedTurns.emplace(Direction::RIGHTANDSTRAIGHT);
if (allowedTurns.size() > static_cast<size_t>(nLanes)) {
if (pair.second->isTrafficLight()) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 14.4 rule Note

MISRA 14.4 rule
auto& tl = dynamic_cast<TrafficLight&>(*pair.second);
auto const& cycles{tl.cycles()};

Check warning on line 354 in src/dsm/sources/RoadNetwork.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/RoadNetwork.cpp#L353-L354

Added lines #L353 - L354 were not covered by tests
if (cycles.contains(pInStreet->id())) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 14.4 rule Note

MISRA 14.4 rule
if (cycles.size() == static_cast<size_t>(nLanes)) {
// Replace with the traffic light cycles
Logger::debug(
std::format("Using traffic light {} cycles for street {} -> {}",
tl.id(),
pInStreet->source(),
pInStreet->target()));
auto const& cycle{cycles.at(pInStreet->id())};
allowedTurns.clear();

Check warning on line 364 in src/dsm/sources/RoadNetwork.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/RoadNetwork.cpp#L358-L364

Added lines #L358 - L364 were not covered by tests
for (auto const& [direction, cycle] : cycle) {
allowedTurns.emplace(direction);

Check warning on line 366 in src/dsm/sources/RoadNetwork.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/RoadNetwork.cpp#L366

Added line #L366 was not covered by tests
}
} else if (cycles.at(pInStreet->id())

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 14.4 rule Note

MISRA 14.4 rule
.contains(Direction::LEFTANDSTRAIGHT)) {
allowedTurns.erase(Direction::LEFT);
allowedTurns.erase(Direction::STRAIGHT);
allowedTurns.emplace(Direction::LEFTANDSTRAIGHT);

Check warning on line 372 in src/dsm/sources/RoadNetwork.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/RoadNetwork.cpp#L369-L372

Added lines #L369 - L372 were not covered by tests

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
} else if (cycles.at(pInStreet->id())

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 14.4 rule Note

MISRA 14.4 rule
.contains(Direction::RIGHTANDSTRAIGHT)) {
allowedTurns.erase(Direction::RIGHT);
allowedTurns.erase(Direction::STRAIGHT);
allowedTurns.emplace(Direction::RIGHTANDSTRAIGHT);

Check warning on line 377 in src/dsm/sources/RoadNetwork.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/RoadNetwork.cpp#L374-L377

Added lines #L374 - L377 were not covered by tests
}

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 15.7 rule Note

MISRA 15.7 rule
}
}
}
switch (nLanes) {
case 1:
Expand All @@ -361,11 +387,44 @@
if (allowedTurns.contains(Direction::STRAIGHT) &&
allowedTurns.contains(Direction::RIGHT) &&
allowedTurns.contains(Direction::LEFT)) {
break;
if (pair.second->isTrafficLight()) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 14.4 rule Note

MISRA 14.4 rule
auto& tl = dynamic_cast<TrafficLight&>(*pair.second);
auto const& cycles{tl.cycles()};

Check warning on line 392 in src/dsm/sources/RoadNetwork.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/RoadNetwork.cpp#L391-L392

Added lines #L391 - L392 were not covered by tests

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
if (cycles.contains(pInStreet->id())) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 14.4 rule Note

MISRA 14.4 rule
auto const& cycle{cycles.at(pInStreet->id())};

Check warning on line 394 in src/dsm/sources/RoadNetwork.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/RoadNetwork.cpp#L394

Added line #L394 was not covered by tests
if (cycle.contains(Direction::LEFTANDSTRAIGHT) &&
cycle.contains(Direction::RIGHT)) {
allowedTurns.erase(Direction::LEFT);
allowedTurns.erase(Direction::STRAIGHT);
allowedTurns.emplace(Direction::LEFTANDSTRAIGHT);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
break;

Check warning on line 400 in src/dsm/sources/RoadNetwork.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/RoadNetwork.cpp#L396-L400

Added lines #L396 - L400 were not covered by tests
}
}
}
allowedTurns.clear();
allowedTurns.emplace(Direction::RIGHTANDSTRAIGHT);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
allowedTurns.emplace(Direction::LEFT);

Check warning on line 406 in src/dsm/sources/RoadNetwork.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/RoadNetwork.cpp#L404-L406

Added lines #L404 - L406 were not covered by tests
}
if (allowedTurns.size() > 2) {
// Remove duplicates
std::set<Direction> uniqueDirections;
std::copy(allowedTurns.begin(),

Check warning on line 411 in src/dsm/sources/RoadNetwork.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/RoadNetwork.cpp#L410-L411

Added lines #L410 - L411 were not covered by tests
allowedTurns.end(),
std::inserter(uniqueDirections, uniqueDirections.begin()));
allowedTurns.clear();
std::copy(uniqueDirections.begin(),

Check warning on line 415 in src/dsm/sources/RoadNetwork.cpp

View check run for this annotation

Codecov / codecov/patch

src/dsm/sources/RoadNetwork.cpp#L414-L415

Added lines #L414 - L415 were not covered by tests
uniqueDirections.end(),
std::inserter(allowedTurns, allowedTurns.begin()));
}
[[fallthrough]];
default:
assert(allowedTurns.size() == static_cast<size_t>(nLanes));
// Logger::info(
// std::format("Street {}->{} with {} lanes and {} allowed turns",

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 15.5 rule Note

MISRA 15.5 rule
// pInStreet->source(),
// pInStreet->target(),
// nLanes,
// allowedTurns.size()));
std::vector<Direction> newMapping(nLanes);
auto it{allowedTurns.cbegin()};
for (size_t i{0}; i < allowedTurns.size(); ++i, ++it) {
Expand Down
Loading