trajopt
 All Classes Namespaces Files Functions Variables Typedefs Pages
osgviewer.hpp
1 #pragma once
2 #include <openrave/openrave.h>
3 #include <osg/Geometry>
4 #include <osgViewer/Viewer>
5 #include <boost/function.hpp>
6 #include <osgGA/TrackballManipulator>
7 #include "macros.h"
8 typedef OpenRAVE::RaveVector<float> RaveVectorf;
9 
13 class TRAJOPT_API OSGViewer : public OpenRAVE::ViewerBase {
14 public:
15 
16  static boost::shared_ptr<OSGViewer> GetOrCreate(OpenRAVE::EnvironmentBasePtr env);
17  OSGViewer(OpenRAVE::EnvironmentBasePtr env);
18  ~OSGViewer();
19  virtual int main(bool bShow = true);
20  virtual void quitmainloop() {}
21  void RemoveKinBody(OpenRAVE::KinBodyPtr pbody);
22  void Draw();
23  void Idle(); // should be called ToggleIdle
24  void UpdateSceneData();
25  const std::string& GetName() const {return m_name;}
26  void SetBkgndColor(const RaveVectorf &) {printf("warning: SetBkgndColor not implemented\n");}
27 
28 
29  // return false if you want to disable the default TrackballManipulator handling
30  typedef boost::function<bool(const osgGA::GUIEventAdapter &)> MouseCallback;
31  void AddMouseCallback(const MouseCallback& cb);
32  typedef boost::function<void(const osgGA::GUIEventAdapter &)> KeyCallback;
33  void AddKeyCallback(int key, const KeyCallback& cb, const std::string& help="");
34  void PrintHelp();
35 
36  OpenRAVE::GraphHandlePtr drawarrow(const RaveVectorf& p1, const RaveVectorf& p2, float fwidth, const RaveVectorf& color);
37  OpenRAVE::GraphHandlePtr drawtrimesh (const float *ppoints, int stride, const int *pIndices, int numTriangles, const RaveVectorf &color);
38 // OpenRAVE::GraphHandlePtr drawtrimesh (const float *ppoints, int stride, const int *pIndices, int numTriangles, const boost::multi_array< float, 2 > &colors);
39  OpenRAVE::GraphHandlePtr plot3 (const float *ppoints, int numPoints, int stride, float fPointSize, const OpenRAVE::RaveVector< float > &color, int drawstyle=0);
40  OpenRAVE::GraphHandlePtr plot3(const float* ppoints, int numPoints, int stride, float pointsize, const float* colors, int drawstyle=0, bool bhasalpha=false);
41  OpenRAVE::GraphHandlePtr drawlinelist(const float *ppoints, int numPoints, int stride, float fwidth, const RaveVectorf &color);
42  OpenRAVE::GraphHandlePtr drawlinestrip(const float *ppoints, int numPoints, int stride, float fwidth, const RaveVectorf &color);
43  OpenRAVE::GraphHandlePtr _drawlines(osg::PrimitiveSet::Mode mode, const float *ppoints, int numPoints, int stride, float fwidth, const RaveVectorf &color);
44  OpenRAVE::GraphHandlePtr drawtext(const std::string& text, float x, float y, float fontsize, const OpenRAVE::Vector& color);
45 
46  void SetAllTransparency(float alpha);
47  void SetTransparency(OpenRAVE::KinBodyPtr, float alpha);
48 
49  OpenRAVE::GraphHandlePtr PlotAxes(const OpenRAVE::Transform& T, float size);
50  OpenRAVE::GraphHandlePtr PlotSphere(const OpenRAVE::Vector& x, float radius);
51 
53  OpenRAVE::GraphHandlePtr PlotKinBody(const OpenRAVE::KinBodyPtr);
54  OpenRAVE::GraphHandlePtr PlotLink(const OpenRAVE::KinBody::LinkPtr);
55 
56  struct EventHandler : public osgGA::TrackballManipulator {
57  typedef std::map<int, OSGViewer::KeyCallback> KeyCallbackMap;
58  std::vector<MouseCallback> event_cbs;
59  KeyCallbackMap key_cbs;
60  typedef std::map<int, std::string> Key2Desc;
61  Key2Desc descs; // callback descriptions
62  bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa);
63  };
64 
65 
66  osg::ref_ptr<EventHandler> m_handler;
67  osg::ref_ptr<osg::Group> m_root;
68  private:
69 
70 // osg::ref_ptr<osgViewer::Viewer> m_viewer;
71  osg::ref_ptr<osg::Camera> m_cam, m_hudcam;
72  osgViewer::Viewer m_viewer;
73  bool m_idling, m_request_stop_idling;
74  std::string m_name;
75 
76 
77 };
78 typedef boost::shared_ptr<OSGViewer> OSGViewerPtr;
79 
80 
81 void TRAJOPT_API SetColor(OpenRAVE::GraphHandlePtr handle, const osg::Vec4& color);
82 void TRAJOPT_API SetTransparency(OpenRAVE::GraphHandlePtr handle, float alpha);