├── CMakeLists.txt ├── include └── myviz │ └── myviz.hpp ├── package.xml └── src ├── main.cpp └── myviz.cpp /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(rviz_embed_test) 3 | 4 | # Default to C++14 5 | if(NOT CMAKE_CXX_STANDARD) 6 | set(CMAKE_CXX_STANDARD 14) 7 | endif() 8 | 9 | find_package(ament_cmake REQUIRED) 10 | find_package(rclcpp REQUIRED) 11 | find_package(rviz_common REQUIRED) 12 | find_package(rviz_default_plugins REQUIRED) 13 | find_package(rviz_ogre_vendor REQUIRED) 14 | find_package(rviz_rendering REQUIRED) 15 | find_package(Qt5 REQUIRED COMPONENTS Widgets Core) 16 | 17 | set(THIS_PACKAGE_INCLUDE_DEPENDS 18 | rclcpp 19 | rclcpp_components 20 | rviz_common 21 | rviz_default_plugins 22 | rviz_ogre_vendor 23 | rviz_rendering 24 | ) 25 | 26 | include_directories( 27 | include 28 | ${rclcpp_INCLUDE_DIRS} 29 | ${rviz_common_INCLUDE_DIRS} 30 | ${Qt5Core_INCLUDE_DIRS} 31 | ${Qt5Widgets_INCLUDE_DIRS} 32 | ) 33 | 34 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 35 | 36 | set(headers_to_moc 37 | include/myviz/myviz.hpp 38 | ) 39 | 40 | foreach(header "${headers_to_moc}") 41 | qt5_wrap_cpp(moc_files "${header}") 42 | endforeach() 43 | 44 | set(SRC_FILES 45 | src/myviz.cpp 46 | src/main.cpp 47 | ${moc_files} 48 | ) 49 | 50 | add_executable(${PROJECT_NAME} ${SRC_FILES}) 51 | ament_target_dependencies(${PROJECT_NAME} ${THIS_PACKAGE_INCLUDE_DEPENDS}) 52 | 53 | target_link_libraries(${PROJECT_NAME} 54 | Qt5::Core 55 | Qt5::Widgets 56 | rviz_common::rviz_common 57 | rviz_rendering::rviz_rendering 58 | rviz_ogre_vendor::OgreMain 59 | rviz_ogre_vendor::OgreOverlay 60 | ) 61 | 62 | ## Install 63 | install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION lib/${PROJECT_NAME}) 64 | 65 | ament_package() 66 | -------------------------------------------------------------------------------- /include/myviz/myviz.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "rclcpp/rclcpp.hpp" 10 | #include "rviz_common/display.hpp" 11 | #include 12 | #include "rviz_common/window_manager_interface.hpp" 13 | #include "rviz_common/ros_integration/ros_node_abstraction.hpp" 14 | 15 | namespace rviz_common 16 | { 17 | class Display; 18 | class RenderPanel; 19 | class VisualizationManager; 20 | } 21 | 22 | class MyViz: public QMainWindow, public rviz_common::WindowManagerInterface 23 | { 24 | Q_OBJECT 25 | public: 26 | MyViz(QApplication *app, rviz_common::ros_integration::RosNodeAbstractionIface::WeakPtr rviz_ros_node, QWidget * parent = 0); 27 | 28 | QWidget * getParentWindow() override; 29 | rviz_common::PanelDockWidget * addPane(const QString & name, QWidget * pane, Qt::DockWidgetArea area, bool floating) override; 30 | void setStatus(const QString & message) override; 31 | 32 | void DisplayGrid(); 33 | 34 | private slots: 35 | void setThickness( int thickness_percent ); 36 | void setCellSize( int cell_size_percent ); 37 | void closeEvent(QCloseEvent *event); 38 | 39 | private: 40 | void initializeRViz(); 41 | 42 | QApplication * app_; 43 | QWidget * central_widget; 44 | QVBoxLayout * main_layout; 45 | 46 | rviz_common::RenderPanel * render_panel_; 47 | rviz_common::Display * grid_; 48 | rviz_common::VisualizationManager * manager_; 49 | 50 | rviz_common::ros_integration::RosNodeAbstractionIface::WeakPtr rviz_ros_node_; 51 | }; 52 | -------------------------------------------------------------------------------- /package.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | rviz_embed_test 5 | 0.0.0 6 | TODO: Package description 7 | michael 8 | TODO: License declaration 9 | 10 | ament_cmake 11 | 12 | rclcpp 13 | rclcpp_components 14 | rviz_common 15 | rviz_default_plugins 16 | rviz_ogre_vendor 17 | rviz_rendering 18 | 19 | ament_lint_auto 20 | ament_lint_common 21 | 22 | 23 | ament_cmake 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | #include "myviz/myviz.hpp" 6 | #include "rviz_common/ros_integration/ros_node_abstraction.hpp" 7 | 8 | int main(int argc, char ** argv) 9 | { 10 | QApplication app(argc, argv); 11 | rclcpp::init(argc, argv); 12 | 13 | auto ros_node_abs = 14 | std::make_shared("rviz_render_node"); 15 | 16 | auto myviz = std::make_shared(&app, ros_node_abs); 17 | myviz->show(); 18 | 19 | while (rclcpp::ok()) { 20 | app.processEvents(); 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /src/myviz.cpp: -------------------------------------------------------------------------------- 1 | #include "myviz/myviz.hpp" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include "rclcpp/clock.hpp" 8 | #include "rviz_common/render_panel.hpp" 9 | #include "rviz_common/ros_integration/ros_node_abstraction.hpp" 10 | #include "rviz_common/visualization_manager.hpp" 11 | #include "rviz_rendering/render_window.hpp" 12 | 13 | MyViz::MyViz( 14 | QApplication * app, 15 | rviz_common::ros_integration::RosNodeAbstractionIface::WeakPtr rviz_ros_node, 16 | QWidget * parent) 17 | : app_(app), rviz_ros_node_(rviz_ros_node), QMainWindow(parent) 18 | { 19 | // Construct the layout 20 | QLabel * thickness_label = new QLabel("Line Thickness"); 21 | QSlider * thickness_slider = new QSlider(Qt::Horizontal); 22 | thickness_slider->setMinimum(1); 23 | thickness_slider->setMaximum(100); 24 | QLabel * cell_size_label = new QLabel("Cell Size"); 25 | QSlider * cell_size_slider = new QSlider(Qt::Horizontal); 26 | cell_size_slider->setMinimum(1); 27 | cell_size_slider->setMaximum(100); 28 | QGridLayout * controls_layout = new QGridLayout(); 29 | controls_layout->addWidget(thickness_label, 0, 0); 30 | controls_layout->addWidget(thickness_slider, 0, 1); 31 | controls_layout->addWidget(cell_size_label, 1, 0); 32 | controls_layout->addWidget(cell_size_slider, 1, 1); 33 | 34 | // Add visualization 35 | main_layout = new QVBoxLayout; 36 | main_layout->addLayout(controls_layout); 37 | central_widget = new QWidget(); 38 | main_layout->setSpacing(0); 39 | main_layout->setMargin(0); 40 | 41 | // Initialize the classes we need from rviz 42 | initializeRViz(); 43 | 44 | central_widget->setLayout(main_layout); 45 | setCentralWidget(central_widget); 46 | main_layout->addWidget(render_panel_); 47 | 48 | // Signals 49 | connect(thickness_slider, SIGNAL(valueChanged(int)), this, SLOT(setThickness(int))); 50 | connect(cell_size_slider, SIGNAL(valueChanged(int)), this, SLOT(setCellSize(int))); 51 | 52 | // Display the rviz grid plugin 53 | DisplayGrid(); 54 | 55 | // Intialize the sliders 56 | thickness_slider->setValue(25); 57 | cell_size_slider->setValue(10); 58 | } 59 | 60 | QWidget * 61 | MyViz::getParentWindow() 62 | { 63 | return this; 64 | } 65 | 66 | rviz_common::PanelDockWidget * 67 | MyViz::addPane(const QString & name, QWidget * pane, Qt::DockWidgetArea area, bool floating) 68 | { 69 | // TODO(mjeronimo) 70 | return nullptr; 71 | } 72 | 73 | void 74 | MyViz::setStatus(const QString & message) 75 | { 76 | // TODO(mjeronimo) 77 | } 78 | 79 | void MyViz::DisplayGrid() 80 | { 81 | grid_ = manager_->createDisplay("rviz_default_plugins/Grid", "adjustable grid", true); 82 | assert(grid_ != NULL); 83 | grid_->subProp("Line Style")->setValue("Billboards"); 84 | grid_->subProp("Color")->setValue(QColor(Qt::yellow)); 85 | } 86 | 87 | void MyViz::initializeRViz() 88 | { 89 | app_->processEvents(); 90 | render_panel_ = new rviz_common::RenderPanel(central_widget); 91 | app_->processEvents(); 92 | render_panel_->getRenderWindow()->initialize(); 93 | auto clock = rviz_ros_node_.lock()->get_raw_node()->get_clock(); 94 | manager_ = new rviz_common::VisualizationManager(render_panel_, rviz_ros_node_, this, clock); 95 | render_panel_->initialize(manager_); 96 | app_->processEvents(); 97 | manager_->initialize(); 98 | manager_->startUpdate(); 99 | } 100 | 101 | void MyViz::setThickness(int thickness_percent) 102 | { 103 | if (grid_ != NULL) { 104 | grid_->subProp("Line Style")->subProp("Line Width")->setValue(thickness_percent / 100.0f); 105 | } 106 | } 107 | 108 | void MyViz::setCellSize(int cell_size_percent) 109 | { 110 | if (grid_ != NULL) { 111 | grid_->subProp("Cell Size")->setValue(cell_size_percent / 10.0f); 112 | } 113 | } 114 | 115 | void MyViz::closeEvent(QCloseEvent * event) 116 | { 117 | QMainWindow::closeEvent(event); 118 | rclcpp::shutdown(); 119 | } 120 | --------------------------------------------------------------------------------