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 <QProcessEnvironment> // check env variables
32 #include <QTimer>
33 
34 #include <boost/program_options.hpp>
35 #include <boost/filesystem.hpp>
36 
37 #include <OgreMaterialManager.h>
38 #include <OgreGpuProgramManager.h>
39 #include <OgreHighLevelGpuProgramManager.h>
40 #include <std_srvs/Empty.h>
41 
42 #ifdef Q_OS_MAC
43 #include <ApplicationServices/ApplicationServices.h>
44 // Apparently OSX #defines 'check' to be an empty string somewhere.
45 // That was fun to figure out.
46 #undef check
47 #endif
48 
49 #include <ros/console.h>
50 #include <ros/ros.h>
51 
53 #include <rviz/env_config.h>
58 #include <rviz/noetic_eol_dialog.h>
60 
61 #include <rviz/visualizer_app.h>
62 
63 #define CATCH_EXCEPTIONS 0
64 
65 namespace po = boost::program_options;
66 namespace fs = boost::filesystem;
67 
68 namespace rviz
69 {
70 bool reloadShaders(std_srvs::Empty::Request& /*unused*/, std_srvs::Empty::Response& /*unused*/)
71 {
72  ROS_INFO("Reloading materials.");
73  {
74  Ogre::ResourceManager::ResourceMapIterator it =
75  Ogre::MaterialManager::getSingleton().getResourceIterator();
76  while (it.hasMoreElements())
77  {
78  Ogre::ResourcePtr resource = it.getNext();
79  resource->reload();
80  }
81  }
82  ROS_INFO("Reloading high-level gpu shaders.");
83  {
84  Ogre::ResourceManager::ResourceMapIterator it =
85  Ogre::HighLevelGpuProgramManager::getSingleton().getResourceIterator();
86  while (it.hasMoreElements())
87  {
88  Ogre::ResourcePtr resource = it.getNext();
89  resource->reload();
90  }
91  }
92  ROS_INFO("Reloading gpu shaders.");
93  {
94  Ogre::ResourceManager::ResourceMapIterator it =
95  Ogre::GpuProgramManager::getSingleton().getResourceIterator();
96  while (it.hasMoreElements())
97  {
98  Ogre::ResourcePtr resource = it.getNext();
99  resource->reload();
100  }
101  }
102  return true;
103 }
104 
105 VisualizerApp::VisualizerApp() : continue_timer_(nullptr), frame_(nullptr)
106 {
107 }
108 
109 void VisualizerApp::setApp(QApplication* app)
110 {
111  Q_UNUSED(app);
112 }
113 
114 bool VisualizerApp::init(int argc, char** argv)
115 {
116  ROS_INFO("rviz version %s", get_version().c_str());
117  ROS_INFO("compiled against Qt version " QT_VERSION_STR);
118  ROS_INFO("compiled against OGRE version %d.%d.%d%s (%s)", OGRE_VERSION_MAJOR, OGRE_VERSION_MINOR,
119  OGRE_VERSION_PATCH, OGRE_VERSION_SUFFIX, OGRE_VERSION_NAME);
120 
121 #ifdef Q_OS_MAC
122  ProcessSerialNumber PSN;
123  GetCurrentProcess(&PSN);
124  TransformProcessType(&PSN, kProcessTransformToForegroundApplication);
125  SetFrontProcess(&PSN);
126 #endif
127 
128 #if CATCH_EXCEPTIONS
129  try
130  {
131 #endif
132  ros::init(argc, argv, "rviz", ros::init_options::AnonymousName);
133 
135 
136  std::string display_config, fixed_frame, splash_path, help_path;
137  int force_gl_version = 0;
138 
139  po::options_description options;
140  options.add_options() // clang-format off
141  ("help,h", "Produce this help message")
142  ("splash-screen,s", po::value<std::string>(&splash_path),
143  "A custom splash-screen image to display")
144  ("help-file", po::value<std::string>(&help_path),
145  "A custom html file to show as the help screen")
146  ("display-config,d", po::value<std::string>(&display_config),
147  "A display config file (.rviz) to load")
148  ("fullscreen", "Trigger fullscreen display")
149  ("fixed-frame,f", po::value<std::string>(&fixed_frame), "Set the fixed frame")
150  ("ogre-log,l", "Enable the Ogre.log file (output in cwd) and console output.")
151  ("in-mc-wrapper", "Signal that this is running inside a master-chooser wrapper")
152  ("opengl", po::value<int>(&force_gl_version),
153  "Force OpenGL version (use '--opengl 210' for OpenGL 2.1 compatibility mode)")
154  ("disable-anti-aliasing", "Prevent rviz from trying to use anti-aliasing when rendering.")
155  ("no-stereo", "Disable the use of stereo rendering.")("verbose,v", "Enable debug visualizations")
156  ("log-level-debug", "Sets the ROS logger level to debug.");
157  // clang-format on
158  po::variables_map vm;
159  try
160  {
161  po::store(po::parse_command_line(argc, argv, options), vm);
162  po::notify(vm);
163 
164  if (vm.count("help"))
165  {
166  std::cout << "rviz command line options:\n" << options;
167  return false;
168  }
169 
170  if (vm.count("log-level-debug"))
171  {
173  {
175  }
176  }
177  }
178  catch (std::exception& e)
179  {
180  ROS_ERROR("Error parsing command line: %s", e.what());
181  return false;
182  }
183 
184  if (!ros::master::check())
185  {
186  WaitForMasterDialog dialog;
187  if (dialog.exec() != QDialog::Accepted)
188  {
189  return false;
190  }
191  }
192 
193  nh_.reset(new ros::NodeHandle);
194 
195  if (vm.count("ogre-log"))
197 
198  RenderSystem::forceGlVersion(force_gl_version);
199 
200  if (vm.count("disable-anti-aliasing"))
202 
203  if (vm.count("no-stereo"))
205 
206  frame_ = new VisualizationFrame();
207  if (!help_path.empty())
208  {
209  frame_->setHelpPath(QString::fromStdString(help_path));
210  }
211  frame_->setShowChooseNewMaster(vm.count("in-mc-wrapper") > 0);
212  if (vm.count("splash-screen"))
213  frame_->setSplashPath(QString::fromStdString(splash_path));
214 
215  frame_->initialize(QString::fromStdString(display_config));
216 
217  if (!fixed_frame.empty())
218  frame_->getManager()->setFixedFrame(QString::fromStdString(fixed_frame));
219 
220  frame_->getManager()->getSelectionManager()->setDebugMode(vm.count("verbose") > 0);
221 
222  if (vm.count("fullscreen"))
223  frame_->setFullScreen(true);
224  frame_->show();
225 
226  ros::NodeHandle private_nh("~");
227  reload_shaders_service_ = private_nh.advertiseService("reload_shaders", reloadShaders);
228 
230  private_nh.advertiseService("load_config", &VisualizerApp::loadConfigCallback, this);
232  "load_config_discarding_changes", &VisualizerApp::loadConfigDiscardingCallback, this);
234  private_nh.advertiseService("save_config", &VisualizerApp::saveConfigCallback, this);
235 
236  // Notify users of ROS 1 EOL Date
237  // Get the environment variables
238  QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
239  // If environment variable is not set
240  if(!env.contains("DISABLE_ROS1_EOL_WARNINGS"))
241  {
242  // Create a warning pop up
243  NoeticEOLDialog eol_dialog;
244  eol_dialog.exec();
245  }
246 
247 #if CATCH_EXCEPTIONS
248  }
249  catch (std::exception& e)
250  {
251  ROS_ERROR("Caught exception while loading: %s", e.what());
252  return false;
253  }
254 #endif
255  return true;
256 }
257 
259 {
260  delete continue_timer_;
261  delete frame_;
262 }
263 
265 {
266  continue_timer_ = new QTimer(this);
267  connect(continue_timer_, &QTimer::timeout, this, &VisualizerApp::checkContinue);
268  continue_timer_->start(100);
269 }
270 
272 {
273  if (!ros::ok())
274  {
275  if (frame_)
276  {
277  // Make sure the window doesn't ask if we want to save first.
278  frame_->setWindowModified(false);
279  }
280  QApplication::closeAllWindows();
281  }
282 }
283 
284 bool VisualizerApp::loadConfigCallback(rviz::SendFilePathRequest& req, rviz::SendFilePathResponse& res)
285 {
286  fs::path path = req.path.data;
287  if (fs::is_regular_file(path))
288  res.success = frame_->loadDisplayConfigHelper(path.string());
289  else
290  res.success = false;
291  return true;
292 }
293 
294 bool VisualizerApp::loadConfigDiscardingCallback(rviz::SendFilePathRequest& req,
295  rviz::SendFilePathResponse& res)
296 {
297  fs::path path = req.path.data;
298  if (fs::is_regular_file(path))
299  {
300  bool discard_changes = true;
301  res.success = frame_->loadDisplayConfigHelper(path.string(), discard_changes);
302  }
303  else
304  res.success = false;
305  return true;
306 }
307 
308 bool VisualizerApp::saveConfigCallback(rviz::SendFilePathRequest& req, rviz::SendFilePathResponse& res)
309 {
310  res.success = frame_->saveDisplayConfig(QString::fromStdString(req.path.data));
311  return true;
312 }
313 
314 
315 } // namespace rviz
ros::init_options::AnonymousName
AnonymousName
ros::master::check
ROSCPP_DECL bool check()
render_system.h
rviz::VisualizerApp::checkContinue
void checkContinue()
Definition: visualizer_app.cpp:271
wait_for_master_dialog.h
env_config.h
rviz::VisualizerApp::setApp
void setApp(QApplication *app)
Definition: visualizer_app.cpp:109
rviz::VisualizerApp::load_config_service_
ros::ServiceServer load_config_service_
Definition: visualizer_app.h:74
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
noetic_eol_dialog.h
rviz::VisualizationFrame::setFullScreen
void setFullScreen(bool full_screen)
Set full screen mode.
Definition: visualization_frame.cpp:1368
rviz::VisualizerApp::loadConfigDiscardingCallback
bool loadConfigDiscardingCallback(rviz::SendFilePathRequest &req, rviz::SendFilePathResponse &res)
Definition: visualizer_app.cpp:294
ros.h
rviz::VisualizationManager::getSelectionManager
SelectionManager * getSelectionManager() const override
Return a pointer to the SelectionManager.
Definition: visualization_manager.h:258
rviz::VisualizerApp::nh_
ros::NodeHandlePtr nh_
Definition: visualizer_app.h:72
ros::NodeHandle::advertiseService
ServiceServer advertiseService(AdvertiseServiceOptions &ops)
rviz::RenderSystem::forceGlVersion
static void forceGlVersion(int version)
Definition: render_system.cpp:85
selection_manager.h
rviz::VisualizationFrame::setHelpPath
void setHelpPath(const QString &help_path)
Set the path to the help file. Should contain HTML. Default is a file in the RViz package.
Definition: visualization_frame.cpp:232
ros::console::set_logger_level
ROSCONSOLE_DECL bool set_logger_level(const std::string &name, levels::Level level)
ogre_logging.h
rviz::VisualizerApp::load_config_discarding_service_
ros::ServiceServer load_config_discarding_service_
Definition: visualizer_app.h:75
ros::ok
ROSCPP_DECL bool ok()
rviz::VisualizationFrame::saveDisplayConfig
bool saveDisplayConfig(const QString &path)
Save display and other settings to the given file.
Definition: visualization_frame.cpp:833
visualizer_app.h
console.h
rviz::VisualizationFrame::setShowChooseNewMaster
void setShowChooseNewMaster(bool show)
Call this before initialize() to have it take effect.
Definition: visualization_frame.cpp:227
ros::console::levels::Debug
Debug
rviz::VisualizationFrame
The main rviz window.
Definition: visualization_frame.h:74
rviz
Definition: add_display_dialog.cpp:54
rviz::VisualizationFrame::loadDisplayConfigHelper
bool loadDisplayConfigHelper(const std::string &full_path, const bool discard_changes=false)
Load display and other settings from the given full file path.
Definition: visualization_frame.cpp:752
rviz::VisualizerApp::~VisualizerApp
~VisualizerApp() override
Definition: visualizer_app.cpp:258
rviz::VisualizationFrame::getManager
VisualizationManager * getManager()
Definition: visualization_frame.h:107
rviz::SelectionManager::setDebugMode
void setDebugMode(bool debug)
Enables or disables publishing of picking and depth rendering images.
Definition: selection_manager.cpp:117
rviz::VisualizerApp::frame_
VisualizationFrame * frame_
Definition: visualizer_app.h:71
app
app
rviz::VisualizerApp::continue_timer_
QTimer * continue_timer_
Definition: visualizer_app.h:70
rviz::VisualizerApp::save_config_service_
ros::ServiceServer save_config_service_
Definition: visualizer_app.h:76
visualization_manager.h
rviz::VisualizationManager::setFixedFrame
void setFixedFrame(const QString &frame)
Set the coordinate frame we should be transforming all fixed data into.
Definition: visualization_manager.cpp:602
ros::console::notifyLoggerLevelsChanged
ROSCONSOLE_DECL void notifyLoggerLevelsChanged()
rviz::OgreLogging::useRosLog
static void useRosLog()
Configure Ogre to write output to the ROS logger.
Definition: ogre_logging.cpp:69
rviz::RenderSystem::disableAntiAliasing
static void disableAntiAliasing()
Definition: render_system.cpp:91
ROS_ERROR
#define ROS_ERROR(...)
rviz::VisualizerApp::VisualizerApp
VisualizerApp()
Definition: visualizer_app.cpp:105
visualization_frame.h
rviz::VisualizerApp::saveConfigCallback
bool saveConfigCallback(rviz::SendFilePathRequest &req, rviz::SendFilePathResponse &res)
Definition: visualizer_app.cpp:308
rviz::VisualizationFrame::setSplashPath
void setSplashPath(const QString &splash_path)
Set the path to the "splash" image file. This image is shown during initialization and loading of the...
Definition: visualization_frame.cpp:238
rviz::NoeticEOLDialog
Definition: noetic_eol_dialog.h:8
rviz::WaitForMasterDialog
Definition: wait_for_master_dialog.h:36
ROSCONSOLE_DEFAULT_NAME
#define ROSCONSOLE_DEFAULT_NAME
rviz::reloadShaders
bool reloadShaders(std_srvs::Empty::Request &, std_srvs::Empty::Response &)
Definition: visualizer_app.cpp:70
rviz::RenderSystem::forceNoStereo
static void forceNoStereo()
Definition: render_system.cpp:97
ROS_INFO
#define ROS_INFO(...)
rviz::VisualizerApp::reload_shaders_service_
ros::ServiceServer reload_shaders_service_
Definition: visualizer_app.h:73
rviz::VisualizerApp::loadConfigCallback
bool loadConfigCallback(rviz::SendFilePathRequest &req, rviz::SendFilePathResponse &res)
Definition: visualizer_app.cpp:284
rviz::get_version
std::string get_version()
rviz::VisualizerApp::startContinueChecker
void startContinueChecker()
Definition: visualizer_app.cpp:264
ros::NodeHandle
rviz::VisualizationFrame::initialize
void initialize(const QString &display_config_file="")
Initialize the visualizer. Creates the VisualizationManager.
Definition: visualization_frame.cpp:243
rviz::VisualizerApp::init
bool init(int argc, char **argv)
Definition: visualizer_app.cpp:114


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Sun May 4 2025 02:31:27