Apollo 10.0
自动驾驶开放平台
path_matcher.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 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
22
23#include <algorithm>
24#include <cmath>
25#include <vector>
26
27#include "glog/logging.h"
28
30
31namespace apollo {
32namespace common {
33namespace math {
34
35PathPoint PathMatcher::MatchToPath(const std::vector<PathPoint>& reference_line,
36 const double x, const double y) {
37 CHECK_GT(reference_line.size(), 0U);
38
39 auto func_distance_square = [](const PathPoint& point, const double x,
40 const double y) {
41 double dx = point.x() - x;
42 double dy = point.y() - y;
43 return dx * dx + dy * dy;
44 };
45
46 double distance_min = func_distance_square(reference_line.front(), x, y);
47 std::size_t index_min = 0;
48
49 for (std::size_t i = 1; i < reference_line.size(); ++i) {
50 double distance_temp = func_distance_square(reference_line[i], x, y);
51 if (distance_temp < distance_min) {
52 distance_min = distance_temp;
53 index_min = i;
54 }
55 }
56
57 std::size_t index_start = (index_min == 0) ? index_min : index_min - 1;
58 std::size_t index_end =
59 (index_min + 1 == reference_line.size()) ? index_min : index_min + 1;
60
61 if (index_start == index_end) {
62 return reference_line[index_start];
63 }
64
65 return FindProjectionPoint(reference_line[index_start],
66 reference_line[index_end], x, y);
67}
68
69std::pair<double, double> PathMatcher::GetPathFrenetCoordinate(
70 const std::vector<PathPoint>& reference_line, const double x,
71 const double y) {
72 auto matched_path_point = MatchToPath(reference_line, x, y);
73 double rtheta = matched_path_point.theta();
74 double rx = matched_path_point.x();
75 double ry = matched_path_point.y();
76 double delta_x = x - rx;
77 double delta_y = y - ry;
78 double side = std::cos(rtheta) * delta_y - std::sin(rtheta) * delta_x;
79 std::pair<double, double> relative_coordinate;
80 relative_coordinate.first = matched_path_point.s();
81 relative_coordinate.second =
82 std::copysign(std::hypot(delta_x, delta_y), side);
83 return relative_coordinate;
84}
85
86PathPoint PathMatcher::MatchToPath(const std::vector<PathPoint>& reference_line,
87 const double s) {
88 auto comp = [](const PathPoint& point, const double s) {
89 return point.s() < s;
90 };
91
92 auto it_lower =
93 std::lower_bound(reference_line.begin(), reference_line.end(), s, comp);
94 if (it_lower == reference_line.begin()) {
95 return reference_line.front();
96 } else if (it_lower == reference_line.end()) {
97 return reference_line.back();
98 }
99
100 // interpolate between it_lower - 1 and it_lower
101 // return interpolate(*(it_lower - 1), *it_lower, s);
102 return InterpolateUsingLinearApproximation(*(it_lower - 1), *it_lower, s);
103}
104
105PathPoint PathMatcher::FindProjectionPoint(const PathPoint& p0,
106 const PathPoint& p1, const double x,
107 const double y) {
108 double v0x = x - p0.x();
109 double v0y = y - p0.y();
110
111 double v1x = p1.x() - p0.x();
112 double v1y = p1.y() - p0.y();
113
114 double v1_norm = std::sqrt(v1x * v1x + v1y * v1y);
115 double dot = v0x * v1x + v0y * v1y;
116
117 double delta_s = dot / v1_norm;
118 return InterpolateUsingLinearApproximation(p0, p1, p0.s() + delta_s);
119}
120
121} // namespace math
122} // namespace common
123} // namespace apollo
static std::pair< double, double > GetPathFrenetCoordinate(const std::vector< PathPoint > &reference_line, const double x, const double y)
static PathPoint MatchToPath(const std::vector< PathPoint > &reference_line, const double x, const double y)
Linear interpolation functions.
SLPoint InterpolateUsingLinearApproximation(const SLPoint &p0, const SLPoint &p1, const double w)
class register implement
Definition arena_queue.h:37