Apollo 10.0
自动驾驶开放平台
third_party_perception_util.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
24
30namespace apollo {
31namespace third_party_perception {
32
38
39double GetAngleFromQuaternion(const Quaternion quaternion) {
40 double theta = std::atan2(2.0 * (quaternion.qw() * quaternion.qz() +
41 quaternion.qx() * quaternion.qy()),
42 1.0 - 2.0 * (quaternion.qy() * quaternion.qy() +
43 quaternion.qz() * quaternion.qz())) +
44 std::acos(-1.0) / 2.0;
45 return theta;
46}
47
48void FillPerceptionPolygon(PerceptionObstacle* const perception_obstacle,
49 const double mid_x, const double mid_y,
50 const double mid_z, const double length,
51 const double width, const double height,
52 const double heading) {
53 // Generate a 2D cube whose vertices are given in counter-clock order when
54 // viewed from top
55 const int sign_l[4] = {1, 1, -1, -1};
56 const int sign_w[4] = {1, -1, -1, 1};
57 for (int i = 0; i < 4; ++i) {
58 perception_obstacle->add_polygon_point();
59 perception_obstacle->mutable_polygon_point(i)->set_x(
60 mid_x + sign_l[i] * length * std::cos(heading) / 2.0 +
61 sign_w[i] * width * std::sin(heading) / 2.0);
62 perception_obstacle->mutable_polygon_point(i)->set_y(
63 mid_y + sign_l[i] * length * std::sin(heading) / 2.0 -
64 sign_w[i] * width * std::cos(heading) / 2.0);
65 perception_obstacle->mutable_polygon_point(i)->set_z(mid_z);
66 }
67}
68
69double GetDefaultObjectLength(const int object_type) {
70 double default_object_length = 0.0;
71 switch (object_type) {
72 case 0: {
73 default_object_length = FLAGS_default_car_length;
74 break;
75 }
76 case 1: {
77 default_object_length = FLAGS_default_truck_length;
78 break;
79 }
80 case 2: {
81 default_object_length = FLAGS_default_bike_length;
82 break;
83 }
84 case 3: {
85 default_object_length = FLAGS_default_ped_length;
86 break;
87 }
88 case 4: {
89 default_object_length = FLAGS_default_unknown_length;
90 break;
91 }
92 }
93 return default_object_length;
94}
95
96double GetDefaultObjectWidth(const int object_type) {
97 double default_object_width = 0.0;
98 switch (object_type) {
99 case 0: {
100 default_object_width = FLAGS_default_car_width;
101 break;
102 }
103 case 1: {
104 default_object_width = FLAGS_default_truck_width;
105 break;
106 }
107 case 2: {
108 default_object_width = FLAGS_default_bike_width;
109 break;
110 }
111 case 3: {
112 default_object_width = FLAGS_default_ped_width;
113 break;
114 }
115 case 4: {
116 default_object_width = FLAGS_default_unknown_width;
117 break;
118 }
119 }
120 return default_object_width;
121}
122
123Point SLtoXY(const Point& point, const double theta) {
124 return SLtoXY(point.x(), point.y(), theta);
125}
126
127Point SLtoXY(const double x, const double y, const double theta) {
128 Point converted_point;
129 converted_point.set_x(x * std::cos(theta) + y * std::sin(theta));
130 converted_point.set_y(x * std::sin(theta) - y * std::cos(theta));
131 return converted_point;
132}
133
134double Distance(const Point& point1, const Point& point2) {
135 double distance =
136 std::sqrt((point1.x() - point2.x()) * (point1.x() - point2.x()) +
137 (point1.y() - point2.y()) * (point1.y() - point2.y()));
138 return distance;
139}
140
141double GetNearestLaneHeading(const PointENU& point_enu) {
142 auto* hdmap = HDMapUtil::BaseMapPtr();
143 if (hdmap == nullptr) {
144 AERROR << "Failed to get nearest lane for point "
145 << point_enu.DebugString();
146 return -1.0;
147 }
148
149 hdmap::LaneInfoConstPtr nearest_lane;
150
151 double nearest_s;
152 double nearest_l;
153
154 int status =
155 hdmap->GetNearestLane(point_enu, &nearest_lane, &nearest_s, &nearest_l);
156 // TODO(lizh): make it a formal status below
157 if (status != 0) {
158 AERROR << "Failed to get nearest lane for point "
159 << point_enu.DebugString();
160 return -1.0;
161 }
162 double lane_heading = nearest_lane->Heading(nearest_s);
163 return lane_heading;
164}
165
166double GetNearestLaneHeading(const Point& point) {
167 auto* hdmap = HDMapUtil::BaseMapPtr();
168 if (hdmap == nullptr) {
169 AERROR << "Failed to get nearest lane for point " << point.DebugString();
170 return -1.0;
171 }
172
173 PointENU point_enu;
174 point_enu.set_x(point.x());
175 point_enu.set_y(point.y());
176 point_enu.set_z(point.z());
177
178 return GetNearestLaneHeading(point_enu);
179}
180
181double GetNearestLaneHeading(const double x, const double y, const double z) {
182 auto* hdmap = HDMapUtil::BaseMapPtr();
183 if (hdmap == nullptr) {
184 AERROR << "Failed to get nearest lane for point (" << x << ", " << y << ", "
185 << z << ")";
186 return -1.0;
187 }
188
189 PointENU point_enu;
190 point_enu.set_x(x);
191 point_enu.set_y(y);
192 point_enu.set_z(z);
193
194 return GetNearestLaneHeading(point_enu);
195}
196
198 auto* hdmap = HDMapUtil::BaseMapPtr();
199 if (hdmap == nullptr) {
200 AERROR << "Failed to get nearest lane for point " << point.DebugString();
201 return -1.0;
202 }
203
204 PointENU point_enu;
205 point_enu.set_x(point.x());
206 point_enu.set_y(point.y());
207 point_enu.set_z(point.z());
208 hdmap::LaneInfoConstPtr nearest_lane;
209
210 double nearest_s;
211 double nearest_l;
212
213 int status =
214 hdmap->GetNearestLane(point_enu, &nearest_lane, &nearest_s, &nearest_l);
215 // TODO(lizh): make it a formal status below
216 if (status != 0) {
217 AERROR << "Failed to get nearest lane for point " << point.DebugString();
218 return -1.0;
219 }
220 AINFO << "Dist: " << nearest_l;
221 return nearest_l;
222}
223
224double Speed(const Point& point) {
225 return std::sqrt(point.x() * point.x() + point.y() + point.y());
226}
227
228double Speed(const double vx, const double vy) {
229 return std::sqrt(vx * vx + vy * vy);
230}
231
232double HeadingDifference(const double theta1, const double theta2) {
233 double theta_diff = std::abs(theta1 - theta2);
234 return theta_diff > M_PI ? (2 * M_PI - theta_diff) : theta_diff;
235}
236
237} // namespace third_party_perception
238} // namespace apollo
static const HDMap * BaseMapPtr()
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
std::shared_ptr< const LaneInfo > LaneInfoConstPtr
void FillPerceptionPolygon(PerceptionObstacle *const perception_obstacle, const double mid_x, const double mid_y, const double mid_z, const double length, const double width, const double height, const double heading)
double GetDefaultObjectLength(const int object_type)
double GetAngleFromQuaternion(const Quaternion quaternion)
double Distance(const Point &point1, const Point &point2)
double GetLateralDistanceToNearestLane(const Point &point)
Point SLtoXY(const Point &point, const double theta)
double HeadingDifference(const double theta1, const double theta2)
double GetNearestLaneHeading(const PointENU &point_enu)
double GetDefaultObjectWidth(const int object_type)
class register implement
Definition arena_queue.h:37