interactive_marker.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, 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 #ifndef RVIZ_INTERACTIVE_MARKER_H_
00031 #define RVIZ_INTERACTIVE_MARKER_H_
00032 
00033 #include <boost/shared_ptr.hpp>
00034 #include <boost/thread/mutex.hpp>
00035 #include <boost/thread/recursive_mutex.hpp>
00036 #include <boost/thread/thread.hpp>
00037 
00038 #include <OgreVector3.h>
00039 #include <OgreQuaternion.h>
00040 
00041 #include <visualization_msgs/InteractiveMarker.h>
00042 #include <visualization_msgs/InteractiveMarkerPose.h>
00043 #include <visualization_msgs/InteractiveMarkerFeedback.h>
00044 #include <geometry_msgs/Pose.h>
00045 
00046 #include <ros/publisher.h>
00047 
00048 #include "rviz/selection/forwards.h"
00049 #include "rviz/ogre_helpers/axes.h"
00050 
00051 #include "rviz/default_plugin/interactive_markers/interactive_marker_control.h"
00052 #include "rviz/properties/status_property.h"
00053 
00054 namespace Ogre {
00055 class SceneNode;
00056 }
00057 
00058 class QMenu;
00059 
00060 namespace rviz
00061 {
00062 class DisplayContext;
00063 class InteractiveMarkerDisplay;
00064 
00065 class InteractiveMarker : public QObject
00066 {
00067 Q_OBJECT
00068 public:
00069   InteractiveMarker( Ogre::SceneNode* scene_node, DisplayContext* context );
00070   virtual ~InteractiveMarker();
00071 
00072   // reset contents to reflect the data from a new message
00073   // @return success
00074   bool processMessage( const visualization_msgs::InteractiveMarker& message );
00075 
00076   // reset contents to reflect the data from a new message
00077   // @return success
00078   void processMessage( const visualization_msgs::InteractiveMarkerPose& message );
00079 
00080   // called every frame update
00081   void update(float wall_dt);
00082 
00083   // directly set the pose, relative to parent frame
00084   // if publish is set to true, publish the change
00085   void setPose( Ogre::Vector3 position, Ogre::Quaternion orientation, const std::string &control_name );
00086 
00087   void translate( Ogre::Vector3 delta_position, const std::string &control_name );
00088   void rotate( Ogre::Quaternion delta_orientation, const std::string &control_name );
00089 
00090   // schedule a pose reset once dragging is finished
00091   void requestPoseUpdate( Ogre::Vector3 position, Ogre::Quaternion orientation );
00092 
00093   void startDragging();
00094   void stopDragging();
00095 
00096   const Ogre::Vector3& getPosition() { return position_; }
00097   const Ogre::Quaternion& getOrientation() { return orientation_; }
00098 
00099   float getSize() { return scale_; }
00100   const std::string &getReferenceFrame() { return reference_frame_; }
00101   const std::string& getName() { return name_; }
00102 
00103   // show name above marker
00104   void setShowDescription( bool show );
00105 
00106   // show axes in origin
00107   void setShowAxes( bool show );
00108 
00109   // show visual helpers while dragging
00110   void setShowVisualAids( bool show );
00111 
00112   // @return true if the mouse event was intercepted, false if it was ignored
00113   bool handleMouseEvent(ViewportMouseEvent& event, const std::string &control_name );
00114 
00124   bool handle3DCursorEvent(ViewportMouseEvent& event, const Ogre::Vector3& cursor_pos, const Ogre::Quaternion& cursor_rot, const std::string &control_name);
00125 
00126 
00135   void showMenu( ViewportMouseEvent& event, const std::string &control_name, const Ogre::Vector3 &three_d_point, bool valid_point );
00136 
00137   // fill in current marker pose & name, publish
00138   void publishFeedback(visualization_msgs::InteractiveMarkerFeedback &feedback,
00139                        bool mouse_point_valid = false,
00140                        const Ogre::Vector3& mouse_point_rel_world = Ogre::Vector3(0,0,0) );
00141 
00142   bool hasMenu() { return has_menu_; }
00143 
00145   boost::shared_ptr<QMenu> getMenu() { return menu_; }
00146 
00147 Q_SIGNALS:
00148 
00149 void userFeedback(visualization_msgs::InteractiveMarkerFeedback &feedback);
00150 void statusUpdate( StatusProperty::Level level, const std::string& name, const std::string& text );
00151 
00152 protected Q_SLOTS:
00153   void handleMenuSelect( int menu_item_id );
00154 
00155 protected:
00156 
00157   void publishPose();
00158 
00159   void reset();
00160 
00161   // set the pose of the parent frame, relative to the fixed frame
00162   void updateReferencePose();
00163 
00164   QString makeMenuString( const std::string &entry );
00165 
00166   // Recursively append menu and submenu entries to menu, based on a
00167   // vector of menu entry id numbers describing the menu entries at the
00168   // current level.
00169   void populateMenu( QMenu* menu, std::vector<uint32_t>& ids );
00170 
00171   DisplayContext* context_;
00172 
00173   // pose of parent coordinate frame
00174   std::string reference_frame_;
00175   ros::Time reference_time_;
00176   bool frame_locked_;
00177 
00178   // node representing reference frame in tf, like /map, /base_link, /head, etc.
00179   Ogre::SceneNode *reference_node_;
00180 
00181   // pose being controlled, relative to reference frame
00182   Ogre::Vector3 position_;
00183   Ogre::Quaternion orientation_;
00184 
00185   // has the pose changed since the last feedback was sent?
00186   bool pose_changed_;
00187   double time_since_last_feedback_;
00188 
00189   typedef boost::shared_ptr<InteractiveMarkerControl> InteractiveMarkerControlPtr;
00190   typedef std::map<std::string, InteractiveMarkerControlPtr> M_ControlPtr;
00191   M_ControlPtr controls_;
00192 
00193   std::string name_;
00194   std::string description_;
00195 
00196   bool dragging_;
00197 
00198   // pose being controlled
00199   bool pose_update_requested_;
00200   Ogre::Vector3 requested_position_;
00201   Ogre::Quaternion requested_orientation_;
00202 
00203   float scale_;
00204 
00205   boost::shared_ptr<QMenu> menu_;
00206   bool has_menu_;
00207 
00208   // Helper to more simply represent the menu tree.
00209   struct MenuNode
00210   {
00211     visualization_msgs::MenuEntry entry;
00212     std::vector<uint32_t> child_ids;
00213   };
00214 
00215   // maps menu index to menu entry and item
00216   std::map< uint32_t, MenuNode > menu_entries_;
00217 
00218   // Helper to store the top level of the menu tree.
00219   std::vector<uint32_t> top_level_menu_ids_;
00220 
00221   // which control has popped up the menu
00222   std::string last_control_name_;
00223 
00224   double heart_beat_t_;
00225 
00226   // visual aids
00227 
00228   Axes *axes_;
00229 
00230   InteractiveMarkerControlPtr description_control_;
00231 
00232   std::string topic_ns_;
00233   std::string client_id_;
00234 
00235   boost::recursive_mutex mutex_;
00236 
00237   boost::shared_ptr< boost::thread > sys_thread_;
00238 
00239   bool got_3d_point_for_menu_;
00240   Ogre::Vector3 three_d_point_for_menu_;
00241 
00242   bool show_visual_aids_;
00243 };
00244 
00245 
00246 }
00247 
00248 #endif /* INTERACTIVE_MARKER_H_ */


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Aug 27 2015 15:02:27