Skip to content

Commit 9d24e4a

Browse files
authored
refactor(guidance): return std::optional from findObviousTurn instead of 0 sentinel (#7648)
1 parent 6678ebc commit 9d24e4a

3 files changed

Lines changed: 32 additions & 25 deletions

File tree

include/guidance/intersection_handler.hpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ class IntersectionHandler
8383
// other possible turns. The function will consider road categories and other inputs like the
8484
// turn angles.
8585
template <typename IntersectionType> // works with Intersection and IntersectionView
86-
std::size_t findObviousTurn(const EdgeID via_edge, const IntersectionType &intersection) const;
86+
std::optional<std::size_t> findObviousTurn(const EdgeID via_edge,
87+
const IntersectionType &intersection) const;
8788

8889
// Obvious turns can still take multiple forms. This function looks at the turn onto a road
8990
// candidate when coming from a via_edge and determines the best instruction to emit.
@@ -484,13 +485,14 @@ inline bool IntersectionHandler::IsDistinctTurn(const EdgeID via_edge,
484485

485486
// Impl.
486487
template <typename IntersectionType> // works with Intersection and IntersectionView
487-
std::size_t IntersectionHandler::findObviousTurn(const EdgeID via_edge,
488-
const IntersectionType &intersection) const
488+
std::optional<std::size_t>
489+
IntersectionHandler::findObviousTurn(const EdgeID via_edge,
490+
const IntersectionType &intersection) const
489491
{
490492

491493
// no obvious road
492494
if (intersection.size() == 1)
493-
return 0;
495+
return std::nullopt;
494496

495497
// a single non u-turn is obvious
496498
if (intersection.size() == 2)
@@ -550,15 +552,20 @@ std::size_t IntersectionHandler::findObviousTurn(const EdgeID via_edge,
550552

551553
// this check is not part of the main conditions, so that if the turn looks obvious from all
552554
// other perspectives, a mode change will not result in different classification
553-
auto const to_index_if_valid = [&](auto const iterator) -> std::size_t
555+
auto const to_index_if_valid = [&](auto const iterator) -> std::optional<std::size_t>
554556
{
555557
auto const &from_data = node_based_graph.GetEdgeData(via_edge);
556558
auto const &to_data = node_based_graph.GetEdgeData(iterator->eid);
557559

558560
if (from_data.flags.roundabout != to_data.flags.roundabout)
559-
return 0;
561+
return std::nullopt;
560562

561-
return std::distance(intersection.begin(), iterator);
563+
auto const index = std::distance(intersection.begin(), iterator);
564+
// index 0 is the u-turn approach road — never an obvious turn
565+
if (index == 0)
566+
return std::nullopt;
567+
568+
return index;
562569
};
563570

564571
// in case the continuing road is distinct, we prefer continuing on the current road.
@@ -610,7 +617,7 @@ std::size_t IntersectionHandler::findObviousTurn(const EdgeID via_edge,
610617
STRAIGHT_ANGLE, [&](auto const &road) { return !road.entry_allowed; });
611618
// no valid turns
612619
if (straightmost_valid == intersection.end())
613-
return 0;
620+
return std::nullopt;
614621

615622
auto const non_sharp_turns = intersection.Count(
616623
[&](auto const &road) { return util::angularDeviation(road.angle, STRAIGHT_ANGLE) <= 90; });
@@ -659,7 +666,7 @@ std::size_t IntersectionHandler::findObviousTurn(const EdgeID via_edge,
659666
return to_index_if_valid(straightmost_valid);
660667
}
661668

662-
return 0;
669+
return std::nullopt;
663670
}
664671

665672
} // namespace osrm::guidance

src/guidance/sliproad_handler.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,12 @@ Intersection SliproadHandler::operator()(const NodeID /*nid*/,
373373
{
374374
const auto index = findObviousTurn(sliproad_edge, target_intersection);
375375

376-
if (index == 0)
376+
if (!index)
377377
{
378378
continue;
379379
}
380380

381-
const auto onto = target_intersection[index];
381+
const auto onto = target_intersection[*index];
382382
const auto angle_deviation = angularDeviation(onto.angle, STRAIGHT_ANGLE);
383383
const auto is_narrow_turn = angle_deviation <= 2 * NARROW_TURN_ANGLE;
384384

@@ -643,9 +643,9 @@ std::optional<std::size_t> SliproadHandler::getObviousIndexWithSliproads(
643643
// If a turn is obvious without taking Sliproads into account use this
644644
const auto index = findObviousTurn(from, intersection);
645645

646-
if (index != 0)
646+
if (index)
647647
{
648-
return std::make_optional(index);
648+
return index;
649649
}
650650

651651
// Otherwise check if the road is forking into two and one of them is a Sliproad;

src/guidance/turn_handler.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ Intersection TurnHandler::handleThreeWayTurn(const EdgeID via_edge, Intersection
257257
{ return node_based_graph.GetEdgeData(road.eid).flags.road_classification.IsLinkClass(); });
258258

259259
auto fork = findFork(via_edge, intersection);
260-
if (fork && (all_links || obvious_index == 0))
260+
if (fork && (all_links || !obvious_index))
261261
{
262262
assignFork(via_edge, fork->getLeft(), fork->getRight());
263263
}
@@ -269,7 +269,7 @@ Intersection TurnHandler::handleThreeWayTurn(const EdgeID via_edge, Intersection
269269
I
270270
I
271271
*/
272-
else if (isEndOfRoad(intersection[0], intersection[1], intersection[2]) && obvious_index == 0)
272+
else if (isEndOfRoad(intersection[0], intersection[1], intersection[2]) && !obvious_index)
273273
{
274274
if (intersection[1].entry_allowed)
275275
{
@@ -286,11 +286,11 @@ Intersection TurnHandler::handleThreeWayTurn(const EdgeID via_edge, Intersection
286286
intersection[2].instruction = {TurnType::OnRamp, DirectionModifier::Left};
287287
}
288288
}
289-
else if (obvious_index != 0) // has an obvious continuing road/obvious turn
289+
else if (obvious_index) // has an obvious continuing road/obvious turn
290290
{
291291
const auto direction_at_one = getTurnDirection(intersection[1].angle);
292292
const auto direction_at_two = getTurnDirection(intersection[2].angle);
293-
if (obvious_index == 1)
293+
if (*obvious_index == 1)
294294
{
295295
intersection[1].instruction =
296296
getInstructionForObvious(3,
@@ -312,7 +312,7 @@ Intersection TurnHandler::handleThreeWayTurn(const EdgeID via_edge, Intersection
312312
}
313313
else
314314
{
315-
BOOST_ASSERT(obvious_index == 2);
315+
BOOST_ASSERT(*obvious_index == 2);
316316
intersection[2].instruction =
317317
getInstructionForObvious(3,
318318
via_edge,
@@ -344,30 +344,30 @@ Intersection TurnHandler::handleThreeWayTurn(const EdgeID via_edge, Intersection
344344

345345
Intersection TurnHandler::handleComplexTurn(const EdgeID via_edge, Intersection intersection) const
346346
{
347-
const std::size_t obvious_index = findObviousTurn(via_edge, intersection);
347+
const auto obvious_index = findObviousTurn(via_edge, intersection);
348348
const auto fork = findFork(via_edge, intersection);
349349

350350
const auto straightmost = intersection.findClosestTurn(STRAIGHT_ANGLE);
351351
const auto straightmost_index = std::distance(intersection.begin(), straightmost);
352352
const auto straightmost_angle_dev = angularDeviation(straightmost->angle, STRAIGHT_ANGLE);
353353

354354
// check whether the obvious choice is actually a through street
355-
if (obvious_index != 0)
355+
if (obvious_index)
356356
{
357-
intersection[obvious_index].instruction =
357+
intersection[*obvious_index].instruction =
358358
getInstructionForObvious(intersection.size(),
359359
via_edge,
360-
isThroughStreet(obvious_index,
360+
isThroughStreet(*obvious_index,
361361
intersection,
362362
node_based_graph,
363363
node_data_container,
364364
string_table,
365365
street_name_suffix_table),
366-
intersection[obvious_index]);
366+
intersection[*obvious_index]);
367367

368368
// assign left/right turns
369-
intersection = assignLeftTurns(via_edge, std::move(intersection), obvious_index);
370-
intersection = assignRightTurns(via_edge, std::move(intersection), obvious_index);
369+
intersection = assignLeftTurns(via_edge, std::move(intersection), *obvious_index);
370+
intersection = assignRightTurns(via_edge, std::move(intersection), *obvious_index);
371371
}
372372
else if (fork) // found fork
373373
{

0 commit comments

Comments
 (0)