00001 /* 00002 * Copyright (c) 2012, Willow Garage, Inc. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of the Willow Garage, Inc. nor the names of its 00014 * contributors may be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00030 00031 #ifndef RVIZ_VISUALIZATION_MANAGER_H_ 00032 #define RVIZ_VISUALIZATION_MANAGER_H_ 00033 00034 #include <QTimer> 00035 00036 #include <boost/thread.hpp> 00037 00038 #include <vector> 00039 #include <map> 00040 #include <set> 00041 00042 #include <ros/ros.h> 00043 #include <ros/callback_queue.h> 00044 #include <ros/time.h> 00045 00046 #include <pluginlib/class_loader.h> 00047 00048 #include "rviz/status_callback.h" 00049 #include "rviz/display_context.h" 00050 00051 class QKeyEvent; 00052 00053 namespace YAML 00054 { 00055 class Node; 00056 class Emitter; 00057 } 00058 00059 namespace Ogre 00060 { 00061 class Root; 00062 class SceneManager; 00063 class SceneNode; 00064 } 00065 00066 namespace tf 00067 { 00068 class TransformListener; 00069 } 00070 00071 namespace rviz 00072 { 00073 00074 class ColorProperty; 00075 class Display; 00076 class DisplayFactory; 00077 class DisplayGroup; 00078 class FrameManager; 00079 class Property; 00080 class PropertyTreeModel; 00081 class RenderPanel; 00082 class SelectionManager; 00083 class StatusList; 00084 class TfFrameProperty; 00085 class Tool; 00086 class ViewController; 00087 class ViewportMouseEvent; 00088 class WindowManagerInterface; 00089 typedef boost::shared_ptr<FrameManager> FrameManagerPtr; 00090 00105 class VisualizationManager: public DisplayContext 00106 { 00107 Q_OBJECT 00108 public: 00116 VisualizationManager( RenderPanel* render_panel, WindowManagerInterface* wm = 0 ); 00117 00122 virtual ~VisualizationManager(); 00123 00129 void initialize(const StatusCallback& cb = StatusCallback(), bool verbose=false); 00130 00135 void startUpdate(); 00136 00145 Display* createDisplay( const QString& class_lookup_name, const QString& name, bool enabled ); 00146 00151 void addDisplay( Display* display, bool enabled ); 00152 00156 void removeAllDisplays(); 00157 00159 void addTool( const std::string& tool_class_lookup_name ); 00160 00165 Tool* getCurrentTool() { return current_tool_; } 00166 00172 Tool* getTool( int index ); 00173 00174 int numTools() { return tools_.size(); } 00175 void removeTool( int index ); 00176 00184 void setCurrentTool( Tool* tool ); 00185 00197 void setDefaultTool( Tool* tool ); 00198 00203 Tool* getDefaultTool() { return default_tool_; } 00204 00214 void load( const YAML::Node& yaml_node, const StatusCallback& cb ); 00215 00224 void save( YAML::Emitter& emitter ); 00225 00228 QString getFixedFrame() const; 00229 00233 void setFixedFrame( const QString& frame ); 00234 00237 QString getTargetFrame() const; 00238 00254 void setTargetFrame( const QString& frame ); 00255 00259 tf::TransformListener* getTFClient() const; 00260 00264 Ogre::SceneManager* getSceneManager() const { return scene_manager_; } 00265 00269 RenderPanel* getRenderPanel() { return render_panel_; } 00270 00274 double getWallClock(); 00275 00279 double getROSTime(); 00280 00284 double getWallClockElapsed(); 00285 00289 double getROSTimeElapsed(); 00290 00298 void handleChar( QKeyEvent* event, RenderPanel* panel ); 00299 00307 void handleMouseEvent( const ViewportMouseEvent& event ); 00308 00312 void resetTime(); 00313 00317 ViewController* getCurrentViewController() { return view_controller_; } 00318 00323 std::string getCurrentViewControllerType(); 00324 00344 bool setCurrentViewControllerType(const std::string& type); 00345 00349 SelectionManager* getSelectionManager() const { return selection_manager_; } 00350 00354 void lockRender() { render_mutex_.lock(); } 00355 00359 void unlockRender() { render_mutex_.unlock(); } 00360 00365 void queueRender(); 00366 00370 WindowManagerInterface* getWindowManager() const { return window_manager_; } 00371 00375 ros::CallbackQueueInterface* getUpdateQueue() { return ros::getGlobalCallbackQueue(); } 00376 00380 ros::CallbackQueueInterface* getThreadedQueue() { return &threaded_queue_; } 00381 00382 pluginlib::ClassLoader<Tool>* getToolClassLoader() { return tool_class_loader_; } 00383 std::set<std::string> getToolClasses(); 00384 00386 FrameManager* getFrameManager() const { return frame_manager_; } 00387 00393 uint64_t getFrameCount() const { return frame_count_; } 00394 00397 void notifyConfigChanged(); 00398 00400 virtual DisplayFactory* getDisplayFactory() const { return display_factory_; } 00401 00402 PropertyTreeModel* getDisplayTreeModel() const { return display_property_tree_model_; } 00403 00404 Q_SIGNALS: 00408 void toolAdded( Tool* ); 00409 00414 void toolChanged( Tool* ); 00415 00416 void toolRemoved( Tool* ); 00417 00423 void viewControllerTypeAdded( const std::string& class_name, const std::string& name ); 00424 00428 void viewControllerChanged( ViewController* ); 00429 00433 void timeChanged(); 00434 00436 void configChanged(); 00437 00438 protected Q_SLOTS: 00448 void onUpdate(); 00449 00454 void onIdle(); 00455 00456 protected: 00457 void addViewController(const std::string& class_name, const std::string& name); 00458 00459 void updateRelativeNode(); 00460 00461 void incomingROSTime(); 00462 00463 void updateTime(); 00464 void updateFrames(); 00465 00466 void createColorMaterials(); 00467 00468 void threadedQueueThreadFunc(); 00469 00470 Ogre::Root* ogre_root_; 00471 Ogre::SceneManager* scene_manager_; 00472 00473 QTimer* update_timer_; 00474 ros::Time last_update_ros_time_; 00475 ros::WallTime last_update_wall_time_; 00476 00477 QTimer* idle_timer_; 00478 00479 ros::CallbackQueue threaded_queue_; 00480 boost::thread_group threaded_queue_threads_; 00481 ros::NodeHandle update_nh_; 00482 ros::NodeHandle threaded_nh_; 00483 volatile bool shutting_down_; 00484 00485 PropertyTreeModel* display_property_tree_model_; 00486 PropertyTreeModel* tool_property_tree_model_; 00487 DisplayGroup* root_display_group_; 00488 00489 struct ToolRecord 00490 { 00491 Tool* tool; 00492 std::string lookup_name; // for looking up the class with pluginlib 00493 }; 00494 typedef std::vector<ToolRecord> V_ToolRecord; 00495 V_ToolRecord tools_; 00496 Tool* current_tool_; 00497 Tool* default_tool_; 00498 00499 Property* global_options_; 00500 TfFrameProperty* target_frame_property_; 00501 TfFrameProperty* fixed_frame_property_; 00502 StatusList* global_status_; 00503 00504 RenderPanel* render_panel_; 00505 00506 ros::WallTime wall_clock_begin_; 00507 ros::Time ros_time_begin_; 00508 ros::WallDuration wall_clock_elapsed_; 00509 ros::Duration ros_time_elapsed_; 00510 00511 ColorProperty* background_color_property_; 00512 00513 float time_update_timer_; 00514 float frame_update_timer_; 00515 00516 ViewController* view_controller_; 00517 00518 SelectionManager* selection_manager_; 00519 00520 boost::mutex render_mutex_; 00521 uint32_t render_requested_; 00522 uint64_t frame_count_; 00523 ros::WallTime last_render_; 00524 00525 WindowManagerInterface* window_manager_; 00526 00527 pluginlib::ClassLoader<Tool>* tool_class_loader_; 00528 00529 FrameManager* frame_manager_; 00530 00531 bool disable_update_; 00532 bool target_frame_is_fixed_frame_; 00533 00534 Ogre::SceneNode *target_scene_node_; 00535 00536 std::deque<ViewportMouseEvent> vme_queue_; 00537 boost::mutex vme_queue_mutex_; 00538 00539 private Q_SLOTS: 00540 void updateFixedFrame(); 00541 void updateTargetFrame(); 00542 void updateBackgroundColor(); 00543 00544 private: 00545 DisplayFactory* display_factory_; 00546 }; 00547 00548 } 00549 00550 #endif /* RVIZ_VISUALIZATION_MANAGER_H_ */