37 const uint32_t spline_order)
38 : x_knots_(x_knots), spline_order_(spline_order) {
40 (
static_cast<uint32_t
>(x_knots.size()) > 1
41 ? (
static_cast<uint32_t
>(x_knots.size()) - 1) * (1 + spline_order_)
43 kernel_matrix_ = Eigen::MatrixXd::Zero(total_params_, total_params_);
44 offset_ = Eigen::MatrixXd::Zero(total_params_, 1);
48 Eigen::MatrixXd id_matrix =
49 Eigen::MatrixXd::Identity(kernel_matrix_.rows(), kernel_matrix_.cols());
50 kernel_matrix_ += 2.0 * id_matrix * regularized_param;
54 const Eigen::MatrixXd& offset,
55 const double weight) {
56 if (kernel.rows() != kernel.cols() ||
57 kernel.rows() != kernel_matrix_.rows() ||
offset.cols() != 1 ||
58 offset.rows() != offset_.rows()) {
61 kernel_matrix_ += kernel * weight;
62 offset_ +=
offset * weight;
67 const double weight) {
68 Eigen::MatrixXd
offset = Eigen::MatrixXd::Zero(kernel.rows(), 1);
73 return &kernel_matrix_;
79 return kernel_matrix_;
85void Spline1dKernel::AddNthDerivativekernelMatrix(
const uint32_t n,
86 const double weight) {
87 const uint32_t num_params = spline_order_ + 1;
88 for (uint32_t i = 0; i + 1 < x_knots_.size(); ++i) {
89 Eigen::MatrixXd cur_kernel =
91 SplineSegKernel::Instance()->NthDerivativeKernel(
92 n, num_params, x_knots_[i + 1] - x_knots_[i]) *
94 kernel_matrix_.block(i * num_params, i * num_params, num_params,
95 num_params) += cur_kernel;
100 AddNthDerivativekernelMatrix(1, weight);
104 AddNthDerivativekernelMatrix(2, weight);
108 AddNthDerivativekernelMatrix(3, weight);
111void Spline1dKernel::AddNthDerivativekernelMatrixForSplineK(
112 const uint32_t n,
const uint32_t k,
const double weight) {
113 if (k + 1 >= x_knots_.size()) {
114 AERROR <<
"Cannot add NthDerivativeKernel for spline K because k is out of "
119 const uint32_t num_params = spline_order_ + 1;
120 Eigen::MatrixXd cur_kernel =
122 SplineSegKernel::Instance()->NthDerivativeKernel(
123 n, num_params, x_knots_[k + 1] - x_knots_[k]) *
125 kernel_matrix_.block(k * num_params, k * num_params, num_params,
126 num_params) += cur_kernel;
130 const double weight) {
131 AddNthDerivativekernelMatrixForSplineK(1, k, weight);
135 const uint32_t k,
const double weight) {
136 AddNthDerivativekernelMatrixForSplineK(2, k, weight);
140 const uint32_t k,
const double weight) {
141 AddNthDerivativekernelMatrixForSplineK(3, k, weight);
145 const std::vector<double>& x_coord,
const std::vector<double>& ref_x,
146 const double weight) {
147 if (ref_x.size() != x_coord.size()) {
151 const uint32_t num_params = spline_order_ + 1;
152 for (
size_t i = 0; i < x_coord.size(); ++i) {
153 auto cur_index = FindIndex(x_coord[i]);
154 double cur_rel_x = x_coord[i] - x_knots_[cur_index];
156 double offset_coef = -2.0 * ref_x[i] * weight;
157 for (
size_t j = 0; j < num_params; ++j) {
158 offset_(j + cur_index * num_params, 0) += offset_coef;
159 offset_coef *= cur_rel_x;
162 Eigen::MatrixXd ref_kernel(num_params, num_params);
165 std::vector<double> power_x;
166 for (uint32_t n = 0; n + 1 < 2 * num_params; ++n) {
167 power_x.emplace_back(cur_x);
171 for (uint32_t r = 0; r < num_params; ++r) {
172 for (uint32_t c = 0; c < num_params; ++c) {
173 ref_kernel(r, c) = 2.0 * power_x[r + c];
177 kernel_matrix_.block(cur_index * num_params, cur_index * num_params,
178 num_params, num_params) += weight * ref_kernel;
183uint32_t Spline1dKernel::FindIndex(
const double x)
const {
184 auto upper_bound = std::upper_bound(x_knots_.begin() + 1, x_knots_.end(), x);
185 return std::min(
static_cast<uint32_t
>(x_knots_.size() - 1),
186 static_cast<uint32_t
>(upper_bound - x_knots_.begin())) -
191 for (uint32_t i = 1; i < x_knots_.size(); ++i) {
192 const double cur_x = x_knots_[i] - x_knots_[i - 1];
193 double pw_x = 2.0 * weight;
194 for (uint32_t j = 0; j < spline_order_ + 1; ++j) {
195 offset_((i - 1) * (spline_order_ + 1) + j, 0) -= pw_x;
Spline1dKernel(const Spline1d &spline1d)
Eigen::MatrixXd * mutable_offset()
void AddSecondOrderDerivativeMatrix(const double weight)
void AddDerivativeKernelMatrixForSplineK(const uint32_t k, const double weight)
void AddDistanceOffset(const double weight)
void AddRegularization(const double regularized_param)
const Eigen::MatrixXd & kernel_matrix() const
const Eigen::MatrixXd & offset() const
void AddThirdOrderDerivativeMatrix(const double weight)
void AddThirdOrderDerivativeMatrixForSplineK(const uint32_t k, const double weight)
Eigen::MatrixXd * mutable_kernel_matrix()
void AddDerivativeKernelMatrix(const double weight)
void AddSecondOrderDerivativeMatrixForSplineK(const uint32_t k, const double weight)
bool AddKernel(const Eigen::MatrixXd &kernel, const Eigen::MatrixXd &offset, const double weight)
bool AddReferenceLineKernelMatrix(const std::vector< double > &x_coord, const std::vector< double > &ref_fx, const double weight)
Planning module main class.
: wrap up solver constraint interface with direct methods and preset methods
: generating integrated kernels for smoothing spline