<commit_before>/******************************************************************************
  * Copyright 2017 The Apollo Authors. All Rights Reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *****************************************************************************/

#include "modules/routing/topo_creator/graph_creator.h"

#include "glog/logging.h"

#include "modules/common/util/file.h"
#include "modules/map/hdmap/adapter/opendrive_adapter.h"
#include "modules/routing/topo_creator/edge_creator.h"
#include "modules/routing/topo_creator/node_creator.h"

namespace apollo {
namespace routing {

using ::google::protobuf::RepeatedPtrField;
using ::apollo::hdmap::Id;
using ::apollo::routing::Node;
using ::apollo::routing::Edge;

GraphCreator::GraphCreator(const std::string& base_map_file_path,
                           const std::string& dump_topo_file_path)
    : base_map_file_path_(base_map_file_path),
      dump_topo_file_path_(dump_topo_file_path) {}

bool GraphCreator::Create() {
  if (common::util::EndWith(base_map_file_path_, ".xml")) {
    if (!hdmap::adapter::OpendriveAdapter::LoadData(base_map_file_path_,
                                                    &pbmap_)) {
      AERROR << "Failed to load base map file from " << base_map_file_path_;
      return false;
    }
  } else {
    if (!common::util::GetProtoFromFile(base_map_file_path_, &pbmap_)) {
      AERROR << "Failed to load base map file from " << base_map_file_path_;
      return false;
    }
  }

  AINFO << "Number of lanes: " << pbmap_.lane_size();

  graph_.set_hdmap_version(pbmap_.header().version());
  graph_.set_hdmap_district(pbmap_.header().district());

  node_index_map_.clear();
  road_id_map_.clear();
  showed_edge_id_set_.clear();

  for (const auto& road : pbmap_.road()) {
    for (const auto& section : road.section()) {
      for (const auto& lane_id : section.lane_id()) {
        road_id_map_[lane_id.id()] = road.id().id();
      }
    }
  }

  for (const auto& lane : pbmap_.lane()) {
    if (lane.type() != hdmap::Lane::CITY_DRIVING) {
      AINFO << "Ignored lane id: " << lane.id().id()
            << " because its type is NOT CITY_DRIVING.";
      continue;
    }
    AINFO << "Current lane id: " << lane.id().id();
    node_index_map_[lane.id().id()] = graph_.node_size();
    const auto iter = road_id_map_.find(lane.id().id());
    if (iter != road_id_map_.end()) {
      NodeCreator::GetPbNode(lane, iter->second, graph_.add_node());
    } else {
      LOG(WARNING) << "Failed to find road id of lane " << lane.id().id();
      NodeCreator::GetPbNode(lane, "", graph_.add_node());
    }
  }

  std::string edge_id = "";
  for (const auto& lane : pbmap_.lane()) {
    if (lane.type() != hdmap::Lane::CITY_DRIVING) {
      AINFO << "Ignored lane id: " << lane.id().id()
            << " because its type is NOT CITY_DRIVING.";
      continue;
    }
    const auto& from_node = graph_.node(node_index_map_[lane.id().id()]);
    // TODO: check boundary type here.
    AddEdge(from_node, lane.left_neighbor_forward_lane_id(), Edge::LEFT);
    AddEdge(from_node, lane.right_neighbor_forward_lane_id(), Edge::RIGHT);
    AddEdge(from_node, lane.successor_id(), Edge::FORWARD);
  }

  if (!::apollo::common::util::SetProtoToASCIIFile(graph_,
                                                   dump_topo_file_path_)) {
    AERROR << "Failed to dump topo data into file " << dump_topo_file_path_;
    return false;
  }
  AINFO << "File is dumped successfully. Path: " << dump_topo_file_path_;
  return true;
}

std::string GraphCreator::GetEdgeID(const std::string& from_id,
                                    const std::string& to_id) {
  return from_id + "->" + to_id;<commit_msg>routing: check boundary type before adding as an edge.<commit_after>/******************************************************************************
  * Copyright 2017 The Apollo Authors. All Rights Reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *****************************************************************************/

#include "modules/routing/topo_creator/graph_creator.h"

#include "glog/logging.h"

#include "modules/common/util/file.h"
#include "modules/map/hdmap/adapter/opendrive_adapter.h"
#include "modules/routing/topo_creator/edge_creator.h"
#include "modules/routing/topo_creator/node_creator.h"

namespace apollo {
namespace routing {

using ::google::protobuf::RepeatedPtrField;
using ::apollo::hdmap::Id;
using ::apollo::routing::Node;
using ::apollo::routing::Edge;

using ::apollo::hdmap::LaneBoundary;
using ::apollo::hdmap::LaneBoundaryType;

namespace {

bool IsAllowedToCross(const LaneBoundary& boundary) {
  for (int i = 0; i < boundary.boundary_type_size(); ++i) {
    if (boundary.boundary_type(i).types(0) != LaneBoundaryType::DOTTED_YELLOW &&
        boundary.boundary_type(i).types(0) != LaneBoundaryType::DOTTED_WHITE) {
      return false;
    }
  }
  return true;
}
}

GraphCreator::GraphCreator(const std::string& base_map_file_path,
                           const std::string& dump_topo_file_path)
    : base_map_file_path_(base_map_file_path),
      dump_topo_file_path_(dump_topo_file_path) {}

bool GraphCreator::Create() {
  if (common::util::EndWith(base_map_file_path_, ".xml")) {
    if (!hdmap::adapter::OpendriveAdapter::LoadData(base_map_file_path_,
                                                    &pbmap_)) {
      AERROR << "Failed to load base map file from " << base_map_file_path_;
      return false;
    }
  } else {
    if (!common::util::GetProtoFromFile(base_map_file_path_, &pbmap_)) {
      AERROR << "Failed to load base map file from " << base_map_file_path_;
      return false;
    }
  }

  AINFO << "Number of lanes: " << pbmap_.lane_size();

  graph_.set_hdmap_version(pbmap_.header().version());
  graph_.set_hdmap_district(pbmap_.header().district());

  node_index_map_.clear();
  road_id_map_.clear();
  showed_edge_id_set_.clear();

  for (const auto& road : pbmap_.road()) {
    for (const auto& section : road.section()) {
      for (const auto& lane_id : section.lane_id()) {
        road_id_map_[lane_id.id()] = road.id().id();
      }
    }
  }

  for (const auto& lane : pbmap_.lane()) {
    if (lane.type() != hdmap::Lane::CITY_DRIVING) {
      ADEBUG << "Ignored lane id: " << lane.id().id()
             << " because its type is NOT CITY_DRIVING.";
      continue;
    }
    AINFO << "Current lane id: " << lane.id().id();
    node_index_map_[lane.id().id()] = graph_.node_size();
    const auto iter = road_id_map_.find(lane.id().id());
    if (iter != road_id_map_.end()) {
      NodeCreator::GetPbNode(lane, iter->second, graph_.add_node());
    } else {
      LOG(WARNING) << "Failed to find road id of lane " << lane.id().id();
      NodeCreator::GetPbNode(lane, "", graph_.add_node());
    }
  }

  std::string edge_id = "";
  for (const auto& lane : pbmap_.lane()) {
    if (lane.type() != hdmap::Lane::CITY_DRIVING) {
      ADEBUG << "Ignored lane id: " << lane.id().id()
             << " because its type is NOT CITY_DRIVING.";
      continue;
    }
    const auto& from_node = graph_.node(node_index_map_[lane.id().id()]);

    if (lane.has_left_boundary() && IsAllowedToCross(lane.left_boundary())) {
      AddEdge(from_node, lane.left_neighbor_forward_lane_id(), Edge::LEFT);
    }

    if (lane.has_right_boundary() && IsAllowedToCross(lane.right_boundary())) {
      AddEdge(from_node, lane.right_neighbor_forward_lane_id(), Edge::RIGHT);
    }
    AddEdge(from_node, lane.successor_id(), Edge::FORWARD);
  }

  if (!::apollo::common::util::SetProtoToASCIIFile(graph_,
                                                   dump_topo_file_path_)) {
    AERROR << "Failed to dump topo data into file " << dump_topo_file_path_;
    return false;
  }
  AINFO << "File is dumped successfully. Path: " << dump_topo_file_path_;
  return true;
}

std::string GraphCreator::GetEdgeID(const std::string& from_id,
                                    const std::string& to_id) {
  return from_id + "->" + to_id;
{'fraction_non_alphanumeric': 0.16845126393641682, 'fraction_numerical': 0.0020973617397063694, 'mean_word_length': 3.487369985141159, 'pattern_counts': {'":': 0, '<': 84, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3824040', 'n_tokens_mistral': 2922, 'n_tokens_neox': 2870, 'n_words': 637}