From b157ae015c7a022caf840a9843c7fa9ba561f0b1 Mon Sep 17 00:00:00 2001 From: Oleksandr Shyshatskyi Date: Wed, 28 Oct 2026 20:31:48 +0200 Subject: [PATCH 1/2] Fix rounded entrances and oblique T-junction instructions --- backends/osmand/omaps_turns.cpp | 5 +- tests/CMakeLists.txt | 7 + tests/organicmaps_turns_test.cpp | 138 ++++++++++++++++++ third_party/organicmaps-routing/README.md | 15 +- .../routing/car_directions.cpp | 38 +++-- .../routing/turns_generator.cpp | 62 ++++++++ .../routing/turns_generator.hpp | 5 + tools/junction_lab/cases.tsv | 1 + 8 files changed, 251 insertions(+), 20 deletions(-) create mode 100644 tests/organicmaps_turns_test.cpp diff --git a/backends/osmand/omaps_turns.cpp b/backends/osmand/omaps_turns.cpp index 61b306e..5d0f022 100644 --- a/backends/osmand/omaps_turns.cpp +++ b/backends/osmand/omaps_turns.cpp @@ -885,10 +885,7 @@ Result Builder::Run(std::vector> const & route, d if (outgoing.m_path.empty() || outgoing.m_path.front().GetPoint() != m_path[pathIndex].GetPoint() || m_pathSegments[k - 1].m_segmentRange.IsEmpty() || outgoing.m_segmentRange.IsEmpty()) continue; - routing::turns::TurnInfo const info(&m_pathSegments[k - 1], &outgoing); - double const curve = routing::turns::CalcTurnAngle(routingResult, k, numMwmIds, settings); - double const first = routing::turns::CalcOneSegmentTurnAngle(info); - turn.route_angle = (curve >= 0) == (first >= 0) ? (std::abs(curve) > std::abs(first) ? curve : first) : curve; + turn.route_angle = routing::turns::CalcInstructionTurnAngle(routingResult, k, numMwmIds, settings); size_t ingoing = 0; routing::turns::TurnCandidates candidates; routingResult.GetPossibleTurns(m_pathSegments[k - 1].m_segmentRange, m_path[pathIndex].GetPoint(), ingoing, diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b6a7304..0bc396c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -163,3 +163,10 @@ foreach(check separate_arguments(command UNIX_COMMAND "${command}") add_test(NAME check_${name} COMMAND ${command} WORKING_DIRECTORY ${ONC_ROOT}) endforeach() + +# Organic Maps decisions on small synthetic road graphs; no OBF files required. +include(${ONC_ROOT}/third_party/organicmaps-routing/sources.cmake) +add_library(organicmaps_test_support STATIC ${ONC_ORGANICMAPS_SOURCES}) +target_include_directories(organicmaps_test_support SYSTEM PUBLIC ${ONC_ORGANICMAPS_INCLUDES}) +target_compile_options(organicmaps_test_support PRIVATE -w) +onc_test(organicmaps_turns_test SOURCES organicmaps_turns_test.cpp LIBS organicmaps_test_support) diff --git a/tests/organicmaps_turns_test.cpp b/tests/organicmaps_turns_test.cpp new file mode 100644 index 0000000..bc02acf --- /dev/null +++ b/tests/organicmaps_turns_test.cpp @@ -0,0 +1,138 @@ +#include "routing/car_directions.hpp" +#include "routing/turns_generator.hpp" +#include "routing/turns_generator_utils.hpp" +#include "geometry/mercator.hpp" + +#include +#include +#include +#include + +// Internal car decision entry point: run the complete classification, including +// suppression and leftmost/rightmost correction, on a small routing result. +namespace routing { +void GetTurnDirectionBasic(turns::IRoutingResult const &, size_t, NumMwmIds const &, + RoutingSettings const &, turns::TurnItem &); +} +namespace { +using namespace routing; +using namespace routing::turns; +using ftypes::HighwayClass; + +void require(bool ok, char const * message) +{ + if (!ok) { std::fprintf(stderr, "%s\n", message); std::exit(1); } +} + +m2::PointD metres(double east, double north) +{ + return mercator::FromLatLon(north / 111317.0, east / 111317.0); +} + +struct Route : IRoutingResult +{ + TUnpackedPathSegments segments{2}; + TurnCandidates candidates; + Route(std::vector const & outgoing) + { + segments[0].m_path = {metres(0, -80), metres(0, 0)}; + for (auto const & p : outgoing) segments[1].m_path.emplace_back(p); + for (size_t i = 0; i < 2; ++i) + { + auto & s = segments[i]; + s.m_highwayClass = HighwayClass::LivingStreet; + s.m_segmentRange = SegmentRange(FeatureID(0, i + 1), 5, 5, true, + s.m_path.front().GetPoint(), s.m_path.back().GetPoint()); + } + } + TUnpackedPathSegments const & GetSegments() const override { return segments; } + void GetPossibleTurns(SegmentRange const &, m2::PointD const &, size_t & count, + TurnCandidates & turns) const override { count = 3; turns = candidates; } + double GetPathLength() const override { return 200; } + geometry::PointWithAltitude GetStartPoint() const override { return segments[0].m_path.front(); } + geometry::PointWithAltitude GetEndPoint() const override { return segments[1].m_path.back(); } + double angle() const + { return CalcInstructionTurnAngle(*this, 1, NumMwmIds{}, GetRoutingSettings(VehicleType::Car)); } + CarDirection direction() const + { + TurnItem turn; + GetTurnDirectionBasic(*this, 1, NumMwmIds{}, GetRoutingSettings(VehicleType::Car), turn); + return turn.m_turn; + } +}; + +void entranceTests() +{ + // The reported entrance at Вереснева площа: short perpendicular entrance, + // then a diagonal street. Use its actual geometry as well as synthetic cases. + Route reported({mercator::FromLatLon(48.485428, 34.954022), + mercator::FromLatLon(48.485426, 34.953947), + mercator::FromLatLon(48.485430, 34.953880), + mercator::FromLatLon(48.485503, 34.953754), + mercator::FromLatLon(48.485778, 34.953483), + mercator::FromLatLon(48.486694, 34.952391)}); + reported.segments[0].m_path = {mercator::FromLatLon(48.484921, 34.954151), + mercator::FromLatLon(48.485428, 34.954022)}; + require(reported.angle() > -40 && reported.angle() < -30, "reported entrance should use the diagonal heading"); + for (double sign : {-1.0, 1.0}) + { + Route rounded({metres(0, 0), metres(sign * 5, 0), metres(sign * 10, 5), metres(sign * 50, 45)}); + require(std::abs(std::abs(rounded.angle()) - 45) < 1, "rounded entrance should use diagonal heading"); + // Other exits: opposite diagonal and ahead. A U-turn is included as in the adapter. + rounded.candidates.candidates = {{-135, Segment(0, 3, 0, true), HighwayClass::LivingStreet, false}, + {sign * 90, Segment(0, 2, 5, true), HighwayClass::LivingStreet, false}, + {10, Segment(0, 4, 0, true), HighwayClass::LivingStreet, false}, + {180, Segment(0, 1, 5, false), HighwayClass::LivingStreet, false}}; + std::sort(rounded.candidates.candidates.begin(), rounded.candidates.candidates.end(), + [](auto const & a, auto const & b) { return a.m_angle < b.m_angle; }); + require(rounded.direction() == (sign < 0 ? CarDirection::TurnSlightLeft : CarDirection::TurnSlightRight), + "rounded entrance must produce a slight instruction"); + + Route square({metres(0, 0), metres(sign * 5, 0), metres(sign * 80, 0)}); + require(std::abs(std::abs(square.angle()) - 90) < 1, "square corner must stay square"); + Route sharp({metres(0, 0), metres(sign * 2, -5), metres(sign * 60, 40)}); + require(std::abs(sharp.angle()) > 135, "sharp turn must remain sharp"); + Route shortRoad({metres(0, 0), metres(sign * 5, 0), metres(sign * 10, 5)}); + require(std::abs(shortRoad.angle()) > 80, "must not infer a settled heading beyond the next junction"); + Route laterBend({metres(0, 0), metres(sign * 40, 0), metres(sign * 70, 30)}); + require(std::abs(laterBend.angle()) > 80, "a later bend must not soften the corner"); + Route sBend({metres(0, 0), metres(sign * 5, 0), metres(sign * 10, 10), metres(sign * 25, 12)}); + require(std::abs(sBend.angle()) > 80, "an S-bend must not soften the corner"); + } +} + +void tJunctionTests() +{ + for (double sign : {-1.0, 1.0}) + { + Route t({metres(0, 0), metres(sign * 59, 81)}); + t.candidates.candidates = {{sign * 36, Segment(0, 2, 5, true), HighwayClass::LivingStreet, false}, + {-sign * 138, Segment(0, 2, 4, false), HighwayClass::LivingStreet, false}}; + std::sort(t.candidates.candidates.begin(), t.candidates.candidates.end(), + [](auto const & a, auto const & b) { return a.m_angle < b.m_angle; }); + require(t.direction() == (sign < 0 ? CarDirection::TurnSlightLeft : CarDirection::TurnSlightRight), + "angled T-junction needs an instruction in either direction"); + auto & incoming = t.segments[0]; + auto const originalRange = incoming.m_segmentRange; + incoming.m_segmentRange = SegmentRange(FeatureID(0, 2), 3, 3, true, + incoming.m_path.front().GetPoint(), incoming.m_path.back().GetPoint()); + require(t.direction() == CarDirection::None, "continuing on the same feature should keep the old suppression"); + incoming.m_segmentRange = originalRange; + t.segments[1].m_isLink = true; + require(t.direction() == CarDirection::None, "link roads should keep their existing rules"); + t.segments[1].m_isLink = false; + for (auto & c : t.candidates.candidates) + if (c.m_segment.GetSegmentIdx() == 4) c.m_segment = Segment(0, 3, 4, false); + require(t.direction() == CarDirection::None, "ordinary continuation past a rear branch stays silent"); + t.candidates.candidates.erase(t.candidates.candidates.begin() + 1, t.candidates.candidates.end()); + t.candidates.candidates[0] = {sign * 36, Segment(0, 2, 5, true), HighwayClass::LivingStreet, false}; + require(t.direction() == CarDirection::None, "a road bend without a choice stays silent"); + } +} +} +int main() +{ + entranceTests(); + tJunctionTests(); + std::puts("Organic Maps entrance and T-junction regressions passed"); +} diff --git a/third_party/organicmaps-routing/README.md b/third_party/organicmaps-routing/README.md index c7ce0bf..e19a2ec 100644 --- a/third_party/organicmaps-routing/README.md +++ b/third_party/organicmaps-routing/README.md @@ -15,7 +15,7 @@ The code is C++17 (no C++20 features), so it can later build with QNX GCC 8.5. ## Files -Unchanged turn/lane logic: `routing/car_directions.*`, `turns.*`, +Turn/lane logic copied from upstream, except for the behavioural fixes below: `routing/car_directions.*`, `turns.*`, `turns_generator.*`, `turns_generator_utils.*`, `turn_candidate.hpp`, `routing_result_graph.hpp`, `loaded_path_segment.hpp`, `lanes/lane_info.*`, `lanes/lanes_recommendation.*`. @@ -44,6 +44,19 @@ Behavioural fixes (the only changes to OM's decisions; candidates to report upst (48.487081,34.941727 → 48.489282,34.937865: the exit right onto the link at 48.488918,34.938895). Returns `i` now: only the turns inside the U-turn. +- `turns_generator.cpp` `CalcInstructionTurnAngle`: a normal turn with an entrance + edge at most 10 m long may use the heading over metres 10..30 when the road + consistently opens into a slight turn. Sampling stays inside the outgoing + loaded segment, with no junction crossing; sharp turns and S-bends retain the + original angle. The backend uses this same angle for its cluster symbol. +- `car_directions.cpp` `CanDiscardTurnByHighwayClassOrAngles`: retain a slight + turn of at least 25 degrees when the only two exits are opposite directions + of the same crossing feature, distinct from the incoming road (an oblique + T-junction). The sharper alternative must not silence the instruction. + +Regression coverage: `tests/organicmaps_turns_test.cpp` (portable `make test`) +plus the Вереснева площа route in `tools/junction_lab/cases.tsv`. + Shims (`shim/`): `base/assert.hpp` (ASSERT compiled out as in OM release builds; CHECK throws instead of aborting), `base/logging.hpp`, `base/math.hpp`, `base/stl_helpers.hpp`, `base/string_utils.hpp`, `base/internal/message.hpp`, diff --git a/third_party/organicmaps-routing/routing/car_directions.cpp b/third_party/organicmaps-routing/routing/car_directions.cpp index 7c0b717..500cd18 100644 --- a/third_party/organicmaps-routing/routing/car_directions.cpp +++ b/third_party/organicmaps-routing/routing/car_directions.cpp @@ -313,6 +313,27 @@ bool CanDiscardTurnByHighwayClassOrAngles(CarDirection const routeDirection, dou Segment firstOutgoingSegment; turnInfo.m_outgoing->m_segmentRange.GetFirstSegment(numMwmIds, firstOutgoingSegment); + // At an oblique T-junction both exits can belong to the crossing road. + // Choosing the less sharp exit still requires an instruction: the incoming + // road ends here. Do not suppress it as an obvious continuation merely + // because the other direction of that road points mostly backwards. + Segment incomingSegment; + if (turnCandidates.size() == 2 && std::abs(routeAngle) >= 25.0 && + !turnInfo.m_ingoing->m_isLink && !turnInfo.m_outgoing->m_isLink && + turnInfo.m_ingoing->m_segmentRange.GetFirstSegment(numMwmIds, incomingSegment)) + { + auto const & a = turnCandidates[0]; + auto const & b = turnCandidates[1]; + auto const sameFeature = [](Segment const & x, Segment const & y) + { return x.GetMwmId() == y.GetMwmId() && x.GetFeatureId() == y.GetFeatureId(); }; + if (sameFeature(a.m_segment, firstOutgoingSegment) && sameFeature(b.m_segment, firstOutgoingSegment) && + !sameFeature(incomingSegment, firstOutgoingSegment) && + a.m_segment.IsForward() != b.m_segment.IsForward() && + a.m_segment.GetPointId(false) == b.m_segment.GetPointId(false) && + std::abs(std::abs(a.m_angle - b.m_angle) - 180.0) <= 25.0) + return false; + } + for (auto const & t : turnCandidates) { // Let's consider all outgoing segments except for route outgoing segment. @@ -499,21 +520,8 @@ void GetTurnDirectionBasic(IRoutingResult const & result, size_t const outgoingS return; } - double const turnAngle = CalcTurnAngle(result, outgoingSegmentIndex, numMwmIds, vehicleSettings); - - // This angle is calculated using only 1 segment back and forward, not like turnAngle. - double const turnOneSegmentAngle = CalcOneSegmentTurnAngle(turnInfo); - - // To not discard some disputable turns let's use max by modulus from turnOneSegmentAngle and turnAngle. - // It's natural since angles of turnCandidates are calculated in IRoutingResult::GetPossibleTurns() - // according to CalcOneSegmentTurnAngle logic. And to be safe turnAngle is used too. - double turnAngleToCompare = turnAngle; - if (turnOneSegmentAngle <= 0 && turnAngle <= 0) - turnAngleToCompare = std::min(turnOneSegmentAngle, turnAngle); - else if (turnOneSegmentAngle >= 0 && turnAngle >= 0) - turnAngleToCompare = std::max(turnOneSegmentAngle, turnAngle); - else if (std::abs(turnOneSegmentAngle) > 10) - LOG(LWARNING, ("Significant angles are expected to have the same sign.")); + double const turnAngleToCompare = + CalcInstructionTurnAngle(result, outgoingSegmentIndex, numMwmIds, vehicleSettings); CarDirection const intermediateDirection = IntermediateDirection(turnAngleToCompare); diff --git a/third_party/organicmaps-routing/routing/turns_generator.cpp b/third_party/organicmaps-routing/routing/turns_generator.cpp index 07be470..bde9ed3 100644 --- a/third_party/organicmaps-routing/routing/turns_generator.cpp +++ b/third_party/organicmaps-routing/routing/turns_generator.cpp @@ -6,6 +6,8 @@ #include "geometry/mercator.hpp" +#include "base/logging.hpp" + namespace routing { namespace turns @@ -283,6 +285,66 @@ double CalcTurnAngle(IRoutingResult const & result, size_t const outgoingSegment return math::RadToDeg(PiMinusTwoVectorsAngle(junctionPoint, ingoingPoint, outgoingPoint)); } +double CalcInstructionTurnAngle(IRoutingResult const & result, size_t outgoingSegmentIndex, + NumMwmIds const & numMwmIds, RoutingSettings const & vehicleSettings) +{ + double const curve = CalcTurnAngle(result, outgoingSegmentIndex, numMwmIds, vehicleSettings); + TurnInfo info; + if (!GetTurnInfo(result, outgoingSegmentIndex, vehicleSettings, info)) + return curve; + double const first = CalcOneSegmentTurnAngle(info); + double usual = curve; + if (first <= 0 && curve <= 0) + usual = std::min(first, curve); + else if (first >= 0 && curve >= 0) + usual = std::max(first, curve); + else if (std::abs(first) > 10) + LOG(LWARNING, ("Significant angles are expected to have the same sign.")); + + // A few metres perpendicular to the incoming road can be a rounded entrance + // to a diagonal street. Measure its settled heading over metres 10..30, only + // inside this loaded segment (never across another junction). Keep ordinary + // corners, sharp turns and S-bends on the original angle calculation. + auto const & path = info.m_outgoing->m_path; + if (path.size() < 3 || std::abs(first) < 50.0 || std::abs(first) >= 135.0 || + mercator::DistanceOnEarth(path[0].GetPoint(), path[1].GetPoint()) > 10.0) + return usual; + auto const junction = path.front().GetPoint(); + auto const & incoming = info.m_ingoing->m_path; + auto const back = incoming[incoming.size() - 2].GetPoint() - junction; + auto const angleOf = [&](m2::PointD const & direction) + { return math::RadToDeg(PiMinusTwoVectorsAngle(m2::PointD::Zero(), back, direction)); }; + m2::PointD sampleStart; + double distance = 0.0; + double previousAngle = first; + for (size_t i = 1; i < path.size(); ++i) + { + auto const a = path[i - 1].GetPoint(); + auto const b = path[i].GetPoint(); + double const length = mercator::DistanceOnEarth(a, b); + if (length < 0.001) + continue; + double const angle = angleOf(b - a); + // Only a bend that consistently opens out, on the same side of the approach. + if ((angle >= 0) != (first >= 0) || std::abs(angle) > std::abs(previousAngle) + 5.0) + return usual; + if (distance <= 10.0 && distance + length >= 10.0) + sampleStart = a + (b - a) * ((10.0 - distance) / length); + if (distance + length >= 30.0) + { + auto const sampleEnd = a + (b - a) * ((30.0 - distance) / length); + double const settled = angleOf(sampleEnd - sampleStart); + if (std::abs(settled) >= 10.0 && std::abs(settled) < 50.0 && + std::abs(first) - std::abs(settled) >= 25.0 && std::abs(settled - angle) <= 15.0) + return settled; + return usual; + } + distance += length; + previousAngle = angle; + } + return usual; +} + void CorrectCandidatesSegmentByOutgoing(TurnInfo const & turnInfo, Segment const & firstOutgoingSeg, TurnCandidates & nodes) { diff --git a/third_party/organicmaps-routing/routing/turns_generator.hpp b/third_party/organicmaps-routing/routing/turns_generator.hpp index 10ae377..bf31dcc 100644 --- a/third_party/organicmaps-routing/routing/turns_generator.hpp +++ b/third_party/organicmaps-routing/routing/turns_generator.hpp @@ -61,6 +61,11 @@ bool GetTurnInfo(IRoutingResult const & result, size_t const outgoingSegmentInde double CalcTurnAngle(IRoutingResult const & result, size_t const outgoingSegmentIndex, NumMwmIds const & numMwmIds, RoutingSettings const & vehicleSettings); +// Angle used by car instructions and the cluster picture. Accounts for a short +// rounded entrance without looking beyond the next junction. +double CalcInstructionTurnAngle(IRoutingResult const & result, size_t outgoingSegmentIndex, + NumMwmIds const & numMwmIds, RoutingSettings const & vehicleSettings); + void RemoveUTurnCandidate(TurnInfo const & turnInfo, NumMwmIds const & numMwmIds, std::vector & turnCandidates); diff --git a/tools/junction_lab/cases.tsv b/tools/junction_lab/cases.tsv index b3fe30f..17e9fa9 100644 --- a/tools/junction_lab/cases.tsv +++ b/tools/junction_lab/cases.tsv @@ -6,3 +6,4 @@ 48.4647,35.0462 48.5300,34.9500 TurnRight UTurnLeft ExitHighwayToRight ExitHighwayToRight TurnSlightRight TurnRight TurnRight Dnipro: overlapping OSM ways and a 0.3 m stub at 48.516022,34.999655 give no extra go-straight / left-then-right 50.4501,30.5234 50.4547,30.5238 TurnLeft TurnSlightLeft GoStraight TurnSlightRight TurnRight TurnRight UTurnLeft TurnSlightLeft TurnRight central Kyiv 48.428973,35.065269 48.410061,35.139341 TurnRight OsmAnd:RNDB3 TurnSlightLeft/keep_left TurnRight TurnSlightLeft/keep_left TurnRight TurnLeft GoStraight TurnLeft Dnipro, Набережна Перемоги: the road bears right, a ramp (tagged primary) leaves 9 degrees further right; OM tie-break slight left is keep left (48.410077,35.086365; also 48.41268,35.104355) +48.481865,34.954953 48.489861,34.948475 TurnSlightLeft TurnSlightRight TurnSlightLeft/keep_left TurnLeft TurnRight TurnRight Dnipro: rounded entrance onto Вереснева площа uses the diagonal heading; preserve the 36-degree right at the angled T-junction onto Семена Бардадима From a8ebe23120636ceb3da9ef0e9421531bfde7a294 Mon Sep 17 00:00:00 2001 From: Oleksandr Shyshatskyi Date: Wed, 28 Oct 2026 20:35:09 +0200 Subject: [PATCH 2/2] Include integer types explicitly in lane definitions --- third_party/organicmaps-routing/routing/lanes/lane_way.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/third_party/organicmaps-routing/routing/lanes/lane_way.hpp b/third_party/organicmaps-routing/routing/lanes/lane_way.hpp index 8047f8b..348315c 100644 --- a/third_party/organicmaps-routing/routing/lanes/lane_way.hpp +++ b/third_party/organicmaps-routing/routing/lanes/lane_way.hpp @@ -3,6 +3,7 @@ #include "base/assert.hpp" #include +#include #include #include #include