当前位置: 首页 > 工具软件 > oct > 使用案例 >

octomap学习

关飞翼
2023-12-01
#include<octomap/octomap.h>

octomap::OcTree tree(o.o5);  //参数为分辨率
octomap::Pointcloud clound;  //octomap中的点云
cloud.push_back(pointWorld[0],pointWorld[1],pointWorld[2]);  //将世界坐标系中的点放入点云
tree.insertPointCloud(cloud,octomap::point3d(T(0,3),T(1,3),T(2,3)));  
//将点云存入八叉树地图,给定原点,这样可以计算射线
//函数原型为:void insertPointCloud(const Pointcloud &scan,
								  const octomap::point3d &sensor_origin,//是指传感器相较于全局参考帧的原点
								  double maxrange = -1.,//单个波束插入的最大范围
								  bool 	lazy_eval = false,//是否更新被遗漏的内部结点,为真则必须调用updateInnerOccupancy()
								  bool 	discretize = false )	//	和离散化有关的参数,
tree.updateInnerOccupancy();  //更新中间节点的占据信息并写入磁盘
tree.writeBinary(octomap.bt);  //将八叉树地图写入二进制文件

CMakeLists.txt的写法

cmake_minimum_required(VERSION 2.8)

project(dense_mapping)
set(CMAKE_CXX_FLAGS "-std=c++11")
find_package(octomap REQUIRED)
include_directories(${OCTOMAP_INCLUDE_DIRS})
add_executable(tum_octomap tum_octomap.cpp)
target_link_libraries(tum_octomap ${OCTOMAP_LIBRARIES})

官方给的octomap示例程序的核心语句分析

#include <octomap/octomap.h>
#include <octomap/OcTree.h>

using namespace std;
using namespace octomap;

OcTree tree (0.1);  // create empty tree with resolution 0.1
tree.updateNode(endpoint, true); // endpoint为octomap::point3d类型的世界坐标点,true表示“占据结点”
//调用此成员函数,返回endpoint所在结点的地址
//由此我们联想到另两个比较重要的成员函数
//void updateInnerOccupancy();
//void updateNodeLogOdds(OcTreeNode *occupancyNode,const float &update);//log_ods值

tree.updateNode(endpoint, false); // endpoint为octomap::point3d类型的世界坐标点,true表示“自由结点”
OcTreeNode* result = tree.search (query_point);
//query_point为octomap::point3d类型的世界坐标点,寻找query_point所在OcTreeNode

result->getOccupancy();//查询result指向结点的占据信息.

ColorOcTree的使用方法

#include <octomap/octomap.h>
#include <octomap/ColorOcTree.h>

using namespace std;
using namespace octomap;


int main(int argc, char** argv)
 {
  	std::string filename(argv[1]);

  	std::ifstream infile(filename.c_str(), std::ios_base::in |std::ios_base::binary);
	  if (!infile.is_open()) 
 	 {
    	cout << "file "<< filename << " could not be opened for reading.\n";
    	return -1;
 	 }

  ColorOcTree tree (0.1);
  tree.readData(infile);
  infile.close();
  cout << "color tree read from "<< filename <<"\n"; 

  tree.writeColorHistogram("histogram.eps");

  return 0;
}

octomap中常见的数据类型octomap::point3d 是类型octomap::Vector3的别名,其数据元素都是float类型
调用其成员函数的方法是

octomap::point3d you(1,2,3);
you.x();//调用第一个元素
you.y();//调用第二个元素
you.z();//调用第三个元素

在用search()成员函数时,要检查返回的结点指针是否为空(NULL)
返回结点为空表示未知空间

point3d query_point(1,1,1);
OcTreeNode* result=tree.search(query_point);
if(result==NULL)
	cout<<"我是空结点"<<endl;
else
	cout<<"我不空"<<endl;

octomap的常用函数分析

bool computeRay(const point3d &origin,const point3d &end,std::vector< point3d > &ray )	
参数分析:
	origin:射线的起始坐标
	end:射线的终点坐标
	ray:返回射线所经过的所有结点的坐标,不包括end坐标
仍然需要检查坐标处的结点是否存在,例如使用search()函数。
bool castRay(const point3d &origin,//起始射线坐标{输入参数}
			 const point3d &direction,//射线方向,不用归一化{输入参数}
			 point3d &end,//射线碰到的第一个非自由体素,返回其坐标{输出参数}
			 bool ignoreUnknownCells = false,
			 double maxRange = -1.0 
)	
castRay() returns true if an occupied node was hit by the raycas。
If the raycast returns false you can search() the node at 'end' and see whether it's unknown space.

OcTreeNode类的几个成员函数

float 	getLogOdds () const
float 	getMaxChildLogOdds () const
double 	getMeanChildLogOdds () const
double 	getOccupancy () const
float 	getValue () const
 类似资料: