Apollo代码学习一维查表

interpolation_1d.h

#pragma once

#include <memory>
#include <utility>
#include <vector>

#include "Eigen/Core"
#include "unsupported/Eigen/Splines"

namespace apollo {
namespace control {

class Interpolation1D {
 public:
  typedef std::vector<std::pair<double, double>> DataType;
//构造函数,C++11中,当类中自定义了带参数的构造函数,那么编译器
//就不会再自动生成默认构造函数,但有时候需要创建一个默认的对象但是类中编译器又没有自动生成一个
//默认构造函数,那么为了让编译器生成这个默认构造函数就需要default这个属性
  Interpolation1D() = default;

  // Return true if init is ok.
  bool Init(const DataType& xy);

  // Only interpolate x between [x_min, x_max]
  // For x out of range, start or end y value is returned.
  double Interpolate(double x) const;

 private:
  // Helpers to scale X values down to [0, 1] 不明白为什么非要弄到0-1,Eigen中spline的特性吗
  double ScaledValue(double x) const;

  Eigen::RowVectorXd ScaledValues(Eigen::VectorXd const& x_vec) const;

  double x_min_ = 0.0;
  double x_max_ = 0.0;
  double y_start_ = 0.0;
  double y_end_ = 0.0;

  // Spline of one-dimensional "points."
 //Eigen为c++线性代数库 
 //unique_ptr为智能指针,可以解决内存泄漏,具体见收藏文件夹
  std::unique_ptr<Eigen::Spline<double, 1>> spline_;
};

}  // namespace control
}  // namespace apollo

interpolation_1d.cc

/******************************************************************************
 * Copyright 2017 The Apollo Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/
#include "modules/control/common/interpolation_1d.h"

#include <algorithm>

#include "cyber/common/log.h"

namespace apollo {
namespace control {

const double kDoubleEpsilon = 1e-6;

bool Interpolation1D::Init(const DataType& xy) {
  if (xy.empty()) {
    AERROR << "empty input.";
    return false;
  }
  //拷贝构造
  auto data(xy);
  std::sort(data.begin(), data.end());
  //Eigen::VectorXd:动态长度double型列向量
  Eigen::VectorXd x(data.size());
  Eigen::VectorXd y(data.size());
  //获取插值的上下限
  for (unsigned i = 0; i < data.size(); ++i) {
    x(i) = data[i].first;
    y(i) = data[i].second;
  }
  x_min_ = data.front().first;
  x_max_ = data.back().first;
  y_start_ = data.front().second;
  y_end_ = data.back().second;
//通过输入插值点集合和指定插值曲线次数,进行曲线拟合,有点长 看不懂
  // Spline fitting here. X values are scaled down to [0, 1] for this.
  spline_.reset(new Eigen::Spline<double, 1>(
      Eigen::SplineFitting<Eigen::Spline<double, 1>>::Interpolate(
          y.transpose(),
          // No more than cubic spline, but accept short vectors.
          static_cast<Eigen::DenseIndex>(std::min<size_t>(x.size() - 1, 3)),
          ScaledValues(x))));
  return true;
}

double Interpolation1D::Interpolate(double x) const {
  if (x < x_min_) {
    return y_start_;
  }
  if (x > x_max_) {
    return y_end_;
  }
  // x values need to be scaled down in extraction as well.
  return (*spline_)(ScaledValue(x))(0);
}

double Interpolation1D::ScaledValue(double x) const {
  if (std::fabs(x_max_ - x_min_) < kDoubleEpsilon) {
    return x_min_;
  }
  return (x - x_min_) / (x_max_ - x_min_);
}
//看不懂
Eigen::RowVectorXd Interpolation1D::ScaledValues(
    Eigen::VectorXd const& x_vec) const {
  return x_vec.unaryExpr([this](double x) { return ScaledValue(x); })
      .transpose();
}

}  // namespace control
}  // namespace apollo

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了学习Apollo代码,你可以按照以下步骤进行: 1. 下载Apollo源码并导入到开发工具中。你可以从Apollo的GitHub或者Gitee仓库中获取源码。 2. 学习Apollo的核心概念和工作原理。了解各个模块的职责以及分步执行流程。 3. 阅读Apollo的文档和代码注释。文档中提供了详细的使用说明和示例代码代码注释可以帮助你理解代码的功能和实现细节。 4. 运行Apollo的示例代码Apollo提供了一些示例代码,可以帮助你更好地理解如何使用Apollo来实现配置中心功能。 5. 自己编写测试代码。根据自己的需求和学习进度,可以编写一些测试代码来验证和深入理解Apollo的功能。 6. 练习修改配置和热发布。通过修改配置文件并观察变化,可以更好地理解Apollo的配置管理功能和热发布机制。 请注意,学习Apollo代码需要一定的Java和Spring框架的基础知识。如果你对这些领域不熟悉,建议先学习Java和Spring相关的知识再进行深入的学习。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Apollo学习(超详细)](https://blog.csdn.net/hyzsuccess/article/details/127867287)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值