11 #include <boost/geometry/geometries/box.hpp> 14 namespace traffic_manager {
17 using namespace constants::Map;
19 using TopologyList = std::vector<std::pair<WaypointPtr, WaypointPtr>>;
26 return std::make_tuple(wp->GetRoadId(), wp->GetLaneId(), wp->GetSectionId());
37 if (segment_topology.find(segment_id) == segment_topology.end()) {
41 for (
const auto &successor_segment_id : segment_topology.at(segment_id).second) {
42 if (segment_map.find(successor_segment_id) == segment_map.end()) {
43 auto successors =
GetSuccessors(successor_segment_id, segment_topology, segment_map);
44 result.insert(result.end(), successors.begin(), successors.end());
46 result.emplace_back(segment_map.at(successor_segment_id).front());
56 if (segment_topology.find(segment_id) == segment_topology.end()) {
60 for (
const auto &predecessor_segment_id : segment_topology.at(segment_id).first) {
61 if (segment_map.find(predecessor_segment_id) == segment_map.end()) {
62 auto predecessors =
GetPredecessors(predecessor_segment_id, segment_topology, segment_map);
63 result.insert(result.end(), predecessors.begin(), predecessors.end());
65 result.emplace_back(segment_map.at(predecessor_segment_id).back());
85 std::ofstream out_file;
86 out_file.open(filename, std::ios::binary);
87 if (!out_file.is_open()) {
94 out_file.write(reinterpret_cast<const char *>(&total),
sizeof(uint32_t));
97 std::unordered_set<uint64_t> used_ids;
99 if (used_ids.find(wp->GetId()) != used_ids.end()) {
100 log_error(
"Could not generate the binary file. There are repeated waypoints");
103 cached_wp.
Write(out_file);
105 used_ids.insert(wp->GetId());
113 unsigned long pos = 0;
114 std::vector<CachedSimpleWaypoint> cached_waypoints;
115 std::unordered_map<uint64_t, uint32_t> id2index;
119 memcpy(&total, &content[pos],
sizeof(total));
120 pos +=
sizeof(total);
123 for (uint32_t i=0; i < total; i++) {
125 cached_wp.
Read(content, pos);
126 cached_waypoints.push_back(cached_wp);
133 wp->SetRoadOption(static_cast<RoadOption>(cached_wp.
road_option));
140 auto cached_wp = cached_waypoints.at(i);
142 std::vector<SimpleWaypointPtr> next_waypoints;
143 for (
auto id : cached_wp.next_waypoints) {
146 std::vector<SimpleWaypointPtr> previous_waypoints;
147 for (
auto id : cached_wp.previous_waypoints) {
150 wp->SetNextWaypoint(next_waypoints);
151 wp->SetPreviousWaypoint(previous_waypoints);
152 if (cached_wp.next_left_waypoint > 0) {
153 wp->SetLeftWaypoint(
dense_topology.at(id2index.at(cached_wp.next_left_waypoint)));
155 if (cached_wp.next_right_waypoint > 0) {
156 wp->SetRightWaypoint(
dense_topology.at(id2index.at(cached_wp.next_right_waypoint)));
169 assert(
_world_map !=
nullptr &&
"No map reference found.");
170 auto waypoint_topology =
_world_map->GetTopology();
173 std::unordered_map<int64_t, std::pair<std::set<crd::RoadId>, std::set<crd::RoadId>>> std_road_connectivity;
174 std::unordered_map<crd::RoadId, bool> is_real_junction;
176 for (
auto &connection : waypoint_topology) {
177 auto &waypoint = connection.first;
178 auto &successor = connection.second;
183 if (waypoint_segment_id == successor_segment_id){
188 using SegIdVectorPair = std::pair<std::vector<SegmentId>, std::vector<SegmentId>>;
189 SegIdVectorPair &connection_first = segment_topology[waypoint_segment_id];
190 SegIdVectorPair &connection_second = segment_topology[successor_segment_id];
191 connection_first.second.push_back(successor_segment_id);
192 connection_second.first.push_back(waypoint_segment_id);
195 bool waypoint_is_junction = waypoint->IsJunction();
196 bool successor_is_junction = successor->IsJunction();
197 if (waypoint_is_junction && !successor_is_junction) {
199 int64_t std_road_id =
static_cast<int64_t
>(successor->GetRoadId());
200 std_road_id = (successor->GetLaneId() < 0) ? -1 * std_road_id : std_road_id;
202 std::set<crd::RoadId> &in_paths = std_road_connectivity[std_road_id].first;
203 in_paths.insert(path_id);
205 if (in_paths.size() >= 2) {
206 for (
auto &in_path_id: in_paths) {
207 is_real_junction[in_path_id] =
true;
213 if (!waypoint_is_junction && successor_is_junction) {
215 int64_t std_road_id =
static_cast<int64_t
>(waypoint->GetRoadId());
216 std_road_id = (waypoint->GetLaneId() < 0) ? -1 * std_road_id : std_road_id;
218 std::set<crd::RoadId> &out_paths = std_road_connectivity[std_road_id].second;
219 out_paths.insert(path_id);
221 if (out_paths.size() >= 2) {
222 for (
auto &out_path_id: out_paths) {
223 is_real_junction[out_path_id] =
true;
231 assert(
_world_map !=
nullptr &&
"No map reference found.");
233 for (
auto &waypoint_ptr: raw_dense_topology) {
236 segment_map[
GetSegmentId(waypoint_ptr)].emplace_back(std::make_shared<SimpleWaypoint>(waypoint_ptr));
244 auto square = [](
float input) {
return std::pow(input, 2);};
246 return (swp1->GetWaypoint()->GetDistance() < swp2->GetWaypoint()->GetDistance());
251 auto max = [](int16_t x, int16_t y) {
252 return x ^ ((x ^ y) & -(x < y));
256 for (
auto &segment: segment_map) {
257 auto &segment_waypoints = segment.second;
260 ++geodesic_grid_id_counter;
263 std::sort(segment_waypoints.begin(), segment_waypoints.end(), compare_s);
264 auto lane_id = segment_waypoints.front()->GetWaypoint()->GetLaneId();
266 std::reverse(segment_waypoints.begin(), segment_waypoints.end());
270 for (std::size_t i = 0; i < segment_waypoints.size() - 1; ++i) {
271 double distance = std::abs(segment_waypoints.at(i)->GetWaypoint()->GetDistance() - segment_waypoints.at(i+1)->GetWaypoint()->GetDistance());
272 double angle = wpt_angle(segment_waypoints.at(i)->GetTransform().GetForwardVector(), segment_waypoints.at(i+1)->GetTransform().GetForwardVector());
274 int16_t distance_splits =
static_cast<int16_t
>((distance*distance)/
MAX_WPT_DISTANCE);
275 auto max_splits = max(angle_splits, distance_splits);
276 if (max_splits >= 1) {
278 for (uint16_t j = 0; j < max_splits; ++j) {
279 auto next_waypoints = segment_waypoints.at(i)->GetWaypoint()->GetNext(distance/(max_splits+1));
280 if (next_waypoints.size() != 0) {
281 auto new_waypoint = next_waypoints.front();
283 segment_waypoints.insert(segment_waypoints.begin()+
static_cast<int64_t
>(i), std::make_shared<SimpleWaypoint>(new_waypoint));
293 cg::Location grid_edge_location = segment_waypoints.front()->GetLocation();
294 for (std::size_t i = 0; i < segment_waypoints.size() - 1; ++i) {
298 if (distance_squared(grid_edge_location, current_waypoint->GetLocation()) >
300 ++geodesic_grid_id_counter;
301 grid_edge_location = current_waypoint->GetLocation();
303 current_waypoint->SetGeodesicGridId(geodesic_grid_id_counter);
305 current_waypoint->SetNextWaypoint({next_waypoint});
306 next_waypoint->SetPreviousWaypoint({current_waypoint});
309 segment_waypoints.back()->SetGeodesicGridId(geodesic_grid_id_counter);
312 for (
auto swp: segment_waypoints) {
314 auto wpt = swp->GetWaypoint();
315 auto road_id = wpt->GetRoadId();
316 if (wpt->IsJunction() && !is_real_junction.count(road_id)) {
317 swp->SetIsJunction(
false);
319 swp->SetIsJunction(swp->GetWaypoint()->IsJunction());
329 for (
auto &segment : segment_map) {
331 auto &segment_waypoints = segment.second;
333 auto successors =
GetSuccessors(segment_id, segment_topology, segment_map);
334 auto predecessors =
GetPredecessors(segment_id, segment_topology, segment_map);
336 segment_waypoints.front()->SetPreviousWaypoint(predecessors);
337 segment_waypoints.back()->SetNextWaypoint(successors);
342 if (!swp->CheckJunction()) {
348 for (
auto &swp : dense_topology) {
349 if (swp->GetNextWaypoint().empty()) {
350 auto neighbour = swp->GetRightWaypoint();
352 neighbour = swp->GetLeftWaypoint();
356 swp->SetNextWaypoint(neighbour->GetNextWaypoint());
357 for (
auto next_waypoint : neighbour->GetNextWaypoint()) {
358 next_waypoint->SetPreviousWaypoint({swp});
370 if (simple_waypoint !=
nullptr) {
371 const cg::Location loc = simple_waypoint->GetLocation();
373 rtree.insert(std::make_pair(point, simple_waypoint));
380 std::vector<SimpleWaypointPtr> next_waypoints = swp->GetNextWaypoint();
381 std::size_t next_swp_size = next_waypoints.size();
383 if (next_swp_size == 0) {
388 else if (next_swp_size > 1 || (!swp->CheckJunction() && next_waypoints.front()->CheckJunction())) {
392 bool found_landmark=
false;
393 if (next_swp_size <= 1) {
395 auto all_landmarks = swp->GetWaypoint()->GetAllLandmarksInDistance(15.0);
397 if (all_landmarks.empty()) {
401 for (
auto &landmark : all_landmarks) {
402 auto landmark_type = landmark->GetType();
403 if (landmark_type ==
"1000001" || landmark_type ==
"206" || landmark_type ==
"205") {
405 found_landmark=
true;
409 if (!found_landmark) {
417 if (found_landmark || next_swp_size > 1) {
419 for (
auto &next_swp : next_waypoints) {
420 std::vector<SimpleWaypointPtr> traversed_waypoints;
423 if (next_swp_size > 1) {
424 junction_end_waypoint = next_swp;
426 junction_end_waypoint = next_waypoints.front();
429 while (junction_end_waypoint->CheckJunction()){
430 traversed_waypoints.push_back(junction_end_waypoint);
431 std::vector<SimpleWaypointPtr> temp = junction_end_waypoint->GetNextWaypoint();
435 junction_end_waypoint = temp.front();
439 int16_t current_angle =
static_cast<int16_t
>(traversed_waypoints.front()->GetTransform().rotation.yaw);
440 int16_t junction_end_angle =
static_cast<int16_t
>(traversed_waypoints.back()->GetTransform().rotation.yaw);
441 int16_t diff_angle = (junction_end_angle - current_angle) % 360;
442 bool straight = (diff_angle < STRAIGHT_DEG && diff_angle > -
STRAIGHT_DEG) ||
445 bool right = (diff_angle >=
STRAIGHT_DEG && diff_angle <= 180) ||
446 (diff_angle <= -180 && diff_angle >= -360+
STRAIGHT_DEG);
448 auto assign_option = [](
RoadOption ro, std::vector<SimpleWaypointPtr> traversed_waypoints) {
449 for (
auto &twp : traversed_waypoints) {
450 twp->SetRoadOption(ro);
470 std::vector<SpatialTreeEntry> result_1;
472 rtree.query(bgi::nearest(query_point, 1), std::back_inserter(result_1));
477 return closest_point;
488 Box lower_query_box(lower_p2, lower_p1);
489 Box upper_query_box(upper_p2, upper_p1);
493 for (Rtree::const_query_iterator
494 it =
rtree.qbegin(bgi::within(upper_query_box)
495 && !bgi::within(lower_query_box)
496 && bgi::satisfies([&](
SpatialTreeEntry const& v) { return !v.second->CheckJunction();}));
500 result.push_back(it->second);
514 const WaypointPtr raw_waypoint = reference_waypoint->GetWaypoint();
515 const crd::element::LaneMarking::LaneChange lane_change = raw_waypoint->GetLaneChange();
521 case crd::element::LaneMarking::LaneChange::Left:
523 const WaypointPtr left_waypoint = raw_waypoint->GetLeft();
524 if (left_waypoint !=
nullptr &&
525 left_waypoint->GetType() == crd::Lane::LaneType::Driving &&
526 (left_waypoint->GetLaneId() * raw_waypoint->GetLaneId() > 0)) {
529 reference_waypoint->SetLeftWaypoint(closest_simple_waypoint);
535 case crd::element::LaneMarking::LaneChange::Right:
537 const WaypointPtr right_waypoint = raw_waypoint->GetRight();
538 if(right_waypoint !=
nullptr &&
539 right_waypoint->GetType() == crd::Lane::LaneType::Driving &&
540 (right_waypoint->GetLaneId() * raw_waypoint->GetLaneId() > 0)) {
543 reference_waypoint->SetRightWaypoint(closest_simple_waypoint);
552 const WaypointPtr right_waypoint = raw_waypoint->GetRight();
553 if (right_waypoint !=
nullptr &&
554 right_waypoint->GetType() == crd::Lane::LaneType::Driving &&
555 (right_waypoint->GetLaneId() * raw_waypoint->GetLaneId() > 0)) {
558 reference_waypoint->SetRightWaypoint(closest_simple_waypointR);
562 const WaypointPtr left_waypoint = raw_waypoint->GetLeft();
563 if (left_waypoint !=
nullptr &&
564 left_waypoint->GetType() == crd::Lane::LaneType::Driving &&
565 (left_waypoint->GetLaneId() * raw_waypoint->GetLaneId() > 0)) {
568 reference_waypoint->SetLeftWaypoint(closest_simple_waypointL);
579 assert(
_world_map !=
nullptr &&
"No map reference found.");
SegmentId GetSegmentId(const WaypointPtr &wp) const
Computes the segment id of a given waypoint.
static float const STRAIGHT_DEG
NodeList GetPredecessors(const SegmentId segment_id, const SegmentTopology &segment_topology, const SegmentMap &segment_map)
static const float MAX_WPT_RADIANS
NodeList GetDenseTopology() const
This method returns the full list of discrete samples of the map in the local cache.
Rtree rtree
Spatial quadratic R-tree for indexing and querying waypoints.
bg::model::point< float, 3, bg::cs::cartesian > Point3D
InMemoryMap(WorldMap world_map)
void reverse(I begin, I end)
SimpleWaypointPtr GetWaypoint(const cg::Location loc) const
This method returns the closest waypoint to a given location on the map.
static void log_error(Args &&... args)
static const float MAP_RESOLUTION
This file contains definitions of common data structures used in traffic manager. ...
static void Cook(WorldMap world_map, const std::string &path)
NodeList GetSuccessors(const SegmentId segment_id, const SegmentTopology &segment_topology, const SegmentMap &segment_map)
void FindAndLinkLaneChange(SimpleWaypointPtr reference_waypoint)
This method is used to find and place lane change links.
static auto DistanceSquared(const Vector3D &a, const Vector3D &b)
std::vector< std::pair< WaypointPtr, WaypointPtr > > TopologyList
bg::model::box< Point3D > Box
void Save(const std::string &path)
std::map< SegmentId, std::vector< SimpleWaypointPtr > > SegmentMap
static const double MIN_LANE_WIDTH
static const float MAX_GEODESIC_GRID_LENGTH
std::map< SegmentId, std::pair< std::vector< SegmentId >, std::vector< SegmentId > >> SegmentTopology
carla::SharedPtr< cc::Waypoint > WaypointPtr
NodeList GetWaypointsInDelta(const cg::Location loc, const uint16_t n_points, const float random_sample) const
This method returns n waypoints in an delta area with a certain distance from the ego vehicle...
std::vector< SimpleWaypointPtr > NodeList
static float const Z_DELTA
static const float MAX_WPT_DISTANCE
void Read(const std::vector< uint8_t > &content, unsigned long &start)
bool Load(const std::vector< uint8_t > &content)
static double GetVectorAngle(const Vector3D &a, const Vector3D &b)
Returns the angle between 2 vectors in radians.
std::vector< WaypointPtr > RawNodeList
void Write(std::ofstream &out_file)
std::shared_ptr< SimpleWaypoint > SimpleWaypointPtr
std::pair< Point3D, SimpleWaypointPtr > SpatialTreeEntry
NodeList dense_topology
Structure to hold all custom waypoint objects after interpolation of sparse topology.
const cc::Map & GetMap() const
carla::SharedPtr< const cc::Map > WorldMap
void sort(I begin, I end, const Pred &pred)
This class builds a discretized local map-cache.
void SetUp()
This method constructs the local map with a resolution of sampling_resolution.
WorldMap _world_map
Object to hold the world map received by the constructor.
std::tuple< crd::RoadId, crd::LaneId, crd::SectionId > SegmentId