数据结构迭代器的实现示例

news/2024/7/7 20:43:52

系列文章目录

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
TODO:写完再整理

文章目录

  • 系列文章目录
  • 前言
  • 速度迭代器示例
    • 1、速度空间迭代器的实现
    • 2、【使用示例】使用迭代器生成速度采样空间
  • 轨迹迭代器


前言

认知有限,望大家多多包涵,有什么问题也希望能够与大家多交流,共同成长!

本文先对数据结构迭代器的实现示例做个简单的介绍,具体内容后续再更,其他模块可以参考去我其他文章


提示:以下是本篇文章正文内容

速度迭代器示例

1、速度空间迭代器的实现

输入速度采样空间的最大值、最小值和分辨率,得到一系列的采样空间,并一共设置、查询、初始化、复位等操作


#ifndef DWA_LOCAL_PLANNER_VELOCITY_ITERATOR_H_
#define DWA_LOCAL_PLANNER_VELOCITY_ITERATOR_H_
#include <algorithm>
#include <cmath>
#include <vector>

/**
 * We use the class to get even sized samples between min and max, including
 * zero if it is not included (and range goes from negative to positive
 */
class VelocityIterator {
   public:
    VelocityIterator(double min, double max, int num_samples)
        : current_index(0) {
        if (min == max) {
            samples_.push_back(min);
        } else {
            // 如果速度边界不同,至少有两个采样维度
            num_samples = std::max(2, num_samples);

            // 速度采样的单位角度e.g. for 4 samples, split distance in 3 even
            // parts
            double step_size =
                (max - min) / double(std::max(1, (num_samples - 1)));

            // 我们确保避免最小值和最大值之间的舍入误差。
            double current;
            double next = min;
            for (int j = 0; j < num_samples - 1; ++j) {
                current = next;
                next += step_size;
                samples_.push_back(current);
                // if 0 is among samples, this is never true. Else it inserts a
                // 0 between the positive and negative samples
                if ((current < 0) && (next > 0)) {
                    samples_.push_back(0.0);
                }
            }
            samples_.push_back(max);
        }
    }

    double getVelocity() { return samples_.at(current_index); }

    VelocityIterator& operator++(int) {
        current_index++;
        return *this;
    }

    void reset() { current_index = 0; }

    bool isFinished() { return current_index >= samples_.size(); }

   private:
    std::vector<double> samples_;  // 储存各采样角度序列
    unsigned int current_index;
};
#endif

2、【使用示例】使用迭代器生成速度采样空间

    std::vector<Eigen::Vector3f> sample_params_;
     VelocityIterator x_it(min_vel[0], max_vel[0], vsamples[0]);
     VelocityIterator y_it(min_vel[1], max_vel[1], vsamples[1]);
     VelocityIterator th_it(min_vel[2], max_vel[2], vsamples[2]);
     Eigen::Vector3f vel_samp = Eigen::Vector3f::Zero();
     // 把分开三个维度采样的整合到一个整个速度采样空间sample_params中
     for (; !x_it.isFinished(); x_it++) {
         vel_samp[0] = x_it.getVelocity();
         for (; !y_it.isFinished(); y_it++) {
             vel_samp[1] = y_it.getVelocity();
             for (; !th_it.isFinished(); th_it++) {
                 vel_samp[2] = th_it.getVelocity();
                 // MCU_LOG("Sample %f, %f, %f", vel_samp[0], vel_samp[1],
                 //         vel_samp[2]);
                 sample_params_.push_back(vel_samp);
             }
             th_it.reset();
         }
         y_it.reset();
     }

轨迹迭代器

#ifndef TRAJECTORY_ROLLOUT_TRAJECTORY_H_
#define TRAJECTORY_ROLLOUT_TRAJECTORY_H_

#include <vector>

/**
 * @class Trajectory
 * @brief x速度、y速度和角速度生成的轨迹
 * 轨迹既有位姿信息,也有速度信息,还有代价信息
 */
class Trajectory {
   public:
    Trajectory();

    Trajectory(double xv, double yv, double thetav, double time_delta,
               unsigned int num_pts);

    double xv_, yv_, thetav_;  // 该pose轨迹的速度信息

    double cost_;  // The cost/score of the trajectory

    double time_delta_;  // The time gap between points

    /**
     * @brief  Get a point within the trajectory
     * @param index The index of the point to get
     * @param x Will be set to the x position of the point
     * @param y Will be set to the y position of the point
     * @param th Will be set to the theta position of the point
     */
    void getPoint(unsigned int index, double& x, double& y, double& th) const;

    /**
     * @brief  Set a point within the trajectory
     * @param index The index of the point to set
     * @param x The x position
     * @param y The y position
     * @param th The theta position
     */
    void setPoint(unsigned int index, double x, double y, double th);

    /**
     * @brief  Add a point to the end of a trajectory
     * @param x The x position
     * @param y The y position
     * @param th The theta position
     */
    void addPoint(double x, double y, double th);

    /**
     * @brief  Get the last point of the trajectory
     * @param x Will be set to the x position of the point
     * @param y Will be set to the y position of the point
     * @param th Will be set to the theta position of the point
     */
    void getEndpoint(double& x, double& y, double& th) const;

    /**
     * @brief  Clear the trajectory's points
     */
    void resetPoints();

    /**
     * @brief  Return the number of points in the trajectory
     * @return The number of points in the trajectory
     */
    unsigned int getPointsSize() const;

   private:
    std::vector<double> x_pts_;  ///< @brief The x points in the
                                 ///< trajectory,该轨迹中所有点的x坐标
    std::vector<double> y_pts_;  ///< @brief The y points in the
                                 ///< trajectory,该轨迹中所有点的y坐标
    std::vector<double> th_pts_;  ///< @brief The theta points in the
                                  ///< trajectory,该轨迹中所有点的朝向th
};
#endif


#include <trajectory.h>

Trajectory::Trajectory() : xv_(0.0), yv_(0.0), thetav_(0.0), cost_(-1.0) {}

Trajectory::Trajectory(double xv, double yv, double thetav, double time_delta,
                       unsigned int num_pts)
    : xv_(xv),
      yv_(yv),
      thetav_(thetav),
      cost_(-1.0),
      time_delta_(time_delta),
      x_pts_(num_pts),
      y_pts_(num_pts),
      th_pts_(num_pts) {}

void Trajectory::getPoint(unsigned int index, double& x, double& y,
                          double& th) const {
    x = x_pts_[index];
    y = y_pts_[index];
    th = th_pts_[index];
}

void Trajectory::setPoint(unsigned int index, double x, double y, double th) {
    x_pts_[index] = x;
    y_pts_[index] = y;
    th_pts_[index] = th;
}

void Trajectory::addPoint(double x, double y, double th) {
    x_pts_.push_back(x);
    y_pts_.push_back(y);
    th_pts_.push_back(th);
}

void Trajectory::resetPoints() {
    x_pts_.clear();
    y_pts_.clear();
    th_pts_.clear();
}

void Trajectory::getEndpoint(double& x, double& y, double& th) const {
    x = x_pts_.back();
    y = y_pts_.back();
    th = th_pts_.back();
}

unsigned int Trajectory::getPointsSize() const { return x_pts_.size(); }



http://lihuaxi.xjx100.cn/news/1863634.html

相关文章

案例研究|作为一家BI厂商,飞致云是如何人人使用DataEase的?

杭州飞致云信息科技有限公司&#xff08;以下简称为飞致云&#xff09;长期秉持“软件用起来才有价值&#xff0c;才有改进的机会”的核心价值观&#xff0c;以“为数字经济时代创造好软件”为使命&#xff0c;致力于成为中国数字化团队首选的通用工具软件提供商。在软件产品不…

KDE指导聚类分析(一)噪声问题

不考虑相关性易受噪声干扰&#xff1f; 作者在看文献[1]盛魁,马健.基于核密度估计的物联网聚类分析模型[J].控制工程,2018,25(06):1098-1102.DOI:10.14107/j.cnki.kzgc.170739.时发现了这句话&#xff1a; FCM 聚类算法对于样本点之间的联系信息基本不考虑&#xff0c;这使得其…

【电源专题】什么是电源管理

电源管理为什么重要? 在电子系统和电路的设计中,负载往往需要恒定的电流电压,所以最先考虑的就是电源电路的设计。电源管理所考虑的问题是如何将电源有效分配给系统的不同组件,保障系统不同的负载正常运行。 如电源的输入是交流 (AC) 或直流 (DC)?输入电压是高于或低于输…

leetcode:93. 复原 IP 地址

复原 IP 地址 中等 1.4K 相关企业 有效 IP 地址 正好由四个整数&#xff08;每个整数位于 0 到 255 之间组成&#xff0c;且不能含有前导 0&#xff09;&#xff0c;整数之间用 ‘.’ 分隔。 例如&#xff1a;“0.1.2.201” 和 “192.168.1.1” 是 有效 IP 地址&#xff0c;但…

具有五层协议的网络体系结构

目录 一、计算机的网络体系结构 二、五层协议的体系结构 1、物理层 2、数据链路层 3、网络层 4、传输层 5、应用层 三、数据在各层之间传输的过程 一、计算机的网络体系结构 二、五层协议的体系结构 1、物理层 利用传输介质为通信的网络结点之间建立、管理和释放物理连…

react结合vant的Dialog实现签到弹框操作

1.需求 有时候在开发的时候&#xff0c;需要实现一个签到获取积分的功能&#xff0c;使用react怎么实现呢&#xff1f; 需求如下&#xff1a; 1.当点击“签到”按钮时&#xff0c;弹出签到框 2.展示签到信息&#xff1a; 签到天数&#xff0c; 对应天数签到能够获取的积分&…

抖音集团面试挂在2面,复盘后,决定二战.....

先说下我基本情况&#xff0c;本科不是计算机专业&#xff0c;现在是学通信&#xff0c;然后做图像处理&#xff0c;可能面试官看我不是科班出身没有问太多计算机相关的问题&#xff0c;因为第一次找工作&#xff0c;字节的游戏专场又是最早开始的&#xff0c;就投递了&#xf…

xattr -r -d com.apple.quarantine是用于删除文件的扩展属性的命令

xattr -r -d com.apple.quarantine 是一个macOS终端命令&#xff0c;它用于递归地删除指定目录中的全部文件的“quarantine”扩展属性。 在macOS系统中&#xff0c;当你从网络或其他未知来源下载并打开文件时&#xff0c;系统会将该文件标记为“quarantine”&#xff0c;以防止…