Apollo 10.0
自动驾驶开放平台
map_xysl.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 "gflags/gflags.h"
18
19#include "cyber/common/file.h"
20#include "cyber/common/log.h"
26#include "modules/common_msgs/map_msgs/map_geometry.pb.h"
27
28DEFINE_bool(xy_to_sl, false, "calculate xy to sl");
29DEFINE_bool(sl_to_xy, false, "calculate sl to xy");
30DEFINE_bool(xy_to_lane, false, "calculate xy to lane");
31DEFINE_bool(lane_to_lane, false, "calculate lane to lane");
32DEFINE_bool(dump_lane_width, false, "dump all lane width info");
33DEFINE_string(dump_txt_map, "", "text file name for dumping map");
34DEFINE_string(dump_bin_map, "", "binary file name for dumping map");
35DEFINE_string(overlap, "", "get overlap information");
36DEFINE_string(signal_info, "", "print signal info");
37DEFINE_double(x, 0.0, "x");
38DEFINE_double(y, 0.0, "y");
39DEFINE_string(lane, "", "lane_id");
40DEFINE_string(from_lane, "", "from_lane");
41DEFINE_string(to_lane, "", "to_lane");
42DEFINE_double(s, 0.0, "s");
43DEFINE_double(l, 0.0, "l");
44
46
47namespace apollo {
48namespace hdmap {
49
50#define QUIT_IF(CONDITION, RET, LEVEL, MSG, ...) \
51 do { \
52 if (CONDITION) { \
53 RAW_LOG(LEVEL, MSG, ##__VA_ARGS__); \
54 return RET; \
55 } \
56 } while (0);
57
58std::ostream &operator<<(
59 std::ostream &os,
60 const ::google::protobuf::RepeatedPtrField<apollo::hdmap::Id> &ids) {
61 for (int i = 0; i < ids.size(); ++i) {
62 os << ids.Get(i).id();
63 if (i != ids.size() - 1) {
64 os << ", ";
65 }
66 }
67 return os;
68}
69
70#define GET_ELEMENT_BY_ID(TYPE) \
71 const TYPE##InfoConstPtr Get##TYPE(const std::string &id) { \
72 auto ret = HDMapUtil::BaseMap().Get##TYPE##ById(MakeMapId(id)); \
73 AERROR_IF(ret == nullptr) \
74 << "failed to find " << #TYPE << " with id: " << id; \
75 return ret; \
76 }
77
78class MapUtil {
79 public:
80 const OverlapInfo *GetOverlap(const std::string &overlap_id) const {
81 auto ret = HDMapUtil::BaseMap().GetOverlapById(MakeMapId(overlap_id));
82 AERROR_IF(ret == nullptr) << "failed to find overlap[" << overlap_id << "]";
83 return ret.get();
84 }
85
94
95 template <class T>
96 void Print(const T &t) {
97 std::cout << t.DebugString();
98 }
99
100 int PointToSL(const PointENU &point, std::string *lane_id, double *s,
101 double *l, double *heading) const {
102 QUIT_IF(lane_id == nullptr, -1, ERROR, "arg lane id is null");
103 QUIT_IF(s == nullptr, -2, ERROR, "arg s is null");
104 QUIT_IF(l == nullptr, -3, ERROR, "arg l is null");
105 LaneInfoConstPtr lane = nullptr;
106 int ret = HDMapUtil::BaseMap().GetNearestLane(point, &lane, s, l);
107 QUIT_IF(ret != 0, -4, ERROR, "get_nearest_lane failed with ret[%d]", ret);
108 QUIT_IF(lane == nullptr, -5, ERROR, "lane is null");
109 *lane_id = lane->id().id();
110 *heading = lane->Heading(*s);
111 return 0;
112 }
113
114 int SLToPoint(LaneInfoConstPtr lane_ptr, const double s, const double l,
115 PointENU *point, double *heading) const {
116 QUIT_IF(point == nullptr, -1, ERROR, "arg point is null");
117 QUIT_IF(heading == nullptr, -2, ERROR, "arg heading is null");
118 QUIT_IF(lane_ptr == nullptr, -3, ERROR, "the provided lane_ptr is null");
119 *point = lane_ptr->GetSmoothPoint(s);
120 *heading = lane_ptr->Heading(s);
121 auto normal_vec =
122 common::math::Vec2d::CreateUnitVec2d(*heading + M_PI / 2.0) * l;
123 point->set_x(point->x() + normal_vec.x());
124 point->set_y(point->y() + normal_vec.y());
125
126 return 0;
127 }
128
130 const std::string &lane_id, double *s, double *l,
131 double *heading) const {
132 QUIT_IF(s == nullptr, -1, ERROR, "arg s is nullptr");
133 const auto lane = HDMapUtil::BaseMap().GetLaneById(MakeMapId(lane_id));
134 QUIT_IF(lane == nullptr, -2, ERROR, "GetSignal_by_id[%s] failed",
135 lane_id.c_str());
136 bool ret = lane->GetProjection(vec2d, s, l);
137 QUIT_IF(!ret, -3, ERROR, "lane[%s] get projection for point[%f, %f] failed",
138 lane_id.c_str(), vec2d.x(), vec2d.y());
139 *heading = lane->Heading(*s);
140 return 0;
141 }
142
143 void PrintOverlap(const std::string &overlap_id) {
144 const auto *overlap_ptr = GetOverlap(FLAGS_overlap);
145 if (overlap_ptr == nullptr) {
146 AERROR << "overlap_ptr is nullptr.";
147 return;
148 }
149 ADEBUG << "overlap[" << overlap_ptr->id().id() << "] info["
150 << overlap_ptr->overlap().DebugString() << "]" << std::endl;
151
152 for (const auto &object_info : overlap_ptr->overlap().object()) {
153 if (object_info.has_lane_overlap_info()) {
154 std::cout << "Lane : " << object_info.id().id() << std::endl;
155 PrintLane(GetLane(object_info.id().id()));
156 } else if (object_info.has_signal_overlap_info()) {
157 std::cout << "Signal: " << object_info.id().id() << std::endl;
158 Print(GetSignal(object_info.id().id())->signal());
159 } else if (object_info.has_stop_sign_overlap_info()) {
160 std::cout << "StopSign: " << object_info.id().id() << std::endl;
161 Print(GetStopSign(object_info.id().id())->stop_sign());
162 } else if (object_info.has_crosswalk_overlap_info()) {
163 std::cout << "Crosswalk: " << object_info.id().id() << std::endl;
164 Print(GetCrosswalk(object_info.id().id())->crosswalk());
165 } else if (object_info.has_junction_overlap_info()) {
166 std::cout << "Junction: " << object_info.id().id() << std::endl;
167 Print(GetJunction(object_info.id().id())->junction());
168 } else if (object_info.has_yield_sign_overlap_info()) {
169 std::cout << "YieldSign: " << object_info.id().id() << std::endl;
170 Print(GetYieldSign(object_info.id().id())->yield_sign());
171 } else if (object_info.has_clear_area_overlap_info()) {
172 std::cout << "ClearArea: " << object_info.id().id() << std::endl;
173 Print(GetClearArea(object_info.id().id())->clear_area());
174 } else if (object_info.has_speed_bump_overlap_info()) {
175 std::cout << "SpeedBump: " << object_info.id().id() << std::endl;
176 Print(GetSpeedBump(object_info.id().id())->speed_bump());
177 } else if (object_info.has_parking_space_overlap_info()) {
178 std::cout << "ParkingSpace: " << object_info.id().id() << std::endl;
179 } else {
180 std::cout << "Unknown overlap type: " << object_info.DebugString();
181 }
182 }
183 }
184
185 void PrintLane(const std::string &lane_id) { PrintLane(GetLane(lane_id)); }
187 const auto &lane = lane_ptr->lane();
188 PointENU start_point;
189 double start_heading = 0.0;
190 SLToPoint(lane_ptr, 0, 0, &start_point, &start_heading);
191
192 PointENU end_point;
193 double end_heading = 0.0;
194 SLToPoint(lane_ptr, lane_ptr->total_length(), 0, &end_point, &end_heading);
195
196 double left_width = 0.0;
197 double right_width = 0.0;
198 lane_ptr->GetWidth(FLAGS_s, &left_width, &right_width);
199
200 std::cout << "lane[" << FLAGS_lane << std::fixed << "] length["
201 << lane_ptr->total_length() << "] type["
202 << Lane_LaneType_Name(lane.type()) << "] turn["
203 << Lane_LaneTurn_Name(lane.turn()) << "] speed_limit["
204 << lane.speed_limit() << "] predecessor[" << lane.predecessor_id()
205 << "] successor[" << lane.successor_id() << "] left_forward["
206 << lane.left_neighbor_forward_lane_id() << "] right_forward["
207 << lane.right_neighbor_forward_lane_id() << "] left_reverse["
208 << lane.left_neighbor_reverse_lane_id() << "] right_reverse["
209 << lane.right_neighbor_reverse_lane_id() << "], "
210 << "Left Boundary: [ virtual?:" << std::boolalpha
211 << lane.left_boundary().virtual_() << ", Type: [";
212 for (const auto &boundary_type : lane.left_boundary().boundary_type()) {
213 std::cout << "s: " << boundary_type.s() << "->";
214 for (const auto t : boundary_type.types()) {
215 std::cout << LaneBoundaryType::Type_Name(
216 static_cast<LaneBoundaryType::Type>(t))
217 << ", ";
218 }
219 }
220
221 std::cout << "]; Right Boundary: [ virtual?:" << std::boolalpha
222 << lane.right_boundary().virtual_() << ", Type: ";
223 for (const auto &boundary_type : lane.left_boundary().boundary_type()) {
224 std::cout << "s: " << boundary_type.s() << "->";
225 for (const auto t : boundary_type.types()) {
226 std::cout << LaneBoundaryType::Type_Name(
227 static_cast<LaneBoundaryType::Type>(t))
228 << ", ";
229 }
230 }
231 std::cout << "] overlap[" << lane.overlap_id() << "];"
232 << " start point(x,y,heading):" << start_point.x() << ","
233 << start_point.y() << "," << start_heading
234 << " end point(x,y,heading):" << end_point.x() << ","
235 << end_point.y() << "," << end_heading
236 << " left_width:" << left_width << " right_width:" << right_width
237 << std::endl;
238 std::cout.unsetf(std::ios_base::fixed);
239
240 if (FLAGS_dump_lane_width) {
241 const auto sample_left_widthes = lane_ptr->sampled_left_width();
242 std::cout << "left width num: " << sample_left_widthes.size()
243 << std::endl;
244 int num = 0;
245 for (auto w : sample_left_widthes) {
246 std::cout << " " << w.second;
247 if (++num % 10 == 0) {
248 std::cout << std::endl;
249 }
250 }
251 std::cout << std::endl;
252 num = 0;
253 const auto sample_right_widthes = lane_ptr->sampled_right_width();
254 std::cout << "right width num: " << sample_right_widthes.size()
255 << std::endl;
256 for (auto w : sample_right_widthes) {
257 std::cout << " " << w.second;
258 if (++num % 10 == 0) {
259 std::cout << std::endl;
260 }
261 }
262 std::cout << std::endl;
263 }
264 }
265}; // namespace hdmap
266
267} // namespace hdmap
268} // namespace apollo
269
270int main(int argc, char *argv[]) {
271 google::InitGoogleLogging(argv[0]);
272 google::ParseCommandLineFlags(&argc, &argv, true);
273 const std::string map_file = apollo::hdmap::BaseMapFile();
274 bool valid_arg = false;
275
276 apollo::hdmap::MapUtil map_util;
277
278 if (FLAGS_xy_to_sl) {
279 double x = FLAGS_x;
280 double y = FLAGS_y;
281 PointENU point;
282 point.set_x(x);
283 point.set_y(y);
284 point.set_z(0);
285 std::string lane_id;
286 double s = 0.0;
287 double l = 0.0;
288 double heading = 0.0;
289 map_util.PointToSL(point, &lane_id, &s, &l, &heading);
290 printf("lane_id[%s], s[%f], l[%f], heading[%f]\n", lane_id.c_str(), s, l,
291 heading);
292 map_util.PrintLane(lane_id);
293 valid_arg = true;
294 }
295 if (FLAGS_sl_to_xy) {
296 PointENU point;
297 double heading = 0.0;
298 map_util.SLToPoint(map_util.GetLane(FLAGS_lane), FLAGS_s, FLAGS_l, &point,
299 &heading);
300 printf("x[%f] y[%f], heading[%f]\n", point.x(), point.y(), heading);
301 map_util.PrintLane(FLAGS_lane);
302 valid_arg = true;
303 }
304 if (FLAGS_xy_to_lane) {
305 double s = 0.0;
306 double l = 0.0;
307 double heading = 0.0;
308 int ret = map_util.LaneProjection({FLAGS_x, FLAGS_y}, FLAGS_lane, &s, &l,
309 &heading);
310 if (ret != 0) {
311 printf("lane_projection for x[%f], y[%f], lane_id[%s] failed\n", FLAGS_x,
312 FLAGS_y, FLAGS_lane.c_str());
313 return -1;
314 }
315 printf("lane[%s] s[%f], l[%f], heading[%f]\n", FLAGS_lane.c_str(), s, l,
316 heading);
317 map_util.PrintLane(FLAGS_lane);
318 valid_arg = true;
319 }
320 if (FLAGS_lane_to_lane) {
321 PointENU point;
322 double src_heading = 0.0;
323 map_util.SLToPoint(map_util.GetLane(FLAGS_from_lane), FLAGS_s, 0.0, &point,
324 &src_heading);
325 double target_s = 0.0;
326 double target_l = 0.0;
327 double target_heading = 0.0;
328 int ret = map_util.LaneProjection({point.x(), point.y()}, FLAGS_to_lane,
329 &target_s, &target_l, &target_heading);
330 if (ret != 0) {
331 printf("lane_projection for lane[%s], s[%f] to lane_id[%s] failed\n",
332 FLAGS_from_lane.c_str(), FLAGS_s, FLAGS_to_lane.c_str());
333 return -1;
334 }
335 printf("lane[%s] s[%f], l[%f], heading[%f]\n", FLAGS_to_lane.c_str(),
336 target_s, target_l, target_heading);
337 map_util.PrintLane(FLAGS_from_lane);
338 map_util.PrintLane(FLAGS_to_lane);
339 valid_arg = true;
340 }
341 if (!FLAGS_lane.empty()) {
342 const auto lane_ptr = map_util.GetLane(FLAGS_lane);
343 if (!lane_ptr) {
344 std::cout << "Could not find lane " << FLAGS_lane << " on map "
345 << map_file;
346 return 0;
347 }
348 map_util.PrintLane(lane_ptr);
349 valid_arg = true;
350 }
351 if (!FLAGS_overlap.empty()) {
352 map_util.PrintOverlap(FLAGS_overlap);
353 valid_arg = true;
354 }
355 if (!FLAGS_signal_info.empty()) {
356 std::cout << "Signal: " << FLAGS_signal_info << std::endl;
357 map_util.Print(map_util.GetSignal(FLAGS_signal_info)->signal());
358 valid_arg = true;
359 }
360 if (!FLAGS_dump_txt_map.empty()) {
363 ACHECK(apollo::cyber::common::SetProtoToASCIIFile(map, FLAGS_dump_txt_map));
364 valid_arg = true;
365 }
366 if (!FLAGS_dump_bin_map.empty()) {
369 ACHECK(
370 apollo::cyber::common::SetProtoToBinaryFile(map, FLAGS_dump_bin_map));
371 valid_arg = true;
372 }
373 if (!valid_arg) {
374 std::cout << "usage: --map_dir map/file/directory/" << std::endl;
375 std::cout << "usage: --base_map_filename map_file_name" << std::endl;
376 std::cout << "usage: --dump_txt_map text_map_file" << std::endl;
377 std::cout << "usage: --dump_bin_map bin_map_file" << std::endl;
378 std::cout << "usage: --xy_to_sl --x x --y y" << std::endl;
379 std::cout << "usage: --sl_to_xy --lane lane_id --s s --l l" << std::endl;
380 std::cout << "usage: --xy_to_lane --x --y --lane" << std::endl;
381 std::cout
382 << "usage: --lane_to_lane --from_lane lane_id --s s --to_lane lane_id"
383 << std::endl;
384 std::cout << "usage: --lane lane_id" << std::endl;
385 std::cout << "usage: --signal_info signal_id" << std::endl;
386 std::cout << "usage: --overlap overlap_id" << std::endl;
387 }
388 return 0;
389}
Implements a class of 2-dimensional vectors.
Definition vec2d.h:42
static Vec2d CreateUnitVec2d(const double angle)
Creates a unit-vector with a given angle to the positive x semi-axis
Definition vec2d.cc:29
double y() const
Getter for y component
Definition vec2d.h:57
double x() const
Getter for x component
Definition vec2d.h:54
static const HDMap & BaseMap()
LaneInfoConstPtr GetLaneById(const Id &id) const
Definition hdmap.cc:34
int GetNearestLane(const apollo::common::PointENU &point, LaneInfoConstPtr *nearest_lane, double *nearest_s, double *nearest_l) const
get nearest lane from target point,
Definition hdmap.cc:170
OverlapInfoConstPtr GetOverlapById(const Id &id) const
Definition hdmap.cc:74
int SLToPoint(LaneInfoConstPtr lane_ptr, const double s, const double l, PointENU *point, double *heading) const
Definition map_xysl.cc:114
GET_ELEMENT_BY_ID(YieldSign)
const OverlapInfo * GetOverlap(const std::string &overlap_id) const
Definition map_xysl.cc:80
void PrintLane(LaneInfoConstPtr lane_ptr)
Definition map_xysl.cc:186
void Print(const T &t)
Definition map_xysl.cc:96
GET_ELEMENT_BY_ID(SpeedBump)
GET_ELEMENT_BY_ID(ClearArea)
void PrintLane(const std::string &lane_id)
Definition map_xysl.cc:185
GET_ELEMENT_BY_ID(Crosswalk)
void PrintOverlap(const std::string &overlap_id)
Definition map_xysl.cc:143
int LaneProjection(const apollo::common::math::Vec2d &vec2d, const std::string &lane_id, double *s, double *l, double *heading) const
Definition map_xysl.cc:129
int PointToSL(const PointENU &point, std::string *lane_id, double *s, double *l, double *heading) const
Definition map_xysl.cc:100
#define ACHECK(cond)
Definition log.h:80
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AERROR_IF(cond)
Definition log.h:74
int main(int argc, char *argv[])
Definition map_xysl.cc:270
DEFINE_double(x, 0.0, "x")
DEFINE_string(dump_txt_map, "", "text file name for dumping map")
#define QUIT_IF(CONDITION, RET, LEVEL, MSG,...)
Definition map_xysl.cc:50
DEFINE_bool(xy_to_sl, false, "calculate xy to sl")
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
bool SetProtoToBinaryFile(const google::protobuf::Message &message, const std::string &file_name)
Sets the content of the file specified by the file_name to be the binary representation of the input ...
Definition file.cc:111
bool SetProtoToASCIIFile(const google::protobuf::Message &message, int file_descriptor)
Definition file.cc:43
apollo::hdmap::Id MakeMapId(const std::string &id)
create a Map ID given a string.
Definition hdmap_util.h:85
std::shared_ptr< const LaneInfo > LaneInfoConstPtr
std::string BaseMapFile()
get base map file path from flags.
Definition hdmap_util.cc:47
std::ostream & operator<<(std::ostream &os, const ::google::protobuf::RepeatedPtrField< apollo::hdmap::Id > &ids)
Definition map_xysl.cc:58
class register implement
Definition arena_queue.h:37
Some string util functions.