Apollo 11.0
自动驾驶开放平台
spline_2d.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
22
23#include <algorithm>
24
25namespace apollo {
26namespace planning {
27
28Spline2d::Spline2d(const std::vector<double>& t_knots, const uint32_t order)
29 : t_knots_(t_knots), spline_order_(order) {
30 if (t_knots.size() > 1) {
31 for (uint32_t i = 1; i < t_knots_.size(); ++i) {
32 splines_.emplace_back(spline_order_);
33 }
34 }
35}
36
37std::pair<double, double> Spline2d::operator()(const double t) const {
38 if (splines_.empty()) {
39 return std::make_pair(0.0, 0.0);
40 }
41 uint32_t index = find_index(t);
42 return splines_[index](t - t_knots_[index]);
43}
44
45double Spline2d::x(const double t) const {
46 if (splines_.empty()) {
47 return 0.0;
48 }
49 uint32_t index = find_index(t);
50 return splines_[index].x(t - t_knots_[index]);
51}
52
53double Spline2d::y(const double t) const {
54 if (splines_.empty()) {
55 return 0.0;
56 }
57 uint32_t index = find_index(t);
58 return splines_[index].y(t - t_knots_[index]);
59}
60
61double Spline2d::DerivativeX(const double t) const {
62 // zero order spline
63 if (splines_.empty()) {
64 return 0.0;
65 }
66 uint32_t index = find_index(t);
67 return splines_[index].DerivativeX(t - t_knots_[index]);
68}
69
70double Spline2d::DerivativeY(const double t) const {
71 // zero order spline
72 if (splines_.empty()) {
73 return 0.0;
74 }
75 uint32_t index = find_index(t);
76 return splines_[index].DerivativeY(t - t_knots_[index]);
77}
78
79double Spline2d::SecondDerivativeX(const double t) const {
80 if (splines_.empty()) {
81 return 0.0;
82 }
83 uint32_t index = find_index(t);
84 return splines_[index].SecondDerivativeX(t - t_knots_[index]);
85}
86
87double Spline2d::SecondDerivativeY(const double t) const {
88 if (splines_.empty()) {
89 return 0.0;
90 }
91 uint32_t index = find_index(t);
92 return splines_[index].SecondDerivativeY(t - t_knots_[index]);
93}
94
95double Spline2d::ThirdDerivativeX(const double t) const {
96 if (splines_.empty()) {
97 return 0.0;
98 }
99 uint32_t index = find_index(t);
100 return splines_[index].ThirdDerivativeX(t - t_knots_[index]);
101}
102
103double Spline2d::ThirdDerivativeY(const double t) const {
104 if (splines_.empty()) {
105 return 0.0;
106 }
107 uint32_t index = find_index(t);
108 return splines_[index].ThirdDerivativeY(t - t_knots_[index]);
109}
113bool Spline2d::set_splines(const Eigen::MatrixXd& params,
114 const uint32_t order) {
115 const uint32_t num_params = order + 1;
116 // check if the parameter size fit
117 if (2 * t_knots_.size() * num_params !=
118 2 * num_params + static_cast<uint32_t>(params.rows())) {
119 return false;
120 }
121 for (uint32_t i = 0; i < splines_.size(); ++i) {
122 std::vector<double> spline_piece_x(num_params, 0.0);
123 std::vector<double> spline_piece_y(num_params, 0.0);
124 for (uint32_t j = 0; j < num_params; ++j) {
125 spline_piece_x[j] = params(2 * i * num_params + j, 0);
126 spline_piece_y[j] = params((2 * i + 1) * num_params + j, 0);
127 }
128 splines_[i].SetParams(spline_piece_x, spline_piece_y);
129 }
130 spline_order_ = order;
131 return true;
132}
133
134const Spline2dSeg& Spline2d::smoothing_spline(const uint32_t index) const {
135 return splines_[index];
136}
137
138const std::vector<double>& Spline2d::t_knots() const { return t_knots_; }
139
140uint32_t Spline2d::spline_order() const { return spline_order_; }
141
142uint32_t Spline2d::find_index(const double t) const {
143 auto upper_bound = std::upper_bound(t_knots_.begin() + 1, t_knots_.end(), t);
144 return std::min(static_cast<uint32_t>(t_knots_.size() - 1),
145 static_cast<uint32_t>(upper_bound - t_knots_.begin())) -
146 1;
147}
148
149} // namespace planning
150} // namespace apollo
double ThirdDerivativeX(const double t) const
Definition spline_2d.cc:95
double DerivativeX(const double t) const
Definition spline_2d.cc:61
double DerivativeY(const double t) const
Definition spline_2d.cc:70
const std::vector< double > & t_knots() const
Definition spline_2d.cc:138
double x(const double t) const
Definition spline_2d.cc:45
bool set_splines(const Eigen::MatrixXd &params, const uint32_t order)
: set splines
Definition spline_2d.cc:113
double SecondDerivativeX(const double t) const
Definition spline_2d.cc:79
Spline2d(const std::vector< double > &t_knots, const uint32_t order)
Definition spline_2d.cc:28
const Spline2dSeg & smoothing_spline(const uint32_t index) const
Definition spline_2d.cc:134
std::pair< double, double > operator()(const double t) const
Definition spline_2d.cc:37
double y(const double t) const
Definition spline_2d.cc:53
double SecondDerivativeY(const double t) const
Definition spline_2d.cc:87
double ThirdDerivativeY(const double t) const
Definition spline_2d.cc:103
uint32_t spline_order() const
Definition spline_2d.cc:140
Planning module main class.
class register implement
Definition arena_queue.h:37
: piecewise smoothing spline 2d class