visualizer_app.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <QApplication>
31 #include <QTimer>
32 
33 #include <boost/program_options.hpp>
34 #include <boost/filesystem.hpp>
35 
36 #include <OgreMaterialManager.h>
37 #include <OgreGpuProgramManager.h>
38 #include <OgreHighLevelGpuProgramManager.h>
39 #include <std_srvs/Empty.h>
40 
41 #ifdef Q_OS_MAC
42 #include <ApplicationServices/ApplicationServices.h>
43 // Apparently OSX #defines 'check' to be an empty string somewhere.
44 // That was fun to figure out.
45 #undef check
46 #endif
47 
48 #include <ros/console.h>
49 #include <ros/ros.h>
50 
52 #include "rviz/env_config.h"
58 
59 #include "rviz/visualizer_app.h"
60 
61 #define CATCH_EXCEPTIONS 0
62 
63 namespace po = boost::program_options;
64 namespace fs = boost::filesystem;
65 
66 namespace rviz
67 {
68 bool reloadShaders(std_srvs::Empty::Request& /*unused*/, std_srvs::Empty::Response& /*unused*/)
69 {
70  ROS_INFO("Reloading materials.");
71  {
72  Ogre::ResourceManager::ResourceMapIterator it =
73  Ogre::MaterialManager::getSingleton().getResourceIterator();
74  while (it.hasMoreElements())
75  {
76  Ogre::ResourcePtr resource = it.getNext();
77  resource->reload();
78  }
79  }
80  ROS_INFO("Reloading high-level gpu shaders.");
81  {
82  Ogre::ResourceManager::ResourceMapIterator it =
83  Ogre::HighLevelGpuProgramManager::getSingleton().getResourceIterator();
84  while (it.hasMoreElements())
85  {
86  Ogre::ResourcePtr resource = it.getNext();
87  resource->reload();
88  }
89  }
90  ROS_INFO("Reloading gpu shaders.");
91  {
92  Ogre::ResourceManager::ResourceMapIterator it =
93  Ogre::GpuProgramManager::getSingleton().getResourceIterator();
94  while (it.hasMoreElements())
95  {
96  Ogre::ResourcePtr resource = it.getNext();
97  resource->reload();
98  }
99  }
100  return true;
101 }
102 
103 VisualizerApp::VisualizerApp() : app_(nullptr), continue_timer_(nullptr), frame_(nullptr)
104 {
105 }
106 
107 void VisualizerApp::setApp(QApplication* app)
108 {
109  app_ = app;
110 }
111 
112 bool VisualizerApp::init(int argc, char** argv)
113 {
114  ROS_INFO("rviz version %s", get_version().c_str());
115  ROS_INFO("compiled against Qt version " QT_VERSION_STR);
116  ROS_INFO("compiled against OGRE version %d.%d.%d%s (%s)", OGRE_VERSION_MAJOR, OGRE_VERSION_MINOR,
117  OGRE_VERSION_PATCH, OGRE_VERSION_SUFFIX, OGRE_VERSION_NAME);
118 
119 #ifdef Q_OS_MAC
120  ProcessSerialNumber PSN;
121  GetCurrentProcess(&PSN);
122  TransformProcessType(&PSN, kProcessTransformToForegroundApplication);
123  SetFrontProcess(&PSN);
124 #endif
125 
126 #if CATCH_EXCEPTIONS
127  try
128  {
129 #endif
130  ros::init(argc, argv, "rviz", ros::init_options::AnonymousName);
131 
133 
134  std::string display_config, fixed_frame, splash_path, help_path;
135  int force_gl_version = 0;
136 
137  po::options_description options;
138  options.add_options() // clang-format off
139  ("help,h", "Produce this help message")
140  ("splash-screen,s", po::value<std::string>(&splash_path),
141  "A custom splash-screen image to display")
142  ("help-file", po::value<std::string>(&help_path),
143  "A custom html file to show as the help screen")
144  ("display-config,d", po::value<std::string>(&display_config),
145  "A display config file (.rviz) to load")
146  ("fullscreen", "Trigger fullscreen display")
147  ("fixed-frame,f", po::value<std::string>(&fixed_frame), "Set the fixed frame")
148  ("ogre-log,l", "Enable the Ogre.log file (output in cwd) and console output.")
149  ("in-mc-wrapper", "Signal that this is running inside a master-chooser wrapper")
150  ("opengl", po::value<int>(&force_gl_version),
151  "Force OpenGL version (use '--opengl 210' for OpenGL 2.1 compatibility mode)")
152  ("disable-anti-aliasing", "Prevent rviz from trying to use anti-aliasing when rendering.")
153  ("no-stereo", "Disable the use of stereo rendering.")("verbose,v", "Enable debug visualizations")
154  ("log-level-debug", "Sets the ROS logger level to debug.");
155  // clang-format on
156  po::variables_map vm;
157  try
158  {
159  po::store(po::parse_command_line(argc, argv, options), vm);
160  po::notify(vm);
161 
162  if (vm.count("help"))
163  {
164  std::cout << "rviz command line options:\n" << options;
165  return false;
166  }
167 
168  if (vm.count("log-level-debug"))
169  {
171  {
173  }
174  }
175  }
176  catch (std::exception& e)
177  {
178  ROS_ERROR("Error parsing command line: %s", e.what());
179  return false;
180  }
181 
182  if (!ros::master::check())
183  {
184  WaitForMasterDialog dialog;
185  if (dialog.exec() != QDialog::Accepted)
186  {
187  return false;
188  }
189  }
190 
191  nh_.reset(new ros::NodeHandle);
192 
193  if (vm.count("ogre-log"))
195 
196  RenderSystem::forceGlVersion(force_gl_version);
197 
198  if (vm.count("disable-anti-aliasing"))
200 
201  if (vm.count("no-stereo"))
203 
204  frame_ = new VisualizationFrame();
205  frame_->setApp(this->app_);
206  if (!help_path.empty())
207  {
208  frame_->setHelpPath(QString::fromStdString(help_path));
209  }
210  frame_->setShowChooseNewMaster(vm.count("in-mc-wrapper") > 0);
211  if (vm.count("splash-screen"))
212  frame_->setSplashPath(QString::fromStdString(splash_path));
213 
214  frame_->initialize(QString::fromStdString(display_config));
215 
216  if (!fixed_frame.empty())
217  frame_->getManager()->setFixedFrame(QString::fromStdString(fixed_frame));
218 
219  frame_->getManager()->getSelectionManager()->setDebugMode(vm.count("verbose") > 0);
220 
221  if (vm.count("fullscreen"))
222  frame_->setFullScreen(true);
223  frame_->show();
224 
225  ros::NodeHandle private_nh("~");
226  reload_shaders_service_ = private_nh.advertiseService("reload_shaders", reloadShaders);
227 
229  private_nh.advertiseService("load_config", &VisualizerApp::loadConfigCallback, this);
231  private_nh.advertiseService("save_config", &VisualizerApp::saveConfigCallback, this);
232 
233 #if CATCH_EXCEPTIONS
234  }
235  catch (std::exception& e)
236  {
237  ROS_ERROR("Caught exception while loading: %s", e.what());
238  return false;
239  }
240 #endif
241  return true;
242 }
243 
245 {
246  delete continue_timer_;
247  delete frame_;
248 }
249 
251 {
252  continue_timer_ = new QTimer(this);
253  connect(continue_timer_, SIGNAL(timeout()), this, SLOT(checkContinue()));
254  continue_timer_->start(100);
255 }
256 
258 {
259  if (!ros::ok())
260  {
261  if (frame_)
262  {
263  // Make sure the window doesn't ask if we want to save first.
264  frame_->setWindowModified(false);
265  }
266  QApplication::closeAllWindows();
267  }
268 }
269 
270 bool VisualizerApp::loadConfigCallback(rviz::SendFilePathRequest& req, rviz::SendFilePathResponse& res)
271 {
272  fs::path path = req.path.data;
273  if (fs::is_regular_file(path))
274  res.success = frame_->loadDisplayConfigHelper(path.string());
275  else
276  res.success = false;
277  return true;
278 }
279 
280 bool VisualizerApp::saveConfigCallback(rviz::SendFilePathRequest& req, rviz::SendFilePathResponse& res)
281 {
282  res.success = frame_->saveDisplayConfig(QString::fromStdString(req.path.data));
283  return true;
284 }
285 
286 
287 } // namespace rviz
bool saveConfigCallback(rviz::SendFilePathRequest &req, rviz::SendFilePathResponse &res)
void setApp(QApplication *app)
VisualizationManager * getManager()
ROSCPP_DECL bool check()
QApplication * app_
bool loadConfigCallback(rviz::SendFilePathRequest &req, rviz::SendFilePathResponse &res)
ROSCONSOLE_DECL void notifyLoggerLevelsChanged()
bool saveDisplayConfig(const QString &path)
Save display and other settings to the given file.
bool reloadShaders(std_srvs::Empty::Request &, std_srvs::Empty::Response &)
void setSplashPath(const QString &splash_path)
Set the path to the "splash" image file. This image is shown during initialization and loading of the...
static void forceGlVersion(int version)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void setFixedFrame(const QString &frame)
Set the coordinate frame we should be transforming all fixed data into.
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
ros::ServiceServer reload_shaders_service_
ros::ServiceServer load_config_service_
static void disableAntiAliasing()
void initialize(const QString &display_config_file="")
Initialize the visualizer. Creates the VisualizationManager.
void setDebugMode(bool debug)
Enables or disables publishing of picking and depth rendering images.
ros::NodeHandlePtr nh_
#define ROS_INFO(...)
options
void setFullScreen(bool full_screen)
Set full screen mode.
ROSCPP_DECL bool ok()
std::string get_version()
void setHelpPath(const QString &help_path)
Set the path to the help file. Should contain HTML. Default is a file in the RViz package...
bool init(int argc, char **argv)
VisualizationFrame * frame_
bool loadDisplayConfigHelper(const std::string &full_path)
Load display and other settings from the given full file path.
SelectionManager * getSelectionManager() const override
Return a pointer to the SelectionManager.
static void forceNoStereo()
ros::ServiceServer save_config_service_
ROSCONSOLE_DECL bool set_logger_level(const std::string &name, levels::Level level)
void setApp(QApplication *app)
The main rviz window.
#define ROS_ERROR(...)
void setShowChooseNewMaster(bool show)
Call this before initialize() to have it take effect.
#define ROSCONSOLE_DEFAULT_NAME
static void useRosLog()
Configure Ogre to write output to the ROS logger.


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat May 27 2023 02:06:25