Apollo 11.0
自动驾驶开放平台
sim_map_generator.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#include "absl/strings/match.h"
18#include "gflags/gflags.h"
19
20#include "modules/common_msgs/map_msgs/map.pb.h"
21
22#include "cyber/common/file.h"
23#include "cyber/common/log.h"
28
34DEFINE_string(output_dir, "/tmp/", "output map directory");
35DEFINE_double(angle_threshold, 1. / 180 * M_PI, /* 1 degree */
36 "Points are sampled when the accumulated direction change "
37 "exceeds the threshold");
38DEFINE_int32(downsample_distance, 5, "downsample rate for a normal path");
39DEFINE_int32(steep_turn_downsample_distance, 1,
40 "downsample rate for a steep turn path");
41
49
50static void DownsampleCurve(Curve* curve) {
51 auto* line_segment = curve->mutable_segment(0)->mutable_line_segment();
52 std::vector<PointENU> points(line_segment->point().begin(),
53 line_segment->point().end());
54 line_segment->clear_point();
55
56 // NOTE: this not the most efficient implementation, but since this map tool
57 // is only run once for each, we can probably live with that.
58
59 // Downsample points by angle then by distance.
60 auto sampled_indices = DownsampleByAngle(points, FLAGS_angle_threshold);
61 std::vector<PointENU> downsampled_points;
62 for (const size_t index : sampled_indices) {
63 downsampled_points.push_back(points[index]);
64 }
65
66 sampled_indices =
67 DownsampleByDistance(downsampled_points, FLAGS_downsample_distance,
68 FLAGS_steep_turn_downsample_distance);
69
70 for (const size_t index : sampled_indices) {
71 *line_segment->add_point() = downsampled_points[index];
72 }
73 size_t new_size = line_segment->point_size();
74 CHECK_GT(new_size, 1U);
75
76 AINFO << "Lane curve downsampled from " << points.size() << " points to "
77 << new_size << " points.";
78}
79
80static void DownsampleMap(Map* map_pb) {
81 for (int i = 0; i < map_pb->lane_size(); ++i) {
82 auto* lane = map_pb->mutable_lane(i);
83 lane->clear_left_sample();
84 lane->clear_right_sample();
85 lane->clear_left_road_sample();
86 lane->clear_right_road_sample();
87
88 AINFO << "Downsampling lane " << lane->id().id();
89 DownsampleCurve(lane->mutable_central_curve());
90 DownsampleCurve(lane->mutable_left_boundary()->mutable_curve());
91 DownsampleCurve(lane->mutable_right_boundary()->mutable_curve());
92 }
93}
94
95static void OutputMap(const Map& map_pb) {
96 std::ofstream map_txt_file(FLAGS_output_dir + "/sim_map.txt");
97 map_txt_file << map_pb.DebugString();
98 map_txt_file.close();
99
100 std::ofstream map_bin_file(FLAGS_output_dir + "/sim_map.bin");
101 std::string map_str;
102 map_pb.SerializeToString(&map_str);
103 map_bin_file << map_str;
104 map_bin_file.close();
105}
106
107int main(int32_t argc, char** argv) {
108 google::InitGoogleLogging(argv[0]);
109 FLAGS_alsologtostderr = true;
110 FLAGS_v = 3;
111
112 google::ParseCommandLineFlags(&argc, &argv, true);
113
114 Map map_pb;
115 const auto map_file = apollo::hdmap::BaseMapFile();
116 if (absl::EndsWith(map_file, ".xml")) {
117 ACHECK(OpendriveAdapter::LoadData(map_file, &map_pb));
118 } else {
119 ACHECK(GetProtoFromFile(map_file, &map_pb)) << "Fail to open: " << map_file;
120 }
121
122 DownsampleMap(&map_pb);
123 OutputMap(map_pb);
124 AINFO << "sim_map generated at:" << FLAGS_output_dir;
125
126 return 0;
127}
static bool LoadData(const std::string &filename, apollo::hdmap::Map *pb_map)
#define ACHECK(cond)
Definition log.h:80
#define AINFO
Definition log.h:42
std::vector< size_t > DownsampleByAngle(const Points &points, const double angle_threshold)
Downsample the points on the path according to the angle.
std::vector< size_t > DownsampleByDistance(const Points &points, int downsampleDistance, int steepTurnDownsampleDistance)
Downsample the points on the path based on distance.
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
std::string BaseMapFile()
get base map file path from flags.
Definition hdmap_util.cc:47
DEFINE_string(output_dir, "/tmp/", "output map directory")
A map tool to generate a downsampled map to be displayed by dreamview frontend.
int main(int32_t argc, char **argv)
DEFINE_double(angle_threshold, 1./180 *M_PI, "Points are sampled when the accumulated direction change " "exceeds the threshold")
DEFINE_int32(downsample_distance, 5, "downsample rate for a normal path")