一,实现效果
实现效果:
二,核心代码
创建librviz界面类:
qrviz_widget.h
#ifndef QRVIZ_WIDGET_H
#define QRVIZ_WIDGET_H
#include <QWidget>
#include <rviz/visualization_manager.h>
#include <rviz/render_panel.h>
#include <rviz/display.h>
#include <rviz/tool_manager.h>
#include <rviz/visualization_manager.h>
#include <rviz/render_panel.h>
#include <rviz/display.h>
#include<rviz/tool_manager.h>
#include <rviz_visual_tools/rviz_visual_tools.h>
namespace Ui {
class QRviz_widget;
}
class QRviz_widget : public QWidget
{
Q_OBJECT
public:
explicit QRviz_widget(QWidget *parent = 0);
~QRviz_widget();
private:
Ui::QRviz_widget *ui;
};
#endif // QRVIZ_WIDGET_H
qrviz_widget.cpp
主要就是向layout添加rviz的panel
#include "../include/cyrobot_monitor/qrviz_widget.h"
#include "ui_qrviz_widget.h"
QRviz_widget::QRviz_widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::QRviz_widget)
{
ui->setupUi(this);
int argc;
char **argv;
ros::init(argc,argv,"QRviz",ros::init_options::AnonymousName);
//创建rviz容器
rviz::RenderPanel *render_panel_=new rviz::RenderPanel;
//设置鼠标形状
render_panel_->setCursor(Qt::PointingHandCursor);
//向layout添加widget
ui->verticalLayout->addWidget(render_panel_);
//初始化rviz控制对象
rviz::VisualizationManager* manager_=new rviz::VisualizationManager(render_panel_);
//初始化camera 这行代码实现放大 缩小 平移等操作
render_panel_->initialize(manager_->getSceneManager(),manager_);
// //工具管理
rviz::ToolManager* tool_man;
// connect( tool_man, SIGNAL( toolAdded( Tool* )), this, SLOT( addTool( Tool* )));
// connect( tool_man, SIGNAL( toolRemoved( Tool* )), this, SLOT( removeTool( Tool* )));
// connect( tool_man, SIGNAL( toolRefreshed( Tool* )), this, SLOT( refreshTool( Tool* )));
// connect( tool_man, SIGNAL( toolChanged( Tool* )), this, SLOT( indicateToolIsCurrent( Tool* )));
manager_->initialize();
manager_->removeAllDisplays();
rviz::Display* grid_ = manager_->createDisplay( "rviz/Grid", "adjustable grid", true );
ROS_ASSERT( grid_ != NULL );
// Configure the GridDisplay the way we like it.
grid_->subProp( "Line Style" )->setValue( "Billboards" );
grid_->subProp( "Color" )->setValue(QColor(125,125,125));
manager_->startUpdate();
}
QRviz_widget::~QRviz_widget()
{
delete ui;
}
新建widget对象:
<code class="prism language-cpp has-numbering">qrviz<span class="token operator">=</span><span class="token keyword">new</span> <span class="token function">QRviz_widget</span><span class="token punctuation">(</span>ui<span class="token punctuation">.</span>widget_rviz<span class="token punctuation">)</span><span class="token punctuation">;</span> qrviz<span class="token operator">-</span><span class="token operator">></span><span class="token function">showFullScreen</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> qrviz<span class="token operator">-</span><span class="token operator">></span><span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">; </span></code>
其中,为了实现鼠标放大缩小移动的功能,本来以为需要自己监听鼠标事件,读了读rviz源码,发现这一行代码就能实现:
//初始化camera 这行代码实现放大 缩小 平移等操作
render_panel_->initialize(manager_->getSceneManager(),manager_);
三,完整开源项目
在我自己学习的过程中目前发现没有相关类似完整开源项目,为了帮助其他人少走弯路,我决定将自己的完整项目开源:
github
转载自:
<code class="prism language-cpp has-numbering"></code>
<code class="prism language-cpp has-numbering"></code>