@@ -790,6 +790,63 @@ std::vector<Junction> OpenDriveMap::get_junctions() const
790790 return get_map_values (this ->id_to_junction );
791791}
792792
793+ const LaneSection* OpenDriveMap::get_adjacent_lanesection (const std::string& road_id, const double & lanesection_s0, const bool predecessor) const
794+ {
795+ const Road& road = this ->id_to_road .at (road_id);
796+ const auto s_lanesec_iter = road.s_to_lanesection .find (lanesection_s0);
797+ if (s_lanesec_iter == road.s_to_lanesection .end ()) // also catches empty road
798+ return nullptr ;
799+
800+ if (predecessor)
801+ {
802+ if (s_lanesec_iter != road.s_to_lanesection .begin ())
803+ return &(std::prev (s_lanesec_iter)->second );
804+ }
805+ else
806+ {
807+ const auto next_lanesec_iter = std::next (s_lanesec_iter);
808+ if (next_lanesec_iter != road.s_to_lanesection .end ())
809+ return &(next_lanesec_iter->second );
810+ }
811+
812+ // adjacent lanesection not in the same road
813+ const RoadLink& road_link = predecessor ? road.predecessor : road.successor ;
814+ if (road_link.type == RoadLink::Type::Road && road_link.contact_point != RoadLink::ContactPoint::None)
815+ {
816+ const auto next_road_iter = this ->id_to_road .find (road_link.id );
817+ if (next_road_iter == this ->id_to_road .end ())
818+ return nullptr ;
819+
820+ const Road& next_road = next_road_iter->second ;
821+ if (next_road.s_to_lanesection .empty ())
822+ return nullptr ;
823+
824+ const LaneSection& adjacent_lanesection = (road_link.contact_point == RoadLink::ContactPoint::Start)
825+ ? next_road.s_to_lanesection .begin ()->second
826+ : next_road.s_to_lanesection .rbegin ()->second ;
827+ return &adjacent_lanesection;
828+ }
829+
830+ return nullptr ;
831+ }
832+
833+ const Lane* OpenDriveMap::get_adjacent_lane (const Lane& lane, const bool predecessor) const
834+ {
835+ const LaneSection* adjacent_lanesec = this ->get_adjacent_lanesection (lane.key .road_id , lane.key .lanesection_s0 , predecessor);
836+ if (!adjacent_lanesec)
837+ return nullptr ;
838+
839+ const std::optional<int > adjacent_lane_id = predecessor ? lane.predecessor : lane.successor ;
840+ if (adjacent_lane_id)
841+ {
842+ const auto adjacent_lane_iter = adjacent_lanesec->id_to_lane .find (adjacent_lane_id.value ());
843+ if (adjacent_lane_iter != adjacent_lanesec->id_to_lane .end ())
844+ return &(adjacent_lane_iter->second );
845+ }
846+
847+ return nullptr ;
848+ }
849+
793850RoadNetworkMesh OpenDriveMap::get_road_network_mesh (const double eps) const
794851{
795852 RoadNetworkMesh out_mesh;
@@ -853,97 +910,35 @@ RoutingGraph OpenDriveMap::get_routing_graph() const
853910{
854911 RoutingGraph routing_graph;
855912
856- // Lambda to find adjacent lanesection
857- auto get_adjacent_lanesection =
858- [&](const Road& current_road, const LaneSection& current_lanesection, bool predecessors) -> std::optional<LaneSection>
913+ // Parse Roads
914+ for (const auto & [_, road] : id_to_road)
859915 {
860- std::optional<LaneSection> lanesection;
861- auto it = current_road.s_to_lanesection .find (current_lanesection.s0 );
862-
863- if (predecessors)
864- {
865- if (it != current_road.s_to_lanesection .begin ())
866- lanesection = std::prev (it)->second ; // Safe to decrement and return previous section
867- }
868- else
869- {
870- auto next_it = std::next (it);
871- if (next_it != current_road.s_to_lanesection .end ())
872- lanesection = next_it->second ; // Return next section
873- }
874-
875- if (!lanesection)
916+ for (const auto & [_, lanesection] : road.s_to_lanesection )
876917 {
877- const RoadLink& road_link = predecessors ? current_road.predecessor : current_road.successor ;
878- if (road_link.type == RoadLink::Type::Road && road_link.contact_point != RoadLink::ContactPoint::None)
918+ for (const auto & [_, lane] : lanesection.id_to_lane )
879919 {
880- auto next_road_iter = id_to_road.find (road_link.id );
881- if (next_road_iter != id_to_road.end ())
920+ const bool lane_follows_road_direction = lane.key .lane_id < 0 ;
921+
922+ const Lane* predecessor_lane = this ->get_adjacent_lane (lane, lane_follows_road_direction);
923+ if (predecessor_lane)
882924 {
883- const Road& next_road = next_road_iter-> second ;
884- lanesection = (road_link. contact_point == RoadLink::ContactPoint::Start) ? next_road. s_to_lanesection . begin ()-> second
885- : next_road. s_to_lanesection . rbegin ()-> second ;
925+ const Road& predecessor_road = this -> id_to_road . at (predecessor_lane-> key . road_id ) ;
926+ const double lane_length = predecessor_road. get_lanesection_length (predecessor_lane-> key . lanesection_s0 );
927+ routing_graph. add_edge ( RoutingGraphEdge (predecessor_lane-> key , lane. key , lane_length)) ;
886928 }
887- }
888- }
889929
890- return lanesection;
891- };
892-
893- // Lambda to find connecting lane
894- auto get_connecting_lane = [&](const Lane& lane, bool predecessors, const std::optional<LaneSection>& target_lanesection) -> std::optional<Lane>
895- {
896- if (target_lanesection)
897- {
898- std::optional<int > target_lane_id = predecessors ? lane.predecessor : lane.successor ;
899- if (target_lane_id)
900- {
901- auto it = target_lanesection->id_to_lane .find (target_lane_id.value ());
902- if (it != target_lanesection->id_to_lane .end ())
903- return it->second ;
930+ const Lane* successor_lane = this ->get_adjacent_lane (lane, !lane_follows_road_direction);
931+ if (successor_lane)
932+ {
933+ const double lane_length = road.get_lanesection_length (lane.key .lanesection_s0 );
934+ routing_graph.add_edge (RoutingGraphEdge (lane.key , successor_lane->key , lane_length));
935+ }
904936 }
905937 }
906- return std::nullopt ;
907- };
908-
909- // Lambda to add an edge to the graph
910- auto add_edge_to_graph = [&](const LaneKey& from, const LaneKey& to, const Road& road)
911- {
912- const double lane_length = road.get_lanesection_length (from.lanesection_s0 );
913- routing_graph.add_edge (RoutingGraphEdge (from, to, lane_length));
914- };
915-
916- // Lambda to process a single lane
917- auto process_lane = [&](const Road& road, const LaneSection& lanesection, const Lane& lane)
918- {
919- const bool lane_follows_road_direction = lane.key .lane_id < 0 ;
920-
921- auto prev_lanesection = get_adjacent_lanesection (road, lanesection, true );
922- auto next_lanesection = get_adjacent_lanesection (road, lanesection, false );
923-
924- std::optional<Lane> predecessor =
925- get_connecting_lane (lane, lane_follows_road_direction, lane_follows_road_direction ? prev_lanesection : next_lanesection);
926- if (predecessor && this ->id_to_road .count (predecessor->key .road_id ))
927- add_edge_to_graph (predecessor->key , lane.key , this ->get_road (predecessor->key .road_id ));
928-
929- std::optional<Lane> successor =
930- get_connecting_lane (lane, !lane_follows_road_direction, lane_follows_road_direction ? next_lanesection : prev_lanesection);
931- if (successor)
932- add_edge_to_graph (lane.key , successor->key , road);
933- };
934-
935- // Parse roads
936- for (const auto & [_, road] : id_to_road)
937- {
938- for (const auto & [_, lanesection] : road.s_to_lanesection )
939- {
940- for (const auto & [_, lane] : lanesection.id_to_lane )
941- process_lane (road, lanesection, lane);
942- }
943938 }
944939
945- // Lambda to process a single junction
946- auto process_junction = [&] (const Junction& junction)
940+ // Parse Junctions
941+ for (const auto & [_, junction] : id_to_junction )
947942 {
948943 for (const auto & [_, conn] : junction.id_to_connection )
949944 {
@@ -982,11 +977,7 @@ RoutingGraph OpenDriveMap::get_routing_graph() const
982977 routing_graph.add_edge (RoutingGraphEdge (from, to, lane_length));
983978 }
984979 }
985- };
986-
987- // Parse junctions
988- for (const auto & id_junc : id_to_junction)
989- process_junction (id_junc.second );
980+ }
990981
991982 return routing_graph;
992983}
0 commit comments