HBA代码解读(1)tools.hpp

2024-02-20 00:36
文章标签 代码 解读 tools hpp hba

本文主要是介绍HBA代码解读(1)tools.hpp,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

其中,函数jr 和 jr_inv 暂时未找到出处。其他函数都已经注释。

#ifndef TOOLS_HPP
#define TOOLS_HPP#include <Eigen/Core>
#include <unordered_map>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <math.h>#define HASH_P 116101
#define MAX_N 10000000019
#define SMALL_EPS 1e-10
#define SKEW_SYM_MATRX(v) 0.0,-v[2],v[1],v[2],0.0,-v[0],-v[1],v[0],0.0//方阵
#define PLM(a) vector<Eigen::Matrix<double, a, a>, Eigen::aligned_allocator<Eigen::Matrix<double, a, a>>>
//矢量
#define PLV(a) vector<Eigen::Matrix<double, a, 1>, Eigen::aligned_allocator<Eigen::Matrix<double, a, 1>>>
#define VEC(a) Eigen::Matrix<double, a, 1>#define G_m_s2 9.81
#define DIMU 18
#define DIM 15
#define DNOI 12
#define NMATCH 5
#define DVEL 6typedef pcl::PointXYZ PointType;
// typedef pcl::PointXYZI PointType;
// typedef pcl::PointXYZINormal PointType;
using namespace std;Eigen::Matrix3d I33(Eigen::Matrix3d::Identity());
Eigen::Matrix<double, DIMU, DIMU> I_imu(Eigen::Matrix<double, DIMU, DIMU>::Identity());// 根据XYZ值生成 hash位置
class VOXEL_LOC
{
public:int64_t x, y, z;VOXEL_LOC(int64_t vx = 0, int64_t vy = 0, int64_t vz = 0): x(vx), y(vy), z(vz){}bool operator == (const VOXEL_LOC &other) const{return (x == other.x && y == other.y && z == other.z);}
};namespace std
{template<>struct hash<VOXEL_LOC>{size_t operator() (const VOXEL_LOC &s) const{using std::size_t; using std::hash;// return (((hash<int64_t>()(s.z)*HASH_P)%MAX_N + hash<int64_t>()(s.y))*HASH_P)%MAX_N + hash<int64_t>()(s.x);long long index_x, index_y, index_z;double cub_len = 0.125;index_x = int(round(floor((s.x)/cub_len + SMALL_EPS)));index_y = int(round(floor((s.y)/cub_len + SMALL_EPS)));index_z = int(round(floor((s.z)/cub_len + SMALL_EPS)));return (((((index_z * HASH_P) % MAX_N + index_y) * HASH_P) % MAX_N) + index_x) % MAX_N;}};
}//矩阵元素和
double matrixAbsSum(Eigen::MatrixXd mat)
{double sum = 0.0;for (int i = 0; i < mat.rows(); i++)for (int j = 0; j < mat.cols(); j++)sum += fabs(mat(i, j));return sum;
}// 误差柯西函数
double sigmoid_w(double r)
{return 1.0/(1+exp(-r));
}// 向量变成旋转矩阵
Eigen::Matrix3d Exp(const Eigen::Vector3d &ang)
{double ang_norm = ang.norm();Eigen::Matrix3d Eye3 = Eigen::Matrix3d::Identity();if (ang_norm > 0.0000001){Eigen::Vector3d r_axis = ang / ang_norm;Eigen::Matrix3d K;K << SKEW_SYM_MATRX(r_axis);/// Roderigous Tranformationreturn Eye3 + std::sin(ang_norm) * K + (1.0 - std::cos(ang_norm)) * K * K;}else{return Eye3;}
}
// 向量变成旋转矩阵
Eigen::Matrix3d Exp(const Eigen::Vector3d &ang_vel, const double &dt)
{double ang_vel_norm = ang_vel.norm();Eigen::Matrix3d Eye3 = Eigen::Matrix3d::Identity();if (ang_vel_norm > 0.0000001){Eigen::Vector3d r_axis = ang_vel / ang_vel_norm;Eigen::Matrix3d K;K << SKEW_SYM_MATRX(r_axis);double r_ang = ang_vel_norm * dt;/// Roderigous Tranformationreturn Eye3 + std::sin(r_ang) * K + (1.0 - std::cos(r_ang)) * K * K;}else{return Eye3;}
}// 旋转矩阵变成向量
Eigen::Vector3d Log(const Eigen::Matrix3d &R)
{double theta = (R.trace() > 3.0 - 1e-6) ? 0.0 : std::acos(0.5 * (R.trace() - 1));Eigen::Vector3d K(R(2,1) - R(1,2), R(0,2) - R(2,0), R(1,0) - R(0,1));return (std::abs(theta) < 0.001) ? (0.5 * K) : (0.5 * theta / std::sin(theta) * K);
}// 构建v的反对称矩阵
Eigen::Matrix3d hat(const Eigen::Vector3d &v)
{Eigen::Matrix3d Omega;Omega <<  0, -v(2),  v(1),  v(2),     0, -v(0), -v(1),  v(0),     0;return Omega;
}Eigen::Matrix3d jr(Eigen::Vector3d vec)
{double ang = vec.norm();if(ang < 1e-9){return I33;}else{vec /= ang;double ra = sin(ang)/ang;return ra*I33 + (1-ra)*vec*vec.transpose() - (1-cos(ang))/ang * hat(vec);}
}Eigen::Matrix3d jr_inv(const Eigen::Matrix3d &rotR)
{Eigen::AngleAxisd rot_vec(rotR);Eigen::Vector3d axi = rot_vec.axis();double ang = rot_vec.angle();if(ang < 1e-9){return I33;}else{double ctt = ang / 2 / tan(ang/2);return ctt*I33 + (1-ctt)*axi*axi.transpose() + ang/2 * hat(axi);}
}// 主要用到了p和R
struct IMUST
{double t;Eigen::Matrix3d R;Eigen::Vector3d p;Eigen::Vector3d v;Eigen::Vector3d bg;Eigen::Vector3d ba;Eigen::Vector3d g;IMUST(){setZero();}IMUST(double _t, const Eigen::Matrix3d& _R, const Eigen::Vector3d& _p, const Eigen::Vector3d& _v,const Eigen::Vector3d& _bg, const Eigen::Vector3d& _ba,const Eigen::Vector3d& _g = Eigen::Vector3d(0, 0, -G_m_s2)):t(_t), R(_R), p(_p), v(_v), bg(_bg), ba(_ba), g(_g){}IMUST &operator+=(const Eigen::Matrix<double, DIMU, 1> &ist){this->R = this->R * Exp(ist.block<3, 1>(0, 0));this->p += ist.block<3, 1>(3, 0);this->v += ist.block<3, 1>(6, 0);this->bg += ist.block<3, 1>(9, 0);this->ba += ist.block<3, 1>(12, 0);this->g += ist.block<3, 1>(15, 0);return *this;}Eigen::Matrix<double, DIMU, 1> operator-(const IMUST &b) {Eigen::Matrix<double, DIMU, 1> a;a.block<3, 1>(0, 0) = Log(b.R.transpose() * this->R);a.block<3, 1>(3, 0) = this->p - b.p;a.block<3, 1>(6, 0) = this->v - b.v;a.block<3, 1>(9, 0) = this->bg - b.bg;a.block<3, 1>(12, 0) = this->ba - b.ba;a.block<3, 1>(15, 0) = this->g - b.g;return a;}IMUST &operator=(const IMUST &b){this->R = b.R;this->p = b.p;this->v = b.v;this->bg = b.bg;this->ba = b.ba;this->g = b.g;this->t = b.t;return *this;}void setZero(){t = 0; R.setIdentity();p.setZero(); v.setZero();bg.setZero(); ba.setZero();g << 0, 0, -G_m_s2;}
};// 复制四元数 q,t
void assign_qt(Eigen::Quaterniond& q, Eigen::Vector3d& t,const Eigen::Quaterniond& q_, const Eigen::Vector3d& t_)
{q.w() = q_.w(); q.x() = q_.x(); q.y() = q_.y(); q.z() = q_.z();t(0) = t_(0); t(1) = t_(1); t(2) = t_(2);
}//该结构体主要服务于 downsample_voxel
struct M_POINT
{float xyz[3];int count = 0;
};//计算每一个体素中的均值点
void downsample_voxel(pcl::PointCloud<PointType>& pc, double voxel_size)
{if (voxel_size < 0.01)return;std::unordered_map<VOXEL_LOC, M_POINT> feature_map;size_t pt_size = pc.size();  // pt_size 在此时是点个数for (size_t i = 0; i < pt_size; i++){PointType &pt_trans = pc[i];float loc_xyz[3];for (int j = 0; j < 3; j++){loc_xyz[j] = pt_trans.data[j] / voxel_size;  // 计算点所在体素if (loc_xyz[j] < 0)loc_xyz[j] -= 1.0;}VOXEL_LOC position((int64_t)loc_xyz[0], (int64_t)loc_xyz[1], (int64_t)loc_xyz[2]);  //计算所在体素索引auto iter = feature_map.find(position);if (iter != feature_map.end()) {iter->second.xyz[0] += pt_trans.x;  //加入该点坐标iter->second.xyz[1] += pt_trans.y;iter->second.xyz[2] += pt_trans.z;iter->second.count++;}else{M_POINT anp;  // 第一个加入该体素的点anp.xyz[0] = pt_trans.x;anp.xyz[1] = pt_trans.y;anp.xyz[2] = pt_trans.z;anp.count = 1;feature_map[position] = anp;}}pt_size = feature_map.size();   // pt_size 在此时是体素个数pc.clear();pc.resize(pt_size);size_t i = 0;for (auto iter = feature_map.begin(); iter != feature_map.end(); ++iter) // 求得体素内的点的均值点{pc[i].x = iter->second.xyz[0] / iter->second.count;pc[i].y = iter->second.xyz[1] / iter->second.count;pc[i].z = iter->second.xyz[2] / iter->second.count;i++;}
}/*rr是旋转矩阵,tt是平移矩阵pl1 是vector<Eigen::Vector3d>对自身进行RT变换
*/
void pl_transform(pcl::PointCloud<PointType> &pl1, const Eigen::Matrix3d &rr, const Eigen::Vector3d &tt)
{for(PointType &ap : pl1.points){Eigen::Vector3d pvec(ap.x, ap.y, ap.z);pvec = rr * pvec + tt;ap.x = pvec[0];ap.y = pvec[1];ap.z = pvec[2];}
}/*该函数是通过 IMUST 中的R,P把 porig中的三维点 变换到 ptran中PLV 是vector<Eigen::Vector3d>IMUST 中的 R和 P表示旋转和平移
*/
void plvec_trans(PLV(3) &porig, PLV(3) &ptran, IMUST &stat)
{uint asize = porig.size();ptran.resize(asize);for(uint i=0; i<asize; i++)ptran[i] = stat.R * porig[i] + stat.p;
}/*VOX_FACTOR VOX_FACTOR 的数据结构包含了一个协方差P,一个中心点V和一个点数N。函数解释:1.1 void push(const Eigen::Vector3d &vec) 加入一个点并更新 P V N。1.2  cov() 就是计算协方差了1.3 VOX_FACTOR & operator+=(const VOX_FACTOR& sigv) 就是P V N 的加1.4  transform(const VOX_FACTOR &sigv, const IMUST &stat) 就是R和T对 P V N 的变化
*/
class VOX_FACTOR
{
public:Eigen::Matrix3d P;Eigen::Vector3d v;int N;VOX_FACTOR(){P.setZero();v.setZero();N = 0;}void clear(){P.setZero();v.setZero();N = 0;}void push(const Eigen::Vector3d &vec){N++;P += vec * vec.transpose();v += vec;}Eigen::Matrix3d cov(){Eigen::Vector3d center = v / N;return P/N - center*center.transpose();}VOX_FACTOR & operator+=(const VOX_FACTOR& sigv){this->P += sigv.P;this->v += sigv.v;this->N += sigv.N;return *this;}void transform(const VOX_FACTOR &sigv, const IMUST &stat){N = sigv.N;v = stat.R*sigv.v + N*stat.p;Eigen::Matrix3d rp = stat.R * sigv.v * stat.p.transpose();P = stat.R*sigv.P*stat.R.transpose() + rp + rp.transpose() + N*stat.p*stat.p.transpose();}
};//平面参数计算
const double threshold = 0.1;
bool esti_plane(Eigen::Vector4d& pca_result, const pcl::PointCloud<PointType>& point)
{Eigen::Matrix<double, NMATCH, 3> A;Eigen::Matrix<double, NMATCH, 1> b;b.setOnes();b *= -1.0f;   // AX + BY + CZ +D =0; 其中D等于 1for (int j = 0; j < NMATCH; j++){A(j, 0) = point[j].x;  // A的系数A(j, 1) = point[j].y;  // B的系数A(j, 2) = point[j].z;  // C的系数}Eigen::Vector3d normvec = A.colPivHouseholderQr().solve(b); // A B C 参数for (int j = 0; j < NMATCH; j++){if (fabs(normvec.dot(A.row(j)) + 1.0) > threshold)  // 点到面的距离 大于 阈值(0.1)return false;  // 表示不是一个平面}double n = normvec.norm();   // sqrt(A*A + B*B + C*C)pca_result(0) = normvec(0) / n;  // Apca_result(1) = normvec(1) / n; // Bpca_result(2) = normvec(2) / n; // Cpca_result(3) = 1.0 / n;  // D = 1.0return true;
}#endif


 

这篇关于HBA代码解读(1)tools.hpp的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/726502

相关文章

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

对Django中时区的解读

《对Django中时区的解读》:本文主要介绍对Django中时区的解读方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录背景前端数据库中存储接口返回AI的解释问题:这样设置的作用答案获取当前时间(自动带时区)转换为北京时间显示总结背景设置时区为北京时间 TIM

使用Python自动化生成PPT并结合LLM生成内容的代码解析

《使用Python自动化生成PPT并结合LLM生成内容的代码解析》PowerPoint是常用的文档工具,但手动设计和排版耗时耗力,本文将展示如何通过Python自动化提取PPT样式并生成新PPT,同时... 目录核心代码解析1. 提取 PPT 样式到 jsON关键步骤:代码片段:2. 应用 JSON 样式到

SpringBoot实现二维码生成的详细步骤与完整代码

《SpringBoot实现二维码生成的详细步骤与完整代码》如今,二维码的应用场景非常广泛,从支付到信息分享,二维码都扮演着重要角色,SpringBoot是一个非常流行的Java基于Spring框架的微... 目录一、环境搭建二、创建 Spring Boot 项目三、引入二维码生成依赖四、编写二维码生成代码五

Java中的内部类和常用类用法解读

《Java中的内部类和常用类用法解读》:本文主要介绍Java中的内部类和常用类用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录内部类和常用类内部类成员内部类静态内部类局部内部类匿名内部类常用类Object类包装类String类StringBuffer和Stri

使用Python和PaddleOCR实现图文识别的代码和步骤

《使用Python和PaddleOCR实现图文识别的代码和步骤》在当今数字化时代,图文识别技术的应用越来越广泛,如文档数字化、信息提取等,PaddleOCR是百度开源的一款强大的OCR工具包,它集成了... 目录一、引言二、环境准备2.1 安装 python2.2 安装 PaddlePaddle2.3 安装

JVM垃圾回收机制之GC解读

《JVM垃圾回收机制之GC解读》:本文主要介绍JVM垃圾回收机制之GC,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、死亡对象的判断算法1.1 引用计数算法1.2 可达性分析算法二、垃圾回收算法2.1 标记-清除算法2.2 复制算法2.3 标记-整理算法2.4

SpringBoot中四种AOP实战应用场景及代码实现

《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO... 目录引言场景一:日志记录与性能监控业务需求实现方案使用示例扩展:MDC实现请求跟踪场景二:权限控制与

SpringBoot中配置文件的加载顺序解读

《SpringBoot中配置文件的加载顺序解读》:本文主要介绍SpringBoot中配置文件的加载顺序,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot配置文件的加载顺序1、命令⾏参数2、Java系统属性3、操作系统环境变量5、项目【外部】的ap

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模