首页 > 编程语言 >c++(4) sophus可视化和计算误差

c++(4) sophus可视化和计算误差

时间:2024-07-22 21:51:22浏览次数:12  
标签:p2 p1 1.0 TrajectoryType pangolin c++ 计算误差 sophus translation

 

 

 

 

 

 

 

 

 CMakeLists.txt

project(test)


find_package(Pangolin REQUIRED)
include_directories(${Pangolin_INCLUDE_DIRS})

find_package(fmt REQUIRED)
set(FMT_LIBRARIES fmt::fmt)

#set(v1_node_file main.cpp)

add_executable(v1_node main.cpp)
target_link_libraries(v1_node ${Pangolin_LIBRARIES} ${FMT_LIBRARIES})

  main.cpp

#ifndef MAIN_CPP
#define MAIN_CPP


#include "API_draw.h"


string groundtruth_file = "./data/groundtruth.txt";
string estimated_file = "./data/estimated.txt";



int main(int argc, char **argv) {
  TrajectoryType groundtruth = ReadTrajectory(groundtruth_file);
  TrajectoryType estimated = ReadTrajectory(estimated_file);
  assert(!groundtruth.empty() && !estimated.empty());
  assert(groundtruth.size() == estimated.size());

  // compute rmse
  double rmse = 0;
  for (size_t i = 0; i < estimated.size(); i++) {
    Sophus::SE3d p1 = estimated[i], p2 = groundtruth[i];
    double error = (p2.inverse() * p1).log().norm();
    rmse += error * error;
  }
  rmse = rmse / double(estimated.size());
  rmse = sqrt(rmse);
  cout << "RMSE = " << rmse << endl;

  DrawTrajectory(groundtruth, estimated);
  return 0;
}




#endif 

  API_draw.h

 

#ifndef DRAW_CPP
#define DRAW_CPP




#include <iostream>
#include <fstream>
#include <unistd.h>
#include <pangolin/pangolin.h>
#include <sophus/se3.hpp>

using namespace Sophus;
using namespace std;



typedef vector<Sophus::SE3d, Eigen::aligned_allocator<Sophus::SE3d>> TrajectoryType;

void DrawTrajectory(const TrajectoryType &gt, const TrajectoryType &esti);

TrajectoryType ReadTrajectory(const string &path);

TrajectoryType ReadTrajectory(const string &path) {
  ifstream fin(path);
  TrajectoryType trajectory;
  if (!fin) {
    cerr << "trajectory " << path << " not found." << endl;
    return trajectory;
  }

  while (!fin.eof()) {
    double time, tx, ty, tz, qx, qy, qz, qw;
    fin >> time >> tx >> ty >> tz >> qx >> qy >> qz >> qw;
    Sophus::SE3d p1(Eigen::Quaterniond(qw, qx, qy, qz), Eigen::Vector3d(tx, ty, tz));
    trajectory.push_back(p1);
  }
  return trajectory;
}

void DrawTrajectory(const TrajectoryType &gt, const TrajectoryType &esti) {
  // create pangolin window and plot the trajectory
  pangolin::CreateWindowAndBind("Trajectory Viewer", 1024, 768);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  pangolin::OpenGlRenderState s_cam(
      pangolin::ProjectionMatrix(1024, 768, 500, 500, 512, 389, 0.1, 1000),
      pangolin::ModelViewLookAt(0, -0.1, -1.8, 0, 0, 0, 0.0, -1.0, 0.0)
  );

  pangolin::View &d_cam = pangolin::CreateDisplay()
      .SetBounds(0.0, 1.0, pangolin::Attach::Pix(175), 1.0, -1024.0f / 768.0f)
      .SetHandler(new pangolin::Handler3D(s_cam));


  while (pangolin::ShouldQuit() == false) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    d_cam.Activate(s_cam);
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

    glLineWidth(2);
    for (size_t i = 0; i < gt.size() - 1; i++) {
      glColor3f(0.0f, 0.0f, 1.0f);  // blue for ground truth
      glBegin(GL_LINES);
      auto p1 = gt[i], p2 = gt[i + 1];
      glVertex3d(p1.translation()[0], p1.translation()[1], p1.translation()[2]);
      glVertex3d(p2.translation()[0], p2.translation()[1], p2.translation()[2]);
      glEnd();
    }

    for (size_t i = 0; i < esti.size() - 1; i++) {
      glColor3f(1.0f, 0.0f, 0.0f);  // red for estimated
      glBegin(GL_LINES);
      auto p1 = esti[i], p2 = esti[i + 1];
      glVertex3d(p1.translation()[0], p1.translation()[1], p1.translation()[2]);
      glVertex3d(p2.translation()[0], p2.translation()[1], p2.translation()[2]);
      glEnd();
    }
    pangolin::FinishFrame();
    usleep(5000);   // sleep 5 ms
  }

}


#endif 

  

#ifndef DRAW_CPP #defineDRAW_CPP



#include<iostream> #include<fstream> #include<unistd.h> #include<pangolin/pangolin.h> #include<sophus/se3.hpp>
using namespace Sophus; using namespace std;


typedef vector<Sophus::SE3d, Eigen::aligned_allocator<Sophus::SE3d>> TrajectoryType;
void DrawTrajectory(const TrajectoryType &gt, const TrajectoryType &esti);
TrajectoryType ReadTrajectory(const string &path);
TrajectoryType ReadTrajectory(const string &path) { ifstream fin(path); TrajectoryType trajectory; if (!fin) { cerr << "trajectory " << path << " not found." << endl; return trajectory; }
while (!fin.eof()) { double time, tx, ty, tz, qx, qy, qz, qw; fin >> time >> tx >> ty >> tz >> qx >> qy >> qz >> qw; Sophus::SE3d p1(Eigen::Quaterniond(qw, qx, qy, qz), Eigen::Vector3d(tx, ty, tz)); trajectory.push_back(p1); } return trajectory; }
void DrawTrajectory(const TrajectoryType &gt, const TrajectoryType &esti) { // create pangolin window and plot the trajectory pangolin::CreateWindowAndBind("Trajectory Viewer", 1024, 768); glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
pangolin::OpenGlRenderState s_cam( pangolin::ProjectionMatrix(1024, 768, 500, 500, 512, 389, 0.1, 1000), pangolin::ModelViewLookAt(0, -0.1, -1.8, 0, 0, 0, 0.0, -1.0, 0.0) );
pangolin::View &d_cam = pangolin::CreateDisplay() .SetBounds(0.0, 1.0, pangolin::Attach::Pix(175), 1.0, -1024.0f / 768.0f) .SetHandler(new pangolin::Handler3D(s_cam));

while (pangolin::ShouldQuit() == false) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
d_cam.Activate(s_cam); glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glLineWidth(2); for (size_t i = 0; i < gt.size() - 1; i++) { glColor3f(0.0f, 0.0f, 1.0f); // blue for ground truth glBegin(GL_LINES); auto p1 = gt[i], p2 = gt[i + 1]; glVertex3d(p1.translation()[0], p1.translation()[1], p1.translation()[2]); glVertex3d(p2.translation()[0], p2.translation()[1], p2.translation()[2]); glEnd(); }
for (size_t i = 0; i < esti.size() - 1; i++) { glColor3f(1.0f, 0.0f, 0.0f); // red for estimated glBegin(GL_LINES); auto p1 = esti[i], p2 = esti[i + 1]; glVertex3d(p1.translation()[0], p1.translation()[1], p1.translation()[2]); glVertex3d(p2.translation()[0], p2.translation()[1], p2.translation()[2]); glEnd(); } pangolin::FinishFrame(); usleep(5000); // sleep 5 ms }
}

#endif  

标签:p2,p1,1.0,TrajectoryType,pangolin,c++,计算误差,sophus,translation
From: https://www.cnblogs.com/gooutlook/p/18317025

相关文章

  • 【c++经典面试题】有关string类的深浅拷贝
    题目背景基于自实现string类substr成员函数时遇到的问题。代码展示stringstring::substr(size_tpos,size_tlen)//声明时len的参省值位npos { assert(pos<_size); if(len>_size-pos)//如果len的长度大于有效字符长度,那么重置为有效字符长度 { le......
  • c++(0) sophus矩阵转换
     1安装sophus2使用代码2-1R,t矩阵q四元数转换so3和se3 CMakeLists.txtcmake_minimum_required(VERSION3.0)project(useSophus)#为使用sophus,需要使用find_package命令找到它find_package(SophusREQUIRED)#Eigeninclude_directories("/usr/include/eigen3"......
  • c++零基础知识要点整理(7)
    *请搭配c++零基础知识要点整理(5)使用位或运算符的应用: | (有1即1)1.设置标记位(使某一个位置的值变为1)inta=0b101101;//(以使第五位变为1举例,即使a变为:0b11101)cout<<(a|0b10000)<<endl;//要使第五个位置的值变为1,则将这个数和0b10000进行位或//以此类推:需要使第四个......
  • C++树的介绍
    目录树的基本概念和术语树的种类实现树的例子遍历树在C++中,树(Tree)是一种非常重要的数据结构,用于模拟具有层级关系的数据。树结构是递归定义的,一个树由零个或多个节点(node)组成,其中一个节点被称为根节点(rootnode),其余节点分为若干个不相交的子树(subtree),每个子树也是一棵树......
  • 聊聊C++string
    原文链接聊聊c++string  相信大家对string都不陌生,但不知道大家有没有这样的疑问:两个string之间赋值,是指向同一个字符串还是不同的字符串?  如果是深拷贝,按理说要指向不同的字符串,那么内部数据的地址要不同;如果是浅拷贝,怎么避免指针doublefree的问题?  先看个简单......
  • zig vs c++:控制x11鼠标移动
    zigDebug输出大小:2.3MBReleaseSmall输出大小:11.3kBconststd=@import("std");constx11=@cImport({@cInclude("X11/Xlib.h");});//Convertsbetweennumerictypes:.Enum,.Intand.Float.pubinlinefnas(comptimeT:type,from:anyty......
  • C++ 学习笔记十一 封装
    封装4.1.1封装的意义封装是C++面向对象三大特性之一封装的意义:将属性和行为作为一个整体,表现生活中的事物将属性和行为加以权限控制封装意义一:​在设计类的时候,属性和行为写在一起,表现事物语法:class类名{访问权限:属性/行为};**示例1:**设计一个圆类,求圆的周......
  • 手写Kd树(C++模板非递归实现)
    手写Kd树(C++模板非递归实现)1.Kd树1.1Kd树简介1.2Kd树的建立1.3Kd树的查找2.C++完整代码实现3.测试代码3.1代码实现3.2测试结果4.与PCL中的Kd树做对比本文实现的Kd树实现参考了高翔博士的书《自动驾驶与机器人中的slam技术从理论到实践》;高博士原书中是递归......
  • C++:istream、ostream和fstream类
    1、基类istream和ostream(1)istreamA.What输入流的抽象类,是所有输入流类的基类B.Why(输入流的作用)用于从数据源(如文件、标准输入设备等外部设备)读取数据到内存中(2)ostreamA.What输出流的抽象类,是所有输出流类的基类B.Why(输出流的作用)输出流用于将数据从......
  • c++中字符串之string和char
    c++string初始化的几种方式相对于C#来说,c++中string的初始化方式真的非常多,比如以下都可以用来初始化string:usingnamespacestd;intmain(){ stringstr1="test01";//直接赋值 stringstr2(5,'c');//结果:str2='ccccc',以length为长度的ch的拷贝(即length个ch) ......