Apollo 11.0
自动驾驶开放平台
spline_1d.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
23
24namespace apollo {
25namespace planning {
26
27Spline1d::Spline1d(const std::vector<double>& x_knots, const uint32_t order)
28 : x_knots_(x_knots), spline_order_(order) {
29 for (uint32_t i = 1; i < x_knots_.size(); ++i) {
30 splines_.emplace_back(spline_order_);
31 }
32}
33
34double Spline1d::operator()(const double x) const {
35 if (splines_.empty()) {
36 return 0.0;
37 }
38 uint32_t index = FindIndex(x);
39 return splines_[index](x - x_knots_[index]);
40}
41
42double Spline1d::Derivative(const double x) const {
43 // zero order spline
44 if (splines_.empty()) {
45 return 0.0;
46 }
47 uint32_t index = FindIndex(x);
48 return splines_[index].Derivative(x - x_knots_[index]);
49}
50
51double Spline1d::SecondOrderDerivative(const double x) const {
52 if (splines_.empty()) {
53 return 0.0;
54 }
55 uint32_t index = FindIndex(x);
56 return splines_[index].SecondOrderDerivative(x - x_knots_[index]);
57}
58
59double Spline1d::ThirdOrderDerivative(const double x) const {
60 if (splines_.empty()) {
61 return 0.0;
62 }
63 uint32_t index = FindIndex(x);
64 return splines_[index].ThirdOrderDerivative(x - x_knots_[index]);
65}
66
67bool Spline1d::SetSplineSegs(const Eigen::MatrixXd& param_matrix,
68 const uint32_t order) {
69 const uint32_t num_params = order + 1;
70 // check if the parameter size fit
71 if (x_knots_.size() * num_params !=
72 num_params + static_cast<uint32_t>(param_matrix.rows())) {
73 return false;
74 }
75 for (uint32_t i = 0; i < splines_.size(); ++i) {
76 std::vector<double> spline_piece(num_params, 0.0);
77 for (uint32_t j = 0; j < num_params; ++j) {
78 spline_piece[j] = param_matrix(i * num_params + j, 0);
79 }
80 splines_[i].SetParams(spline_piece);
81 }
82 spline_order_ = order;
83 return true;
84}
85
86const std::vector<double>& Spline1d::x_knots() const { return x_knots_; }
87
88uint32_t Spline1d::spline_order() const { return spline_order_; }
89
90const std::vector<Spline1dSeg>& Spline1d::splines() const { return splines_; }
91
92uint32_t Spline1d::FindIndex(const double x) const {
93 auto upper_bound = std::upper_bound(x_knots_.begin() + 1, x_knots_.end(), x);
94 const uint32_t dis =
95 static_cast<uint32_t>(std::distance(x_knots_.begin(), upper_bound));
96 if (dis < x_knots_.size()) {
97 return dis - 1;
98 } else {
99 return static_cast<uint32_t>(x_knots_.size()) - 2;
100 }
101}
102
103} // namespace planning
104} // namespace apollo
double operator()(const double x) const
Definition spline_1d.cc:34
Spline1d(const std::vector< double > &x_knots, const uint32_t order)
Definition spline_1d.cc:27
double SecondOrderDerivative(const double x) const
Definition spline_1d.cc:51
const std::vector< Spline1dSeg > & splines() const
Definition spline_1d.cc:90
uint32_t spline_order() const
Definition spline_1d.cc:88
double Derivative(const double x) const
Definition spline_1d.cc:42
const std::vector< double > & x_knots() const
Definition spline_1d.cc:86
double ThirdOrderDerivative(const double x) const
Definition spline_1d.cc:59
bool SetSplineSegs(const Eigen::MatrixXd &param_matrix, const uint32_t order)
Definition spline_1d.cc:67
Planning module main class.
class register implement
Definition arena_queue.h:37
: piecewise smoothing spline class