Librviz Tutorial

Overview

RViz is not just a visualizer application, it is also a library! Much of RViz’s functionality can be accessed within your own application by linking against librviz.so (or whatever your OS likes to call it).

This tutorial shows a very simple example of creating a 3D visualizer widget (rviz::RenderPanel), programmatically creating a new Grid display within it, then using Qt slider controls to adjust a couple of the grid’s properties. The app is called “myviz”.

The source code for this tutorial is in the librviz_tutorial package. You can check out the source directly or (if you use Ubuntu) you can just apt-get install the pre-compiled Debian package like so:

sudo apt-get install ros-hydro-visualization-tutorials

The running application looks like this:

_images/myviz.png

The Code

The code for myviz is in these files: src/main.cpp, src/myviz.h, and src/myviz.cpp.

main.cpp

The full text of main.cpp is here: src/main.cpp

The main() for this “myviz” example is very simple, it just initializes ROS, creates a QApplication, creates the top-level widget (of type “MyViz”), shows it, and runs the Qt event loop.

#include <QApplication>
#include <ros/ros.h>
#include "myviz.h"

int main(int argc, char **argv)
{
  if( !ros::isInitialized() )
  {
    ros::init( argc, argv, "myviz", ros::init_options::AnonymousName );
  }

  QApplication app( argc, argv );

  MyViz* myviz = new MyViz();
  myviz->show();

  app.exec();

  delete myviz;
}

myviz.h

The full text of myviz.h is here: src/myviz.h

Class “MyViz” implements the top level widget for this example.

class MyViz: public QWidget
{
Q_OBJECT
public:
  MyViz( QWidget* parent = 0 );
  virtual ~MyViz();

private Q_SLOTS:
  void setThickness( int thickness_percent );
  void setCellSize( int cell_size_percent );

private:
  rviz::VisualizationManager* manager_;
  rviz::RenderPanel* render_panel_;
  rviz::Display* grid_;
};

myviz.cpp

The full text of myviz.cpp is here: src/myviz.cpp

Constructor for MyViz. This does most of the work of the class.

MyViz::MyViz( QWidget* parent )
  : QWidget( parent )
{

Construct and lay out labels and slider controls.

QLabel* thickness_label = new QLabel( "Line Thickness" );
QSlider* thickness_slider = new QSlider( Qt::Horizontal );
thickness_slider->setMinimum( 1 );
thickness_slider->setMaximum( 100 );
QLabel* cell_size_label = new QLabel( "Cell Size" );
QSlider* cell_size_slider = new QSlider( Qt::Horizontal );
cell_size_slider->setMinimum( 1 );
cell_size_slider->setMaximum( 100 );
QGridLayout* controls_layout = new QGridLayout();
controls_layout->addWidget( thickness_label, 0, 0 );
controls_layout->addWidget( thickness_slider, 0, 1 );
controls_layout->addWidget( cell_size_label, 1, 0 );
controls_layout->addWidget( cell_size_slider, 1, 1 );

Construct and lay out render panel.

render_panel_ = new rviz::RenderPanel();
QVBoxLayout* main_layout = new QVBoxLayout;
main_layout->addLayout( controls_layout );
main_layout->addWidget( render_panel_ );

Set the top-level layout for this MyViz widget.

setLayout( main_layout );

Make signal/slot connections.

connect( thickness_slider, SIGNAL( valueChanged( int )), this, SLOT( setThickness( int )));
connect( cell_size_slider, SIGNAL( valueChanged( int )), this, SLOT( setCellSize( int )));

Next we initialize the main RViz classes.

The VisualizationManager is the container for Display objects, holds the main Ogre scene, holds the ViewController, etc. It is very central and we will probably need one in every usage of librviz.

manager_ = new rviz::VisualizationManager( render_panel_ );
render_panel_->initialize( manager_->getSceneManager(), manager_ );
manager_->initialize();
manager_->startUpdate();

Create a Grid 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( Qt::yellow ) );

Initialize the slider values.

  thickness_slider->setValue( 25 );
  cell_size_slider->setValue( 10 );
}

Destructor.

MyViz::~MyViz()
{
  delete manager_;
}

This function is a Qt slot connected to a QSlider’s valueChanged() signal. It sets the line thickness of the grid by changing the grid’s “Line Width” property.

void MyViz::setThickness( int thickness_percent )
{
  if( grid_ != NULL )
  {
    grid_->subProp( "Line Style" )->subProp( "Line Width" )->setValue( thickness_percent / 100.0f );
  }
}

This function is a Qt slot connected to a QSlider’s valueChanged() signal. It sets the cell size of the grid by changing the grid’s “Cell Size” Property.

void MyViz::setCellSize( int cell_size_percent )
{
  if( grid_ != NULL )
  {
    grid_->subProp( "Cell Size" )->setValue( cell_size_percent / 10.0f );
  }
}

Building

The full text of CMakeLists.txt is here: CMakeLists.txt

Running

Just type:

rosrun librviz_tutorial myviz